[cfe-dev] [PATCH] Ordinary name lookup for class and enum names in C++

Chris Lattner clattner at apple.com
Sat Apr 12 17:06:17 PDT 2008


On Apr 12, 2008, at 12:54 PM, Doug Gregor wrote:

> The patch introduces support for ordinary name lookup of class and
> enum names in C++. For example:
>
>  class C { };
>  C c;
>
> Incidentally, it also introduces support for declaring classes with
> "class" in C++, but that's rather trite.

Great!

> I briefly considered eliminating the notion of the implicit typedef,
> and instead modifying name lookup to look into both places in C++...
> but this approach is cleaner.

Lets talk about this a bit.  I always assumed that we would modify the  
name lookup machinery to handle this instead of adding a typedef...  
however I admit that I haven't fully thought through the implications  
and so I may not see all the problems with this approach.

It seems that it would be possible to handle this by making the scope  
lookup take a bitmask of different namespaces to search.  When looking  
up 'class foo' only the tag namespace would be searched, but when  
looking up 'foo' in C++, both the tag and normal namespace would be  
searched.

I'm sure there are more details to it than this, but avoiding creation  
of a dummy typedef decl for every struct/class/union is a pretty big  
win.  What drawbacks do you see?

-Chris

>
>
> Interesting bits in this patch:
>  - Sema::ActOnTag: In C++, we introduce an implicit typedef into the
> same scope as the tag type. This allows the name of a class or enum to
> be found via ordinary name lookup. Note that we don't introduce this
> implicit typedef if there is already a declaration with that name,
> because the existing declaration takes precedence, e.g.,
>      int D(int);
>      class D { };
>      // ordinary name lookup of "D" finds the function. Use "class D"
> to get to the class.
>
>  - Sema::ActOnDeclarator: Introducing a declaration for something
> that has the same name as a class is okay; the new declaration hides
> the class name. For example:
>      class D { };
>      int D(int);
>      // ordinary name lookup of "D" finds the function. Use "class D"
> to get to the class.
>    To deal with this, we just go ahead and delete the implicit
> typedef when a new declaration comes in.
>
>
>  - Doug
> <clang-class- 
> names.patch>_______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev




More information about the cfe-dev mailing list