Replies: 1 comment 3 replies
-
Where is the vtable represented?I have two contrasting thoughts here:
I think I'm leaning towards a slightly hybrid approach: that SemIR contains something like a How is the vtable represented?Perhaps something like: a When is the vtable representation built?I think building the vtable as part of completing the class definition is probably reasonable. Prior to that point, we could track information in a side-table incrementally as we process the class, or we could do an additional pass through the class to find virtual functions and assign them to vtable slots; either approach seems feasible to me, though I think I'm inclined to prefer temporarily storing extra data to avoid rescanning the class. |
Beta Was this translation helpful? Give feedback.
-
After doing some relatively simple work on the vtable pointer in class layout (vptr member, vtable_ptr instruction).
But I'm reaching the point where a larger discussion about the internal representation might be suitable.
The initial question is: How/where is the vtable layout computed and that computation reused (it /could/ be done in lowering, walking the class's block to find virtual functions, etc - but that might be inefficient (walking the whole class potentially multiple times - to emit the vtable, and then looking that up/matching things up when making virtual calls))
Also, possibly computing some of this earlier (during checking, perhaps at the end of the class) might allow for better diagnostics/a good place to group a bunch of checking (eg: #4648 feels a bit awkward - perhaps we could implement the constraint that /all/ dynamic functions (virtual, impl, abstract) appear after the base decl, in which case we'd need to test that at the end of the class, or at least only once we discover the base decl - and having a list of the dynamic functions isolated from the rest of the class contents may be faster than walking the whole pending class block)
@chandlerc suggested this might be a question for @jonmeow and @zygoloid - but his thoughts were that maybe we have another side table (
SemIR::Context::dynamic_class_info()
?) that is associated with classes (a map keyed byClassId
? an extra field inClassId
that's populated for dynamic classes?). Would that be reasonable? What would we want in there, how should this be rendered to textual SemIR? Oh, right, @chandlerc was suggesting this might possibly be some constant instruction block that computes things like the vtable layout? (I wasn't sure how much the vtable would be explicitly rendered in SemIR, or only a lowering artifact - the impression I got from previous discussions with @zygoloid was that it might be more opaque/an artifact of lowering to some degree, though having a list of virtual functions, and maybe some association between impl and base virtual functions would be helpful)Open to suggestions, widening or narrowing of the scope we should be considering at this point, etc.
Beta Was this translation helpful? Give feedback.
All reactions