[cfe-commits] r71799 - in /cfe/trunk: lib/Sema/Sema.cpp lib/Sema/Sema.h lib/Sema/SemaTemplateInstantiateDecl.cpp lib/Sema/SemaTemplateInstantiateExpr.cpp test/SemaTemplate/instantiate-expr-2.cpp

Douglas Gregor dgregor at apple.com
Thu May 14 14:44:50 PDT 2009


Author: dgregor
Date: Thu May 14 16:44:34 2009
New Revision: 71799

URL: http://llvm.org/viewvc/llvm-project?rev=71799&view=rev
Log:
Introduce a stack of instantiation scopes that are used to store the mapping from variable declarations that occur within templates to their instantiated counterparts

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

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

==============================================================================
--- cfe/trunk/lib/Sema/Sema.cpp (original)
+++ cfe/trunk/lib/Sema/Sema.cpp Thu May 14 16:44:34 2009
@@ -182,7 +182,8 @@
     ExternalSource(0), CurContext(0), PreDeclaratorDC(0),
     CurBlock(0), PackContext(0), IdResolver(pp.getLangOptions()),
     GlobalNewDeleteDeclared(false), 
-    CompleteTranslationUnit(CompleteTranslationUnit) {
+    CompleteTranslationUnit(CompleteTranslationUnit),
+    CurrentInstantiationScope(0) {
   
   StdNamespace = 0;
   TUScope = 0;

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

==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Thu May 14 16:44:34 2009
@@ -2092,6 +2092,73 @@
 
   void PrintInstantiationStack();
 
+  /// \brief A stack-allocated class that identifies which local
+  /// variable declaration instantiations are present in this scope.
+  ///
+  /// A new instance of this class type will be created whenever we
+  /// instantiate a new function declaration, which will have its own
+  /// set of parameter declarations.
+  class LocalInstantiationScope {
+    /// \brief Reference to the semantic analysis that is performing
+    /// this template instantiation.
+    Sema &SemaRef;
+
+    /// \brief A mapping from local variable declarations that occur
+    /// within a template to their instantiations.
+    ///
+    /// This mapping is used during instantiation to keep track of,
+    /// e.g., function parameter and variable declarations. For example,
+    /// given:
+    ///
+    /// \code
+    ///   template<typename T> T add(T x, T y) { return x + y; }
+    /// \endcode
+    ///
+    /// when we instantiate add<int>, we will introduce a mapping from
+    /// the ParmVarDecl for 'x' that occurs in the template to the
+    /// instantiated ParmVarDecl for 'x'.
+    llvm::DenseMap<VarDecl *, VarDecl *> LocalDecls;
+
+    /// \brief The outer scope, in which contains local variable
+    /// definitions from some other instantiation (that is not
+    /// relevant to this particular scope).
+    LocalInstantiationScope *Outer;
+
+    // This class is non-copyable
+    LocalInstantiationScope(const LocalInstantiationScope &);
+    LocalInstantiationScope &operator=(const LocalInstantiationScope &);
+
+  public:
+    LocalInstantiationScope(Sema &SemaRef)
+      : SemaRef(SemaRef), Outer(SemaRef.CurrentInstantiationScope) { 
+      SemaRef.CurrentInstantiationScope = this;
+    }
+
+    ~LocalInstantiationScope() {
+      SemaRef.CurrentInstantiationScope = Outer;
+    }
+
+    VarDecl *getInstantiationOf(VarDecl *Var) {
+      VarDecl *Result = LocalDecls[Var];
+      assert(Result && "Variable was not instantiated in this scope!");
+      return Result;
+    }
+
+    ParmVarDecl *getInstantiationOf(ParmVarDecl *Var) {
+      return cast<ParmVarDecl>(getInstantiationOf(cast<VarDecl>(Var)));
+    }
+
+    void InstantiatedLocal(VarDecl *Var, VarDecl *VarInst) {
+      VarDecl *&Stored = LocalDecls[Var];
+      assert(!Stored && "Already instantiated this local variable");
+      Stored = VarInst;
+    }
+  };
+
+  /// \brief The current instantiation scope used to store local
+  /// variables.
+  LocalInstantiationScope *CurrentInstantiationScope;
+
   QualType InstantiateType(QualType T, const TemplateArgumentList &TemplateArgs,
                            SourceLocation Loc, DeclarationName Entity);
 

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp Thu May 14 16:44:34 2009
@@ -279,6 +279,8 @@
   if (D->getKind() != Decl::CXXMethod)
     return 0;
 
+  Sema::LocalInstantiationScope Scope(SemaRef);
+
   llvm::SmallVector<ParmVarDecl *, 16> Params;
   QualType T = InstantiateFunctionType(D, Params);
   if (T.isNull())
@@ -320,6 +322,8 @@
 }
 
 Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
+  Sema::LocalInstantiationScope Scope(SemaRef);
+
   llvm::SmallVector<ParmVarDecl *, 16> Params;
   QualType T = InstantiateFunctionType(D, Params);
   if (T.isNull())
@@ -363,6 +367,8 @@
 }
 
 Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
+  Sema::LocalInstantiationScope Scope(SemaRef);
+
   llvm::SmallVector<ParmVarDecl *, 16> Params;
   QualType T = InstantiateFunctionType(D, Params);
   if (T.isNull())
@@ -391,6 +397,8 @@
 }
 
 Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
+  Sema::LocalInstantiationScope Scope(SemaRef);
+
   llvm::SmallVector<ParmVarDecl *, 16> Params;
   QualType T = InstantiateFunctionType(D, Params);
   if (T.isNull())
@@ -452,6 +460,7 @@
   // Note: we don't try to instantiate function parameters until after
   // we've instantiated the function's type. Therefore, we don't have
   // to check for 'void' parameter types here.
+  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
   return Param;
 }
 

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplateInstantiateExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplateInstantiateExpr.cpp Thu May 14 16:44:34 2009
@@ -85,6 +85,15 @@
                                                  *Arg.getAsIntegral(),
                                                  T, 
                                        E->getSourceRange().getBegin()));
+  } else if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D)) {
+    ParmVarDecl *ParmInst 
+      = SemaRef.CurrentInstantiationScope->getInstantiationOf(Parm);
+    QualType T = ParmInst->getType();
+    return SemaRef.Owned(new (SemaRef.Context) DeclRefExpr(ParmInst,
+                                                      T.getNonReferenceType(),
+                                                           E->getLocation(),
+                                                        T->isDependentType(),
+                                                        T->isDependentType()));
   } else
     assert(false && "Can't handle arbitrary declaration references");
 

Modified: cfe/trunk/test/SemaTemplate/instantiate-expr-2.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/instantiate-expr-2.cpp?rev=71799&r1=71798&r2=71799&view=diff

==============================================================================
--- cfe/trunk/test/SemaTemplate/instantiate-expr-2.cpp (original)
+++ cfe/trunk/test/SemaTemplate/instantiate-expr-2.cpp Thu May 14 16:44:34 2009
@@ -120,8 +120,6 @@
   typedef Cond<true, int, double>::Type Type;
 }
 
-#if 0
-// FIXME: Unable to handle general declaration references at this point.
 template<typename T, unsigned long N> struct IntegralConstant { };
 
 template<typename T>
@@ -130,6 +128,5 @@
 };
 
 void test_X0(X0<int> x, IntegralConstant<int, sizeof(int)> ic) {
-  x.f(ic);
+  x.f(5, ic);
 }
-#endif





More information about the cfe-commits mailing list