[cfe-dev] Cleaning up the representation of Decls in the AST

Ted Kremenek kremenek at apple.com
Fri Aug 22 17:17:58 PDT 2008

On Aug 22, 2008, at 3:52 PM, Argiris Kirtzidis wrote:

> Hi Ted,
> This looks like a great design!
> Just a few questions:
> 1) Who will own struct references/definitions, will there be  
> DeclGroup owners for them too ?
> For example:
> struct S1;
> struct S1;
> struct S1 { int x; };
> struct S2;
> struct S2 {int x; } foo;

Fixing RecordDecls is another issue that we've thought about.  Right  
now we combine multiple struct declarations into a single RecordDecl.   
This seems very broken.

The natural solution is to have a separate RecordDecl for struct  
declaration.  We can then chain them together, just like we do within  
FunctionDecls.  The chain would not imply ownership; just a way to  
walk between the RecordDecls.  Once this is done, we can naturally use  
DeclGroups with RecordDecls.  Having multiple RecordDecls also solves  
a whole bunch of other problems (such as the loss of source  

Once we have multiple RecordDecls, the RecordDecl that actually  
defines the struct can have a bit in it indicating that this is the  
case.  This allows us to resolve between forward declarations and  
actual struct implementations.

Once we have multiple RecordDecls, for your example each of these  
declarations would be wrapped in a DeclGroup.  A DeclGroup is  
basically just a smart pointer, and would be optimized to have the  
same overhead as a pointer for all of the cases except the last.

For the last case ("struct S2 {int x; } foo;"), DeclGroup would point  
to a block of memory that contained the RecordDecl for 'struct S2  
{ int x; }' and a VarDecl for 'foo'.

> who owns these and what will the pretty-print output be for the  
> above example ?

The pretty-printed output (once the pretty-printer is fixed) should be  
the same as your example.

> 2)
>> DeclGroup -> TypeDecl Decl+
>>           -> Decl
> Given a RecordDecl how can I find out its owner, will TypeDecl point  
> to its owning DeclGroup (or Type point to the DeclGroup owning the  
> TypeDecl) ?

We can find the owner by building a back-map, just like we do with  
Stmts (ala ParentMap).

Just to be clear, DeclGroups are a separate concept than DeclContexts.

More information about the cfe-dev mailing list