blob: 0182b3dcc40044c6e7d1d6a61b660b75a3331fea [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 Gregore737f502010-08-12 20:07:10 +000013#include "clang/Sema/Lookup.h"
John McCallf312b1e2010-08-26 23:41:50 +000014#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall7cd088e2010-08-24 07:21:54 +000015#include "clang/Sema/Template.h"
Douglas Gregoraba43bb2009-05-26 20:50:29 +000016#include "clang/AST/ASTConsumer.h"
Douglas Gregor8dbc2692009-03-17 21:15:40 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/DeclTemplate.h"
19#include "clang/AST/DeclVisitor.h"
John McCall0c01d182010-03-24 05:22:00 +000020#include "clang/AST/DependentDiagnostic.h"
Douglas Gregor8dbc2692009-03-17 21:15:40 +000021#include "clang/AST/Expr.h"
Douglas Gregora88cfbf2009-12-12 18:16:41 +000022#include "clang/AST/ExprCXX.h"
John McCall21ef0fa2010-03-11 09:03:00 +000023#include "clang/AST/TypeLoc.h"
Douglas Gregor83ddad32009-08-26 21:14:46 +000024#include "clang/Lex/Preprocessor.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
John McCall1d8d1cc2010-08-01 02:01:53 +000063void Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs,
DeLesley Hutchins23323e02012-01-20 22:50:54 +000064 const Decl *Tmpl, Decl *New,
65 LateInstantiatedAttrVec *LateAttrs,
66 LocalInstantiationScope *OuterMostScope) {
Sean Huntcf807c42010-08-18 23:23:40 +000067 for (AttrVec::const_iterator i = Tmpl->attr_begin(), e = Tmpl->attr_end();
68 i != e; ++i) {
69 const Attr *TmplAttr = *i;
DeLesley Hutchins23323e02012-01-20 22:50:54 +000070
Chandler Carruth4ced79f2010-06-25 03:22:07 +000071 // FIXME: This should be generalized to more than just the AlignedAttr.
72 if (const AlignedAttr *Aligned = dyn_cast<AlignedAttr>(TmplAttr)) {
Sean Huntcf807c42010-08-18 23:23:40 +000073 if (Aligned->isAlignmentDependent()) {
Sean Huntcf807c42010-08-18 23:23:40 +000074 if (Aligned->isAlignmentExpr()) {
Richard Smithf6702a32011-12-20 02:08:33 +000075 // The alignment expression is a constant expression.
76 EnterExpressionEvaluationContext Unevaluated(*this,
77 Sema::ConstantEvaluated);
78
John McCall60d7b3a2010-08-24 06:29:42 +000079 ExprResult Result = SubstExpr(Aligned->getAlignmentExpr(),
Nick Lewycky7663f392010-11-20 01:29:55 +000080 TemplateArgs);
Sean Huntcf807c42010-08-18 23:23:40 +000081 if (!Result.isInvalid())
82 AddAlignedAttr(Aligned->getLocation(), New, Result.takeAs<Expr>());
Richard Smithf6702a32011-12-20 02:08:33 +000083 } else {
Sean Huntcf807c42010-08-18 23:23:40 +000084 TypeSourceInfo *Result = SubstType(Aligned->getAlignmentType(),
Nick Lewycky7663f392010-11-20 01:29:55 +000085 TemplateArgs,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +000086 Aligned->getLocation(),
Nick Lewycky7663f392010-11-20 01:29:55 +000087 DeclarationName());
Sean Huntcf807c42010-08-18 23:23:40 +000088 if (Result)
89 AddAlignedAttr(Aligned->getLocation(), New, Result);
90 }
Chandler Carruth4ced79f2010-06-25 03:22:07 +000091 continue;
92 }
93 }
94
DeLesley Hutchins23323e02012-01-20 22:50:54 +000095 if (TmplAttr->isLateParsed() && LateAttrs) {
96 // Late parsed attributes must be instantiated and attached after the
97 // enclosing class has been instantiated. See Sema::InstantiateClass.
98 LocalInstantiationScope *Saved = 0;
99 if (CurrentInstantiationScope)
100 Saved = CurrentInstantiationScope->cloneScopes(OuterMostScope);
101 LateAttrs->push_back(LateInstantiatedAttribute(TmplAttr, Saved, New));
102 } else {
Benjamin Kramer5bbc3852012-02-06 11:13:08 +0000103 Attr *NewAttr = sema::instantiateTemplateAttribute(TmplAttr, Context,
104 *this, TemplateArgs);
Rafael Espindola31c195a2012-05-15 14:09:55 +0000105 if (NewAttr)
106 New->addAttr(NewAttr);
DeLesley Hutchins23323e02012-01-20 22:50:54 +0000107 }
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000108 }
109}
110
Douglas Gregor4f722be2009-03-25 15:45:12 +0000111Decl *
112TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000113 llvm_unreachable("Translation units cannot be instantiated");
Douglas Gregor4f722be2009-03-25 15:45:12 +0000114}
115
116Decl *
Chris Lattner57ad3782011-02-17 20:34:02 +0000117TemplateDeclInstantiator::VisitLabelDecl(LabelDecl *D) {
118 LabelDecl *Inst = LabelDecl::Create(SemaRef.Context, Owner, D->getLocation(),
119 D->getIdentifier());
120 Owner->addDecl(Inst);
121 return Inst;
122}
123
124Decl *
Douglas Gregor4f722be2009-03-25 15:45:12 +0000125TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000126 llvm_unreachable("Namespaces cannot be instantiated");
Douglas Gregor4f722be2009-03-25 15:45:12 +0000127}
128
John McCall3dbd3d52010-02-16 06:53:13 +0000129Decl *
130TemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
131 NamespaceAliasDecl *Inst
132 = NamespaceAliasDecl::Create(SemaRef.Context, Owner,
133 D->getNamespaceLoc(),
134 D->getAliasLoc(),
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +0000135 D->getIdentifier(),
136 D->getQualifierLoc(),
John McCall3dbd3d52010-02-16 06:53:13 +0000137 D->getTargetNameLoc(),
138 D->getNamespace());
139 Owner->addDecl(Inst);
140 return Inst;
141}
142
Richard Smith3e4c6c42011-05-05 21:57:07 +0000143Decl *TemplateDeclInstantiator::InstantiateTypedefNameDecl(TypedefNameDecl *D,
144 bool IsTypeAlias) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000145 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000146 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor561f8122011-07-01 01:22:09 +0000147 if (DI->getType()->isInstantiationDependentType() ||
Douglas Gregor836adf62010-05-24 17:22:01 +0000148 DI->getType()->isVariablyModifiedType()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000149 DI = SemaRef.SubstType(DI, TemplateArgs,
150 D->getLocation(), D->getDeclName());
151 if (!DI) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000152 Invalid = true;
John McCalla93c9342009-12-07 02:54:59 +0000153 DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000154 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000155 } else {
156 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000157 }
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000159 // Create the new typedef
Richard Smith162e1c12011-04-15 14:24:37 +0000160 TypedefNameDecl *Typedef;
161 if (IsTypeAlias)
162 Typedef = TypeAliasDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
163 D->getLocation(), D->getIdentifier(), DI);
164 else
165 Typedef = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
166 D->getLocation(), D->getIdentifier(), DI);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000167 if (Invalid)
168 Typedef->setInvalidDecl();
169
John McCallcde5a402011-02-01 08:20:08 +0000170 // If the old typedef was the name for linkage purposes of an anonymous
171 // tag decl, re-establish that relationship for the new typedef.
172 if (const TagType *oldTagType = D->getUnderlyingType()->getAs<TagType>()) {
173 TagDecl *oldTag = oldTagType->getDecl();
Richard Smith162e1c12011-04-15 14:24:37 +0000174 if (oldTag->getTypedefNameForAnonDecl() == D) {
John McCallcde5a402011-02-01 08:20:08 +0000175 TagDecl *newTag = DI->getType()->castAs<TagType>()->getDecl();
Richard Smith162e1c12011-04-15 14:24:37 +0000176 assert(!newTag->getIdentifier() && !newTag->getTypedefNameForAnonDecl());
177 newTag->setTypedefNameForAnonDecl(Typedef);
John McCallcde5a402011-02-01 08:20:08 +0000178 }
Douglas Gregord57a38e2010-04-23 16:25:07 +0000179 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000180
Douglas Gregoref96ee02012-01-14 16:38:05 +0000181 if (TypedefNameDecl *Prev = D->getPreviousDecl()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000182 NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
183 TemplateArgs);
Douglas Gregorb7107222011-03-04 19:46:35 +0000184 if (!InstPrev)
185 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000186
Rafael Espindola5df37bd2011-12-26 22:42:47 +0000187 TypedefNameDecl *InstPrevTypedef = cast<TypedefNameDecl>(InstPrev);
188
189 // If the typedef types are not identical, reject them.
190 SemaRef.isIncompatibleTypedef(InstPrevTypedef, Typedef);
191
192 Typedef->setPreviousDeclaration(InstPrevTypedef);
John McCall5126fd02009-12-30 00:31:22 +0000193 }
194
John McCall1d8d1cc2010-08-01 02:01:53 +0000195 SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef);
Douglas Gregord57a38e2010-04-23 16:25:07 +0000196
John McCall46460a62010-01-20 21:53:11 +0000197 Typedef->setAccess(D->getAccess());
Mike Stump1eb44332009-09-09 15:08:12 +0000198
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000199 return Typedef;
200}
201
Richard Smith162e1c12011-04-15 14:24:37 +0000202Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
Richard Smith3e4c6c42011-05-05 21:57:07 +0000203 Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/false);
204 Owner->addDecl(Typedef);
205 return Typedef;
Richard Smith162e1c12011-04-15 14:24:37 +0000206}
207
208Decl *TemplateDeclInstantiator::VisitTypeAliasDecl(TypeAliasDecl *D) {
Richard Smith3e4c6c42011-05-05 21:57:07 +0000209 Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/true);
210 Owner->addDecl(Typedef);
211 return Typedef;
212}
213
214Decl *
215TemplateDeclInstantiator::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
216 // Create a local instantiation scope for this type alias template, which
217 // will contain the instantiations of the template parameters.
218 LocalInstantiationScope Scope(SemaRef);
219
220 TemplateParameterList *TempParams = D->getTemplateParameters();
221 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
222 if (!InstParams)
223 return 0;
224
225 TypeAliasDecl *Pattern = D->getTemplatedDecl();
226
227 TypeAliasTemplateDecl *PrevAliasTemplate = 0;
Douglas Gregoref96ee02012-01-14 16:38:05 +0000228 if (Pattern->getPreviousDecl()) {
Richard Smith3e4c6c42011-05-05 21:57:07 +0000229 DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
230 if (Found.first != Found.second) {
231 PrevAliasTemplate = dyn_cast<TypeAliasTemplateDecl>(*Found.first);
232 }
233 }
234
235 TypeAliasDecl *AliasInst = cast_or_null<TypeAliasDecl>(
236 InstantiateTypedefNameDecl(Pattern, /*IsTypeAlias=*/true));
237 if (!AliasInst)
238 return 0;
239
240 TypeAliasTemplateDecl *Inst
241 = TypeAliasTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
242 D->getDeclName(), InstParams, AliasInst);
243 if (PrevAliasTemplate)
244 Inst->setPreviousDeclaration(PrevAliasTemplate);
245
246 Inst->setAccess(D->getAccess());
247
248 if (!PrevAliasTemplate)
249 Inst->setInstantiatedFromMemberTemplate(D);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000250
Richard Smith3e4c6c42011-05-05 21:57:07 +0000251 Owner->addDecl(Inst);
252
253 return Inst;
Richard Smith162e1c12011-04-15 14:24:37 +0000254}
255
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000256Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
Douglas Gregor9901c572010-05-21 00:31:19 +0000257 // If this is the variable for an anonymous struct or union,
258 // instantiate the anonymous struct/union type first.
259 if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
260 if (RecordTy->getDecl()->isAnonymousStructOrUnion())
261 if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
262 return 0;
263
John McCallce3ff2b2009-08-25 22:02:44 +0000264 // Do substitution on the type of the declaration
John McCalla93c9342009-12-07 02:54:59 +0000265 TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
John McCall0a5fa062009-10-21 02:39:02 +0000266 TemplateArgs,
267 D->getTypeSpecStartLoc(),
268 D->getDeclName());
269 if (!DI)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000270 return 0;
271
Douglas Gregorc6dbc3f2010-09-12 07:37:24 +0000272 if (DI->getType()->isFunctionType()) {
273 SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function)
274 << D->isStaticDataMember() << DI->getType();
275 return 0;
276 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000277
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000278 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000279 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000280 D->getInnerLocStart(),
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000281 D->getLocation(), D->getIdentifier(),
John McCall0a5fa062009-10-21 02:39:02 +0000282 DI->getType(), DI,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000283 D->getStorageClass(),
284 D->getStorageClassAsWritten());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000285 Var->setThreadSpecified(D->isThreadSpecified());
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000286 Var->setInitStyle(D->getInitStyle());
Richard Smithad762fc2011-04-14 22:09:26 +0000287 Var->setCXXForRangeDecl(D->isCXXForRangeDecl());
Richard Smith796c1a12012-01-19 22:46:17 +0000288 Var->setConstexpr(D->isConstexpr());
Mike Stump1eb44332009-09-09 15:08:12 +0000289
John McCallb6217662010-03-15 10:12:16 +0000290 // Substitute the nested name specifier, if any.
291 if (SubstQualifier(D, Var))
292 return 0;
293
Mike Stump1eb44332009-09-09 15:08:12 +0000294 // If we are instantiating a static data member defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000295 // out-of-line, the instantiation will have the same lexical
296 // context (which will be a namespace scope) as the template.
297 if (D->isOutOfLine())
298 Var->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000299
John McCall46460a62010-01-20 21:53:11 +0000300 Var->setAccess(D->getAccess());
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000301
Argyrios Kyrtzidis6b6b42a2011-04-19 19:51:10 +0000302 if (!D->isStaticDataMember()) {
Douglas Gregorc070cc62010-06-17 23:14:26 +0000303 Var->setUsed(D->isUsed(false));
Argyrios Kyrtzidis6b6b42a2011-04-19 19:51:10 +0000304 Var->setReferenced(D->isReferenced());
305 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000306
Mike Stump390b4cc2009-05-16 07:39:55 +0000307 // FIXME: In theory, we could have a previous declaration for variables that
308 // are not static data members.
John McCall68263142009-11-18 22:49:29 +0000309 // FIXME: having to fake up a LookupResult is dumb.
310 LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
Douglas Gregor449d0a82010-03-01 19:11:54 +0000311 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Douglas Gregor60c93c92010-02-09 07:26:29 +0000312 if (D->isStaticDataMember())
313 SemaRef.LookupQualifiedName(Previous, Owner, false);
Douglas Gregor9aab9c42011-12-10 01:22:52 +0000314
315 // In ARC, infer 'retaining' for variables of retainable type.
David Blaikie4e4d0842012-03-11 07:00:24 +0000316 if (SemaRef.getLangOpts().ObjCAutoRefCount &&
Douglas Gregor9aab9c42011-12-10 01:22:52 +0000317 SemaRef.inferObjCARCLifetime(Var))
318 Var->setInvalidDecl();
319
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +0000320 SemaRef.CheckVariableDeclaration(Var, Previous);
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Douglas Gregor7caa6822009-07-24 20:34:43 +0000322 if (D->isOutOfLine()) {
Richard Smith3e9ea0b2011-12-21 00:25:33 +0000323 D->getLexicalDeclContext()->addDecl(Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000324 Owner->makeDeclVisibleInContext(Var);
325 } else {
326 Owner->addDecl(Var);
Douglas Gregorf7d72f52010-05-03 20:22:41 +0000327 if (Owner->isFunctionOrMethod())
328 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000329 }
DeLesley Hutchinsdd5756c2012-02-16 17:30:51 +0000330 SemaRef.InstantiateAttrs(TemplateArgs, D, Var, LateAttrs, StartingScope);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000331
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000332 // Link instantiations of static data members back to the template from
333 // which they were instantiated.
334 if (Var->isStaticDataMember())
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000335 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000336 TSK_ImplicitInstantiation);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000337
Douglas Gregor60c93c92010-02-09 07:26:29 +0000338 if (Var->getAnyInitializer()) {
339 // We already have an initializer in the class.
340 } else if (D->getInit()) {
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000341 if (Var->isStaticDataMember() && !D->isOutOfLine())
Richard Smithf6702a32011-12-20 02:08:33 +0000342 SemaRef.PushExpressionEvaluationContext(Sema::ConstantEvaluated);
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000343 else
344 SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
345
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000346 // Instantiate the initializer.
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000347 ExprResult Init = SemaRef.SubstInitializer(D->getInit(), TemplateArgs,
348 D->getInitStyle() == VarDecl::CallInit);
349 if (!Init.isInvalid()) {
Richard Smith34b41d92011-02-20 03:19:35 +0000350 bool TypeMayContainAuto = true;
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000351 if (Init.get()) {
352 bool DirectInit = D->isDirectInit();
353 SemaRef.AddInitializerToDecl(Var, Init.take(), DirectInit,
354 TypeMayContainAuto);
355 } else
Eli Friedman6aeaa602012-01-05 22:34:08 +0000356 SemaRef.ActOnUninitializedDecl(Var, TypeMayContainAuto);
Douglas Gregor6eef5192009-12-14 19:27:10 +0000357 } else {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000358 // FIXME: Not too happy about invalidating the declaration
359 // because of a bogus initializer.
360 Var->setInvalidDecl();
Douglas Gregor6eef5192009-12-14 19:27:10 +0000361 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000362
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000363 SemaRef.PopExpressionEvaluationContext();
Richard Smithad762fc2011-04-14 22:09:26 +0000364 } else if ((!Var->isStaticDataMember() || Var->isOutOfLine()) &&
365 !Var->isCXXForRangeDecl())
John McCalld226f652010-08-21 09:40:31 +0000366 SemaRef.ActOnUninitializedDecl(Var, false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000367
Richard Smithe3499ca2011-06-21 23:42:09 +0000368 // Diagnose unused local variables with dependent types, where the diagnostic
369 // will have been deferred.
370 if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed() &&
371 D->getType()->isDependentType())
Douglas Gregor5764f612010-05-08 23:05:03 +0000372 SemaRef.DiagnoseUnusedDecl(Var);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +0000373
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000374 return Var;
375}
376
Abramo Bagnara6206d532010-06-05 05:09:32 +0000377Decl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
378 AccessSpecDecl* AD
379 = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
380 D->getAccessSpecifierLoc(), D->getColonLoc());
381 Owner->addHiddenDecl(AD);
382 return AD;
383}
384
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000385Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
386 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000387 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor561f8122011-07-01 01:22:09 +0000388 if (DI->getType()->isInstantiationDependentType() ||
Douglas Gregor836adf62010-05-24 17:22:01 +0000389 DI->getType()->isVariablyModifiedType()) {
John McCall07fb6be2009-10-22 23:33:21 +0000390 DI = SemaRef.SubstType(DI, TemplateArgs,
391 D->getLocation(), D->getDeclName());
392 if (!DI) {
John McCalla93c9342009-12-07 02:54:59 +0000393 DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000394 Invalid = true;
395 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000396 // C++ [temp.arg.type]p3:
397 // If a declaration acquires a function type through a type
398 // dependent on a template-parameter and this causes a
399 // declaration that does not use the syntactic form of a
400 // function declarator to have function type, the program is
401 // ill-formed.
402 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000403 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000404 Invalid = true;
405 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000406 } else {
407 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000408 }
409
410 Expr *BitWidth = D->getBitWidth();
411 if (Invalid)
412 BitWidth = 0;
413 else if (BitWidth) {
Richard Smithf6702a32011-12-20 02:08:33 +0000414 // The bit-width expression is a constant expression.
415 EnterExpressionEvaluationContext Unevaluated(SemaRef,
416 Sema::ConstantEvaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000417
John McCall60d7b3a2010-08-24 06:29:42 +0000418 ExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000419 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000420 if (InstantiatedBitWidth.isInvalid()) {
421 Invalid = true;
422 BitWidth = 0;
423 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000424 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000425 }
426
John McCall07fb6be2009-10-22 23:33:21 +0000427 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
428 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000429 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000430 D->getLocation(),
431 D->isMutable(),
432 BitWidth,
Richard Smith7a614d82011-06-11 17:19:42 +0000433 D->hasInClassInitializer(),
Steve Naroffea218b82009-07-14 14:58:18 +0000434 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000435 D->getAccess(),
436 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000437 if (!Field) {
438 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000439 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000440 }
Mike Stump1eb44332009-09-09 15:08:12 +0000441
DeLesley Hutchins23323e02012-01-20 22:50:54 +0000442 SemaRef.InstantiateAttrs(TemplateArgs, D, Field, LateAttrs, StartingScope);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000443
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000444 if (Invalid)
445 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000447 if (!Field->getDeclName()) {
448 // Keep track of where this decl came from.
449 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000450 }
Douglas Gregor9901c572010-05-21 00:31:19 +0000451 if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
452 if (Parent->isAnonymousStructOrUnion() &&
Sebastian Redl7a126a42010-08-31 00:36:30 +0000453 Parent->getRedeclContext()->isFunctionOrMethod())
Douglas Gregor9901c572010-05-21 00:31:19 +0000454 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000455 }
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000457 Field->setImplicit(D->isImplicit());
John McCall46460a62010-01-20 21:53:11 +0000458 Field->setAccess(D->getAccess());
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000459 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000460
461 return Field;
462}
463
Francois Pichet87c2e122010-11-21 06:08:52 +0000464Decl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
465 NamedDecl **NamedChain =
466 new (SemaRef.Context)NamedDecl*[D->getChainingSize()];
467
468 int i = 0;
469 for (IndirectFieldDecl::chain_iterator PI =
470 D->chain_begin(), PE = D->chain_end();
Douglas Gregorb7107222011-03-04 19:46:35 +0000471 PI != PE; ++PI) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000472 NamedDecl *Next = SemaRef.FindInstantiatedDecl(D->getLocation(), *PI,
Douglas Gregorb7107222011-03-04 19:46:35 +0000473 TemplateArgs);
474 if (!Next)
475 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000476
Douglas Gregorb7107222011-03-04 19:46:35 +0000477 NamedChain[i++] = Next;
478 }
Francois Pichet87c2e122010-11-21 06:08:52 +0000479
Francois Pichet40e17752010-12-09 10:07:54 +0000480 QualType T = cast<FieldDecl>(NamedChain[i-1])->getType();
Francois Pichet87c2e122010-11-21 06:08:52 +0000481 IndirectFieldDecl* IndirectField
482 = IndirectFieldDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Francois Pichet40e17752010-12-09 10:07:54 +0000483 D->getIdentifier(), T,
Francois Pichet87c2e122010-11-21 06:08:52 +0000484 NamedChain, D->getChainingSize());
485
486
487 IndirectField->setImplicit(D->isImplicit());
488 IndirectField->setAccess(D->getAccess());
489 Owner->addDecl(IndirectField);
490 return IndirectField;
491}
492
John McCall02cace72009-08-28 07:59:38 +0000493Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
John McCall02cace72009-08-28 07:59:38 +0000494 // Handle friend type expressions by simply substituting template
Douglas Gregor06245bf2010-04-07 17:57:12 +0000495 // parameters into the pattern type and checking the result.
John McCall32f2fb52010-03-25 18:04:51 +0000496 if (TypeSourceInfo *Ty = D->getFriendType()) {
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000497 TypeSourceInfo *InstTy;
498 // If this is an unsupported friend, don't bother substituting template
499 // arguments into it. The actual type referred to won't be used by any
500 // parts of Clang, and may not be valid for instantiating. Just use the
501 // same info for the instantiated friend.
502 if (D->isUnsupportedFriend()) {
503 InstTy = Ty;
504 } else {
505 InstTy = SemaRef.SubstType(Ty, TemplateArgs,
506 D->getLocation(), DeclarationName());
507 }
508 if (!InstTy)
Douglas Gregor7557a132009-12-24 20:56:24 +0000509 return 0;
John McCall02cace72009-08-28 07:59:38 +0000510
Abramo Bagnara0216df82011-10-29 20:52:52 +0000511 FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getLocation(),
512 D->getFriendLoc(), InstTy);
Douglas Gregor06245bf2010-04-07 17:57:12 +0000513 if (!FD)
514 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000515
Douglas Gregor06245bf2010-04-07 17:57:12 +0000516 FD->setAccess(AS_public);
John McCall9a34edb2010-10-19 01:40:49 +0000517 FD->setUnsupportedFriend(D->isUnsupportedFriend());
Douglas Gregor06245bf2010-04-07 17:57:12 +0000518 Owner->addDecl(FD);
519 return FD;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000520 }
521
Douglas Gregor06245bf2010-04-07 17:57:12 +0000522 NamedDecl *ND = D->getFriendDecl();
523 assert(ND && "friend decl must be a decl or a type!");
524
John McCallaf2094e2010-04-08 09:05:18 +0000525 // All of the Visit implementations for the various potential friend
526 // declarations have to be carefully written to work for friend
527 // objects, with the most important detail being that the target
528 // decl should almost certainly not be placed in Owner.
529 Decl *NewND = Visit(ND);
Douglas Gregor06245bf2010-04-07 17:57:12 +0000530 if (!NewND) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000531
John McCall02cace72009-08-28 07:59:38 +0000532 FriendDecl *FD =
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000533 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor06245bf2010-04-07 17:57:12 +0000534 cast<NamedDecl>(NewND), D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000535 FD->setAccess(AS_public);
John McCall9a34edb2010-10-19 01:40:49 +0000536 FD->setUnsupportedFriend(D->isUnsupportedFriend());
John McCall02cace72009-08-28 07:59:38 +0000537 Owner->addDecl(FD);
538 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000539}
540
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000541Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
542 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Richard Smithf6702a32011-12-20 02:08:33 +0000544 // The expression in a static assertion is a constant expression.
545 EnterExpressionEvaluationContext Unevaluated(SemaRef,
546 Sema::ConstantEvaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000547
John McCall60d7b3a2010-08-24 06:29:42 +0000548 ExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000549 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000550 if (InstantiatedAssertExpr.isInvalid())
551 return 0;
552
John McCall60d7b3a2010-08-24 06:29:42 +0000553 ExprResult Message(D->getMessage());
John McCall3fa5cae2010-10-26 07:05:15 +0000554 D->getMessage();
John McCalld226f652010-08-21 09:40:31 +0000555 return SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
John McCall9ae2f072010-08-23 23:25:46 +0000556 InstantiatedAssertExpr.get(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000557 Message.get(),
558 D->getRParenLoc());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000559}
560
561Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Richard Smith38f0df32012-03-26 04:58:10 +0000562 EnumDecl *PrevDecl = 0;
563 if (D->getPreviousDecl()) {
564 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
565 D->getPreviousDecl(),
566 TemplateArgs);
567 if (!Prev) return 0;
568 PrevDecl = cast<EnumDecl>(Prev);
569 }
570
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000571 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000572 D->getLocation(), D->getIdentifier(),
Richard Smith38f0df32012-03-26 04:58:10 +0000573 PrevDecl, D->isScoped(),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +0000574 D->isScopedUsingClassTag(), D->isFixed());
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000575 if (D->isFixed()) {
Richard Smithf1c66b42012-03-14 23:13:10 +0000576 if (TypeSourceInfo *TI = D->getIntegerTypeSourceInfo()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000577 // If we have type source information for the underlying type, it means it
578 // has been explicitly set by the user. Perform substitution on it before
579 // moving on.
580 SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
Richard Smithf1c66b42012-03-14 23:13:10 +0000581 TypeSourceInfo *NewTI = SemaRef.SubstType(TI, TemplateArgs, UnderlyingLoc,
582 DeclarationName());
583 if (!NewTI || SemaRef.CheckEnumUnderlyingType(NewTI))
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000584 Enum->setIntegerType(SemaRef.Context.IntTy);
Richard Smithf1c66b42012-03-14 23:13:10 +0000585 else
586 Enum->setIntegerTypeSourceInfo(NewTI);
587 } else {
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000588 assert(!D->getIntegerType()->isDependentType()
589 && "Dependent type without type source info");
590 Enum->setIntegerType(D->getIntegerType());
591 }
592 }
593
John McCall5b629aa2010-10-22 23:36:17 +0000594 SemaRef.InstantiateAttrs(TemplateArgs, D, Enum);
595
Richard Smithf1c66b42012-03-14 23:13:10 +0000596 Enum->setInstantiationOfMemberEnum(D, TSK_ImplicitInstantiation);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000597 Enum->setAccess(D->getAccess());
John McCallb6217662010-03-15 10:12:16 +0000598 if (SubstQualifier(D, Enum)) return 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000599 Owner->addDecl(Enum);
Richard Smithf1c66b42012-03-14 23:13:10 +0000600
Richard Smith4ca93d92012-03-26 04:08:46 +0000601 EnumDecl *Def = D->getDefinition();
602 if (Def && Def != D) {
603 // If this is an out-of-line definition of an enum member template, check
604 // that the underlying types match in the instantiation of both
605 // declarations.
606 if (TypeSourceInfo *TI = Def->getIntegerTypeSourceInfo()) {
607 SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
608 QualType DefnUnderlying =
609 SemaRef.SubstType(TI->getType(), TemplateArgs,
610 UnderlyingLoc, DeclarationName());
611 SemaRef.CheckEnumRedeclaration(Def->getLocation(), Def->isScoped(),
612 DefnUnderlying, Enum);
613 }
614 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000615
Douglas Gregor96084f12010-03-01 19:00:07 +0000616 if (D->getDeclContext()->isFunctionOrMethod())
617 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000618
Richard Smithf1c66b42012-03-14 23:13:10 +0000619 // C++11 [temp.inst]p1: The implicit instantiation of a class template
620 // specialization causes the implicit instantiation of the declarations, but
621 // not the definitions of scoped member enumerations.
622 // FIXME: There appears to be no wording for what happens for an enum defined
Richard Smith38f0df32012-03-26 04:58:10 +0000623 // within a block scope, but we treat that much like a member template. Only
624 // instantiate the definition when visiting the definition in that case, since
625 // we will visit all redeclarations.
626 if (!Enum->isScoped() && Def &&
627 (!D->getDeclContext()->isFunctionOrMethod() || D->isCompleteDefinition()))
Richard Smith4ca93d92012-03-26 04:08:46 +0000628 InstantiateEnumDefinition(Enum, Def);
Richard Smithf1c66b42012-03-14 23:13:10 +0000629
630 return Enum;
631}
632
633void TemplateDeclInstantiator::InstantiateEnumDefinition(
634 EnumDecl *Enum, EnumDecl *Pattern) {
635 Enum->startDefinition();
636
Richard Smith1af83c42012-03-23 03:33:32 +0000637 // Update the location to refer to the definition.
638 Enum->setLocation(Pattern->getLocation());
639
Chris Lattner5f9e2722011-07-23 10:55:15 +0000640 SmallVector<Decl*, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000641
642 EnumConstantDecl *LastEnumConst = 0;
Richard Smithf1c66b42012-03-14 23:13:10 +0000643 for (EnumDecl::enumerator_iterator EC = Pattern->enumerator_begin(),
644 ECEnd = Pattern->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000645 EC != ECEnd; ++EC) {
646 // The specified value for the enumerator.
John McCall60d7b3a2010-08-24 06:29:42 +0000647 ExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000648 if (Expr *UninstValue = EC->getInitExpr()) {
Richard Smithf6702a32011-12-20 02:08:33 +0000649 // The enumerator's value expression is a constant expression.
Mike Stump1eb44332009-09-09 15:08:12 +0000650 EnterExpressionEvaluationContext Unevaluated(SemaRef,
Richard Smithf6702a32011-12-20 02:08:33 +0000651 Sema::ConstantEvaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000652
John McCallce3ff2b2009-08-25 22:02:44 +0000653 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000654 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000655
656 // Drop the initial value and continue.
657 bool isInvalid = false;
658 if (Value.isInvalid()) {
659 Value = SemaRef.Owned((Expr *)0);
660 isInvalid = true;
661 }
662
Mike Stump1eb44332009-09-09 15:08:12 +0000663 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000664 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
665 EC->getLocation(), EC->getIdentifier(),
John McCall9ae2f072010-08-23 23:25:46 +0000666 Value.get());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000667
668 if (isInvalid) {
669 if (EnumConst)
670 EnumConst->setInvalidDecl();
671 Enum->setInvalidDecl();
672 }
673
674 if (EnumConst) {
David Blaikie262bc182012-04-30 02:36:29 +0000675 SemaRef.InstantiateAttrs(TemplateArgs, &*EC, EnumConst);
John McCall5b629aa2010-10-22 23:36:17 +0000676
John McCall3b85ecf2010-01-23 22:37:59 +0000677 EnumConst->setAccess(Enum->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000678 Enum->addDecl(EnumConst);
John McCalld226f652010-08-21 09:40:31 +0000679 Enumerators.push_back(EnumConst);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000680 LastEnumConst = EnumConst;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000681
Richard Smithf1c66b42012-03-14 23:13:10 +0000682 if (Pattern->getDeclContext()->isFunctionOrMethod() &&
683 !Enum->isScoped()) {
Douglas Gregor96084f12010-03-01 19:00:07 +0000684 // If the enumeration is within a function or method, record the enum
685 // constant as a local.
David Blaikie262bc182012-04-30 02:36:29 +0000686 SemaRef.CurrentInstantiationScope->InstantiatedLocal(&*EC, EnumConst);
Douglas Gregor96084f12010-03-01 19:00:07 +0000687 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000688 }
689 }
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Richard Smithf1c66b42012-03-14 23:13:10 +0000691 // FIXME: Fixup LBraceLoc
692 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(),
693 Enum->getRBraceLoc(), Enum,
Eli Friedmande7a0fc2010-08-15 02:27:09 +0000694 Enumerators.data(), Enumerators.size(),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000695 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000696}
697
Douglas Gregor6477b692009-03-25 15:04:13 +0000698Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000699 llvm_unreachable("EnumConstantDecls can only occur within EnumDecls.");
Douglas Gregor6477b692009-03-25 15:04:13 +0000700}
701
John McCalle29ba202009-08-20 01:44:21 +0000702Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
John McCall93ba8572010-03-25 06:39:04 +0000703 bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
704
Douglas Gregor550d9b22009-10-31 17:21:17 +0000705 // Create a local instantiation scope for this class template, which
706 // will contain the instantiations of the template parameters.
John McCall2a7fb272010-08-25 05:32:35 +0000707 LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000708 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000709 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000710 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000711 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000712
713 CXXRecordDecl *Pattern = D->getTemplatedDecl();
John McCall93ba8572010-03-25 06:39:04 +0000714
715 // Instantiate the qualifier. We have to do this first in case
716 // we're a friend declaration, because if we are then we need to put
717 // the new declaration in the appropriate context.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000718 NestedNameSpecifierLoc QualifierLoc = Pattern->getQualifierLoc();
719 if (QualifierLoc) {
720 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
721 TemplateArgs);
722 if (!QualifierLoc)
723 return 0;
John McCall93ba8572010-03-25 06:39:04 +0000724 }
725
726 CXXRecordDecl *PrevDecl = 0;
727 ClassTemplateDecl *PrevClassTemplate = 0;
728
Douglas Gregoref96ee02012-01-14 16:38:05 +0000729 if (!isFriend && Pattern->getPreviousDecl()) {
Nick Lewycky37574f52010-11-08 23:29:42 +0000730 DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
731 if (Found.first != Found.second) {
732 PrevClassTemplate = dyn_cast<ClassTemplateDecl>(*Found.first);
733 if (PrevClassTemplate)
734 PrevDecl = PrevClassTemplate->getTemplatedDecl();
735 }
736 }
737
John McCall93ba8572010-03-25 06:39:04 +0000738 // If this isn't a friend, then it's a member template, in which
739 // case we just want to build the instantiation in the
740 // specialization. If it is a friend, we want to build it in
741 // the appropriate context.
742 DeclContext *DC = Owner;
743 if (isFriend) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000744 if (QualifierLoc) {
John McCall93ba8572010-03-25 06:39:04 +0000745 CXXScopeSpec SS;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000746 SS.Adopt(QualifierLoc);
John McCall93ba8572010-03-25 06:39:04 +0000747 DC = SemaRef.computeDeclContext(SS);
748 if (!DC) return 0;
749 } else {
750 DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
751 Pattern->getDeclContext(),
752 TemplateArgs);
753 }
754
755 // Look for a previous declaration of the template in the owning
756 // context.
757 LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
758 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
759 SemaRef.LookupQualifiedName(R, DC);
760
761 if (R.isSingleResult()) {
762 PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
763 if (PrevClassTemplate)
764 PrevDecl = PrevClassTemplate->getTemplatedDecl();
765 }
766
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000767 if (!PrevClassTemplate && QualifierLoc) {
John McCall93ba8572010-03-25 06:39:04 +0000768 SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
Douglas Gregor1eabb7d2010-03-31 23:17:41 +0000769 << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000770 << QualifierLoc.getSourceRange();
John McCall93ba8572010-03-25 06:39:04 +0000771 return 0;
772 }
773
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000774 bool AdoptedPreviousTemplateParams = false;
John McCall93ba8572010-03-25 06:39:04 +0000775 if (PrevClassTemplate) {
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000776 bool Complain = true;
777
778 // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
779 // template for struct std::tr1::__detail::_Map_base, where the
780 // template parameters of the friend declaration don't match the
781 // template parameters of the original declaration. In this one
782 // case, we don't complain about the ill-formed friend
783 // declaration.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000784 if (isFriend && Pattern->getIdentifier() &&
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000785 Pattern->getIdentifier()->isStr("_Map_base") &&
786 DC->isNamespace() &&
787 cast<NamespaceDecl>(DC)->getIdentifier() &&
788 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
789 DeclContext *DCParent = DC->getParent();
790 if (DCParent->isNamespace() &&
791 cast<NamespaceDecl>(DCParent)->getIdentifier() &&
792 cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
793 DeclContext *DCParent2 = DCParent->getParent();
794 if (DCParent2->isNamespace() &&
795 cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
796 cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
797 DCParent2->getParent()->isTranslationUnit())
798 Complain = false;
799 }
800 }
801
John McCall93ba8572010-03-25 06:39:04 +0000802 TemplateParameterList *PrevParams
803 = PrevClassTemplate->getTemplateParameters();
804
805 // Make sure the parameter lists match.
806 if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000807 Complain,
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000808 Sema::TPL_TemplateMatch)) {
809 if (Complain)
810 return 0;
811
812 AdoptedPreviousTemplateParams = true;
813 InstParams = PrevParams;
814 }
John McCall93ba8572010-03-25 06:39:04 +0000815
816 // Do some additional validation, then merge default arguments
817 // from the existing declarations.
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000818 if (!AdoptedPreviousTemplateParams &&
819 SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
John McCall93ba8572010-03-25 06:39:04 +0000820 Sema::TPC_ClassTemplate))
821 return 0;
822 }
823 }
824
John McCalle29ba202009-08-20 01:44:21 +0000825 CXXRecordDecl *RecordInst
John McCall93ba8572010-03-25 06:39:04 +0000826 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000827 Pattern->getLocStart(), Pattern->getLocation(),
828 Pattern->getIdentifier(), PrevDecl,
Douglas Gregorf0510d42009-10-12 23:11:44 +0000829 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000830
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000831 if (QualifierLoc)
832 RecordInst->setQualifierInfo(QualifierLoc);
John McCallb6217662010-03-15 10:12:16 +0000833
John McCalle29ba202009-08-20 01:44:21 +0000834 ClassTemplateDecl *Inst
John McCall93ba8572010-03-25 06:39:04 +0000835 = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
836 D->getIdentifier(), InstParams, RecordInst,
837 PrevClassTemplate);
John McCalle29ba202009-08-20 01:44:21 +0000838 RecordInst->setDescribedClassTemplate(Inst);
John McCallea7390c2010-04-08 20:25:50 +0000839
John McCall93ba8572010-03-25 06:39:04 +0000840 if (isFriend) {
John McCallea7390c2010-04-08 20:25:50 +0000841 if (PrevClassTemplate)
842 Inst->setAccess(PrevClassTemplate->getAccess());
843 else
844 Inst->setAccess(D->getAccess());
845
John McCall93ba8572010-03-25 06:39:04 +0000846 Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
847 // TODO: do we want to track the instantiation progeny of this
848 // friend target decl?
849 } else {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000850 Inst->setAccess(D->getAccess());
Nick Lewycky37574f52010-11-08 23:29:42 +0000851 if (!PrevClassTemplate)
852 Inst->setInstantiatedFromMemberTemplate(D);
John McCall93ba8572010-03-25 06:39:04 +0000853 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000854
Douglas Gregorf0510d42009-10-12 23:11:44 +0000855 // Trigger creation of the type for the instantiation.
John McCall3cb0ebd2010-03-10 03:28:59 +0000856 SemaRef.Context.getInjectedClassNameType(RecordInst,
Douglas Gregor24bae922010-07-08 18:37:38 +0000857 Inst->getInjectedClassNameSpecialization());
John McCallea7390c2010-04-08 20:25:50 +0000858
Douglas Gregor259571e2009-10-30 22:42:42 +0000859 // Finish handling of friends.
John McCall93ba8572010-03-25 06:39:04 +0000860 if (isFriend) {
Richard Smith1b7f9cb2012-03-13 03:12:56 +0000861 DC->makeDeclVisibleInContext(Inst);
Abramo Bagnara4c515482011-11-26 13:33:46 +0000862 Inst->setLexicalDeclContext(Owner);
863 RecordInst->setLexicalDeclContext(Owner);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000864 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000865 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000866
Abramo Bagnara4c515482011-11-26 13:33:46 +0000867 if (D->isOutOfLine()) {
868 Inst->setLexicalDeclContext(D->getLexicalDeclContext());
869 RecordInst->setLexicalDeclContext(D->getLexicalDeclContext());
870 }
871
John McCalle29ba202009-08-20 01:44:21 +0000872 Owner->addDecl(Inst);
Douglas Gregord65587f2010-11-10 19:44:59 +0000873
874 if (!PrevClassTemplate) {
875 // Queue up any out-of-line partial specializations of this member
876 // class template; the client will force their instantiation once
877 // the enclosing class has been instantiated.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000878 SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
Douglas Gregord65587f2010-11-10 19:44:59 +0000879 D->getPartialSpecializations(PartialSpecs);
880 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
881 if (PartialSpecs[I]->isOutOfLine())
882 OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I]));
883 }
884
John McCalle29ba202009-08-20 01:44:21 +0000885 return Inst;
886}
887
Douglas Gregord60e1052009-08-27 16:57:43 +0000888Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000889TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
890 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000891 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000892
Douglas Gregored9c0f92009-10-29 00:04:11 +0000893 // Lookup the already-instantiated declaration in the instantiation
894 // of the class template and return that.
895 DeclContext::lookup_result Found
896 = Owner->lookup(ClassTemplate->getDeclName());
897 if (Found.first == Found.second)
898 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000899
Douglas Gregored9c0f92009-10-29 00:04:11 +0000900 ClassTemplateDecl *InstClassTemplate
901 = dyn_cast<ClassTemplateDecl>(*Found.first);
902 if (!InstClassTemplate)
903 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000904
Douglas Gregord65587f2010-11-10 19:44:59 +0000905 if (ClassTemplatePartialSpecializationDecl *Result
906 = InstClassTemplate->findPartialSpecInstantiatedFromMember(D))
907 return Result;
908
909 return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D);
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000910}
911
912Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000913TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000914 // Create a local instantiation scope for this function template, which
915 // will contain the instantiations of the template parameters and then get
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000916 // merged with the local instantiation scope for the function template
Douglas Gregor550d9b22009-10-31 17:21:17 +0000917 // itself.
John McCall2a7fb272010-08-25 05:32:35 +0000918 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor895162d2010-04-30 18:55:50 +0000919
Douglas Gregord60e1052009-08-27 16:57:43 +0000920 TemplateParameterList *TempParams = D->getTemplateParameters();
921 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000922 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000923 return NULL;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000924
Douglas Gregora735b202009-10-13 14:39:41 +0000925 FunctionDecl *Instantiated = 0;
926 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000927 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
Douglas Gregora735b202009-10-13 14:39:41 +0000928 InstParams));
929 else
930 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000931 D->getTemplatedDecl(),
Douglas Gregora735b202009-10-13 14:39:41 +0000932 InstParams));
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000933
Douglas Gregora735b202009-10-13 14:39:41 +0000934 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000935 return 0;
936
John McCall46460a62010-01-20 21:53:11 +0000937 Instantiated->setAccess(D->getAccess());
938
Mike Stump1eb44332009-09-09 15:08:12 +0000939 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000940 // template from which it was instantiated.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000941 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000942 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000943 InstTemplate->setAccess(D->getAccess());
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000944 assert(InstTemplate &&
Douglas Gregora735b202009-10-13 14:39:41 +0000945 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
John McCalle976ffe2009-12-14 23:19:40 +0000946
John McCallb1a56e72010-03-26 23:10:15 +0000947 bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
948
John McCalle976ffe2009-12-14 23:19:40 +0000949 // Link the instantiation back to the pattern *unless* this is a
950 // non-definition friend declaration.
951 if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
John McCallb1a56e72010-03-26 23:10:15 +0000952 !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
Douglas Gregora735b202009-10-13 14:39:41 +0000953 InstTemplate->setInstantiatedFromMemberTemplate(D);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000954
John McCallb1a56e72010-03-26 23:10:15 +0000955 // Make declarations visible in the appropriate context.
956 if (!isFriend)
Douglas Gregora735b202009-10-13 14:39:41 +0000957 Owner->addDecl(InstTemplate);
John McCallb1a56e72010-03-26 23:10:15 +0000958
Douglas Gregord60e1052009-08-27 16:57:43 +0000959 return InstTemplate;
960}
961
Douglas Gregord475b8d2009-03-25 21:17:03 +0000962Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
963 CXXRecordDecl *PrevDecl = 0;
964 if (D->isInjectedClassName())
965 PrevDecl = cast<CXXRecordDecl>(Owner);
Douglas Gregoref96ee02012-01-14 16:38:05 +0000966 else if (D->getPreviousDecl()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000967 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
Douglas Gregoref96ee02012-01-14 16:38:05 +0000968 D->getPreviousDecl(),
John McCall6c1c1b82009-12-15 22:29:06 +0000969 TemplateArgs);
970 if (!Prev) return 0;
971 PrevDecl = cast<CXXRecordDecl>(Prev);
972 }
Douglas Gregord475b8d2009-03-25 21:17:03 +0000973
974 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000975 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000976 D->getLocStart(), D->getLocation(),
977 D->getIdentifier(), PrevDecl);
John McCallb6217662010-03-15 10:12:16 +0000978
979 // Substitute the nested name specifier, if any.
980 if (SubstQualifier(D, Record))
981 return 0;
982
Douglas Gregord475b8d2009-03-25 21:17:03 +0000983 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000984 // FIXME: Check against AS_none is an ugly hack to work around the issue that
985 // the tag decls introduced by friend class declarations don't have an access
986 // specifier. Remove once this area of the code gets sorted out.
987 if (D->getAccess() != AS_none)
988 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000989 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000990 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000991
John McCall02cace72009-08-28 07:59:38 +0000992 // If the original function was part of a friend declaration,
993 // inherit its namespace state.
994 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
995 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
996
Douglas Gregor9901c572010-05-21 00:31:19 +0000997 // Make sure that anonymous structs and unions are recorded.
998 if (D->isAnonymousStructOrUnion()) {
999 Record->setAnonymousStructOrUnion(true);
Sebastian Redl7a126a42010-08-31 00:36:30 +00001000 if (Record->getDeclContext()->getRedeclContext()->isFunctionOrMethod())
Douglas Gregor9901c572010-05-21 00:31:19 +00001001 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
1002 }
Anders Carlssond8b285f2009-09-01 04:26:58 +00001003
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001004 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001005 return Record;
1006}
1007
John McCall02cace72009-08-28 07:59:38 +00001008/// Normal class members are of more specific types and therefore
1009/// don't make it here. This function serves two purposes:
1010/// 1) instantiating function templates
1011/// 2) substituting friend declarations
1012/// FIXME: preserve function definitions in case #2
Douglas Gregor7557a132009-12-24 20:56:24 +00001013Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
Douglas Gregora735b202009-10-13 14:39:41 +00001014 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +00001015 // Check whether there is already a function template specialization for
1016 // this declaration.
1017 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
John McCallb0cb0222010-03-27 05:57:59 +00001018 if (FunctionTemplate && !TemplateParams) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001019 std::pair<const TemplateArgument *, unsigned> Innermost
Douglas Gregor24bae922010-07-08 18:37:38 +00001020 = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +00001021
Douglas Gregor1e1e9722012-03-28 14:34:23 +00001022 void *InsertPos = 0;
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001023 FunctionDecl *SpecFunc
1024 = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
1025 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001026
Douglas Gregor127102b2009-06-29 20:59:39 +00001027 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001028 if (SpecFunc)
1029 return SpecFunc;
Douglas Gregor127102b2009-06-29 20:59:39 +00001030 }
Mike Stump1eb44332009-09-09 15:08:12 +00001031
John McCallb0cb0222010-03-27 05:57:59 +00001032 bool isFriend;
1033 if (FunctionTemplate)
1034 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1035 else
1036 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1037
Douglas Gregor79c22782010-01-16 20:21:20 +00001038 bool MergeWithParentScope = (TemplateParams != 0) ||
Douglas Gregorb212d9a2010-05-21 21:25:08 +00001039 Owner->isFunctionOrMethod() ||
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001040 !(isa<Decl>(Owner) &&
Douglas Gregor79c22782010-01-16 20:21:20 +00001041 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
John McCall2a7fb272010-08-25 05:32:35 +00001042 LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001043
Chris Lattner5f9e2722011-07-23 10:55:15 +00001044 SmallVector<ParmVarDecl *, 4> Params;
David Blaikie64b4b432011-11-10 05:42:04 +00001045 TypeSourceInfo *TInfo = SubstFunctionType(D, Params);
John McCall21ef0fa2010-03-11 09:03:00 +00001046 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001047 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +00001048 QualType T = TInfo->getType();
John McCallfd810b12009-08-14 02:03:10 +00001049
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001050 NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
1051 if (QualifierLoc) {
1052 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
1053 TemplateArgs);
1054 if (!QualifierLoc)
1055 return 0;
John McCalld325daa2010-03-26 04:53:08 +00001056 }
1057
John McCall68b6b872010-02-06 01:50:47 +00001058 // If we're instantiating a local function declaration, put the result
1059 // in the owner; otherwise we need to find the instantiated context.
1060 DeclContext *DC;
1061 if (D->getDeclContext()->isFunctionOrMethod())
1062 DC = Owner;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001063 else if (isFriend && QualifierLoc) {
John McCalld325daa2010-03-26 04:53:08 +00001064 CXXScopeSpec SS;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001065 SS.Adopt(QualifierLoc);
John McCalld325daa2010-03-26 04:53:08 +00001066 DC = SemaRef.computeDeclContext(SS);
1067 if (!DC) return 0;
1068 } else {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001069 DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001070 TemplateArgs);
John McCalld325daa2010-03-26 04:53:08 +00001071 }
John McCall68b6b872010-02-06 01:50:47 +00001072
John McCall02cace72009-08-28 07:59:38 +00001073 FunctionDecl *Function =
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001074 FunctionDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(),
1075 D->getLocation(), D->getDeclName(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001076 D->getStorageClass(), D->getStorageClassAsWritten(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001077 D->isInlineSpecified(), D->hasWrittenPrototype(),
Richard Smith86c3ae42012-02-13 03:54:03 +00001078 D->isConstexpr());
John McCallb6217662010-03-15 10:12:16 +00001079
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001080 if (QualifierLoc)
1081 Function->setQualifierInfo(QualifierLoc);
John McCallb6217662010-03-15 10:12:16 +00001082
John McCallb1a56e72010-03-26 23:10:15 +00001083 DeclContext *LexicalDC = Owner;
1084 if (!isFriend && D->isOutOfLine()) {
1085 assert(D->getDeclContext()->isFileContext());
1086 LexicalDC = D->getDeclContext();
1087 }
1088
1089 Function->setLexicalDeclContext(LexicalDC);
Mike Stump1eb44332009-09-09 15:08:12 +00001090
Douglas Gregore53060f2009-06-25 22:08:12 +00001091 // Attach the parameters
Douglas Gregor5cbe1012011-07-05 18:30:26 +00001092 if (isa<FunctionProtoType>(Function->getType().IgnoreParens())) {
Douglas Gregor1d441ee2011-06-22 18:16:25 +00001093 // Adopt the already-instantiated parameters into our own context.
1094 for (unsigned P = 0; P < Params.size(); ++P)
1095 if (Params[P])
1096 Params[P]->setOwningFunction(Function);
1097 } else {
1098 // Since we were instantiated via a typedef of a function type, create
1099 // new parameters.
1100 const FunctionProtoType *Proto
1101 = Function->getType()->getAs<FunctionProtoType>();
1102 assert(Proto && "No function prototype in template instantiation?");
1103 for (FunctionProtoType::arg_type_iterator AI = Proto->arg_type_begin(),
1104 AE = Proto->arg_type_end(); AI != AE; ++AI) {
1105 ParmVarDecl *Param
1106 = SemaRef.BuildParmVarDeclForTypedef(Function, Function->getLocation(),
1107 *AI);
1108 Param->setScopeInfo(0, Params.size());
1109 Params.push_back(Param);
1110 }
1111 }
David Blaikie4278c652011-09-21 18:16:56 +00001112 Function->setParams(Params);
John McCall02cace72009-08-28 07:59:38 +00001113
Douglas Gregorac7c2c82010-05-17 16:38:00 +00001114 SourceLocation InstantiateAtPOI;
Douglas Gregora735b202009-10-13 14:39:41 +00001115 if (TemplateParams) {
1116 // Our resulting instantiation is actually a function template, since we
1117 // are substituting only the outer template parameters. For example, given
1118 //
1119 // template<typename T>
1120 // struct X {
1121 // template<typename U> friend void f(T, U);
1122 // };
1123 //
1124 // X<int> x;
1125 //
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001126 // We are instantiating the friend function template "f" within X<int>,
Douglas Gregora735b202009-10-13 14:39:41 +00001127 // which means substituting int for T, but leaving "f" as a friend function
1128 // template.
1129 // Build the function template itself.
John McCalld325daa2010-03-26 04:53:08 +00001130 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
Douglas Gregora735b202009-10-13 14:39:41 +00001131 Function->getLocation(),
1132 Function->getDeclName(),
1133 TemplateParams, Function);
1134 Function->setDescribedFunctionTemplate(FunctionTemplate);
John McCallb1a56e72010-03-26 23:10:15 +00001135
1136 FunctionTemplate->setLexicalDeclContext(LexicalDC);
John McCalld325daa2010-03-26 04:53:08 +00001137
1138 if (isFriend && D->isThisDeclarationADefinition()) {
1139 // TODO: should we remember this connection regardless of whether
1140 // the friend declaration provided a body?
1141 FunctionTemplate->setInstantiatedFromMemberTemplate(
1142 D->getDescribedFunctionTemplate());
1143 }
Douglas Gregor66724ea2009-11-14 01:20:54 +00001144 } else if (FunctionTemplate) {
1145 // Record this function template specialization.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001146 std::pair<const TemplateArgument *, unsigned> Innermost
Douglas Gregor24bae922010-07-08 18:37:38 +00001147 = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001148 Function->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001149 TemplateArgumentList::CreateCopy(SemaRef.Context,
Douglas Gregor24bae922010-07-08 18:37:38 +00001150 Innermost.first,
1151 Innermost.second),
Douglas Gregor1e1e9722012-03-28 14:34:23 +00001152 /*InsertPos=*/0);
Chandler Carruth80f5b162011-08-18 09:09:59 +00001153 } else if (isFriend) {
1154 // Note, we need this connection even if the friend doesn't have a body.
1155 // Its body may exist but not have been attached yet due to deferred
1156 // parsing.
1157 // FIXME: It might be cleaner to set this when attaching the body to the
1158 // friend function declaration, however that would require finding all the
1159 // instantiations and modifying them.
John McCalld325daa2010-03-26 04:53:08 +00001160 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
John McCall02cace72009-08-28 07:59:38 +00001161 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001162
Douglas Gregore53060f2009-06-25 22:08:12 +00001163 if (InitFunctionInstantiation(Function, D))
1164 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001165
John McCallaf2094e2010-04-08 09:05:18 +00001166 bool isExplicitSpecialization = false;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001167
John McCall68263142009-11-18 22:49:29 +00001168 LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
1169 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
1170
John McCallaf2094e2010-04-08 09:05:18 +00001171 if (DependentFunctionTemplateSpecializationInfo *Info
1172 = D->getDependentSpecializationInfo()) {
1173 assert(isFriend && "non-friend has dependent specialization info?");
1174
1175 // This needs to be set now for future sanity.
1176 Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1177
1178 // Instantiate the explicit template arguments.
1179 TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1180 Info->getRAngleLoc());
Douglas Gregore02e2622010-12-22 21:19:48 +00001181 if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(),
1182 ExplicitArgs, TemplateArgs))
1183 return 0;
John McCallaf2094e2010-04-08 09:05:18 +00001184
1185 // Map the candidate templates to their instantiations.
1186 for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1187 Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1188 Info->getTemplate(I),
1189 TemplateArgs);
1190 if (!Temp) return 0;
1191
1192 Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1193 }
1194
1195 if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1196 &ExplicitArgs,
1197 Previous))
1198 Function->setInvalidDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001199
John McCallaf2094e2010-04-08 09:05:18 +00001200 isExplicitSpecialization = true;
1201
1202 } else if (TemplateParams || !FunctionTemplate) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001203 // Look only into the namespace where the friend would be declared to
1204 // find a previous declaration. This is the innermost enclosing namespace,
Douglas Gregora735b202009-10-13 14:39:41 +00001205 // as described in ActOnFriendFunctionDecl.
John McCall68263142009-11-18 22:49:29 +00001206 SemaRef.LookupQualifiedName(Previous, DC);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001207
Douglas Gregora735b202009-10-13 14:39:41 +00001208 // In C++, the previous declaration we find might be a tag type
1209 // (class or enum). In this case, the new declaration will hide the
1210 // tag type. Note that this does does not apply if we're declaring a
1211 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001212 if (Previous.isSingleTagDecl())
1213 Previous.clear();
Douglas Gregora735b202009-10-13 14:39:41 +00001214 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001215
John McCall9f54ad42009-12-10 09:41:52 +00001216 SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00001217 isExplicitSpecialization);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001218
John McCall76d32642010-04-24 01:30:58 +00001219 NamedDecl *PrincipalDecl = (TemplateParams
1220 ? cast<NamedDecl>(FunctionTemplate)
1221 : Function);
1222
Douglas Gregora735b202009-10-13 14:39:41 +00001223 // If the original function was part of a friend declaration,
1224 // inherit its namespace state and add it to the owner.
John McCalld325daa2010-03-26 04:53:08 +00001225 if (isFriend) {
John McCall68263142009-11-18 22:49:29 +00001226 NamedDecl *PrevDecl;
John McCall76d32642010-04-24 01:30:58 +00001227 if (TemplateParams)
Douglas Gregoref96ee02012-01-14 16:38:05 +00001228 PrevDecl = FunctionTemplate->getPreviousDecl();
John McCall76d32642010-04-24 01:30:58 +00001229 else
Douglas Gregoref96ee02012-01-14 16:38:05 +00001230 PrevDecl = Function->getPreviousDecl();
John McCall76d32642010-04-24 01:30:58 +00001231
1232 PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001233 DC->makeDeclVisibleInContext(PrincipalDecl);
Gabor Greifab297ac2010-08-30 21:10:05 +00001234
Gabor Greif77535df2010-08-30 22:25:56 +00001235 bool queuedInstantiation = false;
Gabor Greifab297ac2010-08-30 21:10:05 +00001236
Richard Smith53e53512011-10-19 00:54:10 +00001237 // C++98 [temp.friend]p5: When a function is defined in a friend function
1238 // declaration in a class template, the function is defined at each
1239 // instantiation of the class template. The function is defined even if it
1240 // is never used.
1241 // C++11 [temp.friend]p4: When a function is defined in a friend function
1242 // declaration in a class template, the function is instantiated when the
1243 // function is odr-used.
1244 //
1245 // If -Wc++98-compat is enabled, we go through the motions of checking for a
1246 // redefinition, but don't instantiate the function.
David Blaikie4e4d0842012-03-11 07:00:24 +00001247 if ((!SemaRef.getLangOpts().CPlusPlus0x ||
Richard Smith53e53512011-10-19 00:54:10 +00001248 SemaRef.Diags.getDiagnosticLevel(
1249 diag::warn_cxx98_compat_friend_redefinition,
1250 Function->getLocation())
1251 != DiagnosticsEngine::Ignored) &&
Douglas Gregor238058c2010-05-18 05:45:02 +00001252 D->isThisDeclarationADefinition()) {
1253 // Check for a function body.
1254 const FunctionDecl *Definition = 0;
Sean Hunt10620eb2011-05-06 20:44:56 +00001255 if (Function->isDefined(Definition) &&
Douglas Gregor238058c2010-05-18 05:45:02 +00001256 Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
Richard Smith53e53512011-10-19 00:54:10 +00001257 SemaRef.Diag(Function->getLocation(),
David Blaikie4e4d0842012-03-11 07:00:24 +00001258 SemaRef.getLangOpts().CPlusPlus0x ?
Richard Smith53e53512011-10-19 00:54:10 +00001259 diag::warn_cxx98_compat_friend_redefinition :
1260 diag::err_redefinition) << Function->getDeclName();
Douglas Gregor238058c2010-05-18 05:45:02 +00001261 SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
David Blaikie4e4d0842012-03-11 07:00:24 +00001262 if (!SemaRef.getLangOpts().CPlusPlus0x)
Richard Smith53e53512011-10-19 00:54:10 +00001263 Function->setInvalidDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001264 }
Douglas Gregor238058c2010-05-18 05:45:02 +00001265 // Check for redefinitions due to other instantiations of this or
1266 // a similar friend function.
1267 else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1268 REnd = Function->redecls_end();
1269 R != REnd; ++R) {
Gabor Greif13a8aff2010-08-28 15:42:30 +00001270 if (*R == Function)
1271 continue;
Gabor Greifab297ac2010-08-30 21:10:05 +00001272 switch (R->getFriendObjectKind()) {
1273 case Decl::FOK_None:
David Blaikie4e4d0842012-03-11 07:00:24 +00001274 if (!SemaRef.getLangOpts().CPlusPlus0x &&
Richard Smith53e53512011-10-19 00:54:10 +00001275 !queuedInstantiation && R->isUsed(false)) {
Gabor Greifab297ac2010-08-30 21:10:05 +00001276 if (MemberSpecializationInfo *MSInfo
1277 = Function->getMemberSpecializationInfo()) {
1278 if (MSInfo->getPointOfInstantiation().isInvalid()) {
1279 SourceLocation Loc = R->getLocation(); // FIXME
1280 MSInfo->setPointOfInstantiation(Loc);
1281 SemaRef.PendingLocalImplicitInstantiations.push_back(
1282 std::make_pair(Function, Loc));
1283 queuedInstantiation = true;
1284 }
1285 }
1286 }
1287 break;
1288 default:
Douglas Gregor238058c2010-05-18 05:45:02 +00001289 if (const FunctionDecl *RPattern
Gabor Greif6a557d82010-08-28 15:46:56 +00001290 = R->getTemplateInstantiationPattern())
Sean Hunt10620eb2011-05-06 20:44:56 +00001291 if (RPattern->isDefined(RPattern)) {
Richard Smith53e53512011-10-19 00:54:10 +00001292 SemaRef.Diag(Function->getLocation(),
David Blaikie4e4d0842012-03-11 07:00:24 +00001293 SemaRef.getLangOpts().CPlusPlus0x ?
Richard Smith53e53512011-10-19 00:54:10 +00001294 diag::warn_cxx98_compat_friend_redefinition :
1295 diag::err_redefinition)
Douglas Gregor238058c2010-05-18 05:45:02 +00001296 << Function->getDeclName();
Gabor Greif6a557d82010-08-28 15:46:56 +00001297 SemaRef.Diag(R->getLocation(), diag::note_previous_definition);
David Blaikie4e4d0842012-03-11 07:00:24 +00001298 if (!SemaRef.getLangOpts().CPlusPlus0x)
Richard Smith53e53512011-10-19 00:54:10 +00001299 Function->setInvalidDecl();
Douglas Gregor238058c2010-05-18 05:45:02 +00001300 break;
1301 }
1302 }
1303 }
1304 }
Douglas Gregora735b202009-10-13 14:39:41 +00001305 }
1306
John McCall76d32642010-04-24 01:30:58 +00001307 if (Function->isOverloadedOperator() && !DC->isRecord() &&
1308 PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
1309 PrincipalDecl->setNonMemberOperator();
1310
Sean Hunteb88ae52011-05-23 21:07:59 +00001311 assert(!D->isDefaulted() && "only methods should be defaulted");
Douglas Gregore53060f2009-06-25 22:08:12 +00001312 return Function;
1313}
1314
Douglas Gregord60e1052009-08-27 16:57:43 +00001315Decl *
1316TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
Francois Pichetaf0f4d02011-08-14 03:52:19 +00001317 TemplateParameterList *TemplateParams,
1318 bool IsClassScopeSpecialization) {
Douglas Gregor6b906862009-08-21 00:16:32 +00001319 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
Douglas Gregord60e1052009-08-27 16:57:43 +00001320 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +00001321 // We are creating a function template specialization from a function
1322 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +00001323 // specialization for this particular set of template arguments.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001324 std::pair<const TemplateArgument *, unsigned> Innermost
Douglas Gregor24bae922010-07-08 18:37:38 +00001325 = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +00001326
Douglas Gregor1e1e9722012-03-28 14:34:23 +00001327 void *InsertPos = 0;
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001328 FunctionDecl *SpecFunc
1329 = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
1330 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001331
Douglas Gregor6b906862009-08-21 00:16:32 +00001332 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001333 if (SpecFunc)
1334 return SpecFunc;
Douglas Gregor6b906862009-08-21 00:16:32 +00001335 }
1336
John McCallb0cb0222010-03-27 05:57:59 +00001337 bool isFriend;
1338 if (FunctionTemplate)
1339 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1340 else
1341 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1342
Douglas Gregor79c22782010-01-16 20:21:20 +00001343 bool MergeWithParentScope = (TemplateParams != 0) ||
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001344 !(isa<Decl>(Owner) &&
Douglas Gregor79c22782010-01-16 20:21:20 +00001345 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
John McCall2a7fb272010-08-25 05:32:35 +00001346 LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Douglas Gregor48dd19b2009-05-14 21:44:34 +00001347
John McCall4eab39f2010-10-19 02:26:41 +00001348 // Instantiate enclosing template arguments for friends.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001349 SmallVector<TemplateParameterList *, 4> TempParamLists;
John McCall4eab39f2010-10-19 02:26:41 +00001350 unsigned NumTempParamLists = 0;
1351 if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) {
1352 TempParamLists.set_size(NumTempParamLists);
1353 for (unsigned I = 0; I != NumTempParamLists; ++I) {
1354 TemplateParameterList *TempParams = D->getTemplateParameterList(I);
1355 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1356 if (!InstParams)
1357 return NULL;
1358 TempParamLists[I] = InstParams;
1359 }
1360 }
1361
Chris Lattner5f9e2722011-07-23 10:55:15 +00001362 SmallVector<ParmVarDecl *, 4> Params;
Benjamin Kramerdc370c12012-01-20 14:42:32 +00001363 TypeSourceInfo *TInfo = SubstFunctionType(D, Params);
John McCall21ef0fa2010-03-11 09:03:00 +00001364 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001365 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +00001366 QualType T = TInfo->getType();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001367
Abramo Bagnara723df242010-12-14 22:11:44 +00001368 // \brief If the type of this function, after ignoring parentheses,
1369 // is not *directly* a function type, then we're instantiating a function
1370 // that was declared via a typedef, e.g.,
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001371 //
1372 // typedef int functype(int, int);
1373 // functype func;
1374 //
1375 // In this case, we'll just go instantiate the ParmVarDecls that we
1376 // synthesized in the method declaration.
Abramo Bagnara723df242010-12-14 22:11:44 +00001377 if (!isa<FunctionProtoType>(T.IgnoreParens())) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001378 assert(!Params.size() && "Instantiating type could not yield parameters");
Chris Lattner5f9e2722011-07-23 10:55:15 +00001379 SmallVector<QualType, 4> ParamTypes;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001380 if (SemaRef.SubstParmTypes(D->getLocation(), D->param_begin(),
1381 D->getNumParams(), TemplateArgs, ParamTypes,
Douglas Gregor12c9c002011-01-07 16:43:16 +00001382 &Params))
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001383 return 0;
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001384 }
1385
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001386 NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
1387 if (QualifierLoc) {
1388 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
John McCallb0cb0222010-03-27 05:57:59 +00001389 TemplateArgs);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001390 if (!QualifierLoc)
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001391 return 0;
John McCallb0cb0222010-03-27 05:57:59 +00001392 }
1393
1394 DeclContext *DC = Owner;
1395 if (isFriend) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001396 if (QualifierLoc) {
John McCallb0cb0222010-03-27 05:57:59 +00001397 CXXScopeSpec SS;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001398 SS.Adopt(QualifierLoc);
John McCallb0cb0222010-03-27 05:57:59 +00001399 DC = SemaRef.computeDeclContext(SS);
John McCallc54d6882010-10-19 05:01:53 +00001400
1401 if (DC && SemaRef.RequireCompleteDeclContext(SS, DC))
1402 return 0;
John McCallb0cb0222010-03-27 05:57:59 +00001403 } else {
1404 DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1405 D->getDeclContext(),
1406 TemplateArgs);
1407 }
1408 if (!DC) return 0;
1409 }
1410
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001411 // Build the instantiated method declaration.
John McCallb0cb0222010-03-27 05:57:59 +00001412 CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
Douglas Gregordec06662009-08-21 18:42:58 +00001413 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001414
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001415 SourceLocation StartLoc = D->getInnerLocStart();
Abramo Bagnara25777432010-08-11 22:01:17 +00001416 DeclarationNameInfo NameInfo
1417 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001418 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001419 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001420 StartLoc, NameInfo, T, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001421 Constructor->isExplicit(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001422 Constructor->isInlineSpecified(),
Richard Smith86c3ae42012-02-13 03:54:03 +00001423 false, Constructor->isConstexpr());
Douglas Gregor17e32f32009-08-21 22:43:28 +00001424 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001425 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001426 StartLoc, NameInfo, T, TInfo,
Abramo Bagnara25777432010-08-11 22:01:17 +00001427 Destructor->isInlineSpecified(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001428 false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001429 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001430 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001431 StartLoc, NameInfo, T, TInfo,
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001432 Conversion->isInlineSpecified(),
Douglas Gregorf5251602011-03-08 17:10:18 +00001433 Conversion->isExplicit(),
Richard Smith86c3ae42012-02-13 03:54:03 +00001434 Conversion->isConstexpr(),
Richard Smith9f569cc2011-10-01 02:31:28 +00001435 Conversion->getLocEnd());
Douglas Gregordec06662009-08-21 18:42:58 +00001436 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00001437 Method = CXXMethodDecl::Create(SemaRef.Context, Record,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001438 StartLoc, NameInfo, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001439 D->isStatic(),
1440 D->getStorageClassAsWritten(),
Douglas Gregorf5251602011-03-08 17:10:18 +00001441 D->isInlineSpecified(),
Richard Smith86c3ae42012-02-13 03:54:03 +00001442 D->isConstexpr(), D->getLocEnd());
Douglas Gregordec06662009-08-21 18:42:58 +00001443 }
Douglas Gregor6b906862009-08-21 00:16:32 +00001444
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001445 if (QualifierLoc)
1446 Method->setQualifierInfo(QualifierLoc);
John McCallb6217662010-03-15 10:12:16 +00001447
Douglas Gregord60e1052009-08-27 16:57:43 +00001448 if (TemplateParams) {
1449 // Our resulting instantiation is actually a function template, since we
1450 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +00001451 //
Douglas Gregord60e1052009-08-27 16:57:43 +00001452 // template<typename T>
1453 // struct X {
1454 // template<typename U> void f(T, U);
1455 // };
1456 //
1457 // X<int> x;
1458 //
1459 // We are instantiating the member template "f" within X<int>, which means
1460 // substituting int for T, but leaving "f" as a member function template.
1461 // Build the function template itself.
1462 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1463 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001464 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +00001465 TemplateParams, Method);
John McCallb0cb0222010-03-27 05:57:59 +00001466 if (isFriend) {
1467 FunctionTemplate->setLexicalDeclContext(Owner);
1468 FunctionTemplate->setObjectOfFriendDecl(true);
1469 } else if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +00001470 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +00001471 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001472 } else if (FunctionTemplate) {
1473 // Record this function template specialization.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001474 std::pair<const TemplateArgument *, unsigned> Innermost
Douglas Gregor24bae922010-07-08 18:37:38 +00001475 = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001476 Method->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001477 TemplateArgumentList::CreateCopy(SemaRef.Context,
1478 Innermost.first,
1479 Innermost.second),
Douglas Gregor1e1e9722012-03-28 14:34:23 +00001480 /*InsertPos=*/0);
John McCallb0cb0222010-03-27 05:57:59 +00001481 } else if (!isFriend) {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001482 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +00001483 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001484 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001485
Mike Stump1eb44332009-09-09 15:08:12 +00001486 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +00001487 // out-of-line, the instantiation will have the same lexical
1488 // context (which will be a namespace scope) as the template.
John McCallb0cb0222010-03-27 05:57:59 +00001489 if (isFriend) {
John McCall4eab39f2010-10-19 02:26:41 +00001490 if (NumTempParamLists)
1491 Method->setTemplateParameterListsInfo(SemaRef.Context,
1492 NumTempParamLists,
1493 TempParamLists.data());
1494
John McCallb0cb0222010-03-27 05:57:59 +00001495 Method->setLexicalDeclContext(Owner);
1496 Method->setObjectOfFriendDecl(true);
1497 } else if (D->isOutOfLine())
Douglas Gregor7caa6822009-07-24 20:34:43 +00001498 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001499
Douglas Gregor5545e162009-03-24 00:38:23 +00001500 // Attach the parameters
1501 for (unsigned P = 0; P < Params.size(); ++P)
1502 Params[P]->setOwningFunction(Method);
David Blaikie4278c652011-09-21 18:16:56 +00001503 Method->setParams(Params);
Douglas Gregor5545e162009-03-24 00:38:23 +00001504
1505 if (InitMethodInstantiation(Method, D))
1506 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001507
Abramo Bagnara25777432010-08-11 22:01:17 +00001508 LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
1509 Sema::ForRedeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001510
John McCallb0cb0222010-03-27 05:57:59 +00001511 if (!FunctionTemplate || TemplateParams || isFriend) {
1512 SemaRef.LookupQualifiedName(Previous, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001513
Douglas Gregordec06662009-08-21 18:42:58 +00001514 // In C++, the previous declaration we find might be a tag type
1515 // (class or enum). In this case, the new declaration will hide the
1516 // tag type. Note that this does does not apply if we're declaring a
1517 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001518 if (Previous.isSingleTagDecl())
1519 Previous.clear();
Douglas Gregordec06662009-08-21 18:42:58 +00001520 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001521
Francois Pichetaf0f4d02011-08-14 03:52:19 +00001522 if (!IsClassScopeSpecialization)
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00001523 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001524
Douglas Gregor4ba31362009-12-01 17:24:26 +00001525 if (D->isPure())
1526 SemaRef.CheckPureMethod(Method, SourceRange());
1527
John McCall46460a62010-01-20 21:53:11 +00001528 Method->setAccess(D->getAccess());
1529
Anders Carlsson9eefa222011-01-20 06:52:44 +00001530 SemaRef.CheckOverrideControl(Method);
1531
Eli Friedman3bc45152011-11-15 22:39:08 +00001532 // If a function is defined as defaulted or deleted, mark it as such now.
1533 if (D->isDefaulted())
1534 Method->setDefaulted();
1535 if (D->isDeletedAsWritten())
1536 Method->setDeletedAsWritten();
1537
John McCallb0cb0222010-03-27 05:57:59 +00001538 if (FunctionTemplate) {
1539 // If there's a function template, let our caller handle it.
1540 } else if (Method->isInvalidDecl() && !Previous.empty()) {
1541 // Don't hide a (potentially) valid declaration with an invalid one.
1542 } else {
1543 NamedDecl *DeclToAdd = (TemplateParams
1544 ? cast<NamedDecl>(FunctionTemplate)
1545 : Method);
1546 if (isFriend)
1547 Record->makeDeclVisibleInContext(DeclToAdd);
Francois Pichetaf0f4d02011-08-14 03:52:19 +00001548 else if (!IsClassScopeSpecialization)
John McCallb0cb0222010-03-27 05:57:59 +00001549 Owner->addDecl(DeclToAdd);
1550 }
Sean Hunteb88ae52011-05-23 21:07:59 +00001551
1552 if (D->isExplicitlyDefaulted()) {
1553 SemaRef.SetDeclDefaulted(Method, Method->getLocation());
1554 } else {
1555 assert(!D->isDefaulted() &&
1556 "should not implicitly default uninstantiated function");
1557 }
1558
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001559 return Method;
1560}
1561
Douglas Gregor615c5d42009-03-24 16:43:20 +00001562Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +00001563 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +00001564}
1565
Douglas Gregor03b2b072009-03-24 00:15:49 +00001566Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001567 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +00001568}
1569
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001570Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001571 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001572}
1573
Douglas Gregor6477b692009-03-25 15:04:13 +00001574ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
John McCallfb44de92011-05-01 22:35:37 +00001575 return SemaRef.SubstParmVarDecl(D, TemplateArgs, /*indexAdjustment*/ 0,
Douglas Gregord1bb4ae2012-01-25 16:15:54 +00001576 llvm::Optional<unsigned>(),
1577 /*ExpectParameterPack=*/false);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001578}
1579
John McCalle29ba202009-08-20 01:44:21 +00001580Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1581 TemplateTypeParmDecl *D) {
1582 // TODO: don't always clone when decls are refcounted.
Chandler Carruth4fb86f82011-05-01 00:51:33 +00001583 assert(D->getTypeForDecl()->isTemplateTypeParmType());
Mike Stump1eb44332009-09-09 15:08:12 +00001584
John McCalle29ba202009-08-20 01:44:21 +00001585 TemplateTypeParmDecl *Inst =
Abramo Bagnara344577e2011-03-06 15:48:19 +00001586 TemplateTypeParmDecl::Create(SemaRef.Context, Owner,
1587 D->getLocStart(), D->getLocation(),
Chandler Carruth4fb86f82011-05-01 00:51:33 +00001588 D->getDepth() - TemplateArgs.getNumLevels(),
1589 D->getIndex(), D->getIdentifier(),
John McCalle29ba202009-08-20 01:44:21 +00001590 D->wasDeclaredWithTypename(),
1591 D->isParameterPack());
Douglas Gregor9a299e02011-03-04 17:52:15 +00001592 Inst->setAccess(AS_public);
John McCalle29ba202009-08-20 01:44:21 +00001593
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001594 if (D->hasDefaultArgument())
1595 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
1596
1597 // Introduce this template parameter's instantiation into the instantiation
Douglas Gregor550d9b22009-10-31 17:21:17 +00001598 // scope.
1599 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001600
John McCalle29ba202009-08-20 01:44:21 +00001601 return Inst;
1602}
1603
Douglas Gregor33642df2009-10-23 23:25:44 +00001604Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1605 NonTypeTemplateParmDecl *D) {
1606 // Substitute into the type of the non-type template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001607 TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc();
Chris Lattner5f9e2722011-07-23 10:55:15 +00001608 SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten;
1609 SmallVector<QualType, 4> ExpandedParameterPackTypes;
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001610 bool IsExpandedParameterPack = false;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001611 TypeSourceInfo *DI;
Douglas Gregor33642df2009-10-23 23:25:44 +00001612 QualType T;
Douglas Gregor33642df2009-10-23 23:25:44 +00001613 bool Invalid = false;
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001614
1615 if (D->isExpandedParameterPack()) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001616 // The non-type template parameter pack is an already-expanded pack
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001617 // expansion of types. Substitute into each of the expanded types.
1618 ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes());
1619 ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes());
1620 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
1621 TypeSourceInfo *NewDI =SemaRef.SubstType(D->getExpansionTypeSourceInfo(I),
1622 TemplateArgs,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001623 D->getLocation(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001624 D->getDeclName());
1625 if (!NewDI)
1626 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001627
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001628 ExpandedParameterPackTypesAsWritten.push_back(NewDI);
1629 QualType NewT =SemaRef.CheckNonTypeTemplateParameterType(NewDI->getType(),
1630 D->getLocation());
1631 if (NewT.isNull())
1632 return 0;
1633 ExpandedParameterPackTypes.push_back(NewT);
1634 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001635
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001636 IsExpandedParameterPack = true;
1637 DI = D->getTypeSourceInfo();
1638 T = DI->getType();
1639 } else if (isa<PackExpansionTypeLoc>(TL)) {
1640 // The non-type template parameter pack's type is a pack expansion of types.
1641 // Determine whether we need to expand this parameter pack into separate
1642 // types.
1643 PackExpansionTypeLoc Expansion = cast<PackExpansionTypeLoc>(TL);
1644 TypeLoc Pattern = Expansion.getPatternLoc();
Chris Lattner5f9e2722011-07-23 10:55:15 +00001645 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001646 SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001647
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001648 // Determine whether the set of unexpanded parameter packs can and should
1649 // be expanded.
1650 bool Expand = true;
1651 bool RetainExpansion = false;
1652 llvm::Optional<unsigned> OrigNumExpansions
1653 = Expansion.getTypePtr()->getNumExpansions();
1654 llvm::Optional<unsigned> NumExpansions = OrigNumExpansions;
1655 if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(),
1656 Pattern.getSourceRange(),
David Blaikiea71f9d02011-09-22 02:34:54 +00001657 Unexpanded,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001658 TemplateArgs,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001659 Expand, RetainExpansion,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001660 NumExpansions))
1661 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001662
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001663 if (Expand) {
1664 for (unsigned I = 0; I != *NumExpansions; ++I) {
1665 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
1666 TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001667 D->getLocation(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001668 D->getDeclName());
1669 if (!NewDI)
1670 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001671
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001672 ExpandedParameterPackTypesAsWritten.push_back(NewDI);
1673 QualType NewT = SemaRef.CheckNonTypeTemplateParameterType(
1674 NewDI->getType(),
1675 D->getLocation());
1676 if (NewT.isNull())
1677 return 0;
1678 ExpandedParameterPackTypes.push_back(NewT);
1679 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001680
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001681 // Note that we have an expanded parameter pack. The "type" of this
1682 // expanded parameter pack is the original expansion type, but callers
1683 // will end up using the expanded parameter pack types for type-checking.
1684 IsExpandedParameterPack = true;
1685 DI = D->getTypeSourceInfo();
1686 T = DI->getType();
1687 } else {
1688 // We cannot fully expand the pack expansion now, so substitute into the
1689 // pattern and create a new pack expansion type.
1690 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
1691 TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001692 D->getLocation(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001693 D->getDeclName());
1694 if (!NewPattern)
1695 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001696
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001697 DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(),
1698 NumExpansions);
1699 if (!DI)
1700 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001701
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001702 T = DI->getType();
1703 }
1704 } else {
1705 // Simple case: substitution into a parameter that is not a parameter pack.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001706 DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001707 D->getLocation(), D->getDeclName());
1708 if (!DI)
1709 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001710
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001711 // Check that this type is acceptable for a non-type template parameter.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001712 T = SemaRef.CheckNonTypeTemplateParameterType(DI->getType(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001713 D->getLocation());
1714 if (T.isNull()) {
1715 T = SemaRef.Context.IntTy;
1716 Invalid = true;
1717 }
Douglas Gregor33642df2009-10-23 23:25:44 +00001718 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001719
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001720 NonTypeTemplateParmDecl *Param;
1721 if (IsExpandedParameterPack)
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001722 Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001723 D->getInnerLocStart(),
1724 D->getLocation(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001725 D->getDepth() - TemplateArgs.getNumLevels(),
1726 D->getPosition(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001727 D->getIdentifier(), T,
1728 DI,
1729 ExpandedParameterPackTypes.data(),
1730 ExpandedParameterPackTypes.size(),
1731 ExpandedParameterPackTypesAsWritten.data());
1732 else
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001733 Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001734 D->getInnerLocStart(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001735 D->getLocation(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001736 D->getDepth() - TemplateArgs.getNumLevels(),
1737 D->getPosition(),
1738 D->getIdentifier(), T,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001739 D->isParameterPack(), DI);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001740
Douglas Gregor9a299e02011-03-04 17:52:15 +00001741 Param->setAccess(AS_public);
Douglas Gregor33642df2009-10-23 23:25:44 +00001742 if (Invalid)
1743 Param->setInvalidDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001744
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001745 Param->setDefaultArgument(D->getDefaultArgument(), false);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001746
1747 // Introduce this template parameter's instantiation into the instantiation
Douglas Gregor550d9b22009-10-31 17:21:17 +00001748 // scope.
1749 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001750 return Param;
1751}
1752
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001753Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001754TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1755 TemplateTemplateParmDecl *D) {
1756 // Instantiate the template parameter list of the template template parameter.
1757 TemplateParameterList *TempParams = D->getTemplateParameters();
1758 TemplateParameterList *InstParams;
1759 {
1760 // Perform the actual substitution of template parameters within a new,
1761 // local instantiation scope.
John McCall2a7fb272010-08-25 05:32:35 +00001762 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor9106ef72009-11-11 16:58:32 +00001763 InstParams = SubstTemplateParams(TempParams);
1764 if (!InstParams)
1765 return NULL;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001766 }
1767
Douglas Gregor9106ef72009-11-11 16:58:32 +00001768 // Build the template template parameter.
1769 TemplateTemplateParmDecl *Param
1770 = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001771 D->getDepth() - TemplateArgs.getNumLevels(),
1772 D->getPosition(), D->isParameterPack(),
Douglas Gregor61c4d282011-01-05 15:48:55 +00001773 D->getIdentifier(), InstParams);
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001774 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor9a299e02011-03-04 17:52:15 +00001775 Param->setAccess(AS_public);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001776
1777 // Introduce this template parameter's instantiation into the instantiation
Douglas Gregor9106ef72009-11-11 16:58:32 +00001778 // scope.
1779 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001780
Douglas Gregor9106ef72009-11-11 16:58:32 +00001781 return Param;
1782}
1783
Douglas Gregor48c32a72009-11-17 06:07:40 +00001784Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Douglas Gregordb992412011-02-25 16:33:46 +00001785 // Using directives are never dependent (and never contain any types or
1786 // expressions), so they require no explicit instantiation work.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001787
Douglas Gregor48c32a72009-11-17 06:07:40 +00001788 UsingDirectiveDecl *Inst
1789 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001790 D->getNamespaceKeyLocation(),
Douglas Gregordb992412011-02-25 16:33:46 +00001791 D->getQualifierLoc(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001792 D->getIdentLocation(),
1793 D->getNominatedNamespace(),
Douglas Gregor48c32a72009-11-17 06:07:40 +00001794 D->getCommonAncestor());
1795 Owner->addDecl(Inst);
1796 return Inst;
1797}
1798
John McCalled976492009-12-04 22:46:56 +00001799Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
Douglas Gregor1b398202010-09-29 17:58:28 +00001800
1801 // The nested name specifier may be dependent, for example
1802 // template <typename T> struct t {
1803 // struct s1 { T f1(); };
1804 // struct s2 : s1 { using s1::f1; };
1805 // };
1806 // template struct t<int>;
1807 // Here, in using s1::f1, s1 refers to t<T>::s1;
1808 // we need to substitute for t<int>::s1.
Douglas Gregor5149f372011-02-25 15:54:31 +00001809 NestedNameSpecifierLoc QualifierLoc
1810 = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
1811 TemplateArgs);
1812 if (!QualifierLoc)
Douglas Gregordc355712011-02-25 00:36:19 +00001813 return 0;
Douglas Gregor1b398202010-09-29 17:58:28 +00001814
1815 // The name info is non-dependent, so no transformation
1816 // is required.
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001817 DeclarationNameInfo NameInfo = D->getNameInfo();
John McCalled976492009-12-04 22:46:56 +00001818
John McCall9f54ad42009-12-10 09:41:52 +00001819 // We only need to do redeclaration lookups if we're in a class
1820 // scope (in fact, it's not really even possible in non-class
1821 // scopes).
1822 bool CheckRedeclaration = Owner->isRecord();
1823
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001824 LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
1825 Sema::ForRedeclaration);
John McCall9f54ad42009-12-10 09:41:52 +00001826
John McCalled976492009-12-04 22:46:56 +00001827 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
John McCalled976492009-12-04 22:46:56 +00001828 D->getUsingLocation(),
Douglas Gregor5149f372011-02-25 15:54:31 +00001829 QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001830 NameInfo,
John McCalled976492009-12-04 22:46:56 +00001831 D->isTypeName());
1832
Douglas Gregor5149f372011-02-25 15:54:31 +00001833 CXXScopeSpec SS;
1834 SS.Adopt(QualifierLoc);
John McCall9f54ad42009-12-10 09:41:52 +00001835 if (CheckRedeclaration) {
1836 Prev.setHideTags(false);
1837 SemaRef.LookupQualifiedName(Prev, Owner);
1838
1839 // Check for invalid redeclarations.
1840 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
1841 D->isTypeName(), SS,
1842 D->getLocation(), Prev))
1843 NewUD->setInvalidDecl();
1844
1845 }
1846
1847 if (!NewUD->isInvalidDecl() &&
1848 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
John McCalled976492009-12-04 22:46:56 +00001849 D->getLocation()))
1850 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00001851
John McCalled976492009-12-04 22:46:56 +00001852 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1853 NewUD->setAccess(D->getAccess());
1854 Owner->addDecl(NewUD);
1855
John McCall9f54ad42009-12-10 09:41:52 +00001856 // Don't process the shadow decls for an invalid decl.
1857 if (NewUD->isInvalidDecl())
1858 return NewUD;
1859
Richard Smithc5a89a12012-04-02 01:30:27 +00001860 if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) {
1861 if (SemaRef.CheckInheritingConstructorUsingDecl(NewUD))
1862 NewUD->setInvalidDecl();
1863 return NewUD;
1864 }
1865
John McCall323c3102009-12-22 22:26:37 +00001866 bool isFunctionScope = Owner->isFunctionOrMethod();
1867
John McCall9f54ad42009-12-10 09:41:52 +00001868 // Process the shadow decls.
1869 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
1870 I != E; ++I) {
1871 UsingShadowDecl *Shadow = *I;
1872 NamedDecl *InstTarget =
Douglas Gregorb7107222011-03-04 19:46:35 +00001873 cast_or_null<NamedDecl>(SemaRef.FindInstantiatedDecl(
1874 Shadow->getLocation(),
1875 Shadow->getTargetDecl(),
1876 TemplateArgs));
1877 if (!InstTarget)
1878 return 0;
John McCall9f54ad42009-12-10 09:41:52 +00001879
1880 if (CheckRedeclaration &&
1881 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
1882 continue;
1883
1884 UsingShadowDecl *InstShadow
1885 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
1886 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
John McCall323c3102009-12-22 22:26:37 +00001887
1888 if (isFunctionScope)
1889 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
John McCall9f54ad42009-12-10 09:41:52 +00001890 }
John McCalled976492009-12-04 22:46:56 +00001891
1892 return NewUD;
1893}
1894
1895Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00001896 // Ignore these; we handle them in bulk when processing the UsingDecl.
1897 return 0;
John McCalled976492009-12-04 22:46:56 +00001898}
1899
John McCall7ba107a2009-11-18 02:36:19 +00001900Decl * TemplateDeclInstantiator
1901 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Douglas Gregor5149f372011-02-25 15:54:31 +00001902 NestedNameSpecifierLoc QualifierLoc
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001903 = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
Douglas Gregor5149f372011-02-25 15:54:31 +00001904 TemplateArgs);
1905 if (!QualifierLoc)
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001906 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001907
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001908 CXXScopeSpec SS;
Douglas Gregor5149f372011-02-25 15:54:31 +00001909 SS.Adopt(QualifierLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001910
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001911 // Since NameInfo refers to a typename, it cannot be a C++ special name.
1912 // Hence, no tranformation is required for it.
1913 DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001914 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00001915 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001916 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00001917 /*instantiation*/ true,
1918 /*typename*/ true, D->getTypenameLoc());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001919 if (UD)
John McCalled976492009-12-04 22:46:56 +00001920 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1921
John McCall7ba107a2009-11-18 02:36:19 +00001922 return UD;
1923}
1924
1925Decl * TemplateDeclInstantiator
1926 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Douglas Gregor5149f372011-02-25 15:54:31 +00001927 NestedNameSpecifierLoc QualifierLoc
1928 = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), TemplateArgs);
1929 if (!QualifierLoc)
John McCall7ba107a2009-11-18 02:36:19 +00001930 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001931
John McCall7ba107a2009-11-18 02:36:19 +00001932 CXXScopeSpec SS;
Douglas Gregor5149f372011-02-25 15:54:31 +00001933 SS.Adopt(QualifierLoc);
John McCall7ba107a2009-11-18 02:36:19 +00001934
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001935 DeclarationNameInfo NameInfo
1936 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
1937
John McCall7ba107a2009-11-18 02:36:19 +00001938 NamedDecl *UD =
1939 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001940 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00001941 /*instantiation*/ true,
1942 /*typename*/ false, SourceLocation());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001943 if (UD)
John McCalled976492009-12-04 22:46:56 +00001944 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1945
Anders Carlsson0d8df782009-08-29 19:37:28 +00001946 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001947}
1948
Francois Pichetaf0f4d02011-08-14 03:52:19 +00001949
1950Decl *TemplateDeclInstantiator::VisitClassScopeFunctionSpecializationDecl(
1951 ClassScopeFunctionSpecializationDecl *Decl) {
1952 CXXMethodDecl *OldFD = Decl->getSpecialization();
1953 CXXMethodDecl *NewFD = cast<CXXMethodDecl>(VisitCXXMethodDecl(OldFD, 0, true));
1954
1955 LookupResult Previous(SemaRef, NewFD->getNameInfo(), Sema::LookupOrdinaryName,
1956 Sema::ForRedeclaration);
1957
1958 SemaRef.LookupQualifiedName(Previous, SemaRef.CurContext);
1959 if (SemaRef.CheckFunctionTemplateSpecialization(NewFD, 0, Previous)) {
1960 NewFD->setInvalidDecl();
1961 return NewFD;
1962 }
1963
1964 // Associate the specialization with the pattern.
1965 FunctionDecl *Specialization = cast<FunctionDecl>(Previous.getFoundDecl());
1966 assert(Specialization && "Class scope Specialization is null");
1967 SemaRef.Context.setClassScopeSpecializationPattern(Specialization, OldFD);
1968
1969 return NewFD;
1970}
1971
John McCallce3ff2b2009-08-25 22:02:44 +00001972Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001973 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001974 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor2fa98002010-02-16 19:28:15 +00001975 if (D->isInvalidDecl())
1976 return 0;
1977
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001978 return Instantiator.Visit(D);
1979}
1980
John McCalle29ba202009-08-20 01:44:21 +00001981/// \brief Instantiates a nested template parameter list in the current
1982/// instantiation context.
1983///
1984/// \param L The parameter list to instantiate
1985///
1986/// \returns NULL if there was an error
1987TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001988TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001989 // Get errors for all the parameters before bailing out.
1990 bool Invalid = false;
1991
1992 unsigned N = L->size();
Chris Lattner5f9e2722011-07-23 10:55:15 +00001993 typedef SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001994 ParamVector Params;
1995 Params.reserve(N);
1996 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1997 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001998 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001999 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00002000 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00002001 }
2002
2003 // Clean up if we had an error.
Douglas Gregorff331c12010-07-25 18:17:45 +00002004 if (Invalid)
John McCalle29ba202009-08-20 01:44:21 +00002005 return NULL;
John McCalle29ba202009-08-20 01:44:21 +00002006
2007 TemplateParameterList *InstL
2008 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
2009 L->getLAngleLoc(), &Params.front(), N,
2010 L->getRAngleLoc());
2011 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00002012}
John McCalle29ba202009-08-20 01:44:21 +00002013
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002014/// \brief Instantiate the declaration of a class template partial
Douglas Gregored9c0f92009-10-29 00:04:11 +00002015/// specialization.
2016///
2017/// \param ClassTemplate the (instantiated) class template that is partially
2018// specialized by the instantiation of \p PartialSpec.
2019///
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002020/// \param PartialSpec the (uninstantiated) class template partial
Douglas Gregored9c0f92009-10-29 00:04:11 +00002021/// specialization that we are instantiating.
2022///
Douglas Gregord65587f2010-11-10 19:44:59 +00002023/// \returns The instantiated partial specialization, if successful; otherwise,
2024/// NULL to indicate an error.
2025ClassTemplatePartialSpecializationDecl *
Douglas Gregored9c0f92009-10-29 00:04:11 +00002026TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
2027 ClassTemplateDecl *ClassTemplate,
2028 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00002029 // Create a local instantiation scope for this class template partial
2030 // specialization, which will contain the instantiations of the template
2031 // parameters.
John McCall2a7fb272010-08-25 05:32:35 +00002032 LocalInstantiationScope Scope(SemaRef);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002033
Douglas Gregored9c0f92009-10-29 00:04:11 +00002034 // Substitute into the template parameters of the class template partial
2035 // specialization.
2036 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
2037 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
2038 if (!InstParams)
Douglas Gregord65587f2010-11-10 19:44:59 +00002039 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002040
Douglas Gregored9c0f92009-10-29 00:04:11 +00002041 // Substitute into the template arguments of the class template partial
2042 // specialization.
John McCalld5532b62009-11-23 01:53:49 +00002043 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002044 if (SemaRef.Subst(PartialSpec->getTemplateArgsAsWritten(),
2045 PartialSpec->getNumTemplateArgsAsWritten(),
Douglas Gregore02e2622010-12-22 21:19:48 +00002046 InstTemplateArgs, TemplateArgs))
2047 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002048
Douglas Gregored9c0f92009-10-29 00:04:11 +00002049 // Check that the template argument list is well-formed for this
2050 // class template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002051 SmallVector<TemplateArgument, 4> Converted;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002052 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002053 PartialSpec->getLocation(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002054 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002055 false,
2056 Converted))
Douglas Gregord65587f2010-11-10 19:44:59 +00002057 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00002058
2059 // Figure out where to insert this class template partial specialization
2060 // in the member template's set of class template partial specializations.
Douglas Gregored9c0f92009-10-29 00:04:11 +00002061 void *InsertPos = 0;
2062 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00002063 = ClassTemplate->findPartialSpecialization(Converted.data(),
2064 Converted.size(), InsertPos);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002065
Douglas Gregored9c0f92009-10-29 00:04:11 +00002066 // Build the canonical type that describes the converted template
2067 // arguments of the class template partial specialization.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002068 QualType CanonType
Douglas Gregored9c0f92009-10-29 00:04:11 +00002069 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
Douglas Gregor910f8002010-11-07 23:05:16 +00002070 Converted.data(),
2071 Converted.size());
Douglas Gregored9c0f92009-10-29 00:04:11 +00002072
2073 // Build the fully-sugared type for this class template
2074 // specialization as the user wrote in the specialization
2075 // itself. This means that we'll pretty-print the type retrieved
2076 // from the specialization's declaration the way that the user
2077 // actually wrote the specialization, rather than formatting the
2078 // name based on the "canonical" representation used to store the
2079 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00002080 TypeSourceInfo *WrittenTy
2081 = SemaRef.Context.getTemplateSpecializationTypeInfo(
2082 TemplateName(ClassTemplate),
2083 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00002084 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002085 CanonType);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002086
Douglas Gregored9c0f92009-10-29 00:04:11 +00002087 if (PrevDecl) {
2088 // We've already seen a partial specialization with the same template
2089 // parameters and template arguments. This can happen, for example, when
2090 // substituting the outer template arguments ends up causing two
2091 // class template partial specializations of a member class template
2092 // to have identical forms, e.g.,
2093 //
2094 // template<typename T, typename U>
2095 // struct Outer {
2096 // template<typename X, typename Y> struct Inner;
2097 // template<typename Y> struct Inner<T, Y>;
2098 // template<typename Y> struct Inner<U, Y>;
2099 // };
2100 //
2101 // Outer<int, int> outer; // error: the partial specializations of Inner
2102 // // have the same signature.
2103 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
Douglas Gregord65587f2010-11-10 19:44:59 +00002104 << WrittenTy->getType();
Douglas Gregored9c0f92009-10-29 00:04:11 +00002105 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
2106 << SemaRef.Context.getTypeDeclType(PrevDecl);
Douglas Gregord65587f2010-11-10 19:44:59 +00002107 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00002108 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002109
2110
Douglas Gregored9c0f92009-10-29 00:04:11 +00002111 // Create the class template partial specialization declaration.
2112 ClassTemplatePartialSpecializationDecl *InstPartialSpec
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002113 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00002114 PartialSpec->getTagKind(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002115 Owner,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002116 PartialSpec->getLocStart(),
2117 PartialSpec->getLocation(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002118 InstParams,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002119 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00002120 Converted.data(),
2121 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00002122 InstTemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00002123 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00002124 0,
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00002125 ClassTemplate->getNextPartialSpecSequenceNumber());
John McCallb6217662010-03-15 10:12:16 +00002126 // Substitute the nested name specifier, if any.
2127 if (SubstQualifier(PartialSpec, InstPartialSpec))
2128 return 0;
2129
Douglas Gregored9c0f92009-10-29 00:04:11 +00002130 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00002131 InstPartialSpec->setTypeAsWritten(WrittenTy);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002132
Douglas Gregored9c0f92009-10-29 00:04:11 +00002133 // Add this partial specialization to the set of class template partial
2134 // specializations.
Douglas Gregor1e1e9722012-03-28 14:34:23 +00002135 ClassTemplate->AddPartialSpecialization(InstPartialSpec, /*InsertPos=*/0);
Douglas Gregord65587f2010-11-10 19:44:59 +00002136 return InstPartialSpec;
Douglas Gregored9c0f92009-10-29 00:04:11 +00002137}
2138
John McCall21ef0fa2010-03-11 09:03:00 +00002139TypeSourceInfo*
John McCallce3ff2b2009-08-25 22:02:44 +00002140TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002141 SmallVectorImpl<ParmVarDecl *> &Params) {
John McCall21ef0fa2010-03-11 09:03:00 +00002142 TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
2143 assert(OldTInfo && "substituting function without type source info");
2144 assert(Params.empty() && "parameter vector is non-empty at start");
Douglas Gregorcefc3af2012-04-16 07:05:22 +00002145
2146 CXXRecordDecl *ThisContext = 0;
2147 unsigned ThisTypeQuals = 0;
2148 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2149 ThisContext = Method->getParent();
2150 ThisTypeQuals = Method->getTypeQualifiers();
2151 }
2152
John McCall6cd3b9f2010-04-09 17:38:44 +00002153 TypeSourceInfo *NewTInfo
2154 = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
2155 D->getTypeSpecStartLoc(),
Douglas Gregorcefc3af2012-04-16 07:05:22 +00002156 D->getDeclName(),
2157 ThisContext, ThisTypeQuals);
John McCall21ef0fa2010-03-11 09:03:00 +00002158 if (!NewTInfo)
2159 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +00002160
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00002161 if (NewTInfo != OldTInfo) {
2162 // Get parameters from the new type info.
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00002163 TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00002164 if (FunctionProtoTypeLoc *OldProtoLoc
2165 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00002166 TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00002167 FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
2168 assert(NewProtoLoc && "Missing prototype?");
Douglas Gregor12c9c002011-01-07 16:43:16 +00002169 unsigned NewIdx = 0, NumNewParams = NewProtoLoc->getNumArgs();
2170 for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc->getNumArgs();
2171 OldIdx != NumOldParams; ++OldIdx) {
2172 ParmVarDecl *OldParam = OldProtoLoc->getArg(OldIdx);
2173 if (!OldParam->isParameterPack() ||
Richard Smith7c5d28b2012-03-13 06:56:52 +00002174 // FIXME: Is this right? OldParam could expand to an empty parameter
2175 // pack and the next parameter could be an unexpanded parameter pack
Douglas Gregor12c9c002011-01-07 16:43:16 +00002176 (NewIdx < NumNewParams &&
2177 NewProtoLoc->getArg(NewIdx)->isParameterPack())) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002178 // Simple case: normal parameter, or a parameter pack that's
Douglas Gregor12c9c002011-01-07 16:43:16 +00002179 // instantiated to a (still-dependent) parameter pack.
2180 ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++);
2181 Params.push_back(NewParam);
2182 SemaRef.CurrentInstantiationScope->InstantiatedLocal(OldParam,
2183 NewParam);
2184 continue;
2185 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002186
Douglas Gregor12c9c002011-01-07 16:43:16 +00002187 // Parameter pack: make the instantiation an argument pack.
2188 SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(
2189 OldParam);
Douglas Gregor21371ea2011-01-11 03:14:20 +00002190 unsigned NumArgumentsInExpansion
2191 = SemaRef.getNumArgumentsInExpansion(OldParam->getType(),
2192 TemplateArgs);
2193 while (NumArgumentsInExpansion--) {
Douglas Gregor12c9c002011-01-07 16:43:16 +00002194 ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++);
2195 Params.push_back(NewParam);
2196 SemaRef.CurrentInstantiationScope->InstantiatedLocalPackArg(OldParam,
2197 NewParam);
2198 }
Douglas Gregor6920cdc2010-05-03 15:32:18 +00002199 }
Douglas Gregor895162d2010-04-30 18:55:50 +00002200 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00002201 } else {
2202 // The function type itself was not dependent and therefore no
2203 // substitution occurred. However, we still need to instantiate
2204 // the function parameters themselves.
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00002205 TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00002206 if (FunctionProtoTypeLoc *OldProtoLoc
2207 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
2208 for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
2209 ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
2210 if (!Parm)
2211 return 0;
2212 Params.push_back(Parm);
2213 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00002214 }
2215 }
John McCall21ef0fa2010-03-11 09:03:00 +00002216 return NewTInfo;
Douglas Gregor5545e162009-03-24 00:38:23 +00002217}
2218
Richard Smithe6975e92012-04-17 00:58:00 +00002219/// Introduce the instantiated function parameters into the local
2220/// instantiation scope, and set the parameter names to those used
2221/// in the template.
2222static void addInstantiatedParametersToScope(Sema &S, FunctionDecl *Function,
2223 const FunctionDecl *PatternDecl,
2224 LocalInstantiationScope &Scope,
2225 const MultiLevelTemplateArgumentList &TemplateArgs) {
2226 unsigned FParamIdx = 0;
2227 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
2228 const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
2229 if (!PatternParam->isParameterPack()) {
2230 // Simple case: not a parameter pack.
2231 assert(FParamIdx < Function->getNumParams());
2232 ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
2233 FunctionParam->setDeclName(PatternParam->getDeclName());
2234 Scope.InstantiatedLocal(PatternParam, FunctionParam);
2235 ++FParamIdx;
2236 continue;
2237 }
2238
2239 // Expand the parameter pack.
2240 Scope.MakeInstantiatedLocalArgPack(PatternParam);
2241 unsigned NumArgumentsInExpansion
2242 = S.getNumArgumentsInExpansion(PatternParam->getType(), TemplateArgs);
2243 for (unsigned Arg = 0; Arg < NumArgumentsInExpansion; ++Arg) {
2244 ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
2245 FunctionParam->setDeclName(PatternParam->getDeclName());
2246 Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam);
2247 ++FParamIdx;
2248 }
2249 }
2250}
2251
2252static void InstantiateExceptionSpec(Sema &SemaRef, FunctionDecl *New,
2253 const FunctionProtoType *Proto,
2254 const MultiLevelTemplateArgumentList &TemplateArgs) {
Richard Smith13bffc52012-04-19 00:08:28 +00002255 assert(Proto->getExceptionSpecType() != EST_Uninstantiated);
2256
Richard Smithe6975e92012-04-17 00:58:00 +00002257 // C++11 [expr.prim.general]p3:
2258 // If a declaration declares a member function or member function
2259 // template of a class X, the expression this is a prvalue of type
2260 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
2261 // and the end of the function-definition, member-declarator, or
2262 // declarator.
2263 CXXRecordDecl *ThisContext = 0;
2264 unsigned ThisTypeQuals = 0;
2265 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(New)) {
2266 ThisContext = Method->getParent();
2267 ThisTypeQuals = Method->getTypeQualifiers();
2268 }
2269 Sema::CXXThisScopeRAII ThisScope(SemaRef, ThisContext, ThisTypeQuals,
2270 SemaRef.getLangOpts().CPlusPlus0x);
2271
2272 // The function has an exception specification or a "noreturn"
2273 // attribute. Substitute into each of the exception types.
2274 SmallVector<QualType, 4> Exceptions;
2275 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
2276 // FIXME: Poor location information!
2277 if (const PackExpansionType *PackExpansion
2278 = Proto->getExceptionType(I)->getAs<PackExpansionType>()) {
2279 // We have a pack expansion. Instantiate it.
2280 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2281 SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(),
2282 Unexpanded);
2283 assert(!Unexpanded.empty() &&
2284 "Pack expansion without parameter packs?");
2285
2286 bool Expand = false;
2287 bool RetainExpansion = false;
2288 llvm::Optional<unsigned> NumExpansions
2289 = PackExpansion->getNumExpansions();
2290 if (SemaRef.CheckParameterPacksForExpansion(New->getLocation(),
2291 SourceRange(),
2292 Unexpanded,
2293 TemplateArgs,
2294 Expand,
2295 RetainExpansion,
2296 NumExpansions))
2297 break;
2298
2299 if (!Expand) {
2300 // We can't expand this pack expansion into separate arguments yet;
2301 // just substitute into the pattern and create a new pack expansion
2302 // type.
2303 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
2304 QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2305 TemplateArgs,
2306 New->getLocation(), New->getDeclName());
2307 if (T.isNull())
2308 break;
2309
2310 T = SemaRef.Context.getPackExpansionType(T, NumExpansions);
2311 Exceptions.push_back(T);
2312 continue;
2313 }
2314
2315 // Substitute into the pack expansion pattern for each template
2316 bool Invalid = false;
2317 for (unsigned ArgIdx = 0; ArgIdx != *NumExpansions; ++ArgIdx) {
2318 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, ArgIdx);
2319
2320 QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2321 TemplateArgs,
2322 New->getLocation(), New->getDeclName());
2323 if (T.isNull()) {
2324 Invalid = true;
2325 break;
2326 }
2327
2328 Exceptions.push_back(T);
2329 }
2330
2331 if (Invalid)
2332 break;
2333
2334 continue;
2335 }
2336
2337 QualType T
2338 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
2339 New->getLocation(), New->getDeclName());
2340 if (T.isNull() ||
2341 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
2342 continue;
2343
2344 Exceptions.push_back(T);
2345 }
2346 Expr *NoexceptExpr = 0;
2347 if (Expr *OldNoexceptExpr = Proto->getNoexceptExpr()) {
2348 EnterExpressionEvaluationContext Unevaluated(SemaRef,
2349 Sema::ConstantEvaluated);
2350 ExprResult E = SemaRef.SubstExpr(OldNoexceptExpr, TemplateArgs);
2351 if (E.isUsable())
2352 E = SemaRef.CheckBooleanCondition(E.get(), E.get()->getLocStart());
2353
2354 if (E.isUsable()) {
2355 NoexceptExpr = E.take();
2356 if (!NoexceptExpr->isTypeDependent() &&
2357 !NoexceptExpr->isValueDependent())
Douglas Gregorab41fe92012-05-04 22:38:52 +00002358 NoexceptExpr
2359 = SemaRef.VerifyIntegerConstantExpression(NoexceptExpr,
2360 0, diag::err_noexcept_needs_constant_expression,
2361 /*AllowFold*/ false).take();
Richard Smithe6975e92012-04-17 00:58:00 +00002362 }
2363 }
2364
2365 // Rebuild the function type
2366 const FunctionProtoType *NewProto
2367 = New->getType()->getAs<FunctionProtoType>();
2368 assert(NewProto && "Template instantiation without function prototype?");
2369
2370 FunctionProtoType::ExtProtoInfo EPI = NewProto->getExtProtoInfo();
2371 EPI.ExceptionSpecType = Proto->getExceptionSpecType();
2372 EPI.NumExceptions = Exceptions.size();
2373 EPI.Exceptions = Exceptions.data();
2374 EPI.NoexceptExpr = NoexceptExpr;
2375
2376 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
2377 NewProto->arg_type_begin(),
2378 NewProto->getNumArgs(),
2379 EPI));
2380}
2381
2382void Sema::InstantiateExceptionSpec(SourceLocation PointOfInstantiation,
2383 FunctionDecl *Decl) {
Richard Smith13bffc52012-04-19 00:08:28 +00002384 const FunctionProtoType *Proto = Decl->getType()->castAs<FunctionProtoType>();
2385 if (Proto->getExceptionSpecType() != EST_Uninstantiated)
Richard Smithe6975e92012-04-17 00:58:00 +00002386 return;
2387
2388 InstantiatingTemplate Inst(*this, PointOfInstantiation, Decl,
2389 InstantiatingTemplate::ExceptionSpecification());
2390 if (Inst)
2391 return;
2392
2393 // Enter the scope of this instantiation. We don't use
2394 // PushDeclContext because we don't have a scope.
2395 Sema::ContextRAII savedContext(*this, Decl);
2396 LocalInstantiationScope Scope(*this);
2397
2398 MultiLevelTemplateArgumentList TemplateArgs =
2399 getTemplateInstantiationArgs(Decl, 0, /*RelativeToPrimary*/true);
2400
Richard Smith13bffc52012-04-19 00:08:28 +00002401 FunctionDecl *Template = Proto->getExceptionSpecTemplate();
2402 addInstantiatedParametersToScope(*this, Decl, Template, Scope, TemplateArgs);
Richard Smithe6975e92012-04-17 00:58:00 +00002403
Richard Smith13bffc52012-04-19 00:08:28 +00002404 ::InstantiateExceptionSpec(*this, Decl,
2405 Template->getType()->castAs<FunctionProtoType>(),
2406 TemplateArgs);
Richard Smithe6975e92012-04-17 00:58:00 +00002407}
2408
Mike Stump1eb44332009-09-09 15:08:12 +00002409/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00002410/// declaration (New) from the corresponding fields of its template (Tmpl).
2411///
2412/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00002413bool
2414TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00002415 FunctionDecl *Tmpl) {
Sean Hunt10620eb2011-05-06 20:44:56 +00002416 if (Tmpl->isDeletedAsWritten())
2417 New->setDeletedAsWritten();
Mike Stump1eb44332009-09-09 15:08:12 +00002418
Douglas Gregorcca9e962009-07-01 22:01:06 +00002419 // If we are performing substituting explicitly-specified template arguments
2420 // or deduced template arguments into a function template and we reach this
2421 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00002422 // to keeping the new function template specialization. We therefore
2423 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00002424 // into a template instantiation for this specific function template
2425 // specialization, which is not a SFINAE context, so that we diagnose any
2426 // further errors in the declaration itself.
2427 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
2428 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
2429 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
2430 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00002431 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00002432 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002433 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00002434 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00002435 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00002436 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
2437 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
Douglas Gregorf35f8282009-11-11 21:54:23 +00002438 --SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +00002439 }
2440 }
Mike Stump1eb44332009-09-09 15:08:12 +00002441
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002442 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
2443 assert(Proto && "Function template without prototype?");
2444
Sebastian Redl60618fa2011-03-12 11:50:43 +00002445 if (Proto->hasExceptionSpec() || Proto->getNoReturnAttr()) {
John McCalle23cf432010-12-14 08:05:40 +00002446 FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
John McCalle23cf432010-12-14 08:05:40 +00002447
Richard Smithe6975e92012-04-17 00:58:00 +00002448 // DR1330: In C++11, defer instantiation of a non-trivial
2449 // exception specification.
2450 if (SemaRef.getLangOpts().CPlusPlus0x &&
2451 EPI.ExceptionSpecType != EST_None &&
2452 EPI.ExceptionSpecType != EST_DynamicNone &&
2453 EPI.ExceptionSpecType != EST_BasicNoexcept) {
Richard Smith13bffc52012-04-19 00:08:28 +00002454 FunctionDecl *ExceptionSpecTemplate = Tmpl;
2455 if (EPI.ExceptionSpecType == EST_Uninstantiated)
2456 ExceptionSpecTemplate = EPI.ExceptionSpecTemplate;
2457
Richard Smithe6975e92012-04-17 00:58:00 +00002458 // Mark the function has having an uninstantiated exception specification.
2459 const FunctionProtoType *NewProto
2460 = New->getType()->getAs<FunctionProtoType>();
2461 assert(NewProto && "Template instantiation without function prototype?");
2462 EPI = NewProto->getExtProtoInfo();
2463 EPI.ExceptionSpecType = EST_Uninstantiated;
2464 EPI.ExceptionSpecDecl = New;
Richard Smith13bffc52012-04-19 00:08:28 +00002465 EPI.ExceptionSpecTemplate = ExceptionSpecTemplate;
Richard Smithe6975e92012-04-17 00:58:00 +00002466 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
2467 NewProto->arg_type_begin(),
2468 NewProto->getNumArgs(),
2469 EPI));
2470 } else {
2471 ::InstantiateExceptionSpec(SemaRef, New, Proto, TemplateArgs);
2472 }
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002473 }
2474
Rafael Espindola19f74ac2011-07-06 15:46:09 +00002475 // Get the definition. Leaves the variable unchanged if undefined.
Richard Smithe6975e92012-04-17 00:58:00 +00002476 const FunctionDecl *Definition = Tmpl;
Rafael Espindola19f74ac2011-07-06 15:46:09 +00002477 Tmpl->isDefined(Definition);
2478
DeLesley Hutchins23323e02012-01-20 22:50:54 +00002479 SemaRef.InstantiateAttrs(TemplateArgs, Definition, New,
2480 LateAttrs, StartingScope);
Douglas Gregor7cf84d62010-06-15 17:05:35 +00002481
Douglas Gregore53060f2009-06-25 22:08:12 +00002482 return false;
2483}
2484
Douglas Gregor5545e162009-03-24 00:38:23 +00002485/// \brief Initializes common fields of an instantiated method
2486/// declaration (New) from the corresponding fields of its template
2487/// (Tmpl).
2488///
2489/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00002490bool
2491TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00002492 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00002493 if (InitFunctionInstantiation(New, Tmpl))
2494 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002495
Douglas Gregor5545e162009-03-24 00:38:23 +00002496 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00002497 if (Tmpl->isVirtualAsWritten())
Douglas Gregor85606eb2010-09-28 20:50:54 +00002498 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +00002499
2500 // FIXME: attributes
2501 // FIXME: New needs a pointer to Tmpl
2502 return false;
2503}
Douglas Gregora58861f2009-05-13 20:28:22 +00002504
2505/// \brief Instantiate the definition of the given function from its
2506/// template.
2507///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002508/// \param PointOfInstantiation the point at which the instantiation was
2509/// required. Note that this is not precisely a "point of instantiation"
2510/// for the function, but it's close.
2511///
Douglas Gregora58861f2009-05-13 20:28:22 +00002512/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002513/// function template specialization or member function of a class template
2514/// specialization.
2515///
2516/// \param Recursive if true, recursively instantiates any functions that
2517/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002518///
2519/// \param DefinitionRequired if true, then we are performing an explicit
2520/// instantiation where the body of the function is required. Complain if
2521/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002522void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002523 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002524 bool Recursive,
2525 bool DefinitionRequired) {
Sean Hunt10620eb2011-05-06 20:44:56 +00002526 if (Function->isInvalidDecl() || Function->isDefined())
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002527 return;
2528
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002529 // Never instantiate an explicit specialization except if it is a class scope
2530 // explicit specialization.
2531 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization &&
2532 !Function->getClassScopeSpecializationPattern())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002533 return;
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002534
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002535 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00002536 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Sean Huntf996e052011-05-27 20:00:14 +00002537 assert(PatternDecl && "instantiating a non-template");
2538
2539 Stmt *Pattern = PatternDecl->getBody(PatternDecl);
2540 assert(PatternDecl && "template definition is not a template");
2541 if (!Pattern) {
2542 // Try to find a defaulted definition
2543 PatternDecl->isDefined(PatternDecl);
Sean Huntdfab8542011-05-25 22:02:25 +00002544 }
Sean Huntf996e052011-05-27 20:00:14 +00002545 assert(PatternDecl && "template definition is not a template");
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002546
Francois Pichet8387e2a2011-04-22 22:18:13 +00002547 // Postpone late parsed template instantiations.
Sean Huntf996e052011-05-27 20:00:14 +00002548 if (PatternDecl->isLateTemplateParsed() &&
Nick Lewycky8a29bc02011-05-12 03:51:24 +00002549 !LateTemplateParser) {
Francois Pichet8387e2a2011-04-22 22:18:13 +00002550 PendingInstantiations.push_back(
2551 std::make_pair(Function, PointOfInstantiation));
2552 return;
2553 }
2554
2555 // Call the LateTemplateParser callback if there a need to late parse
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002556 // a templated function definition.
Sean Huntf996e052011-05-27 20:00:14 +00002557 if (!Pattern && PatternDecl->isLateTemplateParsed() &&
Francois Pichet8387e2a2011-04-22 22:18:13 +00002558 LateTemplateParser) {
Francois Pichet4a47e8d2011-04-23 11:52:20 +00002559 LateTemplateParser(OpaqueParser, PatternDecl);
Francois Pichet8387e2a2011-04-22 22:18:13 +00002560 Pattern = PatternDecl->getBody(PatternDecl);
2561 }
2562
Sean Huntf996e052011-05-27 20:00:14 +00002563 if (!Pattern && !PatternDecl->isDefaulted()) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002564 if (DefinitionRequired) {
2565 if (Function->getPrimaryTemplate())
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002566 Diag(PointOfInstantiation,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002567 diag::err_explicit_instantiation_undefined_func_template)
2568 << Function->getPrimaryTemplate();
2569 else
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002570 Diag(PointOfInstantiation,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002571 diag::err_explicit_instantiation_undefined_member)
2572 << 1 << Function->getDeclName() << Function->getDeclContext();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002573
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002574 if (PatternDecl)
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002575 Diag(PatternDecl->getLocation(),
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002576 diag::note_explicit_instantiation_here);
Douglas Gregorcfe833b2010-05-17 17:57:54 +00002577 Function->setInvalidDecl();
Chandler Carruth58e390e2010-08-25 08:27:02 +00002578 } else if (Function->getTemplateSpecializationKind()
2579 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002580 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00002581 std::make_pair(Function, PointOfInstantiation));
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002582 }
Chandler Carruth58e390e2010-08-25 08:27:02 +00002583
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002584 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002585 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002586
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002587 // C++0x [temp.explicit]p9:
2588 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00002589 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002590 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00002591 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002592 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002593 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002594 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002595
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002596 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2597 if (Inst)
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002598 return;
2599
Abramo Bagnarae9946242011-11-18 08:08:52 +00002600 // Copy the inner loc start from the pattern.
2601 Function->setInnerLocStart(PatternDecl->getInnerLocStart());
2602
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002603 // If we're performing recursive template instantiation, create our own
2604 // queue of pending implicit instantiations that we will instantiate later,
2605 // while we're still within our own instantiation context.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002606 SmallVector<VTableUse, 16> SavedVTableUses;
Chandler Carruth62c78d52010-08-25 08:44:16 +00002607 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002608 if (Recursive) {
2609 VTableUses.swap(SavedVTableUses);
Chandler Carruth62c78d52010-08-25 08:44:16 +00002610 PendingInstantiations.swap(SavedPendingInstantiations);
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002611 }
Mike Stump1eb44332009-09-09 15:08:12 +00002612
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002613 EnterExpressionEvaluationContext EvalContext(*this,
John McCallf312b1e2010-08-26 23:41:50 +00002614 Sema::PotentiallyEvaluated);
John McCalld226f652010-08-21 09:40:31 +00002615 ActOnStartOfFunctionDef(0, Function);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002616
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002617 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00002618 // recorded, unless we're actually a member function within a local
2619 // class, in which case we need to merge our results with the parent
2620 // scope (of the enclosing function).
2621 bool MergeWithParentScope = false;
2622 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
2623 MergeWithParentScope = Rec->isLocalClass();
2624
2625 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002626
Richard Smith7c5d28b2012-03-13 06:56:52 +00002627 // Enter the scope of this instantiation. We don't use
2628 // PushDeclContext because we don't have a scope.
2629 Sema::ContextRAII savedContext(*this, Function);
2630
2631 MultiLevelTemplateArgumentList TemplateArgs =
2632 getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
2633
Richard Smithe6975e92012-04-17 00:58:00 +00002634 addInstantiatedParametersToScope(*this, Function, PatternDecl, Scope,
2635 TemplateArgs);
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002636
Sean Huntcd10dec2011-05-23 23:14:04 +00002637 if (PatternDecl->isDefaulted()) {
2638 ActOnFinishFunctionBody(Function, 0, /*IsInstantiation=*/true);
2639
2640 SetDeclDefaulted(Function, PatternDecl->getLocation());
Sean Huntcd10dec2011-05-23 23:14:04 +00002641 } else {
2642 // If this is a constructor, instantiate the member initializers.
2643 if (const CXXConstructorDecl *Ctor =
2644 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2645 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2646 TemplateArgs);
2647 }
2648
2649 // Instantiate the function body.
2650 StmtResult Body = SubstStmt(Pattern, TemplateArgs);
2651
2652 if (Body.isInvalid())
2653 Function->setInvalidDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002654
Sean Huntcd10dec2011-05-23 23:14:04 +00002655 ActOnFinishFunctionBody(Function, Body.get(),
2656 /*IsInstantiation=*/true);
Mike Stump1eb44332009-09-09 15:08:12 +00002657 }
2658
John McCall0c01d182010-03-24 05:22:00 +00002659 PerformDependentDiagnostics(PatternDecl, TemplateArgs);
2660
John McCalleee1d542011-02-14 07:13:47 +00002661 savedContext.pop();
Douglas Gregoraba43bb2009-05-26 20:50:29 +00002662
2663 DeclGroupRef DG(Function);
2664 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00002665
Douglas Gregor60406be2010-01-16 22:29:39 +00002666 // This class may have local implicit instantiations that need to be
2667 // instantiation within this scope.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002668 PerformPendingInstantiations(/*LocalOnly=*/true);
Douglas Gregor60406be2010-01-16 22:29:39 +00002669 Scope.Exit();
2670
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002671 if (Recursive) {
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002672 // Define any pending vtables.
2673 DefineUsedVTables();
2674
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002675 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002676 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002677 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002678
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002679 // Restore the set of pending vtables.
Nick Lewycky81559102011-05-31 07:58:42 +00002680 assert(VTableUses.empty() &&
2681 "VTableUses should be empty before it is discarded.");
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002682 VTableUses.swap(SavedVTableUses);
2683
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002684 // Restore the set of pending implicit instantiations.
Nick Lewycky81559102011-05-31 07:58:42 +00002685 assert(PendingInstantiations.empty() &&
2686 "PendingInstantiations should be empty before it is discarded.");
Chandler Carruth62c78d52010-08-25 08:44:16 +00002687 PendingInstantiations.swap(SavedPendingInstantiations);
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002688 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002689}
2690
2691/// \brief Instantiate the definition of the given variable from its
2692/// template.
2693///
Douglas Gregor7caa6822009-07-24 20:34:43 +00002694/// \param PointOfInstantiation the point at which the instantiation was
2695/// required. Note that this is not precisely a "point of instantiation"
2696/// for the function, but it's close.
2697///
2698/// \param Var the already-instantiated declaration of a static member
2699/// variable of a class template specialization.
2700///
2701/// \param Recursive if true, recursively instantiates any functions that
2702/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002703///
2704/// \param DefinitionRequired if true, then we are performing an explicit
2705/// instantiation where an out-of-line definition of the member variable
2706/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002707void Sema::InstantiateStaticDataMemberDefinition(
2708 SourceLocation PointOfInstantiation,
2709 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002710 bool Recursive,
2711 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002712 if (Var->isInvalidDecl())
2713 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002714
Douglas Gregor7caa6822009-07-24 20:34:43 +00002715 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002716 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002717 assert(Def && "This data member was not instantiated from a template?");
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002718 assert(Def->isStaticDataMember() && "Not a static data member?");
Douglas Gregor0d035142009-10-27 18:42:08 +00002719 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002720
Douglas Gregor0d035142009-10-27 18:42:08 +00002721 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002722 // We did not find an out-of-line definition of this static data member,
2723 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00002724 // instantiate this definition (or provide a specialization for it) in
2725 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002726 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00002727 Def = Var->getInstantiatedFromStaticDataMember();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002728 Diag(PointOfInstantiation,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002729 diag::err_explicit_instantiation_undefined_member)
2730 << 2 << Var->getDeclName() << Var->getDeclContext();
2731 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
Chandler Carruth58e390e2010-08-25 08:27:02 +00002732 } else if (Var->getTemplateSpecializationKind()
2733 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002734 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00002735 std::make_pair(Var, PointOfInstantiation));
2736 }
2737
Douglas Gregor7caa6822009-07-24 20:34:43 +00002738 return;
2739 }
2740
Rafael Espindola234fe652012-03-05 10:54:55 +00002741 TemplateSpecializationKind TSK = Var->getTemplateSpecializationKind();
2742
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002743 // Never instantiate an explicit specialization.
Rafael Espindola234fe652012-03-05 10:54:55 +00002744 if (TSK == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002745 return;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002746
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002747 // C++0x [temp.explicit]p9:
2748 // Except for inline functions, other explicit instantiation declarations
2749 // have the effect of suppressing the implicit instantiation of the entity
2750 // to which they refer.
Rafael Espindola234fe652012-03-05 10:54:55 +00002751 if (TSK == TSK_ExplicitInstantiationDeclaration)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002752 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002753
Rafael Espindola02503932012-03-08 15:51:03 +00002754 Consumer.HandleCXXStaticMemberVarInstantiation(Var);
2755
Douglas Gregorf15748a2011-06-03 03:35:07 +00002756 // If we already have a definition, we're done.
Nick Lewycky95e38722012-04-04 02:38:36 +00002757 if (VarDecl *Def = Var->getDefinition()) {
2758 // We may be explicitly instantiating something we've already implicitly
2759 // instantiated.
2760 Def->setTemplateSpecializationKind(Var->getTemplateSpecializationKind(),
2761 PointOfInstantiation);
Douglas Gregorf15748a2011-06-03 03:35:07 +00002762 return;
Nick Lewycky95e38722012-04-04 02:38:36 +00002763 }
Douglas Gregorf15748a2011-06-03 03:35:07 +00002764
Douglas Gregor7caa6822009-07-24 20:34:43 +00002765 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
2766 if (Inst)
2767 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002768
Douglas Gregor7caa6822009-07-24 20:34:43 +00002769 // If we're performing recursive template instantiation, create our own
2770 // queue of pending implicit instantiations that we will instantiate later,
2771 // while we're still within our own instantiation context.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002772 SmallVector<VTableUse, 16> SavedVTableUses;
Chandler Carruth62c78d52010-08-25 08:44:16 +00002773 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Nick Lewycky81559102011-05-31 07:58:42 +00002774 if (Recursive) {
2775 VTableUses.swap(SavedVTableUses);
Chandler Carruth62c78d52010-08-25 08:44:16 +00002776 PendingInstantiations.swap(SavedPendingInstantiations);
Nick Lewycky81559102011-05-31 07:58:42 +00002777 }
Mike Stump1eb44332009-09-09 15:08:12 +00002778
Douglas Gregor7caa6822009-07-24 20:34:43 +00002779 // Enter the scope of this instantiation. We don't use
2780 // PushDeclContext because we don't have a scope.
John McCallf5ba7e02011-02-14 20:37:25 +00002781 ContextRAII previousContext(*this, Var->getDeclContext());
Douglas Gregor7bdc1522012-02-16 21:36:18 +00002782 LocalInstantiationScope Local(*this);
2783
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002784 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00002785 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Nico Weber6bb4dcb2010-11-28 22:53:37 +00002786 getTemplateInstantiationArgs(Var)));
John McCallf5ba7e02011-02-14 20:37:25 +00002787
2788 previousContext.pop();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002789
2790 if (Var) {
Douglas Gregor583f33b2009-10-15 18:07:02 +00002791 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2792 assert(MSInfo && "Missing member specialization information?");
2793 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2794 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00002795 DeclGroupRef DG(Var);
2796 Consumer.HandleTopLevelDecl(DG);
2797 }
Douglas Gregor7bdc1522012-02-16 21:36:18 +00002798 Local.Exit();
2799
Douglas Gregor7caa6822009-07-24 20:34:43 +00002800 if (Recursive) {
Nick Lewycky81559102011-05-31 07:58:42 +00002801 // Define any newly required vtables.
2802 DefineUsedVTables();
2803
Douglas Gregor7caa6822009-07-24 20:34:43 +00002804 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002805 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002806 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002807
Nick Lewycky81559102011-05-31 07:58:42 +00002808 // Restore the set of pending vtables.
2809 assert(VTableUses.empty() &&
2810 "VTableUses should be empty before it is discarded, "
2811 "while instantiating static data member.");
2812 VTableUses.swap(SavedVTableUses);
2813
Douglas Gregor7caa6822009-07-24 20:34:43 +00002814 // Restore the set of pending implicit instantiations.
Nick Lewycky81559102011-05-31 07:58:42 +00002815 assert(PendingInstantiations.empty() &&
2816 "PendingInstantiations should be empty before it is discarded, "
2817 "while instantiating static data member.");
Chandler Carruth62c78d52010-08-25 08:44:16 +00002818 PendingInstantiations.swap(SavedPendingInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002819 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002820}
Douglas Gregor815215d2009-05-27 05:35:12 +00002821
Anders Carlsson09025312009-08-29 05:16:22 +00002822void
2823Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
2824 const CXXConstructorDecl *Tmpl,
2825 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00002826
Richard Trieu90ab75b2011-09-09 03:18:59 +00002827 SmallVector<CXXCtorInitializer*, 4> NewInits;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002828 bool AnyErrors = false;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002829
Anders Carlsson09025312009-08-29 05:16:22 +00002830 // Instantiate all the initializers.
2831 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00002832 InitsEnd = Tmpl->init_end();
2833 Inits != InitsEnd; ++Inits) {
Sean Huntcbb67482011-01-08 20:30:50 +00002834 CXXCtorInitializer *Init = *Inits;
Anders Carlsson09025312009-08-29 05:16:22 +00002835
Chandler Carruth030ef472010-09-03 21:54:20 +00002836 // Only instantiate written initializers, let Sema re-construct implicit
2837 // ones.
2838 if (!Init->isWritten())
2839 continue;
2840
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002841 SourceLocation EllipsisLoc;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002842
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002843 if (Init->isPackExpansion()) {
2844 // This is a pack expansion. We should expand it now.
Douglas Gregor76852c22011-11-01 01:16:03 +00002845 TypeLoc BaseTL = Init->getTypeSourceInfo()->getTypeLoc();
Chris Lattner5f9e2722011-07-23 10:55:15 +00002846 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002847 collectUnexpandedParameterPacks(BaseTL, Unexpanded);
2848 bool ShouldExpand = false;
Douglas Gregord3731192011-01-10 07:32:04 +00002849 bool RetainExpansion = false;
Douglas Gregorcded4f62011-01-14 17:04:44 +00002850 llvm::Optional<unsigned> NumExpansions;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002851 if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(),
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002852 BaseTL.getSourceRange(),
David Blaikiea71f9d02011-09-22 02:34:54 +00002853 Unexpanded,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002854 TemplateArgs, ShouldExpand,
Douglas Gregord3731192011-01-10 07:32:04 +00002855 RetainExpansion,
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002856 NumExpansions)) {
2857 AnyErrors = true;
2858 New->setInvalidDecl();
2859 continue;
2860 }
2861 assert(ShouldExpand && "Partial instantiation of base initializer?");
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002862
2863 // Loop over all of the arguments in the argument pack(s),
Douglas Gregorcded4f62011-01-14 17:04:44 +00002864 for (unsigned I = 0; I != *NumExpansions; ++I) {
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002865 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
2866
2867 // Instantiate the initializer.
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00002868 ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs,
2869 /*CXXDirectInit=*/true);
2870 if (TempInit.isInvalid()) {
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002871 AnyErrors = true;
2872 break;
2873 }
2874
2875 // Instantiate the base type.
Douglas Gregor76852c22011-11-01 01:16:03 +00002876 TypeSourceInfo *BaseTInfo = SubstType(Init->getTypeSourceInfo(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002877 TemplateArgs,
2878 Init->getSourceLocation(),
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002879 New->getDeclName());
2880 if (!BaseTInfo) {
2881 AnyErrors = true;
2882 break;
2883 }
2884
2885 // Build the initializer.
Sebastian Redl6df65482011-09-24 17:48:25 +00002886 MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(),
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00002887 BaseTInfo, TempInit.take(),
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002888 New->getParent(),
2889 SourceLocation());
2890 if (NewInit.isInvalid()) {
2891 AnyErrors = true;
2892 break;
2893 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002894
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002895 NewInits.push_back(NewInit.get());
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002896 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002897
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002898 continue;
2899 }
2900
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002901 // Instantiate the initializer.
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00002902 ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs,
2903 /*CXXDirectInit=*/true);
2904 if (TempInit.isInvalid()) {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002905 AnyErrors = true;
2906 continue;
Anders Carlsson09025312009-08-29 05:16:22 +00002907 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002908
Anders Carlsson09025312009-08-29 05:16:22 +00002909 MemInitResult NewInit;
Douglas Gregor76852c22011-11-01 01:16:03 +00002910 if (Init->isDelegatingInitializer() || Init->isBaseInitializer()) {
2911 TypeSourceInfo *TInfo = SubstType(Init->getTypeSourceInfo(),
2912 TemplateArgs,
2913 Init->getSourceLocation(),
2914 New->getDeclName());
2915 if (!TInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002916 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00002917 New->setInvalidDecl();
2918 continue;
2919 }
Sebastian Redl6df65482011-09-24 17:48:25 +00002920
Douglas Gregor76852c22011-11-01 01:16:03 +00002921 if (Init->isBaseInitializer())
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00002922 NewInit = BuildBaseInitializer(TInfo->getType(), TInfo, TempInit.take(),
Douglas Gregor76852c22011-11-01 01:16:03 +00002923 New->getParent(), EllipsisLoc);
2924 else
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00002925 NewInit = BuildDelegatingInitializer(TInfo, TempInit.take(),
Douglas Gregor76852c22011-11-01 01:16:03 +00002926 cast<CXXRecordDecl>(CurContext->getParent()));
Anders Carlsson09025312009-08-29 05:16:22 +00002927 } else if (Init->isMemberInitializer()) {
Douglas Gregorb7107222011-03-04 19:46:35 +00002928 FieldDecl *Member = cast_or_null<FieldDecl>(FindInstantiatedDecl(
Francois Pichet00eb3f92010-12-04 09:14:42 +00002929 Init->getMemberLocation(),
2930 Init->getMember(),
2931 TemplateArgs));
Douglas Gregorb7107222011-03-04 19:46:35 +00002932 if (!Member) {
2933 AnyErrors = true;
2934 New->setInvalidDecl();
2935 continue;
2936 }
Mike Stump1eb44332009-09-09 15:08:12 +00002937
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00002938 NewInit = BuildMemberInitializer(Member, TempInit.take(),
Sebastian Redl6df65482011-09-24 17:48:25 +00002939 Init->getSourceLocation());
Francois Pichet00eb3f92010-12-04 09:14:42 +00002940 } else if (Init->isIndirectMemberInitializer()) {
2941 IndirectFieldDecl *IndirectMember =
Douglas Gregorb7107222011-03-04 19:46:35 +00002942 cast_or_null<IndirectFieldDecl>(FindInstantiatedDecl(
Francois Pichet00eb3f92010-12-04 09:14:42 +00002943 Init->getMemberLocation(),
2944 Init->getIndirectMember(), TemplateArgs));
2945
Douglas Gregorb7107222011-03-04 19:46:35 +00002946 if (!IndirectMember) {
2947 AnyErrors = true;
2948 New->setInvalidDecl();
Sebastian Redl6df65482011-09-24 17:48:25 +00002949 continue;
Douglas Gregorb7107222011-03-04 19:46:35 +00002950 }
Sebastian Redl6df65482011-09-24 17:48:25 +00002951
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00002952 NewInit = BuildMemberInitializer(IndirectMember, TempInit.take(),
Sebastian Redl6df65482011-09-24 17:48:25 +00002953 Init->getSourceLocation());
Anders Carlsson09025312009-08-29 05:16:22 +00002954 }
2955
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002956 if (NewInit.isInvalid()) {
2957 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00002958 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002959 } else {
Richard Trieu90ab75b2011-09-09 03:18:59 +00002960 NewInits.push_back(NewInit.get());
Anders Carlsson09025312009-08-29 05:16:22 +00002961 }
2962 }
Mike Stump1eb44332009-09-09 15:08:12 +00002963
Anders Carlsson09025312009-08-29 05:16:22 +00002964 // Assign all the initializers to the new constructor.
John McCalld226f652010-08-21 09:40:31 +00002965 ActOnMemInitializers(New,
Anders Carlsson09025312009-08-29 05:16:22 +00002966 /*FIXME: ColonLoc */
2967 SourceLocation(),
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002968 NewInits.data(), NewInits.size(),
2969 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00002970}
2971
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00002972ExprResult Sema::SubstInitializer(Expr *Init,
2973 const MultiLevelTemplateArgumentList &TemplateArgs,
2974 bool CXXDirectInit) {
2975 // Initializers are instantiated like expressions, except that various outer
2976 // layers are stripped.
2977 if (!Init)
2978 return Owned(Init);
2979
2980 if (ExprWithCleanups *ExprTemp = dyn_cast<ExprWithCleanups>(Init))
2981 Init = ExprTemp->getSubExpr();
2982
2983 while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init))
2984 Init = Binder->getSubExpr();
2985
2986 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init))
2987 Init = ICE->getSubExprAsWritten();
2988
2989 // If this is a direct-initializer, we take apart CXXConstructExprs.
2990 // Everything else is passed through.
2991 CXXConstructExpr *Construct;
2992 if (!CXXDirectInit || !(Construct = dyn_cast<CXXConstructExpr>(Init)) ||
2993 isa<CXXTemporaryObjectExpr>(Construct))
2994 return SubstExpr(Init, TemplateArgs);
2995
2996 ASTOwningVector<Expr*> NewArgs(*this);
2997 if (SubstExprs(Construct->getArgs(), Construct->getNumArgs(), true,
2998 TemplateArgs, NewArgs))
2999 return ExprError();
3000
3001 // Treat an empty initializer like none.
3002 if (NewArgs.empty())
3003 return Owned((Expr*)0);
3004
3005 // Build a ParenListExpr to represent anything else.
3006 // FIXME: Fake locations!
3007 SourceLocation Loc = PP.getLocForEndOfToken(Init->getLocStart());
3008 return ActOnParenListExpr(Loc, Loc, move_arg(NewArgs));
3009}
3010
John McCall52a575a2009-08-29 08:11:13 +00003011// TODO: this could be templated if the various decl types used the
3012// same method name.
3013static bool isInstantiationOf(ClassTemplateDecl *Pattern,
3014 ClassTemplateDecl *Instance) {
3015 Pattern = Pattern->getCanonicalDecl();
3016
3017 do {
3018 Instance = Instance->getCanonicalDecl();
3019 if (Pattern == Instance) return true;
3020 Instance = Instance->getInstantiatedFromMemberTemplate();
3021 } while (Instance);
3022
3023 return false;
3024}
3025
Douglas Gregor0d696532009-09-28 06:34:35 +00003026static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
3027 FunctionTemplateDecl *Instance) {
3028 Pattern = Pattern->getCanonicalDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003029
Douglas Gregor0d696532009-09-28 06:34:35 +00003030 do {
3031 Instance = Instance->getCanonicalDecl();
3032 if (Pattern == Instance) return true;
3033 Instance = Instance->getInstantiatedFromMemberTemplate();
3034 } while (Instance);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003035
Douglas Gregor0d696532009-09-28 06:34:35 +00003036 return false;
3037}
3038
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003039static bool
Douglas Gregored9c0f92009-10-29 00:04:11 +00003040isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
3041 ClassTemplatePartialSpecializationDecl *Instance) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003042 Pattern
Douglas Gregored9c0f92009-10-29 00:04:11 +00003043 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
3044 do {
3045 Instance = cast<ClassTemplatePartialSpecializationDecl>(
3046 Instance->getCanonicalDecl());
3047 if (Pattern == Instance)
3048 return true;
3049 Instance = Instance->getInstantiatedFromMember();
3050 } while (Instance);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003051
Douglas Gregored9c0f92009-10-29 00:04:11 +00003052 return false;
3053}
3054
John McCall52a575a2009-08-29 08:11:13 +00003055static bool isInstantiationOf(CXXRecordDecl *Pattern,
3056 CXXRecordDecl *Instance) {
3057 Pattern = Pattern->getCanonicalDecl();
3058
3059 do {
3060 Instance = Instance->getCanonicalDecl();
3061 if (Pattern == Instance) return true;
3062 Instance = Instance->getInstantiatedFromMemberClass();
3063 } while (Instance);
3064
3065 return false;
3066}
3067
3068static bool isInstantiationOf(FunctionDecl *Pattern,
3069 FunctionDecl *Instance) {
3070 Pattern = Pattern->getCanonicalDecl();
3071
3072 do {
3073 Instance = Instance->getCanonicalDecl();
3074 if (Pattern == Instance) return true;
3075 Instance = Instance->getInstantiatedFromMemberFunction();
3076 } while (Instance);
3077
3078 return false;
3079}
3080
3081static bool isInstantiationOf(EnumDecl *Pattern,
3082 EnumDecl *Instance) {
3083 Pattern = Pattern->getCanonicalDecl();
3084
3085 do {
3086 Instance = Instance->getCanonicalDecl();
3087 if (Pattern == Instance) return true;
3088 Instance = Instance->getInstantiatedFromMemberEnum();
3089 } while (Instance);
3090
3091 return false;
3092}
3093
John McCalled976492009-12-04 22:46:56 +00003094static bool isInstantiationOf(UsingShadowDecl *Pattern,
3095 UsingShadowDecl *Instance,
3096 ASTContext &C) {
3097 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
3098}
3099
3100static bool isInstantiationOf(UsingDecl *Pattern,
3101 UsingDecl *Instance,
3102 ASTContext &C) {
3103 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
3104}
3105
John McCall7ba107a2009-11-18 02:36:19 +00003106static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
3107 UsingDecl *Instance,
3108 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00003109 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00003110}
3111
3112static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00003113 UsingDecl *Instance,
3114 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00003115 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00003116}
3117
John McCall52a575a2009-08-29 08:11:13 +00003118static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
3119 VarDecl *Instance) {
3120 assert(Instance->isStaticDataMember());
3121
3122 Pattern = Pattern->getCanonicalDecl();
3123
3124 do {
3125 Instance = Instance->getCanonicalDecl();
3126 if (Pattern == Instance) return true;
3127 Instance = Instance->getInstantiatedFromStaticDataMember();
3128 } while (Instance);
3129
3130 return false;
3131}
3132
John McCalled976492009-12-04 22:46:56 +00003133// Other is the prospective instantiation
3134// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00003135static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00003136 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00003137 if (UnresolvedUsingTypenameDecl *UUD
3138 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
3139 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
3140 return isInstantiationOf(UUD, UD, Ctx);
3141 }
3142 }
3143
3144 if (UnresolvedUsingValueDecl *UUD
3145 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00003146 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
3147 return isInstantiationOf(UUD, UD, Ctx);
3148 }
3149 }
Douglas Gregor815215d2009-05-27 05:35:12 +00003150
Anders Carlsson0d8df782009-08-29 19:37:28 +00003151 return false;
3152 }
Mike Stump1eb44332009-09-09 15:08:12 +00003153
John McCall52a575a2009-08-29 08:11:13 +00003154 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
3155 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00003156
John McCall52a575a2009-08-29 08:11:13 +00003157 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
3158 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00003159
John McCall52a575a2009-08-29 08:11:13 +00003160 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
3161 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00003162
Douglas Gregor7caa6822009-07-24 20:34:43 +00003163 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00003164 if (Var->isStaticDataMember())
3165 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
3166
3167 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
3168 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00003169
Douglas Gregor0d696532009-09-28 06:34:35 +00003170 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
3171 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
3172
Douglas Gregored9c0f92009-10-29 00:04:11 +00003173 if (ClassTemplatePartialSpecializationDecl *PartialSpec
3174 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
3175 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
3176 PartialSpec);
3177
Anders Carlssond8b285f2009-09-01 04:26:58 +00003178 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
3179 if (!Field->getDeclName()) {
3180 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00003181 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00003182 cast<FieldDecl>(D);
3183 }
3184 }
Mike Stump1eb44332009-09-09 15:08:12 +00003185
John McCalled976492009-12-04 22:46:56 +00003186 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
3187 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
3188
3189 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
3190 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
3191
Douglas Gregor815215d2009-05-27 05:35:12 +00003192 return D->getDeclName() && isa<NamedDecl>(Other) &&
3193 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
3194}
3195
3196template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00003197static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00003198 NamedDecl *D,
3199 ForwardIterator first,
3200 ForwardIterator last) {
3201 for (; first != last; ++first)
3202 if (isInstantiationOf(Ctx, D, *first))
3203 return cast<NamedDecl>(*first);
3204
3205 return 0;
3206}
3207
John McCall02cace72009-08-28 07:59:38 +00003208/// \brief Finds the instantiation of the given declaration context
3209/// within the current instantiation.
3210///
3211/// \returns NULL if there was an error
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003212DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
Douglas Gregore95b4092009-09-16 18:34:49 +00003213 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00003214 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003215 Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00003216 return cast_or_null<DeclContext>(ID);
3217 } else return DC;
3218}
3219
Douglas Gregored961e72009-05-27 17:54:46 +00003220/// \brief Find the instantiation of the given declaration within the
3221/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00003222///
3223/// This routine is intended to be used when \p D is a declaration
3224/// referenced from within a template, that needs to mapped into the
3225/// corresponding declaration within an instantiation. For example,
3226/// given:
3227///
3228/// \code
3229/// template<typename T>
3230/// struct X {
3231/// enum Kind {
3232/// KnownValue = sizeof(T)
3233/// };
3234///
3235/// bool getKind() const { return KnownValue; }
3236/// };
3237///
3238/// template struct X<int>;
3239/// \endcode
3240///
3241/// In the instantiation of X<int>::getKind(), we need to map the
3242/// EnumConstantDecl for KnownValue (which refers to
3243/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00003244/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
3245/// this mapping from within the instantiation of X<int>.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003246NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
Douglas Gregore95b4092009-09-16 18:34:49 +00003247 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00003248 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00003249 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00003250 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
Douglas Gregor7bdc1522012-02-16 21:36:18 +00003251 (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext()) ||
3252 (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda())) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00003253 // D is a local of some kind. Look into the map of local
3254 // declarations to their instantiations.
Chris Lattnerd8e54992011-02-17 19:47:42 +00003255 typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
3256 llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found
3257 = CurrentInstantiationScope->findInstantiationOf(D);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003258
Chris Lattner57ad3782011-02-17 20:34:02 +00003259 if (Found) {
3260 if (Decl *FD = Found->dyn_cast<Decl *>())
3261 return cast<NamedDecl>(FD);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003262
Chris Lattner57ad3782011-02-17 20:34:02 +00003263 unsigned PackIdx = ArgumentPackSubstitutionIndex;
3264 return cast<NamedDecl>((*Found->get<DeclArgumentPack *>())[PackIdx]);
3265 }
3266
3267 // If we didn't find the decl, then we must have a label decl that hasn't
3268 // been found yet. Lazily instantiate it and return it now.
3269 assert(isa<LabelDecl>(D));
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003270
Chris Lattner57ad3782011-02-17 20:34:02 +00003271 Decl *Inst = SubstDecl(D, CurContext, TemplateArgs);
3272 assert(Inst && "Failed to instantiate label??");
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003273
Chris Lattner57ad3782011-02-17 20:34:02 +00003274 CurrentInstantiationScope->InstantiatedLocal(D, Inst);
3275 return cast<LabelDecl>(Inst);
Douglas Gregor2bba76b2009-05-27 17:07:49 +00003276 }
Douglas Gregor815215d2009-05-27 05:35:12 +00003277
Douglas Gregore95b4092009-09-16 18:34:49 +00003278 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
3279 if (!Record->isDependentContext())
3280 return D;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003281
Douglas Gregor2c1227c2011-11-07 17:43:18 +00003282 // Determine whether this record is the "templated" declaration describing
3283 // a class template or class template partial specialization.
Douglas Gregore95b4092009-09-16 18:34:49 +00003284 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
Douglas Gregor2c1227c2011-11-07 17:43:18 +00003285 if (ClassTemplate)
3286 ClassTemplate = ClassTemplate->getCanonicalDecl();
3287 else if (ClassTemplatePartialSpecializationDecl *PartialSpec
3288 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record))
3289 ClassTemplate = PartialSpec->getSpecializedTemplate()->getCanonicalDecl();
3290
3291 // Walk the current context to find either the record or an instantiation of
3292 // it.
3293 DeclContext *DC = CurContext;
3294 while (!DC->isFileContext()) {
3295 // If we're performing substitution while we're inside the template
3296 // definition, we'll find our own context. We're done.
3297 if (DC->Equals(Record))
3298 return Record;
3299
3300 if (CXXRecordDecl *InstRecord = dyn_cast<CXXRecordDecl>(DC)) {
3301 // Check whether we're in the process of instantiating a class template
3302 // specialization of the template we're mapping.
3303 if (ClassTemplateSpecializationDecl *InstSpec
3304 = dyn_cast<ClassTemplateSpecializationDecl>(InstRecord)){
3305 ClassTemplateDecl *SpecTemplate = InstSpec->getSpecializedTemplate();
3306 if (ClassTemplate && isInstantiationOf(ClassTemplate, SpecTemplate))
3307 return InstRecord;
3308 }
3309
3310 // Check whether we're in the process of instantiating a member class.
3311 if (isInstantiationOf(Record, InstRecord))
3312 return InstRecord;
Douglas Gregore95b4092009-09-16 18:34:49 +00003313 }
Douglas Gregor2c1227c2011-11-07 17:43:18 +00003314
3315
3316 // Move to the outer template scope.
3317 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) {
3318 if (FD->getFriendObjectKind() && FD->getDeclContext()->isFileContext()){
3319 DC = FD->getLexicalDeclContext();
3320 continue;
3321 }
John McCall52a575a2009-08-29 08:11:13 +00003322 }
Douglas Gregor2c1227c2011-11-07 17:43:18 +00003323
3324 DC = DC->getParent();
John McCall52a575a2009-08-29 08:11:13 +00003325 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00003326
Douglas Gregore95b4092009-09-16 18:34:49 +00003327 // Fall through to deal with other dependent record types (e.g.,
3328 // anonymous unions in class templates).
3329 }
John McCall52a575a2009-08-29 08:11:13 +00003330
Douglas Gregore95b4092009-09-16 18:34:49 +00003331 if (!ParentDC->isDependentContext())
3332 return D;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003333
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003334 ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00003335 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00003336 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003337
Douglas Gregor815215d2009-05-27 05:35:12 +00003338 if (ParentDC != D->getDeclContext()) {
3339 // We performed some kind of instantiation in the parent context,
3340 // so now we need to look into the instantiated parent context to
3341 // find the instantiation of the declaration D.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003342
John McCall3cb0ebd2010-03-10 03:28:59 +00003343 // If our context used to be dependent, we may need to instantiate
3344 // it before performing lookup into that context.
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00003345 bool IsBeingInstantiated = false;
John McCall3cb0ebd2010-03-10 03:28:59 +00003346 if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003347 if (!Spec->isDependentContext()) {
3348 QualType T = Context.getTypeDeclType(Spec);
John McCall3cb0ebd2010-03-10 03:28:59 +00003349 const RecordType *Tag = T->getAs<RecordType>();
3350 assert(Tag && "type of non-dependent record is not a RecordType");
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00003351 if (Tag->isBeingDefined())
3352 IsBeingInstantiated = true;
John McCall3cb0ebd2010-03-10 03:28:59 +00003353 if (!Tag->isBeingDefined() &&
3354 RequireCompleteType(Loc, T, diag::err_incomplete_type))
3355 return 0;
Douglas Gregora43064c2010-11-05 23:22:45 +00003356
3357 ParentDC = Tag->getDecl();
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003358 }
3359 }
3360
Douglas Gregor815215d2009-05-27 05:35:12 +00003361 NamedDecl *Result = 0;
3362 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003363 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00003364 Result = findInstantiationOf(Context, D, Found.first, Found.second);
3365 } else {
3366 // Since we don't have a name for the entity we're looking for,
3367 // our only option is to walk through all of the declarations to
3368 // find that name. This will occur in a few cases:
3369 //
3370 // - anonymous struct/union within a template
3371 // - unnamed class/struct/union/enum within a template
3372 //
3373 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00003374 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003375 ParentDC->decls_begin(),
3376 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00003377 }
Mike Stump1eb44332009-09-09 15:08:12 +00003378
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00003379 if (!Result) {
3380 if (isa<UsingShadowDecl>(D)) {
3381 // UsingShadowDecls can instantiate to nothing because of using hiding.
3382 } else if (Diags.hasErrorOccurred()) {
3383 // We've already complained about something, so most likely this
3384 // declaration failed to instantiate. There's no point in complaining
3385 // further, since this is normal in invalid code.
3386 } else if (IsBeingInstantiated) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003387 // The class in which this member exists is currently being
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00003388 // instantiated, and we haven't gotten around to instantiating this
3389 // member yet. This can happen when the code uses forward declarations
3390 // of member classes, and introduces ordering dependencies via
3391 // template instantiation.
3392 Diag(Loc, diag::err_member_not_yet_instantiated)
3393 << D->getDeclName()
3394 << Context.getTypeDeclType(cast<CXXRecordDecl>(ParentDC));
3395 Diag(D->getLocation(), diag::note_non_instantiated_member_here);
Richard Smith0724b7c2012-03-26 20:28:16 +00003396 } else if (EnumConstantDecl *ED = dyn_cast<EnumConstantDecl>(D)) {
3397 // This enumeration constant was found when the template was defined,
3398 // but can't be found in the instantiation. This can happen if an
3399 // unscoped enumeration member is explicitly specialized.
3400 EnumDecl *Enum = cast<EnumDecl>(ED->getLexicalDeclContext());
3401 EnumDecl *Spec = cast<EnumDecl>(FindInstantiatedDecl(Loc, Enum,
3402 TemplateArgs));
3403 assert(Spec->getTemplateSpecializationKind() ==
3404 TSK_ExplicitSpecialization);
3405 Diag(Loc, diag::err_enumerator_does_not_exist)
3406 << D->getDeclName()
3407 << Context.getTypeDeclType(cast<TypeDecl>(Spec->getDeclContext()));
3408 Diag(Spec->getLocation(), diag::note_enum_specialized_here)
3409 << Context.getTypeDeclType(Spec);
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00003410 } else {
3411 // We should have found something, but didn't.
3412 llvm_unreachable("Unable to find instantiation of declaration!");
3413 }
3414 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003415
Douglas Gregor815215d2009-05-27 05:35:12 +00003416 D = Result;
3417 }
3418
Douglas Gregor815215d2009-05-27 05:35:12 +00003419 return D;
3420}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003421
Mike Stump1eb44332009-09-09 15:08:12 +00003422/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003423/// instantiations we have seen until this point.
Nick Lewycky81559102011-05-31 07:58:42 +00003424void Sema::PerformPendingInstantiations(bool LocalOnly) {
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00003425 // Load pending instantiations from the external source.
3426 if (!LocalOnly && ExternalSource) {
3427 SmallVector<std::pair<ValueDecl *, SourceLocation>, 4> Pending;
3428 ExternalSource->ReadPendingInstantiations(Pending);
3429 PendingInstantiations.insert(PendingInstantiations.begin(),
3430 Pending.begin(), Pending.end());
3431 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003432
Douglas Gregor60406be2010-01-16 22:29:39 +00003433 while (!PendingLocalImplicitInstantiations.empty() ||
Chandler Carruth62c78d52010-08-25 08:44:16 +00003434 (!LocalOnly && !PendingInstantiations.empty())) {
Douglas Gregor60406be2010-01-16 22:29:39 +00003435 PendingImplicitInstantiation Inst;
3436
3437 if (PendingLocalImplicitInstantiations.empty()) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00003438 Inst = PendingInstantiations.front();
3439 PendingInstantiations.pop_front();
Douglas Gregor60406be2010-01-16 22:29:39 +00003440 } else {
3441 Inst = PendingLocalImplicitInstantiations.front();
3442 PendingLocalImplicitInstantiations.pop_front();
3443 }
Mike Stump1eb44332009-09-09 15:08:12 +00003444
Douglas Gregor7caa6822009-07-24 20:34:43 +00003445 // Instantiate function definitions
3446 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
John McCallf312b1e2010-08-26 23:41:50 +00003447 PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(),
3448 "instantiating function definition");
Chandler Carruth58e390e2010-08-25 08:27:02 +00003449 bool DefinitionRequired = Function->getTemplateSpecializationKind() ==
3450 TSK_ExplicitInstantiationDefinition;
3451 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true,
3452 DefinitionRequired);
Douglas Gregor7caa6822009-07-24 20:34:43 +00003453 continue;
3454 }
Mike Stump1eb44332009-09-09 15:08:12 +00003455
Douglas Gregor7caa6822009-07-24 20:34:43 +00003456 // Instantiate static data member definitions.
3457 VarDecl *Var = cast<VarDecl>(Inst.first);
3458 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00003459
Chandler Carruth291b4412010-02-13 10:17:50 +00003460 // Don't try to instantiate declarations if the most recent redeclaration
3461 // is invalid.
Douglas Gregoref96ee02012-01-14 16:38:05 +00003462 if (Var->getMostRecentDecl()->isInvalidDecl())
Chandler Carruth291b4412010-02-13 10:17:50 +00003463 continue;
3464
3465 // Check if the most recent declaration has changed the specialization kind
3466 // and removed the need for implicit instantiation.
Douglas Gregoref96ee02012-01-14 16:38:05 +00003467 switch (Var->getMostRecentDecl()->getTemplateSpecializationKind()) {
Chandler Carruth291b4412010-02-13 10:17:50 +00003468 case TSK_Undeclared:
David Blaikieb219cfc2011-09-23 05:06:16 +00003469 llvm_unreachable("Cannot instantitiate an undeclared specialization.");
Chandler Carruth291b4412010-02-13 10:17:50 +00003470 case TSK_ExplicitInstantiationDeclaration:
Chandler Carruth291b4412010-02-13 10:17:50 +00003471 case TSK_ExplicitSpecialization:
Chandler Carruth58e390e2010-08-25 08:27:02 +00003472 continue; // No longer need to instantiate this type.
3473 case TSK_ExplicitInstantiationDefinition:
3474 // We only need an instantiation if the pending instantiation *is* the
3475 // explicit instantiation.
Douglas Gregoref96ee02012-01-14 16:38:05 +00003476 if (Var != Var->getMostRecentDecl()) continue;
Chandler Carruth291b4412010-02-13 10:17:50 +00003477 case TSK_ImplicitInstantiation:
3478 break;
3479 }
3480
John McCallf312b1e2010-08-26 23:41:50 +00003481 PrettyDeclStackTraceEntry CrashInfo(*this, Var, Var->getLocation(),
3482 "instantiating static data member "
3483 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00003484
Chandler Carruth58e390e2010-08-25 08:27:02 +00003485 bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
3486 TSK_ExplicitInstantiationDefinition;
3487 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true,
3488 DefinitionRequired);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003489 }
3490}
John McCall0c01d182010-03-24 05:22:00 +00003491
3492void Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
3493 const MultiLevelTemplateArgumentList &TemplateArgs) {
3494 for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
3495 E = Pattern->ddiag_end(); I != E; ++I) {
3496 DependentDiagnostic *DD = *I;
3497
3498 switch (DD->getKind()) {
3499 case DependentDiagnostic::Access:
3500 HandleDependentAccessCheck(*DD, TemplateArgs);
3501 break;
3502 }
3503 }
3504}