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

Support definitionOrder with other declarations #1807

Open
deadlocklogic opened this issue Dec 14, 2023 · 3 comments
Open

Support definitionOrder with other declarations #1807

deadlocklogic opened this issue Dec 14, 2023 · 3 comments

Comments

@deadlocklogic
Copy link
Contributor

deadlocklogic commented Dec 14, 2023

Why DefinitionOrder is exclusive for Class and 0 for other declarations?

Declaration* Parser::WalkDeclarationDef(clang::Decl* D)
{
auto Decl = WalkDeclaration(D);
if (!Decl || Decl->definitionOrder > 0)
return Decl;
// We store a definition order index into the declarations.
// This is needed because declarations are added to their contexts as
// soon as they are referenced and we need to know the original order
// of the declarations.
clang::RecordDecl* RecordDecl;
if ((RecordDecl = llvm::dyn_cast<clang::RecordDecl>(D)) &&
RecordDecl->isCompleteDefinition())
Decl->definitionOrder = index++;
return Decl;
}

This is useful when we need to sort declarations based on definition order.
Because some scripting languages like pybind11: wants every base class registered before the child class, so if the base class is inside a namespace in the same TranslationUnit and the registration order is wrong, we get errors.
Currently, any declaration other than Class has its definition order set to 0.

Just a quick question: DefinitionOrder is the order of definition as parsed or as created in the AST?
Otherwise, we can use source location: line, column.

Edit:

There are only methods for line querying, none for columns: LineNumberStart/LineNumberEnd.

@tritao
Copy link
Collaborator

tritao commented Dec 18, 2023

Missed this earlier... Not really sure why we only do it for classes, maybe we can relax that a little bit.

Because some scripting languages like pybind11: wants every base class registered before the child class, so if the base class is inside a namespace in the same TranslationUnit and the registration order is wrong, we get errors.

Either way, I think relying on the original declaration order for this might not be the best approach. Seems like it would be better to run some kind of ordering pass in the generator regardless. Something simple that comes to mind is building a directed graph of the declarations, then running topo sort on it to get the proper ordering.

@deadlocklogic
Copy link
Contributor Author

deadlocklogic commented Dec 18, 2023

Actually I am thinking of adding to the Declaration class:

public int ColumnNumberStart { get; set; }
public int ColumnNumberEnd { get; set; }

At least we can order then the concrete declarations as parsed in the translation unit.

Declaration order is important in certain binding libraries, for example with pybind11 you can't bind a class before its base classes. Therefore you must respect the same order of definition in a translation unit, and also respect the order of inclusion, so another step would be adding a pass to sort translation units by order of inclusion.

@deadlocklogic
Copy link
Contributor Author

We still need 2 important concepts to implement:

  1. Equality visitor
  2. Clone visitor

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

No branches or pull requests

2 participants