[cfe-commits] r71818 - in /cfe/trunk: lib/Sema/Sema.h lib/Sema/SemaTemplateInstantiateDecl.cpp lib/Sema/SemaTemplateInstantiateExpr.cpp test/SemaTemplate/instantiate-function-1.cpp

Douglas Gregor dgregor at apple.com
Thu May 14 17:01:08 PDT 2009


Author: dgregor
Date: Thu May 14 19:01:03 2009
New Revision: 71818

URL: http://llvm.org/viewvc/llvm-project?rev=71818&view=rev
Log:
Implement template instantiation for DeclStmt

Modified:
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp
    cfe/trunk/lib/Sema/SemaTemplateInstantiateExpr.cpp
    cfe/trunk/test/SemaTemplate/instantiate-function-1.cpp

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

==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Thu May 14 19:01:03 2009
@@ -1978,7 +1978,7 @@
     /// \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

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp Thu May 14 19:01:03 2009
@@ -107,7 +107,7 @@
   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 @@
   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 @@
     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 @@
     Function->setInvalidDecl(true);
   else
     Function->setBody(Body.takeAs<Stmt>());
+
+  CurContext = PreviousContext;
 }
 
 /// \brief Instantiate the definition of the given variable from its

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplateInstantiateExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplateInstantiateExpr.cpp Thu May 14 19:01:03 2009
@@ -441,6 +441,7 @@
     // 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 @@
   };
 }
 
+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()));
 }

Modified: cfe/trunk/test/SemaTemplate/instantiate-function-1.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/instantiate-function-1.cpp?rev=71818&r1=71817&r2=71818&view=diff

==============================================================================
--- cfe/trunk/test/SemaTemplate/instantiate-function-1.cpp (original)
+++ cfe/trunk/test/SemaTemplate/instantiate-function-1.cpp Thu May 14 19:01:03 2009
@@ -18,8 +18,11 @@
   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}}





More information about the cfe-commits mailing list