blob: b971a04d61a4df53310af83e462474f48989b36b [file] [log] [blame]
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001//===--- SemaTemplateInstantiateDecl.cpp - C++ Template Decl Instantiation ===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//
9// This file implements C++ template instantiation for declarations.
10//
11//===----------------------------------------------------------------------===/
John McCall2d887082010-08-25 22:03:47 +000012#include "clang/Sema/SemaInternal.h"
Douglas Gregoraba43bb2009-05-26 20:50:29 +000013#include "clang/AST/ASTConsumer.h"
Douglas Gregor8dbc2692009-03-17 21:15:40 +000014#include "clang/AST/ASTContext.h"
15#include "clang/AST/DeclTemplate.h"
16#include "clang/AST/DeclVisitor.h"
John McCall0c01d182010-03-24 05:22:00 +000017#include "clang/AST/DependentDiagnostic.h"
Douglas Gregor8dbc2692009-03-17 21:15:40 +000018#include "clang/AST/Expr.h"
Douglas Gregora88cfbf2009-12-12 18:16:41 +000019#include "clang/AST/ExprCXX.h"
John McCall21ef0fa2010-03-11 09:03:00 +000020#include "clang/AST/TypeLoc.h"
Douglas Gregor83ddad32009-08-26 21:14:46 +000021#include "clang/Lex/Preprocessor.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000022#include "clang/Sema/Lookup.h"
23#include "clang/Sema/PrettyDeclStackTrace.h"
24#include "clang/Sema/Template.h"
Douglas Gregor8dbc2692009-03-17 21:15:40 +000025
26using namespace clang;
27
John McCallb6217662010-03-15 10:12:16 +000028bool TemplateDeclInstantiator::SubstQualifier(const DeclaratorDecl *OldDecl,
29 DeclaratorDecl *NewDecl) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +000030 if (!OldDecl->getQualifierLoc())
31 return false;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +000032
Douglas Gregorc22b5ff2011-02-25 02:25:35 +000033 NestedNameSpecifierLoc NewQualifierLoc
NAKAMURA Takumia789ca92011-10-08 11:31:46 +000034 = SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(),
Douglas Gregorc22b5ff2011-02-25 02:25:35 +000035 TemplateArgs);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +000036
Douglas Gregorc22b5ff2011-02-25 02:25:35 +000037 if (!NewQualifierLoc)
John McCallb6217662010-03-15 10:12:16 +000038 return true;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +000039
Douglas Gregorc22b5ff2011-02-25 02:25:35 +000040 NewDecl->setQualifierInfo(NewQualifierLoc);
John McCallb6217662010-03-15 10:12:16 +000041 return false;
42}
43
44bool TemplateDeclInstantiator::SubstQualifier(const TagDecl *OldDecl,
45 TagDecl *NewDecl) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +000046 if (!OldDecl->getQualifierLoc())
47 return false;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +000048
Douglas Gregorc22b5ff2011-02-25 02:25:35 +000049 NestedNameSpecifierLoc NewQualifierLoc
NAKAMURA Takumia789ca92011-10-08 11:31:46 +000050 = SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(),
Douglas Gregorc22b5ff2011-02-25 02:25:35 +000051 TemplateArgs);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +000052
Douglas Gregorc22b5ff2011-02-25 02:25:35 +000053 if (!NewQualifierLoc)
John McCallb6217662010-03-15 10:12:16 +000054 return true;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +000055
Douglas Gregorc22b5ff2011-02-25 02:25:35 +000056 NewDecl->setQualifierInfo(NewQualifierLoc);
John McCallb6217662010-03-15 10:12:16 +000057 return false;
58}
59
DeLesley Hutchins7b9ff0c2012-01-20 22:37:06 +000060// Include attribute instantiation code.
61#include "clang/Sema/AttrTemplateInstantiate.inc"
62
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())
Michael Hana31f65b2013-02-01 01:19:17 +000082 AddAlignedAttr(Aligned->getLocation(), New, Result.takeAs<Expr>(),
Richard Smith8f3aacc2013-01-29 04:21:28 +000083 Aligned->getSpellingListIndex());
Richard Smithf6702a32011-12-20 02:08:33 +000084 } else {
Sean Huntcf807c42010-08-18 23:23:40 +000085 TypeSourceInfo *Result = SubstType(Aligned->getAlignmentType(),
Nick Lewycky7663f392010-11-20 01:29:55 +000086 TemplateArgs,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +000087 Aligned->getLocation(),
Nick Lewycky7663f392010-11-20 01:29:55 +000088 DeclarationName());
Sean Huntcf807c42010-08-18 23:23:40 +000089 if (Result)
Richard Smith8f3aacc2013-01-29 04:21:28 +000090 AddAlignedAttr(Aligned->getLocation(), New, Result,
Richard Smith8f3aacc2013-01-29 04:21:28 +000091 Aligned->getSpellingListIndex());
Sean Huntcf807c42010-08-18 23:23:40 +000092 }
Chandler Carruth4ced79f2010-06-25 03:22:07 +000093 continue;
94 }
95 }
96
DeLesley Hutchins23323e02012-01-20 22:50:54 +000097 if (TmplAttr->isLateParsed() && LateAttrs) {
98 // Late parsed attributes must be instantiated and attached after the
99 // enclosing class has been instantiated. See Sema::InstantiateClass.
100 LocalInstantiationScope *Saved = 0;
101 if (CurrentInstantiationScope)
102 Saved = CurrentInstantiationScope->cloneScopes(OuterMostScope);
103 LateAttrs->push_back(LateInstantiatedAttribute(TmplAttr, Saved, New));
104 } else {
Benjamin Kramer5bbc3852012-02-06 11:13:08 +0000105 Attr *NewAttr = sema::instantiateTemplateAttribute(TmplAttr, Context,
106 *this, TemplateArgs);
Rafael Espindola31c195a2012-05-15 14:09:55 +0000107 if (NewAttr)
108 New->addAttr(NewAttr);
DeLesley Hutchins23323e02012-01-20 22:50:54 +0000109 }
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000110 }
111}
112
Douglas Gregor4f722be2009-03-25 15:45:12 +0000113Decl *
114TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000115 llvm_unreachable("Translation units cannot be instantiated");
Douglas Gregor4f722be2009-03-25 15:45:12 +0000116}
117
118Decl *
Chris Lattner57ad3782011-02-17 20:34:02 +0000119TemplateDeclInstantiator::VisitLabelDecl(LabelDecl *D) {
120 LabelDecl *Inst = LabelDecl::Create(SemaRef.Context, Owner, D->getLocation(),
121 D->getIdentifier());
122 Owner->addDecl(Inst);
123 return Inst;
124}
125
126Decl *
Douglas Gregor4f722be2009-03-25 15:45:12 +0000127TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000128 llvm_unreachable("Namespaces cannot be instantiated");
Douglas Gregor4f722be2009-03-25 15:45:12 +0000129}
130
John McCall3dbd3d52010-02-16 06:53:13 +0000131Decl *
132TemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
133 NamespaceAliasDecl *Inst
134 = NamespaceAliasDecl::Create(SemaRef.Context, Owner,
135 D->getNamespaceLoc(),
136 D->getAliasLoc(),
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +0000137 D->getIdentifier(),
138 D->getQualifierLoc(),
John McCall3dbd3d52010-02-16 06:53:13 +0000139 D->getTargetNameLoc(),
140 D->getNamespace());
141 Owner->addDecl(Inst);
142 return Inst;
143}
144
Richard Smith3e4c6c42011-05-05 21:57:07 +0000145Decl *TemplateDeclInstantiator::InstantiateTypedefNameDecl(TypedefNameDecl *D,
146 bool IsTypeAlias) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000147 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000148 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor561f8122011-07-01 01:22:09 +0000149 if (DI->getType()->isInstantiationDependentType() ||
Douglas Gregor836adf62010-05-24 17:22:01 +0000150 DI->getType()->isVariablyModifiedType()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000151 DI = SemaRef.SubstType(DI, TemplateArgs,
152 D->getLocation(), D->getDeclName());
153 if (!DI) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000154 Invalid = true;
John McCalla93c9342009-12-07 02:54:59 +0000155 DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000156 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000157 } else {
158 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000159 }
Mike Stump1eb44332009-09-09 15:08:12 +0000160
Richard Smithb5b37d12012-10-23 00:32:41 +0000161 // HACK: g++ has a bug where it gets the value kind of ?: wrong.
162 // libstdc++ relies upon this bug in its implementation of common_type.
163 // If we happen to be processing that implementation, fake up the g++ ?:
164 // semantics. See LWG issue 2141 for more information on the bug.
165 const DecltypeType *DT = DI->getType()->getAs<DecltypeType>();
166 CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D->getDeclContext());
167 if (DT && RD && isa<ConditionalOperator>(DT->getUnderlyingExpr()) &&
168 DT->isReferenceType() &&
169 RD->getEnclosingNamespaceContext() == SemaRef.getStdNamespace() &&
170 RD->getIdentifier() && RD->getIdentifier()->isStr("common_type") &&
171 D->getIdentifier() && D->getIdentifier()->isStr("type") &&
172 SemaRef.getSourceManager().isInSystemHeader(D->getLocStart()))
173 // Fold it to the (non-reference) type which g++ would have produced.
174 DI = SemaRef.Context.getTrivialTypeSourceInfo(
175 DI->getType().getNonReferenceType());
176
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000177 // Create the new typedef
Richard Smith162e1c12011-04-15 14:24:37 +0000178 TypedefNameDecl *Typedef;
179 if (IsTypeAlias)
180 Typedef = TypeAliasDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
181 D->getLocation(), D->getIdentifier(), DI);
182 else
183 Typedef = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
184 D->getLocation(), D->getIdentifier(), DI);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000185 if (Invalid)
186 Typedef->setInvalidDecl();
187
John McCallcde5a402011-02-01 08:20:08 +0000188 // If the old typedef was the name for linkage purposes of an anonymous
189 // tag decl, re-establish that relationship for the new typedef.
190 if (const TagType *oldTagType = D->getUnderlyingType()->getAs<TagType>()) {
191 TagDecl *oldTag = oldTagType->getDecl();
Richard Smith162e1c12011-04-15 14:24:37 +0000192 if (oldTag->getTypedefNameForAnonDecl() == D) {
John McCallcde5a402011-02-01 08:20:08 +0000193 TagDecl *newTag = DI->getType()->castAs<TagType>()->getDecl();
Richard Smith162e1c12011-04-15 14:24:37 +0000194 assert(!newTag->getIdentifier() && !newTag->getTypedefNameForAnonDecl());
195 newTag->setTypedefNameForAnonDecl(Typedef);
John McCallcde5a402011-02-01 08:20:08 +0000196 }
Douglas Gregord57a38e2010-04-23 16:25:07 +0000197 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000198
Douglas Gregoref96ee02012-01-14 16:38:05 +0000199 if (TypedefNameDecl *Prev = D->getPreviousDecl()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000200 NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
201 TemplateArgs);
Douglas Gregorb7107222011-03-04 19:46:35 +0000202 if (!InstPrev)
203 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000204
Rafael Espindola5df37bd2011-12-26 22:42:47 +0000205 TypedefNameDecl *InstPrevTypedef = cast<TypedefNameDecl>(InstPrev);
206
207 // If the typedef types are not identical, reject them.
208 SemaRef.isIncompatibleTypedef(InstPrevTypedef, Typedef);
209
210 Typedef->setPreviousDeclaration(InstPrevTypedef);
John McCall5126fd02009-12-30 00:31:22 +0000211 }
212
John McCall1d8d1cc2010-08-01 02:01:53 +0000213 SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef);
Douglas Gregord57a38e2010-04-23 16:25:07 +0000214
John McCall46460a62010-01-20 21:53:11 +0000215 Typedef->setAccess(D->getAccess());
Mike Stump1eb44332009-09-09 15:08:12 +0000216
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000217 return Typedef;
218}
219
Richard Smith162e1c12011-04-15 14:24:37 +0000220Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
Richard Smith3e4c6c42011-05-05 21:57:07 +0000221 Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/false);
222 Owner->addDecl(Typedef);
223 return Typedef;
Richard Smith162e1c12011-04-15 14:24:37 +0000224}
225
226Decl *TemplateDeclInstantiator::VisitTypeAliasDecl(TypeAliasDecl *D) {
Richard Smith3e4c6c42011-05-05 21:57:07 +0000227 Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/true);
228 Owner->addDecl(Typedef);
229 return Typedef;
230}
231
232Decl *
233TemplateDeclInstantiator::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
234 // Create a local instantiation scope for this type alias template, which
235 // will contain the instantiations of the template parameters.
236 LocalInstantiationScope Scope(SemaRef);
237
238 TemplateParameterList *TempParams = D->getTemplateParameters();
239 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
240 if (!InstParams)
241 return 0;
242
243 TypeAliasDecl *Pattern = D->getTemplatedDecl();
244
245 TypeAliasTemplateDecl *PrevAliasTemplate = 0;
Douglas Gregoref96ee02012-01-14 16:38:05 +0000246 if (Pattern->getPreviousDecl()) {
Richard Smith3e4c6c42011-05-05 21:57:07 +0000247 DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
David Blaikie3bc93e32012-12-19 00:45:41 +0000248 if (!Found.empty()) {
249 PrevAliasTemplate = dyn_cast<TypeAliasTemplateDecl>(Found.front());
Richard Smith3e4c6c42011-05-05 21:57:07 +0000250 }
251 }
252
253 TypeAliasDecl *AliasInst = cast_or_null<TypeAliasDecl>(
254 InstantiateTypedefNameDecl(Pattern, /*IsTypeAlias=*/true));
255 if (!AliasInst)
256 return 0;
257
258 TypeAliasTemplateDecl *Inst
259 = TypeAliasTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
260 D->getDeclName(), InstParams, AliasInst);
261 if (PrevAliasTemplate)
262 Inst->setPreviousDeclaration(PrevAliasTemplate);
263
264 Inst->setAccess(D->getAccess());
265
266 if (!PrevAliasTemplate)
267 Inst->setInstantiatedFromMemberTemplate(D);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000268
Richard Smith3e4c6c42011-05-05 21:57:07 +0000269 Owner->addDecl(Inst);
270
271 return Inst;
Richard Smith162e1c12011-04-15 14:24:37 +0000272}
273
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000274Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
Douglas Gregor9901c572010-05-21 00:31:19 +0000275 // If this is the variable for an anonymous struct or union,
276 // instantiate the anonymous struct/union type first.
277 if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
278 if (RecordTy->getDecl()->isAnonymousStructOrUnion())
279 if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
280 return 0;
281
John McCallce3ff2b2009-08-25 22:02:44 +0000282 // Do substitution on the type of the declaration
John McCalla93c9342009-12-07 02:54:59 +0000283 TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
John McCall0a5fa062009-10-21 02:39:02 +0000284 TemplateArgs,
285 D->getTypeSpecStartLoc(),
286 D->getDeclName());
287 if (!DI)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000288 return 0;
289
Douglas Gregorc6dbc3f2010-09-12 07:37:24 +0000290 if (DI->getType()->isFunctionType()) {
291 SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function)
292 << D->isStaticDataMember() << DI->getType();
293 return 0;
294 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000295
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000296 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000297 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000298 D->getInnerLocStart(),
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000299 D->getLocation(), D->getIdentifier(),
John McCall0a5fa062009-10-21 02:39:02 +0000300 DI->getType(), DI,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000301 D->getStorageClass(),
302 D->getStorageClassAsWritten());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000303 Var->setThreadSpecified(D->isThreadSpecified());
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000304 Var->setInitStyle(D->getInitStyle());
Richard Smithad762fc2011-04-14 22:09:26 +0000305 Var->setCXXForRangeDecl(D->isCXXForRangeDecl());
Richard Smith796c1a12012-01-19 22:46:17 +0000306 Var->setConstexpr(D->isConstexpr());
Mike Stump1eb44332009-09-09 15:08:12 +0000307
John McCallb6217662010-03-15 10:12:16 +0000308 // Substitute the nested name specifier, if any.
309 if (SubstQualifier(D, Var))
310 return 0;
311
Mike Stump1eb44332009-09-09 15:08:12 +0000312 // If we are instantiating a static data member defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000313 // out-of-line, the instantiation will have the same lexical
314 // context (which will be a namespace scope) as the template.
315 if (D->isOutOfLine())
316 Var->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000317
John McCall46460a62010-01-20 21:53:11 +0000318 Var->setAccess(D->getAccess());
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000319
Argyrios Kyrtzidis6b6b42a2011-04-19 19:51:10 +0000320 if (!D->isStaticDataMember()) {
Douglas Gregorc070cc62010-06-17 23:14:26 +0000321 Var->setUsed(D->isUsed(false));
Argyrios Kyrtzidis6b6b42a2011-04-19 19:51:10 +0000322 Var->setReferenced(D->isReferenced());
323 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000324
Mike Stump390b4cc2009-05-16 07:39:55 +0000325 // FIXME: In theory, we could have a previous declaration for variables that
326 // are not static data members.
John McCall68263142009-11-18 22:49:29 +0000327 // FIXME: having to fake up a LookupResult is dumb.
328 LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
Douglas Gregor449d0a82010-03-01 19:11:54 +0000329 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Douglas Gregor60c93c92010-02-09 07:26:29 +0000330 if (D->isStaticDataMember())
331 SemaRef.LookupQualifiedName(Previous, Owner, false);
Douglas Gregor9aab9c42011-12-10 01:22:52 +0000332
333 // In ARC, infer 'retaining' for variables of retainable type.
David Blaikie4e4d0842012-03-11 07:00:24 +0000334 if (SemaRef.getLangOpts().ObjCAutoRefCount &&
Douglas Gregor9aab9c42011-12-10 01:22:52 +0000335 SemaRef.inferObjCARCLifetime(Var))
336 Var->setInvalidDecl();
337
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +0000338 SemaRef.CheckVariableDeclaration(Var, Previous);
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Douglas Gregor7caa6822009-07-24 20:34:43 +0000340 if (D->isOutOfLine()) {
Richard Smith3e9ea0b2011-12-21 00:25:33 +0000341 D->getLexicalDeclContext()->addDecl(Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000342 Owner->makeDeclVisibleInContext(Var);
343 } else {
344 Owner->addDecl(Var);
Douglas Gregorf7d72f52010-05-03 20:22:41 +0000345 if (Owner->isFunctionOrMethod())
346 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000347 }
DeLesley Hutchinsdd5756c2012-02-16 17:30:51 +0000348 SemaRef.InstantiateAttrs(TemplateArgs, D, Var, LateAttrs, StartingScope);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000349
Richard Smithbe507b62013-02-01 08:12:08 +0000350 if (Var->hasAttrs())
351 SemaRef.CheckAlignasUnderalignment(Var);
352
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000353 // Link instantiations of static data members back to the template from
354 // which they were instantiated.
355 if (Var->isStaticDataMember())
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000356 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000357 TSK_ImplicitInstantiation);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000358
Douglas Gregor60c93c92010-02-09 07:26:29 +0000359 if (Var->getAnyInitializer()) {
360 // We already have an initializer in the class.
361 } else if (D->getInit()) {
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000362 if (Var->isStaticDataMember() && !D->isOutOfLine())
Richard Smithadb1d4c2012-07-22 23:45:10 +0000363 SemaRef.PushExpressionEvaluationContext(Sema::ConstantEvaluated, D);
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000364 else
Richard Smithadb1d4c2012-07-22 23:45:10 +0000365 SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated, D);
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000366
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000367 // Instantiate the initializer.
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000368 ExprResult Init = SemaRef.SubstInitializer(D->getInit(), TemplateArgs,
369 D->getInitStyle() == VarDecl::CallInit);
370 if (!Init.isInvalid()) {
Richard Smith34b41d92011-02-20 03:19:35 +0000371 bool TypeMayContainAuto = true;
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000372 if (Init.get()) {
373 bool DirectInit = D->isDirectInit();
374 SemaRef.AddInitializerToDecl(Var, Init.take(), DirectInit,
375 TypeMayContainAuto);
376 } else
Eli Friedman6aeaa602012-01-05 22:34:08 +0000377 SemaRef.ActOnUninitializedDecl(Var, TypeMayContainAuto);
Douglas Gregor6eef5192009-12-14 19:27:10 +0000378 } else {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000379 // FIXME: Not too happy about invalidating the declaration
380 // because of a bogus initializer.
381 Var->setInvalidDecl();
Douglas Gregor6eef5192009-12-14 19:27:10 +0000382 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000383
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000384 SemaRef.PopExpressionEvaluationContext();
Richard Smithad762fc2011-04-14 22:09:26 +0000385 } else if ((!Var->isStaticDataMember() || Var->isOutOfLine()) &&
386 !Var->isCXXForRangeDecl())
John McCalld226f652010-08-21 09:40:31 +0000387 SemaRef.ActOnUninitializedDecl(Var, false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000388
Richard Smithe3499ca2011-06-21 23:42:09 +0000389 // Diagnose unused local variables with dependent types, where the diagnostic
390 // will have been deferred.
391 if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed() &&
392 D->getType()->isDependentType())
Douglas Gregor5764f612010-05-08 23:05:03 +0000393 SemaRef.DiagnoseUnusedDecl(Var);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +0000394
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000395 return Var;
396}
397
Abramo Bagnara6206d532010-06-05 05:09:32 +0000398Decl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
399 AccessSpecDecl* AD
400 = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
401 D->getAccessSpecifierLoc(), D->getColonLoc());
402 Owner->addHiddenDecl(AD);
403 return AD;
404}
405
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000406Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
407 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000408 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor561f8122011-07-01 01:22:09 +0000409 if (DI->getType()->isInstantiationDependentType() ||
Douglas Gregor836adf62010-05-24 17:22:01 +0000410 DI->getType()->isVariablyModifiedType()) {
John McCall07fb6be2009-10-22 23:33:21 +0000411 DI = SemaRef.SubstType(DI, TemplateArgs,
412 D->getLocation(), D->getDeclName());
413 if (!DI) {
John McCalla93c9342009-12-07 02:54:59 +0000414 DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000415 Invalid = true;
416 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000417 // C++ [temp.arg.type]p3:
418 // If a declaration acquires a function type through a type
419 // dependent on a template-parameter and this causes a
420 // declaration that does not use the syntactic form of a
421 // function declarator to have function type, the program is
422 // ill-formed.
423 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000424 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000425 Invalid = true;
426 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000427 } else {
428 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000429 }
430
431 Expr *BitWidth = D->getBitWidth();
432 if (Invalid)
433 BitWidth = 0;
434 else if (BitWidth) {
Richard Smithf6702a32011-12-20 02:08:33 +0000435 // The bit-width expression is a constant expression.
436 EnterExpressionEvaluationContext Unevaluated(SemaRef,
437 Sema::ConstantEvaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000438
John McCall60d7b3a2010-08-24 06:29:42 +0000439 ExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000440 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000441 if (InstantiatedBitWidth.isInvalid()) {
442 Invalid = true;
443 BitWidth = 0;
444 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000445 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000446 }
447
John McCall07fb6be2009-10-22 23:33:21 +0000448 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
449 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000450 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000451 D->getLocation(),
452 D->isMutable(),
453 BitWidth,
Richard Smithca523302012-06-10 03:12:00 +0000454 D->getInClassInitStyle(),
Richard Smith703b6012012-05-23 04:22:22 +0000455 D->getInnerLocStart(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000456 D->getAccess(),
457 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000458 if (!Field) {
459 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000460 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000461 }
Mike Stump1eb44332009-09-09 15:08:12 +0000462
DeLesley Hutchins23323e02012-01-20 22:50:54 +0000463 SemaRef.InstantiateAttrs(TemplateArgs, D, Field, LateAttrs, StartingScope);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000464
Richard Smithbe507b62013-02-01 08:12:08 +0000465 if (Field->hasAttrs())
466 SemaRef.CheckAlignasUnderalignment(Field);
467
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000468 if (Invalid)
469 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000471 if (!Field->getDeclName()) {
472 // Keep track of where this decl came from.
473 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000474 }
Douglas Gregor9901c572010-05-21 00:31:19 +0000475 if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
476 if (Parent->isAnonymousStructOrUnion() &&
Sebastian Redl7a126a42010-08-31 00:36:30 +0000477 Parent->getRedeclContext()->isFunctionOrMethod())
Douglas Gregor9901c572010-05-21 00:31:19 +0000478 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000479 }
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000481 Field->setImplicit(D->isImplicit());
John McCall46460a62010-01-20 21:53:11 +0000482 Field->setAccess(D->getAccess());
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000483 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000484
485 return Field;
486}
487
Francois Pichet87c2e122010-11-21 06:08:52 +0000488Decl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
489 NamedDecl **NamedChain =
490 new (SemaRef.Context)NamedDecl*[D->getChainingSize()];
491
492 int i = 0;
493 for (IndirectFieldDecl::chain_iterator PI =
494 D->chain_begin(), PE = D->chain_end();
Douglas Gregorb7107222011-03-04 19:46:35 +0000495 PI != PE; ++PI) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000496 NamedDecl *Next = SemaRef.FindInstantiatedDecl(D->getLocation(), *PI,
Douglas Gregorb7107222011-03-04 19:46:35 +0000497 TemplateArgs);
498 if (!Next)
499 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000500
Douglas Gregorb7107222011-03-04 19:46:35 +0000501 NamedChain[i++] = Next;
502 }
Francois Pichet87c2e122010-11-21 06:08:52 +0000503
Francois Pichet40e17752010-12-09 10:07:54 +0000504 QualType T = cast<FieldDecl>(NamedChain[i-1])->getType();
Francois Pichet87c2e122010-11-21 06:08:52 +0000505 IndirectFieldDecl* IndirectField
506 = IndirectFieldDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Francois Pichet40e17752010-12-09 10:07:54 +0000507 D->getIdentifier(), T,
Francois Pichet87c2e122010-11-21 06:08:52 +0000508 NamedChain, D->getChainingSize());
509
510
511 IndirectField->setImplicit(D->isImplicit());
512 IndirectField->setAccess(D->getAccess());
513 Owner->addDecl(IndirectField);
514 return IndirectField;
515}
516
John McCall02cace72009-08-28 07:59:38 +0000517Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
John McCall02cace72009-08-28 07:59:38 +0000518 // Handle friend type expressions by simply substituting template
Douglas Gregor06245bf2010-04-07 17:57:12 +0000519 // parameters into the pattern type and checking the result.
John McCall32f2fb52010-03-25 18:04:51 +0000520 if (TypeSourceInfo *Ty = D->getFriendType()) {
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000521 TypeSourceInfo *InstTy;
522 // If this is an unsupported friend, don't bother substituting template
523 // arguments into it. The actual type referred to won't be used by any
524 // parts of Clang, and may not be valid for instantiating. Just use the
525 // same info for the instantiated friend.
526 if (D->isUnsupportedFriend()) {
527 InstTy = Ty;
528 } else {
529 InstTy = SemaRef.SubstType(Ty, TemplateArgs,
530 D->getLocation(), DeclarationName());
531 }
532 if (!InstTy)
Douglas Gregor7557a132009-12-24 20:56:24 +0000533 return 0;
John McCall02cace72009-08-28 07:59:38 +0000534
Richard Smithd6f80da2012-09-20 01:31:00 +0000535 FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getLocStart(),
Abramo Bagnara0216df82011-10-29 20:52:52 +0000536 D->getFriendLoc(), InstTy);
Douglas Gregor06245bf2010-04-07 17:57:12 +0000537 if (!FD)
538 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000539
Douglas Gregor06245bf2010-04-07 17:57:12 +0000540 FD->setAccess(AS_public);
John McCall9a34edb2010-10-19 01:40:49 +0000541 FD->setUnsupportedFriend(D->isUnsupportedFriend());
Douglas Gregor06245bf2010-04-07 17:57:12 +0000542 Owner->addDecl(FD);
543 return FD;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000544 }
545
Douglas Gregor06245bf2010-04-07 17:57:12 +0000546 NamedDecl *ND = D->getFriendDecl();
547 assert(ND && "friend decl must be a decl or a type!");
548
John McCallaf2094e2010-04-08 09:05:18 +0000549 // All of the Visit implementations for the various potential friend
550 // declarations have to be carefully written to work for friend
551 // objects, with the most important detail being that the target
552 // decl should almost certainly not be placed in Owner.
553 Decl *NewND = Visit(ND);
Douglas Gregor06245bf2010-04-07 17:57:12 +0000554 if (!NewND) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000555
John McCall02cace72009-08-28 07:59:38 +0000556 FriendDecl *FD =
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000557 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor06245bf2010-04-07 17:57:12 +0000558 cast<NamedDecl>(NewND), D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000559 FD->setAccess(AS_public);
John McCall9a34edb2010-10-19 01:40:49 +0000560 FD->setUnsupportedFriend(D->isUnsupportedFriend());
John McCall02cace72009-08-28 07:59:38 +0000561 Owner->addDecl(FD);
562 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000563}
564
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000565Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
566 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Richard Smithf6702a32011-12-20 02:08:33 +0000568 // The expression in a static assertion is a constant expression.
569 EnterExpressionEvaluationContext Unevaluated(SemaRef,
570 Sema::ConstantEvaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000571
John McCall60d7b3a2010-08-24 06:29:42 +0000572 ExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000573 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000574 if (InstantiatedAssertExpr.isInvalid())
575 return 0;
576
Richard Smithe3f470a2012-07-11 22:37:56 +0000577 return SemaRef.BuildStaticAssertDeclaration(D->getLocation(),
John McCall9ae2f072010-08-23 23:25:46 +0000578 InstantiatedAssertExpr.get(),
Richard Smithe3f470a2012-07-11 22:37:56 +0000579 D->getMessage(),
580 D->getRParenLoc(),
581 D->isFailed());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000582}
583
584Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Richard Smith38f0df32012-03-26 04:58:10 +0000585 EnumDecl *PrevDecl = 0;
586 if (D->getPreviousDecl()) {
587 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
588 D->getPreviousDecl(),
589 TemplateArgs);
590 if (!Prev) return 0;
591 PrevDecl = cast<EnumDecl>(Prev);
592 }
593
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000594 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000595 D->getLocation(), D->getIdentifier(),
Richard Smith38f0df32012-03-26 04:58:10 +0000596 PrevDecl, D->isScoped(),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +0000597 D->isScopedUsingClassTag(), D->isFixed());
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000598 if (D->isFixed()) {
Richard Smithf1c66b42012-03-14 23:13:10 +0000599 if (TypeSourceInfo *TI = D->getIntegerTypeSourceInfo()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000600 // If we have type source information for the underlying type, it means it
601 // has been explicitly set by the user. Perform substitution on it before
602 // moving on.
603 SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
Richard Smithf1c66b42012-03-14 23:13:10 +0000604 TypeSourceInfo *NewTI = SemaRef.SubstType(TI, TemplateArgs, UnderlyingLoc,
605 DeclarationName());
606 if (!NewTI || SemaRef.CheckEnumUnderlyingType(NewTI))
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000607 Enum->setIntegerType(SemaRef.Context.IntTy);
Richard Smithf1c66b42012-03-14 23:13:10 +0000608 else
609 Enum->setIntegerTypeSourceInfo(NewTI);
610 } else {
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000611 assert(!D->getIntegerType()->isDependentType()
612 && "Dependent type without type source info");
613 Enum->setIntegerType(D->getIntegerType());
614 }
615 }
616
John McCall5b629aa2010-10-22 23:36:17 +0000617 SemaRef.InstantiateAttrs(TemplateArgs, D, Enum);
618
Richard Smithf1c66b42012-03-14 23:13:10 +0000619 Enum->setInstantiationOfMemberEnum(D, TSK_ImplicitInstantiation);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000620 Enum->setAccess(D->getAccess());
John McCallb6217662010-03-15 10:12:16 +0000621 if (SubstQualifier(D, Enum)) return 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000622 Owner->addDecl(Enum);
Richard Smithf1c66b42012-03-14 23:13:10 +0000623
Richard Smith4ca93d92012-03-26 04:08:46 +0000624 EnumDecl *Def = D->getDefinition();
625 if (Def && Def != D) {
626 // If this is an out-of-line definition of an enum member template, check
627 // that the underlying types match in the instantiation of both
628 // declarations.
629 if (TypeSourceInfo *TI = Def->getIntegerTypeSourceInfo()) {
630 SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
631 QualType DefnUnderlying =
632 SemaRef.SubstType(TI->getType(), TemplateArgs,
633 UnderlyingLoc, DeclarationName());
634 SemaRef.CheckEnumRedeclaration(Def->getLocation(), Def->isScoped(),
635 DefnUnderlying, Enum);
636 }
637 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000638
Douglas Gregor96084f12010-03-01 19:00:07 +0000639 if (D->getDeclContext()->isFunctionOrMethod())
640 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000641
Richard Smithf1c66b42012-03-14 23:13:10 +0000642 // C++11 [temp.inst]p1: The implicit instantiation of a class template
643 // specialization causes the implicit instantiation of the declarations, but
644 // not the definitions of scoped member enumerations.
645 // FIXME: There appears to be no wording for what happens for an enum defined
Richard Smith38f0df32012-03-26 04:58:10 +0000646 // within a block scope, but we treat that much like a member template. Only
647 // instantiate the definition when visiting the definition in that case, since
648 // we will visit all redeclarations.
649 if (!Enum->isScoped() && Def &&
650 (!D->getDeclContext()->isFunctionOrMethod() || D->isCompleteDefinition()))
Richard Smith4ca93d92012-03-26 04:08:46 +0000651 InstantiateEnumDefinition(Enum, Def);
Richard Smithf1c66b42012-03-14 23:13:10 +0000652
653 return Enum;
654}
655
656void TemplateDeclInstantiator::InstantiateEnumDefinition(
657 EnumDecl *Enum, EnumDecl *Pattern) {
658 Enum->startDefinition();
659
Richard Smith1af83c42012-03-23 03:33:32 +0000660 // Update the location to refer to the definition.
661 Enum->setLocation(Pattern->getLocation());
662
Chris Lattner5f9e2722011-07-23 10:55:15 +0000663 SmallVector<Decl*, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000664
665 EnumConstantDecl *LastEnumConst = 0;
Richard Smithf1c66b42012-03-14 23:13:10 +0000666 for (EnumDecl::enumerator_iterator EC = Pattern->enumerator_begin(),
667 ECEnd = Pattern->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000668 EC != ECEnd; ++EC) {
669 // The specified value for the enumerator.
John McCall60d7b3a2010-08-24 06:29:42 +0000670 ExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000671 if (Expr *UninstValue = EC->getInitExpr()) {
Richard Smithf6702a32011-12-20 02:08:33 +0000672 // The enumerator's value expression is a constant expression.
Mike Stump1eb44332009-09-09 15:08:12 +0000673 EnterExpressionEvaluationContext Unevaluated(SemaRef,
Richard Smithf6702a32011-12-20 02:08:33 +0000674 Sema::ConstantEvaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000675
John McCallce3ff2b2009-08-25 22:02:44 +0000676 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000677 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000678
679 // Drop the initial value and continue.
680 bool isInvalid = false;
681 if (Value.isInvalid()) {
682 Value = SemaRef.Owned((Expr *)0);
683 isInvalid = true;
684 }
685
Mike Stump1eb44332009-09-09 15:08:12 +0000686 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000687 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
688 EC->getLocation(), EC->getIdentifier(),
John McCall9ae2f072010-08-23 23:25:46 +0000689 Value.get());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000690
691 if (isInvalid) {
692 if (EnumConst)
693 EnumConst->setInvalidDecl();
694 Enum->setInvalidDecl();
695 }
696
697 if (EnumConst) {
David Blaikie581deb32012-06-06 20:45:41 +0000698 SemaRef.InstantiateAttrs(TemplateArgs, *EC, EnumConst);
John McCall5b629aa2010-10-22 23:36:17 +0000699
John McCall3b85ecf2010-01-23 22:37:59 +0000700 EnumConst->setAccess(Enum->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000701 Enum->addDecl(EnumConst);
John McCalld226f652010-08-21 09:40:31 +0000702 Enumerators.push_back(EnumConst);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000703 LastEnumConst = EnumConst;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000704
Richard Smithf1c66b42012-03-14 23:13:10 +0000705 if (Pattern->getDeclContext()->isFunctionOrMethod() &&
706 !Enum->isScoped()) {
Douglas Gregor96084f12010-03-01 19:00:07 +0000707 // If the enumeration is within a function or method, record the enum
708 // constant as a local.
David Blaikie581deb32012-06-06 20:45:41 +0000709 SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
Douglas Gregor96084f12010-03-01 19:00:07 +0000710 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000711 }
712 }
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Richard Smithf1c66b42012-03-14 23:13:10 +0000714 // FIXME: Fixup LBraceLoc
715 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(),
716 Enum->getRBraceLoc(), Enum,
Eli Friedmande7a0fc2010-08-15 02:27:09 +0000717 Enumerators.data(), Enumerators.size(),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000718 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000719}
720
Douglas Gregor6477b692009-03-25 15:04:13 +0000721Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000722 llvm_unreachable("EnumConstantDecls can only occur within EnumDecls.");
Douglas Gregor6477b692009-03-25 15:04:13 +0000723}
724
John McCalle29ba202009-08-20 01:44:21 +0000725Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
John McCall93ba8572010-03-25 06:39:04 +0000726 bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
727
Douglas Gregor550d9b22009-10-31 17:21:17 +0000728 // Create a local instantiation scope for this class template, which
729 // will contain the instantiations of the template parameters.
John McCall2a7fb272010-08-25 05:32:35 +0000730 LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000731 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000732 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000733 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000734 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000735
736 CXXRecordDecl *Pattern = D->getTemplatedDecl();
John McCall93ba8572010-03-25 06:39:04 +0000737
738 // Instantiate the qualifier. We have to do this first in case
739 // we're a friend declaration, because if we are then we need to put
740 // the new declaration in the appropriate context.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000741 NestedNameSpecifierLoc QualifierLoc = Pattern->getQualifierLoc();
742 if (QualifierLoc) {
743 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
744 TemplateArgs);
745 if (!QualifierLoc)
746 return 0;
John McCall93ba8572010-03-25 06:39:04 +0000747 }
748
749 CXXRecordDecl *PrevDecl = 0;
750 ClassTemplateDecl *PrevClassTemplate = 0;
751
Douglas Gregoref96ee02012-01-14 16:38:05 +0000752 if (!isFriend && Pattern->getPreviousDecl()) {
Nick Lewycky37574f52010-11-08 23:29:42 +0000753 DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
David Blaikie3bc93e32012-12-19 00:45:41 +0000754 if (!Found.empty()) {
755 PrevClassTemplate = dyn_cast<ClassTemplateDecl>(Found.front());
Nick Lewycky37574f52010-11-08 23:29:42 +0000756 if (PrevClassTemplate)
757 PrevDecl = PrevClassTemplate->getTemplatedDecl();
758 }
759 }
760
John McCall93ba8572010-03-25 06:39:04 +0000761 // If this isn't a friend, then it's a member template, in which
762 // case we just want to build the instantiation in the
763 // specialization. If it is a friend, we want to build it in
764 // the appropriate context.
765 DeclContext *DC = Owner;
766 if (isFriend) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000767 if (QualifierLoc) {
John McCall93ba8572010-03-25 06:39:04 +0000768 CXXScopeSpec SS;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000769 SS.Adopt(QualifierLoc);
John McCall93ba8572010-03-25 06:39:04 +0000770 DC = SemaRef.computeDeclContext(SS);
771 if (!DC) return 0;
772 } else {
773 DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
774 Pattern->getDeclContext(),
775 TemplateArgs);
776 }
777
778 // Look for a previous declaration of the template in the owning
779 // context.
780 LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
781 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
782 SemaRef.LookupQualifiedName(R, DC);
783
784 if (R.isSingleResult()) {
785 PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
786 if (PrevClassTemplate)
787 PrevDecl = PrevClassTemplate->getTemplatedDecl();
788 }
789
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000790 if (!PrevClassTemplate && QualifierLoc) {
John McCall93ba8572010-03-25 06:39:04 +0000791 SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
Douglas Gregor1eabb7d2010-03-31 23:17:41 +0000792 << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000793 << QualifierLoc.getSourceRange();
John McCall93ba8572010-03-25 06:39:04 +0000794 return 0;
795 }
796
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000797 bool AdoptedPreviousTemplateParams = false;
John McCall93ba8572010-03-25 06:39:04 +0000798 if (PrevClassTemplate) {
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000799 bool Complain = true;
800
801 // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
802 // template for struct std::tr1::__detail::_Map_base, where the
803 // template parameters of the friend declaration don't match the
804 // template parameters of the original declaration. In this one
805 // case, we don't complain about the ill-formed friend
806 // declaration.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000807 if (isFriend && Pattern->getIdentifier() &&
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000808 Pattern->getIdentifier()->isStr("_Map_base") &&
809 DC->isNamespace() &&
810 cast<NamespaceDecl>(DC)->getIdentifier() &&
811 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
812 DeclContext *DCParent = DC->getParent();
813 if (DCParent->isNamespace() &&
814 cast<NamespaceDecl>(DCParent)->getIdentifier() &&
815 cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
816 DeclContext *DCParent2 = DCParent->getParent();
817 if (DCParent2->isNamespace() &&
818 cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
819 cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
820 DCParent2->getParent()->isTranslationUnit())
821 Complain = false;
822 }
823 }
824
John McCall93ba8572010-03-25 06:39:04 +0000825 TemplateParameterList *PrevParams
826 = PrevClassTemplate->getTemplateParameters();
827
828 // Make sure the parameter lists match.
829 if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000830 Complain,
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000831 Sema::TPL_TemplateMatch)) {
832 if (Complain)
833 return 0;
834
835 AdoptedPreviousTemplateParams = true;
836 InstParams = PrevParams;
837 }
John McCall93ba8572010-03-25 06:39:04 +0000838
839 // Do some additional validation, then merge default arguments
840 // from the existing declarations.
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000841 if (!AdoptedPreviousTemplateParams &&
842 SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
John McCall93ba8572010-03-25 06:39:04 +0000843 Sema::TPC_ClassTemplate))
844 return 0;
845 }
846 }
847
John McCalle29ba202009-08-20 01:44:21 +0000848 CXXRecordDecl *RecordInst
John McCall93ba8572010-03-25 06:39:04 +0000849 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000850 Pattern->getLocStart(), Pattern->getLocation(),
851 Pattern->getIdentifier(), PrevDecl,
Douglas Gregorf0510d42009-10-12 23:11:44 +0000852 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000853
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000854 if (QualifierLoc)
855 RecordInst->setQualifierInfo(QualifierLoc);
John McCallb6217662010-03-15 10:12:16 +0000856
John McCalle29ba202009-08-20 01:44:21 +0000857 ClassTemplateDecl *Inst
John McCall93ba8572010-03-25 06:39:04 +0000858 = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
859 D->getIdentifier(), InstParams, RecordInst,
860 PrevClassTemplate);
John McCalle29ba202009-08-20 01:44:21 +0000861 RecordInst->setDescribedClassTemplate(Inst);
John McCallea7390c2010-04-08 20:25:50 +0000862
John McCall93ba8572010-03-25 06:39:04 +0000863 if (isFriend) {
John McCallea7390c2010-04-08 20:25:50 +0000864 if (PrevClassTemplate)
865 Inst->setAccess(PrevClassTemplate->getAccess());
866 else
867 Inst->setAccess(D->getAccess());
868
John McCall93ba8572010-03-25 06:39:04 +0000869 Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
870 // TODO: do we want to track the instantiation progeny of this
871 // friend target decl?
872 } else {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000873 Inst->setAccess(D->getAccess());
Nick Lewycky37574f52010-11-08 23:29:42 +0000874 if (!PrevClassTemplate)
875 Inst->setInstantiatedFromMemberTemplate(D);
John McCall93ba8572010-03-25 06:39:04 +0000876 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000877
Douglas Gregorf0510d42009-10-12 23:11:44 +0000878 // Trigger creation of the type for the instantiation.
John McCall3cb0ebd2010-03-10 03:28:59 +0000879 SemaRef.Context.getInjectedClassNameType(RecordInst,
Douglas Gregor24bae922010-07-08 18:37:38 +0000880 Inst->getInjectedClassNameSpecialization());
John McCallea7390c2010-04-08 20:25:50 +0000881
Douglas Gregor259571e2009-10-30 22:42:42 +0000882 // Finish handling of friends.
John McCall93ba8572010-03-25 06:39:04 +0000883 if (isFriend) {
Richard Smith1b7f9cb2012-03-13 03:12:56 +0000884 DC->makeDeclVisibleInContext(Inst);
Abramo Bagnara4c515482011-11-26 13:33:46 +0000885 Inst->setLexicalDeclContext(Owner);
886 RecordInst->setLexicalDeclContext(Owner);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000887 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000888 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000889
Abramo Bagnara4c515482011-11-26 13:33:46 +0000890 if (D->isOutOfLine()) {
891 Inst->setLexicalDeclContext(D->getLexicalDeclContext());
892 RecordInst->setLexicalDeclContext(D->getLexicalDeclContext());
893 }
894
John McCalle29ba202009-08-20 01:44:21 +0000895 Owner->addDecl(Inst);
Douglas Gregord65587f2010-11-10 19:44:59 +0000896
897 if (!PrevClassTemplate) {
898 // Queue up any out-of-line partial specializations of this member
899 // class template; the client will force their instantiation once
900 // the enclosing class has been instantiated.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000901 SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
Douglas Gregord65587f2010-11-10 19:44:59 +0000902 D->getPartialSpecializations(PartialSpecs);
903 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
904 if (PartialSpecs[I]->isOutOfLine())
905 OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I]));
906 }
907
John McCalle29ba202009-08-20 01:44:21 +0000908 return Inst;
909}
910
Douglas Gregord60e1052009-08-27 16:57:43 +0000911Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000912TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
913 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000914 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000915
Douglas Gregored9c0f92009-10-29 00:04:11 +0000916 // Lookup the already-instantiated declaration in the instantiation
917 // of the class template and return that.
918 DeclContext::lookup_result Found
919 = Owner->lookup(ClassTemplate->getDeclName());
David Blaikie3bc93e32012-12-19 00:45:41 +0000920 if (Found.empty())
Douglas Gregored9c0f92009-10-29 00:04:11 +0000921 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000922
Douglas Gregored9c0f92009-10-29 00:04:11 +0000923 ClassTemplateDecl *InstClassTemplate
David Blaikie3bc93e32012-12-19 00:45:41 +0000924 = dyn_cast<ClassTemplateDecl>(Found.front());
Douglas Gregored9c0f92009-10-29 00:04:11 +0000925 if (!InstClassTemplate)
926 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000927
Douglas Gregord65587f2010-11-10 19:44:59 +0000928 if (ClassTemplatePartialSpecializationDecl *Result
929 = InstClassTemplate->findPartialSpecInstantiatedFromMember(D))
930 return Result;
931
932 return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D);
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000933}
934
935Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000936TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000937 // Create a local instantiation scope for this function template, which
938 // will contain the instantiations of the template parameters and then get
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000939 // merged with the local instantiation scope for the function template
Douglas Gregor550d9b22009-10-31 17:21:17 +0000940 // itself.
John McCall2a7fb272010-08-25 05:32:35 +0000941 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor895162d2010-04-30 18:55:50 +0000942
Douglas Gregord60e1052009-08-27 16:57:43 +0000943 TemplateParameterList *TempParams = D->getTemplateParameters();
944 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000945 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000946 return NULL;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000947
Douglas Gregora735b202009-10-13 14:39:41 +0000948 FunctionDecl *Instantiated = 0;
949 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000950 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
Douglas Gregora735b202009-10-13 14:39:41 +0000951 InstParams));
952 else
953 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000954 D->getTemplatedDecl(),
Douglas Gregora735b202009-10-13 14:39:41 +0000955 InstParams));
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000956
Douglas Gregora735b202009-10-13 14:39:41 +0000957 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000958 return 0;
959
Mike Stump1eb44332009-09-09 15:08:12 +0000960 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000961 // template from which it was instantiated.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000962 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000963 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000964 InstTemplate->setAccess(D->getAccess());
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000965 assert(InstTemplate &&
Douglas Gregora735b202009-10-13 14:39:41 +0000966 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
John McCalle976ffe2009-12-14 23:19:40 +0000967
John McCallb1a56e72010-03-26 23:10:15 +0000968 bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
969
John McCalle976ffe2009-12-14 23:19:40 +0000970 // Link the instantiation back to the pattern *unless* this is a
971 // non-definition friend declaration.
972 if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
John McCallb1a56e72010-03-26 23:10:15 +0000973 !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
Douglas Gregora735b202009-10-13 14:39:41 +0000974 InstTemplate->setInstantiatedFromMemberTemplate(D);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000975
John McCallb1a56e72010-03-26 23:10:15 +0000976 // Make declarations visible in the appropriate context.
John McCall1f2e1a92012-08-10 03:15:35 +0000977 if (!isFriend) {
Douglas Gregora735b202009-10-13 14:39:41 +0000978 Owner->addDecl(InstTemplate);
John McCall1f2e1a92012-08-10 03:15:35 +0000979 } else if (InstTemplate->getDeclContext()->isRecord() &&
980 !D->getPreviousDecl()) {
981 SemaRef.CheckFriendAccess(InstTemplate);
982 }
John McCallb1a56e72010-03-26 23:10:15 +0000983
Douglas Gregord60e1052009-08-27 16:57:43 +0000984 return InstTemplate;
985}
986
Douglas Gregord475b8d2009-03-25 21:17:03 +0000987Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
988 CXXRecordDecl *PrevDecl = 0;
989 if (D->isInjectedClassName())
990 PrevDecl = cast<CXXRecordDecl>(Owner);
Douglas Gregoref96ee02012-01-14 16:38:05 +0000991 else if (D->getPreviousDecl()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000992 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
Douglas Gregoref96ee02012-01-14 16:38:05 +0000993 D->getPreviousDecl(),
John McCall6c1c1b82009-12-15 22:29:06 +0000994 TemplateArgs);
995 if (!Prev) return 0;
996 PrevDecl = cast<CXXRecordDecl>(Prev);
997 }
Douglas Gregord475b8d2009-03-25 21:17:03 +0000998
999 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +00001000 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00001001 D->getLocStart(), D->getLocation(),
1002 D->getIdentifier(), PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00001003
1004 // Substitute the nested name specifier, if any.
1005 if (SubstQualifier(D, Record))
1006 return 0;
1007
Douglas Gregord475b8d2009-03-25 21:17:03 +00001008 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +00001009 // FIXME: Check against AS_none is an ugly hack to work around the issue that
1010 // the tag decls introduced by friend class declarations don't have an access
1011 // specifier. Remove once this area of the code gets sorted out.
1012 if (D->getAccess() != AS_none)
1013 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +00001014 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001015 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001016
John McCall02cace72009-08-28 07:59:38 +00001017 // If the original function was part of a friend declaration,
1018 // inherit its namespace state.
1019 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
1020 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
1021
Douglas Gregor9901c572010-05-21 00:31:19 +00001022 // Make sure that anonymous structs and unions are recorded.
1023 if (D->isAnonymousStructOrUnion()) {
1024 Record->setAnonymousStructOrUnion(true);
Sebastian Redl7a126a42010-08-31 00:36:30 +00001025 if (Record->getDeclContext()->getRedeclContext()->isFunctionOrMethod())
Douglas Gregor9901c572010-05-21 00:31:19 +00001026 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
1027 }
Anders Carlssond8b285f2009-09-01 04:26:58 +00001028
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001029 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001030 return Record;
1031}
1032
Douglas Gregor71074fd2012-09-13 21:56:43 +00001033/// \brief Adjust the given function type for an instantiation of the
1034/// given declaration, to cope with modifications to the function's type that
1035/// aren't reflected in the type-source information.
1036///
1037/// \param D The declaration we're instantiating.
1038/// \param TInfo The already-instantiated type.
1039static QualType adjustFunctionTypeForInstantiation(ASTContext &Context,
1040 FunctionDecl *D,
1041 TypeSourceInfo *TInfo) {
Douglas Gregorbed51fe2012-09-13 22:01:49 +00001042 const FunctionProtoType *OrigFunc
1043 = D->getType()->castAs<FunctionProtoType>();
1044 const FunctionProtoType *NewFunc
1045 = TInfo->getType()->castAs<FunctionProtoType>();
1046 if (OrigFunc->getExtInfo() == NewFunc->getExtInfo())
1047 return TInfo->getType();
1048
1049 FunctionProtoType::ExtProtoInfo NewEPI = NewFunc->getExtProtoInfo();
1050 NewEPI.ExtInfo = OrigFunc->getExtInfo();
1051 return Context.getFunctionType(NewFunc->getResultType(),
1052 NewFunc->arg_type_begin(),
1053 NewFunc->getNumArgs(),
1054 NewEPI);
Douglas Gregor71074fd2012-09-13 21:56:43 +00001055}
1056
John McCall02cace72009-08-28 07:59:38 +00001057/// Normal class members are of more specific types and therefore
1058/// don't make it here. This function serves two purposes:
1059/// 1) instantiating function templates
1060/// 2) substituting friend declarations
1061/// FIXME: preserve function definitions in case #2
Douglas Gregor7557a132009-12-24 20:56:24 +00001062Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
Douglas Gregora735b202009-10-13 14:39:41 +00001063 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +00001064 // Check whether there is already a function template specialization for
1065 // this declaration.
1066 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
John McCallb0cb0222010-03-27 05:57:59 +00001067 if (FunctionTemplate && !TemplateParams) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001068 std::pair<const TemplateArgument *, unsigned> Innermost
Douglas Gregor24bae922010-07-08 18:37:38 +00001069 = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +00001070
Douglas Gregor1e1e9722012-03-28 14:34:23 +00001071 void *InsertPos = 0;
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001072 FunctionDecl *SpecFunc
1073 = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
1074 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001075
Douglas Gregor127102b2009-06-29 20:59:39 +00001076 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001077 if (SpecFunc)
1078 return SpecFunc;
Douglas Gregor127102b2009-06-29 20:59:39 +00001079 }
Mike Stump1eb44332009-09-09 15:08:12 +00001080
John McCallb0cb0222010-03-27 05:57:59 +00001081 bool isFriend;
1082 if (FunctionTemplate)
1083 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1084 else
1085 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1086
Douglas Gregor79c22782010-01-16 20:21:20 +00001087 bool MergeWithParentScope = (TemplateParams != 0) ||
Douglas Gregorb212d9a2010-05-21 21:25:08 +00001088 Owner->isFunctionOrMethod() ||
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001089 !(isa<Decl>(Owner) &&
Douglas Gregor79c22782010-01-16 20:21:20 +00001090 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
John McCall2a7fb272010-08-25 05:32:35 +00001091 LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001092
Chris Lattner5f9e2722011-07-23 10:55:15 +00001093 SmallVector<ParmVarDecl *, 4> Params;
David Blaikie64b4b432011-11-10 05:42:04 +00001094 TypeSourceInfo *TInfo = SubstFunctionType(D, Params);
John McCall21ef0fa2010-03-11 09:03:00 +00001095 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001096 return 0;
Douglas Gregor71074fd2012-09-13 21:56:43 +00001097 QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo);
John McCallfd810b12009-08-14 02:03:10 +00001098
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001099 NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
1100 if (QualifierLoc) {
1101 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
1102 TemplateArgs);
1103 if (!QualifierLoc)
1104 return 0;
John McCalld325daa2010-03-26 04:53:08 +00001105 }
1106
John McCall68b6b872010-02-06 01:50:47 +00001107 // If we're instantiating a local function declaration, put the result
1108 // in the owner; otherwise we need to find the instantiated context.
1109 DeclContext *DC;
1110 if (D->getDeclContext()->isFunctionOrMethod())
1111 DC = Owner;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001112 else if (isFriend && QualifierLoc) {
John McCalld325daa2010-03-26 04:53:08 +00001113 CXXScopeSpec SS;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001114 SS.Adopt(QualifierLoc);
John McCalld325daa2010-03-26 04:53:08 +00001115 DC = SemaRef.computeDeclContext(SS);
1116 if (!DC) return 0;
1117 } else {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001118 DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001119 TemplateArgs);
John McCalld325daa2010-03-26 04:53:08 +00001120 }
John McCall68b6b872010-02-06 01:50:47 +00001121
John McCall02cace72009-08-28 07:59:38 +00001122 FunctionDecl *Function =
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001123 FunctionDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(),
Abramo Bagnara635311f2012-10-04 21:40:42 +00001124 D->getNameInfo(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001125 D->getStorageClass(), D->getStorageClassAsWritten(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001126 D->isInlineSpecified(), D->hasWrittenPrototype(),
Richard Smith86c3ae42012-02-13 03:54:03 +00001127 D->isConstexpr());
John McCallb6217662010-03-15 10:12:16 +00001128
Richard Smithd4497dd2013-01-25 00:08:28 +00001129 if (D->isInlined())
1130 Function->setImplicitlyInline();
1131
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001132 if (QualifierLoc)
1133 Function->setQualifierInfo(QualifierLoc);
John McCallb6217662010-03-15 10:12:16 +00001134
John McCallb1a56e72010-03-26 23:10:15 +00001135 DeclContext *LexicalDC = Owner;
1136 if (!isFriend && D->isOutOfLine()) {
1137 assert(D->getDeclContext()->isFileContext());
1138 LexicalDC = D->getDeclContext();
1139 }
1140
1141 Function->setLexicalDeclContext(LexicalDC);
Mike Stump1eb44332009-09-09 15:08:12 +00001142
Douglas Gregore53060f2009-06-25 22:08:12 +00001143 // Attach the parameters
Douglas Gregor5cbe1012011-07-05 18:30:26 +00001144 if (isa<FunctionProtoType>(Function->getType().IgnoreParens())) {
Douglas Gregor1d441ee2011-06-22 18:16:25 +00001145 // Adopt the already-instantiated parameters into our own context.
1146 for (unsigned P = 0; P < Params.size(); ++P)
1147 if (Params[P])
1148 Params[P]->setOwningFunction(Function);
1149 } else {
1150 // Since we were instantiated via a typedef of a function type, create
1151 // new parameters.
1152 const FunctionProtoType *Proto
1153 = Function->getType()->getAs<FunctionProtoType>();
1154 assert(Proto && "No function prototype in template instantiation?");
1155 for (FunctionProtoType::arg_type_iterator AI = Proto->arg_type_begin(),
1156 AE = Proto->arg_type_end(); AI != AE; ++AI) {
1157 ParmVarDecl *Param
1158 = SemaRef.BuildParmVarDeclForTypedef(Function, Function->getLocation(),
1159 *AI);
1160 Param->setScopeInfo(0, Params.size());
1161 Params.push_back(Param);
1162 }
1163 }
David Blaikie4278c652011-09-21 18:16:56 +00001164 Function->setParams(Params);
John McCall02cace72009-08-28 07:59:38 +00001165
Douglas Gregorac7c2c82010-05-17 16:38:00 +00001166 SourceLocation InstantiateAtPOI;
Douglas Gregora735b202009-10-13 14:39:41 +00001167 if (TemplateParams) {
1168 // Our resulting instantiation is actually a function template, since we
1169 // are substituting only the outer template parameters. For example, given
1170 //
1171 // template<typename T>
1172 // struct X {
1173 // template<typename U> friend void f(T, U);
1174 // };
1175 //
1176 // X<int> x;
1177 //
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001178 // We are instantiating the friend function template "f" within X<int>,
Douglas Gregora735b202009-10-13 14:39:41 +00001179 // which means substituting int for T, but leaving "f" as a friend function
1180 // template.
1181 // Build the function template itself.
John McCalld325daa2010-03-26 04:53:08 +00001182 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
Douglas Gregora735b202009-10-13 14:39:41 +00001183 Function->getLocation(),
1184 Function->getDeclName(),
1185 TemplateParams, Function);
1186 Function->setDescribedFunctionTemplate(FunctionTemplate);
John McCallb1a56e72010-03-26 23:10:15 +00001187
1188 FunctionTemplate->setLexicalDeclContext(LexicalDC);
John McCalld325daa2010-03-26 04:53:08 +00001189
1190 if (isFriend && D->isThisDeclarationADefinition()) {
1191 // TODO: should we remember this connection regardless of whether
1192 // the friend declaration provided a body?
1193 FunctionTemplate->setInstantiatedFromMemberTemplate(
1194 D->getDescribedFunctionTemplate());
1195 }
Douglas Gregor66724ea2009-11-14 01:20:54 +00001196 } else if (FunctionTemplate) {
1197 // Record this function template specialization.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001198 std::pair<const TemplateArgument *, unsigned> Innermost
Douglas Gregor24bae922010-07-08 18:37:38 +00001199 = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001200 Function->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001201 TemplateArgumentList::CreateCopy(SemaRef.Context,
Douglas Gregor24bae922010-07-08 18:37:38 +00001202 Innermost.first,
1203 Innermost.second),
Douglas Gregor1e1e9722012-03-28 14:34:23 +00001204 /*InsertPos=*/0);
Chandler Carruth80f5b162011-08-18 09:09:59 +00001205 } else if (isFriend) {
1206 // Note, we need this connection even if the friend doesn't have a body.
1207 // Its body may exist but not have been attached yet due to deferred
1208 // parsing.
1209 // FIXME: It might be cleaner to set this when attaching the body to the
1210 // friend function declaration, however that would require finding all the
1211 // instantiations and modifying them.
John McCalld325daa2010-03-26 04:53:08 +00001212 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
John McCall02cace72009-08-28 07:59:38 +00001213 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001214
Douglas Gregore53060f2009-06-25 22:08:12 +00001215 if (InitFunctionInstantiation(Function, D))
1216 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001217
John McCallaf2094e2010-04-08 09:05:18 +00001218 bool isExplicitSpecialization = false;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001219
John McCall68263142009-11-18 22:49:29 +00001220 LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
1221 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
1222
John McCallaf2094e2010-04-08 09:05:18 +00001223 if (DependentFunctionTemplateSpecializationInfo *Info
1224 = D->getDependentSpecializationInfo()) {
1225 assert(isFriend && "non-friend has dependent specialization info?");
1226
1227 // This needs to be set now for future sanity.
1228 Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1229
1230 // Instantiate the explicit template arguments.
1231 TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1232 Info->getRAngleLoc());
Douglas Gregore02e2622010-12-22 21:19:48 +00001233 if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(),
1234 ExplicitArgs, TemplateArgs))
1235 return 0;
John McCallaf2094e2010-04-08 09:05:18 +00001236
1237 // Map the candidate templates to their instantiations.
1238 for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1239 Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1240 Info->getTemplate(I),
1241 TemplateArgs);
1242 if (!Temp) return 0;
1243
1244 Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1245 }
1246
1247 if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1248 &ExplicitArgs,
1249 Previous))
1250 Function->setInvalidDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001251
John McCallaf2094e2010-04-08 09:05:18 +00001252 isExplicitSpecialization = true;
1253
1254 } else if (TemplateParams || !FunctionTemplate) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001255 // Look only into the namespace where the friend would be declared to
1256 // find a previous declaration. This is the innermost enclosing namespace,
Douglas Gregora735b202009-10-13 14:39:41 +00001257 // as described in ActOnFriendFunctionDecl.
John McCall68263142009-11-18 22:49:29 +00001258 SemaRef.LookupQualifiedName(Previous, DC);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001259
Douglas Gregora735b202009-10-13 14:39:41 +00001260 // In C++, the previous declaration we find might be a tag type
1261 // (class or enum). In this case, the new declaration will hide the
1262 // tag type. Note that this does does not apply if we're declaring a
1263 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001264 if (Previous.isSingleTagDecl())
1265 Previous.clear();
Douglas Gregora735b202009-10-13 14:39:41 +00001266 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001267
John McCall9f54ad42009-12-10 09:41:52 +00001268 SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00001269 isExplicitSpecialization);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001270
John McCall76d32642010-04-24 01:30:58 +00001271 NamedDecl *PrincipalDecl = (TemplateParams
1272 ? cast<NamedDecl>(FunctionTemplate)
1273 : Function);
1274
Douglas Gregora735b202009-10-13 14:39:41 +00001275 // If the original function was part of a friend declaration,
1276 // inherit its namespace state and add it to the owner.
John McCalld325daa2010-03-26 04:53:08 +00001277 if (isFriend) {
John McCall68263142009-11-18 22:49:29 +00001278 NamedDecl *PrevDecl;
John McCall76d32642010-04-24 01:30:58 +00001279 if (TemplateParams)
Douglas Gregoref96ee02012-01-14 16:38:05 +00001280 PrevDecl = FunctionTemplate->getPreviousDecl();
John McCall76d32642010-04-24 01:30:58 +00001281 else
Douglas Gregoref96ee02012-01-14 16:38:05 +00001282 PrevDecl = Function->getPreviousDecl();
John McCall76d32642010-04-24 01:30:58 +00001283
1284 PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001285 DC->makeDeclVisibleInContext(PrincipalDecl);
Gabor Greifab297ac2010-08-30 21:10:05 +00001286
Gabor Greif77535df2010-08-30 22:25:56 +00001287 bool queuedInstantiation = false;
Gabor Greifab297ac2010-08-30 21:10:05 +00001288
Richard Smith53e53512011-10-19 00:54:10 +00001289 // C++98 [temp.friend]p5: When a function is defined in a friend function
1290 // declaration in a class template, the function is defined at each
1291 // instantiation of the class template. The function is defined even if it
1292 // is never used.
1293 // C++11 [temp.friend]p4: When a function is defined in a friend function
1294 // declaration in a class template, the function is instantiated when the
1295 // function is odr-used.
1296 //
1297 // If -Wc++98-compat is enabled, we go through the motions of checking for a
1298 // redefinition, but don't instantiate the function.
Richard Smith80ad52f2013-01-02 11:42:31 +00001299 if ((!SemaRef.getLangOpts().CPlusPlus11 ||
Richard Smith53e53512011-10-19 00:54:10 +00001300 SemaRef.Diags.getDiagnosticLevel(
1301 diag::warn_cxx98_compat_friend_redefinition,
1302 Function->getLocation())
1303 != DiagnosticsEngine::Ignored) &&
Douglas Gregor238058c2010-05-18 05:45:02 +00001304 D->isThisDeclarationADefinition()) {
1305 // Check for a function body.
1306 const FunctionDecl *Definition = 0;
Sean Hunt10620eb2011-05-06 20:44:56 +00001307 if (Function->isDefined(Definition) &&
Douglas Gregor238058c2010-05-18 05:45:02 +00001308 Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
Richard Smith53e53512011-10-19 00:54:10 +00001309 SemaRef.Diag(Function->getLocation(),
Richard Smith80ad52f2013-01-02 11:42:31 +00001310 SemaRef.getLangOpts().CPlusPlus11 ?
Richard Smith53e53512011-10-19 00:54:10 +00001311 diag::warn_cxx98_compat_friend_redefinition :
1312 diag::err_redefinition) << Function->getDeclName();
Douglas Gregor238058c2010-05-18 05:45:02 +00001313 SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
Richard Smith80ad52f2013-01-02 11:42:31 +00001314 if (!SemaRef.getLangOpts().CPlusPlus11)
Richard Smith53e53512011-10-19 00:54:10 +00001315 Function->setInvalidDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001316 }
Douglas Gregor238058c2010-05-18 05:45:02 +00001317 // Check for redefinitions due to other instantiations of this or
1318 // a similar friend function.
1319 else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1320 REnd = Function->redecls_end();
1321 R != REnd; ++R) {
Gabor Greif13a8aff2010-08-28 15:42:30 +00001322 if (*R == Function)
1323 continue;
Gabor Greifab297ac2010-08-30 21:10:05 +00001324 switch (R->getFriendObjectKind()) {
1325 case Decl::FOK_None:
Richard Smith80ad52f2013-01-02 11:42:31 +00001326 if (!SemaRef.getLangOpts().CPlusPlus11 &&
Richard Smith53e53512011-10-19 00:54:10 +00001327 !queuedInstantiation && R->isUsed(false)) {
Gabor Greifab297ac2010-08-30 21:10:05 +00001328 if (MemberSpecializationInfo *MSInfo
1329 = Function->getMemberSpecializationInfo()) {
1330 if (MSInfo->getPointOfInstantiation().isInvalid()) {
1331 SourceLocation Loc = R->getLocation(); // FIXME
1332 MSInfo->setPointOfInstantiation(Loc);
1333 SemaRef.PendingLocalImplicitInstantiations.push_back(
1334 std::make_pair(Function, Loc));
1335 queuedInstantiation = true;
1336 }
1337 }
1338 }
1339 break;
1340 default:
Douglas Gregor238058c2010-05-18 05:45:02 +00001341 if (const FunctionDecl *RPattern
Gabor Greif6a557d82010-08-28 15:46:56 +00001342 = R->getTemplateInstantiationPattern())
Sean Hunt10620eb2011-05-06 20:44:56 +00001343 if (RPattern->isDefined(RPattern)) {
Richard Smith53e53512011-10-19 00:54:10 +00001344 SemaRef.Diag(Function->getLocation(),
Richard Smith80ad52f2013-01-02 11:42:31 +00001345 SemaRef.getLangOpts().CPlusPlus11 ?
Richard Smith53e53512011-10-19 00:54:10 +00001346 diag::warn_cxx98_compat_friend_redefinition :
1347 diag::err_redefinition)
Douglas Gregor238058c2010-05-18 05:45:02 +00001348 << Function->getDeclName();
Gabor Greif6a557d82010-08-28 15:46:56 +00001349 SemaRef.Diag(R->getLocation(), diag::note_previous_definition);
Richard Smith80ad52f2013-01-02 11:42:31 +00001350 if (!SemaRef.getLangOpts().CPlusPlus11)
Richard Smith53e53512011-10-19 00:54:10 +00001351 Function->setInvalidDecl();
Douglas Gregor238058c2010-05-18 05:45:02 +00001352 break;
1353 }
1354 }
1355 }
1356 }
Douglas Gregora735b202009-10-13 14:39:41 +00001357 }
1358
John McCall76d32642010-04-24 01:30:58 +00001359 if (Function->isOverloadedOperator() && !DC->isRecord() &&
1360 PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
1361 PrincipalDecl->setNonMemberOperator();
1362
Sean Hunteb88ae52011-05-23 21:07:59 +00001363 assert(!D->isDefaulted() && "only methods should be defaulted");
Douglas Gregore53060f2009-06-25 22:08:12 +00001364 return Function;
1365}
1366
Douglas Gregord60e1052009-08-27 16:57:43 +00001367Decl *
1368TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
Francois Pichetaf0f4d02011-08-14 03:52:19 +00001369 TemplateParameterList *TemplateParams,
1370 bool IsClassScopeSpecialization) {
Douglas Gregor6b906862009-08-21 00:16:32 +00001371 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
Douglas Gregord60e1052009-08-27 16:57:43 +00001372 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +00001373 // We are creating a function template specialization from a function
1374 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +00001375 // specialization for this particular set of template arguments.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001376 std::pair<const TemplateArgument *, unsigned> Innermost
Douglas Gregor24bae922010-07-08 18:37:38 +00001377 = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +00001378
Douglas Gregor1e1e9722012-03-28 14:34:23 +00001379 void *InsertPos = 0;
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001380 FunctionDecl *SpecFunc
1381 = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
1382 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001383
Douglas Gregor6b906862009-08-21 00:16:32 +00001384 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001385 if (SpecFunc)
1386 return SpecFunc;
Douglas Gregor6b906862009-08-21 00:16:32 +00001387 }
1388
John McCallb0cb0222010-03-27 05:57:59 +00001389 bool isFriend;
1390 if (FunctionTemplate)
1391 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1392 else
1393 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1394
Douglas Gregor79c22782010-01-16 20:21:20 +00001395 bool MergeWithParentScope = (TemplateParams != 0) ||
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001396 !(isa<Decl>(Owner) &&
Douglas Gregor79c22782010-01-16 20:21:20 +00001397 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
John McCall2a7fb272010-08-25 05:32:35 +00001398 LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Douglas Gregor48dd19b2009-05-14 21:44:34 +00001399
John McCall4eab39f2010-10-19 02:26:41 +00001400 // Instantiate enclosing template arguments for friends.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001401 SmallVector<TemplateParameterList *, 4> TempParamLists;
John McCall4eab39f2010-10-19 02:26:41 +00001402 unsigned NumTempParamLists = 0;
1403 if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) {
1404 TempParamLists.set_size(NumTempParamLists);
1405 for (unsigned I = 0; I != NumTempParamLists; ++I) {
1406 TemplateParameterList *TempParams = D->getTemplateParameterList(I);
1407 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1408 if (!InstParams)
1409 return NULL;
1410 TempParamLists[I] = InstParams;
1411 }
1412 }
1413
Chris Lattner5f9e2722011-07-23 10:55:15 +00001414 SmallVector<ParmVarDecl *, 4> Params;
Benjamin Kramerdc370c12012-01-20 14:42:32 +00001415 TypeSourceInfo *TInfo = SubstFunctionType(D, Params);
John McCall21ef0fa2010-03-11 09:03:00 +00001416 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001417 return 0;
Douglas Gregor71074fd2012-09-13 21:56:43 +00001418 QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001419
Abramo Bagnara723df242010-12-14 22:11:44 +00001420 // \brief If the type of this function, after ignoring parentheses,
1421 // is not *directly* a function type, then we're instantiating a function
1422 // that was declared via a typedef, e.g.,
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001423 //
1424 // typedef int functype(int, int);
1425 // functype func;
1426 //
1427 // In this case, we'll just go instantiate the ParmVarDecls that we
1428 // synthesized in the method declaration.
Abramo Bagnara723df242010-12-14 22:11:44 +00001429 if (!isa<FunctionProtoType>(T.IgnoreParens())) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001430 assert(!Params.size() && "Instantiating type could not yield parameters");
Chris Lattner5f9e2722011-07-23 10:55:15 +00001431 SmallVector<QualType, 4> ParamTypes;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001432 if (SemaRef.SubstParmTypes(D->getLocation(), D->param_begin(),
1433 D->getNumParams(), TemplateArgs, ParamTypes,
Douglas Gregor12c9c002011-01-07 16:43:16 +00001434 &Params))
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001435 return 0;
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001436 }
1437
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001438 NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
1439 if (QualifierLoc) {
1440 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
John McCallb0cb0222010-03-27 05:57:59 +00001441 TemplateArgs);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001442 if (!QualifierLoc)
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001443 return 0;
John McCallb0cb0222010-03-27 05:57:59 +00001444 }
1445
1446 DeclContext *DC = Owner;
1447 if (isFriend) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001448 if (QualifierLoc) {
John McCallb0cb0222010-03-27 05:57:59 +00001449 CXXScopeSpec SS;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001450 SS.Adopt(QualifierLoc);
John McCallb0cb0222010-03-27 05:57:59 +00001451 DC = SemaRef.computeDeclContext(SS);
John McCallc54d6882010-10-19 05:01:53 +00001452
1453 if (DC && SemaRef.RequireCompleteDeclContext(SS, DC))
1454 return 0;
John McCallb0cb0222010-03-27 05:57:59 +00001455 } else {
1456 DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1457 D->getDeclContext(),
1458 TemplateArgs);
1459 }
1460 if (!DC) return 0;
1461 }
1462
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001463 // Build the instantiated method declaration.
John McCallb0cb0222010-03-27 05:57:59 +00001464 CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
Douglas Gregordec06662009-08-21 18:42:58 +00001465 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001466
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001467 SourceLocation StartLoc = D->getInnerLocStart();
Abramo Bagnara25777432010-08-11 22:01:17 +00001468 DeclarationNameInfo NameInfo
1469 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001470 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001471 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001472 StartLoc, NameInfo, T, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001473 Constructor->isExplicit(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001474 Constructor->isInlineSpecified(),
Richard Smith86c3ae42012-02-13 03:54:03 +00001475 false, Constructor->isConstexpr());
Douglas Gregor17e32f32009-08-21 22:43:28 +00001476 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001477 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001478 StartLoc, NameInfo, T, TInfo,
Abramo Bagnara25777432010-08-11 22:01:17 +00001479 Destructor->isInlineSpecified(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001480 false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001481 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001482 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001483 StartLoc, NameInfo, T, TInfo,
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001484 Conversion->isInlineSpecified(),
Douglas Gregorf5251602011-03-08 17:10:18 +00001485 Conversion->isExplicit(),
Richard Smith86c3ae42012-02-13 03:54:03 +00001486 Conversion->isConstexpr(),
Richard Smith9f569cc2011-10-01 02:31:28 +00001487 Conversion->getLocEnd());
Douglas Gregordec06662009-08-21 18:42:58 +00001488 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00001489 Method = CXXMethodDecl::Create(SemaRef.Context, Record,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001490 StartLoc, NameInfo, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001491 D->isStatic(),
1492 D->getStorageClassAsWritten(),
Douglas Gregorf5251602011-03-08 17:10:18 +00001493 D->isInlineSpecified(),
Richard Smith86c3ae42012-02-13 03:54:03 +00001494 D->isConstexpr(), D->getLocEnd());
Douglas Gregordec06662009-08-21 18:42:58 +00001495 }
Douglas Gregor6b906862009-08-21 00:16:32 +00001496
Richard Smithd4497dd2013-01-25 00:08:28 +00001497 if (D->isInlined())
1498 Method->setImplicitlyInline();
1499
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001500 if (QualifierLoc)
1501 Method->setQualifierInfo(QualifierLoc);
John McCallb6217662010-03-15 10:12:16 +00001502
Douglas Gregord60e1052009-08-27 16:57:43 +00001503 if (TemplateParams) {
1504 // Our resulting instantiation is actually a function template, since we
1505 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +00001506 //
Douglas Gregord60e1052009-08-27 16:57:43 +00001507 // template<typename T>
1508 // struct X {
1509 // template<typename U> void f(T, U);
1510 // };
1511 //
1512 // X<int> x;
1513 //
1514 // We are instantiating the member template "f" within X<int>, which means
1515 // substituting int for T, but leaving "f" as a member function template.
1516 // Build the function template itself.
1517 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1518 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001519 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +00001520 TemplateParams, Method);
John McCallb0cb0222010-03-27 05:57:59 +00001521 if (isFriend) {
1522 FunctionTemplate->setLexicalDeclContext(Owner);
1523 FunctionTemplate->setObjectOfFriendDecl(true);
1524 } else if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +00001525 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +00001526 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001527 } else if (FunctionTemplate) {
1528 // Record this function template specialization.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001529 std::pair<const TemplateArgument *, unsigned> Innermost
Douglas Gregor24bae922010-07-08 18:37:38 +00001530 = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001531 Method->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001532 TemplateArgumentList::CreateCopy(SemaRef.Context,
1533 Innermost.first,
1534 Innermost.second),
Douglas Gregor1e1e9722012-03-28 14:34:23 +00001535 /*InsertPos=*/0);
John McCallb0cb0222010-03-27 05:57:59 +00001536 } else if (!isFriend) {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001537 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +00001538 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001539 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001540
Mike Stump1eb44332009-09-09 15:08:12 +00001541 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +00001542 // out-of-line, the instantiation will have the same lexical
1543 // context (which will be a namespace scope) as the template.
John McCallb0cb0222010-03-27 05:57:59 +00001544 if (isFriend) {
John McCall4eab39f2010-10-19 02:26:41 +00001545 if (NumTempParamLists)
1546 Method->setTemplateParameterListsInfo(SemaRef.Context,
1547 NumTempParamLists,
1548 TempParamLists.data());
1549
John McCallb0cb0222010-03-27 05:57:59 +00001550 Method->setLexicalDeclContext(Owner);
1551 Method->setObjectOfFriendDecl(true);
1552 } else if (D->isOutOfLine())
Douglas Gregor7caa6822009-07-24 20:34:43 +00001553 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001554
Douglas Gregor5545e162009-03-24 00:38:23 +00001555 // Attach the parameters
1556 for (unsigned P = 0; P < Params.size(); ++P)
1557 Params[P]->setOwningFunction(Method);
David Blaikie4278c652011-09-21 18:16:56 +00001558 Method->setParams(Params);
Douglas Gregor5545e162009-03-24 00:38:23 +00001559
1560 if (InitMethodInstantiation(Method, D))
1561 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001562
Abramo Bagnara25777432010-08-11 22:01:17 +00001563 LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
1564 Sema::ForRedeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001565
John McCallb0cb0222010-03-27 05:57:59 +00001566 if (!FunctionTemplate || TemplateParams || isFriend) {
1567 SemaRef.LookupQualifiedName(Previous, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001568
Douglas Gregordec06662009-08-21 18:42:58 +00001569 // In C++, the previous declaration we find might be a tag type
1570 // (class or enum). In this case, the new declaration will hide the
1571 // tag type. Note that this does does not apply if we're declaring a
1572 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001573 if (Previous.isSingleTagDecl())
1574 Previous.clear();
Douglas Gregordec06662009-08-21 18:42:58 +00001575 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001576
Francois Pichetaf0f4d02011-08-14 03:52:19 +00001577 if (!IsClassScopeSpecialization)
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00001578 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001579
Douglas Gregor4ba31362009-12-01 17:24:26 +00001580 if (D->isPure())
1581 SemaRef.CheckPureMethod(Method, SourceRange());
1582
John McCall1f2e1a92012-08-10 03:15:35 +00001583 // Propagate access. For a non-friend declaration, the access is
1584 // whatever we're propagating from. For a friend, it should be the
1585 // previous declaration we just found.
1586 if (isFriend && Method->getPreviousDecl())
1587 Method->setAccess(Method->getPreviousDecl()->getAccess());
1588 else
1589 Method->setAccess(D->getAccess());
1590 if (FunctionTemplate)
1591 FunctionTemplate->setAccess(Method->getAccess());
John McCall46460a62010-01-20 21:53:11 +00001592
Anders Carlsson9eefa222011-01-20 06:52:44 +00001593 SemaRef.CheckOverrideControl(Method);
1594
Eli Friedman3bc45152011-11-15 22:39:08 +00001595 // If a function is defined as defaulted or deleted, mark it as such now.
Richard Smithac713512012-12-08 02:53:02 +00001596 if (D->isExplicitlyDefaulted())
1597 SemaRef.SetDeclDefaulted(Method, Method->getLocation());
Eli Friedman3bc45152011-11-15 22:39:08 +00001598 if (D->isDeletedAsWritten())
Richard Smithac713512012-12-08 02:53:02 +00001599 SemaRef.SetDeclDeleted(Method, Method->getLocation());
Eli Friedman3bc45152011-11-15 22:39:08 +00001600
John McCall1f2e1a92012-08-10 03:15:35 +00001601 // If there's a function template, let our caller handle it.
John McCallb0cb0222010-03-27 05:57:59 +00001602 if (FunctionTemplate) {
John McCall1f2e1a92012-08-10 03:15:35 +00001603 // do nothing
1604
1605 // Don't hide a (potentially) valid declaration with an invalid one.
John McCallb0cb0222010-03-27 05:57:59 +00001606 } else if (Method->isInvalidDecl() && !Previous.empty()) {
John McCall1f2e1a92012-08-10 03:15:35 +00001607 // do nothing
1608
1609 // Otherwise, check access to friends and make them visible.
1610 } else if (isFriend) {
1611 // We only need to re-check access for methods which we didn't
1612 // manage to match during parsing.
1613 if (!D->getPreviousDecl())
1614 SemaRef.CheckFriendAccess(Method);
1615
1616 Record->makeDeclVisibleInContext(Method);
1617
1618 // Otherwise, add the declaration. We don't need to do this for
1619 // class-scope specializations because we'll have matched them with
1620 // the appropriate template.
1621 } else if (!IsClassScopeSpecialization) {
1622 Owner->addDecl(Method);
John McCallb0cb0222010-03-27 05:57:59 +00001623 }
Sean Hunteb88ae52011-05-23 21:07:59 +00001624
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001625 return Method;
1626}
1627
Douglas Gregor615c5d42009-03-24 16:43:20 +00001628Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +00001629 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +00001630}
1631
Douglas Gregor03b2b072009-03-24 00:15:49 +00001632Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001633 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +00001634}
1635
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001636Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001637 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001638}
1639
Douglas Gregor6477b692009-03-25 15:04:13 +00001640ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
John McCallfb44de92011-05-01 22:35:37 +00001641 return SemaRef.SubstParmVarDecl(D, TemplateArgs, /*indexAdjustment*/ 0,
David Blaikiedc84cd52013-02-20 22:23:23 +00001642 Optional<unsigned>(),
Douglas Gregord1bb4ae2012-01-25 16:15:54 +00001643 /*ExpectParameterPack=*/false);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001644}
1645
John McCalle29ba202009-08-20 01:44:21 +00001646Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1647 TemplateTypeParmDecl *D) {
1648 // TODO: don't always clone when decls are refcounted.
Chandler Carruth4fb86f82011-05-01 00:51:33 +00001649 assert(D->getTypeForDecl()->isTemplateTypeParmType());
Mike Stump1eb44332009-09-09 15:08:12 +00001650
John McCalle29ba202009-08-20 01:44:21 +00001651 TemplateTypeParmDecl *Inst =
Abramo Bagnara344577e2011-03-06 15:48:19 +00001652 TemplateTypeParmDecl::Create(SemaRef.Context, Owner,
1653 D->getLocStart(), D->getLocation(),
Chandler Carruth4fb86f82011-05-01 00:51:33 +00001654 D->getDepth() - TemplateArgs.getNumLevels(),
1655 D->getIndex(), D->getIdentifier(),
John McCalle29ba202009-08-20 01:44:21 +00001656 D->wasDeclaredWithTypename(),
1657 D->isParameterPack());
Douglas Gregor9a299e02011-03-04 17:52:15 +00001658 Inst->setAccess(AS_public);
John McCalle29ba202009-08-20 01:44:21 +00001659
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001660 if (D->hasDefaultArgument())
1661 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
1662
1663 // Introduce this template parameter's instantiation into the instantiation
Douglas Gregor550d9b22009-10-31 17:21:17 +00001664 // scope.
1665 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001666
John McCalle29ba202009-08-20 01:44:21 +00001667 return Inst;
1668}
1669
Douglas Gregor33642df2009-10-23 23:25:44 +00001670Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1671 NonTypeTemplateParmDecl *D) {
1672 // Substitute into the type of the non-type template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001673 TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc();
Chris Lattner5f9e2722011-07-23 10:55:15 +00001674 SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten;
1675 SmallVector<QualType, 4> ExpandedParameterPackTypes;
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001676 bool IsExpandedParameterPack = false;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001677 TypeSourceInfo *DI;
Douglas Gregor33642df2009-10-23 23:25:44 +00001678 QualType T;
Douglas Gregor33642df2009-10-23 23:25:44 +00001679 bool Invalid = false;
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001680
1681 if (D->isExpandedParameterPack()) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001682 // The non-type template parameter pack is an already-expanded pack
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001683 // expansion of types. Substitute into each of the expanded types.
1684 ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes());
1685 ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes());
1686 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
1687 TypeSourceInfo *NewDI =SemaRef.SubstType(D->getExpansionTypeSourceInfo(I),
1688 TemplateArgs,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001689 D->getLocation(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001690 D->getDeclName());
1691 if (!NewDI)
1692 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001693
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001694 ExpandedParameterPackTypesAsWritten.push_back(NewDI);
1695 QualType NewT =SemaRef.CheckNonTypeTemplateParameterType(NewDI->getType(),
1696 D->getLocation());
1697 if (NewT.isNull())
1698 return 0;
1699 ExpandedParameterPackTypes.push_back(NewT);
1700 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001701
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001702 IsExpandedParameterPack = true;
1703 DI = D->getTypeSourceInfo();
1704 T = DI->getType();
Richard Smith6964b3f2012-09-07 02:06:42 +00001705 } else if (D->isPackExpansion()) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001706 // The non-type template parameter pack's type is a pack expansion of types.
1707 // Determine whether we need to expand this parameter pack into separate
1708 // types.
David Blaikie39e6ab42013-02-18 22:06:02 +00001709 PackExpansionTypeLoc Expansion = TL.castAs<PackExpansionTypeLoc>();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001710 TypeLoc Pattern = Expansion.getPatternLoc();
Chris Lattner5f9e2722011-07-23 10:55:15 +00001711 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001712 SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001713
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001714 // Determine whether the set of unexpanded parameter packs can and should
1715 // be expanded.
1716 bool Expand = true;
1717 bool RetainExpansion = false;
David Blaikiedc84cd52013-02-20 22:23:23 +00001718 Optional<unsigned> OrigNumExpansions
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001719 = Expansion.getTypePtr()->getNumExpansions();
David Blaikiedc84cd52013-02-20 22:23:23 +00001720 Optional<unsigned> NumExpansions = OrigNumExpansions;
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001721 if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(),
1722 Pattern.getSourceRange(),
David Blaikiea71f9d02011-09-22 02:34:54 +00001723 Unexpanded,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001724 TemplateArgs,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001725 Expand, RetainExpansion,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001726 NumExpansions))
1727 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001728
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001729 if (Expand) {
1730 for (unsigned I = 0; I != *NumExpansions; ++I) {
1731 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
1732 TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001733 D->getLocation(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001734 D->getDeclName());
1735 if (!NewDI)
1736 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001737
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001738 ExpandedParameterPackTypesAsWritten.push_back(NewDI);
1739 QualType NewT = SemaRef.CheckNonTypeTemplateParameterType(
1740 NewDI->getType(),
1741 D->getLocation());
1742 if (NewT.isNull())
1743 return 0;
1744 ExpandedParameterPackTypes.push_back(NewT);
1745 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001746
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001747 // Note that we have an expanded parameter pack. The "type" of this
1748 // expanded parameter pack is the original expansion type, but callers
1749 // will end up using the expanded parameter pack types for type-checking.
1750 IsExpandedParameterPack = true;
1751 DI = D->getTypeSourceInfo();
1752 T = DI->getType();
1753 } else {
1754 // We cannot fully expand the pack expansion now, so substitute into the
1755 // pattern and create a new pack expansion type.
1756 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
1757 TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001758 D->getLocation(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001759 D->getDeclName());
1760 if (!NewPattern)
1761 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001762
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001763 DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(),
1764 NumExpansions);
1765 if (!DI)
1766 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001767
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001768 T = DI->getType();
1769 }
1770 } else {
1771 // Simple case: substitution into a parameter that is not a parameter pack.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001772 DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001773 D->getLocation(), D->getDeclName());
1774 if (!DI)
1775 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001776
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001777 // Check that this type is acceptable for a non-type template parameter.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001778 T = SemaRef.CheckNonTypeTemplateParameterType(DI->getType(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001779 D->getLocation());
1780 if (T.isNull()) {
1781 T = SemaRef.Context.IntTy;
1782 Invalid = true;
1783 }
Douglas Gregor33642df2009-10-23 23:25:44 +00001784 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001785
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001786 NonTypeTemplateParmDecl *Param;
1787 if (IsExpandedParameterPack)
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001788 Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001789 D->getInnerLocStart(),
1790 D->getLocation(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001791 D->getDepth() - TemplateArgs.getNumLevels(),
1792 D->getPosition(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001793 D->getIdentifier(), T,
1794 DI,
1795 ExpandedParameterPackTypes.data(),
1796 ExpandedParameterPackTypes.size(),
1797 ExpandedParameterPackTypesAsWritten.data());
1798 else
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001799 Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001800 D->getInnerLocStart(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001801 D->getLocation(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001802 D->getDepth() - TemplateArgs.getNumLevels(),
1803 D->getPosition(),
1804 D->getIdentifier(), T,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001805 D->isParameterPack(), DI);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001806
Douglas Gregor9a299e02011-03-04 17:52:15 +00001807 Param->setAccess(AS_public);
Douglas Gregor33642df2009-10-23 23:25:44 +00001808 if (Invalid)
1809 Param->setInvalidDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001810
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001811 Param->setDefaultArgument(D->getDefaultArgument(), false);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001812
1813 // Introduce this template parameter's instantiation into the instantiation
Douglas Gregor550d9b22009-10-31 17:21:17 +00001814 // scope.
1815 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001816 return Param;
1817}
1818
Richard Smith6964b3f2012-09-07 02:06:42 +00001819static void collectUnexpandedParameterPacks(
1820 Sema &S,
1821 TemplateParameterList *Params,
1822 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
1823 for (TemplateParameterList::const_iterator I = Params->begin(),
1824 E = Params->end(); I != E; ++I) {
1825 if ((*I)->isTemplateParameterPack())
1826 continue;
1827 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*I))
1828 S.collectUnexpandedParameterPacks(NTTP->getTypeSourceInfo()->getTypeLoc(),
1829 Unexpanded);
1830 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(*I))
1831 collectUnexpandedParameterPacks(S, TTP->getTemplateParameters(),
1832 Unexpanded);
1833 }
1834}
1835
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001836Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001837TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1838 TemplateTemplateParmDecl *D) {
1839 // Instantiate the template parameter list of the template template parameter.
1840 TemplateParameterList *TempParams = D->getTemplateParameters();
1841 TemplateParameterList *InstParams;
Richard Smith6964b3f2012-09-07 02:06:42 +00001842 SmallVector<TemplateParameterList*, 8> ExpandedParams;
1843
1844 bool IsExpandedParameterPack = false;
1845
1846 if (D->isExpandedParameterPack()) {
1847 // The template template parameter pack is an already-expanded pack
1848 // expansion of template parameters. Substitute into each of the expanded
1849 // parameters.
1850 ExpandedParams.reserve(D->getNumExpansionTemplateParameters());
1851 for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
1852 I != N; ++I) {
1853 LocalInstantiationScope Scope(SemaRef);
1854 TemplateParameterList *Expansion =
1855 SubstTemplateParams(D->getExpansionTemplateParameters(I));
1856 if (!Expansion)
1857 return 0;
1858 ExpandedParams.push_back(Expansion);
1859 }
1860
1861 IsExpandedParameterPack = true;
1862 InstParams = TempParams;
1863 } else if (D->isPackExpansion()) {
1864 // The template template parameter pack expands to a pack of template
1865 // template parameters. Determine whether we need to expand this parameter
1866 // pack into separate parameters.
1867 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
1868 collectUnexpandedParameterPacks(SemaRef, D->getTemplateParameters(),
1869 Unexpanded);
1870
1871 // Determine whether the set of unexpanded parameter packs can and should
1872 // be expanded.
1873 bool Expand = true;
1874 bool RetainExpansion = false;
David Blaikiedc84cd52013-02-20 22:23:23 +00001875 Optional<unsigned> NumExpansions;
Richard Smith6964b3f2012-09-07 02:06:42 +00001876 if (SemaRef.CheckParameterPacksForExpansion(D->getLocation(),
1877 TempParams->getSourceRange(),
1878 Unexpanded,
1879 TemplateArgs,
1880 Expand, RetainExpansion,
1881 NumExpansions))
1882 return 0;
1883
1884 if (Expand) {
1885 for (unsigned I = 0; I != *NumExpansions; ++I) {
1886 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
1887 LocalInstantiationScope Scope(SemaRef);
1888 TemplateParameterList *Expansion = SubstTemplateParams(TempParams);
1889 if (!Expansion)
1890 return 0;
1891 ExpandedParams.push_back(Expansion);
1892 }
1893
1894 // Note that we have an expanded parameter pack. The "type" of this
1895 // expanded parameter pack is the original expansion type, but callers
1896 // will end up using the expanded parameter pack types for type-checking.
1897 IsExpandedParameterPack = true;
1898 InstParams = TempParams;
1899 } else {
1900 // We cannot fully expand the pack expansion now, so just substitute
1901 // into the pattern.
1902 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
1903
1904 LocalInstantiationScope Scope(SemaRef);
1905 InstParams = SubstTemplateParams(TempParams);
1906 if (!InstParams)
1907 return 0;
1908 }
1909 } else {
Douglas Gregor9106ef72009-11-11 16:58:32 +00001910 // Perform the actual substitution of template parameters within a new,
1911 // local instantiation scope.
John McCall2a7fb272010-08-25 05:32:35 +00001912 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor9106ef72009-11-11 16:58:32 +00001913 InstParams = SubstTemplateParams(TempParams);
1914 if (!InstParams)
Richard Smith6964b3f2012-09-07 02:06:42 +00001915 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001916 }
1917
Douglas Gregor9106ef72009-11-11 16:58:32 +00001918 // Build the template template parameter.
Richard Smith6964b3f2012-09-07 02:06:42 +00001919 TemplateTemplateParmDecl *Param;
1920 if (IsExpandedParameterPack)
1921 Param = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner,
1922 D->getLocation(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001923 D->getDepth() - TemplateArgs.getNumLevels(),
Richard Smith6964b3f2012-09-07 02:06:42 +00001924 D->getPosition(),
1925 D->getIdentifier(), InstParams,
1926 ExpandedParams);
1927 else
1928 Param = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner,
1929 D->getLocation(),
1930 D->getDepth() - TemplateArgs.getNumLevels(),
1931 D->getPosition(),
1932 D->isParameterPack(),
1933 D->getIdentifier(), InstParams);
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001934 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor9a299e02011-03-04 17:52:15 +00001935 Param->setAccess(AS_public);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001936
1937 // Introduce this template parameter's instantiation into the instantiation
Douglas Gregor9106ef72009-11-11 16:58:32 +00001938 // scope.
1939 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001940
Douglas Gregor9106ef72009-11-11 16:58:32 +00001941 return Param;
1942}
1943
Douglas Gregor48c32a72009-11-17 06:07:40 +00001944Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Douglas Gregordb992412011-02-25 16:33:46 +00001945 // Using directives are never dependent (and never contain any types or
1946 // expressions), so they require no explicit instantiation work.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001947
Douglas Gregor48c32a72009-11-17 06:07:40 +00001948 UsingDirectiveDecl *Inst
1949 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001950 D->getNamespaceKeyLocation(),
Douglas Gregordb992412011-02-25 16:33:46 +00001951 D->getQualifierLoc(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001952 D->getIdentLocation(),
1953 D->getNominatedNamespace(),
Douglas Gregor48c32a72009-11-17 06:07:40 +00001954 D->getCommonAncestor());
Abramo Bagnara536afbe2012-09-05 09:55:10 +00001955
1956 // Add the using directive to its declaration context
1957 // only if this is not a function or method.
1958 if (!Owner->isFunctionOrMethod())
1959 Owner->addDecl(Inst);
1960
Douglas Gregor48c32a72009-11-17 06:07:40 +00001961 return Inst;
1962}
1963
John McCalled976492009-12-04 22:46:56 +00001964Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
Douglas Gregor1b398202010-09-29 17:58:28 +00001965
1966 // The nested name specifier may be dependent, for example
1967 // template <typename T> struct t {
1968 // struct s1 { T f1(); };
1969 // struct s2 : s1 { using s1::f1; };
1970 // };
1971 // template struct t<int>;
1972 // Here, in using s1::f1, s1 refers to t<T>::s1;
1973 // we need to substitute for t<int>::s1.
Douglas Gregor5149f372011-02-25 15:54:31 +00001974 NestedNameSpecifierLoc QualifierLoc
1975 = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
1976 TemplateArgs);
1977 if (!QualifierLoc)
Douglas Gregordc355712011-02-25 00:36:19 +00001978 return 0;
Douglas Gregor1b398202010-09-29 17:58:28 +00001979
1980 // The name info is non-dependent, so no transformation
1981 // is required.
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001982 DeclarationNameInfo NameInfo = D->getNameInfo();
John McCalled976492009-12-04 22:46:56 +00001983
John McCall9f54ad42009-12-10 09:41:52 +00001984 // We only need to do redeclaration lookups if we're in a class
1985 // scope (in fact, it's not really even possible in non-class
1986 // scopes).
1987 bool CheckRedeclaration = Owner->isRecord();
1988
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001989 LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
1990 Sema::ForRedeclaration);
John McCall9f54ad42009-12-10 09:41:52 +00001991
John McCalled976492009-12-04 22:46:56 +00001992 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
John McCalled976492009-12-04 22:46:56 +00001993 D->getUsingLocation(),
Douglas Gregor5149f372011-02-25 15:54:31 +00001994 QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001995 NameInfo,
John McCalled976492009-12-04 22:46:56 +00001996 D->isTypeName());
1997
Douglas Gregor5149f372011-02-25 15:54:31 +00001998 CXXScopeSpec SS;
1999 SS.Adopt(QualifierLoc);
John McCall9f54ad42009-12-10 09:41:52 +00002000 if (CheckRedeclaration) {
2001 Prev.setHideTags(false);
2002 SemaRef.LookupQualifiedName(Prev, Owner);
2003
2004 // Check for invalid redeclarations.
2005 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
2006 D->isTypeName(), SS,
2007 D->getLocation(), Prev))
2008 NewUD->setInvalidDecl();
2009
2010 }
2011
2012 if (!NewUD->isInvalidDecl() &&
2013 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
John McCalled976492009-12-04 22:46:56 +00002014 D->getLocation()))
2015 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00002016
John McCalled976492009-12-04 22:46:56 +00002017 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
2018 NewUD->setAccess(D->getAccess());
2019 Owner->addDecl(NewUD);
2020
John McCall9f54ad42009-12-10 09:41:52 +00002021 // Don't process the shadow decls for an invalid decl.
2022 if (NewUD->isInvalidDecl())
2023 return NewUD;
2024
Richard Smithc5a89a12012-04-02 01:30:27 +00002025 if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) {
2026 if (SemaRef.CheckInheritingConstructorUsingDecl(NewUD))
2027 NewUD->setInvalidDecl();
2028 return NewUD;
2029 }
2030
John McCall323c3102009-12-22 22:26:37 +00002031 bool isFunctionScope = Owner->isFunctionOrMethod();
2032
John McCall9f54ad42009-12-10 09:41:52 +00002033 // Process the shadow decls.
2034 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
2035 I != E; ++I) {
2036 UsingShadowDecl *Shadow = *I;
2037 NamedDecl *InstTarget =
Douglas Gregorb7107222011-03-04 19:46:35 +00002038 cast_or_null<NamedDecl>(SemaRef.FindInstantiatedDecl(
2039 Shadow->getLocation(),
2040 Shadow->getTargetDecl(),
2041 TemplateArgs));
2042 if (!InstTarget)
2043 return 0;
John McCall9f54ad42009-12-10 09:41:52 +00002044
2045 if (CheckRedeclaration &&
2046 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
2047 continue;
2048
2049 UsingShadowDecl *InstShadow
2050 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
2051 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
John McCall323c3102009-12-22 22:26:37 +00002052
2053 if (isFunctionScope)
2054 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
John McCall9f54ad42009-12-10 09:41:52 +00002055 }
John McCalled976492009-12-04 22:46:56 +00002056
2057 return NewUD;
2058}
2059
2060Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00002061 // Ignore these; we handle them in bulk when processing the UsingDecl.
2062 return 0;
John McCalled976492009-12-04 22:46:56 +00002063}
2064
John McCall7ba107a2009-11-18 02:36:19 +00002065Decl * TemplateDeclInstantiator
2066 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Douglas Gregor5149f372011-02-25 15:54:31 +00002067 NestedNameSpecifierLoc QualifierLoc
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002068 = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
Douglas Gregor5149f372011-02-25 15:54:31 +00002069 TemplateArgs);
2070 if (!QualifierLoc)
Anders Carlsson0dde18e2009-08-28 15:18:15 +00002071 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002072
Anders Carlsson0dde18e2009-08-28 15:18:15 +00002073 CXXScopeSpec SS;
Douglas Gregor5149f372011-02-25 15:54:31 +00002074 SS.Adopt(QualifierLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002075
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002076 // Since NameInfo refers to a typename, it cannot be a C++ special name.
Benjamin Krameraccaf192012-11-14 15:08:31 +00002077 // Hence, no transformation is required for it.
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002078 DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00002079 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00002080 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002081 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00002082 /*instantiation*/ true,
2083 /*typename*/ true, D->getTypenameLoc());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00002084 if (UD)
John McCalled976492009-12-04 22:46:56 +00002085 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
2086
John McCall7ba107a2009-11-18 02:36:19 +00002087 return UD;
2088}
2089
2090Decl * TemplateDeclInstantiator
2091 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Douglas Gregor5149f372011-02-25 15:54:31 +00002092 NestedNameSpecifierLoc QualifierLoc
2093 = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), TemplateArgs);
2094 if (!QualifierLoc)
John McCall7ba107a2009-11-18 02:36:19 +00002095 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002096
John McCall7ba107a2009-11-18 02:36:19 +00002097 CXXScopeSpec SS;
Douglas Gregor5149f372011-02-25 15:54:31 +00002098 SS.Adopt(QualifierLoc);
John McCall7ba107a2009-11-18 02:36:19 +00002099
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002100 DeclarationNameInfo NameInfo
2101 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
2102
John McCall7ba107a2009-11-18 02:36:19 +00002103 NamedDecl *UD =
2104 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002105 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00002106 /*instantiation*/ true,
2107 /*typename*/ false, SourceLocation());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00002108 if (UD)
John McCalled976492009-12-04 22:46:56 +00002109 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
2110
Anders Carlsson0d8df782009-08-29 19:37:28 +00002111 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00002112}
2113
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002114
2115Decl *TemplateDeclInstantiator::VisitClassScopeFunctionSpecializationDecl(
2116 ClassScopeFunctionSpecializationDecl *Decl) {
2117 CXXMethodDecl *OldFD = Decl->getSpecialization();
Nico Weber6b020092012-06-25 17:21:05 +00002118 CXXMethodDecl *NewFD = cast<CXXMethodDecl>(VisitCXXMethodDecl(OldFD,
2119 0, true));
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002120
2121 LookupResult Previous(SemaRef, NewFD->getNameInfo(), Sema::LookupOrdinaryName,
2122 Sema::ForRedeclaration);
2123
Nico Weber6b020092012-06-25 17:21:05 +00002124 TemplateArgumentListInfo TemplateArgs;
2125 TemplateArgumentListInfo* TemplateArgsPtr = 0;
2126 if (Decl->hasExplicitTemplateArgs()) {
2127 TemplateArgs = Decl->templateArgs();
2128 TemplateArgsPtr = &TemplateArgs;
2129 }
2130
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002131 SemaRef.LookupQualifiedName(Previous, SemaRef.CurContext);
Nico Weber6b020092012-06-25 17:21:05 +00002132 if (SemaRef.CheckFunctionTemplateSpecialization(NewFD, TemplateArgsPtr,
2133 Previous)) {
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002134 NewFD->setInvalidDecl();
2135 return NewFD;
2136 }
2137
2138 // Associate the specialization with the pattern.
2139 FunctionDecl *Specialization = cast<FunctionDecl>(Previous.getFoundDecl());
2140 assert(Specialization && "Class scope Specialization is null");
2141 SemaRef.Context.setClassScopeSpecializationPattern(Specialization, OldFD);
2142
2143 return NewFD;
2144}
2145
John McCallce3ff2b2009-08-25 22:02:44 +00002146Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00002147 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00002148 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor2fa98002010-02-16 19:28:15 +00002149 if (D->isInvalidDecl())
2150 return 0;
2151
Douglas Gregor8dbc2692009-03-17 21:15:40 +00002152 return Instantiator.Visit(D);
2153}
2154
John McCalle29ba202009-08-20 01:44:21 +00002155/// \brief Instantiates a nested template parameter list in the current
2156/// instantiation context.
2157///
2158/// \param L The parameter list to instantiate
2159///
2160/// \returns NULL if there was an error
2161TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00002162TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00002163 // Get errors for all the parameters before bailing out.
2164 bool Invalid = false;
2165
2166 unsigned N = L->size();
Chris Lattner5f9e2722011-07-23 10:55:15 +00002167 typedef SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00002168 ParamVector Params;
2169 Params.reserve(N);
2170 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
2171 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002172 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00002173 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00002174 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00002175 }
2176
2177 // Clean up if we had an error.
Douglas Gregorff331c12010-07-25 18:17:45 +00002178 if (Invalid)
John McCalle29ba202009-08-20 01:44:21 +00002179 return NULL;
John McCalle29ba202009-08-20 01:44:21 +00002180
2181 TemplateParameterList *InstL
2182 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
2183 L->getLAngleLoc(), &Params.front(), N,
2184 L->getRAngleLoc());
2185 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00002186}
John McCalle29ba202009-08-20 01:44:21 +00002187
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002188/// \brief Instantiate the declaration of a class template partial
Douglas Gregored9c0f92009-10-29 00:04:11 +00002189/// specialization.
2190///
2191/// \param ClassTemplate the (instantiated) class template that is partially
2192// specialized by the instantiation of \p PartialSpec.
2193///
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002194/// \param PartialSpec the (uninstantiated) class template partial
Douglas Gregored9c0f92009-10-29 00:04:11 +00002195/// specialization that we are instantiating.
2196///
Douglas Gregord65587f2010-11-10 19:44:59 +00002197/// \returns The instantiated partial specialization, if successful; otherwise,
2198/// NULL to indicate an error.
2199ClassTemplatePartialSpecializationDecl *
Douglas Gregored9c0f92009-10-29 00:04:11 +00002200TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
2201 ClassTemplateDecl *ClassTemplate,
2202 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00002203 // Create a local instantiation scope for this class template partial
2204 // specialization, which will contain the instantiations of the template
2205 // parameters.
John McCall2a7fb272010-08-25 05:32:35 +00002206 LocalInstantiationScope Scope(SemaRef);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002207
Douglas Gregored9c0f92009-10-29 00:04:11 +00002208 // Substitute into the template parameters of the class template partial
2209 // specialization.
2210 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
2211 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
2212 if (!InstParams)
Douglas Gregord65587f2010-11-10 19:44:59 +00002213 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002214
Douglas Gregored9c0f92009-10-29 00:04:11 +00002215 // Substitute into the template arguments of the class template partial
2216 // specialization.
John McCalld5532b62009-11-23 01:53:49 +00002217 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002218 if (SemaRef.Subst(PartialSpec->getTemplateArgsAsWritten(),
2219 PartialSpec->getNumTemplateArgsAsWritten(),
Douglas Gregore02e2622010-12-22 21:19:48 +00002220 InstTemplateArgs, TemplateArgs))
2221 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002222
Douglas Gregored9c0f92009-10-29 00:04:11 +00002223 // Check that the template argument list is well-formed for this
2224 // class template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002225 SmallVector<TemplateArgument, 4> Converted;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002226 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002227 PartialSpec->getLocation(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002228 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002229 false,
2230 Converted))
Douglas Gregord65587f2010-11-10 19:44:59 +00002231 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00002232
2233 // Figure out where to insert this class template partial specialization
2234 // in the member template's set of class template partial specializations.
Douglas Gregored9c0f92009-10-29 00:04:11 +00002235 void *InsertPos = 0;
2236 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00002237 = ClassTemplate->findPartialSpecialization(Converted.data(),
2238 Converted.size(), InsertPos);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002239
Douglas Gregored9c0f92009-10-29 00:04:11 +00002240 // Build the canonical type that describes the converted template
2241 // arguments of the class template partial specialization.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002242 QualType CanonType
Douglas Gregored9c0f92009-10-29 00:04:11 +00002243 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
Douglas Gregor910f8002010-11-07 23:05:16 +00002244 Converted.data(),
2245 Converted.size());
Douglas Gregored9c0f92009-10-29 00:04:11 +00002246
2247 // Build the fully-sugared type for this class template
2248 // specialization as the user wrote in the specialization
2249 // itself. This means that we'll pretty-print the type retrieved
2250 // from the specialization's declaration the way that the user
2251 // actually wrote the specialization, rather than formatting the
2252 // name based on the "canonical" representation used to store the
2253 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00002254 TypeSourceInfo *WrittenTy
2255 = SemaRef.Context.getTemplateSpecializationTypeInfo(
2256 TemplateName(ClassTemplate),
2257 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00002258 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002259 CanonType);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002260
Douglas Gregored9c0f92009-10-29 00:04:11 +00002261 if (PrevDecl) {
2262 // We've already seen a partial specialization with the same template
2263 // parameters and template arguments. This can happen, for example, when
2264 // substituting the outer template arguments ends up causing two
2265 // class template partial specializations of a member class template
2266 // to have identical forms, e.g.,
2267 //
2268 // template<typename T, typename U>
2269 // struct Outer {
2270 // template<typename X, typename Y> struct Inner;
2271 // template<typename Y> struct Inner<T, Y>;
2272 // template<typename Y> struct Inner<U, Y>;
2273 // };
2274 //
2275 // Outer<int, int> outer; // error: the partial specializations of Inner
2276 // // have the same signature.
2277 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
Douglas Gregord65587f2010-11-10 19:44:59 +00002278 << WrittenTy->getType();
Douglas Gregored9c0f92009-10-29 00:04:11 +00002279 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
2280 << SemaRef.Context.getTypeDeclType(PrevDecl);
Douglas Gregord65587f2010-11-10 19:44:59 +00002281 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00002282 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002283
2284
Douglas Gregored9c0f92009-10-29 00:04:11 +00002285 // Create the class template partial specialization declaration.
2286 ClassTemplatePartialSpecializationDecl *InstPartialSpec
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002287 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00002288 PartialSpec->getTagKind(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002289 Owner,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002290 PartialSpec->getLocStart(),
2291 PartialSpec->getLocation(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002292 InstParams,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002293 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00002294 Converted.data(),
2295 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00002296 InstTemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00002297 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00002298 0,
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00002299 ClassTemplate->getNextPartialSpecSequenceNumber());
John McCallb6217662010-03-15 10:12:16 +00002300 // Substitute the nested name specifier, if any.
2301 if (SubstQualifier(PartialSpec, InstPartialSpec))
2302 return 0;
2303
Douglas Gregored9c0f92009-10-29 00:04:11 +00002304 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00002305 InstPartialSpec->setTypeAsWritten(WrittenTy);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002306
Douglas Gregored9c0f92009-10-29 00:04:11 +00002307 // Add this partial specialization to the set of class template partial
2308 // specializations.
Douglas Gregor1e1e9722012-03-28 14:34:23 +00002309 ClassTemplate->AddPartialSpecialization(InstPartialSpec, /*InsertPos=*/0);
Douglas Gregord65587f2010-11-10 19:44:59 +00002310 return InstPartialSpec;
Douglas Gregored9c0f92009-10-29 00:04:11 +00002311}
2312
John McCall21ef0fa2010-03-11 09:03:00 +00002313TypeSourceInfo*
John McCallce3ff2b2009-08-25 22:02:44 +00002314TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002315 SmallVectorImpl<ParmVarDecl *> &Params) {
John McCall21ef0fa2010-03-11 09:03:00 +00002316 TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
2317 assert(OldTInfo && "substituting function without type source info");
2318 assert(Params.empty() && "parameter vector is non-empty at start");
Douglas Gregorcefc3af2012-04-16 07:05:22 +00002319
2320 CXXRecordDecl *ThisContext = 0;
2321 unsigned ThisTypeQuals = 0;
2322 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2323 ThisContext = Method->getParent();
2324 ThisTypeQuals = Method->getTypeQualifiers();
2325 }
2326
John McCall6cd3b9f2010-04-09 17:38:44 +00002327 TypeSourceInfo *NewTInfo
2328 = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
2329 D->getTypeSpecStartLoc(),
Douglas Gregorcefc3af2012-04-16 07:05:22 +00002330 D->getDeclName(),
2331 ThisContext, ThisTypeQuals);
John McCall21ef0fa2010-03-11 09:03:00 +00002332 if (!NewTInfo)
2333 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +00002334
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00002335 if (NewTInfo != OldTInfo) {
2336 // Get parameters from the new type info.
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00002337 TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
David Blaikie39e6ab42013-02-18 22:06:02 +00002338 if (FunctionProtoTypeLoc OldProtoLoc =
2339 OldTL.getAs<FunctionProtoTypeLoc>()) {
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00002340 TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens();
David Blaikie39e6ab42013-02-18 22:06:02 +00002341 FunctionProtoTypeLoc NewProtoLoc = NewTL.castAs<FunctionProtoTypeLoc>();
Richard Smith500d7292012-07-18 01:29:05 +00002342 unsigned NewIdx = 0;
David Blaikie39e6ab42013-02-18 22:06:02 +00002343 for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc.getNumArgs();
Douglas Gregor12c9c002011-01-07 16:43:16 +00002344 OldIdx != NumOldParams; ++OldIdx) {
David Blaikie39e6ab42013-02-18 22:06:02 +00002345 ParmVarDecl *OldParam = OldProtoLoc.getArg(OldIdx);
Richard Smith500d7292012-07-18 01:29:05 +00002346 LocalInstantiationScope *Scope = SemaRef.CurrentInstantiationScope;
2347
David Blaikiedc84cd52013-02-20 22:23:23 +00002348 Optional<unsigned> NumArgumentsInExpansion;
Richard Smith500d7292012-07-18 01:29:05 +00002349 if (OldParam->isParameterPack())
2350 NumArgumentsInExpansion =
2351 SemaRef.getNumArgumentsInExpansion(OldParam->getType(),
2352 TemplateArgs);
2353 if (!NumArgumentsInExpansion) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002354 // Simple case: normal parameter, or a parameter pack that's
Douglas Gregor12c9c002011-01-07 16:43:16 +00002355 // instantiated to a (still-dependent) parameter pack.
David Blaikie39e6ab42013-02-18 22:06:02 +00002356 ParmVarDecl *NewParam = NewProtoLoc.getArg(NewIdx++);
Douglas Gregor12c9c002011-01-07 16:43:16 +00002357 Params.push_back(NewParam);
Richard Smith500d7292012-07-18 01:29:05 +00002358 Scope->InstantiatedLocal(OldParam, NewParam);
2359 } else {
2360 // Parameter pack expansion: make the instantiation an argument pack.
2361 Scope->MakeInstantiatedLocalArgPack(OldParam);
2362 for (unsigned I = 0; I != *NumArgumentsInExpansion; ++I) {
David Blaikie39e6ab42013-02-18 22:06:02 +00002363 ParmVarDecl *NewParam = NewProtoLoc.getArg(NewIdx++);
Richard Smith500d7292012-07-18 01:29:05 +00002364 Params.push_back(NewParam);
2365 Scope->InstantiatedLocalPackArg(OldParam, NewParam);
2366 }
Douglas Gregor12c9c002011-01-07 16:43:16 +00002367 }
Douglas Gregor6920cdc2010-05-03 15:32:18 +00002368 }
Douglas Gregor895162d2010-04-30 18:55:50 +00002369 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00002370 } else {
2371 // The function type itself was not dependent and therefore no
2372 // substitution occurred. However, we still need to instantiate
2373 // the function parameters themselves.
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00002374 TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
David Blaikie39e6ab42013-02-18 22:06:02 +00002375 if (FunctionProtoTypeLoc OldProtoLoc =
2376 OldTL.getAs<FunctionProtoTypeLoc>()) {
2377 for (unsigned i = 0, i_end = OldProtoLoc.getNumArgs(); i != i_end; ++i) {
2378 ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc.getArg(i));
Douglas Gregor6920cdc2010-05-03 15:32:18 +00002379 if (!Parm)
2380 return 0;
2381 Params.push_back(Parm);
2382 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00002383 }
2384 }
John McCall21ef0fa2010-03-11 09:03:00 +00002385 return NewTInfo;
Douglas Gregor5545e162009-03-24 00:38:23 +00002386}
2387
Richard Smithe6975e92012-04-17 00:58:00 +00002388/// Introduce the instantiated function parameters into the local
2389/// instantiation scope, and set the parameter names to those used
2390/// in the template.
2391static void addInstantiatedParametersToScope(Sema &S, FunctionDecl *Function,
2392 const FunctionDecl *PatternDecl,
2393 LocalInstantiationScope &Scope,
2394 const MultiLevelTemplateArgumentList &TemplateArgs) {
2395 unsigned FParamIdx = 0;
2396 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
2397 const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
2398 if (!PatternParam->isParameterPack()) {
2399 // Simple case: not a parameter pack.
2400 assert(FParamIdx < Function->getNumParams());
2401 ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
2402 FunctionParam->setDeclName(PatternParam->getDeclName());
2403 Scope.InstantiatedLocal(PatternParam, FunctionParam);
2404 ++FParamIdx;
2405 continue;
2406 }
2407
2408 // Expand the parameter pack.
2409 Scope.MakeInstantiatedLocalArgPack(PatternParam);
David Blaikiedc84cd52013-02-20 22:23:23 +00002410 Optional<unsigned> NumArgumentsInExpansion
Richard Smithe6975e92012-04-17 00:58:00 +00002411 = S.getNumArgumentsInExpansion(PatternParam->getType(), TemplateArgs);
Richard Smith500d7292012-07-18 01:29:05 +00002412 assert(NumArgumentsInExpansion &&
2413 "should only be called when all template arguments are known");
2414 for (unsigned Arg = 0; Arg < *NumArgumentsInExpansion; ++Arg) {
Richard Smithe6975e92012-04-17 00:58:00 +00002415 ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
2416 FunctionParam->setDeclName(PatternParam->getDeclName());
2417 Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam);
2418 ++FParamIdx;
2419 }
2420 }
2421}
2422
2423static void InstantiateExceptionSpec(Sema &SemaRef, FunctionDecl *New,
2424 const FunctionProtoType *Proto,
2425 const MultiLevelTemplateArgumentList &TemplateArgs) {
Richard Smith13bffc52012-04-19 00:08:28 +00002426 assert(Proto->getExceptionSpecType() != EST_Uninstantiated);
2427
Richard Smithe6975e92012-04-17 00:58:00 +00002428 // C++11 [expr.prim.general]p3:
2429 // If a declaration declares a member function or member function
2430 // template of a class X, the expression this is a prvalue of type
2431 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
2432 // and the end of the function-definition, member-declarator, or
2433 // declarator.
2434 CXXRecordDecl *ThisContext = 0;
2435 unsigned ThisTypeQuals = 0;
2436 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(New)) {
2437 ThisContext = Method->getParent();
2438 ThisTypeQuals = Method->getTypeQualifiers();
2439 }
2440 Sema::CXXThisScopeRAII ThisScope(SemaRef, ThisContext, ThisTypeQuals,
Richard Smith80ad52f2013-01-02 11:42:31 +00002441 SemaRef.getLangOpts().CPlusPlus11);
Richard Smithe6975e92012-04-17 00:58:00 +00002442
2443 // The function has an exception specification or a "noreturn"
2444 // attribute. Substitute into each of the exception types.
2445 SmallVector<QualType, 4> Exceptions;
2446 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
2447 // FIXME: Poor location information!
2448 if (const PackExpansionType *PackExpansion
2449 = Proto->getExceptionType(I)->getAs<PackExpansionType>()) {
2450 // We have a pack expansion. Instantiate it.
2451 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2452 SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(),
2453 Unexpanded);
2454 assert(!Unexpanded.empty() &&
2455 "Pack expansion without parameter packs?");
2456
2457 bool Expand = false;
2458 bool RetainExpansion = false;
David Blaikiedc84cd52013-02-20 22:23:23 +00002459 Optional<unsigned> NumExpansions
Richard Smithe6975e92012-04-17 00:58:00 +00002460 = PackExpansion->getNumExpansions();
2461 if (SemaRef.CheckParameterPacksForExpansion(New->getLocation(),
2462 SourceRange(),
2463 Unexpanded,
2464 TemplateArgs,
2465 Expand,
2466 RetainExpansion,
2467 NumExpansions))
2468 break;
2469
2470 if (!Expand) {
2471 // We can't expand this pack expansion into separate arguments yet;
2472 // just substitute into the pattern and create a new pack expansion
2473 // type.
2474 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
2475 QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2476 TemplateArgs,
2477 New->getLocation(), New->getDeclName());
2478 if (T.isNull())
2479 break;
2480
2481 T = SemaRef.Context.getPackExpansionType(T, NumExpansions);
2482 Exceptions.push_back(T);
2483 continue;
2484 }
2485
2486 // Substitute into the pack expansion pattern for each template
2487 bool Invalid = false;
2488 for (unsigned ArgIdx = 0; ArgIdx != *NumExpansions; ++ArgIdx) {
2489 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, ArgIdx);
2490
2491 QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2492 TemplateArgs,
2493 New->getLocation(), New->getDeclName());
2494 if (T.isNull()) {
2495 Invalid = true;
2496 break;
2497 }
2498
2499 Exceptions.push_back(T);
2500 }
2501
2502 if (Invalid)
2503 break;
2504
2505 continue;
2506 }
2507
2508 QualType T
2509 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
2510 New->getLocation(), New->getDeclName());
2511 if (T.isNull() ||
2512 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
2513 continue;
2514
2515 Exceptions.push_back(T);
2516 }
2517 Expr *NoexceptExpr = 0;
2518 if (Expr *OldNoexceptExpr = Proto->getNoexceptExpr()) {
2519 EnterExpressionEvaluationContext Unevaluated(SemaRef,
2520 Sema::ConstantEvaluated);
2521 ExprResult E = SemaRef.SubstExpr(OldNoexceptExpr, TemplateArgs);
2522 if (E.isUsable())
2523 E = SemaRef.CheckBooleanCondition(E.get(), E.get()->getLocStart());
2524
2525 if (E.isUsable()) {
2526 NoexceptExpr = E.take();
2527 if (!NoexceptExpr->isTypeDependent() &&
2528 !NoexceptExpr->isValueDependent())
Douglas Gregorab41fe92012-05-04 22:38:52 +00002529 NoexceptExpr
2530 = SemaRef.VerifyIntegerConstantExpression(NoexceptExpr,
2531 0, diag::err_noexcept_needs_constant_expression,
2532 /*AllowFold*/ false).take();
Richard Smithe6975e92012-04-17 00:58:00 +00002533 }
2534 }
2535
2536 // Rebuild the function type
2537 const FunctionProtoType *NewProto
2538 = New->getType()->getAs<FunctionProtoType>();
2539 assert(NewProto && "Template instantiation without function prototype?");
2540
2541 FunctionProtoType::ExtProtoInfo EPI = NewProto->getExtProtoInfo();
2542 EPI.ExceptionSpecType = Proto->getExceptionSpecType();
2543 EPI.NumExceptions = Exceptions.size();
2544 EPI.Exceptions = Exceptions.data();
2545 EPI.NoexceptExpr = NoexceptExpr;
2546
2547 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
2548 NewProto->arg_type_begin(),
2549 NewProto->getNumArgs(),
2550 EPI));
2551}
2552
2553void Sema::InstantiateExceptionSpec(SourceLocation PointOfInstantiation,
2554 FunctionDecl *Decl) {
Richard Smith13bffc52012-04-19 00:08:28 +00002555 const FunctionProtoType *Proto = Decl->getType()->castAs<FunctionProtoType>();
2556 if (Proto->getExceptionSpecType() != EST_Uninstantiated)
Richard Smithe6975e92012-04-17 00:58:00 +00002557 return;
2558
2559 InstantiatingTemplate Inst(*this, PointOfInstantiation, Decl,
2560 InstantiatingTemplate::ExceptionSpecification());
Richard Smithb9d0b762012-07-27 04:22:15 +00002561 if (Inst) {
2562 // We hit the instantiation depth limit. Clear the exception specification
2563 // so that our callers don't have to cope with EST_Uninstantiated.
2564 FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
2565 EPI.ExceptionSpecType = EST_None;
2566 Decl->setType(Context.getFunctionType(Proto->getResultType(),
2567 Proto->arg_type_begin(),
2568 Proto->getNumArgs(),
2569 EPI));
Richard Smithe6975e92012-04-17 00:58:00 +00002570 return;
Richard Smithb9d0b762012-07-27 04:22:15 +00002571 }
Richard Smithe6975e92012-04-17 00:58:00 +00002572
2573 // Enter the scope of this instantiation. We don't use
2574 // PushDeclContext because we don't have a scope.
2575 Sema::ContextRAII savedContext(*this, Decl);
2576 LocalInstantiationScope Scope(*this);
2577
2578 MultiLevelTemplateArgumentList TemplateArgs =
2579 getTemplateInstantiationArgs(Decl, 0, /*RelativeToPrimary*/true);
2580
Richard Smith13bffc52012-04-19 00:08:28 +00002581 FunctionDecl *Template = Proto->getExceptionSpecTemplate();
2582 addInstantiatedParametersToScope(*this, Decl, Template, Scope, TemplateArgs);
Richard Smithe6975e92012-04-17 00:58:00 +00002583
Richard Smith13bffc52012-04-19 00:08:28 +00002584 ::InstantiateExceptionSpec(*this, Decl,
2585 Template->getType()->castAs<FunctionProtoType>(),
2586 TemplateArgs);
Richard Smithe6975e92012-04-17 00:58:00 +00002587}
2588
Mike Stump1eb44332009-09-09 15:08:12 +00002589/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00002590/// declaration (New) from the corresponding fields of its template (Tmpl).
2591///
2592/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00002593bool
2594TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00002595 FunctionDecl *Tmpl) {
David Blaikie85f485a2012-07-16 18:50:45 +00002596 if (Tmpl->isDeleted())
Sean Hunt10620eb2011-05-06 20:44:56 +00002597 New->setDeletedAsWritten();
Mike Stump1eb44332009-09-09 15:08:12 +00002598
Douglas Gregorcca9e962009-07-01 22:01:06 +00002599 // If we are performing substituting explicitly-specified template arguments
2600 // or deduced template arguments into a function template and we reach this
2601 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00002602 // to keeping the new function template specialization. We therefore
2603 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00002604 // into a template instantiation for this specific function template
2605 // specialization, which is not a SFINAE context, so that we diagnose any
2606 // further errors in the declaration itself.
2607 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
2608 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
2609 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
2610 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00002611 if (FunctionTemplateDecl *FunTmpl
Nick Lewycky4a9e60f2012-11-16 08:40:59 +00002612 = dyn_cast<FunctionTemplateDecl>(ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002613 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00002614 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00002615 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00002616 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
Nick Lewycky4a9e60f2012-11-16 08:40:59 +00002617 ActiveInst.Entity = New;
Douglas Gregorcca9e962009-07-01 22:01:06 +00002618 }
2619 }
Mike Stump1eb44332009-09-09 15:08:12 +00002620
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002621 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
2622 assert(Proto && "Function template without prototype?");
2623
Sebastian Redl60618fa2011-03-12 11:50:43 +00002624 if (Proto->hasExceptionSpec() || Proto->getNoReturnAttr()) {
John McCalle23cf432010-12-14 08:05:40 +00002625 FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
John McCalle23cf432010-12-14 08:05:40 +00002626
Richard Smithe6975e92012-04-17 00:58:00 +00002627 // DR1330: In C++11, defer instantiation of a non-trivial
2628 // exception specification.
Richard Smith80ad52f2013-01-02 11:42:31 +00002629 if (SemaRef.getLangOpts().CPlusPlus11 &&
Richard Smithe6975e92012-04-17 00:58:00 +00002630 EPI.ExceptionSpecType != EST_None &&
2631 EPI.ExceptionSpecType != EST_DynamicNone &&
2632 EPI.ExceptionSpecType != EST_BasicNoexcept) {
Richard Smith13bffc52012-04-19 00:08:28 +00002633 FunctionDecl *ExceptionSpecTemplate = Tmpl;
2634 if (EPI.ExceptionSpecType == EST_Uninstantiated)
2635 ExceptionSpecTemplate = EPI.ExceptionSpecTemplate;
Richard Smithb9d0b762012-07-27 04:22:15 +00002636 assert(EPI.ExceptionSpecType != EST_Unevaluated &&
2637 "instantiating implicitly-declared special member");
Richard Smith13bffc52012-04-19 00:08:28 +00002638
Richard Smithe6975e92012-04-17 00:58:00 +00002639 // Mark the function has having an uninstantiated exception specification.
2640 const FunctionProtoType *NewProto
2641 = New->getType()->getAs<FunctionProtoType>();
2642 assert(NewProto && "Template instantiation without function prototype?");
2643 EPI = NewProto->getExtProtoInfo();
2644 EPI.ExceptionSpecType = EST_Uninstantiated;
2645 EPI.ExceptionSpecDecl = New;
Richard Smith13bffc52012-04-19 00:08:28 +00002646 EPI.ExceptionSpecTemplate = ExceptionSpecTemplate;
Richard Smithe6975e92012-04-17 00:58:00 +00002647 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
2648 NewProto->arg_type_begin(),
2649 NewProto->getNumArgs(),
2650 EPI));
2651 } else {
2652 ::InstantiateExceptionSpec(SemaRef, New, Proto, TemplateArgs);
2653 }
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002654 }
2655
Rafael Espindola19f74ac2011-07-06 15:46:09 +00002656 // Get the definition. Leaves the variable unchanged if undefined.
Richard Smithe6975e92012-04-17 00:58:00 +00002657 const FunctionDecl *Definition = Tmpl;
Rafael Espindola19f74ac2011-07-06 15:46:09 +00002658 Tmpl->isDefined(Definition);
2659
DeLesley Hutchins23323e02012-01-20 22:50:54 +00002660 SemaRef.InstantiateAttrs(TemplateArgs, Definition, New,
2661 LateAttrs, StartingScope);
Douglas Gregor7cf84d62010-06-15 17:05:35 +00002662
Douglas Gregore53060f2009-06-25 22:08:12 +00002663 return false;
2664}
2665
Douglas Gregor5545e162009-03-24 00:38:23 +00002666/// \brief Initializes common fields of an instantiated method
2667/// declaration (New) from the corresponding fields of its template
2668/// (Tmpl).
2669///
2670/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00002671bool
2672TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00002673 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00002674 if (InitFunctionInstantiation(New, Tmpl))
2675 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002676
Douglas Gregor5545e162009-03-24 00:38:23 +00002677 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00002678 if (Tmpl->isVirtualAsWritten())
Douglas Gregor85606eb2010-09-28 20:50:54 +00002679 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +00002680
2681 // FIXME: attributes
2682 // FIXME: New needs a pointer to Tmpl
2683 return false;
2684}
Douglas Gregora58861f2009-05-13 20:28:22 +00002685
2686/// \brief Instantiate the definition of the given function from its
2687/// template.
2688///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002689/// \param PointOfInstantiation the point at which the instantiation was
2690/// required. Note that this is not precisely a "point of instantiation"
2691/// for the function, but it's close.
2692///
Douglas Gregora58861f2009-05-13 20:28:22 +00002693/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002694/// function template specialization or member function of a class template
2695/// specialization.
2696///
2697/// \param Recursive if true, recursively instantiates any functions that
2698/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002699///
2700/// \param DefinitionRequired if true, then we are performing an explicit
2701/// instantiation where the body of the function is required. Complain if
2702/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002703void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002704 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002705 bool Recursive,
2706 bool DefinitionRequired) {
Sean Hunt10620eb2011-05-06 20:44:56 +00002707 if (Function->isInvalidDecl() || Function->isDefined())
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002708 return;
2709
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002710 // Never instantiate an explicit specialization except if it is a class scope
2711 // explicit specialization.
2712 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization &&
2713 !Function->getClassScopeSpecializationPattern())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002714 return;
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002715
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002716 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00002717 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Sean Huntf996e052011-05-27 20:00:14 +00002718 assert(PatternDecl && "instantiating a non-template");
2719
2720 Stmt *Pattern = PatternDecl->getBody(PatternDecl);
2721 assert(PatternDecl && "template definition is not a template");
2722 if (!Pattern) {
2723 // Try to find a defaulted definition
2724 PatternDecl->isDefined(PatternDecl);
Sean Huntdfab8542011-05-25 22:02:25 +00002725 }
Sean Huntf996e052011-05-27 20:00:14 +00002726 assert(PatternDecl && "template definition is not a template");
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002727
Francois Pichet8387e2a2011-04-22 22:18:13 +00002728 // Postpone late parsed template instantiations.
Sean Huntf996e052011-05-27 20:00:14 +00002729 if (PatternDecl->isLateTemplateParsed() &&
Nick Lewycky8a29bc02011-05-12 03:51:24 +00002730 !LateTemplateParser) {
Francois Pichet8387e2a2011-04-22 22:18:13 +00002731 PendingInstantiations.push_back(
2732 std::make_pair(Function, PointOfInstantiation));
2733 return;
2734 }
2735
2736 // Call the LateTemplateParser callback if there a need to late parse
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002737 // a templated function definition.
Sean Huntf996e052011-05-27 20:00:14 +00002738 if (!Pattern && PatternDecl->isLateTemplateParsed() &&
Francois Pichet8387e2a2011-04-22 22:18:13 +00002739 LateTemplateParser) {
Francois Pichet4a47e8d2011-04-23 11:52:20 +00002740 LateTemplateParser(OpaqueParser, PatternDecl);
Francois Pichet8387e2a2011-04-22 22:18:13 +00002741 Pattern = PatternDecl->getBody(PatternDecl);
2742 }
2743
Sean Huntf996e052011-05-27 20:00:14 +00002744 if (!Pattern && !PatternDecl->isDefaulted()) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002745 if (DefinitionRequired) {
2746 if (Function->getPrimaryTemplate())
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002747 Diag(PointOfInstantiation,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002748 diag::err_explicit_instantiation_undefined_func_template)
2749 << Function->getPrimaryTemplate();
2750 else
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002751 Diag(PointOfInstantiation,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002752 diag::err_explicit_instantiation_undefined_member)
2753 << 1 << Function->getDeclName() << Function->getDeclContext();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002754
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002755 if (PatternDecl)
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002756 Diag(PatternDecl->getLocation(),
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002757 diag::note_explicit_instantiation_here);
Douglas Gregorcfe833b2010-05-17 17:57:54 +00002758 Function->setInvalidDecl();
Chandler Carruth58e390e2010-08-25 08:27:02 +00002759 } else if (Function->getTemplateSpecializationKind()
2760 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002761 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00002762 std::make_pair(Function, PointOfInstantiation));
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002763 }
Chandler Carruth58e390e2010-08-25 08:27:02 +00002764
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002765 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002766 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002767
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002768 // C++0x [temp.explicit]p9:
2769 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00002770 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002771 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00002772 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002773 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002774 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002775 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002776
Richard Smithd4497dd2013-01-25 00:08:28 +00002777 if (PatternDecl->isInlined())
2778 Function->setImplicitlyInline();
2779
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002780 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2781 if (Inst)
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002782 return;
2783
Abramo Bagnarae9946242011-11-18 08:08:52 +00002784 // Copy the inner loc start from the pattern.
2785 Function->setInnerLocStart(PatternDecl->getInnerLocStart());
2786
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002787 // If we're performing recursive template instantiation, create our own
2788 // queue of pending implicit instantiations that we will instantiate later,
2789 // while we're still within our own instantiation context.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002790 SmallVector<VTableUse, 16> SavedVTableUses;
Chandler Carruth62c78d52010-08-25 08:44:16 +00002791 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002792 if (Recursive) {
2793 VTableUses.swap(SavedVTableUses);
Chandler Carruth62c78d52010-08-25 08:44:16 +00002794 PendingInstantiations.swap(SavedPendingInstantiations);
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002795 }
Mike Stump1eb44332009-09-09 15:08:12 +00002796
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002797 EnterExpressionEvaluationContext EvalContext(*this,
John McCallf312b1e2010-08-26 23:41:50 +00002798 Sema::PotentiallyEvaluated);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002799
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002800 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00002801 // recorded, unless we're actually a member function within a local
2802 // class, in which case we need to merge our results with the parent
2803 // scope (of the enclosing function).
2804 bool MergeWithParentScope = false;
2805 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
2806 MergeWithParentScope = Rec->isLocalClass();
2807
2808 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002809
Richard Smith1d28caf2012-12-11 01:14:52 +00002810 if (PatternDecl->isDefaulted())
Sean Huntcd10dec2011-05-23 23:14:04 +00002811 SetDeclDefaulted(Function, PatternDecl->getLocation());
Richard Smith1d28caf2012-12-11 01:14:52 +00002812 else {
2813 ActOnStartOfFunctionDef(0, Function);
2814
2815 // Enter the scope of this instantiation. We don't use
2816 // PushDeclContext because we don't have a scope.
2817 Sema::ContextRAII savedContext(*this, Function);
2818
2819 MultiLevelTemplateArgumentList TemplateArgs =
2820 getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
2821
2822 addInstantiatedParametersToScope(*this, Function, PatternDecl, Scope,
2823 TemplateArgs);
2824
Sean Huntcd10dec2011-05-23 23:14:04 +00002825 // If this is a constructor, instantiate the member initializers.
2826 if (const CXXConstructorDecl *Ctor =
2827 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2828 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2829 TemplateArgs);
2830 }
2831
2832 // Instantiate the function body.
2833 StmtResult Body = SubstStmt(Pattern, TemplateArgs);
2834
2835 if (Body.isInvalid())
2836 Function->setInvalidDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002837
Sean Huntcd10dec2011-05-23 23:14:04 +00002838 ActOnFinishFunctionBody(Function, Body.get(),
2839 /*IsInstantiation=*/true);
Richard Smith1d28caf2012-12-11 01:14:52 +00002840
2841 PerformDependentDiagnostics(PatternDecl, TemplateArgs);
2842
2843 savedContext.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00002844 }
2845
Douglas Gregoraba43bb2009-05-26 20:50:29 +00002846 DeclGroupRef DG(Function);
2847 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00002848
Douglas Gregor60406be2010-01-16 22:29:39 +00002849 // This class may have local implicit instantiations that need to be
2850 // instantiation within this scope.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002851 PerformPendingInstantiations(/*LocalOnly=*/true);
Douglas Gregor60406be2010-01-16 22:29:39 +00002852 Scope.Exit();
2853
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002854 if (Recursive) {
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002855 // Define any pending vtables.
2856 DefineUsedVTables();
2857
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002858 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002859 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002860 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002861
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002862 // Restore the set of pending vtables.
Nick Lewycky81559102011-05-31 07:58:42 +00002863 assert(VTableUses.empty() &&
2864 "VTableUses should be empty before it is discarded.");
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002865 VTableUses.swap(SavedVTableUses);
2866
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002867 // Restore the set of pending implicit instantiations.
Nick Lewycky81559102011-05-31 07:58:42 +00002868 assert(PendingInstantiations.empty() &&
2869 "PendingInstantiations should be empty before it is discarded.");
Chandler Carruth62c78d52010-08-25 08:44:16 +00002870 PendingInstantiations.swap(SavedPendingInstantiations);
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002871 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002872}
2873
2874/// \brief Instantiate the definition of the given variable from its
2875/// template.
2876///
Douglas Gregor7caa6822009-07-24 20:34:43 +00002877/// \param PointOfInstantiation the point at which the instantiation was
2878/// required. Note that this is not precisely a "point of instantiation"
2879/// for the function, but it's close.
2880///
2881/// \param Var the already-instantiated declaration of a static member
2882/// variable of a class template specialization.
2883///
2884/// \param Recursive if true, recursively instantiates any functions that
2885/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002886///
2887/// \param DefinitionRequired if true, then we are performing an explicit
2888/// instantiation where an out-of-line definition of the member variable
2889/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002890void Sema::InstantiateStaticDataMemberDefinition(
2891 SourceLocation PointOfInstantiation,
2892 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002893 bool Recursive,
2894 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002895 if (Var->isInvalidDecl())
2896 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002897
Douglas Gregor7caa6822009-07-24 20:34:43 +00002898 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002899 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002900 assert(Def && "This data member was not instantiated from a template?");
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002901 assert(Def->isStaticDataMember() && "Not a static data member?");
Douglas Gregor0d035142009-10-27 18:42:08 +00002902 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002903
Douglas Gregor0d035142009-10-27 18:42:08 +00002904 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002905 // We did not find an out-of-line definition of this static data member,
2906 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00002907 // instantiate this definition (or provide a specialization for it) in
2908 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002909 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00002910 Def = Var->getInstantiatedFromStaticDataMember();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002911 Diag(PointOfInstantiation,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002912 diag::err_explicit_instantiation_undefined_member)
2913 << 2 << Var->getDeclName() << Var->getDeclContext();
2914 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
Chandler Carruth58e390e2010-08-25 08:27:02 +00002915 } else if (Var->getTemplateSpecializationKind()
2916 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002917 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00002918 std::make_pair(Var, PointOfInstantiation));
2919 }
2920
Douglas Gregor7caa6822009-07-24 20:34:43 +00002921 return;
2922 }
2923
Rafael Espindola234fe652012-03-05 10:54:55 +00002924 TemplateSpecializationKind TSK = Var->getTemplateSpecializationKind();
2925
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002926 // Never instantiate an explicit specialization.
Rafael Espindola234fe652012-03-05 10:54:55 +00002927 if (TSK == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002928 return;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002929
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002930 // C++0x [temp.explicit]p9:
2931 // Except for inline functions, other explicit instantiation declarations
2932 // have the effect of suppressing the implicit instantiation of the entity
2933 // to which they refer.
Rafael Espindola234fe652012-03-05 10:54:55 +00002934 if (TSK == TSK_ExplicitInstantiationDeclaration)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002935 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002936
Rafael Espindola02503932012-03-08 15:51:03 +00002937 Consumer.HandleCXXStaticMemberVarInstantiation(Var);
2938
Douglas Gregorf15748a2011-06-03 03:35:07 +00002939 // If we already have a definition, we're done.
Nick Lewycky95e38722012-04-04 02:38:36 +00002940 if (VarDecl *Def = Var->getDefinition()) {
2941 // We may be explicitly instantiating something we've already implicitly
2942 // instantiated.
2943 Def->setTemplateSpecializationKind(Var->getTemplateSpecializationKind(),
2944 PointOfInstantiation);
Douglas Gregorf15748a2011-06-03 03:35:07 +00002945 return;
Nick Lewycky95e38722012-04-04 02:38:36 +00002946 }
Douglas Gregorf15748a2011-06-03 03:35:07 +00002947
Douglas Gregor7caa6822009-07-24 20:34:43 +00002948 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
2949 if (Inst)
2950 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002951
Douglas Gregor7caa6822009-07-24 20:34:43 +00002952 // If we're performing recursive template instantiation, create our own
2953 // queue of pending implicit instantiations that we will instantiate later,
2954 // while we're still within our own instantiation context.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002955 SmallVector<VTableUse, 16> SavedVTableUses;
Chandler Carruth62c78d52010-08-25 08:44:16 +00002956 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Nick Lewycky81559102011-05-31 07:58:42 +00002957 if (Recursive) {
2958 VTableUses.swap(SavedVTableUses);
Chandler Carruth62c78d52010-08-25 08:44:16 +00002959 PendingInstantiations.swap(SavedPendingInstantiations);
Nick Lewycky81559102011-05-31 07:58:42 +00002960 }
Mike Stump1eb44332009-09-09 15:08:12 +00002961
Douglas Gregor7caa6822009-07-24 20:34:43 +00002962 // Enter the scope of this instantiation. We don't use
2963 // PushDeclContext because we don't have a scope.
John McCallf5ba7e02011-02-14 20:37:25 +00002964 ContextRAII previousContext(*this, Var->getDeclContext());
Douglas Gregor7bdc1522012-02-16 21:36:18 +00002965 LocalInstantiationScope Local(*this);
2966
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002967 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00002968 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Nico Weber6bb4dcb2010-11-28 22:53:37 +00002969 getTemplateInstantiationArgs(Var)));
John McCallf5ba7e02011-02-14 20:37:25 +00002970
2971 previousContext.pop();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002972
2973 if (Var) {
Douglas Gregor583f33b2009-10-15 18:07:02 +00002974 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2975 assert(MSInfo && "Missing member specialization information?");
2976 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2977 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00002978 DeclGroupRef DG(Var);
2979 Consumer.HandleTopLevelDecl(DG);
2980 }
Douglas Gregor7bdc1522012-02-16 21:36:18 +00002981 Local.Exit();
2982
Douglas Gregor7caa6822009-07-24 20:34:43 +00002983 if (Recursive) {
Nick Lewycky81559102011-05-31 07:58:42 +00002984 // Define any newly required vtables.
2985 DefineUsedVTables();
2986
Douglas Gregor7caa6822009-07-24 20:34:43 +00002987 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002988 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002989 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002990
Nick Lewycky81559102011-05-31 07:58:42 +00002991 // Restore the set of pending vtables.
2992 assert(VTableUses.empty() &&
2993 "VTableUses should be empty before it is discarded, "
2994 "while instantiating static data member.");
2995 VTableUses.swap(SavedVTableUses);
2996
Douglas Gregor7caa6822009-07-24 20:34:43 +00002997 // Restore the set of pending implicit instantiations.
Nick Lewycky81559102011-05-31 07:58:42 +00002998 assert(PendingInstantiations.empty() &&
2999 "PendingInstantiations should be empty before it is discarded, "
3000 "while instantiating static data member.");
Chandler Carruth62c78d52010-08-25 08:44:16 +00003001 PendingInstantiations.swap(SavedPendingInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00003002 }
Douglas Gregora58861f2009-05-13 20:28:22 +00003003}
Douglas Gregor815215d2009-05-27 05:35:12 +00003004
Anders Carlsson09025312009-08-29 05:16:22 +00003005void
3006Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
3007 const CXXConstructorDecl *Tmpl,
3008 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00003009
Richard Trieu90ab75b2011-09-09 03:18:59 +00003010 SmallVector<CXXCtorInitializer*, 4> NewInits;
Richard Smith54b3ba82012-09-25 00:23:05 +00003011 bool AnyErrors = Tmpl->isInvalidDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003012
Anders Carlsson09025312009-08-29 05:16:22 +00003013 // Instantiate all the initializers.
3014 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00003015 InitsEnd = Tmpl->init_end();
3016 Inits != InitsEnd; ++Inits) {
Sean Huntcbb67482011-01-08 20:30:50 +00003017 CXXCtorInitializer *Init = *Inits;
Anders Carlsson09025312009-08-29 05:16:22 +00003018
Chandler Carruth030ef472010-09-03 21:54:20 +00003019 // Only instantiate written initializers, let Sema re-construct implicit
3020 // ones.
3021 if (!Init->isWritten())
3022 continue;
3023
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003024 SourceLocation EllipsisLoc;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003025
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003026 if (Init->isPackExpansion()) {
3027 // This is a pack expansion. We should expand it now.
Douglas Gregor76852c22011-11-01 01:16:03 +00003028 TypeLoc BaseTL = Init->getTypeSourceInfo()->getTypeLoc();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003029 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003030 collectUnexpandedParameterPacks(BaseTL, Unexpanded);
3031 bool ShouldExpand = false;
Douglas Gregord3731192011-01-10 07:32:04 +00003032 bool RetainExpansion = false;
David Blaikiedc84cd52013-02-20 22:23:23 +00003033 Optional<unsigned> NumExpansions;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003034 if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(),
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003035 BaseTL.getSourceRange(),
David Blaikiea71f9d02011-09-22 02:34:54 +00003036 Unexpanded,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003037 TemplateArgs, ShouldExpand,
Douglas Gregord3731192011-01-10 07:32:04 +00003038 RetainExpansion,
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003039 NumExpansions)) {
3040 AnyErrors = true;
3041 New->setInvalidDecl();
3042 continue;
3043 }
3044 assert(ShouldExpand && "Partial instantiation of base initializer?");
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003045
3046 // Loop over all of the arguments in the argument pack(s),
Douglas Gregorcded4f62011-01-14 17:04:44 +00003047 for (unsigned I = 0; I != *NumExpansions; ++I) {
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003048 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
3049
3050 // Instantiate the initializer.
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003051 ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs,
3052 /*CXXDirectInit=*/true);
3053 if (TempInit.isInvalid()) {
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003054 AnyErrors = true;
3055 break;
3056 }
3057
3058 // Instantiate the base type.
Douglas Gregor76852c22011-11-01 01:16:03 +00003059 TypeSourceInfo *BaseTInfo = SubstType(Init->getTypeSourceInfo(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003060 TemplateArgs,
3061 Init->getSourceLocation(),
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003062 New->getDeclName());
3063 if (!BaseTInfo) {
3064 AnyErrors = true;
3065 break;
3066 }
3067
3068 // Build the initializer.
Sebastian Redl6df65482011-09-24 17:48:25 +00003069 MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(),
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003070 BaseTInfo, TempInit.take(),
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003071 New->getParent(),
3072 SourceLocation());
3073 if (NewInit.isInvalid()) {
3074 AnyErrors = true;
3075 break;
3076 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003077
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003078 NewInits.push_back(NewInit.get());
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003079 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003080
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003081 continue;
3082 }
3083
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00003084 // Instantiate the initializer.
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003085 ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs,
3086 /*CXXDirectInit=*/true);
3087 if (TempInit.isInvalid()) {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00003088 AnyErrors = true;
3089 continue;
Anders Carlsson09025312009-08-29 05:16:22 +00003090 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003091
Anders Carlsson09025312009-08-29 05:16:22 +00003092 MemInitResult NewInit;
Douglas Gregor76852c22011-11-01 01:16:03 +00003093 if (Init->isDelegatingInitializer() || Init->isBaseInitializer()) {
3094 TypeSourceInfo *TInfo = SubstType(Init->getTypeSourceInfo(),
3095 TemplateArgs,
3096 Init->getSourceLocation(),
3097 New->getDeclName());
3098 if (!TInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00003099 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00003100 New->setInvalidDecl();
3101 continue;
3102 }
Sebastian Redl6df65482011-09-24 17:48:25 +00003103
Douglas Gregor76852c22011-11-01 01:16:03 +00003104 if (Init->isBaseInitializer())
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003105 NewInit = BuildBaseInitializer(TInfo->getType(), TInfo, TempInit.take(),
Douglas Gregor76852c22011-11-01 01:16:03 +00003106 New->getParent(), EllipsisLoc);
3107 else
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003108 NewInit = BuildDelegatingInitializer(TInfo, TempInit.take(),
Douglas Gregor76852c22011-11-01 01:16:03 +00003109 cast<CXXRecordDecl>(CurContext->getParent()));
Anders Carlsson09025312009-08-29 05:16:22 +00003110 } else if (Init->isMemberInitializer()) {
Douglas Gregorb7107222011-03-04 19:46:35 +00003111 FieldDecl *Member = cast_or_null<FieldDecl>(FindInstantiatedDecl(
Francois Pichet00eb3f92010-12-04 09:14:42 +00003112 Init->getMemberLocation(),
3113 Init->getMember(),
3114 TemplateArgs));
Douglas Gregorb7107222011-03-04 19:46:35 +00003115 if (!Member) {
3116 AnyErrors = true;
3117 New->setInvalidDecl();
3118 continue;
3119 }
Mike Stump1eb44332009-09-09 15:08:12 +00003120
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003121 NewInit = BuildMemberInitializer(Member, TempInit.take(),
Sebastian Redl6df65482011-09-24 17:48:25 +00003122 Init->getSourceLocation());
Francois Pichet00eb3f92010-12-04 09:14:42 +00003123 } else if (Init->isIndirectMemberInitializer()) {
3124 IndirectFieldDecl *IndirectMember =
Douglas Gregorb7107222011-03-04 19:46:35 +00003125 cast_or_null<IndirectFieldDecl>(FindInstantiatedDecl(
Francois Pichet00eb3f92010-12-04 09:14:42 +00003126 Init->getMemberLocation(),
3127 Init->getIndirectMember(), TemplateArgs));
3128
Douglas Gregorb7107222011-03-04 19:46:35 +00003129 if (!IndirectMember) {
3130 AnyErrors = true;
3131 New->setInvalidDecl();
Sebastian Redl6df65482011-09-24 17:48:25 +00003132 continue;
Douglas Gregorb7107222011-03-04 19:46:35 +00003133 }
Sebastian Redl6df65482011-09-24 17:48:25 +00003134
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003135 NewInit = BuildMemberInitializer(IndirectMember, TempInit.take(),
Sebastian Redl6df65482011-09-24 17:48:25 +00003136 Init->getSourceLocation());
Anders Carlsson09025312009-08-29 05:16:22 +00003137 }
3138
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00003139 if (NewInit.isInvalid()) {
3140 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00003141 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00003142 } else {
Richard Trieu90ab75b2011-09-09 03:18:59 +00003143 NewInits.push_back(NewInit.get());
Anders Carlsson09025312009-08-29 05:16:22 +00003144 }
3145 }
Mike Stump1eb44332009-09-09 15:08:12 +00003146
Anders Carlsson09025312009-08-29 05:16:22 +00003147 // Assign all the initializers to the new constructor.
John McCalld226f652010-08-21 09:40:31 +00003148 ActOnMemInitializers(New,
Anders Carlsson09025312009-08-29 05:16:22 +00003149 /*FIXME: ColonLoc */
3150 SourceLocation(),
David Blaikie93c86172013-01-17 05:26:25 +00003151 NewInits,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00003152 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00003153}
3154
John McCall52a575a2009-08-29 08:11:13 +00003155// TODO: this could be templated if the various decl types used the
3156// same method name.
3157static bool isInstantiationOf(ClassTemplateDecl *Pattern,
3158 ClassTemplateDecl *Instance) {
3159 Pattern = Pattern->getCanonicalDecl();
3160
3161 do {
3162 Instance = Instance->getCanonicalDecl();
3163 if (Pattern == Instance) return true;
3164 Instance = Instance->getInstantiatedFromMemberTemplate();
3165 } while (Instance);
3166
3167 return false;
3168}
3169
Douglas Gregor0d696532009-09-28 06:34:35 +00003170static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
3171 FunctionTemplateDecl *Instance) {
3172 Pattern = Pattern->getCanonicalDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003173
Douglas Gregor0d696532009-09-28 06:34:35 +00003174 do {
3175 Instance = Instance->getCanonicalDecl();
3176 if (Pattern == Instance) return true;
3177 Instance = Instance->getInstantiatedFromMemberTemplate();
3178 } while (Instance);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003179
Douglas Gregor0d696532009-09-28 06:34:35 +00003180 return false;
3181}
3182
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003183static bool
Douglas Gregored9c0f92009-10-29 00:04:11 +00003184isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
3185 ClassTemplatePartialSpecializationDecl *Instance) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003186 Pattern
Douglas Gregored9c0f92009-10-29 00:04:11 +00003187 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
3188 do {
3189 Instance = cast<ClassTemplatePartialSpecializationDecl>(
3190 Instance->getCanonicalDecl());
3191 if (Pattern == Instance)
3192 return true;
3193 Instance = Instance->getInstantiatedFromMember();
3194 } while (Instance);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003195
Douglas Gregored9c0f92009-10-29 00:04:11 +00003196 return false;
3197}
3198
John McCall52a575a2009-08-29 08:11:13 +00003199static bool isInstantiationOf(CXXRecordDecl *Pattern,
3200 CXXRecordDecl *Instance) {
3201 Pattern = Pattern->getCanonicalDecl();
3202
3203 do {
3204 Instance = Instance->getCanonicalDecl();
3205 if (Pattern == Instance) return true;
3206 Instance = Instance->getInstantiatedFromMemberClass();
3207 } while (Instance);
3208
3209 return false;
3210}
3211
3212static bool isInstantiationOf(FunctionDecl *Pattern,
3213 FunctionDecl *Instance) {
3214 Pattern = Pattern->getCanonicalDecl();
3215
3216 do {
3217 Instance = Instance->getCanonicalDecl();
3218 if (Pattern == Instance) return true;
3219 Instance = Instance->getInstantiatedFromMemberFunction();
3220 } while (Instance);
3221
3222 return false;
3223}
3224
3225static bool isInstantiationOf(EnumDecl *Pattern,
3226 EnumDecl *Instance) {
3227 Pattern = Pattern->getCanonicalDecl();
3228
3229 do {
3230 Instance = Instance->getCanonicalDecl();
3231 if (Pattern == Instance) return true;
3232 Instance = Instance->getInstantiatedFromMemberEnum();
3233 } while (Instance);
3234
3235 return false;
3236}
3237
John McCalled976492009-12-04 22:46:56 +00003238static bool isInstantiationOf(UsingShadowDecl *Pattern,
3239 UsingShadowDecl *Instance,
3240 ASTContext &C) {
3241 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
3242}
3243
3244static bool isInstantiationOf(UsingDecl *Pattern,
3245 UsingDecl *Instance,
3246 ASTContext &C) {
3247 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
3248}
3249
John McCall7ba107a2009-11-18 02:36:19 +00003250static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
3251 UsingDecl *Instance,
3252 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00003253 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00003254}
3255
3256static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00003257 UsingDecl *Instance,
3258 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00003259 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00003260}
3261
John McCall52a575a2009-08-29 08:11:13 +00003262static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
3263 VarDecl *Instance) {
3264 assert(Instance->isStaticDataMember());
3265
3266 Pattern = Pattern->getCanonicalDecl();
3267
3268 do {
3269 Instance = Instance->getCanonicalDecl();
3270 if (Pattern == Instance) return true;
3271 Instance = Instance->getInstantiatedFromStaticDataMember();
3272 } while (Instance);
3273
3274 return false;
3275}
3276
John McCalled976492009-12-04 22:46:56 +00003277// Other is the prospective instantiation
3278// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00003279static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00003280 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00003281 if (UnresolvedUsingTypenameDecl *UUD
3282 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
3283 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
3284 return isInstantiationOf(UUD, UD, Ctx);
3285 }
3286 }
3287
3288 if (UnresolvedUsingValueDecl *UUD
3289 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00003290 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
3291 return isInstantiationOf(UUD, UD, Ctx);
3292 }
3293 }
Douglas Gregor815215d2009-05-27 05:35:12 +00003294
Anders Carlsson0d8df782009-08-29 19:37:28 +00003295 return false;
3296 }
Mike Stump1eb44332009-09-09 15:08:12 +00003297
John McCall52a575a2009-08-29 08:11:13 +00003298 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
3299 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00003300
John McCall52a575a2009-08-29 08:11:13 +00003301 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
3302 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00003303
John McCall52a575a2009-08-29 08:11:13 +00003304 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
3305 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00003306
Douglas Gregor7caa6822009-07-24 20:34:43 +00003307 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00003308 if (Var->isStaticDataMember())
3309 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
3310
3311 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
3312 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00003313
Douglas Gregor0d696532009-09-28 06:34:35 +00003314 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
3315 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
3316
Douglas Gregored9c0f92009-10-29 00:04:11 +00003317 if (ClassTemplatePartialSpecializationDecl *PartialSpec
3318 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
3319 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
3320 PartialSpec);
3321
Anders Carlssond8b285f2009-09-01 04:26:58 +00003322 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
3323 if (!Field->getDeclName()) {
3324 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00003325 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00003326 cast<FieldDecl>(D);
3327 }
3328 }
Mike Stump1eb44332009-09-09 15:08:12 +00003329
John McCalled976492009-12-04 22:46:56 +00003330 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
3331 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
3332
3333 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
3334 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
3335
Douglas Gregor815215d2009-05-27 05:35:12 +00003336 return D->getDeclName() && isa<NamedDecl>(Other) &&
3337 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
3338}
3339
3340template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00003341static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00003342 NamedDecl *D,
3343 ForwardIterator first,
3344 ForwardIterator last) {
3345 for (; first != last; ++first)
3346 if (isInstantiationOf(Ctx, D, *first))
3347 return cast<NamedDecl>(*first);
3348
3349 return 0;
3350}
3351
John McCall02cace72009-08-28 07:59:38 +00003352/// \brief Finds the instantiation of the given declaration context
3353/// within the current instantiation.
3354///
3355/// \returns NULL if there was an error
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003356DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
Douglas Gregore95b4092009-09-16 18:34:49 +00003357 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00003358 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003359 Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00003360 return cast_or_null<DeclContext>(ID);
3361 } else return DC;
3362}
3363
Douglas Gregored961e72009-05-27 17:54:46 +00003364/// \brief Find the instantiation of the given declaration within the
3365/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00003366///
3367/// This routine is intended to be used when \p D is a declaration
3368/// referenced from within a template, that needs to mapped into the
3369/// corresponding declaration within an instantiation. For example,
3370/// given:
3371///
3372/// \code
3373/// template<typename T>
3374/// struct X {
3375/// enum Kind {
3376/// KnownValue = sizeof(T)
3377/// };
3378///
3379/// bool getKind() const { return KnownValue; }
3380/// };
3381///
3382/// template struct X<int>;
3383/// \endcode
3384///
3385/// In the instantiation of X<int>::getKind(), we need to map the
3386/// EnumConstantDecl for KnownValue (which refers to
James Dennettf198d122012-06-17 03:36:08 +00003387/// X<T>::\<Kind>\::KnownValue) to its instantiation
James Dennettef2b5b32012-06-15 22:23:43 +00003388/// (X<int>::\<Kind>\::KnownValue). InstantiateCurrentDeclRef() performs
Douglas Gregored961e72009-05-27 17:54:46 +00003389/// this mapping from within the instantiation of X<int>.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003390NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
Douglas Gregore95b4092009-09-16 18:34:49 +00003391 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00003392 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00003393 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00003394 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
Douglas Gregor7bdc1522012-02-16 21:36:18 +00003395 (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext()) ||
3396 (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda())) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00003397 // D is a local of some kind. Look into the map of local
3398 // declarations to their instantiations.
Chris Lattnerd8e54992011-02-17 19:47:42 +00003399 typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
3400 llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found
3401 = CurrentInstantiationScope->findInstantiationOf(D);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003402
Chris Lattner57ad3782011-02-17 20:34:02 +00003403 if (Found) {
3404 if (Decl *FD = Found->dyn_cast<Decl *>())
3405 return cast<NamedDecl>(FD);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003406
Richard Smith9a4db032012-09-12 00:56:43 +00003407 int PackIdx = ArgumentPackSubstitutionIndex;
3408 assert(PackIdx != -1 && "found declaration pack but not pack expanding");
Chris Lattner57ad3782011-02-17 20:34:02 +00003409 return cast<NamedDecl>((*Found->get<DeclArgumentPack *>())[PackIdx]);
3410 }
3411
3412 // If we didn't find the decl, then we must have a label decl that hasn't
3413 // been found yet. Lazily instantiate it and return it now.
3414 assert(isa<LabelDecl>(D));
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003415
Chris Lattner57ad3782011-02-17 20:34:02 +00003416 Decl *Inst = SubstDecl(D, CurContext, TemplateArgs);
3417 assert(Inst && "Failed to instantiate label??");
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003418
Chris Lattner57ad3782011-02-17 20:34:02 +00003419 CurrentInstantiationScope->InstantiatedLocal(D, Inst);
3420 return cast<LabelDecl>(Inst);
Douglas Gregor2bba76b2009-05-27 17:07:49 +00003421 }
Douglas Gregor815215d2009-05-27 05:35:12 +00003422
Douglas Gregore95b4092009-09-16 18:34:49 +00003423 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
3424 if (!Record->isDependentContext())
3425 return D;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003426
Douglas Gregor2c1227c2011-11-07 17:43:18 +00003427 // Determine whether this record is the "templated" declaration describing
3428 // a class template or class template partial specialization.
Douglas Gregore95b4092009-09-16 18:34:49 +00003429 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
Douglas Gregor2c1227c2011-11-07 17:43:18 +00003430 if (ClassTemplate)
3431 ClassTemplate = ClassTemplate->getCanonicalDecl();
3432 else if (ClassTemplatePartialSpecializationDecl *PartialSpec
3433 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record))
3434 ClassTemplate = PartialSpec->getSpecializedTemplate()->getCanonicalDecl();
3435
3436 // Walk the current context to find either the record or an instantiation of
3437 // it.
3438 DeclContext *DC = CurContext;
3439 while (!DC->isFileContext()) {
3440 // If we're performing substitution while we're inside the template
3441 // definition, we'll find our own context. We're done.
3442 if (DC->Equals(Record))
3443 return Record;
3444
3445 if (CXXRecordDecl *InstRecord = dyn_cast<CXXRecordDecl>(DC)) {
3446 // Check whether we're in the process of instantiating a class template
3447 // specialization of the template we're mapping.
3448 if (ClassTemplateSpecializationDecl *InstSpec
3449 = dyn_cast<ClassTemplateSpecializationDecl>(InstRecord)){
3450 ClassTemplateDecl *SpecTemplate = InstSpec->getSpecializedTemplate();
3451 if (ClassTemplate && isInstantiationOf(ClassTemplate, SpecTemplate))
3452 return InstRecord;
3453 }
3454
3455 // Check whether we're in the process of instantiating a member class.
3456 if (isInstantiationOf(Record, InstRecord))
3457 return InstRecord;
Douglas Gregore95b4092009-09-16 18:34:49 +00003458 }
Douglas Gregor2c1227c2011-11-07 17:43:18 +00003459
3460
3461 // Move to the outer template scope.
3462 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) {
3463 if (FD->getFriendObjectKind() && FD->getDeclContext()->isFileContext()){
3464 DC = FD->getLexicalDeclContext();
3465 continue;
3466 }
John McCall52a575a2009-08-29 08:11:13 +00003467 }
Douglas Gregor2c1227c2011-11-07 17:43:18 +00003468
3469 DC = DC->getParent();
John McCall52a575a2009-08-29 08:11:13 +00003470 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00003471
Douglas Gregore95b4092009-09-16 18:34:49 +00003472 // Fall through to deal with other dependent record types (e.g.,
3473 // anonymous unions in class templates).
3474 }
John McCall52a575a2009-08-29 08:11:13 +00003475
Douglas Gregore95b4092009-09-16 18:34:49 +00003476 if (!ParentDC->isDependentContext())
3477 return D;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003478
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003479 ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00003480 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00003481 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003482
Douglas Gregor815215d2009-05-27 05:35:12 +00003483 if (ParentDC != D->getDeclContext()) {
3484 // We performed some kind of instantiation in the parent context,
3485 // so now we need to look into the instantiated parent context to
3486 // find the instantiation of the declaration D.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003487
John McCall3cb0ebd2010-03-10 03:28:59 +00003488 // If our context used to be dependent, we may need to instantiate
3489 // it before performing lookup into that context.
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00003490 bool IsBeingInstantiated = false;
John McCall3cb0ebd2010-03-10 03:28:59 +00003491 if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003492 if (!Spec->isDependentContext()) {
3493 QualType T = Context.getTypeDeclType(Spec);
John McCall3cb0ebd2010-03-10 03:28:59 +00003494 const RecordType *Tag = T->getAs<RecordType>();
3495 assert(Tag && "type of non-dependent record is not a RecordType");
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00003496 if (Tag->isBeingDefined())
3497 IsBeingInstantiated = true;
John McCall3cb0ebd2010-03-10 03:28:59 +00003498 if (!Tag->isBeingDefined() &&
3499 RequireCompleteType(Loc, T, diag::err_incomplete_type))
3500 return 0;
Douglas Gregora43064c2010-11-05 23:22:45 +00003501
3502 ParentDC = Tag->getDecl();
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003503 }
3504 }
3505
Douglas Gregor815215d2009-05-27 05:35:12 +00003506 NamedDecl *Result = 0;
3507 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003508 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
David Blaikie3bc93e32012-12-19 00:45:41 +00003509 Result = findInstantiationOf(Context, D, Found.begin(), Found.end());
Douglas Gregor815215d2009-05-27 05:35:12 +00003510 } else {
3511 // Since we don't have a name for the entity we're looking for,
3512 // our only option is to walk through all of the declarations to
3513 // find that name. This will occur in a few cases:
3514 //
3515 // - anonymous struct/union within a template
3516 // - unnamed class/struct/union/enum within a template
3517 //
3518 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00003519 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003520 ParentDC->decls_begin(),
3521 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00003522 }
Mike Stump1eb44332009-09-09 15:08:12 +00003523
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00003524 if (!Result) {
3525 if (isa<UsingShadowDecl>(D)) {
3526 // UsingShadowDecls can instantiate to nothing because of using hiding.
3527 } else if (Diags.hasErrorOccurred()) {
3528 // We've already complained about something, so most likely this
3529 // declaration failed to instantiate. There's no point in complaining
3530 // further, since this is normal in invalid code.
3531 } else if (IsBeingInstantiated) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003532 // The class in which this member exists is currently being
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00003533 // instantiated, and we haven't gotten around to instantiating this
3534 // member yet. This can happen when the code uses forward declarations
3535 // of member classes, and introduces ordering dependencies via
3536 // template instantiation.
3537 Diag(Loc, diag::err_member_not_yet_instantiated)
3538 << D->getDeclName()
3539 << Context.getTypeDeclType(cast<CXXRecordDecl>(ParentDC));
3540 Diag(D->getLocation(), diag::note_non_instantiated_member_here);
Richard Smith0724b7c2012-03-26 20:28:16 +00003541 } else if (EnumConstantDecl *ED = dyn_cast<EnumConstantDecl>(D)) {
3542 // This enumeration constant was found when the template was defined,
3543 // but can't be found in the instantiation. This can happen if an
3544 // unscoped enumeration member is explicitly specialized.
3545 EnumDecl *Enum = cast<EnumDecl>(ED->getLexicalDeclContext());
3546 EnumDecl *Spec = cast<EnumDecl>(FindInstantiatedDecl(Loc, Enum,
3547 TemplateArgs));
3548 assert(Spec->getTemplateSpecializationKind() ==
3549 TSK_ExplicitSpecialization);
3550 Diag(Loc, diag::err_enumerator_does_not_exist)
3551 << D->getDeclName()
3552 << Context.getTypeDeclType(cast<TypeDecl>(Spec->getDeclContext()));
3553 Diag(Spec->getLocation(), diag::note_enum_specialized_here)
3554 << Context.getTypeDeclType(Spec);
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00003555 } else {
3556 // We should have found something, but didn't.
3557 llvm_unreachable("Unable to find instantiation of declaration!");
3558 }
3559 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003560
Douglas Gregor815215d2009-05-27 05:35:12 +00003561 D = Result;
3562 }
3563
Douglas Gregor815215d2009-05-27 05:35:12 +00003564 return D;
3565}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003566
Mike Stump1eb44332009-09-09 15:08:12 +00003567/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003568/// instantiations we have seen until this point.
Nick Lewycky81559102011-05-31 07:58:42 +00003569void Sema::PerformPendingInstantiations(bool LocalOnly) {
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00003570 // Load pending instantiations from the external source.
3571 if (!LocalOnly && ExternalSource) {
Richard Smithb9d0b762012-07-27 04:22:15 +00003572 SmallVector<PendingImplicitInstantiation, 4> Pending;
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00003573 ExternalSource->ReadPendingInstantiations(Pending);
3574 PendingInstantiations.insert(PendingInstantiations.begin(),
3575 Pending.begin(), Pending.end());
3576 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003577
Douglas Gregor60406be2010-01-16 22:29:39 +00003578 while (!PendingLocalImplicitInstantiations.empty() ||
Chandler Carruth62c78d52010-08-25 08:44:16 +00003579 (!LocalOnly && !PendingInstantiations.empty())) {
Douglas Gregor60406be2010-01-16 22:29:39 +00003580 PendingImplicitInstantiation Inst;
3581
3582 if (PendingLocalImplicitInstantiations.empty()) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00003583 Inst = PendingInstantiations.front();
3584 PendingInstantiations.pop_front();
Douglas Gregor60406be2010-01-16 22:29:39 +00003585 } else {
3586 Inst = PendingLocalImplicitInstantiations.front();
3587 PendingLocalImplicitInstantiations.pop_front();
3588 }
Mike Stump1eb44332009-09-09 15:08:12 +00003589
Douglas Gregor7caa6822009-07-24 20:34:43 +00003590 // Instantiate function definitions
3591 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
John McCallf312b1e2010-08-26 23:41:50 +00003592 PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(),
3593 "instantiating function definition");
Chandler Carruth58e390e2010-08-25 08:27:02 +00003594 bool DefinitionRequired = Function->getTemplateSpecializationKind() ==
3595 TSK_ExplicitInstantiationDefinition;
3596 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true,
3597 DefinitionRequired);
Douglas Gregor7caa6822009-07-24 20:34:43 +00003598 continue;
3599 }
Mike Stump1eb44332009-09-09 15:08:12 +00003600
Douglas Gregor7caa6822009-07-24 20:34:43 +00003601 // Instantiate static data member definitions.
3602 VarDecl *Var = cast<VarDecl>(Inst.first);
3603 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00003604
Chandler Carruth291b4412010-02-13 10:17:50 +00003605 // Don't try to instantiate declarations if the most recent redeclaration
3606 // is invalid.
Douglas Gregoref96ee02012-01-14 16:38:05 +00003607 if (Var->getMostRecentDecl()->isInvalidDecl())
Chandler Carruth291b4412010-02-13 10:17:50 +00003608 continue;
3609
3610 // Check if the most recent declaration has changed the specialization kind
3611 // and removed the need for implicit instantiation.
Douglas Gregoref96ee02012-01-14 16:38:05 +00003612 switch (Var->getMostRecentDecl()->getTemplateSpecializationKind()) {
Chandler Carruth291b4412010-02-13 10:17:50 +00003613 case TSK_Undeclared:
David Blaikieb219cfc2011-09-23 05:06:16 +00003614 llvm_unreachable("Cannot instantitiate an undeclared specialization.");
Chandler Carruth291b4412010-02-13 10:17:50 +00003615 case TSK_ExplicitInstantiationDeclaration:
Chandler Carruth291b4412010-02-13 10:17:50 +00003616 case TSK_ExplicitSpecialization:
Chandler Carruth58e390e2010-08-25 08:27:02 +00003617 continue; // No longer need to instantiate this type.
3618 case TSK_ExplicitInstantiationDefinition:
3619 // We only need an instantiation if the pending instantiation *is* the
3620 // explicit instantiation.
Douglas Gregoref96ee02012-01-14 16:38:05 +00003621 if (Var != Var->getMostRecentDecl()) continue;
Chandler Carruth291b4412010-02-13 10:17:50 +00003622 case TSK_ImplicitInstantiation:
3623 break;
3624 }
3625
John McCallf312b1e2010-08-26 23:41:50 +00003626 PrettyDeclStackTraceEntry CrashInfo(*this, Var, Var->getLocation(),
3627 "instantiating static data member "
3628 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00003629
Chandler Carruth58e390e2010-08-25 08:27:02 +00003630 bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
3631 TSK_ExplicitInstantiationDefinition;
3632 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true,
3633 DefinitionRequired);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003634 }
3635}
John McCall0c01d182010-03-24 05:22:00 +00003636
3637void Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
3638 const MultiLevelTemplateArgumentList &TemplateArgs) {
3639 for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
3640 E = Pattern->ddiag_end(); I != E; ++I) {
3641 DependentDiagnostic *DD = *I;
3642
3643 switch (DD->getKind()) {
3644 case DependentDiagnostic::Access:
3645 HandleDependentAccessCheck(*DD, TemplateArgs);
3646 break;
3647 }
3648 }
3649}