[cfe-dev] Patch for ContextDecl

Argiris Kirtzidis akyrtzi at gmail.com
Sun Apr 6 13:32:03 PDT 2008


Chris Lattner wrote:
> What is actually going on here is that getNext is horribly overloaded 
> for scope decls.  When parsing is happening, the "next" pointer is 
> used to represent the scope chains hanging off the identifier info.  
> This keeps track of shadowing.  However, after parsing is done, these 
> fields are dead, so they are "repurposed" to be the linked list of 
> decls within a context (e.g. functiondecl).
>
> A truly elegant solution would be to get rid of parsing-specific stuff 
> from the AST, but I'm not sure how to do this while retaining 
> efficiency at parse time for walking and maintaining the shadow stacks.
That's a good idea. A suggestion for this is the attached patch.

Basically, identifiers instead of being associated with just a 
ScopedDecl (the last one of the shadowed scope chain), they are 
associated with a IdDeclInfo class that is supposed to keep
track of information about decls associated with a specific identifier.
To keep track of the scope chain stack, it uses a SmallVector.
You add a decl at the scope chain like this:

IdDeclInfos[Id].PushShadowed(New);

and search for a decl like this (it's in Sema::LookupDecl):

  // if no IdDeclInfo is created for this id, it means no decl is associated
  // with this id.
  if (IdDeclInfo *IDI = IdDeclInfos.get(II)) {
    // Scan up the scope chain looking for a decl that matches this 
identifier
    // that is in the appropriate namespace.  This search should not 
take long, as
    // shadowing of names is uncommon, and deep shadowing is extremely 
uncommon.
    for (IdDeclInfo::ShadowedIter SI = IDI->shadowed_begin();
            SI != IDI->shadowed_end(); ++SI)
      if ((*SI)->getIdentifierNamespace() == NS)
        return *SI;
  }

Let me know what you think.

-Argiris
-------------- next part --------------
A non-text attachment was scrubbed...
Name: id-decl-map.patch
Type: text/x-diff
Size: 10277 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20080406/1b5e785c/attachment.patch>


More information about the cfe-dev mailing list