]> granicus.if.org Git - clang/commitdiff
Instantiate default argument expressions even if their associated parameter
authorJohn McCall <rjmccall@apple.com>
Fri, 9 Apr 2010 17:38:44 +0000 (17:38 +0000)
committerJohn McCall <rjmccall@apple.com>
Fri, 9 Apr 2010 17:38:44 +0000 (17:38 +0000)
type isn't dependent.  Fixes rdar://problem/7838962.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@100871 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/Sema.h
lib/Sema/SemaTemplateInstantiate.cpp
lib/Sema/SemaTemplateInstantiateDecl.cpp
test/CXX/temp/temp.spec/temp.inst/p11.cpp [new file with mode: 0644]

index 65c5793be8e0f7f4bec5534222d9c7ce2ea2bc64..3250870d63799e2ecb0eba12ea100fa6b87ebdd6 100644 (file)
@@ -3638,6 +3638,11 @@ public:
                      const MultiLevelTemplateArgumentList &TemplateArgs,
                      SourceLocation Loc, DeclarationName Entity);
 
+  TypeSourceInfo *SubstFunctionDeclType(TypeSourceInfo *T,
+                            const MultiLevelTemplateArgumentList &TemplateArgs,
+                                        SourceLocation Loc,
+                                        DeclarationName Entity);
+
   OwningExprResult SubstExpr(Expr *E,
                             const MultiLevelTemplateArgumentList &TemplateArgs);
 
index d21862b71e8938c6b4c54bdd0e71e4d98e15ee1c..6895364abf3340dd4ba0665b03a6014bbde14784 100644 (file)
@@ -959,6 +959,56 @@ QualType Sema::SubstType(QualType T,
   return Instantiator.TransformType(T);
 }
 
+static bool NeedsInstantiationAsFunctionType(TypeSourceInfo *T) {
+  if (T->getType()->isDependentType())
+    return true;
+
+  TypeLoc TL = T->getTypeLoc();
+  if (!isa<FunctionProtoTypeLoc>(TL))
+    return false;
+
+  FunctionProtoTypeLoc FP = cast<FunctionProtoTypeLoc>(TL);
+  for (unsigned I = 0, E = FP.getNumArgs(); I != E; ++I) {
+    ParmVarDecl *P = FP.getArg(I);
+
+    // TODO: currently we always rebuild expressions.  When we
+    // properly get lazier about this, we should use the same
+    // logic to avoid rebuilding prototypes here.
+    if (P->hasInit())
+      return true;
+  }
+
+  return false;
+}
+
+/// A form of SubstType intended specifically for instantiating the
+/// type of a FunctionDecl.  Its purpose is solely to force the
+/// instantiation of default-argument expressions.
+TypeSourceInfo *Sema::SubstFunctionDeclType(TypeSourceInfo *T,
+                                const MultiLevelTemplateArgumentList &Args,
+                                SourceLocation Loc,
+                                DeclarationName Entity) {
+  assert(!ActiveTemplateInstantiations.empty() &&
+         "Cannot perform an instantiation without some context on the "
+         "instantiation stack");
+  
+  if (!NeedsInstantiationAsFunctionType(T))
+    return T;
+
+  TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
+
+  TypeLocBuilder TLB;
+
+  TypeLoc TL = T->getTypeLoc();
+  TLB.reserve(TL.getFullDataSize());
+
+  QualType Result = Instantiator.TransformType(TLB, TL, QualType());
+  if (Result.isNull())
+    return 0;
+
+  return TLB.getTypeSourceInfo(Context, Result);
+}
+
 /// \brief Perform substitution on the base class specifiers of the
 /// given class template specialization.
 ///
index 1ac854c770cb82a394afc7418bed364f51ef959f..82270451cbcc419e8bf7a7180552432e0b4d7453 100644 (file)
@@ -1826,9 +1826,10 @@ TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
   TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
   assert(OldTInfo && "substituting function without type source info");
   assert(Params.empty() && "parameter vector is non-empty at start");
-  TypeSourceInfo *NewTInfo = SemaRef.SubstType(OldTInfo, TemplateArgs,
-                                               D->getTypeSpecStartLoc(),
-                                               D->getDeclName());
+  TypeSourceInfo *NewTInfo
+    = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
+                                    D->getTypeSpecStartLoc(),
+                                    D->getDeclName());
   if (!NewTInfo)
     return 0;
 
diff --git a/test/CXX/temp/temp.spec/temp.inst/p11.cpp b/test/CXX/temp/temp.spec/temp.inst/p11.cpp
new file mode 100644 (file)
index 0000000..d375beb
--- /dev/null
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -verify -emit-llvm-only %s
+
+// rdar://problem/7838962
+namespace test0 {
+  template<typename T> unsigned f0() {
+    return T::MaxSize; // expected-error {{'int' cannot be used prior to '::'}}
+  };
+  template<typename T> class A {
+    void Allocate(unsigned Alignment
+                    = f0<T>()) // expected-note {{in instantiation}}
+    {}
+  };
+  void f1(A<int> x) { x.Allocate(); }
+  
+}