blob: e03e8c55c9af18ec4cad6c6e69e585e33fda7aea [file] [log] [blame]
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001//===--- 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 McCall2d887082010-08-25 22:03:47 +000012#include "clang/Sema/SemaInternal.h"
Douglas Gregoraba43bb2009-05-26 20:50:29 +000013#include "clang/AST/ASTConsumer.h"
Douglas Gregor8dbc2692009-03-17 21:15:40 +000014#include "clang/AST/ASTContext.h"
15#include "clang/AST/DeclTemplate.h"
16#include "clang/AST/DeclVisitor.h"
John McCall0c01d182010-03-24 05:22:00 +000017#include "clang/AST/DependentDiagnostic.h"
Douglas Gregor8dbc2692009-03-17 21:15:40 +000018#include "clang/AST/Expr.h"
Douglas Gregora88cfbf2009-12-12 18:16:41 +000019#include "clang/AST/ExprCXX.h"
John McCall21ef0fa2010-03-11 09:03:00 +000020#include "clang/AST/TypeLoc.h"
Douglas Gregor83ddad32009-08-26 21:14:46 +000021#include "clang/Lex/Preprocessor.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000022#include "clang/Sema/Lookup.h"
23#include "clang/Sema/PrettyDeclStackTrace.h"
24#include "clang/Sema/Template.h"
Douglas Gregor8dbc2692009-03-17 21:15:40 +000025
26using namespace clang;
27
John McCallb6217662010-03-15 10:12:16 +000028bool TemplateDeclInstantiator::SubstQualifier(const DeclaratorDecl *OldDecl,
29 DeclaratorDecl *NewDecl) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +000030 if (!OldDecl->getQualifierLoc())
31 return false;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +000032
Douglas Gregorc22b5ff2011-02-25 02:25:35 +000033 NestedNameSpecifierLoc NewQualifierLoc
NAKAMURA Takumia789ca92011-10-08 11:31:46 +000034 = SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(),
Douglas Gregorc22b5ff2011-02-25 02:25:35 +000035 TemplateArgs);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +000036
Douglas Gregorc22b5ff2011-02-25 02:25:35 +000037 if (!NewQualifierLoc)
John McCallb6217662010-03-15 10:12:16 +000038 return true;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +000039
Douglas Gregorc22b5ff2011-02-25 02:25:35 +000040 NewDecl->setQualifierInfo(NewQualifierLoc);
John McCallb6217662010-03-15 10:12:16 +000041 return false;
42}
43
44bool TemplateDeclInstantiator::SubstQualifier(const TagDecl *OldDecl,
45 TagDecl *NewDecl) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +000046 if (!OldDecl->getQualifierLoc())
47 return false;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +000048
Douglas Gregorc22b5ff2011-02-25 02:25:35 +000049 NestedNameSpecifierLoc NewQualifierLoc
NAKAMURA Takumia789ca92011-10-08 11:31:46 +000050 = SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(),
Douglas Gregorc22b5ff2011-02-25 02:25:35 +000051 TemplateArgs);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +000052
Douglas Gregorc22b5ff2011-02-25 02:25:35 +000053 if (!NewQualifierLoc)
John McCallb6217662010-03-15 10:12:16 +000054 return true;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +000055
Douglas Gregorc22b5ff2011-02-25 02:25:35 +000056 NewDecl->setQualifierInfo(NewQualifierLoc);
John McCallb6217662010-03-15 10:12:16 +000057 return false;
58}
59
DeLesley Hutchins7b9ff0c2012-01-20 22:37:06 +000060// Include attribute instantiation code.
61#include "clang/Sema/AttrTemplateInstantiate.inc"
62
Richard Smithf6565a92013-02-22 08:32:16 +000063static void instantiateDependentAlignedAttr(
64 Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs,
65 const AlignedAttr *Aligned, Decl *New, bool IsPackExpansion) {
66 if (Aligned->isAlignmentExpr()) {
67 // The alignment expression is a constant expression.
68 EnterExpressionEvaluationContext Unevaluated(S, Sema::ConstantEvaluated);
69 ExprResult Result = S.SubstExpr(Aligned->getAlignmentExpr(), TemplateArgs);
70 if (!Result.isInvalid())
71 S.AddAlignedAttr(Aligned->getLocation(), New, Result.takeAs<Expr>(),
72 Aligned->getSpellingListIndex(), IsPackExpansion);
73 } else {
74 TypeSourceInfo *Result = S.SubstType(Aligned->getAlignmentType(),
75 TemplateArgs, Aligned->getLocation(),
76 DeclarationName());
77 if (Result)
78 S.AddAlignedAttr(Aligned->getLocation(), New, Result,
79 Aligned->getSpellingListIndex(), IsPackExpansion);
80 }
81}
82
83static void instantiateDependentAlignedAttr(
84 Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs,
85 const AlignedAttr *Aligned, Decl *New) {
86 if (!Aligned->isPackExpansion()) {
87 instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, false);
88 return;
89 }
90
91 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
92 if (Aligned->isAlignmentExpr())
93 S.collectUnexpandedParameterPacks(Aligned->getAlignmentExpr(),
94 Unexpanded);
95 else
96 S.collectUnexpandedParameterPacks(Aligned->getAlignmentType()->getTypeLoc(),
97 Unexpanded);
98 assert(!Unexpanded.empty() && "Pack expansion without parameter packs?");
99
100 // Determine whether we can expand this attribute pack yet.
101 bool Expand = true, RetainExpansion = false;
102 Optional<unsigned> NumExpansions;
103 // FIXME: Use the actual location of the ellipsis.
104 SourceLocation EllipsisLoc = Aligned->getLocation();
105 if (S.CheckParameterPacksForExpansion(EllipsisLoc, Aligned->getRange(),
106 Unexpanded, TemplateArgs, Expand,
107 RetainExpansion, NumExpansions))
108 return;
109
110 if (!Expand) {
111 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(S, -1);
112 instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, true);
113 } else {
114 for (unsigned I = 0; I != *NumExpansions; ++I) {
115 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(S, I);
116 instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, false);
117 }
118 }
119}
120
John McCall1d8d1cc2010-08-01 02:01:53 +0000121void Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs,
DeLesley Hutchins23323e02012-01-20 22:50:54 +0000122 const Decl *Tmpl, Decl *New,
123 LateInstantiatedAttrVec *LateAttrs,
124 LocalInstantiationScope *OuterMostScope) {
Sean Huntcf807c42010-08-18 23:23:40 +0000125 for (AttrVec::const_iterator i = Tmpl->attr_begin(), e = Tmpl->attr_end();
126 i != e; ++i) {
127 const Attr *TmplAttr = *i;
DeLesley Hutchins23323e02012-01-20 22:50:54 +0000128
Chandler Carruth4ced79f2010-06-25 03:22:07 +0000129 // FIXME: This should be generalized to more than just the AlignedAttr.
Richard Smithf6565a92013-02-22 08:32:16 +0000130 const AlignedAttr *Aligned = dyn_cast<AlignedAttr>(TmplAttr);
131 if (Aligned && Aligned->isAlignmentDependent()) {
132 instantiateDependentAlignedAttr(*this, TemplateArgs, Aligned, New);
133 continue;
Chandler Carruth4ced79f2010-06-25 03:22:07 +0000134 }
135
Richard Smithf6565a92013-02-22 08:32:16 +0000136 assert(!TmplAttr->isPackExpansion());
DeLesley Hutchins23323e02012-01-20 22:50:54 +0000137 if (TmplAttr->isLateParsed() && LateAttrs) {
138 // Late parsed attributes must be instantiated and attached after the
139 // enclosing class has been instantiated. See Sema::InstantiateClass.
140 LocalInstantiationScope *Saved = 0;
141 if (CurrentInstantiationScope)
142 Saved = CurrentInstantiationScope->cloneScopes(OuterMostScope);
143 LateAttrs->push_back(LateInstantiatedAttribute(TmplAttr, Saved, New));
144 } else {
Richard Smithcafeb942013-06-07 02:33:37 +0000145 // Allow 'this' within late-parsed attributes.
146 NamedDecl *ND = dyn_cast<NamedDecl>(New);
147 CXXRecordDecl *ThisContext =
148 dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext());
149 CXXThisScopeRAII ThisScope(*this, ThisContext, /*TypeQuals*/0,
150 ND && ND->isCXXInstanceMember());
151
Benjamin Kramer5bbc3852012-02-06 11:13:08 +0000152 Attr *NewAttr = sema::instantiateTemplateAttribute(TmplAttr, Context,
153 *this, TemplateArgs);
Rafael Espindola31c195a2012-05-15 14:09:55 +0000154 if (NewAttr)
155 New->addAttr(NewAttr);
DeLesley Hutchins23323e02012-01-20 22:50:54 +0000156 }
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000157 }
158}
159
Douglas Gregor4f722be2009-03-25 15:45:12 +0000160Decl *
161TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000162 llvm_unreachable("Translation units cannot be instantiated");
Douglas Gregor4f722be2009-03-25 15:45:12 +0000163}
164
165Decl *
Chris Lattner57ad3782011-02-17 20:34:02 +0000166TemplateDeclInstantiator::VisitLabelDecl(LabelDecl *D) {
167 LabelDecl *Inst = LabelDecl::Create(SemaRef.Context, Owner, D->getLocation(),
168 D->getIdentifier());
169 Owner->addDecl(Inst);
170 return Inst;
171}
172
173Decl *
Douglas Gregor4f722be2009-03-25 15:45:12 +0000174TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000175 llvm_unreachable("Namespaces cannot be instantiated");
Douglas Gregor4f722be2009-03-25 15:45:12 +0000176}
177
John McCall3dbd3d52010-02-16 06:53:13 +0000178Decl *
179TemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
180 NamespaceAliasDecl *Inst
181 = NamespaceAliasDecl::Create(SemaRef.Context, Owner,
182 D->getNamespaceLoc(),
183 D->getAliasLoc(),
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +0000184 D->getIdentifier(),
185 D->getQualifierLoc(),
John McCall3dbd3d52010-02-16 06:53:13 +0000186 D->getTargetNameLoc(),
187 D->getNamespace());
188 Owner->addDecl(Inst);
189 return Inst;
190}
191
Richard Smith3e4c6c42011-05-05 21:57:07 +0000192Decl *TemplateDeclInstantiator::InstantiateTypedefNameDecl(TypedefNameDecl *D,
193 bool IsTypeAlias) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000194 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000195 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor561f8122011-07-01 01:22:09 +0000196 if (DI->getType()->isInstantiationDependentType() ||
Douglas Gregor836adf62010-05-24 17:22:01 +0000197 DI->getType()->isVariablyModifiedType()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000198 DI = SemaRef.SubstType(DI, TemplateArgs,
199 D->getLocation(), D->getDeclName());
200 if (!DI) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000201 Invalid = true;
John McCalla93c9342009-12-07 02:54:59 +0000202 DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000203 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000204 } else {
205 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000206 }
Mike Stump1eb44332009-09-09 15:08:12 +0000207
Richard Smithb5b37d12012-10-23 00:32:41 +0000208 // HACK: g++ has a bug where it gets the value kind of ?: wrong.
209 // libstdc++ relies upon this bug in its implementation of common_type.
210 // If we happen to be processing that implementation, fake up the g++ ?:
211 // semantics. See LWG issue 2141 for more information on the bug.
212 const DecltypeType *DT = DI->getType()->getAs<DecltypeType>();
213 CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D->getDeclContext());
214 if (DT && RD && isa<ConditionalOperator>(DT->getUnderlyingExpr()) &&
215 DT->isReferenceType() &&
216 RD->getEnclosingNamespaceContext() == SemaRef.getStdNamespace() &&
217 RD->getIdentifier() && RD->getIdentifier()->isStr("common_type") &&
218 D->getIdentifier() && D->getIdentifier()->isStr("type") &&
219 SemaRef.getSourceManager().isInSystemHeader(D->getLocStart()))
220 // Fold it to the (non-reference) type which g++ would have produced.
221 DI = SemaRef.Context.getTrivialTypeSourceInfo(
222 DI->getType().getNonReferenceType());
223
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000224 // Create the new typedef
Richard Smith162e1c12011-04-15 14:24:37 +0000225 TypedefNameDecl *Typedef;
226 if (IsTypeAlias)
227 Typedef = TypeAliasDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
228 D->getLocation(), D->getIdentifier(), DI);
229 else
230 Typedef = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
231 D->getLocation(), D->getIdentifier(), DI);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000232 if (Invalid)
233 Typedef->setInvalidDecl();
234
John McCallcde5a402011-02-01 08:20:08 +0000235 // If the old typedef was the name for linkage purposes of an anonymous
236 // tag decl, re-establish that relationship for the new typedef.
237 if (const TagType *oldTagType = D->getUnderlyingType()->getAs<TagType>()) {
238 TagDecl *oldTag = oldTagType->getDecl();
Douglas Gregorc61361b2013-03-08 22:15:15 +0000239 if (oldTag->getTypedefNameForAnonDecl() == D && !Invalid) {
John McCallcde5a402011-02-01 08:20:08 +0000240 TagDecl *newTag = DI->getType()->castAs<TagType>()->getDecl();
John McCall83972f12013-03-09 00:54:27 +0000241 assert(!newTag->hasNameForLinkage());
Richard Smith162e1c12011-04-15 14:24:37 +0000242 newTag->setTypedefNameForAnonDecl(Typedef);
John McCallcde5a402011-02-01 08:20:08 +0000243 }
Douglas Gregord57a38e2010-04-23 16:25:07 +0000244 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000245
Douglas Gregoref96ee02012-01-14 16:38:05 +0000246 if (TypedefNameDecl *Prev = D->getPreviousDecl()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000247 NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
248 TemplateArgs);
Douglas Gregorb7107222011-03-04 19:46:35 +0000249 if (!InstPrev)
250 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000251
Rafael Espindola5df37bd2011-12-26 22:42:47 +0000252 TypedefNameDecl *InstPrevTypedef = cast<TypedefNameDecl>(InstPrev);
253
254 // If the typedef types are not identical, reject them.
255 SemaRef.isIncompatibleTypedef(InstPrevTypedef, Typedef);
256
257 Typedef->setPreviousDeclaration(InstPrevTypedef);
John McCall5126fd02009-12-30 00:31:22 +0000258 }
259
John McCall1d8d1cc2010-08-01 02:01:53 +0000260 SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef);
Douglas Gregord57a38e2010-04-23 16:25:07 +0000261
John McCall46460a62010-01-20 21:53:11 +0000262 Typedef->setAccess(D->getAccess());
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000264 return Typedef;
265}
266
Richard Smith162e1c12011-04-15 14:24:37 +0000267Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
Richard Smith3e4c6c42011-05-05 21:57:07 +0000268 Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/false);
269 Owner->addDecl(Typedef);
270 return Typedef;
Richard Smith162e1c12011-04-15 14:24:37 +0000271}
272
273Decl *TemplateDeclInstantiator::VisitTypeAliasDecl(TypeAliasDecl *D) {
Richard Smith3e4c6c42011-05-05 21:57:07 +0000274 Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/true);
275 Owner->addDecl(Typedef);
276 return Typedef;
277}
278
279Decl *
280TemplateDeclInstantiator::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
281 // Create a local instantiation scope for this type alias template, which
282 // will contain the instantiations of the template parameters.
283 LocalInstantiationScope Scope(SemaRef);
284
285 TemplateParameterList *TempParams = D->getTemplateParameters();
286 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
287 if (!InstParams)
288 return 0;
289
290 TypeAliasDecl *Pattern = D->getTemplatedDecl();
291
292 TypeAliasTemplateDecl *PrevAliasTemplate = 0;
Douglas Gregoref96ee02012-01-14 16:38:05 +0000293 if (Pattern->getPreviousDecl()) {
Richard Smith3e4c6c42011-05-05 21:57:07 +0000294 DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
David Blaikie3bc93e32012-12-19 00:45:41 +0000295 if (!Found.empty()) {
296 PrevAliasTemplate = dyn_cast<TypeAliasTemplateDecl>(Found.front());
Richard Smith3e4c6c42011-05-05 21:57:07 +0000297 }
298 }
299
300 TypeAliasDecl *AliasInst = cast_or_null<TypeAliasDecl>(
301 InstantiateTypedefNameDecl(Pattern, /*IsTypeAlias=*/true));
302 if (!AliasInst)
303 return 0;
304
305 TypeAliasTemplateDecl *Inst
306 = TypeAliasTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
307 D->getDeclName(), InstParams, AliasInst);
308 if (PrevAliasTemplate)
309 Inst->setPreviousDeclaration(PrevAliasTemplate);
310
311 Inst->setAccess(D->getAccess());
312
313 if (!PrevAliasTemplate)
314 Inst->setInstantiatedFromMemberTemplate(D);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000315
Richard Smith3e4c6c42011-05-05 21:57:07 +0000316 Owner->addDecl(Inst);
317
318 return Inst;
Richard Smith162e1c12011-04-15 14:24:37 +0000319}
320
Larisse Voufoef4579c2013-08-06 01:03:05 +0000321// FIXME: Revise for static member templates.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000322Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
Larisse Voufo567f9172013-08-22 00:59:14 +0000323 return VisitVarDecl(D, /*InstantiatingVarTemplate=*/false);
Larisse Voufoef4579c2013-08-06 01:03:05 +0000324}
325
Larisse Voufo567f9172013-08-22 00:59:14 +0000326Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D,
327 bool InstantiatingVarTemplate) {
Larisse Voufoef4579c2013-08-06 01:03:05 +0000328
Douglas Gregor9901c572010-05-21 00:31:19 +0000329 // If this is the variable for an anonymous struct or union,
330 // instantiate the anonymous struct/union type first.
331 if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
332 if (RecordTy->getDecl()->isAnonymousStructOrUnion())
333 if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
334 return 0;
335
John McCallce3ff2b2009-08-25 22:02:44 +0000336 // Do substitution on the type of the declaration
John McCalla93c9342009-12-07 02:54:59 +0000337 TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
John McCall0a5fa062009-10-21 02:39:02 +0000338 TemplateArgs,
339 D->getTypeSpecStartLoc(),
340 D->getDeclName());
341 if (!DI)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000342 return 0;
343
Douglas Gregorc6dbc3f2010-09-12 07:37:24 +0000344 if (DI->getType()->isFunctionType()) {
345 SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function)
346 << D->isStaticDataMember() << DI->getType();
347 return 0;
348 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000349
Richard Smitha41c97a2013-09-20 01:15:31 +0000350 DeclContext *DC = Owner;
351 if (D->isLocalExternDecl())
352 SemaRef.adjustContextForLocalExternDecl(DC);
353
Larisse Voufoef4579c2013-08-06 01:03:05 +0000354 // Build the instantiated declaration.
Richard Smitha41c97a2013-09-20 01:15:31 +0000355 VarDecl *Var = VarDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(),
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000356 D->getLocation(), D->getIdentifier(),
Larisse Voufoef4579c2013-08-06 01:03:05 +0000357 DI->getType(), DI, D->getStorageClass());
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Douglas Gregor9aab9c42011-12-10 01:22:52 +0000359 // In ARC, infer 'retaining' for variables of retainable type.
David Blaikie4e4d0842012-03-11 07:00:24 +0000360 if (SemaRef.getLangOpts().ObjCAutoRefCount &&
Douglas Gregor9aab9c42011-12-10 01:22:52 +0000361 SemaRef.inferObjCARCLifetime(Var))
362 Var->setInvalidDecl();
363
Larisse Voufoef4579c2013-08-06 01:03:05 +0000364 // Substitute the nested name specifier, if any.
365 if (SubstQualifier(D, Var))
366 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Richard Smitha41c97a2013-09-20 01:15:31 +0000368 SemaRef.BuildVariableInstantiation(Var, D, TemplateArgs, LateAttrs, Owner,
Larisse Voufo567f9172013-08-22 00:59:14 +0000369 StartingScope, InstantiatingVarTemplate);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000370 return Var;
371}
372
Abramo Bagnara6206d532010-06-05 05:09:32 +0000373Decl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
374 AccessSpecDecl* AD
375 = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
376 D->getAccessSpecifierLoc(), D->getColonLoc());
377 Owner->addHiddenDecl(AD);
378 return AD;
379}
380
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000381Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
382 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000383 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor561f8122011-07-01 01:22:09 +0000384 if (DI->getType()->isInstantiationDependentType() ||
Douglas Gregor836adf62010-05-24 17:22:01 +0000385 DI->getType()->isVariablyModifiedType()) {
John McCall07fb6be2009-10-22 23:33:21 +0000386 DI = SemaRef.SubstType(DI, TemplateArgs,
387 D->getLocation(), D->getDeclName());
388 if (!DI) {
John McCalla93c9342009-12-07 02:54:59 +0000389 DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000390 Invalid = true;
391 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000392 // C++ [temp.arg.type]p3:
393 // If a declaration acquires a function type through a type
394 // dependent on a template-parameter and this causes a
395 // declaration that does not use the syntactic form of a
396 // function declarator to have function type, the program is
397 // ill-formed.
398 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000399 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000400 Invalid = true;
401 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000402 } else {
403 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000404 }
405
406 Expr *BitWidth = D->getBitWidth();
407 if (Invalid)
408 BitWidth = 0;
409 else if (BitWidth) {
Richard Smithf6702a32011-12-20 02:08:33 +0000410 // The bit-width expression is a constant expression.
411 EnterExpressionEvaluationContext Unevaluated(SemaRef,
412 Sema::ConstantEvaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000413
John McCall60d7b3a2010-08-24 06:29:42 +0000414 ExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000415 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000416 if (InstantiatedBitWidth.isInvalid()) {
417 Invalid = true;
418 BitWidth = 0;
419 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000420 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000421 }
422
John McCall07fb6be2009-10-22 23:33:21 +0000423 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
424 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000425 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000426 D->getLocation(),
427 D->isMutable(),
428 BitWidth,
Richard Smithca523302012-06-10 03:12:00 +0000429 D->getInClassInitStyle(),
Richard Smith703b6012012-05-23 04:22:22 +0000430 D->getInnerLocStart(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000431 D->getAccess(),
432 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000433 if (!Field) {
434 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000435 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000436 }
Mike Stump1eb44332009-09-09 15:08:12 +0000437
DeLesley Hutchins23323e02012-01-20 22:50:54 +0000438 SemaRef.InstantiateAttrs(TemplateArgs, D, Field, LateAttrs, StartingScope);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000439
Richard Smithbe507b62013-02-01 08:12:08 +0000440 if (Field->hasAttrs())
441 SemaRef.CheckAlignasUnderalignment(Field);
442
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000443 if (Invalid)
444 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000446 if (!Field->getDeclName()) {
447 // Keep track of where this decl came from.
448 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000449 }
Douglas Gregor9901c572010-05-21 00:31:19 +0000450 if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
451 if (Parent->isAnonymousStructOrUnion() &&
Sebastian Redl7a126a42010-08-31 00:36:30 +0000452 Parent->getRedeclContext()->isFunctionOrMethod())
Douglas Gregor9901c572010-05-21 00:31:19 +0000453 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000454 }
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000456 Field->setImplicit(D->isImplicit());
John McCall46460a62010-01-20 21:53:11 +0000457 Field->setAccess(D->getAccess());
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000458 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000459
460 return Field;
461}
462
John McCall76da55d2013-04-16 07:28:30 +0000463Decl *TemplateDeclInstantiator::VisitMSPropertyDecl(MSPropertyDecl *D) {
464 bool Invalid = false;
465 TypeSourceInfo *DI = D->getTypeSourceInfo();
466
467 if (DI->getType()->isVariablyModifiedType()) {
468 SemaRef.Diag(D->getLocation(), diag::err_property_is_variably_modified)
469 << D->getName();
470 Invalid = true;
471 } else if (DI->getType()->isInstantiationDependentType()) {
472 DI = SemaRef.SubstType(DI, TemplateArgs,
473 D->getLocation(), D->getDeclName());
474 if (!DI) {
475 DI = D->getTypeSourceInfo();
476 Invalid = true;
477 } else if (DI->getType()->isFunctionType()) {
478 // C++ [temp.arg.type]p3:
479 // If a declaration acquires a function type through a type
480 // dependent on a template-parameter and this causes a
481 // declaration that does not use the syntactic form of a
482 // function declarator to have function type, the program is
483 // ill-formed.
484 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
485 << DI->getType();
486 Invalid = true;
487 }
488 } else {
489 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
490 }
491
492 MSPropertyDecl *Property = new (SemaRef.Context)
493 MSPropertyDecl(Owner, D->getLocation(),
494 D->getDeclName(), DI->getType(), DI,
495 D->getLocStart(),
496 D->getGetterId(), D->getSetterId());
497
498 SemaRef.InstantiateAttrs(TemplateArgs, D, Property, LateAttrs,
499 StartingScope);
500
501 if (Invalid)
502 Property->setInvalidDecl();
503
504 Property->setAccess(D->getAccess());
505 Owner->addDecl(Property);
506
507 return Property;
508}
509
Francois Pichet87c2e122010-11-21 06:08:52 +0000510Decl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
511 NamedDecl **NamedChain =
512 new (SemaRef.Context)NamedDecl*[D->getChainingSize()];
513
514 int i = 0;
515 for (IndirectFieldDecl::chain_iterator PI =
516 D->chain_begin(), PE = D->chain_end();
Douglas Gregorb7107222011-03-04 19:46:35 +0000517 PI != PE; ++PI) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000518 NamedDecl *Next = SemaRef.FindInstantiatedDecl(D->getLocation(), *PI,
Douglas Gregorb7107222011-03-04 19:46:35 +0000519 TemplateArgs);
520 if (!Next)
521 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000522
Douglas Gregorb7107222011-03-04 19:46:35 +0000523 NamedChain[i++] = Next;
524 }
Francois Pichet87c2e122010-11-21 06:08:52 +0000525
Francois Pichet40e17752010-12-09 10:07:54 +0000526 QualType T = cast<FieldDecl>(NamedChain[i-1])->getType();
Francois Pichet87c2e122010-11-21 06:08:52 +0000527 IndirectFieldDecl* IndirectField
528 = IndirectFieldDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Francois Pichet40e17752010-12-09 10:07:54 +0000529 D->getIdentifier(), T,
Francois Pichet87c2e122010-11-21 06:08:52 +0000530 NamedChain, D->getChainingSize());
531
532
533 IndirectField->setImplicit(D->isImplicit());
534 IndirectField->setAccess(D->getAccess());
535 Owner->addDecl(IndirectField);
536 return IndirectField;
537}
538
John McCall02cace72009-08-28 07:59:38 +0000539Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
John McCall02cace72009-08-28 07:59:38 +0000540 // Handle friend type expressions by simply substituting template
Douglas Gregor06245bf2010-04-07 17:57:12 +0000541 // parameters into the pattern type and checking the result.
John McCall32f2fb52010-03-25 18:04:51 +0000542 if (TypeSourceInfo *Ty = D->getFriendType()) {
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000543 TypeSourceInfo *InstTy;
544 // If this is an unsupported friend, don't bother substituting template
545 // arguments into it. The actual type referred to won't be used by any
546 // parts of Clang, and may not be valid for instantiating. Just use the
547 // same info for the instantiated friend.
548 if (D->isUnsupportedFriend()) {
549 InstTy = Ty;
550 } else {
551 InstTy = SemaRef.SubstType(Ty, TemplateArgs,
552 D->getLocation(), DeclarationName());
553 }
554 if (!InstTy)
Douglas Gregor7557a132009-12-24 20:56:24 +0000555 return 0;
John McCall02cace72009-08-28 07:59:38 +0000556
Richard Smithd6f80da2012-09-20 01:31:00 +0000557 FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getLocStart(),
Abramo Bagnara0216df82011-10-29 20:52:52 +0000558 D->getFriendLoc(), InstTy);
Douglas Gregor06245bf2010-04-07 17:57:12 +0000559 if (!FD)
560 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000561
Douglas Gregor06245bf2010-04-07 17:57:12 +0000562 FD->setAccess(AS_public);
John McCall9a34edb2010-10-19 01:40:49 +0000563 FD->setUnsupportedFriend(D->isUnsupportedFriend());
Douglas Gregor06245bf2010-04-07 17:57:12 +0000564 Owner->addDecl(FD);
565 return FD;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000566 }
567
Douglas Gregor06245bf2010-04-07 17:57:12 +0000568 NamedDecl *ND = D->getFriendDecl();
569 assert(ND && "friend decl must be a decl or a type!");
570
John McCallaf2094e2010-04-08 09:05:18 +0000571 // All of the Visit implementations for the various potential friend
572 // declarations have to be carefully written to work for friend
573 // objects, with the most important detail being that the target
574 // decl should almost certainly not be placed in Owner.
575 Decl *NewND = Visit(ND);
Douglas Gregor06245bf2010-04-07 17:57:12 +0000576 if (!NewND) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000577
John McCall02cace72009-08-28 07:59:38 +0000578 FriendDecl *FD =
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000579 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor06245bf2010-04-07 17:57:12 +0000580 cast<NamedDecl>(NewND), D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000581 FD->setAccess(AS_public);
John McCall9a34edb2010-10-19 01:40:49 +0000582 FD->setUnsupportedFriend(D->isUnsupportedFriend());
John McCall02cace72009-08-28 07:59:38 +0000583 Owner->addDecl(FD);
584 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000585}
586
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000587Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
588 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000589
Richard Smithf6702a32011-12-20 02:08:33 +0000590 // The expression in a static assertion is a constant expression.
591 EnterExpressionEvaluationContext Unevaluated(SemaRef,
592 Sema::ConstantEvaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000593
John McCall60d7b3a2010-08-24 06:29:42 +0000594 ExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000595 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000596 if (InstantiatedAssertExpr.isInvalid())
597 return 0;
598
Richard Smithe3f470a2012-07-11 22:37:56 +0000599 return SemaRef.BuildStaticAssertDeclaration(D->getLocation(),
John McCall9ae2f072010-08-23 23:25:46 +0000600 InstantiatedAssertExpr.get(),
Richard Smithe3f470a2012-07-11 22:37:56 +0000601 D->getMessage(),
602 D->getRParenLoc(),
603 D->isFailed());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000604}
605
606Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Richard Smith38f0df32012-03-26 04:58:10 +0000607 EnumDecl *PrevDecl = 0;
608 if (D->getPreviousDecl()) {
609 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
610 D->getPreviousDecl(),
611 TemplateArgs);
612 if (!Prev) return 0;
613 PrevDecl = cast<EnumDecl>(Prev);
614 }
615
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000616 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000617 D->getLocation(), D->getIdentifier(),
Richard Smith38f0df32012-03-26 04:58:10 +0000618 PrevDecl, D->isScoped(),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +0000619 D->isScopedUsingClassTag(), D->isFixed());
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000620 if (D->isFixed()) {
Richard Smithf1c66b42012-03-14 23:13:10 +0000621 if (TypeSourceInfo *TI = D->getIntegerTypeSourceInfo()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000622 // If we have type source information for the underlying type, it means it
623 // has been explicitly set by the user. Perform substitution on it before
624 // moving on.
625 SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
Richard Smithf1c66b42012-03-14 23:13:10 +0000626 TypeSourceInfo *NewTI = SemaRef.SubstType(TI, TemplateArgs, UnderlyingLoc,
627 DeclarationName());
628 if (!NewTI || SemaRef.CheckEnumUnderlyingType(NewTI))
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000629 Enum->setIntegerType(SemaRef.Context.IntTy);
Richard Smithf1c66b42012-03-14 23:13:10 +0000630 else
631 Enum->setIntegerTypeSourceInfo(NewTI);
632 } else {
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000633 assert(!D->getIntegerType()->isDependentType()
634 && "Dependent type without type source info");
635 Enum->setIntegerType(D->getIntegerType());
636 }
637 }
638
John McCall5b629aa2010-10-22 23:36:17 +0000639 SemaRef.InstantiateAttrs(TemplateArgs, D, Enum);
640
Richard Smithf1c66b42012-03-14 23:13:10 +0000641 Enum->setInstantiationOfMemberEnum(D, TSK_ImplicitInstantiation);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000642 Enum->setAccess(D->getAccess());
John McCallb6217662010-03-15 10:12:16 +0000643 if (SubstQualifier(D, Enum)) return 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000644 Owner->addDecl(Enum);
Richard Smithf1c66b42012-03-14 23:13:10 +0000645
Richard Smith4ca93d92012-03-26 04:08:46 +0000646 EnumDecl *Def = D->getDefinition();
647 if (Def && Def != D) {
648 // If this is an out-of-line definition of an enum member template, check
649 // that the underlying types match in the instantiation of both
650 // declarations.
651 if (TypeSourceInfo *TI = Def->getIntegerTypeSourceInfo()) {
652 SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
653 QualType DefnUnderlying =
654 SemaRef.SubstType(TI->getType(), TemplateArgs,
655 UnderlyingLoc, DeclarationName());
656 SemaRef.CheckEnumRedeclaration(Def->getLocation(), Def->isScoped(),
657 DefnUnderlying, Enum);
658 }
659 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000660
Douglas Gregor96084f12010-03-01 19:00:07 +0000661 if (D->getDeclContext()->isFunctionOrMethod())
662 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000663
Richard Smithf1c66b42012-03-14 23:13:10 +0000664 // C++11 [temp.inst]p1: The implicit instantiation of a class template
665 // specialization causes the implicit instantiation of the declarations, but
666 // not the definitions of scoped member enumerations.
667 // FIXME: There appears to be no wording for what happens for an enum defined
Richard Smith38f0df32012-03-26 04:58:10 +0000668 // within a block scope, but we treat that much like a member template. Only
669 // instantiate the definition when visiting the definition in that case, since
670 // we will visit all redeclarations.
671 if (!Enum->isScoped() && Def &&
672 (!D->getDeclContext()->isFunctionOrMethod() || D->isCompleteDefinition()))
Richard Smith4ca93d92012-03-26 04:08:46 +0000673 InstantiateEnumDefinition(Enum, Def);
Richard Smithf1c66b42012-03-14 23:13:10 +0000674
675 return Enum;
676}
677
678void TemplateDeclInstantiator::InstantiateEnumDefinition(
679 EnumDecl *Enum, EnumDecl *Pattern) {
680 Enum->startDefinition();
681
Richard Smith1af83c42012-03-23 03:33:32 +0000682 // Update the location to refer to the definition.
683 Enum->setLocation(Pattern->getLocation());
684
Chris Lattner5f9e2722011-07-23 10:55:15 +0000685 SmallVector<Decl*, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000686
687 EnumConstantDecl *LastEnumConst = 0;
Richard Smithf1c66b42012-03-14 23:13:10 +0000688 for (EnumDecl::enumerator_iterator EC = Pattern->enumerator_begin(),
689 ECEnd = Pattern->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000690 EC != ECEnd; ++EC) {
691 // The specified value for the enumerator.
John McCall60d7b3a2010-08-24 06:29:42 +0000692 ExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000693 if (Expr *UninstValue = EC->getInitExpr()) {
Richard Smithf6702a32011-12-20 02:08:33 +0000694 // The enumerator's value expression is a constant expression.
Mike Stump1eb44332009-09-09 15:08:12 +0000695 EnterExpressionEvaluationContext Unevaluated(SemaRef,
Richard Smithf6702a32011-12-20 02:08:33 +0000696 Sema::ConstantEvaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000697
John McCallce3ff2b2009-08-25 22:02:44 +0000698 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000699 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000700
701 // Drop the initial value and continue.
702 bool isInvalid = false;
703 if (Value.isInvalid()) {
704 Value = SemaRef.Owned((Expr *)0);
705 isInvalid = true;
706 }
707
Mike Stump1eb44332009-09-09 15:08:12 +0000708 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000709 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
710 EC->getLocation(), EC->getIdentifier(),
John McCall9ae2f072010-08-23 23:25:46 +0000711 Value.get());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000712
713 if (isInvalid) {
714 if (EnumConst)
715 EnumConst->setInvalidDecl();
716 Enum->setInvalidDecl();
717 }
718
719 if (EnumConst) {
David Blaikie581deb32012-06-06 20:45:41 +0000720 SemaRef.InstantiateAttrs(TemplateArgs, *EC, EnumConst);
John McCall5b629aa2010-10-22 23:36:17 +0000721
John McCall3b85ecf2010-01-23 22:37:59 +0000722 EnumConst->setAccess(Enum->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000723 Enum->addDecl(EnumConst);
John McCalld226f652010-08-21 09:40:31 +0000724 Enumerators.push_back(EnumConst);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000725 LastEnumConst = EnumConst;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000726
Richard Smithf1c66b42012-03-14 23:13:10 +0000727 if (Pattern->getDeclContext()->isFunctionOrMethod() &&
728 !Enum->isScoped()) {
Douglas Gregor96084f12010-03-01 19:00:07 +0000729 // If the enumeration is within a function or method, record the enum
730 // constant as a local.
David Blaikie581deb32012-06-06 20:45:41 +0000731 SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
Douglas Gregor96084f12010-03-01 19:00:07 +0000732 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000733 }
734 }
Mike Stump1eb44332009-09-09 15:08:12 +0000735
Richard Smithf1c66b42012-03-14 23:13:10 +0000736 // FIXME: Fixup LBraceLoc
737 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(),
738 Enum->getRBraceLoc(), Enum,
Dmitri Gribenko9ff2b422013-04-27 20:23:52 +0000739 Enumerators,
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000740 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000741}
742
Douglas Gregor6477b692009-03-25 15:04:13 +0000743Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000744 llvm_unreachable("EnumConstantDecls can only occur within EnumDecls.");
Douglas Gregor6477b692009-03-25 15:04:13 +0000745}
746
John McCalle29ba202009-08-20 01:44:21 +0000747Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
John McCall93ba8572010-03-25 06:39:04 +0000748 bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
749
Douglas Gregor550d9b22009-10-31 17:21:17 +0000750 // Create a local instantiation scope for this class template, which
751 // will contain the instantiations of the template parameters.
John McCall2a7fb272010-08-25 05:32:35 +0000752 LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000753 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000754 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000755 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000756 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000757
758 CXXRecordDecl *Pattern = D->getTemplatedDecl();
John McCall93ba8572010-03-25 06:39:04 +0000759
760 // Instantiate the qualifier. We have to do this first in case
761 // we're a friend declaration, because if we are then we need to put
762 // the new declaration in the appropriate context.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000763 NestedNameSpecifierLoc QualifierLoc = Pattern->getQualifierLoc();
764 if (QualifierLoc) {
765 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
766 TemplateArgs);
767 if (!QualifierLoc)
768 return 0;
John McCall93ba8572010-03-25 06:39:04 +0000769 }
770
771 CXXRecordDecl *PrevDecl = 0;
772 ClassTemplateDecl *PrevClassTemplate = 0;
773
Douglas Gregoref96ee02012-01-14 16:38:05 +0000774 if (!isFriend && Pattern->getPreviousDecl()) {
Nick Lewycky37574f52010-11-08 23:29:42 +0000775 DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
David Blaikie3bc93e32012-12-19 00:45:41 +0000776 if (!Found.empty()) {
777 PrevClassTemplate = dyn_cast<ClassTemplateDecl>(Found.front());
Nick Lewycky37574f52010-11-08 23:29:42 +0000778 if (PrevClassTemplate)
779 PrevDecl = PrevClassTemplate->getTemplatedDecl();
780 }
781 }
782
John McCall93ba8572010-03-25 06:39:04 +0000783 // If this isn't a friend, then it's a member template, in which
784 // case we just want to build the instantiation in the
785 // specialization. If it is a friend, we want to build it in
786 // the appropriate context.
787 DeclContext *DC = Owner;
788 if (isFriend) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000789 if (QualifierLoc) {
John McCall93ba8572010-03-25 06:39:04 +0000790 CXXScopeSpec SS;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000791 SS.Adopt(QualifierLoc);
John McCall93ba8572010-03-25 06:39:04 +0000792 DC = SemaRef.computeDeclContext(SS);
793 if (!DC) return 0;
794 } else {
795 DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
796 Pattern->getDeclContext(),
797 TemplateArgs);
798 }
799
800 // Look for a previous declaration of the template in the owning
801 // context.
802 LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
803 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
804 SemaRef.LookupQualifiedName(R, DC);
805
806 if (R.isSingleResult()) {
807 PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
808 if (PrevClassTemplate)
809 PrevDecl = PrevClassTemplate->getTemplatedDecl();
810 }
811
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000812 if (!PrevClassTemplate && QualifierLoc) {
John McCall93ba8572010-03-25 06:39:04 +0000813 SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
Douglas Gregor1eabb7d2010-03-31 23:17:41 +0000814 << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000815 << QualifierLoc.getSourceRange();
John McCall93ba8572010-03-25 06:39:04 +0000816 return 0;
817 }
818
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000819 bool AdoptedPreviousTemplateParams = false;
John McCall93ba8572010-03-25 06:39:04 +0000820 if (PrevClassTemplate) {
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000821 bool Complain = true;
822
823 // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
824 // template for struct std::tr1::__detail::_Map_base, where the
825 // template parameters of the friend declaration don't match the
826 // template parameters of the original declaration. In this one
827 // case, we don't complain about the ill-formed friend
828 // declaration.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000829 if (isFriend && Pattern->getIdentifier() &&
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000830 Pattern->getIdentifier()->isStr("_Map_base") &&
831 DC->isNamespace() &&
832 cast<NamespaceDecl>(DC)->getIdentifier() &&
833 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
834 DeclContext *DCParent = DC->getParent();
835 if (DCParent->isNamespace() &&
836 cast<NamespaceDecl>(DCParent)->getIdentifier() &&
837 cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
838 DeclContext *DCParent2 = DCParent->getParent();
839 if (DCParent2->isNamespace() &&
840 cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
841 cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
842 DCParent2->getParent()->isTranslationUnit())
843 Complain = false;
844 }
845 }
846
John McCall93ba8572010-03-25 06:39:04 +0000847 TemplateParameterList *PrevParams
848 = PrevClassTemplate->getTemplateParameters();
849
850 // Make sure the parameter lists match.
851 if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000852 Complain,
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000853 Sema::TPL_TemplateMatch)) {
854 if (Complain)
855 return 0;
856
857 AdoptedPreviousTemplateParams = true;
858 InstParams = PrevParams;
859 }
John McCall93ba8572010-03-25 06:39:04 +0000860
861 // Do some additional validation, then merge default arguments
862 // from the existing declarations.
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000863 if (!AdoptedPreviousTemplateParams &&
864 SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
John McCall93ba8572010-03-25 06:39:04 +0000865 Sema::TPC_ClassTemplate))
866 return 0;
867 }
868 }
869
John McCalle29ba202009-08-20 01:44:21 +0000870 CXXRecordDecl *RecordInst
John McCall93ba8572010-03-25 06:39:04 +0000871 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000872 Pattern->getLocStart(), Pattern->getLocation(),
873 Pattern->getIdentifier(), PrevDecl,
Douglas Gregorf0510d42009-10-12 23:11:44 +0000874 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000875
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000876 if (QualifierLoc)
877 RecordInst->setQualifierInfo(QualifierLoc);
John McCallb6217662010-03-15 10:12:16 +0000878
John McCalle29ba202009-08-20 01:44:21 +0000879 ClassTemplateDecl *Inst
John McCall93ba8572010-03-25 06:39:04 +0000880 = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
881 D->getIdentifier(), InstParams, RecordInst,
882 PrevClassTemplate);
John McCalle29ba202009-08-20 01:44:21 +0000883 RecordInst->setDescribedClassTemplate(Inst);
John McCallea7390c2010-04-08 20:25:50 +0000884
John McCall93ba8572010-03-25 06:39:04 +0000885 if (isFriend) {
John McCallea7390c2010-04-08 20:25:50 +0000886 if (PrevClassTemplate)
887 Inst->setAccess(PrevClassTemplate->getAccess());
888 else
889 Inst->setAccess(D->getAccess());
890
Richard Smith22050f22013-07-17 23:53:16 +0000891 Inst->setObjectOfFriendDecl();
John McCall93ba8572010-03-25 06:39:04 +0000892 // TODO: do we want to track the instantiation progeny of this
893 // friend target decl?
894 } else {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000895 Inst->setAccess(D->getAccess());
Nick Lewycky37574f52010-11-08 23:29:42 +0000896 if (!PrevClassTemplate)
897 Inst->setInstantiatedFromMemberTemplate(D);
John McCall93ba8572010-03-25 06:39:04 +0000898 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000899
Douglas Gregorf0510d42009-10-12 23:11:44 +0000900 // Trigger creation of the type for the instantiation.
John McCall3cb0ebd2010-03-10 03:28:59 +0000901 SemaRef.Context.getInjectedClassNameType(RecordInst,
Douglas Gregor24bae922010-07-08 18:37:38 +0000902 Inst->getInjectedClassNameSpecialization());
John McCallea7390c2010-04-08 20:25:50 +0000903
Douglas Gregor259571e2009-10-30 22:42:42 +0000904 // Finish handling of friends.
John McCall93ba8572010-03-25 06:39:04 +0000905 if (isFriend) {
Richard Smith1b7f9cb2012-03-13 03:12:56 +0000906 DC->makeDeclVisibleInContext(Inst);
Abramo Bagnara4c515482011-11-26 13:33:46 +0000907 Inst->setLexicalDeclContext(Owner);
908 RecordInst->setLexicalDeclContext(Owner);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000909 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000910 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000911
Abramo Bagnara4c515482011-11-26 13:33:46 +0000912 if (D->isOutOfLine()) {
913 Inst->setLexicalDeclContext(D->getLexicalDeclContext());
914 RecordInst->setLexicalDeclContext(D->getLexicalDeclContext());
915 }
916
John McCalle29ba202009-08-20 01:44:21 +0000917 Owner->addDecl(Inst);
Douglas Gregord65587f2010-11-10 19:44:59 +0000918
919 if (!PrevClassTemplate) {
920 // Queue up any out-of-line partial specializations of this member
921 // class template; the client will force their instantiation once
922 // the enclosing class has been instantiated.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000923 SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
Douglas Gregord65587f2010-11-10 19:44:59 +0000924 D->getPartialSpecializations(PartialSpecs);
925 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
926 if (PartialSpecs[I]->isOutOfLine())
927 OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I]));
928 }
929
John McCalle29ba202009-08-20 01:44:21 +0000930 return Inst;
931}
932
Douglas Gregord60e1052009-08-27 16:57:43 +0000933Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000934TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
935 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000936 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000937
Douglas Gregored9c0f92009-10-29 00:04:11 +0000938 // Lookup the already-instantiated declaration in the instantiation
939 // of the class template and return that.
940 DeclContext::lookup_result Found
941 = Owner->lookup(ClassTemplate->getDeclName());
David Blaikie3bc93e32012-12-19 00:45:41 +0000942 if (Found.empty())
Douglas Gregored9c0f92009-10-29 00:04:11 +0000943 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000944
Douglas Gregored9c0f92009-10-29 00:04:11 +0000945 ClassTemplateDecl *InstClassTemplate
David Blaikie3bc93e32012-12-19 00:45:41 +0000946 = dyn_cast<ClassTemplateDecl>(Found.front());
Douglas Gregored9c0f92009-10-29 00:04:11 +0000947 if (!InstClassTemplate)
948 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000949
Douglas Gregord65587f2010-11-10 19:44:59 +0000950 if (ClassTemplatePartialSpecializationDecl *Result
951 = InstClassTemplate->findPartialSpecInstantiatedFromMember(D))
952 return Result;
953
954 return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D);
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000955}
956
Larisse Voufoef4579c2013-08-06 01:03:05 +0000957Decl *TemplateDeclInstantiator::VisitVarTemplateDecl(VarTemplateDecl *D) {
958 assert(D->getTemplatedDecl()->isStaticDataMember() &&
959 "Only static data member templates are allowed.");
Larisse Voufoef4579c2013-08-06 01:03:05 +0000960
961 // Create a local instantiation scope for this variable template, which
962 // will contain the instantiations of the template parameters.
963 LocalInstantiationScope Scope(SemaRef);
964 TemplateParameterList *TempParams = D->getTemplateParameters();
965 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
966 if (!InstParams)
967 return NULL;
968
969 VarDecl *Pattern = D->getTemplatedDecl();
970 VarTemplateDecl *PrevVarTemplate = 0;
971
972 if (Pattern->getPreviousDecl()) {
973 DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
974 if (!Found.empty())
975 PrevVarTemplate = dyn_cast<VarTemplateDecl>(Found.front());
976 }
977
Richard Smithdd9459f2013-08-13 18:18:50 +0000978 VarDecl *VarInst =
Larisse Voufo567f9172013-08-22 00:59:14 +0000979 cast_or_null<VarDecl>(VisitVarDecl(Pattern,
980 /*InstantiatingVarTemplate=*/true));
Larisse Voufoef4579c2013-08-06 01:03:05 +0000981
982 DeclContext *DC = Owner;
983
Larisse Voufoef4579c2013-08-06 01:03:05 +0000984 VarTemplateDecl *Inst = VarTemplateDecl::Create(
985 SemaRef.Context, DC, D->getLocation(), D->getIdentifier(), InstParams,
986 VarInst, PrevVarTemplate);
987 VarInst->setDescribedVarTemplate(Inst);
988
989 Inst->setAccess(D->getAccess());
990 if (!PrevVarTemplate)
991 Inst->setInstantiatedFromMemberTemplate(D);
992
993 if (D->isOutOfLine()) {
994 Inst->setLexicalDeclContext(D->getLexicalDeclContext());
995 VarInst->setLexicalDeclContext(D->getLexicalDeclContext());
996 }
997
998 Owner->addDecl(Inst);
999
1000 if (!PrevVarTemplate) {
1001 // Queue up any out-of-line partial specializations of this member
1002 // variable template; the client will force their instantiation once
1003 // the enclosing class has been instantiated.
1004 SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs;
1005 D->getPartialSpecializations(PartialSpecs);
1006 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
1007 if (PartialSpecs[I]->isOutOfLine())
1008 OutOfLineVarPartialSpecs.push_back(
1009 std::make_pair(Inst, PartialSpecs[I]));
1010 }
1011
1012 return Inst;
1013}
1014
1015Decl *TemplateDeclInstantiator::VisitVarTemplatePartialSpecializationDecl(
1016 VarTemplatePartialSpecializationDecl *D) {
1017 assert(D->isStaticDataMember() &&
1018 "Only static data member templates are allowed.");
Larisse Voufoef4579c2013-08-06 01:03:05 +00001019
1020 VarTemplateDecl *VarTemplate = D->getSpecializedTemplate();
1021
1022 // Lookup the already-instantiated declaration and return that.
1023 DeclContext::lookup_result Found = Owner->lookup(VarTemplate->getDeclName());
1024 assert(!Found.empty() && "Instantiation found nothing?");
1025
1026 VarTemplateDecl *InstVarTemplate = dyn_cast<VarTemplateDecl>(Found.front());
1027 assert(InstVarTemplate && "Instantiation did not find a variable template?");
1028
1029 if (VarTemplatePartialSpecializationDecl *Result =
1030 InstVarTemplate->findPartialSpecInstantiatedFromMember(D))
1031 return Result;
1032
1033 return InstantiateVarTemplatePartialSpecialization(InstVarTemplate, D);
1034}
1035
Douglas Gregor7974c3b2009-10-07 17:21:34 +00001036Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +00001037TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001038 // Create a local instantiation scope for this function template, which
1039 // will contain the instantiations of the template parameters and then get
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001040 // merged with the local instantiation scope for the function template
Douglas Gregor550d9b22009-10-31 17:21:17 +00001041 // itself.
John McCall2a7fb272010-08-25 05:32:35 +00001042 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor895162d2010-04-30 18:55:50 +00001043
Douglas Gregord60e1052009-08-27 16:57:43 +00001044 TemplateParameterList *TempParams = D->getTemplateParameters();
1045 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +00001046 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +00001047 return NULL;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001048
Douglas Gregora735b202009-10-13 14:39:41 +00001049 FunctionDecl *Instantiated = 0;
1050 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001051 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
Douglas Gregora735b202009-10-13 14:39:41 +00001052 InstParams));
1053 else
1054 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001055 D->getTemplatedDecl(),
Douglas Gregora735b202009-10-13 14:39:41 +00001056 InstParams));
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001057
Douglas Gregora735b202009-10-13 14:39:41 +00001058 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +00001059 return 0;
1060
Mike Stump1eb44332009-09-09 15:08:12 +00001061 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +00001062 // template from which it was instantiated.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001063 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +00001064 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +00001065 InstTemplate->setAccess(D->getAccess());
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001066 assert(InstTemplate &&
Douglas Gregora735b202009-10-13 14:39:41 +00001067 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
John McCalle976ffe2009-12-14 23:19:40 +00001068
John McCallb1a56e72010-03-26 23:10:15 +00001069 bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
1070
John McCalle976ffe2009-12-14 23:19:40 +00001071 // Link the instantiation back to the pattern *unless* this is a
1072 // non-definition friend declaration.
1073 if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
John McCallb1a56e72010-03-26 23:10:15 +00001074 !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
Douglas Gregora735b202009-10-13 14:39:41 +00001075 InstTemplate->setInstantiatedFromMemberTemplate(D);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001076
John McCallb1a56e72010-03-26 23:10:15 +00001077 // Make declarations visible in the appropriate context.
John McCall1f2e1a92012-08-10 03:15:35 +00001078 if (!isFriend) {
Douglas Gregora735b202009-10-13 14:39:41 +00001079 Owner->addDecl(InstTemplate);
John McCall1f2e1a92012-08-10 03:15:35 +00001080 } else if (InstTemplate->getDeclContext()->isRecord() &&
1081 !D->getPreviousDecl()) {
1082 SemaRef.CheckFriendAccess(InstTemplate);
1083 }
John McCallb1a56e72010-03-26 23:10:15 +00001084
Douglas Gregord60e1052009-08-27 16:57:43 +00001085 return InstTemplate;
1086}
1087
Douglas Gregord475b8d2009-03-25 21:17:03 +00001088Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
1089 CXXRecordDecl *PrevDecl = 0;
1090 if (D->isInjectedClassName())
1091 PrevDecl = cast<CXXRecordDecl>(Owner);
Douglas Gregoref96ee02012-01-14 16:38:05 +00001092 else if (D->getPreviousDecl()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001093 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
Douglas Gregoref96ee02012-01-14 16:38:05 +00001094 D->getPreviousDecl(),
John McCall6c1c1b82009-12-15 22:29:06 +00001095 TemplateArgs);
1096 if (!Prev) return 0;
1097 PrevDecl = cast<CXXRecordDecl>(Prev);
1098 }
Douglas Gregord475b8d2009-03-25 21:17:03 +00001099
1100 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +00001101 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00001102 D->getLocStart(), D->getLocation(),
1103 D->getIdentifier(), PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00001104
1105 // Substitute the nested name specifier, if any.
1106 if (SubstQualifier(D, Record))
1107 return 0;
1108
Douglas Gregord475b8d2009-03-25 21:17:03 +00001109 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +00001110 // FIXME: Check against AS_none is an ugly hack to work around the issue that
1111 // the tag decls introduced by friend class declarations don't have an access
1112 // specifier. Remove once this area of the code gets sorted out.
1113 if (D->getAccess() != AS_none)
1114 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +00001115 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001116 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001117
John McCall02cace72009-08-28 07:59:38 +00001118 // If the original function was part of a friend declaration,
1119 // inherit its namespace state.
Richard Smith22050f22013-07-17 23:53:16 +00001120 if (D->getFriendObjectKind())
1121 Record->setObjectOfFriendDecl();
John McCall02cace72009-08-28 07:59:38 +00001122
Douglas Gregor9901c572010-05-21 00:31:19 +00001123 // Make sure that anonymous structs and unions are recorded.
1124 if (D->isAnonymousStructOrUnion()) {
1125 Record->setAnonymousStructOrUnion(true);
Sebastian Redl7a126a42010-08-31 00:36:30 +00001126 if (Record->getDeclContext()->getRedeclContext()->isFunctionOrMethod())
Douglas Gregor9901c572010-05-21 00:31:19 +00001127 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
1128 }
Anders Carlssond8b285f2009-09-01 04:26:58 +00001129
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001130 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001131 return Record;
1132}
1133
Douglas Gregor71074fd2012-09-13 21:56:43 +00001134/// \brief Adjust the given function type for an instantiation of the
1135/// given declaration, to cope with modifications to the function's type that
1136/// aren't reflected in the type-source information.
1137///
1138/// \param D The declaration we're instantiating.
1139/// \param TInfo The already-instantiated type.
1140static QualType adjustFunctionTypeForInstantiation(ASTContext &Context,
1141 FunctionDecl *D,
1142 TypeSourceInfo *TInfo) {
Douglas Gregorbed51fe2012-09-13 22:01:49 +00001143 const FunctionProtoType *OrigFunc
1144 = D->getType()->castAs<FunctionProtoType>();
1145 const FunctionProtoType *NewFunc
1146 = TInfo->getType()->castAs<FunctionProtoType>();
1147 if (OrigFunc->getExtInfo() == NewFunc->getExtInfo())
1148 return TInfo->getType();
1149
1150 FunctionProtoType::ExtProtoInfo NewEPI = NewFunc->getExtProtoInfo();
1151 NewEPI.ExtInfo = OrigFunc->getExtInfo();
1152 return Context.getFunctionType(NewFunc->getResultType(),
Reid Kleckner0567a792013-06-10 20:51:09 +00001153 NewFunc->getArgTypes(), NewEPI);
Douglas Gregor71074fd2012-09-13 21:56:43 +00001154}
1155
John McCall02cace72009-08-28 07:59:38 +00001156/// Normal class members are of more specific types and therefore
1157/// don't make it here. This function serves two purposes:
1158/// 1) instantiating function templates
1159/// 2) substituting friend declarations
1160/// FIXME: preserve function definitions in case #2
Douglas Gregor7557a132009-12-24 20:56:24 +00001161Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
Douglas Gregora735b202009-10-13 14:39:41 +00001162 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +00001163 // Check whether there is already a function template specialization for
1164 // this declaration.
1165 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
John McCallb0cb0222010-03-27 05:57:59 +00001166 if (FunctionTemplate && !TemplateParams) {
Richard Smithc95d4132013-05-03 23:46:09 +00001167 ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +00001168
Douglas Gregor1e1e9722012-03-28 14:34:23 +00001169 void *InsertPos = 0;
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001170 FunctionDecl *SpecFunc
Richard Smithc95d4132013-05-03 23:46:09 +00001171 = FunctionTemplate->findSpecialization(Innermost.begin(), Innermost.size(),
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001172 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001173
Douglas Gregor127102b2009-06-29 20:59:39 +00001174 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001175 if (SpecFunc)
1176 return SpecFunc;
Douglas Gregor127102b2009-06-29 20:59:39 +00001177 }
Mike Stump1eb44332009-09-09 15:08:12 +00001178
John McCallb0cb0222010-03-27 05:57:59 +00001179 bool isFriend;
1180 if (FunctionTemplate)
1181 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1182 else
1183 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1184
Douglas Gregor79c22782010-01-16 20:21:20 +00001185 bool MergeWithParentScope = (TemplateParams != 0) ||
Douglas Gregorb212d9a2010-05-21 21:25:08 +00001186 Owner->isFunctionOrMethod() ||
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001187 !(isa<Decl>(Owner) &&
Douglas Gregor79c22782010-01-16 20:21:20 +00001188 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
John McCall2a7fb272010-08-25 05:32:35 +00001189 LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001190
Chris Lattner5f9e2722011-07-23 10:55:15 +00001191 SmallVector<ParmVarDecl *, 4> Params;
David Blaikie64b4b432011-11-10 05:42:04 +00001192 TypeSourceInfo *TInfo = SubstFunctionType(D, Params);
John McCall21ef0fa2010-03-11 09:03:00 +00001193 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001194 return 0;
Douglas Gregor71074fd2012-09-13 21:56:43 +00001195 QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo);
John McCallfd810b12009-08-14 02:03:10 +00001196
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001197 NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
1198 if (QualifierLoc) {
1199 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
1200 TemplateArgs);
1201 if (!QualifierLoc)
1202 return 0;
John McCalld325daa2010-03-26 04:53:08 +00001203 }
1204
John McCall68b6b872010-02-06 01:50:47 +00001205 // If we're instantiating a local function declaration, put the result
Richard Smitha41c97a2013-09-20 01:15:31 +00001206 // in the enclosing namespace; otherwise we need to find the instantiated
1207 // context.
John McCall68b6b872010-02-06 01:50:47 +00001208 DeclContext *DC;
Richard Smitha41c97a2013-09-20 01:15:31 +00001209 if (D->isLocalExternDecl()) {
John McCall68b6b872010-02-06 01:50:47 +00001210 DC = Owner;
Richard Smitha41c97a2013-09-20 01:15:31 +00001211 SemaRef.adjustContextForLocalExternDecl(DC);
1212 } else if (isFriend && QualifierLoc) {
John McCalld325daa2010-03-26 04:53:08 +00001213 CXXScopeSpec SS;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001214 SS.Adopt(QualifierLoc);
John McCalld325daa2010-03-26 04:53:08 +00001215 DC = SemaRef.computeDeclContext(SS);
1216 if (!DC) return 0;
1217 } else {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001218 DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001219 TemplateArgs);
John McCalld325daa2010-03-26 04:53:08 +00001220 }
John McCall68b6b872010-02-06 01:50:47 +00001221
John McCall02cace72009-08-28 07:59:38 +00001222 FunctionDecl *Function =
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001223 FunctionDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(),
Abramo Bagnara635311f2012-10-04 21:40:42 +00001224 D->getNameInfo(), T, TInfo,
Rafael Espindola459ef032013-04-16 02:29:15 +00001225 D->getCanonicalDecl()->getStorageClass(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001226 D->isInlineSpecified(), D->hasWrittenPrototype(),
Richard Smith86c3ae42012-02-13 03:54:03 +00001227 D->isConstexpr());
Enea Zaffanellade9ed712013-07-19 18:02:36 +00001228 Function->setRangeEnd(D->getSourceRange().getEnd());
John McCallb6217662010-03-15 10:12:16 +00001229
Richard Smithd4497dd2013-01-25 00:08:28 +00001230 if (D->isInlined())
1231 Function->setImplicitlyInline();
1232
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001233 if (QualifierLoc)
1234 Function->setQualifierInfo(QualifierLoc);
John McCallb6217662010-03-15 10:12:16 +00001235
Richard Smitha41c97a2013-09-20 01:15:31 +00001236 if (D->isLocalExternDecl())
1237 Function->setLocalExternDecl();
1238
John McCallb1a56e72010-03-26 23:10:15 +00001239 DeclContext *LexicalDC = Owner;
Richard Smitha41c97a2013-09-20 01:15:31 +00001240 if (!isFriend && D->isOutOfLine() && !D->isLocalExternDecl()) {
John McCallb1a56e72010-03-26 23:10:15 +00001241 assert(D->getDeclContext()->isFileContext());
1242 LexicalDC = D->getDeclContext();
1243 }
1244
1245 Function->setLexicalDeclContext(LexicalDC);
Mike Stump1eb44332009-09-09 15:08:12 +00001246
Douglas Gregore53060f2009-06-25 22:08:12 +00001247 // Attach the parameters
Reid Klecknerc66e7e92013-07-31 21:00:18 +00001248 for (unsigned P = 0; P < Params.size(); ++P)
1249 if (Params[P])
1250 Params[P]->setOwningFunction(Function);
David Blaikie4278c652011-09-21 18:16:56 +00001251 Function->setParams(Params);
John McCall02cace72009-08-28 07:59:38 +00001252
Douglas Gregorac7c2c82010-05-17 16:38:00 +00001253 SourceLocation InstantiateAtPOI;
Douglas Gregora735b202009-10-13 14:39:41 +00001254 if (TemplateParams) {
1255 // Our resulting instantiation is actually a function template, since we
1256 // are substituting only the outer template parameters. For example, given
1257 //
1258 // template<typename T>
1259 // struct X {
1260 // template<typename U> friend void f(T, U);
1261 // };
1262 //
1263 // X<int> x;
1264 //
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001265 // We are instantiating the friend function template "f" within X<int>,
Douglas Gregora735b202009-10-13 14:39:41 +00001266 // which means substituting int for T, but leaving "f" as a friend function
1267 // template.
1268 // Build the function template itself.
John McCalld325daa2010-03-26 04:53:08 +00001269 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
Douglas Gregora735b202009-10-13 14:39:41 +00001270 Function->getLocation(),
1271 Function->getDeclName(),
1272 TemplateParams, Function);
1273 Function->setDescribedFunctionTemplate(FunctionTemplate);
John McCallb1a56e72010-03-26 23:10:15 +00001274
1275 FunctionTemplate->setLexicalDeclContext(LexicalDC);
John McCalld325daa2010-03-26 04:53:08 +00001276
1277 if (isFriend && D->isThisDeclarationADefinition()) {
1278 // TODO: should we remember this connection regardless of whether
1279 // the friend declaration provided a body?
1280 FunctionTemplate->setInstantiatedFromMemberTemplate(
1281 D->getDescribedFunctionTemplate());
1282 }
Douglas Gregor66724ea2009-11-14 01:20:54 +00001283 } else if (FunctionTemplate) {
1284 // Record this function template specialization.
Richard Smithc95d4132013-05-03 23:46:09 +00001285 ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001286 Function->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001287 TemplateArgumentList::CreateCopy(SemaRef.Context,
Richard Smithc95d4132013-05-03 23:46:09 +00001288 Innermost.begin(),
1289 Innermost.size()),
Douglas Gregor1e1e9722012-03-28 14:34:23 +00001290 /*InsertPos=*/0);
Chandler Carruth80f5b162011-08-18 09:09:59 +00001291 } else if (isFriend) {
1292 // Note, we need this connection even if the friend doesn't have a body.
1293 // Its body may exist but not have been attached yet due to deferred
1294 // parsing.
1295 // FIXME: It might be cleaner to set this when attaching the body to the
1296 // friend function declaration, however that would require finding all the
1297 // instantiations and modifying them.
John McCalld325daa2010-03-26 04:53:08 +00001298 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
John McCall02cace72009-08-28 07:59:38 +00001299 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001300
Douglas Gregore53060f2009-06-25 22:08:12 +00001301 if (InitFunctionInstantiation(Function, D))
1302 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001303
John McCallaf2094e2010-04-08 09:05:18 +00001304 bool isExplicitSpecialization = false;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001305
Richard Smitha41c97a2013-09-20 01:15:31 +00001306 LookupResult Previous(
1307 SemaRef, Function->getDeclName(), SourceLocation(),
1308 D->isLocalExternDecl() ? Sema::LookupRedeclarationWithLinkage
1309 : Sema::LookupOrdinaryName,
1310 Sema::ForRedeclaration);
John McCall68263142009-11-18 22:49:29 +00001311
John McCallaf2094e2010-04-08 09:05:18 +00001312 if (DependentFunctionTemplateSpecializationInfo *Info
1313 = D->getDependentSpecializationInfo()) {
1314 assert(isFriend && "non-friend has dependent specialization info?");
1315
1316 // This needs to be set now for future sanity.
Richard Smith22050f22013-07-17 23:53:16 +00001317 Function->setObjectOfFriendDecl();
John McCallaf2094e2010-04-08 09:05:18 +00001318
1319 // Instantiate the explicit template arguments.
1320 TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1321 Info->getRAngleLoc());
Douglas Gregore02e2622010-12-22 21:19:48 +00001322 if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(),
1323 ExplicitArgs, TemplateArgs))
1324 return 0;
John McCallaf2094e2010-04-08 09:05:18 +00001325
1326 // Map the candidate templates to their instantiations.
1327 for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1328 Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1329 Info->getTemplate(I),
1330 TemplateArgs);
1331 if (!Temp) return 0;
1332
1333 Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1334 }
1335
1336 if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1337 &ExplicitArgs,
1338 Previous))
1339 Function->setInvalidDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001340
John McCallaf2094e2010-04-08 09:05:18 +00001341 isExplicitSpecialization = true;
1342
1343 } else if (TemplateParams || !FunctionTemplate) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001344 // Look only into the namespace where the friend would be declared to
1345 // find a previous declaration. This is the innermost enclosing namespace,
Douglas Gregora735b202009-10-13 14:39:41 +00001346 // as described in ActOnFriendFunctionDecl.
John McCall68263142009-11-18 22:49:29 +00001347 SemaRef.LookupQualifiedName(Previous, DC);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001348
Douglas Gregora735b202009-10-13 14:39:41 +00001349 // In C++, the previous declaration we find might be a tag type
1350 // (class or enum). In this case, the new declaration will hide the
1351 // tag type. Note that this does does not apply if we're declaring a
1352 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001353 if (Previous.isSingleTagDecl())
1354 Previous.clear();
Douglas Gregora735b202009-10-13 14:39:41 +00001355 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001356
John McCall9f54ad42009-12-10 09:41:52 +00001357 SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00001358 isExplicitSpecialization);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001359
John McCall76d32642010-04-24 01:30:58 +00001360 NamedDecl *PrincipalDecl = (TemplateParams
1361 ? cast<NamedDecl>(FunctionTemplate)
1362 : Function);
1363
Douglas Gregora735b202009-10-13 14:39:41 +00001364 // If the original function was part of a friend declaration,
1365 // inherit its namespace state and add it to the owner.
John McCalld325daa2010-03-26 04:53:08 +00001366 if (isFriend) {
Richard Smith22050f22013-07-17 23:53:16 +00001367 PrincipalDecl->setObjectOfFriendDecl();
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001368 DC->makeDeclVisibleInContext(PrincipalDecl);
Gabor Greifab297ac2010-08-30 21:10:05 +00001369
Gabor Greif77535df2010-08-30 22:25:56 +00001370 bool queuedInstantiation = false;
Gabor Greifab297ac2010-08-30 21:10:05 +00001371
Richard Smith53e53512011-10-19 00:54:10 +00001372 // C++98 [temp.friend]p5: When a function is defined in a friend function
1373 // declaration in a class template, the function is defined at each
1374 // instantiation of the class template. The function is defined even if it
1375 // is never used.
1376 // C++11 [temp.friend]p4: When a function is defined in a friend function
1377 // declaration in a class template, the function is instantiated when the
1378 // function is odr-used.
1379 //
1380 // If -Wc++98-compat is enabled, we go through the motions of checking for a
1381 // redefinition, but don't instantiate the function.
Richard Smith80ad52f2013-01-02 11:42:31 +00001382 if ((!SemaRef.getLangOpts().CPlusPlus11 ||
Richard Smith53e53512011-10-19 00:54:10 +00001383 SemaRef.Diags.getDiagnosticLevel(
1384 diag::warn_cxx98_compat_friend_redefinition,
1385 Function->getLocation())
1386 != DiagnosticsEngine::Ignored) &&
Douglas Gregor238058c2010-05-18 05:45:02 +00001387 D->isThisDeclarationADefinition()) {
1388 // Check for a function body.
1389 const FunctionDecl *Definition = 0;
Sean Hunt10620eb2011-05-06 20:44:56 +00001390 if (Function->isDefined(Definition) &&
Douglas Gregor238058c2010-05-18 05:45:02 +00001391 Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
Richard Smith53e53512011-10-19 00:54:10 +00001392 SemaRef.Diag(Function->getLocation(),
Richard Smith80ad52f2013-01-02 11:42:31 +00001393 SemaRef.getLangOpts().CPlusPlus11 ?
Richard Smith53e53512011-10-19 00:54:10 +00001394 diag::warn_cxx98_compat_friend_redefinition :
1395 diag::err_redefinition) << Function->getDeclName();
Douglas Gregor238058c2010-05-18 05:45:02 +00001396 SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
Richard Smith80ad52f2013-01-02 11:42:31 +00001397 if (!SemaRef.getLangOpts().CPlusPlus11)
Richard Smith53e53512011-10-19 00:54:10 +00001398 Function->setInvalidDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001399 }
Douglas Gregor238058c2010-05-18 05:45:02 +00001400 // Check for redefinitions due to other instantiations of this or
1401 // a similar friend function.
1402 else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1403 REnd = Function->redecls_end();
1404 R != REnd; ++R) {
Gabor Greif13a8aff2010-08-28 15:42:30 +00001405 if (*R == Function)
1406 continue;
Gabor Greifab297ac2010-08-30 21:10:05 +00001407 switch (R->getFriendObjectKind()) {
1408 case Decl::FOK_None:
Richard Smith80ad52f2013-01-02 11:42:31 +00001409 if (!SemaRef.getLangOpts().CPlusPlus11 &&
Richard Smith53e53512011-10-19 00:54:10 +00001410 !queuedInstantiation && R->isUsed(false)) {
Gabor Greifab297ac2010-08-30 21:10:05 +00001411 if (MemberSpecializationInfo *MSInfo
1412 = Function->getMemberSpecializationInfo()) {
1413 if (MSInfo->getPointOfInstantiation().isInvalid()) {
1414 SourceLocation Loc = R->getLocation(); // FIXME
1415 MSInfo->setPointOfInstantiation(Loc);
1416 SemaRef.PendingLocalImplicitInstantiations.push_back(
1417 std::make_pair(Function, Loc));
1418 queuedInstantiation = true;
1419 }
1420 }
1421 }
1422 break;
1423 default:
Douglas Gregor238058c2010-05-18 05:45:02 +00001424 if (const FunctionDecl *RPattern
Gabor Greif6a557d82010-08-28 15:46:56 +00001425 = R->getTemplateInstantiationPattern())
Sean Hunt10620eb2011-05-06 20:44:56 +00001426 if (RPattern->isDefined(RPattern)) {
Richard Smith53e53512011-10-19 00:54:10 +00001427 SemaRef.Diag(Function->getLocation(),
Richard Smith80ad52f2013-01-02 11:42:31 +00001428 SemaRef.getLangOpts().CPlusPlus11 ?
Richard Smith53e53512011-10-19 00:54:10 +00001429 diag::warn_cxx98_compat_friend_redefinition :
1430 diag::err_redefinition)
Douglas Gregor238058c2010-05-18 05:45:02 +00001431 << Function->getDeclName();
Gabor Greif6a557d82010-08-28 15:46:56 +00001432 SemaRef.Diag(R->getLocation(), diag::note_previous_definition);
Richard Smith80ad52f2013-01-02 11:42:31 +00001433 if (!SemaRef.getLangOpts().CPlusPlus11)
Richard Smith53e53512011-10-19 00:54:10 +00001434 Function->setInvalidDecl();
Douglas Gregor238058c2010-05-18 05:45:02 +00001435 break;
1436 }
1437 }
1438 }
1439 }
Douglas Gregora735b202009-10-13 14:39:41 +00001440 }
1441
Richard Smitha41c97a2013-09-20 01:15:31 +00001442 if (Function->isLocalExternDecl() && !Function->getPreviousDecl())
1443 DC->makeDeclVisibleInContext(PrincipalDecl);
1444
John McCall76d32642010-04-24 01:30:58 +00001445 if (Function->isOverloadedOperator() && !DC->isRecord() &&
1446 PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
1447 PrincipalDecl->setNonMemberOperator();
1448
Sean Hunteb88ae52011-05-23 21:07:59 +00001449 assert(!D->isDefaulted() && "only methods should be defaulted");
Douglas Gregore53060f2009-06-25 22:08:12 +00001450 return Function;
1451}
1452
Douglas Gregord60e1052009-08-27 16:57:43 +00001453Decl *
1454TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
Francois Pichetaf0f4d02011-08-14 03:52:19 +00001455 TemplateParameterList *TemplateParams,
1456 bool IsClassScopeSpecialization) {
Douglas Gregor6b906862009-08-21 00:16:32 +00001457 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
Douglas Gregord60e1052009-08-27 16:57:43 +00001458 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +00001459 // We are creating a function template specialization from a function
1460 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +00001461 // specialization for this particular set of template arguments.
Richard Smithc95d4132013-05-03 23:46:09 +00001462 ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +00001463
Douglas Gregor1e1e9722012-03-28 14:34:23 +00001464 void *InsertPos = 0;
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001465 FunctionDecl *SpecFunc
Richard Smithc95d4132013-05-03 23:46:09 +00001466 = FunctionTemplate->findSpecialization(Innermost.begin(),
1467 Innermost.size(),
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001468 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001469
Douglas Gregor6b906862009-08-21 00:16:32 +00001470 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001471 if (SpecFunc)
1472 return SpecFunc;
Douglas Gregor6b906862009-08-21 00:16:32 +00001473 }
1474
John McCallb0cb0222010-03-27 05:57:59 +00001475 bool isFriend;
1476 if (FunctionTemplate)
1477 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1478 else
1479 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1480
Douglas Gregor79c22782010-01-16 20:21:20 +00001481 bool MergeWithParentScope = (TemplateParams != 0) ||
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001482 !(isa<Decl>(Owner) &&
Douglas Gregor79c22782010-01-16 20:21:20 +00001483 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
John McCall2a7fb272010-08-25 05:32:35 +00001484 LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Douglas Gregor48dd19b2009-05-14 21:44:34 +00001485
John McCall4eab39f2010-10-19 02:26:41 +00001486 // Instantiate enclosing template arguments for friends.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001487 SmallVector<TemplateParameterList *, 4> TempParamLists;
John McCall4eab39f2010-10-19 02:26:41 +00001488 unsigned NumTempParamLists = 0;
1489 if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) {
1490 TempParamLists.set_size(NumTempParamLists);
1491 for (unsigned I = 0; I != NumTempParamLists; ++I) {
1492 TemplateParameterList *TempParams = D->getTemplateParameterList(I);
1493 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1494 if (!InstParams)
1495 return NULL;
1496 TempParamLists[I] = InstParams;
1497 }
1498 }
1499
Chris Lattner5f9e2722011-07-23 10:55:15 +00001500 SmallVector<ParmVarDecl *, 4> Params;
Benjamin Kramerdc370c12012-01-20 14:42:32 +00001501 TypeSourceInfo *TInfo = SubstFunctionType(D, Params);
John McCall21ef0fa2010-03-11 09:03:00 +00001502 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001503 return 0;
Douglas Gregor71074fd2012-09-13 21:56:43 +00001504 QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001505
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001506 NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
1507 if (QualifierLoc) {
1508 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
John McCallb0cb0222010-03-27 05:57:59 +00001509 TemplateArgs);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001510 if (!QualifierLoc)
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001511 return 0;
John McCallb0cb0222010-03-27 05:57:59 +00001512 }
1513
1514 DeclContext *DC = Owner;
1515 if (isFriend) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001516 if (QualifierLoc) {
John McCallb0cb0222010-03-27 05:57:59 +00001517 CXXScopeSpec SS;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001518 SS.Adopt(QualifierLoc);
John McCallb0cb0222010-03-27 05:57:59 +00001519 DC = SemaRef.computeDeclContext(SS);
John McCallc54d6882010-10-19 05:01:53 +00001520
1521 if (DC && SemaRef.RequireCompleteDeclContext(SS, DC))
1522 return 0;
John McCallb0cb0222010-03-27 05:57:59 +00001523 } else {
1524 DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1525 D->getDeclContext(),
1526 TemplateArgs);
1527 }
1528 if (!DC) return 0;
1529 }
1530
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001531 // Build the instantiated method declaration.
John McCallb0cb0222010-03-27 05:57:59 +00001532 CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
Douglas Gregordec06662009-08-21 18:42:58 +00001533 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001534
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001535 SourceLocation StartLoc = D->getInnerLocStart();
Abramo Bagnara25777432010-08-11 22:01:17 +00001536 DeclarationNameInfo NameInfo
1537 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001538 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001539 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001540 StartLoc, NameInfo, T, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001541 Constructor->isExplicit(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001542 Constructor->isInlineSpecified(),
Richard Smith86c3ae42012-02-13 03:54:03 +00001543 false, Constructor->isConstexpr());
Richard Smithb5eb3f52013-05-17 02:19:35 +00001544
Richard Smith4841ca52013-04-10 05:48:59 +00001545 // Claim that the instantiation of a constructor or constructor template
1546 // inherits the same constructor that the template does.
Richard Smithb5eb3f52013-05-17 02:19:35 +00001547 if (CXXConstructorDecl *Inh = const_cast<CXXConstructorDecl *>(
1548 Constructor->getInheritedConstructor())) {
1549 // If we're instantiating a specialization of a function template, our
1550 // "inherited constructor" will actually itself be a function template.
1551 // Instantiate a declaration of it, too.
1552 if (FunctionTemplate) {
1553 assert(!TemplateParams && Inh->getDescribedFunctionTemplate() &&
1554 !Inh->getParent()->isDependentContext() &&
1555 "inheriting constructor template in dependent context?");
1556 Sema::InstantiatingTemplate Inst(SemaRef, Constructor->getLocation(),
1557 Inh);
1558 if (Inst)
1559 return 0;
1560 Sema::ContextRAII SavedContext(SemaRef, Inh->getDeclContext());
1561 LocalInstantiationScope LocalScope(SemaRef);
1562
1563 // Use the same template arguments that we deduced for the inheriting
1564 // constructor. There's no way they could be deduced differently.
1565 MultiLevelTemplateArgumentList InheritedArgs;
1566 InheritedArgs.addOuterTemplateArguments(TemplateArgs.getInnermost());
1567 Inh = cast_or_null<CXXConstructorDecl>(
1568 SemaRef.SubstDecl(Inh, Inh->getDeclContext(), InheritedArgs));
1569 if (!Inh)
1570 return 0;
1571 }
Richard Smith4841ca52013-04-10 05:48:59 +00001572 cast<CXXConstructorDecl>(Method)->setInheritedConstructor(Inh);
Richard Smithb5eb3f52013-05-17 02:19:35 +00001573 }
Douglas Gregor17e32f32009-08-21 22:43:28 +00001574 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001575 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001576 StartLoc, NameInfo, T, TInfo,
Abramo Bagnara25777432010-08-11 22:01:17 +00001577 Destructor->isInlineSpecified(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001578 false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001579 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001580 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001581 StartLoc, NameInfo, T, TInfo,
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001582 Conversion->isInlineSpecified(),
Douglas Gregorf5251602011-03-08 17:10:18 +00001583 Conversion->isExplicit(),
Richard Smith86c3ae42012-02-13 03:54:03 +00001584 Conversion->isConstexpr(),
Richard Smith9f569cc2011-10-01 02:31:28 +00001585 Conversion->getLocEnd());
Douglas Gregordec06662009-08-21 18:42:58 +00001586 } else {
Rafael Espindola72fdc892013-04-15 12:38:20 +00001587 StorageClass SC = D->isStatic() ? SC_Static : SC_None;
Abramo Bagnara25777432010-08-11 22:01:17 +00001588 Method = CXXMethodDecl::Create(SemaRef.Context, Record,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001589 StartLoc, NameInfo, T, TInfo,
Rafael Espindola72fdc892013-04-15 12:38:20 +00001590 SC, D->isInlineSpecified(),
Richard Smith86c3ae42012-02-13 03:54:03 +00001591 D->isConstexpr(), D->getLocEnd());
Douglas Gregordec06662009-08-21 18:42:58 +00001592 }
Douglas Gregor6b906862009-08-21 00:16:32 +00001593
Richard Smithd4497dd2013-01-25 00:08:28 +00001594 if (D->isInlined())
1595 Method->setImplicitlyInline();
1596
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001597 if (QualifierLoc)
1598 Method->setQualifierInfo(QualifierLoc);
John McCallb6217662010-03-15 10:12:16 +00001599
Douglas Gregord60e1052009-08-27 16:57:43 +00001600 if (TemplateParams) {
1601 // Our resulting instantiation is actually a function template, since we
1602 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +00001603 //
Douglas Gregord60e1052009-08-27 16:57:43 +00001604 // template<typename T>
1605 // struct X {
1606 // template<typename U> void f(T, U);
1607 // };
1608 //
1609 // X<int> x;
1610 //
1611 // We are instantiating the member template "f" within X<int>, which means
1612 // substituting int for T, but leaving "f" as a member function template.
1613 // Build the function template itself.
1614 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1615 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001616 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +00001617 TemplateParams, Method);
John McCallb0cb0222010-03-27 05:57:59 +00001618 if (isFriend) {
1619 FunctionTemplate->setLexicalDeclContext(Owner);
Richard Smith22050f22013-07-17 23:53:16 +00001620 FunctionTemplate->setObjectOfFriendDecl();
John McCallb0cb0222010-03-27 05:57:59 +00001621 } else if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +00001622 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +00001623 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001624 } else if (FunctionTemplate) {
1625 // Record this function template specialization.
Richard Smithc95d4132013-05-03 23:46:09 +00001626 ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001627 Method->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001628 TemplateArgumentList::CreateCopy(SemaRef.Context,
Richard Smithc95d4132013-05-03 23:46:09 +00001629 Innermost.begin(),
1630 Innermost.size()),
Douglas Gregor1e1e9722012-03-28 14:34:23 +00001631 /*InsertPos=*/0);
John McCallb0cb0222010-03-27 05:57:59 +00001632 } else if (!isFriend) {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001633 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +00001634 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001635 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001636
Mike Stump1eb44332009-09-09 15:08:12 +00001637 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +00001638 // out-of-line, the instantiation will have the same lexical
1639 // context (which will be a namespace scope) as the template.
John McCallb0cb0222010-03-27 05:57:59 +00001640 if (isFriend) {
John McCall4eab39f2010-10-19 02:26:41 +00001641 if (NumTempParamLists)
1642 Method->setTemplateParameterListsInfo(SemaRef.Context,
1643 NumTempParamLists,
1644 TempParamLists.data());
1645
John McCallb0cb0222010-03-27 05:57:59 +00001646 Method->setLexicalDeclContext(Owner);
Richard Smith22050f22013-07-17 23:53:16 +00001647 Method->setObjectOfFriendDecl();
John McCallb0cb0222010-03-27 05:57:59 +00001648 } else if (D->isOutOfLine())
Douglas Gregor7caa6822009-07-24 20:34:43 +00001649 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001650
Douglas Gregor5545e162009-03-24 00:38:23 +00001651 // Attach the parameters
1652 for (unsigned P = 0; P < Params.size(); ++P)
1653 Params[P]->setOwningFunction(Method);
David Blaikie4278c652011-09-21 18:16:56 +00001654 Method->setParams(Params);
Douglas Gregor5545e162009-03-24 00:38:23 +00001655
1656 if (InitMethodInstantiation(Method, D))
1657 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001658
Abramo Bagnara25777432010-08-11 22:01:17 +00001659 LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
1660 Sema::ForRedeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001661
John McCallb0cb0222010-03-27 05:57:59 +00001662 if (!FunctionTemplate || TemplateParams || isFriend) {
1663 SemaRef.LookupQualifiedName(Previous, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001664
Douglas Gregordec06662009-08-21 18:42:58 +00001665 // In C++, the previous declaration we find might be a tag type
1666 // (class or enum). In this case, the new declaration will hide the
1667 // tag type. Note that this does does not apply if we're declaring a
1668 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001669 if (Previous.isSingleTagDecl())
1670 Previous.clear();
Douglas Gregordec06662009-08-21 18:42:58 +00001671 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001672
Francois Pichetaf0f4d02011-08-14 03:52:19 +00001673 if (!IsClassScopeSpecialization)
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00001674 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001675
Douglas Gregor4ba31362009-12-01 17:24:26 +00001676 if (D->isPure())
1677 SemaRef.CheckPureMethod(Method, SourceRange());
1678
John McCall1f2e1a92012-08-10 03:15:35 +00001679 // Propagate access. For a non-friend declaration, the access is
1680 // whatever we're propagating from. For a friend, it should be the
1681 // previous declaration we just found.
1682 if (isFriend && Method->getPreviousDecl())
1683 Method->setAccess(Method->getPreviousDecl()->getAccess());
1684 else
1685 Method->setAccess(D->getAccess());
1686 if (FunctionTemplate)
1687 FunctionTemplate->setAccess(Method->getAccess());
John McCall46460a62010-01-20 21:53:11 +00001688
Anders Carlsson9eefa222011-01-20 06:52:44 +00001689 SemaRef.CheckOverrideControl(Method);
1690
Eli Friedman3bc45152011-11-15 22:39:08 +00001691 // If a function is defined as defaulted or deleted, mark it as such now.
Richard Smithac713512012-12-08 02:53:02 +00001692 if (D->isExplicitlyDefaulted())
1693 SemaRef.SetDeclDefaulted(Method, Method->getLocation());
Eli Friedman3bc45152011-11-15 22:39:08 +00001694 if (D->isDeletedAsWritten())
Richard Smithac713512012-12-08 02:53:02 +00001695 SemaRef.SetDeclDeleted(Method, Method->getLocation());
Eli Friedman3bc45152011-11-15 22:39:08 +00001696
John McCall1f2e1a92012-08-10 03:15:35 +00001697 // If there's a function template, let our caller handle it.
John McCallb0cb0222010-03-27 05:57:59 +00001698 if (FunctionTemplate) {
John McCall1f2e1a92012-08-10 03:15:35 +00001699 // do nothing
1700
1701 // Don't hide a (potentially) valid declaration with an invalid one.
John McCallb0cb0222010-03-27 05:57:59 +00001702 } else if (Method->isInvalidDecl() && !Previous.empty()) {
John McCall1f2e1a92012-08-10 03:15:35 +00001703 // do nothing
1704
1705 // Otherwise, check access to friends and make them visible.
1706 } else if (isFriend) {
1707 // We only need to re-check access for methods which we didn't
1708 // manage to match during parsing.
1709 if (!D->getPreviousDecl())
1710 SemaRef.CheckFriendAccess(Method);
1711
1712 Record->makeDeclVisibleInContext(Method);
1713
1714 // Otherwise, add the declaration. We don't need to do this for
1715 // class-scope specializations because we'll have matched them with
1716 // the appropriate template.
1717 } else if (!IsClassScopeSpecialization) {
1718 Owner->addDecl(Method);
John McCallb0cb0222010-03-27 05:57:59 +00001719 }
Sean Hunteb88ae52011-05-23 21:07:59 +00001720
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001721 return Method;
1722}
1723
Douglas Gregor615c5d42009-03-24 16:43:20 +00001724Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +00001725 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +00001726}
1727
Douglas Gregor03b2b072009-03-24 00:15:49 +00001728Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001729 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +00001730}
1731
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001732Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001733 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001734}
1735
Eli Friedmanded99792013-06-27 23:21:55 +00001736Decl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
David Blaikie66874fb2013-02-21 01:47:18 +00001737 return SemaRef.SubstParmVarDecl(D, TemplateArgs, /*indexAdjustment*/ 0, None,
1738 /*ExpectParameterPack=*/ false);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001739}
1740
John McCalle29ba202009-08-20 01:44:21 +00001741Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1742 TemplateTypeParmDecl *D) {
1743 // TODO: don't always clone when decls are refcounted.
Chandler Carruth4fb86f82011-05-01 00:51:33 +00001744 assert(D->getTypeForDecl()->isTemplateTypeParmType());
Mike Stump1eb44332009-09-09 15:08:12 +00001745
John McCalle29ba202009-08-20 01:44:21 +00001746 TemplateTypeParmDecl *Inst =
Abramo Bagnara344577e2011-03-06 15:48:19 +00001747 TemplateTypeParmDecl::Create(SemaRef.Context, Owner,
1748 D->getLocStart(), D->getLocation(),
Chandler Carruth4fb86f82011-05-01 00:51:33 +00001749 D->getDepth() - TemplateArgs.getNumLevels(),
1750 D->getIndex(), D->getIdentifier(),
John McCalle29ba202009-08-20 01:44:21 +00001751 D->wasDeclaredWithTypename(),
1752 D->isParameterPack());
Douglas Gregor9a299e02011-03-04 17:52:15 +00001753 Inst->setAccess(AS_public);
John McCalle29ba202009-08-20 01:44:21 +00001754
David Majnemer9d57b8d2013-08-28 23:48:32 +00001755 if (D->hasDefaultArgument()) {
1756 TypeSourceInfo *InstantiatedDefaultArg =
1757 SemaRef.SubstType(D->getDefaultArgumentInfo(), TemplateArgs,
1758 D->getDefaultArgumentLoc(), D->getDeclName());
1759 if (InstantiatedDefaultArg)
1760 Inst->setDefaultArgument(InstantiatedDefaultArg, false);
1761 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001762
1763 // Introduce this template parameter's instantiation into the instantiation
Douglas Gregor550d9b22009-10-31 17:21:17 +00001764 // scope.
1765 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001766
John McCalle29ba202009-08-20 01:44:21 +00001767 return Inst;
1768}
1769
Douglas Gregor33642df2009-10-23 23:25:44 +00001770Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1771 NonTypeTemplateParmDecl *D) {
1772 // Substitute into the type of the non-type template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001773 TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc();
Chris Lattner5f9e2722011-07-23 10:55:15 +00001774 SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten;
1775 SmallVector<QualType, 4> ExpandedParameterPackTypes;
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001776 bool IsExpandedParameterPack = false;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001777 TypeSourceInfo *DI;
Douglas Gregor33642df2009-10-23 23:25:44 +00001778 QualType T;
Douglas Gregor33642df2009-10-23 23:25:44 +00001779 bool Invalid = false;
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001780
1781 if (D->isExpandedParameterPack()) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001782 // The non-type template parameter pack is an already-expanded pack
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001783 // expansion of types. Substitute into each of the expanded types.
1784 ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes());
1785 ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes());
1786 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
1787 TypeSourceInfo *NewDI =SemaRef.SubstType(D->getExpansionTypeSourceInfo(I),
1788 TemplateArgs,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001789 D->getLocation(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001790 D->getDeclName());
1791 if (!NewDI)
1792 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001793
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001794 ExpandedParameterPackTypesAsWritten.push_back(NewDI);
1795 QualType NewT =SemaRef.CheckNonTypeTemplateParameterType(NewDI->getType(),
1796 D->getLocation());
1797 if (NewT.isNull())
1798 return 0;
1799 ExpandedParameterPackTypes.push_back(NewT);
1800 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001801
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001802 IsExpandedParameterPack = true;
1803 DI = D->getTypeSourceInfo();
1804 T = DI->getType();
Richard Smith6964b3f2012-09-07 02:06:42 +00001805 } else if (D->isPackExpansion()) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001806 // The non-type template parameter pack's type is a pack expansion of types.
1807 // Determine whether we need to expand this parameter pack into separate
1808 // types.
David Blaikie39e6ab42013-02-18 22:06:02 +00001809 PackExpansionTypeLoc Expansion = TL.castAs<PackExpansionTypeLoc>();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001810 TypeLoc Pattern = Expansion.getPatternLoc();
Chris Lattner5f9e2722011-07-23 10:55:15 +00001811 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001812 SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001813
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001814 // Determine whether the set of unexpanded parameter packs can and should
1815 // be expanded.
1816 bool Expand = true;
1817 bool RetainExpansion = false;
David Blaikiedc84cd52013-02-20 22:23:23 +00001818 Optional<unsigned> OrigNumExpansions
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001819 = Expansion.getTypePtr()->getNumExpansions();
David Blaikiedc84cd52013-02-20 22:23:23 +00001820 Optional<unsigned> NumExpansions = OrigNumExpansions;
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001821 if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(),
1822 Pattern.getSourceRange(),
David Blaikiea71f9d02011-09-22 02:34:54 +00001823 Unexpanded,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001824 TemplateArgs,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001825 Expand, RetainExpansion,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001826 NumExpansions))
1827 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001828
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001829 if (Expand) {
1830 for (unsigned I = 0; I != *NumExpansions; ++I) {
1831 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
1832 TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001833 D->getLocation(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001834 D->getDeclName());
1835 if (!NewDI)
1836 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001837
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001838 ExpandedParameterPackTypesAsWritten.push_back(NewDI);
1839 QualType NewT = SemaRef.CheckNonTypeTemplateParameterType(
1840 NewDI->getType(),
1841 D->getLocation());
1842 if (NewT.isNull())
1843 return 0;
1844 ExpandedParameterPackTypes.push_back(NewT);
1845 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001846
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001847 // Note that we have an expanded parameter pack. The "type" of this
1848 // expanded parameter pack is the original expansion type, but callers
1849 // will end up using the expanded parameter pack types for type-checking.
1850 IsExpandedParameterPack = true;
1851 DI = D->getTypeSourceInfo();
1852 T = DI->getType();
1853 } else {
1854 // We cannot fully expand the pack expansion now, so substitute into the
1855 // pattern and create a new pack expansion type.
1856 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
1857 TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001858 D->getLocation(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001859 D->getDeclName());
1860 if (!NewPattern)
1861 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001862
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001863 DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(),
1864 NumExpansions);
1865 if (!DI)
1866 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001867
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001868 T = DI->getType();
1869 }
1870 } else {
1871 // Simple case: substitution into a parameter that is not a parameter pack.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001872 DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001873 D->getLocation(), D->getDeclName());
1874 if (!DI)
1875 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001876
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001877 // Check that this type is acceptable for a non-type template parameter.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001878 T = SemaRef.CheckNonTypeTemplateParameterType(DI->getType(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001879 D->getLocation());
1880 if (T.isNull()) {
1881 T = SemaRef.Context.IntTy;
1882 Invalid = true;
1883 }
Douglas Gregor33642df2009-10-23 23:25:44 +00001884 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001885
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001886 NonTypeTemplateParmDecl *Param;
1887 if (IsExpandedParameterPack)
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001888 Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001889 D->getInnerLocStart(),
1890 D->getLocation(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001891 D->getDepth() - TemplateArgs.getNumLevels(),
1892 D->getPosition(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001893 D->getIdentifier(), T,
1894 DI,
1895 ExpandedParameterPackTypes.data(),
1896 ExpandedParameterPackTypes.size(),
1897 ExpandedParameterPackTypesAsWritten.data());
1898 else
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001899 Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001900 D->getInnerLocStart(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001901 D->getLocation(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001902 D->getDepth() - TemplateArgs.getNumLevels(),
1903 D->getPosition(),
1904 D->getIdentifier(), T,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001905 D->isParameterPack(), DI);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001906
Douglas Gregor9a299e02011-03-04 17:52:15 +00001907 Param->setAccess(AS_public);
Douglas Gregor33642df2009-10-23 23:25:44 +00001908 if (Invalid)
1909 Param->setInvalidDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001910
David Majnemer9d57b8d2013-08-28 23:48:32 +00001911 if (D->hasDefaultArgument()) {
1912 ExprResult Value = SemaRef.SubstExpr(D->getDefaultArgument(), TemplateArgs);
1913 if (!Value.isInvalid())
1914 Param->setDefaultArgument(Value.get(), false);
1915 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001916
1917 // Introduce this template parameter's instantiation into the instantiation
Douglas Gregor550d9b22009-10-31 17:21:17 +00001918 // scope.
1919 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001920 return Param;
1921}
1922
Richard Smith6964b3f2012-09-07 02:06:42 +00001923static void collectUnexpandedParameterPacks(
1924 Sema &S,
1925 TemplateParameterList *Params,
1926 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
1927 for (TemplateParameterList::const_iterator I = Params->begin(),
1928 E = Params->end(); I != E; ++I) {
1929 if ((*I)->isTemplateParameterPack())
1930 continue;
1931 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*I))
1932 S.collectUnexpandedParameterPacks(NTTP->getTypeSourceInfo()->getTypeLoc(),
1933 Unexpanded);
1934 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(*I))
1935 collectUnexpandedParameterPacks(S, TTP->getTemplateParameters(),
1936 Unexpanded);
1937 }
1938}
1939
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001940Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001941TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1942 TemplateTemplateParmDecl *D) {
1943 // Instantiate the template parameter list of the template template parameter.
1944 TemplateParameterList *TempParams = D->getTemplateParameters();
1945 TemplateParameterList *InstParams;
Richard Smith6964b3f2012-09-07 02:06:42 +00001946 SmallVector<TemplateParameterList*, 8> ExpandedParams;
1947
1948 bool IsExpandedParameterPack = false;
1949
1950 if (D->isExpandedParameterPack()) {
1951 // The template template parameter pack is an already-expanded pack
1952 // expansion of template parameters. Substitute into each of the expanded
1953 // parameters.
1954 ExpandedParams.reserve(D->getNumExpansionTemplateParameters());
1955 for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
1956 I != N; ++I) {
1957 LocalInstantiationScope Scope(SemaRef);
1958 TemplateParameterList *Expansion =
1959 SubstTemplateParams(D->getExpansionTemplateParameters(I));
1960 if (!Expansion)
1961 return 0;
1962 ExpandedParams.push_back(Expansion);
1963 }
1964
1965 IsExpandedParameterPack = true;
1966 InstParams = TempParams;
1967 } else if (D->isPackExpansion()) {
1968 // The template template parameter pack expands to a pack of template
1969 // template parameters. Determine whether we need to expand this parameter
1970 // pack into separate parameters.
1971 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
1972 collectUnexpandedParameterPacks(SemaRef, D->getTemplateParameters(),
1973 Unexpanded);
1974
1975 // Determine whether the set of unexpanded parameter packs can and should
1976 // be expanded.
1977 bool Expand = true;
1978 bool RetainExpansion = false;
David Blaikiedc84cd52013-02-20 22:23:23 +00001979 Optional<unsigned> NumExpansions;
Richard Smith6964b3f2012-09-07 02:06:42 +00001980 if (SemaRef.CheckParameterPacksForExpansion(D->getLocation(),
1981 TempParams->getSourceRange(),
1982 Unexpanded,
1983 TemplateArgs,
1984 Expand, RetainExpansion,
1985 NumExpansions))
1986 return 0;
1987
1988 if (Expand) {
1989 for (unsigned I = 0; I != *NumExpansions; ++I) {
1990 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
1991 LocalInstantiationScope Scope(SemaRef);
1992 TemplateParameterList *Expansion = SubstTemplateParams(TempParams);
1993 if (!Expansion)
1994 return 0;
1995 ExpandedParams.push_back(Expansion);
1996 }
1997
1998 // Note that we have an expanded parameter pack. The "type" of this
1999 // expanded parameter pack is the original expansion type, but callers
2000 // will end up using the expanded parameter pack types for type-checking.
2001 IsExpandedParameterPack = true;
2002 InstParams = TempParams;
2003 } else {
2004 // We cannot fully expand the pack expansion now, so just substitute
2005 // into the pattern.
2006 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
2007
2008 LocalInstantiationScope Scope(SemaRef);
2009 InstParams = SubstTemplateParams(TempParams);
2010 if (!InstParams)
2011 return 0;
2012 }
2013 } else {
Douglas Gregor9106ef72009-11-11 16:58:32 +00002014 // Perform the actual substitution of template parameters within a new,
2015 // local instantiation scope.
John McCall2a7fb272010-08-25 05:32:35 +00002016 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor9106ef72009-11-11 16:58:32 +00002017 InstParams = SubstTemplateParams(TempParams);
2018 if (!InstParams)
Richard Smith6964b3f2012-09-07 02:06:42 +00002019 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002020 }
2021
Douglas Gregor9106ef72009-11-11 16:58:32 +00002022 // Build the template template parameter.
Richard Smith6964b3f2012-09-07 02:06:42 +00002023 TemplateTemplateParmDecl *Param;
2024 if (IsExpandedParameterPack)
2025 Param = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner,
2026 D->getLocation(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002027 D->getDepth() - TemplateArgs.getNumLevels(),
Richard Smith6964b3f2012-09-07 02:06:42 +00002028 D->getPosition(),
2029 D->getIdentifier(), InstParams,
2030 ExpandedParams);
2031 else
2032 Param = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner,
2033 D->getLocation(),
2034 D->getDepth() - TemplateArgs.getNumLevels(),
2035 D->getPosition(),
2036 D->isParameterPack(),
2037 D->getIdentifier(), InstParams);
David Majnemer9d57b8d2013-08-28 23:48:32 +00002038 if (D->hasDefaultArgument()) {
2039 NestedNameSpecifierLoc QualifierLoc =
2040 D->getDefaultArgument().getTemplateQualifierLoc();
2041 QualifierLoc =
2042 SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgs);
2043 TemplateName TName = SemaRef.SubstTemplateName(
2044 QualifierLoc, D->getDefaultArgument().getArgument().getAsTemplate(),
2045 D->getDefaultArgument().getTemplateNameLoc(), TemplateArgs);
2046 if (!TName.isNull())
2047 Param->setDefaultArgument(
2048 TemplateArgumentLoc(TemplateArgument(TName),
2049 D->getDefaultArgument().getTemplateQualifierLoc(),
2050 D->getDefaultArgument().getTemplateNameLoc()),
2051 false);
2052 }
Douglas Gregor9a299e02011-03-04 17:52:15 +00002053 Param->setAccess(AS_public);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002054
2055 // Introduce this template parameter's instantiation into the instantiation
Douglas Gregor9106ef72009-11-11 16:58:32 +00002056 // scope.
2057 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002058
Douglas Gregor9106ef72009-11-11 16:58:32 +00002059 return Param;
2060}
2061
Douglas Gregor48c32a72009-11-17 06:07:40 +00002062Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Douglas Gregordb992412011-02-25 16:33:46 +00002063 // Using directives are never dependent (and never contain any types or
2064 // expressions), so they require no explicit instantiation work.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002065
Douglas Gregor48c32a72009-11-17 06:07:40 +00002066 UsingDirectiveDecl *Inst
2067 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002068 D->getNamespaceKeyLocation(),
Douglas Gregordb992412011-02-25 16:33:46 +00002069 D->getQualifierLoc(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002070 D->getIdentLocation(),
2071 D->getNominatedNamespace(),
Douglas Gregor48c32a72009-11-17 06:07:40 +00002072 D->getCommonAncestor());
Abramo Bagnara536afbe2012-09-05 09:55:10 +00002073
2074 // Add the using directive to its declaration context
2075 // only if this is not a function or method.
2076 if (!Owner->isFunctionOrMethod())
2077 Owner->addDecl(Inst);
2078
Douglas Gregor48c32a72009-11-17 06:07:40 +00002079 return Inst;
2080}
2081
John McCalled976492009-12-04 22:46:56 +00002082Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
Douglas Gregor1b398202010-09-29 17:58:28 +00002083
2084 // The nested name specifier may be dependent, for example
2085 // template <typename T> struct t {
2086 // struct s1 { T f1(); };
2087 // struct s2 : s1 { using s1::f1; };
2088 // };
2089 // template struct t<int>;
2090 // Here, in using s1::f1, s1 refers to t<T>::s1;
2091 // we need to substitute for t<int>::s1.
Douglas Gregor5149f372011-02-25 15:54:31 +00002092 NestedNameSpecifierLoc QualifierLoc
2093 = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
2094 TemplateArgs);
2095 if (!QualifierLoc)
Douglas Gregordc355712011-02-25 00:36:19 +00002096 return 0;
Douglas Gregor1b398202010-09-29 17:58:28 +00002097
2098 // The name info is non-dependent, so no transformation
2099 // is required.
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002100 DeclarationNameInfo NameInfo = D->getNameInfo();
John McCalled976492009-12-04 22:46:56 +00002101
John McCall9f54ad42009-12-10 09:41:52 +00002102 // We only need to do redeclaration lookups if we're in a class
2103 // scope (in fact, it's not really even possible in non-class
2104 // scopes).
2105 bool CheckRedeclaration = Owner->isRecord();
2106
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002107 LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
2108 Sema::ForRedeclaration);
John McCall9f54ad42009-12-10 09:41:52 +00002109
John McCalled976492009-12-04 22:46:56 +00002110 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
Enea Zaffanella8d030c72013-07-22 10:54:09 +00002111 D->getUsingLoc(),
Douglas Gregor5149f372011-02-25 15:54:31 +00002112 QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002113 NameInfo,
Enea Zaffanella8d030c72013-07-22 10:54:09 +00002114 D->hasTypename());
John McCalled976492009-12-04 22:46:56 +00002115
Douglas Gregor5149f372011-02-25 15:54:31 +00002116 CXXScopeSpec SS;
2117 SS.Adopt(QualifierLoc);
John McCall9f54ad42009-12-10 09:41:52 +00002118 if (CheckRedeclaration) {
2119 Prev.setHideTags(false);
2120 SemaRef.LookupQualifiedName(Prev, Owner);
2121
2122 // Check for invalid redeclarations.
Enea Zaffanella8d030c72013-07-22 10:54:09 +00002123 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLoc(),
2124 D->hasTypename(), SS,
John McCall9f54ad42009-12-10 09:41:52 +00002125 D->getLocation(), Prev))
2126 NewUD->setInvalidDecl();
2127
2128 }
2129
2130 if (!NewUD->isInvalidDecl() &&
Enea Zaffanella8d030c72013-07-22 10:54:09 +00002131 SemaRef.CheckUsingDeclQualifier(D->getUsingLoc(), SS,
John McCalled976492009-12-04 22:46:56 +00002132 D->getLocation()))
2133 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00002134
John McCalled976492009-12-04 22:46:56 +00002135 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
2136 NewUD->setAccess(D->getAccess());
2137 Owner->addDecl(NewUD);
2138
John McCall9f54ad42009-12-10 09:41:52 +00002139 // Don't process the shadow decls for an invalid decl.
2140 if (NewUD->isInvalidDecl())
2141 return NewUD;
2142
Richard Smithc5a89a12012-04-02 01:30:27 +00002143 if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) {
2144 if (SemaRef.CheckInheritingConstructorUsingDecl(NewUD))
2145 NewUD->setInvalidDecl();
2146 return NewUD;
2147 }
2148
John McCall323c3102009-12-22 22:26:37 +00002149 bool isFunctionScope = Owner->isFunctionOrMethod();
2150
John McCall9f54ad42009-12-10 09:41:52 +00002151 // Process the shadow decls.
2152 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
2153 I != E; ++I) {
2154 UsingShadowDecl *Shadow = *I;
2155 NamedDecl *InstTarget =
Douglas Gregorb7107222011-03-04 19:46:35 +00002156 cast_or_null<NamedDecl>(SemaRef.FindInstantiatedDecl(
2157 Shadow->getLocation(),
2158 Shadow->getTargetDecl(),
2159 TemplateArgs));
2160 if (!InstTarget)
2161 return 0;
John McCall9f54ad42009-12-10 09:41:52 +00002162
2163 if (CheckRedeclaration &&
2164 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
2165 continue;
2166
2167 UsingShadowDecl *InstShadow
2168 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
2169 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
John McCall323c3102009-12-22 22:26:37 +00002170
2171 if (isFunctionScope)
2172 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
John McCall9f54ad42009-12-10 09:41:52 +00002173 }
John McCalled976492009-12-04 22:46:56 +00002174
2175 return NewUD;
2176}
2177
2178Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00002179 // Ignore these; we handle them in bulk when processing the UsingDecl.
2180 return 0;
John McCalled976492009-12-04 22:46:56 +00002181}
2182
John McCall7ba107a2009-11-18 02:36:19 +00002183Decl * TemplateDeclInstantiator
2184 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Douglas Gregor5149f372011-02-25 15:54:31 +00002185 NestedNameSpecifierLoc QualifierLoc
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002186 = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
Douglas Gregor5149f372011-02-25 15:54:31 +00002187 TemplateArgs);
2188 if (!QualifierLoc)
Anders Carlsson0dde18e2009-08-28 15:18:15 +00002189 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002190
Anders Carlsson0dde18e2009-08-28 15:18:15 +00002191 CXXScopeSpec SS;
Douglas Gregor5149f372011-02-25 15:54:31 +00002192 SS.Adopt(QualifierLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002193
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002194 // Since NameInfo refers to a typename, it cannot be a C++ special name.
Benjamin Krameraccaf192012-11-14 15:08:31 +00002195 // Hence, no transformation is required for it.
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002196 DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00002197 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00002198 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002199 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00002200 /*instantiation*/ true,
2201 /*typename*/ true, D->getTypenameLoc());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00002202 if (UD)
John McCalled976492009-12-04 22:46:56 +00002203 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
2204
John McCall7ba107a2009-11-18 02:36:19 +00002205 return UD;
2206}
2207
2208Decl * TemplateDeclInstantiator
2209 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Douglas Gregor5149f372011-02-25 15:54:31 +00002210 NestedNameSpecifierLoc QualifierLoc
2211 = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), TemplateArgs);
2212 if (!QualifierLoc)
John McCall7ba107a2009-11-18 02:36:19 +00002213 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002214
John McCall7ba107a2009-11-18 02:36:19 +00002215 CXXScopeSpec SS;
Douglas Gregor5149f372011-02-25 15:54:31 +00002216 SS.Adopt(QualifierLoc);
John McCall7ba107a2009-11-18 02:36:19 +00002217
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002218 DeclarationNameInfo NameInfo
2219 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
2220
John McCall7ba107a2009-11-18 02:36:19 +00002221 NamedDecl *UD =
2222 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002223 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00002224 /*instantiation*/ true,
2225 /*typename*/ false, SourceLocation());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00002226 if (UD)
John McCalled976492009-12-04 22:46:56 +00002227 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
2228
Anders Carlsson0d8df782009-08-29 19:37:28 +00002229 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00002230}
2231
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002232
2233Decl *TemplateDeclInstantiator::VisitClassScopeFunctionSpecializationDecl(
2234 ClassScopeFunctionSpecializationDecl *Decl) {
2235 CXXMethodDecl *OldFD = Decl->getSpecialization();
Nico Weber6b020092012-06-25 17:21:05 +00002236 CXXMethodDecl *NewFD = cast<CXXMethodDecl>(VisitCXXMethodDecl(OldFD,
2237 0, true));
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002238
2239 LookupResult Previous(SemaRef, NewFD->getNameInfo(), Sema::LookupOrdinaryName,
2240 Sema::ForRedeclaration);
2241
Nico Weber6b020092012-06-25 17:21:05 +00002242 TemplateArgumentListInfo TemplateArgs;
2243 TemplateArgumentListInfo* TemplateArgsPtr = 0;
2244 if (Decl->hasExplicitTemplateArgs()) {
2245 TemplateArgs = Decl->templateArgs();
2246 TemplateArgsPtr = &TemplateArgs;
2247 }
2248
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002249 SemaRef.LookupQualifiedName(Previous, SemaRef.CurContext);
Nico Weber6b020092012-06-25 17:21:05 +00002250 if (SemaRef.CheckFunctionTemplateSpecialization(NewFD, TemplateArgsPtr,
2251 Previous)) {
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002252 NewFD->setInvalidDecl();
2253 return NewFD;
2254 }
2255
2256 // Associate the specialization with the pattern.
2257 FunctionDecl *Specialization = cast<FunctionDecl>(Previous.getFoundDecl());
2258 assert(Specialization && "Class scope Specialization is null");
2259 SemaRef.Context.setClassScopeSpecializationPattern(Specialization, OldFD);
2260
2261 return NewFD;
2262}
2263
Alexey Bataevc6400582013-03-22 06:34:35 +00002264Decl *TemplateDeclInstantiator::VisitOMPThreadPrivateDecl(
2265 OMPThreadPrivateDecl *D) {
Alexey Bataev6af701f2013-05-13 04:18:18 +00002266 SmallVector<Expr *, 5> Vars;
2267 for (ArrayRef<Expr *>::iterator I = D->varlist_begin(),
2268 E = D->varlist_end();
Alexey Bataevc6400582013-03-22 06:34:35 +00002269 I != E; ++I) {
2270 Expr *Var = SemaRef.SubstExpr(*I, TemplateArgs).take();
2271 assert(isa<DeclRefExpr>(Var) && "threadprivate arg is not a DeclRefExpr");
Alexey Bataev6af701f2013-05-13 04:18:18 +00002272 Vars.push_back(Var);
Alexey Bataevc6400582013-03-22 06:34:35 +00002273 }
2274
2275 OMPThreadPrivateDecl *TD =
2276 SemaRef.CheckOMPThreadPrivateDecl(D->getLocation(), Vars);
2277
2278 return TD;
2279}
2280
Eli Friedmanded99792013-06-27 23:21:55 +00002281Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D) {
2282 return VisitFunctionDecl(D, 0);
2283}
2284
2285Decl *TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D) {
2286 return VisitCXXMethodDecl(D, 0);
2287}
2288
2289Decl *TemplateDeclInstantiator::VisitRecordDecl(RecordDecl *D) {
2290 llvm_unreachable("There are only CXXRecordDecls in C++");
2291}
2292
2293Decl *
2294TemplateDeclInstantiator::VisitClassTemplateSpecializationDecl(
2295 ClassTemplateSpecializationDecl *D) {
2296 llvm_unreachable("Only ClassTemplatePartialSpecializationDecls occur"
2297 "inside templates");
2298}
2299
Larisse Voufoef4579c2013-08-06 01:03:05 +00002300Decl *TemplateDeclInstantiator::VisitVarTemplateSpecializationDecl(
2301 VarTemplateSpecializationDecl *D) {
2302
2303 TemplateArgumentListInfo VarTemplateArgsInfo;
2304 VarTemplateDecl *VarTemplate = D->getSpecializedTemplate();
2305 assert(VarTemplate &&
2306 "A template specialization without specialized template?");
2307
2308 // Substitute the current template arguments.
2309 const TemplateArgumentListInfo &TemplateArgsInfo = D->getTemplateArgsInfo();
2310 VarTemplateArgsInfo.setLAngleLoc(TemplateArgsInfo.getLAngleLoc());
2311 VarTemplateArgsInfo.setRAngleLoc(TemplateArgsInfo.getRAngleLoc());
2312
2313 if (SemaRef.Subst(TemplateArgsInfo.getArgumentArray(),
2314 TemplateArgsInfo.size(), VarTemplateArgsInfo, TemplateArgs))
2315 return 0;
2316
2317 // Check that the template argument list is well-formed for this template.
2318 SmallVector<TemplateArgument, 4> Converted;
2319 bool ExpansionIntoFixedList = false;
2320 if (SemaRef.CheckTemplateArgumentList(
2321 VarTemplate, VarTemplate->getLocStart(),
2322 const_cast<TemplateArgumentListInfo &>(VarTemplateArgsInfo), false,
2323 Converted, &ExpansionIntoFixedList))
2324 return 0;
2325
2326 // Find the variable template specialization declaration that
2327 // corresponds to these arguments.
2328 void *InsertPos = 0;
2329 if (VarTemplateSpecializationDecl *VarSpec = VarTemplate->findSpecialization(
2330 Converted.data(), Converted.size(), InsertPos))
2331 // If we already have a variable template specialization, return it.
2332 return VarSpec;
2333
2334 return VisitVarTemplateSpecializationDecl(VarTemplate, D, InsertPos,
2335 VarTemplateArgsInfo, Converted);
2336}
2337
2338Decl *TemplateDeclInstantiator::VisitVarTemplateSpecializationDecl(
2339 VarTemplateDecl *VarTemplate, VarDecl *D, void *InsertPos,
2340 const TemplateArgumentListInfo &TemplateArgsInfo,
2341 SmallVectorImpl<TemplateArgument> &Converted) {
2342
2343 // If this is the variable for an anonymous struct or union,
2344 // instantiate the anonymous struct/union type first.
2345 if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
2346 if (RecordTy->getDecl()->isAnonymousStructOrUnion())
2347 if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
2348 return 0;
2349
2350 // Do substitution on the type of the declaration
2351 TypeSourceInfo *DI =
2352 SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs,
2353 D->getTypeSpecStartLoc(), D->getDeclName());
2354 if (!DI)
2355 return 0;
2356
2357 if (DI->getType()->isFunctionType()) {
2358 SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function)
2359 << D->isStaticDataMember() << DI->getType();
2360 return 0;
2361 }
2362
2363 // Build the instantiated declaration
2364 VarTemplateSpecializationDecl *Var = VarTemplateSpecializationDecl::Create(
2365 SemaRef.Context, Owner, D->getInnerLocStart(), D->getLocation(),
2366 VarTemplate, DI->getType(), DI, D->getStorageClass(), Converted.data(),
2367 Converted.size());
2368 Var->setTemplateArgsInfo(TemplateArgsInfo);
2369 VarTemplate->AddSpecialization(Var, InsertPos);
2370
2371 // Substitute the nested name specifier, if any.
2372 if (SubstQualifier(D, Var))
2373 return 0;
2374
2375 SemaRef.BuildVariableInstantiation(Var, D, TemplateArgs, LateAttrs,
Richard Smitha41c97a2013-09-20 01:15:31 +00002376 Owner, StartingScope);
Larisse Voufoef4579c2013-08-06 01:03:05 +00002377
2378 return Var;
2379}
2380
Eli Friedmanded99792013-06-27 23:21:55 +00002381Decl *TemplateDeclInstantiator::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
2382 llvm_unreachable("@defs is not supported in Objective-C++");
2383}
2384
2385Decl *TemplateDeclInstantiator::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
2386 // FIXME: We need to be able to instantiate FriendTemplateDecls.
2387 unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID(
2388 DiagnosticsEngine::Error,
2389 "cannot instantiate %0 yet");
2390 SemaRef.Diag(D->getLocation(), DiagID)
2391 << D->getDeclKindName();
2392
2393 return 0;
2394}
2395
2396Decl *TemplateDeclInstantiator::VisitDecl(Decl *D) {
2397 llvm_unreachable("Unexpected decl");
2398}
2399
John McCallce3ff2b2009-08-25 22:02:44 +00002400Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00002401 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00002402 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor2fa98002010-02-16 19:28:15 +00002403 if (D->isInvalidDecl())
2404 return 0;
2405
Douglas Gregor8dbc2692009-03-17 21:15:40 +00002406 return Instantiator.Visit(D);
2407}
2408
John McCalle29ba202009-08-20 01:44:21 +00002409/// \brief Instantiates a nested template parameter list in the current
2410/// instantiation context.
2411///
2412/// \param L The parameter list to instantiate
2413///
2414/// \returns NULL if there was an error
2415TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00002416TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00002417 // Get errors for all the parameters before bailing out.
2418 bool Invalid = false;
2419
2420 unsigned N = L->size();
Chris Lattner5f9e2722011-07-23 10:55:15 +00002421 typedef SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00002422 ParamVector Params;
2423 Params.reserve(N);
2424 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
2425 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002426 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00002427 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00002428 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00002429 }
2430
2431 // Clean up if we had an error.
Douglas Gregorff331c12010-07-25 18:17:45 +00002432 if (Invalid)
John McCalle29ba202009-08-20 01:44:21 +00002433 return NULL;
John McCalle29ba202009-08-20 01:44:21 +00002434
2435 TemplateParameterList *InstL
2436 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
2437 L->getLAngleLoc(), &Params.front(), N,
2438 L->getRAngleLoc());
2439 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00002440}
John McCalle29ba202009-08-20 01:44:21 +00002441
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002442/// \brief Instantiate the declaration of a class template partial
Douglas Gregored9c0f92009-10-29 00:04:11 +00002443/// specialization.
2444///
2445/// \param ClassTemplate the (instantiated) class template that is partially
2446// specialized by the instantiation of \p PartialSpec.
2447///
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002448/// \param PartialSpec the (uninstantiated) class template partial
Douglas Gregored9c0f92009-10-29 00:04:11 +00002449/// specialization that we are instantiating.
2450///
Douglas Gregord65587f2010-11-10 19:44:59 +00002451/// \returns The instantiated partial specialization, if successful; otherwise,
2452/// NULL to indicate an error.
2453ClassTemplatePartialSpecializationDecl *
Douglas Gregored9c0f92009-10-29 00:04:11 +00002454TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
2455 ClassTemplateDecl *ClassTemplate,
2456 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00002457 // Create a local instantiation scope for this class template partial
2458 // specialization, which will contain the instantiations of the template
2459 // parameters.
John McCall2a7fb272010-08-25 05:32:35 +00002460 LocalInstantiationScope Scope(SemaRef);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002461
Douglas Gregored9c0f92009-10-29 00:04:11 +00002462 // Substitute into the template parameters of the class template partial
2463 // specialization.
2464 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
2465 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
2466 if (!InstParams)
Douglas Gregord65587f2010-11-10 19:44:59 +00002467 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002468
Douglas Gregored9c0f92009-10-29 00:04:11 +00002469 // Substitute into the template arguments of the class template partial
2470 // specialization.
Enea Zaffanellac1cef082013-08-10 07:24:53 +00002471 const ASTTemplateArgumentListInfo *TemplArgInfo
2472 = PartialSpec->getTemplateArgsAsWritten();
2473 TemplateArgumentListInfo InstTemplateArgs(TemplArgInfo->LAngleLoc,
2474 TemplArgInfo->RAngleLoc);
2475 if (SemaRef.Subst(TemplArgInfo->getTemplateArgs(),
2476 TemplArgInfo->NumTemplateArgs,
Douglas Gregore02e2622010-12-22 21:19:48 +00002477 InstTemplateArgs, TemplateArgs))
2478 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002479
Douglas Gregored9c0f92009-10-29 00:04:11 +00002480 // Check that the template argument list is well-formed for this
2481 // class template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002482 SmallVector<TemplateArgument, 4> Converted;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002483 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002484 PartialSpec->getLocation(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002485 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002486 false,
2487 Converted))
Douglas Gregord65587f2010-11-10 19:44:59 +00002488 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00002489
2490 // Figure out where to insert this class template partial specialization
2491 // in the member template's set of class template partial specializations.
Douglas Gregored9c0f92009-10-29 00:04:11 +00002492 void *InsertPos = 0;
2493 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00002494 = ClassTemplate->findPartialSpecialization(Converted.data(),
2495 Converted.size(), InsertPos);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002496
Douglas Gregored9c0f92009-10-29 00:04:11 +00002497 // Build the canonical type that describes the converted template
2498 // arguments of the class template partial specialization.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002499 QualType CanonType
Douglas Gregored9c0f92009-10-29 00:04:11 +00002500 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
Douglas Gregor910f8002010-11-07 23:05:16 +00002501 Converted.data(),
2502 Converted.size());
Douglas Gregored9c0f92009-10-29 00:04:11 +00002503
2504 // Build the fully-sugared type for this class template
2505 // specialization as the user wrote in the specialization
2506 // itself. This means that we'll pretty-print the type retrieved
2507 // from the specialization's declaration the way that the user
2508 // actually wrote the specialization, rather than formatting the
2509 // name based on the "canonical" representation used to store the
2510 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00002511 TypeSourceInfo *WrittenTy
2512 = SemaRef.Context.getTemplateSpecializationTypeInfo(
2513 TemplateName(ClassTemplate),
2514 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00002515 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002516 CanonType);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002517
Douglas Gregored9c0f92009-10-29 00:04:11 +00002518 if (PrevDecl) {
2519 // We've already seen a partial specialization with the same template
2520 // parameters and template arguments. This can happen, for example, when
2521 // substituting the outer template arguments ends up causing two
2522 // class template partial specializations of a member class template
2523 // to have identical forms, e.g.,
2524 //
2525 // template<typename T, typename U>
2526 // struct Outer {
2527 // template<typename X, typename Y> struct Inner;
2528 // template<typename Y> struct Inner<T, Y>;
2529 // template<typename Y> struct Inner<U, Y>;
2530 // };
2531 //
2532 // Outer<int, int> outer; // error: the partial specializations of Inner
2533 // // have the same signature.
2534 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
Douglas Gregord65587f2010-11-10 19:44:59 +00002535 << WrittenTy->getType();
Douglas Gregored9c0f92009-10-29 00:04:11 +00002536 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
2537 << SemaRef.Context.getTypeDeclType(PrevDecl);
Douglas Gregord65587f2010-11-10 19:44:59 +00002538 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00002539 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002540
2541
Douglas Gregored9c0f92009-10-29 00:04:11 +00002542 // Create the class template partial specialization declaration.
2543 ClassTemplatePartialSpecializationDecl *InstPartialSpec
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002544 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00002545 PartialSpec->getTagKind(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002546 Owner,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002547 PartialSpec->getLocStart(),
2548 PartialSpec->getLocation(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002549 InstParams,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002550 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00002551 Converted.data(),
2552 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00002553 InstTemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00002554 CanonType,
Richard Smith37fd27d2013-08-22 23:27:37 +00002555 0);
John McCallb6217662010-03-15 10:12:16 +00002556 // Substitute the nested name specifier, if any.
2557 if (SubstQualifier(PartialSpec, InstPartialSpec))
2558 return 0;
2559
Douglas Gregored9c0f92009-10-29 00:04:11 +00002560 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00002561 InstPartialSpec->setTypeAsWritten(WrittenTy);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002562
Douglas Gregored9c0f92009-10-29 00:04:11 +00002563 // Add this partial specialization to the set of class template partial
2564 // specializations.
Douglas Gregor1e1e9722012-03-28 14:34:23 +00002565 ClassTemplate->AddPartialSpecialization(InstPartialSpec, /*InsertPos=*/0);
Douglas Gregord65587f2010-11-10 19:44:59 +00002566 return InstPartialSpec;
Douglas Gregored9c0f92009-10-29 00:04:11 +00002567}
2568
Larisse Voufoef4579c2013-08-06 01:03:05 +00002569/// \brief Instantiate the declaration of a variable template partial
2570/// specialization.
2571///
2572/// \param VarTemplate the (instantiated) variable template that is partially
2573/// specialized by the instantiation of \p PartialSpec.
2574///
2575/// \param PartialSpec the (uninstantiated) variable template partial
2576/// specialization that we are instantiating.
2577///
2578/// \returns The instantiated partial specialization, if successful; otherwise,
2579/// NULL to indicate an error.
2580VarTemplatePartialSpecializationDecl *
2581TemplateDeclInstantiator::InstantiateVarTemplatePartialSpecialization(
2582 VarTemplateDecl *VarTemplate,
2583 VarTemplatePartialSpecializationDecl *PartialSpec) {
2584 // Create a local instantiation scope for this variable template partial
2585 // specialization, which will contain the instantiations of the template
2586 // parameters.
2587 LocalInstantiationScope Scope(SemaRef);
2588
2589 // Substitute into the template parameters of the variable template partial
2590 // specialization.
2591 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
2592 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
2593 if (!InstParams)
2594 return 0;
2595
2596 // Substitute into the template arguments of the variable template partial
2597 // specialization.
Enea Zaffanellac1cef082013-08-10 07:24:53 +00002598 const ASTTemplateArgumentListInfo *TemplArgInfo
2599 = PartialSpec->getTemplateArgsAsWritten();
2600 TemplateArgumentListInfo InstTemplateArgs(TemplArgInfo->LAngleLoc,
2601 TemplArgInfo->RAngleLoc);
2602 if (SemaRef.Subst(TemplArgInfo->getTemplateArgs(),
2603 TemplArgInfo->NumTemplateArgs,
Larisse Voufoef4579c2013-08-06 01:03:05 +00002604 InstTemplateArgs, TemplateArgs))
2605 return 0;
2606
2607 // Check that the template argument list is well-formed for this
2608 // class template.
2609 SmallVector<TemplateArgument, 4> Converted;
2610 if (SemaRef.CheckTemplateArgumentList(VarTemplate, PartialSpec->getLocation(),
2611 InstTemplateArgs, false, Converted))
2612 return 0;
2613
2614 // Figure out where to insert this variable template partial specialization
2615 // in the member template's set of variable template partial specializations.
2616 void *InsertPos = 0;
2617 VarTemplateSpecializationDecl *PrevDecl =
2618 VarTemplate->findPartialSpecialization(Converted.data(), Converted.size(),
2619 InsertPos);
2620
2621 // Build the canonical type that describes the converted template
2622 // arguments of the variable template partial specialization.
2623 QualType CanonType = SemaRef.Context.getTemplateSpecializationType(
2624 TemplateName(VarTemplate), Converted.data(), Converted.size());
2625
2626 // Build the fully-sugared type for this variable template
2627 // specialization as the user wrote in the specialization
2628 // itself. This means that we'll pretty-print the type retrieved
2629 // from the specialization's declaration the way that the user
2630 // actually wrote the specialization, rather than formatting the
2631 // name based on the "canonical" representation used to store the
2632 // template arguments in the specialization.
2633 TypeSourceInfo *WrittenTy = SemaRef.Context.getTemplateSpecializationTypeInfo(
2634 TemplateName(VarTemplate), PartialSpec->getLocation(), InstTemplateArgs,
2635 CanonType);
2636
2637 if (PrevDecl) {
2638 // We've already seen a partial specialization with the same template
2639 // parameters and template arguments. This can happen, for example, when
2640 // substituting the outer template arguments ends up causing two
2641 // variable template partial specializations of a member variable template
2642 // to have identical forms, e.g.,
2643 //
2644 // template<typename T, typename U>
2645 // struct Outer {
2646 // template<typename X, typename Y> pair<X,Y> p;
2647 // template<typename Y> pair<T, Y> p;
2648 // template<typename Y> pair<U, Y> p;
2649 // };
2650 //
2651 // Outer<int, int> outer; // error: the partial specializations of Inner
2652 // // have the same signature.
2653 SemaRef.Diag(PartialSpec->getLocation(),
2654 diag::err_var_partial_spec_redeclared)
2655 << WrittenTy->getType();
2656 SemaRef.Diag(PrevDecl->getLocation(),
2657 diag::note_var_prev_partial_spec_here);
2658 return 0;
2659 }
2660
2661 // Do substitution on the type of the declaration
2662 TypeSourceInfo *DI = SemaRef.SubstType(
2663 PartialSpec->getTypeSourceInfo(), TemplateArgs,
2664 PartialSpec->getTypeSpecStartLoc(), PartialSpec->getDeclName());
2665 if (!DI)
2666 return 0;
2667
2668 if (DI->getType()->isFunctionType()) {
2669 SemaRef.Diag(PartialSpec->getLocation(),
2670 diag::err_variable_instantiates_to_function)
2671 << PartialSpec->isStaticDataMember() << DI->getType();
2672 return 0;
2673 }
2674
2675 // Create the variable template partial specialization declaration.
2676 VarTemplatePartialSpecializationDecl *InstPartialSpec =
2677 VarTemplatePartialSpecializationDecl::Create(
2678 SemaRef.Context, Owner, PartialSpec->getInnerLocStart(),
2679 PartialSpec->getLocation(), InstParams, VarTemplate, DI->getType(),
2680 DI, PartialSpec->getStorageClass(), Converted.data(),
Richard Smith37fd27d2013-08-22 23:27:37 +00002681 Converted.size(), InstTemplateArgs);
Larisse Voufoef4579c2013-08-06 01:03:05 +00002682
2683 // Substitute the nested name specifier, if any.
2684 if (SubstQualifier(PartialSpec, InstPartialSpec))
2685 return 0;
2686
2687 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
2688 InstPartialSpec->setTypeAsWritten(WrittenTy);
2689
Larisse Voufoef4579c2013-08-06 01:03:05 +00002690 // Add this partial specialization to the set of variable template partial
2691 // specializations. The instantiation of the initializer is not necessary.
2692 VarTemplate->AddPartialSpecialization(InstPartialSpec, /*InsertPos=*/0);
Larisse Voufo04592e72013-08-22 00:28:27 +00002693
2694 // Set the initializer, to use as pattern for initialization.
2695 if (VarDecl *Def = PartialSpec->getDefinition(SemaRef.getASTContext()))
2696 PartialSpec = cast<VarTemplatePartialSpecializationDecl>(Def);
2697 SemaRef.BuildVariableInstantiation(InstPartialSpec, PartialSpec, TemplateArgs,
Richard Smitha41c97a2013-09-20 01:15:31 +00002698 LateAttrs, Owner, StartingScope);
Larisse Voufo04592e72013-08-22 00:28:27 +00002699 InstPartialSpec->setInit(PartialSpec->getInit());
2700
Larisse Voufoef4579c2013-08-06 01:03:05 +00002701 return InstPartialSpec;
2702}
2703
John McCall21ef0fa2010-03-11 09:03:00 +00002704TypeSourceInfo*
John McCallce3ff2b2009-08-25 22:02:44 +00002705TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002706 SmallVectorImpl<ParmVarDecl *> &Params) {
John McCall21ef0fa2010-03-11 09:03:00 +00002707 TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
2708 assert(OldTInfo && "substituting function without type source info");
2709 assert(Params.empty() && "parameter vector is non-empty at start");
Douglas Gregorcefc3af2012-04-16 07:05:22 +00002710
2711 CXXRecordDecl *ThisContext = 0;
2712 unsigned ThisTypeQuals = 0;
2713 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Richard Smithcafeb942013-06-07 02:33:37 +00002714 ThisContext = cast<CXXRecordDecl>(Owner);
Douglas Gregorcefc3af2012-04-16 07:05:22 +00002715 ThisTypeQuals = Method->getTypeQualifiers();
2716 }
2717
John McCall6cd3b9f2010-04-09 17:38:44 +00002718 TypeSourceInfo *NewTInfo
2719 = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
2720 D->getTypeSpecStartLoc(),
Douglas Gregorcefc3af2012-04-16 07:05:22 +00002721 D->getDeclName(),
2722 ThisContext, ThisTypeQuals);
John McCall21ef0fa2010-03-11 09:03:00 +00002723 if (!NewTInfo)
2724 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +00002725
Reid Klecknerc66e7e92013-07-31 21:00:18 +00002726 TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
2727 if (FunctionProtoTypeLoc OldProtoLoc = OldTL.getAs<FunctionProtoTypeLoc>()) {
2728 if (NewTInfo != OldTInfo) {
2729 // Get parameters from the new type info.
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00002730 TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens();
David Blaikie39e6ab42013-02-18 22:06:02 +00002731 FunctionProtoTypeLoc NewProtoLoc = NewTL.castAs<FunctionProtoTypeLoc>();
Richard Smith500d7292012-07-18 01:29:05 +00002732 unsigned NewIdx = 0;
David Blaikie39e6ab42013-02-18 22:06:02 +00002733 for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc.getNumArgs();
Douglas Gregor12c9c002011-01-07 16:43:16 +00002734 OldIdx != NumOldParams; ++OldIdx) {
David Blaikie39e6ab42013-02-18 22:06:02 +00002735 ParmVarDecl *OldParam = OldProtoLoc.getArg(OldIdx);
Richard Smith500d7292012-07-18 01:29:05 +00002736 LocalInstantiationScope *Scope = SemaRef.CurrentInstantiationScope;
2737
David Blaikiedc84cd52013-02-20 22:23:23 +00002738 Optional<unsigned> NumArgumentsInExpansion;
Richard Smith500d7292012-07-18 01:29:05 +00002739 if (OldParam->isParameterPack())
2740 NumArgumentsInExpansion =
2741 SemaRef.getNumArgumentsInExpansion(OldParam->getType(),
2742 TemplateArgs);
2743 if (!NumArgumentsInExpansion) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002744 // Simple case: normal parameter, or a parameter pack that's
Douglas Gregor12c9c002011-01-07 16:43:16 +00002745 // instantiated to a (still-dependent) parameter pack.
David Blaikie39e6ab42013-02-18 22:06:02 +00002746 ParmVarDecl *NewParam = NewProtoLoc.getArg(NewIdx++);
Douglas Gregor12c9c002011-01-07 16:43:16 +00002747 Params.push_back(NewParam);
Richard Smith500d7292012-07-18 01:29:05 +00002748 Scope->InstantiatedLocal(OldParam, NewParam);
2749 } else {
2750 // Parameter pack expansion: make the instantiation an argument pack.
2751 Scope->MakeInstantiatedLocalArgPack(OldParam);
2752 for (unsigned I = 0; I != *NumArgumentsInExpansion; ++I) {
David Blaikie39e6ab42013-02-18 22:06:02 +00002753 ParmVarDecl *NewParam = NewProtoLoc.getArg(NewIdx++);
Richard Smith500d7292012-07-18 01:29:05 +00002754 Params.push_back(NewParam);
2755 Scope->InstantiatedLocalPackArg(OldParam, NewParam);
2756 }
Douglas Gregor12c9c002011-01-07 16:43:16 +00002757 }
Douglas Gregor6920cdc2010-05-03 15:32:18 +00002758 }
Reid Klecknerc66e7e92013-07-31 21:00:18 +00002759 } else {
2760 // The function type itself was not dependent and therefore no
2761 // substitution occurred. However, we still need to instantiate
2762 // the function parameters themselves.
2763 const FunctionProtoType *OldProto =
2764 cast<FunctionProtoType>(OldProtoLoc.getType());
David Blaikie39e6ab42013-02-18 22:06:02 +00002765 for (unsigned i = 0, i_end = OldProtoLoc.getNumArgs(); i != i_end; ++i) {
Reid Klecknerc66e7e92013-07-31 21:00:18 +00002766 ParmVarDecl *OldParam = OldProtoLoc.getArg(i);
2767 if (!OldParam) {
2768 Params.push_back(SemaRef.BuildParmVarDeclForTypedef(
2769 D, D->getLocation(), OldProto->getArgType(i)));
2770 continue;
2771 }
2772
Eli Friedmanded99792013-06-27 23:21:55 +00002773 ParmVarDecl *Parm =
Reid Klecknerc66e7e92013-07-31 21:00:18 +00002774 cast_or_null<ParmVarDecl>(VisitParmVarDecl(OldParam));
Douglas Gregor6920cdc2010-05-03 15:32:18 +00002775 if (!Parm)
2776 return 0;
2777 Params.push_back(Parm);
2778 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00002779 }
Reid Klecknerc66e7e92013-07-31 21:00:18 +00002780 } else {
2781 // If the type of this function, after ignoring parentheses, is not
2782 // *directly* a function type, then we're instantiating a function that
2783 // was declared via a typedef or with attributes, e.g.,
2784 //
2785 // typedef int functype(int, int);
2786 // functype func;
2787 // int __cdecl meth(int, int);
2788 //
2789 // In this case, we'll just go instantiate the ParmVarDecls that we
2790 // synthesized in the method declaration.
2791 SmallVector<QualType, 4> ParamTypes;
2792 if (SemaRef.SubstParmTypes(D->getLocation(), D->param_begin(),
2793 D->getNumParams(), TemplateArgs, ParamTypes,
2794 &Params))
2795 return 0;
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00002796 }
Reid Klecknerc66e7e92013-07-31 21:00:18 +00002797
John McCall21ef0fa2010-03-11 09:03:00 +00002798 return NewTInfo;
Douglas Gregor5545e162009-03-24 00:38:23 +00002799}
2800
Richard Smithe6975e92012-04-17 00:58:00 +00002801/// Introduce the instantiated function parameters into the local
2802/// instantiation scope, and set the parameter names to those used
2803/// in the template.
2804static void addInstantiatedParametersToScope(Sema &S, FunctionDecl *Function,
2805 const FunctionDecl *PatternDecl,
2806 LocalInstantiationScope &Scope,
2807 const MultiLevelTemplateArgumentList &TemplateArgs) {
2808 unsigned FParamIdx = 0;
2809 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
2810 const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
2811 if (!PatternParam->isParameterPack()) {
2812 // Simple case: not a parameter pack.
2813 assert(FParamIdx < Function->getNumParams());
2814 ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
2815 FunctionParam->setDeclName(PatternParam->getDeclName());
2816 Scope.InstantiatedLocal(PatternParam, FunctionParam);
2817 ++FParamIdx;
2818 continue;
2819 }
2820
2821 // Expand the parameter pack.
2822 Scope.MakeInstantiatedLocalArgPack(PatternParam);
David Blaikiedc84cd52013-02-20 22:23:23 +00002823 Optional<unsigned> NumArgumentsInExpansion
Richard Smithe6975e92012-04-17 00:58:00 +00002824 = S.getNumArgumentsInExpansion(PatternParam->getType(), TemplateArgs);
Richard Smith500d7292012-07-18 01:29:05 +00002825 assert(NumArgumentsInExpansion &&
2826 "should only be called when all template arguments are known");
2827 for (unsigned Arg = 0; Arg < *NumArgumentsInExpansion; ++Arg) {
Richard Smithe6975e92012-04-17 00:58:00 +00002828 ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
2829 FunctionParam->setDeclName(PatternParam->getDeclName());
2830 Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam);
2831 ++FParamIdx;
2832 }
2833 }
2834}
2835
2836static void InstantiateExceptionSpec(Sema &SemaRef, FunctionDecl *New,
2837 const FunctionProtoType *Proto,
2838 const MultiLevelTemplateArgumentList &TemplateArgs) {
Richard Smith13bffc52012-04-19 00:08:28 +00002839 assert(Proto->getExceptionSpecType() != EST_Uninstantiated);
2840
Richard Smithe6975e92012-04-17 00:58:00 +00002841 // C++11 [expr.prim.general]p3:
2842 // If a declaration declares a member function or member function
2843 // template of a class X, the expression this is a prvalue of type
2844 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
2845 // and the end of the function-definition, member-declarator, or
2846 // declarator.
2847 CXXRecordDecl *ThisContext = 0;
2848 unsigned ThisTypeQuals = 0;
2849 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(New)) {
2850 ThisContext = Method->getParent();
2851 ThisTypeQuals = Method->getTypeQualifiers();
2852 }
2853 Sema::CXXThisScopeRAII ThisScope(SemaRef, ThisContext, ThisTypeQuals,
Richard Smith80ad52f2013-01-02 11:42:31 +00002854 SemaRef.getLangOpts().CPlusPlus11);
Richard Smithe6975e92012-04-17 00:58:00 +00002855
2856 // The function has an exception specification or a "noreturn"
2857 // attribute. Substitute into each of the exception types.
2858 SmallVector<QualType, 4> Exceptions;
2859 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
2860 // FIXME: Poor location information!
2861 if (const PackExpansionType *PackExpansion
2862 = Proto->getExceptionType(I)->getAs<PackExpansionType>()) {
2863 // We have a pack expansion. Instantiate it.
2864 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2865 SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(),
2866 Unexpanded);
2867 assert(!Unexpanded.empty() &&
2868 "Pack expansion without parameter packs?");
2869
2870 bool Expand = false;
2871 bool RetainExpansion = false;
Richard Smithcafeb942013-06-07 02:33:37 +00002872 Optional<unsigned> NumExpansions = PackExpansion->getNumExpansions();
Richard Smithe6975e92012-04-17 00:58:00 +00002873 if (SemaRef.CheckParameterPacksForExpansion(New->getLocation(),
2874 SourceRange(),
2875 Unexpanded,
2876 TemplateArgs,
2877 Expand,
2878 RetainExpansion,
2879 NumExpansions))
2880 break;
2881
2882 if (!Expand) {
2883 // We can't expand this pack expansion into separate arguments yet;
2884 // just substitute into the pattern and create a new pack expansion
2885 // type.
2886 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
2887 QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2888 TemplateArgs,
2889 New->getLocation(), New->getDeclName());
2890 if (T.isNull())
2891 break;
2892
2893 T = SemaRef.Context.getPackExpansionType(T, NumExpansions);
2894 Exceptions.push_back(T);
2895 continue;
2896 }
2897
2898 // Substitute into the pack expansion pattern for each template
2899 bool Invalid = false;
2900 for (unsigned ArgIdx = 0; ArgIdx != *NumExpansions; ++ArgIdx) {
2901 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, ArgIdx);
2902
2903 QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2904 TemplateArgs,
2905 New->getLocation(), New->getDeclName());
2906 if (T.isNull()) {
2907 Invalid = true;
2908 break;
2909 }
2910
2911 Exceptions.push_back(T);
2912 }
2913
2914 if (Invalid)
2915 break;
2916
2917 continue;
2918 }
2919
2920 QualType T
2921 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
2922 New->getLocation(), New->getDeclName());
2923 if (T.isNull() ||
2924 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
2925 continue;
2926
2927 Exceptions.push_back(T);
2928 }
2929 Expr *NoexceptExpr = 0;
2930 if (Expr *OldNoexceptExpr = Proto->getNoexceptExpr()) {
2931 EnterExpressionEvaluationContext Unevaluated(SemaRef,
2932 Sema::ConstantEvaluated);
2933 ExprResult E = SemaRef.SubstExpr(OldNoexceptExpr, TemplateArgs);
2934 if (E.isUsable())
2935 E = SemaRef.CheckBooleanCondition(E.get(), E.get()->getLocStart());
2936
2937 if (E.isUsable()) {
2938 NoexceptExpr = E.take();
2939 if (!NoexceptExpr->isTypeDependent() &&
2940 !NoexceptExpr->isValueDependent())
Douglas Gregorab41fe92012-05-04 22:38:52 +00002941 NoexceptExpr
2942 = SemaRef.VerifyIntegerConstantExpression(NoexceptExpr,
2943 0, diag::err_noexcept_needs_constant_expression,
2944 /*AllowFold*/ false).take();
Richard Smithe6975e92012-04-17 00:58:00 +00002945 }
2946 }
2947
2948 // Rebuild the function type
2949 const FunctionProtoType *NewProto
2950 = New->getType()->getAs<FunctionProtoType>();
2951 assert(NewProto && "Template instantiation without function prototype?");
2952
2953 FunctionProtoType::ExtProtoInfo EPI = NewProto->getExtProtoInfo();
2954 EPI.ExceptionSpecType = Proto->getExceptionSpecType();
2955 EPI.NumExceptions = Exceptions.size();
2956 EPI.Exceptions = Exceptions.data();
2957 EPI.NoexceptExpr = NoexceptExpr;
2958
2959 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
Reid Kleckner0567a792013-06-10 20:51:09 +00002960 NewProto->getArgTypes(), EPI));
Richard Smithe6975e92012-04-17 00:58:00 +00002961}
2962
2963void Sema::InstantiateExceptionSpec(SourceLocation PointOfInstantiation,
2964 FunctionDecl *Decl) {
Richard Smith13bffc52012-04-19 00:08:28 +00002965 const FunctionProtoType *Proto = Decl->getType()->castAs<FunctionProtoType>();
2966 if (Proto->getExceptionSpecType() != EST_Uninstantiated)
Richard Smithe6975e92012-04-17 00:58:00 +00002967 return;
2968
2969 InstantiatingTemplate Inst(*this, PointOfInstantiation, Decl,
2970 InstantiatingTemplate::ExceptionSpecification());
Richard Smithb9d0b762012-07-27 04:22:15 +00002971 if (Inst) {
2972 // We hit the instantiation depth limit. Clear the exception specification
2973 // so that our callers don't have to cope with EST_Uninstantiated.
2974 FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
2975 EPI.ExceptionSpecType = EST_None;
2976 Decl->setType(Context.getFunctionType(Proto->getResultType(),
Reid Kleckner0567a792013-06-10 20:51:09 +00002977 Proto->getArgTypes(), EPI));
Richard Smithe6975e92012-04-17 00:58:00 +00002978 return;
Richard Smithb9d0b762012-07-27 04:22:15 +00002979 }
Richard Smithe6975e92012-04-17 00:58:00 +00002980
2981 // Enter the scope of this instantiation. We don't use
2982 // PushDeclContext because we don't have a scope.
2983 Sema::ContextRAII savedContext(*this, Decl);
2984 LocalInstantiationScope Scope(*this);
2985
2986 MultiLevelTemplateArgumentList TemplateArgs =
2987 getTemplateInstantiationArgs(Decl, 0, /*RelativeToPrimary*/true);
2988
Richard Smith13bffc52012-04-19 00:08:28 +00002989 FunctionDecl *Template = Proto->getExceptionSpecTemplate();
2990 addInstantiatedParametersToScope(*this, Decl, Template, Scope, TemplateArgs);
Richard Smithe6975e92012-04-17 00:58:00 +00002991
Richard Smith13bffc52012-04-19 00:08:28 +00002992 ::InstantiateExceptionSpec(*this, Decl,
2993 Template->getType()->castAs<FunctionProtoType>(),
2994 TemplateArgs);
Richard Smithe6975e92012-04-17 00:58:00 +00002995}
2996
Mike Stump1eb44332009-09-09 15:08:12 +00002997/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00002998/// declaration (New) from the corresponding fields of its template (Tmpl).
2999///
3000/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00003001bool
3002TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00003003 FunctionDecl *Tmpl) {
David Blaikie85f485a2012-07-16 18:50:45 +00003004 if (Tmpl->isDeleted())
Sean Hunt10620eb2011-05-06 20:44:56 +00003005 New->setDeletedAsWritten();
Mike Stump1eb44332009-09-09 15:08:12 +00003006
Douglas Gregorcca9e962009-07-01 22:01:06 +00003007 // If we are performing substituting explicitly-specified template arguments
3008 // or deduced template arguments into a function template and we reach this
3009 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00003010 // to keeping the new function template specialization. We therefore
3011 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00003012 // into a template instantiation for this specific function template
3013 // specialization, which is not a SFINAE context, so that we diagnose any
3014 // further errors in the declaration itself.
3015 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
3016 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
3017 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
3018 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00003019 if (FunctionTemplateDecl *FunTmpl
Nick Lewycky4a9e60f2012-11-16 08:40:59 +00003020 = dyn_cast<FunctionTemplateDecl>(ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00003021 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00003022 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00003023 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00003024 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
Nick Lewycky4a9e60f2012-11-16 08:40:59 +00003025 ActiveInst.Entity = New;
Douglas Gregorcca9e962009-07-01 22:01:06 +00003026 }
3027 }
Mike Stump1eb44332009-09-09 15:08:12 +00003028
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00003029 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
3030 assert(Proto && "Function template without prototype?");
3031
Sebastian Redl60618fa2011-03-12 11:50:43 +00003032 if (Proto->hasExceptionSpec() || Proto->getNoReturnAttr()) {
John McCalle23cf432010-12-14 08:05:40 +00003033 FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
John McCalle23cf432010-12-14 08:05:40 +00003034
Richard Smithe6975e92012-04-17 00:58:00 +00003035 // DR1330: In C++11, defer instantiation of a non-trivial
3036 // exception specification.
Richard Smith80ad52f2013-01-02 11:42:31 +00003037 if (SemaRef.getLangOpts().CPlusPlus11 &&
Richard Smithe6975e92012-04-17 00:58:00 +00003038 EPI.ExceptionSpecType != EST_None &&
3039 EPI.ExceptionSpecType != EST_DynamicNone &&
3040 EPI.ExceptionSpecType != EST_BasicNoexcept) {
Richard Smith13bffc52012-04-19 00:08:28 +00003041 FunctionDecl *ExceptionSpecTemplate = Tmpl;
3042 if (EPI.ExceptionSpecType == EST_Uninstantiated)
3043 ExceptionSpecTemplate = EPI.ExceptionSpecTemplate;
Richard Smith4841ca52013-04-10 05:48:59 +00003044 ExceptionSpecificationType NewEST = EST_Uninstantiated;
3045 if (EPI.ExceptionSpecType == EST_Unevaluated)
3046 NewEST = EST_Unevaluated;
Richard Smith13bffc52012-04-19 00:08:28 +00003047
Richard Smithe6975e92012-04-17 00:58:00 +00003048 // Mark the function has having an uninstantiated exception specification.
3049 const FunctionProtoType *NewProto
3050 = New->getType()->getAs<FunctionProtoType>();
3051 assert(NewProto && "Template instantiation without function prototype?");
3052 EPI = NewProto->getExtProtoInfo();
Richard Smith4841ca52013-04-10 05:48:59 +00003053 EPI.ExceptionSpecType = NewEST;
Richard Smithe6975e92012-04-17 00:58:00 +00003054 EPI.ExceptionSpecDecl = New;
Richard Smith13bffc52012-04-19 00:08:28 +00003055 EPI.ExceptionSpecTemplate = ExceptionSpecTemplate;
Reid Kleckner0567a792013-06-10 20:51:09 +00003056 New->setType(SemaRef.Context.getFunctionType(
3057 NewProto->getResultType(), NewProto->getArgTypes(), EPI));
Richard Smithe6975e92012-04-17 00:58:00 +00003058 } else {
3059 ::InstantiateExceptionSpec(SemaRef, New, Proto, TemplateArgs);
3060 }
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00003061 }
3062
Rafael Espindola19f74ac2011-07-06 15:46:09 +00003063 // Get the definition. Leaves the variable unchanged if undefined.
Richard Smithe6975e92012-04-17 00:58:00 +00003064 const FunctionDecl *Definition = Tmpl;
Rafael Espindola19f74ac2011-07-06 15:46:09 +00003065 Tmpl->isDefined(Definition);
3066
DeLesley Hutchins23323e02012-01-20 22:50:54 +00003067 SemaRef.InstantiateAttrs(TemplateArgs, Definition, New,
3068 LateAttrs, StartingScope);
Douglas Gregor7cf84d62010-06-15 17:05:35 +00003069
Douglas Gregore53060f2009-06-25 22:08:12 +00003070 return false;
3071}
3072
Douglas Gregor5545e162009-03-24 00:38:23 +00003073/// \brief Initializes common fields of an instantiated method
3074/// declaration (New) from the corresponding fields of its template
3075/// (Tmpl).
3076///
3077/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00003078bool
3079TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00003080 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00003081 if (InitFunctionInstantiation(New, Tmpl))
3082 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003083
Douglas Gregor5545e162009-03-24 00:38:23 +00003084 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00003085 if (Tmpl->isVirtualAsWritten())
Douglas Gregor85606eb2010-09-28 20:50:54 +00003086 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +00003087
Douglas Gregor5545e162009-03-24 00:38:23 +00003088 // FIXME: New needs a pointer to Tmpl
3089 return false;
3090}
Douglas Gregora58861f2009-05-13 20:28:22 +00003091
3092/// \brief Instantiate the definition of the given function from its
3093/// template.
3094///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00003095/// \param PointOfInstantiation the point at which the instantiation was
3096/// required. Note that this is not precisely a "point of instantiation"
3097/// for the function, but it's close.
3098///
Douglas Gregora58861f2009-05-13 20:28:22 +00003099/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00003100/// function template specialization or member function of a class template
3101/// specialization.
3102///
3103/// \param Recursive if true, recursively instantiates any functions that
3104/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00003105///
3106/// \param DefinitionRequired if true, then we are performing an explicit
3107/// instantiation where the body of the function is required. Complain if
3108/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00003109void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00003110 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00003111 bool Recursive,
3112 bool DefinitionRequired) {
Sean Hunt10620eb2011-05-06 20:44:56 +00003113 if (Function->isInvalidDecl() || Function->isDefined())
Douglas Gregor54dabfc2009-05-14 23:26:13 +00003114 return;
3115
Francois Pichetaf0f4d02011-08-14 03:52:19 +00003116 // Never instantiate an explicit specialization except if it is a class scope
3117 // explicit specialization.
3118 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization &&
3119 !Function->getClassScopeSpecializationPattern())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00003120 return;
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00003121
Douglas Gregor1eee0e72009-05-14 21:06:31 +00003122 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00003123 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Sean Huntf996e052011-05-27 20:00:14 +00003124 assert(PatternDecl && "instantiating a non-template");
3125
3126 Stmt *Pattern = PatternDecl->getBody(PatternDecl);
3127 assert(PatternDecl && "template definition is not a template");
3128 if (!Pattern) {
3129 // Try to find a defaulted definition
3130 PatternDecl->isDefined(PatternDecl);
Sean Huntdfab8542011-05-25 22:02:25 +00003131 }
Sean Huntf996e052011-05-27 20:00:14 +00003132 assert(PatternDecl && "template definition is not a template");
Douglas Gregor1eee0e72009-05-14 21:06:31 +00003133
Francois Pichet8387e2a2011-04-22 22:18:13 +00003134 // Postpone late parsed template instantiations.
Sean Huntf996e052011-05-27 20:00:14 +00003135 if (PatternDecl->isLateTemplateParsed() &&
Nick Lewycky8a29bc02011-05-12 03:51:24 +00003136 !LateTemplateParser) {
Francois Pichet8387e2a2011-04-22 22:18:13 +00003137 PendingInstantiations.push_back(
3138 std::make_pair(Function, PointOfInstantiation));
3139 return;
3140 }
3141
David Majnemer360d23e2013-08-16 08:29:13 +00003142 // Call the LateTemplateParser callback if there is a need to late parse
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003143 // a templated function definition.
Sean Huntf996e052011-05-27 20:00:14 +00003144 if (!Pattern && PatternDecl->isLateTemplateParsed() &&
Francois Pichet8387e2a2011-04-22 22:18:13 +00003145 LateTemplateParser) {
Richard Smithac32d902013-08-07 21:41:30 +00003146 // FIXME: Optimize to allow individual templates to be deserialized.
3147 if (PatternDecl->isFromASTFile())
3148 ExternalSource->ReadLateParsedTemplates(LateParsedTemplateMap);
3149
3150 LateParsedTemplate *LPT = LateParsedTemplateMap.lookup(PatternDecl);
3151 assert(LPT && "missing LateParsedTemplate");
3152 LateTemplateParser(OpaqueParser, *LPT);
Francois Pichet8387e2a2011-04-22 22:18:13 +00003153 Pattern = PatternDecl->getBody(PatternDecl);
3154 }
3155
Sean Huntf996e052011-05-27 20:00:14 +00003156 if (!Pattern && !PatternDecl->isDefaulted()) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00003157 if (DefinitionRequired) {
3158 if (Function->getPrimaryTemplate())
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003159 Diag(PointOfInstantiation,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00003160 diag::err_explicit_instantiation_undefined_func_template)
3161 << Function->getPrimaryTemplate();
3162 else
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003163 Diag(PointOfInstantiation,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00003164 diag::err_explicit_instantiation_undefined_member)
3165 << 1 << Function->getDeclName() << Function->getDeclContext();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003166
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00003167 if (PatternDecl)
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003168 Diag(PatternDecl->getLocation(),
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00003169 diag::note_explicit_instantiation_here);
Douglas Gregorcfe833b2010-05-17 17:57:54 +00003170 Function->setInvalidDecl();
Chandler Carruth58e390e2010-08-25 08:27:02 +00003171 } else if (Function->getTemplateSpecializationKind()
3172 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00003173 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00003174 std::make_pair(Function, PointOfInstantiation));
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00003175 }
Chandler Carruth58e390e2010-08-25 08:27:02 +00003176
Douglas Gregor1eee0e72009-05-14 21:06:31 +00003177 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00003178 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00003179
Richard Smith60e141e2013-05-04 07:00:32 +00003180 // C++1y [temp.explicit]p10:
3181 // Except for inline functions, declarations with types deduced from their
3182 // initializer or return value, and class template specializations, other
3183 // explicit instantiation declarations have the effect of suppressing the
3184 // implicit instantiation of the entity to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00003185 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00003186 == TSK_ExplicitInstantiationDeclaration &&
Richard Smith60e141e2013-05-04 07:00:32 +00003187 !PatternDecl->isInlined() &&
Richard Smith37e849a2013-08-14 20:16:31 +00003188 !PatternDecl->getResultType()->getContainedAutoType())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00003189 return;
Mike Stump1eb44332009-09-09 15:08:12 +00003190
Richard Smithd4497dd2013-01-25 00:08:28 +00003191 if (PatternDecl->isInlined())
3192 Function->setImplicitlyInline();
3193
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00003194 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
3195 if (Inst)
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003196 return;
3197
Abramo Bagnarae9946242011-11-18 08:08:52 +00003198 // Copy the inner loc start from the pattern.
3199 Function->setInnerLocStart(PatternDecl->getInnerLocStart());
3200
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00003201 // If we're performing recursive template instantiation, create our own
3202 // queue of pending implicit instantiations that we will instantiate later,
3203 // while we're still within our own instantiation context.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003204 SmallVector<VTableUse, 16> SavedVTableUses;
Chandler Carruth62c78d52010-08-25 08:44:16 +00003205 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Faisal Vali86648b12013-06-26 02:34:24 +00003206 std::deque<PendingImplicitInstantiation>
3207 SavedPendingLocalImplicitInstantiations;
3208 SavedPendingLocalImplicitInstantiations.swap(
3209 PendingLocalImplicitInstantiations);
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00003210 if (Recursive) {
3211 VTableUses.swap(SavedVTableUses);
Chandler Carruth62c78d52010-08-25 08:44:16 +00003212 PendingInstantiations.swap(SavedPendingInstantiations);
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00003213 }
Mike Stump1eb44332009-09-09 15:08:12 +00003214
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003215 EnterExpressionEvaluationContext EvalContext(*this,
John McCallf312b1e2010-08-26 23:41:50 +00003216 Sema::PotentiallyEvaluated);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00003217
Douglas Gregor54dabfc2009-05-14 23:26:13 +00003218 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00003219 // recorded, unless we're actually a member function within a local
3220 // class, in which case we need to merge our results with the parent
3221 // scope (of the enclosing function).
3222 bool MergeWithParentScope = false;
3223 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
3224 MergeWithParentScope = Rec->isLocalClass();
3225
3226 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00003227
Richard Smith1d28caf2012-12-11 01:14:52 +00003228 if (PatternDecl->isDefaulted())
Sean Huntcd10dec2011-05-23 23:14:04 +00003229 SetDeclDefaulted(Function, PatternDecl->getLocation());
Richard Smith1d28caf2012-12-11 01:14:52 +00003230 else {
3231 ActOnStartOfFunctionDef(0, Function);
3232
3233 // Enter the scope of this instantiation. We don't use
3234 // PushDeclContext because we don't have a scope.
3235 Sema::ContextRAII savedContext(*this, Function);
3236
3237 MultiLevelTemplateArgumentList TemplateArgs =
3238 getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
3239
3240 addInstantiatedParametersToScope(*this, Function, PatternDecl, Scope,
3241 TemplateArgs);
3242
Sean Huntcd10dec2011-05-23 23:14:04 +00003243 // If this is a constructor, instantiate the member initializers.
3244 if (const CXXConstructorDecl *Ctor =
3245 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
3246 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
3247 TemplateArgs);
3248 }
3249
3250 // Instantiate the function body.
3251 StmtResult Body = SubstStmt(Pattern, TemplateArgs);
3252
3253 if (Body.isInvalid())
3254 Function->setInvalidDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003255
Sean Huntcd10dec2011-05-23 23:14:04 +00003256 ActOnFinishFunctionBody(Function, Body.get(),
3257 /*IsInstantiation=*/true);
Richard Smith1d28caf2012-12-11 01:14:52 +00003258
3259 PerformDependentDiagnostics(PatternDecl, TemplateArgs);
3260
3261 savedContext.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00003262 }
3263
Douglas Gregoraba43bb2009-05-26 20:50:29 +00003264 DeclGroupRef DG(Function);
3265 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00003266
Douglas Gregor60406be2010-01-16 22:29:39 +00003267 // This class may have local implicit instantiations that need to be
3268 // instantiation within this scope.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003269 PerformPendingInstantiations(/*LocalOnly=*/true);
Douglas Gregor60406be2010-01-16 22:29:39 +00003270 Scope.Exit();
3271
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00003272 if (Recursive) {
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00003273 // Define any pending vtables.
3274 DefineUsedVTables();
3275
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00003276 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00003277 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003278 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00003279
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00003280 // Restore the set of pending vtables.
Nick Lewycky81559102011-05-31 07:58:42 +00003281 assert(VTableUses.empty() &&
3282 "VTableUses should be empty before it is discarded.");
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00003283 VTableUses.swap(SavedVTableUses);
3284
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00003285 // Restore the set of pending implicit instantiations.
Nick Lewycky81559102011-05-31 07:58:42 +00003286 assert(PendingInstantiations.empty() &&
3287 "PendingInstantiations should be empty before it is discarded.");
Chandler Carruth62c78d52010-08-25 08:44:16 +00003288 PendingInstantiations.swap(SavedPendingInstantiations);
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00003289 }
Faisal Vali86648b12013-06-26 02:34:24 +00003290 SavedPendingLocalImplicitInstantiations.swap(
3291 PendingLocalImplicitInstantiations);
Douglas Gregora58861f2009-05-13 20:28:22 +00003292}
3293
Larisse Voufoef4579c2013-08-06 01:03:05 +00003294VarTemplateSpecializationDecl *Sema::BuildVarTemplateInstantiation(
3295 VarTemplateDecl *VarTemplate, VarDecl *FromVar,
3296 const TemplateArgumentList &TemplateArgList,
3297 const TemplateArgumentListInfo &TemplateArgsInfo,
3298 SmallVectorImpl<TemplateArgument> &Converted,
3299 SourceLocation PointOfInstantiation, void *InsertPos,
3300 LateInstantiatedAttrVec *LateAttrs,
3301 LocalInstantiationScope *StartingScope) {
3302 if (FromVar->isInvalidDecl())
3303 return 0;
3304
3305 InstantiatingTemplate Inst(*this, PointOfInstantiation, FromVar);
3306 if (Inst)
3307 return 0;
3308
3309 MultiLevelTemplateArgumentList TemplateArgLists;
3310 TemplateArgLists.addOuterTemplateArguments(&TemplateArgList);
3311
3312 TemplateDeclInstantiator Instantiator(
3313 *this, VarTemplate->getDeclContext(),
3314 MultiLevelTemplateArgumentList(TemplateArgList));
3315
3316 // TODO: Set LateAttrs and StartingScope ...
3317
3318 return cast_or_null<VarTemplateSpecializationDecl>(
3319 Instantiator.VisitVarTemplateSpecializationDecl(
3320 VarTemplate, FromVar, InsertPos, TemplateArgsInfo, Converted));
3321}
3322
3323/// \brief Instantiates a variable template specialization by completing it
3324/// with appropriate type information and initializer.
3325VarTemplateSpecializationDecl *Sema::CompleteVarTemplateSpecializationDecl(
3326 VarTemplateSpecializationDecl *VarSpec, VarDecl *PatternDecl,
3327 const MultiLevelTemplateArgumentList &TemplateArgs) {
3328
3329 // Do substitution on the type of the declaration
Larisse Voufo04592e72013-08-22 00:28:27 +00003330 MultiLevelTemplateArgumentList Innermost;
3331 Innermost.addOuterTemplateArguments(TemplateArgs.getInnermost());
Larisse Voufoef4579c2013-08-06 01:03:05 +00003332 TypeSourceInfo *DI =
Larisse Voufo04592e72013-08-22 00:28:27 +00003333 SubstType(PatternDecl->getTypeSourceInfo(), Innermost,
Larisse Voufoef4579c2013-08-06 01:03:05 +00003334 PatternDecl->getTypeSpecStartLoc(), PatternDecl->getDeclName());
3335 if (!DI)
3336 return 0;
3337
3338 // Update the type of this variable template specialization.
3339 VarSpec->setType(DI->getType());
3340
3341 // Instantiate the initializer.
3342 InstantiateVariableInitializer(VarSpec, PatternDecl, TemplateArgs);
3343
3344 return VarSpec;
3345}
3346
3347/// BuildVariableInstantiation - Used after a new variable has been created.
3348/// Sets basic variable data and decides whether to postpone the
3349/// variable instantiation.
3350void Sema::BuildVariableInstantiation(
3351 VarDecl *NewVar, VarDecl *OldVar,
3352 const MultiLevelTemplateArgumentList &TemplateArgs,
Richard Smitha41c97a2013-09-20 01:15:31 +00003353 LateInstantiatedAttrVec *LateAttrs, DeclContext *Owner,
3354 LocalInstantiationScope *StartingScope,
Larisse Voufo567f9172013-08-22 00:59:14 +00003355 bool InstantiatingVarTemplate) {
Larisse Voufoef4579c2013-08-06 01:03:05 +00003356
Richard Smitha41c97a2013-09-20 01:15:31 +00003357 // If we are instantiating a local extern declaration, the
3358 // instantiation belongs lexically to the containing function.
Larisse Voufoef4579c2013-08-06 01:03:05 +00003359 // If we are instantiating a static data member defined
3360 // out-of-line, the instantiation will have the same lexical
3361 // context (which will be a namespace scope) as the template.
Richard Smitha41c97a2013-09-20 01:15:31 +00003362 if (OldVar->isLocalExternDecl()) {
3363 NewVar->setLocalExternDecl();
3364 NewVar->setLexicalDeclContext(Owner);
3365 } else if (OldVar->isOutOfLine())
Larisse Voufoef4579c2013-08-06 01:03:05 +00003366 NewVar->setLexicalDeclContext(OldVar->getLexicalDeclContext());
3367 NewVar->setTSCSpec(OldVar->getTSCSpec());
3368 NewVar->setInitStyle(OldVar->getInitStyle());
3369 NewVar->setCXXForRangeDecl(OldVar->isCXXForRangeDecl());
3370 NewVar->setConstexpr(OldVar->isConstexpr());
Richard Smithdd9459f2013-08-13 18:18:50 +00003371 NewVar->setPreviousDeclInSameBlockScope(
3372 OldVar->isPreviousDeclInSameBlockScope());
Larisse Voufoef4579c2013-08-06 01:03:05 +00003373 NewVar->setAccess(OldVar->getAccess());
3374
3375 if (!OldVar->isStaticDataMember()) {
Eli Friedman86164e82013-09-05 00:02:25 +00003376 NewVar->setIsUsed(OldVar->isUsed(false));
Larisse Voufoef4579c2013-08-06 01:03:05 +00003377 NewVar->setReferenced(OldVar->isReferenced());
3378 }
3379
David Majnemeraa824612013-09-17 23:57:10 +00003380 // See if the old variable had a type-specifier that defined an anonymous tag.
3381 // If it did, mark the new variable as being the declarator for the new
3382 // anonymous tag.
3383 if (const TagType *OldTagType = OldVar->getType()->getAs<TagType>()) {
3384 TagDecl *OldTag = OldTagType->getDecl();
3385 if (OldTag->getDeclaratorForAnonDecl() == OldVar) {
3386 TagDecl *NewTag = NewVar->getType()->castAs<TagType>()->getDecl();
3387 assert(!NewTag->hasNameForLinkage() &&
3388 !NewTag->hasDeclaratorForAnonDecl());
3389 NewTag->setDeclaratorForAnonDecl(NewVar);
3390 }
3391 }
3392
Larisse Voufoef4579c2013-08-06 01:03:05 +00003393 InstantiateAttrs(TemplateArgs, OldVar, NewVar, LateAttrs, StartingScope);
3394
3395 if (NewVar->hasAttrs())
3396 CheckAlignasUnderalignment(NewVar);
3397
Richard Smitha41c97a2013-09-20 01:15:31 +00003398 LookupResult Previous(
3399 *this, NewVar->getDeclName(), NewVar->getLocation(),
3400 NewVar->isLocalExternDecl() ? Sema::LookupRedeclarationWithLinkage
3401 : Sema::LookupOrdinaryName,
3402 Sema::ForRedeclaration);
Larisse Voufoef4579c2013-08-06 01:03:05 +00003403
Richard Smitha41c97a2013-09-20 01:15:31 +00003404 if (NewVar->isLocalExternDecl() && OldVar->getPreviousDecl()) {
Richard Smithdd9459f2013-08-13 18:18:50 +00003405 // We have a previous declaration. Use that one, so we merge with the
3406 // right type.
3407 if (NamedDecl *NewPrev = FindInstantiatedDecl(
3408 NewVar->getLocation(), OldVar->getPreviousDecl(), TemplateArgs))
3409 Previous.addDecl(NewPrev);
3410 } else if (!isa<VarTemplateSpecializationDecl>(NewVar) &&
3411 OldVar->hasLinkage())
Larisse Voufoef4579c2013-08-06 01:03:05 +00003412 LookupQualifiedName(Previous, NewVar->getDeclContext(), false);
Larisse Voufo567f9172013-08-22 00:59:14 +00003413 CheckVariableDeclaration(NewVar, Previous);
Larisse Voufoef4579c2013-08-06 01:03:05 +00003414
Richard Smitha41c97a2013-09-20 01:15:31 +00003415 if (!InstantiatingVarTemplate) {
3416 NewVar->getLexicalDeclContext()->addHiddenDecl(NewVar);
3417 if (!NewVar->isLocalExternDecl() || !NewVar->getPreviousDecl())
Larisse Voufoef4579c2013-08-06 01:03:05 +00003418 NewVar->getDeclContext()->makeDeclVisibleInContext(NewVar);
Richard Smitha41c97a2013-09-20 01:15:31 +00003419 }
3420
3421 if (!OldVar->isOutOfLine()) {
Larisse Voufoef4579c2013-08-06 01:03:05 +00003422 if (NewVar->getDeclContext()->isFunctionOrMethod())
3423 CurrentInstantiationScope->InstantiatedLocal(OldVar, NewVar);
3424 }
3425
3426 // Link instantiations of static data members back to the template from
3427 // which they were instantiated.
Larisse Voufo567f9172013-08-22 00:59:14 +00003428 if (NewVar->isStaticDataMember() && !InstantiatingVarTemplate)
Larisse Voufoef4579c2013-08-06 01:03:05 +00003429 NewVar->setInstantiationOfStaticDataMember(OldVar,
3430 TSK_ImplicitInstantiation);
3431
3432 if (isa<VarTemplateSpecializationDecl>(NewVar)) {
3433 // Do not instantiate the variable just yet.
Larisse Voufo567f9172013-08-22 00:59:14 +00003434 } else if (InstantiatingVarTemplate) {
Larisse Voufo04592e72013-08-22 00:28:27 +00003435 assert(!NewVar->getInit() &&
3436 "A variable should not have an initializer if it is templated"
3437 " and we are instantiating its template");
3438 NewVar->setInit(OldVar->getInit());
Larisse Voufoef4579c2013-08-06 01:03:05 +00003439 } else
3440 InstantiateVariableInitializer(NewVar, OldVar, TemplateArgs);
3441
3442 // Diagnose unused local variables with dependent types, where the diagnostic
3443 // will have been deferred.
3444 if (!NewVar->isInvalidDecl() &&
3445 NewVar->getDeclContext()->isFunctionOrMethod() && !NewVar->isUsed() &&
3446 OldVar->getType()->isDependentType())
3447 DiagnoseUnusedDecl(NewVar);
3448}
3449
3450/// \brief Instantiate the initializer of a variable.
3451void Sema::InstantiateVariableInitializer(
3452 VarDecl *Var, VarDecl *OldVar,
3453 const MultiLevelTemplateArgumentList &TemplateArgs) {
3454
3455 if (Var->getAnyInitializer())
3456 // We already have an initializer in the class.
3457 return;
3458
3459 if (OldVar->getInit()) {
3460 if (Var->isStaticDataMember() && !OldVar->isOutOfLine())
3461 PushExpressionEvaluationContext(Sema::ConstantEvaluated, OldVar);
3462 else
3463 PushExpressionEvaluationContext(Sema::PotentiallyEvaluated, OldVar);
3464
3465 // Instantiate the initializer.
3466 ExprResult Init =
3467 SubstInitializer(OldVar->getInit(), TemplateArgs,
3468 OldVar->getInitStyle() == VarDecl::CallInit);
3469 if (!Init.isInvalid()) {
3470 bool TypeMayContainAuto = true;
3471 if (Init.get()) {
3472 bool DirectInit = OldVar->isDirectInit();
3473 AddInitializerToDecl(Var, Init.take(), DirectInit, TypeMayContainAuto);
3474 } else
3475 ActOnUninitializedDecl(Var, TypeMayContainAuto);
3476 } else {
3477 // FIXME: Not too happy about invalidating the declaration
3478 // because of a bogus initializer.
3479 Var->setInvalidDecl();
3480 }
3481
3482 PopExpressionEvaluationContext();
3483 } else if ((!Var->isStaticDataMember() || Var->isOutOfLine()) &&
3484 !Var->isCXXForRangeDecl())
3485 ActOnUninitializedDecl(Var, false);
3486}
3487
Douglas Gregora58861f2009-05-13 20:28:22 +00003488/// \brief Instantiate the definition of the given variable from its
3489/// template.
3490///
Douglas Gregor7caa6822009-07-24 20:34:43 +00003491/// \param PointOfInstantiation the point at which the instantiation was
3492/// required. Note that this is not precisely a "point of instantiation"
3493/// for the function, but it's close.
3494///
3495/// \param Var the already-instantiated declaration of a static member
3496/// variable of a class template specialization.
3497///
3498/// \param Recursive if true, recursively instantiates any functions that
3499/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00003500///
3501/// \param DefinitionRequired if true, then we are performing an explicit
3502/// instantiation where an out-of-line definition of the member variable
3503/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00003504void Sema::InstantiateStaticDataMemberDefinition(
3505 SourceLocation PointOfInstantiation,
3506 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00003507 bool Recursive,
3508 bool DefinitionRequired) {
Larisse Voufoef4579c2013-08-06 01:03:05 +00003509 InstantiateVariableDefinition(PointOfInstantiation, Var, Recursive,
3510 DefinitionRequired);
3511}
3512
3513void Sema::InstantiateVariableDefinition(SourceLocation PointOfInstantiation,
3514 VarDecl *Var, bool Recursive,
3515 bool DefinitionRequired) {
3516
Douglas Gregor7caa6822009-07-24 20:34:43 +00003517 if (Var->isInvalidDecl())
3518 return;
Mike Stump1eb44332009-09-09 15:08:12 +00003519
Larisse Voufoef4579c2013-08-06 01:03:05 +00003520 VarTemplateSpecializationDecl *VarSpec =
3521 dyn_cast<VarTemplateSpecializationDecl>(Var);
3522 assert((VarSpec || Var->isStaticDataMember()) &&
3523 "Not a static data member, nor a variable template specialization?");
3524 VarDecl *PatternDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003525
Larisse Voufoef4579c2013-08-06 01:03:05 +00003526 // If this is a variable template specialization, make sure that it is
3527 // non-dependent, then find its instantiation pattern.
3528 if (VarSpec) {
3529 bool InstantiationDependent = false;
3530 assert(!TemplateSpecializationType::anyDependentTemplateArguments(
3531 VarSpec->getTemplateArgsInfo(), InstantiationDependent) &&
3532 "Only instantiate variable template specializations that are "
3533 "not type-dependent");
Larisse Voufo3151b7c2013-08-06 03:57:41 +00003534 (void)InstantiationDependent;
Larisse Voufoef4579c2013-08-06 01:03:05 +00003535
3536 // Find the variable initialization that we'll be substituting.
3537 assert(VarSpec->getSpecializedTemplate() &&
3538 "Specialization without specialized template?");
3539 llvm::PointerUnion<VarTemplateDecl *,
3540 VarTemplatePartialSpecializationDecl *> PatternPtr =
3541 VarSpec->getSpecializedTemplateOrPartial();
Larisse Voufo439d6652013-08-13 02:02:26 +00003542 if (PatternPtr.is<VarTemplatePartialSpecializationDecl *>()) {
Larisse Voufoef4579c2013-08-06 01:03:05 +00003543 PatternDecl = cast<VarDecl>(
3544 PatternPtr.get<VarTemplatePartialSpecializationDecl *>());
Larisse Voufo439d6652013-08-13 02:02:26 +00003545
3546 // Find actual definition
3547 if (VarDecl *Def = PatternDecl->getDefinition(getASTContext()))
3548 PatternDecl = Def;
3549 } else {
3550 VarTemplateDecl *PatternTemplate = PatternPtr.get<VarTemplateDecl *>();
3551
3552 // Find actual definition
3553 if (VarTemplateDecl *Def = PatternTemplate->getDefinition())
3554 PatternTemplate = Def;
3555
3556 PatternDecl = PatternTemplate->getTemplatedDecl();
3557 }
Larisse Voufoef4579c2013-08-06 01:03:05 +00003558 assert(PatternDecl && "instantiating a non-template");
3559 }
3560
3561 // If this is a static data member, find its out-of-line definition.
3562 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
3563 if (Var->isStaticDataMember()) {
3564 assert(Def && "This data member was not instantiated from a template?");
3565 assert(Def->isStaticDataMember() && "Not a static data member?");
3566 Def = Def->getOutOfLineDefinition();
3567 }
3568
3569 // If the instantiation pattern does not have an initializer, or if an
3570 // out-of-line definition is not found, we won't perform any instantiation.
3571 // Rather, we rely on the user to instantiate this definition (or provide
3572 // a specialization for it) in another translation unit.
3573 if ((VarSpec && !PatternDecl->getInit()) ||
3574 (!VarSpec && Var->isStaticDataMember() && !Def)) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00003575 if (DefinitionRequired) {
Larisse Voufoef4579c2013-08-06 01:03:05 +00003576 if (!Var->isStaticDataMember()) {
3577 Diag(PointOfInstantiation,
3578 diag::err_explicit_instantiation_undefined_var_template)
3579 << PatternDecl;
3580 Diag(PatternDecl->getLocation(),
3581 diag::note_explicit_instantiation_here);
3582 } else {
3583 Def = Var->getInstantiatedFromStaticDataMember();
3584 Diag(PointOfInstantiation,
3585 diag::err_explicit_instantiation_undefined_member)
3586 << 3 << Var->getDeclName() << Var->getDeclContext();
3587 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
3588 }
3589 if (VarSpec)
3590 Var->setInvalidDecl();
Chandler Carruth58e390e2010-08-25 08:27:02 +00003591 } else if (Var->getTemplateSpecializationKind()
3592 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00003593 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00003594 std::make_pair(Var, PointOfInstantiation));
3595 }
3596
Douglas Gregor7caa6822009-07-24 20:34:43 +00003597 return;
3598 }
3599
Rafael Espindola234fe652012-03-05 10:54:55 +00003600 TemplateSpecializationKind TSK = Var->getTemplateSpecializationKind();
3601
Douglas Gregor251b4ff2009-10-08 07:24:58 +00003602 // Never instantiate an explicit specialization.
Rafael Espindola234fe652012-03-05 10:54:55 +00003603 if (TSK == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00003604 return;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003605
Douglas Gregor251b4ff2009-10-08 07:24:58 +00003606 // C++0x [temp.explicit]p9:
3607 // Except for inline functions, other explicit instantiation declarations
3608 // have the effect of suppressing the implicit instantiation of the entity
3609 // to which they refer.
Larisse Voufoef4579c2013-08-06 01:03:05 +00003610 //
3611 // C++11 [temp.explicit]p10:
3612 // Except for inline functions, [...] explicit instantiation declarations
3613 // have the effect of suppressing the implicit instantiation of the entity
3614 // to which they refer.
Rafael Espindola234fe652012-03-05 10:54:55 +00003615 if (TSK == TSK_ExplicitInstantiationDeclaration)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00003616 return;
Mike Stump1eb44332009-09-09 15:08:12 +00003617
Argyrios Kyrtzidisafda9052013-02-24 00:05:01 +00003618 // Make sure to pass the instantiated variable to the consumer at the end.
3619 struct PassToConsumerRAII {
3620 ASTConsumer &Consumer;
3621 VarDecl *Var;
3622
3623 PassToConsumerRAII(ASTConsumer &Consumer, VarDecl *Var)
3624 : Consumer(Consumer), Var(Var) { }
3625
3626 ~PassToConsumerRAII() {
Larisse Voufoef4579c2013-08-06 01:03:05 +00003627 if (Var->isStaticDataMember())
3628 Consumer.HandleCXXStaticMemberVarInstantiation(Var);
3629 else {
3630 DeclGroupRef DG(Var);
3631 Consumer.HandleTopLevelDecl(DG);
3632 }
Argyrios Kyrtzidisafda9052013-02-24 00:05:01 +00003633 }
3634 } PassToConsumerRAII(Consumer, Var);
Rafael Espindola02503932012-03-08 15:51:03 +00003635
Larisse Voufoef4579c2013-08-06 01:03:05 +00003636 if (!VarSpec) {
3637 // If we already have a definition, we're done.
3638 if (VarDecl *Def = Var->getDefinition()) {
3639 // We may be explicitly instantiating something we've already implicitly
3640 // instantiated.
3641 Def->setTemplateSpecializationKind(Var->getTemplateSpecializationKind(),
3642 PointOfInstantiation);
3643 return;
3644 }
Nick Lewycky95e38722012-04-04 02:38:36 +00003645 }
Douglas Gregorf15748a2011-06-03 03:35:07 +00003646
Douglas Gregor7caa6822009-07-24 20:34:43 +00003647 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
3648 if (Inst)
3649 return;
Mike Stump1eb44332009-09-09 15:08:12 +00003650
Douglas Gregor7caa6822009-07-24 20:34:43 +00003651 // If we're performing recursive template instantiation, create our own
3652 // queue of pending implicit instantiations that we will instantiate later,
3653 // while we're still within our own instantiation context.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003654 SmallVector<VTableUse, 16> SavedVTableUses;
Chandler Carruth62c78d52010-08-25 08:44:16 +00003655 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Nick Lewycky81559102011-05-31 07:58:42 +00003656 if (Recursive) {
3657 VTableUses.swap(SavedVTableUses);
Chandler Carruth62c78d52010-08-25 08:44:16 +00003658 PendingInstantiations.swap(SavedPendingInstantiations);
Nick Lewycky81559102011-05-31 07:58:42 +00003659 }
Mike Stump1eb44332009-09-09 15:08:12 +00003660
Douglas Gregor7caa6822009-07-24 20:34:43 +00003661 // Enter the scope of this instantiation. We don't use
3662 // PushDeclContext because we don't have a scope.
Larisse Voufoef4579c2013-08-06 01:03:05 +00003663 ContextRAII PreviousContext(*this, Var->getDeclContext());
Douglas Gregor7bdc1522012-02-16 21:36:18 +00003664 LocalInstantiationScope Local(*this);
John McCallf5ba7e02011-02-14 20:37:25 +00003665
Larisse Voufoef4579c2013-08-06 01:03:05 +00003666 VarDecl *OldVar = Var;
3667 if (!VarSpec)
3668 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
3669 getTemplateInstantiationArgs(Var)));
3670 else
3671 // Construct a VarTemplateSpecializationDecl to avoid name clashing with
3672 // the primary template. (Note that unlike function declarations, variable
3673 // declarations cannot be overloaded.)
3674 // In fact, there is no need to construct a new declaration from scratch.
3675 // Thus, simply complete its definition with an appropriately substituted
3676 // type and initializer.
3677 Var = CompleteVarTemplateSpecializationDecl(
3678 VarSpec, PatternDecl, getTemplateInstantiationArgs(Var));
3679
3680 PreviousContext.pop();
Douglas Gregor7caa6822009-07-24 20:34:43 +00003681
3682 if (Var) {
Douglas Gregor583f33b2009-10-15 18:07:02 +00003683 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
Larisse Voufoef4579c2013-08-06 01:03:05 +00003684 if (!VarSpec)
3685 assert(MSInfo && "Missing member specialization information?");
3686
3687 PassToConsumerRAII.Var = Var;
3688 if (MSInfo)
3689 Var->setTemplateSpecializationKind(
3690 MSInfo->getTemplateSpecializationKind(),
3691 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00003692 }
Larisse Voufoef4579c2013-08-06 01:03:05 +00003693
3694 // This variable may have local implicit instantiations that need to be
3695 // instantiated within this scope.
3696 PerformPendingInstantiations(/*LocalOnly=*/true);
3697
Douglas Gregor7bdc1522012-02-16 21:36:18 +00003698 Local.Exit();
3699
Douglas Gregor7caa6822009-07-24 20:34:43 +00003700 if (Recursive) {
Nick Lewycky81559102011-05-31 07:58:42 +00003701 // Define any newly required vtables.
3702 DefineUsedVTables();
3703
Douglas Gregor7caa6822009-07-24 20:34:43 +00003704 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00003705 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003706 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00003707
Nick Lewycky81559102011-05-31 07:58:42 +00003708 // Restore the set of pending vtables.
3709 assert(VTableUses.empty() &&
Larisse Voufoef4579c2013-08-06 01:03:05 +00003710 "VTableUses should be empty before it is discarded.");
Nick Lewycky81559102011-05-31 07:58:42 +00003711 VTableUses.swap(SavedVTableUses);
3712
Douglas Gregor7caa6822009-07-24 20:34:43 +00003713 // Restore the set of pending implicit instantiations.
Nick Lewycky81559102011-05-31 07:58:42 +00003714 assert(PendingInstantiations.empty() &&
Larisse Voufoef4579c2013-08-06 01:03:05 +00003715 "PendingInstantiations should be empty before it is discarded.");
Chandler Carruth62c78d52010-08-25 08:44:16 +00003716 PendingInstantiations.swap(SavedPendingInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00003717 }
Douglas Gregora58861f2009-05-13 20:28:22 +00003718}
Douglas Gregor815215d2009-05-27 05:35:12 +00003719
Anders Carlsson09025312009-08-29 05:16:22 +00003720void
3721Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
3722 const CXXConstructorDecl *Tmpl,
3723 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00003724
Richard Trieu90ab75b2011-09-09 03:18:59 +00003725 SmallVector<CXXCtorInitializer*, 4> NewInits;
Richard Smith54b3ba82012-09-25 00:23:05 +00003726 bool AnyErrors = Tmpl->isInvalidDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003727
Anders Carlsson09025312009-08-29 05:16:22 +00003728 // Instantiate all the initializers.
3729 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00003730 InitsEnd = Tmpl->init_end();
3731 Inits != InitsEnd; ++Inits) {
Sean Huntcbb67482011-01-08 20:30:50 +00003732 CXXCtorInitializer *Init = *Inits;
Anders Carlsson09025312009-08-29 05:16:22 +00003733
Chandler Carruth030ef472010-09-03 21:54:20 +00003734 // Only instantiate written initializers, let Sema re-construct implicit
3735 // ones.
3736 if (!Init->isWritten())
3737 continue;
3738
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003739 SourceLocation EllipsisLoc;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003740
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003741 if (Init->isPackExpansion()) {
3742 // This is a pack expansion. We should expand it now.
Douglas Gregor76852c22011-11-01 01:16:03 +00003743 TypeLoc BaseTL = Init->getTypeSourceInfo()->getTypeLoc();
Nick Lewycky98a75582013-06-13 00:45:47 +00003744 SmallVector<UnexpandedParameterPack, 4> Unexpanded;
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003745 collectUnexpandedParameterPacks(BaseTL, Unexpanded);
Nick Lewycky98a75582013-06-13 00:45:47 +00003746 collectUnexpandedParameterPacks(Init->getInit(), Unexpanded);
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003747 bool ShouldExpand = false;
Douglas Gregord3731192011-01-10 07:32:04 +00003748 bool RetainExpansion = false;
David Blaikiedc84cd52013-02-20 22:23:23 +00003749 Optional<unsigned> NumExpansions;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003750 if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(),
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003751 BaseTL.getSourceRange(),
David Blaikiea71f9d02011-09-22 02:34:54 +00003752 Unexpanded,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003753 TemplateArgs, ShouldExpand,
Douglas Gregord3731192011-01-10 07:32:04 +00003754 RetainExpansion,
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003755 NumExpansions)) {
3756 AnyErrors = true;
3757 New->setInvalidDecl();
3758 continue;
3759 }
3760 assert(ShouldExpand && "Partial instantiation of base initializer?");
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003761
3762 // Loop over all of the arguments in the argument pack(s),
Douglas Gregorcded4f62011-01-14 17:04:44 +00003763 for (unsigned I = 0; I != *NumExpansions; ++I) {
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003764 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
3765
3766 // Instantiate the initializer.
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003767 ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs,
3768 /*CXXDirectInit=*/true);
3769 if (TempInit.isInvalid()) {
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003770 AnyErrors = true;
3771 break;
3772 }
3773
3774 // Instantiate the base type.
Douglas Gregor76852c22011-11-01 01:16:03 +00003775 TypeSourceInfo *BaseTInfo = SubstType(Init->getTypeSourceInfo(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003776 TemplateArgs,
3777 Init->getSourceLocation(),
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003778 New->getDeclName());
3779 if (!BaseTInfo) {
3780 AnyErrors = true;
3781 break;
3782 }
3783
3784 // Build the initializer.
Sebastian Redl6df65482011-09-24 17:48:25 +00003785 MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(),
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003786 BaseTInfo, TempInit.take(),
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003787 New->getParent(),
3788 SourceLocation());
3789 if (NewInit.isInvalid()) {
3790 AnyErrors = true;
3791 break;
3792 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003793
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003794 NewInits.push_back(NewInit.get());
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003795 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003796
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003797 continue;
3798 }
3799
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00003800 // Instantiate the initializer.
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003801 ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs,
3802 /*CXXDirectInit=*/true);
3803 if (TempInit.isInvalid()) {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00003804 AnyErrors = true;
3805 continue;
Anders Carlsson09025312009-08-29 05:16:22 +00003806 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003807
Anders Carlsson09025312009-08-29 05:16:22 +00003808 MemInitResult NewInit;
Douglas Gregor76852c22011-11-01 01:16:03 +00003809 if (Init->isDelegatingInitializer() || Init->isBaseInitializer()) {
3810 TypeSourceInfo *TInfo = SubstType(Init->getTypeSourceInfo(),
3811 TemplateArgs,
3812 Init->getSourceLocation(),
3813 New->getDeclName());
3814 if (!TInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00003815 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00003816 New->setInvalidDecl();
3817 continue;
3818 }
Sebastian Redl6df65482011-09-24 17:48:25 +00003819
Douglas Gregor76852c22011-11-01 01:16:03 +00003820 if (Init->isBaseInitializer())
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003821 NewInit = BuildBaseInitializer(TInfo->getType(), TInfo, TempInit.take(),
Douglas Gregor76852c22011-11-01 01:16:03 +00003822 New->getParent(), EllipsisLoc);
3823 else
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003824 NewInit = BuildDelegatingInitializer(TInfo, TempInit.take(),
Douglas Gregor76852c22011-11-01 01:16:03 +00003825 cast<CXXRecordDecl>(CurContext->getParent()));
Anders Carlsson09025312009-08-29 05:16:22 +00003826 } else if (Init->isMemberInitializer()) {
Douglas Gregorb7107222011-03-04 19:46:35 +00003827 FieldDecl *Member = cast_or_null<FieldDecl>(FindInstantiatedDecl(
Francois Pichet00eb3f92010-12-04 09:14:42 +00003828 Init->getMemberLocation(),
3829 Init->getMember(),
3830 TemplateArgs));
Douglas Gregorb7107222011-03-04 19:46:35 +00003831 if (!Member) {
3832 AnyErrors = true;
3833 New->setInvalidDecl();
3834 continue;
3835 }
Mike Stump1eb44332009-09-09 15:08:12 +00003836
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003837 NewInit = BuildMemberInitializer(Member, TempInit.take(),
Sebastian Redl6df65482011-09-24 17:48:25 +00003838 Init->getSourceLocation());
Francois Pichet00eb3f92010-12-04 09:14:42 +00003839 } else if (Init->isIndirectMemberInitializer()) {
3840 IndirectFieldDecl *IndirectMember =
Douglas Gregorb7107222011-03-04 19:46:35 +00003841 cast_or_null<IndirectFieldDecl>(FindInstantiatedDecl(
Francois Pichet00eb3f92010-12-04 09:14:42 +00003842 Init->getMemberLocation(),
3843 Init->getIndirectMember(), TemplateArgs));
3844
Douglas Gregorb7107222011-03-04 19:46:35 +00003845 if (!IndirectMember) {
3846 AnyErrors = true;
3847 New->setInvalidDecl();
Sebastian Redl6df65482011-09-24 17:48:25 +00003848 continue;
Douglas Gregorb7107222011-03-04 19:46:35 +00003849 }
Sebastian Redl6df65482011-09-24 17:48:25 +00003850
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003851 NewInit = BuildMemberInitializer(IndirectMember, TempInit.take(),
Sebastian Redl6df65482011-09-24 17:48:25 +00003852 Init->getSourceLocation());
Anders Carlsson09025312009-08-29 05:16:22 +00003853 }
3854
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00003855 if (NewInit.isInvalid()) {
3856 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00003857 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00003858 } else {
Richard Trieu90ab75b2011-09-09 03:18:59 +00003859 NewInits.push_back(NewInit.get());
Anders Carlsson09025312009-08-29 05:16:22 +00003860 }
3861 }
Mike Stump1eb44332009-09-09 15:08:12 +00003862
Anders Carlsson09025312009-08-29 05:16:22 +00003863 // Assign all the initializers to the new constructor.
John McCalld226f652010-08-21 09:40:31 +00003864 ActOnMemInitializers(New,
Anders Carlsson09025312009-08-29 05:16:22 +00003865 /*FIXME: ColonLoc */
3866 SourceLocation(),
David Blaikie93c86172013-01-17 05:26:25 +00003867 NewInits,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00003868 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00003869}
3870
John McCall52a575a2009-08-29 08:11:13 +00003871// TODO: this could be templated if the various decl types used the
3872// same method name.
3873static bool isInstantiationOf(ClassTemplateDecl *Pattern,
3874 ClassTemplateDecl *Instance) {
3875 Pattern = Pattern->getCanonicalDecl();
3876
3877 do {
3878 Instance = Instance->getCanonicalDecl();
3879 if (Pattern == Instance) return true;
3880 Instance = Instance->getInstantiatedFromMemberTemplate();
3881 } while (Instance);
3882
3883 return false;
3884}
3885
Douglas Gregor0d696532009-09-28 06:34:35 +00003886static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
3887 FunctionTemplateDecl *Instance) {
3888 Pattern = Pattern->getCanonicalDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003889
Douglas Gregor0d696532009-09-28 06:34:35 +00003890 do {
3891 Instance = Instance->getCanonicalDecl();
3892 if (Pattern == Instance) return true;
3893 Instance = Instance->getInstantiatedFromMemberTemplate();
3894 } while (Instance);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003895
Douglas Gregor0d696532009-09-28 06:34:35 +00003896 return false;
3897}
3898
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003899static bool
Douglas Gregored9c0f92009-10-29 00:04:11 +00003900isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
3901 ClassTemplatePartialSpecializationDecl *Instance) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003902 Pattern
Douglas Gregored9c0f92009-10-29 00:04:11 +00003903 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
3904 do {
3905 Instance = cast<ClassTemplatePartialSpecializationDecl>(
3906 Instance->getCanonicalDecl());
3907 if (Pattern == Instance)
3908 return true;
3909 Instance = Instance->getInstantiatedFromMember();
3910 } while (Instance);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003911
Douglas Gregored9c0f92009-10-29 00:04:11 +00003912 return false;
3913}
3914
John McCall52a575a2009-08-29 08:11:13 +00003915static bool isInstantiationOf(CXXRecordDecl *Pattern,
3916 CXXRecordDecl *Instance) {
3917 Pattern = Pattern->getCanonicalDecl();
3918
3919 do {
3920 Instance = Instance->getCanonicalDecl();
3921 if (Pattern == Instance) return true;
3922 Instance = Instance->getInstantiatedFromMemberClass();
3923 } while (Instance);
3924
3925 return false;
3926}
3927
3928static bool isInstantiationOf(FunctionDecl *Pattern,
3929 FunctionDecl *Instance) {
3930 Pattern = Pattern->getCanonicalDecl();
3931
3932 do {
3933 Instance = Instance->getCanonicalDecl();
3934 if (Pattern == Instance) return true;
3935 Instance = Instance->getInstantiatedFromMemberFunction();
3936 } while (Instance);
3937
3938 return false;
3939}
3940
3941static bool isInstantiationOf(EnumDecl *Pattern,
3942 EnumDecl *Instance) {
3943 Pattern = Pattern->getCanonicalDecl();
3944
3945 do {
3946 Instance = Instance->getCanonicalDecl();
3947 if (Pattern == Instance) return true;
3948 Instance = Instance->getInstantiatedFromMemberEnum();
3949 } while (Instance);
3950
3951 return false;
3952}
3953
John McCalled976492009-12-04 22:46:56 +00003954static bool isInstantiationOf(UsingShadowDecl *Pattern,
3955 UsingShadowDecl *Instance,
3956 ASTContext &C) {
3957 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
3958}
3959
3960static bool isInstantiationOf(UsingDecl *Pattern,
3961 UsingDecl *Instance,
3962 ASTContext &C) {
3963 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
3964}
3965
John McCall7ba107a2009-11-18 02:36:19 +00003966static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
3967 UsingDecl *Instance,
3968 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00003969 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00003970}
3971
3972static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00003973 UsingDecl *Instance,
3974 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00003975 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00003976}
3977
John McCall52a575a2009-08-29 08:11:13 +00003978static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
3979 VarDecl *Instance) {
3980 assert(Instance->isStaticDataMember());
3981
3982 Pattern = Pattern->getCanonicalDecl();
3983
3984 do {
3985 Instance = Instance->getCanonicalDecl();
3986 if (Pattern == Instance) return true;
3987 Instance = Instance->getInstantiatedFromStaticDataMember();
3988 } while (Instance);
3989
3990 return false;
3991}
3992
John McCalled976492009-12-04 22:46:56 +00003993// Other is the prospective instantiation
3994// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00003995static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00003996 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00003997 if (UnresolvedUsingTypenameDecl *UUD
3998 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
3999 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
4000 return isInstantiationOf(UUD, UD, Ctx);
4001 }
4002 }
4003
4004 if (UnresolvedUsingValueDecl *UUD
4005 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00004006 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
4007 return isInstantiationOf(UUD, UD, Ctx);
4008 }
4009 }
Douglas Gregor815215d2009-05-27 05:35:12 +00004010
Anders Carlsson0d8df782009-08-29 19:37:28 +00004011 return false;
4012 }
Mike Stump1eb44332009-09-09 15:08:12 +00004013
John McCall52a575a2009-08-29 08:11:13 +00004014 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
4015 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00004016
John McCall52a575a2009-08-29 08:11:13 +00004017 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
4018 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00004019
John McCall52a575a2009-08-29 08:11:13 +00004020 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
4021 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00004022
Douglas Gregor7caa6822009-07-24 20:34:43 +00004023 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00004024 if (Var->isStaticDataMember())
4025 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
4026
4027 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
4028 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00004029
Douglas Gregor0d696532009-09-28 06:34:35 +00004030 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
4031 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
4032
Douglas Gregored9c0f92009-10-29 00:04:11 +00004033 if (ClassTemplatePartialSpecializationDecl *PartialSpec
4034 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
4035 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
4036 PartialSpec);
4037
Anders Carlssond8b285f2009-09-01 04:26:58 +00004038 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
4039 if (!Field->getDeclName()) {
4040 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00004041 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00004042 cast<FieldDecl>(D);
4043 }
4044 }
Mike Stump1eb44332009-09-09 15:08:12 +00004045
John McCalled976492009-12-04 22:46:56 +00004046 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
4047 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
4048
4049 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
4050 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
4051
Douglas Gregor815215d2009-05-27 05:35:12 +00004052 return D->getDeclName() && isa<NamedDecl>(Other) &&
4053 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
4054}
4055
4056template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00004057static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00004058 NamedDecl *D,
4059 ForwardIterator first,
4060 ForwardIterator last) {
4061 for (; first != last; ++first)
4062 if (isInstantiationOf(Ctx, D, *first))
4063 return cast<NamedDecl>(*first);
4064
4065 return 0;
4066}
4067
John McCall02cace72009-08-28 07:59:38 +00004068/// \brief Finds the instantiation of the given declaration context
4069/// within the current instantiation.
4070///
4071/// \returns NULL if there was an error
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00004072DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
Douglas Gregore95b4092009-09-16 18:34:49 +00004073 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00004074 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00004075 Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00004076 return cast_or_null<DeclContext>(ID);
4077 } else return DC;
4078}
4079
Douglas Gregored961e72009-05-27 17:54:46 +00004080/// \brief Find the instantiation of the given declaration within the
4081/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00004082///
4083/// This routine is intended to be used when \p D is a declaration
4084/// referenced from within a template, that needs to mapped into the
4085/// corresponding declaration within an instantiation. For example,
4086/// given:
4087///
4088/// \code
4089/// template<typename T>
4090/// struct X {
4091/// enum Kind {
4092/// KnownValue = sizeof(T)
4093/// };
4094///
4095/// bool getKind() const { return KnownValue; }
4096/// };
4097///
4098/// template struct X<int>;
4099/// \endcode
4100///
Serge Pavlov041d10c2013-07-10 04:59:14 +00004101/// In the instantiation of <tt>X<int>::getKind()</tt>, we need to map the
4102/// \p EnumConstantDecl for \p KnownValue (which refers to
4103/// <tt>X<T>::<Kind>::KnownValue</tt>) to its instantiation
4104/// (<tt>X<int>::<Kind>::KnownValue</tt>). \p FindInstantiatedDecl performs
4105/// this mapping from within the instantiation of <tt>X<int></tt>.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00004106NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
Douglas Gregore95b4092009-09-16 18:34:49 +00004107 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00004108 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00004109 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00004110 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
Douglas Gregor7bdc1522012-02-16 21:36:18 +00004111 (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext()) ||
4112 (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda())) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00004113 // D is a local of some kind. Look into the map of local
4114 // declarations to their instantiations.
Chris Lattnerd8e54992011-02-17 19:47:42 +00004115 typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
4116 llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found
4117 = CurrentInstantiationScope->findInstantiationOf(D);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00004118
Chris Lattner57ad3782011-02-17 20:34:02 +00004119 if (Found) {
4120 if (Decl *FD = Found->dyn_cast<Decl *>())
4121 return cast<NamedDecl>(FD);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00004122
Richard Smith9a4db032012-09-12 00:56:43 +00004123 int PackIdx = ArgumentPackSubstitutionIndex;
4124 assert(PackIdx != -1 && "found declaration pack but not pack expanding");
Chris Lattner57ad3782011-02-17 20:34:02 +00004125 return cast<NamedDecl>((*Found->get<DeclArgumentPack *>())[PackIdx]);
4126 }
4127
Serge Pavlovdc49d522013-07-15 06:14:07 +00004128 // If we're performing a partial substitution during template argument
4129 // deduction, we may not have values for template parameters yet. They
4130 // just map to themselves.
4131 if (isa<NonTypeTemplateParmDecl>(D) || isa<TemplateTypeParmDecl>(D) ||
4132 isa<TemplateTemplateParmDecl>(D))
4133 return D;
4134
Serge Pavlov29a46e62013-08-10 12:00:21 +00004135 if (D->isInvalidDecl())
4136 return 0;
4137
Chris Lattner57ad3782011-02-17 20:34:02 +00004138 // If we didn't find the decl, then we must have a label decl that hasn't
4139 // been found yet. Lazily instantiate it and return it now.
4140 assert(isa<LabelDecl>(D));
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00004141
Chris Lattner57ad3782011-02-17 20:34:02 +00004142 Decl *Inst = SubstDecl(D, CurContext, TemplateArgs);
4143 assert(Inst && "Failed to instantiate label??");
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00004144
Chris Lattner57ad3782011-02-17 20:34:02 +00004145 CurrentInstantiationScope->InstantiatedLocal(D, Inst);
4146 return cast<LabelDecl>(Inst);
Douglas Gregor2bba76b2009-05-27 17:07:49 +00004147 }
Douglas Gregor815215d2009-05-27 05:35:12 +00004148
Larisse Voufoef4579c2013-08-06 01:03:05 +00004149 // For variable template specializations, update those that are still
4150 // type-dependent.
4151 if (VarTemplateSpecializationDecl *VarSpec =
4152 dyn_cast<VarTemplateSpecializationDecl>(D)) {
4153 bool InstantiationDependent = false;
4154 const TemplateArgumentListInfo &VarTemplateArgs =
4155 VarSpec->getTemplateArgsInfo();
4156 if (TemplateSpecializationType::anyDependentTemplateArguments(
4157 VarTemplateArgs, InstantiationDependent))
4158 D = cast<NamedDecl>(
4159 SubstDecl(D, VarSpec->getDeclContext(), TemplateArgs));
4160 return D;
4161 }
4162
Douglas Gregore95b4092009-09-16 18:34:49 +00004163 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
4164 if (!Record->isDependentContext())
4165 return D;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00004166
Douglas Gregor2c1227c2011-11-07 17:43:18 +00004167 // Determine whether this record is the "templated" declaration describing
4168 // a class template or class template partial specialization.
Douglas Gregore95b4092009-09-16 18:34:49 +00004169 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
Douglas Gregor2c1227c2011-11-07 17:43:18 +00004170 if (ClassTemplate)
4171 ClassTemplate = ClassTemplate->getCanonicalDecl();
4172 else if (ClassTemplatePartialSpecializationDecl *PartialSpec
4173 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record))
4174 ClassTemplate = PartialSpec->getSpecializedTemplate()->getCanonicalDecl();
Larisse Voufoef4579c2013-08-06 01:03:05 +00004175
Douglas Gregor2c1227c2011-11-07 17:43:18 +00004176 // Walk the current context to find either the record or an instantiation of
4177 // it.
4178 DeclContext *DC = CurContext;
4179 while (!DC->isFileContext()) {
4180 // If we're performing substitution while we're inside the template
4181 // definition, we'll find our own context. We're done.
4182 if (DC->Equals(Record))
4183 return Record;
Larisse Voufoef4579c2013-08-06 01:03:05 +00004184
Douglas Gregor2c1227c2011-11-07 17:43:18 +00004185 if (CXXRecordDecl *InstRecord = dyn_cast<CXXRecordDecl>(DC)) {
4186 // Check whether we're in the process of instantiating a class template
4187 // specialization of the template we're mapping.
4188 if (ClassTemplateSpecializationDecl *InstSpec
4189 = dyn_cast<ClassTemplateSpecializationDecl>(InstRecord)){
4190 ClassTemplateDecl *SpecTemplate = InstSpec->getSpecializedTemplate();
4191 if (ClassTemplate && isInstantiationOf(ClassTemplate, SpecTemplate))
4192 return InstRecord;
4193 }
Larisse Voufoef4579c2013-08-06 01:03:05 +00004194
Douglas Gregor2c1227c2011-11-07 17:43:18 +00004195 // Check whether we're in the process of instantiating a member class.
4196 if (isInstantiationOf(Record, InstRecord))
4197 return InstRecord;
Douglas Gregore95b4092009-09-16 18:34:49 +00004198 }
Larisse Voufoef4579c2013-08-06 01:03:05 +00004199
Douglas Gregor2c1227c2011-11-07 17:43:18 +00004200 // Move to the outer template scope.
4201 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) {
4202 if (FD->getFriendObjectKind() && FD->getDeclContext()->isFileContext()){
4203 DC = FD->getLexicalDeclContext();
4204 continue;
4205 }
John McCall52a575a2009-08-29 08:11:13 +00004206 }
Larisse Voufoef4579c2013-08-06 01:03:05 +00004207
Douglas Gregor2c1227c2011-11-07 17:43:18 +00004208 DC = DC->getParent();
John McCall52a575a2009-08-29 08:11:13 +00004209 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00004210
Douglas Gregore95b4092009-09-16 18:34:49 +00004211 // Fall through to deal with other dependent record types (e.g.,
4212 // anonymous unions in class templates).
4213 }
John McCall52a575a2009-08-29 08:11:13 +00004214
Douglas Gregore95b4092009-09-16 18:34:49 +00004215 if (!ParentDC->isDependentContext())
4216 return D;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00004217
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00004218 ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00004219 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00004220 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00004221
Douglas Gregor815215d2009-05-27 05:35:12 +00004222 if (ParentDC != D->getDeclContext()) {
4223 // We performed some kind of instantiation in the parent context,
4224 // so now we need to look into the instantiated parent context to
4225 // find the instantiation of the declaration D.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00004226
John McCall3cb0ebd2010-03-10 03:28:59 +00004227 // If our context used to be dependent, we may need to instantiate
4228 // it before performing lookup into that context.
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00004229 bool IsBeingInstantiated = false;
John McCall3cb0ebd2010-03-10 03:28:59 +00004230 if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00004231 if (!Spec->isDependentContext()) {
4232 QualType T = Context.getTypeDeclType(Spec);
John McCall3cb0ebd2010-03-10 03:28:59 +00004233 const RecordType *Tag = T->getAs<RecordType>();
4234 assert(Tag && "type of non-dependent record is not a RecordType");
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00004235 if (Tag->isBeingDefined())
4236 IsBeingInstantiated = true;
John McCall3cb0ebd2010-03-10 03:28:59 +00004237 if (!Tag->isBeingDefined() &&
4238 RequireCompleteType(Loc, T, diag::err_incomplete_type))
4239 return 0;
Douglas Gregora43064c2010-11-05 23:22:45 +00004240
4241 ParentDC = Tag->getDecl();
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00004242 }
4243 }
4244
Douglas Gregor815215d2009-05-27 05:35:12 +00004245 NamedDecl *Result = 0;
4246 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004247 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
David Blaikie3bc93e32012-12-19 00:45:41 +00004248 Result = findInstantiationOf(Context, D, Found.begin(), Found.end());
Douglas Gregor815215d2009-05-27 05:35:12 +00004249 } else {
4250 // Since we don't have a name for the entity we're looking for,
4251 // our only option is to walk through all of the declarations to
4252 // find that name. This will occur in a few cases:
4253 //
4254 // - anonymous struct/union within a template
4255 // - unnamed class/struct/union/enum within a template
4256 //
4257 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00004258 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004259 ParentDC->decls_begin(),
4260 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00004261 }
Mike Stump1eb44332009-09-09 15:08:12 +00004262
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00004263 if (!Result) {
4264 if (isa<UsingShadowDecl>(D)) {
4265 // UsingShadowDecls can instantiate to nothing because of using hiding.
4266 } else if (Diags.hasErrorOccurred()) {
4267 // We've already complained about something, so most likely this
4268 // declaration failed to instantiate. There's no point in complaining
4269 // further, since this is normal in invalid code.
4270 } else if (IsBeingInstantiated) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00004271 // The class in which this member exists is currently being
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00004272 // instantiated, and we haven't gotten around to instantiating this
4273 // member yet. This can happen when the code uses forward declarations
4274 // of member classes, and introduces ordering dependencies via
4275 // template instantiation.
4276 Diag(Loc, diag::err_member_not_yet_instantiated)
4277 << D->getDeclName()
4278 << Context.getTypeDeclType(cast<CXXRecordDecl>(ParentDC));
4279 Diag(D->getLocation(), diag::note_non_instantiated_member_here);
Richard Smith0724b7c2012-03-26 20:28:16 +00004280 } else if (EnumConstantDecl *ED = dyn_cast<EnumConstantDecl>(D)) {
4281 // This enumeration constant was found when the template was defined,
4282 // but can't be found in the instantiation. This can happen if an
4283 // unscoped enumeration member is explicitly specialized.
4284 EnumDecl *Enum = cast<EnumDecl>(ED->getLexicalDeclContext());
4285 EnumDecl *Spec = cast<EnumDecl>(FindInstantiatedDecl(Loc, Enum,
4286 TemplateArgs));
4287 assert(Spec->getTemplateSpecializationKind() ==
4288 TSK_ExplicitSpecialization);
4289 Diag(Loc, diag::err_enumerator_does_not_exist)
4290 << D->getDeclName()
4291 << Context.getTypeDeclType(cast<TypeDecl>(Spec->getDeclContext()));
4292 Diag(Spec->getLocation(), diag::note_enum_specialized_here)
4293 << Context.getTypeDeclType(Spec);
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00004294 } else {
4295 // We should have found something, but didn't.
4296 llvm_unreachable("Unable to find instantiation of declaration!");
4297 }
4298 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00004299
Douglas Gregor815215d2009-05-27 05:35:12 +00004300 D = Result;
4301 }
4302
Douglas Gregor815215d2009-05-27 05:35:12 +00004303 return D;
4304}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00004305
Mike Stump1eb44332009-09-09 15:08:12 +00004306/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00004307/// instantiations we have seen until this point.
Nick Lewycky81559102011-05-31 07:58:42 +00004308void Sema::PerformPendingInstantiations(bool LocalOnly) {
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00004309 // Load pending instantiations from the external source.
4310 if (!LocalOnly && ExternalSource) {
Richard Smithb9d0b762012-07-27 04:22:15 +00004311 SmallVector<PendingImplicitInstantiation, 4> Pending;
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00004312 ExternalSource->ReadPendingInstantiations(Pending);
4313 PendingInstantiations.insert(PendingInstantiations.begin(),
4314 Pending.begin(), Pending.end());
4315 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00004316
Douglas Gregor60406be2010-01-16 22:29:39 +00004317 while (!PendingLocalImplicitInstantiations.empty() ||
Chandler Carruth62c78d52010-08-25 08:44:16 +00004318 (!LocalOnly && !PendingInstantiations.empty())) {
Douglas Gregor60406be2010-01-16 22:29:39 +00004319 PendingImplicitInstantiation Inst;
4320
4321 if (PendingLocalImplicitInstantiations.empty()) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00004322 Inst = PendingInstantiations.front();
4323 PendingInstantiations.pop_front();
Douglas Gregor60406be2010-01-16 22:29:39 +00004324 } else {
4325 Inst = PendingLocalImplicitInstantiations.front();
4326 PendingLocalImplicitInstantiations.pop_front();
4327 }
Mike Stump1eb44332009-09-09 15:08:12 +00004328
Douglas Gregor7caa6822009-07-24 20:34:43 +00004329 // Instantiate function definitions
4330 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
John McCallf312b1e2010-08-26 23:41:50 +00004331 PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(),
4332 "instantiating function definition");
Chandler Carruth58e390e2010-08-25 08:27:02 +00004333 bool DefinitionRequired = Function->getTemplateSpecializationKind() ==
4334 TSK_ExplicitInstantiationDefinition;
4335 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true,
4336 DefinitionRequired);
Douglas Gregor7caa6822009-07-24 20:34:43 +00004337 continue;
4338 }
Mike Stump1eb44332009-09-09 15:08:12 +00004339
Larisse Voufoef4579c2013-08-06 01:03:05 +00004340 // Instantiate variable definitions
Douglas Gregor7caa6822009-07-24 20:34:43 +00004341 VarDecl *Var = cast<VarDecl>(Inst.first);
Larisse Voufoef4579c2013-08-06 01:03:05 +00004342
4343 assert((Var->isStaticDataMember() ||
4344 isa<VarTemplateSpecializationDecl>(Var)) &&
4345 "Not a static data member, nor a variable template"
4346 " specialization?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00004347
Chandler Carruth291b4412010-02-13 10:17:50 +00004348 // Don't try to instantiate declarations if the most recent redeclaration
4349 // is invalid.
Douglas Gregoref96ee02012-01-14 16:38:05 +00004350 if (Var->getMostRecentDecl()->isInvalidDecl())
Chandler Carruth291b4412010-02-13 10:17:50 +00004351 continue;
4352
4353 // Check if the most recent declaration has changed the specialization kind
4354 // and removed the need for implicit instantiation.
Douglas Gregoref96ee02012-01-14 16:38:05 +00004355 switch (Var->getMostRecentDecl()->getTemplateSpecializationKind()) {
Chandler Carruth291b4412010-02-13 10:17:50 +00004356 case TSK_Undeclared:
David Blaikieb219cfc2011-09-23 05:06:16 +00004357 llvm_unreachable("Cannot instantitiate an undeclared specialization.");
Chandler Carruth291b4412010-02-13 10:17:50 +00004358 case TSK_ExplicitInstantiationDeclaration:
Chandler Carruth291b4412010-02-13 10:17:50 +00004359 case TSK_ExplicitSpecialization:
Chandler Carruth58e390e2010-08-25 08:27:02 +00004360 continue; // No longer need to instantiate this type.
4361 case TSK_ExplicitInstantiationDefinition:
4362 // We only need an instantiation if the pending instantiation *is* the
4363 // explicit instantiation.
Douglas Gregoref96ee02012-01-14 16:38:05 +00004364 if (Var != Var->getMostRecentDecl()) continue;
Chandler Carruth291b4412010-02-13 10:17:50 +00004365 case TSK_ImplicitInstantiation:
4366 break;
4367 }
4368
Larisse Voufoef4579c2013-08-06 01:03:05 +00004369 PrettyDeclStackTraceEntry CrashInfo(*this, Var, SourceLocation(),
4370 "instantiating variable definition");
Chandler Carruth58e390e2010-08-25 08:27:02 +00004371 bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
4372 TSK_ExplicitInstantiationDefinition;
Larisse Voufoef4579c2013-08-06 01:03:05 +00004373
4374 // Instantiate static data member definitions or variable template
4375 // specializations.
4376 InstantiateVariableDefinition(/*FIXME:*/ Inst.second, Var, true,
4377 DefinitionRequired);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00004378 }
4379}
John McCall0c01d182010-03-24 05:22:00 +00004380
4381void Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
4382 const MultiLevelTemplateArgumentList &TemplateArgs) {
4383 for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
4384 E = Pattern->ddiag_end(); I != E; ++I) {
4385 DependentDiagnostic *DD = *I;
4386
4387 switch (DD->getKind()) {
4388 case DependentDiagnostic::Access:
4389 HandleDependentAccessCheck(*DD, TemplateArgs);
4390 break;
4391 }
4392 }
4393}