[cfe-commits] r81573 - in /cfe/trunk: lib/Sema/SemaExpr.cpp lib/Sema/SemaTemplate.cpp lib/Sema/SemaTemplateInstantiate.cpp lib/Sema/SemaTemplateInstantiateDecl.cpp test/SemaTemplate/extern-templates.cpp

Douglas Gregor dgregor at apple.com
Fri Sep 11 14:19:12 PDT 2009


Author: dgregor
Date: Fri Sep 11 16:19:12 2009
New Revision: 81573

URL: http://llvm.org/viewvc/llvm-project?rev=81573&view=rev
Log:
Slight improvement for extern templates, so that an explicit
instantiation definition can follow an explicit instantiation
declaration. This is as far as I want to go with extern templates now,
but they will still need quite a bit more work to get all of the C++0x
semantics right.

Modified:
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/lib/Sema/SemaTemplate.cpp
    cfe/trunk/lib/Sema/SemaTemplateInstantiate.cpp
    cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp
    cfe/trunk/test/SemaTemplate/extern-templates.cpp

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Fri Sep 11 16:19:12 2009
@@ -6123,6 +6123,7 @@
     // Implicit instantiation of static data members of class templates.
     // FIXME: distinguish between implicit instantiations (which we need to
     // actually instantiate) and explicit specializations.
+    // FIXME: extern templates
     if (Var->isStaticDataMember() &&
         Var->getInstantiatedFromStaticDataMember())
       PendingImplicitInstantiations.push_back(std::make_pair(Var, Loc));

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplate.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplate.cpp Fri Sep 11 16:19:12 2009
@@ -2977,7 +2977,7 @@
                                                   Converted, 0);
       Specialization->setLexicalDeclContext(CurContext);
       CurContext->addDecl(Specialization);
-      return DeclPtrTy::make(Specialization);
+      return DeclPtrTy::make(PrevDecl);
     }
 
     // If we have already (implicitly) instantiated this
@@ -2985,14 +2985,19 @@
     if (PrevDecl->getSpecializationKind() == TSK_ImplicitInstantiation)
       SpecializationRequiresInstantiation = false;
 
-    // Since the only prior class template specialization with these
-    // arguments was referenced but not declared, reuse that
-    // declaration node as our own, updating its source location to
-    // reflect our new declaration.
-    Specialization = PrevDecl;
-    Specialization->setLocation(TemplateNameLoc);
-    PrevDecl = 0;
-  } else {
+    if (PrevDecl->getSpecializationKind() == TSK_ImplicitInstantiation ||
+        PrevDecl->getSpecializationKind() == TSK_Undeclared) {
+      // Since the only prior class template specialization with these
+      // arguments was referenced but not declared, reuse that
+      // declaration node as our own, updating its source location to
+      // reflect our new declaration.
+      Specialization = PrevDecl;
+      Specialization->setLocation(TemplateNameLoc);
+      PrevDecl = 0;
+    }
+  } 
+  
+  if (!Specialization) {
     // Create a new class template specialization declaration node for
     // this explicit specialization.
     Specialization
@@ -3000,10 +3005,17 @@
                                              ClassTemplate->getDeclContext(),
                                                 TemplateNameLoc,
                                                 ClassTemplate,
-                                                Converted, 0);
+                                                Converted, PrevDecl);
 
-    ClassTemplate->getSpecializations().InsertNode(Specialization,
-                                                   InsertPos);
+    if (PrevDecl) {
+      // Remove the previous declaration from the folding set, since we want
+      // to introduce a new declaration.
+      ClassTemplate->getSpecializations().RemoveNode(PrevDecl);
+      ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos);
+    } 
+    
+    // Insert the new specialization.
+    ClassTemplate->getSpecializations().InsertNode(Specialization, InsertPos);
   }
 
   // Build the fully-sugared type for this explicit instantiation as

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplateInstantiate.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplateInstantiate.cpp Fri Sep 11 16:19:12 2009
@@ -820,11 +820,28 @@
   ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
                                          ClassTemplateSpec->getCanonicalDecl());
 
-  // We can only instantiate something that hasn't already been
-  // instantiated or specialized. Fail without any diagnostics: our
-  // caller will provide an error message.
-  if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared)
+  // Check whether we have already instantiated or specialized this class
+  // template specialization.
+  if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
+    if (ClassTemplateSpec->getSpecializationKind() == 
+          TSK_ExplicitInstantiationDeclaration &&
+        TSK == TSK_ExplicitInstantiationDefinition) {
+      // An explicit instantiation definition follows an explicit instantiation
+      // declaration (C++0x [temp.explicit]p10); go ahead and perform the
+      // explicit instantiation.
+      ClassTemplateSpec->setSpecializationKind(TSK);
+      InstantiateClassTemplateSpecializationMembers(
+                        /*FIXME?*/ClassTemplateSpec->getPointOfInstantiation(), 
+                                  ClassTemplateSpec,
+                                  TSK);
+      return false;
+    }
+    
+    // We can only instantiate something that hasn't already been
+    // instantiated or specialized. Fail without any diagnostics: our
+    // caller will provide an error message.    
     return true;
+  }
 
   ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
   CXXRecordDecl *Pattern = 0;

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp Fri Sep 11 16:19:12 2009
@@ -1031,6 +1031,9 @@
   // Instantiate the function body.
   OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
 
+  if (Body.isInvalid())
+    Function->setInvalidDecl();
+  
   ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
                           /*IsInstantiation=*/true);
 

Modified: cfe/trunk/test/SemaTemplate/extern-templates.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/extern-templates.cpp?rev=81573&r1=81572&r2=81573&view=diff

==============================================================================
--- cfe/trunk/test/SemaTemplate/extern-templates.cpp (original)
+++ cfe/trunk/test/SemaTemplate/extern-templates.cpp Fri Sep 11 16:19:12 2009
@@ -1,4 +1,4 @@
-// RUN: clang-cc -fsyntax-only %s
+// RUN: clang-cc -fsyntax-only -verify %s
 
 template<typename T>
 class X0 {
@@ -12,7 +12,7 @@
 
 template<typename T>
 void X0<T>::f(T t) {
-  t = 17;
+  t = 17; // expected-error{{incompatible}}
 }
 
 extern template class X0<int>;
@@ -21,10 +21,21 @@
 
 template<typename T>
 void X0<T>::Inner::g(T t) {
-  t = 17;
+  t = 17; // expected-error{{incompatible}}
 }
 
 void test_intptr(X0<int*> xi, X0<int*>::Inner xii) {
   xi.f(0);
   xii.g(0);
 }
+
+// FIXME: we would like the notes to point to the explicit instantiation at the
+// bottom.
+extern template class X0<long*>; // expected-note{{instantiation}}
+
+void test_longptr(X0<long*> xl, X0<long*>::Inner xli) {
+  xl.f(0);
+  xli.g(0); // expected-note{{instantiation}}
+}
+
+template class X0<long*>;





More information about the cfe-commits mailing list