[cfe-commits] r66833 - in /cfe/trunk: include/clang/AST/DeclTemplate.h lib/Sema/SemaTemplate.cpp lib/Sema/SemaTemplateInstantiate.cpp

Douglas Gregor dgregor at apple.com
Thu Mar 12 15:20:26 PDT 2009


Author: dgregor
Date: Thu Mar 12 17:20:26 2009
New Revision: 66833

URL: http://llvm.org/viewvc/llvm-project?rev=66833&view=rev
Log:
Store the type of the integral value within a TemplateArgument, so that we can more efficiently reconstruct an IntegerLiteral from it during template instantiation

Modified:
    cfe/trunk/include/clang/AST/DeclTemplate.h
    cfe/trunk/lib/Sema/SemaTemplate.cpp
    cfe/trunk/lib/Sema/SemaTemplateInstantiate.cpp

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

==============================================================================
--- cfe/trunk/include/clang/AST/DeclTemplate.h (original)
+++ cfe/trunk/include/clang/AST/DeclTemplate.h Thu Mar 12 17:20:26 2009
@@ -406,7 +406,10 @@
 class TemplateArgument {
   union {
     uintptr_t TypeOrValue;
-    char IntegralValue[sizeof(llvm::APInt)];
+    struct {
+      char Value[sizeof(llvm::APInt)];
+      void *Type;
+    } Integer;
   };
 
   /// \brief Location of the beginning of this template argument.
@@ -446,9 +449,11 @@
   }
 
   /// \brief Construct an integral constant template argument.
-  TemplateArgument(SourceLocation Loc, const llvm::APInt &Value)
+  TemplateArgument(SourceLocation Loc, const llvm::APInt &Value,
+                   QualType Type)
     : Kind(Integral) {
-    new (IntegralValue) llvm::APInt(Value);
+    new (Integer.Value) llvm::APInt(Value);
+    Integer.Type = Type.getAsOpaquePtr();
     StartLoc = Loc;
   }
 
@@ -461,8 +466,10 @@
 
   /// \brief Copy constructor for a template argument.
   TemplateArgument(const TemplateArgument &Other) : Kind(Other.Kind) {
-    if (Kind == Integral)
-      new (IntegralValue) llvm::APInt(*Other.getAsIntegral());
+    if (Kind == Integral) {
+      new (Integer.Value) llvm::APInt(*Other.getAsIntegral());
+      Integer.Type = Other.Integer.Type;
+    }
     else
       TypeOrValue = Other.TypeOrValue;
     StartLoc = Other.StartLoc;
@@ -476,6 +483,7 @@
     if (Kind == Other.Kind && Kind == Integral) {
       // Copy integral values.
       *this->getAsIntegral() = *Other.getAsIntegral();
+      Integer.Type = Other.Integer.Type; 
     } else {
       // Destroy the current integral value, if that's what we're holding.
       if (Kind == Integral)
@@ -483,9 +491,10 @@
       
       Kind = Other.Kind;
       
-      if (Other.Kind == Integral)
-        new (IntegralValue) llvm::APInt(*Other.getAsIntegral());
-      else
+      if (Other.Kind == Integral) {
+        new (Integer.Value) llvm::APInt(*Other.getAsIntegral());
+        Integer.Type = Other.Integer.Type;
+      } else
         TypeOrValue = Other.TypeOrValue;
     }
     StartLoc = Other.StartLoc;
@@ -523,13 +532,21 @@
   llvm::APInt *getAsIntegral() {
     if (Kind != Integral)
       return 0;
-    return reinterpret_cast<llvm::APInt*>(&IntegralValue[0]);
+    return reinterpret_cast<llvm::APInt*>(&Integer.Value[0]);
   }
 
   const llvm::APInt *getAsIntegral() const {
     return const_cast<TemplateArgument*>(this)->getAsIntegral();
   }
 
+  /// \brief Retrieve the type of the integral value.
+  QualType getIntegralType() const {
+    if (Kind != Integral)
+      return QualType();
+
+    return QualType::getFromOpaquePtr(Integer.Type);
+  }
+
   /// \brief Retrieve the template argument as an expression.
   Expr *getAsExpr() const {
     if (Kind != Expression)
@@ -555,6 +572,7 @@
 
     case Integral:
       getAsIntegral()->Profile(ID);
+      getIntegralType().Profile(ID);
       break;
 
     case Expression:

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplate.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplate.cpp Thu Mar 12 17:20:26 2009
@@ -1280,7 +1280,8 @@
                                IntegerType->isSignedIntegerType());
       CanonicalArg = Value;
 
-      Converted->push_back(TemplateArgument(StartLoc, CanonicalArg));
+      Converted->push_back(TemplateArgument(StartLoc, CanonicalArg,
+                                   Context.getCanonicalType(IntegerType)));
     }
 
     return false;

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplateInstantiate.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplateInstantiate.cpp Thu Mar 12 17:20:26 2009
@@ -595,19 +595,11 @@
   Decl *D = E->getDecl();
   if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
     assert(NTTP->getDepth() == 0 && "No nested templates yet");
-    QualType T = NTTP->getType();
-    if (T->isDependentType()) {
-      // FIXME: We'll be doing this instantiation a lot. Should we
-      // cache this information in the TemplateArgument itself?
-      T = SemaRef.InstantiateType(T, TemplateArgs, NumTemplateArgs,
-                                  E->getSourceRange().getBegin(),
-                                  NTTP->getDeclName());
-      if (T.isNull())
-        return SemaRef.ExprError();
-    }
+    const TemplateArgument &Arg = TemplateArgs[NTTP->getPosition()]; 
     return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral(
-                           *TemplateArgs[NTTP->getPosition()].getAsIntegral(),
-                            T, E->getSourceRange().getBegin()));
+                                                 *Arg.getAsIntegral(),
+                                                 Arg.getIntegralType(), 
+                                       E->getSourceRange().getBegin()));
   } else
     assert(false && "Can't handle arbitrary declaration references");
 





More information about the cfe-commits mailing list