Transferring typers between ASTContexts

Hi @clang,

is there an easy way to reuse types (given by Type*) from a particular ASTContext in a different ASTContext?
What I have here are multiple configuration files written in C++. I don't want to preserve the complete ASTContext for each file during the entire runtime of the program. I just need a way to represent types in a manner so that I can rebuild the actual Type pointer later in a different ASTContext (I have only simple types here not dependent on any expressions and yes, I know, that this constraint simplifies my problem quite a lot). Holding types on my own doesn't work due to the private constructors of a lot of types (which I don't like BTW).
So one way could be to create and use a separate (dummy) ASTContext. Or I utilize the PCH stream but I'm completely unsure how to proceed then and if this is a feasible solution at all.

Best regards
Olaf Krzikalla

Hi @clang,

is there an easy way to reuse types (given by Type*) from a particular
ASTContext in a different ASTContext?

Not directly. The uniquing that forms the underpinning of the canonical-types system means that a type from one ASTContext can't be used in another ASTContext.

What I have here are multiple configuration files written in C++. I
don't want to preserve the complete ASTContext for each file during the
entire runtime of the program. I just need a way to represent types in a
manner so that I can rebuild the actual Type pointer later in a
different ASTContext (I have only simple types here not dependent on any
expressions and yes, I know, that this constraint simplifies my problem
quite a lot). Holding types on my own doesn't work due to the private
constructors of a lot of types (which I don't like BTW).
So one way could be to create and use a separate (dummy) ASTContext. Or
I utilize the PCH stream but I'm completely unsure how to proceed then
and if this is a feasible solution at all.

Check out the ASTImporter in include/clang/AST/ASTImporter. It's far from complete, but the idea is that it allows you to import specific types/declarations/etc. from one ASTContext into another, merging the imported declarations with those declarations already in the destination context (and trying to detect ODR violations along the way).

  - Doug