blob: b679d49215bed8651c5c4a29f9d7df40a01875ae [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())
Aaron Ballmanfc685ac2012-06-19 22:09:27 +000082 AddAlignedAttr(Aligned->getLocation(), New, Result.takeAs<Expr>(),
83 Aligned->getIsMSDeclSpec());
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)
Aaron Ballmanfc685ac2012-06-19 22:09:27 +000090 AddAlignedAttr(Aligned->getLocation(), New, Result,
91 Aligned->getIsMSDeclSpec());
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
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000350 // Link instantiations of static data members back to the template from
351 // which they were instantiated.
352 if (Var->isStaticDataMember())
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000353 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000354 TSK_ImplicitInstantiation);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000355
Douglas Gregor60c93c92010-02-09 07:26:29 +0000356 if (Var->getAnyInitializer()) {
357 // We already have an initializer in the class.
358 } else if (D->getInit()) {
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000359 if (Var->isStaticDataMember() && !D->isOutOfLine())
Richard Smithadb1d4c2012-07-22 23:45:10 +0000360 SemaRef.PushExpressionEvaluationContext(Sema::ConstantEvaluated, D);
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000361 else
Richard Smithadb1d4c2012-07-22 23:45:10 +0000362 SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated, D);
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000363
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000364 // Instantiate the initializer.
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000365 ExprResult Init = SemaRef.SubstInitializer(D->getInit(), TemplateArgs,
366 D->getInitStyle() == VarDecl::CallInit);
367 if (!Init.isInvalid()) {
Richard Smith34b41d92011-02-20 03:19:35 +0000368 bool TypeMayContainAuto = true;
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000369 if (Init.get()) {
370 bool DirectInit = D->isDirectInit();
371 SemaRef.AddInitializerToDecl(Var, Init.take(), DirectInit,
372 TypeMayContainAuto);
373 } else
Eli Friedman6aeaa602012-01-05 22:34:08 +0000374 SemaRef.ActOnUninitializedDecl(Var, TypeMayContainAuto);
Douglas Gregor6eef5192009-12-14 19:27:10 +0000375 } else {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000376 // FIXME: Not too happy about invalidating the declaration
377 // because of a bogus initializer.
378 Var->setInvalidDecl();
Douglas Gregor6eef5192009-12-14 19:27:10 +0000379 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000380
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000381 SemaRef.PopExpressionEvaluationContext();
Richard Smithad762fc2011-04-14 22:09:26 +0000382 } else if ((!Var->isStaticDataMember() || Var->isOutOfLine()) &&
383 !Var->isCXXForRangeDecl())
John McCalld226f652010-08-21 09:40:31 +0000384 SemaRef.ActOnUninitializedDecl(Var, false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000385
Richard Smithe3499ca2011-06-21 23:42:09 +0000386 // Diagnose unused local variables with dependent types, where the diagnostic
387 // will have been deferred.
388 if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed() &&
389 D->getType()->isDependentType())
Douglas Gregor5764f612010-05-08 23:05:03 +0000390 SemaRef.DiagnoseUnusedDecl(Var);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +0000391
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000392 return Var;
393}
394
Abramo Bagnara6206d532010-06-05 05:09:32 +0000395Decl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
396 AccessSpecDecl* AD
397 = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
398 D->getAccessSpecifierLoc(), D->getColonLoc());
399 Owner->addHiddenDecl(AD);
400 return AD;
401}
402
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000403Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
404 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000405 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor561f8122011-07-01 01:22:09 +0000406 if (DI->getType()->isInstantiationDependentType() ||
Douglas Gregor836adf62010-05-24 17:22:01 +0000407 DI->getType()->isVariablyModifiedType()) {
John McCall07fb6be2009-10-22 23:33:21 +0000408 DI = SemaRef.SubstType(DI, TemplateArgs,
409 D->getLocation(), D->getDeclName());
410 if (!DI) {
John McCalla93c9342009-12-07 02:54:59 +0000411 DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000412 Invalid = true;
413 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000414 // C++ [temp.arg.type]p3:
415 // If a declaration acquires a function type through a type
416 // dependent on a template-parameter and this causes a
417 // declaration that does not use the syntactic form of a
418 // function declarator to have function type, the program is
419 // ill-formed.
420 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000421 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000422 Invalid = true;
423 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000424 } else {
425 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000426 }
427
428 Expr *BitWidth = D->getBitWidth();
429 if (Invalid)
430 BitWidth = 0;
431 else if (BitWidth) {
Richard Smithf6702a32011-12-20 02:08:33 +0000432 // The bit-width expression is a constant expression.
433 EnterExpressionEvaluationContext Unevaluated(SemaRef,
434 Sema::ConstantEvaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000435
John McCall60d7b3a2010-08-24 06:29:42 +0000436 ExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000437 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000438 if (InstantiatedBitWidth.isInvalid()) {
439 Invalid = true;
440 BitWidth = 0;
441 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000442 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000443 }
444
John McCall07fb6be2009-10-22 23:33:21 +0000445 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
446 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000447 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000448 D->getLocation(),
449 D->isMutable(),
450 BitWidth,
Richard Smithca523302012-06-10 03:12:00 +0000451 D->getInClassInitStyle(),
Richard Smith703b6012012-05-23 04:22:22 +0000452 D->getInnerLocStart(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000453 D->getAccess(),
454 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000455 if (!Field) {
456 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000457 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000458 }
Mike Stump1eb44332009-09-09 15:08:12 +0000459
DeLesley Hutchins23323e02012-01-20 22:50:54 +0000460 SemaRef.InstantiateAttrs(TemplateArgs, D, Field, LateAttrs, StartingScope);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000461
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000462 if (Invalid)
463 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000465 if (!Field->getDeclName()) {
466 // Keep track of where this decl came from.
467 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000468 }
Douglas Gregor9901c572010-05-21 00:31:19 +0000469 if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
470 if (Parent->isAnonymousStructOrUnion() &&
Sebastian Redl7a126a42010-08-31 00:36:30 +0000471 Parent->getRedeclContext()->isFunctionOrMethod())
Douglas Gregor9901c572010-05-21 00:31:19 +0000472 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000473 }
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000475 Field->setImplicit(D->isImplicit());
John McCall46460a62010-01-20 21:53:11 +0000476 Field->setAccess(D->getAccess());
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000477 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000478
479 return Field;
480}
481
Francois Pichet87c2e122010-11-21 06:08:52 +0000482Decl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
483 NamedDecl **NamedChain =
484 new (SemaRef.Context)NamedDecl*[D->getChainingSize()];
485
486 int i = 0;
487 for (IndirectFieldDecl::chain_iterator PI =
488 D->chain_begin(), PE = D->chain_end();
Douglas Gregorb7107222011-03-04 19:46:35 +0000489 PI != PE; ++PI) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000490 NamedDecl *Next = SemaRef.FindInstantiatedDecl(D->getLocation(), *PI,
Douglas Gregorb7107222011-03-04 19:46:35 +0000491 TemplateArgs);
492 if (!Next)
493 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000494
Douglas Gregorb7107222011-03-04 19:46:35 +0000495 NamedChain[i++] = Next;
496 }
Francois Pichet87c2e122010-11-21 06:08:52 +0000497
Francois Pichet40e17752010-12-09 10:07:54 +0000498 QualType T = cast<FieldDecl>(NamedChain[i-1])->getType();
Francois Pichet87c2e122010-11-21 06:08:52 +0000499 IndirectFieldDecl* IndirectField
500 = IndirectFieldDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Francois Pichet40e17752010-12-09 10:07:54 +0000501 D->getIdentifier(), T,
Francois Pichet87c2e122010-11-21 06:08:52 +0000502 NamedChain, D->getChainingSize());
503
504
505 IndirectField->setImplicit(D->isImplicit());
506 IndirectField->setAccess(D->getAccess());
507 Owner->addDecl(IndirectField);
508 return IndirectField;
509}
510
John McCall02cace72009-08-28 07:59:38 +0000511Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
John McCall02cace72009-08-28 07:59:38 +0000512 // Handle friend type expressions by simply substituting template
Douglas Gregor06245bf2010-04-07 17:57:12 +0000513 // parameters into the pattern type and checking the result.
John McCall32f2fb52010-03-25 18:04:51 +0000514 if (TypeSourceInfo *Ty = D->getFriendType()) {
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000515 TypeSourceInfo *InstTy;
516 // If this is an unsupported friend, don't bother substituting template
517 // arguments into it. The actual type referred to won't be used by any
518 // parts of Clang, and may not be valid for instantiating. Just use the
519 // same info for the instantiated friend.
520 if (D->isUnsupportedFriend()) {
521 InstTy = Ty;
522 } else {
523 InstTy = SemaRef.SubstType(Ty, TemplateArgs,
524 D->getLocation(), DeclarationName());
525 }
526 if (!InstTy)
Douglas Gregor7557a132009-12-24 20:56:24 +0000527 return 0;
John McCall02cace72009-08-28 07:59:38 +0000528
Richard Smithd6f80da2012-09-20 01:31:00 +0000529 FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getLocStart(),
Abramo Bagnara0216df82011-10-29 20:52:52 +0000530 D->getFriendLoc(), InstTy);
Douglas Gregor06245bf2010-04-07 17:57:12 +0000531 if (!FD)
532 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000533
Douglas Gregor06245bf2010-04-07 17:57:12 +0000534 FD->setAccess(AS_public);
John McCall9a34edb2010-10-19 01:40:49 +0000535 FD->setUnsupportedFriend(D->isUnsupportedFriend());
Douglas Gregor06245bf2010-04-07 17:57:12 +0000536 Owner->addDecl(FD);
537 return FD;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000538 }
539
Douglas Gregor06245bf2010-04-07 17:57:12 +0000540 NamedDecl *ND = D->getFriendDecl();
541 assert(ND && "friend decl must be a decl or a type!");
542
John McCallaf2094e2010-04-08 09:05:18 +0000543 // All of the Visit implementations for the various potential friend
544 // declarations have to be carefully written to work for friend
545 // objects, with the most important detail being that the target
546 // decl should almost certainly not be placed in Owner.
547 Decl *NewND = Visit(ND);
Douglas Gregor06245bf2010-04-07 17:57:12 +0000548 if (!NewND) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000549
John McCall02cace72009-08-28 07:59:38 +0000550 FriendDecl *FD =
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000551 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor06245bf2010-04-07 17:57:12 +0000552 cast<NamedDecl>(NewND), D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000553 FD->setAccess(AS_public);
John McCall9a34edb2010-10-19 01:40:49 +0000554 FD->setUnsupportedFriend(D->isUnsupportedFriend());
John McCall02cace72009-08-28 07:59:38 +0000555 Owner->addDecl(FD);
556 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000557}
558
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000559Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
560 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000561
Richard Smithf6702a32011-12-20 02:08:33 +0000562 // The expression in a static assertion is a constant expression.
563 EnterExpressionEvaluationContext Unevaluated(SemaRef,
564 Sema::ConstantEvaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000565
John McCall60d7b3a2010-08-24 06:29:42 +0000566 ExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000567 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000568 if (InstantiatedAssertExpr.isInvalid())
569 return 0;
570
Richard Smithe3f470a2012-07-11 22:37:56 +0000571 return SemaRef.BuildStaticAssertDeclaration(D->getLocation(),
John McCall9ae2f072010-08-23 23:25:46 +0000572 InstantiatedAssertExpr.get(),
Richard Smithe3f470a2012-07-11 22:37:56 +0000573 D->getMessage(),
574 D->getRParenLoc(),
575 D->isFailed());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000576}
577
578Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Richard Smith38f0df32012-03-26 04:58:10 +0000579 EnumDecl *PrevDecl = 0;
580 if (D->getPreviousDecl()) {
581 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
582 D->getPreviousDecl(),
583 TemplateArgs);
584 if (!Prev) return 0;
585 PrevDecl = cast<EnumDecl>(Prev);
586 }
587
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000588 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000589 D->getLocation(), D->getIdentifier(),
Richard Smith38f0df32012-03-26 04:58:10 +0000590 PrevDecl, D->isScoped(),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +0000591 D->isScopedUsingClassTag(), D->isFixed());
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000592 if (D->isFixed()) {
Richard Smithf1c66b42012-03-14 23:13:10 +0000593 if (TypeSourceInfo *TI = D->getIntegerTypeSourceInfo()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000594 // If we have type source information for the underlying type, it means it
595 // has been explicitly set by the user. Perform substitution on it before
596 // moving on.
597 SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
Richard Smithf1c66b42012-03-14 23:13:10 +0000598 TypeSourceInfo *NewTI = SemaRef.SubstType(TI, TemplateArgs, UnderlyingLoc,
599 DeclarationName());
600 if (!NewTI || SemaRef.CheckEnumUnderlyingType(NewTI))
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000601 Enum->setIntegerType(SemaRef.Context.IntTy);
Richard Smithf1c66b42012-03-14 23:13:10 +0000602 else
603 Enum->setIntegerTypeSourceInfo(NewTI);
604 } else {
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000605 assert(!D->getIntegerType()->isDependentType()
606 && "Dependent type without type source info");
607 Enum->setIntegerType(D->getIntegerType());
608 }
609 }
610
John McCall5b629aa2010-10-22 23:36:17 +0000611 SemaRef.InstantiateAttrs(TemplateArgs, D, Enum);
612
Richard Smithf1c66b42012-03-14 23:13:10 +0000613 Enum->setInstantiationOfMemberEnum(D, TSK_ImplicitInstantiation);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000614 Enum->setAccess(D->getAccess());
John McCallb6217662010-03-15 10:12:16 +0000615 if (SubstQualifier(D, Enum)) return 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000616 Owner->addDecl(Enum);
Richard Smithf1c66b42012-03-14 23:13:10 +0000617
Richard Smith4ca93d92012-03-26 04:08:46 +0000618 EnumDecl *Def = D->getDefinition();
619 if (Def && Def != D) {
620 // If this is an out-of-line definition of an enum member template, check
621 // that the underlying types match in the instantiation of both
622 // declarations.
623 if (TypeSourceInfo *TI = Def->getIntegerTypeSourceInfo()) {
624 SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
625 QualType DefnUnderlying =
626 SemaRef.SubstType(TI->getType(), TemplateArgs,
627 UnderlyingLoc, DeclarationName());
628 SemaRef.CheckEnumRedeclaration(Def->getLocation(), Def->isScoped(),
629 DefnUnderlying, Enum);
630 }
631 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000632
Douglas Gregor96084f12010-03-01 19:00:07 +0000633 if (D->getDeclContext()->isFunctionOrMethod())
634 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000635
Richard Smithf1c66b42012-03-14 23:13:10 +0000636 // C++11 [temp.inst]p1: The implicit instantiation of a class template
637 // specialization causes the implicit instantiation of the declarations, but
638 // not the definitions of scoped member enumerations.
639 // FIXME: There appears to be no wording for what happens for an enum defined
Richard Smith38f0df32012-03-26 04:58:10 +0000640 // within a block scope, but we treat that much like a member template. Only
641 // instantiate the definition when visiting the definition in that case, since
642 // we will visit all redeclarations.
643 if (!Enum->isScoped() && Def &&
644 (!D->getDeclContext()->isFunctionOrMethod() || D->isCompleteDefinition()))
Richard Smith4ca93d92012-03-26 04:08:46 +0000645 InstantiateEnumDefinition(Enum, Def);
Richard Smithf1c66b42012-03-14 23:13:10 +0000646
647 return Enum;
648}
649
650void TemplateDeclInstantiator::InstantiateEnumDefinition(
651 EnumDecl *Enum, EnumDecl *Pattern) {
652 Enum->startDefinition();
653
Richard Smith1af83c42012-03-23 03:33:32 +0000654 // Update the location to refer to the definition.
655 Enum->setLocation(Pattern->getLocation());
656
Chris Lattner5f9e2722011-07-23 10:55:15 +0000657 SmallVector<Decl*, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000658
659 EnumConstantDecl *LastEnumConst = 0;
Richard Smithf1c66b42012-03-14 23:13:10 +0000660 for (EnumDecl::enumerator_iterator EC = Pattern->enumerator_begin(),
661 ECEnd = Pattern->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000662 EC != ECEnd; ++EC) {
663 // The specified value for the enumerator.
John McCall60d7b3a2010-08-24 06:29:42 +0000664 ExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000665 if (Expr *UninstValue = EC->getInitExpr()) {
Richard Smithf6702a32011-12-20 02:08:33 +0000666 // The enumerator's value expression is a constant expression.
Mike Stump1eb44332009-09-09 15:08:12 +0000667 EnterExpressionEvaluationContext Unevaluated(SemaRef,
Richard Smithf6702a32011-12-20 02:08:33 +0000668 Sema::ConstantEvaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000669
John McCallce3ff2b2009-08-25 22:02:44 +0000670 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000671 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000672
673 // Drop the initial value and continue.
674 bool isInvalid = false;
675 if (Value.isInvalid()) {
676 Value = SemaRef.Owned((Expr *)0);
677 isInvalid = true;
678 }
679
Mike Stump1eb44332009-09-09 15:08:12 +0000680 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000681 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
682 EC->getLocation(), EC->getIdentifier(),
John McCall9ae2f072010-08-23 23:25:46 +0000683 Value.get());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000684
685 if (isInvalid) {
686 if (EnumConst)
687 EnumConst->setInvalidDecl();
688 Enum->setInvalidDecl();
689 }
690
691 if (EnumConst) {
David Blaikie581deb32012-06-06 20:45:41 +0000692 SemaRef.InstantiateAttrs(TemplateArgs, *EC, EnumConst);
John McCall5b629aa2010-10-22 23:36:17 +0000693
John McCall3b85ecf2010-01-23 22:37:59 +0000694 EnumConst->setAccess(Enum->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000695 Enum->addDecl(EnumConst);
John McCalld226f652010-08-21 09:40:31 +0000696 Enumerators.push_back(EnumConst);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000697 LastEnumConst = EnumConst;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000698
Richard Smithf1c66b42012-03-14 23:13:10 +0000699 if (Pattern->getDeclContext()->isFunctionOrMethod() &&
700 !Enum->isScoped()) {
Douglas Gregor96084f12010-03-01 19:00:07 +0000701 // If the enumeration is within a function or method, record the enum
702 // constant as a local.
David Blaikie581deb32012-06-06 20:45:41 +0000703 SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
Douglas Gregor96084f12010-03-01 19:00:07 +0000704 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000705 }
706 }
Mike Stump1eb44332009-09-09 15:08:12 +0000707
Richard Smithf1c66b42012-03-14 23:13:10 +0000708 // FIXME: Fixup LBraceLoc
709 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(),
710 Enum->getRBraceLoc(), Enum,
Eli Friedmande7a0fc2010-08-15 02:27:09 +0000711 Enumerators.data(), Enumerators.size(),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000712 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000713}
714
Douglas Gregor6477b692009-03-25 15:04:13 +0000715Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000716 llvm_unreachable("EnumConstantDecls can only occur within EnumDecls.");
Douglas Gregor6477b692009-03-25 15:04:13 +0000717}
718
John McCalle29ba202009-08-20 01:44:21 +0000719Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
John McCall93ba8572010-03-25 06:39:04 +0000720 bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
721
Douglas Gregor550d9b22009-10-31 17:21:17 +0000722 // Create a local instantiation scope for this class template, which
723 // will contain the instantiations of the template parameters.
John McCall2a7fb272010-08-25 05:32:35 +0000724 LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000725 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000726 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000727 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000728 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000729
730 CXXRecordDecl *Pattern = D->getTemplatedDecl();
John McCall93ba8572010-03-25 06:39:04 +0000731
732 // Instantiate the qualifier. We have to do this first in case
733 // we're a friend declaration, because if we are then we need to put
734 // the new declaration in the appropriate context.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000735 NestedNameSpecifierLoc QualifierLoc = Pattern->getQualifierLoc();
736 if (QualifierLoc) {
737 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
738 TemplateArgs);
739 if (!QualifierLoc)
740 return 0;
John McCall93ba8572010-03-25 06:39:04 +0000741 }
742
743 CXXRecordDecl *PrevDecl = 0;
744 ClassTemplateDecl *PrevClassTemplate = 0;
745
Douglas Gregoref96ee02012-01-14 16:38:05 +0000746 if (!isFriend && Pattern->getPreviousDecl()) {
Nick Lewycky37574f52010-11-08 23:29:42 +0000747 DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
David Blaikie3bc93e32012-12-19 00:45:41 +0000748 if (!Found.empty()) {
749 PrevClassTemplate = dyn_cast<ClassTemplateDecl>(Found.front());
Nick Lewycky37574f52010-11-08 23:29:42 +0000750 if (PrevClassTemplate)
751 PrevDecl = PrevClassTemplate->getTemplatedDecl();
752 }
753 }
754
John McCall93ba8572010-03-25 06:39:04 +0000755 // If this isn't a friend, then it's a member template, in which
756 // case we just want to build the instantiation in the
757 // specialization. If it is a friend, we want to build it in
758 // the appropriate context.
759 DeclContext *DC = Owner;
760 if (isFriend) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000761 if (QualifierLoc) {
John McCall93ba8572010-03-25 06:39:04 +0000762 CXXScopeSpec SS;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000763 SS.Adopt(QualifierLoc);
John McCall93ba8572010-03-25 06:39:04 +0000764 DC = SemaRef.computeDeclContext(SS);
765 if (!DC) return 0;
766 } else {
767 DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
768 Pattern->getDeclContext(),
769 TemplateArgs);
770 }
771
772 // Look for a previous declaration of the template in the owning
773 // context.
774 LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
775 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
776 SemaRef.LookupQualifiedName(R, DC);
777
778 if (R.isSingleResult()) {
779 PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
780 if (PrevClassTemplate)
781 PrevDecl = PrevClassTemplate->getTemplatedDecl();
782 }
783
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000784 if (!PrevClassTemplate && QualifierLoc) {
John McCall93ba8572010-03-25 06:39:04 +0000785 SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
Douglas Gregor1eabb7d2010-03-31 23:17:41 +0000786 << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000787 << QualifierLoc.getSourceRange();
John McCall93ba8572010-03-25 06:39:04 +0000788 return 0;
789 }
790
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000791 bool AdoptedPreviousTemplateParams = false;
John McCall93ba8572010-03-25 06:39:04 +0000792 if (PrevClassTemplate) {
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000793 bool Complain = true;
794
795 // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
796 // template for struct std::tr1::__detail::_Map_base, where the
797 // template parameters of the friend declaration don't match the
798 // template parameters of the original declaration. In this one
799 // case, we don't complain about the ill-formed friend
800 // declaration.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000801 if (isFriend && Pattern->getIdentifier() &&
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000802 Pattern->getIdentifier()->isStr("_Map_base") &&
803 DC->isNamespace() &&
804 cast<NamespaceDecl>(DC)->getIdentifier() &&
805 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
806 DeclContext *DCParent = DC->getParent();
807 if (DCParent->isNamespace() &&
808 cast<NamespaceDecl>(DCParent)->getIdentifier() &&
809 cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
810 DeclContext *DCParent2 = DCParent->getParent();
811 if (DCParent2->isNamespace() &&
812 cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
813 cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
814 DCParent2->getParent()->isTranslationUnit())
815 Complain = false;
816 }
817 }
818
John McCall93ba8572010-03-25 06:39:04 +0000819 TemplateParameterList *PrevParams
820 = PrevClassTemplate->getTemplateParameters();
821
822 // Make sure the parameter lists match.
823 if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000824 Complain,
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000825 Sema::TPL_TemplateMatch)) {
826 if (Complain)
827 return 0;
828
829 AdoptedPreviousTemplateParams = true;
830 InstParams = PrevParams;
831 }
John McCall93ba8572010-03-25 06:39:04 +0000832
833 // Do some additional validation, then merge default arguments
834 // from the existing declarations.
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000835 if (!AdoptedPreviousTemplateParams &&
836 SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
John McCall93ba8572010-03-25 06:39:04 +0000837 Sema::TPC_ClassTemplate))
838 return 0;
839 }
840 }
841
John McCalle29ba202009-08-20 01:44:21 +0000842 CXXRecordDecl *RecordInst
John McCall93ba8572010-03-25 06:39:04 +0000843 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000844 Pattern->getLocStart(), Pattern->getLocation(),
845 Pattern->getIdentifier(), PrevDecl,
Douglas Gregorf0510d42009-10-12 23:11:44 +0000846 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000847
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000848 if (QualifierLoc)
849 RecordInst->setQualifierInfo(QualifierLoc);
John McCallb6217662010-03-15 10:12:16 +0000850
John McCalle29ba202009-08-20 01:44:21 +0000851 ClassTemplateDecl *Inst
John McCall93ba8572010-03-25 06:39:04 +0000852 = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
853 D->getIdentifier(), InstParams, RecordInst,
854 PrevClassTemplate);
John McCalle29ba202009-08-20 01:44:21 +0000855 RecordInst->setDescribedClassTemplate(Inst);
John McCallea7390c2010-04-08 20:25:50 +0000856
John McCall93ba8572010-03-25 06:39:04 +0000857 if (isFriend) {
John McCallea7390c2010-04-08 20:25:50 +0000858 if (PrevClassTemplate)
859 Inst->setAccess(PrevClassTemplate->getAccess());
860 else
861 Inst->setAccess(D->getAccess());
862
John McCall93ba8572010-03-25 06:39:04 +0000863 Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
864 // TODO: do we want to track the instantiation progeny of this
865 // friend target decl?
866 } else {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000867 Inst->setAccess(D->getAccess());
Nick Lewycky37574f52010-11-08 23:29:42 +0000868 if (!PrevClassTemplate)
869 Inst->setInstantiatedFromMemberTemplate(D);
John McCall93ba8572010-03-25 06:39:04 +0000870 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000871
Douglas Gregorf0510d42009-10-12 23:11:44 +0000872 // Trigger creation of the type for the instantiation.
John McCall3cb0ebd2010-03-10 03:28:59 +0000873 SemaRef.Context.getInjectedClassNameType(RecordInst,
Douglas Gregor24bae922010-07-08 18:37:38 +0000874 Inst->getInjectedClassNameSpecialization());
John McCallea7390c2010-04-08 20:25:50 +0000875
Douglas Gregor259571e2009-10-30 22:42:42 +0000876 // Finish handling of friends.
John McCall93ba8572010-03-25 06:39:04 +0000877 if (isFriend) {
Richard Smith1b7f9cb2012-03-13 03:12:56 +0000878 DC->makeDeclVisibleInContext(Inst);
Abramo Bagnara4c515482011-11-26 13:33:46 +0000879 Inst->setLexicalDeclContext(Owner);
880 RecordInst->setLexicalDeclContext(Owner);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000881 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000882 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000883
Abramo Bagnara4c515482011-11-26 13:33:46 +0000884 if (D->isOutOfLine()) {
885 Inst->setLexicalDeclContext(D->getLexicalDeclContext());
886 RecordInst->setLexicalDeclContext(D->getLexicalDeclContext());
887 }
888
John McCalle29ba202009-08-20 01:44:21 +0000889 Owner->addDecl(Inst);
Douglas Gregord65587f2010-11-10 19:44:59 +0000890
891 if (!PrevClassTemplate) {
892 // Queue up any out-of-line partial specializations of this member
893 // class template; the client will force their instantiation once
894 // the enclosing class has been instantiated.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000895 SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
Douglas Gregord65587f2010-11-10 19:44:59 +0000896 D->getPartialSpecializations(PartialSpecs);
897 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
898 if (PartialSpecs[I]->isOutOfLine())
899 OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I]));
900 }
901
John McCalle29ba202009-08-20 01:44:21 +0000902 return Inst;
903}
904
Douglas Gregord60e1052009-08-27 16:57:43 +0000905Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000906TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
907 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000908 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000909
Douglas Gregored9c0f92009-10-29 00:04:11 +0000910 // Lookup the already-instantiated declaration in the instantiation
911 // of the class template and return that.
912 DeclContext::lookup_result Found
913 = Owner->lookup(ClassTemplate->getDeclName());
David Blaikie3bc93e32012-12-19 00:45:41 +0000914 if (Found.empty())
Douglas Gregored9c0f92009-10-29 00:04:11 +0000915 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000916
Douglas Gregored9c0f92009-10-29 00:04:11 +0000917 ClassTemplateDecl *InstClassTemplate
David Blaikie3bc93e32012-12-19 00:45:41 +0000918 = dyn_cast<ClassTemplateDecl>(Found.front());
Douglas Gregored9c0f92009-10-29 00:04:11 +0000919 if (!InstClassTemplate)
920 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000921
Douglas Gregord65587f2010-11-10 19:44:59 +0000922 if (ClassTemplatePartialSpecializationDecl *Result
923 = InstClassTemplate->findPartialSpecInstantiatedFromMember(D))
924 return Result;
925
926 return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D);
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000927}
928
929Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000930TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000931 // Create a local instantiation scope for this function template, which
932 // will contain the instantiations of the template parameters and then get
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000933 // merged with the local instantiation scope for the function template
Douglas Gregor550d9b22009-10-31 17:21:17 +0000934 // itself.
John McCall2a7fb272010-08-25 05:32:35 +0000935 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor895162d2010-04-30 18:55:50 +0000936
Douglas Gregord60e1052009-08-27 16:57:43 +0000937 TemplateParameterList *TempParams = D->getTemplateParameters();
938 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000939 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000940 return NULL;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000941
Douglas Gregora735b202009-10-13 14:39:41 +0000942 FunctionDecl *Instantiated = 0;
943 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000944 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
Douglas Gregora735b202009-10-13 14:39:41 +0000945 InstParams));
946 else
947 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000948 D->getTemplatedDecl(),
Douglas Gregora735b202009-10-13 14:39:41 +0000949 InstParams));
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000950
Douglas Gregora735b202009-10-13 14:39:41 +0000951 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000952 return 0;
953
Mike Stump1eb44332009-09-09 15:08:12 +0000954 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000955 // template from which it was instantiated.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000956 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000957 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000958 InstTemplate->setAccess(D->getAccess());
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000959 assert(InstTemplate &&
Douglas Gregora735b202009-10-13 14:39:41 +0000960 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
John McCalle976ffe2009-12-14 23:19:40 +0000961
John McCallb1a56e72010-03-26 23:10:15 +0000962 bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
963
John McCalle976ffe2009-12-14 23:19:40 +0000964 // Link the instantiation back to the pattern *unless* this is a
965 // non-definition friend declaration.
966 if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
John McCallb1a56e72010-03-26 23:10:15 +0000967 !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
Douglas Gregora735b202009-10-13 14:39:41 +0000968 InstTemplate->setInstantiatedFromMemberTemplate(D);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000969
John McCallb1a56e72010-03-26 23:10:15 +0000970 // Make declarations visible in the appropriate context.
John McCall1f2e1a92012-08-10 03:15:35 +0000971 if (!isFriend) {
Douglas Gregora735b202009-10-13 14:39:41 +0000972 Owner->addDecl(InstTemplate);
John McCall1f2e1a92012-08-10 03:15:35 +0000973 } else if (InstTemplate->getDeclContext()->isRecord() &&
974 !D->getPreviousDecl()) {
975 SemaRef.CheckFriendAccess(InstTemplate);
976 }
John McCallb1a56e72010-03-26 23:10:15 +0000977
Douglas Gregord60e1052009-08-27 16:57:43 +0000978 return InstTemplate;
979}
980
Douglas Gregord475b8d2009-03-25 21:17:03 +0000981Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
982 CXXRecordDecl *PrevDecl = 0;
983 if (D->isInjectedClassName())
984 PrevDecl = cast<CXXRecordDecl>(Owner);
Douglas Gregoref96ee02012-01-14 16:38:05 +0000985 else if (D->getPreviousDecl()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000986 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
Douglas Gregoref96ee02012-01-14 16:38:05 +0000987 D->getPreviousDecl(),
John McCall6c1c1b82009-12-15 22:29:06 +0000988 TemplateArgs);
989 if (!Prev) return 0;
990 PrevDecl = cast<CXXRecordDecl>(Prev);
991 }
Douglas Gregord475b8d2009-03-25 21:17:03 +0000992
993 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000994 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000995 D->getLocStart(), D->getLocation(),
996 D->getIdentifier(), PrevDecl);
John McCallb6217662010-03-15 10:12:16 +0000997
998 // Substitute the nested name specifier, if any.
999 if (SubstQualifier(D, Record))
1000 return 0;
1001
Douglas Gregord475b8d2009-03-25 21:17:03 +00001002 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +00001003 // FIXME: Check against AS_none is an ugly hack to work around the issue that
1004 // the tag decls introduced by friend class declarations don't have an access
1005 // specifier. Remove once this area of the code gets sorted out.
1006 if (D->getAccess() != AS_none)
1007 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +00001008 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001009 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001010
John McCall02cace72009-08-28 07:59:38 +00001011 // If the original function was part of a friend declaration,
1012 // inherit its namespace state.
1013 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
1014 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
1015
Douglas Gregor9901c572010-05-21 00:31:19 +00001016 // Make sure that anonymous structs and unions are recorded.
1017 if (D->isAnonymousStructOrUnion()) {
1018 Record->setAnonymousStructOrUnion(true);
Sebastian Redl7a126a42010-08-31 00:36:30 +00001019 if (Record->getDeclContext()->getRedeclContext()->isFunctionOrMethod())
Douglas Gregor9901c572010-05-21 00:31:19 +00001020 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
1021 }
Anders Carlssond8b285f2009-09-01 04:26:58 +00001022
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001023 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001024 return Record;
1025}
1026
Douglas Gregor71074fd2012-09-13 21:56:43 +00001027/// \brief Adjust the given function type for an instantiation of the
1028/// given declaration, to cope with modifications to the function's type that
1029/// aren't reflected in the type-source information.
1030///
1031/// \param D The declaration we're instantiating.
1032/// \param TInfo The already-instantiated type.
1033static QualType adjustFunctionTypeForInstantiation(ASTContext &Context,
1034 FunctionDecl *D,
1035 TypeSourceInfo *TInfo) {
Douglas Gregorbed51fe2012-09-13 22:01:49 +00001036 const FunctionProtoType *OrigFunc
1037 = D->getType()->castAs<FunctionProtoType>();
1038 const FunctionProtoType *NewFunc
1039 = TInfo->getType()->castAs<FunctionProtoType>();
1040 if (OrigFunc->getExtInfo() == NewFunc->getExtInfo())
1041 return TInfo->getType();
1042
1043 FunctionProtoType::ExtProtoInfo NewEPI = NewFunc->getExtProtoInfo();
1044 NewEPI.ExtInfo = OrigFunc->getExtInfo();
1045 return Context.getFunctionType(NewFunc->getResultType(),
1046 NewFunc->arg_type_begin(),
1047 NewFunc->getNumArgs(),
1048 NewEPI);
Douglas Gregor71074fd2012-09-13 21:56:43 +00001049}
1050
John McCall02cace72009-08-28 07:59:38 +00001051/// Normal class members are of more specific types and therefore
1052/// don't make it here. This function serves two purposes:
1053/// 1) instantiating function templates
1054/// 2) substituting friend declarations
1055/// FIXME: preserve function definitions in case #2
Douglas Gregor7557a132009-12-24 20:56:24 +00001056Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
Douglas Gregora735b202009-10-13 14:39:41 +00001057 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +00001058 // Check whether there is already a function template specialization for
1059 // this declaration.
1060 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
John McCallb0cb0222010-03-27 05:57:59 +00001061 if (FunctionTemplate && !TemplateParams) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001062 std::pair<const TemplateArgument *, unsigned> Innermost
Douglas Gregor24bae922010-07-08 18:37:38 +00001063 = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +00001064
Douglas Gregor1e1e9722012-03-28 14:34:23 +00001065 void *InsertPos = 0;
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001066 FunctionDecl *SpecFunc
1067 = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
1068 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001069
Douglas Gregor127102b2009-06-29 20:59:39 +00001070 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001071 if (SpecFunc)
1072 return SpecFunc;
Douglas Gregor127102b2009-06-29 20:59:39 +00001073 }
Mike Stump1eb44332009-09-09 15:08:12 +00001074
John McCallb0cb0222010-03-27 05:57:59 +00001075 bool isFriend;
1076 if (FunctionTemplate)
1077 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1078 else
1079 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1080
Douglas Gregor79c22782010-01-16 20:21:20 +00001081 bool MergeWithParentScope = (TemplateParams != 0) ||
Douglas Gregorb212d9a2010-05-21 21:25:08 +00001082 Owner->isFunctionOrMethod() ||
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001083 !(isa<Decl>(Owner) &&
Douglas Gregor79c22782010-01-16 20:21:20 +00001084 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
John McCall2a7fb272010-08-25 05:32:35 +00001085 LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001086
Chris Lattner5f9e2722011-07-23 10:55:15 +00001087 SmallVector<ParmVarDecl *, 4> Params;
David Blaikie64b4b432011-11-10 05:42:04 +00001088 TypeSourceInfo *TInfo = SubstFunctionType(D, Params);
John McCall21ef0fa2010-03-11 09:03:00 +00001089 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001090 return 0;
Douglas Gregor71074fd2012-09-13 21:56:43 +00001091 QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo);
John McCallfd810b12009-08-14 02:03:10 +00001092
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001093 NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
1094 if (QualifierLoc) {
1095 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
1096 TemplateArgs);
1097 if (!QualifierLoc)
1098 return 0;
John McCalld325daa2010-03-26 04:53:08 +00001099 }
1100
John McCall68b6b872010-02-06 01:50:47 +00001101 // If we're instantiating a local function declaration, put the result
1102 // in the owner; otherwise we need to find the instantiated context.
1103 DeclContext *DC;
1104 if (D->getDeclContext()->isFunctionOrMethod())
1105 DC = Owner;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001106 else if (isFriend && QualifierLoc) {
John McCalld325daa2010-03-26 04:53:08 +00001107 CXXScopeSpec SS;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001108 SS.Adopt(QualifierLoc);
John McCalld325daa2010-03-26 04:53:08 +00001109 DC = SemaRef.computeDeclContext(SS);
1110 if (!DC) return 0;
1111 } else {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001112 DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001113 TemplateArgs);
John McCalld325daa2010-03-26 04:53:08 +00001114 }
John McCall68b6b872010-02-06 01:50:47 +00001115
John McCall02cace72009-08-28 07:59:38 +00001116 FunctionDecl *Function =
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001117 FunctionDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(),
Abramo Bagnara635311f2012-10-04 21:40:42 +00001118 D->getNameInfo(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001119 D->getStorageClass(), D->getStorageClassAsWritten(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001120 D->isInlineSpecified(), D->hasWrittenPrototype(),
Richard Smith86c3ae42012-02-13 03:54:03 +00001121 D->isConstexpr());
John McCallb6217662010-03-15 10:12:16 +00001122
Richard Smithd4497dd2013-01-25 00:08:28 +00001123 if (D->isInlined())
1124 Function->setImplicitlyInline();
1125
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001126 if (QualifierLoc)
1127 Function->setQualifierInfo(QualifierLoc);
John McCallb6217662010-03-15 10:12:16 +00001128
John McCallb1a56e72010-03-26 23:10:15 +00001129 DeclContext *LexicalDC = Owner;
1130 if (!isFriend && D->isOutOfLine()) {
1131 assert(D->getDeclContext()->isFileContext());
1132 LexicalDC = D->getDeclContext();
1133 }
1134
1135 Function->setLexicalDeclContext(LexicalDC);
Mike Stump1eb44332009-09-09 15:08:12 +00001136
Douglas Gregore53060f2009-06-25 22:08:12 +00001137 // Attach the parameters
Douglas Gregor5cbe1012011-07-05 18:30:26 +00001138 if (isa<FunctionProtoType>(Function->getType().IgnoreParens())) {
Douglas Gregor1d441ee2011-06-22 18:16:25 +00001139 // Adopt the already-instantiated parameters into our own context.
1140 for (unsigned P = 0; P < Params.size(); ++P)
1141 if (Params[P])
1142 Params[P]->setOwningFunction(Function);
1143 } else {
1144 // Since we were instantiated via a typedef of a function type, create
1145 // new parameters.
1146 const FunctionProtoType *Proto
1147 = Function->getType()->getAs<FunctionProtoType>();
1148 assert(Proto && "No function prototype in template instantiation?");
1149 for (FunctionProtoType::arg_type_iterator AI = Proto->arg_type_begin(),
1150 AE = Proto->arg_type_end(); AI != AE; ++AI) {
1151 ParmVarDecl *Param
1152 = SemaRef.BuildParmVarDeclForTypedef(Function, Function->getLocation(),
1153 *AI);
1154 Param->setScopeInfo(0, Params.size());
1155 Params.push_back(Param);
1156 }
1157 }
David Blaikie4278c652011-09-21 18:16:56 +00001158 Function->setParams(Params);
John McCall02cace72009-08-28 07:59:38 +00001159
Douglas Gregorac7c2c82010-05-17 16:38:00 +00001160 SourceLocation InstantiateAtPOI;
Douglas Gregora735b202009-10-13 14:39:41 +00001161 if (TemplateParams) {
1162 // Our resulting instantiation is actually a function template, since we
1163 // are substituting only the outer template parameters. For example, given
1164 //
1165 // template<typename T>
1166 // struct X {
1167 // template<typename U> friend void f(T, U);
1168 // };
1169 //
1170 // X<int> x;
1171 //
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001172 // We are instantiating the friend function template "f" within X<int>,
Douglas Gregora735b202009-10-13 14:39:41 +00001173 // which means substituting int for T, but leaving "f" as a friend function
1174 // template.
1175 // Build the function template itself.
John McCalld325daa2010-03-26 04:53:08 +00001176 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
Douglas Gregora735b202009-10-13 14:39:41 +00001177 Function->getLocation(),
1178 Function->getDeclName(),
1179 TemplateParams, Function);
1180 Function->setDescribedFunctionTemplate(FunctionTemplate);
John McCallb1a56e72010-03-26 23:10:15 +00001181
1182 FunctionTemplate->setLexicalDeclContext(LexicalDC);
John McCalld325daa2010-03-26 04:53:08 +00001183
1184 if (isFriend && D->isThisDeclarationADefinition()) {
1185 // TODO: should we remember this connection regardless of whether
1186 // the friend declaration provided a body?
1187 FunctionTemplate->setInstantiatedFromMemberTemplate(
1188 D->getDescribedFunctionTemplate());
1189 }
Douglas Gregor66724ea2009-11-14 01:20:54 +00001190 } else if (FunctionTemplate) {
1191 // Record this function template specialization.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001192 std::pair<const TemplateArgument *, unsigned> Innermost
Douglas Gregor24bae922010-07-08 18:37:38 +00001193 = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001194 Function->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001195 TemplateArgumentList::CreateCopy(SemaRef.Context,
Douglas Gregor24bae922010-07-08 18:37:38 +00001196 Innermost.first,
1197 Innermost.second),
Douglas Gregor1e1e9722012-03-28 14:34:23 +00001198 /*InsertPos=*/0);
Chandler Carruth80f5b162011-08-18 09:09:59 +00001199 } else if (isFriend) {
1200 // Note, we need this connection even if the friend doesn't have a body.
1201 // Its body may exist but not have been attached yet due to deferred
1202 // parsing.
1203 // FIXME: It might be cleaner to set this when attaching the body to the
1204 // friend function declaration, however that would require finding all the
1205 // instantiations and modifying them.
John McCalld325daa2010-03-26 04:53:08 +00001206 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
John McCall02cace72009-08-28 07:59:38 +00001207 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001208
Douglas Gregore53060f2009-06-25 22:08:12 +00001209 if (InitFunctionInstantiation(Function, D))
1210 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001211
John McCallaf2094e2010-04-08 09:05:18 +00001212 bool isExplicitSpecialization = false;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001213
John McCall68263142009-11-18 22:49:29 +00001214 LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
1215 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
1216
John McCallaf2094e2010-04-08 09:05:18 +00001217 if (DependentFunctionTemplateSpecializationInfo *Info
1218 = D->getDependentSpecializationInfo()) {
1219 assert(isFriend && "non-friend has dependent specialization info?");
1220
1221 // This needs to be set now for future sanity.
1222 Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1223
1224 // Instantiate the explicit template arguments.
1225 TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1226 Info->getRAngleLoc());
Douglas Gregore02e2622010-12-22 21:19:48 +00001227 if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(),
1228 ExplicitArgs, TemplateArgs))
1229 return 0;
John McCallaf2094e2010-04-08 09:05:18 +00001230
1231 // Map the candidate templates to their instantiations.
1232 for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1233 Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1234 Info->getTemplate(I),
1235 TemplateArgs);
1236 if (!Temp) return 0;
1237
1238 Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1239 }
1240
1241 if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1242 &ExplicitArgs,
1243 Previous))
1244 Function->setInvalidDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001245
John McCallaf2094e2010-04-08 09:05:18 +00001246 isExplicitSpecialization = true;
1247
1248 } else if (TemplateParams || !FunctionTemplate) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001249 // Look only into the namespace where the friend would be declared to
1250 // find a previous declaration. This is the innermost enclosing namespace,
Douglas Gregora735b202009-10-13 14:39:41 +00001251 // as described in ActOnFriendFunctionDecl.
John McCall68263142009-11-18 22:49:29 +00001252 SemaRef.LookupQualifiedName(Previous, DC);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001253
Douglas Gregora735b202009-10-13 14:39:41 +00001254 // In C++, the previous declaration we find might be a tag type
1255 // (class or enum). In this case, the new declaration will hide the
1256 // tag type. Note that this does does not apply if we're declaring a
1257 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001258 if (Previous.isSingleTagDecl())
1259 Previous.clear();
Douglas Gregora735b202009-10-13 14:39:41 +00001260 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001261
John McCall9f54ad42009-12-10 09:41:52 +00001262 SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00001263 isExplicitSpecialization);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001264
John McCall76d32642010-04-24 01:30:58 +00001265 NamedDecl *PrincipalDecl = (TemplateParams
1266 ? cast<NamedDecl>(FunctionTemplate)
1267 : Function);
1268
Douglas Gregora735b202009-10-13 14:39:41 +00001269 // If the original function was part of a friend declaration,
1270 // inherit its namespace state and add it to the owner.
John McCalld325daa2010-03-26 04:53:08 +00001271 if (isFriend) {
John McCall68263142009-11-18 22:49:29 +00001272 NamedDecl *PrevDecl;
John McCall76d32642010-04-24 01:30:58 +00001273 if (TemplateParams)
Douglas Gregoref96ee02012-01-14 16:38:05 +00001274 PrevDecl = FunctionTemplate->getPreviousDecl();
John McCall76d32642010-04-24 01:30:58 +00001275 else
Douglas Gregoref96ee02012-01-14 16:38:05 +00001276 PrevDecl = Function->getPreviousDecl();
John McCall76d32642010-04-24 01:30:58 +00001277
1278 PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001279 DC->makeDeclVisibleInContext(PrincipalDecl);
Gabor Greifab297ac2010-08-30 21:10:05 +00001280
Gabor Greif77535df2010-08-30 22:25:56 +00001281 bool queuedInstantiation = false;
Gabor Greifab297ac2010-08-30 21:10:05 +00001282
Richard Smith53e53512011-10-19 00:54:10 +00001283 // C++98 [temp.friend]p5: When a function is defined in a friend function
1284 // declaration in a class template, the function is defined at each
1285 // instantiation of the class template. The function is defined even if it
1286 // is never used.
1287 // C++11 [temp.friend]p4: When a function is defined in a friend function
1288 // declaration in a class template, the function is instantiated when the
1289 // function is odr-used.
1290 //
1291 // If -Wc++98-compat is enabled, we go through the motions of checking for a
1292 // redefinition, but don't instantiate the function.
Richard Smith80ad52f2013-01-02 11:42:31 +00001293 if ((!SemaRef.getLangOpts().CPlusPlus11 ||
Richard Smith53e53512011-10-19 00:54:10 +00001294 SemaRef.Diags.getDiagnosticLevel(
1295 diag::warn_cxx98_compat_friend_redefinition,
1296 Function->getLocation())
1297 != DiagnosticsEngine::Ignored) &&
Douglas Gregor238058c2010-05-18 05:45:02 +00001298 D->isThisDeclarationADefinition()) {
1299 // Check for a function body.
1300 const FunctionDecl *Definition = 0;
Sean Hunt10620eb2011-05-06 20:44:56 +00001301 if (Function->isDefined(Definition) &&
Douglas Gregor238058c2010-05-18 05:45:02 +00001302 Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
Richard Smith53e53512011-10-19 00:54:10 +00001303 SemaRef.Diag(Function->getLocation(),
Richard Smith80ad52f2013-01-02 11:42:31 +00001304 SemaRef.getLangOpts().CPlusPlus11 ?
Richard Smith53e53512011-10-19 00:54:10 +00001305 diag::warn_cxx98_compat_friend_redefinition :
1306 diag::err_redefinition) << Function->getDeclName();
Douglas Gregor238058c2010-05-18 05:45:02 +00001307 SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
Richard Smith80ad52f2013-01-02 11:42:31 +00001308 if (!SemaRef.getLangOpts().CPlusPlus11)
Richard Smith53e53512011-10-19 00:54:10 +00001309 Function->setInvalidDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001310 }
Douglas Gregor238058c2010-05-18 05:45:02 +00001311 // Check for redefinitions due to other instantiations of this or
1312 // a similar friend function.
1313 else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1314 REnd = Function->redecls_end();
1315 R != REnd; ++R) {
Gabor Greif13a8aff2010-08-28 15:42:30 +00001316 if (*R == Function)
1317 continue;
Gabor Greifab297ac2010-08-30 21:10:05 +00001318 switch (R->getFriendObjectKind()) {
1319 case Decl::FOK_None:
Richard Smith80ad52f2013-01-02 11:42:31 +00001320 if (!SemaRef.getLangOpts().CPlusPlus11 &&
Richard Smith53e53512011-10-19 00:54:10 +00001321 !queuedInstantiation && R->isUsed(false)) {
Gabor Greifab297ac2010-08-30 21:10:05 +00001322 if (MemberSpecializationInfo *MSInfo
1323 = Function->getMemberSpecializationInfo()) {
1324 if (MSInfo->getPointOfInstantiation().isInvalid()) {
1325 SourceLocation Loc = R->getLocation(); // FIXME
1326 MSInfo->setPointOfInstantiation(Loc);
1327 SemaRef.PendingLocalImplicitInstantiations.push_back(
1328 std::make_pair(Function, Loc));
1329 queuedInstantiation = true;
1330 }
1331 }
1332 }
1333 break;
1334 default:
Douglas Gregor238058c2010-05-18 05:45:02 +00001335 if (const FunctionDecl *RPattern
Gabor Greif6a557d82010-08-28 15:46:56 +00001336 = R->getTemplateInstantiationPattern())
Sean Hunt10620eb2011-05-06 20:44:56 +00001337 if (RPattern->isDefined(RPattern)) {
Richard Smith53e53512011-10-19 00:54:10 +00001338 SemaRef.Diag(Function->getLocation(),
Richard Smith80ad52f2013-01-02 11:42:31 +00001339 SemaRef.getLangOpts().CPlusPlus11 ?
Richard Smith53e53512011-10-19 00:54:10 +00001340 diag::warn_cxx98_compat_friend_redefinition :
1341 diag::err_redefinition)
Douglas Gregor238058c2010-05-18 05:45:02 +00001342 << Function->getDeclName();
Gabor Greif6a557d82010-08-28 15:46:56 +00001343 SemaRef.Diag(R->getLocation(), diag::note_previous_definition);
Richard Smith80ad52f2013-01-02 11:42:31 +00001344 if (!SemaRef.getLangOpts().CPlusPlus11)
Richard Smith53e53512011-10-19 00:54:10 +00001345 Function->setInvalidDecl();
Douglas Gregor238058c2010-05-18 05:45:02 +00001346 break;
1347 }
1348 }
1349 }
1350 }
Douglas Gregora735b202009-10-13 14:39:41 +00001351 }
1352
John McCall76d32642010-04-24 01:30:58 +00001353 if (Function->isOverloadedOperator() && !DC->isRecord() &&
1354 PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
1355 PrincipalDecl->setNonMemberOperator();
1356
Sean Hunteb88ae52011-05-23 21:07:59 +00001357 assert(!D->isDefaulted() && "only methods should be defaulted");
Douglas Gregore53060f2009-06-25 22:08:12 +00001358 return Function;
1359}
1360
Douglas Gregord60e1052009-08-27 16:57:43 +00001361Decl *
1362TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
Francois Pichetaf0f4d02011-08-14 03:52:19 +00001363 TemplateParameterList *TemplateParams,
1364 bool IsClassScopeSpecialization) {
Douglas Gregor6b906862009-08-21 00:16:32 +00001365 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
Douglas Gregord60e1052009-08-27 16:57:43 +00001366 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +00001367 // We are creating a function template specialization from a function
1368 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +00001369 // specialization for this particular set of template arguments.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001370 std::pair<const TemplateArgument *, unsigned> Innermost
Douglas Gregor24bae922010-07-08 18:37:38 +00001371 = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +00001372
Douglas Gregor1e1e9722012-03-28 14:34:23 +00001373 void *InsertPos = 0;
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001374 FunctionDecl *SpecFunc
1375 = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
1376 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001377
Douglas Gregor6b906862009-08-21 00:16:32 +00001378 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001379 if (SpecFunc)
1380 return SpecFunc;
Douglas Gregor6b906862009-08-21 00:16:32 +00001381 }
1382
John McCallb0cb0222010-03-27 05:57:59 +00001383 bool isFriend;
1384 if (FunctionTemplate)
1385 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1386 else
1387 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1388
Douglas Gregor79c22782010-01-16 20:21:20 +00001389 bool MergeWithParentScope = (TemplateParams != 0) ||
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001390 !(isa<Decl>(Owner) &&
Douglas Gregor79c22782010-01-16 20:21:20 +00001391 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
John McCall2a7fb272010-08-25 05:32:35 +00001392 LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Douglas Gregor48dd19b2009-05-14 21:44:34 +00001393
John McCall4eab39f2010-10-19 02:26:41 +00001394 // Instantiate enclosing template arguments for friends.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001395 SmallVector<TemplateParameterList *, 4> TempParamLists;
John McCall4eab39f2010-10-19 02:26:41 +00001396 unsigned NumTempParamLists = 0;
1397 if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) {
1398 TempParamLists.set_size(NumTempParamLists);
1399 for (unsigned I = 0; I != NumTempParamLists; ++I) {
1400 TemplateParameterList *TempParams = D->getTemplateParameterList(I);
1401 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1402 if (!InstParams)
1403 return NULL;
1404 TempParamLists[I] = InstParams;
1405 }
1406 }
1407
Chris Lattner5f9e2722011-07-23 10:55:15 +00001408 SmallVector<ParmVarDecl *, 4> Params;
Benjamin Kramerdc370c12012-01-20 14:42:32 +00001409 TypeSourceInfo *TInfo = SubstFunctionType(D, Params);
John McCall21ef0fa2010-03-11 09:03:00 +00001410 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001411 return 0;
Douglas Gregor71074fd2012-09-13 21:56:43 +00001412 QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001413
Abramo Bagnara723df242010-12-14 22:11:44 +00001414 // \brief If the type of this function, after ignoring parentheses,
1415 // is not *directly* a function type, then we're instantiating a function
1416 // that was declared via a typedef, e.g.,
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001417 //
1418 // typedef int functype(int, int);
1419 // functype func;
1420 //
1421 // In this case, we'll just go instantiate the ParmVarDecls that we
1422 // synthesized in the method declaration.
Abramo Bagnara723df242010-12-14 22:11:44 +00001423 if (!isa<FunctionProtoType>(T.IgnoreParens())) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001424 assert(!Params.size() && "Instantiating type could not yield parameters");
Chris Lattner5f9e2722011-07-23 10:55:15 +00001425 SmallVector<QualType, 4> ParamTypes;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001426 if (SemaRef.SubstParmTypes(D->getLocation(), D->param_begin(),
1427 D->getNumParams(), TemplateArgs, ParamTypes,
Douglas Gregor12c9c002011-01-07 16:43:16 +00001428 &Params))
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001429 return 0;
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001430 }
1431
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001432 NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
1433 if (QualifierLoc) {
1434 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
John McCallb0cb0222010-03-27 05:57:59 +00001435 TemplateArgs);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001436 if (!QualifierLoc)
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001437 return 0;
John McCallb0cb0222010-03-27 05:57:59 +00001438 }
1439
1440 DeclContext *DC = Owner;
1441 if (isFriend) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001442 if (QualifierLoc) {
John McCallb0cb0222010-03-27 05:57:59 +00001443 CXXScopeSpec SS;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001444 SS.Adopt(QualifierLoc);
John McCallb0cb0222010-03-27 05:57:59 +00001445 DC = SemaRef.computeDeclContext(SS);
John McCallc54d6882010-10-19 05:01:53 +00001446
1447 if (DC && SemaRef.RequireCompleteDeclContext(SS, DC))
1448 return 0;
John McCallb0cb0222010-03-27 05:57:59 +00001449 } else {
1450 DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1451 D->getDeclContext(),
1452 TemplateArgs);
1453 }
1454 if (!DC) return 0;
1455 }
1456
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001457 // Build the instantiated method declaration.
John McCallb0cb0222010-03-27 05:57:59 +00001458 CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
Douglas Gregordec06662009-08-21 18:42:58 +00001459 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001460
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001461 SourceLocation StartLoc = D->getInnerLocStart();
Abramo Bagnara25777432010-08-11 22:01:17 +00001462 DeclarationNameInfo NameInfo
1463 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001464 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001465 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001466 StartLoc, NameInfo, T, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001467 Constructor->isExplicit(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001468 Constructor->isInlineSpecified(),
Richard Smith86c3ae42012-02-13 03:54:03 +00001469 false, Constructor->isConstexpr());
Douglas Gregor17e32f32009-08-21 22:43:28 +00001470 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001471 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001472 StartLoc, NameInfo, T, TInfo,
Abramo Bagnara25777432010-08-11 22:01:17 +00001473 Destructor->isInlineSpecified(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001474 false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001475 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001476 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001477 StartLoc, NameInfo, T, TInfo,
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001478 Conversion->isInlineSpecified(),
Douglas Gregorf5251602011-03-08 17:10:18 +00001479 Conversion->isExplicit(),
Richard Smith86c3ae42012-02-13 03:54:03 +00001480 Conversion->isConstexpr(),
Richard Smith9f569cc2011-10-01 02:31:28 +00001481 Conversion->getLocEnd());
Douglas Gregordec06662009-08-21 18:42:58 +00001482 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00001483 Method = CXXMethodDecl::Create(SemaRef.Context, Record,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001484 StartLoc, NameInfo, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001485 D->isStatic(),
1486 D->getStorageClassAsWritten(),
Douglas Gregorf5251602011-03-08 17:10:18 +00001487 D->isInlineSpecified(),
Richard Smith86c3ae42012-02-13 03:54:03 +00001488 D->isConstexpr(), D->getLocEnd());
Douglas Gregordec06662009-08-21 18:42:58 +00001489 }
Douglas Gregor6b906862009-08-21 00:16:32 +00001490
Richard Smithd4497dd2013-01-25 00:08:28 +00001491 if (D->isInlined())
1492 Method->setImplicitlyInline();
1493
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001494 if (QualifierLoc)
1495 Method->setQualifierInfo(QualifierLoc);
John McCallb6217662010-03-15 10:12:16 +00001496
Douglas Gregord60e1052009-08-27 16:57:43 +00001497 if (TemplateParams) {
1498 // Our resulting instantiation is actually a function template, since we
1499 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +00001500 //
Douglas Gregord60e1052009-08-27 16:57:43 +00001501 // template<typename T>
1502 // struct X {
1503 // template<typename U> void f(T, U);
1504 // };
1505 //
1506 // X<int> x;
1507 //
1508 // We are instantiating the member template "f" within X<int>, which means
1509 // substituting int for T, but leaving "f" as a member function template.
1510 // Build the function template itself.
1511 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1512 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001513 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +00001514 TemplateParams, Method);
John McCallb0cb0222010-03-27 05:57:59 +00001515 if (isFriend) {
1516 FunctionTemplate->setLexicalDeclContext(Owner);
1517 FunctionTemplate->setObjectOfFriendDecl(true);
1518 } else if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +00001519 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +00001520 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001521 } else if (FunctionTemplate) {
1522 // Record this function template specialization.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001523 std::pair<const TemplateArgument *, unsigned> Innermost
Douglas Gregor24bae922010-07-08 18:37:38 +00001524 = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001525 Method->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001526 TemplateArgumentList::CreateCopy(SemaRef.Context,
1527 Innermost.first,
1528 Innermost.second),
Douglas Gregor1e1e9722012-03-28 14:34:23 +00001529 /*InsertPos=*/0);
John McCallb0cb0222010-03-27 05:57:59 +00001530 } else if (!isFriend) {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001531 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +00001532 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001533 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001534
Mike Stump1eb44332009-09-09 15:08:12 +00001535 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +00001536 // out-of-line, the instantiation will have the same lexical
1537 // context (which will be a namespace scope) as the template.
John McCallb0cb0222010-03-27 05:57:59 +00001538 if (isFriend) {
John McCall4eab39f2010-10-19 02:26:41 +00001539 if (NumTempParamLists)
1540 Method->setTemplateParameterListsInfo(SemaRef.Context,
1541 NumTempParamLists,
1542 TempParamLists.data());
1543
John McCallb0cb0222010-03-27 05:57:59 +00001544 Method->setLexicalDeclContext(Owner);
1545 Method->setObjectOfFriendDecl(true);
1546 } else if (D->isOutOfLine())
Douglas Gregor7caa6822009-07-24 20:34:43 +00001547 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001548
Douglas Gregor5545e162009-03-24 00:38:23 +00001549 // Attach the parameters
1550 for (unsigned P = 0; P < Params.size(); ++P)
1551 Params[P]->setOwningFunction(Method);
David Blaikie4278c652011-09-21 18:16:56 +00001552 Method->setParams(Params);
Douglas Gregor5545e162009-03-24 00:38:23 +00001553
1554 if (InitMethodInstantiation(Method, D))
1555 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001556
Abramo Bagnara25777432010-08-11 22:01:17 +00001557 LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
1558 Sema::ForRedeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001559
John McCallb0cb0222010-03-27 05:57:59 +00001560 if (!FunctionTemplate || TemplateParams || isFriend) {
1561 SemaRef.LookupQualifiedName(Previous, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001562
Douglas Gregordec06662009-08-21 18:42:58 +00001563 // In C++, the previous declaration we find might be a tag type
1564 // (class or enum). In this case, the new declaration will hide the
1565 // tag type. Note that this does does not apply if we're declaring a
1566 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001567 if (Previous.isSingleTagDecl())
1568 Previous.clear();
Douglas Gregordec06662009-08-21 18:42:58 +00001569 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001570
Francois Pichetaf0f4d02011-08-14 03:52:19 +00001571 if (!IsClassScopeSpecialization)
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00001572 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001573
Douglas Gregor4ba31362009-12-01 17:24:26 +00001574 if (D->isPure())
1575 SemaRef.CheckPureMethod(Method, SourceRange());
1576
John McCall1f2e1a92012-08-10 03:15:35 +00001577 // Propagate access. For a non-friend declaration, the access is
1578 // whatever we're propagating from. For a friend, it should be the
1579 // previous declaration we just found.
1580 if (isFriend && Method->getPreviousDecl())
1581 Method->setAccess(Method->getPreviousDecl()->getAccess());
1582 else
1583 Method->setAccess(D->getAccess());
1584 if (FunctionTemplate)
1585 FunctionTemplate->setAccess(Method->getAccess());
John McCall46460a62010-01-20 21:53:11 +00001586
Anders Carlsson9eefa222011-01-20 06:52:44 +00001587 SemaRef.CheckOverrideControl(Method);
1588
Eli Friedman3bc45152011-11-15 22:39:08 +00001589 // If a function is defined as defaulted or deleted, mark it as such now.
Richard Smithac713512012-12-08 02:53:02 +00001590 if (D->isExplicitlyDefaulted())
1591 SemaRef.SetDeclDefaulted(Method, Method->getLocation());
Eli Friedman3bc45152011-11-15 22:39:08 +00001592 if (D->isDeletedAsWritten())
Richard Smithac713512012-12-08 02:53:02 +00001593 SemaRef.SetDeclDeleted(Method, Method->getLocation());
Eli Friedman3bc45152011-11-15 22:39:08 +00001594
John McCall1f2e1a92012-08-10 03:15:35 +00001595 // If there's a function template, let our caller handle it.
John McCallb0cb0222010-03-27 05:57:59 +00001596 if (FunctionTemplate) {
John McCall1f2e1a92012-08-10 03:15:35 +00001597 // do nothing
1598
1599 // Don't hide a (potentially) valid declaration with an invalid one.
John McCallb0cb0222010-03-27 05:57:59 +00001600 } else if (Method->isInvalidDecl() && !Previous.empty()) {
John McCall1f2e1a92012-08-10 03:15:35 +00001601 // do nothing
1602
1603 // Otherwise, check access to friends and make them visible.
1604 } else if (isFriend) {
1605 // We only need to re-check access for methods which we didn't
1606 // manage to match during parsing.
1607 if (!D->getPreviousDecl())
1608 SemaRef.CheckFriendAccess(Method);
1609
1610 Record->makeDeclVisibleInContext(Method);
1611
1612 // Otherwise, add the declaration. We don't need to do this for
1613 // class-scope specializations because we'll have matched them with
1614 // the appropriate template.
1615 } else if (!IsClassScopeSpecialization) {
1616 Owner->addDecl(Method);
John McCallb0cb0222010-03-27 05:57:59 +00001617 }
Sean Hunteb88ae52011-05-23 21:07:59 +00001618
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001619 return Method;
1620}
1621
Douglas Gregor615c5d42009-03-24 16:43:20 +00001622Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +00001623 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +00001624}
1625
Douglas Gregor03b2b072009-03-24 00:15:49 +00001626Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001627 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +00001628}
1629
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001630Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001631 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001632}
1633
Douglas Gregor6477b692009-03-25 15:04:13 +00001634ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
John McCallfb44de92011-05-01 22:35:37 +00001635 return SemaRef.SubstParmVarDecl(D, TemplateArgs, /*indexAdjustment*/ 0,
Douglas Gregord1bb4ae2012-01-25 16:15:54 +00001636 llvm::Optional<unsigned>(),
1637 /*ExpectParameterPack=*/false);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001638}
1639
John McCalle29ba202009-08-20 01:44:21 +00001640Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1641 TemplateTypeParmDecl *D) {
1642 // TODO: don't always clone when decls are refcounted.
Chandler Carruth4fb86f82011-05-01 00:51:33 +00001643 assert(D->getTypeForDecl()->isTemplateTypeParmType());
Mike Stump1eb44332009-09-09 15:08:12 +00001644
John McCalle29ba202009-08-20 01:44:21 +00001645 TemplateTypeParmDecl *Inst =
Abramo Bagnara344577e2011-03-06 15:48:19 +00001646 TemplateTypeParmDecl::Create(SemaRef.Context, Owner,
1647 D->getLocStart(), D->getLocation(),
Chandler Carruth4fb86f82011-05-01 00:51:33 +00001648 D->getDepth() - TemplateArgs.getNumLevels(),
1649 D->getIndex(), D->getIdentifier(),
John McCalle29ba202009-08-20 01:44:21 +00001650 D->wasDeclaredWithTypename(),
1651 D->isParameterPack());
Douglas Gregor9a299e02011-03-04 17:52:15 +00001652 Inst->setAccess(AS_public);
John McCalle29ba202009-08-20 01:44:21 +00001653
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001654 if (D->hasDefaultArgument())
1655 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
1656
1657 // Introduce this template parameter's instantiation into the instantiation
Douglas Gregor550d9b22009-10-31 17:21:17 +00001658 // scope.
1659 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001660
John McCalle29ba202009-08-20 01:44:21 +00001661 return Inst;
1662}
1663
Douglas Gregor33642df2009-10-23 23:25:44 +00001664Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1665 NonTypeTemplateParmDecl *D) {
1666 // Substitute into the type of the non-type template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001667 TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc();
Chris Lattner5f9e2722011-07-23 10:55:15 +00001668 SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten;
1669 SmallVector<QualType, 4> ExpandedParameterPackTypes;
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001670 bool IsExpandedParameterPack = false;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001671 TypeSourceInfo *DI;
Douglas Gregor33642df2009-10-23 23:25:44 +00001672 QualType T;
Douglas Gregor33642df2009-10-23 23:25:44 +00001673 bool Invalid = false;
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001674
1675 if (D->isExpandedParameterPack()) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001676 // The non-type template parameter pack is an already-expanded pack
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001677 // expansion of types. Substitute into each of the expanded types.
1678 ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes());
1679 ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes());
1680 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
1681 TypeSourceInfo *NewDI =SemaRef.SubstType(D->getExpansionTypeSourceInfo(I),
1682 TemplateArgs,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001683 D->getLocation(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001684 D->getDeclName());
1685 if (!NewDI)
1686 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001687
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001688 ExpandedParameterPackTypesAsWritten.push_back(NewDI);
1689 QualType NewT =SemaRef.CheckNonTypeTemplateParameterType(NewDI->getType(),
1690 D->getLocation());
1691 if (NewT.isNull())
1692 return 0;
1693 ExpandedParameterPackTypes.push_back(NewT);
1694 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001695
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001696 IsExpandedParameterPack = true;
1697 DI = D->getTypeSourceInfo();
1698 T = DI->getType();
Richard Smith6964b3f2012-09-07 02:06:42 +00001699 } else if (D->isPackExpansion()) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001700 // The non-type template parameter pack's type is a pack expansion of types.
1701 // Determine whether we need to expand this parameter pack into separate
1702 // types.
1703 PackExpansionTypeLoc Expansion = cast<PackExpansionTypeLoc>(TL);
1704 TypeLoc Pattern = Expansion.getPatternLoc();
Chris Lattner5f9e2722011-07-23 10:55:15 +00001705 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001706 SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001707
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001708 // Determine whether the set of unexpanded parameter packs can and should
1709 // be expanded.
1710 bool Expand = true;
1711 bool RetainExpansion = false;
1712 llvm::Optional<unsigned> OrigNumExpansions
1713 = Expansion.getTypePtr()->getNumExpansions();
1714 llvm::Optional<unsigned> NumExpansions = OrigNumExpansions;
1715 if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(),
1716 Pattern.getSourceRange(),
David Blaikiea71f9d02011-09-22 02:34:54 +00001717 Unexpanded,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001718 TemplateArgs,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001719 Expand, RetainExpansion,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001720 NumExpansions))
1721 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001722
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001723 if (Expand) {
1724 for (unsigned I = 0; I != *NumExpansions; ++I) {
1725 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
1726 TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001727 D->getLocation(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001728 D->getDeclName());
1729 if (!NewDI)
1730 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001731
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001732 ExpandedParameterPackTypesAsWritten.push_back(NewDI);
1733 QualType NewT = SemaRef.CheckNonTypeTemplateParameterType(
1734 NewDI->getType(),
1735 D->getLocation());
1736 if (NewT.isNull())
1737 return 0;
1738 ExpandedParameterPackTypes.push_back(NewT);
1739 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001740
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001741 // Note that we have an expanded parameter pack. The "type" of this
1742 // expanded parameter pack is the original expansion type, but callers
1743 // will end up using the expanded parameter pack types for type-checking.
1744 IsExpandedParameterPack = true;
1745 DI = D->getTypeSourceInfo();
1746 T = DI->getType();
1747 } else {
1748 // We cannot fully expand the pack expansion now, so substitute into the
1749 // pattern and create a new pack expansion type.
1750 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
1751 TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001752 D->getLocation(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001753 D->getDeclName());
1754 if (!NewPattern)
1755 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001756
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001757 DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(),
1758 NumExpansions);
1759 if (!DI)
1760 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001761
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001762 T = DI->getType();
1763 }
1764 } else {
1765 // Simple case: substitution into a parameter that is not a parameter pack.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001766 DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001767 D->getLocation(), D->getDeclName());
1768 if (!DI)
1769 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001770
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001771 // Check that this type is acceptable for a non-type template parameter.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001772 T = SemaRef.CheckNonTypeTemplateParameterType(DI->getType(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001773 D->getLocation());
1774 if (T.isNull()) {
1775 T = SemaRef.Context.IntTy;
1776 Invalid = true;
1777 }
Douglas Gregor33642df2009-10-23 23:25:44 +00001778 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001779
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001780 NonTypeTemplateParmDecl *Param;
1781 if (IsExpandedParameterPack)
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001782 Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001783 D->getInnerLocStart(),
1784 D->getLocation(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001785 D->getDepth() - TemplateArgs.getNumLevels(),
1786 D->getPosition(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001787 D->getIdentifier(), T,
1788 DI,
1789 ExpandedParameterPackTypes.data(),
1790 ExpandedParameterPackTypes.size(),
1791 ExpandedParameterPackTypesAsWritten.data());
1792 else
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001793 Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001794 D->getInnerLocStart(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001795 D->getLocation(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001796 D->getDepth() - TemplateArgs.getNumLevels(),
1797 D->getPosition(),
1798 D->getIdentifier(), T,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001799 D->isParameterPack(), DI);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001800
Douglas Gregor9a299e02011-03-04 17:52:15 +00001801 Param->setAccess(AS_public);
Douglas Gregor33642df2009-10-23 23:25:44 +00001802 if (Invalid)
1803 Param->setInvalidDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001804
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001805 Param->setDefaultArgument(D->getDefaultArgument(), false);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001806
1807 // Introduce this template parameter's instantiation into the instantiation
Douglas Gregor550d9b22009-10-31 17:21:17 +00001808 // scope.
1809 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001810 return Param;
1811}
1812
Richard Smith6964b3f2012-09-07 02:06:42 +00001813static void collectUnexpandedParameterPacks(
1814 Sema &S,
1815 TemplateParameterList *Params,
1816 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
1817 for (TemplateParameterList::const_iterator I = Params->begin(),
1818 E = Params->end(); I != E; ++I) {
1819 if ((*I)->isTemplateParameterPack())
1820 continue;
1821 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*I))
1822 S.collectUnexpandedParameterPacks(NTTP->getTypeSourceInfo()->getTypeLoc(),
1823 Unexpanded);
1824 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(*I))
1825 collectUnexpandedParameterPacks(S, TTP->getTemplateParameters(),
1826 Unexpanded);
1827 }
1828}
1829
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001830Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001831TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1832 TemplateTemplateParmDecl *D) {
1833 // Instantiate the template parameter list of the template template parameter.
1834 TemplateParameterList *TempParams = D->getTemplateParameters();
1835 TemplateParameterList *InstParams;
Richard Smith6964b3f2012-09-07 02:06:42 +00001836 SmallVector<TemplateParameterList*, 8> ExpandedParams;
1837
1838 bool IsExpandedParameterPack = false;
1839
1840 if (D->isExpandedParameterPack()) {
1841 // The template template parameter pack is an already-expanded pack
1842 // expansion of template parameters. Substitute into each of the expanded
1843 // parameters.
1844 ExpandedParams.reserve(D->getNumExpansionTemplateParameters());
1845 for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
1846 I != N; ++I) {
1847 LocalInstantiationScope Scope(SemaRef);
1848 TemplateParameterList *Expansion =
1849 SubstTemplateParams(D->getExpansionTemplateParameters(I));
1850 if (!Expansion)
1851 return 0;
1852 ExpandedParams.push_back(Expansion);
1853 }
1854
1855 IsExpandedParameterPack = true;
1856 InstParams = TempParams;
1857 } else if (D->isPackExpansion()) {
1858 // The template template parameter pack expands to a pack of template
1859 // template parameters. Determine whether we need to expand this parameter
1860 // pack into separate parameters.
1861 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
1862 collectUnexpandedParameterPacks(SemaRef, D->getTemplateParameters(),
1863 Unexpanded);
1864
1865 // Determine whether the set of unexpanded parameter packs can and should
1866 // be expanded.
1867 bool Expand = true;
1868 bool RetainExpansion = false;
1869 llvm::Optional<unsigned> NumExpansions;
1870 if (SemaRef.CheckParameterPacksForExpansion(D->getLocation(),
1871 TempParams->getSourceRange(),
1872 Unexpanded,
1873 TemplateArgs,
1874 Expand, RetainExpansion,
1875 NumExpansions))
1876 return 0;
1877
1878 if (Expand) {
1879 for (unsigned I = 0; I != *NumExpansions; ++I) {
1880 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
1881 LocalInstantiationScope Scope(SemaRef);
1882 TemplateParameterList *Expansion = SubstTemplateParams(TempParams);
1883 if (!Expansion)
1884 return 0;
1885 ExpandedParams.push_back(Expansion);
1886 }
1887
1888 // Note that we have an expanded parameter pack. The "type" of this
1889 // expanded parameter pack is the original expansion type, but callers
1890 // will end up using the expanded parameter pack types for type-checking.
1891 IsExpandedParameterPack = true;
1892 InstParams = TempParams;
1893 } else {
1894 // We cannot fully expand the pack expansion now, so just substitute
1895 // into the pattern.
1896 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
1897
1898 LocalInstantiationScope Scope(SemaRef);
1899 InstParams = SubstTemplateParams(TempParams);
1900 if (!InstParams)
1901 return 0;
1902 }
1903 } else {
Douglas Gregor9106ef72009-11-11 16:58:32 +00001904 // Perform the actual substitution of template parameters within a new,
1905 // local instantiation scope.
John McCall2a7fb272010-08-25 05:32:35 +00001906 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor9106ef72009-11-11 16:58:32 +00001907 InstParams = SubstTemplateParams(TempParams);
1908 if (!InstParams)
Richard Smith6964b3f2012-09-07 02:06:42 +00001909 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001910 }
1911
Douglas Gregor9106ef72009-11-11 16:58:32 +00001912 // Build the template template parameter.
Richard Smith6964b3f2012-09-07 02:06:42 +00001913 TemplateTemplateParmDecl *Param;
1914 if (IsExpandedParameterPack)
1915 Param = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner,
1916 D->getLocation(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001917 D->getDepth() - TemplateArgs.getNumLevels(),
Richard Smith6964b3f2012-09-07 02:06:42 +00001918 D->getPosition(),
1919 D->getIdentifier(), InstParams,
1920 ExpandedParams);
1921 else
1922 Param = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner,
1923 D->getLocation(),
1924 D->getDepth() - TemplateArgs.getNumLevels(),
1925 D->getPosition(),
1926 D->isParameterPack(),
1927 D->getIdentifier(), InstParams);
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001928 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor9a299e02011-03-04 17:52:15 +00001929 Param->setAccess(AS_public);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001930
1931 // Introduce this template parameter's instantiation into the instantiation
Douglas Gregor9106ef72009-11-11 16:58:32 +00001932 // scope.
1933 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001934
Douglas Gregor9106ef72009-11-11 16:58:32 +00001935 return Param;
1936}
1937
Douglas Gregor48c32a72009-11-17 06:07:40 +00001938Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Douglas Gregordb992412011-02-25 16:33:46 +00001939 // Using directives are never dependent (and never contain any types or
1940 // expressions), so they require no explicit instantiation work.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001941
Douglas Gregor48c32a72009-11-17 06:07:40 +00001942 UsingDirectiveDecl *Inst
1943 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001944 D->getNamespaceKeyLocation(),
Douglas Gregordb992412011-02-25 16:33:46 +00001945 D->getQualifierLoc(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001946 D->getIdentLocation(),
1947 D->getNominatedNamespace(),
Douglas Gregor48c32a72009-11-17 06:07:40 +00001948 D->getCommonAncestor());
Abramo Bagnara536afbe2012-09-05 09:55:10 +00001949
1950 // Add the using directive to its declaration context
1951 // only if this is not a function or method.
1952 if (!Owner->isFunctionOrMethod())
1953 Owner->addDecl(Inst);
1954
Douglas Gregor48c32a72009-11-17 06:07:40 +00001955 return Inst;
1956}
1957
John McCalled976492009-12-04 22:46:56 +00001958Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
Douglas Gregor1b398202010-09-29 17:58:28 +00001959
1960 // The nested name specifier may be dependent, for example
1961 // template <typename T> struct t {
1962 // struct s1 { T f1(); };
1963 // struct s2 : s1 { using s1::f1; };
1964 // };
1965 // template struct t<int>;
1966 // Here, in using s1::f1, s1 refers to t<T>::s1;
1967 // we need to substitute for t<int>::s1.
Douglas Gregor5149f372011-02-25 15:54:31 +00001968 NestedNameSpecifierLoc QualifierLoc
1969 = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
1970 TemplateArgs);
1971 if (!QualifierLoc)
Douglas Gregordc355712011-02-25 00:36:19 +00001972 return 0;
Douglas Gregor1b398202010-09-29 17:58:28 +00001973
1974 // The name info is non-dependent, so no transformation
1975 // is required.
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001976 DeclarationNameInfo NameInfo = D->getNameInfo();
John McCalled976492009-12-04 22:46:56 +00001977
John McCall9f54ad42009-12-10 09:41:52 +00001978 // We only need to do redeclaration lookups if we're in a class
1979 // scope (in fact, it's not really even possible in non-class
1980 // scopes).
1981 bool CheckRedeclaration = Owner->isRecord();
1982
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001983 LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
1984 Sema::ForRedeclaration);
John McCall9f54ad42009-12-10 09:41:52 +00001985
John McCalled976492009-12-04 22:46:56 +00001986 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
John McCalled976492009-12-04 22:46:56 +00001987 D->getUsingLocation(),
Douglas Gregor5149f372011-02-25 15:54:31 +00001988 QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001989 NameInfo,
John McCalled976492009-12-04 22:46:56 +00001990 D->isTypeName());
1991
Douglas Gregor5149f372011-02-25 15:54:31 +00001992 CXXScopeSpec SS;
1993 SS.Adopt(QualifierLoc);
John McCall9f54ad42009-12-10 09:41:52 +00001994 if (CheckRedeclaration) {
1995 Prev.setHideTags(false);
1996 SemaRef.LookupQualifiedName(Prev, Owner);
1997
1998 // Check for invalid redeclarations.
1999 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
2000 D->isTypeName(), SS,
2001 D->getLocation(), Prev))
2002 NewUD->setInvalidDecl();
2003
2004 }
2005
2006 if (!NewUD->isInvalidDecl() &&
2007 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
John McCalled976492009-12-04 22:46:56 +00002008 D->getLocation()))
2009 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00002010
John McCalled976492009-12-04 22:46:56 +00002011 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
2012 NewUD->setAccess(D->getAccess());
2013 Owner->addDecl(NewUD);
2014
John McCall9f54ad42009-12-10 09:41:52 +00002015 // Don't process the shadow decls for an invalid decl.
2016 if (NewUD->isInvalidDecl())
2017 return NewUD;
2018
Richard Smithc5a89a12012-04-02 01:30:27 +00002019 if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) {
2020 if (SemaRef.CheckInheritingConstructorUsingDecl(NewUD))
2021 NewUD->setInvalidDecl();
2022 return NewUD;
2023 }
2024
John McCall323c3102009-12-22 22:26:37 +00002025 bool isFunctionScope = Owner->isFunctionOrMethod();
2026
John McCall9f54ad42009-12-10 09:41:52 +00002027 // Process the shadow decls.
2028 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
2029 I != E; ++I) {
2030 UsingShadowDecl *Shadow = *I;
2031 NamedDecl *InstTarget =
Douglas Gregorb7107222011-03-04 19:46:35 +00002032 cast_or_null<NamedDecl>(SemaRef.FindInstantiatedDecl(
2033 Shadow->getLocation(),
2034 Shadow->getTargetDecl(),
2035 TemplateArgs));
2036 if (!InstTarget)
2037 return 0;
John McCall9f54ad42009-12-10 09:41:52 +00002038
2039 if (CheckRedeclaration &&
2040 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
2041 continue;
2042
2043 UsingShadowDecl *InstShadow
2044 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
2045 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
John McCall323c3102009-12-22 22:26:37 +00002046
2047 if (isFunctionScope)
2048 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
John McCall9f54ad42009-12-10 09:41:52 +00002049 }
John McCalled976492009-12-04 22:46:56 +00002050
2051 return NewUD;
2052}
2053
2054Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00002055 // Ignore these; we handle them in bulk when processing the UsingDecl.
2056 return 0;
John McCalled976492009-12-04 22:46:56 +00002057}
2058
John McCall7ba107a2009-11-18 02:36:19 +00002059Decl * TemplateDeclInstantiator
2060 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Douglas Gregor5149f372011-02-25 15:54:31 +00002061 NestedNameSpecifierLoc QualifierLoc
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002062 = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
Douglas Gregor5149f372011-02-25 15:54:31 +00002063 TemplateArgs);
2064 if (!QualifierLoc)
Anders Carlsson0dde18e2009-08-28 15:18:15 +00002065 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002066
Anders Carlsson0dde18e2009-08-28 15:18:15 +00002067 CXXScopeSpec SS;
Douglas Gregor5149f372011-02-25 15:54:31 +00002068 SS.Adopt(QualifierLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002069
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002070 // Since NameInfo refers to a typename, it cannot be a C++ special name.
Benjamin Krameraccaf192012-11-14 15:08:31 +00002071 // Hence, no transformation is required for it.
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002072 DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00002073 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00002074 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002075 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00002076 /*instantiation*/ true,
2077 /*typename*/ true, D->getTypenameLoc());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00002078 if (UD)
John McCalled976492009-12-04 22:46:56 +00002079 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
2080
John McCall7ba107a2009-11-18 02:36:19 +00002081 return UD;
2082}
2083
2084Decl * TemplateDeclInstantiator
2085 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Douglas Gregor5149f372011-02-25 15:54:31 +00002086 NestedNameSpecifierLoc QualifierLoc
2087 = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), TemplateArgs);
2088 if (!QualifierLoc)
John McCall7ba107a2009-11-18 02:36:19 +00002089 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002090
John McCall7ba107a2009-11-18 02:36:19 +00002091 CXXScopeSpec SS;
Douglas Gregor5149f372011-02-25 15:54:31 +00002092 SS.Adopt(QualifierLoc);
John McCall7ba107a2009-11-18 02:36:19 +00002093
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002094 DeclarationNameInfo NameInfo
2095 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
2096
John McCall7ba107a2009-11-18 02:36:19 +00002097 NamedDecl *UD =
2098 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002099 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00002100 /*instantiation*/ true,
2101 /*typename*/ false, SourceLocation());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00002102 if (UD)
John McCalled976492009-12-04 22:46:56 +00002103 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
2104
Anders Carlsson0d8df782009-08-29 19:37:28 +00002105 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00002106}
2107
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002108
2109Decl *TemplateDeclInstantiator::VisitClassScopeFunctionSpecializationDecl(
2110 ClassScopeFunctionSpecializationDecl *Decl) {
2111 CXXMethodDecl *OldFD = Decl->getSpecialization();
Nico Weber6b020092012-06-25 17:21:05 +00002112 CXXMethodDecl *NewFD = cast<CXXMethodDecl>(VisitCXXMethodDecl(OldFD,
2113 0, true));
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002114
2115 LookupResult Previous(SemaRef, NewFD->getNameInfo(), Sema::LookupOrdinaryName,
2116 Sema::ForRedeclaration);
2117
Nico Weber6b020092012-06-25 17:21:05 +00002118 TemplateArgumentListInfo TemplateArgs;
2119 TemplateArgumentListInfo* TemplateArgsPtr = 0;
2120 if (Decl->hasExplicitTemplateArgs()) {
2121 TemplateArgs = Decl->templateArgs();
2122 TemplateArgsPtr = &TemplateArgs;
2123 }
2124
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002125 SemaRef.LookupQualifiedName(Previous, SemaRef.CurContext);
Nico Weber6b020092012-06-25 17:21:05 +00002126 if (SemaRef.CheckFunctionTemplateSpecialization(NewFD, TemplateArgsPtr,
2127 Previous)) {
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002128 NewFD->setInvalidDecl();
2129 return NewFD;
2130 }
2131
2132 // Associate the specialization with the pattern.
2133 FunctionDecl *Specialization = cast<FunctionDecl>(Previous.getFoundDecl());
2134 assert(Specialization && "Class scope Specialization is null");
2135 SemaRef.Context.setClassScopeSpecializationPattern(Specialization, OldFD);
2136
2137 return NewFD;
2138}
2139
John McCallce3ff2b2009-08-25 22:02:44 +00002140Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00002141 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00002142 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor2fa98002010-02-16 19:28:15 +00002143 if (D->isInvalidDecl())
2144 return 0;
2145
Douglas Gregor8dbc2692009-03-17 21:15:40 +00002146 return Instantiator.Visit(D);
2147}
2148
John McCalle29ba202009-08-20 01:44:21 +00002149/// \brief Instantiates a nested template parameter list in the current
2150/// instantiation context.
2151///
2152/// \param L The parameter list to instantiate
2153///
2154/// \returns NULL if there was an error
2155TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00002156TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00002157 // Get errors for all the parameters before bailing out.
2158 bool Invalid = false;
2159
2160 unsigned N = L->size();
Chris Lattner5f9e2722011-07-23 10:55:15 +00002161 typedef SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00002162 ParamVector Params;
2163 Params.reserve(N);
2164 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
2165 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002166 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00002167 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00002168 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00002169 }
2170
2171 // Clean up if we had an error.
Douglas Gregorff331c12010-07-25 18:17:45 +00002172 if (Invalid)
John McCalle29ba202009-08-20 01:44:21 +00002173 return NULL;
John McCalle29ba202009-08-20 01:44:21 +00002174
2175 TemplateParameterList *InstL
2176 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
2177 L->getLAngleLoc(), &Params.front(), N,
2178 L->getRAngleLoc());
2179 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00002180}
John McCalle29ba202009-08-20 01:44:21 +00002181
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002182/// \brief Instantiate the declaration of a class template partial
Douglas Gregored9c0f92009-10-29 00:04:11 +00002183/// specialization.
2184///
2185/// \param ClassTemplate the (instantiated) class template that is partially
2186// specialized by the instantiation of \p PartialSpec.
2187///
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002188/// \param PartialSpec the (uninstantiated) class template partial
Douglas Gregored9c0f92009-10-29 00:04:11 +00002189/// specialization that we are instantiating.
2190///
Douglas Gregord65587f2010-11-10 19:44:59 +00002191/// \returns The instantiated partial specialization, if successful; otherwise,
2192/// NULL to indicate an error.
2193ClassTemplatePartialSpecializationDecl *
Douglas Gregored9c0f92009-10-29 00:04:11 +00002194TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
2195 ClassTemplateDecl *ClassTemplate,
2196 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00002197 // Create a local instantiation scope for this class template partial
2198 // specialization, which will contain the instantiations of the template
2199 // parameters.
John McCall2a7fb272010-08-25 05:32:35 +00002200 LocalInstantiationScope Scope(SemaRef);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002201
Douglas Gregored9c0f92009-10-29 00:04:11 +00002202 // Substitute into the template parameters of the class template partial
2203 // specialization.
2204 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
2205 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
2206 if (!InstParams)
Douglas Gregord65587f2010-11-10 19:44:59 +00002207 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002208
Douglas Gregored9c0f92009-10-29 00:04:11 +00002209 // Substitute into the template arguments of the class template partial
2210 // specialization.
John McCalld5532b62009-11-23 01:53:49 +00002211 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002212 if (SemaRef.Subst(PartialSpec->getTemplateArgsAsWritten(),
2213 PartialSpec->getNumTemplateArgsAsWritten(),
Douglas Gregore02e2622010-12-22 21:19:48 +00002214 InstTemplateArgs, TemplateArgs))
2215 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002216
Douglas Gregored9c0f92009-10-29 00:04:11 +00002217 // Check that the template argument list is well-formed for this
2218 // class template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002219 SmallVector<TemplateArgument, 4> Converted;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002220 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002221 PartialSpec->getLocation(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002222 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002223 false,
2224 Converted))
Douglas Gregord65587f2010-11-10 19:44:59 +00002225 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00002226
2227 // Figure out where to insert this class template partial specialization
2228 // in the member template's set of class template partial specializations.
Douglas Gregored9c0f92009-10-29 00:04:11 +00002229 void *InsertPos = 0;
2230 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00002231 = ClassTemplate->findPartialSpecialization(Converted.data(),
2232 Converted.size(), InsertPos);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002233
Douglas Gregored9c0f92009-10-29 00:04:11 +00002234 // Build the canonical type that describes the converted template
2235 // arguments of the class template partial specialization.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002236 QualType CanonType
Douglas Gregored9c0f92009-10-29 00:04:11 +00002237 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
Douglas Gregor910f8002010-11-07 23:05:16 +00002238 Converted.data(),
2239 Converted.size());
Douglas Gregored9c0f92009-10-29 00:04:11 +00002240
2241 // Build the fully-sugared type for this class template
2242 // specialization as the user wrote in the specialization
2243 // itself. This means that we'll pretty-print the type retrieved
2244 // from the specialization's declaration the way that the user
2245 // actually wrote the specialization, rather than formatting the
2246 // name based on the "canonical" representation used to store the
2247 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00002248 TypeSourceInfo *WrittenTy
2249 = SemaRef.Context.getTemplateSpecializationTypeInfo(
2250 TemplateName(ClassTemplate),
2251 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00002252 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002253 CanonType);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002254
Douglas Gregored9c0f92009-10-29 00:04:11 +00002255 if (PrevDecl) {
2256 // We've already seen a partial specialization with the same template
2257 // parameters and template arguments. This can happen, for example, when
2258 // substituting the outer template arguments ends up causing two
2259 // class template partial specializations of a member class template
2260 // to have identical forms, e.g.,
2261 //
2262 // template<typename T, typename U>
2263 // struct Outer {
2264 // template<typename X, typename Y> struct Inner;
2265 // template<typename Y> struct Inner<T, Y>;
2266 // template<typename Y> struct Inner<U, Y>;
2267 // };
2268 //
2269 // Outer<int, int> outer; // error: the partial specializations of Inner
2270 // // have the same signature.
2271 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
Douglas Gregord65587f2010-11-10 19:44:59 +00002272 << WrittenTy->getType();
Douglas Gregored9c0f92009-10-29 00:04:11 +00002273 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
2274 << SemaRef.Context.getTypeDeclType(PrevDecl);
Douglas Gregord65587f2010-11-10 19:44:59 +00002275 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00002276 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002277
2278
Douglas Gregored9c0f92009-10-29 00:04:11 +00002279 // Create the class template partial specialization declaration.
2280 ClassTemplatePartialSpecializationDecl *InstPartialSpec
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002281 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00002282 PartialSpec->getTagKind(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002283 Owner,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002284 PartialSpec->getLocStart(),
2285 PartialSpec->getLocation(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002286 InstParams,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002287 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00002288 Converted.data(),
2289 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00002290 InstTemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00002291 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00002292 0,
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00002293 ClassTemplate->getNextPartialSpecSequenceNumber());
John McCallb6217662010-03-15 10:12:16 +00002294 // Substitute the nested name specifier, if any.
2295 if (SubstQualifier(PartialSpec, InstPartialSpec))
2296 return 0;
2297
Douglas Gregored9c0f92009-10-29 00:04:11 +00002298 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00002299 InstPartialSpec->setTypeAsWritten(WrittenTy);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002300
Douglas Gregored9c0f92009-10-29 00:04:11 +00002301 // Add this partial specialization to the set of class template partial
2302 // specializations.
Douglas Gregor1e1e9722012-03-28 14:34:23 +00002303 ClassTemplate->AddPartialSpecialization(InstPartialSpec, /*InsertPos=*/0);
Douglas Gregord65587f2010-11-10 19:44:59 +00002304 return InstPartialSpec;
Douglas Gregored9c0f92009-10-29 00:04:11 +00002305}
2306
John McCall21ef0fa2010-03-11 09:03:00 +00002307TypeSourceInfo*
John McCallce3ff2b2009-08-25 22:02:44 +00002308TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002309 SmallVectorImpl<ParmVarDecl *> &Params) {
John McCall21ef0fa2010-03-11 09:03:00 +00002310 TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
2311 assert(OldTInfo && "substituting function without type source info");
2312 assert(Params.empty() && "parameter vector is non-empty at start");
Douglas Gregorcefc3af2012-04-16 07:05:22 +00002313
2314 CXXRecordDecl *ThisContext = 0;
2315 unsigned ThisTypeQuals = 0;
2316 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2317 ThisContext = Method->getParent();
2318 ThisTypeQuals = Method->getTypeQualifiers();
2319 }
2320
John McCall6cd3b9f2010-04-09 17:38:44 +00002321 TypeSourceInfo *NewTInfo
2322 = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
2323 D->getTypeSpecStartLoc(),
Douglas Gregorcefc3af2012-04-16 07:05:22 +00002324 D->getDeclName(),
2325 ThisContext, ThisTypeQuals);
John McCall21ef0fa2010-03-11 09:03:00 +00002326 if (!NewTInfo)
2327 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +00002328
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00002329 if (NewTInfo != OldTInfo) {
2330 // Get parameters from the new type info.
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00002331 TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00002332 if (FunctionProtoTypeLoc *OldProtoLoc
2333 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00002334 TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00002335 FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
2336 assert(NewProtoLoc && "Missing prototype?");
Richard Smith500d7292012-07-18 01:29:05 +00002337 unsigned NewIdx = 0;
Douglas Gregor12c9c002011-01-07 16:43:16 +00002338 for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc->getNumArgs();
2339 OldIdx != NumOldParams; ++OldIdx) {
2340 ParmVarDecl *OldParam = OldProtoLoc->getArg(OldIdx);
Richard Smith500d7292012-07-18 01:29:05 +00002341 LocalInstantiationScope *Scope = SemaRef.CurrentInstantiationScope;
2342
2343 llvm::Optional<unsigned> NumArgumentsInExpansion;
2344 if (OldParam->isParameterPack())
2345 NumArgumentsInExpansion =
2346 SemaRef.getNumArgumentsInExpansion(OldParam->getType(),
2347 TemplateArgs);
2348 if (!NumArgumentsInExpansion) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002349 // Simple case: normal parameter, or a parameter pack that's
Douglas Gregor12c9c002011-01-07 16:43:16 +00002350 // instantiated to a (still-dependent) parameter pack.
2351 ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++);
2352 Params.push_back(NewParam);
Richard Smith500d7292012-07-18 01:29:05 +00002353 Scope->InstantiatedLocal(OldParam, NewParam);
2354 } else {
2355 // Parameter pack expansion: make the instantiation an argument pack.
2356 Scope->MakeInstantiatedLocalArgPack(OldParam);
2357 for (unsigned I = 0; I != *NumArgumentsInExpansion; ++I) {
2358 ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++);
2359 Params.push_back(NewParam);
2360 Scope->InstantiatedLocalPackArg(OldParam, NewParam);
2361 }
Douglas Gregor12c9c002011-01-07 16:43:16 +00002362 }
Douglas Gregor6920cdc2010-05-03 15:32:18 +00002363 }
Douglas Gregor895162d2010-04-30 18:55:50 +00002364 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00002365 } else {
2366 // The function type itself was not dependent and therefore no
2367 // substitution occurred. However, we still need to instantiate
2368 // the function parameters themselves.
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00002369 TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00002370 if (FunctionProtoTypeLoc *OldProtoLoc
2371 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
2372 for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
2373 ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
2374 if (!Parm)
2375 return 0;
2376 Params.push_back(Parm);
2377 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00002378 }
2379 }
John McCall21ef0fa2010-03-11 09:03:00 +00002380 return NewTInfo;
Douglas Gregor5545e162009-03-24 00:38:23 +00002381}
2382
Richard Smithe6975e92012-04-17 00:58:00 +00002383/// Introduce the instantiated function parameters into the local
2384/// instantiation scope, and set the parameter names to those used
2385/// in the template.
2386static void addInstantiatedParametersToScope(Sema &S, FunctionDecl *Function,
2387 const FunctionDecl *PatternDecl,
2388 LocalInstantiationScope &Scope,
2389 const MultiLevelTemplateArgumentList &TemplateArgs) {
2390 unsigned FParamIdx = 0;
2391 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
2392 const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
2393 if (!PatternParam->isParameterPack()) {
2394 // Simple case: not a parameter pack.
2395 assert(FParamIdx < Function->getNumParams());
2396 ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
2397 FunctionParam->setDeclName(PatternParam->getDeclName());
2398 Scope.InstantiatedLocal(PatternParam, FunctionParam);
2399 ++FParamIdx;
2400 continue;
2401 }
2402
2403 // Expand the parameter pack.
2404 Scope.MakeInstantiatedLocalArgPack(PatternParam);
Richard Smith500d7292012-07-18 01:29:05 +00002405 llvm::Optional<unsigned> NumArgumentsInExpansion
Richard Smithe6975e92012-04-17 00:58:00 +00002406 = S.getNumArgumentsInExpansion(PatternParam->getType(), TemplateArgs);
Richard Smith500d7292012-07-18 01:29:05 +00002407 assert(NumArgumentsInExpansion &&
2408 "should only be called when all template arguments are known");
2409 for (unsigned Arg = 0; Arg < *NumArgumentsInExpansion; ++Arg) {
Richard Smithe6975e92012-04-17 00:58:00 +00002410 ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
2411 FunctionParam->setDeclName(PatternParam->getDeclName());
2412 Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam);
2413 ++FParamIdx;
2414 }
2415 }
2416}
2417
2418static void InstantiateExceptionSpec(Sema &SemaRef, FunctionDecl *New,
2419 const FunctionProtoType *Proto,
2420 const MultiLevelTemplateArgumentList &TemplateArgs) {
Richard Smith13bffc52012-04-19 00:08:28 +00002421 assert(Proto->getExceptionSpecType() != EST_Uninstantiated);
2422
Richard Smithe6975e92012-04-17 00:58:00 +00002423 // C++11 [expr.prim.general]p3:
2424 // If a declaration declares a member function or member function
2425 // template of a class X, the expression this is a prvalue of type
2426 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
2427 // and the end of the function-definition, member-declarator, or
2428 // declarator.
2429 CXXRecordDecl *ThisContext = 0;
2430 unsigned ThisTypeQuals = 0;
2431 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(New)) {
2432 ThisContext = Method->getParent();
2433 ThisTypeQuals = Method->getTypeQualifiers();
2434 }
2435 Sema::CXXThisScopeRAII ThisScope(SemaRef, ThisContext, ThisTypeQuals,
Richard Smith80ad52f2013-01-02 11:42:31 +00002436 SemaRef.getLangOpts().CPlusPlus11);
Richard Smithe6975e92012-04-17 00:58:00 +00002437
2438 // The function has an exception specification or a "noreturn"
2439 // attribute. Substitute into each of the exception types.
2440 SmallVector<QualType, 4> Exceptions;
2441 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
2442 // FIXME: Poor location information!
2443 if (const PackExpansionType *PackExpansion
2444 = Proto->getExceptionType(I)->getAs<PackExpansionType>()) {
2445 // We have a pack expansion. Instantiate it.
2446 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2447 SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(),
2448 Unexpanded);
2449 assert(!Unexpanded.empty() &&
2450 "Pack expansion without parameter packs?");
2451
2452 bool Expand = false;
2453 bool RetainExpansion = false;
2454 llvm::Optional<unsigned> NumExpansions
2455 = PackExpansion->getNumExpansions();
2456 if (SemaRef.CheckParameterPacksForExpansion(New->getLocation(),
2457 SourceRange(),
2458 Unexpanded,
2459 TemplateArgs,
2460 Expand,
2461 RetainExpansion,
2462 NumExpansions))
2463 break;
2464
2465 if (!Expand) {
2466 // We can't expand this pack expansion into separate arguments yet;
2467 // just substitute into the pattern and create a new pack expansion
2468 // type.
2469 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
2470 QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2471 TemplateArgs,
2472 New->getLocation(), New->getDeclName());
2473 if (T.isNull())
2474 break;
2475
2476 T = SemaRef.Context.getPackExpansionType(T, NumExpansions);
2477 Exceptions.push_back(T);
2478 continue;
2479 }
2480
2481 // Substitute into the pack expansion pattern for each template
2482 bool Invalid = false;
2483 for (unsigned ArgIdx = 0; ArgIdx != *NumExpansions; ++ArgIdx) {
2484 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, ArgIdx);
2485
2486 QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2487 TemplateArgs,
2488 New->getLocation(), New->getDeclName());
2489 if (T.isNull()) {
2490 Invalid = true;
2491 break;
2492 }
2493
2494 Exceptions.push_back(T);
2495 }
2496
2497 if (Invalid)
2498 break;
2499
2500 continue;
2501 }
2502
2503 QualType T
2504 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
2505 New->getLocation(), New->getDeclName());
2506 if (T.isNull() ||
2507 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
2508 continue;
2509
2510 Exceptions.push_back(T);
2511 }
2512 Expr *NoexceptExpr = 0;
2513 if (Expr *OldNoexceptExpr = Proto->getNoexceptExpr()) {
2514 EnterExpressionEvaluationContext Unevaluated(SemaRef,
2515 Sema::ConstantEvaluated);
2516 ExprResult E = SemaRef.SubstExpr(OldNoexceptExpr, TemplateArgs);
2517 if (E.isUsable())
2518 E = SemaRef.CheckBooleanCondition(E.get(), E.get()->getLocStart());
2519
2520 if (E.isUsable()) {
2521 NoexceptExpr = E.take();
2522 if (!NoexceptExpr->isTypeDependent() &&
2523 !NoexceptExpr->isValueDependent())
Douglas Gregorab41fe92012-05-04 22:38:52 +00002524 NoexceptExpr
2525 = SemaRef.VerifyIntegerConstantExpression(NoexceptExpr,
2526 0, diag::err_noexcept_needs_constant_expression,
2527 /*AllowFold*/ false).take();
Richard Smithe6975e92012-04-17 00:58:00 +00002528 }
2529 }
2530
2531 // Rebuild the function type
2532 const FunctionProtoType *NewProto
2533 = New->getType()->getAs<FunctionProtoType>();
2534 assert(NewProto && "Template instantiation without function prototype?");
2535
2536 FunctionProtoType::ExtProtoInfo EPI = NewProto->getExtProtoInfo();
2537 EPI.ExceptionSpecType = Proto->getExceptionSpecType();
2538 EPI.NumExceptions = Exceptions.size();
2539 EPI.Exceptions = Exceptions.data();
2540 EPI.NoexceptExpr = NoexceptExpr;
2541
2542 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
2543 NewProto->arg_type_begin(),
2544 NewProto->getNumArgs(),
2545 EPI));
2546}
2547
2548void Sema::InstantiateExceptionSpec(SourceLocation PointOfInstantiation,
2549 FunctionDecl *Decl) {
Richard Smith13bffc52012-04-19 00:08:28 +00002550 const FunctionProtoType *Proto = Decl->getType()->castAs<FunctionProtoType>();
2551 if (Proto->getExceptionSpecType() != EST_Uninstantiated)
Richard Smithe6975e92012-04-17 00:58:00 +00002552 return;
2553
2554 InstantiatingTemplate Inst(*this, PointOfInstantiation, Decl,
2555 InstantiatingTemplate::ExceptionSpecification());
Richard Smithb9d0b762012-07-27 04:22:15 +00002556 if (Inst) {
2557 // We hit the instantiation depth limit. Clear the exception specification
2558 // so that our callers don't have to cope with EST_Uninstantiated.
2559 FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
2560 EPI.ExceptionSpecType = EST_None;
2561 Decl->setType(Context.getFunctionType(Proto->getResultType(),
2562 Proto->arg_type_begin(),
2563 Proto->getNumArgs(),
2564 EPI));
Richard Smithe6975e92012-04-17 00:58:00 +00002565 return;
Richard Smithb9d0b762012-07-27 04:22:15 +00002566 }
Richard Smithe6975e92012-04-17 00:58:00 +00002567
2568 // Enter the scope of this instantiation. We don't use
2569 // PushDeclContext because we don't have a scope.
2570 Sema::ContextRAII savedContext(*this, Decl);
2571 LocalInstantiationScope Scope(*this);
2572
2573 MultiLevelTemplateArgumentList TemplateArgs =
2574 getTemplateInstantiationArgs(Decl, 0, /*RelativeToPrimary*/true);
2575
Richard Smith13bffc52012-04-19 00:08:28 +00002576 FunctionDecl *Template = Proto->getExceptionSpecTemplate();
2577 addInstantiatedParametersToScope(*this, Decl, Template, Scope, TemplateArgs);
Richard Smithe6975e92012-04-17 00:58:00 +00002578
Richard Smith13bffc52012-04-19 00:08:28 +00002579 ::InstantiateExceptionSpec(*this, Decl,
2580 Template->getType()->castAs<FunctionProtoType>(),
2581 TemplateArgs);
Richard Smithe6975e92012-04-17 00:58:00 +00002582}
2583
Mike Stump1eb44332009-09-09 15:08:12 +00002584/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00002585/// declaration (New) from the corresponding fields of its template (Tmpl).
2586///
2587/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00002588bool
2589TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00002590 FunctionDecl *Tmpl) {
David Blaikie85f485a2012-07-16 18:50:45 +00002591 if (Tmpl->isDeleted())
Sean Hunt10620eb2011-05-06 20:44:56 +00002592 New->setDeletedAsWritten();
Mike Stump1eb44332009-09-09 15:08:12 +00002593
Douglas Gregorcca9e962009-07-01 22:01:06 +00002594 // If we are performing substituting explicitly-specified template arguments
2595 // or deduced template arguments into a function template and we reach this
2596 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00002597 // to keeping the new function template specialization. We therefore
2598 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00002599 // into a template instantiation for this specific function template
2600 // specialization, which is not a SFINAE context, so that we diagnose any
2601 // further errors in the declaration itself.
2602 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
2603 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
2604 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
2605 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00002606 if (FunctionTemplateDecl *FunTmpl
Nick Lewycky4a9e60f2012-11-16 08:40:59 +00002607 = dyn_cast<FunctionTemplateDecl>(ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002608 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00002609 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00002610 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00002611 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
Nick Lewycky4a9e60f2012-11-16 08:40:59 +00002612 ActiveInst.Entity = New;
Douglas Gregorcca9e962009-07-01 22:01:06 +00002613 }
2614 }
Mike Stump1eb44332009-09-09 15:08:12 +00002615
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002616 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
2617 assert(Proto && "Function template without prototype?");
2618
Sebastian Redl60618fa2011-03-12 11:50:43 +00002619 if (Proto->hasExceptionSpec() || Proto->getNoReturnAttr()) {
John McCalle23cf432010-12-14 08:05:40 +00002620 FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
John McCalle23cf432010-12-14 08:05:40 +00002621
Richard Smithe6975e92012-04-17 00:58:00 +00002622 // DR1330: In C++11, defer instantiation of a non-trivial
2623 // exception specification.
Richard Smith80ad52f2013-01-02 11:42:31 +00002624 if (SemaRef.getLangOpts().CPlusPlus11 &&
Richard Smithe6975e92012-04-17 00:58:00 +00002625 EPI.ExceptionSpecType != EST_None &&
2626 EPI.ExceptionSpecType != EST_DynamicNone &&
2627 EPI.ExceptionSpecType != EST_BasicNoexcept) {
Richard Smith13bffc52012-04-19 00:08:28 +00002628 FunctionDecl *ExceptionSpecTemplate = Tmpl;
2629 if (EPI.ExceptionSpecType == EST_Uninstantiated)
2630 ExceptionSpecTemplate = EPI.ExceptionSpecTemplate;
Richard Smithb9d0b762012-07-27 04:22:15 +00002631 assert(EPI.ExceptionSpecType != EST_Unevaluated &&
2632 "instantiating implicitly-declared special member");
Richard Smith13bffc52012-04-19 00:08:28 +00002633
Richard Smithe6975e92012-04-17 00:58:00 +00002634 // Mark the function has having an uninstantiated exception specification.
2635 const FunctionProtoType *NewProto
2636 = New->getType()->getAs<FunctionProtoType>();
2637 assert(NewProto && "Template instantiation without function prototype?");
2638 EPI = NewProto->getExtProtoInfo();
2639 EPI.ExceptionSpecType = EST_Uninstantiated;
2640 EPI.ExceptionSpecDecl = New;
Richard Smith13bffc52012-04-19 00:08:28 +00002641 EPI.ExceptionSpecTemplate = ExceptionSpecTemplate;
Richard Smithe6975e92012-04-17 00:58:00 +00002642 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
2643 NewProto->arg_type_begin(),
2644 NewProto->getNumArgs(),
2645 EPI));
2646 } else {
2647 ::InstantiateExceptionSpec(SemaRef, New, Proto, TemplateArgs);
2648 }
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002649 }
2650
Rafael Espindola19f74ac2011-07-06 15:46:09 +00002651 // Get the definition. Leaves the variable unchanged if undefined.
Richard Smithe6975e92012-04-17 00:58:00 +00002652 const FunctionDecl *Definition = Tmpl;
Rafael Espindola19f74ac2011-07-06 15:46:09 +00002653 Tmpl->isDefined(Definition);
2654
DeLesley Hutchins23323e02012-01-20 22:50:54 +00002655 SemaRef.InstantiateAttrs(TemplateArgs, Definition, New,
2656 LateAttrs, StartingScope);
Douglas Gregor7cf84d62010-06-15 17:05:35 +00002657
Douglas Gregore53060f2009-06-25 22:08:12 +00002658 return false;
2659}
2660
Douglas Gregor5545e162009-03-24 00:38:23 +00002661/// \brief Initializes common fields of an instantiated method
2662/// declaration (New) from the corresponding fields of its template
2663/// (Tmpl).
2664///
2665/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00002666bool
2667TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00002668 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00002669 if (InitFunctionInstantiation(New, Tmpl))
2670 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002671
Douglas Gregor5545e162009-03-24 00:38:23 +00002672 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00002673 if (Tmpl->isVirtualAsWritten())
Douglas Gregor85606eb2010-09-28 20:50:54 +00002674 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +00002675
2676 // FIXME: attributes
2677 // FIXME: New needs a pointer to Tmpl
2678 return false;
2679}
Douglas Gregora58861f2009-05-13 20:28:22 +00002680
2681/// \brief Instantiate the definition of the given function from its
2682/// template.
2683///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002684/// \param PointOfInstantiation the point at which the instantiation was
2685/// required. Note that this is not precisely a "point of instantiation"
2686/// for the function, but it's close.
2687///
Douglas Gregora58861f2009-05-13 20:28:22 +00002688/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002689/// function template specialization or member function of a class template
2690/// specialization.
2691///
2692/// \param Recursive if true, recursively instantiates any functions that
2693/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002694///
2695/// \param DefinitionRequired if true, then we are performing an explicit
2696/// instantiation where the body of the function is required. Complain if
2697/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002698void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002699 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002700 bool Recursive,
2701 bool DefinitionRequired) {
Sean Hunt10620eb2011-05-06 20:44:56 +00002702 if (Function->isInvalidDecl() || Function->isDefined())
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002703 return;
2704
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002705 // Never instantiate an explicit specialization except if it is a class scope
2706 // explicit specialization.
2707 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization &&
2708 !Function->getClassScopeSpecializationPattern())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002709 return;
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002710
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002711 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00002712 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Sean Huntf996e052011-05-27 20:00:14 +00002713 assert(PatternDecl && "instantiating a non-template");
2714
2715 Stmt *Pattern = PatternDecl->getBody(PatternDecl);
2716 assert(PatternDecl && "template definition is not a template");
2717 if (!Pattern) {
2718 // Try to find a defaulted definition
2719 PatternDecl->isDefined(PatternDecl);
Sean Huntdfab8542011-05-25 22:02:25 +00002720 }
Sean Huntf996e052011-05-27 20:00:14 +00002721 assert(PatternDecl && "template definition is not a template");
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002722
Francois Pichet8387e2a2011-04-22 22:18:13 +00002723 // Postpone late parsed template instantiations.
Sean Huntf996e052011-05-27 20:00:14 +00002724 if (PatternDecl->isLateTemplateParsed() &&
Nick Lewycky8a29bc02011-05-12 03:51:24 +00002725 !LateTemplateParser) {
Francois Pichet8387e2a2011-04-22 22:18:13 +00002726 PendingInstantiations.push_back(
2727 std::make_pair(Function, PointOfInstantiation));
2728 return;
2729 }
2730
2731 // Call the LateTemplateParser callback if there a need to late parse
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002732 // a templated function definition.
Sean Huntf996e052011-05-27 20:00:14 +00002733 if (!Pattern && PatternDecl->isLateTemplateParsed() &&
Francois Pichet8387e2a2011-04-22 22:18:13 +00002734 LateTemplateParser) {
Francois Pichet4a47e8d2011-04-23 11:52:20 +00002735 LateTemplateParser(OpaqueParser, PatternDecl);
Francois Pichet8387e2a2011-04-22 22:18:13 +00002736 Pattern = PatternDecl->getBody(PatternDecl);
2737 }
2738
Sean Huntf996e052011-05-27 20:00:14 +00002739 if (!Pattern && !PatternDecl->isDefaulted()) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002740 if (DefinitionRequired) {
2741 if (Function->getPrimaryTemplate())
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002742 Diag(PointOfInstantiation,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002743 diag::err_explicit_instantiation_undefined_func_template)
2744 << Function->getPrimaryTemplate();
2745 else
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002746 Diag(PointOfInstantiation,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002747 diag::err_explicit_instantiation_undefined_member)
2748 << 1 << Function->getDeclName() << Function->getDeclContext();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002749
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002750 if (PatternDecl)
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002751 Diag(PatternDecl->getLocation(),
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002752 diag::note_explicit_instantiation_here);
Douglas Gregorcfe833b2010-05-17 17:57:54 +00002753 Function->setInvalidDecl();
Chandler Carruth58e390e2010-08-25 08:27:02 +00002754 } else if (Function->getTemplateSpecializationKind()
2755 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002756 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00002757 std::make_pair(Function, PointOfInstantiation));
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002758 }
Chandler Carruth58e390e2010-08-25 08:27:02 +00002759
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002760 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002761 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002762
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002763 // C++0x [temp.explicit]p9:
2764 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00002765 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002766 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00002767 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002768 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002769 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002770 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002771
Richard Smithd4497dd2013-01-25 00:08:28 +00002772 if (PatternDecl->isInlined())
2773 Function->setImplicitlyInline();
2774
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002775 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2776 if (Inst)
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002777 return;
2778
Abramo Bagnarae9946242011-11-18 08:08:52 +00002779 // Copy the inner loc start from the pattern.
2780 Function->setInnerLocStart(PatternDecl->getInnerLocStart());
2781
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002782 // If we're performing recursive template instantiation, create our own
2783 // queue of pending implicit instantiations that we will instantiate later,
2784 // while we're still within our own instantiation context.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002785 SmallVector<VTableUse, 16> SavedVTableUses;
Chandler Carruth62c78d52010-08-25 08:44:16 +00002786 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002787 if (Recursive) {
2788 VTableUses.swap(SavedVTableUses);
Chandler Carruth62c78d52010-08-25 08:44:16 +00002789 PendingInstantiations.swap(SavedPendingInstantiations);
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002790 }
Mike Stump1eb44332009-09-09 15:08:12 +00002791
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002792 EnterExpressionEvaluationContext EvalContext(*this,
John McCallf312b1e2010-08-26 23:41:50 +00002793 Sema::PotentiallyEvaluated);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002794
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002795 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00002796 // recorded, unless we're actually a member function within a local
2797 // class, in which case we need to merge our results with the parent
2798 // scope (of the enclosing function).
2799 bool MergeWithParentScope = false;
2800 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
2801 MergeWithParentScope = Rec->isLocalClass();
2802
2803 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002804
Richard Smith1d28caf2012-12-11 01:14:52 +00002805 if (PatternDecl->isDefaulted())
Sean Huntcd10dec2011-05-23 23:14:04 +00002806 SetDeclDefaulted(Function, PatternDecl->getLocation());
Richard Smith1d28caf2012-12-11 01:14:52 +00002807 else {
2808 ActOnStartOfFunctionDef(0, Function);
2809
2810 // Enter the scope of this instantiation. We don't use
2811 // PushDeclContext because we don't have a scope.
2812 Sema::ContextRAII savedContext(*this, Function);
2813
2814 MultiLevelTemplateArgumentList TemplateArgs =
2815 getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
2816
2817 addInstantiatedParametersToScope(*this, Function, PatternDecl, Scope,
2818 TemplateArgs);
2819
Sean Huntcd10dec2011-05-23 23:14:04 +00002820 // If this is a constructor, instantiate the member initializers.
2821 if (const CXXConstructorDecl *Ctor =
2822 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2823 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2824 TemplateArgs);
2825 }
2826
2827 // Instantiate the function body.
2828 StmtResult Body = SubstStmt(Pattern, TemplateArgs);
2829
2830 if (Body.isInvalid())
2831 Function->setInvalidDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002832
Sean Huntcd10dec2011-05-23 23:14:04 +00002833 ActOnFinishFunctionBody(Function, Body.get(),
2834 /*IsInstantiation=*/true);
Richard Smith1d28caf2012-12-11 01:14:52 +00002835
2836 PerformDependentDiagnostics(PatternDecl, TemplateArgs);
2837
2838 savedContext.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00002839 }
2840
Douglas Gregoraba43bb2009-05-26 20:50:29 +00002841 DeclGroupRef DG(Function);
2842 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00002843
Douglas Gregor60406be2010-01-16 22:29:39 +00002844 // This class may have local implicit instantiations that need to be
2845 // instantiation within this scope.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002846 PerformPendingInstantiations(/*LocalOnly=*/true);
Douglas Gregor60406be2010-01-16 22:29:39 +00002847 Scope.Exit();
2848
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002849 if (Recursive) {
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002850 // Define any pending vtables.
2851 DefineUsedVTables();
2852
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002853 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002854 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002855 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002856
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002857 // Restore the set of pending vtables.
Nick Lewycky81559102011-05-31 07:58:42 +00002858 assert(VTableUses.empty() &&
2859 "VTableUses should be empty before it is discarded.");
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002860 VTableUses.swap(SavedVTableUses);
2861
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002862 // Restore the set of pending implicit instantiations.
Nick Lewycky81559102011-05-31 07:58:42 +00002863 assert(PendingInstantiations.empty() &&
2864 "PendingInstantiations should be empty before it is discarded.");
Chandler Carruth62c78d52010-08-25 08:44:16 +00002865 PendingInstantiations.swap(SavedPendingInstantiations);
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002866 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002867}
2868
2869/// \brief Instantiate the definition of the given variable from its
2870/// template.
2871///
Douglas Gregor7caa6822009-07-24 20:34:43 +00002872/// \param PointOfInstantiation the point at which the instantiation was
2873/// required. Note that this is not precisely a "point of instantiation"
2874/// for the function, but it's close.
2875///
2876/// \param Var the already-instantiated declaration of a static member
2877/// variable of a class template specialization.
2878///
2879/// \param Recursive if true, recursively instantiates any functions that
2880/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002881///
2882/// \param DefinitionRequired if true, then we are performing an explicit
2883/// instantiation where an out-of-line definition of the member variable
2884/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002885void Sema::InstantiateStaticDataMemberDefinition(
2886 SourceLocation PointOfInstantiation,
2887 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002888 bool Recursive,
2889 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002890 if (Var->isInvalidDecl())
2891 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002892
Douglas Gregor7caa6822009-07-24 20:34:43 +00002893 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002894 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002895 assert(Def && "This data member was not instantiated from a template?");
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002896 assert(Def->isStaticDataMember() && "Not a static data member?");
Douglas Gregor0d035142009-10-27 18:42:08 +00002897 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002898
Douglas Gregor0d035142009-10-27 18:42:08 +00002899 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002900 // We did not find an out-of-line definition of this static data member,
2901 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00002902 // instantiate this definition (or provide a specialization for it) in
2903 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002904 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00002905 Def = Var->getInstantiatedFromStaticDataMember();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002906 Diag(PointOfInstantiation,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002907 diag::err_explicit_instantiation_undefined_member)
2908 << 2 << Var->getDeclName() << Var->getDeclContext();
2909 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
Chandler Carruth58e390e2010-08-25 08:27:02 +00002910 } else if (Var->getTemplateSpecializationKind()
2911 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002912 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00002913 std::make_pair(Var, PointOfInstantiation));
2914 }
2915
Douglas Gregor7caa6822009-07-24 20:34:43 +00002916 return;
2917 }
2918
Rafael Espindola234fe652012-03-05 10:54:55 +00002919 TemplateSpecializationKind TSK = Var->getTemplateSpecializationKind();
2920
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002921 // Never instantiate an explicit specialization.
Rafael Espindola234fe652012-03-05 10:54:55 +00002922 if (TSK == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002923 return;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002924
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002925 // C++0x [temp.explicit]p9:
2926 // Except for inline functions, other explicit instantiation declarations
2927 // have the effect of suppressing the implicit instantiation of the entity
2928 // to which they refer.
Rafael Espindola234fe652012-03-05 10:54:55 +00002929 if (TSK == TSK_ExplicitInstantiationDeclaration)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002930 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002931
Rafael Espindola02503932012-03-08 15:51:03 +00002932 Consumer.HandleCXXStaticMemberVarInstantiation(Var);
2933
Douglas Gregorf15748a2011-06-03 03:35:07 +00002934 // If we already have a definition, we're done.
Nick Lewycky95e38722012-04-04 02:38:36 +00002935 if (VarDecl *Def = Var->getDefinition()) {
2936 // We may be explicitly instantiating something we've already implicitly
2937 // instantiated.
2938 Def->setTemplateSpecializationKind(Var->getTemplateSpecializationKind(),
2939 PointOfInstantiation);
Douglas Gregorf15748a2011-06-03 03:35:07 +00002940 return;
Nick Lewycky95e38722012-04-04 02:38:36 +00002941 }
Douglas Gregorf15748a2011-06-03 03:35:07 +00002942
Douglas Gregor7caa6822009-07-24 20:34:43 +00002943 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
2944 if (Inst)
2945 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002946
Douglas Gregor7caa6822009-07-24 20:34:43 +00002947 // If we're performing recursive template instantiation, create our own
2948 // queue of pending implicit instantiations that we will instantiate later,
2949 // while we're still within our own instantiation context.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002950 SmallVector<VTableUse, 16> SavedVTableUses;
Chandler Carruth62c78d52010-08-25 08:44:16 +00002951 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Nick Lewycky81559102011-05-31 07:58:42 +00002952 if (Recursive) {
2953 VTableUses.swap(SavedVTableUses);
Chandler Carruth62c78d52010-08-25 08:44:16 +00002954 PendingInstantiations.swap(SavedPendingInstantiations);
Nick Lewycky81559102011-05-31 07:58:42 +00002955 }
Mike Stump1eb44332009-09-09 15:08:12 +00002956
Douglas Gregor7caa6822009-07-24 20:34:43 +00002957 // Enter the scope of this instantiation. We don't use
2958 // PushDeclContext because we don't have a scope.
John McCallf5ba7e02011-02-14 20:37:25 +00002959 ContextRAII previousContext(*this, Var->getDeclContext());
Douglas Gregor7bdc1522012-02-16 21:36:18 +00002960 LocalInstantiationScope Local(*this);
2961
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002962 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00002963 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Nico Weber6bb4dcb2010-11-28 22:53:37 +00002964 getTemplateInstantiationArgs(Var)));
John McCallf5ba7e02011-02-14 20:37:25 +00002965
2966 previousContext.pop();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002967
2968 if (Var) {
Douglas Gregor583f33b2009-10-15 18:07:02 +00002969 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2970 assert(MSInfo && "Missing member specialization information?");
2971 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2972 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00002973 DeclGroupRef DG(Var);
2974 Consumer.HandleTopLevelDecl(DG);
2975 }
Douglas Gregor7bdc1522012-02-16 21:36:18 +00002976 Local.Exit();
2977
Douglas Gregor7caa6822009-07-24 20:34:43 +00002978 if (Recursive) {
Nick Lewycky81559102011-05-31 07:58:42 +00002979 // Define any newly required vtables.
2980 DefineUsedVTables();
2981
Douglas Gregor7caa6822009-07-24 20:34:43 +00002982 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002983 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002984 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002985
Nick Lewycky81559102011-05-31 07:58:42 +00002986 // Restore the set of pending vtables.
2987 assert(VTableUses.empty() &&
2988 "VTableUses should be empty before it is discarded, "
2989 "while instantiating static data member.");
2990 VTableUses.swap(SavedVTableUses);
2991
Douglas Gregor7caa6822009-07-24 20:34:43 +00002992 // Restore the set of pending implicit instantiations.
Nick Lewycky81559102011-05-31 07:58:42 +00002993 assert(PendingInstantiations.empty() &&
2994 "PendingInstantiations should be empty before it is discarded, "
2995 "while instantiating static data member.");
Chandler Carruth62c78d52010-08-25 08:44:16 +00002996 PendingInstantiations.swap(SavedPendingInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002997 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002998}
Douglas Gregor815215d2009-05-27 05:35:12 +00002999
Anders Carlsson09025312009-08-29 05:16:22 +00003000void
3001Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
3002 const CXXConstructorDecl *Tmpl,
3003 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00003004
Richard Trieu90ab75b2011-09-09 03:18:59 +00003005 SmallVector<CXXCtorInitializer*, 4> NewInits;
Richard Smith54b3ba82012-09-25 00:23:05 +00003006 bool AnyErrors = Tmpl->isInvalidDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003007
Anders Carlsson09025312009-08-29 05:16:22 +00003008 // Instantiate all the initializers.
3009 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00003010 InitsEnd = Tmpl->init_end();
3011 Inits != InitsEnd; ++Inits) {
Sean Huntcbb67482011-01-08 20:30:50 +00003012 CXXCtorInitializer *Init = *Inits;
Anders Carlsson09025312009-08-29 05:16:22 +00003013
Chandler Carruth030ef472010-09-03 21:54:20 +00003014 // Only instantiate written initializers, let Sema re-construct implicit
3015 // ones.
3016 if (!Init->isWritten())
3017 continue;
3018
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003019 SourceLocation EllipsisLoc;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003020
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003021 if (Init->isPackExpansion()) {
3022 // This is a pack expansion. We should expand it now.
Douglas Gregor76852c22011-11-01 01:16:03 +00003023 TypeLoc BaseTL = Init->getTypeSourceInfo()->getTypeLoc();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003024 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003025 collectUnexpandedParameterPacks(BaseTL, Unexpanded);
3026 bool ShouldExpand = false;
Douglas Gregord3731192011-01-10 07:32:04 +00003027 bool RetainExpansion = false;
Douglas Gregorcded4f62011-01-14 17:04:44 +00003028 llvm::Optional<unsigned> NumExpansions;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003029 if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(),
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003030 BaseTL.getSourceRange(),
David Blaikiea71f9d02011-09-22 02:34:54 +00003031 Unexpanded,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003032 TemplateArgs, ShouldExpand,
Douglas Gregord3731192011-01-10 07:32:04 +00003033 RetainExpansion,
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003034 NumExpansions)) {
3035 AnyErrors = true;
3036 New->setInvalidDecl();
3037 continue;
3038 }
3039 assert(ShouldExpand && "Partial instantiation of base initializer?");
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003040
3041 // Loop over all of the arguments in the argument pack(s),
Douglas Gregorcded4f62011-01-14 17:04:44 +00003042 for (unsigned I = 0; I != *NumExpansions; ++I) {
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003043 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
3044
3045 // Instantiate the initializer.
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003046 ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs,
3047 /*CXXDirectInit=*/true);
3048 if (TempInit.isInvalid()) {
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003049 AnyErrors = true;
3050 break;
3051 }
3052
3053 // Instantiate the base type.
Douglas Gregor76852c22011-11-01 01:16:03 +00003054 TypeSourceInfo *BaseTInfo = SubstType(Init->getTypeSourceInfo(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003055 TemplateArgs,
3056 Init->getSourceLocation(),
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003057 New->getDeclName());
3058 if (!BaseTInfo) {
3059 AnyErrors = true;
3060 break;
3061 }
3062
3063 // Build the initializer.
Sebastian Redl6df65482011-09-24 17:48:25 +00003064 MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(),
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003065 BaseTInfo, TempInit.take(),
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003066 New->getParent(),
3067 SourceLocation());
3068 if (NewInit.isInvalid()) {
3069 AnyErrors = true;
3070 break;
3071 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003072
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003073 NewInits.push_back(NewInit.get());
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003074 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003075
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003076 continue;
3077 }
3078
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00003079 // Instantiate the initializer.
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003080 ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs,
3081 /*CXXDirectInit=*/true);
3082 if (TempInit.isInvalid()) {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00003083 AnyErrors = true;
3084 continue;
Anders Carlsson09025312009-08-29 05:16:22 +00003085 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003086
Anders Carlsson09025312009-08-29 05:16:22 +00003087 MemInitResult NewInit;
Douglas Gregor76852c22011-11-01 01:16:03 +00003088 if (Init->isDelegatingInitializer() || Init->isBaseInitializer()) {
3089 TypeSourceInfo *TInfo = SubstType(Init->getTypeSourceInfo(),
3090 TemplateArgs,
3091 Init->getSourceLocation(),
3092 New->getDeclName());
3093 if (!TInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00003094 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00003095 New->setInvalidDecl();
3096 continue;
3097 }
Sebastian Redl6df65482011-09-24 17:48:25 +00003098
Douglas Gregor76852c22011-11-01 01:16:03 +00003099 if (Init->isBaseInitializer())
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003100 NewInit = BuildBaseInitializer(TInfo->getType(), TInfo, TempInit.take(),
Douglas Gregor76852c22011-11-01 01:16:03 +00003101 New->getParent(), EllipsisLoc);
3102 else
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003103 NewInit = BuildDelegatingInitializer(TInfo, TempInit.take(),
Douglas Gregor76852c22011-11-01 01:16:03 +00003104 cast<CXXRecordDecl>(CurContext->getParent()));
Anders Carlsson09025312009-08-29 05:16:22 +00003105 } else if (Init->isMemberInitializer()) {
Douglas Gregorb7107222011-03-04 19:46:35 +00003106 FieldDecl *Member = cast_or_null<FieldDecl>(FindInstantiatedDecl(
Francois Pichet00eb3f92010-12-04 09:14:42 +00003107 Init->getMemberLocation(),
3108 Init->getMember(),
3109 TemplateArgs));
Douglas Gregorb7107222011-03-04 19:46:35 +00003110 if (!Member) {
3111 AnyErrors = true;
3112 New->setInvalidDecl();
3113 continue;
3114 }
Mike Stump1eb44332009-09-09 15:08:12 +00003115
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003116 NewInit = BuildMemberInitializer(Member, TempInit.take(),
Sebastian Redl6df65482011-09-24 17:48:25 +00003117 Init->getSourceLocation());
Francois Pichet00eb3f92010-12-04 09:14:42 +00003118 } else if (Init->isIndirectMemberInitializer()) {
3119 IndirectFieldDecl *IndirectMember =
Douglas Gregorb7107222011-03-04 19:46:35 +00003120 cast_or_null<IndirectFieldDecl>(FindInstantiatedDecl(
Francois Pichet00eb3f92010-12-04 09:14:42 +00003121 Init->getMemberLocation(),
3122 Init->getIndirectMember(), TemplateArgs));
3123
Douglas Gregorb7107222011-03-04 19:46:35 +00003124 if (!IndirectMember) {
3125 AnyErrors = true;
3126 New->setInvalidDecl();
Sebastian Redl6df65482011-09-24 17:48:25 +00003127 continue;
Douglas Gregorb7107222011-03-04 19:46:35 +00003128 }
Sebastian Redl6df65482011-09-24 17:48:25 +00003129
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003130 NewInit = BuildMemberInitializer(IndirectMember, TempInit.take(),
Sebastian Redl6df65482011-09-24 17:48:25 +00003131 Init->getSourceLocation());
Anders Carlsson09025312009-08-29 05:16:22 +00003132 }
3133
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00003134 if (NewInit.isInvalid()) {
3135 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00003136 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00003137 } else {
Richard Trieu90ab75b2011-09-09 03:18:59 +00003138 NewInits.push_back(NewInit.get());
Anders Carlsson09025312009-08-29 05:16:22 +00003139 }
3140 }
Mike Stump1eb44332009-09-09 15:08:12 +00003141
Anders Carlsson09025312009-08-29 05:16:22 +00003142 // Assign all the initializers to the new constructor.
John McCalld226f652010-08-21 09:40:31 +00003143 ActOnMemInitializers(New,
Anders Carlsson09025312009-08-29 05:16:22 +00003144 /*FIXME: ColonLoc */
3145 SourceLocation(),
David Blaikie93c86172013-01-17 05:26:25 +00003146 NewInits,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00003147 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00003148}
3149
John McCall52a575a2009-08-29 08:11:13 +00003150// TODO: this could be templated if the various decl types used the
3151// same method name.
3152static bool isInstantiationOf(ClassTemplateDecl *Pattern,
3153 ClassTemplateDecl *Instance) {
3154 Pattern = Pattern->getCanonicalDecl();
3155
3156 do {
3157 Instance = Instance->getCanonicalDecl();
3158 if (Pattern == Instance) return true;
3159 Instance = Instance->getInstantiatedFromMemberTemplate();
3160 } while (Instance);
3161
3162 return false;
3163}
3164
Douglas Gregor0d696532009-09-28 06:34:35 +00003165static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
3166 FunctionTemplateDecl *Instance) {
3167 Pattern = Pattern->getCanonicalDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003168
Douglas Gregor0d696532009-09-28 06:34:35 +00003169 do {
3170 Instance = Instance->getCanonicalDecl();
3171 if (Pattern == Instance) return true;
3172 Instance = Instance->getInstantiatedFromMemberTemplate();
3173 } while (Instance);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003174
Douglas Gregor0d696532009-09-28 06:34:35 +00003175 return false;
3176}
3177
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003178static bool
Douglas Gregored9c0f92009-10-29 00:04:11 +00003179isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
3180 ClassTemplatePartialSpecializationDecl *Instance) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003181 Pattern
Douglas Gregored9c0f92009-10-29 00:04:11 +00003182 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
3183 do {
3184 Instance = cast<ClassTemplatePartialSpecializationDecl>(
3185 Instance->getCanonicalDecl());
3186 if (Pattern == Instance)
3187 return true;
3188 Instance = Instance->getInstantiatedFromMember();
3189 } while (Instance);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003190
Douglas Gregored9c0f92009-10-29 00:04:11 +00003191 return false;
3192}
3193
John McCall52a575a2009-08-29 08:11:13 +00003194static bool isInstantiationOf(CXXRecordDecl *Pattern,
3195 CXXRecordDecl *Instance) {
3196 Pattern = Pattern->getCanonicalDecl();
3197
3198 do {
3199 Instance = Instance->getCanonicalDecl();
3200 if (Pattern == Instance) return true;
3201 Instance = Instance->getInstantiatedFromMemberClass();
3202 } while (Instance);
3203
3204 return false;
3205}
3206
3207static bool isInstantiationOf(FunctionDecl *Pattern,
3208 FunctionDecl *Instance) {
3209 Pattern = Pattern->getCanonicalDecl();
3210
3211 do {
3212 Instance = Instance->getCanonicalDecl();
3213 if (Pattern == Instance) return true;
3214 Instance = Instance->getInstantiatedFromMemberFunction();
3215 } while (Instance);
3216
3217 return false;
3218}
3219
3220static bool isInstantiationOf(EnumDecl *Pattern,
3221 EnumDecl *Instance) {
3222 Pattern = Pattern->getCanonicalDecl();
3223
3224 do {
3225 Instance = Instance->getCanonicalDecl();
3226 if (Pattern == Instance) return true;
3227 Instance = Instance->getInstantiatedFromMemberEnum();
3228 } while (Instance);
3229
3230 return false;
3231}
3232
John McCalled976492009-12-04 22:46:56 +00003233static bool isInstantiationOf(UsingShadowDecl *Pattern,
3234 UsingShadowDecl *Instance,
3235 ASTContext &C) {
3236 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
3237}
3238
3239static bool isInstantiationOf(UsingDecl *Pattern,
3240 UsingDecl *Instance,
3241 ASTContext &C) {
3242 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
3243}
3244
John McCall7ba107a2009-11-18 02:36:19 +00003245static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
3246 UsingDecl *Instance,
3247 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00003248 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00003249}
3250
3251static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00003252 UsingDecl *Instance,
3253 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00003254 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00003255}
3256
John McCall52a575a2009-08-29 08:11:13 +00003257static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
3258 VarDecl *Instance) {
3259 assert(Instance->isStaticDataMember());
3260
3261 Pattern = Pattern->getCanonicalDecl();
3262
3263 do {
3264 Instance = Instance->getCanonicalDecl();
3265 if (Pattern == Instance) return true;
3266 Instance = Instance->getInstantiatedFromStaticDataMember();
3267 } while (Instance);
3268
3269 return false;
3270}
3271
John McCalled976492009-12-04 22:46:56 +00003272// Other is the prospective instantiation
3273// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00003274static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00003275 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00003276 if (UnresolvedUsingTypenameDecl *UUD
3277 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
3278 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
3279 return isInstantiationOf(UUD, UD, Ctx);
3280 }
3281 }
3282
3283 if (UnresolvedUsingValueDecl *UUD
3284 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00003285 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
3286 return isInstantiationOf(UUD, UD, Ctx);
3287 }
3288 }
Douglas Gregor815215d2009-05-27 05:35:12 +00003289
Anders Carlsson0d8df782009-08-29 19:37:28 +00003290 return false;
3291 }
Mike Stump1eb44332009-09-09 15:08:12 +00003292
John McCall52a575a2009-08-29 08:11:13 +00003293 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
3294 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00003295
John McCall52a575a2009-08-29 08:11:13 +00003296 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
3297 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00003298
John McCall52a575a2009-08-29 08:11:13 +00003299 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
3300 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00003301
Douglas Gregor7caa6822009-07-24 20:34:43 +00003302 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00003303 if (Var->isStaticDataMember())
3304 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
3305
3306 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
3307 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00003308
Douglas Gregor0d696532009-09-28 06:34:35 +00003309 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
3310 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
3311
Douglas Gregored9c0f92009-10-29 00:04:11 +00003312 if (ClassTemplatePartialSpecializationDecl *PartialSpec
3313 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
3314 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
3315 PartialSpec);
3316
Anders Carlssond8b285f2009-09-01 04:26:58 +00003317 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
3318 if (!Field->getDeclName()) {
3319 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00003320 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00003321 cast<FieldDecl>(D);
3322 }
3323 }
Mike Stump1eb44332009-09-09 15:08:12 +00003324
John McCalled976492009-12-04 22:46:56 +00003325 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
3326 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
3327
3328 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
3329 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
3330
Douglas Gregor815215d2009-05-27 05:35:12 +00003331 return D->getDeclName() && isa<NamedDecl>(Other) &&
3332 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
3333}
3334
3335template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00003336static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00003337 NamedDecl *D,
3338 ForwardIterator first,
3339 ForwardIterator last) {
3340 for (; first != last; ++first)
3341 if (isInstantiationOf(Ctx, D, *first))
3342 return cast<NamedDecl>(*first);
3343
3344 return 0;
3345}
3346
John McCall02cace72009-08-28 07:59:38 +00003347/// \brief Finds the instantiation of the given declaration context
3348/// within the current instantiation.
3349///
3350/// \returns NULL if there was an error
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003351DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
Douglas Gregore95b4092009-09-16 18:34:49 +00003352 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00003353 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003354 Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00003355 return cast_or_null<DeclContext>(ID);
3356 } else return DC;
3357}
3358
Douglas Gregored961e72009-05-27 17:54:46 +00003359/// \brief Find the instantiation of the given declaration within the
3360/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00003361///
3362/// This routine is intended to be used when \p D is a declaration
3363/// referenced from within a template, that needs to mapped into the
3364/// corresponding declaration within an instantiation. For example,
3365/// given:
3366///
3367/// \code
3368/// template<typename T>
3369/// struct X {
3370/// enum Kind {
3371/// KnownValue = sizeof(T)
3372/// };
3373///
3374/// bool getKind() const { return KnownValue; }
3375/// };
3376///
3377/// template struct X<int>;
3378/// \endcode
3379///
3380/// In the instantiation of X<int>::getKind(), we need to map the
3381/// EnumConstantDecl for KnownValue (which refers to
James Dennettf198d122012-06-17 03:36:08 +00003382/// X<T>::\<Kind>\::KnownValue) to its instantiation
James Dennettef2b5b32012-06-15 22:23:43 +00003383/// (X<int>::\<Kind>\::KnownValue). InstantiateCurrentDeclRef() performs
Douglas Gregored961e72009-05-27 17:54:46 +00003384/// this mapping from within the instantiation of X<int>.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003385NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
Douglas Gregore95b4092009-09-16 18:34:49 +00003386 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00003387 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00003388 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00003389 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
Douglas Gregor7bdc1522012-02-16 21:36:18 +00003390 (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext()) ||
3391 (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda())) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00003392 // D is a local of some kind. Look into the map of local
3393 // declarations to their instantiations.
Chris Lattnerd8e54992011-02-17 19:47:42 +00003394 typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
3395 llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found
3396 = CurrentInstantiationScope->findInstantiationOf(D);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003397
Chris Lattner57ad3782011-02-17 20:34:02 +00003398 if (Found) {
3399 if (Decl *FD = Found->dyn_cast<Decl *>())
3400 return cast<NamedDecl>(FD);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003401
Richard Smith9a4db032012-09-12 00:56:43 +00003402 int PackIdx = ArgumentPackSubstitutionIndex;
3403 assert(PackIdx != -1 && "found declaration pack but not pack expanding");
Chris Lattner57ad3782011-02-17 20:34:02 +00003404 return cast<NamedDecl>((*Found->get<DeclArgumentPack *>())[PackIdx]);
3405 }
3406
3407 // If we didn't find the decl, then we must have a label decl that hasn't
3408 // been found yet. Lazily instantiate it and return it now.
3409 assert(isa<LabelDecl>(D));
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003410
Chris Lattner57ad3782011-02-17 20:34:02 +00003411 Decl *Inst = SubstDecl(D, CurContext, TemplateArgs);
3412 assert(Inst && "Failed to instantiate label??");
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003413
Chris Lattner57ad3782011-02-17 20:34:02 +00003414 CurrentInstantiationScope->InstantiatedLocal(D, Inst);
3415 return cast<LabelDecl>(Inst);
Douglas Gregor2bba76b2009-05-27 17:07:49 +00003416 }
Douglas Gregor815215d2009-05-27 05:35:12 +00003417
Douglas Gregore95b4092009-09-16 18:34:49 +00003418 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
3419 if (!Record->isDependentContext())
3420 return D;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003421
Douglas Gregor2c1227c2011-11-07 17:43:18 +00003422 // Determine whether this record is the "templated" declaration describing
3423 // a class template or class template partial specialization.
Douglas Gregore95b4092009-09-16 18:34:49 +00003424 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
Douglas Gregor2c1227c2011-11-07 17:43:18 +00003425 if (ClassTemplate)
3426 ClassTemplate = ClassTemplate->getCanonicalDecl();
3427 else if (ClassTemplatePartialSpecializationDecl *PartialSpec
3428 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record))
3429 ClassTemplate = PartialSpec->getSpecializedTemplate()->getCanonicalDecl();
3430
3431 // Walk the current context to find either the record or an instantiation of
3432 // it.
3433 DeclContext *DC = CurContext;
3434 while (!DC->isFileContext()) {
3435 // If we're performing substitution while we're inside the template
3436 // definition, we'll find our own context. We're done.
3437 if (DC->Equals(Record))
3438 return Record;
3439
3440 if (CXXRecordDecl *InstRecord = dyn_cast<CXXRecordDecl>(DC)) {
3441 // Check whether we're in the process of instantiating a class template
3442 // specialization of the template we're mapping.
3443 if (ClassTemplateSpecializationDecl *InstSpec
3444 = dyn_cast<ClassTemplateSpecializationDecl>(InstRecord)){
3445 ClassTemplateDecl *SpecTemplate = InstSpec->getSpecializedTemplate();
3446 if (ClassTemplate && isInstantiationOf(ClassTemplate, SpecTemplate))
3447 return InstRecord;
3448 }
3449
3450 // Check whether we're in the process of instantiating a member class.
3451 if (isInstantiationOf(Record, InstRecord))
3452 return InstRecord;
Douglas Gregore95b4092009-09-16 18:34:49 +00003453 }
Douglas Gregor2c1227c2011-11-07 17:43:18 +00003454
3455
3456 // Move to the outer template scope.
3457 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) {
3458 if (FD->getFriendObjectKind() && FD->getDeclContext()->isFileContext()){
3459 DC = FD->getLexicalDeclContext();
3460 continue;
3461 }
John McCall52a575a2009-08-29 08:11:13 +00003462 }
Douglas Gregor2c1227c2011-11-07 17:43:18 +00003463
3464 DC = DC->getParent();
John McCall52a575a2009-08-29 08:11:13 +00003465 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00003466
Douglas Gregore95b4092009-09-16 18:34:49 +00003467 // Fall through to deal with other dependent record types (e.g.,
3468 // anonymous unions in class templates).
3469 }
John McCall52a575a2009-08-29 08:11:13 +00003470
Douglas Gregore95b4092009-09-16 18:34:49 +00003471 if (!ParentDC->isDependentContext())
3472 return D;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003473
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003474 ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00003475 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00003476 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003477
Douglas Gregor815215d2009-05-27 05:35:12 +00003478 if (ParentDC != D->getDeclContext()) {
3479 // We performed some kind of instantiation in the parent context,
3480 // so now we need to look into the instantiated parent context to
3481 // find the instantiation of the declaration D.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003482
John McCall3cb0ebd2010-03-10 03:28:59 +00003483 // If our context used to be dependent, we may need to instantiate
3484 // it before performing lookup into that context.
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00003485 bool IsBeingInstantiated = false;
John McCall3cb0ebd2010-03-10 03:28:59 +00003486 if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003487 if (!Spec->isDependentContext()) {
3488 QualType T = Context.getTypeDeclType(Spec);
John McCall3cb0ebd2010-03-10 03:28:59 +00003489 const RecordType *Tag = T->getAs<RecordType>();
3490 assert(Tag && "type of non-dependent record is not a RecordType");
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00003491 if (Tag->isBeingDefined())
3492 IsBeingInstantiated = true;
John McCall3cb0ebd2010-03-10 03:28:59 +00003493 if (!Tag->isBeingDefined() &&
3494 RequireCompleteType(Loc, T, diag::err_incomplete_type))
3495 return 0;
Douglas Gregora43064c2010-11-05 23:22:45 +00003496
3497 ParentDC = Tag->getDecl();
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003498 }
3499 }
3500
Douglas Gregor815215d2009-05-27 05:35:12 +00003501 NamedDecl *Result = 0;
3502 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003503 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
David Blaikie3bc93e32012-12-19 00:45:41 +00003504 Result = findInstantiationOf(Context, D, Found.begin(), Found.end());
Douglas Gregor815215d2009-05-27 05:35:12 +00003505 } else {
3506 // Since we don't have a name for the entity we're looking for,
3507 // our only option is to walk through all of the declarations to
3508 // find that name. This will occur in a few cases:
3509 //
3510 // - anonymous struct/union within a template
3511 // - unnamed class/struct/union/enum within a template
3512 //
3513 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00003514 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003515 ParentDC->decls_begin(),
3516 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00003517 }
Mike Stump1eb44332009-09-09 15:08:12 +00003518
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00003519 if (!Result) {
3520 if (isa<UsingShadowDecl>(D)) {
3521 // UsingShadowDecls can instantiate to nothing because of using hiding.
3522 } else if (Diags.hasErrorOccurred()) {
3523 // We've already complained about something, so most likely this
3524 // declaration failed to instantiate. There's no point in complaining
3525 // further, since this is normal in invalid code.
3526 } else if (IsBeingInstantiated) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003527 // The class in which this member exists is currently being
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00003528 // instantiated, and we haven't gotten around to instantiating this
3529 // member yet. This can happen when the code uses forward declarations
3530 // of member classes, and introduces ordering dependencies via
3531 // template instantiation.
3532 Diag(Loc, diag::err_member_not_yet_instantiated)
3533 << D->getDeclName()
3534 << Context.getTypeDeclType(cast<CXXRecordDecl>(ParentDC));
3535 Diag(D->getLocation(), diag::note_non_instantiated_member_here);
Richard Smith0724b7c2012-03-26 20:28:16 +00003536 } else if (EnumConstantDecl *ED = dyn_cast<EnumConstantDecl>(D)) {
3537 // This enumeration constant was found when the template was defined,
3538 // but can't be found in the instantiation. This can happen if an
3539 // unscoped enumeration member is explicitly specialized.
3540 EnumDecl *Enum = cast<EnumDecl>(ED->getLexicalDeclContext());
3541 EnumDecl *Spec = cast<EnumDecl>(FindInstantiatedDecl(Loc, Enum,
3542 TemplateArgs));
3543 assert(Spec->getTemplateSpecializationKind() ==
3544 TSK_ExplicitSpecialization);
3545 Diag(Loc, diag::err_enumerator_does_not_exist)
3546 << D->getDeclName()
3547 << Context.getTypeDeclType(cast<TypeDecl>(Spec->getDeclContext()));
3548 Diag(Spec->getLocation(), diag::note_enum_specialized_here)
3549 << Context.getTypeDeclType(Spec);
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00003550 } else {
3551 // We should have found something, but didn't.
3552 llvm_unreachable("Unable to find instantiation of declaration!");
3553 }
3554 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003555
Douglas Gregor815215d2009-05-27 05:35:12 +00003556 D = Result;
3557 }
3558
Douglas Gregor815215d2009-05-27 05:35:12 +00003559 return D;
3560}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003561
Mike Stump1eb44332009-09-09 15:08:12 +00003562/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003563/// instantiations we have seen until this point.
Nick Lewycky81559102011-05-31 07:58:42 +00003564void Sema::PerformPendingInstantiations(bool LocalOnly) {
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00003565 // Load pending instantiations from the external source.
3566 if (!LocalOnly && ExternalSource) {
Richard Smithb9d0b762012-07-27 04:22:15 +00003567 SmallVector<PendingImplicitInstantiation, 4> Pending;
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00003568 ExternalSource->ReadPendingInstantiations(Pending);
3569 PendingInstantiations.insert(PendingInstantiations.begin(),
3570 Pending.begin(), Pending.end());
3571 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003572
Douglas Gregor60406be2010-01-16 22:29:39 +00003573 while (!PendingLocalImplicitInstantiations.empty() ||
Chandler Carruth62c78d52010-08-25 08:44:16 +00003574 (!LocalOnly && !PendingInstantiations.empty())) {
Douglas Gregor60406be2010-01-16 22:29:39 +00003575 PendingImplicitInstantiation Inst;
3576
3577 if (PendingLocalImplicitInstantiations.empty()) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00003578 Inst = PendingInstantiations.front();
3579 PendingInstantiations.pop_front();
Douglas Gregor60406be2010-01-16 22:29:39 +00003580 } else {
3581 Inst = PendingLocalImplicitInstantiations.front();
3582 PendingLocalImplicitInstantiations.pop_front();
3583 }
Mike Stump1eb44332009-09-09 15:08:12 +00003584
Douglas Gregor7caa6822009-07-24 20:34:43 +00003585 // Instantiate function definitions
3586 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
John McCallf312b1e2010-08-26 23:41:50 +00003587 PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(),
3588 "instantiating function definition");
Chandler Carruth58e390e2010-08-25 08:27:02 +00003589 bool DefinitionRequired = Function->getTemplateSpecializationKind() ==
3590 TSK_ExplicitInstantiationDefinition;
3591 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true,
3592 DefinitionRequired);
Douglas Gregor7caa6822009-07-24 20:34:43 +00003593 continue;
3594 }
Mike Stump1eb44332009-09-09 15:08:12 +00003595
Douglas Gregor7caa6822009-07-24 20:34:43 +00003596 // Instantiate static data member definitions.
3597 VarDecl *Var = cast<VarDecl>(Inst.first);
3598 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00003599
Chandler Carruth291b4412010-02-13 10:17:50 +00003600 // Don't try to instantiate declarations if the most recent redeclaration
3601 // is invalid.
Douglas Gregoref96ee02012-01-14 16:38:05 +00003602 if (Var->getMostRecentDecl()->isInvalidDecl())
Chandler Carruth291b4412010-02-13 10:17:50 +00003603 continue;
3604
3605 // Check if the most recent declaration has changed the specialization kind
3606 // and removed the need for implicit instantiation.
Douglas Gregoref96ee02012-01-14 16:38:05 +00003607 switch (Var->getMostRecentDecl()->getTemplateSpecializationKind()) {
Chandler Carruth291b4412010-02-13 10:17:50 +00003608 case TSK_Undeclared:
David Blaikieb219cfc2011-09-23 05:06:16 +00003609 llvm_unreachable("Cannot instantitiate an undeclared specialization.");
Chandler Carruth291b4412010-02-13 10:17:50 +00003610 case TSK_ExplicitInstantiationDeclaration:
Chandler Carruth291b4412010-02-13 10:17:50 +00003611 case TSK_ExplicitSpecialization:
Chandler Carruth58e390e2010-08-25 08:27:02 +00003612 continue; // No longer need to instantiate this type.
3613 case TSK_ExplicitInstantiationDefinition:
3614 // We only need an instantiation if the pending instantiation *is* the
3615 // explicit instantiation.
Douglas Gregoref96ee02012-01-14 16:38:05 +00003616 if (Var != Var->getMostRecentDecl()) continue;
Chandler Carruth291b4412010-02-13 10:17:50 +00003617 case TSK_ImplicitInstantiation:
3618 break;
3619 }
3620
John McCallf312b1e2010-08-26 23:41:50 +00003621 PrettyDeclStackTraceEntry CrashInfo(*this, Var, Var->getLocation(),
3622 "instantiating static data member "
3623 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00003624
Chandler Carruth58e390e2010-08-25 08:27:02 +00003625 bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
3626 TSK_ExplicitInstantiationDefinition;
3627 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true,
3628 DefinitionRequired);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003629 }
3630}
John McCall0c01d182010-03-24 05:22:00 +00003631
3632void Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
3633 const MultiLevelTemplateArgumentList &TemplateArgs) {
3634 for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
3635 E = Pattern->ddiag_end(); I != E; ++I) {
3636 DependentDiagnostic *DD = *I;
3637
3638 switch (DD->getKind()) {
3639 case DependentDiagnostic::Access:
3640 HandleDependentAccessCheck(*DD, TemplateArgs);
3641 break;
3642 }
3643 }
3644}