]> granicus.if.org Git - clang/commitdiff
Implement template instantiation for DeclStmt
authorDouglas Gregor <dgregor@apple.com>
Fri, 15 May 2009 00:01:03 +0000 (00:01 +0000)
committerDouglas Gregor <dgregor@apple.com>
Fri, 15 May 2009 00:01:03 +0000 (00:01 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@71818 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/Sema.h
lib/Sema/SemaTemplateInstantiateDecl.cpp
lib/Sema/SemaTemplateInstantiateExpr.cpp
test/SemaTemplate/instantiate-function-1.cpp

index 87c01075a8e4c3652525b31fdcfb1b4cfbbd9904..9947d3ef4f32ab285498229ccad094b6868ebfb2 100644 (file)
@@ -1978,7 +1978,7 @@ public:
     /// \brief The kind of template instantiation we are performing
     enum {
       /// We are instantiating a template declaration. The entity is
-      /// the declaration we're instantiation (e.g., a CXXRecordDecl).
+      /// the declaration we're instantiating (e.g., a CXXRecordDecl).
       TemplateInstantiation,
 
       /// We are instantiating a default argument for a template
index eb006dc3f3e051fcbcc4a424ba697d682f4b229f..113003fd44284f317dfadddd30bdf775471bb600 100644 (file)
@@ -107,7 +107,7 @@ Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
   if (T.isNull())
     return 0;
 
-  // Build the instantiataed declaration
+  // Build the instantiated declaration
   VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
                                  D->getLocation(), D->getIdentifier(),
                                  T, D->getStorageClass(),
@@ -585,6 +585,8 @@ void Sema::InstantiateFunctionDefinition(FunctionDecl *Function) {
   if (!Pattern)
     return;
 
+  // FIXME: add to the instantiation stack.
+
   // Introduce a new scope where local variable instantiations will be
   // recorded.
   LocalInstantiationScope Scope(*this);
@@ -595,6 +597,11 @@ void Sema::InstantiateFunctionDefinition(FunctionDecl *Function) {
     Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
                             Function->getParamDecl(I));
 
+  // Enter the scope of this instantiation. We don't use
+  // PushDeclContext because we don't have a scope.
+  DeclContext *PreviousContext = CurContext;
+  CurContext = Function;
+
   // Instantiate the function body.
   OwningStmtResult Body 
     = InstantiateStmt(Pattern, getTemplateInstantiationArgs(Function));
@@ -602,6 +609,8 @@ void Sema::InstantiateFunctionDefinition(FunctionDecl *Function) {
     Function->setInvalidDecl(true);
   else
     Function->setBody(Body.takeAs<Stmt>());
+
+  CurContext = PreviousContext;
 }
 
 /// \brief Instantiate the definition of the given variable from its
index 52daa5d6a0313c2496f71e134045c23c587816e8..2dca993f741ea766963c07880611c1faa4450c87 100644 (file)
@@ -441,6 +441,7 @@ namespace {
     // FIXME: Once we get closer to completion, replace these
     // manually-written declarations with automatically-generated ones
     // from clang/AST/StmtNodes.def.
+    OwningStmtResult VisitDeclStmt(DeclStmt *S);
     OwningStmtResult VisitNullStmt(NullStmt *S);
     OwningStmtResult VisitCompoundStmt(CompoundStmt *S);
     OwningStmtResult VisitExpr(Expr *E);
@@ -454,6 +455,28 @@ namespace {
   };
 }
 
+Sema::OwningStmtResult TemplateStmtInstantiator::VisitDeclStmt(DeclStmt *S) {
+  llvm::SmallVector<Decl *, 8> Decls;
+  for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
+       D != DEnd; ++D) {
+    Decl *Instantiated = SemaRef.InstantiateDecl(*D, SemaRef.CurContext, 
+                                                 TemplateArgs);
+    if (!Instantiated)
+      return SemaRef.StmtError();
+
+    Decls.push_back(Instantiated);
+    SemaRef.CurrentInstantiationScope->InstantiatedLocal(cast<VarDecl>(*D),
+                                                  cast<VarDecl>(Instantiated));
+  }
+
+  return SemaRef.Owned(new (SemaRef.Context) DeclStmt(
+                                         DeclGroupRef::Create(SemaRef.Context,
+                                                              &Decls[0],
+                                                              Decls.size()),
+                                                      S->getStartLoc(),
+                                                      S->getEndLoc()));
+}
+
 Sema::OwningStmtResult TemplateStmtInstantiator::VisitNullStmt(NullStmt *S) {
   return SemaRef.Owned(new (SemaRef.Context) NullStmt(S->getSemiLoc()));
 }
index 9cf2ca5129e30ab2f12ac3606289ea967b3c88ff..6b755c8ea954912623a6350f68b73910f73b6981 100644 (file)
@@ -18,8 +18,11 @@ struct X2 {
   void f(T);
 
   T g(T x, T y) {
+    /* DeclStmt */;
+    T *xp = &x, &yr = y; // expected-error{{pointer to a reference}}
     /* NullStmt */;
   }
 };
 
 template struct X2<int>;
+template struct X2<int&>; // expected-note{{instantiation of}}