This is becoming a snow ball, but I really think the patch has
everything it needs now. What was missing from the previous one is
that we were not producing the constructor for

 template<class T> struct C {
   virtual ~C();
 };
 template class C<int>;

The new patch fixes this, but got a bit larger since it has to handle
some corner cases of when an implicit copy assignment is legal.

Just finished a release bootstrap. OK if it also passes a debug bootstrap?

Cheers,
-- 
Rafael Ávila de Espíndola
diff --git a/lib/CodeGen/CGVtable.cpp b/lib/CodeGen/CGVtable.cpp
index 9204e4e..cedefba 100644
--- a/lib/CodeGen/CGVtable.cpp
+++ b/lib/CodeGen/CGVtable.cpp
@@ -3805,7 +3805,17 @@ void CodeGenVTables::EmitVTableRelatedData(GlobalDecl GD) {
     return;
 
   TemplateSpecializationKind kind = RD->getTemplateSpecializationKind();
-  if (kind == TSK_ImplicitInstantiation)
+
+
+  // The reason we have TSK_ExplicitInstantiationDeclaration in here (but not
+  // in  Sema::MaybeMarkVirtualMembersReferenced) is for the case
+  // template<> void stdio_sync_filebuf<wchar_t>::xsgetn() {
+  // }
+  // extern template class stdio_sync_filebuf<wchar_t>;
+  // Since we are called after the extern declaration is seen.
+
+  if (kind == TSK_ImplicitInstantiation ||
+      kind == TSK_ExplicitInstantiationDeclaration)
     CGM.DeferredVtables.push_back(RD);
   else
     GenerateClassData(CGM.getVtableLinkage(RD), RD);
diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp
index 1606710..41f5cf9 100644
--- a/lib/CodeGen/CodeGenModule.cpp
+++ b/lib/CodeGen/CodeGenModule.cpp
@@ -268,6 +268,15 @@ GetLinkageForFunction(ASTContext &Context, const FunctionDecl *FD,
       FD->getType()->getLinkage() == UniqueExternalLinkage)
     L = UniqueExternalLinkage;
   
+  // We check the specialization kind of the class for implicit methods.
+  // They have a TSK_Undeclared specialization kind.
+  TemplateSpecializationKind TSK;
+  const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
+  if (MD && MD->isImplicit())
+    TSK = MD->getParent()->getTemplateSpecializationKind();
+  else
+    TSK = FD->getTemplateSpecializationKind();
+
   switch (L) {
   case NoLinkage:
   case InternalLinkage:
@@ -275,7 +284,7 @@ GetLinkageForFunction(ASTContext &Context, const FunctionDecl *FD,
     return CodeGenModule::GVA_Internal;
     
   case ExternalLinkage:
-    switch (FD->getTemplateSpecializationKind()) {
+    switch (TSK) {
     case TSK_Undeclared:
     case TSK_ExplicitSpecialization:
       External = CodeGenModule::GVA_StrongExternal;
@@ -310,8 +319,7 @@ GetLinkageForFunction(ASTContext &Context, const FunctionDecl *FD,
   //   instantiated when used so that the body can be considered for 
   //   inlining, but that no out-of-line copy of the inline function would be
   //   generated in the translation unit. -- end note ]
-  if (FD->getTemplateSpecializationKind() 
-                                       == TSK_ExplicitInstantiationDeclaration)
+  if (TSK == TSK_ExplicitInstantiationDeclaration)
     return CodeGenModule::GVA_C99Inline;
   
   return CodeGenModule::GVA_CXXInline;
@@ -675,7 +683,7 @@ void CodeGenModule::EmitGlobal(GlobalDecl GD) {
   // Ignore declarations, they will be emitted on their first use.
   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) {
     // Forward declarations are emitted lazily on first use.
-    if (!FD->isThisDeclarationADefinition())
+    if (!FD->isThisDeclarationADefinition() && !FD->isImplicit())
       return;
   } else {
     const VarDecl *VD = cast<VarDecl>(Global);
diff --git a/lib/Sema/SemaTemplate.cpp b/lib/Sema/SemaTemplate.cpp
index abe9363..d7b613e 100644
--- a/lib/Sema/SemaTemplate.cpp
+++ b/lib/Sema/SemaTemplate.cpp
@@ -4389,13 +4389,7 @@ Sema::ActOnExplicitInstantiation(Scope *S,
   Def = cast_or_null<ClassTemplateSpecializationDecl>(
                                        Specialization->getDefinition());
   if (Def) {
-    TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
-
-    // Fix a TSK_ExplicitInstantiationDeclaration followed by a
-    // TSK_ExplicitInstantiationDefinition
-    if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
-        TSK == TSK_ExplicitInstantiationDefinition)
-      Def->setTemplateSpecializationKind(TSK);
+    Def->setTemplateSpecializationKind(TSK);
 
     InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
   }
diff --git a/lib/Sema/SemaTemplateInstantiate.cpp b/lib/Sema/SemaTemplateInstantiate.cpp
index 0d6acd0..1b4bed0 100644
--- a/lib/Sema/SemaTemplateInstantiate.cpp
+++ b/lib/Sema/SemaTemplateInstantiate.cpp
@@ -1380,6 +1380,42 @@ Sema::InstantiateClassTemplateSpecialization(
   return Result;
 }
 
+static bool CanProduceAssignmentOp(CXXRecordDecl *RD, ASTContext &Context) {
+  for (CXXRecordDecl::base_class_iterator Base = RD->bases_begin(),
+       E = RD->bases_end(); Base != E; ++Base) {
+    CXXRecordDecl *BaseClassDecl
+      = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
+    if (!CanProduceAssignmentOp(BaseClassDecl, Context))
+      return false;
+  }
+
+  for (CXXRecordDecl::field_iterator F = RD->field_begin(),
+         E = RD->field_end(); F != E; ++F) {
+    QualType FieldType = Context.getCanonicalType((*F)->getType());
+    if (FieldType->isReferenceType() || FieldType.isConstQualified())
+      return false;
+    const RecordType *FieldClassType = FieldType->getAs<RecordType>();
+    if (!FieldClassType)
+      continue;
+    CXXRecordDecl *FieldClassDecl
+      = cast<CXXRecordDecl>(FieldClassType->getDecl());
+    if (!CanProduceAssignmentOp(FieldClassDecl, Context))
+      return false;
+  }
+  return true;
+}
+
+static bool CanProduceImplicitFunction(FunctionDecl *Function,
+                                       bool AssignmentOK) {
+  if (Function->isTrivial())
+    return false;
+
+  if (!Function->isCopyAssignment())
+    return true;
+
+  return AssignmentOK;
+}
+
 /// \brief Instantiates the definitions of all of the member
 /// of the given class, which is an instantiation of a class template
 /// or a member class of a template.
@@ -1388,12 +1424,20 @@ Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
                               CXXRecordDecl *Instantiation,
                         const MultiLevelTemplateArgumentList &TemplateArgs,
                               TemplateSpecializationKind TSK) {
+  const bool AssignmentOK = CanProduceAssignmentOp(Instantiation,
+                                                   Context);
+
   for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
                                DEnd = Instantiation->decls_end();
        D != DEnd; ++D) {
     bool SuppressNew = false;
     if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
-      if (FunctionDecl *Pattern
+      if (Function->isImplicit() &&
+          CanProduceImplicitFunction(Function, AssignmentOK)) {
+        MarkDeclarationReferenced(PointOfInstantiation, Function);
+        DeclGroupRef DG(Function);
+        Consumer.HandleTopLevelDecl(DG);
+      } else if (FunctionDecl *Pattern
             = Function->getInstantiatedFromMemberFunction()) {
         MemberSpecializationInfo *MSInfo 
           = Function->getMemberSpecializationInfo();
diff --git a/lib/Sema/SemaTemplateInstantiateDecl.cpp b/lib/Sema/SemaTemplateInstantiateDecl.cpp
index dbe041c..e59514c 100644
--- a/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -1841,6 +1841,8 @@ void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
 
   CurContext = PreviousContext;
 
+  MarkDeclarationReferenced(PointOfInstantiation, Function);
+
   DeclGroupRef DG(Function);
   Consumer.HandleTopLevelDecl(DG);
 
diff --git a/test/CodeGenCXX/PR6677.cpp b/test/CodeGenCXX/PR6677.cpp
deleted file mode 100644
index 8d168f1..0000000
--- a/test/CodeGenCXX/PR6677.cpp
+++ /dev/null
@@ -1,33 +0,0 @@
-// RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -emit-llvm -o - | FileCheck %s
-
-// CHECK-NOT: @_ZTVN5test118stdio_sync_filebufIwEE = constant
-// CHECK: @_ZTVN5test018stdio_sync_filebufIwEE = constant
-
-namespace test0 {
-  struct  basic_streambuf   {
-    virtual       ~basic_streambuf();
-  };
-  template<typename _CharT >
-  struct stdio_sync_filebuf : public basic_streambuf {
-    virtual void      xsgetn();
-  };
-
-  // This specialization should cause the vtable to be emitted, even with
-  // the following extern template declaration.
-  template<> void stdio_sync_filebuf<wchar_t>::xsgetn()  {
-  }
-  extern template class stdio_sync_filebuf<wchar_t>;
-}
-
-namespace test1 {
-  struct  basic_streambuf   {
-    virtual       ~basic_streambuf();
-  };
-  template<typename _CharT >
-  struct stdio_sync_filebuf : public basic_streambuf {
-    virtual void      xsgetn();
-  };
-
-  // Just a declaration should not force the vtable to be emitted.
-  template<> void stdio_sync_filebuf<wchar_t>::xsgetn();
-}
diff --git a/test/CodeGenCXX/template-explicit-instantiation.cpp b/test/CodeGenCXX/template-explicit-instantiation.cpp
new file mode 100644
index 0000000..ad1eab6
--- /dev/null
+++ b/test/CodeGenCXX/template-explicit-instantiation.cpp
@@ -0,0 +1,80 @@
+// RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -emit-llvm -o - | FileCheck %s
+
+// CHECK-NOT: @_ZTVN5test118stdio_sync_filebufIwEE = constant
+// CHECK: @_ZTVN5test018stdio_sync_filebufIwEE = constant
+
+// CHECK: define linkonce_odr void @_ZN5test21CIiE5fobarIdEEvT_
+// CHECK: define available_externally void @_ZN5test21CIiE6zedbarEd
+
+// CHECK: define weak_odr void @_ZN5test31CIiEC1Ev(
+// CHECK: define weak_odr void @_ZN5test31CIiEC2Ev(
+// CHECK: define weak_odr void @_ZN5test31CIiEC1ERKS1_(
+// CHECK: define weak_odr void @_ZN5test31CIiEC2ERKS1_(
+// CHECK: define weak_odr {{.*}} @_ZN5test31CIiEaSERKS1_(
+
+namespace test0 {
+  struct  basic_streambuf   {
+    virtual       ~basic_streambuf();
+  };
+  template<typename _CharT >
+  struct stdio_sync_filebuf : public basic_streambuf {
+    virtual void      xsgetn();
+  };
+
+  // This specialization should cause the vtable to be emitted, even with
+  // the following extern template declaration (test at the top).
+
+  // The existance of the extern template declaration should prevent us from emitting
+  // destructors.
+  // CHECK: define available_externally void @_ZN5test018stdio_sync_filebufIwED0Ev
+  // CHECK: define available_externally void @_ZN5test018stdio_sync_filebufIwED2Ev
+  template<> void stdio_sync_filebuf<wchar_t>::xsgetn()  {
+  }
+  extern template class stdio_sync_filebuf<wchar_t>;
+}
+
+namespace test1 {
+  struct  basic_streambuf   {
+    virtual       ~basic_streambuf();
+  };
+  template<typename _CharT >
+  struct stdio_sync_filebuf : public basic_streambuf {
+    virtual void      xsgetn();
+  };
+
+  // Just a declaration should not force the vtable to be emitted
+  // (test at the top).
+  template<> void stdio_sync_filebuf<wchar_t>::xsgetn();
+}
+
+namespace test2 {
+  template<typename T1>
+  class C {
+    void zedbar(double) {
+    }
+    template<typename T2>
+    void fobar(T2 foo) {
+    }
+  };
+  extern template class C<int>;
+  void g() {
+    C<int> a;
+    // The extern template declaration should not prevent us from producing
+    /// foobar.
+    // (test at the top).
+    a.fobar(0.0);
+
+    // But it should prevent zebbar
+    // (test at the top).
+    a.zedbar(0.0);
+  }
+}
+
+namespace test3 {
+  template<class T> struct C {
+    virtual ~C();
+  };
+  // This should force the generation of the implicit methods of C.
+  // (test at the top).
+  template class C<int>;
+}
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to