[cfe-commits] r77488 - in /cfe/trunk: include/clang/AST/ASTContext.h include/clang/AST/TemplateName.h lib/AST/ASTContext.cpp lib/AST/TemplateName.cpp lib/Sema/SemaExpr.cpp lib/Sema/SemaLookup.cpp lib/Sema/SemaTemplate.cpp lib/Sema/SemaTemplateInstantiateExpr.cpp

Douglas Gregor dgregor at apple.com
Wed Jul 29 11:26:59 PDT 2009


Author: dgregor
Date: Wed Jul 29 13:26:50 2009
New Revision: 77488

URL: http://llvm.org/viewvc/llvm-project?rev=77488&view=rev
Log:
[llvm up]

A template name can refer to a set of overloaded function
templates. Model this in TemplateName, which can now refer to an
OverloadedFunctionDecl that contains function templates. This removes
an unspeakable hack in Sema::isTemplateName.

Modified:
    cfe/trunk/include/clang/AST/ASTContext.h
    cfe/trunk/include/clang/AST/TemplateName.h
    cfe/trunk/lib/AST/ASTContext.cpp
    cfe/trunk/lib/AST/TemplateName.cpp
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/lib/Sema/SemaLookup.cpp
    cfe/trunk/lib/Sema/SemaTemplate.cpp
    cfe/trunk/lib/Sema/SemaTemplateInstantiateExpr.cpp

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

==============================================================================
--- cfe/trunk/include/clang/AST/ASTContext.h (original)
+++ cfe/trunk/include/clang/AST/ASTContext.h Wed Jul 29 13:26:50 2009
@@ -583,7 +583,10 @@
   TemplateName getQualifiedTemplateName(NestedNameSpecifier *NNS, 
                                         bool TemplateKeyword,
                                         TemplateDecl *Template);
-
+  TemplateName getQualifiedTemplateName(NestedNameSpecifier *NNS, 
+                                        bool TemplateKeyword,
+                                        OverloadedFunctionDecl *Template);
+  
   TemplateName getDependentTemplateName(NestedNameSpecifier *NNS, 
                                         const IdentifierInfo *Name);
 

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

==============================================================================
--- cfe/trunk/include/clang/AST/TemplateName.h (original)
+++ cfe/trunk/include/clang/AST/TemplateName.h Wed Jul 29 13:26:50 2009
@@ -28,7 +28,9 @@
 class NestedNameSpecifier;
 struct PrintingPolicy;
 class QualifiedTemplateName;
+class NamedDecl;
 class TemplateDecl;
+class OverloadedFunctionDecl;
 
 /// \brief Represents a C++ template name within the type system.
 ///
@@ -58,7 +60,8 @@
 /// specifier in the typedef. "apply" is a nested template, and can
 /// only be understood in the context of
 class TemplateName {
-  typedef llvm::PointerUnion3<TemplateDecl *, QualifiedTemplateName *, 
+  typedef llvm::PointerUnion4<TemplateDecl *, OverloadedFunctionDecl *,
+                              QualifiedTemplateName *, 
                               DependentTemplateName *> StorageType;
 
   StorageType Storage;
@@ -70,6 +73,8 @@
 public:
   TemplateName() : Storage() { }
   explicit TemplateName(TemplateDecl *Template) : Storage(Template) { }
+  explicit TemplateName(OverloadedFunctionDecl *FunctionTemplates)
+    : Storage(FunctionTemplates) { }
   explicit TemplateName(QualifiedTemplateName *Qual) : Storage(Qual) { }
   explicit TemplateName(DependentTemplateName *Dep) : Storage(Dep) { }
 
@@ -78,9 +83,19 @@
   ///
   /// \returns The template declaration that this template name refers
   /// to, if any. If the template name does not refer to a specific
-  /// declaration because it is a dependent name, returns NULL.
+  /// declaration because it is a dependent name, or if it refers to a 
+  /// set of function templates, returns NULL.
   TemplateDecl *getAsTemplateDecl() const;
 
+  /// \brief Retrieve the the underlying, overloaded function template 
+  // declarations that this template name refers to, if known.
+  ///
+  /// \returns The set of overloaded function templates that this template 
+  /// name refers to, if known. If the template name does not refer to a 
+  /// specific set of function templates because it is a dependent name or
+  /// refers to a single template, returns NULL.
+  OverloadedFunctionDecl *getAsOverloadedFunctionDecl() const;
+  
   /// \brief Retrieve the underlying qualified template name
   /// structure, if any.
   QualifiedTemplateName *getAsQualifiedTemplateName() const {
@@ -145,16 +160,22 @@
   /// this name with DependentTemplateName).
   llvm::PointerIntPair<NestedNameSpecifier *, 1> Qualifier;
 
-  /// \brief The template declaration that this qualified name refers
-  /// to.
-  TemplateDecl *Template;
+  /// \brief The template declaration or set of overloaded function templates
+  /// that this qualified name refers to.
+  NamedDecl *Template;
 
   friend class ASTContext;
 
   QualifiedTemplateName(NestedNameSpecifier *NNS, bool TemplateKeyword,
                         TemplateDecl *Template)
-    : Qualifier(NNS, TemplateKeyword? 1 : 0), Template(Template) { }
+    : Qualifier(NNS, TemplateKeyword? 1 : 0), 
+      Template(reinterpret_cast<NamedDecl *>(Template)) { }
 
+  QualifiedTemplateName(NestedNameSpecifier *NNS, bool TemplateKeyword,
+                        OverloadedFunctionDecl *Template)
+  : Qualifier(NNS, TemplateKeyword? 1 : 0), 
+    Template(reinterpret_cast<NamedDecl *>(Template)) { }
+  
 public:
   /// \brief Return the nested name specifier that qualifies this name.
   NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); }
@@ -163,16 +184,26 @@
   /// keyword.
   bool hasTemplateKeyword() const { return Qualifier.getInt(); }
 
+  /// \brief The template declaration or set of overloaded functions that
+  /// that qualified name refers to.
+  NamedDecl *getDecl() const { return Template; }
+  
   /// \brief The template declaration to which this qualified name
-  /// refers.
-  TemplateDecl *getTemplateDecl() const { return Template; }
-
+  /// refers, or NULL if this qualified name refers to a set of overloaded
+  /// function templates.
+  TemplateDecl *getTemplateDecl() const;
+
+  /// \brief The set of overloaded function tempaltes to which this qualified
+  /// name refers, or NULL if this qualified name refers to a single 
+  /// template declaration.
+  OverloadedFunctionDecl *getOverloadedFunctionDecl() const;
+  
   void Profile(llvm::FoldingSetNodeID &ID) {
-    Profile(ID, getQualifier(), hasTemplateKeyword(), getTemplateDecl());
+    Profile(ID, getQualifier(), hasTemplateKeyword(), getDecl());
   }
 
   static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS, 
-                      bool TemplateKeyword, TemplateDecl *Template) {
+                      bool TemplateKeyword, NamedDecl *Template) {
     ID.AddPointer(NNS);
     ID.AddBoolean(TemplateKeyword);
     ID.AddPointer(Template);

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

==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Wed Jul 29 13:26:50 2009
@@ -2036,6 +2036,12 @@
   if (TemplateDecl *Template = Name.getAsTemplateDecl())
     return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
 
+  // If this template name refers to a set of overloaded function templates, 
+  /// the canonical template name merely stores the set of function templates.
+  if (OverloadedFunctionDecl *Ovl = Name.getAsOverloadedFunctionDecl())
+    // FIXME: Can't really canonicalize a set of overloaded functions, can we?
+    return TemplateName(Ovl);
+  
   DependentTemplateName *DTN = Name.getAsDependentTemplateName();
   assert(DTN && "Non-dependent template names must refer to template decls.");
   return DTN->CanonicalTemplateName;
@@ -3052,6 +3058,25 @@
   return TemplateName(QTN);
 }
 
+/// \brief Retrieve the template name that represents a qualified
+/// template name such as \c std::vector.
+TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, 
+                                                  bool TemplateKeyword,
+                                            OverloadedFunctionDecl *Template) {
+  llvm::FoldingSetNodeID ID;
+  QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
+  
+  void *InsertPos = 0;
+  QualifiedTemplateName *QTN =
+  QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
+  if (!QTN) {
+    QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
+    QualifiedTemplateNames.InsertNode(QTN, InsertPos);
+  }
+  
+  return TemplateName(QTN);
+}
+
 /// \brief Retrieve the template name that represents a dependent
 /// template name such as \c MetaFun::template apply.
 TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, 

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

==============================================================================
--- cfe/trunk/lib/AST/TemplateName.cpp (original)
+++ cfe/trunk/lib/AST/TemplateName.cpp Wed Jul 29 13:26:50 2009
@@ -29,14 +29,26 @@
   return 0;
 }
 
+OverloadedFunctionDecl *TemplateName::getAsOverloadedFunctionDecl() const {
+  if (OverloadedFunctionDecl *Ovl 
+        = Storage.dyn_cast<OverloadedFunctionDecl *>())
+    return Ovl;
+  
+  if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName())
+    return QTN->getOverloadedFunctionDecl();
+  
+  return 0;
+}
+
 bool TemplateName::isDependent() const {
   if (TemplateDecl *Template = getAsTemplateDecl()) {
-    // FIXME: We don't yet have a notion of dependent
-    // declarations. When we do, check that. This hack won't last
-    // long!.
-    return isa<TemplateTemplateParmDecl>(Template);
+    return isa<TemplateTemplateParmDecl>(Template) || 
+      Template->getDeclContext()->isDependentContext();
   }
 
+  if (OverloadedFunctionDecl *Ovl = getAsOverloadedFunctionDecl())
+    return Ovl->getDeclContext()->isDependentContext();
+  
   return true;
 }
 
@@ -45,16 +57,20 @@
                     bool SuppressNNS) const {
   if (TemplateDecl *Template = Storage.dyn_cast<TemplateDecl *>())
     OS << Template->getIdentifier()->getName();
+  else if (OverloadedFunctionDecl *Ovl 
+             = Storage.dyn_cast<OverloadedFunctionDecl *>())
+    OS << Ovl->getNameAsString();
   else if (QualifiedTemplateName *QTN = getAsQualifiedTemplateName()) {
     if (!SuppressNNS)
       QTN->getQualifier()->print(OS, Policy);
     if (QTN->hasTemplateKeyword())
       OS << "template ";
-    OS << QTN->getTemplateDecl()->getIdentifier()->getName();
+    OS << QTN->getDecl()->getNameAsString();
   } else if (DependentTemplateName *DTN = getAsDependentTemplateName()) {
     if (!SuppressNNS)
       DTN->getQualifier()->print(OS, Policy);
     OS << "template ";
+    // FIXME: Shouldn't we have a more general kind of name?
     OS << DTN->getName()->getName();
   }
 }
@@ -65,3 +81,13 @@
   LO.Bool = true;
   print(llvm::errs(), PrintingPolicy(LO));
 }
+
+TemplateDecl *QualifiedTemplateName::getTemplateDecl() const { 
+  return dyn_cast<TemplateDecl>(Template); 
+}
+
+OverloadedFunctionDecl *
+QualifiedTemplateName::getOverloadedFunctionDecl() const {
+  return dyn_cast<OverloadedFunctionDecl>(Template); 
+}
+

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Wed Jul 29 13:26:50 2009
@@ -2701,6 +2701,8 @@
     } else if (TemplateIdRefExpr *TemplateIdRef 
                  = dyn_cast<TemplateIdRefExpr>(FnExpr)) {
       NDecl = TemplateIdRef->getTemplateName().getAsTemplateDecl();
+      if (!NDecl)
+        NDecl = TemplateIdRef->getTemplateName().getAsOverloadedFunctionDecl();
       HasExplicitTemplateArgs = true;
       ExplicitTemplateArgs = TemplateIdRef->getTemplateArgs();
       NumExplicitTemplateArgs = TemplateIdRef->getNumTemplateArgs();

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaLookup.cpp (original)
+++ cfe/trunk/lib/Sema/SemaLookup.cpp Wed Jul 29 13:26:50 2009
@@ -1596,8 +1596,7 @@
     if (DRE)
       Ovl = dyn_cast<OverloadedFunctionDecl>(DRE->getDecl());
     else if (TIRE)
-      Ovl = dyn_cast_or_null<OverloadedFunctionDecl>(
-                                  TIRE->getTemplateName().getAsTemplateDecl());
+      Ovl = TIRE->getTemplateName().getAsOverloadedFunctionDecl();
     if (!Ovl)
       continue;
 

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplate.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplate.cpp Wed Jul 29 13:26:50 2009
@@ -93,10 +93,18 @@
               if (isa<FunctionTemplateDecl>(*F))
                 OvlTemplate->addOverload(*F);
             }
-            
-            // FIXME: HACK! We need TemplateName to be able to refer to
-            // sets of overloaded function templates.
-            TemplateResult = TemplateTy::make(OvlTemplate);
+
+            // Form the resulting TemplateName
+            if (SS && SS->isSet() && !SS->isInvalid()) {
+              NestedNameSpecifier *Qualifier 
+                = static_cast<NestedNameSpecifier *>(SS->getScopeRep());
+              TemplateResult 
+                = TemplateTy::make(Context.getQualifiedTemplateName(Qualifier, 
+                                                                    false,
+                                                                  OvlTemplate));              
+            } else {
+              TemplateResult = TemplateTy::make(TemplateName(OvlTemplate));
+            }
             return TNK_Function_template;
           }
           

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplateInstantiateExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplateInstantiateExpr.cpp Wed Jul 29 13:26:50 2009
@@ -109,7 +109,8 @@
 Sema::OwningExprResult
 TemplateExprInstantiator::VisitTemplateIdRefExpr(TemplateIdRefExpr *E) {
   TemplateName Template 
-    = SemaRef.InstantiateTemplateName(E->getTemplateName(), E->getTemplateNameLoc(),
+    = SemaRef.InstantiateTemplateName(E->getTemplateName(), 
+                                      E->getTemplateNameLoc(),
                                       TemplateArgs);
   // FIXME: Can InstantiateTemplateName report an error?
   





More information about the cfe-commits mailing list