]> granicus.if.org Git - clang/commitdiff
At the end of the translation unit, defining a vtable can introduce
authorDouglas Gregor <dgregor@apple.com>
Fri, 22 Apr 2011 22:25:37 +0000 (22:25 +0000)
committerDouglas Gregor <dgregor@apple.com>
Fri, 22 Apr 2011 22:25:37 +0000 (22:25 +0000)
new templates that need to be instantiated and vice-versa. Iterate
until we've instantiated all required templates and defined all
required vtables. Fixed PR9325 / <rdar://problem/9055177>.

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

include/clang/Sema/Sema.h
lib/Sema/Sema.cpp
lib/Sema/SemaDeclCXX.cpp
lib/Sema/SemaTemplateInstantiateDecl.cpp
test/SemaCXX/vtable-instantiation.cc

index 7cb0ad9970e414db0a72f9a777c96fdbd637e335..b1746ac308805a275b5353f6c986177347f35d9c 100644 (file)
@@ -4349,7 +4349,7 @@ public:
   /// types, static variables, enumerators, etc.
   std::deque<PendingImplicitInstantiation> PendingLocalImplicitInstantiations;
 
-  void PerformPendingInstantiations(bool LocalOnly = false);
+  bool PerformPendingInstantiations(bool LocalOnly = false);
 
   TypeSourceInfo *SubstType(TypeSourceInfo *T,
                             const MultiLevelTemplateArgumentList &TemplateArgs,
index dc1270243dd06661dec5baf311a96b2d6efc6c32..c954c64e7d0b1c9a12bf2a6d793ea29d1cb9bf7b 100644 (file)
@@ -373,22 +373,30 @@ void Sema::ActOnEndOfTranslationUnit() {
       }
     }
 
-    // If DefinedUsedVTables ends up marking any virtual member functions it
-    // might lead to more pending template instantiations, which we then need
-    // to instantiate.
-    DefineUsedVTables();
-
-    // C++: Perform implicit template instantiations.
-    //
-    // FIXME: When we perform these implicit instantiations, we do not
-    // carefully keep track of the point of instantiation (C++ [temp.point]).
-    // This means that name lookup that occurs within the template
-    // instantiation will always happen at the end of the translation unit,
-    // so it will find some names that should not be found. Although this is
-    // common behavior for C++ compilers, it is technically wrong. In the
-    // future, we either need to be able to filter the results of name lookup
-    // or we need to perform template instantiations earlier.
-    PerformPendingInstantiations();
+    bool SomethingChanged;
+    do {
+      SomethingChanged = false;
+      
+      // If DefinedUsedVTables ends up marking any virtual member functions it
+      // might lead to more pending template instantiations, which we then need
+      // to instantiate.
+      if (DefineUsedVTables())
+        SomethingChanged = true;
+
+      // C++: Perform implicit template instantiations.
+      //
+      // FIXME: When we perform these implicit instantiations, we do not
+      // carefully keep track of the point of instantiation (C++ [temp.point]).
+      // This means that name lookup that occurs within the template
+      // instantiation will always happen at the end of the translation unit,
+      // so it will find some names that should not be found. Although this is
+      // common behavior for C++ compilers, it is technically wrong. In the
+      // future, we either need to be able to filter the results of name lookup
+      // or we need to perform template instantiations earlier.
+      if (PerformPendingInstantiations())
+        SomethingChanged = true;
+      
+    } while (SomethingChanged);
   }
   
   // Remove file scoped decls that turned out to be used.
index 5f3f600c8ca8369f22ca5e2f63cd347da11ba8b5..3a87cfd9665ce5576724965e0b13f1611050b507 100644 (file)
@@ -7751,6 +7751,7 @@ bool Sema::DefineUsedVTables() {
   // the members of a class as "used", so we check the size each
   // time through the loop and prefer indices (with are stable) to
   // iterators (which are not).
+  bool DefinedAnything = false;
   for (unsigned I = 0; I != VTableUses.size(); ++I) {
     CXXRecordDecl *Class = VTableUses[I].first->getDefinition();
     if (!Class)
@@ -7803,6 +7804,7 @@ bool Sema::DefineUsedVTables() {
     // Mark all of the virtual members of this class as referenced, so
     // that we can build a vtable. Then, tell the AST consumer that a
     // vtable for this class is required.
+    DefinedAnything = true;
     MarkVirtualMembersReferenced(Loc, Class);
     CXXRecordDecl *Canonical = cast<CXXRecordDecl>(Class->getCanonicalDecl());
     Consumer.HandleVTable(Class, VTablesUsed[Canonical]);
@@ -7816,7 +7818,7 @@ bool Sema::DefineUsedVTables() {
   }
   VTableUses.clear();
 
-  return true;
+  return DefinedAnything;
 }
 
 void Sema::MarkVirtualMembersReferenced(SourceLocation Loc,
index 588501f50ede8a0d0722c720a98ce707a3bdd95f..c972c6602e1f9273dca0667fc412101ea8aec226 100644 (file)
@@ -3109,7 +3109,10 @@ NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
 
 /// \brief Performs template instantiation for all implicit template
 /// instantiations we have seen until this point.
-void Sema::PerformPendingInstantiations(bool LocalOnly) {
+///
+/// \returns true if anything was instantiated.
+bool Sema::PerformPendingInstantiations(bool LocalOnly) {
+  bool InstantiatedAnything = false;
   while (!PendingLocalImplicitInstantiations.empty() ||
          (!LocalOnly && !PendingInstantiations.empty())) {
     PendingImplicitInstantiation Inst;
@@ -3130,6 +3133,7 @@ void Sema::PerformPendingInstantiations(bool LocalOnly) {
                                 TSK_ExplicitInstantiationDefinition;
       InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true,
                                     DefinitionRequired);
+      InstantiatedAnything = true;
       continue;
     }
 
@@ -3166,7 +3170,10 @@ void Sema::PerformPendingInstantiations(bool LocalOnly) {
                               TSK_ExplicitInstantiationDefinition;
     InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true,
                                           DefinitionRequired);
+    InstantiatedAnything = true;
   }
+  
+  return InstantiatedAnything;
 }
 
 void Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
index 5a13d9505b3d5e9ba4359b1eacd6fd12c26ff535..49949a7736758ae38bbc28973cf6a957dcaeef78 100644 (file)
@@ -18,3 +18,29 @@ void f() {
   c2.c2();  // expected-note {{in instantiation of member function}}
 }
 
+namespace PR9325 {
+  template<typename T>
+  class Target
+  {
+  public:
+    virtual T Value() const
+    {
+      return 1; // expected-error{{cannot initialize return object of type 'int *' with an rvalue of type 'int'}}
+    }
+  };
+
+  template<typename T>
+  struct Provider
+  {
+    static Target<T> Instance;
+  };
+
+  template<typename T>
+  Target<T> Provider<T>::Instance; // expected-note{{in instantiation of}}
+
+  void f()
+  {
+    Target<int*>* traits = &Provider<int*>::Instance;
+  }
+
+}