]> granicus.if.org Git - clang/commitdiff
Revert r300653 and r300650. The underlying commit fixes one issue with
authorChandler Carruth <chandlerc@gmail.com>
Wed, 19 Apr 2017 05:25:13 +0000 (05:25 +0000)
committerChandler Carruth <chandlerc@gmail.com>
Wed, 19 Apr 2017 05:25:13 +0000 (05:25 +0000)
modules but exposes much more widespread issues. Example and more
information is on the review thread for r300650.

Original commit summary:
[modules] Properly look up the owning module for an instantiation of a merged template.

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

lib/AST/Decl.cpp
lib/AST/DeclCXX.cpp
lib/Sema/SemaLookup.cpp
test/Modules/Inputs/template-default-args/a.h
test/Modules/template-default-args.cpp

index 5fe0706521277cfb49de486a95d90223cef2afa4..2b22e5bb50a5c90b315bde08c0a2bb6da239b73d 100644 (file)
@@ -2251,14 +2251,6 @@ bool VarDecl::checkInitIsICE() const {
   return Eval->IsICE;
 }
 
-template<typename DeclT>
-static DeclT *getDefinitionOrSelf(DeclT *D) {
-  assert(D);
-  if (auto *Def = D->getDefinition())
-    return Def;
-  return D;
-}
-
 VarDecl *VarDecl::getTemplateInstantiationPattern() const {
   // If it's a variable template specialization, find the template or partial
   // specialization from which it was instantiated.
@@ -2270,7 +2262,7 @@ VarDecl *VarDecl::getTemplateInstantiationPattern() const {
           break;
         VTD = NewVTD;
       }
-      return getDefinitionOrSelf(VTD->getTemplatedDecl());
+      return VTD->getTemplatedDecl()->getDefinition();
     }
     if (auto *VTPSD =
             From.dyn_cast<VarTemplatePartialSpecializationDecl *>()) {
@@ -2279,7 +2271,7 @@ VarDecl *VarDecl::getTemplateInstantiationPattern() const {
           break;
         VTPSD = NewVTPSD;
       }
-      return getDefinitionOrSelf<VarDecl>(VTPSD);
+      return VTPSD->getDefinition();
     }
   }
 
@@ -2288,18 +2280,23 @@ VarDecl *VarDecl::getTemplateInstantiationPattern() const {
       VarDecl *VD = getInstantiatedFromStaticDataMember();
       while (auto *NewVD = VD->getInstantiatedFromStaticDataMember())
         VD = NewVD;
-      return getDefinitionOrSelf(VD);
+      return VD->getDefinition();
     }
   }
 
   if (VarTemplateDecl *VarTemplate = getDescribedVarTemplate()) {
+
     while (VarTemplate->getInstantiatedFromMemberTemplate()) {
       if (VarTemplate->isMemberSpecialization())
         break;
       VarTemplate = VarTemplate->getInstantiatedFromMemberTemplate();
     }
 
-    return getDefinitionOrSelf(VarTemplate->getTemplatedDecl());
+    assert((!VarTemplate->getTemplatedDecl() ||
+            !isTemplateInstantiation(getTemplateSpecializationKind())) &&
+           "couldn't find pattern for variable instantiation");
+
+    return VarTemplate->getTemplatedDecl();
   }
   return nullptr;
 }
@@ -3203,12 +3200,9 @@ bool FunctionDecl::isTemplateInstantiation() const {
    
 FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
   // Handle class scope explicit specialization special case.
-  if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization) {
-    if (auto *Spec = getClassScopeSpecializationPattern())
-      return getDefinitionOrSelf(Spec);
-    return nullptr;
-  }
-
+  if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
+    return getClassScopeSpecializationPattern();
+  
   // If this is a generic lambda call operator specialization, its 
   // instantiation pattern is always its primary template's pattern
   // even if its primary template was instantiated from another 
@@ -3220,10 +3214,16 @@ FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
 
   if (isGenericLambdaCallOperatorSpecialization(
           dyn_cast<CXXMethodDecl>(this))) {
-    assert(getPrimaryTemplate() && "not a generic lambda call operator?");
-    return getDefinitionOrSelf(getPrimaryTemplate()->getTemplatedDecl());
+    assert(getPrimaryTemplate() && "A generic lambda specialization must be "
+                                   "generated from a primary call operator "
+                                   "template");
+    assert(getPrimaryTemplate()->getTemplatedDecl()->getBody() &&
+           "A generic lambda call operator template must always have a body - "
+           "even if instantiated from a prototype (i.e. as written) member "
+           "template");
+    return getPrimaryTemplate()->getTemplatedDecl();
   }
-
+  
   if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
     while (Primary->getInstantiatedFromMemberTemplate()) {
       // If we have hit a point where the user provided a specialization of
@@ -3232,14 +3232,11 @@ FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
         break;
       Primary = Primary->getInstantiatedFromMemberTemplate();
     }
-
-    return getDefinitionOrSelf(Primary->getTemplatedDecl());
+    
+    return Primary->getTemplatedDecl();
   } 
-
-  if (auto *MFD = getInstantiatedFromMemberFunction())
-    return getDefinitionOrSelf(MFD);
-
-  return nullptr;
+    
+  return getInstantiatedFromMemberFunction();
 }
 
 FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
@@ -3783,7 +3780,7 @@ EnumDecl *EnumDecl::getTemplateInstantiationPattern() const {
       EnumDecl *ED = getInstantiatedFromMemberEnum();
       while (auto *NewED = ED->getInstantiatedFromMemberEnum())
         ED = NewED;
-      return getDefinitionOrSelf(ED);
+      return ED;
     }
   }
 
index dd8f768c57117dd81a5c1d4945637665a75220ec..2e5cec9c108f1a23f1678961987a075b0344025a 100644 (file)
@@ -1364,13 +1364,6 @@ CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
 }
 
 const CXXRecordDecl *CXXRecordDecl::getTemplateInstantiationPattern() const {
-  auto GetDefinitionOrSelf =
-      [](const CXXRecordDecl *D) -> const CXXRecordDecl * {
-    if (auto *Def = D->getDefinition())
-      return Def;
-    return D;
-  };
-
   // If it's a class template specialization, find the template or partial
   // specialization from which it was instantiated.
   if (auto *TD = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
@@ -1381,7 +1374,7 @@ const CXXRecordDecl *CXXRecordDecl::getTemplateInstantiationPattern() const {
           break;
         CTD = NewCTD;
       }
-      return GetDefinitionOrSelf(CTD->getTemplatedDecl());
+      return CTD->getTemplatedDecl()->getDefinition();
     }
     if (auto *CTPSD =
             From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) {
@@ -1390,7 +1383,7 @@ const CXXRecordDecl *CXXRecordDecl::getTemplateInstantiationPattern() const {
           break;
         CTPSD = NewCTPSD;
       }
-      return GetDefinitionOrSelf(CTPSD);
+      return CTPSD->getDefinition();
     }
   }
 
@@ -1399,7 +1392,7 @@ const CXXRecordDecl *CXXRecordDecl::getTemplateInstantiationPattern() const {
       const CXXRecordDecl *RD = this;
       while (auto *NewRD = RD->getInstantiatedFromMemberClass())
         RD = NewRD;
-      return GetDefinitionOrSelf(RD);
+      return RD->getDefinition();
     }
   }
 
index 0ac3eebcf29bc54d28680a5d1e5fb64ecc49983d..e4d420f36832b6770c1dff5b17b7ad755ea2d36c 100644 (file)
@@ -1326,6 +1326,12 @@ bool Sema::CppLookupName(LookupResult &R, Scope *S) {
   return !R.empty();
 }
 
+/// \brief Find the declaration that a class temploid member specialization was
+/// instantiated from, or the member itself if it is an explicit specialization.
+static Decl *getInstantiatedFrom(Decl *D, MemberSpecializationInfo *MSInfo) {
+  return MSInfo->isExplicitSpecialization() ? D : MSInfo->getInstantiatedFrom();
+}
+
 Module *Sema::getOwningModule(Decl *Entity) {
   // If it's imported, grab its owning module.
   Module *M = Entity->getImportedOwningModule();
@@ -1407,14 +1413,20 @@ static Module *getDefiningModule(Sema &S, Decl *Entity) {
     if (CXXRecordDecl *Pattern = RD->getTemplateInstantiationPattern())
       Entity = Pattern;
   } else if (EnumDecl *ED = dyn_cast<EnumDecl>(Entity)) {
-    if (auto *Pattern = ED->getTemplateInstantiationPattern())
-      Entity = Pattern;
+    if (MemberSpecializationInfo *MSInfo = ED->getMemberSpecializationInfo())
+      Entity = getInstantiatedFrom(ED, MSInfo);
   } else if (VarDecl *VD = dyn_cast<VarDecl>(Entity)) {
-    if (VarDecl *Pattern = VD->getTemplateInstantiationPattern())
-      Entity = Pattern;
+    // FIXME: Map from variable template specializations back to the template.
+    if (MemberSpecializationInfo *MSInfo = VD->getMemberSpecializationInfo())
+      Entity = getInstantiatedFrom(VD, MSInfo);
   }
 
-  return S.getOwningModule(Entity);
+  // Walk up to the containing context. That might also have been instantiated
+  // from a template.
+  DeclContext *Context = Entity->getDeclContext();
+  if (Context->isFileContext())
+    return S.getOwningModule(Entity);
+  return getDefiningModule(S, cast<Decl>(Context));
 }
 
 llvm::DenseSet<Module*> &Sema::getLookupModules() {
index 0f4a39a1892a36149afc89a2699f78746040805f..532cbc8be035326bf2c3c515d9061750ad6a0158 100644 (file)
@@ -14,11 +14,3 @@ struct FriendL {
   template<typename T> friend struct L;
 };
 END
-
-namespace DeferredLookup {
-  template<typename T, typename U = T> using X = U;
-  template<typename T> void f() { (void) X<T>(); }
-  template<typename T> int n = X<T>();
-  template<typename T> struct S { X<T> xt; enum E : int; };
-  template<typename T> enum S<T>::E : int { a = X<T>() };
-}
index 1d31592fd1fa4221717b713b01bfedc150bc0894..9d16cbf43420cc9788e43d1c1e40e95be081e24b 100644 (file)
@@ -44,18 +44,3 @@ H<> h; // expected-error {{default argument of 'H' must be imported from module
 I<> i;
 L<> *l;
 END
-
-namespace DeferredLookup {
-  template<typename T, typename U = T> using X = U;
-  template<typename T> void f() { (void) X<T>(); }
-  template<typename T> int n = X<T>(); // expected-warning {{extension}}
-  template<typename T> struct S { X<T> xt; enum E : int; };
-  template<typename T> enum S<T>::E : int { a = X<T>() };
-
-  void test() {
-    f<int>();
-    n<int> = 1;
-    S<int> s;
-    S<int>::E e = S<int>::E::a;
-  }
-}