[cfe-commits] r80123 - in /cfe/trunk: include/clang/Parse/Action.h lib/Parse/MinimalAction.cpp lib/Parse/ParseDecl.cpp lib/Parse/ParseDeclCXX.cpp lib/Sema/Sema.h lib/Sema/SemaDecl.cpp test/SemaTemplate/dependent-base-member-init.cpp

Douglas Gregor dgregor at apple.com
Wed Aug 26 11:27:52 PDT 2009


Author: dgregor
Date: Wed Aug 26 13:27:52 2009
New Revision: 80123

URL: http://llvm.org/viewvc/llvm-project?rev=80123&view=rev
Log:
When we know that we are parsing a class-name, implicitly construct a
TypenameType if getTypeName is looking at a member of an unknown
specialization. This allows us to properly parse class templates that
derived from type that could only otherwise be described by a typename type, 
e.g.,

  template<class T> struct X {};
  template<typename T> struct Y : public X<T>::X { }; 

Fixes PR4381.

Modified:
    cfe/trunk/include/clang/Parse/Action.h
    cfe/trunk/lib/Parse/MinimalAction.cpp
    cfe/trunk/lib/Parse/ParseDecl.cpp
    cfe/trunk/lib/Parse/ParseDeclCXX.cpp
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/test/SemaTemplate/dependent-base-member-init.cpp

Modified: cfe/trunk/include/clang/Parse/Action.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Parse/Action.h?rev=80123&r1=80122&r2=80123&view=diff

==============================================================================
--- cfe/trunk/include/clang/Parse/Action.h (original)
+++ cfe/trunk/include/clang/Parse/Action.h Wed Aug 26 13:27:52 2009
@@ -170,11 +170,27 @@
   
   /// getTypeName - Return non-null if the specified identifier is a type name
   /// in the current scope.
-  /// An optional CXXScopeSpec can be passed to indicate the C++ scope (class or
-  /// namespace) that the identifier must be a member of.
-  /// i.e. for "foo::bar", 'II' will be "bar" and 'SS' will be "foo::".
+  ///
+  /// \param II the identifier for which we are performing name lookup
+  ///
+  /// \param NameLoc the location of the identifier
+  ///
+  /// \param S the scope in which this name lookup occurs
+  ///
+  /// \param SS if non-NULL, the C++ scope specifier that precedes the 
+  /// identifier
+  ///
+  /// \param isClassName whether this is a C++ class-name production, in 
+  /// which we can end up referring to a member of an unknown specialization 
+  /// that we know (from the grammar) is supposed to be a type. For example,
+  /// this occurs when deriving from "std::vector<T>::allocator_type", where T 
+  /// is a template parameter.
+  ///
+  /// \returns the type referred to by this identifier, or NULL if the type
+  /// does not name an identifier.
   virtual TypeTy *getTypeName(IdentifierInfo &II, SourceLocation NameLoc,
-                              Scope *S, const CXXScopeSpec *SS = 0) = 0;
+                              Scope *S, const CXXScopeSpec *SS = 0,
+                              bool isClassName = false) = 0;
 
   /// isTagName() - This method is called *for error recovery purposes only*
   /// to determine if the specified name is a valid tag name ("struct foo").  If
@@ -2009,8 +2025,27 @@
 
   /// getTypeName - This looks at the IdentifierInfo::FETokenInfo field to
   /// determine whether the name is a typedef or not in this scope.
+  ///
+  /// \param II the identifier for which we are performing name lookup
+  ///
+  /// \param NameLoc the location of the identifier
+  ///
+  /// \param S the scope in which this name lookup occurs
+  ///
+  /// \param SS if non-NULL, the C++ scope specifier that precedes the 
+  /// identifier
+  ///
+  /// \param isClassName whether this is a C++ class-name production, in 
+  /// which we can end up referring to a member of an unknown specialization 
+  /// that we know (from the grammar) is supposed to be a type. For example,
+  /// this occurs when deriving from "std::vector<T>::allocator_type", where T 
+  /// is a template parameter.
+  ///
+  /// \returns the type referred to by this identifier, or NULL if the type
+  /// does not name an identifier.
   virtual TypeTy *getTypeName(IdentifierInfo &II, SourceLocation NameLoc,
-                              Scope *S, const CXXScopeSpec *SS);
+                              Scope *S, const CXXScopeSpec *SS,
+                              bool isClassName = false);
 
   /// isCurrentClassName - Always returns false, because MinimalAction
   /// does not support C++ classes with constructors.

Modified: cfe/trunk/lib/Parse/MinimalAction.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/MinimalAction.cpp?rev=80123&r1=80122&r2=80123&view=diff

==============================================================================
--- cfe/trunk/lib/Parse/MinimalAction.cpp (original)
+++ cfe/trunk/lib/Parse/MinimalAction.cpp Wed Aug 26 13:27:52 2009
@@ -143,7 +143,8 @@
 /// FIXME: Use the passed CXXScopeSpec for accurate C++ type checking.
 Action::TypeTy *
 MinimalAction::getTypeName(IdentifierInfo &II, SourceLocation Loc,
-                           Scope *S, const CXXScopeSpec *SS) {
+                           Scope *S, const CXXScopeSpec *SS,
+                           bool isClassName) {
   if (TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>())
     if (TI->isTypeName)
       return TI;

Modified: cfe/trunk/lib/Parse/ParseDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseDecl.cpp?rev=80123&r1=80122&r2=80123&view=diff

==============================================================================
--- cfe/trunk/lib/Parse/ParseDecl.cpp (original)
+++ cfe/trunk/lib/Parse/ParseDecl.cpp Wed Aug 26 13:27:52 2009
@@ -2248,7 +2248,7 @@
       } else if (Tok.is(tok::tilde)) {
         // This should be a C++ destructor.
         SourceLocation TildeLoc = ConsumeToken();
-        if (Tok.is(tok::identifier)) {
+        if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
           // FIXME: Inaccurate.
           SourceLocation NameLoc = Tok.getLocation();
           SourceLocation EndLoc;

Modified: cfe/trunk/lib/Parse/ParseDeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseDeclCXX.cpp?rev=80123&r1=80122&r2=80123&view=diff

==============================================================================
--- cfe/trunk/lib/Parse/ParseDeclCXX.cpp (original)
+++ cfe/trunk/lib/Parse/ParseDeclCXX.cpp Wed Aug 26 13:27:52 2009
@@ -457,7 +457,8 @@
 
   // We have an identifier; check whether it is actually a type.
   TypeTy *Type = Actions.getTypeName(*Tok.getIdentifierInfo(), 
-                                     Tok.getLocation(), CurScope, SS);
+                                     Tok.getLocation(), CurScope, SS,
+                                     true);
   if (!Type) {
     Diag(Tok, DestrExpected ? diag::err_destructor_class_name 
                             : diag::err_expected_class_name);

Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=80123&r1=80122&r2=80123&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Wed Aug 26 13:27:52 2009
@@ -501,7 +501,8 @@
   DeclGroupPtrTy ConvertDeclToDeclGroup(DeclPtrTy Ptr);
 
   virtual TypeTy *getTypeName(IdentifierInfo &II, SourceLocation NameLoc, 
-                              Scope *S, const CXXScopeSpec *SS);
+                              Scope *S, const CXXScopeSpec *SS,
+                              bool isClassName = false);
   virtual DeclSpec::TST isTagName(IdentifierInfo &II, Scope *S);
   
   virtual DeclPtrTy ActOnDeclarator(Scope *S, Declarator &D) {

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=80123&r1=80122&r2=80123&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Wed Aug 26 13:27:52 2009
@@ -60,7 +60,8 @@
 /// If name lookup results in an ambiguity, this routine will complain
 /// and then return NULL.
 Sema::TypeTy *Sema::getTypeName(IdentifierInfo &II, SourceLocation NameLoc,
-                                Scope *S, const CXXScopeSpec *SS) {
+                                Scope *S, const CXXScopeSpec *SS,
+                                bool isClassName) {
   // C++ [temp.res]p3:
   //   A qualified-id that refers to a type and in which the
   //   nested-name-specifier depends on a template-parameter (14.6.2)
@@ -70,8 +71,17 @@
   //
   // We therefore do not perform any name lookup if the result would
   // refer to a member of an unknown specialization.
-  if (SS && isUnknownSpecialization(*SS))
-    return 0;
+  if (SS && isUnknownSpecialization(*SS)) {
+    if (!isClassName)
+      return 0;
+    
+    // We know from the grammar that this name refers to a type, so build a 
+    // TypenameType node to describe the type.
+    // FIXME: Record somewhere that this TypenameType node has no "typename"
+    // keyword associated with it.
+    return CheckTypenameType((NestedNameSpecifier *)SS->getScopeRep(),
+                             II, SS->getRange()).getAsOpaquePtr();
+  }
   
   LookupResult Result 
     = LookupParsedName(S, SS, &II, LookupOrdinaryName, false, false);

Modified: cfe/trunk/test/SemaTemplate/dependent-base-member-init.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/dependent-base-member-init.cpp?rev=80123&r1=80122&r2=80123&view=diff

==============================================================================
--- cfe/trunk/test/SemaTemplate/dependent-base-member-init.cpp (original)
+++ cfe/trunk/test/SemaTemplate/dependent-base-member-init.cpp Wed Aug 26 13:27:52 2009
@@ -1,5 +1,9 @@
 // RUN: clang-cc -fsyntax-only -verify %s
 
+// PR4381
+template<class T> struct X {};
+template<typename T> struct Y : public X<T>::X { };
+
 // PR4621
 class A1 {
   A1(int x) {}





More information about the cfe-commits mailing list