blob: 80c452fb03dd88727fb82f65cf5403e36fe93871 [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 {
Benjamin Kramer5bbc3852012-02-06 11:13:08 +0000145 Attr *NewAttr = sema::instantiateTemplateAttribute(TmplAttr, Context,
146 *this, TemplateArgs);
Rafael Espindola31c195a2012-05-15 14:09:55 +0000147 if (NewAttr)
148 New->addAttr(NewAttr);
DeLesley Hutchins23323e02012-01-20 22:50:54 +0000149 }
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000150 }
151}
152
Douglas Gregor4f722be2009-03-25 15:45:12 +0000153Decl *
154TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000155 llvm_unreachable("Translation units cannot be instantiated");
Douglas Gregor4f722be2009-03-25 15:45:12 +0000156}
157
158Decl *
Chris Lattner57ad3782011-02-17 20:34:02 +0000159TemplateDeclInstantiator::VisitLabelDecl(LabelDecl *D) {
160 LabelDecl *Inst = LabelDecl::Create(SemaRef.Context, Owner, D->getLocation(),
161 D->getIdentifier());
162 Owner->addDecl(Inst);
163 return Inst;
164}
165
166Decl *
Douglas Gregor4f722be2009-03-25 15:45:12 +0000167TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000168 llvm_unreachable("Namespaces cannot be instantiated");
Douglas Gregor4f722be2009-03-25 15:45:12 +0000169}
170
John McCall3dbd3d52010-02-16 06:53:13 +0000171Decl *
172TemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
173 NamespaceAliasDecl *Inst
174 = NamespaceAliasDecl::Create(SemaRef.Context, Owner,
175 D->getNamespaceLoc(),
176 D->getAliasLoc(),
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +0000177 D->getIdentifier(),
178 D->getQualifierLoc(),
John McCall3dbd3d52010-02-16 06:53:13 +0000179 D->getTargetNameLoc(),
180 D->getNamespace());
181 Owner->addDecl(Inst);
182 return Inst;
183}
184
Richard Smith3e4c6c42011-05-05 21:57:07 +0000185Decl *TemplateDeclInstantiator::InstantiateTypedefNameDecl(TypedefNameDecl *D,
186 bool IsTypeAlias) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000187 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000188 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor561f8122011-07-01 01:22:09 +0000189 if (DI->getType()->isInstantiationDependentType() ||
Douglas Gregor836adf62010-05-24 17:22:01 +0000190 DI->getType()->isVariablyModifiedType()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000191 DI = SemaRef.SubstType(DI, TemplateArgs,
192 D->getLocation(), D->getDeclName());
193 if (!DI) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000194 Invalid = true;
John McCalla93c9342009-12-07 02:54:59 +0000195 DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000196 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000197 } else {
198 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000199 }
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Richard Smithb5b37d12012-10-23 00:32:41 +0000201 // HACK: g++ has a bug where it gets the value kind of ?: wrong.
202 // libstdc++ relies upon this bug in its implementation of common_type.
203 // If we happen to be processing that implementation, fake up the g++ ?:
204 // semantics. See LWG issue 2141 for more information on the bug.
205 const DecltypeType *DT = DI->getType()->getAs<DecltypeType>();
206 CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D->getDeclContext());
207 if (DT && RD && isa<ConditionalOperator>(DT->getUnderlyingExpr()) &&
208 DT->isReferenceType() &&
209 RD->getEnclosingNamespaceContext() == SemaRef.getStdNamespace() &&
210 RD->getIdentifier() && RD->getIdentifier()->isStr("common_type") &&
211 D->getIdentifier() && D->getIdentifier()->isStr("type") &&
212 SemaRef.getSourceManager().isInSystemHeader(D->getLocStart()))
213 // Fold it to the (non-reference) type which g++ would have produced.
214 DI = SemaRef.Context.getTrivialTypeSourceInfo(
215 DI->getType().getNonReferenceType());
216
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000217 // Create the new typedef
Richard Smith162e1c12011-04-15 14:24:37 +0000218 TypedefNameDecl *Typedef;
219 if (IsTypeAlias)
220 Typedef = TypeAliasDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
221 D->getLocation(), D->getIdentifier(), DI);
222 else
223 Typedef = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
224 D->getLocation(), D->getIdentifier(), DI);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000225 if (Invalid)
226 Typedef->setInvalidDecl();
227
John McCallcde5a402011-02-01 08:20:08 +0000228 // If the old typedef was the name for linkage purposes of an anonymous
229 // tag decl, re-establish that relationship for the new typedef.
230 if (const TagType *oldTagType = D->getUnderlyingType()->getAs<TagType>()) {
231 TagDecl *oldTag = oldTagType->getDecl();
Douglas Gregorc61361b2013-03-08 22:15:15 +0000232 if (oldTag->getTypedefNameForAnonDecl() == D && !Invalid) {
John McCallcde5a402011-02-01 08:20:08 +0000233 TagDecl *newTag = DI->getType()->castAs<TagType>()->getDecl();
John McCall83972f12013-03-09 00:54:27 +0000234 assert(!newTag->hasNameForLinkage());
Richard Smith162e1c12011-04-15 14:24:37 +0000235 newTag->setTypedefNameForAnonDecl(Typedef);
John McCallcde5a402011-02-01 08:20:08 +0000236 }
Douglas Gregord57a38e2010-04-23 16:25:07 +0000237 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000238
Douglas Gregoref96ee02012-01-14 16:38:05 +0000239 if (TypedefNameDecl *Prev = D->getPreviousDecl()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000240 NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
241 TemplateArgs);
Douglas Gregorb7107222011-03-04 19:46:35 +0000242 if (!InstPrev)
243 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000244
Rafael Espindola5df37bd2011-12-26 22:42:47 +0000245 TypedefNameDecl *InstPrevTypedef = cast<TypedefNameDecl>(InstPrev);
246
247 // If the typedef types are not identical, reject them.
248 SemaRef.isIncompatibleTypedef(InstPrevTypedef, Typedef);
249
250 Typedef->setPreviousDeclaration(InstPrevTypedef);
John McCall5126fd02009-12-30 00:31:22 +0000251 }
252
John McCall1d8d1cc2010-08-01 02:01:53 +0000253 SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef);
Douglas Gregord57a38e2010-04-23 16:25:07 +0000254
John McCall46460a62010-01-20 21:53:11 +0000255 Typedef->setAccess(D->getAccess());
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000257 return Typedef;
258}
259
Richard Smith162e1c12011-04-15 14:24:37 +0000260Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
Richard Smith3e4c6c42011-05-05 21:57:07 +0000261 Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/false);
262 Owner->addDecl(Typedef);
263 return Typedef;
Richard Smith162e1c12011-04-15 14:24:37 +0000264}
265
266Decl *TemplateDeclInstantiator::VisitTypeAliasDecl(TypeAliasDecl *D) {
Richard Smith3e4c6c42011-05-05 21:57:07 +0000267 Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/true);
268 Owner->addDecl(Typedef);
269 return Typedef;
270}
271
272Decl *
273TemplateDeclInstantiator::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
274 // Create a local instantiation scope for this type alias template, which
275 // will contain the instantiations of the template parameters.
276 LocalInstantiationScope Scope(SemaRef);
277
278 TemplateParameterList *TempParams = D->getTemplateParameters();
279 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
280 if (!InstParams)
281 return 0;
282
283 TypeAliasDecl *Pattern = D->getTemplatedDecl();
284
285 TypeAliasTemplateDecl *PrevAliasTemplate = 0;
Douglas Gregoref96ee02012-01-14 16:38:05 +0000286 if (Pattern->getPreviousDecl()) {
Richard Smith3e4c6c42011-05-05 21:57:07 +0000287 DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
David Blaikie3bc93e32012-12-19 00:45:41 +0000288 if (!Found.empty()) {
289 PrevAliasTemplate = dyn_cast<TypeAliasTemplateDecl>(Found.front());
Richard Smith3e4c6c42011-05-05 21:57:07 +0000290 }
291 }
292
293 TypeAliasDecl *AliasInst = cast_or_null<TypeAliasDecl>(
294 InstantiateTypedefNameDecl(Pattern, /*IsTypeAlias=*/true));
295 if (!AliasInst)
296 return 0;
297
298 TypeAliasTemplateDecl *Inst
299 = TypeAliasTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
300 D->getDeclName(), InstParams, AliasInst);
301 if (PrevAliasTemplate)
302 Inst->setPreviousDeclaration(PrevAliasTemplate);
303
304 Inst->setAccess(D->getAccess());
305
306 if (!PrevAliasTemplate)
307 Inst->setInstantiatedFromMemberTemplate(D);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000308
Richard Smith3e4c6c42011-05-05 21:57:07 +0000309 Owner->addDecl(Inst);
310
311 return Inst;
Richard Smith162e1c12011-04-15 14:24:37 +0000312}
313
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000314Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
Douglas Gregor9901c572010-05-21 00:31:19 +0000315 // If this is the variable for an anonymous struct or union,
316 // instantiate the anonymous struct/union type first.
317 if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
318 if (RecordTy->getDecl()->isAnonymousStructOrUnion())
319 if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
320 return 0;
321
John McCallce3ff2b2009-08-25 22:02:44 +0000322 // Do substitution on the type of the declaration
John McCalla93c9342009-12-07 02:54:59 +0000323 TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
John McCall0a5fa062009-10-21 02:39:02 +0000324 TemplateArgs,
325 D->getTypeSpecStartLoc(),
326 D->getDeclName());
327 if (!DI)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000328 return 0;
329
Douglas Gregorc6dbc3f2010-09-12 07:37:24 +0000330 if (DI->getType()->isFunctionType()) {
331 SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function)
332 << D->isStaticDataMember() << DI->getType();
333 return 0;
334 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000335
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000336 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000337 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000338 D->getInnerLocStart(),
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000339 D->getLocation(), D->getIdentifier(),
John McCall0a5fa062009-10-21 02:39:02 +0000340 DI->getType(), DI,
Rafael Espindolad2615cc2013-04-03 19:27:57 +0000341 D->getStorageClass());
Richard Smith38afbc72013-04-13 02:43:54 +0000342 Var->setTLSKind(D->getTLSKind());
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000343 Var->setInitStyle(D->getInitStyle());
Richard Smithad762fc2011-04-14 22:09:26 +0000344 Var->setCXXForRangeDecl(D->isCXXForRangeDecl());
Richard Smith796c1a12012-01-19 22:46:17 +0000345 Var->setConstexpr(D->isConstexpr());
Mike Stump1eb44332009-09-09 15:08:12 +0000346
John McCallb6217662010-03-15 10:12:16 +0000347 // Substitute the nested name specifier, if any.
348 if (SubstQualifier(D, Var))
349 return 0;
350
Mike Stump1eb44332009-09-09 15:08:12 +0000351 // If we are instantiating a static data member defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000352 // out-of-line, the instantiation will have the same lexical
353 // context (which will be a namespace scope) as the template.
354 if (D->isOutOfLine())
355 Var->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000356
John McCall46460a62010-01-20 21:53:11 +0000357 Var->setAccess(D->getAccess());
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000358
Argyrios Kyrtzidis6b6b42a2011-04-19 19:51:10 +0000359 if (!D->isStaticDataMember()) {
Douglas Gregorc070cc62010-06-17 23:14:26 +0000360 Var->setUsed(D->isUsed(false));
Argyrios Kyrtzidis6b6b42a2011-04-19 19:51:10 +0000361 Var->setReferenced(D->isReferenced());
362 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000363
Richard Smith671b3212013-02-22 04:55:39 +0000364 SemaRef.InstantiateAttrs(TemplateArgs, D, Var, LateAttrs, StartingScope);
365
366 if (Var->hasAttrs())
367 SemaRef.CheckAlignasUnderalignment(Var);
368
Mike Stump390b4cc2009-05-16 07:39:55 +0000369 // FIXME: In theory, we could have a previous declaration for variables that
370 // are not static data members.
John McCall68263142009-11-18 22:49:29 +0000371 // FIXME: having to fake up a LookupResult is dumb.
372 LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
Douglas Gregor449d0a82010-03-01 19:11:54 +0000373 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Douglas Gregor60c93c92010-02-09 07:26:29 +0000374 if (D->isStaticDataMember())
375 SemaRef.LookupQualifiedName(Previous, Owner, false);
Douglas Gregor9aab9c42011-12-10 01:22:52 +0000376
377 // In ARC, infer 'retaining' for variables of retainable type.
David Blaikie4e4d0842012-03-11 07:00:24 +0000378 if (SemaRef.getLangOpts().ObjCAutoRefCount &&
Douglas Gregor9aab9c42011-12-10 01:22:52 +0000379 SemaRef.inferObjCARCLifetime(Var))
380 Var->setInvalidDecl();
381
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +0000382 SemaRef.CheckVariableDeclaration(Var, Previous);
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Douglas Gregor7caa6822009-07-24 20:34:43 +0000384 if (D->isOutOfLine()) {
Richard Smith3e9ea0b2011-12-21 00:25:33 +0000385 D->getLexicalDeclContext()->addDecl(Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000386 Owner->makeDeclVisibleInContext(Var);
387 } else {
388 Owner->addDecl(Var);
Douglas Gregorf7d72f52010-05-03 20:22:41 +0000389 if (Owner->isFunctionOrMethod())
390 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000391 }
Richard Smithbe507b62013-02-01 08:12:08 +0000392
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000393 // Link instantiations of static data members back to the template from
394 // which they were instantiated.
395 if (Var->isStaticDataMember())
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000396 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000397 TSK_ImplicitInstantiation);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000398
Douglas Gregor60c93c92010-02-09 07:26:29 +0000399 if (Var->getAnyInitializer()) {
400 // We already have an initializer in the class.
401 } else if (D->getInit()) {
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000402 if (Var->isStaticDataMember() && !D->isOutOfLine())
Richard Smithadb1d4c2012-07-22 23:45:10 +0000403 SemaRef.PushExpressionEvaluationContext(Sema::ConstantEvaluated, D);
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000404 else
Richard Smithadb1d4c2012-07-22 23:45:10 +0000405 SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated, D);
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000406
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000407 // Instantiate the initializer.
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000408 ExprResult Init = SemaRef.SubstInitializer(D->getInit(), TemplateArgs,
409 D->getInitStyle() == VarDecl::CallInit);
410 if (!Init.isInvalid()) {
Richard Smith34b41d92011-02-20 03:19:35 +0000411 bool TypeMayContainAuto = true;
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000412 if (Init.get()) {
413 bool DirectInit = D->isDirectInit();
414 SemaRef.AddInitializerToDecl(Var, Init.take(), DirectInit,
415 TypeMayContainAuto);
416 } else
Eli Friedman6aeaa602012-01-05 22:34:08 +0000417 SemaRef.ActOnUninitializedDecl(Var, TypeMayContainAuto);
Douglas Gregor6eef5192009-12-14 19:27:10 +0000418 } else {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000419 // FIXME: Not too happy about invalidating the declaration
420 // because of a bogus initializer.
421 Var->setInvalidDecl();
Douglas Gregor6eef5192009-12-14 19:27:10 +0000422 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000423
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000424 SemaRef.PopExpressionEvaluationContext();
Richard Smithad762fc2011-04-14 22:09:26 +0000425 } else if ((!Var->isStaticDataMember() || Var->isOutOfLine()) &&
426 !Var->isCXXForRangeDecl())
John McCalld226f652010-08-21 09:40:31 +0000427 SemaRef.ActOnUninitializedDecl(Var, false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000428
Richard Smithe3499ca2011-06-21 23:42:09 +0000429 // Diagnose unused local variables with dependent types, where the diagnostic
430 // will have been deferred.
431 if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed() &&
432 D->getType()->isDependentType())
Douglas Gregor5764f612010-05-08 23:05:03 +0000433 SemaRef.DiagnoseUnusedDecl(Var);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +0000434
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000435 return Var;
436}
437
Abramo Bagnara6206d532010-06-05 05:09:32 +0000438Decl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
439 AccessSpecDecl* AD
440 = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
441 D->getAccessSpecifierLoc(), D->getColonLoc());
442 Owner->addHiddenDecl(AD);
443 return AD;
444}
445
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000446Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
447 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000448 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor561f8122011-07-01 01:22:09 +0000449 if (DI->getType()->isInstantiationDependentType() ||
Douglas Gregor836adf62010-05-24 17:22:01 +0000450 DI->getType()->isVariablyModifiedType()) {
John McCall07fb6be2009-10-22 23:33:21 +0000451 DI = SemaRef.SubstType(DI, TemplateArgs,
452 D->getLocation(), D->getDeclName());
453 if (!DI) {
John McCalla93c9342009-12-07 02:54:59 +0000454 DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000455 Invalid = true;
456 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000457 // C++ [temp.arg.type]p3:
458 // If a declaration acquires a function type through a type
459 // dependent on a template-parameter and this causes a
460 // declaration that does not use the syntactic form of a
461 // function declarator to have function type, the program is
462 // ill-formed.
463 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000464 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000465 Invalid = true;
466 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000467 } else {
468 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000469 }
470
471 Expr *BitWidth = D->getBitWidth();
472 if (Invalid)
473 BitWidth = 0;
474 else if (BitWidth) {
Richard Smithf6702a32011-12-20 02:08:33 +0000475 // The bit-width expression is a constant expression.
476 EnterExpressionEvaluationContext Unevaluated(SemaRef,
477 Sema::ConstantEvaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000478
John McCall60d7b3a2010-08-24 06:29:42 +0000479 ExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000480 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000481 if (InstantiatedBitWidth.isInvalid()) {
482 Invalid = true;
483 BitWidth = 0;
484 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000485 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000486 }
487
John McCall07fb6be2009-10-22 23:33:21 +0000488 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
489 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000490 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000491 D->getLocation(),
492 D->isMutable(),
493 BitWidth,
Richard Smithca523302012-06-10 03:12:00 +0000494 D->getInClassInitStyle(),
Richard Smith703b6012012-05-23 04:22:22 +0000495 D->getInnerLocStart(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000496 D->getAccess(),
497 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000498 if (!Field) {
499 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000500 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000501 }
Mike Stump1eb44332009-09-09 15:08:12 +0000502
DeLesley Hutchins23323e02012-01-20 22:50:54 +0000503 SemaRef.InstantiateAttrs(TemplateArgs, D, Field, LateAttrs, StartingScope);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000504
Richard Smithbe507b62013-02-01 08:12:08 +0000505 if (Field->hasAttrs())
506 SemaRef.CheckAlignasUnderalignment(Field);
507
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000508 if (Invalid)
509 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000510
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000511 if (!Field->getDeclName()) {
512 // Keep track of where this decl came from.
513 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000514 }
Douglas Gregor9901c572010-05-21 00:31:19 +0000515 if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
516 if (Parent->isAnonymousStructOrUnion() &&
Sebastian Redl7a126a42010-08-31 00:36:30 +0000517 Parent->getRedeclContext()->isFunctionOrMethod())
Douglas Gregor9901c572010-05-21 00:31:19 +0000518 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000519 }
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000521 Field->setImplicit(D->isImplicit());
John McCall46460a62010-01-20 21:53:11 +0000522 Field->setAccess(D->getAccess());
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000523 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000524
525 return Field;
526}
527
Francois Pichet87c2e122010-11-21 06:08:52 +0000528Decl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
529 NamedDecl **NamedChain =
530 new (SemaRef.Context)NamedDecl*[D->getChainingSize()];
531
532 int i = 0;
533 for (IndirectFieldDecl::chain_iterator PI =
534 D->chain_begin(), PE = D->chain_end();
Douglas Gregorb7107222011-03-04 19:46:35 +0000535 PI != PE; ++PI) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000536 NamedDecl *Next = SemaRef.FindInstantiatedDecl(D->getLocation(), *PI,
Douglas Gregorb7107222011-03-04 19:46:35 +0000537 TemplateArgs);
538 if (!Next)
539 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000540
Douglas Gregorb7107222011-03-04 19:46:35 +0000541 NamedChain[i++] = Next;
542 }
Francois Pichet87c2e122010-11-21 06:08:52 +0000543
Francois Pichet40e17752010-12-09 10:07:54 +0000544 QualType T = cast<FieldDecl>(NamedChain[i-1])->getType();
Francois Pichet87c2e122010-11-21 06:08:52 +0000545 IndirectFieldDecl* IndirectField
546 = IndirectFieldDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Francois Pichet40e17752010-12-09 10:07:54 +0000547 D->getIdentifier(), T,
Francois Pichet87c2e122010-11-21 06:08:52 +0000548 NamedChain, D->getChainingSize());
549
550
551 IndirectField->setImplicit(D->isImplicit());
552 IndirectField->setAccess(D->getAccess());
553 Owner->addDecl(IndirectField);
554 return IndirectField;
555}
556
John McCall02cace72009-08-28 07:59:38 +0000557Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
John McCall02cace72009-08-28 07:59:38 +0000558 // Handle friend type expressions by simply substituting template
Douglas Gregor06245bf2010-04-07 17:57:12 +0000559 // parameters into the pattern type and checking the result.
John McCall32f2fb52010-03-25 18:04:51 +0000560 if (TypeSourceInfo *Ty = D->getFriendType()) {
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000561 TypeSourceInfo *InstTy;
562 // If this is an unsupported friend, don't bother substituting template
563 // arguments into it. The actual type referred to won't be used by any
564 // parts of Clang, and may not be valid for instantiating. Just use the
565 // same info for the instantiated friend.
566 if (D->isUnsupportedFriend()) {
567 InstTy = Ty;
568 } else {
569 InstTy = SemaRef.SubstType(Ty, TemplateArgs,
570 D->getLocation(), DeclarationName());
571 }
572 if (!InstTy)
Douglas Gregor7557a132009-12-24 20:56:24 +0000573 return 0;
John McCall02cace72009-08-28 07:59:38 +0000574
Richard Smithd6f80da2012-09-20 01:31:00 +0000575 FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getLocStart(),
Abramo Bagnara0216df82011-10-29 20:52:52 +0000576 D->getFriendLoc(), InstTy);
Douglas Gregor06245bf2010-04-07 17:57:12 +0000577 if (!FD)
578 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000579
Douglas Gregor06245bf2010-04-07 17:57:12 +0000580 FD->setAccess(AS_public);
John McCall9a34edb2010-10-19 01:40:49 +0000581 FD->setUnsupportedFriend(D->isUnsupportedFriend());
Douglas Gregor06245bf2010-04-07 17:57:12 +0000582 Owner->addDecl(FD);
583 return FD;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000584 }
585
Douglas Gregor06245bf2010-04-07 17:57:12 +0000586 NamedDecl *ND = D->getFriendDecl();
587 assert(ND && "friend decl must be a decl or a type!");
588
John McCallaf2094e2010-04-08 09:05:18 +0000589 // All of the Visit implementations for the various potential friend
590 // declarations have to be carefully written to work for friend
591 // objects, with the most important detail being that the target
592 // decl should almost certainly not be placed in Owner.
593 Decl *NewND = Visit(ND);
Douglas Gregor06245bf2010-04-07 17:57:12 +0000594 if (!NewND) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000595
John McCall02cace72009-08-28 07:59:38 +0000596 FriendDecl *FD =
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000597 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor06245bf2010-04-07 17:57:12 +0000598 cast<NamedDecl>(NewND), D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000599 FD->setAccess(AS_public);
John McCall9a34edb2010-10-19 01:40:49 +0000600 FD->setUnsupportedFriend(D->isUnsupportedFriend());
John McCall02cace72009-08-28 07:59:38 +0000601 Owner->addDecl(FD);
602 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000603}
604
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000605Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
606 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000607
Richard Smithf6702a32011-12-20 02:08:33 +0000608 // The expression in a static assertion is a constant expression.
609 EnterExpressionEvaluationContext Unevaluated(SemaRef,
610 Sema::ConstantEvaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000611
John McCall60d7b3a2010-08-24 06:29:42 +0000612 ExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000613 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000614 if (InstantiatedAssertExpr.isInvalid())
615 return 0;
616
Richard Smithe3f470a2012-07-11 22:37:56 +0000617 return SemaRef.BuildStaticAssertDeclaration(D->getLocation(),
John McCall9ae2f072010-08-23 23:25:46 +0000618 InstantiatedAssertExpr.get(),
Richard Smithe3f470a2012-07-11 22:37:56 +0000619 D->getMessage(),
620 D->getRParenLoc(),
621 D->isFailed());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000622}
623
624Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Richard Smith38f0df32012-03-26 04:58:10 +0000625 EnumDecl *PrevDecl = 0;
626 if (D->getPreviousDecl()) {
627 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
628 D->getPreviousDecl(),
629 TemplateArgs);
630 if (!Prev) return 0;
631 PrevDecl = cast<EnumDecl>(Prev);
632 }
633
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000634 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000635 D->getLocation(), D->getIdentifier(),
Richard Smith38f0df32012-03-26 04:58:10 +0000636 PrevDecl, D->isScoped(),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +0000637 D->isScopedUsingClassTag(), D->isFixed());
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000638 if (D->isFixed()) {
Richard Smithf1c66b42012-03-14 23:13:10 +0000639 if (TypeSourceInfo *TI = D->getIntegerTypeSourceInfo()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000640 // If we have type source information for the underlying type, it means it
641 // has been explicitly set by the user. Perform substitution on it before
642 // moving on.
643 SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
Richard Smithf1c66b42012-03-14 23:13:10 +0000644 TypeSourceInfo *NewTI = SemaRef.SubstType(TI, TemplateArgs, UnderlyingLoc,
645 DeclarationName());
646 if (!NewTI || SemaRef.CheckEnumUnderlyingType(NewTI))
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000647 Enum->setIntegerType(SemaRef.Context.IntTy);
Richard Smithf1c66b42012-03-14 23:13:10 +0000648 else
649 Enum->setIntegerTypeSourceInfo(NewTI);
650 } else {
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000651 assert(!D->getIntegerType()->isDependentType()
652 && "Dependent type without type source info");
653 Enum->setIntegerType(D->getIntegerType());
654 }
655 }
656
John McCall5b629aa2010-10-22 23:36:17 +0000657 SemaRef.InstantiateAttrs(TemplateArgs, D, Enum);
658
Richard Smithf1c66b42012-03-14 23:13:10 +0000659 Enum->setInstantiationOfMemberEnum(D, TSK_ImplicitInstantiation);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000660 Enum->setAccess(D->getAccess());
John McCallb6217662010-03-15 10:12:16 +0000661 if (SubstQualifier(D, Enum)) return 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000662 Owner->addDecl(Enum);
Richard Smithf1c66b42012-03-14 23:13:10 +0000663
Richard Smith4ca93d92012-03-26 04:08:46 +0000664 EnumDecl *Def = D->getDefinition();
665 if (Def && Def != D) {
666 // If this is an out-of-line definition of an enum member template, check
667 // that the underlying types match in the instantiation of both
668 // declarations.
669 if (TypeSourceInfo *TI = Def->getIntegerTypeSourceInfo()) {
670 SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
671 QualType DefnUnderlying =
672 SemaRef.SubstType(TI->getType(), TemplateArgs,
673 UnderlyingLoc, DeclarationName());
674 SemaRef.CheckEnumRedeclaration(Def->getLocation(), Def->isScoped(),
675 DefnUnderlying, Enum);
676 }
677 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000678
Douglas Gregor96084f12010-03-01 19:00:07 +0000679 if (D->getDeclContext()->isFunctionOrMethod())
680 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000681
Richard Smithf1c66b42012-03-14 23:13:10 +0000682 // C++11 [temp.inst]p1: The implicit instantiation of a class template
683 // specialization causes the implicit instantiation of the declarations, but
684 // not the definitions of scoped member enumerations.
685 // FIXME: There appears to be no wording for what happens for an enum defined
Richard Smith38f0df32012-03-26 04:58:10 +0000686 // within a block scope, but we treat that much like a member template. Only
687 // instantiate the definition when visiting the definition in that case, since
688 // we will visit all redeclarations.
689 if (!Enum->isScoped() && Def &&
690 (!D->getDeclContext()->isFunctionOrMethod() || D->isCompleteDefinition()))
Richard Smith4ca93d92012-03-26 04:08:46 +0000691 InstantiateEnumDefinition(Enum, Def);
Richard Smithf1c66b42012-03-14 23:13:10 +0000692
693 return Enum;
694}
695
696void TemplateDeclInstantiator::InstantiateEnumDefinition(
697 EnumDecl *Enum, EnumDecl *Pattern) {
698 Enum->startDefinition();
699
Richard Smith1af83c42012-03-23 03:33:32 +0000700 // Update the location to refer to the definition.
701 Enum->setLocation(Pattern->getLocation());
702
Chris Lattner5f9e2722011-07-23 10:55:15 +0000703 SmallVector<Decl*, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000704
705 EnumConstantDecl *LastEnumConst = 0;
Richard Smithf1c66b42012-03-14 23:13:10 +0000706 for (EnumDecl::enumerator_iterator EC = Pattern->enumerator_begin(),
707 ECEnd = Pattern->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000708 EC != ECEnd; ++EC) {
709 // The specified value for the enumerator.
John McCall60d7b3a2010-08-24 06:29:42 +0000710 ExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000711 if (Expr *UninstValue = EC->getInitExpr()) {
Richard Smithf6702a32011-12-20 02:08:33 +0000712 // The enumerator's value expression is a constant expression.
Mike Stump1eb44332009-09-09 15:08:12 +0000713 EnterExpressionEvaluationContext Unevaluated(SemaRef,
Richard Smithf6702a32011-12-20 02:08:33 +0000714 Sema::ConstantEvaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000715
John McCallce3ff2b2009-08-25 22:02:44 +0000716 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000717 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000718
719 // Drop the initial value and continue.
720 bool isInvalid = false;
721 if (Value.isInvalid()) {
722 Value = SemaRef.Owned((Expr *)0);
723 isInvalid = true;
724 }
725
Mike Stump1eb44332009-09-09 15:08:12 +0000726 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000727 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
728 EC->getLocation(), EC->getIdentifier(),
John McCall9ae2f072010-08-23 23:25:46 +0000729 Value.get());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000730
731 if (isInvalid) {
732 if (EnumConst)
733 EnumConst->setInvalidDecl();
734 Enum->setInvalidDecl();
735 }
736
737 if (EnumConst) {
David Blaikie581deb32012-06-06 20:45:41 +0000738 SemaRef.InstantiateAttrs(TemplateArgs, *EC, EnumConst);
John McCall5b629aa2010-10-22 23:36:17 +0000739
John McCall3b85ecf2010-01-23 22:37:59 +0000740 EnumConst->setAccess(Enum->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000741 Enum->addDecl(EnumConst);
John McCalld226f652010-08-21 09:40:31 +0000742 Enumerators.push_back(EnumConst);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000743 LastEnumConst = EnumConst;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000744
Richard Smithf1c66b42012-03-14 23:13:10 +0000745 if (Pattern->getDeclContext()->isFunctionOrMethod() &&
746 !Enum->isScoped()) {
Douglas Gregor96084f12010-03-01 19:00:07 +0000747 // If the enumeration is within a function or method, record the enum
748 // constant as a local.
David Blaikie581deb32012-06-06 20:45:41 +0000749 SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
Douglas Gregor96084f12010-03-01 19:00:07 +0000750 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000751 }
752 }
Mike Stump1eb44332009-09-09 15:08:12 +0000753
Richard Smithf1c66b42012-03-14 23:13:10 +0000754 // FIXME: Fixup LBraceLoc
755 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(),
756 Enum->getRBraceLoc(), Enum,
Eli Friedmande7a0fc2010-08-15 02:27:09 +0000757 Enumerators.data(), Enumerators.size(),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000758 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000759}
760
Douglas Gregor6477b692009-03-25 15:04:13 +0000761Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000762 llvm_unreachable("EnumConstantDecls can only occur within EnumDecls.");
Douglas Gregor6477b692009-03-25 15:04:13 +0000763}
764
John McCalle29ba202009-08-20 01:44:21 +0000765Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
John McCall93ba8572010-03-25 06:39:04 +0000766 bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
767
Douglas Gregor550d9b22009-10-31 17:21:17 +0000768 // Create a local instantiation scope for this class template, which
769 // will contain the instantiations of the template parameters.
John McCall2a7fb272010-08-25 05:32:35 +0000770 LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000771 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000772 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000773 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000774 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000775
776 CXXRecordDecl *Pattern = D->getTemplatedDecl();
John McCall93ba8572010-03-25 06:39:04 +0000777
778 // Instantiate the qualifier. We have to do this first in case
779 // we're a friend declaration, because if we are then we need to put
780 // the new declaration in the appropriate context.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000781 NestedNameSpecifierLoc QualifierLoc = Pattern->getQualifierLoc();
782 if (QualifierLoc) {
783 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
784 TemplateArgs);
785 if (!QualifierLoc)
786 return 0;
John McCall93ba8572010-03-25 06:39:04 +0000787 }
788
789 CXXRecordDecl *PrevDecl = 0;
790 ClassTemplateDecl *PrevClassTemplate = 0;
791
Douglas Gregoref96ee02012-01-14 16:38:05 +0000792 if (!isFriend && Pattern->getPreviousDecl()) {
Nick Lewycky37574f52010-11-08 23:29:42 +0000793 DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
David Blaikie3bc93e32012-12-19 00:45:41 +0000794 if (!Found.empty()) {
795 PrevClassTemplate = dyn_cast<ClassTemplateDecl>(Found.front());
Nick Lewycky37574f52010-11-08 23:29:42 +0000796 if (PrevClassTemplate)
797 PrevDecl = PrevClassTemplate->getTemplatedDecl();
798 }
799 }
800
John McCall93ba8572010-03-25 06:39:04 +0000801 // If this isn't a friend, then it's a member template, in which
802 // case we just want to build the instantiation in the
803 // specialization. If it is a friend, we want to build it in
804 // the appropriate context.
805 DeclContext *DC = Owner;
806 if (isFriend) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000807 if (QualifierLoc) {
John McCall93ba8572010-03-25 06:39:04 +0000808 CXXScopeSpec SS;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000809 SS.Adopt(QualifierLoc);
John McCall93ba8572010-03-25 06:39:04 +0000810 DC = SemaRef.computeDeclContext(SS);
811 if (!DC) return 0;
812 } else {
813 DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
814 Pattern->getDeclContext(),
815 TemplateArgs);
816 }
817
818 // Look for a previous declaration of the template in the owning
819 // context.
820 LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
821 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
822 SemaRef.LookupQualifiedName(R, DC);
823
824 if (R.isSingleResult()) {
825 PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
826 if (PrevClassTemplate)
827 PrevDecl = PrevClassTemplate->getTemplatedDecl();
828 }
829
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000830 if (!PrevClassTemplate && QualifierLoc) {
John McCall93ba8572010-03-25 06:39:04 +0000831 SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
Douglas Gregor1eabb7d2010-03-31 23:17:41 +0000832 << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000833 << QualifierLoc.getSourceRange();
John McCall93ba8572010-03-25 06:39:04 +0000834 return 0;
835 }
836
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000837 bool AdoptedPreviousTemplateParams = false;
John McCall93ba8572010-03-25 06:39:04 +0000838 if (PrevClassTemplate) {
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000839 bool Complain = true;
840
841 // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
842 // template for struct std::tr1::__detail::_Map_base, where the
843 // template parameters of the friend declaration don't match the
844 // template parameters of the original declaration. In this one
845 // case, we don't complain about the ill-formed friend
846 // declaration.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000847 if (isFriend && Pattern->getIdentifier() &&
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000848 Pattern->getIdentifier()->isStr("_Map_base") &&
849 DC->isNamespace() &&
850 cast<NamespaceDecl>(DC)->getIdentifier() &&
851 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
852 DeclContext *DCParent = DC->getParent();
853 if (DCParent->isNamespace() &&
854 cast<NamespaceDecl>(DCParent)->getIdentifier() &&
855 cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
856 DeclContext *DCParent2 = DCParent->getParent();
857 if (DCParent2->isNamespace() &&
858 cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
859 cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
860 DCParent2->getParent()->isTranslationUnit())
861 Complain = false;
862 }
863 }
864
John McCall93ba8572010-03-25 06:39:04 +0000865 TemplateParameterList *PrevParams
866 = PrevClassTemplate->getTemplateParameters();
867
868 // Make sure the parameter lists match.
869 if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000870 Complain,
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000871 Sema::TPL_TemplateMatch)) {
872 if (Complain)
873 return 0;
874
875 AdoptedPreviousTemplateParams = true;
876 InstParams = PrevParams;
877 }
John McCall93ba8572010-03-25 06:39:04 +0000878
879 // Do some additional validation, then merge default arguments
880 // from the existing declarations.
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000881 if (!AdoptedPreviousTemplateParams &&
882 SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
John McCall93ba8572010-03-25 06:39:04 +0000883 Sema::TPC_ClassTemplate))
884 return 0;
885 }
886 }
887
John McCalle29ba202009-08-20 01:44:21 +0000888 CXXRecordDecl *RecordInst
John McCall93ba8572010-03-25 06:39:04 +0000889 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000890 Pattern->getLocStart(), Pattern->getLocation(),
891 Pattern->getIdentifier(), PrevDecl,
Douglas Gregorf0510d42009-10-12 23:11:44 +0000892 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000893
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000894 if (QualifierLoc)
895 RecordInst->setQualifierInfo(QualifierLoc);
John McCallb6217662010-03-15 10:12:16 +0000896
John McCalle29ba202009-08-20 01:44:21 +0000897 ClassTemplateDecl *Inst
John McCall93ba8572010-03-25 06:39:04 +0000898 = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
899 D->getIdentifier(), InstParams, RecordInst,
900 PrevClassTemplate);
John McCalle29ba202009-08-20 01:44:21 +0000901 RecordInst->setDescribedClassTemplate(Inst);
John McCallea7390c2010-04-08 20:25:50 +0000902
John McCall93ba8572010-03-25 06:39:04 +0000903 if (isFriend) {
John McCallea7390c2010-04-08 20:25:50 +0000904 if (PrevClassTemplate)
905 Inst->setAccess(PrevClassTemplate->getAccess());
906 else
907 Inst->setAccess(D->getAccess());
908
John McCall93ba8572010-03-25 06:39:04 +0000909 Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
910 // TODO: do we want to track the instantiation progeny of this
911 // friend target decl?
912 } else {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000913 Inst->setAccess(D->getAccess());
Nick Lewycky37574f52010-11-08 23:29:42 +0000914 if (!PrevClassTemplate)
915 Inst->setInstantiatedFromMemberTemplate(D);
John McCall93ba8572010-03-25 06:39:04 +0000916 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000917
Douglas Gregorf0510d42009-10-12 23:11:44 +0000918 // Trigger creation of the type for the instantiation.
John McCall3cb0ebd2010-03-10 03:28:59 +0000919 SemaRef.Context.getInjectedClassNameType(RecordInst,
Douglas Gregor24bae922010-07-08 18:37:38 +0000920 Inst->getInjectedClassNameSpecialization());
John McCallea7390c2010-04-08 20:25:50 +0000921
Douglas Gregor259571e2009-10-30 22:42:42 +0000922 // Finish handling of friends.
John McCall93ba8572010-03-25 06:39:04 +0000923 if (isFriend) {
Richard Smith1b7f9cb2012-03-13 03:12:56 +0000924 DC->makeDeclVisibleInContext(Inst);
Abramo Bagnara4c515482011-11-26 13:33:46 +0000925 Inst->setLexicalDeclContext(Owner);
926 RecordInst->setLexicalDeclContext(Owner);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000927 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000928 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000929
Abramo Bagnara4c515482011-11-26 13:33:46 +0000930 if (D->isOutOfLine()) {
931 Inst->setLexicalDeclContext(D->getLexicalDeclContext());
932 RecordInst->setLexicalDeclContext(D->getLexicalDeclContext());
933 }
934
John McCalle29ba202009-08-20 01:44:21 +0000935 Owner->addDecl(Inst);
Douglas Gregord65587f2010-11-10 19:44:59 +0000936
937 if (!PrevClassTemplate) {
938 // Queue up any out-of-line partial specializations of this member
939 // class template; the client will force their instantiation once
940 // the enclosing class has been instantiated.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000941 SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
Douglas Gregord65587f2010-11-10 19:44:59 +0000942 D->getPartialSpecializations(PartialSpecs);
943 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
944 if (PartialSpecs[I]->isOutOfLine())
945 OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I]));
946 }
947
John McCalle29ba202009-08-20 01:44:21 +0000948 return Inst;
949}
950
Douglas Gregord60e1052009-08-27 16:57:43 +0000951Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000952TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
953 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000954 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000955
Douglas Gregored9c0f92009-10-29 00:04:11 +0000956 // Lookup the already-instantiated declaration in the instantiation
957 // of the class template and return that.
958 DeclContext::lookup_result Found
959 = Owner->lookup(ClassTemplate->getDeclName());
David Blaikie3bc93e32012-12-19 00:45:41 +0000960 if (Found.empty())
Douglas Gregored9c0f92009-10-29 00:04:11 +0000961 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000962
Douglas Gregored9c0f92009-10-29 00:04:11 +0000963 ClassTemplateDecl *InstClassTemplate
David Blaikie3bc93e32012-12-19 00:45:41 +0000964 = dyn_cast<ClassTemplateDecl>(Found.front());
Douglas Gregored9c0f92009-10-29 00:04:11 +0000965 if (!InstClassTemplate)
966 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000967
Douglas Gregord65587f2010-11-10 19:44:59 +0000968 if (ClassTemplatePartialSpecializationDecl *Result
969 = InstClassTemplate->findPartialSpecInstantiatedFromMember(D))
970 return Result;
971
972 return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D);
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000973}
974
975Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000976TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000977 // Create a local instantiation scope for this function template, which
978 // will contain the instantiations of the template parameters and then get
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000979 // merged with the local instantiation scope for the function template
Douglas Gregor550d9b22009-10-31 17:21:17 +0000980 // itself.
John McCall2a7fb272010-08-25 05:32:35 +0000981 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor895162d2010-04-30 18:55:50 +0000982
Douglas Gregord60e1052009-08-27 16:57:43 +0000983 TemplateParameterList *TempParams = D->getTemplateParameters();
984 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000985 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000986 return NULL;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000987
Douglas Gregora735b202009-10-13 14:39:41 +0000988 FunctionDecl *Instantiated = 0;
989 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000990 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
Douglas Gregora735b202009-10-13 14:39:41 +0000991 InstParams));
992 else
993 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000994 D->getTemplatedDecl(),
Douglas Gregora735b202009-10-13 14:39:41 +0000995 InstParams));
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000996
Douglas Gregora735b202009-10-13 14:39:41 +0000997 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000998 return 0;
999
Mike Stump1eb44332009-09-09 15:08:12 +00001000 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +00001001 // template from which it was instantiated.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001002 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +00001003 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +00001004 InstTemplate->setAccess(D->getAccess());
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001005 assert(InstTemplate &&
Douglas Gregora735b202009-10-13 14:39:41 +00001006 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
John McCalle976ffe2009-12-14 23:19:40 +00001007
John McCallb1a56e72010-03-26 23:10:15 +00001008 bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
1009
John McCalle976ffe2009-12-14 23:19:40 +00001010 // Link the instantiation back to the pattern *unless* this is a
1011 // non-definition friend declaration.
1012 if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
John McCallb1a56e72010-03-26 23:10:15 +00001013 !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
Douglas Gregora735b202009-10-13 14:39:41 +00001014 InstTemplate->setInstantiatedFromMemberTemplate(D);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001015
John McCallb1a56e72010-03-26 23:10:15 +00001016 // Make declarations visible in the appropriate context.
John McCall1f2e1a92012-08-10 03:15:35 +00001017 if (!isFriend) {
Douglas Gregora735b202009-10-13 14:39:41 +00001018 Owner->addDecl(InstTemplate);
John McCall1f2e1a92012-08-10 03:15:35 +00001019 } else if (InstTemplate->getDeclContext()->isRecord() &&
1020 !D->getPreviousDecl()) {
1021 SemaRef.CheckFriendAccess(InstTemplate);
1022 }
John McCallb1a56e72010-03-26 23:10:15 +00001023
Douglas Gregord60e1052009-08-27 16:57:43 +00001024 return InstTemplate;
1025}
1026
Douglas Gregord475b8d2009-03-25 21:17:03 +00001027Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
1028 CXXRecordDecl *PrevDecl = 0;
1029 if (D->isInjectedClassName())
1030 PrevDecl = cast<CXXRecordDecl>(Owner);
Douglas Gregoref96ee02012-01-14 16:38:05 +00001031 else if (D->getPreviousDecl()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001032 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
Douglas Gregoref96ee02012-01-14 16:38:05 +00001033 D->getPreviousDecl(),
John McCall6c1c1b82009-12-15 22:29:06 +00001034 TemplateArgs);
1035 if (!Prev) return 0;
1036 PrevDecl = cast<CXXRecordDecl>(Prev);
1037 }
Douglas Gregord475b8d2009-03-25 21:17:03 +00001038
1039 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +00001040 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00001041 D->getLocStart(), D->getLocation(),
1042 D->getIdentifier(), PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00001043
1044 // Substitute the nested name specifier, if any.
1045 if (SubstQualifier(D, Record))
1046 return 0;
1047
Douglas Gregord475b8d2009-03-25 21:17:03 +00001048 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +00001049 // FIXME: Check against AS_none is an ugly hack to work around the issue that
1050 // the tag decls introduced by friend class declarations don't have an access
1051 // specifier. Remove once this area of the code gets sorted out.
1052 if (D->getAccess() != AS_none)
1053 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +00001054 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001055 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001056
John McCall02cace72009-08-28 07:59:38 +00001057 // If the original function was part of a friend declaration,
1058 // inherit its namespace state.
1059 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
1060 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
1061
Douglas Gregor9901c572010-05-21 00:31:19 +00001062 // Make sure that anonymous structs and unions are recorded.
1063 if (D->isAnonymousStructOrUnion()) {
1064 Record->setAnonymousStructOrUnion(true);
Sebastian Redl7a126a42010-08-31 00:36:30 +00001065 if (Record->getDeclContext()->getRedeclContext()->isFunctionOrMethod())
Douglas Gregor9901c572010-05-21 00:31:19 +00001066 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
1067 }
Anders Carlssond8b285f2009-09-01 04:26:58 +00001068
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001069 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001070 return Record;
1071}
1072
Douglas Gregor71074fd2012-09-13 21:56:43 +00001073/// \brief Adjust the given function type for an instantiation of the
1074/// given declaration, to cope with modifications to the function's type that
1075/// aren't reflected in the type-source information.
1076///
1077/// \param D The declaration we're instantiating.
1078/// \param TInfo The already-instantiated type.
1079static QualType adjustFunctionTypeForInstantiation(ASTContext &Context,
1080 FunctionDecl *D,
1081 TypeSourceInfo *TInfo) {
Douglas Gregorbed51fe2012-09-13 22:01:49 +00001082 const FunctionProtoType *OrigFunc
1083 = D->getType()->castAs<FunctionProtoType>();
1084 const FunctionProtoType *NewFunc
1085 = TInfo->getType()->castAs<FunctionProtoType>();
1086 if (OrigFunc->getExtInfo() == NewFunc->getExtInfo())
1087 return TInfo->getType();
1088
1089 FunctionProtoType::ExtProtoInfo NewEPI = NewFunc->getExtProtoInfo();
1090 NewEPI.ExtInfo = OrigFunc->getExtInfo();
1091 return Context.getFunctionType(NewFunc->getResultType(),
Jordan Rosebea522f2013-03-08 21:51:21 +00001092 ArrayRef<QualType>(NewFunc->arg_type_begin(),
1093 NewFunc->getNumArgs()),
Douglas Gregorbed51fe2012-09-13 22:01:49 +00001094 NewEPI);
Douglas Gregor71074fd2012-09-13 21:56:43 +00001095}
1096
John McCall02cace72009-08-28 07:59:38 +00001097/// Normal class members are of more specific types and therefore
1098/// don't make it here. This function serves two purposes:
1099/// 1) instantiating function templates
1100/// 2) substituting friend declarations
1101/// FIXME: preserve function definitions in case #2
Douglas Gregor7557a132009-12-24 20:56:24 +00001102Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
Douglas Gregora735b202009-10-13 14:39:41 +00001103 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +00001104 // Check whether there is already a function template specialization for
1105 // this declaration.
1106 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
John McCallb0cb0222010-03-27 05:57:59 +00001107 if (FunctionTemplate && !TemplateParams) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001108 std::pair<const TemplateArgument *, unsigned> Innermost
Douglas Gregor24bae922010-07-08 18:37:38 +00001109 = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +00001110
Douglas Gregor1e1e9722012-03-28 14:34:23 +00001111 void *InsertPos = 0;
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001112 FunctionDecl *SpecFunc
1113 = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
1114 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001115
Douglas Gregor127102b2009-06-29 20:59:39 +00001116 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001117 if (SpecFunc)
1118 return SpecFunc;
Douglas Gregor127102b2009-06-29 20:59:39 +00001119 }
Mike Stump1eb44332009-09-09 15:08:12 +00001120
John McCallb0cb0222010-03-27 05:57:59 +00001121 bool isFriend;
1122 if (FunctionTemplate)
1123 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1124 else
1125 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1126
Douglas Gregor79c22782010-01-16 20:21:20 +00001127 bool MergeWithParentScope = (TemplateParams != 0) ||
Douglas Gregorb212d9a2010-05-21 21:25:08 +00001128 Owner->isFunctionOrMethod() ||
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001129 !(isa<Decl>(Owner) &&
Douglas Gregor79c22782010-01-16 20:21:20 +00001130 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
John McCall2a7fb272010-08-25 05:32:35 +00001131 LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001132
Chris Lattner5f9e2722011-07-23 10:55:15 +00001133 SmallVector<ParmVarDecl *, 4> Params;
David Blaikie64b4b432011-11-10 05:42:04 +00001134 TypeSourceInfo *TInfo = SubstFunctionType(D, Params);
John McCall21ef0fa2010-03-11 09:03:00 +00001135 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001136 return 0;
Douglas Gregor71074fd2012-09-13 21:56:43 +00001137 QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo);
John McCallfd810b12009-08-14 02:03:10 +00001138
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001139 NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
1140 if (QualifierLoc) {
1141 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
1142 TemplateArgs);
1143 if (!QualifierLoc)
1144 return 0;
John McCalld325daa2010-03-26 04:53:08 +00001145 }
1146
John McCall68b6b872010-02-06 01:50:47 +00001147 // If we're instantiating a local function declaration, put the result
1148 // in the owner; otherwise we need to find the instantiated context.
1149 DeclContext *DC;
1150 if (D->getDeclContext()->isFunctionOrMethod())
1151 DC = Owner;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001152 else if (isFriend && QualifierLoc) {
John McCalld325daa2010-03-26 04:53:08 +00001153 CXXScopeSpec SS;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001154 SS.Adopt(QualifierLoc);
John McCalld325daa2010-03-26 04:53:08 +00001155 DC = SemaRef.computeDeclContext(SS);
1156 if (!DC) return 0;
1157 } else {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001158 DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001159 TemplateArgs);
John McCalld325daa2010-03-26 04:53:08 +00001160 }
John McCall68b6b872010-02-06 01:50:47 +00001161
John McCall02cace72009-08-28 07:59:38 +00001162 FunctionDecl *Function =
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001163 FunctionDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(),
Abramo Bagnara635311f2012-10-04 21:40:42 +00001164 D->getNameInfo(), T, TInfo,
Rafael Espindola459ef032013-04-16 02:29:15 +00001165 D->getCanonicalDecl()->getStorageClass(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001166 D->isInlineSpecified(), D->hasWrittenPrototype(),
Richard Smith86c3ae42012-02-13 03:54:03 +00001167 D->isConstexpr());
John McCallb6217662010-03-15 10:12:16 +00001168
Richard Smithd4497dd2013-01-25 00:08:28 +00001169 if (D->isInlined())
1170 Function->setImplicitlyInline();
1171
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001172 if (QualifierLoc)
1173 Function->setQualifierInfo(QualifierLoc);
John McCallb6217662010-03-15 10:12:16 +00001174
John McCallb1a56e72010-03-26 23:10:15 +00001175 DeclContext *LexicalDC = Owner;
1176 if (!isFriend && D->isOutOfLine()) {
1177 assert(D->getDeclContext()->isFileContext());
1178 LexicalDC = D->getDeclContext();
1179 }
1180
1181 Function->setLexicalDeclContext(LexicalDC);
Mike Stump1eb44332009-09-09 15:08:12 +00001182
Douglas Gregore53060f2009-06-25 22:08:12 +00001183 // Attach the parameters
Douglas Gregor5cbe1012011-07-05 18:30:26 +00001184 if (isa<FunctionProtoType>(Function->getType().IgnoreParens())) {
Douglas Gregor1d441ee2011-06-22 18:16:25 +00001185 // Adopt the already-instantiated parameters into our own context.
1186 for (unsigned P = 0; P < Params.size(); ++P)
1187 if (Params[P])
1188 Params[P]->setOwningFunction(Function);
1189 } else {
1190 // Since we were instantiated via a typedef of a function type, create
1191 // new parameters.
1192 const FunctionProtoType *Proto
1193 = Function->getType()->getAs<FunctionProtoType>();
1194 assert(Proto && "No function prototype in template instantiation?");
1195 for (FunctionProtoType::arg_type_iterator AI = Proto->arg_type_begin(),
1196 AE = Proto->arg_type_end(); AI != AE; ++AI) {
1197 ParmVarDecl *Param
1198 = SemaRef.BuildParmVarDeclForTypedef(Function, Function->getLocation(),
1199 *AI);
1200 Param->setScopeInfo(0, Params.size());
1201 Params.push_back(Param);
1202 }
1203 }
David Blaikie4278c652011-09-21 18:16:56 +00001204 Function->setParams(Params);
John McCall02cace72009-08-28 07:59:38 +00001205
Douglas Gregorac7c2c82010-05-17 16:38:00 +00001206 SourceLocation InstantiateAtPOI;
Douglas Gregora735b202009-10-13 14:39:41 +00001207 if (TemplateParams) {
1208 // Our resulting instantiation is actually a function template, since we
1209 // are substituting only the outer template parameters. For example, given
1210 //
1211 // template<typename T>
1212 // struct X {
1213 // template<typename U> friend void f(T, U);
1214 // };
1215 //
1216 // X<int> x;
1217 //
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001218 // We are instantiating the friend function template "f" within X<int>,
Douglas Gregora735b202009-10-13 14:39:41 +00001219 // which means substituting int for T, but leaving "f" as a friend function
1220 // template.
1221 // Build the function template itself.
John McCalld325daa2010-03-26 04:53:08 +00001222 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
Douglas Gregora735b202009-10-13 14:39:41 +00001223 Function->getLocation(),
1224 Function->getDeclName(),
1225 TemplateParams, Function);
1226 Function->setDescribedFunctionTemplate(FunctionTemplate);
John McCallb1a56e72010-03-26 23:10:15 +00001227
1228 FunctionTemplate->setLexicalDeclContext(LexicalDC);
John McCalld325daa2010-03-26 04:53:08 +00001229
1230 if (isFriend && D->isThisDeclarationADefinition()) {
1231 // TODO: should we remember this connection regardless of whether
1232 // the friend declaration provided a body?
1233 FunctionTemplate->setInstantiatedFromMemberTemplate(
1234 D->getDescribedFunctionTemplate());
1235 }
Douglas Gregor66724ea2009-11-14 01:20:54 +00001236 } else if (FunctionTemplate) {
1237 // Record this function template specialization.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001238 std::pair<const TemplateArgument *, unsigned> Innermost
Douglas Gregor24bae922010-07-08 18:37:38 +00001239 = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001240 Function->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001241 TemplateArgumentList::CreateCopy(SemaRef.Context,
Douglas Gregor24bae922010-07-08 18:37:38 +00001242 Innermost.first,
1243 Innermost.second),
Douglas Gregor1e1e9722012-03-28 14:34:23 +00001244 /*InsertPos=*/0);
Chandler Carruth80f5b162011-08-18 09:09:59 +00001245 } else if (isFriend) {
1246 // Note, we need this connection even if the friend doesn't have a body.
1247 // Its body may exist but not have been attached yet due to deferred
1248 // parsing.
1249 // FIXME: It might be cleaner to set this when attaching the body to the
1250 // friend function declaration, however that would require finding all the
1251 // instantiations and modifying them.
John McCalld325daa2010-03-26 04:53:08 +00001252 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
John McCall02cace72009-08-28 07:59:38 +00001253 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001254
Douglas Gregore53060f2009-06-25 22:08:12 +00001255 if (InitFunctionInstantiation(Function, D))
1256 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001257
John McCallaf2094e2010-04-08 09:05:18 +00001258 bool isExplicitSpecialization = false;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001259
John McCall68263142009-11-18 22:49:29 +00001260 LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
1261 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
1262
John McCallaf2094e2010-04-08 09:05:18 +00001263 if (DependentFunctionTemplateSpecializationInfo *Info
1264 = D->getDependentSpecializationInfo()) {
1265 assert(isFriend && "non-friend has dependent specialization info?");
1266
1267 // This needs to be set now for future sanity.
1268 Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1269
1270 // Instantiate the explicit template arguments.
1271 TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1272 Info->getRAngleLoc());
Douglas Gregore02e2622010-12-22 21:19:48 +00001273 if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(),
1274 ExplicitArgs, TemplateArgs))
1275 return 0;
John McCallaf2094e2010-04-08 09:05:18 +00001276
1277 // Map the candidate templates to their instantiations.
1278 for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1279 Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1280 Info->getTemplate(I),
1281 TemplateArgs);
1282 if (!Temp) return 0;
1283
1284 Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1285 }
1286
1287 if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1288 &ExplicitArgs,
1289 Previous))
1290 Function->setInvalidDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001291
John McCallaf2094e2010-04-08 09:05:18 +00001292 isExplicitSpecialization = true;
1293
1294 } else if (TemplateParams || !FunctionTemplate) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001295 // Look only into the namespace where the friend would be declared to
1296 // find a previous declaration. This is the innermost enclosing namespace,
Douglas Gregora735b202009-10-13 14:39:41 +00001297 // as described in ActOnFriendFunctionDecl.
John McCall68263142009-11-18 22:49:29 +00001298 SemaRef.LookupQualifiedName(Previous, DC);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001299
Douglas Gregora735b202009-10-13 14:39:41 +00001300 // In C++, the previous declaration we find might be a tag type
1301 // (class or enum). In this case, the new declaration will hide the
1302 // tag type. Note that this does does not apply if we're declaring a
1303 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001304 if (Previous.isSingleTagDecl())
1305 Previous.clear();
Douglas Gregora735b202009-10-13 14:39:41 +00001306 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001307
John McCall9f54ad42009-12-10 09:41:52 +00001308 SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00001309 isExplicitSpecialization);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001310
John McCall76d32642010-04-24 01:30:58 +00001311 NamedDecl *PrincipalDecl = (TemplateParams
1312 ? cast<NamedDecl>(FunctionTemplate)
1313 : Function);
1314
Douglas Gregora735b202009-10-13 14:39:41 +00001315 // If the original function was part of a friend declaration,
1316 // inherit its namespace state and add it to the owner.
John McCalld325daa2010-03-26 04:53:08 +00001317 if (isFriend) {
John McCall68263142009-11-18 22:49:29 +00001318 NamedDecl *PrevDecl;
John McCall76d32642010-04-24 01:30:58 +00001319 if (TemplateParams)
Douglas Gregoref96ee02012-01-14 16:38:05 +00001320 PrevDecl = FunctionTemplate->getPreviousDecl();
John McCall76d32642010-04-24 01:30:58 +00001321 else
Douglas Gregoref96ee02012-01-14 16:38:05 +00001322 PrevDecl = Function->getPreviousDecl();
John McCall76d32642010-04-24 01:30:58 +00001323
1324 PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001325 DC->makeDeclVisibleInContext(PrincipalDecl);
Gabor Greifab297ac2010-08-30 21:10:05 +00001326
Gabor Greif77535df2010-08-30 22:25:56 +00001327 bool queuedInstantiation = false;
Gabor Greifab297ac2010-08-30 21:10:05 +00001328
Richard Smith53e53512011-10-19 00:54:10 +00001329 // C++98 [temp.friend]p5: When a function is defined in a friend function
1330 // declaration in a class template, the function is defined at each
1331 // instantiation of the class template. The function is defined even if it
1332 // is never used.
1333 // C++11 [temp.friend]p4: When a function is defined in a friend function
1334 // declaration in a class template, the function is instantiated when the
1335 // function is odr-used.
1336 //
1337 // If -Wc++98-compat is enabled, we go through the motions of checking for a
1338 // redefinition, but don't instantiate the function.
Richard Smith80ad52f2013-01-02 11:42:31 +00001339 if ((!SemaRef.getLangOpts().CPlusPlus11 ||
Richard Smith53e53512011-10-19 00:54:10 +00001340 SemaRef.Diags.getDiagnosticLevel(
1341 diag::warn_cxx98_compat_friend_redefinition,
1342 Function->getLocation())
1343 != DiagnosticsEngine::Ignored) &&
Douglas Gregor238058c2010-05-18 05:45:02 +00001344 D->isThisDeclarationADefinition()) {
1345 // Check for a function body.
1346 const FunctionDecl *Definition = 0;
Sean Hunt10620eb2011-05-06 20:44:56 +00001347 if (Function->isDefined(Definition) &&
Douglas Gregor238058c2010-05-18 05:45:02 +00001348 Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
Richard Smith53e53512011-10-19 00:54:10 +00001349 SemaRef.Diag(Function->getLocation(),
Richard Smith80ad52f2013-01-02 11:42:31 +00001350 SemaRef.getLangOpts().CPlusPlus11 ?
Richard Smith53e53512011-10-19 00:54:10 +00001351 diag::warn_cxx98_compat_friend_redefinition :
1352 diag::err_redefinition) << Function->getDeclName();
Douglas Gregor238058c2010-05-18 05:45:02 +00001353 SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
Richard Smith80ad52f2013-01-02 11:42:31 +00001354 if (!SemaRef.getLangOpts().CPlusPlus11)
Richard Smith53e53512011-10-19 00:54:10 +00001355 Function->setInvalidDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001356 }
Douglas Gregor238058c2010-05-18 05:45:02 +00001357 // Check for redefinitions due to other instantiations of this or
1358 // a similar friend function.
1359 else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1360 REnd = Function->redecls_end();
1361 R != REnd; ++R) {
Gabor Greif13a8aff2010-08-28 15:42:30 +00001362 if (*R == Function)
1363 continue;
Gabor Greifab297ac2010-08-30 21:10:05 +00001364 switch (R->getFriendObjectKind()) {
1365 case Decl::FOK_None:
Richard Smith80ad52f2013-01-02 11:42:31 +00001366 if (!SemaRef.getLangOpts().CPlusPlus11 &&
Richard Smith53e53512011-10-19 00:54:10 +00001367 !queuedInstantiation && R->isUsed(false)) {
Gabor Greifab297ac2010-08-30 21:10:05 +00001368 if (MemberSpecializationInfo *MSInfo
1369 = Function->getMemberSpecializationInfo()) {
1370 if (MSInfo->getPointOfInstantiation().isInvalid()) {
1371 SourceLocation Loc = R->getLocation(); // FIXME
1372 MSInfo->setPointOfInstantiation(Loc);
1373 SemaRef.PendingLocalImplicitInstantiations.push_back(
1374 std::make_pair(Function, Loc));
1375 queuedInstantiation = true;
1376 }
1377 }
1378 }
1379 break;
1380 default:
Douglas Gregor238058c2010-05-18 05:45:02 +00001381 if (const FunctionDecl *RPattern
Gabor Greif6a557d82010-08-28 15:46:56 +00001382 = R->getTemplateInstantiationPattern())
Sean Hunt10620eb2011-05-06 20:44:56 +00001383 if (RPattern->isDefined(RPattern)) {
Richard Smith53e53512011-10-19 00:54:10 +00001384 SemaRef.Diag(Function->getLocation(),
Richard Smith80ad52f2013-01-02 11:42:31 +00001385 SemaRef.getLangOpts().CPlusPlus11 ?
Richard Smith53e53512011-10-19 00:54:10 +00001386 diag::warn_cxx98_compat_friend_redefinition :
1387 diag::err_redefinition)
Douglas Gregor238058c2010-05-18 05:45:02 +00001388 << Function->getDeclName();
Gabor Greif6a557d82010-08-28 15:46:56 +00001389 SemaRef.Diag(R->getLocation(), diag::note_previous_definition);
Richard Smith80ad52f2013-01-02 11:42:31 +00001390 if (!SemaRef.getLangOpts().CPlusPlus11)
Richard Smith53e53512011-10-19 00:54:10 +00001391 Function->setInvalidDecl();
Douglas Gregor238058c2010-05-18 05:45:02 +00001392 break;
1393 }
1394 }
1395 }
1396 }
Douglas Gregora735b202009-10-13 14:39:41 +00001397 }
1398
John McCall76d32642010-04-24 01:30:58 +00001399 if (Function->isOverloadedOperator() && !DC->isRecord() &&
1400 PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
1401 PrincipalDecl->setNonMemberOperator();
1402
Sean Hunteb88ae52011-05-23 21:07:59 +00001403 assert(!D->isDefaulted() && "only methods should be defaulted");
Douglas Gregore53060f2009-06-25 22:08:12 +00001404 return Function;
1405}
1406
Douglas Gregord60e1052009-08-27 16:57:43 +00001407Decl *
1408TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
Francois Pichetaf0f4d02011-08-14 03:52:19 +00001409 TemplateParameterList *TemplateParams,
1410 bool IsClassScopeSpecialization) {
Douglas Gregor6b906862009-08-21 00:16:32 +00001411 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
Douglas Gregord60e1052009-08-27 16:57:43 +00001412 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +00001413 // We are creating a function template specialization from a function
1414 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +00001415 // specialization for this particular set of template arguments.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001416 std::pair<const TemplateArgument *, unsigned> Innermost
Douglas Gregor24bae922010-07-08 18:37:38 +00001417 = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +00001418
Douglas Gregor1e1e9722012-03-28 14:34:23 +00001419 void *InsertPos = 0;
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001420 FunctionDecl *SpecFunc
1421 = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
1422 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001423
Douglas Gregor6b906862009-08-21 00:16:32 +00001424 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001425 if (SpecFunc)
1426 return SpecFunc;
Douglas Gregor6b906862009-08-21 00:16:32 +00001427 }
1428
John McCallb0cb0222010-03-27 05:57:59 +00001429 bool isFriend;
1430 if (FunctionTemplate)
1431 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1432 else
1433 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1434
Douglas Gregor79c22782010-01-16 20:21:20 +00001435 bool MergeWithParentScope = (TemplateParams != 0) ||
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001436 !(isa<Decl>(Owner) &&
Douglas Gregor79c22782010-01-16 20:21:20 +00001437 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
John McCall2a7fb272010-08-25 05:32:35 +00001438 LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Douglas Gregor48dd19b2009-05-14 21:44:34 +00001439
John McCall4eab39f2010-10-19 02:26:41 +00001440 // Instantiate enclosing template arguments for friends.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001441 SmallVector<TemplateParameterList *, 4> TempParamLists;
John McCall4eab39f2010-10-19 02:26:41 +00001442 unsigned NumTempParamLists = 0;
1443 if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) {
1444 TempParamLists.set_size(NumTempParamLists);
1445 for (unsigned I = 0; I != NumTempParamLists; ++I) {
1446 TemplateParameterList *TempParams = D->getTemplateParameterList(I);
1447 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1448 if (!InstParams)
1449 return NULL;
1450 TempParamLists[I] = InstParams;
1451 }
1452 }
1453
Chris Lattner5f9e2722011-07-23 10:55:15 +00001454 SmallVector<ParmVarDecl *, 4> Params;
Benjamin Kramerdc370c12012-01-20 14:42:32 +00001455 TypeSourceInfo *TInfo = SubstFunctionType(D, Params);
John McCall21ef0fa2010-03-11 09:03:00 +00001456 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001457 return 0;
Douglas Gregor71074fd2012-09-13 21:56:43 +00001458 QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001459
Abramo Bagnara723df242010-12-14 22:11:44 +00001460 // \brief If the type of this function, after ignoring parentheses,
1461 // is not *directly* a function type, then we're instantiating a function
1462 // that was declared via a typedef, e.g.,
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001463 //
1464 // typedef int functype(int, int);
1465 // functype func;
1466 //
1467 // In this case, we'll just go instantiate the ParmVarDecls that we
1468 // synthesized in the method declaration.
Abramo Bagnara723df242010-12-14 22:11:44 +00001469 if (!isa<FunctionProtoType>(T.IgnoreParens())) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001470 assert(!Params.size() && "Instantiating type could not yield parameters");
Chris Lattner5f9e2722011-07-23 10:55:15 +00001471 SmallVector<QualType, 4> ParamTypes;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001472 if (SemaRef.SubstParmTypes(D->getLocation(), D->param_begin(),
1473 D->getNumParams(), TemplateArgs, ParamTypes,
Douglas Gregor12c9c002011-01-07 16:43:16 +00001474 &Params))
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001475 return 0;
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001476 }
1477
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001478 NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
1479 if (QualifierLoc) {
1480 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
John McCallb0cb0222010-03-27 05:57:59 +00001481 TemplateArgs);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001482 if (!QualifierLoc)
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001483 return 0;
John McCallb0cb0222010-03-27 05:57:59 +00001484 }
1485
1486 DeclContext *DC = Owner;
1487 if (isFriend) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001488 if (QualifierLoc) {
John McCallb0cb0222010-03-27 05:57:59 +00001489 CXXScopeSpec SS;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001490 SS.Adopt(QualifierLoc);
John McCallb0cb0222010-03-27 05:57:59 +00001491 DC = SemaRef.computeDeclContext(SS);
John McCallc54d6882010-10-19 05:01:53 +00001492
1493 if (DC && SemaRef.RequireCompleteDeclContext(SS, DC))
1494 return 0;
John McCallb0cb0222010-03-27 05:57:59 +00001495 } else {
1496 DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1497 D->getDeclContext(),
1498 TemplateArgs);
1499 }
1500 if (!DC) return 0;
1501 }
1502
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001503 // Build the instantiated method declaration.
John McCallb0cb0222010-03-27 05:57:59 +00001504 CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
Douglas Gregordec06662009-08-21 18:42:58 +00001505 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001506
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001507 SourceLocation StartLoc = D->getInnerLocStart();
Abramo Bagnara25777432010-08-11 22:01:17 +00001508 DeclarationNameInfo NameInfo
1509 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001510 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001511 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001512 StartLoc, NameInfo, T, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001513 Constructor->isExplicit(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001514 Constructor->isInlineSpecified(),
Richard Smith86c3ae42012-02-13 03:54:03 +00001515 false, Constructor->isConstexpr());
Richard Smith4841ca52013-04-10 05:48:59 +00001516 // Claim that the instantiation of a constructor or constructor template
1517 // inherits the same constructor that the template does.
1518 if (const CXXConstructorDecl *Inh = Constructor->getInheritedConstructor())
1519 cast<CXXConstructorDecl>(Method)->setInheritedConstructor(Inh);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001520 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001521 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001522 StartLoc, NameInfo, T, TInfo,
Abramo Bagnara25777432010-08-11 22:01:17 +00001523 Destructor->isInlineSpecified(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001524 false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001525 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001526 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001527 StartLoc, NameInfo, T, TInfo,
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001528 Conversion->isInlineSpecified(),
Douglas Gregorf5251602011-03-08 17:10:18 +00001529 Conversion->isExplicit(),
Richard Smith86c3ae42012-02-13 03:54:03 +00001530 Conversion->isConstexpr(),
Richard Smith9f569cc2011-10-01 02:31:28 +00001531 Conversion->getLocEnd());
Douglas Gregordec06662009-08-21 18:42:58 +00001532 } else {
Rafael Espindola72fdc892013-04-15 12:38:20 +00001533 StorageClass SC = D->isStatic() ? SC_Static : SC_None;
Abramo Bagnara25777432010-08-11 22:01:17 +00001534 Method = CXXMethodDecl::Create(SemaRef.Context, Record,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001535 StartLoc, NameInfo, T, TInfo,
Rafael Espindola72fdc892013-04-15 12:38:20 +00001536 SC, D->isInlineSpecified(),
Richard Smith86c3ae42012-02-13 03:54:03 +00001537 D->isConstexpr(), D->getLocEnd());
Douglas Gregordec06662009-08-21 18:42:58 +00001538 }
Douglas Gregor6b906862009-08-21 00:16:32 +00001539
Richard Smithd4497dd2013-01-25 00:08:28 +00001540 if (D->isInlined())
1541 Method->setImplicitlyInline();
1542
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001543 if (QualifierLoc)
1544 Method->setQualifierInfo(QualifierLoc);
John McCallb6217662010-03-15 10:12:16 +00001545
Douglas Gregord60e1052009-08-27 16:57:43 +00001546 if (TemplateParams) {
1547 // Our resulting instantiation is actually a function template, since we
1548 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +00001549 //
Douglas Gregord60e1052009-08-27 16:57:43 +00001550 // template<typename T>
1551 // struct X {
1552 // template<typename U> void f(T, U);
1553 // };
1554 //
1555 // X<int> x;
1556 //
1557 // We are instantiating the member template "f" within X<int>, which means
1558 // substituting int for T, but leaving "f" as a member function template.
1559 // Build the function template itself.
1560 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1561 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001562 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +00001563 TemplateParams, Method);
John McCallb0cb0222010-03-27 05:57:59 +00001564 if (isFriend) {
1565 FunctionTemplate->setLexicalDeclContext(Owner);
1566 FunctionTemplate->setObjectOfFriendDecl(true);
1567 } else if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +00001568 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +00001569 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001570 } else if (FunctionTemplate) {
1571 // Record this function template specialization.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001572 std::pair<const TemplateArgument *, unsigned> Innermost
Douglas Gregor24bae922010-07-08 18:37:38 +00001573 = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001574 Method->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001575 TemplateArgumentList::CreateCopy(SemaRef.Context,
1576 Innermost.first,
1577 Innermost.second),
Douglas Gregor1e1e9722012-03-28 14:34:23 +00001578 /*InsertPos=*/0);
John McCallb0cb0222010-03-27 05:57:59 +00001579 } else if (!isFriend) {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001580 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +00001581 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001582 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001583
Mike Stump1eb44332009-09-09 15:08:12 +00001584 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +00001585 // out-of-line, the instantiation will have the same lexical
1586 // context (which will be a namespace scope) as the template.
John McCallb0cb0222010-03-27 05:57:59 +00001587 if (isFriend) {
John McCall4eab39f2010-10-19 02:26:41 +00001588 if (NumTempParamLists)
1589 Method->setTemplateParameterListsInfo(SemaRef.Context,
1590 NumTempParamLists,
1591 TempParamLists.data());
1592
John McCallb0cb0222010-03-27 05:57:59 +00001593 Method->setLexicalDeclContext(Owner);
1594 Method->setObjectOfFriendDecl(true);
1595 } else if (D->isOutOfLine())
Douglas Gregor7caa6822009-07-24 20:34:43 +00001596 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001597
Douglas Gregor5545e162009-03-24 00:38:23 +00001598 // Attach the parameters
1599 for (unsigned P = 0; P < Params.size(); ++P)
1600 Params[P]->setOwningFunction(Method);
David Blaikie4278c652011-09-21 18:16:56 +00001601 Method->setParams(Params);
Douglas Gregor5545e162009-03-24 00:38:23 +00001602
1603 if (InitMethodInstantiation(Method, D))
1604 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001605
Abramo Bagnara25777432010-08-11 22:01:17 +00001606 LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
1607 Sema::ForRedeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001608
John McCallb0cb0222010-03-27 05:57:59 +00001609 if (!FunctionTemplate || TemplateParams || isFriend) {
1610 SemaRef.LookupQualifiedName(Previous, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001611
Douglas Gregordec06662009-08-21 18:42:58 +00001612 // In C++, the previous declaration we find might be a tag type
1613 // (class or enum). In this case, the new declaration will hide the
1614 // tag type. Note that this does does not apply if we're declaring a
1615 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001616 if (Previous.isSingleTagDecl())
1617 Previous.clear();
Douglas Gregordec06662009-08-21 18:42:58 +00001618 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001619
Francois Pichetaf0f4d02011-08-14 03:52:19 +00001620 if (!IsClassScopeSpecialization)
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00001621 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001622
Douglas Gregor4ba31362009-12-01 17:24:26 +00001623 if (D->isPure())
1624 SemaRef.CheckPureMethod(Method, SourceRange());
1625
John McCall1f2e1a92012-08-10 03:15:35 +00001626 // Propagate access. For a non-friend declaration, the access is
1627 // whatever we're propagating from. For a friend, it should be the
1628 // previous declaration we just found.
1629 if (isFriend && Method->getPreviousDecl())
1630 Method->setAccess(Method->getPreviousDecl()->getAccess());
1631 else
1632 Method->setAccess(D->getAccess());
1633 if (FunctionTemplate)
1634 FunctionTemplate->setAccess(Method->getAccess());
John McCall46460a62010-01-20 21:53:11 +00001635
Anders Carlsson9eefa222011-01-20 06:52:44 +00001636 SemaRef.CheckOverrideControl(Method);
1637
Eli Friedman3bc45152011-11-15 22:39:08 +00001638 // If a function is defined as defaulted or deleted, mark it as such now.
Richard Smithac713512012-12-08 02:53:02 +00001639 if (D->isExplicitlyDefaulted())
1640 SemaRef.SetDeclDefaulted(Method, Method->getLocation());
Eli Friedman3bc45152011-11-15 22:39:08 +00001641 if (D->isDeletedAsWritten())
Richard Smithac713512012-12-08 02:53:02 +00001642 SemaRef.SetDeclDeleted(Method, Method->getLocation());
Eli Friedman3bc45152011-11-15 22:39:08 +00001643
John McCall1f2e1a92012-08-10 03:15:35 +00001644 // If there's a function template, let our caller handle it.
John McCallb0cb0222010-03-27 05:57:59 +00001645 if (FunctionTemplate) {
John McCall1f2e1a92012-08-10 03:15:35 +00001646 // do nothing
1647
1648 // Don't hide a (potentially) valid declaration with an invalid one.
John McCallb0cb0222010-03-27 05:57:59 +00001649 } else if (Method->isInvalidDecl() && !Previous.empty()) {
John McCall1f2e1a92012-08-10 03:15:35 +00001650 // do nothing
1651
1652 // Otherwise, check access to friends and make them visible.
1653 } else if (isFriend) {
1654 // We only need to re-check access for methods which we didn't
1655 // manage to match during parsing.
1656 if (!D->getPreviousDecl())
1657 SemaRef.CheckFriendAccess(Method);
1658
1659 Record->makeDeclVisibleInContext(Method);
1660
1661 // Otherwise, add the declaration. We don't need to do this for
1662 // class-scope specializations because we'll have matched them with
1663 // the appropriate template.
1664 } else if (!IsClassScopeSpecialization) {
1665 Owner->addDecl(Method);
John McCallb0cb0222010-03-27 05:57:59 +00001666 }
Sean Hunteb88ae52011-05-23 21:07:59 +00001667
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001668 return Method;
1669}
1670
Douglas Gregor615c5d42009-03-24 16:43:20 +00001671Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +00001672 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +00001673}
1674
Douglas Gregor03b2b072009-03-24 00:15:49 +00001675Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001676 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +00001677}
1678
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001679Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001680 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001681}
1682
Douglas Gregor6477b692009-03-25 15:04:13 +00001683ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
David Blaikie66874fb2013-02-21 01:47:18 +00001684 return SemaRef.SubstParmVarDecl(D, TemplateArgs, /*indexAdjustment*/ 0, None,
1685 /*ExpectParameterPack=*/ false);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001686}
1687
John McCalle29ba202009-08-20 01:44:21 +00001688Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1689 TemplateTypeParmDecl *D) {
1690 // TODO: don't always clone when decls are refcounted.
Chandler Carruth4fb86f82011-05-01 00:51:33 +00001691 assert(D->getTypeForDecl()->isTemplateTypeParmType());
Mike Stump1eb44332009-09-09 15:08:12 +00001692
John McCalle29ba202009-08-20 01:44:21 +00001693 TemplateTypeParmDecl *Inst =
Abramo Bagnara344577e2011-03-06 15:48:19 +00001694 TemplateTypeParmDecl::Create(SemaRef.Context, Owner,
1695 D->getLocStart(), D->getLocation(),
Chandler Carruth4fb86f82011-05-01 00:51:33 +00001696 D->getDepth() - TemplateArgs.getNumLevels(),
1697 D->getIndex(), D->getIdentifier(),
John McCalle29ba202009-08-20 01:44:21 +00001698 D->wasDeclaredWithTypename(),
1699 D->isParameterPack());
Douglas Gregor9a299e02011-03-04 17:52:15 +00001700 Inst->setAccess(AS_public);
John McCalle29ba202009-08-20 01:44:21 +00001701
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001702 if (D->hasDefaultArgument())
1703 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
1704
1705 // Introduce this template parameter's instantiation into the instantiation
Douglas Gregor550d9b22009-10-31 17:21:17 +00001706 // scope.
1707 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001708
John McCalle29ba202009-08-20 01:44:21 +00001709 return Inst;
1710}
1711
Douglas Gregor33642df2009-10-23 23:25:44 +00001712Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1713 NonTypeTemplateParmDecl *D) {
1714 // Substitute into the type of the non-type template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001715 TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc();
Chris Lattner5f9e2722011-07-23 10:55:15 +00001716 SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten;
1717 SmallVector<QualType, 4> ExpandedParameterPackTypes;
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001718 bool IsExpandedParameterPack = false;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001719 TypeSourceInfo *DI;
Douglas Gregor33642df2009-10-23 23:25:44 +00001720 QualType T;
Douglas Gregor33642df2009-10-23 23:25:44 +00001721 bool Invalid = false;
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001722
1723 if (D->isExpandedParameterPack()) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001724 // The non-type template parameter pack is an already-expanded pack
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001725 // expansion of types. Substitute into each of the expanded types.
1726 ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes());
1727 ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes());
1728 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
1729 TypeSourceInfo *NewDI =SemaRef.SubstType(D->getExpansionTypeSourceInfo(I),
1730 TemplateArgs,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001731 D->getLocation(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001732 D->getDeclName());
1733 if (!NewDI)
1734 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001735
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001736 ExpandedParameterPackTypesAsWritten.push_back(NewDI);
1737 QualType NewT =SemaRef.CheckNonTypeTemplateParameterType(NewDI->getType(),
1738 D->getLocation());
1739 if (NewT.isNull())
1740 return 0;
1741 ExpandedParameterPackTypes.push_back(NewT);
1742 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001743
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001744 IsExpandedParameterPack = true;
1745 DI = D->getTypeSourceInfo();
1746 T = DI->getType();
Richard Smith6964b3f2012-09-07 02:06:42 +00001747 } else if (D->isPackExpansion()) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001748 // The non-type template parameter pack's type is a pack expansion of types.
1749 // Determine whether we need to expand this parameter pack into separate
1750 // types.
David Blaikie39e6ab42013-02-18 22:06:02 +00001751 PackExpansionTypeLoc Expansion = TL.castAs<PackExpansionTypeLoc>();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001752 TypeLoc Pattern = Expansion.getPatternLoc();
Chris Lattner5f9e2722011-07-23 10:55:15 +00001753 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001754 SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001755
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001756 // Determine whether the set of unexpanded parameter packs can and should
1757 // be expanded.
1758 bool Expand = true;
1759 bool RetainExpansion = false;
David Blaikiedc84cd52013-02-20 22:23:23 +00001760 Optional<unsigned> OrigNumExpansions
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001761 = Expansion.getTypePtr()->getNumExpansions();
David Blaikiedc84cd52013-02-20 22:23:23 +00001762 Optional<unsigned> NumExpansions = OrigNumExpansions;
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001763 if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(),
1764 Pattern.getSourceRange(),
David Blaikiea71f9d02011-09-22 02:34:54 +00001765 Unexpanded,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001766 TemplateArgs,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001767 Expand, RetainExpansion,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001768 NumExpansions))
1769 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001770
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001771 if (Expand) {
1772 for (unsigned I = 0; I != *NumExpansions; ++I) {
1773 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
1774 TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001775 D->getLocation(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001776 D->getDeclName());
1777 if (!NewDI)
1778 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001779
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001780 ExpandedParameterPackTypesAsWritten.push_back(NewDI);
1781 QualType NewT = SemaRef.CheckNonTypeTemplateParameterType(
1782 NewDI->getType(),
1783 D->getLocation());
1784 if (NewT.isNull())
1785 return 0;
1786 ExpandedParameterPackTypes.push_back(NewT);
1787 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001788
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001789 // Note that we have an expanded parameter pack. The "type" of this
1790 // expanded parameter pack is the original expansion type, but callers
1791 // will end up using the expanded parameter pack types for type-checking.
1792 IsExpandedParameterPack = true;
1793 DI = D->getTypeSourceInfo();
1794 T = DI->getType();
1795 } else {
1796 // We cannot fully expand the pack expansion now, so substitute into the
1797 // pattern and create a new pack expansion type.
1798 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
1799 TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001800 D->getLocation(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001801 D->getDeclName());
1802 if (!NewPattern)
1803 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001804
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001805 DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(),
1806 NumExpansions);
1807 if (!DI)
1808 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001809
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001810 T = DI->getType();
1811 }
1812 } else {
1813 // Simple case: substitution into a parameter that is not a parameter pack.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001814 DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001815 D->getLocation(), D->getDeclName());
1816 if (!DI)
1817 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001818
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001819 // Check that this type is acceptable for a non-type template parameter.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001820 T = SemaRef.CheckNonTypeTemplateParameterType(DI->getType(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001821 D->getLocation());
1822 if (T.isNull()) {
1823 T = SemaRef.Context.IntTy;
1824 Invalid = true;
1825 }
Douglas Gregor33642df2009-10-23 23:25:44 +00001826 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001827
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001828 NonTypeTemplateParmDecl *Param;
1829 if (IsExpandedParameterPack)
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001830 Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001831 D->getInnerLocStart(),
1832 D->getLocation(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001833 D->getDepth() - TemplateArgs.getNumLevels(),
1834 D->getPosition(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001835 D->getIdentifier(), T,
1836 DI,
1837 ExpandedParameterPackTypes.data(),
1838 ExpandedParameterPackTypes.size(),
1839 ExpandedParameterPackTypesAsWritten.data());
1840 else
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001841 Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001842 D->getInnerLocStart(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001843 D->getLocation(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001844 D->getDepth() - TemplateArgs.getNumLevels(),
1845 D->getPosition(),
1846 D->getIdentifier(), T,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001847 D->isParameterPack(), DI);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001848
Douglas Gregor9a299e02011-03-04 17:52:15 +00001849 Param->setAccess(AS_public);
Douglas Gregor33642df2009-10-23 23:25:44 +00001850 if (Invalid)
1851 Param->setInvalidDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001852
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001853 Param->setDefaultArgument(D->getDefaultArgument(), false);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001854
1855 // Introduce this template parameter's instantiation into the instantiation
Douglas Gregor550d9b22009-10-31 17:21:17 +00001856 // scope.
1857 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001858 return Param;
1859}
1860
Richard Smith6964b3f2012-09-07 02:06:42 +00001861static void collectUnexpandedParameterPacks(
1862 Sema &S,
1863 TemplateParameterList *Params,
1864 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
1865 for (TemplateParameterList::const_iterator I = Params->begin(),
1866 E = Params->end(); I != E; ++I) {
1867 if ((*I)->isTemplateParameterPack())
1868 continue;
1869 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*I))
1870 S.collectUnexpandedParameterPacks(NTTP->getTypeSourceInfo()->getTypeLoc(),
1871 Unexpanded);
1872 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(*I))
1873 collectUnexpandedParameterPacks(S, TTP->getTemplateParameters(),
1874 Unexpanded);
1875 }
1876}
1877
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001878Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001879TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1880 TemplateTemplateParmDecl *D) {
1881 // Instantiate the template parameter list of the template template parameter.
1882 TemplateParameterList *TempParams = D->getTemplateParameters();
1883 TemplateParameterList *InstParams;
Richard Smith6964b3f2012-09-07 02:06:42 +00001884 SmallVector<TemplateParameterList*, 8> ExpandedParams;
1885
1886 bool IsExpandedParameterPack = false;
1887
1888 if (D->isExpandedParameterPack()) {
1889 // The template template parameter pack is an already-expanded pack
1890 // expansion of template parameters. Substitute into each of the expanded
1891 // parameters.
1892 ExpandedParams.reserve(D->getNumExpansionTemplateParameters());
1893 for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
1894 I != N; ++I) {
1895 LocalInstantiationScope Scope(SemaRef);
1896 TemplateParameterList *Expansion =
1897 SubstTemplateParams(D->getExpansionTemplateParameters(I));
1898 if (!Expansion)
1899 return 0;
1900 ExpandedParams.push_back(Expansion);
1901 }
1902
1903 IsExpandedParameterPack = true;
1904 InstParams = TempParams;
1905 } else if (D->isPackExpansion()) {
1906 // The template template parameter pack expands to a pack of template
1907 // template parameters. Determine whether we need to expand this parameter
1908 // pack into separate parameters.
1909 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
1910 collectUnexpandedParameterPacks(SemaRef, D->getTemplateParameters(),
1911 Unexpanded);
1912
1913 // Determine whether the set of unexpanded parameter packs can and should
1914 // be expanded.
1915 bool Expand = true;
1916 bool RetainExpansion = false;
David Blaikiedc84cd52013-02-20 22:23:23 +00001917 Optional<unsigned> NumExpansions;
Richard Smith6964b3f2012-09-07 02:06:42 +00001918 if (SemaRef.CheckParameterPacksForExpansion(D->getLocation(),
1919 TempParams->getSourceRange(),
1920 Unexpanded,
1921 TemplateArgs,
1922 Expand, RetainExpansion,
1923 NumExpansions))
1924 return 0;
1925
1926 if (Expand) {
1927 for (unsigned I = 0; I != *NumExpansions; ++I) {
1928 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
1929 LocalInstantiationScope Scope(SemaRef);
1930 TemplateParameterList *Expansion = SubstTemplateParams(TempParams);
1931 if (!Expansion)
1932 return 0;
1933 ExpandedParams.push_back(Expansion);
1934 }
1935
1936 // Note that we have an expanded parameter pack. The "type" of this
1937 // expanded parameter pack is the original expansion type, but callers
1938 // will end up using the expanded parameter pack types for type-checking.
1939 IsExpandedParameterPack = true;
1940 InstParams = TempParams;
1941 } else {
1942 // We cannot fully expand the pack expansion now, so just substitute
1943 // into the pattern.
1944 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
1945
1946 LocalInstantiationScope Scope(SemaRef);
1947 InstParams = SubstTemplateParams(TempParams);
1948 if (!InstParams)
1949 return 0;
1950 }
1951 } else {
Douglas Gregor9106ef72009-11-11 16:58:32 +00001952 // Perform the actual substitution of template parameters within a new,
1953 // local instantiation scope.
John McCall2a7fb272010-08-25 05:32:35 +00001954 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor9106ef72009-11-11 16:58:32 +00001955 InstParams = SubstTemplateParams(TempParams);
1956 if (!InstParams)
Richard Smith6964b3f2012-09-07 02:06:42 +00001957 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001958 }
1959
Douglas Gregor9106ef72009-11-11 16:58:32 +00001960 // Build the template template parameter.
Richard Smith6964b3f2012-09-07 02:06:42 +00001961 TemplateTemplateParmDecl *Param;
1962 if (IsExpandedParameterPack)
1963 Param = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner,
1964 D->getLocation(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001965 D->getDepth() - TemplateArgs.getNumLevels(),
Richard Smith6964b3f2012-09-07 02:06:42 +00001966 D->getPosition(),
1967 D->getIdentifier(), InstParams,
1968 ExpandedParams);
1969 else
1970 Param = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner,
1971 D->getLocation(),
1972 D->getDepth() - TemplateArgs.getNumLevels(),
1973 D->getPosition(),
1974 D->isParameterPack(),
1975 D->getIdentifier(), InstParams);
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001976 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor9a299e02011-03-04 17:52:15 +00001977 Param->setAccess(AS_public);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001978
1979 // Introduce this template parameter's instantiation into the instantiation
Douglas Gregor9106ef72009-11-11 16:58:32 +00001980 // scope.
1981 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001982
Douglas Gregor9106ef72009-11-11 16:58:32 +00001983 return Param;
1984}
1985
Douglas Gregor48c32a72009-11-17 06:07:40 +00001986Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Douglas Gregordb992412011-02-25 16:33:46 +00001987 // Using directives are never dependent (and never contain any types or
1988 // expressions), so they require no explicit instantiation work.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001989
Douglas Gregor48c32a72009-11-17 06:07:40 +00001990 UsingDirectiveDecl *Inst
1991 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001992 D->getNamespaceKeyLocation(),
Douglas Gregordb992412011-02-25 16:33:46 +00001993 D->getQualifierLoc(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001994 D->getIdentLocation(),
1995 D->getNominatedNamespace(),
Douglas Gregor48c32a72009-11-17 06:07:40 +00001996 D->getCommonAncestor());
Abramo Bagnara536afbe2012-09-05 09:55:10 +00001997
1998 // Add the using directive to its declaration context
1999 // only if this is not a function or method.
2000 if (!Owner->isFunctionOrMethod())
2001 Owner->addDecl(Inst);
2002
Douglas Gregor48c32a72009-11-17 06:07:40 +00002003 return Inst;
2004}
2005
John McCalled976492009-12-04 22:46:56 +00002006Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
Douglas Gregor1b398202010-09-29 17:58:28 +00002007
2008 // The nested name specifier may be dependent, for example
2009 // template <typename T> struct t {
2010 // struct s1 { T f1(); };
2011 // struct s2 : s1 { using s1::f1; };
2012 // };
2013 // template struct t<int>;
2014 // Here, in using s1::f1, s1 refers to t<T>::s1;
2015 // we need to substitute for t<int>::s1.
Douglas Gregor5149f372011-02-25 15:54:31 +00002016 NestedNameSpecifierLoc QualifierLoc
2017 = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
2018 TemplateArgs);
2019 if (!QualifierLoc)
Douglas Gregordc355712011-02-25 00:36:19 +00002020 return 0;
Douglas Gregor1b398202010-09-29 17:58:28 +00002021
2022 // The name info is non-dependent, so no transformation
2023 // is required.
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002024 DeclarationNameInfo NameInfo = D->getNameInfo();
John McCalled976492009-12-04 22:46:56 +00002025
John McCall9f54ad42009-12-10 09:41:52 +00002026 // We only need to do redeclaration lookups if we're in a class
2027 // scope (in fact, it's not really even possible in non-class
2028 // scopes).
2029 bool CheckRedeclaration = Owner->isRecord();
2030
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002031 LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
2032 Sema::ForRedeclaration);
John McCall9f54ad42009-12-10 09:41:52 +00002033
John McCalled976492009-12-04 22:46:56 +00002034 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
John McCalled976492009-12-04 22:46:56 +00002035 D->getUsingLocation(),
Douglas Gregor5149f372011-02-25 15:54:31 +00002036 QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002037 NameInfo,
John McCalled976492009-12-04 22:46:56 +00002038 D->isTypeName());
2039
Douglas Gregor5149f372011-02-25 15:54:31 +00002040 CXXScopeSpec SS;
2041 SS.Adopt(QualifierLoc);
John McCall9f54ad42009-12-10 09:41:52 +00002042 if (CheckRedeclaration) {
2043 Prev.setHideTags(false);
2044 SemaRef.LookupQualifiedName(Prev, Owner);
2045
2046 // Check for invalid redeclarations.
2047 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
2048 D->isTypeName(), SS,
2049 D->getLocation(), Prev))
2050 NewUD->setInvalidDecl();
2051
2052 }
2053
2054 if (!NewUD->isInvalidDecl() &&
2055 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
John McCalled976492009-12-04 22:46:56 +00002056 D->getLocation()))
2057 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00002058
John McCalled976492009-12-04 22:46:56 +00002059 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
2060 NewUD->setAccess(D->getAccess());
2061 Owner->addDecl(NewUD);
2062
John McCall9f54ad42009-12-10 09:41:52 +00002063 // Don't process the shadow decls for an invalid decl.
2064 if (NewUD->isInvalidDecl())
2065 return NewUD;
2066
Richard Smithc5a89a12012-04-02 01:30:27 +00002067 if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) {
2068 if (SemaRef.CheckInheritingConstructorUsingDecl(NewUD))
2069 NewUD->setInvalidDecl();
2070 return NewUD;
2071 }
2072
John McCall323c3102009-12-22 22:26:37 +00002073 bool isFunctionScope = Owner->isFunctionOrMethod();
2074
John McCall9f54ad42009-12-10 09:41:52 +00002075 // Process the shadow decls.
2076 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
2077 I != E; ++I) {
2078 UsingShadowDecl *Shadow = *I;
2079 NamedDecl *InstTarget =
Douglas Gregorb7107222011-03-04 19:46:35 +00002080 cast_or_null<NamedDecl>(SemaRef.FindInstantiatedDecl(
2081 Shadow->getLocation(),
2082 Shadow->getTargetDecl(),
2083 TemplateArgs));
2084 if (!InstTarget)
2085 return 0;
John McCall9f54ad42009-12-10 09:41:52 +00002086
2087 if (CheckRedeclaration &&
2088 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
2089 continue;
2090
2091 UsingShadowDecl *InstShadow
2092 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
2093 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
John McCall323c3102009-12-22 22:26:37 +00002094
2095 if (isFunctionScope)
2096 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
John McCall9f54ad42009-12-10 09:41:52 +00002097 }
John McCalled976492009-12-04 22:46:56 +00002098
2099 return NewUD;
2100}
2101
2102Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00002103 // Ignore these; we handle them in bulk when processing the UsingDecl.
2104 return 0;
John McCalled976492009-12-04 22:46:56 +00002105}
2106
John McCall7ba107a2009-11-18 02:36:19 +00002107Decl * TemplateDeclInstantiator
2108 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Douglas Gregor5149f372011-02-25 15:54:31 +00002109 NestedNameSpecifierLoc QualifierLoc
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002110 = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
Douglas Gregor5149f372011-02-25 15:54:31 +00002111 TemplateArgs);
2112 if (!QualifierLoc)
Anders Carlsson0dde18e2009-08-28 15:18:15 +00002113 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002114
Anders Carlsson0dde18e2009-08-28 15:18:15 +00002115 CXXScopeSpec SS;
Douglas Gregor5149f372011-02-25 15:54:31 +00002116 SS.Adopt(QualifierLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002117
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002118 // Since NameInfo refers to a typename, it cannot be a C++ special name.
Benjamin Krameraccaf192012-11-14 15:08:31 +00002119 // Hence, no transformation is required for it.
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002120 DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00002121 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00002122 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002123 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00002124 /*instantiation*/ true,
2125 /*typename*/ true, D->getTypenameLoc());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00002126 if (UD)
John McCalled976492009-12-04 22:46:56 +00002127 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
2128
John McCall7ba107a2009-11-18 02:36:19 +00002129 return UD;
2130}
2131
2132Decl * TemplateDeclInstantiator
2133 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Douglas Gregor5149f372011-02-25 15:54:31 +00002134 NestedNameSpecifierLoc QualifierLoc
2135 = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), TemplateArgs);
2136 if (!QualifierLoc)
John McCall7ba107a2009-11-18 02:36:19 +00002137 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002138
John McCall7ba107a2009-11-18 02:36:19 +00002139 CXXScopeSpec SS;
Douglas Gregor5149f372011-02-25 15:54:31 +00002140 SS.Adopt(QualifierLoc);
John McCall7ba107a2009-11-18 02:36:19 +00002141
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002142 DeclarationNameInfo NameInfo
2143 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
2144
John McCall7ba107a2009-11-18 02:36:19 +00002145 NamedDecl *UD =
2146 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002147 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00002148 /*instantiation*/ true,
2149 /*typename*/ false, SourceLocation());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00002150 if (UD)
John McCalled976492009-12-04 22:46:56 +00002151 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
2152
Anders Carlsson0d8df782009-08-29 19:37:28 +00002153 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00002154}
2155
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002156
2157Decl *TemplateDeclInstantiator::VisitClassScopeFunctionSpecializationDecl(
2158 ClassScopeFunctionSpecializationDecl *Decl) {
2159 CXXMethodDecl *OldFD = Decl->getSpecialization();
Nico Weber6b020092012-06-25 17:21:05 +00002160 CXXMethodDecl *NewFD = cast<CXXMethodDecl>(VisitCXXMethodDecl(OldFD,
2161 0, true));
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002162
2163 LookupResult Previous(SemaRef, NewFD->getNameInfo(), Sema::LookupOrdinaryName,
2164 Sema::ForRedeclaration);
2165
Nico Weber6b020092012-06-25 17:21:05 +00002166 TemplateArgumentListInfo TemplateArgs;
2167 TemplateArgumentListInfo* TemplateArgsPtr = 0;
2168 if (Decl->hasExplicitTemplateArgs()) {
2169 TemplateArgs = Decl->templateArgs();
2170 TemplateArgsPtr = &TemplateArgs;
2171 }
2172
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002173 SemaRef.LookupQualifiedName(Previous, SemaRef.CurContext);
Nico Weber6b020092012-06-25 17:21:05 +00002174 if (SemaRef.CheckFunctionTemplateSpecialization(NewFD, TemplateArgsPtr,
2175 Previous)) {
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002176 NewFD->setInvalidDecl();
2177 return NewFD;
2178 }
2179
2180 // Associate the specialization with the pattern.
2181 FunctionDecl *Specialization = cast<FunctionDecl>(Previous.getFoundDecl());
2182 assert(Specialization && "Class scope Specialization is null");
2183 SemaRef.Context.setClassScopeSpecializationPattern(Specialization, OldFD);
2184
2185 return NewFD;
2186}
2187
Alexey Bataevc6400582013-03-22 06:34:35 +00002188Decl *TemplateDeclInstantiator::VisitOMPThreadPrivateDecl(
2189 OMPThreadPrivateDecl *D) {
2190 SmallVector<DeclRefExpr *, 5> Vars;
2191 for (ArrayRef<DeclRefExpr *>::iterator I = D->varlist_begin(),
2192 E = D->varlist_end();
2193 I != E; ++I) {
2194 Expr *Var = SemaRef.SubstExpr(*I, TemplateArgs).take();
2195 assert(isa<DeclRefExpr>(Var) && "threadprivate arg is not a DeclRefExpr");
2196 Vars.push_back(cast<DeclRefExpr>(Var));
2197 }
2198
2199 OMPThreadPrivateDecl *TD =
2200 SemaRef.CheckOMPThreadPrivateDecl(D->getLocation(), Vars);
2201
2202 return TD;
2203}
2204
John McCallce3ff2b2009-08-25 22:02:44 +00002205Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00002206 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00002207 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor2fa98002010-02-16 19:28:15 +00002208 if (D->isInvalidDecl())
2209 return 0;
2210
Douglas Gregor8dbc2692009-03-17 21:15:40 +00002211 return Instantiator.Visit(D);
2212}
2213
John McCalle29ba202009-08-20 01:44:21 +00002214/// \brief Instantiates a nested template parameter list in the current
2215/// instantiation context.
2216///
2217/// \param L The parameter list to instantiate
2218///
2219/// \returns NULL if there was an error
2220TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00002221TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00002222 // Get errors for all the parameters before bailing out.
2223 bool Invalid = false;
2224
2225 unsigned N = L->size();
Chris Lattner5f9e2722011-07-23 10:55:15 +00002226 typedef SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00002227 ParamVector Params;
2228 Params.reserve(N);
2229 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
2230 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002231 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00002232 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00002233 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00002234 }
2235
2236 // Clean up if we had an error.
Douglas Gregorff331c12010-07-25 18:17:45 +00002237 if (Invalid)
John McCalle29ba202009-08-20 01:44:21 +00002238 return NULL;
John McCalle29ba202009-08-20 01:44:21 +00002239
2240 TemplateParameterList *InstL
2241 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
2242 L->getLAngleLoc(), &Params.front(), N,
2243 L->getRAngleLoc());
2244 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00002245}
John McCalle29ba202009-08-20 01:44:21 +00002246
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002247/// \brief Instantiate the declaration of a class template partial
Douglas Gregored9c0f92009-10-29 00:04:11 +00002248/// specialization.
2249///
2250/// \param ClassTemplate the (instantiated) class template that is partially
2251// specialized by the instantiation of \p PartialSpec.
2252///
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002253/// \param PartialSpec the (uninstantiated) class template partial
Douglas Gregored9c0f92009-10-29 00:04:11 +00002254/// specialization that we are instantiating.
2255///
Douglas Gregord65587f2010-11-10 19:44:59 +00002256/// \returns The instantiated partial specialization, if successful; otherwise,
2257/// NULL to indicate an error.
2258ClassTemplatePartialSpecializationDecl *
Douglas Gregored9c0f92009-10-29 00:04:11 +00002259TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
2260 ClassTemplateDecl *ClassTemplate,
2261 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00002262 // Create a local instantiation scope for this class template partial
2263 // specialization, which will contain the instantiations of the template
2264 // parameters.
John McCall2a7fb272010-08-25 05:32:35 +00002265 LocalInstantiationScope Scope(SemaRef);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002266
Douglas Gregored9c0f92009-10-29 00:04:11 +00002267 // Substitute into the template parameters of the class template partial
2268 // specialization.
2269 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
2270 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
2271 if (!InstParams)
Douglas Gregord65587f2010-11-10 19:44:59 +00002272 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002273
Douglas Gregored9c0f92009-10-29 00:04:11 +00002274 // Substitute into the template arguments of the class template partial
2275 // specialization.
John McCalld5532b62009-11-23 01:53:49 +00002276 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002277 if (SemaRef.Subst(PartialSpec->getTemplateArgsAsWritten(),
2278 PartialSpec->getNumTemplateArgsAsWritten(),
Douglas Gregore02e2622010-12-22 21:19:48 +00002279 InstTemplateArgs, TemplateArgs))
2280 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002281
Douglas Gregored9c0f92009-10-29 00:04:11 +00002282 // Check that the template argument list is well-formed for this
2283 // class template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002284 SmallVector<TemplateArgument, 4> Converted;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002285 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002286 PartialSpec->getLocation(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002287 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002288 false,
2289 Converted))
Douglas Gregord65587f2010-11-10 19:44:59 +00002290 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00002291
2292 // Figure out where to insert this class template partial specialization
2293 // in the member template's set of class template partial specializations.
Douglas Gregored9c0f92009-10-29 00:04:11 +00002294 void *InsertPos = 0;
2295 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00002296 = ClassTemplate->findPartialSpecialization(Converted.data(),
2297 Converted.size(), InsertPos);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002298
Douglas Gregored9c0f92009-10-29 00:04:11 +00002299 // Build the canonical type that describes the converted template
2300 // arguments of the class template partial specialization.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002301 QualType CanonType
Douglas Gregored9c0f92009-10-29 00:04:11 +00002302 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
Douglas Gregor910f8002010-11-07 23:05:16 +00002303 Converted.data(),
2304 Converted.size());
Douglas Gregored9c0f92009-10-29 00:04:11 +00002305
2306 // Build the fully-sugared type for this class template
2307 // specialization as the user wrote in the specialization
2308 // itself. This means that we'll pretty-print the type retrieved
2309 // from the specialization's declaration the way that the user
2310 // actually wrote the specialization, rather than formatting the
2311 // name based on the "canonical" representation used to store the
2312 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00002313 TypeSourceInfo *WrittenTy
2314 = SemaRef.Context.getTemplateSpecializationTypeInfo(
2315 TemplateName(ClassTemplate),
2316 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00002317 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002318 CanonType);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002319
Douglas Gregored9c0f92009-10-29 00:04:11 +00002320 if (PrevDecl) {
2321 // We've already seen a partial specialization with the same template
2322 // parameters and template arguments. This can happen, for example, when
2323 // substituting the outer template arguments ends up causing two
2324 // class template partial specializations of a member class template
2325 // to have identical forms, e.g.,
2326 //
2327 // template<typename T, typename U>
2328 // struct Outer {
2329 // template<typename X, typename Y> struct Inner;
2330 // template<typename Y> struct Inner<T, Y>;
2331 // template<typename Y> struct Inner<U, Y>;
2332 // };
2333 //
2334 // Outer<int, int> outer; // error: the partial specializations of Inner
2335 // // have the same signature.
2336 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
Douglas Gregord65587f2010-11-10 19:44:59 +00002337 << WrittenTy->getType();
Douglas Gregored9c0f92009-10-29 00:04:11 +00002338 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
2339 << SemaRef.Context.getTypeDeclType(PrevDecl);
Douglas Gregord65587f2010-11-10 19:44:59 +00002340 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00002341 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002342
2343
Douglas Gregored9c0f92009-10-29 00:04:11 +00002344 // Create the class template partial specialization declaration.
2345 ClassTemplatePartialSpecializationDecl *InstPartialSpec
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002346 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00002347 PartialSpec->getTagKind(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002348 Owner,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002349 PartialSpec->getLocStart(),
2350 PartialSpec->getLocation(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002351 InstParams,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002352 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00002353 Converted.data(),
2354 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00002355 InstTemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00002356 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00002357 0,
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00002358 ClassTemplate->getNextPartialSpecSequenceNumber());
John McCallb6217662010-03-15 10:12:16 +00002359 // Substitute the nested name specifier, if any.
2360 if (SubstQualifier(PartialSpec, InstPartialSpec))
2361 return 0;
2362
Douglas Gregored9c0f92009-10-29 00:04:11 +00002363 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00002364 InstPartialSpec->setTypeAsWritten(WrittenTy);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002365
Douglas Gregored9c0f92009-10-29 00:04:11 +00002366 // Add this partial specialization to the set of class template partial
2367 // specializations.
Douglas Gregor1e1e9722012-03-28 14:34:23 +00002368 ClassTemplate->AddPartialSpecialization(InstPartialSpec, /*InsertPos=*/0);
Douglas Gregord65587f2010-11-10 19:44:59 +00002369 return InstPartialSpec;
Douglas Gregored9c0f92009-10-29 00:04:11 +00002370}
2371
John McCall21ef0fa2010-03-11 09:03:00 +00002372TypeSourceInfo*
John McCallce3ff2b2009-08-25 22:02:44 +00002373TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002374 SmallVectorImpl<ParmVarDecl *> &Params) {
John McCall21ef0fa2010-03-11 09:03:00 +00002375 TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
2376 assert(OldTInfo && "substituting function without type source info");
2377 assert(Params.empty() && "parameter vector is non-empty at start");
Douglas Gregorcefc3af2012-04-16 07:05:22 +00002378
2379 CXXRecordDecl *ThisContext = 0;
2380 unsigned ThisTypeQuals = 0;
2381 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2382 ThisContext = Method->getParent();
2383 ThisTypeQuals = Method->getTypeQualifiers();
2384 }
2385
John McCall6cd3b9f2010-04-09 17:38:44 +00002386 TypeSourceInfo *NewTInfo
2387 = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
2388 D->getTypeSpecStartLoc(),
Douglas Gregorcefc3af2012-04-16 07:05:22 +00002389 D->getDeclName(),
2390 ThisContext, ThisTypeQuals);
John McCall21ef0fa2010-03-11 09:03:00 +00002391 if (!NewTInfo)
2392 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +00002393
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00002394 if (NewTInfo != OldTInfo) {
2395 // Get parameters from the new type info.
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00002396 TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
David Blaikie39e6ab42013-02-18 22:06:02 +00002397 if (FunctionProtoTypeLoc OldProtoLoc =
2398 OldTL.getAs<FunctionProtoTypeLoc>()) {
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00002399 TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens();
David Blaikie39e6ab42013-02-18 22:06:02 +00002400 FunctionProtoTypeLoc NewProtoLoc = NewTL.castAs<FunctionProtoTypeLoc>();
Richard Smith500d7292012-07-18 01:29:05 +00002401 unsigned NewIdx = 0;
David Blaikie39e6ab42013-02-18 22:06:02 +00002402 for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc.getNumArgs();
Douglas Gregor12c9c002011-01-07 16:43:16 +00002403 OldIdx != NumOldParams; ++OldIdx) {
David Blaikie39e6ab42013-02-18 22:06:02 +00002404 ParmVarDecl *OldParam = OldProtoLoc.getArg(OldIdx);
Richard Smith500d7292012-07-18 01:29:05 +00002405 LocalInstantiationScope *Scope = SemaRef.CurrentInstantiationScope;
2406
David Blaikiedc84cd52013-02-20 22:23:23 +00002407 Optional<unsigned> NumArgumentsInExpansion;
Richard Smith500d7292012-07-18 01:29:05 +00002408 if (OldParam->isParameterPack())
2409 NumArgumentsInExpansion =
2410 SemaRef.getNumArgumentsInExpansion(OldParam->getType(),
2411 TemplateArgs);
2412 if (!NumArgumentsInExpansion) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002413 // Simple case: normal parameter, or a parameter pack that's
Douglas Gregor12c9c002011-01-07 16:43:16 +00002414 // instantiated to a (still-dependent) parameter pack.
David Blaikie39e6ab42013-02-18 22:06:02 +00002415 ParmVarDecl *NewParam = NewProtoLoc.getArg(NewIdx++);
Douglas Gregor12c9c002011-01-07 16:43:16 +00002416 Params.push_back(NewParam);
Richard Smith500d7292012-07-18 01:29:05 +00002417 Scope->InstantiatedLocal(OldParam, NewParam);
2418 } else {
2419 // Parameter pack expansion: make the instantiation an argument pack.
2420 Scope->MakeInstantiatedLocalArgPack(OldParam);
2421 for (unsigned I = 0; I != *NumArgumentsInExpansion; ++I) {
David Blaikie39e6ab42013-02-18 22:06:02 +00002422 ParmVarDecl *NewParam = NewProtoLoc.getArg(NewIdx++);
Richard Smith500d7292012-07-18 01:29:05 +00002423 Params.push_back(NewParam);
2424 Scope->InstantiatedLocalPackArg(OldParam, NewParam);
2425 }
Douglas Gregor12c9c002011-01-07 16:43:16 +00002426 }
Douglas Gregor6920cdc2010-05-03 15:32:18 +00002427 }
Douglas Gregor895162d2010-04-30 18:55:50 +00002428 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00002429 } else {
2430 // The function type itself was not dependent and therefore no
2431 // substitution occurred. However, we still need to instantiate
2432 // the function parameters themselves.
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00002433 TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
David Blaikie39e6ab42013-02-18 22:06:02 +00002434 if (FunctionProtoTypeLoc OldProtoLoc =
2435 OldTL.getAs<FunctionProtoTypeLoc>()) {
2436 for (unsigned i = 0, i_end = OldProtoLoc.getNumArgs(); i != i_end; ++i) {
2437 ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc.getArg(i));
Douglas Gregor6920cdc2010-05-03 15:32:18 +00002438 if (!Parm)
2439 return 0;
2440 Params.push_back(Parm);
2441 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00002442 }
2443 }
John McCall21ef0fa2010-03-11 09:03:00 +00002444 return NewTInfo;
Douglas Gregor5545e162009-03-24 00:38:23 +00002445}
2446
Richard Smithe6975e92012-04-17 00:58:00 +00002447/// Introduce the instantiated function parameters into the local
2448/// instantiation scope, and set the parameter names to those used
2449/// in the template.
2450static void addInstantiatedParametersToScope(Sema &S, FunctionDecl *Function,
2451 const FunctionDecl *PatternDecl,
2452 LocalInstantiationScope &Scope,
2453 const MultiLevelTemplateArgumentList &TemplateArgs) {
2454 unsigned FParamIdx = 0;
2455 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
2456 const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
2457 if (!PatternParam->isParameterPack()) {
2458 // Simple case: not a parameter pack.
2459 assert(FParamIdx < Function->getNumParams());
2460 ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
2461 FunctionParam->setDeclName(PatternParam->getDeclName());
2462 Scope.InstantiatedLocal(PatternParam, FunctionParam);
2463 ++FParamIdx;
2464 continue;
2465 }
2466
2467 // Expand the parameter pack.
2468 Scope.MakeInstantiatedLocalArgPack(PatternParam);
David Blaikiedc84cd52013-02-20 22:23:23 +00002469 Optional<unsigned> NumArgumentsInExpansion
Richard Smithe6975e92012-04-17 00:58:00 +00002470 = S.getNumArgumentsInExpansion(PatternParam->getType(), TemplateArgs);
Richard Smith500d7292012-07-18 01:29:05 +00002471 assert(NumArgumentsInExpansion &&
2472 "should only be called when all template arguments are known");
2473 for (unsigned Arg = 0; Arg < *NumArgumentsInExpansion; ++Arg) {
Richard Smithe6975e92012-04-17 00:58:00 +00002474 ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
2475 FunctionParam->setDeclName(PatternParam->getDeclName());
2476 Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam);
2477 ++FParamIdx;
2478 }
2479 }
2480}
2481
2482static void InstantiateExceptionSpec(Sema &SemaRef, FunctionDecl *New,
2483 const FunctionProtoType *Proto,
2484 const MultiLevelTemplateArgumentList &TemplateArgs) {
Richard Smith13bffc52012-04-19 00:08:28 +00002485 assert(Proto->getExceptionSpecType() != EST_Uninstantiated);
2486
Richard Smithe6975e92012-04-17 00:58:00 +00002487 // C++11 [expr.prim.general]p3:
2488 // If a declaration declares a member function or member function
2489 // template of a class X, the expression this is a prvalue of type
2490 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
2491 // and the end of the function-definition, member-declarator, or
2492 // declarator.
2493 CXXRecordDecl *ThisContext = 0;
2494 unsigned ThisTypeQuals = 0;
2495 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(New)) {
2496 ThisContext = Method->getParent();
2497 ThisTypeQuals = Method->getTypeQualifiers();
2498 }
2499 Sema::CXXThisScopeRAII ThisScope(SemaRef, ThisContext, ThisTypeQuals,
Richard Smith80ad52f2013-01-02 11:42:31 +00002500 SemaRef.getLangOpts().CPlusPlus11);
Richard Smithe6975e92012-04-17 00:58:00 +00002501
2502 // The function has an exception specification or a "noreturn"
2503 // attribute. Substitute into each of the exception types.
2504 SmallVector<QualType, 4> Exceptions;
2505 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
2506 // FIXME: Poor location information!
2507 if (const PackExpansionType *PackExpansion
2508 = Proto->getExceptionType(I)->getAs<PackExpansionType>()) {
2509 // We have a pack expansion. Instantiate it.
2510 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2511 SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(),
2512 Unexpanded);
2513 assert(!Unexpanded.empty() &&
2514 "Pack expansion without parameter packs?");
2515
2516 bool Expand = false;
2517 bool RetainExpansion = false;
David Blaikiedc84cd52013-02-20 22:23:23 +00002518 Optional<unsigned> NumExpansions
Richard Smithe6975e92012-04-17 00:58:00 +00002519 = PackExpansion->getNumExpansions();
2520 if (SemaRef.CheckParameterPacksForExpansion(New->getLocation(),
2521 SourceRange(),
2522 Unexpanded,
2523 TemplateArgs,
2524 Expand,
2525 RetainExpansion,
2526 NumExpansions))
2527 break;
2528
2529 if (!Expand) {
2530 // We can't expand this pack expansion into separate arguments yet;
2531 // just substitute into the pattern and create a new pack expansion
2532 // type.
2533 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
2534 QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2535 TemplateArgs,
2536 New->getLocation(), New->getDeclName());
2537 if (T.isNull())
2538 break;
2539
2540 T = SemaRef.Context.getPackExpansionType(T, NumExpansions);
2541 Exceptions.push_back(T);
2542 continue;
2543 }
2544
2545 // Substitute into the pack expansion pattern for each template
2546 bool Invalid = false;
2547 for (unsigned ArgIdx = 0; ArgIdx != *NumExpansions; ++ArgIdx) {
2548 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, ArgIdx);
2549
2550 QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2551 TemplateArgs,
2552 New->getLocation(), New->getDeclName());
2553 if (T.isNull()) {
2554 Invalid = true;
2555 break;
2556 }
2557
2558 Exceptions.push_back(T);
2559 }
2560
2561 if (Invalid)
2562 break;
2563
2564 continue;
2565 }
2566
2567 QualType T
2568 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
2569 New->getLocation(), New->getDeclName());
2570 if (T.isNull() ||
2571 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
2572 continue;
2573
2574 Exceptions.push_back(T);
2575 }
2576 Expr *NoexceptExpr = 0;
2577 if (Expr *OldNoexceptExpr = Proto->getNoexceptExpr()) {
2578 EnterExpressionEvaluationContext Unevaluated(SemaRef,
2579 Sema::ConstantEvaluated);
2580 ExprResult E = SemaRef.SubstExpr(OldNoexceptExpr, TemplateArgs);
2581 if (E.isUsable())
2582 E = SemaRef.CheckBooleanCondition(E.get(), E.get()->getLocStart());
2583
2584 if (E.isUsable()) {
2585 NoexceptExpr = E.take();
2586 if (!NoexceptExpr->isTypeDependent() &&
2587 !NoexceptExpr->isValueDependent())
Douglas Gregorab41fe92012-05-04 22:38:52 +00002588 NoexceptExpr
2589 = SemaRef.VerifyIntegerConstantExpression(NoexceptExpr,
2590 0, diag::err_noexcept_needs_constant_expression,
2591 /*AllowFold*/ false).take();
Richard Smithe6975e92012-04-17 00:58:00 +00002592 }
2593 }
2594
2595 // Rebuild the function type
2596 const FunctionProtoType *NewProto
2597 = New->getType()->getAs<FunctionProtoType>();
2598 assert(NewProto && "Template instantiation without function prototype?");
2599
2600 FunctionProtoType::ExtProtoInfo EPI = NewProto->getExtProtoInfo();
2601 EPI.ExceptionSpecType = Proto->getExceptionSpecType();
2602 EPI.NumExceptions = Exceptions.size();
2603 EPI.Exceptions = Exceptions.data();
2604 EPI.NoexceptExpr = NoexceptExpr;
2605
2606 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
Jordan Rosebea522f2013-03-08 21:51:21 +00002607 ArrayRef<QualType>(NewProto->arg_type_begin(),
2608 NewProto->getNumArgs()),
Richard Smithe6975e92012-04-17 00:58:00 +00002609 EPI));
2610}
2611
2612void Sema::InstantiateExceptionSpec(SourceLocation PointOfInstantiation,
2613 FunctionDecl *Decl) {
Richard Smith13bffc52012-04-19 00:08:28 +00002614 const FunctionProtoType *Proto = Decl->getType()->castAs<FunctionProtoType>();
2615 if (Proto->getExceptionSpecType() != EST_Uninstantiated)
Richard Smithe6975e92012-04-17 00:58:00 +00002616 return;
2617
2618 InstantiatingTemplate Inst(*this, PointOfInstantiation, Decl,
2619 InstantiatingTemplate::ExceptionSpecification());
Richard Smithb9d0b762012-07-27 04:22:15 +00002620 if (Inst) {
2621 // We hit the instantiation depth limit. Clear the exception specification
2622 // so that our callers don't have to cope with EST_Uninstantiated.
2623 FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
2624 EPI.ExceptionSpecType = EST_None;
2625 Decl->setType(Context.getFunctionType(Proto->getResultType(),
Jordan Rosebea522f2013-03-08 21:51:21 +00002626 ArrayRef<QualType>(Proto->arg_type_begin(),
2627 Proto->getNumArgs()),
Richard Smithb9d0b762012-07-27 04:22:15 +00002628 EPI));
Richard Smithe6975e92012-04-17 00:58:00 +00002629 return;
Richard Smithb9d0b762012-07-27 04:22:15 +00002630 }
Richard Smithe6975e92012-04-17 00:58:00 +00002631
2632 // Enter the scope of this instantiation. We don't use
2633 // PushDeclContext because we don't have a scope.
2634 Sema::ContextRAII savedContext(*this, Decl);
2635 LocalInstantiationScope Scope(*this);
2636
2637 MultiLevelTemplateArgumentList TemplateArgs =
2638 getTemplateInstantiationArgs(Decl, 0, /*RelativeToPrimary*/true);
2639
Richard Smith13bffc52012-04-19 00:08:28 +00002640 FunctionDecl *Template = Proto->getExceptionSpecTemplate();
2641 addInstantiatedParametersToScope(*this, Decl, Template, Scope, TemplateArgs);
Richard Smithe6975e92012-04-17 00:58:00 +00002642
Richard Smith13bffc52012-04-19 00:08:28 +00002643 ::InstantiateExceptionSpec(*this, Decl,
2644 Template->getType()->castAs<FunctionProtoType>(),
2645 TemplateArgs);
Richard Smithe6975e92012-04-17 00:58:00 +00002646}
2647
Mike Stump1eb44332009-09-09 15:08:12 +00002648/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00002649/// declaration (New) from the corresponding fields of its template (Tmpl).
2650///
2651/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00002652bool
2653TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00002654 FunctionDecl *Tmpl) {
David Blaikie85f485a2012-07-16 18:50:45 +00002655 if (Tmpl->isDeleted())
Sean Hunt10620eb2011-05-06 20:44:56 +00002656 New->setDeletedAsWritten();
Mike Stump1eb44332009-09-09 15:08:12 +00002657
Douglas Gregorcca9e962009-07-01 22:01:06 +00002658 // If we are performing substituting explicitly-specified template arguments
2659 // or deduced template arguments into a function template and we reach this
2660 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00002661 // to keeping the new function template specialization. We therefore
2662 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00002663 // into a template instantiation for this specific function template
2664 // specialization, which is not a SFINAE context, so that we diagnose any
2665 // further errors in the declaration itself.
2666 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
2667 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
2668 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
2669 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00002670 if (FunctionTemplateDecl *FunTmpl
Nick Lewycky4a9e60f2012-11-16 08:40:59 +00002671 = dyn_cast<FunctionTemplateDecl>(ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002672 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00002673 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00002674 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00002675 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
Nick Lewycky4a9e60f2012-11-16 08:40:59 +00002676 ActiveInst.Entity = New;
Douglas Gregorcca9e962009-07-01 22:01:06 +00002677 }
2678 }
Mike Stump1eb44332009-09-09 15:08:12 +00002679
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002680 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
2681 assert(Proto && "Function template without prototype?");
2682
Sebastian Redl60618fa2011-03-12 11:50:43 +00002683 if (Proto->hasExceptionSpec() || Proto->getNoReturnAttr()) {
John McCalle23cf432010-12-14 08:05:40 +00002684 FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
John McCalle23cf432010-12-14 08:05:40 +00002685
Richard Smithe6975e92012-04-17 00:58:00 +00002686 // DR1330: In C++11, defer instantiation of a non-trivial
2687 // exception specification.
Richard Smith80ad52f2013-01-02 11:42:31 +00002688 if (SemaRef.getLangOpts().CPlusPlus11 &&
Richard Smithe6975e92012-04-17 00:58:00 +00002689 EPI.ExceptionSpecType != EST_None &&
2690 EPI.ExceptionSpecType != EST_DynamicNone &&
2691 EPI.ExceptionSpecType != EST_BasicNoexcept) {
Richard Smith13bffc52012-04-19 00:08:28 +00002692 FunctionDecl *ExceptionSpecTemplate = Tmpl;
2693 if (EPI.ExceptionSpecType == EST_Uninstantiated)
2694 ExceptionSpecTemplate = EPI.ExceptionSpecTemplate;
Richard Smith4841ca52013-04-10 05:48:59 +00002695 ExceptionSpecificationType NewEST = EST_Uninstantiated;
2696 if (EPI.ExceptionSpecType == EST_Unevaluated)
2697 NewEST = EST_Unevaluated;
Richard Smith13bffc52012-04-19 00:08:28 +00002698
Richard Smithe6975e92012-04-17 00:58:00 +00002699 // Mark the function has having an uninstantiated exception specification.
2700 const FunctionProtoType *NewProto
2701 = New->getType()->getAs<FunctionProtoType>();
2702 assert(NewProto && "Template instantiation without function prototype?");
2703 EPI = NewProto->getExtProtoInfo();
Richard Smith4841ca52013-04-10 05:48:59 +00002704 EPI.ExceptionSpecType = NewEST;
Richard Smithe6975e92012-04-17 00:58:00 +00002705 EPI.ExceptionSpecDecl = New;
Richard Smith13bffc52012-04-19 00:08:28 +00002706 EPI.ExceptionSpecTemplate = ExceptionSpecTemplate;
Richard Smithe6975e92012-04-17 00:58:00 +00002707 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
Jordan Rosebea522f2013-03-08 21:51:21 +00002708 ArrayRef<QualType>(NewProto->arg_type_begin(),
2709 NewProto->getNumArgs()),
Richard Smithe6975e92012-04-17 00:58:00 +00002710 EPI));
2711 } else {
2712 ::InstantiateExceptionSpec(SemaRef, New, Proto, TemplateArgs);
2713 }
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002714 }
2715
Rafael Espindola19f74ac2011-07-06 15:46:09 +00002716 // Get the definition. Leaves the variable unchanged if undefined.
Richard Smithe6975e92012-04-17 00:58:00 +00002717 const FunctionDecl *Definition = Tmpl;
Rafael Espindola19f74ac2011-07-06 15:46:09 +00002718 Tmpl->isDefined(Definition);
2719
DeLesley Hutchins23323e02012-01-20 22:50:54 +00002720 SemaRef.InstantiateAttrs(TemplateArgs, Definition, New,
2721 LateAttrs, StartingScope);
Douglas Gregor7cf84d62010-06-15 17:05:35 +00002722
Douglas Gregore53060f2009-06-25 22:08:12 +00002723 return false;
2724}
2725
Douglas Gregor5545e162009-03-24 00:38:23 +00002726/// \brief Initializes common fields of an instantiated method
2727/// declaration (New) from the corresponding fields of its template
2728/// (Tmpl).
2729///
2730/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00002731bool
2732TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00002733 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00002734 if (InitFunctionInstantiation(New, Tmpl))
2735 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002736
Douglas Gregor5545e162009-03-24 00:38:23 +00002737 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00002738 if (Tmpl->isVirtualAsWritten())
Douglas Gregor85606eb2010-09-28 20:50:54 +00002739 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +00002740
Douglas Gregor5545e162009-03-24 00:38:23 +00002741 // FIXME: New needs a pointer to Tmpl
2742 return false;
2743}
Douglas Gregora58861f2009-05-13 20:28:22 +00002744
2745/// \brief Instantiate the definition of the given function from its
2746/// template.
2747///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002748/// \param PointOfInstantiation the point at which the instantiation was
2749/// required. Note that this is not precisely a "point of instantiation"
2750/// for the function, but it's close.
2751///
Douglas Gregora58861f2009-05-13 20:28:22 +00002752/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002753/// function template specialization or member function of a class template
2754/// specialization.
2755///
2756/// \param Recursive if true, recursively instantiates any functions that
2757/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002758///
2759/// \param DefinitionRequired if true, then we are performing an explicit
2760/// instantiation where the body of the function is required. Complain if
2761/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002762void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002763 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002764 bool Recursive,
2765 bool DefinitionRequired) {
Sean Hunt10620eb2011-05-06 20:44:56 +00002766 if (Function->isInvalidDecl() || Function->isDefined())
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002767 return;
2768
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002769 // Never instantiate an explicit specialization except if it is a class scope
2770 // explicit specialization.
2771 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization &&
2772 !Function->getClassScopeSpecializationPattern())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002773 return;
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002774
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002775 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00002776 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Sean Huntf996e052011-05-27 20:00:14 +00002777 assert(PatternDecl && "instantiating a non-template");
2778
2779 Stmt *Pattern = PatternDecl->getBody(PatternDecl);
2780 assert(PatternDecl && "template definition is not a template");
2781 if (!Pattern) {
2782 // Try to find a defaulted definition
2783 PatternDecl->isDefined(PatternDecl);
Sean Huntdfab8542011-05-25 22:02:25 +00002784 }
Sean Huntf996e052011-05-27 20:00:14 +00002785 assert(PatternDecl && "template definition is not a template");
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002786
Francois Pichet8387e2a2011-04-22 22:18:13 +00002787 // Postpone late parsed template instantiations.
Sean Huntf996e052011-05-27 20:00:14 +00002788 if (PatternDecl->isLateTemplateParsed() &&
Nick Lewycky8a29bc02011-05-12 03:51:24 +00002789 !LateTemplateParser) {
Francois Pichet8387e2a2011-04-22 22:18:13 +00002790 PendingInstantiations.push_back(
2791 std::make_pair(Function, PointOfInstantiation));
2792 return;
2793 }
2794
2795 // Call the LateTemplateParser callback if there a need to late parse
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002796 // a templated function definition.
Sean Huntf996e052011-05-27 20:00:14 +00002797 if (!Pattern && PatternDecl->isLateTemplateParsed() &&
Francois Pichet8387e2a2011-04-22 22:18:13 +00002798 LateTemplateParser) {
Francois Pichet4a47e8d2011-04-23 11:52:20 +00002799 LateTemplateParser(OpaqueParser, PatternDecl);
Francois Pichet8387e2a2011-04-22 22:18:13 +00002800 Pattern = PatternDecl->getBody(PatternDecl);
2801 }
2802
Sean Huntf996e052011-05-27 20:00:14 +00002803 if (!Pattern && !PatternDecl->isDefaulted()) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002804 if (DefinitionRequired) {
2805 if (Function->getPrimaryTemplate())
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002806 Diag(PointOfInstantiation,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002807 diag::err_explicit_instantiation_undefined_func_template)
2808 << Function->getPrimaryTemplate();
2809 else
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002810 Diag(PointOfInstantiation,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002811 diag::err_explicit_instantiation_undefined_member)
2812 << 1 << Function->getDeclName() << Function->getDeclContext();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002813
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002814 if (PatternDecl)
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002815 Diag(PatternDecl->getLocation(),
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002816 diag::note_explicit_instantiation_here);
Douglas Gregorcfe833b2010-05-17 17:57:54 +00002817 Function->setInvalidDecl();
Chandler Carruth58e390e2010-08-25 08:27:02 +00002818 } else if (Function->getTemplateSpecializationKind()
2819 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002820 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00002821 std::make_pair(Function, PointOfInstantiation));
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002822 }
Chandler Carruth58e390e2010-08-25 08:27:02 +00002823
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002824 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002825 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002826
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002827 // C++0x [temp.explicit]p9:
2828 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00002829 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002830 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00002831 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002832 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002833 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002834 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002835
Richard Smithd4497dd2013-01-25 00:08:28 +00002836 if (PatternDecl->isInlined())
2837 Function->setImplicitlyInline();
2838
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002839 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2840 if (Inst)
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002841 return;
2842
Abramo Bagnarae9946242011-11-18 08:08:52 +00002843 // Copy the inner loc start from the pattern.
2844 Function->setInnerLocStart(PatternDecl->getInnerLocStart());
2845
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002846 // If we're performing recursive template instantiation, create our own
2847 // queue of pending implicit instantiations that we will instantiate later,
2848 // while we're still within our own instantiation context.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002849 SmallVector<VTableUse, 16> SavedVTableUses;
Chandler Carruth62c78d52010-08-25 08:44:16 +00002850 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002851 if (Recursive) {
2852 VTableUses.swap(SavedVTableUses);
Chandler Carruth62c78d52010-08-25 08:44:16 +00002853 PendingInstantiations.swap(SavedPendingInstantiations);
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002854 }
Mike Stump1eb44332009-09-09 15:08:12 +00002855
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002856 EnterExpressionEvaluationContext EvalContext(*this,
John McCallf312b1e2010-08-26 23:41:50 +00002857 Sema::PotentiallyEvaluated);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002858
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002859 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00002860 // recorded, unless we're actually a member function within a local
2861 // class, in which case we need to merge our results with the parent
2862 // scope (of the enclosing function).
2863 bool MergeWithParentScope = false;
2864 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
2865 MergeWithParentScope = Rec->isLocalClass();
2866
2867 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002868
Richard Smith1d28caf2012-12-11 01:14:52 +00002869 if (PatternDecl->isDefaulted())
Sean Huntcd10dec2011-05-23 23:14:04 +00002870 SetDeclDefaulted(Function, PatternDecl->getLocation());
Richard Smith1d28caf2012-12-11 01:14:52 +00002871 else {
2872 ActOnStartOfFunctionDef(0, Function);
2873
2874 // Enter the scope of this instantiation. We don't use
2875 // PushDeclContext because we don't have a scope.
2876 Sema::ContextRAII savedContext(*this, Function);
2877
2878 MultiLevelTemplateArgumentList TemplateArgs =
2879 getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
2880
2881 addInstantiatedParametersToScope(*this, Function, PatternDecl, Scope,
2882 TemplateArgs);
2883
Sean Huntcd10dec2011-05-23 23:14:04 +00002884 // If this is a constructor, instantiate the member initializers.
2885 if (const CXXConstructorDecl *Ctor =
2886 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2887 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2888 TemplateArgs);
2889 }
2890
2891 // Instantiate the function body.
2892 StmtResult Body = SubstStmt(Pattern, TemplateArgs);
2893
2894 if (Body.isInvalid())
2895 Function->setInvalidDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002896
Sean Huntcd10dec2011-05-23 23:14:04 +00002897 ActOnFinishFunctionBody(Function, Body.get(),
2898 /*IsInstantiation=*/true);
Richard Smith1d28caf2012-12-11 01:14:52 +00002899
2900 PerformDependentDiagnostics(PatternDecl, TemplateArgs);
2901
2902 savedContext.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00002903 }
2904
Douglas Gregoraba43bb2009-05-26 20:50:29 +00002905 DeclGroupRef DG(Function);
2906 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00002907
Douglas Gregor60406be2010-01-16 22:29:39 +00002908 // This class may have local implicit instantiations that need to be
2909 // instantiation within this scope.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002910 PerformPendingInstantiations(/*LocalOnly=*/true);
Douglas Gregor60406be2010-01-16 22:29:39 +00002911 Scope.Exit();
2912
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002913 if (Recursive) {
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002914 // Define any pending vtables.
2915 DefineUsedVTables();
2916
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002917 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002918 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002919 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002920
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002921 // Restore the set of pending vtables.
Nick Lewycky81559102011-05-31 07:58:42 +00002922 assert(VTableUses.empty() &&
2923 "VTableUses should be empty before it is discarded.");
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002924 VTableUses.swap(SavedVTableUses);
2925
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002926 // Restore the set of pending implicit instantiations.
Nick Lewycky81559102011-05-31 07:58:42 +00002927 assert(PendingInstantiations.empty() &&
2928 "PendingInstantiations should be empty before it is discarded.");
Chandler Carruth62c78d52010-08-25 08:44:16 +00002929 PendingInstantiations.swap(SavedPendingInstantiations);
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002930 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002931}
2932
2933/// \brief Instantiate the definition of the given variable from its
2934/// template.
2935///
Douglas Gregor7caa6822009-07-24 20:34:43 +00002936/// \param PointOfInstantiation the point at which the instantiation was
2937/// required. Note that this is not precisely a "point of instantiation"
2938/// for the function, but it's close.
2939///
2940/// \param Var the already-instantiated declaration of a static member
2941/// variable of a class template specialization.
2942///
2943/// \param Recursive if true, recursively instantiates any functions that
2944/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002945///
2946/// \param DefinitionRequired if true, then we are performing an explicit
2947/// instantiation where an out-of-line definition of the member variable
2948/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002949void Sema::InstantiateStaticDataMemberDefinition(
2950 SourceLocation PointOfInstantiation,
2951 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002952 bool Recursive,
2953 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002954 if (Var->isInvalidDecl())
2955 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002956
Douglas Gregor7caa6822009-07-24 20:34:43 +00002957 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002958 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002959 assert(Def && "This data member was not instantiated from a template?");
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002960 assert(Def->isStaticDataMember() && "Not a static data member?");
Douglas Gregor0d035142009-10-27 18:42:08 +00002961 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002962
Douglas Gregor0d035142009-10-27 18:42:08 +00002963 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002964 // We did not find an out-of-line definition of this static data member,
2965 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00002966 // instantiate this definition (or provide a specialization for it) in
2967 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002968 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00002969 Def = Var->getInstantiatedFromStaticDataMember();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002970 Diag(PointOfInstantiation,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002971 diag::err_explicit_instantiation_undefined_member)
2972 << 2 << Var->getDeclName() << Var->getDeclContext();
2973 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
Chandler Carruth58e390e2010-08-25 08:27:02 +00002974 } else if (Var->getTemplateSpecializationKind()
2975 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002976 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00002977 std::make_pair(Var, PointOfInstantiation));
2978 }
2979
Douglas Gregor7caa6822009-07-24 20:34:43 +00002980 return;
2981 }
2982
Rafael Espindola234fe652012-03-05 10:54:55 +00002983 TemplateSpecializationKind TSK = Var->getTemplateSpecializationKind();
2984
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002985 // Never instantiate an explicit specialization.
Rafael Espindola234fe652012-03-05 10:54:55 +00002986 if (TSK == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002987 return;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002988
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002989 // C++0x [temp.explicit]p9:
2990 // Except for inline functions, other explicit instantiation declarations
2991 // have the effect of suppressing the implicit instantiation of the entity
2992 // to which they refer.
Rafael Espindola234fe652012-03-05 10:54:55 +00002993 if (TSK == TSK_ExplicitInstantiationDeclaration)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002994 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002995
Argyrios Kyrtzidisafda9052013-02-24 00:05:01 +00002996 // Make sure to pass the instantiated variable to the consumer at the end.
2997 struct PassToConsumerRAII {
2998 ASTConsumer &Consumer;
2999 VarDecl *Var;
3000
3001 PassToConsumerRAII(ASTConsumer &Consumer, VarDecl *Var)
3002 : Consumer(Consumer), Var(Var) { }
3003
3004 ~PassToConsumerRAII() {
3005 Consumer.HandleCXXStaticMemberVarInstantiation(Var);
3006 }
3007 } PassToConsumerRAII(Consumer, Var);
Rafael Espindola02503932012-03-08 15:51:03 +00003008
Douglas Gregorf15748a2011-06-03 03:35:07 +00003009 // If we already have a definition, we're done.
Nick Lewycky95e38722012-04-04 02:38:36 +00003010 if (VarDecl *Def = Var->getDefinition()) {
3011 // We may be explicitly instantiating something we've already implicitly
3012 // instantiated.
3013 Def->setTemplateSpecializationKind(Var->getTemplateSpecializationKind(),
3014 PointOfInstantiation);
Douglas Gregorf15748a2011-06-03 03:35:07 +00003015 return;
Nick Lewycky95e38722012-04-04 02:38:36 +00003016 }
Douglas Gregorf15748a2011-06-03 03:35:07 +00003017
Douglas Gregor7caa6822009-07-24 20:34:43 +00003018 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
3019 if (Inst)
3020 return;
Mike Stump1eb44332009-09-09 15:08:12 +00003021
Douglas Gregor7caa6822009-07-24 20:34:43 +00003022 // If we're performing recursive template instantiation, create our own
3023 // queue of pending implicit instantiations that we will instantiate later,
3024 // while we're still within our own instantiation context.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003025 SmallVector<VTableUse, 16> SavedVTableUses;
Chandler Carruth62c78d52010-08-25 08:44:16 +00003026 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Nick Lewycky81559102011-05-31 07:58:42 +00003027 if (Recursive) {
3028 VTableUses.swap(SavedVTableUses);
Chandler Carruth62c78d52010-08-25 08:44:16 +00003029 PendingInstantiations.swap(SavedPendingInstantiations);
Nick Lewycky81559102011-05-31 07:58:42 +00003030 }
Mike Stump1eb44332009-09-09 15:08:12 +00003031
Douglas Gregor7caa6822009-07-24 20:34:43 +00003032 // Enter the scope of this instantiation. We don't use
3033 // PushDeclContext because we don't have a scope.
John McCallf5ba7e02011-02-14 20:37:25 +00003034 ContextRAII previousContext(*this, Var->getDeclContext());
Douglas Gregor7bdc1522012-02-16 21:36:18 +00003035 LocalInstantiationScope Local(*this);
3036
Douglas Gregor1028c9f2009-10-14 21:29:40 +00003037 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00003038 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Nico Weber6bb4dcb2010-11-28 22:53:37 +00003039 getTemplateInstantiationArgs(Var)));
John McCallf5ba7e02011-02-14 20:37:25 +00003040
3041 previousContext.pop();
Douglas Gregor7caa6822009-07-24 20:34:43 +00003042
3043 if (Var) {
Argyrios Kyrtzidisafda9052013-02-24 00:05:01 +00003044 PassToConsumerRAII.Var = Var;
Douglas Gregor583f33b2009-10-15 18:07:02 +00003045 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
3046 assert(MSInfo && "Missing member specialization information?");
3047 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
3048 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00003049 }
Douglas Gregor7bdc1522012-02-16 21:36:18 +00003050 Local.Exit();
3051
Douglas Gregor7caa6822009-07-24 20:34:43 +00003052 if (Recursive) {
Nick Lewycky81559102011-05-31 07:58:42 +00003053 // Define any newly required vtables.
3054 DefineUsedVTables();
3055
Douglas Gregor7caa6822009-07-24 20:34:43 +00003056 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00003057 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003058 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00003059
Nick Lewycky81559102011-05-31 07:58:42 +00003060 // Restore the set of pending vtables.
3061 assert(VTableUses.empty() &&
3062 "VTableUses should be empty before it is discarded, "
3063 "while instantiating static data member.");
3064 VTableUses.swap(SavedVTableUses);
3065
Douglas Gregor7caa6822009-07-24 20:34:43 +00003066 // Restore the set of pending implicit instantiations.
Nick Lewycky81559102011-05-31 07:58:42 +00003067 assert(PendingInstantiations.empty() &&
3068 "PendingInstantiations should be empty before it is discarded, "
3069 "while instantiating static data member.");
Chandler Carruth62c78d52010-08-25 08:44:16 +00003070 PendingInstantiations.swap(SavedPendingInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00003071 }
Douglas Gregora58861f2009-05-13 20:28:22 +00003072}
Douglas Gregor815215d2009-05-27 05:35:12 +00003073
Anders Carlsson09025312009-08-29 05:16:22 +00003074void
3075Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
3076 const CXXConstructorDecl *Tmpl,
3077 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00003078
Richard Trieu90ab75b2011-09-09 03:18:59 +00003079 SmallVector<CXXCtorInitializer*, 4> NewInits;
Richard Smith54b3ba82012-09-25 00:23:05 +00003080 bool AnyErrors = Tmpl->isInvalidDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003081
Anders Carlsson09025312009-08-29 05:16:22 +00003082 // Instantiate all the initializers.
3083 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00003084 InitsEnd = Tmpl->init_end();
3085 Inits != InitsEnd; ++Inits) {
Sean Huntcbb67482011-01-08 20:30:50 +00003086 CXXCtorInitializer *Init = *Inits;
Anders Carlsson09025312009-08-29 05:16:22 +00003087
Chandler Carruth030ef472010-09-03 21:54:20 +00003088 // Only instantiate written initializers, let Sema re-construct implicit
3089 // ones.
3090 if (!Init->isWritten())
3091 continue;
3092
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003093 SourceLocation EllipsisLoc;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003094
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003095 if (Init->isPackExpansion()) {
3096 // This is a pack expansion. We should expand it now.
Douglas Gregor76852c22011-11-01 01:16:03 +00003097 TypeLoc BaseTL = Init->getTypeSourceInfo()->getTypeLoc();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003098 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003099 collectUnexpandedParameterPacks(BaseTL, Unexpanded);
3100 bool ShouldExpand = false;
Douglas Gregord3731192011-01-10 07:32:04 +00003101 bool RetainExpansion = false;
David Blaikiedc84cd52013-02-20 22:23:23 +00003102 Optional<unsigned> NumExpansions;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003103 if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(),
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003104 BaseTL.getSourceRange(),
David Blaikiea71f9d02011-09-22 02:34:54 +00003105 Unexpanded,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003106 TemplateArgs, ShouldExpand,
Douglas Gregord3731192011-01-10 07:32:04 +00003107 RetainExpansion,
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003108 NumExpansions)) {
3109 AnyErrors = true;
3110 New->setInvalidDecl();
3111 continue;
3112 }
3113 assert(ShouldExpand && "Partial instantiation of base initializer?");
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003114
3115 // Loop over all of the arguments in the argument pack(s),
Douglas Gregorcded4f62011-01-14 17:04:44 +00003116 for (unsigned I = 0; I != *NumExpansions; ++I) {
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003117 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
3118
3119 // Instantiate the initializer.
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003120 ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs,
3121 /*CXXDirectInit=*/true);
3122 if (TempInit.isInvalid()) {
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003123 AnyErrors = true;
3124 break;
3125 }
3126
3127 // Instantiate the base type.
Douglas Gregor76852c22011-11-01 01:16:03 +00003128 TypeSourceInfo *BaseTInfo = SubstType(Init->getTypeSourceInfo(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003129 TemplateArgs,
3130 Init->getSourceLocation(),
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003131 New->getDeclName());
3132 if (!BaseTInfo) {
3133 AnyErrors = true;
3134 break;
3135 }
3136
3137 // Build the initializer.
Sebastian Redl6df65482011-09-24 17:48:25 +00003138 MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(),
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003139 BaseTInfo, TempInit.take(),
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003140 New->getParent(),
3141 SourceLocation());
3142 if (NewInit.isInvalid()) {
3143 AnyErrors = true;
3144 break;
3145 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003146
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003147 NewInits.push_back(NewInit.get());
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003148 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003149
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003150 continue;
3151 }
3152
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00003153 // Instantiate the initializer.
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003154 ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs,
3155 /*CXXDirectInit=*/true);
3156 if (TempInit.isInvalid()) {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00003157 AnyErrors = true;
3158 continue;
Anders Carlsson09025312009-08-29 05:16:22 +00003159 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003160
Anders Carlsson09025312009-08-29 05:16:22 +00003161 MemInitResult NewInit;
Douglas Gregor76852c22011-11-01 01:16:03 +00003162 if (Init->isDelegatingInitializer() || Init->isBaseInitializer()) {
3163 TypeSourceInfo *TInfo = SubstType(Init->getTypeSourceInfo(),
3164 TemplateArgs,
3165 Init->getSourceLocation(),
3166 New->getDeclName());
3167 if (!TInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00003168 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00003169 New->setInvalidDecl();
3170 continue;
3171 }
Sebastian Redl6df65482011-09-24 17:48:25 +00003172
Douglas Gregor76852c22011-11-01 01:16:03 +00003173 if (Init->isBaseInitializer())
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003174 NewInit = BuildBaseInitializer(TInfo->getType(), TInfo, TempInit.take(),
Douglas Gregor76852c22011-11-01 01:16:03 +00003175 New->getParent(), EllipsisLoc);
3176 else
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003177 NewInit = BuildDelegatingInitializer(TInfo, TempInit.take(),
Douglas Gregor76852c22011-11-01 01:16:03 +00003178 cast<CXXRecordDecl>(CurContext->getParent()));
Anders Carlsson09025312009-08-29 05:16:22 +00003179 } else if (Init->isMemberInitializer()) {
Douglas Gregorb7107222011-03-04 19:46:35 +00003180 FieldDecl *Member = cast_or_null<FieldDecl>(FindInstantiatedDecl(
Francois Pichet00eb3f92010-12-04 09:14:42 +00003181 Init->getMemberLocation(),
3182 Init->getMember(),
3183 TemplateArgs));
Douglas Gregorb7107222011-03-04 19:46:35 +00003184 if (!Member) {
3185 AnyErrors = true;
3186 New->setInvalidDecl();
3187 continue;
3188 }
Mike Stump1eb44332009-09-09 15:08:12 +00003189
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003190 NewInit = BuildMemberInitializer(Member, TempInit.take(),
Sebastian Redl6df65482011-09-24 17:48:25 +00003191 Init->getSourceLocation());
Francois Pichet00eb3f92010-12-04 09:14:42 +00003192 } else if (Init->isIndirectMemberInitializer()) {
3193 IndirectFieldDecl *IndirectMember =
Douglas Gregorb7107222011-03-04 19:46:35 +00003194 cast_or_null<IndirectFieldDecl>(FindInstantiatedDecl(
Francois Pichet00eb3f92010-12-04 09:14:42 +00003195 Init->getMemberLocation(),
3196 Init->getIndirectMember(), TemplateArgs));
3197
Douglas Gregorb7107222011-03-04 19:46:35 +00003198 if (!IndirectMember) {
3199 AnyErrors = true;
3200 New->setInvalidDecl();
Sebastian Redl6df65482011-09-24 17:48:25 +00003201 continue;
Douglas Gregorb7107222011-03-04 19:46:35 +00003202 }
Sebastian Redl6df65482011-09-24 17:48:25 +00003203
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003204 NewInit = BuildMemberInitializer(IndirectMember, TempInit.take(),
Sebastian Redl6df65482011-09-24 17:48:25 +00003205 Init->getSourceLocation());
Anders Carlsson09025312009-08-29 05:16:22 +00003206 }
3207
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00003208 if (NewInit.isInvalid()) {
3209 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00003210 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00003211 } else {
Richard Trieu90ab75b2011-09-09 03:18:59 +00003212 NewInits.push_back(NewInit.get());
Anders Carlsson09025312009-08-29 05:16:22 +00003213 }
3214 }
Mike Stump1eb44332009-09-09 15:08:12 +00003215
Anders Carlsson09025312009-08-29 05:16:22 +00003216 // Assign all the initializers to the new constructor.
John McCalld226f652010-08-21 09:40:31 +00003217 ActOnMemInitializers(New,
Anders Carlsson09025312009-08-29 05:16:22 +00003218 /*FIXME: ColonLoc */
3219 SourceLocation(),
David Blaikie93c86172013-01-17 05:26:25 +00003220 NewInits,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00003221 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00003222}
3223
John McCall52a575a2009-08-29 08:11:13 +00003224// TODO: this could be templated if the various decl types used the
3225// same method name.
3226static bool isInstantiationOf(ClassTemplateDecl *Pattern,
3227 ClassTemplateDecl *Instance) {
3228 Pattern = Pattern->getCanonicalDecl();
3229
3230 do {
3231 Instance = Instance->getCanonicalDecl();
3232 if (Pattern == Instance) return true;
3233 Instance = Instance->getInstantiatedFromMemberTemplate();
3234 } while (Instance);
3235
3236 return false;
3237}
3238
Douglas Gregor0d696532009-09-28 06:34:35 +00003239static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
3240 FunctionTemplateDecl *Instance) {
3241 Pattern = Pattern->getCanonicalDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003242
Douglas Gregor0d696532009-09-28 06:34:35 +00003243 do {
3244 Instance = Instance->getCanonicalDecl();
3245 if (Pattern == Instance) return true;
3246 Instance = Instance->getInstantiatedFromMemberTemplate();
3247 } while (Instance);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003248
Douglas Gregor0d696532009-09-28 06:34:35 +00003249 return false;
3250}
3251
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003252static bool
Douglas Gregored9c0f92009-10-29 00:04:11 +00003253isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
3254 ClassTemplatePartialSpecializationDecl *Instance) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003255 Pattern
Douglas Gregored9c0f92009-10-29 00:04:11 +00003256 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
3257 do {
3258 Instance = cast<ClassTemplatePartialSpecializationDecl>(
3259 Instance->getCanonicalDecl());
3260 if (Pattern == Instance)
3261 return true;
3262 Instance = Instance->getInstantiatedFromMember();
3263 } while (Instance);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003264
Douglas Gregored9c0f92009-10-29 00:04:11 +00003265 return false;
3266}
3267
John McCall52a575a2009-08-29 08:11:13 +00003268static bool isInstantiationOf(CXXRecordDecl *Pattern,
3269 CXXRecordDecl *Instance) {
3270 Pattern = Pattern->getCanonicalDecl();
3271
3272 do {
3273 Instance = Instance->getCanonicalDecl();
3274 if (Pattern == Instance) return true;
3275 Instance = Instance->getInstantiatedFromMemberClass();
3276 } while (Instance);
3277
3278 return false;
3279}
3280
3281static bool isInstantiationOf(FunctionDecl *Pattern,
3282 FunctionDecl *Instance) {
3283 Pattern = Pattern->getCanonicalDecl();
3284
3285 do {
3286 Instance = Instance->getCanonicalDecl();
3287 if (Pattern == Instance) return true;
3288 Instance = Instance->getInstantiatedFromMemberFunction();
3289 } while (Instance);
3290
3291 return false;
3292}
3293
3294static bool isInstantiationOf(EnumDecl *Pattern,
3295 EnumDecl *Instance) {
3296 Pattern = Pattern->getCanonicalDecl();
3297
3298 do {
3299 Instance = Instance->getCanonicalDecl();
3300 if (Pattern == Instance) return true;
3301 Instance = Instance->getInstantiatedFromMemberEnum();
3302 } while (Instance);
3303
3304 return false;
3305}
3306
John McCalled976492009-12-04 22:46:56 +00003307static bool isInstantiationOf(UsingShadowDecl *Pattern,
3308 UsingShadowDecl *Instance,
3309 ASTContext &C) {
3310 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
3311}
3312
3313static bool isInstantiationOf(UsingDecl *Pattern,
3314 UsingDecl *Instance,
3315 ASTContext &C) {
3316 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
3317}
3318
John McCall7ba107a2009-11-18 02:36:19 +00003319static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
3320 UsingDecl *Instance,
3321 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00003322 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00003323}
3324
3325static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00003326 UsingDecl *Instance,
3327 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00003328 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00003329}
3330
John McCall52a575a2009-08-29 08:11:13 +00003331static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
3332 VarDecl *Instance) {
3333 assert(Instance->isStaticDataMember());
3334
3335 Pattern = Pattern->getCanonicalDecl();
3336
3337 do {
3338 Instance = Instance->getCanonicalDecl();
3339 if (Pattern == Instance) return true;
3340 Instance = Instance->getInstantiatedFromStaticDataMember();
3341 } while (Instance);
3342
3343 return false;
3344}
3345
John McCalled976492009-12-04 22:46:56 +00003346// Other is the prospective instantiation
3347// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00003348static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00003349 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00003350 if (UnresolvedUsingTypenameDecl *UUD
3351 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
3352 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
3353 return isInstantiationOf(UUD, UD, Ctx);
3354 }
3355 }
3356
3357 if (UnresolvedUsingValueDecl *UUD
3358 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00003359 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
3360 return isInstantiationOf(UUD, UD, Ctx);
3361 }
3362 }
Douglas Gregor815215d2009-05-27 05:35:12 +00003363
Anders Carlsson0d8df782009-08-29 19:37:28 +00003364 return false;
3365 }
Mike Stump1eb44332009-09-09 15:08:12 +00003366
John McCall52a575a2009-08-29 08:11:13 +00003367 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
3368 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00003369
John McCall52a575a2009-08-29 08:11:13 +00003370 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
3371 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00003372
John McCall52a575a2009-08-29 08:11:13 +00003373 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
3374 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00003375
Douglas Gregor7caa6822009-07-24 20:34:43 +00003376 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00003377 if (Var->isStaticDataMember())
3378 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
3379
3380 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
3381 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00003382
Douglas Gregor0d696532009-09-28 06:34:35 +00003383 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
3384 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
3385
Douglas Gregored9c0f92009-10-29 00:04:11 +00003386 if (ClassTemplatePartialSpecializationDecl *PartialSpec
3387 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
3388 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
3389 PartialSpec);
3390
Anders Carlssond8b285f2009-09-01 04:26:58 +00003391 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
3392 if (!Field->getDeclName()) {
3393 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00003394 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00003395 cast<FieldDecl>(D);
3396 }
3397 }
Mike Stump1eb44332009-09-09 15:08:12 +00003398
John McCalled976492009-12-04 22:46:56 +00003399 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
3400 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
3401
3402 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
3403 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
3404
Douglas Gregor815215d2009-05-27 05:35:12 +00003405 return D->getDeclName() && isa<NamedDecl>(Other) &&
3406 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
3407}
3408
3409template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00003410static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00003411 NamedDecl *D,
3412 ForwardIterator first,
3413 ForwardIterator last) {
3414 for (; first != last; ++first)
3415 if (isInstantiationOf(Ctx, D, *first))
3416 return cast<NamedDecl>(*first);
3417
3418 return 0;
3419}
3420
John McCall02cace72009-08-28 07:59:38 +00003421/// \brief Finds the instantiation of the given declaration context
3422/// within the current instantiation.
3423///
3424/// \returns NULL if there was an error
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003425DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
Douglas Gregore95b4092009-09-16 18:34:49 +00003426 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00003427 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003428 Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00003429 return cast_or_null<DeclContext>(ID);
3430 } else return DC;
3431}
3432
Douglas Gregored961e72009-05-27 17:54:46 +00003433/// \brief Find the instantiation of the given declaration within the
3434/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00003435///
3436/// This routine is intended to be used when \p D is a declaration
3437/// referenced from within a template, that needs to mapped into the
3438/// corresponding declaration within an instantiation. For example,
3439/// given:
3440///
3441/// \code
3442/// template<typename T>
3443/// struct X {
3444/// enum Kind {
3445/// KnownValue = sizeof(T)
3446/// };
3447///
3448/// bool getKind() const { return KnownValue; }
3449/// };
3450///
3451/// template struct X<int>;
3452/// \endcode
3453///
3454/// In the instantiation of X<int>::getKind(), we need to map the
3455/// EnumConstantDecl for KnownValue (which refers to
James Dennettf198d122012-06-17 03:36:08 +00003456/// X<T>::\<Kind>\::KnownValue) to its instantiation
James Dennettef2b5b32012-06-15 22:23:43 +00003457/// (X<int>::\<Kind>\::KnownValue). InstantiateCurrentDeclRef() performs
Douglas Gregored961e72009-05-27 17:54:46 +00003458/// this mapping from within the instantiation of X<int>.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003459NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
Douglas Gregore95b4092009-09-16 18:34:49 +00003460 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00003461 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00003462 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00003463 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
Douglas Gregor7bdc1522012-02-16 21:36:18 +00003464 (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext()) ||
3465 (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda())) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00003466 // D is a local of some kind. Look into the map of local
3467 // declarations to their instantiations.
Chris Lattnerd8e54992011-02-17 19:47:42 +00003468 typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
3469 llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found
3470 = CurrentInstantiationScope->findInstantiationOf(D);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003471
Chris Lattner57ad3782011-02-17 20:34:02 +00003472 if (Found) {
3473 if (Decl *FD = Found->dyn_cast<Decl *>())
3474 return cast<NamedDecl>(FD);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003475
Richard Smith9a4db032012-09-12 00:56:43 +00003476 int PackIdx = ArgumentPackSubstitutionIndex;
3477 assert(PackIdx != -1 && "found declaration pack but not pack expanding");
Chris Lattner57ad3782011-02-17 20:34:02 +00003478 return cast<NamedDecl>((*Found->get<DeclArgumentPack *>())[PackIdx]);
3479 }
3480
3481 // If we didn't find the decl, then we must have a label decl that hasn't
3482 // been found yet. Lazily instantiate it and return it now.
3483 assert(isa<LabelDecl>(D));
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003484
Chris Lattner57ad3782011-02-17 20:34:02 +00003485 Decl *Inst = SubstDecl(D, CurContext, TemplateArgs);
3486 assert(Inst && "Failed to instantiate label??");
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003487
Chris Lattner57ad3782011-02-17 20:34:02 +00003488 CurrentInstantiationScope->InstantiatedLocal(D, Inst);
3489 return cast<LabelDecl>(Inst);
Douglas Gregor2bba76b2009-05-27 17:07:49 +00003490 }
Douglas Gregor815215d2009-05-27 05:35:12 +00003491
Douglas Gregore95b4092009-09-16 18:34:49 +00003492 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
3493 if (!Record->isDependentContext())
3494 return D;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003495
Douglas Gregor2c1227c2011-11-07 17:43:18 +00003496 // Determine whether this record is the "templated" declaration describing
3497 // a class template or class template partial specialization.
Douglas Gregore95b4092009-09-16 18:34:49 +00003498 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
Douglas Gregor2c1227c2011-11-07 17:43:18 +00003499 if (ClassTemplate)
3500 ClassTemplate = ClassTemplate->getCanonicalDecl();
3501 else if (ClassTemplatePartialSpecializationDecl *PartialSpec
3502 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record))
3503 ClassTemplate = PartialSpec->getSpecializedTemplate()->getCanonicalDecl();
3504
3505 // Walk the current context to find either the record or an instantiation of
3506 // it.
3507 DeclContext *DC = CurContext;
3508 while (!DC->isFileContext()) {
3509 // If we're performing substitution while we're inside the template
3510 // definition, we'll find our own context. We're done.
3511 if (DC->Equals(Record))
3512 return Record;
3513
3514 if (CXXRecordDecl *InstRecord = dyn_cast<CXXRecordDecl>(DC)) {
3515 // Check whether we're in the process of instantiating a class template
3516 // specialization of the template we're mapping.
3517 if (ClassTemplateSpecializationDecl *InstSpec
3518 = dyn_cast<ClassTemplateSpecializationDecl>(InstRecord)){
3519 ClassTemplateDecl *SpecTemplate = InstSpec->getSpecializedTemplate();
3520 if (ClassTemplate && isInstantiationOf(ClassTemplate, SpecTemplate))
3521 return InstRecord;
3522 }
3523
3524 // Check whether we're in the process of instantiating a member class.
3525 if (isInstantiationOf(Record, InstRecord))
3526 return InstRecord;
Douglas Gregore95b4092009-09-16 18:34:49 +00003527 }
Douglas Gregor2c1227c2011-11-07 17:43:18 +00003528
3529
3530 // Move to the outer template scope.
3531 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) {
3532 if (FD->getFriendObjectKind() && FD->getDeclContext()->isFileContext()){
3533 DC = FD->getLexicalDeclContext();
3534 continue;
3535 }
John McCall52a575a2009-08-29 08:11:13 +00003536 }
Douglas Gregor2c1227c2011-11-07 17:43:18 +00003537
3538 DC = DC->getParent();
John McCall52a575a2009-08-29 08:11:13 +00003539 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00003540
Douglas Gregore95b4092009-09-16 18:34:49 +00003541 // Fall through to deal with other dependent record types (e.g.,
3542 // anonymous unions in class templates).
3543 }
John McCall52a575a2009-08-29 08:11:13 +00003544
Douglas Gregore95b4092009-09-16 18:34:49 +00003545 if (!ParentDC->isDependentContext())
3546 return D;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003547
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003548 ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00003549 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00003550 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003551
Douglas Gregor815215d2009-05-27 05:35:12 +00003552 if (ParentDC != D->getDeclContext()) {
3553 // We performed some kind of instantiation in the parent context,
3554 // so now we need to look into the instantiated parent context to
3555 // find the instantiation of the declaration D.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003556
John McCall3cb0ebd2010-03-10 03:28:59 +00003557 // If our context used to be dependent, we may need to instantiate
3558 // it before performing lookup into that context.
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00003559 bool IsBeingInstantiated = false;
John McCall3cb0ebd2010-03-10 03:28:59 +00003560 if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003561 if (!Spec->isDependentContext()) {
3562 QualType T = Context.getTypeDeclType(Spec);
John McCall3cb0ebd2010-03-10 03:28:59 +00003563 const RecordType *Tag = T->getAs<RecordType>();
3564 assert(Tag && "type of non-dependent record is not a RecordType");
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00003565 if (Tag->isBeingDefined())
3566 IsBeingInstantiated = true;
John McCall3cb0ebd2010-03-10 03:28:59 +00003567 if (!Tag->isBeingDefined() &&
3568 RequireCompleteType(Loc, T, diag::err_incomplete_type))
3569 return 0;
Douglas Gregora43064c2010-11-05 23:22:45 +00003570
3571 ParentDC = Tag->getDecl();
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003572 }
3573 }
3574
Douglas Gregor815215d2009-05-27 05:35:12 +00003575 NamedDecl *Result = 0;
3576 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003577 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
David Blaikie3bc93e32012-12-19 00:45:41 +00003578 Result = findInstantiationOf(Context, D, Found.begin(), Found.end());
Douglas Gregor815215d2009-05-27 05:35:12 +00003579 } else {
3580 // Since we don't have a name for the entity we're looking for,
3581 // our only option is to walk through all of the declarations to
3582 // find that name. This will occur in a few cases:
3583 //
3584 // - anonymous struct/union within a template
3585 // - unnamed class/struct/union/enum within a template
3586 //
3587 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00003588 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003589 ParentDC->decls_begin(),
3590 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00003591 }
Mike Stump1eb44332009-09-09 15:08:12 +00003592
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00003593 if (!Result) {
3594 if (isa<UsingShadowDecl>(D)) {
3595 // UsingShadowDecls can instantiate to nothing because of using hiding.
3596 } else if (Diags.hasErrorOccurred()) {
3597 // We've already complained about something, so most likely this
3598 // declaration failed to instantiate. There's no point in complaining
3599 // further, since this is normal in invalid code.
3600 } else if (IsBeingInstantiated) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003601 // The class in which this member exists is currently being
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00003602 // instantiated, and we haven't gotten around to instantiating this
3603 // member yet. This can happen when the code uses forward declarations
3604 // of member classes, and introduces ordering dependencies via
3605 // template instantiation.
3606 Diag(Loc, diag::err_member_not_yet_instantiated)
3607 << D->getDeclName()
3608 << Context.getTypeDeclType(cast<CXXRecordDecl>(ParentDC));
3609 Diag(D->getLocation(), diag::note_non_instantiated_member_here);
Richard Smith0724b7c2012-03-26 20:28:16 +00003610 } else if (EnumConstantDecl *ED = dyn_cast<EnumConstantDecl>(D)) {
3611 // This enumeration constant was found when the template was defined,
3612 // but can't be found in the instantiation. This can happen if an
3613 // unscoped enumeration member is explicitly specialized.
3614 EnumDecl *Enum = cast<EnumDecl>(ED->getLexicalDeclContext());
3615 EnumDecl *Spec = cast<EnumDecl>(FindInstantiatedDecl(Loc, Enum,
3616 TemplateArgs));
3617 assert(Spec->getTemplateSpecializationKind() ==
3618 TSK_ExplicitSpecialization);
3619 Diag(Loc, diag::err_enumerator_does_not_exist)
3620 << D->getDeclName()
3621 << Context.getTypeDeclType(cast<TypeDecl>(Spec->getDeclContext()));
3622 Diag(Spec->getLocation(), diag::note_enum_specialized_here)
3623 << Context.getTypeDeclType(Spec);
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00003624 } else {
3625 // We should have found something, but didn't.
3626 llvm_unreachable("Unable to find instantiation of declaration!");
3627 }
3628 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003629
Douglas Gregor815215d2009-05-27 05:35:12 +00003630 D = Result;
3631 }
3632
Douglas Gregor815215d2009-05-27 05:35:12 +00003633 return D;
3634}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003635
Mike Stump1eb44332009-09-09 15:08:12 +00003636/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003637/// instantiations we have seen until this point.
Nick Lewycky81559102011-05-31 07:58:42 +00003638void Sema::PerformPendingInstantiations(bool LocalOnly) {
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00003639 // Load pending instantiations from the external source.
3640 if (!LocalOnly && ExternalSource) {
Richard Smithb9d0b762012-07-27 04:22:15 +00003641 SmallVector<PendingImplicitInstantiation, 4> Pending;
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00003642 ExternalSource->ReadPendingInstantiations(Pending);
3643 PendingInstantiations.insert(PendingInstantiations.begin(),
3644 Pending.begin(), Pending.end());
3645 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003646
Douglas Gregor60406be2010-01-16 22:29:39 +00003647 while (!PendingLocalImplicitInstantiations.empty() ||
Chandler Carruth62c78d52010-08-25 08:44:16 +00003648 (!LocalOnly && !PendingInstantiations.empty())) {
Douglas Gregor60406be2010-01-16 22:29:39 +00003649 PendingImplicitInstantiation Inst;
3650
3651 if (PendingLocalImplicitInstantiations.empty()) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00003652 Inst = PendingInstantiations.front();
3653 PendingInstantiations.pop_front();
Douglas Gregor60406be2010-01-16 22:29:39 +00003654 } else {
3655 Inst = PendingLocalImplicitInstantiations.front();
3656 PendingLocalImplicitInstantiations.pop_front();
3657 }
Mike Stump1eb44332009-09-09 15:08:12 +00003658
Douglas Gregor7caa6822009-07-24 20:34:43 +00003659 // Instantiate function definitions
3660 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
John McCallf312b1e2010-08-26 23:41:50 +00003661 PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(),
3662 "instantiating function definition");
Chandler Carruth58e390e2010-08-25 08:27:02 +00003663 bool DefinitionRequired = Function->getTemplateSpecializationKind() ==
3664 TSK_ExplicitInstantiationDefinition;
3665 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true,
3666 DefinitionRequired);
Douglas Gregor7caa6822009-07-24 20:34:43 +00003667 continue;
3668 }
Mike Stump1eb44332009-09-09 15:08:12 +00003669
Douglas Gregor7caa6822009-07-24 20:34:43 +00003670 // Instantiate static data member definitions.
3671 VarDecl *Var = cast<VarDecl>(Inst.first);
3672 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00003673
Chandler Carruth291b4412010-02-13 10:17:50 +00003674 // Don't try to instantiate declarations if the most recent redeclaration
3675 // is invalid.
Douglas Gregoref96ee02012-01-14 16:38:05 +00003676 if (Var->getMostRecentDecl()->isInvalidDecl())
Chandler Carruth291b4412010-02-13 10:17:50 +00003677 continue;
3678
3679 // Check if the most recent declaration has changed the specialization kind
3680 // and removed the need for implicit instantiation.
Douglas Gregoref96ee02012-01-14 16:38:05 +00003681 switch (Var->getMostRecentDecl()->getTemplateSpecializationKind()) {
Chandler Carruth291b4412010-02-13 10:17:50 +00003682 case TSK_Undeclared:
David Blaikieb219cfc2011-09-23 05:06:16 +00003683 llvm_unreachable("Cannot instantitiate an undeclared specialization.");
Chandler Carruth291b4412010-02-13 10:17:50 +00003684 case TSK_ExplicitInstantiationDeclaration:
Chandler Carruth291b4412010-02-13 10:17:50 +00003685 case TSK_ExplicitSpecialization:
Chandler Carruth58e390e2010-08-25 08:27:02 +00003686 continue; // No longer need to instantiate this type.
3687 case TSK_ExplicitInstantiationDefinition:
3688 // We only need an instantiation if the pending instantiation *is* the
3689 // explicit instantiation.
Douglas Gregoref96ee02012-01-14 16:38:05 +00003690 if (Var != Var->getMostRecentDecl()) continue;
Chandler Carruth291b4412010-02-13 10:17:50 +00003691 case TSK_ImplicitInstantiation:
3692 break;
3693 }
3694
John McCallf312b1e2010-08-26 23:41:50 +00003695 PrettyDeclStackTraceEntry CrashInfo(*this, Var, Var->getLocation(),
3696 "instantiating static data member "
3697 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00003698
Chandler Carruth58e390e2010-08-25 08:27:02 +00003699 bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
3700 TSK_ExplicitInstantiationDefinition;
3701 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true,
3702 DefinitionRequired);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003703 }
3704}
John McCall0c01d182010-03-24 05:22:00 +00003705
3706void Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
3707 const MultiLevelTemplateArgumentList &TemplateArgs) {
3708 for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
3709 E = Pattern->ddiag_end(); I != E; ++I) {
3710 DependentDiagnostic *DD = *I;
3711
3712 switch (DD->getKind()) {
3713 case DependentDiagnostic::Access:
3714 HandleDependentAccessCheck(*DD, TemplateArgs);
3715 break;
3716 }
3717 }
3718}