Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should classes explicitly declare whether they have a vptr? #4488

Open
jonmeow opened this issue Nov 5, 2024 · 2 comments
Open

Should classes explicitly declare whether they have a vptr? #4488

jonmeow opened this issue Nov 5, 2024 · 2 comments
Labels
leads question A question for the leads team

Comments

@jonmeow
Copy link
Contributor

jonmeow commented Nov 5, 2024

Summary of issue:

Right now, whether a class's object representation includes a vptr is implicit from the presence of virtual functions. Should it be explicitly declared?

Details:

A base class may or may not have a virtual function. Similarly, an abstract class may not have abstract functions. As a consequence, we don't really know whether a vptr is present in a type's object representation until the class is complete.

Regarding syntax, we would probably want things to indicate both base and abstract classes with-and-without vptrs. When inheriting, extend base is probably sufficient to infer from the parent. So this is probably mostly a question about whether the incremental overhead of a vptr should be explicit for developer control, or implicit for consistency with C++.

Any other information that you want to share?

In discussion of alternatives, the main advantage to having non-virtual inheritance over planned alternatives (such as mixins) is the ability to down-cast.

Note while Carbon supports an abstract class with no abstract functions, that's a feature on top of C++.

In the toolchain, we're planning on handling the current design by lazily calculating element indices after the class is complete. This may have some edge-case behaviors still, because it's possible to have references to members (e.g. in C++, auto F() -> decltype(my_member_)) but the index shouldn't matter in those cases. So either way seems to be manageable in the toolchain.

@jonmeow jonmeow added the leads question A question for the leads team label Nov 5, 2024
@zygoloid
Copy link
Contributor

zygoloid commented Nov 5, 2024

Explicit vptr syntax would give us (at least) one additional feature beyond those in C++: a class could have a vptr with no virtual functions (similar to how we allow a class to be abstract with no abstract functions). This is occasionally desired in C++ to allow dynamic_cast without other polymorphism.

@chandlerc
Copy link
Contributor

Would it also help with homing of the vtable? I guess that's probably fixed by us always emitting a .o file for the API file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
leads question A question for the leads team
Projects
None yet
Development

No branches or pull requests

3 participants