[cfe-commits] r68166 - in /cfe/trunk: include/clang/AST/ASTContext.h include/clang/AST/NestedNameSpecifier.h include/clang/AST/TemplateName.h include/clang/AST/Type.h include/clang/Basic/DiagnosticParseKinds.td include/clang/Basic/IdentifierTable.h include/clang/Parse/Action.h lib/AST/ASTContext.cpp lib/AST/NestedNameSpecifier.cpp lib/AST/TemplateName.cpp lib/AST/Type.cpp lib/Parse/Parser.cpp lib/Sema/Sema.h lib/Sema/SemaTemplate.cpp lib/Sema/SemaTemplateInstantiate.cpp test/SemaTemplate/typename-specifier-2.cpp

Douglas Gregor dgregor at apple.com
Tue Mar 31 17:28:59 PDT 2009


Author: dgregor
Date: Tue Mar 31 19:28:59 2009
New Revision: 68166

URL: http://llvm.org/viewvc/llvm-project?rev=68166&view=rev
Log:
Parsing, semantic analysis, and template instantiation for typename
specifiers that terminate in a simple-template-id, e.g.,

  typename MetaFun::template apply<T1, T2>

Also, implement template instantiation for dependent
nested-name-specifiers that involve unresolved identifiers, e.g.,

  typename T::type::type




Added:
    cfe/trunk/test/SemaTemplate/typename-specifier-2.cpp
Modified:
    cfe/trunk/include/clang/AST/ASTContext.h
    cfe/trunk/include/clang/AST/NestedNameSpecifier.h
    cfe/trunk/include/clang/AST/TemplateName.h
    cfe/trunk/include/clang/AST/Type.h
    cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td
    cfe/trunk/include/clang/Basic/IdentifierTable.h
    cfe/trunk/include/clang/Parse/Action.h
    cfe/trunk/lib/AST/ASTContext.cpp
    cfe/trunk/lib/AST/NestedNameSpecifier.cpp
    cfe/trunk/lib/AST/TemplateName.cpp
    cfe/trunk/lib/AST/Type.cpp
    cfe/trunk/lib/Parse/Parser.cpp
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaTemplate.cpp
    cfe/trunk/lib/Sema/SemaTemplateInstantiate.cpp

Modified: cfe/trunk/include/clang/AST/ASTContext.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ASTContext.h?rev=68166&r1=68165&r2=68166&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/ASTContext.h (original)
+++ cfe/trunk/include/clang/AST/ASTContext.h Tue Mar 31 19:28:59 2009
@@ -302,6 +302,9 @@
   QualType getTypenameType(NestedNameSpecifier *NNS, 
                            const IdentifierInfo *Name,
                            QualType Canon = QualType());
+  QualType getTypenameType(NestedNameSpecifier *NNS, 
+                           const TemplateSpecializationType *TemplateId,
+                           QualType Canon = QualType());
 
   /// getObjCQualifiedInterfaceType - Return a 
   /// ObjCQualifiedInterfaceType type for the given interface decl and

Modified: cfe/trunk/include/clang/AST/NestedNameSpecifier.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/NestedNameSpecifier.h?rev=68166&r1=68165&r2=68166&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/NestedNameSpecifier.h (original)
+++ cfe/trunk/include/clang/AST/NestedNameSpecifier.h Tue Mar 31 19:28:59 2009
@@ -40,7 +40,11 @@
 class NestedNameSpecifier : public llvm::FoldingSetNode {
   /// \brief The nested name specifier that precedes this nested name
   /// specifier.
-  NestedNameSpecifier *Prefix;
+  ///
+  /// The pointer is the nested-name-specifier that precedes this
+  /// one. The integer stores one of the first four values of type
+  /// SpecifierKind.
+  llvm::PointerIntPair<NestedNameSpecifier *, 2> Prefix;
 
   /// \brief The last component in the nested name specifier, which
   /// can be an identifier, a declaration, or a type.
@@ -48,9 +52,8 @@
   /// When the pointer is NULL, this specifier represents the global
   /// specifier '::'. Otherwise, the pointer is one of
   /// IdentifierInfo*, Namespace*, or Type*, depending on the kind of
-  /// specifier. The integer stores one ofthe first four values of
-  /// type SpecifierKind.
-  llvm::PointerIntPair<void*, 2> Specifier;
+  /// specifier as encoded within the prefix.
+  void* Specifier;
 
 public:
   /// \brief The kind of specifier that completes this nested name
@@ -71,7 +74,7 @@
 
 private:
   /// \brief Builds the global specifier.
-  NestedNameSpecifier() : Prefix(0), Specifier(0, 0) { }
+  NestedNameSpecifier() : Prefix(0, 0), Specifier(0) { }
 
   /// \brief Copy constructor used internally to clone nested name
   /// specifiers.
@@ -84,7 +87,8 @@
 
   /// \brief Either find or insert the given nested name specifier
   /// mockup in the given context.
-  static NestedNameSpecifier *FindOrInsert(ASTContext &Context, const NestedNameSpecifier &Mockup);
+  static NestedNameSpecifier *FindOrInsert(ASTContext &Context, 
+                                           const NestedNameSpecifier &Mockup);
 
 public:
   /// \brief Builds a specifier combining a prefix and an identifier.
@@ -117,20 +121,20 @@
   /// nested name specifier that represents "foo::bar::", the current
   /// specifier will contain "bar::" and the prefix will contain
   /// "foo::".
-  NestedNameSpecifier *getPrefix() const { return Prefix; }
+  NestedNameSpecifier *getPrefix() const { return Prefix.getPointer(); }
 
   /// \brief Determine what kind of nested name specifier is stored.
   SpecifierKind getKind() const { 
-    if (Specifier.getPointer() == 0)
+    if (Specifier == 0)
       return Global;
-    return (SpecifierKind)Specifier.getInt(); 
+    return (SpecifierKind)Prefix.getInt(); 
   }
 
   /// \brief Retrieve the identifier stored in this nested name
   /// specifier.
   IdentifierInfo *getAsIdentifier() const {
-    if (Specifier.getInt() == Identifier)
-      return (IdentifierInfo *)Specifier.getPointer();
+    if (Prefix.getInt() == Identifier)
+      return (IdentifierInfo *)Specifier;
 
     return 0;
   }
@@ -138,17 +142,17 @@
   /// \brief Retrieve the namespace stored in this nested name
   /// specifier.
   NamespaceDecl *getAsNamespace() const {
-    if (Specifier.getInt() == Namespace)
-      return (NamespaceDecl *)Specifier.getPointer();
+    if (Prefix.getInt() == Namespace)
+      return (NamespaceDecl *)Specifier;
 
     return 0;
   }
 
   /// \brief Retrieve the type stored in this nested name specifier.
   Type *getAsType() const {
-    if (Specifier.getInt() == TypeSpec || 
-        Specifier.getInt() == TypeSpecWithTemplate)
-      return (Type *)Specifier.getPointer();
+    if (Prefix.getInt() == TypeSpec || 
+        Prefix.getInt() == TypeSpecWithTemplate)
+      return (Type *)Specifier;
 
     return 0;
   }
@@ -162,9 +166,8 @@
   void print(llvm::raw_ostream &OS) const;
 
   void Profile(llvm::FoldingSetNodeID &ID) const {
-    ID.AddPointer(Prefix);
-    ID.AddPointer(Specifier.getPointer());
-    ID.AddInteger(Specifier.getInt());
+    ID.AddPointer(Prefix.getOpaqueValue());
+    ID.AddPointer(Specifier);
   }
 
   void Destroy(ASTContext &Context);

Modified: cfe/trunk/include/clang/AST/TemplateName.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/TemplateName.h?rev=68166&r1=68165&r2=68166&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/TemplateName.h (original)
+++ cfe/trunk/include/clang/AST/TemplateName.h Tue Mar 31 19:28:59 2009
@@ -97,7 +97,14 @@
   bool isDependent() const;
 
   /// \brief Print the template name.
-  void print(llvm::raw_ostream &OS) const;
+  ///
+  /// \param OS the output stream to which the template name will be
+  /// printed.
+  ///
+  /// \param SuppressNNS if true, don't print the
+  /// nested-name-specifier that precedes the template name (if it has
+  /// one).
+  void print(llvm::raw_ostream &OS, bool SuppressNNS = false) const;
 
   /// \brief Debugging aid that dumps the template name to standard
   /// error.

Modified: cfe/trunk/include/clang/AST/Type.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Type.h?rev=68166&r1=68165&r2=68166&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Type.h (original)
+++ cfe/trunk/include/clang/AST/Type.h Tue Mar 31 19:28:59 2009
@@ -15,12 +15,14 @@
 #define LLVM_CLANG_AST_TYPE_H
 
 #include "clang/Basic/Diagnostic.h"
+#include "clang/Basic/IdentifierTable.h"
 #include "clang/AST/NestedNameSpecifier.h"
 #include "clang/AST/TemplateName.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/ADT/APSInt.h"
 #include "llvm/ADT/FoldingSet.h"
 #include "llvm/ADT/PointerIntPair.h"
+#include "llvm/ADT/PointerUnion.h"
 #include "llvm/Bitcode/SerializationFwd.h"
 
 using llvm::isa;
@@ -1649,9 +1651,11 @@
   /// \brief The nested name specifier containing the qualifier.
   NestedNameSpecifier *NNS;
 
+  typedef llvm::PointerUnion<const IdentifierInfo *, 
+                             const TemplateSpecializationType *> NameType;
+
   /// \brief The type that this typename specifier refers to.
-  /// FIXME: Also need to represent the "template simple-template-id" case.
-  const IdentifierInfo *Name;
+  NameType Name;
 
   TypenameType(NestedNameSpecifier *NNS, const IdentifierInfo *Name,
                QualType CanonType)
@@ -1660,14 +1664,34 @@
            "TypenameType requires a dependent nested-name-specifier");
   }
 
+  TypenameType(NestedNameSpecifier *NNS, const TemplateSpecializationType *Ty,
+               QualType CanonType)
+    : Type(Typename, CanonType, true), NNS(NNS), Name(Ty) { 
+    assert(NNS->isDependent() && 
+           "TypenameType requires a dependent nested-name-specifier");
+  }
+
   friend class ASTContext;  // ASTContext creates these
 
 public:
   /// \brief Retrieve the qualification on this type.
   NestedNameSpecifier *getQualifier() const { return NNS; }
 
-  /// \brief Retrieve the type named by the typename specifier.
-  const IdentifierInfo *getName() const { return Name; }
+  /// \brief Retrieve the type named by the typename specifier as an
+  /// identifier.
+  ///
+  /// This routine will return a non-NULL identifier pointer when the
+  /// form of the original typename was terminated by an identifier,
+  /// e.g., "typename T::type".
+  const IdentifierInfo *getIdentifier() const { 
+    return Name.dyn_cast<const IdentifierInfo *>(); 
+  }
+
+  /// \brief Retrieve the type named by the typename specifier as a
+  /// type specialization.
+  const TemplateSpecializationType *getTemplateId() const {
+    return Name.dyn_cast<const TemplateSpecializationType *>();
+  }
 
   virtual void getAsStringInternal(std::string &InnerString) const;
 
@@ -1676,9 +1700,9 @@
   }
 
   static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
-                      const IdentifierInfo *Name) {
+                      NameType Name) {
     ID.AddPointer(NNS);
-    ID.AddPointer(Name);
+    ID.AddPointer(Name.getOpaqueValue());
   }
 
   static bool classof(const Type *T) { 

Modified: cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td?rev=68166&r1=68165&r2=68166&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td Tue Mar 31 19:28:59 2009
@@ -226,6 +226,8 @@
   "template|<unused>|refers to a template template parameter}1">;
 def err_id_after_template_in_nested_name_spec : Error<
   "expected template name after 'template' keyword in nested name specifier">;
+def err_id_after_template_in_typename_spec : Error<
+  "expected template name after 'template' keyword in typename specifier">;
 def err_less_after_template_name_in_nested_name_spec : Error<
   "expected '<' after 'template %0' in nested name specifier">;
 def err_two_right_angle_brackets_need_space : Error<
@@ -236,6 +238,10 @@
 
 def err_expected_qualified_after_typename : Error<
   "expected a qualified name after 'typename'">;
+def err_typename_refers_to_non_type_template : Error<
+  "typename specifier refers to a non-template">;
+def err_expected_type_name_after_typename : Error<
+  "expected an identifier or template-id after '::'">;
 
 // Language specific pragmas
 // - Generic warnings

Modified: cfe/trunk/include/clang/Basic/IdentifierTable.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/IdentifierTable.h?rev=68166&r1=68165&r2=68166&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/IdentifierTable.h (original)
+++ cfe/trunk/include/clang/Basic/IdentifierTable.h Tue Mar 31 19:28:59 2009
@@ -21,6 +21,7 @@
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/OwningPtr.h"
 #include "llvm/Bitcode/SerializationFwd.h"
+#include "llvm/Support/PointerLikeTypeTraits.h"
 #include <string> 
 #include <cassert> 
 
@@ -512,5 +513,31 @@
   static bool isPod() { return true; }
 };
 
+// Provide PointerLikeTypeTraits for IdentifierInfo pointers, which
+// are not guaranteed to be 8-byte aligned.
+template<>
+class PointerLikeTypeTraits<clang::IdentifierInfo*> {
+public:
+  static inline void *getAsVoidPointer(clang::IdentifierInfo* P) {
+    return P; 
+  }
+  static inline clang::IdentifierInfo *getFromVoidPointer(void *P) {
+    return static_cast<clang::IdentifierInfo*>(P);
+  }
+  enum { NumLowBitsAvailable = 1 };
+};
+
+template<>
+class PointerLikeTypeTraits<const clang::IdentifierInfo*> {
+public:
+  static inline const void *getAsVoidPointer(const clang::IdentifierInfo* P) {
+    return P; 
+  }
+  static inline const clang::IdentifierInfo *getFromVoidPointer(const void *P) {
+    return static_cast<const clang::IdentifierInfo*>(P);
+  }
+  enum { NumLowBitsAvailable = 1 };
+};
+
 }  // end namespace llvm
 #endif

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

==============================================================================
--- cfe/trunk/include/clang/Parse/Action.h (original)
+++ cfe/trunk/include/clang/Parse/Action.h Tue Mar 31 19:28:59 2009
@@ -1305,7 +1305,7 @@
   }
 
   /// \brief Called when the parser has parsed a C++ typename
-  /// specifier, e.g., "typename T::type".
+  /// specifier that ends in an identifier, e.g., "typename T::type".
   ///
   /// \param TypenameLoc the location of the 'typename' keyword
   /// \param SS the nested-name-specifier following the typename (e.g., 'T::').
@@ -1317,6 +1317,20 @@
     return TypeResult();
   }
 
+  /// \brief Called when the parser has parsed a C++ typename
+  /// specifier that ends in a template-id, e.g., 
+  /// "typename MetaFun::template apply<T1, T2>".
+  ///
+  /// \param TypenameLoc the location of the 'typename' keyword
+  /// \param SS the nested-name-specifier following the typename (e.g., 'T::').
+  /// \param TemplateLoc the location of the 'template' keyword, if any.
+  /// \param Ty the type that the typename specifier refers to.
+  virtual TypeResult
+  ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS,
+                    SourceLocation TemplateLoc, TypeTy *Ty) {
+    return TypeResult();
+  }
+
   //===----------------------- Obj-C Declarations -------------------------===//
   
   // ActOnStartClassInterface - this action is called immediately after parsing

Modified: cfe/trunk/lib/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTContext.cpp?rev=68166&r1=68165&r2=68166&view=diff

==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Tue Mar 31 19:28:59 2009
@@ -1454,6 +1454,39 @@
   return QualType(T, 0);  
 }
 
+QualType 
+ASTContext::getTypenameType(NestedNameSpecifier *NNS, 
+                            const TemplateSpecializationType *TemplateId,
+                            QualType Canon) {
+  assert(NNS->isDependent() && "nested-name-specifier must be dependent");
+
+  if (Canon.isNull()) {
+    NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
+    QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
+    if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
+      const TemplateSpecializationType *CanonTemplateId
+        = CanonType->getAsTemplateSpecializationType();
+      assert(CanonTemplateId &&
+             "Canonical type must also be a template specialization type");
+      Canon = getTypenameType(CanonNNS, CanonTemplateId);
+    }
+  }
+
+  llvm::FoldingSetNodeID ID;
+  TypenameType::Profile(ID, NNS, TemplateId);
+
+  void *InsertPos = 0;
+  TypenameType *T 
+    = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
+  if (T)
+    return QualType(T, 0);
+
+  T = new (*this) TypenameType(NNS, TemplateId, Canon);
+  Types.push_back(T);
+  TypenameTypes.InsertNode(T, InsertPos);
+  return QualType(T, 0);    
+}
+
 /// CmpProtocolNames - Comparison predicate for sorting protocols
 /// alphabetically.
 static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,

Modified: cfe/trunk/lib/AST/NestedNameSpecifier.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/NestedNameSpecifier.cpp?rev=68166&r1=68165&r2=68166&view=diff

==============================================================================
--- cfe/trunk/lib/AST/NestedNameSpecifier.cpp (original)
+++ cfe/trunk/lib/AST/NestedNameSpecifier.cpp Tue Mar 31 19:28:59 2009
@@ -30,7 +30,7 @@
   NestedNameSpecifier *NNS 
     = Context.NestedNameSpecifiers.FindNodeOrInsertPos(ID, InsertPos);
   if (!NNS) {
-    NNS = new (Context) NestedNameSpecifier(Mockup);
+    NNS = new (Context, 4) NestedNameSpecifier(Mockup);
     Context.NestedNameSpecifiers.InsertNode(NNS, InsertPos);
   }
 
@@ -44,9 +44,9 @@
   assert(Prefix && Prefix->isDependent() && "Prefix must be dependent");
 
   NestedNameSpecifier Mockup;
-  Mockup.Prefix = Prefix;
-  Mockup.Specifier.setPointer(II);
-  Mockup.Specifier.setInt(Identifier);
+  Mockup.Prefix.setPointer(Prefix);
+  Mockup.Prefix.setInt(Identifier);
+  Mockup.Specifier = II;
   return FindOrInsert(Context, Mockup);
 }
 
@@ -58,9 +58,9 @@
           (Prefix->getAsType() == 0 && Prefix->getAsIdentifier() == 0)) &&
          "Broken nested name specifier");
   NestedNameSpecifier Mockup;
-  Mockup.Prefix = Prefix;
-  Mockup.Specifier.setPointer(NS);
-  Mockup.Specifier.setInt(Namespace);
+  Mockup.Prefix.setPointer(Prefix);
+  Mockup.Prefix.setInt(Namespace);
+  Mockup.Specifier = NS;
   return FindOrInsert(Context, Mockup);
 }
 
@@ -69,15 +69,15 @@
                             bool Template, Type *T) {
   assert(T && "Type cannot be NULL");
   NestedNameSpecifier Mockup;
-  Mockup.Prefix = Prefix;
-  Mockup.Specifier.setPointer(T);
-  Mockup.Specifier.setInt(Template? TypeSpecWithTemplate : TypeSpec);
+  Mockup.Prefix.setPointer(Prefix);
+  Mockup.Prefix.setInt(Template? TypeSpecWithTemplate : TypeSpec);
+  Mockup.Specifier = T;
   return FindOrInsert(Context, Mockup);
 }
   
 NestedNameSpecifier *NestedNameSpecifier::GlobalSpecifier(ASTContext &Context) {
   if (!Context.GlobalNestedNameSpecifier)
-    Context.GlobalNestedNameSpecifier = new (Context) NestedNameSpecifier();
+    Context.GlobalNestedNameSpecifier = new (Context, 4) NestedNameSpecifier();
   return Context.GlobalNestedNameSpecifier;
 }
 
@@ -105,8 +105,8 @@
 /// \brief Print this nested name specifier to the given output
 /// stream.
 void NestedNameSpecifier::print(llvm::raw_ostream &OS) const {
-  if (Prefix)
-    Prefix->print(OS);
+  if (getPrefix())
+    getPrefix()->print(OS);
 
   switch (getKind()) {
   case Identifier:

Modified: cfe/trunk/lib/AST/TemplateName.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/TemplateName.cpp?rev=68166&r1=68165&r2=68166&view=diff

==============================================================================
--- cfe/trunk/lib/AST/TemplateName.cpp (original)
+++ cfe/trunk/lib/AST/TemplateName.cpp Tue Mar 31 19:28:59 2009
@@ -38,16 +38,18 @@
   return true;
 }
 
-void TemplateName::print(llvm::raw_ostream &OS) const {
+void TemplateName::print(llvm::raw_ostream &OS, bool SuppressNNS) const {
   if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>())
     OS << Template->getIdentifier()->getName();
   else if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName()) {
-    QTN->getQualifier()->print(OS);
+    if (!SuppressNNS)
+      QTN->getQualifier()->print(OS);
     if (QTN->hasTemplateKeyword())
       OS << "template ";
     OS << QTN->getTemplateDecl()->getIdentifier()->getName();
   } else if (DependentTemplateName *DTN = getAsDependentTemplateName()) {
-    DTN->getQualifier()->print(OS);
+    if (!SuppressNNS)
+      DTN->getQualifier()->print(OS);
     OS << "template ";
     OS << DTN->getName()->getName();
   }

Modified: cfe/trunk/lib/AST/Type.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Type.cpp?rev=68166&r1=68165&r2=68166&view=diff

==============================================================================
--- cfe/trunk/lib/AST/Type.cpp (original)
+++ cfe/trunk/lib/AST/Type.cpp Tue Mar 31 19:28:59 2009
@@ -1450,7 +1450,14 @@
     llvm::raw_string_ostream OS(MyString);
     OS << "typename ";
     NNS->print(OS);
-    OS << Name->getName();
+
+    if (const IdentifierInfo *Ident = getIdentifier())
+      OS << Ident->getName();
+    else if (const TemplateSpecializationType *Spec = getTemplateId()) {
+      Spec->getTemplateName().print(OS, true);
+      OS << TemplateSpecializationType::PrintTemplateArgumentList(
+                                         Spec->getArgs(), Spec->getNumArgs());
+    }
   }
   
   if (InnerString.empty())

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

==============================================================================
--- cfe/trunk/lib/Parse/Parser.cpp (original)
+++ cfe/trunk/lib/Parse/Parser.cpp Tue Mar 31 19:28:59 2009
@@ -828,7 +828,7 @@
     //   typename-specifier:
     //     'typename' '::' [opt] nested-name-specifier identifier
     //     'typename' '::' [opt] nested-name-specifier template [opt] 
-    //            simple-template-id  [TODO]
+    //            simple-template-id
     SourceLocation TypenameLoc = ConsumeToken();
     CXXScopeSpec SS;
     bool HadNestedNameSpecifier = ParseOptionalCXXScopeSpecifier(SS);
@@ -842,16 +842,35 @@
       // FIXME: check whether the next token is '<', first!
       Ty = Actions.ActOnTypenameType(TypenameLoc, SS, *Tok.getIdentifierInfo(), 
                                      Tok.getLocation());
-      // FIXME: better error recovery!
-      Tok.setKind(tok::annot_typename);
-      Tok.setAnnotationValue(Ty.get());
-      Tok.setAnnotationEndLoc(Tok.getLocation());
-      Tok.setLocation(TypenameLoc);
-      PP.AnnotateCachedTokens(Tok);
-      return true;
-    } 
+    } else if (Tok.is(tok::annot_template_id)) {
+      TemplateIdAnnotation *TemplateId 
+        = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
+      if (TemplateId->Kind == TNK_Function_template) {
+        Diag(Tok, diag::err_typename_refers_to_non_type_template)
+          << Tok.getAnnotationRange();
+        return false;
+      }
+
+      if (AnnotateTemplateIdTokenAsType(0))
+        return false;
+
+      assert(Tok.is(tok::annot_typename) && 
+             "AnnotateTemplateIdTokenAsType isn't working properly");
+      Ty = Actions.ActOnTypenameType(TypenameLoc, SS, SourceLocation(),
+                                     Tok.getAnnotationValue());
+    } else {
+      Diag(Tok, diag::err_expected_type_name_after_typename)
+        << SS.getRange();
+      return false;
+    }
 
-    return false;
+    // FIXME: better error recovery!
+    Tok.setKind(tok::annot_typename);
+    Tok.setAnnotationValue(Ty.get());
+    Tok.setAnnotationEndLoc(Tok.getLocation());
+    Tok.setLocation(TypenameLoc);
+    PP.AnnotateCachedTokens(Tok);
+    return true;
   }
 
   CXXScopeSpec SS;

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

==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Tue Mar 31 19:28:59 2009
@@ -1816,6 +1816,19 @@
   virtual TypeResult
   ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS,
                     const IdentifierInfo &II, SourceLocation IdLoc);
+
+  /// \brief Called when the parser has parsed a C++ typename
+  /// specifier that ends in a template-id, e.g., 
+  /// "typename MetaFun::template apply<T1, T2>".
+  ///
+  /// \param TypenameLoc the location of the 'typename' keyword
+  /// \param SS the nested-name-specifier following the typename (e.g., 'T::').
+  /// \param TemplateLoc the location of the 'template' keyword, if any.
+  /// \param Ty the type that the typename specifier refers to.
+  virtual TypeResult
+  ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS,
+                    SourceLocation TemplateLoc, TypeTy *Ty);
+
   QualType CheckTypenameType(NestedNameSpecifier *NNS,
                              const IdentifierInfo &II,
                              SourceRange Range);

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplate.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplate.cpp Tue Mar 31 19:28:59 2009
@@ -2105,6 +2105,22 @@
   return T.getAsOpaquePtr();
 }
 
+Sema::TypeResult
+Sema::ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS,
+                        SourceLocation TemplateLoc, TypeTy *Ty) {
+  QualType T = QualType::getFromOpaquePtr(Ty);
+  NestedNameSpecifier *NNS 
+    = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
+  const TemplateSpecializationType *TemplateId 
+    = T->getAsTemplateSpecializationType();
+  assert(TemplateId && "Expected a template specialization type");
+
+  if (NNS->isDependent())
+    return Context.getTypenameType(NNS, TemplateId).getAsOpaquePtr();
+
+  return Context.getQualifiedNameType(NNS, T).getAsOpaquePtr();
+}
+
 /// \brief Build the type that describes a C++ typename specifier,
 /// e.g., "typename T::type".
 QualType

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplateInstantiate.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplateInstantiate.cpp Tue Mar 31 19:28:59 2009
@@ -497,6 +497,15 @@
 QualType 
 TemplateTypeInstantiator::
 InstantiateTypenameType(const TypenameType *T, unsigned Quals) const {
+  if (const TemplateSpecializationType *TemplateId = T->getTemplateId()) {
+    // When the typename type refers to a template-id, the template-id
+    // is dependent and has enough information to instantiate the
+    // result of the typename type. Since we don't care about keeping
+    // the spelling of the typename type in template instantiations,
+    // we just instantiate the template-id.
+    return InstantiateTemplateSpecializationType(TemplateId, Quals);
+  }
+
   NestedNameSpecifier *NNS 
     = SemaRef.InstantiateNestedNameSpecifier(T->getQualifier(), 
                                              SourceRange(Loc),
@@ -504,7 +513,7 @@
   if (!NNS)
     return QualType();
 
-  return SemaRef.CheckTypenameType(NNS, *T->getName(), SourceRange(Loc));
+  return SemaRef.CheckTypenameType(NNS, *T->getIdentifier(), SourceRange(Loc));
 }
 
 QualType 
@@ -801,10 +810,20 @@
   }
 
   switch (NNS->getKind()) {
-  case NestedNameSpecifier::Identifier:
-    // FIXME: Implement this lookup!
-    assert(false && "Cannot instantiate this nested-name-specifier");
+  case NestedNameSpecifier::Identifier: {
+    assert(Prefix && 
+           "Can't have an identifier nested-name-specifier with no prefix");
+    CXXScopeSpec SS;
+    // FIXME: The source location information is all wrong.
+    SS.setRange(Range);
+    SS.setScopeRep(Prefix);
+    return static_cast<NestedNameSpecifier *>(
+                                 ActOnCXXNestedNameSpecifier(0, SS,
+                                                             Range.getEnd(),
+                                                             Range.getEnd(),
+                                                   *NNS->getAsIdentifier()));
     break;
+  }
 
   case NestedNameSpecifier::Namespace:
   case NestedNameSpecifier::Global:
@@ -816,9 +835,6 @@
     if (!T->isDependentType())
       return NNS;
 
-    // FIXME: We won't be able to perform the instantiation here when
-    // the template-name is dependent, e.g., we have something like
-    // "T::template apply<U>::type".
     T = InstantiateType(T, TemplateArgs, NumTemplateArgs, Range.getBegin(),
                         DeclarationName());
     if (T.isNull())
@@ -826,9 +842,7 @@
 
     if (T->isRecordType() ||
         (getLangOptions().CPlusPlus0x && T->isEnumeralType())) {
-      // Note that T.getTypePtr(), below, strips cv-qualifiers. This is
-      // perfectly reasonable, since cv-qualified types in
-      // nested-name-specifiers don't matter.
+      assert(T.getCVRQualifiers() == 0 && "Can't get cv-qualifiers here");
       return NestedNameSpecifier::Create(Context, Prefix, 
                  NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
                                          T.getTypePtr());

Added: cfe/trunk/test/SemaTemplate/typename-specifier-2.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/typename-specifier-2.cpp?rev=68166&view=auto

==============================================================================
--- cfe/trunk/test/SemaTemplate/typename-specifier-2.cpp (added)
+++ cfe/trunk/test/SemaTemplate/typename-specifier-2.cpp Tue Mar 31 19:28:59 2009
@@ -0,0 +1,30 @@
+// RUN: clang-cc -fsyntax-only -verify %s
+
+template<typename MetaFun, typename T>
+struct bind_metafun {
+  typedef typename MetaFun::template apply<T> type;
+};
+
+struct add_pointer {
+  template<typename T>
+  struct apply {
+    typedef T* type;
+  };
+};
+
+int i;
+// FIXME: if we make the declarator below a pointer (e.g., with *ip),
+// the error message isn't so good because we don't get the handy
+// 'aka' telling us that we're dealing with an int**. Should we fix
+// getDesugaredType to dig through pointers and such?
+bind_metafun<add_pointer, int>::type::type ip = &i;
+bind_metafun<add_pointer, float>::type::type fp = &i; // expected-error{{incompatible type initializing 'int *', expected 'bind_metafun<struct add_pointer, float>::type::type' (aka 'float *')}}
+
+
+template<typename T>
+struct extract_type_type {
+  typedef typename T::type::type t;
+};
+
+double d;
+extract_type_type<bind_metafun<add_pointer, double> >::t dp = &d;





More information about the cfe-commits mailing list