Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1 | //===--- SemaTemplateInstantiateDecl.cpp - C++ Template Decl Instantiation ===/ |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | //===----------------------------------------------------------------------===/ |
| 8 | // |
| 9 | // This file implements C++ template instantiation for declarations. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===/ |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 12 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | aba43bb | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 13 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTContext.h" |
| 15 | #include "clang/AST/DeclTemplate.h" |
| 16 | #include "clang/AST/DeclVisitor.h" |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 17 | #include "clang/AST/DependentDiagnostic.h" |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 18 | #include "clang/AST/Expr.h" |
Douglas Gregor | a88cfbf | 2009-12-12 18:16:41 +0000 | [diff] [blame] | 19 | #include "clang/AST/ExprCXX.h" |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 20 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 21 | #include "clang/Lex/Preprocessor.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "clang/Sema/Lookup.h" |
| 23 | #include "clang/Sema/PrettyDeclStackTrace.h" |
| 24 | #include "clang/Sema/Template.h" |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace clang; |
| 27 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 28 | bool TemplateDeclInstantiator::SubstQualifier(const DeclaratorDecl *OldDecl, |
| 29 | DeclaratorDecl *NewDecl) { |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 30 | if (!OldDecl->getQualifierLoc()) |
| 31 | return false; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 32 | |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 33 | NestedNameSpecifierLoc NewQualifierLoc |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 34 | = SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(), |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 35 | TemplateArgs); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 36 | |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 37 | if (!NewQualifierLoc) |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 38 | return true; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 39 | |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 40 | NewDecl->setQualifierInfo(NewQualifierLoc); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 41 | return false; |
| 42 | } |
| 43 | |
| 44 | bool TemplateDeclInstantiator::SubstQualifier(const TagDecl *OldDecl, |
| 45 | TagDecl *NewDecl) { |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 46 | if (!OldDecl->getQualifierLoc()) |
| 47 | return false; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 48 | |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 49 | NestedNameSpecifierLoc NewQualifierLoc |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 50 | = SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(), |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 51 | TemplateArgs); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 52 | |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 53 | if (!NewQualifierLoc) |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 54 | return true; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 55 | |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 56 | NewDecl->setQualifierInfo(NewQualifierLoc); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 57 | return false; |
| 58 | } |
| 59 | |
DeLesley Hutchins | 7b9ff0c | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 60 | // Include attribute instantiation code. |
| 61 | #include "clang/Sema/AttrTemplateInstantiate.inc" |
| 62 | |
John McCall | 1d8d1cc | 2010-08-01 02:01:53 +0000 | [diff] [blame] | 63 | void Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs, |
DeLesley Hutchins | 23323e0 | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 64 | const Decl *Tmpl, Decl *New, |
| 65 | LateInstantiatedAttrVec *LateAttrs, |
| 66 | LocalInstantiationScope *OuterMostScope) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 67 | for (AttrVec::const_iterator i = Tmpl->attr_begin(), e = Tmpl->attr_end(); |
| 68 | i != e; ++i) { |
| 69 | const Attr *TmplAttr = *i; |
DeLesley Hutchins | 23323e0 | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 70 | |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 71 | // FIXME: This should be generalized to more than just the AlignedAttr. |
| 72 | if (const AlignedAttr *Aligned = dyn_cast<AlignedAttr>(TmplAttr)) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 73 | if (Aligned->isAlignmentDependent()) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 74 | if (Aligned->isAlignmentExpr()) { |
Richard Smith | f6702a3 | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 75 | // The alignment expression is a constant expression. |
| 76 | EnterExpressionEvaluationContext Unevaluated(*this, |
| 77 | Sema::ConstantEvaluated); |
| 78 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 79 | ExprResult Result = SubstExpr(Aligned->getAlignmentExpr(), |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 80 | TemplateArgs); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 81 | if (!Result.isInvalid()) |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 82 | AddAlignedAttr(Aligned->getLocation(), New, Result.takeAs<Expr>(), |
Richard Smith | 8f3aacc | 2013-01-29 04:21:28 +0000 | [diff] [blame^] | 83 | Aligned->getIsMSDeclSpec(), |
| 84 | Aligned->getSpellingListIndex()); |
Richard Smith | f6702a3 | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 85 | } else { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 86 | TypeSourceInfo *Result = SubstType(Aligned->getAlignmentType(), |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 87 | TemplateArgs, |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 88 | Aligned->getLocation(), |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 89 | DeclarationName()); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 90 | if (Result) |
Richard Smith | 8f3aacc | 2013-01-29 04:21:28 +0000 | [diff] [blame^] | 91 | AddAlignedAttr(Aligned->getLocation(), New, Result, |
| 92 | Aligned->getIsMSDeclSpec(), |
| 93 | Aligned->getSpellingListIndex()); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 94 | } |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 95 | continue; |
| 96 | } |
| 97 | } |
| 98 | |
DeLesley Hutchins | 23323e0 | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 99 | if (TmplAttr->isLateParsed() && LateAttrs) { |
| 100 | // Late parsed attributes must be instantiated and attached after the |
| 101 | // enclosing class has been instantiated. See Sema::InstantiateClass. |
| 102 | LocalInstantiationScope *Saved = 0; |
| 103 | if (CurrentInstantiationScope) |
| 104 | Saved = CurrentInstantiationScope->cloneScopes(OuterMostScope); |
| 105 | LateAttrs->push_back(LateInstantiatedAttribute(TmplAttr, Saved, New)); |
| 106 | } else { |
Benjamin Kramer | 5bbc385 | 2012-02-06 11:13:08 +0000 | [diff] [blame] | 107 | Attr *NewAttr = sema::instantiateTemplateAttribute(TmplAttr, Context, |
| 108 | *this, TemplateArgs); |
Rafael Espindola | 31c195a | 2012-05-15 14:09:55 +0000 | [diff] [blame] | 109 | if (NewAttr) |
| 110 | New->addAttr(NewAttr); |
DeLesley Hutchins | 23323e0 | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 111 | } |
Anders Carlsson | d8fe2d5 | 2009-11-07 06:07:58 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
Douglas Gregor | 4f722be | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 115 | Decl * |
| 116 | TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 117 | llvm_unreachable("Translation units cannot be instantiated"); |
Douglas Gregor | 4f722be | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | Decl * |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 121 | TemplateDeclInstantiator::VisitLabelDecl(LabelDecl *D) { |
| 122 | LabelDecl *Inst = LabelDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 123 | D->getIdentifier()); |
| 124 | Owner->addDecl(Inst); |
| 125 | return Inst; |
| 126 | } |
| 127 | |
| 128 | Decl * |
Douglas Gregor | 4f722be | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 129 | TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 130 | llvm_unreachable("Namespaces cannot be instantiated"); |
Douglas Gregor | 4f722be | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 131 | } |
| 132 | |
John McCall | 3dbd3d5 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 133 | Decl * |
| 134 | TemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { |
| 135 | NamespaceAliasDecl *Inst |
| 136 | = NamespaceAliasDecl::Create(SemaRef.Context, Owner, |
| 137 | D->getNamespaceLoc(), |
| 138 | D->getAliasLoc(), |
Douglas Gregor | 0cfaf6a | 2011-02-25 17:08:07 +0000 | [diff] [blame] | 139 | D->getIdentifier(), |
| 140 | D->getQualifierLoc(), |
John McCall | 3dbd3d5 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 141 | D->getTargetNameLoc(), |
| 142 | D->getNamespace()); |
| 143 | Owner->addDecl(Inst); |
| 144 | return Inst; |
| 145 | } |
| 146 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 147 | Decl *TemplateDeclInstantiator::InstantiateTypedefNameDecl(TypedefNameDecl *D, |
| 148 | bool IsTypeAlias) { |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 149 | bool Invalid = false; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 150 | TypeSourceInfo *DI = D->getTypeSourceInfo(); |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 151 | if (DI->getType()->isInstantiationDependentType() || |
Douglas Gregor | 836adf6 | 2010-05-24 17:22:01 +0000 | [diff] [blame] | 152 | DI->getType()->isVariablyModifiedType()) { |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 153 | DI = SemaRef.SubstType(DI, TemplateArgs, |
| 154 | D->getLocation(), D->getDeclName()); |
| 155 | if (!DI) { |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 156 | Invalid = true; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 157 | DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 158 | } |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 159 | } else { |
| 160 | SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType()); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 161 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 162 | |
Richard Smith | b5b37d1 | 2012-10-23 00:32:41 +0000 | [diff] [blame] | 163 | // HACK: g++ has a bug where it gets the value kind of ?: wrong. |
| 164 | // libstdc++ relies upon this bug in its implementation of common_type. |
| 165 | // If we happen to be processing that implementation, fake up the g++ ?: |
| 166 | // semantics. See LWG issue 2141 for more information on the bug. |
| 167 | const DecltypeType *DT = DI->getType()->getAs<DecltypeType>(); |
| 168 | CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D->getDeclContext()); |
| 169 | if (DT && RD && isa<ConditionalOperator>(DT->getUnderlyingExpr()) && |
| 170 | DT->isReferenceType() && |
| 171 | RD->getEnclosingNamespaceContext() == SemaRef.getStdNamespace() && |
| 172 | RD->getIdentifier() && RD->getIdentifier()->isStr("common_type") && |
| 173 | D->getIdentifier() && D->getIdentifier()->isStr("type") && |
| 174 | SemaRef.getSourceManager().isInSystemHeader(D->getLocStart())) |
| 175 | // Fold it to the (non-reference) type which g++ would have produced. |
| 176 | DI = SemaRef.Context.getTrivialTypeSourceInfo( |
| 177 | DI->getType().getNonReferenceType()); |
| 178 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 179 | // Create the new typedef |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 180 | TypedefNameDecl *Typedef; |
| 181 | if (IsTypeAlias) |
| 182 | Typedef = TypeAliasDecl::Create(SemaRef.Context, Owner, D->getLocStart(), |
| 183 | D->getLocation(), D->getIdentifier(), DI); |
| 184 | else |
| 185 | Typedef = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocStart(), |
| 186 | D->getLocation(), D->getIdentifier(), DI); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 187 | if (Invalid) |
| 188 | Typedef->setInvalidDecl(); |
| 189 | |
John McCall | cde5a40 | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 190 | // If the old typedef was the name for linkage purposes of an anonymous |
| 191 | // tag decl, re-establish that relationship for the new typedef. |
| 192 | if (const TagType *oldTagType = D->getUnderlyingType()->getAs<TagType>()) { |
| 193 | TagDecl *oldTag = oldTagType->getDecl(); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 194 | if (oldTag->getTypedefNameForAnonDecl() == D) { |
John McCall | cde5a40 | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 195 | TagDecl *newTag = DI->getType()->castAs<TagType>()->getDecl(); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 196 | assert(!newTag->getIdentifier() && !newTag->getTypedefNameForAnonDecl()); |
| 197 | newTag->setTypedefNameForAnonDecl(Typedef); |
John McCall | cde5a40 | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 198 | } |
Douglas Gregor | d57a38e | 2010-04-23 16:25:07 +0000 | [diff] [blame] | 199 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 200 | |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 201 | if (TypedefNameDecl *Prev = D->getPreviousDecl()) { |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 202 | NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev, |
| 203 | TemplateArgs); |
Douglas Gregor | b710722 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 204 | if (!InstPrev) |
| 205 | return 0; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 206 | |
Rafael Espindola | 5df37bd | 2011-12-26 22:42:47 +0000 | [diff] [blame] | 207 | TypedefNameDecl *InstPrevTypedef = cast<TypedefNameDecl>(InstPrev); |
| 208 | |
| 209 | // If the typedef types are not identical, reject them. |
| 210 | SemaRef.isIncompatibleTypedef(InstPrevTypedef, Typedef); |
| 211 | |
| 212 | Typedef->setPreviousDeclaration(InstPrevTypedef); |
John McCall | 5126fd0 | 2009-12-30 00:31:22 +0000 | [diff] [blame] | 213 | } |
| 214 | |
John McCall | 1d8d1cc | 2010-08-01 02:01:53 +0000 | [diff] [blame] | 215 | SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef); |
Douglas Gregor | d57a38e | 2010-04-23 16:25:07 +0000 | [diff] [blame] | 216 | |
John McCall | 46460a6 | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 217 | Typedef->setAccess(D->getAccess()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 218 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 219 | return Typedef; |
| 220 | } |
| 221 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 222 | Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) { |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 223 | Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/false); |
| 224 | Owner->addDecl(Typedef); |
| 225 | return Typedef; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | Decl *TemplateDeclInstantiator::VisitTypeAliasDecl(TypeAliasDecl *D) { |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 229 | Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/true); |
| 230 | Owner->addDecl(Typedef); |
| 231 | return Typedef; |
| 232 | } |
| 233 | |
| 234 | Decl * |
| 235 | TemplateDeclInstantiator::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) { |
| 236 | // Create a local instantiation scope for this type alias template, which |
| 237 | // will contain the instantiations of the template parameters. |
| 238 | LocalInstantiationScope Scope(SemaRef); |
| 239 | |
| 240 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 241 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 242 | if (!InstParams) |
| 243 | return 0; |
| 244 | |
| 245 | TypeAliasDecl *Pattern = D->getTemplatedDecl(); |
| 246 | |
| 247 | TypeAliasTemplateDecl *PrevAliasTemplate = 0; |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 248 | if (Pattern->getPreviousDecl()) { |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 249 | DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName()); |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 250 | if (!Found.empty()) { |
| 251 | PrevAliasTemplate = dyn_cast<TypeAliasTemplateDecl>(Found.front()); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
| 255 | TypeAliasDecl *AliasInst = cast_or_null<TypeAliasDecl>( |
| 256 | InstantiateTypedefNameDecl(Pattern, /*IsTypeAlias=*/true)); |
| 257 | if (!AliasInst) |
| 258 | return 0; |
| 259 | |
| 260 | TypeAliasTemplateDecl *Inst |
| 261 | = TypeAliasTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 262 | D->getDeclName(), InstParams, AliasInst); |
| 263 | if (PrevAliasTemplate) |
| 264 | Inst->setPreviousDeclaration(PrevAliasTemplate); |
| 265 | |
| 266 | Inst->setAccess(D->getAccess()); |
| 267 | |
| 268 | if (!PrevAliasTemplate) |
| 269 | Inst->setInstantiatedFromMemberTemplate(D); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 270 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 271 | Owner->addDecl(Inst); |
| 272 | |
| 273 | return Inst; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 276 | Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) { |
Douglas Gregor | 9901c57 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 277 | // If this is the variable for an anonymous struct or union, |
| 278 | // instantiate the anonymous struct/union type first. |
| 279 | if (const RecordType *RecordTy = D->getType()->getAs<RecordType>()) |
| 280 | if (RecordTy->getDecl()->isAnonymousStructOrUnion()) |
| 281 | if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl()))) |
| 282 | return 0; |
| 283 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 284 | // Do substitution on the type of the declaration |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 285 | TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(), |
John McCall | 0a5fa06 | 2009-10-21 02:39:02 +0000 | [diff] [blame] | 286 | TemplateArgs, |
| 287 | D->getTypeSpecStartLoc(), |
| 288 | D->getDeclName()); |
| 289 | if (!DI) |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 290 | return 0; |
| 291 | |
Douglas Gregor | c6dbc3f | 2010-09-12 07:37:24 +0000 | [diff] [blame] | 292 | if (DI->getType()->isFunctionType()) { |
| 293 | SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function) |
| 294 | << D->isStaticDataMember() << DI->getType(); |
| 295 | return 0; |
| 296 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 297 | |
Douglas Gregor | b9f1b8d | 2009-05-15 00:01:03 +0000 | [diff] [blame] | 298 | // Build the instantiated declaration |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 299 | VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 300 | D->getInnerLocStart(), |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 301 | D->getLocation(), D->getIdentifier(), |
John McCall | 0a5fa06 | 2009-10-21 02:39:02 +0000 | [diff] [blame] | 302 | DI->getType(), DI, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 303 | D->getStorageClass(), |
| 304 | D->getStorageClassAsWritten()); |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 305 | Var->setThreadSpecified(D->isThreadSpecified()); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 306 | Var->setInitStyle(D->getInitStyle()); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 307 | Var->setCXXForRangeDecl(D->isCXXForRangeDecl()); |
Richard Smith | 796c1a1 | 2012-01-19 22:46:17 +0000 | [diff] [blame] | 308 | Var->setConstexpr(D->isConstexpr()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 309 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 310 | // Substitute the nested name specifier, if any. |
| 311 | if (SubstQualifier(D, Var)) |
| 312 | return 0; |
| 313 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 314 | // If we are instantiating a static data member defined |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 315 | // out-of-line, the instantiation will have the same lexical |
| 316 | // context (which will be a namespace scope) as the template. |
| 317 | if (D->isOutOfLine()) |
| 318 | Var->setLexicalDeclContext(D->getLexicalDeclContext()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 319 | |
John McCall | 46460a6 | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 320 | Var->setAccess(D->getAccess()); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 321 | |
Argyrios Kyrtzidis | 6b6b42a | 2011-04-19 19:51:10 +0000 | [diff] [blame] | 322 | if (!D->isStaticDataMember()) { |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 323 | Var->setUsed(D->isUsed(false)); |
Argyrios Kyrtzidis | 6b6b42a | 2011-04-19 19:51:10 +0000 | [diff] [blame] | 324 | Var->setReferenced(D->isReferenced()); |
| 325 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 326 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 327 | // FIXME: In theory, we could have a previous declaration for variables that |
| 328 | // are not static data members. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 329 | // FIXME: having to fake up a LookupResult is dumb. |
| 330 | LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(), |
Douglas Gregor | 449d0a8 | 2010-03-01 19:11:54 +0000 | [diff] [blame] | 331 | Sema::LookupOrdinaryName, Sema::ForRedeclaration); |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 332 | if (D->isStaticDataMember()) |
| 333 | SemaRef.LookupQualifiedName(Previous, Owner, false); |
Douglas Gregor | 9aab9c4 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 334 | |
| 335 | // In ARC, infer 'retaining' for variables of retainable type. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 336 | if (SemaRef.getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | 9aab9c4 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 337 | SemaRef.inferObjCARCLifetime(Var)) |
| 338 | Var->setInvalidDecl(); |
| 339 | |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 340 | SemaRef.CheckVariableDeclaration(Var, Previous); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 341 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 342 | if (D->isOutOfLine()) { |
Richard Smith | 3e9ea0b | 2011-12-21 00:25:33 +0000 | [diff] [blame] | 343 | D->getLexicalDeclContext()->addDecl(Var); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 344 | Owner->makeDeclVisibleInContext(Var); |
| 345 | } else { |
| 346 | Owner->addDecl(Var); |
Douglas Gregor | f7d72f5 | 2010-05-03 20:22:41 +0000 | [diff] [blame] | 347 | if (Owner->isFunctionOrMethod()) |
| 348 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 349 | } |
DeLesley Hutchins | dd5756c | 2012-02-16 17:30:51 +0000 | [diff] [blame] | 350 | SemaRef.InstantiateAttrs(TemplateArgs, D, Var, LateAttrs, StartingScope); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 351 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 352 | // Link instantiations of static data members back to the template from |
| 353 | // which they were instantiated. |
| 354 | if (Var->isStaticDataMember()) |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 355 | SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D, |
Douglas Gregor | cf3293e | 2009-11-01 20:32:48 +0000 | [diff] [blame] | 356 | TSK_ImplicitInstantiation); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 357 | |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 358 | if (Var->getAnyInitializer()) { |
| 359 | // We already have an initializer in the class. |
| 360 | } else if (D->getInit()) { |
Douglas Gregor | 1f5f3a4 | 2009-12-03 17:10:37 +0000 | [diff] [blame] | 361 | if (Var->isStaticDataMember() && !D->isOutOfLine()) |
Richard Smith | adb1d4c | 2012-07-22 23:45:10 +0000 | [diff] [blame] | 362 | SemaRef.PushExpressionEvaluationContext(Sema::ConstantEvaluated, D); |
Douglas Gregor | 1f5f3a4 | 2009-12-03 17:10:37 +0000 | [diff] [blame] | 363 | else |
Richard Smith | adb1d4c | 2012-07-22 23:45:10 +0000 | [diff] [blame] | 364 | SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated, D); |
Douglas Gregor | 1f5f3a4 | 2009-12-03 17:10:37 +0000 | [diff] [blame] | 365 | |
Douglas Gregor | 6b98b2e | 2010-03-02 07:38:39 +0000 | [diff] [blame] | 366 | // Instantiate the initializer. |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 367 | ExprResult Init = SemaRef.SubstInitializer(D->getInit(), TemplateArgs, |
| 368 | D->getInitStyle() == VarDecl::CallInit); |
| 369 | if (!Init.isInvalid()) { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 370 | bool TypeMayContainAuto = true; |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 371 | if (Init.get()) { |
| 372 | bool DirectInit = D->isDirectInit(); |
| 373 | SemaRef.AddInitializerToDecl(Var, Init.take(), DirectInit, |
| 374 | TypeMayContainAuto); |
| 375 | } else |
Eli Friedman | 6aeaa60 | 2012-01-05 22:34:08 +0000 | [diff] [blame] | 376 | SemaRef.ActOnUninitializedDecl(Var, TypeMayContainAuto); |
Douglas Gregor | 6eef519 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 377 | } else { |
Douglas Gregor | 6b98b2e | 2010-03-02 07:38:39 +0000 | [diff] [blame] | 378 | // FIXME: Not too happy about invalidating the declaration |
| 379 | // because of a bogus initializer. |
| 380 | Var->setInvalidDecl(); |
Douglas Gregor | 6eef519 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 381 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 382 | |
Douglas Gregor | 1f5f3a4 | 2009-12-03 17:10:37 +0000 | [diff] [blame] | 383 | SemaRef.PopExpressionEvaluationContext(); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 384 | } else if ((!Var->isStaticDataMember() || Var->isOutOfLine()) && |
| 385 | !Var->isCXXForRangeDecl()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 386 | SemaRef.ActOnUninitializedDecl(Var, false); |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 387 | |
Richard Smith | e3499ca | 2011-06-21 23:42:09 +0000 | [diff] [blame] | 388 | // Diagnose unused local variables with dependent types, where the diagnostic |
| 389 | // will have been deferred. |
| 390 | if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed() && |
| 391 | D->getType()->isDependentType()) |
Douglas Gregor | 5764f61 | 2010-05-08 23:05:03 +0000 | [diff] [blame] | 392 | SemaRef.DiagnoseUnusedDecl(Var); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 393 | |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 394 | return Var; |
| 395 | } |
| 396 | |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 397 | Decl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) { |
| 398 | AccessSpecDecl* AD |
| 399 | = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner, |
| 400 | D->getAccessSpecifierLoc(), D->getColonLoc()); |
| 401 | Owner->addHiddenDecl(AD); |
| 402 | return AD; |
| 403 | } |
| 404 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 405 | Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) { |
| 406 | bool Invalid = false; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 407 | TypeSourceInfo *DI = D->getTypeSourceInfo(); |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 408 | if (DI->getType()->isInstantiationDependentType() || |
Douglas Gregor | 836adf6 | 2010-05-24 17:22:01 +0000 | [diff] [blame] | 409 | DI->getType()->isVariablyModifiedType()) { |
John McCall | 07fb6be | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 410 | DI = SemaRef.SubstType(DI, TemplateArgs, |
| 411 | D->getLocation(), D->getDeclName()); |
| 412 | if (!DI) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 413 | DI = D->getTypeSourceInfo(); |
John McCall | 07fb6be | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 414 | Invalid = true; |
| 415 | } else if (DI->getType()->isFunctionType()) { |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 416 | // C++ [temp.arg.type]p3: |
| 417 | // If a declaration acquires a function type through a type |
| 418 | // dependent on a template-parameter and this causes a |
| 419 | // declaration that does not use the syntactic form of a |
| 420 | // function declarator to have function type, the program is |
| 421 | // ill-formed. |
| 422 | SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function) |
John McCall | 07fb6be | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 423 | << DI->getType(); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 424 | Invalid = true; |
| 425 | } |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 426 | } else { |
| 427 | SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType()); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | Expr *BitWidth = D->getBitWidth(); |
| 431 | if (Invalid) |
| 432 | BitWidth = 0; |
| 433 | else if (BitWidth) { |
Richard Smith | f6702a3 | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 434 | // The bit-width expression is a constant expression. |
| 435 | EnterExpressionEvaluationContext Unevaluated(SemaRef, |
| 436 | Sema::ConstantEvaluated); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 437 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 438 | ExprResult InstantiatedBitWidth |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 439 | = SemaRef.SubstExpr(BitWidth, TemplateArgs); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 440 | if (InstantiatedBitWidth.isInvalid()) { |
| 441 | Invalid = true; |
| 442 | BitWidth = 0; |
| 443 | } else |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 444 | BitWidth = InstantiatedBitWidth.takeAs<Expr>(); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 445 | } |
| 446 | |
John McCall | 07fb6be | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 447 | FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), |
| 448 | DI->getType(), DI, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 449 | cast<RecordDecl>(Owner), |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 450 | D->getLocation(), |
| 451 | D->isMutable(), |
| 452 | BitWidth, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 453 | D->getInClassInitStyle(), |
Richard Smith | 703b601 | 2012-05-23 04:22:22 +0000 | [diff] [blame] | 454 | D->getInnerLocStart(), |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 455 | D->getAccess(), |
| 456 | 0); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 457 | if (!Field) { |
| 458 | cast<Decl>(Owner)->setInvalidDecl(); |
Anders Carlsson | f4b5f5c | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 459 | return 0; |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 460 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 461 | |
DeLesley Hutchins | 23323e0 | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 462 | SemaRef.InstantiateAttrs(TemplateArgs, D, Field, LateAttrs, StartingScope); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 463 | |
Anders Carlsson | f4b5f5c | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 464 | if (Invalid) |
| 465 | Field->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 466 | |
Anders Carlsson | f4b5f5c | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 467 | if (!Field->getDeclName()) { |
| 468 | // Keep track of where this decl came from. |
| 469 | SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 470 | } |
Douglas Gregor | 9901c57 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 471 | if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) { |
| 472 | if (Parent->isAnonymousStructOrUnion() && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 473 | Parent->getRedeclContext()->isFunctionOrMethod()) |
Douglas Gregor | 9901c57 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 474 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 475 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 476 | |
Anders Carlsson | f4b5f5c | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 477 | Field->setImplicit(D->isImplicit()); |
John McCall | 46460a6 | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 478 | Field->setAccess(D->getAccess()); |
Anders Carlsson | f4b5f5c | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 479 | Owner->addDecl(Field); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 480 | |
| 481 | return Field; |
| 482 | } |
| 483 | |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 484 | Decl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) { |
| 485 | NamedDecl **NamedChain = |
| 486 | new (SemaRef.Context)NamedDecl*[D->getChainingSize()]; |
| 487 | |
| 488 | int i = 0; |
| 489 | for (IndirectFieldDecl::chain_iterator PI = |
| 490 | D->chain_begin(), PE = D->chain_end(); |
Douglas Gregor | b710722 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 491 | PI != PE; ++PI) { |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 492 | NamedDecl *Next = SemaRef.FindInstantiatedDecl(D->getLocation(), *PI, |
Douglas Gregor | b710722 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 493 | TemplateArgs); |
| 494 | if (!Next) |
| 495 | return 0; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 496 | |
Douglas Gregor | b710722 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 497 | NamedChain[i++] = Next; |
| 498 | } |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 499 | |
Francois Pichet | 40e1775 | 2010-12-09 10:07:54 +0000 | [diff] [blame] | 500 | QualType T = cast<FieldDecl>(NamedChain[i-1])->getType(); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 501 | IndirectFieldDecl* IndirectField |
| 502 | = IndirectFieldDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
Francois Pichet | 40e1775 | 2010-12-09 10:07:54 +0000 | [diff] [blame] | 503 | D->getIdentifier(), T, |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 504 | NamedChain, D->getChainingSize()); |
| 505 | |
| 506 | |
| 507 | IndirectField->setImplicit(D->isImplicit()); |
| 508 | IndirectField->setAccess(D->getAccess()); |
| 509 | Owner->addDecl(IndirectField); |
| 510 | return IndirectField; |
| 511 | } |
| 512 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 513 | Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 514 | // Handle friend type expressions by simply substituting template |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 515 | // parameters into the pattern type and checking the result. |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 516 | if (TypeSourceInfo *Ty = D->getFriendType()) { |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 517 | TypeSourceInfo *InstTy; |
| 518 | // If this is an unsupported friend, don't bother substituting template |
| 519 | // arguments into it. The actual type referred to won't be used by any |
| 520 | // parts of Clang, and may not be valid for instantiating. Just use the |
| 521 | // same info for the instantiated friend. |
| 522 | if (D->isUnsupportedFriend()) { |
| 523 | InstTy = Ty; |
| 524 | } else { |
| 525 | InstTy = SemaRef.SubstType(Ty, TemplateArgs, |
| 526 | D->getLocation(), DeclarationName()); |
| 527 | } |
| 528 | if (!InstTy) |
Douglas Gregor | 7557a13 | 2009-12-24 20:56:24 +0000 | [diff] [blame] | 529 | return 0; |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 530 | |
Richard Smith | d6f80da | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 531 | FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getLocStart(), |
Abramo Bagnara | 0216df8 | 2011-10-29 20:52:52 +0000 | [diff] [blame] | 532 | D->getFriendLoc(), InstTy); |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 533 | if (!FD) |
| 534 | return 0; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 535 | |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 536 | FD->setAccess(AS_public); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 537 | FD->setUnsupportedFriend(D->isUnsupportedFriend()); |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 538 | Owner->addDecl(FD); |
| 539 | return FD; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 542 | NamedDecl *ND = D->getFriendDecl(); |
| 543 | assert(ND && "friend decl must be a decl or a type!"); |
| 544 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 545 | // All of the Visit implementations for the various potential friend |
| 546 | // declarations have to be carefully written to work for friend |
| 547 | // objects, with the most important detail being that the target |
| 548 | // decl should almost certainly not be placed in Owner. |
| 549 | Decl *NewND = Visit(ND); |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 550 | if (!NewND) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 551 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 552 | FriendDecl *FD = |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 553 | FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 554 | cast<NamedDecl>(NewND), D->getFriendLoc()); |
John McCall | 5fee110 | 2009-08-29 03:50:18 +0000 | [diff] [blame] | 555 | FD->setAccess(AS_public); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 556 | FD->setUnsupportedFriend(D->isUnsupportedFriend()); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 557 | Owner->addDecl(FD); |
| 558 | return FD; |
John McCall | fd810b1 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 561 | Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) { |
| 562 | Expr *AssertExpr = D->getAssertExpr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 563 | |
Richard Smith | f6702a3 | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 564 | // The expression in a static assertion is a constant expression. |
| 565 | EnterExpressionEvaluationContext Unevaluated(SemaRef, |
| 566 | Sema::ConstantEvaluated); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 567 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 568 | ExprResult InstantiatedAssertExpr |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 569 | = SemaRef.SubstExpr(AssertExpr, TemplateArgs); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 570 | if (InstantiatedAssertExpr.isInvalid()) |
| 571 | return 0; |
| 572 | |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 573 | return SemaRef.BuildStaticAssertDeclaration(D->getLocation(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 574 | InstantiatedAssertExpr.get(), |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 575 | D->getMessage(), |
| 576 | D->getRParenLoc(), |
| 577 | D->isFailed()); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) { |
Richard Smith | 38f0df3 | 2012-03-26 04:58:10 +0000 | [diff] [blame] | 581 | EnumDecl *PrevDecl = 0; |
| 582 | if (D->getPreviousDecl()) { |
| 583 | NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(), |
| 584 | D->getPreviousDecl(), |
| 585 | TemplateArgs); |
| 586 | if (!Prev) return 0; |
| 587 | PrevDecl = cast<EnumDecl>(Prev); |
| 588 | } |
| 589 | |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 590 | EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner, D->getLocStart(), |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 591 | D->getLocation(), D->getIdentifier(), |
Richard Smith | 38f0df3 | 2012-03-26 04:58:10 +0000 | [diff] [blame] | 592 | PrevDecl, D->isScoped(), |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 593 | D->isScopedUsingClassTag(), D->isFixed()); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 594 | if (D->isFixed()) { |
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 595 | if (TypeSourceInfo *TI = D->getIntegerTypeSourceInfo()) { |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 596 | // If we have type source information for the underlying type, it means it |
| 597 | // has been explicitly set by the user. Perform substitution on it before |
| 598 | // moving on. |
| 599 | SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc(); |
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 600 | TypeSourceInfo *NewTI = SemaRef.SubstType(TI, TemplateArgs, UnderlyingLoc, |
| 601 | DeclarationName()); |
| 602 | if (!NewTI || SemaRef.CheckEnumUnderlyingType(NewTI)) |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 603 | Enum->setIntegerType(SemaRef.Context.IntTy); |
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 604 | else |
| 605 | Enum->setIntegerTypeSourceInfo(NewTI); |
| 606 | } else { |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 607 | assert(!D->getIntegerType()->isDependentType() |
| 608 | && "Dependent type without type source info"); |
| 609 | Enum->setIntegerType(D->getIntegerType()); |
| 610 | } |
| 611 | } |
| 612 | |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 613 | SemaRef.InstantiateAttrs(TemplateArgs, D, Enum); |
| 614 | |
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 615 | Enum->setInstantiationOfMemberEnum(D, TSK_ImplicitInstantiation); |
Douglas Gregor | 06c0fec | 2009-03-25 22:00:53 +0000 | [diff] [blame] | 616 | Enum->setAccess(D->getAccess()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 617 | if (SubstQualifier(D, Enum)) return 0; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 618 | Owner->addDecl(Enum); |
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 619 | |
Richard Smith | 4ca93d9 | 2012-03-26 04:08:46 +0000 | [diff] [blame] | 620 | EnumDecl *Def = D->getDefinition(); |
| 621 | if (Def && Def != D) { |
| 622 | // If this is an out-of-line definition of an enum member template, check |
| 623 | // that the underlying types match in the instantiation of both |
| 624 | // declarations. |
| 625 | if (TypeSourceInfo *TI = Def->getIntegerTypeSourceInfo()) { |
| 626 | SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc(); |
| 627 | QualType DefnUnderlying = |
| 628 | SemaRef.SubstType(TI->getType(), TemplateArgs, |
| 629 | UnderlyingLoc, DeclarationName()); |
| 630 | SemaRef.CheckEnumRedeclaration(Def->getLocation(), Def->isScoped(), |
| 631 | DefnUnderlying, Enum); |
| 632 | } |
| 633 | } |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 634 | |
Douglas Gregor | 96084f1 | 2010-03-01 19:00:07 +0000 | [diff] [blame] | 635 | if (D->getDeclContext()->isFunctionOrMethod()) |
| 636 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 637 | |
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 638 | // C++11 [temp.inst]p1: The implicit instantiation of a class template |
| 639 | // specialization causes the implicit instantiation of the declarations, but |
| 640 | // not the definitions of scoped member enumerations. |
| 641 | // FIXME: There appears to be no wording for what happens for an enum defined |
Richard Smith | 38f0df3 | 2012-03-26 04:58:10 +0000 | [diff] [blame] | 642 | // within a block scope, but we treat that much like a member template. Only |
| 643 | // instantiate the definition when visiting the definition in that case, since |
| 644 | // we will visit all redeclarations. |
| 645 | if (!Enum->isScoped() && Def && |
| 646 | (!D->getDeclContext()->isFunctionOrMethod() || D->isCompleteDefinition())) |
Richard Smith | 4ca93d9 | 2012-03-26 04:08:46 +0000 | [diff] [blame] | 647 | InstantiateEnumDefinition(Enum, Def); |
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 648 | |
| 649 | return Enum; |
| 650 | } |
| 651 | |
| 652 | void TemplateDeclInstantiator::InstantiateEnumDefinition( |
| 653 | EnumDecl *Enum, EnumDecl *Pattern) { |
| 654 | Enum->startDefinition(); |
| 655 | |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 656 | // Update the location to refer to the definition. |
| 657 | Enum->setLocation(Pattern->getLocation()); |
| 658 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 659 | SmallVector<Decl*, 4> Enumerators; |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 660 | |
| 661 | EnumConstantDecl *LastEnumConst = 0; |
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 662 | for (EnumDecl::enumerator_iterator EC = Pattern->enumerator_begin(), |
| 663 | ECEnd = Pattern->enumerator_end(); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 664 | EC != ECEnd; ++EC) { |
| 665 | // The specified value for the enumerator. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 666 | ExprResult Value = SemaRef.Owned((Expr *)0); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 667 | if (Expr *UninstValue = EC->getInitExpr()) { |
Richard Smith | f6702a3 | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 668 | // The enumerator's value expression is a constant expression. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 669 | EnterExpressionEvaluationContext Unevaluated(SemaRef, |
Richard Smith | f6702a3 | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 670 | Sema::ConstantEvaluated); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 671 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 672 | Value = SemaRef.SubstExpr(UninstValue, TemplateArgs); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 673 | } |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 674 | |
| 675 | // Drop the initial value and continue. |
| 676 | bool isInvalid = false; |
| 677 | if (Value.isInvalid()) { |
| 678 | Value = SemaRef.Owned((Expr *)0); |
| 679 | isInvalid = true; |
| 680 | } |
| 681 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 682 | EnumConstantDecl *EnumConst |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 683 | = SemaRef.CheckEnumConstant(Enum, LastEnumConst, |
| 684 | EC->getLocation(), EC->getIdentifier(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 685 | Value.get()); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 686 | |
| 687 | if (isInvalid) { |
| 688 | if (EnumConst) |
| 689 | EnumConst->setInvalidDecl(); |
| 690 | Enum->setInvalidDecl(); |
| 691 | } |
| 692 | |
| 693 | if (EnumConst) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 694 | SemaRef.InstantiateAttrs(TemplateArgs, *EC, EnumConst); |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 695 | |
John McCall | 3b85ecf | 2010-01-23 22:37:59 +0000 | [diff] [blame] | 696 | EnumConst->setAccess(Enum->getAccess()); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 697 | Enum->addDecl(EnumConst); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 698 | Enumerators.push_back(EnumConst); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 699 | LastEnumConst = EnumConst; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 700 | |
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 701 | if (Pattern->getDeclContext()->isFunctionOrMethod() && |
| 702 | !Enum->isScoped()) { |
Douglas Gregor | 96084f1 | 2010-03-01 19:00:07 +0000 | [diff] [blame] | 703 | // If the enumeration is within a function or method, record the enum |
| 704 | // constant as a local. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 705 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst); |
Douglas Gregor | 96084f1 | 2010-03-01 19:00:07 +0000 | [diff] [blame] | 706 | } |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 707 | } |
| 708 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 709 | |
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 710 | // FIXME: Fixup LBraceLoc |
| 711 | SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), |
| 712 | Enum->getRBraceLoc(), Enum, |
Eli Friedman | de7a0fc | 2010-08-15 02:27:09 +0000 | [diff] [blame] | 713 | Enumerators.data(), Enumerators.size(), |
Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 714 | 0, 0); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 715 | } |
| 716 | |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 717 | Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 718 | llvm_unreachable("EnumConstantDecls can only occur within EnumDecls."); |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 719 | } |
| 720 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 721 | Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 722 | bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None); |
| 723 | |
Douglas Gregor | 550d9b2 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 724 | // Create a local instantiation scope for this class template, which |
| 725 | // will contain the instantiations of the template parameters. |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 726 | LocalInstantiationScope Scope(SemaRef); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 727 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 728 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 729 | if (!InstParams) |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 730 | return NULL; |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 731 | |
| 732 | CXXRecordDecl *Pattern = D->getTemplatedDecl(); |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 733 | |
| 734 | // Instantiate the qualifier. We have to do this first in case |
| 735 | // we're a friend declaration, because if we are then we need to put |
| 736 | // the new declaration in the appropriate context. |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 737 | NestedNameSpecifierLoc QualifierLoc = Pattern->getQualifierLoc(); |
| 738 | if (QualifierLoc) { |
| 739 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
| 740 | TemplateArgs); |
| 741 | if (!QualifierLoc) |
| 742 | return 0; |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | CXXRecordDecl *PrevDecl = 0; |
| 746 | ClassTemplateDecl *PrevClassTemplate = 0; |
| 747 | |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 748 | if (!isFriend && Pattern->getPreviousDecl()) { |
Nick Lewycky | 37574f5 | 2010-11-08 23:29:42 +0000 | [diff] [blame] | 749 | DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName()); |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 750 | if (!Found.empty()) { |
| 751 | PrevClassTemplate = dyn_cast<ClassTemplateDecl>(Found.front()); |
Nick Lewycky | 37574f5 | 2010-11-08 23:29:42 +0000 | [diff] [blame] | 752 | if (PrevClassTemplate) |
| 753 | PrevDecl = PrevClassTemplate->getTemplatedDecl(); |
| 754 | } |
| 755 | } |
| 756 | |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 757 | // If this isn't a friend, then it's a member template, in which |
| 758 | // case we just want to build the instantiation in the |
| 759 | // specialization. If it is a friend, we want to build it in |
| 760 | // the appropriate context. |
| 761 | DeclContext *DC = Owner; |
| 762 | if (isFriend) { |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 763 | if (QualifierLoc) { |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 764 | CXXScopeSpec SS; |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 765 | SS.Adopt(QualifierLoc); |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 766 | DC = SemaRef.computeDeclContext(SS); |
| 767 | if (!DC) return 0; |
| 768 | } else { |
| 769 | DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(), |
| 770 | Pattern->getDeclContext(), |
| 771 | TemplateArgs); |
| 772 | } |
| 773 | |
| 774 | // Look for a previous declaration of the template in the owning |
| 775 | // context. |
| 776 | LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(), |
| 777 | Sema::LookupOrdinaryName, Sema::ForRedeclaration); |
| 778 | SemaRef.LookupQualifiedName(R, DC); |
| 779 | |
| 780 | if (R.isSingleResult()) { |
| 781 | PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>(); |
| 782 | if (PrevClassTemplate) |
| 783 | PrevDecl = PrevClassTemplate->getTemplatedDecl(); |
| 784 | } |
| 785 | |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 786 | if (!PrevClassTemplate && QualifierLoc) { |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 787 | SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope) |
Douglas Gregor | 1eabb7d | 2010-03-31 23:17:41 +0000 | [diff] [blame] | 788 | << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 789 | << QualifierLoc.getSourceRange(); |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 790 | return 0; |
| 791 | } |
| 792 | |
Douglas Gregor | c53d0d7 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 793 | bool AdoptedPreviousTemplateParams = false; |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 794 | if (PrevClassTemplate) { |
Douglas Gregor | c53d0d7 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 795 | bool Complain = true; |
| 796 | |
| 797 | // HACK: libstdc++ 4.2.1 contains an ill-formed friend class |
| 798 | // template for struct std::tr1::__detail::_Map_base, where the |
| 799 | // template parameters of the friend declaration don't match the |
| 800 | // template parameters of the original declaration. In this one |
| 801 | // case, we don't complain about the ill-formed friend |
| 802 | // declaration. |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 803 | if (isFriend && Pattern->getIdentifier() && |
Douglas Gregor | c53d0d7 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 804 | Pattern->getIdentifier()->isStr("_Map_base") && |
| 805 | DC->isNamespace() && |
| 806 | cast<NamespaceDecl>(DC)->getIdentifier() && |
| 807 | cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) { |
| 808 | DeclContext *DCParent = DC->getParent(); |
| 809 | if (DCParent->isNamespace() && |
| 810 | cast<NamespaceDecl>(DCParent)->getIdentifier() && |
| 811 | cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) { |
| 812 | DeclContext *DCParent2 = DCParent->getParent(); |
| 813 | if (DCParent2->isNamespace() && |
| 814 | cast<NamespaceDecl>(DCParent2)->getIdentifier() && |
| 815 | cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") && |
| 816 | DCParent2->getParent()->isTranslationUnit()) |
| 817 | Complain = false; |
| 818 | } |
| 819 | } |
| 820 | |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 821 | TemplateParameterList *PrevParams |
| 822 | = PrevClassTemplate->getTemplateParameters(); |
| 823 | |
| 824 | // Make sure the parameter lists match. |
| 825 | if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams, |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 826 | Complain, |
Douglas Gregor | c53d0d7 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 827 | Sema::TPL_TemplateMatch)) { |
| 828 | if (Complain) |
| 829 | return 0; |
| 830 | |
| 831 | AdoptedPreviousTemplateParams = true; |
| 832 | InstParams = PrevParams; |
| 833 | } |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 834 | |
| 835 | // Do some additional validation, then merge default arguments |
| 836 | // from the existing declarations. |
Douglas Gregor | c53d0d7 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 837 | if (!AdoptedPreviousTemplateParams && |
| 838 | SemaRef.CheckTemplateParameterList(InstParams, PrevParams, |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 839 | Sema::TPC_ClassTemplate)) |
| 840 | return 0; |
| 841 | } |
| 842 | } |
| 843 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 844 | CXXRecordDecl *RecordInst |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 845 | = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 846 | Pattern->getLocStart(), Pattern->getLocation(), |
| 847 | Pattern->getIdentifier(), PrevDecl, |
Douglas Gregor | f0510d4 | 2009-10-12 23:11:44 +0000 | [diff] [blame] | 848 | /*DelayTypeCreation=*/true); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 849 | |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 850 | if (QualifierLoc) |
| 851 | RecordInst->setQualifierInfo(QualifierLoc); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 852 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 853 | ClassTemplateDecl *Inst |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 854 | = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(), |
| 855 | D->getIdentifier(), InstParams, RecordInst, |
| 856 | PrevClassTemplate); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 857 | RecordInst->setDescribedClassTemplate(Inst); |
John McCall | ea7390c | 2010-04-08 20:25:50 +0000 | [diff] [blame] | 858 | |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 859 | if (isFriend) { |
John McCall | ea7390c | 2010-04-08 20:25:50 +0000 | [diff] [blame] | 860 | if (PrevClassTemplate) |
| 861 | Inst->setAccess(PrevClassTemplate->getAccess()); |
| 862 | else |
| 863 | Inst->setAccess(D->getAccess()); |
| 864 | |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 865 | Inst->setObjectOfFriendDecl(PrevClassTemplate != 0); |
| 866 | // TODO: do we want to track the instantiation progeny of this |
| 867 | // friend target decl? |
| 868 | } else { |
Douglas Gregor | e8c01bd | 2009-10-30 21:07:27 +0000 | [diff] [blame] | 869 | Inst->setAccess(D->getAccess()); |
Nick Lewycky | 37574f5 | 2010-11-08 23:29:42 +0000 | [diff] [blame] | 870 | if (!PrevClassTemplate) |
| 871 | Inst->setInstantiatedFromMemberTemplate(D); |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 872 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 873 | |
Douglas Gregor | f0510d4 | 2009-10-12 23:11:44 +0000 | [diff] [blame] | 874 | // Trigger creation of the type for the instantiation. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 875 | SemaRef.Context.getInjectedClassNameType(RecordInst, |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 876 | Inst->getInjectedClassNameSpecialization()); |
John McCall | ea7390c | 2010-04-08 20:25:50 +0000 | [diff] [blame] | 877 | |
Douglas Gregor | 259571e | 2009-10-30 22:42:42 +0000 | [diff] [blame] | 878 | // Finish handling of friends. |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 879 | if (isFriend) { |
Richard Smith | 1b7f9cb | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 880 | DC->makeDeclVisibleInContext(Inst); |
Abramo Bagnara | 4c51548 | 2011-11-26 13:33:46 +0000 | [diff] [blame] | 881 | Inst->setLexicalDeclContext(Owner); |
| 882 | RecordInst->setLexicalDeclContext(Owner); |
Douglas Gregor | e8c01bd | 2009-10-30 21:07:27 +0000 | [diff] [blame] | 883 | return Inst; |
Douglas Gregor | 259571e | 2009-10-30 22:42:42 +0000 | [diff] [blame] | 884 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 885 | |
Abramo Bagnara | 4c51548 | 2011-11-26 13:33:46 +0000 | [diff] [blame] | 886 | if (D->isOutOfLine()) { |
| 887 | Inst->setLexicalDeclContext(D->getLexicalDeclContext()); |
| 888 | RecordInst->setLexicalDeclContext(D->getLexicalDeclContext()); |
| 889 | } |
| 890 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 891 | Owner->addDecl(Inst); |
Douglas Gregor | d65587f | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 892 | |
| 893 | if (!PrevClassTemplate) { |
| 894 | // Queue up any out-of-line partial specializations of this member |
| 895 | // class template; the client will force their instantiation once |
| 896 | // the enclosing class has been instantiated. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 897 | SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
Douglas Gregor | d65587f | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 898 | D->getPartialSpecializations(PartialSpecs); |
| 899 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) |
| 900 | if (PartialSpecs[I]->isOutOfLine()) |
| 901 | OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I])); |
| 902 | } |
| 903 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 904 | return Inst; |
| 905 | } |
| 906 | |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 907 | Decl * |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 908 | TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl( |
| 909 | ClassTemplatePartialSpecializationDecl *D) { |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 910 | ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate(); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 911 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 912 | // Lookup the already-instantiated declaration in the instantiation |
| 913 | // of the class template and return that. |
| 914 | DeclContext::lookup_result Found |
| 915 | = Owner->lookup(ClassTemplate->getDeclName()); |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 916 | if (Found.empty()) |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 917 | return 0; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 918 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 919 | ClassTemplateDecl *InstClassTemplate |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 920 | = dyn_cast<ClassTemplateDecl>(Found.front()); |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 921 | if (!InstClassTemplate) |
| 922 | return 0; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 923 | |
Douglas Gregor | d65587f | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 924 | if (ClassTemplatePartialSpecializationDecl *Result |
| 925 | = InstClassTemplate->findPartialSpecInstantiatedFromMember(D)) |
| 926 | return Result; |
| 927 | |
| 928 | return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D); |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | Decl * |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 932 | TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
Douglas Gregor | 550d9b2 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 933 | // Create a local instantiation scope for this function template, which |
| 934 | // will contain the instantiations of the template parameters and then get |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 935 | // merged with the local instantiation scope for the function template |
Douglas Gregor | 550d9b2 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 936 | // itself. |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 937 | LocalInstantiationScope Scope(SemaRef); |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 938 | |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 939 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 940 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 941 | if (!InstParams) |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 942 | return NULL; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 943 | |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 944 | FunctionDecl *Instantiated = 0; |
| 945 | if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl())) |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 946 | Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod, |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 947 | InstParams)); |
| 948 | else |
| 949 | Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl( |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 950 | D->getTemplatedDecl(), |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 951 | InstParams)); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 952 | |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 953 | if (!Instantiated) |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 954 | return 0; |
| 955 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 956 | // Link the instantiated function template declaration to the function |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 957 | // template from which it was instantiated. |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 958 | FunctionTemplateDecl *InstTemplate |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 959 | = Instantiated->getDescribedFunctionTemplate(); |
Douglas Gregor | 37d68185 | 2009-10-12 22:27:17 +0000 | [diff] [blame] | 960 | InstTemplate->setAccess(D->getAccess()); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 961 | assert(InstTemplate && |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 962 | "VisitFunctionDecl/CXXMethodDecl didn't create a template!"); |
John McCall | e976ffe | 2009-12-14 23:19:40 +0000 | [diff] [blame] | 963 | |
John McCall | b1a56e7 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 964 | bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None); |
| 965 | |
John McCall | e976ffe | 2009-12-14 23:19:40 +0000 | [diff] [blame] | 966 | // Link the instantiation back to the pattern *unless* this is a |
| 967 | // non-definition friend declaration. |
| 968 | if (!InstTemplate->getInstantiatedFromMemberTemplate() && |
John McCall | b1a56e7 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 969 | !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition())) |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 970 | InstTemplate->setInstantiatedFromMemberTemplate(D); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 971 | |
John McCall | b1a56e7 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 972 | // Make declarations visible in the appropriate context. |
John McCall | 1f2e1a9 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 973 | if (!isFriend) { |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 974 | Owner->addDecl(InstTemplate); |
John McCall | 1f2e1a9 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 975 | } else if (InstTemplate->getDeclContext()->isRecord() && |
| 976 | !D->getPreviousDecl()) { |
| 977 | SemaRef.CheckFriendAccess(InstTemplate); |
| 978 | } |
John McCall | b1a56e7 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 979 | |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 980 | return InstTemplate; |
| 981 | } |
| 982 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 983 | Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) { |
| 984 | CXXRecordDecl *PrevDecl = 0; |
| 985 | if (D->isInjectedClassName()) |
| 986 | PrevDecl = cast<CXXRecordDecl>(Owner); |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 987 | else if (D->getPreviousDecl()) { |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 988 | NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(), |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 989 | D->getPreviousDecl(), |
John McCall | 6c1c1b8 | 2009-12-15 22:29:06 +0000 | [diff] [blame] | 990 | TemplateArgs); |
| 991 | if (!Prev) return 0; |
| 992 | PrevDecl = cast<CXXRecordDecl>(Prev); |
| 993 | } |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 994 | |
| 995 | CXXRecordDecl *Record |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 996 | = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 997 | D->getLocStart(), D->getLocation(), |
| 998 | D->getIdentifier(), PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 999 | |
| 1000 | // Substitute the nested name specifier, if any. |
| 1001 | if (SubstQualifier(D, Record)) |
| 1002 | return 0; |
| 1003 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1004 | Record->setImplicit(D->isImplicit()); |
Eli Friedman | eaba1af | 2009-08-27 19:11:42 +0000 | [diff] [blame] | 1005 | // FIXME: Check against AS_none is an ugly hack to work around the issue that |
| 1006 | // the tag decls introduced by friend class declarations don't have an access |
| 1007 | // specifier. Remove once this area of the code gets sorted out. |
| 1008 | if (D->getAccess() != AS_none) |
| 1009 | Record->setAccess(D->getAccess()); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1010 | if (!D->isInjectedClassName()) |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1011 | Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1012 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1013 | // If the original function was part of a friend declaration, |
| 1014 | // inherit its namespace state. |
| 1015 | if (Decl::FriendObjectKind FOK = D->getFriendObjectKind()) |
| 1016 | Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared); |
| 1017 | |
Douglas Gregor | 9901c57 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 1018 | // Make sure that anonymous structs and unions are recorded. |
| 1019 | if (D->isAnonymousStructOrUnion()) { |
| 1020 | Record->setAnonymousStructOrUnion(true); |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1021 | if (Record->getDeclContext()->getRedeclContext()->isFunctionOrMethod()) |
Douglas Gregor | 9901c57 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 1022 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record); |
| 1023 | } |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1024 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1025 | Owner->addDecl(Record); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1026 | return Record; |
| 1027 | } |
| 1028 | |
Douglas Gregor | 71074fd | 2012-09-13 21:56:43 +0000 | [diff] [blame] | 1029 | /// \brief Adjust the given function type for an instantiation of the |
| 1030 | /// given declaration, to cope with modifications to the function's type that |
| 1031 | /// aren't reflected in the type-source information. |
| 1032 | /// |
| 1033 | /// \param D The declaration we're instantiating. |
| 1034 | /// \param TInfo The already-instantiated type. |
| 1035 | static QualType adjustFunctionTypeForInstantiation(ASTContext &Context, |
| 1036 | FunctionDecl *D, |
| 1037 | TypeSourceInfo *TInfo) { |
Douglas Gregor | bed51fe | 2012-09-13 22:01:49 +0000 | [diff] [blame] | 1038 | const FunctionProtoType *OrigFunc |
| 1039 | = D->getType()->castAs<FunctionProtoType>(); |
| 1040 | const FunctionProtoType *NewFunc |
| 1041 | = TInfo->getType()->castAs<FunctionProtoType>(); |
| 1042 | if (OrigFunc->getExtInfo() == NewFunc->getExtInfo()) |
| 1043 | return TInfo->getType(); |
| 1044 | |
| 1045 | FunctionProtoType::ExtProtoInfo NewEPI = NewFunc->getExtProtoInfo(); |
| 1046 | NewEPI.ExtInfo = OrigFunc->getExtInfo(); |
| 1047 | return Context.getFunctionType(NewFunc->getResultType(), |
| 1048 | NewFunc->arg_type_begin(), |
| 1049 | NewFunc->getNumArgs(), |
| 1050 | NewEPI); |
Douglas Gregor | 71074fd | 2012-09-13 21:56:43 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1053 | /// Normal class members are of more specific types and therefore |
| 1054 | /// don't make it here. This function serves two purposes: |
| 1055 | /// 1) instantiating function templates |
| 1056 | /// 2) substituting friend declarations |
| 1057 | /// FIXME: preserve function definitions in case #2 |
Douglas Gregor | 7557a13 | 2009-12-24 20:56:24 +0000 | [diff] [blame] | 1058 | Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D, |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1059 | TemplateParameterList *TemplateParams) { |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1060 | // Check whether there is already a function template specialization for |
| 1061 | // this declaration. |
| 1062 | FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1063 | if (FunctionTemplate && !TemplateParams) { |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1064 | std::pair<const TemplateArgument *, unsigned> Innermost |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1065 | = TemplateArgs.getInnermost(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1066 | |
Douglas Gregor | 1e1e972 | 2012-03-28 14:34:23 +0000 | [diff] [blame] | 1067 | void *InsertPos = 0; |
Argyrios Kyrtzidis | 2c853e4 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1068 | FunctionDecl *SpecFunc |
| 1069 | = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second, |
| 1070 | InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1071 | |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1072 | // If we already have a function template specialization, return it. |
Argyrios Kyrtzidis | 2c853e4 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1073 | if (SpecFunc) |
| 1074 | return SpecFunc; |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1075 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1076 | |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1077 | bool isFriend; |
| 1078 | if (FunctionTemplate) |
| 1079 | isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None); |
| 1080 | else |
| 1081 | isFriend = (D->getFriendObjectKind() != Decl::FOK_None); |
| 1082 | |
Douglas Gregor | 79c2278 | 2010-01-16 20:21:20 +0000 | [diff] [blame] | 1083 | bool MergeWithParentScope = (TemplateParams != 0) || |
Douglas Gregor | b212d9a | 2010-05-21 21:25:08 +0000 | [diff] [blame] | 1084 | Owner->isFunctionOrMethod() || |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1085 | !(isa<Decl>(Owner) && |
Douglas Gregor | 79c2278 | 2010-01-16 20:21:20 +0000 | [diff] [blame] | 1086 | cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod()); |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1087 | LocalInstantiationScope Scope(SemaRef, MergeWithParentScope); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1088 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1089 | SmallVector<ParmVarDecl *, 4> Params; |
David Blaikie | 64b4b43 | 2011-11-10 05:42:04 +0000 | [diff] [blame] | 1090 | TypeSourceInfo *TInfo = SubstFunctionType(D, Params); |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 1091 | if (!TInfo) |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1092 | return 0; |
Douglas Gregor | 71074fd | 2012-09-13 21:56:43 +0000 | [diff] [blame] | 1093 | QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo); |
John McCall | fd810b1 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 1094 | |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1095 | NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc(); |
| 1096 | if (QualifierLoc) { |
| 1097 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
| 1098 | TemplateArgs); |
| 1099 | if (!QualifierLoc) |
| 1100 | return 0; |
John McCall | d325daa | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
John McCall | 68b6b87 | 2010-02-06 01:50:47 +0000 | [diff] [blame] | 1103 | // If we're instantiating a local function declaration, put the result |
| 1104 | // in the owner; otherwise we need to find the instantiated context. |
| 1105 | DeclContext *DC; |
| 1106 | if (D->getDeclContext()->isFunctionOrMethod()) |
| 1107 | DC = Owner; |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1108 | else if (isFriend && QualifierLoc) { |
John McCall | d325daa | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1109 | CXXScopeSpec SS; |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1110 | SS.Adopt(QualifierLoc); |
John McCall | d325daa | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1111 | DC = SemaRef.computeDeclContext(SS); |
| 1112 | if (!DC) return 0; |
| 1113 | } else { |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1114 | DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(), |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 1115 | TemplateArgs); |
John McCall | d325daa | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1116 | } |
John McCall | 68b6b87 | 2010-02-06 01:50:47 +0000 | [diff] [blame] | 1117 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1118 | FunctionDecl *Function = |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1119 | FunctionDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(), |
Abramo Bagnara | 635311f | 2012-10-04 21:40:42 +0000 | [diff] [blame] | 1120 | D->getNameInfo(), T, TInfo, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1121 | D->getStorageClass(), D->getStorageClassAsWritten(), |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 1122 | D->isInlineSpecified(), D->hasWrittenPrototype(), |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 1123 | D->isConstexpr()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1124 | |
Richard Smith | d4497dd | 2013-01-25 00:08:28 +0000 | [diff] [blame] | 1125 | if (D->isInlined()) |
| 1126 | Function->setImplicitlyInline(); |
| 1127 | |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1128 | if (QualifierLoc) |
| 1129 | Function->setQualifierInfo(QualifierLoc); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1130 | |
John McCall | b1a56e7 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1131 | DeclContext *LexicalDC = Owner; |
| 1132 | if (!isFriend && D->isOutOfLine()) { |
| 1133 | assert(D->getDeclContext()->isFileContext()); |
| 1134 | LexicalDC = D->getDeclContext(); |
| 1135 | } |
| 1136 | |
| 1137 | Function->setLexicalDeclContext(LexicalDC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1138 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1139 | // Attach the parameters |
Douglas Gregor | 5cbe101 | 2011-07-05 18:30:26 +0000 | [diff] [blame] | 1140 | if (isa<FunctionProtoType>(Function->getType().IgnoreParens())) { |
Douglas Gregor | 1d441ee | 2011-06-22 18:16:25 +0000 | [diff] [blame] | 1141 | // Adopt the already-instantiated parameters into our own context. |
| 1142 | for (unsigned P = 0; P < Params.size(); ++P) |
| 1143 | if (Params[P]) |
| 1144 | Params[P]->setOwningFunction(Function); |
| 1145 | } else { |
| 1146 | // Since we were instantiated via a typedef of a function type, create |
| 1147 | // new parameters. |
| 1148 | const FunctionProtoType *Proto |
| 1149 | = Function->getType()->getAs<FunctionProtoType>(); |
| 1150 | assert(Proto && "No function prototype in template instantiation?"); |
| 1151 | for (FunctionProtoType::arg_type_iterator AI = Proto->arg_type_begin(), |
| 1152 | AE = Proto->arg_type_end(); AI != AE; ++AI) { |
| 1153 | ParmVarDecl *Param |
| 1154 | = SemaRef.BuildParmVarDeclForTypedef(Function, Function->getLocation(), |
| 1155 | *AI); |
| 1156 | Param->setScopeInfo(0, Params.size()); |
| 1157 | Params.push_back(Param); |
| 1158 | } |
| 1159 | } |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 1160 | Function->setParams(Params); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1161 | |
Douglas Gregor | ac7c2c8 | 2010-05-17 16:38:00 +0000 | [diff] [blame] | 1162 | SourceLocation InstantiateAtPOI; |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1163 | if (TemplateParams) { |
| 1164 | // Our resulting instantiation is actually a function template, since we |
| 1165 | // are substituting only the outer template parameters. For example, given |
| 1166 | // |
| 1167 | // template<typename T> |
| 1168 | // struct X { |
| 1169 | // template<typename U> friend void f(T, U); |
| 1170 | // }; |
| 1171 | // |
| 1172 | // X<int> x; |
| 1173 | // |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1174 | // We are instantiating the friend function template "f" within X<int>, |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1175 | // which means substituting int for T, but leaving "f" as a friend function |
| 1176 | // template. |
| 1177 | // Build the function template itself. |
John McCall | d325daa | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1178 | FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC, |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1179 | Function->getLocation(), |
| 1180 | Function->getDeclName(), |
| 1181 | TemplateParams, Function); |
| 1182 | Function->setDescribedFunctionTemplate(FunctionTemplate); |
John McCall | b1a56e7 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1183 | |
| 1184 | FunctionTemplate->setLexicalDeclContext(LexicalDC); |
John McCall | d325daa | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1185 | |
| 1186 | if (isFriend && D->isThisDeclarationADefinition()) { |
| 1187 | // TODO: should we remember this connection regardless of whether |
| 1188 | // the friend declaration provided a body? |
| 1189 | FunctionTemplate->setInstantiatedFromMemberTemplate( |
| 1190 | D->getDescribedFunctionTemplate()); |
| 1191 | } |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1192 | } else if (FunctionTemplate) { |
| 1193 | // Record this function template specialization. |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1194 | std::pair<const TemplateArgument *, unsigned> Innermost |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1195 | = TemplateArgs.getInnermost(); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1196 | Function->setFunctionTemplateSpecialization(FunctionTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1197 | TemplateArgumentList::CreateCopy(SemaRef.Context, |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1198 | Innermost.first, |
| 1199 | Innermost.second), |
Douglas Gregor | 1e1e972 | 2012-03-28 14:34:23 +0000 | [diff] [blame] | 1200 | /*InsertPos=*/0); |
Chandler Carruth | 80f5b16 | 2011-08-18 09:09:59 +0000 | [diff] [blame] | 1201 | } else if (isFriend) { |
| 1202 | // Note, we need this connection even if the friend doesn't have a body. |
| 1203 | // Its body may exist but not have been attached yet due to deferred |
| 1204 | // parsing. |
| 1205 | // FIXME: It might be cleaner to set this when attaching the body to the |
| 1206 | // friend function declaration, however that would require finding all the |
| 1207 | // instantiations and modifying them. |
John McCall | d325daa | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1208 | Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1209 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1210 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1211 | if (InitFunctionInstantiation(Function, D)) |
| 1212 | Function->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1213 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1214 | bool isExplicitSpecialization = false; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1215 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1216 | LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(), |
| 1217 | Sema::LookupOrdinaryName, Sema::ForRedeclaration); |
| 1218 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1219 | if (DependentFunctionTemplateSpecializationInfo *Info |
| 1220 | = D->getDependentSpecializationInfo()) { |
| 1221 | assert(isFriend && "non-friend has dependent specialization info?"); |
| 1222 | |
| 1223 | // This needs to be set now for future sanity. |
| 1224 | Function->setObjectOfFriendDecl(/*HasPrevious*/ true); |
| 1225 | |
| 1226 | // Instantiate the explicit template arguments. |
| 1227 | TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(), |
| 1228 | Info->getRAngleLoc()); |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1229 | if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(), |
| 1230 | ExplicitArgs, TemplateArgs)) |
| 1231 | return 0; |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1232 | |
| 1233 | // Map the candidate templates to their instantiations. |
| 1234 | for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) { |
| 1235 | Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(), |
| 1236 | Info->getTemplate(I), |
| 1237 | TemplateArgs); |
| 1238 | if (!Temp) return 0; |
| 1239 | |
| 1240 | Previous.addDecl(cast<FunctionTemplateDecl>(Temp)); |
| 1241 | } |
| 1242 | |
| 1243 | if (SemaRef.CheckFunctionTemplateSpecialization(Function, |
| 1244 | &ExplicitArgs, |
| 1245 | Previous)) |
| 1246 | Function->setInvalidDecl(); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1247 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1248 | isExplicitSpecialization = true; |
| 1249 | |
| 1250 | } else if (TemplateParams || !FunctionTemplate) { |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1251 | // Look only into the namespace where the friend would be declared to |
| 1252 | // find a previous declaration. This is the innermost enclosing namespace, |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1253 | // as described in ActOnFriendFunctionDecl. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1254 | SemaRef.LookupQualifiedName(Previous, DC); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1255 | |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1256 | // In C++, the previous declaration we find might be a tag type |
| 1257 | // (class or enum). In this case, the new declaration will hide the |
| 1258 | // tag type. Note that this does does not apply if we're declaring a |
| 1259 | // typedef (C++ [dcl.typedef]p4). |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1260 | if (Previous.isSingleTagDecl()) |
| 1261 | Previous.clear(); |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1262 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1263 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1264 | SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous, |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 1265 | isExplicitSpecialization); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1266 | |
John McCall | 76d3264 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 1267 | NamedDecl *PrincipalDecl = (TemplateParams |
| 1268 | ? cast<NamedDecl>(FunctionTemplate) |
| 1269 | : Function); |
| 1270 | |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1271 | // If the original function was part of a friend declaration, |
| 1272 | // inherit its namespace state and add it to the owner. |
John McCall | d325daa | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1273 | if (isFriend) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1274 | NamedDecl *PrevDecl; |
John McCall | 76d3264 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 1275 | if (TemplateParams) |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 1276 | PrevDecl = FunctionTemplate->getPreviousDecl(); |
John McCall | 76d3264 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 1277 | else |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 1278 | PrevDecl = Function->getPreviousDecl(); |
John McCall | 76d3264 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 1279 | |
| 1280 | PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0); |
Richard Smith | 1b7f9cb | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1281 | DC->makeDeclVisibleInContext(PrincipalDecl); |
Gabor Greif | ab297ac | 2010-08-30 21:10:05 +0000 | [diff] [blame] | 1282 | |
Gabor Greif | 77535df | 2010-08-30 22:25:56 +0000 | [diff] [blame] | 1283 | bool queuedInstantiation = false; |
Gabor Greif | ab297ac | 2010-08-30 21:10:05 +0000 | [diff] [blame] | 1284 | |
Richard Smith | 53e5351 | 2011-10-19 00:54:10 +0000 | [diff] [blame] | 1285 | // C++98 [temp.friend]p5: When a function is defined in a friend function |
| 1286 | // declaration in a class template, the function is defined at each |
| 1287 | // instantiation of the class template. The function is defined even if it |
| 1288 | // is never used. |
| 1289 | // C++11 [temp.friend]p4: When a function is defined in a friend function |
| 1290 | // declaration in a class template, the function is instantiated when the |
| 1291 | // function is odr-used. |
| 1292 | // |
| 1293 | // If -Wc++98-compat is enabled, we go through the motions of checking for a |
| 1294 | // redefinition, but don't instantiate the function. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1295 | if ((!SemaRef.getLangOpts().CPlusPlus11 || |
Richard Smith | 53e5351 | 2011-10-19 00:54:10 +0000 | [diff] [blame] | 1296 | SemaRef.Diags.getDiagnosticLevel( |
| 1297 | diag::warn_cxx98_compat_friend_redefinition, |
| 1298 | Function->getLocation()) |
| 1299 | != DiagnosticsEngine::Ignored) && |
Douglas Gregor | 238058c | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1300 | D->isThisDeclarationADefinition()) { |
| 1301 | // Check for a function body. |
| 1302 | const FunctionDecl *Definition = 0; |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 1303 | if (Function->isDefined(Definition) && |
Douglas Gregor | 238058c | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1304 | Definition->getTemplateSpecializationKind() == TSK_Undeclared) { |
Richard Smith | 53e5351 | 2011-10-19 00:54:10 +0000 | [diff] [blame] | 1305 | SemaRef.Diag(Function->getLocation(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1306 | SemaRef.getLangOpts().CPlusPlus11 ? |
Richard Smith | 53e5351 | 2011-10-19 00:54:10 +0000 | [diff] [blame] | 1307 | diag::warn_cxx98_compat_friend_redefinition : |
| 1308 | diag::err_redefinition) << Function->getDeclName(); |
Douglas Gregor | 238058c | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1309 | SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1310 | if (!SemaRef.getLangOpts().CPlusPlus11) |
Richard Smith | 53e5351 | 2011-10-19 00:54:10 +0000 | [diff] [blame] | 1311 | Function->setInvalidDecl(); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1312 | } |
Douglas Gregor | 238058c | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1313 | // Check for redefinitions due to other instantiations of this or |
| 1314 | // a similar friend function. |
| 1315 | else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(), |
| 1316 | REnd = Function->redecls_end(); |
| 1317 | R != REnd; ++R) { |
Gabor Greif | 13a8aff | 2010-08-28 15:42:30 +0000 | [diff] [blame] | 1318 | if (*R == Function) |
| 1319 | continue; |
Gabor Greif | ab297ac | 2010-08-30 21:10:05 +0000 | [diff] [blame] | 1320 | switch (R->getFriendObjectKind()) { |
| 1321 | case Decl::FOK_None: |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1322 | if (!SemaRef.getLangOpts().CPlusPlus11 && |
Richard Smith | 53e5351 | 2011-10-19 00:54:10 +0000 | [diff] [blame] | 1323 | !queuedInstantiation && R->isUsed(false)) { |
Gabor Greif | ab297ac | 2010-08-30 21:10:05 +0000 | [diff] [blame] | 1324 | if (MemberSpecializationInfo *MSInfo |
| 1325 | = Function->getMemberSpecializationInfo()) { |
| 1326 | if (MSInfo->getPointOfInstantiation().isInvalid()) { |
| 1327 | SourceLocation Loc = R->getLocation(); // FIXME |
| 1328 | MSInfo->setPointOfInstantiation(Loc); |
| 1329 | SemaRef.PendingLocalImplicitInstantiations.push_back( |
| 1330 | std::make_pair(Function, Loc)); |
| 1331 | queuedInstantiation = true; |
| 1332 | } |
| 1333 | } |
| 1334 | } |
| 1335 | break; |
| 1336 | default: |
Douglas Gregor | 238058c | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1337 | if (const FunctionDecl *RPattern |
Gabor Greif | 6a557d8 | 2010-08-28 15:46:56 +0000 | [diff] [blame] | 1338 | = R->getTemplateInstantiationPattern()) |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 1339 | if (RPattern->isDefined(RPattern)) { |
Richard Smith | 53e5351 | 2011-10-19 00:54:10 +0000 | [diff] [blame] | 1340 | SemaRef.Diag(Function->getLocation(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1341 | SemaRef.getLangOpts().CPlusPlus11 ? |
Richard Smith | 53e5351 | 2011-10-19 00:54:10 +0000 | [diff] [blame] | 1342 | diag::warn_cxx98_compat_friend_redefinition : |
| 1343 | diag::err_redefinition) |
Douglas Gregor | 238058c | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1344 | << Function->getDeclName(); |
Gabor Greif | 6a557d8 | 2010-08-28 15:46:56 +0000 | [diff] [blame] | 1345 | SemaRef.Diag(R->getLocation(), diag::note_previous_definition); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1346 | if (!SemaRef.getLangOpts().CPlusPlus11) |
Richard Smith | 53e5351 | 2011-10-19 00:54:10 +0000 | [diff] [blame] | 1347 | Function->setInvalidDecl(); |
Douglas Gregor | 238058c | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1348 | break; |
| 1349 | } |
| 1350 | } |
| 1351 | } |
| 1352 | } |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
John McCall | 76d3264 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 1355 | if (Function->isOverloadedOperator() && !DC->isRecord() && |
| 1356 | PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary)) |
| 1357 | PrincipalDecl->setNonMemberOperator(); |
| 1358 | |
Sean Hunt | eb88ae5 | 2011-05-23 21:07:59 +0000 | [diff] [blame] | 1359 | assert(!D->isDefaulted() && "only methods should be defaulted"); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1360 | return Function; |
| 1361 | } |
| 1362 | |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1363 | Decl * |
| 1364 | TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D, |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 1365 | TemplateParameterList *TemplateParams, |
| 1366 | bool IsClassScopeSpecialization) { |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1367 | FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1368 | if (FunctionTemplate && !TemplateParams) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1369 | // We are creating a function template specialization from a function |
| 1370 | // template. Check whether there is already a function template |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1371 | // specialization for this particular set of template arguments. |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1372 | std::pair<const TemplateArgument *, unsigned> Innermost |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1373 | = TemplateArgs.getInnermost(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1374 | |
Douglas Gregor | 1e1e972 | 2012-03-28 14:34:23 +0000 | [diff] [blame] | 1375 | void *InsertPos = 0; |
Argyrios Kyrtzidis | 2c853e4 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1376 | FunctionDecl *SpecFunc |
| 1377 | = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second, |
| 1378 | InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1379 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1380 | // If we already have a function template specialization, return it. |
Argyrios Kyrtzidis | 2c853e4 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1381 | if (SpecFunc) |
| 1382 | return SpecFunc; |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1385 | bool isFriend; |
| 1386 | if (FunctionTemplate) |
| 1387 | isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None); |
| 1388 | else |
| 1389 | isFriend = (D->getFriendObjectKind() != Decl::FOK_None); |
| 1390 | |
Douglas Gregor | 79c2278 | 2010-01-16 20:21:20 +0000 | [diff] [blame] | 1391 | bool MergeWithParentScope = (TemplateParams != 0) || |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1392 | !(isa<Decl>(Owner) && |
Douglas Gregor | 79c2278 | 2010-01-16 20:21:20 +0000 | [diff] [blame] | 1393 | cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod()); |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1394 | LocalInstantiationScope Scope(SemaRef, MergeWithParentScope); |
Douglas Gregor | 48dd19b | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 1395 | |
John McCall | 4eab39f | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 1396 | // Instantiate enclosing template arguments for friends. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1397 | SmallVector<TemplateParameterList *, 4> TempParamLists; |
John McCall | 4eab39f | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 1398 | unsigned NumTempParamLists = 0; |
| 1399 | if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) { |
| 1400 | TempParamLists.set_size(NumTempParamLists); |
| 1401 | for (unsigned I = 0; I != NumTempParamLists; ++I) { |
| 1402 | TemplateParameterList *TempParams = D->getTemplateParameterList(I); |
| 1403 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 1404 | if (!InstParams) |
| 1405 | return NULL; |
| 1406 | TempParamLists[I] = InstParams; |
| 1407 | } |
| 1408 | } |
| 1409 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1410 | SmallVector<ParmVarDecl *, 4> Params; |
Benjamin Kramer | dc370c1 | 2012-01-20 14:42:32 +0000 | [diff] [blame] | 1411 | TypeSourceInfo *TInfo = SubstFunctionType(D, Params); |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 1412 | if (!TInfo) |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1413 | return 0; |
Douglas Gregor | 71074fd | 2012-09-13 21:56:43 +0000 | [diff] [blame] | 1414 | QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1415 | |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 1416 | // \brief If the type of this function, after ignoring parentheses, |
| 1417 | // is not *directly* a function type, then we're instantiating a function |
| 1418 | // that was declared via a typedef, e.g., |
Douglas Gregor | 5f970ee | 2010-05-04 18:18:31 +0000 | [diff] [blame] | 1419 | // |
| 1420 | // typedef int functype(int, int); |
| 1421 | // functype func; |
| 1422 | // |
| 1423 | // In this case, we'll just go instantiate the ParmVarDecls that we |
| 1424 | // synthesized in the method declaration. |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 1425 | if (!isa<FunctionProtoType>(T.IgnoreParens())) { |
Douglas Gregor | 5f970ee | 2010-05-04 18:18:31 +0000 | [diff] [blame] | 1426 | assert(!Params.size() && "Instantiating type could not yield parameters"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1427 | SmallVector<QualType, 4> ParamTypes; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1428 | if (SemaRef.SubstParmTypes(D->getLocation(), D->param_begin(), |
| 1429 | D->getNumParams(), TemplateArgs, ParamTypes, |
Douglas Gregor | 12c9c00 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 1430 | &Params)) |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1431 | return 0; |
Douglas Gregor | 5f970ee | 2010-05-04 18:18:31 +0000 | [diff] [blame] | 1432 | } |
| 1433 | |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1434 | NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc(); |
| 1435 | if (QualifierLoc) { |
| 1436 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1437 | TemplateArgs); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1438 | if (!QualifierLoc) |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1439 | return 0; |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | DeclContext *DC = Owner; |
| 1443 | if (isFriend) { |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1444 | if (QualifierLoc) { |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1445 | CXXScopeSpec SS; |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1446 | SS.Adopt(QualifierLoc); |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1447 | DC = SemaRef.computeDeclContext(SS); |
John McCall | c54d688 | 2010-10-19 05:01:53 +0000 | [diff] [blame] | 1448 | |
| 1449 | if (DC && SemaRef.RequireCompleteDeclContext(SS, DC)) |
| 1450 | return 0; |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1451 | } else { |
| 1452 | DC = SemaRef.FindInstantiatedContext(D->getLocation(), |
| 1453 | D->getDeclContext(), |
| 1454 | TemplateArgs); |
| 1455 | } |
| 1456 | if (!DC) return 0; |
| 1457 | } |
| 1458 | |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1459 | // Build the instantiated method declaration. |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1460 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1461 | CXXMethodDecl *Method = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1462 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1463 | SourceLocation StartLoc = D->getInnerLocStart(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1464 | DeclarationNameInfo NameInfo |
| 1465 | = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs); |
Douglas Gregor | 17e32f3 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 1466 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1467 | Method = CXXConstructorDecl::Create(SemaRef.Context, Record, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1468 | StartLoc, NameInfo, T, TInfo, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1469 | Constructor->isExplicit(), |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1470 | Constructor->isInlineSpecified(), |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 1471 | false, Constructor->isConstexpr()); |
Douglas Gregor | 17e32f3 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 1472 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
Douglas Gregor | 17e32f3 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 1473 | Method = CXXDestructorDecl::Create(SemaRef.Context, Record, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1474 | StartLoc, NameInfo, T, TInfo, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1475 | Destructor->isInlineSpecified(), |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1476 | false); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1477 | } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1478 | Method = CXXConversionDecl::Create(SemaRef.Context, Record, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1479 | StartLoc, NameInfo, T, TInfo, |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 1480 | Conversion->isInlineSpecified(), |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 1481 | Conversion->isExplicit(), |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 1482 | Conversion->isConstexpr(), |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1483 | Conversion->getLocEnd()); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1484 | } else { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1485 | Method = CXXMethodDecl::Create(SemaRef.Context, Record, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1486 | StartLoc, NameInfo, T, TInfo, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1487 | D->isStatic(), |
| 1488 | D->getStorageClassAsWritten(), |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 1489 | D->isInlineSpecified(), |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 1490 | D->isConstexpr(), D->getLocEnd()); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1491 | } |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1492 | |
Richard Smith | d4497dd | 2013-01-25 00:08:28 +0000 | [diff] [blame] | 1493 | if (D->isInlined()) |
| 1494 | Method->setImplicitlyInline(); |
| 1495 | |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1496 | if (QualifierLoc) |
| 1497 | Method->setQualifierInfo(QualifierLoc); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1498 | |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1499 | if (TemplateParams) { |
| 1500 | // Our resulting instantiation is actually a function template, since we |
| 1501 | // are substituting only the outer template parameters. For example, given |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1502 | // |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1503 | // template<typename T> |
| 1504 | // struct X { |
| 1505 | // template<typename U> void f(T, U); |
| 1506 | // }; |
| 1507 | // |
| 1508 | // X<int> x; |
| 1509 | // |
| 1510 | // We are instantiating the member template "f" within X<int>, which means |
| 1511 | // substituting int for T, but leaving "f" as a member function template. |
| 1512 | // Build the function template itself. |
| 1513 | FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record, |
| 1514 | Method->getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1515 | Method->getDeclName(), |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1516 | TemplateParams, Method); |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1517 | if (isFriend) { |
| 1518 | FunctionTemplate->setLexicalDeclContext(Owner); |
| 1519 | FunctionTemplate->setObjectOfFriendDecl(true); |
| 1520 | } else if (D->isOutOfLine()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1521 | FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext()); |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1522 | Method->setDescribedFunctionTemplate(FunctionTemplate); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1523 | } else if (FunctionTemplate) { |
| 1524 | // Record this function template specialization. |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1525 | std::pair<const TemplateArgument *, unsigned> Innermost |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1526 | = TemplateArgs.getInnermost(); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1527 | Method->setFunctionTemplateSpecialization(FunctionTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1528 | TemplateArgumentList::CreateCopy(SemaRef.Context, |
| 1529 | Innermost.first, |
| 1530 | Innermost.second), |
Douglas Gregor | 1e1e972 | 2012-03-28 14:34:23 +0000 | [diff] [blame] | 1531 | /*InsertPos=*/0); |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1532 | } else if (!isFriend) { |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1533 | // Record that this is an instantiation of a member function. |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1534 | Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1535 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1536 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1537 | // If we are instantiating a member function defined |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1538 | // out-of-line, the instantiation will have the same lexical |
| 1539 | // context (which will be a namespace scope) as the template. |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1540 | if (isFriend) { |
John McCall | 4eab39f | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 1541 | if (NumTempParamLists) |
| 1542 | Method->setTemplateParameterListsInfo(SemaRef.Context, |
| 1543 | NumTempParamLists, |
| 1544 | TempParamLists.data()); |
| 1545 | |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1546 | Method->setLexicalDeclContext(Owner); |
| 1547 | Method->setObjectOfFriendDecl(true); |
| 1548 | } else if (D->isOutOfLine()) |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1549 | Method->setLexicalDeclContext(D->getLexicalDeclContext()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1550 | |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1551 | // Attach the parameters |
| 1552 | for (unsigned P = 0; P < Params.size(); ++P) |
| 1553 | Params[P]->setOwningFunction(Method); |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 1554 | Method->setParams(Params); |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1555 | |
| 1556 | if (InitMethodInstantiation(Method, D)) |
| 1557 | Method->setInvalidDecl(); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1558 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1559 | LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName, |
| 1560 | Sema::ForRedeclaration); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1561 | |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1562 | if (!FunctionTemplate || TemplateParams || isFriend) { |
| 1563 | SemaRef.LookupQualifiedName(Previous, Record); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1564 | |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1565 | // In C++, the previous declaration we find might be a tag type |
| 1566 | // (class or enum). In this case, the new declaration will hide the |
| 1567 | // tag type. Note that this does does not apply if we're declaring a |
| 1568 | // typedef (C++ [dcl.typedef]p4). |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1569 | if (Previous.isSingleTagDecl()) |
| 1570 | Previous.clear(); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1571 | } |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1572 | |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 1573 | if (!IsClassScopeSpecialization) |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 1574 | SemaRef.CheckFunctionDeclaration(0, Method, Previous, false); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1575 | |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 1576 | if (D->isPure()) |
| 1577 | SemaRef.CheckPureMethod(Method, SourceRange()); |
| 1578 | |
John McCall | 1f2e1a9 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1579 | // Propagate access. For a non-friend declaration, the access is |
| 1580 | // whatever we're propagating from. For a friend, it should be the |
| 1581 | // previous declaration we just found. |
| 1582 | if (isFriend && Method->getPreviousDecl()) |
| 1583 | Method->setAccess(Method->getPreviousDecl()->getAccess()); |
| 1584 | else |
| 1585 | Method->setAccess(D->getAccess()); |
| 1586 | if (FunctionTemplate) |
| 1587 | FunctionTemplate->setAccess(Method->getAccess()); |
John McCall | 46460a6 | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 1588 | |
Anders Carlsson | 9eefa22 | 2011-01-20 06:52:44 +0000 | [diff] [blame] | 1589 | SemaRef.CheckOverrideControl(Method); |
| 1590 | |
Eli Friedman | 3bc4515 | 2011-11-15 22:39:08 +0000 | [diff] [blame] | 1591 | // If a function is defined as defaulted or deleted, mark it as such now. |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 1592 | if (D->isExplicitlyDefaulted()) |
| 1593 | SemaRef.SetDeclDefaulted(Method, Method->getLocation()); |
Eli Friedman | 3bc4515 | 2011-11-15 22:39:08 +0000 | [diff] [blame] | 1594 | if (D->isDeletedAsWritten()) |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 1595 | SemaRef.SetDeclDeleted(Method, Method->getLocation()); |
Eli Friedman | 3bc4515 | 2011-11-15 22:39:08 +0000 | [diff] [blame] | 1596 | |
John McCall | 1f2e1a9 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1597 | // If there's a function template, let our caller handle it. |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1598 | if (FunctionTemplate) { |
John McCall | 1f2e1a9 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1599 | // do nothing |
| 1600 | |
| 1601 | // Don't hide a (potentially) valid declaration with an invalid one. |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1602 | } else if (Method->isInvalidDecl() && !Previous.empty()) { |
John McCall | 1f2e1a9 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1603 | // do nothing |
| 1604 | |
| 1605 | // Otherwise, check access to friends and make them visible. |
| 1606 | } else if (isFriend) { |
| 1607 | // We only need to re-check access for methods which we didn't |
| 1608 | // manage to match during parsing. |
| 1609 | if (!D->getPreviousDecl()) |
| 1610 | SemaRef.CheckFriendAccess(Method); |
| 1611 | |
| 1612 | Record->makeDeclVisibleInContext(Method); |
| 1613 | |
| 1614 | // Otherwise, add the declaration. We don't need to do this for |
| 1615 | // class-scope specializations because we'll have matched them with |
| 1616 | // the appropriate template. |
| 1617 | } else if (!IsClassScopeSpecialization) { |
| 1618 | Owner->addDecl(Method); |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1619 | } |
Sean Hunt | eb88ae5 | 2011-05-23 21:07:59 +0000 | [diff] [blame] | 1620 | |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1621 | return Method; |
| 1622 | } |
| 1623 | |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 1624 | Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1625 | return VisitCXXMethodDecl(D); |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 1628 | Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) { |
Douglas Gregor | 17e32f3 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 1629 | return VisitCXXMethodDecl(D); |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
Douglas Gregor | bb969ed | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 1632 | Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1633 | return VisitCXXMethodDecl(D); |
Douglas Gregor | bb969ed | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 1634 | } |
| 1635 | |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 1636 | ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) { |
John McCall | fb44de9 | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 1637 | return SemaRef.SubstParmVarDecl(D, TemplateArgs, /*indexAdjustment*/ 0, |
Douglas Gregor | d1bb4ae | 2012-01-25 16:15:54 +0000 | [diff] [blame] | 1638 | llvm::Optional<unsigned>(), |
| 1639 | /*ExpectParameterPack=*/false); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1642 | Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl( |
| 1643 | TemplateTypeParmDecl *D) { |
| 1644 | // TODO: don't always clone when decls are refcounted. |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 1645 | assert(D->getTypeForDecl()->isTemplateTypeParmType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1646 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1647 | TemplateTypeParmDecl *Inst = |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 1648 | TemplateTypeParmDecl::Create(SemaRef.Context, Owner, |
| 1649 | D->getLocStart(), D->getLocation(), |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 1650 | D->getDepth() - TemplateArgs.getNumLevels(), |
| 1651 | D->getIndex(), D->getIdentifier(), |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1652 | D->wasDeclaredWithTypename(), |
| 1653 | D->isParameterPack()); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 1654 | Inst->setAccess(AS_public); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1655 | |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1656 | if (D->hasDefaultArgument()) |
| 1657 | Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false); |
| 1658 | |
| 1659 | // Introduce this template parameter's instantiation into the instantiation |
Douglas Gregor | 550d9b2 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1660 | // scope. |
| 1661 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1662 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1663 | return Inst; |
| 1664 | } |
| 1665 | |
Douglas Gregor | 33642df | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 1666 | Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl( |
| 1667 | NonTypeTemplateParmDecl *D) { |
| 1668 | // Substitute into the type of the non-type template parameter. |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1669 | TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1670 | SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten; |
| 1671 | SmallVector<QualType, 4> ExpandedParameterPackTypes; |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1672 | bool IsExpandedParameterPack = false; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1673 | TypeSourceInfo *DI; |
Douglas Gregor | 33642df | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 1674 | QualType T; |
Douglas Gregor | 33642df | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 1675 | bool Invalid = false; |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1676 | |
| 1677 | if (D->isExpandedParameterPack()) { |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1678 | // The non-type template parameter pack is an already-expanded pack |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1679 | // expansion of types. Substitute into each of the expanded types. |
| 1680 | ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes()); |
| 1681 | ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes()); |
| 1682 | for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) { |
| 1683 | TypeSourceInfo *NewDI =SemaRef.SubstType(D->getExpansionTypeSourceInfo(I), |
| 1684 | TemplateArgs, |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1685 | D->getLocation(), |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1686 | D->getDeclName()); |
| 1687 | if (!NewDI) |
| 1688 | return 0; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1689 | |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1690 | ExpandedParameterPackTypesAsWritten.push_back(NewDI); |
| 1691 | QualType NewT =SemaRef.CheckNonTypeTemplateParameterType(NewDI->getType(), |
| 1692 | D->getLocation()); |
| 1693 | if (NewT.isNull()) |
| 1694 | return 0; |
| 1695 | ExpandedParameterPackTypes.push_back(NewT); |
| 1696 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1697 | |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1698 | IsExpandedParameterPack = true; |
| 1699 | DI = D->getTypeSourceInfo(); |
| 1700 | T = DI->getType(); |
Richard Smith | 6964b3f | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1701 | } else if (D->isPackExpansion()) { |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1702 | // The non-type template parameter pack's type is a pack expansion of types. |
| 1703 | // Determine whether we need to expand this parameter pack into separate |
| 1704 | // types. |
| 1705 | PackExpansionTypeLoc Expansion = cast<PackExpansionTypeLoc>(TL); |
| 1706 | TypeLoc Pattern = Expansion.getPatternLoc(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1707 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1708 | SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1709 | |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1710 | // Determine whether the set of unexpanded parameter packs can and should |
| 1711 | // be expanded. |
| 1712 | bool Expand = true; |
| 1713 | bool RetainExpansion = false; |
| 1714 | llvm::Optional<unsigned> OrigNumExpansions |
| 1715 | = Expansion.getTypePtr()->getNumExpansions(); |
| 1716 | llvm::Optional<unsigned> NumExpansions = OrigNumExpansions; |
| 1717 | if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(), |
| 1718 | Pattern.getSourceRange(), |
David Blaikie | a71f9d0 | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 1719 | Unexpanded, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1720 | TemplateArgs, |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1721 | Expand, RetainExpansion, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1722 | NumExpansions)) |
| 1723 | return 0; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1724 | |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1725 | if (Expand) { |
| 1726 | for (unsigned I = 0; I != *NumExpansions; ++I) { |
| 1727 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I); |
| 1728 | TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs, |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1729 | D->getLocation(), |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1730 | D->getDeclName()); |
| 1731 | if (!NewDI) |
| 1732 | return 0; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1733 | |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1734 | ExpandedParameterPackTypesAsWritten.push_back(NewDI); |
| 1735 | QualType NewT = SemaRef.CheckNonTypeTemplateParameterType( |
| 1736 | NewDI->getType(), |
| 1737 | D->getLocation()); |
| 1738 | if (NewT.isNull()) |
| 1739 | return 0; |
| 1740 | ExpandedParameterPackTypes.push_back(NewT); |
| 1741 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1742 | |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1743 | // Note that we have an expanded parameter pack. The "type" of this |
| 1744 | // expanded parameter pack is the original expansion type, but callers |
| 1745 | // will end up using the expanded parameter pack types for type-checking. |
| 1746 | IsExpandedParameterPack = true; |
| 1747 | DI = D->getTypeSourceInfo(); |
| 1748 | T = DI->getType(); |
| 1749 | } else { |
| 1750 | // We cannot fully expand the pack expansion now, so substitute into the |
| 1751 | // pattern and create a new pack expansion type. |
| 1752 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1); |
| 1753 | TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs, |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1754 | D->getLocation(), |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1755 | D->getDeclName()); |
| 1756 | if (!NewPattern) |
| 1757 | return 0; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1758 | |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1759 | DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(), |
| 1760 | NumExpansions); |
| 1761 | if (!DI) |
| 1762 | return 0; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1763 | |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1764 | T = DI->getType(); |
| 1765 | } |
| 1766 | } else { |
| 1767 | // Simple case: substitution into a parameter that is not a parameter pack. |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1768 | DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1769 | D->getLocation(), D->getDeclName()); |
| 1770 | if (!DI) |
| 1771 | return 0; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1772 | |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1773 | // Check that this type is acceptable for a non-type template parameter. |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1774 | T = SemaRef.CheckNonTypeTemplateParameterType(DI->getType(), |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1775 | D->getLocation()); |
| 1776 | if (T.isNull()) { |
| 1777 | T = SemaRef.Context.IntTy; |
| 1778 | Invalid = true; |
| 1779 | } |
Douglas Gregor | 33642df | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 1780 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1781 | |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1782 | NonTypeTemplateParmDecl *Param; |
| 1783 | if (IsExpandedParameterPack) |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1784 | Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1785 | D->getInnerLocStart(), |
| 1786 | D->getLocation(), |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1787 | D->getDepth() - TemplateArgs.getNumLevels(), |
| 1788 | D->getPosition(), |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1789 | D->getIdentifier(), T, |
| 1790 | DI, |
| 1791 | ExpandedParameterPackTypes.data(), |
| 1792 | ExpandedParameterPackTypes.size(), |
| 1793 | ExpandedParameterPackTypesAsWritten.data()); |
| 1794 | else |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1795 | Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1796 | D->getInnerLocStart(), |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1797 | D->getLocation(), |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1798 | D->getDepth() - TemplateArgs.getNumLevels(), |
| 1799 | D->getPosition(), |
| 1800 | D->getIdentifier(), T, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1801 | D->isParameterPack(), DI); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1802 | |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 1803 | Param->setAccess(AS_public); |
Douglas Gregor | 33642df | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 1804 | if (Invalid) |
| 1805 | Param->setInvalidDecl(); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1806 | |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1807 | Param->setDefaultArgument(D->getDefaultArgument(), false); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1808 | |
| 1809 | // Introduce this template parameter's instantiation into the instantiation |
Douglas Gregor | 550d9b2 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1810 | // scope. |
| 1811 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); |
Douglas Gregor | 33642df | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 1812 | return Param; |
| 1813 | } |
| 1814 | |
Richard Smith | 6964b3f | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1815 | static void collectUnexpandedParameterPacks( |
| 1816 | Sema &S, |
| 1817 | TemplateParameterList *Params, |
| 1818 | SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) { |
| 1819 | for (TemplateParameterList::const_iterator I = Params->begin(), |
| 1820 | E = Params->end(); I != E; ++I) { |
| 1821 | if ((*I)->isTemplateParameterPack()) |
| 1822 | continue; |
| 1823 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*I)) |
| 1824 | S.collectUnexpandedParameterPacks(NTTP->getTypeSourceInfo()->getTypeLoc(), |
| 1825 | Unexpanded); |
| 1826 | if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(*I)) |
| 1827 | collectUnexpandedParameterPacks(S, TTP->getTemplateParameters(), |
| 1828 | Unexpanded); |
| 1829 | } |
| 1830 | } |
| 1831 | |
Anders Carlsson | 0dde18e | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 1832 | Decl * |
Douglas Gregor | 9106ef7 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 1833 | TemplateDeclInstantiator::VisitTemplateTemplateParmDecl( |
| 1834 | TemplateTemplateParmDecl *D) { |
| 1835 | // Instantiate the template parameter list of the template template parameter. |
| 1836 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 1837 | TemplateParameterList *InstParams; |
Richard Smith | 6964b3f | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1838 | SmallVector<TemplateParameterList*, 8> ExpandedParams; |
| 1839 | |
| 1840 | bool IsExpandedParameterPack = false; |
| 1841 | |
| 1842 | if (D->isExpandedParameterPack()) { |
| 1843 | // The template template parameter pack is an already-expanded pack |
| 1844 | // expansion of template parameters. Substitute into each of the expanded |
| 1845 | // parameters. |
| 1846 | ExpandedParams.reserve(D->getNumExpansionTemplateParameters()); |
| 1847 | for (unsigned I = 0, N = D->getNumExpansionTemplateParameters(); |
| 1848 | I != N; ++I) { |
| 1849 | LocalInstantiationScope Scope(SemaRef); |
| 1850 | TemplateParameterList *Expansion = |
| 1851 | SubstTemplateParams(D->getExpansionTemplateParameters(I)); |
| 1852 | if (!Expansion) |
| 1853 | return 0; |
| 1854 | ExpandedParams.push_back(Expansion); |
| 1855 | } |
| 1856 | |
| 1857 | IsExpandedParameterPack = true; |
| 1858 | InstParams = TempParams; |
| 1859 | } else if (D->isPackExpansion()) { |
| 1860 | // The template template parameter pack expands to a pack of template |
| 1861 | // template parameters. Determine whether we need to expand this parameter |
| 1862 | // pack into separate parameters. |
| 1863 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 1864 | collectUnexpandedParameterPacks(SemaRef, D->getTemplateParameters(), |
| 1865 | Unexpanded); |
| 1866 | |
| 1867 | // Determine whether the set of unexpanded parameter packs can and should |
| 1868 | // be expanded. |
| 1869 | bool Expand = true; |
| 1870 | bool RetainExpansion = false; |
| 1871 | llvm::Optional<unsigned> NumExpansions; |
| 1872 | if (SemaRef.CheckParameterPacksForExpansion(D->getLocation(), |
| 1873 | TempParams->getSourceRange(), |
| 1874 | Unexpanded, |
| 1875 | TemplateArgs, |
| 1876 | Expand, RetainExpansion, |
| 1877 | NumExpansions)) |
| 1878 | return 0; |
| 1879 | |
| 1880 | if (Expand) { |
| 1881 | for (unsigned I = 0; I != *NumExpansions; ++I) { |
| 1882 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I); |
| 1883 | LocalInstantiationScope Scope(SemaRef); |
| 1884 | TemplateParameterList *Expansion = SubstTemplateParams(TempParams); |
| 1885 | if (!Expansion) |
| 1886 | return 0; |
| 1887 | ExpandedParams.push_back(Expansion); |
| 1888 | } |
| 1889 | |
| 1890 | // Note that we have an expanded parameter pack. The "type" of this |
| 1891 | // expanded parameter pack is the original expansion type, but callers |
| 1892 | // will end up using the expanded parameter pack types for type-checking. |
| 1893 | IsExpandedParameterPack = true; |
| 1894 | InstParams = TempParams; |
| 1895 | } else { |
| 1896 | // We cannot fully expand the pack expansion now, so just substitute |
| 1897 | // into the pattern. |
| 1898 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1); |
| 1899 | |
| 1900 | LocalInstantiationScope Scope(SemaRef); |
| 1901 | InstParams = SubstTemplateParams(TempParams); |
| 1902 | if (!InstParams) |
| 1903 | return 0; |
| 1904 | } |
| 1905 | } else { |
Douglas Gregor | 9106ef7 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 1906 | // Perform the actual substitution of template parameters within a new, |
| 1907 | // local instantiation scope. |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1908 | LocalInstantiationScope Scope(SemaRef); |
Douglas Gregor | 9106ef7 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 1909 | InstParams = SubstTemplateParams(TempParams); |
| 1910 | if (!InstParams) |
Richard Smith | 6964b3f | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1911 | return 0; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1912 | } |
| 1913 | |
Douglas Gregor | 9106ef7 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 1914 | // Build the template template parameter. |
Richard Smith | 6964b3f | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1915 | TemplateTemplateParmDecl *Param; |
| 1916 | if (IsExpandedParameterPack) |
| 1917 | Param = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, |
| 1918 | D->getLocation(), |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1919 | D->getDepth() - TemplateArgs.getNumLevels(), |
Richard Smith | 6964b3f | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1920 | D->getPosition(), |
| 1921 | D->getIdentifier(), InstParams, |
| 1922 | ExpandedParams); |
| 1923 | else |
| 1924 | Param = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, |
| 1925 | D->getLocation(), |
| 1926 | D->getDepth() - TemplateArgs.getNumLevels(), |
| 1927 | D->getPosition(), |
| 1928 | D->isParameterPack(), |
| 1929 | D->getIdentifier(), InstParams); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1930 | Param->setDefaultArgument(D->getDefaultArgument(), false); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 1931 | Param->setAccess(AS_public); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1932 | |
| 1933 | // Introduce this template parameter's instantiation into the instantiation |
Douglas Gregor | 9106ef7 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 1934 | // scope. |
| 1935 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1936 | |
Douglas Gregor | 9106ef7 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 1937 | return Param; |
| 1938 | } |
| 1939 | |
Douglas Gregor | 48c32a7 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 1940 | Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 1941 | // Using directives are never dependent (and never contain any types or |
| 1942 | // expressions), so they require no explicit instantiation work. |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1943 | |
Douglas Gregor | 48c32a7 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 1944 | UsingDirectiveDecl *Inst |
| 1945 | = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1946 | D->getNamespaceKeyLocation(), |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 1947 | D->getQualifierLoc(), |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1948 | D->getIdentLocation(), |
| 1949 | D->getNominatedNamespace(), |
Douglas Gregor | 48c32a7 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 1950 | D->getCommonAncestor()); |
Abramo Bagnara | 536afbe | 2012-09-05 09:55:10 +0000 | [diff] [blame] | 1951 | |
| 1952 | // Add the using directive to its declaration context |
| 1953 | // only if this is not a function or method. |
| 1954 | if (!Owner->isFunctionOrMethod()) |
| 1955 | Owner->addDecl(Inst); |
| 1956 | |
Douglas Gregor | 48c32a7 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 1957 | return Inst; |
| 1958 | } |
| 1959 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1960 | Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) { |
Douglas Gregor | 1b39820 | 2010-09-29 17:58:28 +0000 | [diff] [blame] | 1961 | |
| 1962 | // The nested name specifier may be dependent, for example |
| 1963 | // template <typename T> struct t { |
| 1964 | // struct s1 { T f1(); }; |
| 1965 | // struct s2 : s1 { using s1::f1; }; |
| 1966 | // }; |
| 1967 | // template struct t<int>; |
| 1968 | // Here, in using s1::f1, s1 refers to t<T>::s1; |
| 1969 | // we need to substitute for t<int>::s1. |
Douglas Gregor | 5149f37 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 1970 | NestedNameSpecifierLoc QualifierLoc |
| 1971 | = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), |
| 1972 | TemplateArgs); |
| 1973 | if (!QualifierLoc) |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1974 | return 0; |
Douglas Gregor | 1b39820 | 2010-09-29 17:58:28 +0000 | [diff] [blame] | 1975 | |
| 1976 | // The name info is non-dependent, so no transformation |
| 1977 | // is required. |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 1978 | DeclarationNameInfo NameInfo = D->getNameInfo(); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1979 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1980 | // We only need to do redeclaration lookups if we're in a class |
| 1981 | // scope (in fact, it's not really even possible in non-class |
| 1982 | // scopes). |
| 1983 | bool CheckRedeclaration = Owner->isRecord(); |
| 1984 | |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 1985 | LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName, |
| 1986 | Sema::ForRedeclaration); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1987 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1988 | UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner, |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1989 | D->getUsingLocation(), |
Douglas Gregor | 5149f37 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 1990 | QualifierLoc, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 1991 | NameInfo, |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1992 | D->isTypeName()); |
| 1993 | |
Douglas Gregor | 5149f37 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 1994 | CXXScopeSpec SS; |
| 1995 | SS.Adopt(QualifierLoc); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1996 | if (CheckRedeclaration) { |
| 1997 | Prev.setHideTags(false); |
| 1998 | SemaRef.LookupQualifiedName(Prev, Owner); |
| 1999 | |
| 2000 | // Check for invalid redeclarations. |
| 2001 | if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(), |
| 2002 | D->isTypeName(), SS, |
| 2003 | D->getLocation(), Prev)) |
| 2004 | NewUD->setInvalidDecl(); |
| 2005 | |
| 2006 | } |
| 2007 | |
| 2008 | if (!NewUD->isInvalidDecl() && |
| 2009 | SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS, |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2010 | D->getLocation())) |
| 2011 | NewUD->setInvalidDecl(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2012 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2013 | SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D); |
| 2014 | NewUD->setAccess(D->getAccess()); |
| 2015 | Owner->addDecl(NewUD); |
| 2016 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2017 | // Don't process the shadow decls for an invalid decl. |
| 2018 | if (NewUD->isInvalidDecl()) |
| 2019 | return NewUD; |
| 2020 | |
Richard Smith | c5a89a1 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 2021 | if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) { |
| 2022 | if (SemaRef.CheckInheritingConstructorUsingDecl(NewUD)) |
| 2023 | NewUD->setInvalidDecl(); |
| 2024 | return NewUD; |
| 2025 | } |
| 2026 | |
John McCall | 323c310 | 2009-12-22 22:26:37 +0000 | [diff] [blame] | 2027 | bool isFunctionScope = Owner->isFunctionOrMethod(); |
| 2028 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2029 | // Process the shadow decls. |
| 2030 | for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end(); |
| 2031 | I != E; ++I) { |
| 2032 | UsingShadowDecl *Shadow = *I; |
| 2033 | NamedDecl *InstTarget = |
Douglas Gregor | b710722 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 2034 | cast_or_null<NamedDecl>(SemaRef.FindInstantiatedDecl( |
| 2035 | Shadow->getLocation(), |
| 2036 | Shadow->getTargetDecl(), |
| 2037 | TemplateArgs)); |
| 2038 | if (!InstTarget) |
| 2039 | return 0; |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2040 | |
| 2041 | if (CheckRedeclaration && |
| 2042 | SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev)) |
| 2043 | continue; |
| 2044 | |
| 2045 | UsingShadowDecl *InstShadow |
| 2046 | = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget); |
| 2047 | SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow); |
John McCall | 323c310 | 2009-12-22 22:26:37 +0000 | [diff] [blame] | 2048 | |
| 2049 | if (isFunctionScope) |
| 2050 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2051 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2052 | |
| 2053 | return NewUD; |
| 2054 | } |
| 2055 | |
| 2056 | Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) { |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2057 | // Ignore these; we handle them in bulk when processing the UsingDecl. |
| 2058 | return 0; |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2059 | } |
| 2060 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2061 | Decl * TemplateDeclInstantiator |
| 2062 | ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) { |
Douglas Gregor | 5149f37 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2063 | NestedNameSpecifierLoc QualifierLoc |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2064 | = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), |
Douglas Gregor | 5149f37 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2065 | TemplateArgs); |
| 2066 | if (!QualifierLoc) |
Anders Carlsson | 0dde18e | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 2067 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2068 | |
Anders Carlsson | 0dde18e | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 2069 | CXXScopeSpec SS; |
Douglas Gregor | 5149f37 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2070 | SS.Adopt(QualifierLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2071 | |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2072 | // Since NameInfo refers to a typename, it cannot be a C++ special name. |
Benjamin Kramer | accaf19 | 2012-11-14 15:08:31 +0000 | [diff] [blame] | 2073 | // Hence, no transformation is required for it. |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2074 | DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2075 | NamedDecl *UD = |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 2076 | SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(), |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2077 | D->getUsingLoc(), SS, NameInfo, 0, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2078 | /*instantiation*/ true, |
| 2079 | /*typename*/ true, D->getTypenameLoc()); |
Douglas Gregor | 4469e8a | 2010-05-19 17:02:24 +0000 | [diff] [blame] | 2080 | if (UD) |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2081 | SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D); |
| 2082 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2083 | return UD; |
| 2084 | } |
| 2085 | |
| 2086 | Decl * TemplateDeclInstantiator |
| 2087 | ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { |
Douglas Gregor | 5149f37 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2088 | NestedNameSpecifierLoc QualifierLoc |
| 2089 | = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), TemplateArgs); |
| 2090 | if (!QualifierLoc) |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2091 | return 0; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2092 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2093 | CXXScopeSpec SS; |
Douglas Gregor | 5149f37 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2094 | SS.Adopt(QualifierLoc); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2095 | |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2096 | DeclarationNameInfo NameInfo |
| 2097 | = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs); |
| 2098 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2099 | NamedDecl *UD = |
| 2100 | SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(), |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2101 | D->getUsingLoc(), SS, NameInfo, 0, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2102 | /*instantiation*/ true, |
| 2103 | /*typename*/ false, SourceLocation()); |
Douglas Gregor | 4469e8a | 2010-05-19 17:02:24 +0000 | [diff] [blame] | 2104 | if (UD) |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2105 | SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D); |
| 2106 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 2107 | return UD; |
Anders Carlsson | 0dde18e | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 2108 | } |
| 2109 | |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2110 | |
| 2111 | Decl *TemplateDeclInstantiator::VisitClassScopeFunctionSpecializationDecl( |
| 2112 | ClassScopeFunctionSpecializationDecl *Decl) { |
| 2113 | CXXMethodDecl *OldFD = Decl->getSpecialization(); |
Nico Weber | 6b02009 | 2012-06-25 17:21:05 +0000 | [diff] [blame] | 2114 | CXXMethodDecl *NewFD = cast<CXXMethodDecl>(VisitCXXMethodDecl(OldFD, |
| 2115 | 0, true)); |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2116 | |
| 2117 | LookupResult Previous(SemaRef, NewFD->getNameInfo(), Sema::LookupOrdinaryName, |
| 2118 | Sema::ForRedeclaration); |
| 2119 | |
Nico Weber | 6b02009 | 2012-06-25 17:21:05 +0000 | [diff] [blame] | 2120 | TemplateArgumentListInfo TemplateArgs; |
| 2121 | TemplateArgumentListInfo* TemplateArgsPtr = 0; |
| 2122 | if (Decl->hasExplicitTemplateArgs()) { |
| 2123 | TemplateArgs = Decl->templateArgs(); |
| 2124 | TemplateArgsPtr = &TemplateArgs; |
| 2125 | } |
| 2126 | |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2127 | SemaRef.LookupQualifiedName(Previous, SemaRef.CurContext); |
Nico Weber | 6b02009 | 2012-06-25 17:21:05 +0000 | [diff] [blame] | 2128 | if (SemaRef.CheckFunctionTemplateSpecialization(NewFD, TemplateArgsPtr, |
| 2129 | Previous)) { |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2130 | NewFD->setInvalidDecl(); |
| 2131 | return NewFD; |
| 2132 | } |
| 2133 | |
| 2134 | // Associate the specialization with the pattern. |
| 2135 | FunctionDecl *Specialization = cast<FunctionDecl>(Previous.getFoundDecl()); |
| 2136 | assert(Specialization && "Class scope Specialization is null"); |
| 2137 | SemaRef.Context.setClassScopeSpecializationPattern(Specialization, OldFD); |
| 2138 | |
| 2139 | return NewFD; |
| 2140 | } |
| 2141 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 2142 | Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 2143 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 2144 | TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs); |
Douglas Gregor | 2fa9800 | 2010-02-16 19:28:15 +0000 | [diff] [blame] | 2145 | if (D->isInvalidDecl()) |
| 2146 | return 0; |
| 2147 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 2148 | return Instantiator.Visit(D); |
| 2149 | } |
| 2150 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2151 | /// \brief Instantiates a nested template parameter list in the current |
| 2152 | /// instantiation context. |
| 2153 | /// |
| 2154 | /// \param L The parameter list to instantiate |
| 2155 | /// |
| 2156 | /// \returns NULL if there was an error |
| 2157 | TemplateParameterList * |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 2158 | TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) { |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2159 | // Get errors for all the parameters before bailing out. |
| 2160 | bool Invalid = false; |
| 2161 | |
| 2162 | unsigned N = L->size(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2163 | typedef SmallVector<NamedDecl *, 8> ParamVector; |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2164 | ParamVector Params; |
| 2165 | Params.reserve(N); |
| 2166 | for (TemplateParameterList::iterator PI = L->begin(), PE = L->end(); |
| 2167 | PI != PE; ++PI) { |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 2168 | NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI)); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2169 | Params.push_back(D); |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 2170 | Invalid = Invalid || !D || D->isInvalidDecl(); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2171 | } |
| 2172 | |
| 2173 | // Clean up if we had an error. |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 2174 | if (Invalid) |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2175 | return NULL; |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2176 | |
| 2177 | TemplateParameterList *InstL |
| 2178 | = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(), |
| 2179 | L->getLAngleLoc(), &Params.front(), N, |
| 2180 | L->getRAngleLoc()); |
| 2181 | return InstL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2182 | } |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2183 | |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2184 | /// \brief Instantiate the declaration of a class template partial |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2185 | /// specialization. |
| 2186 | /// |
| 2187 | /// \param ClassTemplate the (instantiated) class template that is partially |
| 2188 | // specialized by the instantiation of \p PartialSpec. |
| 2189 | /// |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2190 | /// \param PartialSpec the (uninstantiated) class template partial |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2191 | /// specialization that we are instantiating. |
| 2192 | /// |
Douglas Gregor | d65587f | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 2193 | /// \returns The instantiated partial specialization, if successful; otherwise, |
| 2194 | /// NULL to indicate an error. |
| 2195 | ClassTemplatePartialSpecializationDecl * |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2196 | TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization( |
| 2197 | ClassTemplateDecl *ClassTemplate, |
| 2198 | ClassTemplatePartialSpecializationDecl *PartialSpec) { |
Douglas Gregor | 550d9b2 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 2199 | // Create a local instantiation scope for this class template partial |
| 2200 | // specialization, which will contain the instantiations of the template |
| 2201 | // parameters. |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2202 | LocalInstantiationScope Scope(SemaRef); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2203 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2204 | // Substitute into the template parameters of the class template partial |
| 2205 | // specialization. |
| 2206 | TemplateParameterList *TempParams = PartialSpec->getTemplateParameters(); |
| 2207 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 2208 | if (!InstParams) |
Douglas Gregor | d65587f | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 2209 | return 0; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2210 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2211 | // Substitute into the template arguments of the class template partial |
| 2212 | // specialization. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2213 | TemplateArgumentListInfo InstTemplateArgs; // no angle locations |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2214 | if (SemaRef.Subst(PartialSpec->getTemplateArgsAsWritten(), |
| 2215 | PartialSpec->getNumTemplateArgsAsWritten(), |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2216 | InstTemplateArgs, TemplateArgs)) |
| 2217 | return 0; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2218 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2219 | // Check that the template argument list is well-formed for this |
| 2220 | // class template. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2221 | SmallVector<TemplateArgument, 4> Converted; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2222 | if (SemaRef.CheckTemplateArgumentList(ClassTemplate, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2223 | PartialSpec->getLocation(), |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2224 | InstTemplateArgs, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2225 | false, |
| 2226 | Converted)) |
Douglas Gregor | d65587f | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 2227 | return 0; |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2228 | |
| 2229 | // Figure out where to insert this class template partial specialization |
| 2230 | // in the member template's set of class template partial specializations. |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2231 | void *InsertPos = 0; |
| 2232 | ClassTemplateSpecializationDecl *PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2233 | = ClassTemplate->findPartialSpecialization(Converted.data(), |
| 2234 | Converted.size(), InsertPos); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2235 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2236 | // Build the canonical type that describes the converted template |
| 2237 | // arguments of the class template partial specialization. |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2238 | QualType CanonType |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2239 | = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2240 | Converted.data(), |
| 2241 | Converted.size()); |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2242 | |
| 2243 | // Build the fully-sugared type for this class template |
| 2244 | // specialization as the user wrote in the specialization |
| 2245 | // itself. This means that we'll pretty-print the type retrieved |
| 2246 | // from the specialization's declaration the way that the user |
| 2247 | // actually wrote the specialization, rather than formatting the |
| 2248 | // name based on the "canonical" representation used to store the |
| 2249 | // template arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2250 | TypeSourceInfo *WrittenTy |
| 2251 | = SemaRef.Context.getTemplateSpecializationTypeInfo( |
| 2252 | TemplateName(ClassTemplate), |
| 2253 | PartialSpec->getLocation(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2254 | InstTemplateArgs, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2255 | CanonType); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2256 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2257 | if (PrevDecl) { |
| 2258 | // We've already seen a partial specialization with the same template |
| 2259 | // parameters and template arguments. This can happen, for example, when |
| 2260 | // substituting the outer template arguments ends up causing two |
| 2261 | // class template partial specializations of a member class template |
| 2262 | // to have identical forms, e.g., |
| 2263 | // |
| 2264 | // template<typename T, typename U> |
| 2265 | // struct Outer { |
| 2266 | // template<typename X, typename Y> struct Inner; |
| 2267 | // template<typename Y> struct Inner<T, Y>; |
| 2268 | // template<typename Y> struct Inner<U, Y>; |
| 2269 | // }; |
| 2270 | // |
| 2271 | // Outer<int, int> outer; // error: the partial specializations of Inner |
| 2272 | // // have the same signature. |
| 2273 | SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared) |
Douglas Gregor | d65587f | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 2274 | << WrittenTy->getType(); |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2275 | SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here) |
| 2276 | << SemaRef.Context.getTypeDeclType(PrevDecl); |
Douglas Gregor | d65587f | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 2277 | return 0; |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2278 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2279 | |
| 2280 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2281 | // Create the class template partial specialization declaration. |
| 2282 | ClassTemplatePartialSpecializationDecl *InstPartialSpec |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2283 | = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context, |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 2284 | PartialSpec->getTagKind(), |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2285 | Owner, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2286 | PartialSpec->getLocStart(), |
| 2287 | PartialSpec->getLocation(), |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2288 | InstParams, |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2289 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2290 | Converted.data(), |
| 2291 | Converted.size(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2292 | InstTemplateArgs, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2293 | CanonType, |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 2294 | 0, |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 2295 | ClassTemplate->getNextPartialSpecSequenceNumber()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2296 | // Substitute the nested name specifier, if any. |
| 2297 | if (SubstQualifier(PartialSpec, InstPartialSpec)) |
| 2298 | return 0; |
| 2299 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2300 | InstPartialSpec->setInstantiatedFromMember(PartialSpec); |
Douglas Gregor | 4469e8a | 2010-05-19 17:02:24 +0000 | [diff] [blame] | 2301 | InstPartialSpec->setTypeAsWritten(WrittenTy); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2302 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2303 | // Add this partial specialization to the set of class template partial |
| 2304 | // specializations. |
Douglas Gregor | 1e1e972 | 2012-03-28 14:34:23 +0000 | [diff] [blame] | 2305 | ClassTemplate->AddPartialSpecialization(InstPartialSpec, /*InsertPos=*/0); |
Douglas Gregor | d65587f | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 2306 | return InstPartialSpec; |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2307 | } |
| 2308 | |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 2309 | TypeSourceInfo* |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 2310 | TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2311 | SmallVectorImpl<ParmVarDecl *> &Params) { |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 2312 | TypeSourceInfo *OldTInfo = D->getTypeSourceInfo(); |
| 2313 | assert(OldTInfo && "substituting function without type source info"); |
| 2314 | assert(Params.empty() && "parameter vector is non-empty at start"); |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2315 | |
| 2316 | CXXRecordDecl *ThisContext = 0; |
| 2317 | unsigned ThisTypeQuals = 0; |
| 2318 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 2319 | ThisContext = Method->getParent(); |
| 2320 | ThisTypeQuals = Method->getTypeQualifiers(); |
| 2321 | } |
| 2322 | |
John McCall | 6cd3b9f | 2010-04-09 17:38:44 +0000 | [diff] [blame] | 2323 | TypeSourceInfo *NewTInfo |
| 2324 | = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs, |
| 2325 | D->getTypeSpecStartLoc(), |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2326 | D->getDeclName(), |
| 2327 | ThisContext, ThisTypeQuals); |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 2328 | if (!NewTInfo) |
| 2329 | return 0; |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 2330 | |
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 2331 | if (NewTInfo != OldTInfo) { |
| 2332 | // Get parameters from the new type info. |
Abramo Bagnara | 140a2bd | 2010-12-13 22:27:55 +0000 | [diff] [blame] | 2333 | TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens(); |
Douglas Gregor | 6920cdc | 2010-05-03 15:32:18 +0000 | [diff] [blame] | 2334 | if (FunctionProtoTypeLoc *OldProtoLoc |
| 2335 | = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) { |
Abramo Bagnara | 140a2bd | 2010-12-13 22:27:55 +0000 | [diff] [blame] | 2336 | TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens(); |
Douglas Gregor | 6920cdc | 2010-05-03 15:32:18 +0000 | [diff] [blame] | 2337 | FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL); |
| 2338 | assert(NewProtoLoc && "Missing prototype?"); |
Richard Smith | 500d729 | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 2339 | unsigned NewIdx = 0; |
Douglas Gregor | 12c9c00 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 2340 | for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc->getNumArgs(); |
| 2341 | OldIdx != NumOldParams; ++OldIdx) { |
| 2342 | ParmVarDecl *OldParam = OldProtoLoc->getArg(OldIdx); |
Richard Smith | 500d729 | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 2343 | LocalInstantiationScope *Scope = SemaRef.CurrentInstantiationScope; |
| 2344 | |
| 2345 | llvm::Optional<unsigned> NumArgumentsInExpansion; |
| 2346 | if (OldParam->isParameterPack()) |
| 2347 | NumArgumentsInExpansion = |
| 2348 | SemaRef.getNumArgumentsInExpansion(OldParam->getType(), |
| 2349 | TemplateArgs); |
| 2350 | if (!NumArgumentsInExpansion) { |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2351 | // Simple case: normal parameter, or a parameter pack that's |
Douglas Gregor | 12c9c00 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 2352 | // instantiated to a (still-dependent) parameter pack. |
| 2353 | ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++); |
| 2354 | Params.push_back(NewParam); |
Richard Smith | 500d729 | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 2355 | Scope->InstantiatedLocal(OldParam, NewParam); |
| 2356 | } else { |
| 2357 | // Parameter pack expansion: make the instantiation an argument pack. |
| 2358 | Scope->MakeInstantiatedLocalArgPack(OldParam); |
| 2359 | for (unsigned I = 0; I != *NumArgumentsInExpansion; ++I) { |
| 2360 | ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++); |
| 2361 | Params.push_back(NewParam); |
| 2362 | Scope->InstantiatedLocalPackArg(OldParam, NewParam); |
| 2363 | } |
Douglas Gregor | 12c9c00 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 2364 | } |
Douglas Gregor | 6920cdc | 2010-05-03 15:32:18 +0000 | [diff] [blame] | 2365 | } |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 2366 | } |
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 2367 | } else { |
| 2368 | // The function type itself was not dependent and therefore no |
| 2369 | // substitution occurred. However, we still need to instantiate |
| 2370 | // the function parameters themselves. |
Abramo Bagnara | 140a2bd | 2010-12-13 22:27:55 +0000 | [diff] [blame] | 2371 | TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens(); |
Douglas Gregor | 6920cdc | 2010-05-03 15:32:18 +0000 | [diff] [blame] | 2372 | if (FunctionProtoTypeLoc *OldProtoLoc |
| 2373 | = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) { |
| 2374 | for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) { |
| 2375 | ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i)); |
| 2376 | if (!Parm) |
| 2377 | return 0; |
| 2378 | Params.push_back(Parm); |
| 2379 | } |
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 2380 | } |
| 2381 | } |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 2382 | return NewTInfo; |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 2383 | } |
| 2384 | |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 2385 | /// Introduce the instantiated function parameters into the local |
| 2386 | /// instantiation scope, and set the parameter names to those used |
| 2387 | /// in the template. |
| 2388 | static void addInstantiatedParametersToScope(Sema &S, FunctionDecl *Function, |
| 2389 | const FunctionDecl *PatternDecl, |
| 2390 | LocalInstantiationScope &Scope, |
| 2391 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 2392 | unsigned FParamIdx = 0; |
| 2393 | for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) { |
| 2394 | const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I); |
| 2395 | if (!PatternParam->isParameterPack()) { |
| 2396 | // Simple case: not a parameter pack. |
| 2397 | assert(FParamIdx < Function->getNumParams()); |
| 2398 | ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx); |
| 2399 | FunctionParam->setDeclName(PatternParam->getDeclName()); |
| 2400 | Scope.InstantiatedLocal(PatternParam, FunctionParam); |
| 2401 | ++FParamIdx; |
| 2402 | continue; |
| 2403 | } |
| 2404 | |
| 2405 | // Expand the parameter pack. |
| 2406 | Scope.MakeInstantiatedLocalArgPack(PatternParam); |
Richard Smith | 500d729 | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 2407 | llvm::Optional<unsigned> NumArgumentsInExpansion |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 2408 | = S.getNumArgumentsInExpansion(PatternParam->getType(), TemplateArgs); |
Richard Smith | 500d729 | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 2409 | assert(NumArgumentsInExpansion && |
| 2410 | "should only be called when all template arguments are known"); |
| 2411 | for (unsigned Arg = 0; Arg < *NumArgumentsInExpansion; ++Arg) { |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 2412 | ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx); |
| 2413 | FunctionParam->setDeclName(PatternParam->getDeclName()); |
| 2414 | Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam); |
| 2415 | ++FParamIdx; |
| 2416 | } |
| 2417 | } |
| 2418 | } |
| 2419 | |
| 2420 | static void InstantiateExceptionSpec(Sema &SemaRef, FunctionDecl *New, |
| 2421 | const FunctionProtoType *Proto, |
| 2422 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Richard Smith | 13bffc5 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 2423 | assert(Proto->getExceptionSpecType() != EST_Uninstantiated); |
| 2424 | |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 2425 | // C++11 [expr.prim.general]p3: |
| 2426 | // If a declaration declares a member function or member function |
| 2427 | // template of a class X, the expression this is a prvalue of type |
| 2428 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
| 2429 | // and the end of the function-definition, member-declarator, or |
| 2430 | // declarator. |
| 2431 | CXXRecordDecl *ThisContext = 0; |
| 2432 | unsigned ThisTypeQuals = 0; |
| 2433 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(New)) { |
| 2434 | ThisContext = Method->getParent(); |
| 2435 | ThisTypeQuals = Method->getTypeQualifiers(); |
| 2436 | } |
| 2437 | Sema::CXXThisScopeRAII ThisScope(SemaRef, ThisContext, ThisTypeQuals, |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2438 | SemaRef.getLangOpts().CPlusPlus11); |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 2439 | |
| 2440 | // The function has an exception specification or a "noreturn" |
| 2441 | // attribute. Substitute into each of the exception types. |
| 2442 | SmallVector<QualType, 4> Exceptions; |
| 2443 | for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) { |
| 2444 | // FIXME: Poor location information! |
| 2445 | if (const PackExpansionType *PackExpansion |
| 2446 | = Proto->getExceptionType(I)->getAs<PackExpansionType>()) { |
| 2447 | // We have a pack expansion. Instantiate it. |
| 2448 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 2449 | SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(), |
| 2450 | Unexpanded); |
| 2451 | assert(!Unexpanded.empty() && |
| 2452 | "Pack expansion without parameter packs?"); |
| 2453 | |
| 2454 | bool Expand = false; |
| 2455 | bool RetainExpansion = false; |
| 2456 | llvm::Optional<unsigned> NumExpansions |
| 2457 | = PackExpansion->getNumExpansions(); |
| 2458 | if (SemaRef.CheckParameterPacksForExpansion(New->getLocation(), |
| 2459 | SourceRange(), |
| 2460 | Unexpanded, |
| 2461 | TemplateArgs, |
| 2462 | Expand, |
| 2463 | RetainExpansion, |
| 2464 | NumExpansions)) |
| 2465 | break; |
| 2466 | |
| 2467 | if (!Expand) { |
| 2468 | // We can't expand this pack expansion into separate arguments yet; |
| 2469 | // just substitute into the pattern and create a new pack expansion |
| 2470 | // type. |
| 2471 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1); |
| 2472 | QualType T = SemaRef.SubstType(PackExpansion->getPattern(), |
| 2473 | TemplateArgs, |
| 2474 | New->getLocation(), New->getDeclName()); |
| 2475 | if (T.isNull()) |
| 2476 | break; |
| 2477 | |
| 2478 | T = SemaRef.Context.getPackExpansionType(T, NumExpansions); |
| 2479 | Exceptions.push_back(T); |
| 2480 | continue; |
| 2481 | } |
| 2482 | |
| 2483 | // Substitute into the pack expansion pattern for each template |
| 2484 | bool Invalid = false; |
| 2485 | for (unsigned ArgIdx = 0; ArgIdx != *NumExpansions; ++ArgIdx) { |
| 2486 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, ArgIdx); |
| 2487 | |
| 2488 | QualType T = SemaRef.SubstType(PackExpansion->getPattern(), |
| 2489 | TemplateArgs, |
| 2490 | New->getLocation(), New->getDeclName()); |
| 2491 | if (T.isNull()) { |
| 2492 | Invalid = true; |
| 2493 | break; |
| 2494 | } |
| 2495 | |
| 2496 | Exceptions.push_back(T); |
| 2497 | } |
| 2498 | |
| 2499 | if (Invalid) |
| 2500 | break; |
| 2501 | |
| 2502 | continue; |
| 2503 | } |
| 2504 | |
| 2505 | QualType T |
| 2506 | = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs, |
| 2507 | New->getLocation(), New->getDeclName()); |
| 2508 | if (T.isNull() || |
| 2509 | SemaRef.CheckSpecifiedExceptionType(T, New->getLocation())) |
| 2510 | continue; |
| 2511 | |
| 2512 | Exceptions.push_back(T); |
| 2513 | } |
| 2514 | Expr *NoexceptExpr = 0; |
| 2515 | if (Expr *OldNoexceptExpr = Proto->getNoexceptExpr()) { |
| 2516 | EnterExpressionEvaluationContext Unevaluated(SemaRef, |
| 2517 | Sema::ConstantEvaluated); |
| 2518 | ExprResult E = SemaRef.SubstExpr(OldNoexceptExpr, TemplateArgs); |
| 2519 | if (E.isUsable()) |
| 2520 | E = SemaRef.CheckBooleanCondition(E.get(), E.get()->getLocStart()); |
| 2521 | |
| 2522 | if (E.isUsable()) { |
| 2523 | NoexceptExpr = E.take(); |
| 2524 | if (!NoexceptExpr->isTypeDependent() && |
| 2525 | !NoexceptExpr->isValueDependent()) |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 2526 | NoexceptExpr |
| 2527 | = SemaRef.VerifyIntegerConstantExpression(NoexceptExpr, |
| 2528 | 0, diag::err_noexcept_needs_constant_expression, |
| 2529 | /*AllowFold*/ false).take(); |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 2530 | } |
| 2531 | } |
| 2532 | |
| 2533 | // Rebuild the function type |
| 2534 | const FunctionProtoType *NewProto |
| 2535 | = New->getType()->getAs<FunctionProtoType>(); |
| 2536 | assert(NewProto && "Template instantiation without function prototype?"); |
| 2537 | |
| 2538 | FunctionProtoType::ExtProtoInfo EPI = NewProto->getExtProtoInfo(); |
| 2539 | EPI.ExceptionSpecType = Proto->getExceptionSpecType(); |
| 2540 | EPI.NumExceptions = Exceptions.size(); |
| 2541 | EPI.Exceptions = Exceptions.data(); |
| 2542 | EPI.NoexceptExpr = NoexceptExpr; |
| 2543 | |
| 2544 | New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(), |
| 2545 | NewProto->arg_type_begin(), |
| 2546 | NewProto->getNumArgs(), |
| 2547 | EPI)); |
| 2548 | } |
| 2549 | |
| 2550 | void Sema::InstantiateExceptionSpec(SourceLocation PointOfInstantiation, |
| 2551 | FunctionDecl *Decl) { |
Richard Smith | 13bffc5 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 2552 | const FunctionProtoType *Proto = Decl->getType()->castAs<FunctionProtoType>(); |
| 2553 | if (Proto->getExceptionSpecType() != EST_Uninstantiated) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 2554 | return; |
| 2555 | |
| 2556 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Decl, |
| 2557 | InstantiatingTemplate::ExceptionSpecification()); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 2558 | if (Inst) { |
| 2559 | // We hit the instantiation depth limit. Clear the exception specification |
| 2560 | // so that our callers don't have to cope with EST_Uninstantiated. |
| 2561 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
| 2562 | EPI.ExceptionSpecType = EST_None; |
| 2563 | Decl->setType(Context.getFunctionType(Proto->getResultType(), |
| 2564 | Proto->arg_type_begin(), |
| 2565 | Proto->getNumArgs(), |
| 2566 | EPI)); |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 2567 | return; |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 2568 | } |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 2569 | |
| 2570 | // Enter the scope of this instantiation. We don't use |
| 2571 | // PushDeclContext because we don't have a scope. |
| 2572 | Sema::ContextRAII savedContext(*this, Decl); |
| 2573 | LocalInstantiationScope Scope(*this); |
| 2574 | |
| 2575 | MultiLevelTemplateArgumentList TemplateArgs = |
| 2576 | getTemplateInstantiationArgs(Decl, 0, /*RelativeToPrimary*/true); |
| 2577 | |
Richard Smith | 13bffc5 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 2578 | FunctionDecl *Template = Proto->getExceptionSpecTemplate(); |
| 2579 | addInstantiatedParametersToScope(*this, Decl, Template, Scope, TemplateArgs); |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 2580 | |
Richard Smith | 13bffc5 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 2581 | ::InstantiateExceptionSpec(*this, Decl, |
| 2582 | Template->getType()->castAs<FunctionProtoType>(), |
| 2583 | TemplateArgs); |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 2584 | } |
| 2585 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2586 | /// \brief Initializes the common fields of an instantiation function |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2587 | /// declaration (New) from the corresponding fields of its template (Tmpl). |
| 2588 | /// |
| 2589 | /// \returns true if there was an error |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2590 | bool |
| 2591 | TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2592 | FunctionDecl *Tmpl) { |
David Blaikie | 85f485a | 2012-07-16 18:50:45 +0000 | [diff] [blame] | 2593 | if (Tmpl->isDeleted()) |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 2594 | New->setDeletedAsWritten(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2595 | |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 2596 | // If we are performing substituting explicitly-specified template arguments |
| 2597 | // or deduced template arguments into a function template and we reach this |
| 2598 | // point, we are now past the point where SFINAE applies and have committed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2599 | // to keeping the new function template specialization. We therefore |
| 2600 | // convert the active template instantiation for the function template |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 2601 | // into a template instantiation for this specific function template |
| 2602 | // specialization, which is not a SFINAE context, so that we diagnose any |
| 2603 | // further errors in the declaration itself. |
| 2604 | typedef Sema::ActiveTemplateInstantiation ActiveInstType; |
| 2605 | ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back(); |
| 2606 | if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution || |
| 2607 | ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2608 | if (FunctionTemplateDecl *FunTmpl |
Nick Lewycky | 4a9e60f | 2012-11-16 08:40:59 +0000 | [diff] [blame] | 2609 | = dyn_cast<FunctionTemplateDecl>(ActiveInst.Entity)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2610 | assert(FunTmpl->getTemplatedDecl() == Tmpl && |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 2611 | "Deduction from the wrong function template?"); |
Daniel Dunbar | bcbb8bd | 2009-07-16 22:10:11 +0000 | [diff] [blame] | 2612 | (void) FunTmpl; |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 2613 | ActiveInst.Kind = ActiveInstType::TemplateInstantiation; |
Nick Lewycky | 4a9e60f | 2012-11-16 08:40:59 +0000 | [diff] [blame] | 2614 | ActiveInst.Entity = New; |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 2615 | } |
| 2616 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2617 | |
Douglas Gregor | 0ae7b3f | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 2618 | const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>(); |
| 2619 | assert(Proto && "Function template without prototype?"); |
| 2620 | |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2621 | if (Proto->hasExceptionSpec() || Proto->getNoReturnAttr()) { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2622 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2623 | |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 2624 | // DR1330: In C++11, defer instantiation of a non-trivial |
| 2625 | // exception specification. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2626 | if (SemaRef.getLangOpts().CPlusPlus11 && |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 2627 | EPI.ExceptionSpecType != EST_None && |
| 2628 | EPI.ExceptionSpecType != EST_DynamicNone && |
| 2629 | EPI.ExceptionSpecType != EST_BasicNoexcept) { |
Richard Smith | 13bffc5 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 2630 | FunctionDecl *ExceptionSpecTemplate = Tmpl; |
| 2631 | if (EPI.ExceptionSpecType == EST_Uninstantiated) |
| 2632 | ExceptionSpecTemplate = EPI.ExceptionSpecTemplate; |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 2633 | assert(EPI.ExceptionSpecType != EST_Unevaluated && |
| 2634 | "instantiating implicitly-declared special member"); |
Richard Smith | 13bffc5 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 2635 | |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 2636 | // Mark the function has having an uninstantiated exception specification. |
| 2637 | const FunctionProtoType *NewProto |
| 2638 | = New->getType()->getAs<FunctionProtoType>(); |
| 2639 | assert(NewProto && "Template instantiation without function prototype?"); |
| 2640 | EPI = NewProto->getExtProtoInfo(); |
| 2641 | EPI.ExceptionSpecType = EST_Uninstantiated; |
| 2642 | EPI.ExceptionSpecDecl = New; |
Richard Smith | 13bffc5 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 2643 | EPI.ExceptionSpecTemplate = ExceptionSpecTemplate; |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 2644 | New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(), |
| 2645 | NewProto->arg_type_begin(), |
| 2646 | NewProto->getNumArgs(), |
| 2647 | EPI)); |
| 2648 | } else { |
| 2649 | ::InstantiateExceptionSpec(SemaRef, New, Proto, TemplateArgs); |
| 2650 | } |
Douglas Gregor | 0ae7b3f | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 2651 | } |
| 2652 | |
Rafael Espindola | 19f74ac | 2011-07-06 15:46:09 +0000 | [diff] [blame] | 2653 | // Get the definition. Leaves the variable unchanged if undefined. |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 2654 | const FunctionDecl *Definition = Tmpl; |
Rafael Espindola | 19f74ac | 2011-07-06 15:46:09 +0000 | [diff] [blame] | 2655 | Tmpl->isDefined(Definition); |
| 2656 | |
DeLesley Hutchins | 23323e0 | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 2657 | SemaRef.InstantiateAttrs(TemplateArgs, Definition, New, |
| 2658 | LateAttrs, StartingScope); |
Douglas Gregor | 7cf84d6 | 2010-06-15 17:05:35 +0000 | [diff] [blame] | 2659 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2660 | return false; |
| 2661 | } |
| 2662 | |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 2663 | /// \brief Initializes common fields of an instantiated method |
| 2664 | /// declaration (New) from the corresponding fields of its template |
| 2665 | /// (Tmpl). |
| 2666 | /// |
| 2667 | /// \returns true if there was an error |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2668 | bool |
| 2669 | TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New, |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 2670 | CXXMethodDecl *Tmpl) { |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2671 | if (InitFunctionInstantiation(New, Tmpl)) |
| 2672 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2673 | |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 2674 | New->setAccess(Tmpl->getAccess()); |
Fariborz Jahanian | e7184df | 2009-12-03 18:44:40 +0000 | [diff] [blame] | 2675 | if (Tmpl->isVirtualAsWritten()) |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 2676 | New->setVirtualAsWritten(true); |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 2677 | |
| 2678 | // FIXME: attributes |
| 2679 | // FIXME: New needs a pointer to Tmpl |
| 2680 | return false; |
| 2681 | } |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 2682 | |
| 2683 | /// \brief Instantiate the definition of the given function from its |
| 2684 | /// template. |
| 2685 | /// |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 2686 | /// \param PointOfInstantiation the point at which the instantiation was |
| 2687 | /// required. Note that this is not precisely a "point of instantiation" |
| 2688 | /// for the function, but it's close. |
| 2689 | /// |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 2690 | /// \param Function the already-instantiated declaration of a |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 2691 | /// function template specialization or member function of a class template |
| 2692 | /// specialization. |
| 2693 | /// |
| 2694 | /// \param Recursive if true, recursively instantiates any functions that |
| 2695 | /// are required by this instantiation. |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2696 | /// |
| 2697 | /// \param DefinitionRequired if true, then we are performing an explicit |
| 2698 | /// instantiation where the body of the function is required. Complain if |
| 2699 | /// there is no such body. |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 2700 | void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation, |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 2701 | FunctionDecl *Function, |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2702 | bool Recursive, |
| 2703 | bool DefinitionRequired) { |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 2704 | if (Function->isInvalidDecl() || Function->isDefined()) |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 2705 | return; |
| 2706 | |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2707 | // Never instantiate an explicit specialization except if it is a class scope |
| 2708 | // explicit specialization. |
| 2709 | if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization && |
| 2710 | !Function->getClassScopeSpecializationPattern()) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2711 | return; |
Douglas Gregor | 6cfacfe | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 2712 | |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 2713 | // Find the function body that we'll be substituting. |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2714 | const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern(); |
Sean Hunt | f996e05 | 2011-05-27 20:00:14 +0000 | [diff] [blame] | 2715 | assert(PatternDecl && "instantiating a non-template"); |
| 2716 | |
| 2717 | Stmt *Pattern = PatternDecl->getBody(PatternDecl); |
| 2718 | assert(PatternDecl && "template definition is not a template"); |
| 2719 | if (!Pattern) { |
| 2720 | // Try to find a defaulted definition |
| 2721 | PatternDecl->isDefined(PatternDecl); |
Sean Hunt | dfab854 | 2011-05-25 22:02:25 +0000 | [diff] [blame] | 2722 | } |
Sean Hunt | f996e05 | 2011-05-27 20:00:14 +0000 | [diff] [blame] | 2723 | assert(PatternDecl && "template definition is not a template"); |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 2724 | |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 2725 | // Postpone late parsed template instantiations. |
Sean Hunt | f996e05 | 2011-05-27 20:00:14 +0000 | [diff] [blame] | 2726 | if (PatternDecl->isLateTemplateParsed() && |
Nick Lewycky | 8a29bc0 | 2011-05-12 03:51:24 +0000 | [diff] [blame] | 2727 | !LateTemplateParser) { |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 2728 | PendingInstantiations.push_back( |
| 2729 | std::make_pair(Function, PointOfInstantiation)); |
| 2730 | return; |
| 2731 | } |
| 2732 | |
| 2733 | // Call the LateTemplateParser callback if there a need to late parse |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2734 | // a templated function definition. |
Sean Hunt | f996e05 | 2011-05-27 20:00:14 +0000 | [diff] [blame] | 2735 | if (!Pattern && PatternDecl->isLateTemplateParsed() && |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 2736 | LateTemplateParser) { |
Francois Pichet | 4a47e8d | 2011-04-23 11:52:20 +0000 | [diff] [blame] | 2737 | LateTemplateParser(OpaqueParser, PatternDecl); |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 2738 | Pattern = PatternDecl->getBody(PatternDecl); |
| 2739 | } |
| 2740 | |
Sean Hunt | f996e05 | 2011-05-27 20:00:14 +0000 | [diff] [blame] | 2741 | if (!Pattern && !PatternDecl->isDefaulted()) { |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2742 | if (DefinitionRequired) { |
| 2743 | if (Function->getPrimaryTemplate()) |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2744 | Diag(PointOfInstantiation, |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2745 | diag::err_explicit_instantiation_undefined_func_template) |
| 2746 | << Function->getPrimaryTemplate(); |
| 2747 | else |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2748 | Diag(PointOfInstantiation, |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2749 | diag::err_explicit_instantiation_undefined_member) |
| 2750 | << 1 << Function->getDeclName() << Function->getDeclContext(); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2751 | |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2752 | if (PatternDecl) |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2753 | Diag(PatternDecl->getLocation(), |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2754 | diag::note_explicit_instantiation_here); |
Douglas Gregor | cfe833b | 2010-05-17 17:57:54 +0000 | [diff] [blame] | 2755 | Function->setInvalidDecl(); |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 2756 | } else if (Function->getTemplateSpecializationKind() |
| 2757 | == TSK_ExplicitInstantiationDefinition) { |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2758 | PendingInstantiations.push_back( |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 2759 | std::make_pair(Function, PointOfInstantiation)); |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2760 | } |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 2761 | |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 2762 | return; |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2763 | } |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 2764 | |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2765 | // C++0x [temp.explicit]p9: |
| 2766 | // Except for inline functions, other explicit instantiation declarations |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2767 | // have the effect of suppressing the implicit instantiation of the entity |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2768 | // to which they refer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2769 | if (Function->getTemplateSpecializationKind() |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2770 | == TSK_ExplicitInstantiationDeclaration && |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 2771 | !PatternDecl->isInlined()) |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2772 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2773 | |
Richard Smith | d4497dd | 2013-01-25 00:08:28 +0000 | [diff] [blame] | 2774 | if (PatternDecl->isInlined()) |
| 2775 | Function->setImplicitlyInline(); |
| 2776 | |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 2777 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Function); |
| 2778 | if (Inst) |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2779 | return; |
| 2780 | |
Abramo Bagnara | e994624 | 2011-11-18 08:08:52 +0000 | [diff] [blame] | 2781 | // Copy the inner loc start from the pattern. |
| 2782 | Function->setInnerLocStart(PatternDecl->getInnerLocStart()); |
| 2783 | |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 2784 | // If we're performing recursive template instantiation, create our own |
| 2785 | // queue of pending implicit instantiations that we will instantiate later, |
| 2786 | // while we're still within our own instantiation context. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2787 | SmallVector<VTableUse, 16> SavedVTableUses; |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2788 | std::deque<PendingImplicitInstantiation> SavedPendingInstantiations; |
Nick Lewycky | 2a5f99e | 2010-11-25 00:35:20 +0000 | [diff] [blame] | 2789 | if (Recursive) { |
| 2790 | VTableUses.swap(SavedVTableUses); |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2791 | PendingInstantiations.swap(SavedPendingInstantiations); |
Nick Lewycky | 2a5f99e | 2010-11-25 00:35:20 +0000 | [diff] [blame] | 2792 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2793 | |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2794 | EnterExpressionEvaluationContext EvalContext(*this, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2795 | Sema::PotentiallyEvaluated); |
Douglas Gregor | e2c31ff | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 2796 | |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 2797 | // Introduce a new scope where local variable instantiations will be |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 2798 | // recorded, unless we're actually a member function within a local |
| 2799 | // class, in which case we need to merge our results with the parent |
| 2800 | // scope (of the enclosing function). |
| 2801 | bool MergeWithParentScope = false; |
| 2802 | if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext())) |
| 2803 | MergeWithParentScope = Rec->isLocalClass(); |
| 2804 | |
| 2805 | LocalInstantiationScope Scope(*this, MergeWithParentScope); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2806 | |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 2807 | if (PatternDecl->isDefaulted()) |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 2808 | SetDeclDefaulted(Function, PatternDecl->getLocation()); |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 2809 | else { |
| 2810 | ActOnStartOfFunctionDef(0, Function); |
| 2811 | |
| 2812 | // Enter the scope of this instantiation. We don't use |
| 2813 | // PushDeclContext because we don't have a scope. |
| 2814 | Sema::ContextRAII savedContext(*this, Function); |
| 2815 | |
| 2816 | MultiLevelTemplateArgumentList TemplateArgs = |
| 2817 | getTemplateInstantiationArgs(Function, 0, false, PatternDecl); |
| 2818 | |
| 2819 | addInstantiatedParametersToScope(*this, Function, PatternDecl, Scope, |
| 2820 | TemplateArgs); |
| 2821 | |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 2822 | // If this is a constructor, instantiate the member initializers. |
| 2823 | if (const CXXConstructorDecl *Ctor = |
| 2824 | dyn_cast<CXXConstructorDecl>(PatternDecl)) { |
| 2825 | InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor, |
| 2826 | TemplateArgs); |
| 2827 | } |
| 2828 | |
| 2829 | // Instantiate the function body. |
| 2830 | StmtResult Body = SubstStmt(Pattern, TemplateArgs); |
| 2831 | |
| 2832 | if (Body.isInvalid()) |
| 2833 | Function->setInvalidDecl(); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2834 | |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 2835 | ActOnFinishFunctionBody(Function, Body.get(), |
| 2836 | /*IsInstantiation=*/true); |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 2837 | |
| 2838 | PerformDependentDiagnostics(PatternDecl, TemplateArgs); |
| 2839 | |
| 2840 | savedContext.pop(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2841 | } |
| 2842 | |
Douglas Gregor | aba43bb | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 2843 | DeclGroupRef DG(Function); |
| 2844 | Consumer.HandleTopLevelDecl(DG); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2845 | |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 2846 | // This class may have local implicit instantiations that need to be |
| 2847 | // instantiation within this scope. |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2848 | PerformPendingInstantiations(/*LocalOnly=*/true); |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 2849 | Scope.Exit(); |
| 2850 | |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 2851 | if (Recursive) { |
Nick Lewycky | 2a5f99e | 2010-11-25 00:35:20 +0000 | [diff] [blame] | 2852 | // Define any pending vtables. |
| 2853 | DefineUsedVTables(); |
| 2854 | |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 2855 | // Instantiate any pending implicit instantiations found during the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2856 | // instantiation of this template. |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2857 | PerformPendingInstantiations(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2858 | |
Nick Lewycky | 2a5f99e | 2010-11-25 00:35:20 +0000 | [diff] [blame] | 2859 | // Restore the set of pending vtables. |
Nick Lewycky | 8155910 | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 2860 | assert(VTableUses.empty() && |
| 2861 | "VTableUses should be empty before it is discarded."); |
Nick Lewycky | 2a5f99e | 2010-11-25 00:35:20 +0000 | [diff] [blame] | 2862 | VTableUses.swap(SavedVTableUses); |
| 2863 | |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 2864 | // Restore the set of pending implicit instantiations. |
Nick Lewycky | 8155910 | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 2865 | assert(PendingInstantiations.empty() && |
| 2866 | "PendingInstantiations should be empty before it is discarded."); |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2867 | PendingInstantiations.swap(SavedPendingInstantiations); |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 2868 | } |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 2869 | } |
| 2870 | |
| 2871 | /// \brief Instantiate the definition of the given variable from its |
| 2872 | /// template. |
| 2873 | /// |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2874 | /// \param PointOfInstantiation the point at which the instantiation was |
| 2875 | /// required. Note that this is not precisely a "point of instantiation" |
| 2876 | /// for the function, but it's close. |
| 2877 | /// |
| 2878 | /// \param Var the already-instantiated declaration of a static member |
| 2879 | /// variable of a class template specialization. |
| 2880 | /// |
| 2881 | /// \param Recursive if true, recursively instantiates any functions that |
| 2882 | /// are required by this instantiation. |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2883 | /// |
| 2884 | /// \param DefinitionRequired if true, then we are performing an explicit |
| 2885 | /// instantiation where an out-of-line definition of the member variable |
| 2886 | /// is required. Complain if there is no such definition. |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2887 | void Sema::InstantiateStaticDataMemberDefinition( |
| 2888 | SourceLocation PointOfInstantiation, |
| 2889 | VarDecl *Var, |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2890 | bool Recursive, |
| 2891 | bool DefinitionRequired) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2892 | if (Var->isInvalidDecl()) |
| 2893 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2894 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2895 | // Find the out-of-line definition of this static data member. |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2896 | VarDecl *Def = Var->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2897 | assert(Def && "This data member was not instantiated from a template?"); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2898 | assert(Def->isStaticDataMember() && "Not a static data member?"); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2899 | Def = Def->getOutOfLineDefinition(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2900 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2901 | if (!Def) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2902 | // We did not find an out-of-line definition of this static data member, |
| 2903 | // so we won't perform any instantiation. Rather, we rely on the user to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2904 | // instantiate this definition (or provide a specialization for it) in |
| 2905 | // another translation unit. |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2906 | if (DefinitionRequired) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2907 | Def = Var->getInstantiatedFromStaticDataMember(); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2908 | Diag(PointOfInstantiation, |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2909 | diag::err_explicit_instantiation_undefined_member) |
| 2910 | << 2 << Var->getDeclName() << Var->getDeclContext(); |
| 2911 | Diag(Def->getLocation(), diag::note_explicit_instantiation_here); |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 2912 | } else if (Var->getTemplateSpecializationKind() |
| 2913 | == TSK_ExplicitInstantiationDefinition) { |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2914 | PendingInstantiations.push_back( |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 2915 | std::make_pair(Var, PointOfInstantiation)); |
| 2916 | } |
| 2917 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2918 | return; |
| 2919 | } |
| 2920 | |
Rafael Espindola | 234fe65 | 2012-03-05 10:54:55 +0000 | [diff] [blame] | 2921 | TemplateSpecializationKind TSK = Var->getTemplateSpecializationKind(); |
| 2922 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2923 | // Never instantiate an explicit specialization. |
Rafael Espindola | 234fe65 | 2012-03-05 10:54:55 +0000 | [diff] [blame] | 2924 | if (TSK == TSK_ExplicitSpecialization) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2925 | return; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2926 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2927 | // C++0x [temp.explicit]p9: |
| 2928 | // Except for inline functions, other explicit instantiation declarations |
| 2929 | // have the effect of suppressing the implicit instantiation of the entity |
| 2930 | // to which they refer. |
Rafael Espindola | 234fe65 | 2012-03-05 10:54:55 +0000 | [diff] [blame] | 2931 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2932 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2933 | |
Rafael Espindola | 0250393 | 2012-03-08 15:51:03 +0000 | [diff] [blame] | 2934 | Consumer.HandleCXXStaticMemberVarInstantiation(Var); |
| 2935 | |
Douglas Gregor | f15748a | 2011-06-03 03:35:07 +0000 | [diff] [blame] | 2936 | // If we already have a definition, we're done. |
Nick Lewycky | 95e3872 | 2012-04-04 02:38:36 +0000 | [diff] [blame] | 2937 | if (VarDecl *Def = Var->getDefinition()) { |
| 2938 | // We may be explicitly instantiating something we've already implicitly |
| 2939 | // instantiated. |
| 2940 | Def->setTemplateSpecializationKind(Var->getTemplateSpecializationKind(), |
| 2941 | PointOfInstantiation); |
Douglas Gregor | f15748a | 2011-06-03 03:35:07 +0000 | [diff] [blame] | 2942 | return; |
Nick Lewycky | 95e3872 | 2012-04-04 02:38:36 +0000 | [diff] [blame] | 2943 | } |
Douglas Gregor | f15748a | 2011-06-03 03:35:07 +0000 | [diff] [blame] | 2944 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2945 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Var); |
| 2946 | if (Inst) |
| 2947 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2948 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2949 | // If we're performing recursive template instantiation, create our own |
| 2950 | // queue of pending implicit instantiations that we will instantiate later, |
| 2951 | // while we're still within our own instantiation context. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2952 | SmallVector<VTableUse, 16> SavedVTableUses; |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2953 | std::deque<PendingImplicitInstantiation> SavedPendingInstantiations; |
Nick Lewycky | 8155910 | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 2954 | if (Recursive) { |
| 2955 | VTableUses.swap(SavedVTableUses); |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2956 | PendingInstantiations.swap(SavedPendingInstantiations); |
Nick Lewycky | 8155910 | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 2957 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2958 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2959 | // Enter the scope of this instantiation. We don't use |
| 2960 | // PushDeclContext because we don't have a scope. |
John McCall | f5ba7e0 | 2011-02-14 20:37:25 +0000 | [diff] [blame] | 2961 | ContextRAII previousContext(*this, Var->getDeclContext()); |
Douglas Gregor | 7bdc152 | 2012-02-16 21:36:18 +0000 | [diff] [blame] | 2962 | LocalInstantiationScope Local(*this); |
| 2963 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2964 | VarDecl *OldVar = Var; |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 2965 | Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(), |
Nico Weber | 6bb4dcb | 2010-11-28 22:53:37 +0000 | [diff] [blame] | 2966 | getTemplateInstantiationArgs(Var))); |
John McCall | f5ba7e0 | 2011-02-14 20:37:25 +0000 | [diff] [blame] | 2967 | |
| 2968 | previousContext.pop(); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2969 | |
| 2970 | if (Var) { |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 2971 | MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo(); |
| 2972 | assert(MSInfo && "Missing member specialization information?"); |
| 2973 | Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(), |
| 2974 | MSInfo->getPointOfInstantiation()); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2975 | DeclGroupRef DG(Var); |
| 2976 | Consumer.HandleTopLevelDecl(DG); |
| 2977 | } |
Douglas Gregor | 7bdc152 | 2012-02-16 21:36:18 +0000 | [diff] [blame] | 2978 | Local.Exit(); |
| 2979 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2980 | if (Recursive) { |
Nick Lewycky | 8155910 | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 2981 | // Define any newly required vtables. |
| 2982 | DefineUsedVTables(); |
| 2983 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2984 | // Instantiate any pending implicit instantiations found during the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2985 | // instantiation of this template. |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2986 | PerformPendingInstantiations(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2987 | |
Nick Lewycky | 8155910 | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 2988 | // Restore the set of pending vtables. |
| 2989 | assert(VTableUses.empty() && |
| 2990 | "VTableUses should be empty before it is discarded, " |
| 2991 | "while instantiating static data member."); |
| 2992 | VTableUses.swap(SavedVTableUses); |
| 2993 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2994 | // Restore the set of pending implicit instantiations. |
Nick Lewycky | 8155910 | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 2995 | assert(PendingInstantiations.empty() && |
| 2996 | "PendingInstantiations should be empty before it is discarded, " |
| 2997 | "while instantiating static data member."); |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2998 | PendingInstantiations.swap(SavedPendingInstantiations); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2999 | } |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 3000 | } |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 3001 | |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 3002 | void |
| 3003 | Sema::InstantiateMemInitializers(CXXConstructorDecl *New, |
| 3004 | const CXXConstructorDecl *Tmpl, |
| 3005 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3006 | |
Richard Trieu | 90ab75b | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 3007 | SmallVector<CXXCtorInitializer*, 4> NewInits; |
Richard Smith | 54b3ba8 | 2012-09-25 00:23:05 +0000 | [diff] [blame] | 3008 | bool AnyErrors = Tmpl->isInvalidDecl(); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3009 | |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 3010 | // Instantiate all the initializers. |
| 3011 | for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(), |
Douglas Gregor | 72f6d67 | 2009-09-01 21:04:42 +0000 | [diff] [blame] | 3012 | InitsEnd = Tmpl->init_end(); |
| 3013 | Inits != InitsEnd; ++Inits) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3014 | CXXCtorInitializer *Init = *Inits; |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 3015 | |
Chandler Carruth | 030ef47 | 2010-09-03 21:54:20 +0000 | [diff] [blame] | 3016 | // Only instantiate written initializers, let Sema re-construct implicit |
| 3017 | // ones. |
| 3018 | if (!Init->isWritten()) |
| 3019 | continue; |
| 3020 | |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 3021 | SourceLocation EllipsisLoc; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3022 | |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 3023 | if (Init->isPackExpansion()) { |
| 3024 | // This is a pack expansion. We should expand it now. |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 3025 | TypeLoc BaseTL = Init->getTypeSourceInfo()->getTypeLoc(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3026 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 3027 | collectUnexpandedParameterPacks(BaseTL, Unexpanded); |
| 3028 | bool ShouldExpand = false; |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 3029 | bool RetainExpansion = false; |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 3030 | llvm::Optional<unsigned> NumExpansions; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3031 | if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(), |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 3032 | BaseTL.getSourceRange(), |
David Blaikie | a71f9d0 | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 3033 | Unexpanded, |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3034 | TemplateArgs, ShouldExpand, |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 3035 | RetainExpansion, |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 3036 | NumExpansions)) { |
| 3037 | AnyErrors = true; |
| 3038 | New->setInvalidDecl(); |
| 3039 | continue; |
| 3040 | } |
| 3041 | assert(ShouldExpand && "Partial instantiation of base initializer?"); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3042 | |
| 3043 | // Loop over all of the arguments in the argument pack(s), |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 3044 | for (unsigned I = 0; I != *NumExpansions; ++I) { |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 3045 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I); |
| 3046 | |
| 3047 | // Instantiate the initializer. |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3048 | ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs, |
| 3049 | /*CXXDirectInit=*/true); |
| 3050 | if (TempInit.isInvalid()) { |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 3051 | AnyErrors = true; |
| 3052 | break; |
| 3053 | } |
| 3054 | |
| 3055 | // Instantiate the base type. |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 3056 | TypeSourceInfo *BaseTInfo = SubstType(Init->getTypeSourceInfo(), |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3057 | TemplateArgs, |
| 3058 | Init->getSourceLocation(), |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 3059 | New->getDeclName()); |
| 3060 | if (!BaseTInfo) { |
| 3061 | AnyErrors = true; |
| 3062 | break; |
| 3063 | } |
| 3064 | |
| 3065 | // Build the initializer. |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 3066 | MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(), |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3067 | BaseTInfo, TempInit.take(), |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 3068 | New->getParent(), |
| 3069 | SourceLocation()); |
| 3070 | if (NewInit.isInvalid()) { |
| 3071 | AnyErrors = true; |
| 3072 | break; |
| 3073 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3074 | |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 3075 | NewInits.push_back(NewInit.get()); |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 3076 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3077 | |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 3078 | continue; |
| 3079 | } |
| 3080 | |
Douglas Gregor | 6b98b2e | 2010-03-02 07:38:39 +0000 | [diff] [blame] | 3081 | // Instantiate the initializer. |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3082 | ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs, |
| 3083 | /*CXXDirectInit=*/true); |
| 3084 | if (TempInit.isInvalid()) { |
Douglas Gregor | 6b98b2e | 2010-03-02 07:38:39 +0000 | [diff] [blame] | 3085 | AnyErrors = true; |
| 3086 | continue; |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 3087 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3088 | |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 3089 | MemInitResult NewInit; |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 3090 | if (Init->isDelegatingInitializer() || Init->isBaseInitializer()) { |
| 3091 | TypeSourceInfo *TInfo = SubstType(Init->getTypeSourceInfo(), |
| 3092 | TemplateArgs, |
| 3093 | Init->getSourceLocation(), |
| 3094 | New->getDeclName()); |
| 3095 | if (!TInfo) { |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 3096 | AnyErrors = true; |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 3097 | New->setInvalidDecl(); |
| 3098 | continue; |
| 3099 | } |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 3100 | |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 3101 | if (Init->isBaseInitializer()) |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3102 | NewInit = BuildBaseInitializer(TInfo->getType(), TInfo, TempInit.take(), |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 3103 | New->getParent(), EllipsisLoc); |
| 3104 | else |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3105 | NewInit = BuildDelegatingInitializer(TInfo, TempInit.take(), |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 3106 | cast<CXXRecordDecl>(CurContext->getParent())); |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 3107 | } else if (Init->isMemberInitializer()) { |
Douglas Gregor | b710722 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 3108 | FieldDecl *Member = cast_or_null<FieldDecl>(FindInstantiatedDecl( |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3109 | Init->getMemberLocation(), |
| 3110 | Init->getMember(), |
| 3111 | TemplateArgs)); |
Douglas Gregor | b710722 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 3112 | if (!Member) { |
| 3113 | AnyErrors = true; |
| 3114 | New->setInvalidDecl(); |
| 3115 | continue; |
| 3116 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3117 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3118 | NewInit = BuildMemberInitializer(Member, TempInit.take(), |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 3119 | Init->getSourceLocation()); |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3120 | } else if (Init->isIndirectMemberInitializer()) { |
| 3121 | IndirectFieldDecl *IndirectMember = |
Douglas Gregor | b710722 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 3122 | cast_or_null<IndirectFieldDecl>(FindInstantiatedDecl( |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3123 | Init->getMemberLocation(), |
| 3124 | Init->getIndirectMember(), TemplateArgs)); |
| 3125 | |
Douglas Gregor | b710722 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 3126 | if (!IndirectMember) { |
| 3127 | AnyErrors = true; |
| 3128 | New->setInvalidDecl(); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 3129 | continue; |
Douglas Gregor | b710722 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 3130 | } |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 3131 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3132 | NewInit = BuildMemberInitializer(IndirectMember, TempInit.take(), |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 3133 | Init->getSourceLocation()); |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 3134 | } |
| 3135 | |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 3136 | if (NewInit.isInvalid()) { |
| 3137 | AnyErrors = true; |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 3138 | New->setInvalidDecl(); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 3139 | } else { |
Richard Trieu | 90ab75b | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 3140 | NewInits.push_back(NewInit.get()); |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 3141 | } |
| 3142 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3143 | |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 3144 | // Assign all the initializers to the new constructor. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3145 | ActOnMemInitializers(New, |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 3146 | /*FIXME: ColonLoc */ |
| 3147 | SourceLocation(), |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3148 | NewInits, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 3149 | AnyErrors); |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 3150 | } |
| 3151 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 3152 | // TODO: this could be templated if the various decl types used the |
| 3153 | // same method name. |
| 3154 | static bool isInstantiationOf(ClassTemplateDecl *Pattern, |
| 3155 | ClassTemplateDecl *Instance) { |
| 3156 | Pattern = Pattern->getCanonicalDecl(); |
| 3157 | |
| 3158 | do { |
| 3159 | Instance = Instance->getCanonicalDecl(); |
| 3160 | if (Pattern == Instance) return true; |
| 3161 | Instance = Instance->getInstantiatedFromMemberTemplate(); |
| 3162 | } while (Instance); |
| 3163 | |
| 3164 | return false; |
| 3165 | } |
| 3166 | |
Douglas Gregor | 0d69653 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 3167 | static bool isInstantiationOf(FunctionTemplateDecl *Pattern, |
| 3168 | FunctionTemplateDecl *Instance) { |
| 3169 | Pattern = Pattern->getCanonicalDecl(); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3170 | |
Douglas Gregor | 0d69653 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 3171 | do { |
| 3172 | Instance = Instance->getCanonicalDecl(); |
| 3173 | if (Pattern == Instance) return true; |
| 3174 | Instance = Instance->getInstantiatedFromMemberTemplate(); |
| 3175 | } while (Instance); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3176 | |
Douglas Gregor | 0d69653 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 3177 | return false; |
| 3178 | } |
| 3179 | |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3180 | static bool |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3181 | isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern, |
| 3182 | ClassTemplatePartialSpecializationDecl *Instance) { |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3183 | Pattern |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3184 | = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl()); |
| 3185 | do { |
| 3186 | Instance = cast<ClassTemplatePartialSpecializationDecl>( |
| 3187 | Instance->getCanonicalDecl()); |
| 3188 | if (Pattern == Instance) |
| 3189 | return true; |
| 3190 | Instance = Instance->getInstantiatedFromMember(); |
| 3191 | } while (Instance); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3192 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3193 | return false; |
| 3194 | } |
| 3195 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 3196 | static bool isInstantiationOf(CXXRecordDecl *Pattern, |
| 3197 | CXXRecordDecl *Instance) { |
| 3198 | Pattern = Pattern->getCanonicalDecl(); |
| 3199 | |
| 3200 | do { |
| 3201 | Instance = Instance->getCanonicalDecl(); |
| 3202 | if (Pattern == Instance) return true; |
| 3203 | Instance = Instance->getInstantiatedFromMemberClass(); |
| 3204 | } while (Instance); |
| 3205 | |
| 3206 | return false; |
| 3207 | } |
| 3208 | |
| 3209 | static bool isInstantiationOf(FunctionDecl *Pattern, |
| 3210 | FunctionDecl *Instance) { |
| 3211 | Pattern = Pattern->getCanonicalDecl(); |
| 3212 | |
| 3213 | do { |
| 3214 | Instance = Instance->getCanonicalDecl(); |
| 3215 | if (Pattern == Instance) return true; |
| 3216 | Instance = Instance->getInstantiatedFromMemberFunction(); |
| 3217 | } while (Instance); |
| 3218 | |
| 3219 | return false; |
| 3220 | } |
| 3221 | |
| 3222 | static bool isInstantiationOf(EnumDecl *Pattern, |
| 3223 | EnumDecl *Instance) { |
| 3224 | Pattern = Pattern->getCanonicalDecl(); |
| 3225 | |
| 3226 | do { |
| 3227 | Instance = Instance->getCanonicalDecl(); |
| 3228 | if (Pattern == Instance) return true; |
| 3229 | Instance = Instance->getInstantiatedFromMemberEnum(); |
| 3230 | } while (Instance); |
| 3231 | |
| 3232 | return false; |
| 3233 | } |
| 3234 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3235 | static bool isInstantiationOf(UsingShadowDecl *Pattern, |
| 3236 | UsingShadowDecl *Instance, |
| 3237 | ASTContext &C) { |
| 3238 | return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern; |
| 3239 | } |
| 3240 | |
| 3241 | static bool isInstantiationOf(UsingDecl *Pattern, |
| 3242 | UsingDecl *Instance, |
| 3243 | ASTContext &C) { |
| 3244 | return C.getInstantiatedFromUsingDecl(Instance) == Pattern; |
| 3245 | } |
| 3246 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3247 | static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern, |
| 3248 | UsingDecl *Instance, |
| 3249 | ASTContext &C) { |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3250 | return C.getInstantiatedFromUsingDecl(Instance) == Pattern; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3251 | } |
| 3252 | |
| 3253 | static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern, |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 3254 | UsingDecl *Instance, |
| 3255 | ASTContext &C) { |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3256 | return C.getInstantiatedFromUsingDecl(Instance) == Pattern; |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 3257 | } |
| 3258 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 3259 | static bool isInstantiationOfStaticDataMember(VarDecl *Pattern, |
| 3260 | VarDecl *Instance) { |
| 3261 | assert(Instance->isStaticDataMember()); |
| 3262 | |
| 3263 | Pattern = Pattern->getCanonicalDecl(); |
| 3264 | |
| 3265 | do { |
| 3266 | Instance = Instance->getCanonicalDecl(); |
| 3267 | if (Pattern == Instance) return true; |
| 3268 | Instance = Instance->getInstantiatedFromStaticDataMember(); |
| 3269 | } while (Instance); |
| 3270 | |
| 3271 | return false; |
| 3272 | } |
| 3273 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3274 | // Other is the prospective instantiation |
| 3275 | // D is the prospective pattern |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 3276 | static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) { |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 3277 | if (D->getKind() != Other->getKind()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3278 | if (UnresolvedUsingTypenameDecl *UUD |
| 3279 | = dyn_cast<UnresolvedUsingTypenameDecl>(D)) { |
| 3280 | if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) { |
| 3281 | return isInstantiationOf(UUD, UD, Ctx); |
| 3282 | } |
| 3283 | } |
| 3284 | |
| 3285 | if (UnresolvedUsingValueDecl *UUD |
| 3286 | = dyn_cast<UnresolvedUsingValueDecl>(D)) { |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 3287 | if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) { |
| 3288 | return isInstantiationOf(UUD, UD, Ctx); |
| 3289 | } |
| 3290 | } |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 3291 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 3292 | return false; |
| 3293 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3294 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 3295 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other)) |
| 3296 | return isInstantiationOf(cast<CXXRecordDecl>(D), Record); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3297 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 3298 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other)) |
| 3299 | return isInstantiationOf(cast<FunctionDecl>(D), Function); |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 3300 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 3301 | if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other)) |
| 3302 | return isInstantiationOf(cast<EnumDecl>(D), Enum); |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 3303 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3304 | if (VarDecl *Var = dyn_cast<VarDecl>(Other)) |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 3305 | if (Var->isStaticDataMember()) |
| 3306 | return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var); |
| 3307 | |
| 3308 | if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other)) |
| 3309 | return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp); |
Douglas Gregor | a5bf7f1 | 2009-08-28 22:03:51 +0000 | [diff] [blame] | 3310 | |
Douglas Gregor | 0d69653 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 3311 | if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other)) |
| 3312 | return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp); |
| 3313 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3314 | if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 3315 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other)) |
| 3316 | return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D), |
| 3317 | PartialSpec); |
| 3318 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 3319 | if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) { |
| 3320 | if (!Field->getDeclName()) { |
| 3321 | // This is an unnamed field. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3322 | return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) == |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 3323 | cast<FieldDecl>(D); |
| 3324 | } |
| 3325 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3326 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3327 | if (UsingDecl *Using = dyn_cast<UsingDecl>(Other)) |
| 3328 | return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx); |
| 3329 | |
| 3330 | if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other)) |
| 3331 | return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx); |
| 3332 | |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 3333 | return D->getDeclName() && isa<NamedDecl>(Other) && |
| 3334 | D->getDeclName() == cast<NamedDecl>(Other)->getDeclName(); |
| 3335 | } |
| 3336 | |
| 3337 | template<typename ForwardIterator> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3338 | static NamedDecl *findInstantiationOf(ASTContext &Ctx, |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 3339 | NamedDecl *D, |
| 3340 | ForwardIterator first, |
| 3341 | ForwardIterator last) { |
| 3342 | for (; first != last; ++first) |
| 3343 | if (isInstantiationOf(Ctx, D, *first)) |
| 3344 | return cast<NamedDecl>(*first); |
| 3345 | |
| 3346 | return 0; |
| 3347 | } |
| 3348 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 3349 | /// \brief Finds the instantiation of the given declaration context |
| 3350 | /// within the current instantiation. |
| 3351 | /// |
| 3352 | /// \returns NULL if there was an error |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 3353 | DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC, |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 3354 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 3355 | if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) { |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 3356 | Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 3357 | return cast_or_null<DeclContext>(ID); |
| 3358 | } else return DC; |
| 3359 | } |
| 3360 | |
Douglas Gregor | ed961e7 | 2009-05-27 17:54:46 +0000 | [diff] [blame] | 3361 | /// \brief Find the instantiation of the given declaration within the |
| 3362 | /// current instantiation. |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 3363 | /// |
| 3364 | /// This routine is intended to be used when \p D is a declaration |
| 3365 | /// referenced from within a template, that needs to mapped into the |
| 3366 | /// corresponding declaration within an instantiation. For example, |
| 3367 | /// given: |
| 3368 | /// |
| 3369 | /// \code |
| 3370 | /// template<typename T> |
| 3371 | /// struct X { |
| 3372 | /// enum Kind { |
| 3373 | /// KnownValue = sizeof(T) |
| 3374 | /// }; |
| 3375 | /// |
| 3376 | /// bool getKind() const { return KnownValue; } |
| 3377 | /// }; |
| 3378 | /// |
| 3379 | /// template struct X<int>; |
| 3380 | /// \endcode |
| 3381 | /// |
| 3382 | /// In the instantiation of X<int>::getKind(), we need to map the |
| 3383 | /// EnumConstantDecl for KnownValue (which refers to |
James Dennett | f198d12 | 2012-06-17 03:36:08 +0000 | [diff] [blame] | 3384 | /// X<T>::\<Kind>\::KnownValue) to its instantiation |
James Dennett | ef2b5b3 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 3385 | /// (X<int>::\<Kind>\::KnownValue). InstantiateCurrentDeclRef() performs |
Douglas Gregor | ed961e7 | 2009-05-27 17:54:46 +0000 | [diff] [blame] | 3386 | /// this mapping from within the instantiation of X<int>. |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 3387 | NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D, |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 3388 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 3389 | DeclContext *ParentDC = D->getDeclContext(); |
Douglas Gregor | 550d9b2 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 3390 | if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) || |
Douglas Gregor | 6d3e627 | 2010-02-05 19:54:12 +0000 | [diff] [blame] | 3391 | isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) || |
Douglas Gregor | 7bdc152 | 2012-02-16 21:36:18 +0000 | [diff] [blame] | 3392 | (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext()) || |
| 3393 | (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda())) { |
Douglas Gregor | 2bba76b | 2009-05-27 17:07:49 +0000 | [diff] [blame] | 3394 | // D is a local of some kind. Look into the map of local |
| 3395 | // declarations to their instantiations. |
Chris Lattner | d8e5499 | 2011-02-17 19:47:42 +0000 | [diff] [blame] | 3396 | typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack; |
| 3397 | llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found |
| 3398 | = CurrentInstantiationScope->findInstantiationOf(D); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3399 | |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 3400 | if (Found) { |
| 3401 | if (Decl *FD = Found->dyn_cast<Decl *>()) |
| 3402 | return cast<NamedDecl>(FD); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3403 | |
Richard Smith | 9a4db03 | 2012-09-12 00:56:43 +0000 | [diff] [blame] | 3404 | int PackIdx = ArgumentPackSubstitutionIndex; |
| 3405 | assert(PackIdx != -1 && "found declaration pack but not pack expanding"); |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 3406 | return cast<NamedDecl>((*Found->get<DeclArgumentPack *>())[PackIdx]); |
| 3407 | } |
| 3408 | |
| 3409 | // If we didn't find the decl, then we must have a label decl that hasn't |
| 3410 | // been found yet. Lazily instantiate it and return it now. |
| 3411 | assert(isa<LabelDecl>(D)); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3412 | |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 3413 | Decl *Inst = SubstDecl(D, CurContext, TemplateArgs); |
| 3414 | assert(Inst && "Failed to instantiate label??"); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3415 | |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 3416 | CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
| 3417 | return cast<LabelDecl>(Inst); |
Douglas Gregor | 2bba76b | 2009-05-27 17:07:49 +0000 | [diff] [blame] | 3418 | } |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 3419 | |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 3420 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 3421 | if (!Record->isDependentContext()) |
| 3422 | return D; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3423 | |
Douglas Gregor | 2c1227c | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 3424 | // Determine whether this record is the "templated" declaration describing |
| 3425 | // a class template or class template partial specialization. |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 3426 | ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate(); |
Douglas Gregor | 2c1227c | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 3427 | if (ClassTemplate) |
| 3428 | ClassTemplate = ClassTemplate->getCanonicalDecl(); |
| 3429 | else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 3430 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) |
| 3431 | ClassTemplate = PartialSpec->getSpecializedTemplate()->getCanonicalDecl(); |
| 3432 | |
| 3433 | // Walk the current context to find either the record or an instantiation of |
| 3434 | // it. |
| 3435 | DeclContext *DC = CurContext; |
| 3436 | while (!DC->isFileContext()) { |
| 3437 | // If we're performing substitution while we're inside the template |
| 3438 | // definition, we'll find our own context. We're done. |
| 3439 | if (DC->Equals(Record)) |
| 3440 | return Record; |
| 3441 | |
| 3442 | if (CXXRecordDecl *InstRecord = dyn_cast<CXXRecordDecl>(DC)) { |
| 3443 | // Check whether we're in the process of instantiating a class template |
| 3444 | // specialization of the template we're mapping. |
| 3445 | if (ClassTemplateSpecializationDecl *InstSpec |
| 3446 | = dyn_cast<ClassTemplateSpecializationDecl>(InstRecord)){ |
| 3447 | ClassTemplateDecl *SpecTemplate = InstSpec->getSpecializedTemplate(); |
| 3448 | if (ClassTemplate && isInstantiationOf(ClassTemplate, SpecTemplate)) |
| 3449 | return InstRecord; |
| 3450 | } |
| 3451 | |
| 3452 | // Check whether we're in the process of instantiating a member class. |
| 3453 | if (isInstantiationOf(Record, InstRecord)) |
| 3454 | return InstRecord; |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 3455 | } |
Douglas Gregor | 2c1227c | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 3456 | |
| 3457 | |
| 3458 | // Move to the outer template scope. |
| 3459 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) { |
| 3460 | if (FD->getFriendObjectKind() && FD->getDeclContext()->isFileContext()){ |
| 3461 | DC = FD->getLexicalDeclContext(); |
| 3462 | continue; |
| 3463 | } |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 3464 | } |
Douglas Gregor | 2c1227c | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 3465 | |
| 3466 | DC = DC->getParent(); |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 3467 | } |
Douglas Gregor | 8b013bd | 2010-02-05 22:40:03 +0000 | [diff] [blame] | 3468 | |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 3469 | // Fall through to deal with other dependent record types (e.g., |
| 3470 | // anonymous unions in class templates). |
| 3471 | } |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 3472 | |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 3473 | if (!ParentDC->isDependentContext()) |
| 3474 | return D; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3475 | |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 3476 | ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3477 | if (!ParentDC) |
Douglas Gregor | 44c7384 | 2009-09-01 17:53:10 +0000 | [diff] [blame] | 3478 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3479 | |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 3480 | if (ParentDC != D->getDeclContext()) { |
| 3481 | // We performed some kind of instantiation in the parent context, |
| 3482 | // so now we need to look into the instantiated parent context to |
| 3483 | // find the instantiation of the declaration D. |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 3484 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3485 | // If our context used to be dependent, we may need to instantiate |
| 3486 | // it before performing lookup into that context. |
Douglas Gregor | eff1dbe | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 3487 | bool IsBeingInstantiated = false; |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3488 | if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) { |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 3489 | if (!Spec->isDependentContext()) { |
| 3490 | QualType T = Context.getTypeDeclType(Spec); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3491 | const RecordType *Tag = T->getAs<RecordType>(); |
| 3492 | assert(Tag && "type of non-dependent record is not a RecordType"); |
Douglas Gregor | eff1dbe | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 3493 | if (Tag->isBeingDefined()) |
| 3494 | IsBeingInstantiated = true; |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3495 | if (!Tag->isBeingDefined() && |
| 3496 | RequireCompleteType(Loc, T, diag::err_incomplete_type)) |
| 3497 | return 0; |
Douglas Gregor | a43064c | 2010-11-05 23:22:45 +0000 | [diff] [blame] | 3498 | |
| 3499 | ParentDC = Tag->getDecl(); |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 3500 | } |
| 3501 | } |
| 3502 | |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 3503 | NamedDecl *Result = 0; |
| 3504 | if (D->getDeclName()) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3505 | DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName()); |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3506 | Result = findInstantiationOf(Context, D, Found.begin(), Found.end()); |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 3507 | } else { |
| 3508 | // Since we don't have a name for the entity we're looking for, |
| 3509 | // our only option is to walk through all of the declarations to |
| 3510 | // find that name. This will occur in a few cases: |
| 3511 | // |
| 3512 | // - anonymous struct/union within a template |
| 3513 | // - unnamed class/struct/union/enum within a template |
| 3514 | // |
| 3515 | // FIXME: Find a better way to find these instantiations! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3516 | Result = findInstantiationOf(Context, D, |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3517 | ParentDC->decls_begin(), |
| 3518 | ParentDC->decls_end()); |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 3519 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3520 | |
Douglas Gregor | eff1dbe | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 3521 | if (!Result) { |
| 3522 | if (isa<UsingShadowDecl>(D)) { |
| 3523 | // UsingShadowDecls can instantiate to nothing because of using hiding. |
| 3524 | } else if (Diags.hasErrorOccurred()) { |
| 3525 | // We've already complained about something, so most likely this |
| 3526 | // declaration failed to instantiate. There's no point in complaining |
| 3527 | // further, since this is normal in invalid code. |
| 3528 | } else if (IsBeingInstantiated) { |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3529 | // The class in which this member exists is currently being |
Douglas Gregor | eff1dbe | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 3530 | // instantiated, and we haven't gotten around to instantiating this |
| 3531 | // member yet. This can happen when the code uses forward declarations |
| 3532 | // of member classes, and introduces ordering dependencies via |
| 3533 | // template instantiation. |
| 3534 | Diag(Loc, diag::err_member_not_yet_instantiated) |
| 3535 | << D->getDeclName() |
| 3536 | << Context.getTypeDeclType(cast<CXXRecordDecl>(ParentDC)); |
| 3537 | Diag(D->getLocation(), diag::note_non_instantiated_member_here); |
Richard Smith | 0724b7c | 2012-03-26 20:28:16 +0000 | [diff] [blame] | 3538 | } else if (EnumConstantDecl *ED = dyn_cast<EnumConstantDecl>(D)) { |
| 3539 | // This enumeration constant was found when the template was defined, |
| 3540 | // but can't be found in the instantiation. This can happen if an |
| 3541 | // unscoped enumeration member is explicitly specialized. |
| 3542 | EnumDecl *Enum = cast<EnumDecl>(ED->getLexicalDeclContext()); |
| 3543 | EnumDecl *Spec = cast<EnumDecl>(FindInstantiatedDecl(Loc, Enum, |
| 3544 | TemplateArgs)); |
| 3545 | assert(Spec->getTemplateSpecializationKind() == |
| 3546 | TSK_ExplicitSpecialization); |
| 3547 | Diag(Loc, diag::err_enumerator_does_not_exist) |
| 3548 | << D->getDeclName() |
| 3549 | << Context.getTypeDeclType(cast<TypeDecl>(Spec->getDeclContext())); |
| 3550 | Diag(Spec->getLocation(), diag::note_enum_specialized_here) |
| 3551 | << Context.getTypeDeclType(Spec); |
Douglas Gregor | eff1dbe | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 3552 | } else { |
| 3553 | // We should have found something, but didn't. |
| 3554 | llvm_unreachable("Unable to find instantiation of declaration!"); |
| 3555 | } |
| 3556 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3557 | |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 3558 | D = Result; |
| 3559 | } |
| 3560 | |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 3561 | return D; |
| 3562 | } |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 3563 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3564 | /// \brief Performs template instantiation for all implicit template |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 3565 | /// instantiations we have seen until this point. |
Nick Lewycky | 8155910 | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 3566 | void Sema::PerformPendingInstantiations(bool LocalOnly) { |
Douglas Gregor | 6e4a3f5 | 2011-07-28 19:49:54 +0000 | [diff] [blame] | 3567 | // Load pending instantiations from the external source. |
| 3568 | if (!LocalOnly && ExternalSource) { |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 3569 | SmallVector<PendingImplicitInstantiation, 4> Pending; |
Douglas Gregor | 6e4a3f5 | 2011-07-28 19:49:54 +0000 | [diff] [blame] | 3570 | ExternalSource->ReadPendingInstantiations(Pending); |
| 3571 | PendingInstantiations.insert(PendingInstantiations.begin(), |
| 3572 | Pending.begin(), Pending.end()); |
| 3573 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3574 | |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 3575 | while (!PendingLocalImplicitInstantiations.empty() || |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 3576 | (!LocalOnly && !PendingInstantiations.empty())) { |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 3577 | PendingImplicitInstantiation Inst; |
| 3578 | |
| 3579 | if (PendingLocalImplicitInstantiations.empty()) { |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 3580 | Inst = PendingInstantiations.front(); |
| 3581 | PendingInstantiations.pop_front(); |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 3582 | } else { |
| 3583 | Inst = PendingLocalImplicitInstantiations.front(); |
| 3584 | PendingLocalImplicitInstantiations.pop_front(); |
| 3585 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3586 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3587 | // Instantiate function definitions |
| 3588 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3589 | PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(), |
| 3590 | "instantiating function definition"); |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 3591 | bool DefinitionRequired = Function->getTemplateSpecializationKind() == |
| 3592 | TSK_ExplicitInstantiationDefinition; |
| 3593 | InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true, |
| 3594 | DefinitionRequired); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3595 | continue; |
| 3596 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3597 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3598 | // Instantiate static data member definitions. |
| 3599 | VarDecl *Var = cast<VarDecl>(Inst.first); |
| 3600 | assert(Var->isStaticDataMember() && "Not a static data member?"); |
Anders Carlsson | c17fb7b | 2009-09-01 05:12:24 +0000 | [diff] [blame] | 3601 | |
Chandler Carruth | 291b441 | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 3602 | // Don't try to instantiate declarations if the most recent redeclaration |
| 3603 | // is invalid. |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 3604 | if (Var->getMostRecentDecl()->isInvalidDecl()) |
Chandler Carruth | 291b441 | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 3605 | continue; |
| 3606 | |
| 3607 | // Check if the most recent declaration has changed the specialization kind |
| 3608 | // and removed the need for implicit instantiation. |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 3609 | switch (Var->getMostRecentDecl()->getTemplateSpecializationKind()) { |
Chandler Carruth | 291b441 | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 3610 | case TSK_Undeclared: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3611 | llvm_unreachable("Cannot instantitiate an undeclared specialization."); |
Chandler Carruth | 291b441 | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 3612 | case TSK_ExplicitInstantiationDeclaration: |
Chandler Carruth | 291b441 | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 3613 | case TSK_ExplicitSpecialization: |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 3614 | continue; // No longer need to instantiate this type. |
| 3615 | case TSK_ExplicitInstantiationDefinition: |
| 3616 | // We only need an instantiation if the pending instantiation *is* the |
| 3617 | // explicit instantiation. |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 3618 | if (Var != Var->getMostRecentDecl()) continue; |
Chandler Carruth | 291b441 | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 3619 | case TSK_ImplicitInstantiation: |
| 3620 | break; |
| 3621 | } |
| 3622 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3623 | PrettyDeclStackTraceEntry CrashInfo(*this, Var, Var->getLocation(), |
| 3624 | "instantiating static data member " |
| 3625 | "definition"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3626 | |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 3627 | bool DefinitionRequired = Var->getTemplateSpecializationKind() == |
| 3628 | TSK_ExplicitInstantiationDefinition; |
| 3629 | InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true, |
| 3630 | DefinitionRequired); |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 3631 | } |
| 3632 | } |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 3633 | |
| 3634 | void Sema::PerformDependentDiagnostics(const DeclContext *Pattern, |
| 3635 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 3636 | for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(), |
| 3637 | E = Pattern->ddiag_end(); I != E; ++I) { |
| 3638 | DependentDiagnostic *DD = *I; |
| 3639 | |
| 3640 | switch (DD->getKind()) { |
| 3641 | case DependentDiagnostic::Access: |
| 3642 | HandleDependentAccessCheck(*DD, TemplateArgs); |
| 3643 | break; |
| 3644 | } |
| 3645 | } |
| 3646 | } |