blob: 1515a358ff2f77475057c141fa393efb5dd9403e [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) {
David Blaikie66874fb2013-02-21 01:47:18 +00001641 return SemaRef.SubstParmVarDecl(D, TemplateArgs, /*indexAdjustment*/ 0, None,
1642 /*ExpectParameterPack=*/ false);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001643}
1644
John McCalle29ba202009-08-20 01:44:21 +00001645Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1646 TemplateTypeParmDecl *D) {
1647 // TODO: don't always clone when decls are refcounted.
Chandler Carruth4fb86f82011-05-01 00:51:33 +00001648 assert(D->getTypeForDecl()->isTemplateTypeParmType());
Mike Stump1eb44332009-09-09 15:08:12 +00001649
John McCalle29ba202009-08-20 01:44:21 +00001650 TemplateTypeParmDecl *Inst =
Abramo Bagnara344577e2011-03-06 15:48:19 +00001651 TemplateTypeParmDecl::Create(SemaRef.Context, Owner,
1652 D->getLocStart(), D->getLocation(),
Chandler Carruth4fb86f82011-05-01 00:51:33 +00001653 D->getDepth() - TemplateArgs.getNumLevels(),
1654 D->getIndex(), D->getIdentifier(),
John McCalle29ba202009-08-20 01:44:21 +00001655 D->wasDeclaredWithTypename(),
1656 D->isParameterPack());
Douglas Gregor9a299e02011-03-04 17:52:15 +00001657 Inst->setAccess(AS_public);
John McCalle29ba202009-08-20 01:44:21 +00001658
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001659 if (D->hasDefaultArgument())
1660 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
1661
1662 // Introduce this template parameter's instantiation into the instantiation
Douglas Gregor550d9b22009-10-31 17:21:17 +00001663 // scope.
1664 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001665
John McCalle29ba202009-08-20 01:44:21 +00001666 return Inst;
1667}
1668
Douglas Gregor33642df2009-10-23 23:25:44 +00001669Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1670 NonTypeTemplateParmDecl *D) {
1671 // Substitute into the type of the non-type template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001672 TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc();
Chris Lattner5f9e2722011-07-23 10:55:15 +00001673 SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten;
1674 SmallVector<QualType, 4> ExpandedParameterPackTypes;
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001675 bool IsExpandedParameterPack = false;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001676 TypeSourceInfo *DI;
Douglas Gregor33642df2009-10-23 23:25:44 +00001677 QualType T;
Douglas Gregor33642df2009-10-23 23:25:44 +00001678 bool Invalid = false;
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001679
1680 if (D->isExpandedParameterPack()) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001681 // The non-type template parameter pack is an already-expanded pack
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001682 // expansion of types. Substitute into each of the expanded types.
1683 ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes());
1684 ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes());
1685 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
1686 TypeSourceInfo *NewDI =SemaRef.SubstType(D->getExpansionTypeSourceInfo(I),
1687 TemplateArgs,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001688 D->getLocation(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001689 D->getDeclName());
1690 if (!NewDI)
1691 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001692
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001693 ExpandedParameterPackTypesAsWritten.push_back(NewDI);
1694 QualType NewT =SemaRef.CheckNonTypeTemplateParameterType(NewDI->getType(),
1695 D->getLocation());
1696 if (NewT.isNull())
1697 return 0;
1698 ExpandedParameterPackTypes.push_back(NewT);
1699 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001700
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001701 IsExpandedParameterPack = true;
1702 DI = D->getTypeSourceInfo();
1703 T = DI->getType();
Richard Smith6964b3f2012-09-07 02:06:42 +00001704 } else if (D->isPackExpansion()) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001705 // The non-type template parameter pack's type is a pack expansion of types.
1706 // Determine whether we need to expand this parameter pack into separate
1707 // types.
David Blaikie39e6ab42013-02-18 22:06:02 +00001708 PackExpansionTypeLoc Expansion = TL.castAs<PackExpansionTypeLoc>();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001709 TypeLoc Pattern = Expansion.getPatternLoc();
Chris Lattner5f9e2722011-07-23 10:55:15 +00001710 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001711 SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001712
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001713 // Determine whether the set of unexpanded parameter packs can and should
1714 // be expanded.
1715 bool Expand = true;
1716 bool RetainExpansion = false;
David Blaikiedc84cd52013-02-20 22:23:23 +00001717 Optional<unsigned> OrigNumExpansions
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001718 = Expansion.getTypePtr()->getNumExpansions();
David Blaikiedc84cd52013-02-20 22:23:23 +00001719 Optional<unsigned> NumExpansions = OrigNumExpansions;
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001720 if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(),
1721 Pattern.getSourceRange(),
David Blaikiea71f9d02011-09-22 02:34:54 +00001722 Unexpanded,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001723 TemplateArgs,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001724 Expand, RetainExpansion,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001725 NumExpansions))
1726 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001727
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001728 if (Expand) {
1729 for (unsigned I = 0; I != *NumExpansions; ++I) {
1730 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
1731 TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001732 D->getLocation(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001733 D->getDeclName());
1734 if (!NewDI)
1735 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001736
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001737 ExpandedParameterPackTypesAsWritten.push_back(NewDI);
1738 QualType NewT = SemaRef.CheckNonTypeTemplateParameterType(
1739 NewDI->getType(),
1740 D->getLocation());
1741 if (NewT.isNull())
1742 return 0;
1743 ExpandedParameterPackTypes.push_back(NewT);
1744 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001745
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001746 // Note that we have an expanded parameter pack. The "type" of this
1747 // expanded parameter pack is the original expansion type, but callers
1748 // will end up using the expanded parameter pack types for type-checking.
1749 IsExpandedParameterPack = true;
1750 DI = D->getTypeSourceInfo();
1751 T = DI->getType();
1752 } else {
1753 // We cannot fully expand the pack expansion now, so substitute into the
1754 // pattern and create a new pack expansion type.
1755 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
1756 TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001757 D->getLocation(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001758 D->getDeclName());
1759 if (!NewPattern)
1760 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001761
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001762 DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(),
1763 NumExpansions);
1764 if (!DI)
1765 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001766
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001767 T = DI->getType();
1768 }
1769 } else {
1770 // Simple case: substitution into a parameter that is not a parameter pack.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001771 DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001772 D->getLocation(), D->getDeclName());
1773 if (!DI)
1774 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001775
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001776 // Check that this type is acceptable for a non-type template parameter.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001777 T = SemaRef.CheckNonTypeTemplateParameterType(DI->getType(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001778 D->getLocation());
1779 if (T.isNull()) {
1780 T = SemaRef.Context.IntTy;
1781 Invalid = true;
1782 }
Douglas Gregor33642df2009-10-23 23:25:44 +00001783 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001784
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001785 NonTypeTemplateParmDecl *Param;
1786 if (IsExpandedParameterPack)
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001787 Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001788 D->getInnerLocStart(),
1789 D->getLocation(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001790 D->getDepth() - TemplateArgs.getNumLevels(),
1791 D->getPosition(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001792 D->getIdentifier(), T,
1793 DI,
1794 ExpandedParameterPackTypes.data(),
1795 ExpandedParameterPackTypes.size(),
1796 ExpandedParameterPackTypesAsWritten.data());
1797 else
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001798 Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001799 D->getInnerLocStart(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001800 D->getLocation(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001801 D->getDepth() - TemplateArgs.getNumLevels(),
1802 D->getPosition(),
1803 D->getIdentifier(), T,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001804 D->isParameterPack(), DI);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001805
Douglas Gregor9a299e02011-03-04 17:52:15 +00001806 Param->setAccess(AS_public);
Douglas Gregor33642df2009-10-23 23:25:44 +00001807 if (Invalid)
1808 Param->setInvalidDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001809
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001810 Param->setDefaultArgument(D->getDefaultArgument(), false);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001811
1812 // Introduce this template parameter's instantiation into the instantiation
Douglas Gregor550d9b22009-10-31 17:21:17 +00001813 // scope.
1814 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001815 return Param;
1816}
1817
Richard Smith6964b3f2012-09-07 02:06:42 +00001818static void collectUnexpandedParameterPacks(
1819 Sema &S,
1820 TemplateParameterList *Params,
1821 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
1822 for (TemplateParameterList::const_iterator I = Params->begin(),
1823 E = Params->end(); I != E; ++I) {
1824 if ((*I)->isTemplateParameterPack())
1825 continue;
1826 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*I))
1827 S.collectUnexpandedParameterPacks(NTTP->getTypeSourceInfo()->getTypeLoc(),
1828 Unexpanded);
1829 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(*I))
1830 collectUnexpandedParameterPacks(S, TTP->getTemplateParameters(),
1831 Unexpanded);
1832 }
1833}
1834
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001835Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001836TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1837 TemplateTemplateParmDecl *D) {
1838 // Instantiate the template parameter list of the template template parameter.
1839 TemplateParameterList *TempParams = D->getTemplateParameters();
1840 TemplateParameterList *InstParams;
Richard Smith6964b3f2012-09-07 02:06:42 +00001841 SmallVector<TemplateParameterList*, 8> ExpandedParams;
1842
1843 bool IsExpandedParameterPack = false;
1844
1845 if (D->isExpandedParameterPack()) {
1846 // The template template parameter pack is an already-expanded pack
1847 // expansion of template parameters. Substitute into each of the expanded
1848 // parameters.
1849 ExpandedParams.reserve(D->getNumExpansionTemplateParameters());
1850 for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
1851 I != N; ++I) {
1852 LocalInstantiationScope Scope(SemaRef);
1853 TemplateParameterList *Expansion =
1854 SubstTemplateParams(D->getExpansionTemplateParameters(I));
1855 if (!Expansion)
1856 return 0;
1857 ExpandedParams.push_back(Expansion);
1858 }
1859
1860 IsExpandedParameterPack = true;
1861 InstParams = TempParams;
1862 } else if (D->isPackExpansion()) {
1863 // The template template parameter pack expands to a pack of template
1864 // template parameters. Determine whether we need to expand this parameter
1865 // pack into separate parameters.
1866 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
1867 collectUnexpandedParameterPacks(SemaRef, D->getTemplateParameters(),
1868 Unexpanded);
1869
1870 // Determine whether the set of unexpanded parameter packs can and should
1871 // be expanded.
1872 bool Expand = true;
1873 bool RetainExpansion = false;
David Blaikiedc84cd52013-02-20 22:23:23 +00001874 Optional<unsigned> NumExpansions;
Richard Smith6964b3f2012-09-07 02:06:42 +00001875 if (SemaRef.CheckParameterPacksForExpansion(D->getLocation(),
1876 TempParams->getSourceRange(),
1877 Unexpanded,
1878 TemplateArgs,
1879 Expand, RetainExpansion,
1880 NumExpansions))
1881 return 0;
1882
1883 if (Expand) {
1884 for (unsigned I = 0; I != *NumExpansions; ++I) {
1885 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
1886 LocalInstantiationScope Scope(SemaRef);
1887 TemplateParameterList *Expansion = SubstTemplateParams(TempParams);
1888 if (!Expansion)
1889 return 0;
1890 ExpandedParams.push_back(Expansion);
1891 }
1892
1893 // Note that we have an expanded parameter pack. The "type" of this
1894 // expanded parameter pack is the original expansion type, but callers
1895 // will end up using the expanded parameter pack types for type-checking.
1896 IsExpandedParameterPack = true;
1897 InstParams = TempParams;
1898 } else {
1899 // We cannot fully expand the pack expansion now, so just substitute
1900 // into the pattern.
1901 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
1902
1903 LocalInstantiationScope Scope(SemaRef);
1904 InstParams = SubstTemplateParams(TempParams);
1905 if (!InstParams)
1906 return 0;
1907 }
1908 } else {
Douglas Gregor9106ef72009-11-11 16:58:32 +00001909 // Perform the actual substitution of template parameters within a new,
1910 // local instantiation scope.
John McCall2a7fb272010-08-25 05:32:35 +00001911 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor9106ef72009-11-11 16:58:32 +00001912 InstParams = SubstTemplateParams(TempParams);
1913 if (!InstParams)
Richard Smith6964b3f2012-09-07 02:06:42 +00001914 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001915 }
1916
Douglas Gregor9106ef72009-11-11 16:58:32 +00001917 // Build the template template parameter.
Richard Smith6964b3f2012-09-07 02:06:42 +00001918 TemplateTemplateParmDecl *Param;
1919 if (IsExpandedParameterPack)
1920 Param = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner,
1921 D->getLocation(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001922 D->getDepth() - TemplateArgs.getNumLevels(),
Richard Smith6964b3f2012-09-07 02:06:42 +00001923 D->getPosition(),
1924 D->getIdentifier(), InstParams,
1925 ExpandedParams);
1926 else
1927 Param = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner,
1928 D->getLocation(),
1929 D->getDepth() - TemplateArgs.getNumLevels(),
1930 D->getPosition(),
1931 D->isParameterPack(),
1932 D->getIdentifier(), InstParams);
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001933 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor9a299e02011-03-04 17:52:15 +00001934 Param->setAccess(AS_public);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001935
1936 // Introduce this template parameter's instantiation into the instantiation
Douglas Gregor9106ef72009-11-11 16:58:32 +00001937 // scope.
1938 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001939
Douglas Gregor9106ef72009-11-11 16:58:32 +00001940 return Param;
1941}
1942
Douglas Gregor48c32a72009-11-17 06:07:40 +00001943Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Douglas Gregordb992412011-02-25 16:33:46 +00001944 // Using directives are never dependent (and never contain any types or
1945 // expressions), so they require no explicit instantiation work.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001946
Douglas Gregor48c32a72009-11-17 06:07:40 +00001947 UsingDirectiveDecl *Inst
1948 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001949 D->getNamespaceKeyLocation(),
Douglas Gregordb992412011-02-25 16:33:46 +00001950 D->getQualifierLoc(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001951 D->getIdentLocation(),
1952 D->getNominatedNamespace(),
Douglas Gregor48c32a72009-11-17 06:07:40 +00001953 D->getCommonAncestor());
Abramo Bagnara536afbe2012-09-05 09:55:10 +00001954
1955 // Add the using directive to its declaration context
1956 // only if this is not a function or method.
1957 if (!Owner->isFunctionOrMethod())
1958 Owner->addDecl(Inst);
1959
Douglas Gregor48c32a72009-11-17 06:07:40 +00001960 return Inst;
1961}
1962
John McCalled976492009-12-04 22:46:56 +00001963Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
Douglas Gregor1b398202010-09-29 17:58:28 +00001964
1965 // The nested name specifier may be dependent, for example
1966 // template <typename T> struct t {
1967 // struct s1 { T f1(); };
1968 // struct s2 : s1 { using s1::f1; };
1969 // };
1970 // template struct t<int>;
1971 // Here, in using s1::f1, s1 refers to t<T>::s1;
1972 // we need to substitute for t<int>::s1.
Douglas Gregor5149f372011-02-25 15:54:31 +00001973 NestedNameSpecifierLoc QualifierLoc
1974 = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
1975 TemplateArgs);
1976 if (!QualifierLoc)
Douglas Gregordc355712011-02-25 00:36:19 +00001977 return 0;
Douglas Gregor1b398202010-09-29 17:58:28 +00001978
1979 // The name info is non-dependent, so no transformation
1980 // is required.
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001981 DeclarationNameInfo NameInfo = D->getNameInfo();
John McCalled976492009-12-04 22:46:56 +00001982
John McCall9f54ad42009-12-10 09:41:52 +00001983 // We only need to do redeclaration lookups if we're in a class
1984 // scope (in fact, it's not really even possible in non-class
1985 // scopes).
1986 bool CheckRedeclaration = Owner->isRecord();
1987
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001988 LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
1989 Sema::ForRedeclaration);
John McCall9f54ad42009-12-10 09:41:52 +00001990
John McCalled976492009-12-04 22:46:56 +00001991 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
John McCalled976492009-12-04 22:46:56 +00001992 D->getUsingLocation(),
Douglas Gregor5149f372011-02-25 15:54:31 +00001993 QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001994 NameInfo,
John McCalled976492009-12-04 22:46:56 +00001995 D->isTypeName());
1996
Douglas Gregor5149f372011-02-25 15:54:31 +00001997 CXXScopeSpec SS;
1998 SS.Adopt(QualifierLoc);
John McCall9f54ad42009-12-10 09:41:52 +00001999 if (CheckRedeclaration) {
2000 Prev.setHideTags(false);
2001 SemaRef.LookupQualifiedName(Prev, Owner);
2002
2003 // Check for invalid redeclarations.
2004 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
2005 D->isTypeName(), SS,
2006 D->getLocation(), Prev))
2007 NewUD->setInvalidDecl();
2008
2009 }
2010
2011 if (!NewUD->isInvalidDecl() &&
2012 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
John McCalled976492009-12-04 22:46:56 +00002013 D->getLocation()))
2014 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00002015
John McCalled976492009-12-04 22:46:56 +00002016 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
2017 NewUD->setAccess(D->getAccess());
2018 Owner->addDecl(NewUD);
2019
John McCall9f54ad42009-12-10 09:41:52 +00002020 // Don't process the shadow decls for an invalid decl.
2021 if (NewUD->isInvalidDecl())
2022 return NewUD;
2023
Richard Smithc5a89a12012-04-02 01:30:27 +00002024 if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) {
2025 if (SemaRef.CheckInheritingConstructorUsingDecl(NewUD))
2026 NewUD->setInvalidDecl();
2027 return NewUD;
2028 }
2029
John McCall323c3102009-12-22 22:26:37 +00002030 bool isFunctionScope = Owner->isFunctionOrMethod();
2031
John McCall9f54ad42009-12-10 09:41:52 +00002032 // Process the shadow decls.
2033 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
2034 I != E; ++I) {
2035 UsingShadowDecl *Shadow = *I;
2036 NamedDecl *InstTarget =
Douglas Gregorb7107222011-03-04 19:46:35 +00002037 cast_or_null<NamedDecl>(SemaRef.FindInstantiatedDecl(
2038 Shadow->getLocation(),
2039 Shadow->getTargetDecl(),
2040 TemplateArgs));
2041 if (!InstTarget)
2042 return 0;
John McCall9f54ad42009-12-10 09:41:52 +00002043
2044 if (CheckRedeclaration &&
2045 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
2046 continue;
2047
2048 UsingShadowDecl *InstShadow
2049 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
2050 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
John McCall323c3102009-12-22 22:26:37 +00002051
2052 if (isFunctionScope)
2053 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
John McCall9f54ad42009-12-10 09:41:52 +00002054 }
John McCalled976492009-12-04 22:46:56 +00002055
2056 return NewUD;
2057}
2058
2059Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00002060 // Ignore these; we handle them in bulk when processing the UsingDecl.
2061 return 0;
John McCalled976492009-12-04 22:46:56 +00002062}
2063
John McCall7ba107a2009-11-18 02:36:19 +00002064Decl * TemplateDeclInstantiator
2065 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Douglas Gregor5149f372011-02-25 15:54:31 +00002066 NestedNameSpecifierLoc QualifierLoc
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002067 = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
Douglas Gregor5149f372011-02-25 15:54:31 +00002068 TemplateArgs);
2069 if (!QualifierLoc)
Anders Carlsson0dde18e2009-08-28 15:18:15 +00002070 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002071
Anders Carlsson0dde18e2009-08-28 15:18:15 +00002072 CXXScopeSpec SS;
Douglas Gregor5149f372011-02-25 15:54:31 +00002073 SS.Adopt(QualifierLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002074
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002075 // Since NameInfo refers to a typename, it cannot be a C++ special name.
Benjamin Krameraccaf192012-11-14 15:08:31 +00002076 // Hence, no transformation is required for it.
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002077 DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00002078 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00002079 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002080 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00002081 /*instantiation*/ true,
2082 /*typename*/ true, D->getTypenameLoc());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00002083 if (UD)
John McCalled976492009-12-04 22:46:56 +00002084 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
2085
John McCall7ba107a2009-11-18 02:36:19 +00002086 return UD;
2087}
2088
2089Decl * TemplateDeclInstantiator
2090 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Douglas Gregor5149f372011-02-25 15:54:31 +00002091 NestedNameSpecifierLoc QualifierLoc
2092 = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), TemplateArgs);
2093 if (!QualifierLoc)
John McCall7ba107a2009-11-18 02:36:19 +00002094 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002095
John McCall7ba107a2009-11-18 02:36:19 +00002096 CXXScopeSpec SS;
Douglas Gregor5149f372011-02-25 15:54:31 +00002097 SS.Adopt(QualifierLoc);
John McCall7ba107a2009-11-18 02:36:19 +00002098
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002099 DeclarationNameInfo NameInfo
2100 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
2101
John McCall7ba107a2009-11-18 02:36:19 +00002102 NamedDecl *UD =
2103 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002104 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00002105 /*instantiation*/ true,
2106 /*typename*/ false, SourceLocation());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00002107 if (UD)
John McCalled976492009-12-04 22:46:56 +00002108 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
2109
Anders Carlsson0d8df782009-08-29 19:37:28 +00002110 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00002111}
2112
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002113
2114Decl *TemplateDeclInstantiator::VisitClassScopeFunctionSpecializationDecl(
2115 ClassScopeFunctionSpecializationDecl *Decl) {
2116 CXXMethodDecl *OldFD = Decl->getSpecialization();
Nico Weber6b020092012-06-25 17:21:05 +00002117 CXXMethodDecl *NewFD = cast<CXXMethodDecl>(VisitCXXMethodDecl(OldFD,
2118 0, true));
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002119
2120 LookupResult Previous(SemaRef, NewFD->getNameInfo(), Sema::LookupOrdinaryName,
2121 Sema::ForRedeclaration);
2122
Nico Weber6b020092012-06-25 17:21:05 +00002123 TemplateArgumentListInfo TemplateArgs;
2124 TemplateArgumentListInfo* TemplateArgsPtr = 0;
2125 if (Decl->hasExplicitTemplateArgs()) {
2126 TemplateArgs = Decl->templateArgs();
2127 TemplateArgsPtr = &TemplateArgs;
2128 }
2129
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002130 SemaRef.LookupQualifiedName(Previous, SemaRef.CurContext);
Nico Weber6b020092012-06-25 17:21:05 +00002131 if (SemaRef.CheckFunctionTemplateSpecialization(NewFD, TemplateArgsPtr,
2132 Previous)) {
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002133 NewFD->setInvalidDecl();
2134 return NewFD;
2135 }
2136
2137 // Associate the specialization with the pattern.
2138 FunctionDecl *Specialization = cast<FunctionDecl>(Previous.getFoundDecl());
2139 assert(Specialization && "Class scope Specialization is null");
2140 SemaRef.Context.setClassScopeSpecializationPattern(Specialization, OldFD);
2141
2142 return NewFD;
2143}
2144
John McCallce3ff2b2009-08-25 22:02:44 +00002145Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00002146 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00002147 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor2fa98002010-02-16 19:28:15 +00002148 if (D->isInvalidDecl())
2149 return 0;
2150
Douglas Gregor8dbc2692009-03-17 21:15:40 +00002151 return Instantiator.Visit(D);
2152}
2153
John McCalle29ba202009-08-20 01:44:21 +00002154/// \brief Instantiates a nested template parameter list in the current
2155/// instantiation context.
2156///
2157/// \param L The parameter list to instantiate
2158///
2159/// \returns NULL if there was an error
2160TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00002161TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00002162 // Get errors for all the parameters before bailing out.
2163 bool Invalid = false;
2164
2165 unsigned N = L->size();
Chris Lattner5f9e2722011-07-23 10:55:15 +00002166 typedef SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00002167 ParamVector Params;
2168 Params.reserve(N);
2169 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
2170 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002171 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00002172 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00002173 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00002174 }
2175
2176 // Clean up if we had an error.
Douglas Gregorff331c12010-07-25 18:17:45 +00002177 if (Invalid)
John McCalle29ba202009-08-20 01:44:21 +00002178 return NULL;
John McCalle29ba202009-08-20 01:44:21 +00002179
2180 TemplateParameterList *InstL
2181 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
2182 L->getLAngleLoc(), &Params.front(), N,
2183 L->getRAngleLoc());
2184 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00002185}
John McCalle29ba202009-08-20 01:44:21 +00002186
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002187/// \brief Instantiate the declaration of a class template partial
Douglas Gregored9c0f92009-10-29 00:04:11 +00002188/// specialization.
2189///
2190/// \param ClassTemplate the (instantiated) class template that is partially
2191// specialized by the instantiation of \p PartialSpec.
2192///
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002193/// \param PartialSpec the (uninstantiated) class template partial
Douglas Gregored9c0f92009-10-29 00:04:11 +00002194/// specialization that we are instantiating.
2195///
Douglas Gregord65587f2010-11-10 19:44:59 +00002196/// \returns The instantiated partial specialization, if successful; otherwise,
2197/// NULL to indicate an error.
2198ClassTemplatePartialSpecializationDecl *
Douglas Gregored9c0f92009-10-29 00:04:11 +00002199TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
2200 ClassTemplateDecl *ClassTemplate,
2201 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00002202 // Create a local instantiation scope for this class template partial
2203 // specialization, which will contain the instantiations of the template
2204 // parameters.
John McCall2a7fb272010-08-25 05:32:35 +00002205 LocalInstantiationScope Scope(SemaRef);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002206
Douglas Gregored9c0f92009-10-29 00:04:11 +00002207 // Substitute into the template parameters of the class template partial
2208 // specialization.
2209 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
2210 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
2211 if (!InstParams)
Douglas Gregord65587f2010-11-10 19:44:59 +00002212 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002213
Douglas Gregored9c0f92009-10-29 00:04:11 +00002214 // Substitute into the template arguments of the class template partial
2215 // specialization.
John McCalld5532b62009-11-23 01:53:49 +00002216 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002217 if (SemaRef.Subst(PartialSpec->getTemplateArgsAsWritten(),
2218 PartialSpec->getNumTemplateArgsAsWritten(),
Douglas Gregore02e2622010-12-22 21:19:48 +00002219 InstTemplateArgs, TemplateArgs))
2220 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002221
Douglas Gregored9c0f92009-10-29 00:04:11 +00002222 // Check that the template argument list is well-formed for this
2223 // class template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002224 SmallVector<TemplateArgument, 4> Converted;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002225 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002226 PartialSpec->getLocation(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002227 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002228 false,
2229 Converted))
Douglas Gregord65587f2010-11-10 19:44:59 +00002230 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00002231
2232 // Figure out where to insert this class template partial specialization
2233 // in the member template's set of class template partial specializations.
Douglas Gregored9c0f92009-10-29 00:04:11 +00002234 void *InsertPos = 0;
2235 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00002236 = ClassTemplate->findPartialSpecialization(Converted.data(),
2237 Converted.size(), InsertPos);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002238
Douglas Gregored9c0f92009-10-29 00:04:11 +00002239 // Build the canonical type that describes the converted template
2240 // arguments of the class template partial specialization.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002241 QualType CanonType
Douglas Gregored9c0f92009-10-29 00:04:11 +00002242 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
Douglas Gregor910f8002010-11-07 23:05:16 +00002243 Converted.data(),
2244 Converted.size());
Douglas Gregored9c0f92009-10-29 00:04:11 +00002245
2246 // Build the fully-sugared type for this class template
2247 // specialization as the user wrote in the specialization
2248 // itself. This means that we'll pretty-print the type retrieved
2249 // from the specialization's declaration the way that the user
2250 // actually wrote the specialization, rather than formatting the
2251 // name based on the "canonical" representation used to store the
2252 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00002253 TypeSourceInfo *WrittenTy
2254 = SemaRef.Context.getTemplateSpecializationTypeInfo(
2255 TemplateName(ClassTemplate),
2256 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00002257 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002258 CanonType);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002259
Douglas Gregored9c0f92009-10-29 00:04:11 +00002260 if (PrevDecl) {
2261 // We've already seen a partial specialization with the same template
2262 // parameters and template arguments. This can happen, for example, when
2263 // substituting the outer template arguments ends up causing two
2264 // class template partial specializations of a member class template
2265 // to have identical forms, e.g.,
2266 //
2267 // template<typename T, typename U>
2268 // struct Outer {
2269 // template<typename X, typename Y> struct Inner;
2270 // template<typename Y> struct Inner<T, Y>;
2271 // template<typename Y> struct Inner<U, Y>;
2272 // };
2273 //
2274 // Outer<int, int> outer; // error: the partial specializations of Inner
2275 // // have the same signature.
2276 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
Douglas Gregord65587f2010-11-10 19:44:59 +00002277 << WrittenTy->getType();
Douglas Gregored9c0f92009-10-29 00:04:11 +00002278 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
2279 << SemaRef.Context.getTypeDeclType(PrevDecl);
Douglas Gregord65587f2010-11-10 19:44:59 +00002280 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00002281 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002282
2283
Douglas Gregored9c0f92009-10-29 00:04:11 +00002284 // Create the class template partial specialization declaration.
2285 ClassTemplatePartialSpecializationDecl *InstPartialSpec
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002286 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00002287 PartialSpec->getTagKind(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002288 Owner,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002289 PartialSpec->getLocStart(),
2290 PartialSpec->getLocation(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002291 InstParams,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002292 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00002293 Converted.data(),
2294 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00002295 InstTemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00002296 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00002297 0,
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00002298 ClassTemplate->getNextPartialSpecSequenceNumber());
John McCallb6217662010-03-15 10:12:16 +00002299 // Substitute the nested name specifier, if any.
2300 if (SubstQualifier(PartialSpec, InstPartialSpec))
2301 return 0;
2302
Douglas Gregored9c0f92009-10-29 00:04:11 +00002303 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00002304 InstPartialSpec->setTypeAsWritten(WrittenTy);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002305
Douglas Gregored9c0f92009-10-29 00:04:11 +00002306 // Add this partial specialization to the set of class template partial
2307 // specializations.
Douglas Gregor1e1e9722012-03-28 14:34:23 +00002308 ClassTemplate->AddPartialSpecialization(InstPartialSpec, /*InsertPos=*/0);
Douglas Gregord65587f2010-11-10 19:44:59 +00002309 return InstPartialSpec;
Douglas Gregored9c0f92009-10-29 00:04:11 +00002310}
2311
John McCall21ef0fa2010-03-11 09:03:00 +00002312TypeSourceInfo*
John McCallce3ff2b2009-08-25 22:02:44 +00002313TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002314 SmallVectorImpl<ParmVarDecl *> &Params) {
John McCall21ef0fa2010-03-11 09:03:00 +00002315 TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
2316 assert(OldTInfo && "substituting function without type source info");
2317 assert(Params.empty() && "parameter vector is non-empty at start");
Douglas Gregorcefc3af2012-04-16 07:05:22 +00002318
2319 CXXRecordDecl *ThisContext = 0;
2320 unsigned ThisTypeQuals = 0;
2321 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2322 ThisContext = Method->getParent();
2323 ThisTypeQuals = Method->getTypeQualifiers();
2324 }
2325
John McCall6cd3b9f2010-04-09 17:38:44 +00002326 TypeSourceInfo *NewTInfo
2327 = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
2328 D->getTypeSpecStartLoc(),
Douglas Gregorcefc3af2012-04-16 07:05:22 +00002329 D->getDeclName(),
2330 ThisContext, ThisTypeQuals);
John McCall21ef0fa2010-03-11 09:03:00 +00002331 if (!NewTInfo)
2332 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +00002333
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00002334 if (NewTInfo != OldTInfo) {
2335 // Get parameters from the new type info.
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00002336 TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
David Blaikie39e6ab42013-02-18 22:06:02 +00002337 if (FunctionProtoTypeLoc OldProtoLoc =
2338 OldTL.getAs<FunctionProtoTypeLoc>()) {
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00002339 TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens();
David Blaikie39e6ab42013-02-18 22:06:02 +00002340 FunctionProtoTypeLoc NewProtoLoc = NewTL.castAs<FunctionProtoTypeLoc>();
Richard Smith500d7292012-07-18 01:29:05 +00002341 unsigned NewIdx = 0;
David Blaikie39e6ab42013-02-18 22:06:02 +00002342 for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc.getNumArgs();
Douglas Gregor12c9c002011-01-07 16:43:16 +00002343 OldIdx != NumOldParams; ++OldIdx) {
David Blaikie39e6ab42013-02-18 22:06:02 +00002344 ParmVarDecl *OldParam = OldProtoLoc.getArg(OldIdx);
Richard Smith500d7292012-07-18 01:29:05 +00002345 LocalInstantiationScope *Scope = SemaRef.CurrentInstantiationScope;
2346
David Blaikiedc84cd52013-02-20 22:23:23 +00002347 Optional<unsigned> NumArgumentsInExpansion;
Richard Smith500d7292012-07-18 01:29:05 +00002348 if (OldParam->isParameterPack())
2349 NumArgumentsInExpansion =
2350 SemaRef.getNumArgumentsInExpansion(OldParam->getType(),
2351 TemplateArgs);
2352 if (!NumArgumentsInExpansion) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002353 // Simple case: normal parameter, or a parameter pack that's
Douglas Gregor12c9c002011-01-07 16:43:16 +00002354 // instantiated to a (still-dependent) parameter pack.
David Blaikie39e6ab42013-02-18 22:06:02 +00002355 ParmVarDecl *NewParam = NewProtoLoc.getArg(NewIdx++);
Douglas Gregor12c9c002011-01-07 16:43:16 +00002356 Params.push_back(NewParam);
Richard Smith500d7292012-07-18 01:29:05 +00002357 Scope->InstantiatedLocal(OldParam, NewParam);
2358 } else {
2359 // Parameter pack expansion: make the instantiation an argument pack.
2360 Scope->MakeInstantiatedLocalArgPack(OldParam);
2361 for (unsigned I = 0; I != *NumArgumentsInExpansion; ++I) {
David Blaikie39e6ab42013-02-18 22:06:02 +00002362 ParmVarDecl *NewParam = NewProtoLoc.getArg(NewIdx++);
Richard Smith500d7292012-07-18 01:29:05 +00002363 Params.push_back(NewParam);
2364 Scope->InstantiatedLocalPackArg(OldParam, NewParam);
2365 }
Douglas Gregor12c9c002011-01-07 16:43:16 +00002366 }
Douglas Gregor6920cdc2010-05-03 15:32:18 +00002367 }
Douglas Gregor895162d2010-04-30 18:55:50 +00002368 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00002369 } else {
2370 // The function type itself was not dependent and therefore no
2371 // substitution occurred. However, we still need to instantiate
2372 // the function parameters themselves.
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00002373 TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
David Blaikie39e6ab42013-02-18 22:06:02 +00002374 if (FunctionProtoTypeLoc OldProtoLoc =
2375 OldTL.getAs<FunctionProtoTypeLoc>()) {
2376 for (unsigned i = 0, i_end = OldProtoLoc.getNumArgs(); i != i_end; ++i) {
2377 ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc.getArg(i));
Douglas Gregor6920cdc2010-05-03 15:32:18 +00002378 if (!Parm)
2379 return 0;
2380 Params.push_back(Parm);
2381 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00002382 }
2383 }
John McCall21ef0fa2010-03-11 09:03:00 +00002384 return NewTInfo;
Douglas Gregor5545e162009-03-24 00:38:23 +00002385}
2386
Richard Smithe6975e92012-04-17 00:58:00 +00002387/// Introduce the instantiated function parameters into the local
2388/// instantiation scope, and set the parameter names to those used
2389/// in the template.
2390static void addInstantiatedParametersToScope(Sema &S, FunctionDecl *Function,
2391 const FunctionDecl *PatternDecl,
2392 LocalInstantiationScope &Scope,
2393 const MultiLevelTemplateArgumentList &TemplateArgs) {
2394 unsigned FParamIdx = 0;
2395 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
2396 const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
2397 if (!PatternParam->isParameterPack()) {
2398 // Simple case: not a parameter pack.
2399 assert(FParamIdx < Function->getNumParams());
2400 ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
2401 FunctionParam->setDeclName(PatternParam->getDeclName());
2402 Scope.InstantiatedLocal(PatternParam, FunctionParam);
2403 ++FParamIdx;
2404 continue;
2405 }
2406
2407 // Expand the parameter pack.
2408 Scope.MakeInstantiatedLocalArgPack(PatternParam);
David Blaikiedc84cd52013-02-20 22:23:23 +00002409 Optional<unsigned> NumArgumentsInExpansion
Richard Smithe6975e92012-04-17 00:58:00 +00002410 = S.getNumArgumentsInExpansion(PatternParam->getType(), TemplateArgs);
Richard Smith500d7292012-07-18 01:29:05 +00002411 assert(NumArgumentsInExpansion &&
2412 "should only be called when all template arguments are known");
2413 for (unsigned Arg = 0; Arg < *NumArgumentsInExpansion; ++Arg) {
Richard Smithe6975e92012-04-17 00:58:00 +00002414 ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
2415 FunctionParam->setDeclName(PatternParam->getDeclName());
2416 Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam);
2417 ++FParamIdx;
2418 }
2419 }
2420}
2421
2422static void InstantiateExceptionSpec(Sema &SemaRef, FunctionDecl *New,
2423 const FunctionProtoType *Proto,
2424 const MultiLevelTemplateArgumentList &TemplateArgs) {
Richard Smith13bffc52012-04-19 00:08:28 +00002425 assert(Proto->getExceptionSpecType() != EST_Uninstantiated);
2426
Richard Smithe6975e92012-04-17 00:58:00 +00002427 // C++11 [expr.prim.general]p3:
2428 // If a declaration declares a member function or member function
2429 // template of a class X, the expression this is a prvalue of type
2430 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
2431 // and the end of the function-definition, member-declarator, or
2432 // declarator.
2433 CXXRecordDecl *ThisContext = 0;
2434 unsigned ThisTypeQuals = 0;
2435 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(New)) {
2436 ThisContext = Method->getParent();
2437 ThisTypeQuals = Method->getTypeQualifiers();
2438 }
2439 Sema::CXXThisScopeRAII ThisScope(SemaRef, ThisContext, ThisTypeQuals,
Richard Smith80ad52f2013-01-02 11:42:31 +00002440 SemaRef.getLangOpts().CPlusPlus11);
Richard Smithe6975e92012-04-17 00:58:00 +00002441
2442 // The function has an exception specification or a "noreturn"
2443 // attribute. Substitute into each of the exception types.
2444 SmallVector<QualType, 4> Exceptions;
2445 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
2446 // FIXME: Poor location information!
2447 if (const PackExpansionType *PackExpansion
2448 = Proto->getExceptionType(I)->getAs<PackExpansionType>()) {
2449 // We have a pack expansion. Instantiate it.
2450 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2451 SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(),
2452 Unexpanded);
2453 assert(!Unexpanded.empty() &&
2454 "Pack expansion without parameter packs?");
2455
2456 bool Expand = false;
2457 bool RetainExpansion = false;
David Blaikiedc84cd52013-02-20 22:23:23 +00002458 Optional<unsigned> NumExpansions
Richard Smithe6975e92012-04-17 00:58:00 +00002459 = PackExpansion->getNumExpansions();
2460 if (SemaRef.CheckParameterPacksForExpansion(New->getLocation(),
2461 SourceRange(),
2462 Unexpanded,
2463 TemplateArgs,
2464 Expand,
2465 RetainExpansion,
2466 NumExpansions))
2467 break;
2468
2469 if (!Expand) {
2470 // We can't expand this pack expansion into separate arguments yet;
2471 // just substitute into the pattern and create a new pack expansion
2472 // type.
2473 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
2474 QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2475 TemplateArgs,
2476 New->getLocation(), New->getDeclName());
2477 if (T.isNull())
2478 break;
2479
2480 T = SemaRef.Context.getPackExpansionType(T, NumExpansions);
2481 Exceptions.push_back(T);
2482 continue;
2483 }
2484
2485 // Substitute into the pack expansion pattern for each template
2486 bool Invalid = false;
2487 for (unsigned ArgIdx = 0; ArgIdx != *NumExpansions; ++ArgIdx) {
2488 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, ArgIdx);
2489
2490 QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2491 TemplateArgs,
2492 New->getLocation(), New->getDeclName());
2493 if (T.isNull()) {
2494 Invalid = true;
2495 break;
2496 }
2497
2498 Exceptions.push_back(T);
2499 }
2500
2501 if (Invalid)
2502 break;
2503
2504 continue;
2505 }
2506
2507 QualType T
2508 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
2509 New->getLocation(), New->getDeclName());
2510 if (T.isNull() ||
2511 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
2512 continue;
2513
2514 Exceptions.push_back(T);
2515 }
2516 Expr *NoexceptExpr = 0;
2517 if (Expr *OldNoexceptExpr = Proto->getNoexceptExpr()) {
2518 EnterExpressionEvaluationContext Unevaluated(SemaRef,
2519 Sema::ConstantEvaluated);
2520 ExprResult E = SemaRef.SubstExpr(OldNoexceptExpr, TemplateArgs);
2521 if (E.isUsable())
2522 E = SemaRef.CheckBooleanCondition(E.get(), E.get()->getLocStart());
2523
2524 if (E.isUsable()) {
2525 NoexceptExpr = E.take();
2526 if (!NoexceptExpr->isTypeDependent() &&
2527 !NoexceptExpr->isValueDependent())
Douglas Gregorab41fe92012-05-04 22:38:52 +00002528 NoexceptExpr
2529 = SemaRef.VerifyIntegerConstantExpression(NoexceptExpr,
2530 0, diag::err_noexcept_needs_constant_expression,
2531 /*AllowFold*/ false).take();
Richard Smithe6975e92012-04-17 00:58:00 +00002532 }
2533 }
2534
2535 // Rebuild the function type
2536 const FunctionProtoType *NewProto
2537 = New->getType()->getAs<FunctionProtoType>();
2538 assert(NewProto && "Template instantiation without function prototype?");
2539
2540 FunctionProtoType::ExtProtoInfo EPI = NewProto->getExtProtoInfo();
2541 EPI.ExceptionSpecType = Proto->getExceptionSpecType();
2542 EPI.NumExceptions = Exceptions.size();
2543 EPI.Exceptions = Exceptions.data();
2544 EPI.NoexceptExpr = NoexceptExpr;
2545
2546 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
2547 NewProto->arg_type_begin(),
2548 NewProto->getNumArgs(),
2549 EPI));
2550}
2551
2552void Sema::InstantiateExceptionSpec(SourceLocation PointOfInstantiation,
2553 FunctionDecl *Decl) {
Richard Smith13bffc52012-04-19 00:08:28 +00002554 const FunctionProtoType *Proto = Decl->getType()->castAs<FunctionProtoType>();
2555 if (Proto->getExceptionSpecType() != EST_Uninstantiated)
Richard Smithe6975e92012-04-17 00:58:00 +00002556 return;
2557
2558 InstantiatingTemplate Inst(*this, PointOfInstantiation, Decl,
2559 InstantiatingTemplate::ExceptionSpecification());
Richard Smithb9d0b762012-07-27 04:22:15 +00002560 if (Inst) {
2561 // We hit the instantiation depth limit. Clear the exception specification
2562 // so that our callers don't have to cope with EST_Uninstantiated.
2563 FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
2564 EPI.ExceptionSpecType = EST_None;
2565 Decl->setType(Context.getFunctionType(Proto->getResultType(),
2566 Proto->arg_type_begin(),
2567 Proto->getNumArgs(),
2568 EPI));
Richard Smithe6975e92012-04-17 00:58:00 +00002569 return;
Richard Smithb9d0b762012-07-27 04:22:15 +00002570 }
Richard Smithe6975e92012-04-17 00:58:00 +00002571
2572 // Enter the scope of this instantiation. We don't use
2573 // PushDeclContext because we don't have a scope.
2574 Sema::ContextRAII savedContext(*this, Decl);
2575 LocalInstantiationScope Scope(*this);
2576
2577 MultiLevelTemplateArgumentList TemplateArgs =
2578 getTemplateInstantiationArgs(Decl, 0, /*RelativeToPrimary*/true);
2579
Richard Smith13bffc52012-04-19 00:08:28 +00002580 FunctionDecl *Template = Proto->getExceptionSpecTemplate();
2581 addInstantiatedParametersToScope(*this, Decl, Template, Scope, TemplateArgs);
Richard Smithe6975e92012-04-17 00:58:00 +00002582
Richard Smith13bffc52012-04-19 00:08:28 +00002583 ::InstantiateExceptionSpec(*this, Decl,
2584 Template->getType()->castAs<FunctionProtoType>(),
2585 TemplateArgs);
Richard Smithe6975e92012-04-17 00:58:00 +00002586}
2587
Mike Stump1eb44332009-09-09 15:08:12 +00002588/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00002589/// declaration (New) from the corresponding fields of its template (Tmpl).
2590///
2591/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00002592bool
2593TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00002594 FunctionDecl *Tmpl) {
David Blaikie85f485a2012-07-16 18:50:45 +00002595 if (Tmpl->isDeleted())
Sean Hunt10620eb2011-05-06 20:44:56 +00002596 New->setDeletedAsWritten();
Mike Stump1eb44332009-09-09 15:08:12 +00002597
Douglas Gregorcca9e962009-07-01 22:01:06 +00002598 // If we are performing substituting explicitly-specified template arguments
2599 // or deduced template arguments into a function template and we reach this
2600 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00002601 // to keeping the new function template specialization. We therefore
2602 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00002603 // into a template instantiation for this specific function template
2604 // specialization, which is not a SFINAE context, so that we diagnose any
2605 // further errors in the declaration itself.
2606 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
2607 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
2608 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
2609 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00002610 if (FunctionTemplateDecl *FunTmpl
Nick Lewycky4a9e60f2012-11-16 08:40:59 +00002611 = dyn_cast<FunctionTemplateDecl>(ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002612 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00002613 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00002614 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00002615 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
Nick Lewycky4a9e60f2012-11-16 08:40:59 +00002616 ActiveInst.Entity = New;
Douglas Gregorcca9e962009-07-01 22:01:06 +00002617 }
2618 }
Mike Stump1eb44332009-09-09 15:08:12 +00002619
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002620 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
2621 assert(Proto && "Function template without prototype?");
2622
Sebastian Redl60618fa2011-03-12 11:50:43 +00002623 if (Proto->hasExceptionSpec() || Proto->getNoReturnAttr()) {
John McCalle23cf432010-12-14 08:05:40 +00002624 FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
John McCalle23cf432010-12-14 08:05:40 +00002625
Richard Smithe6975e92012-04-17 00:58:00 +00002626 // DR1330: In C++11, defer instantiation of a non-trivial
2627 // exception specification.
Richard Smith80ad52f2013-01-02 11:42:31 +00002628 if (SemaRef.getLangOpts().CPlusPlus11 &&
Richard Smithe6975e92012-04-17 00:58:00 +00002629 EPI.ExceptionSpecType != EST_None &&
2630 EPI.ExceptionSpecType != EST_DynamicNone &&
2631 EPI.ExceptionSpecType != EST_BasicNoexcept) {
Richard Smith13bffc52012-04-19 00:08:28 +00002632 FunctionDecl *ExceptionSpecTemplate = Tmpl;
2633 if (EPI.ExceptionSpecType == EST_Uninstantiated)
2634 ExceptionSpecTemplate = EPI.ExceptionSpecTemplate;
Richard Smithb9d0b762012-07-27 04:22:15 +00002635 assert(EPI.ExceptionSpecType != EST_Unevaluated &&
2636 "instantiating implicitly-declared special member");
Richard Smith13bffc52012-04-19 00:08:28 +00002637
Richard Smithe6975e92012-04-17 00:58:00 +00002638 // Mark the function has having an uninstantiated exception specification.
2639 const FunctionProtoType *NewProto
2640 = New->getType()->getAs<FunctionProtoType>();
2641 assert(NewProto && "Template instantiation without function prototype?");
2642 EPI = NewProto->getExtProtoInfo();
2643 EPI.ExceptionSpecType = EST_Uninstantiated;
2644 EPI.ExceptionSpecDecl = New;
Richard Smith13bffc52012-04-19 00:08:28 +00002645 EPI.ExceptionSpecTemplate = ExceptionSpecTemplate;
Richard Smithe6975e92012-04-17 00:58:00 +00002646 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
2647 NewProto->arg_type_begin(),
2648 NewProto->getNumArgs(),
2649 EPI));
2650 } else {
2651 ::InstantiateExceptionSpec(SemaRef, New, Proto, TemplateArgs);
2652 }
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002653 }
2654
Rafael Espindola19f74ac2011-07-06 15:46:09 +00002655 // Get the definition. Leaves the variable unchanged if undefined.
Richard Smithe6975e92012-04-17 00:58:00 +00002656 const FunctionDecl *Definition = Tmpl;
Rafael Espindola19f74ac2011-07-06 15:46:09 +00002657 Tmpl->isDefined(Definition);
2658
DeLesley Hutchins23323e02012-01-20 22:50:54 +00002659 SemaRef.InstantiateAttrs(TemplateArgs, Definition, New,
2660 LateAttrs, StartingScope);
Douglas Gregor7cf84d62010-06-15 17:05:35 +00002661
Douglas Gregore53060f2009-06-25 22:08:12 +00002662 return false;
2663}
2664
Douglas Gregor5545e162009-03-24 00:38:23 +00002665/// \brief Initializes common fields of an instantiated method
2666/// declaration (New) from the corresponding fields of its template
2667/// (Tmpl).
2668///
2669/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00002670bool
2671TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00002672 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00002673 if (InitFunctionInstantiation(New, Tmpl))
2674 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002675
Douglas Gregor5545e162009-03-24 00:38:23 +00002676 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00002677 if (Tmpl->isVirtualAsWritten())
Douglas Gregor85606eb2010-09-28 20:50:54 +00002678 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +00002679
2680 // FIXME: attributes
2681 // FIXME: New needs a pointer to Tmpl
2682 return false;
2683}
Douglas Gregora58861f2009-05-13 20:28:22 +00002684
2685/// \brief Instantiate the definition of the given function from its
2686/// template.
2687///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002688/// \param PointOfInstantiation the point at which the instantiation was
2689/// required. Note that this is not precisely a "point of instantiation"
2690/// for the function, but it's close.
2691///
Douglas Gregora58861f2009-05-13 20:28:22 +00002692/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002693/// function template specialization or member function of a class template
2694/// specialization.
2695///
2696/// \param Recursive if true, recursively instantiates any functions that
2697/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002698///
2699/// \param DefinitionRequired if true, then we are performing an explicit
2700/// instantiation where the body of the function is required. Complain if
2701/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002702void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002703 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002704 bool Recursive,
2705 bool DefinitionRequired) {
Sean Hunt10620eb2011-05-06 20:44:56 +00002706 if (Function->isInvalidDecl() || Function->isDefined())
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002707 return;
2708
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002709 // Never instantiate an explicit specialization except if it is a class scope
2710 // explicit specialization.
2711 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization &&
2712 !Function->getClassScopeSpecializationPattern())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002713 return;
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002714
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002715 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00002716 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Sean Huntf996e052011-05-27 20:00:14 +00002717 assert(PatternDecl && "instantiating a non-template");
2718
2719 Stmt *Pattern = PatternDecl->getBody(PatternDecl);
2720 assert(PatternDecl && "template definition is not a template");
2721 if (!Pattern) {
2722 // Try to find a defaulted definition
2723 PatternDecl->isDefined(PatternDecl);
Sean Huntdfab8542011-05-25 22:02:25 +00002724 }
Sean Huntf996e052011-05-27 20:00:14 +00002725 assert(PatternDecl && "template definition is not a template");
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002726
Francois Pichet8387e2a2011-04-22 22:18:13 +00002727 // Postpone late parsed template instantiations.
Sean Huntf996e052011-05-27 20:00:14 +00002728 if (PatternDecl->isLateTemplateParsed() &&
Nick Lewycky8a29bc02011-05-12 03:51:24 +00002729 !LateTemplateParser) {
Francois Pichet8387e2a2011-04-22 22:18:13 +00002730 PendingInstantiations.push_back(
2731 std::make_pair(Function, PointOfInstantiation));
2732 return;
2733 }
2734
2735 // Call the LateTemplateParser callback if there a need to late parse
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002736 // a templated function definition.
Sean Huntf996e052011-05-27 20:00:14 +00002737 if (!Pattern && PatternDecl->isLateTemplateParsed() &&
Francois Pichet8387e2a2011-04-22 22:18:13 +00002738 LateTemplateParser) {
Francois Pichet4a47e8d2011-04-23 11:52:20 +00002739 LateTemplateParser(OpaqueParser, PatternDecl);
Francois Pichet8387e2a2011-04-22 22:18:13 +00002740 Pattern = PatternDecl->getBody(PatternDecl);
2741 }
2742
Sean Huntf996e052011-05-27 20:00:14 +00002743 if (!Pattern && !PatternDecl->isDefaulted()) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002744 if (DefinitionRequired) {
2745 if (Function->getPrimaryTemplate())
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002746 Diag(PointOfInstantiation,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002747 diag::err_explicit_instantiation_undefined_func_template)
2748 << Function->getPrimaryTemplate();
2749 else
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002750 Diag(PointOfInstantiation,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002751 diag::err_explicit_instantiation_undefined_member)
2752 << 1 << Function->getDeclName() << Function->getDeclContext();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002753
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002754 if (PatternDecl)
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002755 Diag(PatternDecl->getLocation(),
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002756 diag::note_explicit_instantiation_here);
Douglas Gregorcfe833b2010-05-17 17:57:54 +00002757 Function->setInvalidDecl();
Chandler Carruth58e390e2010-08-25 08:27:02 +00002758 } else if (Function->getTemplateSpecializationKind()
2759 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002760 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00002761 std::make_pair(Function, PointOfInstantiation));
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002762 }
Chandler Carruth58e390e2010-08-25 08:27:02 +00002763
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002764 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002765 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002766
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002767 // C++0x [temp.explicit]p9:
2768 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00002769 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002770 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00002771 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002772 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002773 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002774 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002775
Richard Smithd4497dd2013-01-25 00:08:28 +00002776 if (PatternDecl->isInlined())
2777 Function->setImplicitlyInline();
2778
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002779 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2780 if (Inst)
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002781 return;
2782
Abramo Bagnarae9946242011-11-18 08:08:52 +00002783 // Copy the inner loc start from the pattern.
2784 Function->setInnerLocStart(PatternDecl->getInnerLocStart());
2785
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002786 // If we're performing recursive template instantiation, create our own
2787 // queue of pending implicit instantiations that we will instantiate later,
2788 // while we're still within our own instantiation context.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002789 SmallVector<VTableUse, 16> SavedVTableUses;
Chandler Carruth62c78d52010-08-25 08:44:16 +00002790 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002791 if (Recursive) {
2792 VTableUses.swap(SavedVTableUses);
Chandler Carruth62c78d52010-08-25 08:44:16 +00002793 PendingInstantiations.swap(SavedPendingInstantiations);
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002794 }
Mike Stump1eb44332009-09-09 15:08:12 +00002795
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002796 EnterExpressionEvaluationContext EvalContext(*this,
John McCallf312b1e2010-08-26 23:41:50 +00002797 Sema::PotentiallyEvaluated);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002798
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002799 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00002800 // recorded, unless we're actually a member function within a local
2801 // class, in which case we need to merge our results with the parent
2802 // scope (of the enclosing function).
2803 bool MergeWithParentScope = false;
2804 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
2805 MergeWithParentScope = Rec->isLocalClass();
2806
2807 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002808
Richard Smith1d28caf2012-12-11 01:14:52 +00002809 if (PatternDecl->isDefaulted())
Sean Huntcd10dec2011-05-23 23:14:04 +00002810 SetDeclDefaulted(Function, PatternDecl->getLocation());
Richard Smith1d28caf2012-12-11 01:14:52 +00002811 else {
2812 ActOnStartOfFunctionDef(0, Function);
2813
2814 // Enter the scope of this instantiation. We don't use
2815 // PushDeclContext because we don't have a scope.
2816 Sema::ContextRAII savedContext(*this, Function);
2817
2818 MultiLevelTemplateArgumentList TemplateArgs =
2819 getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
2820
2821 addInstantiatedParametersToScope(*this, Function, PatternDecl, Scope,
2822 TemplateArgs);
2823
Sean Huntcd10dec2011-05-23 23:14:04 +00002824 // If this is a constructor, instantiate the member initializers.
2825 if (const CXXConstructorDecl *Ctor =
2826 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2827 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2828 TemplateArgs);
2829 }
2830
2831 // Instantiate the function body.
2832 StmtResult Body = SubstStmt(Pattern, TemplateArgs);
2833
2834 if (Body.isInvalid())
2835 Function->setInvalidDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002836
Sean Huntcd10dec2011-05-23 23:14:04 +00002837 ActOnFinishFunctionBody(Function, Body.get(),
2838 /*IsInstantiation=*/true);
Richard Smith1d28caf2012-12-11 01:14:52 +00002839
2840 PerformDependentDiagnostics(PatternDecl, TemplateArgs);
2841
2842 savedContext.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00002843 }
2844
Douglas Gregoraba43bb2009-05-26 20:50:29 +00002845 DeclGroupRef DG(Function);
2846 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00002847
Douglas Gregor60406be2010-01-16 22:29:39 +00002848 // This class may have local implicit instantiations that need to be
2849 // instantiation within this scope.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002850 PerformPendingInstantiations(/*LocalOnly=*/true);
Douglas Gregor60406be2010-01-16 22:29:39 +00002851 Scope.Exit();
2852
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002853 if (Recursive) {
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002854 // Define any pending vtables.
2855 DefineUsedVTables();
2856
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002857 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002858 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002859 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002860
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002861 // Restore the set of pending vtables.
Nick Lewycky81559102011-05-31 07:58:42 +00002862 assert(VTableUses.empty() &&
2863 "VTableUses should be empty before it is discarded.");
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002864 VTableUses.swap(SavedVTableUses);
2865
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002866 // Restore the set of pending implicit instantiations.
Nick Lewycky81559102011-05-31 07:58:42 +00002867 assert(PendingInstantiations.empty() &&
2868 "PendingInstantiations should be empty before it is discarded.");
Chandler Carruth62c78d52010-08-25 08:44:16 +00002869 PendingInstantiations.swap(SavedPendingInstantiations);
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002870 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002871}
2872
2873/// \brief Instantiate the definition of the given variable from its
2874/// template.
2875///
Douglas Gregor7caa6822009-07-24 20:34:43 +00002876/// \param PointOfInstantiation the point at which the instantiation was
2877/// required. Note that this is not precisely a "point of instantiation"
2878/// for the function, but it's close.
2879///
2880/// \param Var the already-instantiated declaration of a static member
2881/// variable of a class template specialization.
2882///
2883/// \param Recursive if true, recursively instantiates any functions that
2884/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002885///
2886/// \param DefinitionRequired if true, then we are performing an explicit
2887/// instantiation where an out-of-line definition of the member variable
2888/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002889void Sema::InstantiateStaticDataMemberDefinition(
2890 SourceLocation PointOfInstantiation,
2891 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002892 bool Recursive,
2893 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002894 if (Var->isInvalidDecl())
2895 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002896
Douglas Gregor7caa6822009-07-24 20:34:43 +00002897 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002898 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002899 assert(Def && "This data member was not instantiated from a template?");
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002900 assert(Def->isStaticDataMember() && "Not a static data member?");
Douglas Gregor0d035142009-10-27 18:42:08 +00002901 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002902
Douglas Gregor0d035142009-10-27 18:42:08 +00002903 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002904 // We did not find an out-of-line definition of this static data member,
2905 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00002906 // instantiate this definition (or provide a specialization for it) in
2907 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002908 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00002909 Def = Var->getInstantiatedFromStaticDataMember();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002910 Diag(PointOfInstantiation,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002911 diag::err_explicit_instantiation_undefined_member)
2912 << 2 << Var->getDeclName() << Var->getDeclContext();
2913 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
Chandler Carruth58e390e2010-08-25 08:27:02 +00002914 } else if (Var->getTemplateSpecializationKind()
2915 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002916 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00002917 std::make_pair(Var, PointOfInstantiation));
2918 }
2919
Douglas Gregor7caa6822009-07-24 20:34:43 +00002920 return;
2921 }
2922
Rafael Espindola234fe652012-03-05 10:54:55 +00002923 TemplateSpecializationKind TSK = Var->getTemplateSpecializationKind();
2924
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002925 // Never instantiate an explicit specialization.
Rafael Espindola234fe652012-03-05 10:54:55 +00002926 if (TSK == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002927 return;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002928
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002929 // C++0x [temp.explicit]p9:
2930 // Except for inline functions, other explicit instantiation declarations
2931 // have the effect of suppressing the implicit instantiation of the entity
2932 // to which they refer.
Rafael Espindola234fe652012-03-05 10:54:55 +00002933 if (TSK == TSK_ExplicitInstantiationDeclaration)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002934 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002935
Rafael Espindola02503932012-03-08 15:51:03 +00002936 Consumer.HandleCXXStaticMemberVarInstantiation(Var);
2937
Douglas Gregorf15748a2011-06-03 03:35:07 +00002938 // If we already have a definition, we're done.
Nick Lewycky95e38722012-04-04 02:38:36 +00002939 if (VarDecl *Def = Var->getDefinition()) {
2940 // We may be explicitly instantiating something we've already implicitly
2941 // instantiated.
2942 Def->setTemplateSpecializationKind(Var->getTemplateSpecializationKind(),
2943 PointOfInstantiation);
Douglas Gregorf15748a2011-06-03 03:35:07 +00002944 return;
Nick Lewycky95e38722012-04-04 02:38:36 +00002945 }
Douglas Gregorf15748a2011-06-03 03:35:07 +00002946
Douglas Gregor7caa6822009-07-24 20:34:43 +00002947 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
2948 if (Inst)
2949 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002950
Douglas Gregor7caa6822009-07-24 20:34:43 +00002951 // If we're performing recursive template instantiation, create our own
2952 // queue of pending implicit instantiations that we will instantiate later,
2953 // while we're still within our own instantiation context.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002954 SmallVector<VTableUse, 16> SavedVTableUses;
Chandler Carruth62c78d52010-08-25 08:44:16 +00002955 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Nick Lewycky81559102011-05-31 07:58:42 +00002956 if (Recursive) {
2957 VTableUses.swap(SavedVTableUses);
Chandler Carruth62c78d52010-08-25 08:44:16 +00002958 PendingInstantiations.swap(SavedPendingInstantiations);
Nick Lewycky81559102011-05-31 07:58:42 +00002959 }
Mike Stump1eb44332009-09-09 15:08:12 +00002960
Douglas Gregor7caa6822009-07-24 20:34:43 +00002961 // Enter the scope of this instantiation. We don't use
2962 // PushDeclContext because we don't have a scope.
John McCallf5ba7e02011-02-14 20:37:25 +00002963 ContextRAII previousContext(*this, Var->getDeclContext());
Douglas Gregor7bdc1522012-02-16 21:36:18 +00002964 LocalInstantiationScope Local(*this);
2965
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002966 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00002967 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Nico Weber6bb4dcb2010-11-28 22:53:37 +00002968 getTemplateInstantiationArgs(Var)));
John McCallf5ba7e02011-02-14 20:37:25 +00002969
2970 previousContext.pop();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002971
2972 if (Var) {
Douglas Gregor583f33b2009-10-15 18:07:02 +00002973 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2974 assert(MSInfo && "Missing member specialization information?");
2975 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2976 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00002977 DeclGroupRef DG(Var);
2978 Consumer.HandleTopLevelDecl(DG);
2979 }
Douglas Gregor7bdc1522012-02-16 21:36:18 +00002980 Local.Exit();
2981
Douglas Gregor7caa6822009-07-24 20:34:43 +00002982 if (Recursive) {
Nick Lewycky81559102011-05-31 07:58:42 +00002983 // Define any newly required vtables.
2984 DefineUsedVTables();
2985
Douglas Gregor7caa6822009-07-24 20:34:43 +00002986 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002987 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002988 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002989
Nick Lewycky81559102011-05-31 07:58:42 +00002990 // Restore the set of pending vtables.
2991 assert(VTableUses.empty() &&
2992 "VTableUses should be empty before it is discarded, "
2993 "while instantiating static data member.");
2994 VTableUses.swap(SavedVTableUses);
2995
Douglas Gregor7caa6822009-07-24 20:34:43 +00002996 // Restore the set of pending implicit instantiations.
Nick Lewycky81559102011-05-31 07:58:42 +00002997 assert(PendingInstantiations.empty() &&
2998 "PendingInstantiations should be empty before it is discarded, "
2999 "while instantiating static data member.");
Chandler Carruth62c78d52010-08-25 08:44:16 +00003000 PendingInstantiations.swap(SavedPendingInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00003001 }
Douglas Gregora58861f2009-05-13 20:28:22 +00003002}
Douglas Gregor815215d2009-05-27 05:35:12 +00003003
Anders Carlsson09025312009-08-29 05:16:22 +00003004void
3005Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
3006 const CXXConstructorDecl *Tmpl,
3007 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00003008
Richard Trieu90ab75b2011-09-09 03:18:59 +00003009 SmallVector<CXXCtorInitializer*, 4> NewInits;
Richard Smith54b3ba82012-09-25 00:23:05 +00003010 bool AnyErrors = Tmpl->isInvalidDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003011
Anders Carlsson09025312009-08-29 05:16:22 +00003012 // Instantiate all the initializers.
3013 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00003014 InitsEnd = Tmpl->init_end();
3015 Inits != InitsEnd; ++Inits) {
Sean Huntcbb67482011-01-08 20:30:50 +00003016 CXXCtorInitializer *Init = *Inits;
Anders Carlsson09025312009-08-29 05:16:22 +00003017
Chandler Carruth030ef472010-09-03 21:54:20 +00003018 // Only instantiate written initializers, let Sema re-construct implicit
3019 // ones.
3020 if (!Init->isWritten())
3021 continue;
3022
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003023 SourceLocation EllipsisLoc;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003024
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003025 if (Init->isPackExpansion()) {
3026 // This is a pack expansion. We should expand it now.
Douglas Gregor76852c22011-11-01 01:16:03 +00003027 TypeLoc BaseTL = Init->getTypeSourceInfo()->getTypeLoc();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003028 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003029 collectUnexpandedParameterPacks(BaseTL, Unexpanded);
3030 bool ShouldExpand = false;
Douglas Gregord3731192011-01-10 07:32:04 +00003031 bool RetainExpansion = false;
David Blaikiedc84cd52013-02-20 22:23:23 +00003032 Optional<unsigned> NumExpansions;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003033 if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(),
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003034 BaseTL.getSourceRange(),
David Blaikiea71f9d02011-09-22 02:34:54 +00003035 Unexpanded,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003036 TemplateArgs, ShouldExpand,
Douglas Gregord3731192011-01-10 07:32:04 +00003037 RetainExpansion,
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003038 NumExpansions)) {
3039 AnyErrors = true;
3040 New->setInvalidDecl();
3041 continue;
3042 }
3043 assert(ShouldExpand && "Partial instantiation of base initializer?");
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003044
3045 // Loop over all of the arguments in the argument pack(s),
Douglas Gregorcded4f62011-01-14 17:04:44 +00003046 for (unsigned I = 0; I != *NumExpansions; ++I) {
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003047 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
3048
3049 // Instantiate the initializer.
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003050 ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs,
3051 /*CXXDirectInit=*/true);
3052 if (TempInit.isInvalid()) {
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003053 AnyErrors = true;
3054 break;
3055 }
3056
3057 // Instantiate the base type.
Douglas Gregor76852c22011-11-01 01:16:03 +00003058 TypeSourceInfo *BaseTInfo = SubstType(Init->getTypeSourceInfo(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003059 TemplateArgs,
3060 Init->getSourceLocation(),
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003061 New->getDeclName());
3062 if (!BaseTInfo) {
3063 AnyErrors = true;
3064 break;
3065 }
3066
3067 // Build the initializer.
Sebastian Redl6df65482011-09-24 17:48:25 +00003068 MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(),
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003069 BaseTInfo, TempInit.take(),
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003070 New->getParent(),
3071 SourceLocation());
3072 if (NewInit.isInvalid()) {
3073 AnyErrors = true;
3074 break;
3075 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003076
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003077 NewInits.push_back(NewInit.get());
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003078 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003079
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003080 continue;
3081 }
3082
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00003083 // Instantiate the initializer.
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003084 ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs,
3085 /*CXXDirectInit=*/true);
3086 if (TempInit.isInvalid()) {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00003087 AnyErrors = true;
3088 continue;
Anders Carlsson09025312009-08-29 05:16:22 +00003089 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003090
Anders Carlsson09025312009-08-29 05:16:22 +00003091 MemInitResult NewInit;
Douglas Gregor76852c22011-11-01 01:16:03 +00003092 if (Init->isDelegatingInitializer() || Init->isBaseInitializer()) {
3093 TypeSourceInfo *TInfo = SubstType(Init->getTypeSourceInfo(),
3094 TemplateArgs,
3095 Init->getSourceLocation(),
3096 New->getDeclName());
3097 if (!TInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00003098 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00003099 New->setInvalidDecl();
3100 continue;
3101 }
Sebastian Redl6df65482011-09-24 17:48:25 +00003102
Douglas Gregor76852c22011-11-01 01:16:03 +00003103 if (Init->isBaseInitializer())
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003104 NewInit = BuildBaseInitializer(TInfo->getType(), TInfo, TempInit.take(),
Douglas Gregor76852c22011-11-01 01:16:03 +00003105 New->getParent(), EllipsisLoc);
3106 else
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003107 NewInit = BuildDelegatingInitializer(TInfo, TempInit.take(),
Douglas Gregor76852c22011-11-01 01:16:03 +00003108 cast<CXXRecordDecl>(CurContext->getParent()));
Anders Carlsson09025312009-08-29 05:16:22 +00003109 } else if (Init->isMemberInitializer()) {
Douglas Gregorb7107222011-03-04 19:46:35 +00003110 FieldDecl *Member = cast_or_null<FieldDecl>(FindInstantiatedDecl(
Francois Pichet00eb3f92010-12-04 09:14:42 +00003111 Init->getMemberLocation(),
3112 Init->getMember(),
3113 TemplateArgs));
Douglas Gregorb7107222011-03-04 19:46:35 +00003114 if (!Member) {
3115 AnyErrors = true;
3116 New->setInvalidDecl();
3117 continue;
3118 }
Mike Stump1eb44332009-09-09 15:08:12 +00003119
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003120 NewInit = BuildMemberInitializer(Member, TempInit.take(),
Sebastian Redl6df65482011-09-24 17:48:25 +00003121 Init->getSourceLocation());
Francois Pichet00eb3f92010-12-04 09:14:42 +00003122 } else if (Init->isIndirectMemberInitializer()) {
3123 IndirectFieldDecl *IndirectMember =
Douglas Gregorb7107222011-03-04 19:46:35 +00003124 cast_or_null<IndirectFieldDecl>(FindInstantiatedDecl(
Francois Pichet00eb3f92010-12-04 09:14:42 +00003125 Init->getMemberLocation(),
3126 Init->getIndirectMember(), TemplateArgs));
3127
Douglas Gregorb7107222011-03-04 19:46:35 +00003128 if (!IndirectMember) {
3129 AnyErrors = true;
3130 New->setInvalidDecl();
Sebastian Redl6df65482011-09-24 17:48:25 +00003131 continue;
Douglas Gregorb7107222011-03-04 19:46:35 +00003132 }
Sebastian Redl6df65482011-09-24 17:48:25 +00003133
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003134 NewInit = BuildMemberInitializer(IndirectMember, TempInit.take(),
Sebastian Redl6df65482011-09-24 17:48:25 +00003135 Init->getSourceLocation());
Anders Carlsson09025312009-08-29 05:16:22 +00003136 }
3137
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00003138 if (NewInit.isInvalid()) {
3139 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00003140 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00003141 } else {
Richard Trieu90ab75b2011-09-09 03:18:59 +00003142 NewInits.push_back(NewInit.get());
Anders Carlsson09025312009-08-29 05:16:22 +00003143 }
3144 }
Mike Stump1eb44332009-09-09 15:08:12 +00003145
Anders Carlsson09025312009-08-29 05:16:22 +00003146 // Assign all the initializers to the new constructor.
John McCalld226f652010-08-21 09:40:31 +00003147 ActOnMemInitializers(New,
Anders Carlsson09025312009-08-29 05:16:22 +00003148 /*FIXME: ColonLoc */
3149 SourceLocation(),
David Blaikie93c86172013-01-17 05:26:25 +00003150 NewInits,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00003151 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00003152}
3153
John McCall52a575a2009-08-29 08:11:13 +00003154// TODO: this could be templated if the various decl types used the
3155// same method name.
3156static bool isInstantiationOf(ClassTemplateDecl *Pattern,
3157 ClassTemplateDecl *Instance) {
3158 Pattern = Pattern->getCanonicalDecl();
3159
3160 do {
3161 Instance = Instance->getCanonicalDecl();
3162 if (Pattern == Instance) return true;
3163 Instance = Instance->getInstantiatedFromMemberTemplate();
3164 } while (Instance);
3165
3166 return false;
3167}
3168
Douglas Gregor0d696532009-09-28 06:34:35 +00003169static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
3170 FunctionTemplateDecl *Instance) {
3171 Pattern = Pattern->getCanonicalDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003172
Douglas Gregor0d696532009-09-28 06:34:35 +00003173 do {
3174 Instance = Instance->getCanonicalDecl();
3175 if (Pattern == Instance) return true;
3176 Instance = Instance->getInstantiatedFromMemberTemplate();
3177 } while (Instance);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003178
Douglas Gregor0d696532009-09-28 06:34:35 +00003179 return false;
3180}
3181
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003182static bool
Douglas Gregored9c0f92009-10-29 00:04:11 +00003183isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
3184 ClassTemplatePartialSpecializationDecl *Instance) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003185 Pattern
Douglas Gregored9c0f92009-10-29 00:04:11 +00003186 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
3187 do {
3188 Instance = cast<ClassTemplatePartialSpecializationDecl>(
3189 Instance->getCanonicalDecl());
3190 if (Pattern == Instance)
3191 return true;
3192 Instance = Instance->getInstantiatedFromMember();
3193 } while (Instance);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003194
Douglas Gregored9c0f92009-10-29 00:04:11 +00003195 return false;
3196}
3197
John McCall52a575a2009-08-29 08:11:13 +00003198static bool isInstantiationOf(CXXRecordDecl *Pattern,
3199 CXXRecordDecl *Instance) {
3200 Pattern = Pattern->getCanonicalDecl();
3201
3202 do {
3203 Instance = Instance->getCanonicalDecl();
3204 if (Pattern == Instance) return true;
3205 Instance = Instance->getInstantiatedFromMemberClass();
3206 } while (Instance);
3207
3208 return false;
3209}
3210
3211static bool isInstantiationOf(FunctionDecl *Pattern,
3212 FunctionDecl *Instance) {
3213 Pattern = Pattern->getCanonicalDecl();
3214
3215 do {
3216 Instance = Instance->getCanonicalDecl();
3217 if (Pattern == Instance) return true;
3218 Instance = Instance->getInstantiatedFromMemberFunction();
3219 } while (Instance);
3220
3221 return false;
3222}
3223
3224static bool isInstantiationOf(EnumDecl *Pattern,
3225 EnumDecl *Instance) {
3226 Pattern = Pattern->getCanonicalDecl();
3227
3228 do {
3229 Instance = Instance->getCanonicalDecl();
3230 if (Pattern == Instance) return true;
3231 Instance = Instance->getInstantiatedFromMemberEnum();
3232 } while (Instance);
3233
3234 return false;
3235}
3236
John McCalled976492009-12-04 22:46:56 +00003237static bool isInstantiationOf(UsingShadowDecl *Pattern,
3238 UsingShadowDecl *Instance,
3239 ASTContext &C) {
3240 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
3241}
3242
3243static bool isInstantiationOf(UsingDecl *Pattern,
3244 UsingDecl *Instance,
3245 ASTContext &C) {
3246 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
3247}
3248
John McCall7ba107a2009-11-18 02:36:19 +00003249static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
3250 UsingDecl *Instance,
3251 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00003252 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00003253}
3254
3255static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00003256 UsingDecl *Instance,
3257 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00003258 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00003259}
3260
John McCall52a575a2009-08-29 08:11:13 +00003261static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
3262 VarDecl *Instance) {
3263 assert(Instance->isStaticDataMember());
3264
3265 Pattern = Pattern->getCanonicalDecl();
3266
3267 do {
3268 Instance = Instance->getCanonicalDecl();
3269 if (Pattern == Instance) return true;
3270 Instance = Instance->getInstantiatedFromStaticDataMember();
3271 } while (Instance);
3272
3273 return false;
3274}
3275
John McCalled976492009-12-04 22:46:56 +00003276// Other is the prospective instantiation
3277// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00003278static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00003279 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00003280 if (UnresolvedUsingTypenameDecl *UUD
3281 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
3282 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
3283 return isInstantiationOf(UUD, UD, Ctx);
3284 }
3285 }
3286
3287 if (UnresolvedUsingValueDecl *UUD
3288 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00003289 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
3290 return isInstantiationOf(UUD, UD, Ctx);
3291 }
3292 }
Douglas Gregor815215d2009-05-27 05:35:12 +00003293
Anders Carlsson0d8df782009-08-29 19:37:28 +00003294 return false;
3295 }
Mike Stump1eb44332009-09-09 15:08:12 +00003296
John McCall52a575a2009-08-29 08:11:13 +00003297 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
3298 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00003299
John McCall52a575a2009-08-29 08:11:13 +00003300 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
3301 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00003302
John McCall52a575a2009-08-29 08:11:13 +00003303 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
3304 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00003305
Douglas Gregor7caa6822009-07-24 20:34:43 +00003306 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00003307 if (Var->isStaticDataMember())
3308 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
3309
3310 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
3311 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00003312
Douglas Gregor0d696532009-09-28 06:34:35 +00003313 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
3314 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
3315
Douglas Gregored9c0f92009-10-29 00:04:11 +00003316 if (ClassTemplatePartialSpecializationDecl *PartialSpec
3317 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
3318 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
3319 PartialSpec);
3320
Anders Carlssond8b285f2009-09-01 04:26:58 +00003321 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
3322 if (!Field->getDeclName()) {
3323 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00003324 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00003325 cast<FieldDecl>(D);
3326 }
3327 }
Mike Stump1eb44332009-09-09 15:08:12 +00003328
John McCalled976492009-12-04 22:46:56 +00003329 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
3330 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
3331
3332 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
3333 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
3334
Douglas Gregor815215d2009-05-27 05:35:12 +00003335 return D->getDeclName() && isa<NamedDecl>(Other) &&
3336 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
3337}
3338
3339template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00003340static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00003341 NamedDecl *D,
3342 ForwardIterator first,
3343 ForwardIterator last) {
3344 for (; first != last; ++first)
3345 if (isInstantiationOf(Ctx, D, *first))
3346 return cast<NamedDecl>(*first);
3347
3348 return 0;
3349}
3350
John McCall02cace72009-08-28 07:59:38 +00003351/// \brief Finds the instantiation of the given declaration context
3352/// within the current instantiation.
3353///
3354/// \returns NULL if there was an error
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003355DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
Douglas Gregore95b4092009-09-16 18:34:49 +00003356 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00003357 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003358 Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00003359 return cast_or_null<DeclContext>(ID);
3360 } else return DC;
3361}
3362
Douglas Gregored961e72009-05-27 17:54:46 +00003363/// \brief Find the instantiation of the given declaration within the
3364/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00003365///
3366/// This routine is intended to be used when \p D is a declaration
3367/// referenced from within a template, that needs to mapped into the
3368/// corresponding declaration within an instantiation. For example,
3369/// given:
3370///
3371/// \code
3372/// template<typename T>
3373/// struct X {
3374/// enum Kind {
3375/// KnownValue = sizeof(T)
3376/// };
3377///
3378/// bool getKind() const { return KnownValue; }
3379/// };
3380///
3381/// template struct X<int>;
3382/// \endcode
3383///
3384/// In the instantiation of X<int>::getKind(), we need to map the
3385/// EnumConstantDecl for KnownValue (which refers to
James Dennettf198d122012-06-17 03:36:08 +00003386/// X<T>::\<Kind>\::KnownValue) to its instantiation
James Dennettef2b5b32012-06-15 22:23:43 +00003387/// (X<int>::\<Kind>\::KnownValue). InstantiateCurrentDeclRef() performs
Douglas Gregored961e72009-05-27 17:54:46 +00003388/// this mapping from within the instantiation of X<int>.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003389NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
Douglas Gregore95b4092009-09-16 18:34:49 +00003390 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00003391 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00003392 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00003393 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
Douglas Gregor7bdc1522012-02-16 21:36:18 +00003394 (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext()) ||
3395 (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda())) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00003396 // D is a local of some kind. Look into the map of local
3397 // declarations to their instantiations.
Chris Lattnerd8e54992011-02-17 19:47:42 +00003398 typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
3399 llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found
3400 = CurrentInstantiationScope->findInstantiationOf(D);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003401
Chris Lattner57ad3782011-02-17 20:34:02 +00003402 if (Found) {
3403 if (Decl *FD = Found->dyn_cast<Decl *>())
3404 return cast<NamedDecl>(FD);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003405
Richard Smith9a4db032012-09-12 00:56:43 +00003406 int PackIdx = ArgumentPackSubstitutionIndex;
3407 assert(PackIdx != -1 && "found declaration pack but not pack expanding");
Chris Lattner57ad3782011-02-17 20:34:02 +00003408 return cast<NamedDecl>((*Found->get<DeclArgumentPack *>())[PackIdx]);
3409 }
3410
3411 // If we didn't find the decl, then we must have a label decl that hasn't
3412 // been found yet. Lazily instantiate it and return it now.
3413 assert(isa<LabelDecl>(D));
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003414
Chris Lattner57ad3782011-02-17 20:34:02 +00003415 Decl *Inst = SubstDecl(D, CurContext, TemplateArgs);
3416 assert(Inst && "Failed to instantiate label??");
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003417
Chris Lattner57ad3782011-02-17 20:34:02 +00003418 CurrentInstantiationScope->InstantiatedLocal(D, Inst);
3419 return cast<LabelDecl>(Inst);
Douglas Gregor2bba76b2009-05-27 17:07:49 +00003420 }
Douglas Gregor815215d2009-05-27 05:35:12 +00003421
Douglas Gregore95b4092009-09-16 18:34:49 +00003422 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
3423 if (!Record->isDependentContext())
3424 return D;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003425
Douglas Gregor2c1227c2011-11-07 17:43:18 +00003426 // Determine whether this record is the "templated" declaration describing
3427 // a class template or class template partial specialization.
Douglas Gregore95b4092009-09-16 18:34:49 +00003428 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
Douglas Gregor2c1227c2011-11-07 17:43:18 +00003429 if (ClassTemplate)
3430 ClassTemplate = ClassTemplate->getCanonicalDecl();
3431 else if (ClassTemplatePartialSpecializationDecl *PartialSpec
3432 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record))
3433 ClassTemplate = PartialSpec->getSpecializedTemplate()->getCanonicalDecl();
3434
3435 // Walk the current context to find either the record or an instantiation of
3436 // it.
3437 DeclContext *DC = CurContext;
3438 while (!DC->isFileContext()) {
3439 // If we're performing substitution while we're inside the template
3440 // definition, we'll find our own context. We're done.
3441 if (DC->Equals(Record))
3442 return Record;
3443
3444 if (CXXRecordDecl *InstRecord = dyn_cast<CXXRecordDecl>(DC)) {
3445 // Check whether we're in the process of instantiating a class template
3446 // specialization of the template we're mapping.
3447 if (ClassTemplateSpecializationDecl *InstSpec
3448 = dyn_cast<ClassTemplateSpecializationDecl>(InstRecord)){
3449 ClassTemplateDecl *SpecTemplate = InstSpec->getSpecializedTemplate();
3450 if (ClassTemplate && isInstantiationOf(ClassTemplate, SpecTemplate))
3451 return InstRecord;
3452 }
3453
3454 // Check whether we're in the process of instantiating a member class.
3455 if (isInstantiationOf(Record, InstRecord))
3456 return InstRecord;
Douglas Gregore95b4092009-09-16 18:34:49 +00003457 }
Douglas Gregor2c1227c2011-11-07 17:43:18 +00003458
3459
3460 // Move to the outer template scope.
3461 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) {
3462 if (FD->getFriendObjectKind() && FD->getDeclContext()->isFileContext()){
3463 DC = FD->getLexicalDeclContext();
3464 continue;
3465 }
John McCall52a575a2009-08-29 08:11:13 +00003466 }
Douglas Gregor2c1227c2011-11-07 17:43:18 +00003467
3468 DC = DC->getParent();
John McCall52a575a2009-08-29 08:11:13 +00003469 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00003470
Douglas Gregore95b4092009-09-16 18:34:49 +00003471 // Fall through to deal with other dependent record types (e.g.,
3472 // anonymous unions in class templates).
3473 }
John McCall52a575a2009-08-29 08:11:13 +00003474
Douglas Gregore95b4092009-09-16 18:34:49 +00003475 if (!ParentDC->isDependentContext())
3476 return D;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003477
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003478 ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00003479 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00003480 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003481
Douglas Gregor815215d2009-05-27 05:35:12 +00003482 if (ParentDC != D->getDeclContext()) {
3483 // We performed some kind of instantiation in the parent context,
3484 // so now we need to look into the instantiated parent context to
3485 // find the instantiation of the declaration D.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003486
John McCall3cb0ebd2010-03-10 03:28:59 +00003487 // If our context used to be dependent, we may need to instantiate
3488 // it before performing lookup into that context.
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00003489 bool IsBeingInstantiated = false;
John McCall3cb0ebd2010-03-10 03:28:59 +00003490 if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003491 if (!Spec->isDependentContext()) {
3492 QualType T = Context.getTypeDeclType(Spec);
John McCall3cb0ebd2010-03-10 03:28:59 +00003493 const RecordType *Tag = T->getAs<RecordType>();
3494 assert(Tag && "type of non-dependent record is not a RecordType");
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00003495 if (Tag->isBeingDefined())
3496 IsBeingInstantiated = true;
John McCall3cb0ebd2010-03-10 03:28:59 +00003497 if (!Tag->isBeingDefined() &&
3498 RequireCompleteType(Loc, T, diag::err_incomplete_type))
3499 return 0;
Douglas Gregora43064c2010-11-05 23:22:45 +00003500
3501 ParentDC = Tag->getDecl();
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003502 }
3503 }
3504
Douglas Gregor815215d2009-05-27 05:35:12 +00003505 NamedDecl *Result = 0;
3506 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003507 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
David Blaikie3bc93e32012-12-19 00:45:41 +00003508 Result = findInstantiationOf(Context, D, Found.begin(), Found.end());
Douglas Gregor815215d2009-05-27 05:35:12 +00003509 } else {
3510 // Since we don't have a name for the entity we're looking for,
3511 // our only option is to walk through all of the declarations to
3512 // find that name. This will occur in a few cases:
3513 //
3514 // - anonymous struct/union within a template
3515 // - unnamed class/struct/union/enum within a template
3516 //
3517 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00003518 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003519 ParentDC->decls_begin(),
3520 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00003521 }
Mike Stump1eb44332009-09-09 15:08:12 +00003522
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00003523 if (!Result) {
3524 if (isa<UsingShadowDecl>(D)) {
3525 // UsingShadowDecls can instantiate to nothing because of using hiding.
3526 } else if (Diags.hasErrorOccurred()) {
3527 // We've already complained about something, so most likely this
3528 // declaration failed to instantiate. There's no point in complaining
3529 // further, since this is normal in invalid code.
3530 } else if (IsBeingInstantiated) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003531 // The class in which this member exists is currently being
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00003532 // instantiated, and we haven't gotten around to instantiating this
3533 // member yet. This can happen when the code uses forward declarations
3534 // of member classes, and introduces ordering dependencies via
3535 // template instantiation.
3536 Diag(Loc, diag::err_member_not_yet_instantiated)
3537 << D->getDeclName()
3538 << Context.getTypeDeclType(cast<CXXRecordDecl>(ParentDC));
3539 Diag(D->getLocation(), diag::note_non_instantiated_member_here);
Richard Smith0724b7c2012-03-26 20:28:16 +00003540 } else if (EnumConstantDecl *ED = dyn_cast<EnumConstantDecl>(D)) {
3541 // This enumeration constant was found when the template was defined,
3542 // but can't be found in the instantiation. This can happen if an
3543 // unscoped enumeration member is explicitly specialized.
3544 EnumDecl *Enum = cast<EnumDecl>(ED->getLexicalDeclContext());
3545 EnumDecl *Spec = cast<EnumDecl>(FindInstantiatedDecl(Loc, Enum,
3546 TemplateArgs));
3547 assert(Spec->getTemplateSpecializationKind() ==
3548 TSK_ExplicitSpecialization);
3549 Diag(Loc, diag::err_enumerator_does_not_exist)
3550 << D->getDeclName()
3551 << Context.getTypeDeclType(cast<TypeDecl>(Spec->getDeclContext()));
3552 Diag(Spec->getLocation(), diag::note_enum_specialized_here)
3553 << Context.getTypeDeclType(Spec);
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00003554 } else {
3555 // We should have found something, but didn't.
3556 llvm_unreachable("Unable to find instantiation of declaration!");
3557 }
3558 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003559
Douglas Gregor815215d2009-05-27 05:35:12 +00003560 D = Result;
3561 }
3562
Douglas Gregor815215d2009-05-27 05:35:12 +00003563 return D;
3564}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003565
Mike Stump1eb44332009-09-09 15:08:12 +00003566/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003567/// instantiations we have seen until this point.
Nick Lewycky81559102011-05-31 07:58:42 +00003568void Sema::PerformPendingInstantiations(bool LocalOnly) {
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00003569 // Load pending instantiations from the external source.
3570 if (!LocalOnly && ExternalSource) {
Richard Smithb9d0b762012-07-27 04:22:15 +00003571 SmallVector<PendingImplicitInstantiation, 4> Pending;
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00003572 ExternalSource->ReadPendingInstantiations(Pending);
3573 PendingInstantiations.insert(PendingInstantiations.begin(),
3574 Pending.begin(), Pending.end());
3575 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003576
Douglas Gregor60406be2010-01-16 22:29:39 +00003577 while (!PendingLocalImplicitInstantiations.empty() ||
Chandler Carruth62c78d52010-08-25 08:44:16 +00003578 (!LocalOnly && !PendingInstantiations.empty())) {
Douglas Gregor60406be2010-01-16 22:29:39 +00003579 PendingImplicitInstantiation Inst;
3580
3581 if (PendingLocalImplicitInstantiations.empty()) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00003582 Inst = PendingInstantiations.front();
3583 PendingInstantiations.pop_front();
Douglas Gregor60406be2010-01-16 22:29:39 +00003584 } else {
3585 Inst = PendingLocalImplicitInstantiations.front();
3586 PendingLocalImplicitInstantiations.pop_front();
3587 }
Mike Stump1eb44332009-09-09 15:08:12 +00003588
Douglas Gregor7caa6822009-07-24 20:34:43 +00003589 // Instantiate function definitions
3590 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
John McCallf312b1e2010-08-26 23:41:50 +00003591 PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(),
3592 "instantiating function definition");
Chandler Carruth58e390e2010-08-25 08:27:02 +00003593 bool DefinitionRequired = Function->getTemplateSpecializationKind() ==
3594 TSK_ExplicitInstantiationDefinition;
3595 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true,
3596 DefinitionRequired);
Douglas Gregor7caa6822009-07-24 20:34:43 +00003597 continue;
3598 }
Mike Stump1eb44332009-09-09 15:08:12 +00003599
Douglas Gregor7caa6822009-07-24 20:34:43 +00003600 // Instantiate static data member definitions.
3601 VarDecl *Var = cast<VarDecl>(Inst.first);
3602 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00003603
Chandler Carruth291b4412010-02-13 10:17:50 +00003604 // Don't try to instantiate declarations if the most recent redeclaration
3605 // is invalid.
Douglas Gregoref96ee02012-01-14 16:38:05 +00003606 if (Var->getMostRecentDecl()->isInvalidDecl())
Chandler Carruth291b4412010-02-13 10:17:50 +00003607 continue;
3608
3609 // Check if the most recent declaration has changed the specialization kind
3610 // and removed the need for implicit instantiation.
Douglas Gregoref96ee02012-01-14 16:38:05 +00003611 switch (Var->getMostRecentDecl()->getTemplateSpecializationKind()) {
Chandler Carruth291b4412010-02-13 10:17:50 +00003612 case TSK_Undeclared:
David Blaikieb219cfc2011-09-23 05:06:16 +00003613 llvm_unreachable("Cannot instantitiate an undeclared specialization.");
Chandler Carruth291b4412010-02-13 10:17:50 +00003614 case TSK_ExplicitInstantiationDeclaration:
Chandler Carruth291b4412010-02-13 10:17:50 +00003615 case TSK_ExplicitSpecialization:
Chandler Carruth58e390e2010-08-25 08:27:02 +00003616 continue; // No longer need to instantiate this type.
3617 case TSK_ExplicitInstantiationDefinition:
3618 // We only need an instantiation if the pending instantiation *is* the
3619 // explicit instantiation.
Douglas Gregoref96ee02012-01-14 16:38:05 +00003620 if (Var != Var->getMostRecentDecl()) continue;
Chandler Carruth291b4412010-02-13 10:17:50 +00003621 case TSK_ImplicitInstantiation:
3622 break;
3623 }
3624
John McCallf312b1e2010-08-26 23:41:50 +00003625 PrettyDeclStackTraceEntry CrashInfo(*this, Var, Var->getLocation(),
3626 "instantiating static data member "
3627 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00003628
Chandler Carruth58e390e2010-08-25 08:27:02 +00003629 bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
3630 TSK_ExplicitInstantiationDefinition;
3631 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true,
3632 DefinitionRequired);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003633 }
3634}
John McCall0c01d182010-03-24 05:22:00 +00003635
3636void Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
3637 const MultiLevelTemplateArgumentList &TemplateArgs) {
3638 for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
3639 E = Pattern->ddiag_end(); I != E; ++I) {
3640 DependentDiagnostic *DD = *I;
3641
3642 switch (DD->getKind()) {
3643 case DependentDiagnostic::Access:
3644 HandleDependentAccessCheck(*DD, TemplateArgs);
3645 break;
3646 }
3647 }
3648}