blob: b0935a54ac581c8fe6a0ee70f0c65450c089fc9f [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
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001123 if (QualifierLoc)
1124 Function->setQualifierInfo(QualifierLoc);
John McCallb6217662010-03-15 10:12:16 +00001125
John McCallb1a56e72010-03-26 23:10:15 +00001126 DeclContext *LexicalDC = Owner;
1127 if (!isFriend && D->isOutOfLine()) {
1128 assert(D->getDeclContext()->isFileContext());
1129 LexicalDC = D->getDeclContext();
1130 }
1131
1132 Function->setLexicalDeclContext(LexicalDC);
Mike Stump1eb44332009-09-09 15:08:12 +00001133
Douglas Gregore53060f2009-06-25 22:08:12 +00001134 // Attach the parameters
Douglas Gregor5cbe1012011-07-05 18:30:26 +00001135 if (isa<FunctionProtoType>(Function->getType().IgnoreParens())) {
Douglas Gregor1d441ee2011-06-22 18:16:25 +00001136 // Adopt the already-instantiated parameters into our own context.
1137 for (unsigned P = 0; P < Params.size(); ++P)
1138 if (Params[P])
1139 Params[P]->setOwningFunction(Function);
1140 } else {
1141 // Since we were instantiated via a typedef of a function type, create
1142 // new parameters.
1143 const FunctionProtoType *Proto
1144 = Function->getType()->getAs<FunctionProtoType>();
1145 assert(Proto && "No function prototype in template instantiation?");
1146 for (FunctionProtoType::arg_type_iterator AI = Proto->arg_type_begin(),
1147 AE = Proto->arg_type_end(); AI != AE; ++AI) {
1148 ParmVarDecl *Param
1149 = SemaRef.BuildParmVarDeclForTypedef(Function, Function->getLocation(),
1150 *AI);
1151 Param->setScopeInfo(0, Params.size());
1152 Params.push_back(Param);
1153 }
1154 }
David Blaikie4278c652011-09-21 18:16:56 +00001155 Function->setParams(Params);
John McCall02cace72009-08-28 07:59:38 +00001156
Douglas Gregorac7c2c82010-05-17 16:38:00 +00001157 SourceLocation InstantiateAtPOI;
Douglas Gregora735b202009-10-13 14:39:41 +00001158 if (TemplateParams) {
1159 // Our resulting instantiation is actually a function template, since we
1160 // are substituting only the outer template parameters. For example, given
1161 //
1162 // template<typename T>
1163 // struct X {
1164 // template<typename U> friend void f(T, U);
1165 // };
1166 //
1167 // X<int> x;
1168 //
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001169 // We are instantiating the friend function template "f" within X<int>,
Douglas Gregora735b202009-10-13 14:39:41 +00001170 // which means substituting int for T, but leaving "f" as a friend function
1171 // template.
1172 // Build the function template itself.
John McCalld325daa2010-03-26 04:53:08 +00001173 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
Douglas Gregora735b202009-10-13 14:39:41 +00001174 Function->getLocation(),
1175 Function->getDeclName(),
1176 TemplateParams, Function);
1177 Function->setDescribedFunctionTemplate(FunctionTemplate);
John McCallb1a56e72010-03-26 23:10:15 +00001178
1179 FunctionTemplate->setLexicalDeclContext(LexicalDC);
John McCalld325daa2010-03-26 04:53:08 +00001180
1181 if (isFriend && D->isThisDeclarationADefinition()) {
1182 // TODO: should we remember this connection regardless of whether
1183 // the friend declaration provided a body?
1184 FunctionTemplate->setInstantiatedFromMemberTemplate(
1185 D->getDescribedFunctionTemplate());
1186 }
Douglas Gregor66724ea2009-11-14 01:20:54 +00001187 } else if (FunctionTemplate) {
1188 // Record this function template specialization.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001189 std::pair<const TemplateArgument *, unsigned> Innermost
Douglas Gregor24bae922010-07-08 18:37:38 +00001190 = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001191 Function->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001192 TemplateArgumentList::CreateCopy(SemaRef.Context,
Douglas Gregor24bae922010-07-08 18:37:38 +00001193 Innermost.first,
1194 Innermost.second),
Douglas Gregor1e1e9722012-03-28 14:34:23 +00001195 /*InsertPos=*/0);
Chandler Carruth80f5b162011-08-18 09:09:59 +00001196 } else if (isFriend) {
1197 // Note, we need this connection even if the friend doesn't have a body.
1198 // Its body may exist but not have been attached yet due to deferred
1199 // parsing.
1200 // FIXME: It might be cleaner to set this when attaching the body to the
1201 // friend function declaration, however that would require finding all the
1202 // instantiations and modifying them.
John McCalld325daa2010-03-26 04:53:08 +00001203 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
John McCall02cace72009-08-28 07:59:38 +00001204 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001205
Douglas Gregore53060f2009-06-25 22:08:12 +00001206 if (InitFunctionInstantiation(Function, D))
1207 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001208
John McCallaf2094e2010-04-08 09:05:18 +00001209 bool isExplicitSpecialization = false;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001210
John McCall68263142009-11-18 22:49:29 +00001211 LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
1212 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
1213
John McCallaf2094e2010-04-08 09:05:18 +00001214 if (DependentFunctionTemplateSpecializationInfo *Info
1215 = D->getDependentSpecializationInfo()) {
1216 assert(isFriend && "non-friend has dependent specialization info?");
1217
1218 // This needs to be set now for future sanity.
1219 Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1220
1221 // Instantiate the explicit template arguments.
1222 TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1223 Info->getRAngleLoc());
Douglas Gregore02e2622010-12-22 21:19:48 +00001224 if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(),
1225 ExplicitArgs, TemplateArgs))
1226 return 0;
John McCallaf2094e2010-04-08 09:05:18 +00001227
1228 // Map the candidate templates to their instantiations.
1229 for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1230 Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1231 Info->getTemplate(I),
1232 TemplateArgs);
1233 if (!Temp) return 0;
1234
1235 Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1236 }
1237
1238 if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1239 &ExplicitArgs,
1240 Previous))
1241 Function->setInvalidDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001242
John McCallaf2094e2010-04-08 09:05:18 +00001243 isExplicitSpecialization = true;
1244
1245 } else if (TemplateParams || !FunctionTemplate) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001246 // Look only into the namespace where the friend would be declared to
1247 // find a previous declaration. This is the innermost enclosing namespace,
Douglas Gregora735b202009-10-13 14:39:41 +00001248 // as described in ActOnFriendFunctionDecl.
John McCall68263142009-11-18 22:49:29 +00001249 SemaRef.LookupQualifiedName(Previous, DC);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001250
Douglas Gregora735b202009-10-13 14:39:41 +00001251 // In C++, the previous declaration we find might be a tag type
1252 // (class or enum). In this case, the new declaration will hide the
1253 // tag type. Note that this does does not apply if we're declaring a
1254 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001255 if (Previous.isSingleTagDecl())
1256 Previous.clear();
Douglas Gregora735b202009-10-13 14:39:41 +00001257 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001258
John McCall9f54ad42009-12-10 09:41:52 +00001259 SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00001260 isExplicitSpecialization);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001261
John McCall76d32642010-04-24 01:30:58 +00001262 NamedDecl *PrincipalDecl = (TemplateParams
1263 ? cast<NamedDecl>(FunctionTemplate)
1264 : Function);
1265
Douglas Gregora735b202009-10-13 14:39:41 +00001266 // If the original function was part of a friend declaration,
1267 // inherit its namespace state and add it to the owner.
John McCalld325daa2010-03-26 04:53:08 +00001268 if (isFriend) {
John McCall68263142009-11-18 22:49:29 +00001269 NamedDecl *PrevDecl;
John McCall76d32642010-04-24 01:30:58 +00001270 if (TemplateParams)
Douglas Gregoref96ee02012-01-14 16:38:05 +00001271 PrevDecl = FunctionTemplate->getPreviousDecl();
John McCall76d32642010-04-24 01:30:58 +00001272 else
Douglas Gregoref96ee02012-01-14 16:38:05 +00001273 PrevDecl = Function->getPreviousDecl();
John McCall76d32642010-04-24 01:30:58 +00001274
1275 PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001276 DC->makeDeclVisibleInContext(PrincipalDecl);
Gabor Greifab297ac2010-08-30 21:10:05 +00001277
Gabor Greif77535df2010-08-30 22:25:56 +00001278 bool queuedInstantiation = false;
Gabor Greifab297ac2010-08-30 21:10:05 +00001279
Richard Smith53e53512011-10-19 00:54:10 +00001280 // C++98 [temp.friend]p5: When a function is defined in a friend function
1281 // declaration in a class template, the function is defined at each
1282 // instantiation of the class template. The function is defined even if it
1283 // is never used.
1284 // C++11 [temp.friend]p4: When a function is defined in a friend function
1285 // declaration in a class template, the function is instantiated when the
1286 // function is odr-used.
1287 //
1288 // If -Wc++98-compat is enabled, we go through the motions of checking for a
1289 // redefinition, but don't instantiate the function.
Richard Smith80ad52f2013-01-02 11:42:31 +00001290 if ((!SemaRef.getLangOpts().CPlusPlus11 ||
Richard Smith53e53512011-10-19 00:54:10 +00001291 SemaRef.Diags.getDiagnosticLevel(
1292 diag::warn_cxx98_compat_friend_redefinition,
1293 Function->getLocation())
1294 != DiagnosticsEngine::Ignored) &&
Douglas Gregor238058c2010-05-18 05:45:02 +00001295 D->isThisDeclarationADefinition()) {
1296 // Check for a function body.
1297 const FunctionDecl *Definition = 0;
Sean Hunt10620eb2011-05-06 20:44:56 +00001298 if (Function->isDefined(Definition) &&
Douglas Gregor238058c2010-05-18 05:45:02 +00001299 Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
Richard Smith53e53512011-10-19 00:54:10 +00001300 SemaRef.Diag(Function->getLocation(),
Richard Smith80ad52f2013-01-02 11:42:31 +00001301 SemaRef.getLangOpts().CPlusPlus11 ?
Richard Smith53e53512011-10-19 00:54:10 +00001302 diag::warn_cxx98_compat_friend_redefinition :
1303 diag::err_redefinition) << Function->getDeclName();
Douglas Gregor238058c2010-05-18 05:45:02 +00001304 SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
Richard Smith80ad52f2013-01-02 11:42:31 +00001305 if (!SemaRef.getLangOpts().CPlusPlus11)
Richard Smith53e53512011-10-19 00:54:10 +00001306 Function->setInvalidDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001307 }
Douglas Gregor238058c2010-05-18 05:45:02 +00001308 // Check for redefinitions due to other instantiations of this or
1309 // a similar friend function.
1310 else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1311 REnd = Function->redecls_end();
1312 R != REnd; ++R) {
Gabor Greif13a8aff2010-08-28 15:42:30 +00001313 if (*R == Function)
1314 continue;
Gabor Greifab297ac2010-08-30 21:10:05 +00001315 switch (R->getFriendObjectKind()) {
1316 case Decl::FOK_None:
Richard Smith80ad52f2013-01-02 11:42:31 +00001317 if (!SemaRef.getLangOpts().CPlusPlus11 &&
Richard Smith53e53512011-10-19 00:54:10 +00001318 !queuedInstantiation && R->isUsed(false)) {
Gabor Greifab297ac2010-08-30 21:10:05 +00001319 if (MemberSpecializationInfo *MSInfo
1320 = Function->getMemberSpecializationInfo()) {
1321 if (MSInfo->getPointOfInstantiation().isInvalid()) {
1322 SourceLocation Loc = R->getLocation(); // FIXME
1323 MSInfo->setPointOfInstantiation(Loc);
1324 SemaRef.PendingLocalImplicitInstantiations.push_back(
1325 std::make_pair(Function, Loc));
1326 queuedInstantiation = true;
1327 }
1328 }
1329 }
1330 break;
1331 default:
Douglas Gregor238058c2010-05-18 05:45:02 +00001332 if (const FunctionDecl *RPattern
Gabor Greif6a557d82010-08-28 15:46:56 +00001333 = R->getTemplateInstantiationPattern())
Sean Hunt10620eb2011-05-06 20:44:56 +00001334 if (RPattern->isDefined(RPattern)) {
Richard Smith53e53512011-10-19 00:54:10 +00001335 SemaRef.Diag(Function->getLocation(),
Richard Smith80ad52f2013-01-02 11:42:31 +00001336 SemaRef.getLangOpts().CPlusPlus11 ?
Richard Smith53e53512011-10-19 00:54:10 +00001337 diag::warn_cxx98_compat_friend_redefinition :
1338 diag::err_redefinition)
Douglas Gregor238058c2010-05-18 05:45:02 +00001339 << Function->getDeclName();
Gabor Greif6a557d82010-08-28 15:46:56 +00001340 SemaRef.Diag(R->getLocation(), diag::note_previous_definition);
Richard Smith80ad52f2013-01-02 11:42:31 +00001341 if (!SemaRef.getLangOpts().CPlusPlus11)
Richard Smith53e53512011-10-19 00:54:10 +00001342 Function->setInvalidDecl();
Douglas Gregor238058c2010-05-18 05:45:02 +00001343 break;
1344 }
1345 }
1346 }
1347 }
Douglas Gregora735b202009-10-13 14:39:41 +00001348 }
1349
John McCall76d32642010-04-24 01:30:58 +00001350 if (Function->isOverloadedOperator() && !DC->isRecord() &&
1351 PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
1352 PrincipalDecl->setNonMemberOperator();
1353
Sean Hunteb88ae52011-05-23 21:07:59 +00001354 assert(!D->isDefaulted() && "only methods should be defaulted");
Douglas Gregore53060f2009-06-25 22:08:12 +00001355 return Function;
1356}
1357
Douglas Gregord60e1052009-08-27 16:57:43 +00001358Decl *
1359TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
Francois Pichetaf0f4d02011-08-14 03:52:19 +00001360 TemplateParameterList *TemplateParams,
1361 bool IsClassScopeSpecialization) {
Douglas Gregor6b906862009-08-21 00:16:32 +00001362 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
Douglas Gregord60e1052009-08-27 16:57:43 +00001363 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +00001364 // We are creating a function template specialization from a function
1365 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +00001366 // specialization for this particular set of template arguments.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001367 std::pair<const TemplateArgument *, unsigned> Innermost
Douglas Gregor24bae922010-07-08 18:37:38 +00001368 = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +00001369
Douglas Gregor1e1e9722012-03-28 14:34:23 +00001370 void *InsertPos = 0;
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001371 FunctionDecl *SpecFunc
1372 = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
1373 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001374
Douglas Gregor6b906862009-08-21 00:16:32 +00001375 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001376 if (SpecFunc)
1377 return SpecFunc;
Douglas Gregor6b906862009-08-21 00:16:32 +00001378 }
1379
John McCallb0cb0222010-03-27 05:57:59 +00001380 bool isFriend;
1381 if (FunctionTemplate)
1382 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1383 else
1384 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1385
Douglas Gregor79c22782010-01-16 20:21:20 +00001386 bool MergeWithParentScope = (TemplateParams != 0) ||
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001387 !(isa<Decl>(Owner) &&
Douglas Gregor79c22782010-01-16 20:21:20 +00001388 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
John McCall2a7fb272010-08-25 05:32:35 +00001389 LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Douglas Gregor48dd19b2009-05-14 21:44:34 +00001390
John McCall4eab39f2010-10-19 02:26:41 +00001391 // Instantiate enclosing template arguments for friends.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001392 SmallVector<TemplateParameterList *, 4> TempParamLists;
John McCall4eab39f2010-10-19 02:26:41 +00001393 unsigned NumTempParamLists = 0;
1394 if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) {
1395 TempParamLists.set_size(NumTempParamLists);
1396 for (unsigned I = 0; I != NumTempParamLists; ++I) {
1397 TemplateParameterList *TempParams = D->getTemplateParameterList(I);
1398 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1399 if (!InstParams)
1400 return NULL;
1401 TempParamLists[I] = InstParams;
1402 }
1403 }
1404
Chris Lattner5f9e2722011-07-23 10:55:15 +00001405 SmallVector<ParmVarDecl *, 4> Params;
Benjamin Kramerdc370c12012-01-20 14:42:32 +00001406 TypeSourceInfo *TInfo = SubstFunctionType(D, Params);
John McCall21ef0fa2010-03-11 09:03:00 +00001407 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001408 return 0;
Douglas Gregor71074fd2012-09-13 21:56:43 +00001409 QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001410
Abramo Bagnara723df242010-12-14 22:11:44 +00001411 // \brief If the type of this function, after ignoring parentheses,
1412 // is not *directly* a function type, then we're instantiating a function
1413 // that was declared via a typedef, e.g.,
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001414 //
1415 // typedef int functype(int, int);
1416 // functype func;
1417 //
1418 // In this case, we'll just go instantiate the ParmVarDecls that we
1419 // synthesized in the method declaration.
Abramo Bagnara723df242010-12-14 22:11:44 +00001420 if (!isa<FunctionProtoType>(T.IgnoreParens())) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001421 assert(!Params.size() && "Instantiating type could not yield parameters");
Chris Lattner5f9e2722011-07-23 10:55:15 +00001422 SmallVector<QualType, 4> ParamTypes;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001423 if (SemaRef.SubstParmTypes(D->getLocation(), D->param_begin(),
1424 D->getNumParams(), TemplateArgs, ParamTypes,
Douglas Gregor12c9c002011-01-07 16:43:16 +00001425 &Params))
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001426 return 0;
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001427 }
1428
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001429 NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
1430 if (QualifierLoc) {
1431 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
John McCallb0cb0222010-03-27 05:57:59 +00001432 TemplateArgs);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001433 if (!QualifierLoc)
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001434 return 0;
John McCallb0cb0222010-03-27 05:57:59 +00001435 }
1436
1437 DeclContext *DC = Owner;
1438 if (isFriend) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001439 if (QualifierLoc) {
John McCallb0cb0222010-03-27 05:57:59 +00001440 CXXScopeSpec SS;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001441 SS.Adopt(QualifierLoc);
John McCallb0cb0222010-03-27 05:57:59 +00001442 DC = SemaRef.computeDeclContext(SS);
John McCallc54d6882010-10-19 05:01:53 +00001443
1444 if (DC && SemaRef.RequireCompleteDeclContext(SS, DC))
1445 return 0;
John McCallb0cb0222010-03-27 05:57:59 +00001446 } else {
1447 DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1448 D->getDeclContext(),
1449 TemplateArgs);
1450 }
1451 if (!DC) return 0;
1452 }
1453
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001454 // Build the instantiated method declaration.
John McCallb0cb0222010-03-27 05:57:59 +00001455 CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
Douglas Gregordec06662009-08-21 18:42:58 +00001456 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001457
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001458 SourceLocation StartLoc = D->getInnerLocStart();
Abramo Bagnara25777432010-08-11 22:01:17 +00001459 DeclarationNameInfo NameInfo
1460 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001461 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001462 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001463 StartLoc, NameInfo, T, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001464 Constructor->isExplicit(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001465 Constructor->isInlineSpecified(),
Richard Smith86c3ae42012-02-13 03:54:03 +00001466 false, Constructor->isConstexpr());
Douglas Gregor17e32f32009-08-21 22:43:28 +00001467 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001468 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001469 StartLoc, NameInfo, T, TInfo,
Abramo Bagnara25777432010-08-11 22:01:17 +00001470 Destructor->isInlineSpecified(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001471 false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001472 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001473 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001474 StartLoc, NameInfo, T, TInfo,
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001475 Conversion->isInlineSpecified(),
Douglas Gregorf5251602011-03-08 17:10:18 +00001476 Conversion->isExplicit(),
Richard Smith86c3ae42012-02-13 03:54:03 +00001477 Conversion->isConstexpr(),
Richard Smith9f569cc2011-10-01 02:31:28 +00001478 Conversion->getLocEnd());
Douglas Gregordec06662009-08-21 18:42:58 +00001479 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00001480 Method = CXXMethodDecl::Create(SemaRef.Context, Record,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001481 StartLoc, NameInfo, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001482 D->isStatic(),
1483 D->getStorageClassAsWritten(),
Douglas Gregorf5251602011-03-08 17:10:18 +00001484 D->isInlineSpecified(),
Richard Smith86c3ae42012-02-13 03:54:03 +00001485 D->isConstexpr(), D->getLocEnd());
Douglas Gregordec06662009-08-21 18:42:58 +00001486 }
Douglas Gregor6b906862009-08-21 00:16:32 +00001487
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001488 if (QualifierLoc)
1489 Method->setQualifierInfo(QualifierLoc);
John McCallb6217662010-03-15 10:12:16 +00001490
Douglas Gregord60e1052009-08-27 16:57:43 +00001491 if (TemplateParams) {
1492 // Our resulting instantiation is actually a function template, since we
1493 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +00001494 //
Douglas Gregord60e1052009-08-27 16:57:43 +00001495 // template<typename T>
1496 // struct X {
1497 // template<typename U> void f(T, U);
1498 // };
1499 //
1500 // X<int> x;
1501 //
1502 // We are instantiating the member template "f" within X<int>, which means
1503 // substituting int for T, but leaving "f" as a member function template.
1504 // Build the function template itself.
1505 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1506 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001507 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +00001508 TemplateParams, Method);
John McCallb0cb0222010-03-27 05:57:59 +00001509 if (isFriend) {
1510 FunctionTemplate->setLexicalDeclContext(Owner);
1511 FunctionTemplate->setObjectOfFriendDecl(true);
1512 } else if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +00001513 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +00001514 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001515 } else if (FunctionTemplate) {
1516 // Record this function template specialization.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001517 std::pair<const TemplateArgument *, unsigned> Innermost
Douglas Gregor24bae922010-07-08 18:37:38 +00001518 = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001519 Method->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001520 TemplateArgumentList::CreateCopy(SemaRef.Context,
1521 Innermost.first,
1522 Innermost.second),
Douglas Gregor1e1e9722012-03-28 14:34:23 +00001523 /*InsertPos=*/0);
John McCallb0cb0222010-03-27 05:57:59 +00001524 } else if (!isFriend) {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001525 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +00001526 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001527 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001528
Mike Stump1eb44332009-09-09 15:08:12 +00001529 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +00001530 // out-of-line, the instantiation will have the same lexical
1531 // context (which will be a namespace scope) as the template.
John McCallb0cb0222010-03-27 05:57:59 +00001532 if (isFriend) {
John McCall4eab39f2010-10-19 02:26:41 +00001533 if (NumTempParamLists)
1534 Method->setTemplateParameterListsInfo(SemaRef.Context,
1535 NumTempParamLists,
1536 TempParamLists.data());
1537
John McCallb0cb0222010-03-27 05:57:59 +00001538 Method->setLexicalDeclContext(Owner);
1539 Method->setObjectOfFriendDecl(true);
1540 } else if (D->isOutOfLine())
Douglas Gregor7caa6822009-07-24 20:34:43 +00001541 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001542
Douglas Gregor5545e162009-03-24 00:38:23 +00001543 // Attach the parameters
1544 for (unsigned P = 0; P < Params.size(); ++P)
1545 Params[P]->setOwningFunction(Method);
David Blaikie4278c652011-09-21 18:16:56 +00001546 Method->setParams(Params);
Douglas Gregor5545e162009-03-24 00:38:23 +00001547
1548 if (InitMethodInstantiation(Method, D))
1549 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001550
Abramo Bagnara25777432010-08-11 22:01:17 +00001551 LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
1552 Sema::ForRedeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001553
John McCallb0cb0222010-03-27 05:57:59 +00001554 if (!FunctionTemplate || TemplateParams || isFriend) {
1555 SemaRef.LookupQualifiedName(Previous, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001556
Douglas Gregordec06662009-08-21 18:42:58 +00001557 // In C++, the previous declaration we find might be a tag type
1558 // (class or enum). In this case, the new declaration will hide the
1559 // tag type. Note that this does does not apply if we're declaring a
1560 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001561 if (Previous.isSingleTagDecl())
1562 Previous.clear();
Douglas Gregordec06662009-08-21 18:42:58 +00001563 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001564
Francois Pichetaf0f4d02011-08-14 03:52:19 +00001565 if (!IsClassScopeSpecialization)
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00001566 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001567
Douglas Gregor4ba31362009-12-01 17:24:26 +00001568 if (D->isPure())
1569 SemaRef.CheckPureMethod(Method, SourceRange());
1570
John McCall1f2e1a92012-08-10 03:15:35 +00001571 // Propagate access. For a non-friend declaration, the access is
1572 // whatever we're propagating from. For a friend, it should be the
1573 // previous declaration we just found.
1574 if (isFriend && Method->getPreviousDecl())
1575 Method->setAccess(Method->getPreviousDecl()->getAccess());
1576 else
1577 Method->setAccess(D->getAccess());
1578 if (FunctionTemplate)
1579 FunctionTemplate->setAccess(Method->getAccess());
John McCall46460a62010-01-20 21:53:11 +00001580
Anders Carlsson9eefa222011-01-20 06:52:44 +00001581 SemaRef.CheckOverrideControl(Method);
1582
Eli Friedman3bc45152011-11-15 22:39:08 +00001583 // If a function is defined as defaulted or deleted, mark it as such now.
Richard Smithac713512012-12-08 02:53:02 +00001584 if (D->isExplicitlyDefaulted())
1585 SemaRef.SetDeclDefaulted(Method, Method->getLocation());
Eli Friedman3bc45152011-11-15 22:39:08 +00001586 if (D->isDeletedAsWritten())
Richard Smithac713512012-12-08 02:53:02 +00001587 SemaRef.SetDeclDeleted(Method, Method->getLocation());
Eli Friedman3bc45152011-11-15 22:39:08 +00001588
John McCall1f2e1a92012-08-10 03:15:35 +00001589 // If there's a function template, let our caller handle it.
John McCallb0cb0222010-03-27 05:57:59 +00001590 if (FunctionTemplate) {
John McCall1f2e1a92012-08-10 03:15:35 +00001591 // do nothing
1592
1593 // Don't hide a (potentially) valid declaration with an invalid one.
John McCallb0cb0222010-03-27 05:57:59 +00001594 } else if (Method->isInvalidDecl() && !Previous.empty()) {
John McCall1f2e1a92012-08-10 03:15:35 +00001595 // do nothing
1596
1597 // Otherwise, check access to friends and make them visible.
1598 } else if (isFriend) {
1599 // We only need to re-check access for methods which we didn't
1600 // manage to match during parsing.
1601 if (!D->getPreviousDecl())
1602 SemaRef.CheckFriendAccess(Method);
1603
1604 Record->makeDeclVisibleInContext(Method);
1605
1606 // Otherwise, add the declaration. We don't need to do this for
1607 // class-scope specializations because we'll have matched them with
1608 // the appropriate template.
1609 } else if (!IsClassScopeSpecialization) {
1610 Owner->addDecl(Method);
John McCallb0cb0222010-03-27 05:57:59 +00001611 }
Sean Hunteb88ae52011-05-23 21:07:59 +00001612
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001613 return Method;
1614}
1615
Douglas Gregor615c5d42009-03-24 16:43:20 +00001616Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +00001617 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +00001618}
1619
Douglas Gregor03b2b072009-03-24 00:15:49 +00001620Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001621 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +00001622}
1623
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001624Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001625 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001626}
1627
Douglas Gregor6477b692009-03-25 15:04:13 +00001628ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
John McCallfb44de92011-05-01 22:35:37 +00001629 return SemaRef.SubstParmVarDecl(D, TemplateArgs, /*indexAdjustment*/ 0,
Douglas Gregord1bb4ae2012-01-25 16:15:54 +00001630 llvm::Optional<unsigned>(),
1631 /*ExpectParameterPack=*/false);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001632}
1633
John McCalle29ba202009-08-20 01:44:21 +00001634Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1635 TemplateTypeParmDecl *D) {
1636 // TODO: don't always clone when decls are refcounted.
Chandler Carruth4fb86f82011-05-01 00:51:33 +00001637 assert(D->getTypeForDecl()->isTemplateTypeParmType());
Mike Stump1eb44332009-09-09 15:08:12 +00001638
John McCalle29ba202009-08-20 01:44:21 +00001639 TemplateTypeParmDecl *Inst =
Abramo Bagnara344577e2011-03-06 15:48:19 +00001640 TemplateTypeParmDecl::Create(SemaRef.Context, Owner,
1641 D->getLocStart(), D->getLocation(),
Chandler Carruth4fb86f82011-05-01 00:51:33 +00001642 D->getDepth() - TemplateArgs.getNumLevels(),
1643 D->getIndex(), D->getIdentifier(),
John McCalle29ba202009-08-20 01:44:21 +00001644 D->wasDeclaredWithTypename(),
1645 D->isParameterPack());
Douglas Gregor9a299e02011-03-04 17:52:15 +00001646 Inst->setAccess(AS_public);
John McCalle29ba202009-08-20 01:44:21 +00001647
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001648 if (D->hasDefaultArgument())
1649 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
1650
1651 // Introduce this template parameter's instantiation into the instantiation
Douglas Gregor550d9b22009-10-31 17:21:17 +00001652 // scope.
1653 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001654
John McCalle29ba202009-08-20 01:44:21 +00001655 return Inst;
1656}
1657
Douglas Gregor33642df2009-10-23 23:25:44 +00001658Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1659 NonTypeTemplateParmDecl *D) {
1660 // Substitute into the type of the non-type template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001661 TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc();
Chris Lattner5f9e2722011-07-23 10:55:15 +00001662 SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten;
1663 SmallVector<QualType, 4> ExpandedParameterPackTypes;
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001664 bool IsExpandedParameterPack = false;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001665 TypeSourceInfo *DI;
Douglas Gregor33642df2009-10-23 23:25:44 +00001666 QualType T;
Douglas Gregor33642df2009-10-23 23:25:44 +00001667 bool Invalid = false;
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001668
1669 if (D->isExpandedParameterPack()) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001670 // The non-type template parameter pack is an already-expanded pack
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001671 // expansion of types. Substitute into each of the expanded types.
1672 ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes());
1673 ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes());
1674 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
1675 TypeSourceInfo *NewDI =SemaRef.SubstType(D->getExpansionTypeSourceInfo(I),
1676 TemplateArgs,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001677 D->getLocation(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001678 D->getDeclName());
1679 if (!NewDI)
1680 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001681
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001682 ExpandedParameterPackTypesAsWritten.push_back(NewDI);
1683 QualType NewT =SemaRef.CheckNonTypeTemplateParameterType(NewDI->getType(),
1684 D->getLocation());
1685 if (NewT.isNull())
1686 return 0;
1687 ExpandedParameterPackTypes.push_back(NewT);
1688 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001689
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001690 IsExpandedParameterPack = true;
1691 DI = D->getTypeSourceInfo();
1692 T = DI->getType();
Richard Smith6964b3f2012-09-07 02:06:42 +00001693 } else if (D->isPackExpansion()) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001694 // The non-type template parameter pack's type is a pack expansion of types.
1695 // Determine whether we need to expand this parameter pack into separate
1696 // types.
1697 PackExpansionTypeLoc Expansion = cast<PackExpansionTypeLoc>(TL);
1698 TypeLoc Pattern = Expansion.getPatternLoc();
Chris Lattner5f9e2722011-07-23 10:55:15 +00001699 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001700 SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001701
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001702 // Determine whether the set of unexpanded parameter packs can and should
1703 // be expanded.
1704 bool Expand = true;
1705 bool RetainExpansion = false;
1706 llvm::Optional<unsigned> OrigNumExpansions
1707 = Expansion.getTypePtr()->getNumExpansions();
1708 llvm::Optional<unsigned> NumExpansions = OrigNumExpansions;
1709 if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(),
1710 Pattern.getSourceRange(),
David Blaikiea71f9d02011-09-22 02:34:54 +00001711 Unexpanded,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001712 TemplateArgs,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001713 Expand, RetainExpansion,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001714 NumExpansions))
1715 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001716
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001717 if (Expand) {
1718 for (unsigned I = 0; I != *NumExpansions; ++I) {
1719 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
1720 TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001721 D->getLocation(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001722 D->getDeclName());
1723 if (!NewDI)
1724 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001725
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001726 ExpandedParameterPackTypesAsWritten.push_back(NewDI);
1727 QualType NewT = SemaRef.CheckNonTypeTemplateParameterType(
1728 NewDI->getType(),
1729 D->getLocation());
1730 if (NewT.isNull())
1731 return 0;
1732 ExpandedParameterPackTypes.push_back(NewT);
1733 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001734
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001735 // Note that we have an expanded parameter pack. The "type" of this
1736 // expanded parameter pack is the original expansion type, but callers
1737 // will end up using the expanded parameter pack types for type-checking.
1738 IsExpandedParameterPack = true;
1739 DI = D->getTypeSourceInfo();
1740 T = DI->getType();
1741 } else {
1742 // We cannot fully expand the pack expansion now, so substitute into the
1743 // pattern and create a new pack expansion type.
1744 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
1745 TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001746 D->getLocation(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001747 D->getDeclName());
1748 if (!NewPattern)
1749 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001750
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001751 DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(),
1752 NumExpansions);
1753 if (!DI)
1754 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001755
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001756 T = DI->getType();
1757 }
1758 } else {
1759 // Simple case: substitution into a parameter that is not a parameter pack.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001760 DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001761 D->getLocation(), D->getDeclName());
1762 if (!DI)
1763 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001764
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001765 // Check that this type is acceptable for a non-type template parameter.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001766 T = SemaRef.CheckNonTypeTemplateParameterType(DI->getType(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001767 D->getLocation());
1768 if (T.isNull()) {
1769 T = SemaRef.Context.IntTy;
1770 Invalid = true;
1771 }
Douglas Gregor33642df2009-10-23 23:25:44 +00001772 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001773
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001774 NonTypeTemplateParmDecl *Param;
1775 if (IsExpandedParameterPack)
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001776 Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001777 D->getInnerLocStart(),
1778 D->getLocation(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001779 D->getDepth() - TemplateArgs.getNumLevels(),
1780 D->getPosition(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001781 D->getIdentifier(), T,
1782 DI,
1783 ExpandedParameterPackTypes.data(),
1784 ExpandedParameterPackTypes.size(),
1785 ExpandedParameterPackTypesAsWritten.data());
1786 else
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001787 Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001788 D->getInnerLocStart(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001789 D->getLocation(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001790 D->getDepth() - TemplateArgs.getNumLevels(),
1791 D->getPosition(),
1792 D->getIdentifier(), T,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001793 D->isParameterPack(), DI);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001794
Douglas Gregor9a299e02011-03-04 17:52:15 +00001795 Param->setAccess(AS_public);
Douglas Gregor33642df2009-10-23 23:25:44 +00001796 if (Invalid)
1797 Param->setInvalidDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001798
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001799 Param->setDefaultArgument(D->getDefaultArgument(), false);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001800
1801 // Introduce this template parameter's instantiation into the instantiation
Douglas Gregor550d9b22009-10-31 17:21:17 +00001802 // scope.
1803 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001804 return Param;
1805}
1806
Richard Smith6964b3f2012-09-07 02:06:42 +00001807static void collectUnexpandedParameterPacks(
1808 Sema &S,
1809 TemplateParameterList *Params,
1810 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
1811 for (TemplateParameterList::const_iterator I = Params->begin(),
1812 E = Params->end(); I != E; ++I) {
1813 if ((*I)->isTemplateParameterPack())
1814 continue;
1815 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*I))
1816 S.collectUnexpandedParameterPacks(NTTP->getTypeSourceInfo()->getTypeLoc(),
1817 Unexpanded);
1818 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(*I))
1819 collectUnexpandedParameterPacks(S, TTP->getTemplateParameters(),
1820 Unexpanded);
1821 }
1822}
1823
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001824Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001825TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1826 TemplateTemplateParmDecl *D) {
1827 // Instantiate the template parameter list of the template template parameter.
1828 TemplateParameterList *TempParams = D->getTemplateParameters();
1829 TemplateParameterList *InstParams;
Richard Smith6964b3f2012-09-07 02:06:42 +00001830 SmallVector<TemplateParameterList*, 8> ExpandedParams;
1831
1832 bool IsExpandedParameterPack = false;
1833
1834 if (D->isExpandedParameterPack()) {
1835 // The template template parameter pack is an already-expanded pack
1836 // expansion of template parameters. Substitute into each of the expanded
1837 // parameters.
1838 ExpandedParams.reserve(D->getNumExpansionTemplateParameters());
1839 for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
1840 I != N; ++I) {
1841 LocalInstantiationScope Scope(SemaRef);
1842 TemplateParameterList *Expansion =
1843 SubstTemplateParams(D->getExpansionTemplateParameters(I));
1844 if (!Expansion)
1845 return 0;
1846 ExpandedParams.push_back(Expansion);
1847 }
1848
1849 IsExpandedParameterPack = true;
1850 InstParams = TempParams;
1851 } else if (D->isPackExpansion()) {
1852 // The template template parameter pack expands to a pack of template
1853 // template parameters. Determine whether we need to expand this parameter
1854 // pack into separate parameters.
1855 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
1856 collectUnexpandedParameterPacks(SemaRef, D->getTemplateParameters(),
1857 Unexpanded);
1858
1859 // Determine whether the set of unexpanded parameter packs can and should
1860 // be expanded.
1861 bool Expand = true;
1862 bool RetainExpansion = false;
1863 llvm::Optional<unsigned> NumExpansions;
1864 if (SemaRef.CheckParameterPacksForExpansion(D->getLocation(),
1865 TempParams->getSourceRange(),
1866 Unexpanded,
1867 TemplateArgs,
1868 Expand, RetainExpansion,
1869 NumExpansions))
1870 return 0;
1871
1872 if (Expand) {
1873 for (unsigned I = 0; I != *NumExpansions; ++I) {
1874 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
1875 LocalInstantiationScope Scope(SemaRef);
1876 TemplateParameterList *Expansion = SubstTemplateParams(TempParams);
1877 if (!Expansion)
1878 return 0;
1879 ExpandedParams.push_back(Expansion);
1880 }
1881
1882 // Note that we have an expanded parameter pack. The "type" of this
1883 // expanded parameter pack is the original expansion type, but callers
1884 // will end up using the expanded parameter pack types for type-checking.
1885 IsExpandedParameterPack = true;
1886 InstParams = TempParams;
1887 } else {
1888 // We cannot fully expand the pack expansion now, so just substitute
1889 // into the pattern.
1890 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
1891
1892 LocalInstantiationScope Scope(SemaRef);
1893 InstParams = SubstTemplateParams(TempParams);
1894 if (!InstParams)
1895 return 0;
1896 }
1897 } else {
Douglas Gregor9106ef72009-11-11 16:58:32 +00001898 // Perform the actual substitution of template parameters within a new,
1899 // local instantiation scope.
John McCall2a7fb272010-08-25 05:32:35 +00001900 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor9106ef72009-11-11 16:58:32 +00001901 InstParams = SubstTemplateParams(TempParams);
1902 if (!InstParams)
Richard Smith6964b3f2012-09-07 02:06:42 +00001903 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001904 }
1905
Douglas Gregor9106ef72009-11-11 16:58:32 +00001906 // Build the template template parameter.
Richard Smith6964b3f2012-09-07 02:06:42 +00001907 TemplateTemplateParmDecl *Param;
1908 if (IsExpandedParameterPack)
1909 Param = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner,
1910 D->getLocation(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001911 D->getDepth() - TemplateArgs.getNumLevels(),
Richard Smith6964b3f2012-09-07 02:06:42 +00001912 D->getPosition(),
1913 D->getIdentifier(), InstParams,
1914 ExpandedParams);
1915 else
1916 Param = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner,
1917 D->getLocation(),
1918 D->getDepth() - TemplateArgs.getNumLevels(),
1919 D->getPosition(),
1920 D->isParameterPack(),
1921 D->getIdentifier(), InstParams);
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001922 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor9a299e02011-03-04 17:52:15 +00001923 Param->setAccess(AS_public);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001924
1925 // Introduce this template parameter's instantiation into the instantiation
Douglas Gregor9106ef72009-11-11 16:58:32 +00001926 // scope.
1927 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001928
Douglas Gregor9106ef72009-11-11 16:58:32 +00001929 return Param;
1930}
1931
Douglas Gregor48c32a72009-11-17 06:07:40 +00001932Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Douglas Gregordb992412011-02-25 16:33:46 +00001933 // Using directives are never dependent (and never contain any types or
1934 // expressions), so they require no explicit instantiation work.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001935
Douglas Gregor48c32a72009-11-17 06:07:40 +00001936 UsingDirectiveDecl *Inst
1937 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001938 D->getNamespaceKeyLocation(),
Douglas Gregordb992412011-02-25 16:33:46 +00001939 D->getQualifierLoc(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001940 D->getIdentLocation(),
1941 D->getNominatedNamespace(),
Douglas Gregor48c32a72009-11-17 06:07:40 +00001942 D->getCommonAncestor());
Abramo Bagnara536afbe2012-09-05 09:55:10 +00001943
1944 // Add the using directive to its declaration context
1945 // only if this is not a function or method.
1946 if (!Owner->isFunctionOrMethod())
1947 Owner->addDecl(Inst);
1948
Douglas Gregor48c32a72009-11-17 06:07:40 +00001949 return Inst;
1950}
1951
John McCalled976492009-12-04 22:46:56 +00001952Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
Douglas Gregor1b398202010-09-29 17:58:28 +00001953
1954 // The nested name specifier may be dependent, for example
1955 // template <typename T> struct t {
1956 // struct s1 { T f1(); };
1957 // struct s2 : s1 { using s1::f1; };
1958 // };
1959 // template struct t<int>;
1960 // Here, in using s1::f1, s1 refers to t<T>::s1;
1961 // we need to substitute for t<int>::s1.
Douglas Gregor5149f372011-02-25 15:54:31 +00001962 NestedNameSpecifierLoc QualifierLoc
1963 = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
1964 TemplateArgs);
1965 if (!QualifierLoc)
Douglas Gregordc355712011-02-25 00:36:19 +00001966 return 0;
Douglas Gregor1b398202010-09-29 17:58:28 +00001967
1968 // The name info is non-dependent, so no transformation
1969 // is required.
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001970 DeclarationNameInfo NameInfo = D->getNameInfo();
John McCalled976492009-12-04 22:46:56 +00001971
John McCall9f54ad42009-12-10 09:41:52 +00001972 // We only need to do redeclaration lookups if we're in a class
1973 // scope (in fact, it's not really even possible in non-class
1974 // scopes).
1975 bool CheckRedeclaration = Owner->isRecord();
1976
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001977 LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
1978 Sema::ForRedeclaration);
John McCall9f54ad42009-12-10 09:41:52 +00001979
John McCalled976492009-12-04 22:46:56 +00001980 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
John McCalled976492009-12-04 22:46:56 +00001981 D->getUsingLocation(),
Douglas Gregor5149f372011-02-25 15:54:31 +00001982 QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001983 NameInfo,
John McCalled976492009-12-04 22:46:56 +00001984 D->isTypeName());
1985
Douglas Gregor5149f372011-02-25 15:54:31 +00001986 CXXScopeSpec SS;
1987 SS.Adopt(QualifierLoc);
John McCall9f54ad42009-12-10 09:41:52 +00001988 if (CheckRedeclaration) {
1989 Prev.setHideTags(false);
1990 SemaRef.LookupQualifiedName(Prev, Owner);
1991
1992 // Check for invalid redeclarations.
1993 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
1994 D->isTypeName(), SS,
1995 D->getLocation(), Prev))
1996 NewUD->setInvalidDecl();
1997
1998 }
1999
2000 if (!NewUD->isInvalidDecl() &&
2001 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
John McCalled976492009-12-04 22:46:56 +00002002 D->getLocation()))
2003 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00002004
John McCalled976492009-12-04 22:46:56 +00002005 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
2006 NewUD->setAccess(D->getAccess());
2007 Owner->addDecl(NewUD);
2008
John McCall9f54ad42009-12-10 09:41:52 +00002009 // Don't process the shadow decls for an invalid decl.
2010 if (NewUD->isInvalidDecl())
2011 return NewUD;
2012
Richard Smithc5a89a12012-04-02 01:30:27 +00002013 if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) {
2014 if (SemaRef.CheckInheritingConstructorUsingDecl(NewUD))
2015 NewUD->setInvalidDecl();
2016 return NewUD;
2017 }
2018
John McCall323c3102009-12-22 22:26:37 +00002019 bool isFunctionScope = Owner->isFunctionOrMethod();
2020
John McCall9f54ad42009-12-10 09:41:52 +00002021 // Process the shadow decls.
2022 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
2023 I != E; ++I) {
2024 UsingShadowDecl *Shadow = *I;
2025 NamedDecl *InstTarget =
Douglas Gregorb7107222011-03-04 19:46:35 +00002026 cast_or_null<NamedDecl>(SemaRef.FindInstantiatedDecl(
2027 Shadow->getLocation(),
2028 Shadow->getTargetDecl(),
2029 TemplateArgs));
2030 if (!InstTarget)
2031 return 0;
John McCall9f54ad42009-12-10 09:41:52 +00002032
2033 if (CheckRedeclaration &&
2034 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
2035 continue;
2036
2037 UsingShadowDecl *InstShadow
2038 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
2039 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
John McCall323c3102009-12-22 22:26:37 +00002040
2041 if (isFunctionScope)
2042 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
John McCall9f54ad42009-12-10 09:41:52 +00002043 }
John McCalled976492009-12-04 22:46:56 +00002044
2045 return NewUD;
2046}
2047
2048Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00002049 // Ignore these; we handle them in bulk when processing the UsingDecl.
2050 return 0;
John McCalled976492009-12-04 22:46:56 +00002051}
2052
John McCall7ba107a2009-11-18 02:36:19 +00002053Decl * TemplateDeclInstantiator
2054 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Douglas Gregor5149f372011-02-25 15:54:31 +00002055 NestedNameSpecifierLoc QualifierLoc
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002056 = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
Douglas Gregor5149f372011-02-25 15:54:31 +00002057 TemplateArgs);
2058 if (!QualifierLoc)
Anders Carlsson0dde18e2009-08-28 15:18:15 +00002059 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002060
Anders Carlsson0dde18e2009-08-28 15:18:15 +00002061 CXXScopeSpec SS;
Douglas Gregor5149f372011-02-25 15:54:31 +00002062 SS.Adopt(QualifierLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002063
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002064 // Since NameInfo refers to a typename, it cannot be a C++ special name.
Benjamin Krameraccaf192012-11-14 15:08:31 +00002065 // Hence, no transformation is required for it.
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002066 DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00002067 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00002068 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002069 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00002070 /*instantiation*/ true,
2071 /*typename*/ true, D->getTypenameLoc());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00002072 if (UD)
John McCalled976492009-12-04 22:46:56 +00002073 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
2074
John McCall7ba107a2009-11-18 02:36:19 +00002075 return UD;
2076}
2077
2078Decl * TemplateDeclInstantiator
2079 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Douglas Gregor5149f372011-02-25 15:54:31 +00002080 NestedNameSpecifierLoc QualifierLoc
2081 = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), TemplateArgs);
2082 if (!QualifierLoc)
John McCall7ba107a2009-11-18 02:36:19 +00002083 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002084
John McCall7ba107a2009-11-18 02:36:19 +00002085 CXXScopeSpec SS;
Douglas Gregor5149f372011-02-25 15:54:31 +00002086 SS.Adopt(QualifierLoc);
John McCall7ba107a2009-11-18 02:36:19 +00002087
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002088 DeclarationNameInfo NameInfo
2089 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
2090
John McCall7ba107a2009-11-18 02:36:19 +00002091 NamedDecl *UD =
2092 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002093 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00002094 /*instantiation*/ true,
2095 /*typename*/ false, SourceLocation());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00002096 if (UD)
John McCalled976492009-12-04 22:46:56 +00002097 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
2098
Anders Carlsson0d8df782009-08-29 19:37:28 +00002099 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00002100}
2101
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002102
2103Decl *TemplateDeclInstantiator::VisitClassScopeFunctionSpecializationDecl(
2104 ClassScopeFunctionSpecializationDecl *Decl) {
2105 CXXMethodDecl *OldFD = Decl->getSpecialization();
Nico Weber6b020092012-06-25 17:21:05 +00002106 CXXMethodDecl *NewFD = cast<CXXMethodDecl>(VisitCXXMethodDecl(OldFD,
2107 0, true));
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002108
2109 LookupResult Previous(SemaRef, NewFD->getNameInfo(), Sema::LookupOrdinaryName,
2110 Sema::ForRedeclaration);
2111
Nico Weber6b020092012-06-25 17:21:05 +00002112 TemplateArgumentListInfo TemplateArgs;
2113 TemplateArgumentListInfo* TemplateArgsPtr = 0;
2114 if (Decl->hasExplicitTemplateArgs()) {
2115 TemplateArgs = Decl->templateArgs();
2116 TemplateArgsPtr = &TemplateArgs;
2117 }
2118
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002119 SemaRef.LookupQualifiedName(Previous, SemaRef.CurContext);
Nico Weber6b020092012-06-25 17:21:05 +00002120 if (SemaRef.CheckFunctionTemplateSpecialization(NewFD, TemplateArgsPtr,
2121 Previous)) {
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002122 NewFD->setInvalidDecl();
2123 return NewFD;
2124 }
2125
2126 // Associate the specialization with the pattern.
2127 FunctionDecl *Specialization = cast<FunctionDecl>(Previous.getFoundDecl());
2128 assert(Specialization && "Class scope Specialization is null");
2129 SemaRef.Context.setClassScopeSpecializationPattern(Specialization, OldFD);
2130
2131 return NewFD;
2132}
2133
John McCallce3ff2b2009-08-25 22:02:44 +00002134Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00002135 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00002136 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor2fa98002010-02-16 19:28:15 +00002137 if (D->isInvalidDecl())
2138 return 0;
2139
Douglas Gregor8dbc2692009-03-17 21:15:40 +00002140 return Instantiator.Visit(D);
2141}
2142
John McCalle29ba202009-08-20 01:44:21 +00002143/// \brief Instantiates a nested template parameter list in the current
2144/// instantiation context.
2145///
2146/// \param L The parameter list to instantiate
2147///
2148/// \returns NULL if there was an error
2149TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00002150TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00002151 // Get errors for all the parameters before bailing out.
2152 bool Invalid = false;
2153
2154 unsigned N = L->size();
Chris Lattner5f9e2722011-07-23 10:55:15 +00002155 typedef SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00002156 ParamVector Params;
2157 Params.reserve(N);
2158 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
2159 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002160 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00002161 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00002162 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00002163 }
2164
2165 // Clean up if we had an error.
Douglas Gregorff331c12010-07-25 18:17:45 +00002166 if (Invalid)
John McCalle29ba202009-08-20 01:44:21 +00002167 return NULL;
John McCalle29ba202009-08-20 01:44:21 +00002168
2169 TemplateParameterList *InstL
2170 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
2171 L->getLAngleLoc(), &Params.front(), N,
2172 L->getRAngleLoc());
2173 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00002174}
John McCalle29ba202009-08-20 01:44:21 +00002175
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002176/// \brief Instantiate the declaration of a class template partial
Douglas Gregored9c0f92009-10-29 00:04:11 +00002177/// specialization.
2178///
2179/// \param ClassTemplate the (instantiated) class template that is partially
2180// specialized by the instantiation of \p PartialSpec.
2181///
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002182/// \param PartialSpec the (uninstantiated) class template partial
Douglas Gregored9c0f92009-10-29 00:04:11 +00002183/// specialization that we are instantiating.
2184///
Douglas Gregord65587f2010-11-10 19:44:59 +00002185/// \returns The instantiated partial specialization, if successful; otherwise,
2186/// NULL to indicate an error.
2187ClassTemplatePartialSpecializationDecl *
Douglas Gregored9c0f92009-10-29 00:04:11 +00002188TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
2189 ClassTemplateDecl *ClassTemplate,
2190 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00002191 // Create a local instantiation scope for this class template partial
2192 // specialization, which will contain the instantiations of the template
2193 // parameters.
John McCall2a7fb272010-08-25 05:32:35 +00002194 LocalInstantiationScope Scope(SemaRef);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002195
Douglas Gregored9c0f92009-10-29 00:04:11 +00002196 // Substitute into the template parameters of the class template partial
2197 // specialization.
2198 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
2199 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
2200 if (!InstParams)
Douglas Gregord65587f2010-11-10 19:44:59 +00002201 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002202
Douglas Gregored9c0f92009-10-29 00:04:11 +00002203 // Substitute into the template arguments of the class template partial
2204 // specialization.
John McCalld5532b62009-11-23 01:53:49 +00002205 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002206 if (SemaRef.Subst(PartialSpec->getTemplateArgsAsWritten(),
2207 PartialSpec->getNumTemplateArgsAsWritten(),
Douglas Gregore02e2622010-12-22 21:19:48 +00002208 InstTemplateArgs, TemplateArgs))
2209 return 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002210
Douglas Gregored9c0f92009-10-29 00:04:11 +00002211 // Check that the template argument list is well-formed for this
2212 // class template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002213 SmallVector<TemplateArgument, 4> Converted;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002214 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002215 PartialSpec->getLocation(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002216 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002217 false,
2218 Converted))
Douglas Gregord65587f2010-11-10 19:44:59 +00002219 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00002220
2221 // Figure out where to insert this class template partial specialization
2222 // in the member template's set of class template partial specializations.
Douglas Gregored9c0f92009-10-29 00:04:11 +00002223 void *InsertPos = 0;
2224 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00002225 = ClassTemplate->findPartialSpecialization(Converted.data(),
2226 Converted.size(), InsertPos);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002227
Douglas Gregored9c0f92009-10-29 00:04:11 +00002228 // Build the canonical type that describes the converted template
2229 // arguments of the class template partial specialization.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002230 QualType CanonType
Douglas Gregored9c0f92009-10-29 00:04:11 +00002231 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
Douglas Gregor910f8002010-11-07 23:05:16 +00002232 Converted.data(),
2233 Converted.size());
Douglas Gregored9c0f92009-10-29 00:04:11 +00002234
2235 // Build the fully-sugared type for this class template
2236 // specialization as the user wrote in the specialization
2237 // itself. This means that we'll pretty-print the type retrieved
2238 // from the specialization's declaration the way that the user
2239 // actually wrote the specialization, rather than formatting the
2240 // name based on the "canonical" representation used to store the
2241 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00002242 TypeSourceInfo *WrittenTy
2243 = SemaRef.Context.getTemplateSpecializationTypeInfo(
2244 TemplateName(ClassTemplate),
2245 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00002246 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002247 CanonType);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002248
Douglas Gregored9c0f92009-10-29 00:04:11 +00002249 if (PrevDecl) {
2250 // We've already seen a partial specialization with the same template
2251 // parameters and template arguments. This can happen, for example, when
2252 // substituting the outer template arguments ends up causing two
2253 // class template partial specializations of a member class template
2254 // to have identical forms, e.g.,
2255 //
2256 // template<typename T, typename U>
2257 // struct Outer {
2258 // template<typename X, typename Y> struct Inner;
2259 // template<typename Y> struct Inner<T, Y>;
2260 // template<typename Y> struct Inner<U, Y>;
2261 // };
2262 //
2263 // Outer<int, int> outer; // error: the partial specializations of Inner
2264 // // have the same signature.
2265 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
Douglas Gregord65587f2010-11-10 19:44:59 +00002266 << WrittenTy->getType();
Douglas Gregored9c0f92009-10-29 00:04:11 +00002267 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
2268 << SemaRef.Context.getTypeDeclType(PrevDecl);
Douglas Gregord65587f2010-11-10 19:44:59 +00002269 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00002270 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002271
2272
Douglas Gregored9c0f92009-10-29 00:04:11 +00002273 // Create the class template partial specialization declaration.
2274 ClassTemplatePartialSpecializationDecl *InstPartialSpec
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002275 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00002276 PartialSpec->getTagKind(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002277 Owner,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002278 PartialSpec->getLocStart(),
2279 PartialSpec->getLocation(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002280 InstParams,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002281 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00002282 Converted.data(),
2283 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00002284 InstTemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00002285 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00002286 0,
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00002287 ClassTemplate->getNextPartialSpecSequenceNumber());
John McCallb6217662010-03-15 10:12:16 +00002288 // Substitute the nested name specifier, if any.
2289 if (SubstQualifier(PartialSpec, InstPartialSpec))
2290 return 0;
2291
Douglas Gregored9c0f92009-10-29 00:04:11 +00002292 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00002293 InstPartialSpec->setTypeAsWritten(WrittenTy);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002294
Douglas Gregored9c0f92009-10-29 00:04:11 +00002295 // Add this partial specialization to the set of class template partial
2296 // specializations.
Douglas Gregor1e1e9722012-03-28 14:34:23 +00002297 ClassTemplate->AddPartialSpecialization(InstPartialSpec, /*InsertPos=*/0);
Douglas Gregord65587f2010-11-10 19:44:59 +00002298 return InstPartialSpec;
Douglas Gregored9c0f92009-10-29 00:04:11 +00002299}
2300
John McCall21ef0fa2010-03-11 09:03:00 +00002301TypeSourceInfo*
John McCallce3ff2b2009-08-25 22:02:44 +00002302TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002303 SmallVectorImpl<ParmVarDecl *> &Params) {
John McCall21ef0fa2010-03-11 09:03:00 +00002304 TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
2305 assert(OldTInfo && "substituting function without type source info");
2306 assert(Params.empty() && "parameter vector is non-empty at start");
Douglas Gregorcefc3af2012-04-16 07:05:22 +00002307
2308 CXXRecordDecl *ThisContext = 0;
2309 unsigned ThisTypeQuals = 0;
2310 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2311 ThisContext = Method->getParent();
2312 ThisTypeQuals = Method->getTypeQualifiers();
2313 }
2314
John McCall6cd3b9f2010-04-09 17:38:44 +00002315 TypeSourceInfo *NewTInfo
2316 = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
2317 D->getTypeSpecStartLoc(),
Douglas Gregorcefc3af2012-04-16 07:05:22 +00002318 D->getDeclName(),
2319 ThisContext, ThisTypeQuals);
John McCall21ef0fa2010-03-11 09:03:00 +00002320 if (!NewTInfo)
2321 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +00002322
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00002323 if (NewTInfo != OldTInfo) {
2324 // Get parameters from the new type info.
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00002325 TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00002326 if (FunctionProtoTypeLoc *OldProtoLoc
2327 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00002328 TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00002329 FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
2330 assert(NewProtoLoc && "Missing prototype?");
Richard Smith500d7292012-07-18 01:29:05 +00002331 unsigned NewIdx = 0;
Douglas Gregor12c9c002011-01-07 16:43:16 +00002332 for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc->getNumArgs();
2333 OldIdx != NumOldParams; ++OldIdx) {
2334 ParmVarDecl *OldParam = OldProtoLoc->getArg(OldIdx);
Richard Smith500d7292012-07-18 01:29:05 +00002335 LocalInstantiationScope *Scope = SemaRef.CurrentInstantiationScope;
2336
2337 llvm::Optional<unsigned> NumArgumentsInExpansion;
2338 if (OldParam->isParameterPack())
2339 NumArgumentsInExpansion =
2340 SemaRef.getNumArgumentsInExpansion(OldParam->getType(),
2341 TemplateArgs);
2342 if (!NumArgumentsInExpansion) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002343 // Simple case: normal parameter, or a parameter pack that's
Douglas Gregor12c9c002011-01-07 16:43:16 +00002344 // instantiated to a (still-dependent) parameter pack.
2345 ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++);
2346 Params.push_back(NewParam);
Richard Smith500d7292012-07-18 01:29:05 +00002347 Scope->InstantiatedLocal(OldParam, NewParam);
2348 } else {
2349 // Parameter pack expansion: make the instantiation an argument pack.
2350 Scope->MakeInstantiatedLocalArgPack(OldParam);
2351 for (unsigned I = 0; I != *NumArgumentsInExpansion; ++I) {
2352 ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++);
2353 Params.push_back(NewParam);
2354 Scope->InstantiatedLocalPackArg(OldParam, NewParam);
2355 }
Douglas Gregor12c9c002011-01-07 16:43:16 +00002356 }
Douglas Gregor6920cdc2010-05-03 15:32:18 +00002357 }
Douglas Gregor895162d2010-04-30 18:55:50 +00002358 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00002359 } else {
2360 // The function type itself was not dependent and therefore no
2361 // substitution occurred. However, we still need to instantiate
2362 // the function parameters themselves.
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00002363 TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00002364 if (FunctionProtoTypeLoc *OldProtoLoc
2365 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
2366 for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
2367 ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
2368 if (!Parm)
2369 return 0;
2370 Params.push_back(Parm);
2371 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00002372 }
2373 }
John McCall21ef0fa2010-03-11 09:03:00 +00002374 return NewTInfo;
Douglas Gregor5545e162009-03-24 00:38:23 +00002375}
2376
Richard Smithe6975e92012-04-17 00:58:00 +00002377/// Introduce the instantiated function parameters into the local
2378/// instantiation scope, and set the parameter names to those used
2379/// in the template.
2380static void addInstantiatedParametersToScope(Sema &S, FunctionDecl *Function,
2381 const FunctionDecl *PatternDecl,
2382 LocalInstantiationScope &Scope,
2383 const MultiLevelTemplateArgumentList &TemplateArgs) {
2384 unsigned FParamIdx = 0;
2385 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
2386 const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
2387 if (!PatternParam->isParameterPack()) {
2388 // Simple case: not a parameter pack.
2389 assert(FParamIdx < Function->getNumParams());
2390 ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
2391 FunctionParam->setDeclName(PatternParam->getDeclName());
2392 Scope.InstantiatedLocal(PatternParam, FunctionParam);
2393 ++FParamIdx;
2394 continue;
2395 }
2396
2397 // Expand the parameter pack.
2398 Scope.MakeInstantiatedLocalArgPack(PatternParam);
Richard Smith500d7292012-07-18 01:29:05 +00002399 llvm::Optional<unsigned> NumArgumentsInExpansion
Richard Smithe6975e92012-04-17 00:58:00 +00002400 = S.getNumArgumentsInExpansion(PatternParam->getType(), TemplateArgs);
Richard Smith500d7292012-07-18 01:29:05 +00002401 assert(NumArgumentsInExpansion &&
2402 "should only be called when all template arguments are known");
2403 for (unsigned Arg = 0; Arg < *NumArgumentsInExpansion; ++Arg) {
Richard Smithe6975e92012-04-17 00:58:00 +00002404 ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
2405 FunctionParam->setDeclName(PatternParam->getDeclName());
2406 Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam);
2407 ++FParamIdx;
2408 }
2409 }
2410}
2411
2412static void InstantiateExceptionSpec(Sema &SemaRef, FunctionDecl *New,
2413 const FunctionProtoType *Proto,
2414 const MultiLevelTemplateArgumentList &TemplateArgs) {
Richard Smith13bffc52012-04-19 00:08:28 +00002415 assert(Proto->getExceptionSpecType() != EST_Uninstantiated);
2416
Richard Smithe6975e92012-04-17 00:58:00 +00002417 // C++11 [expr.prim.general]p3:
2418 // If a declaration declares a member function or member function
2419 // template of a class X, the expression this is a prvalue of type
2420 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
2421 // and the end of the function-definition, member-declarator, or
2422 // declarator.
2423 CXXRecordDecl *ThisContext = 0;
2424 unsigned ThisTypeQuals = 0;
2425 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(New)) {
2426 ThisContext = Method->getParent();
2427 ThisTypeQuals = Method->getTypeQualifiers();
2428 }
2429 Sema::CXXThisScopeRAII ThisScope(SemaRef, ThisContext, ThisTypeQuals,
Richard Smith80ad52f2013-01-02 11:42:31 +00002430 SemaRef.getLangOpts().CPlusPlus11);
Richard Smithe6975e92012-04-17 00:58:00 +00002431
2432 // The function has an exception specification or a "noreturn"
2433 // attribute. Substitute into each of the exception types.
2434 SmallVector<QualType, 4> Exceptions;
2435 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
2436 // FIXME: Poor location information!
2437 if (const PackExpansionType *PackExpansion
2438 = Proto->getExceptionType(I)->getAs<PackExpansionType>()) {
2439 // We have a pack expansion. Instantiate it.
2440 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2441 SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(),
2442 Unexpanded);
2443 assert(!Unexpanded.empty() &&
2444 "Pack expansion without parameter packs?");
2445
2446 bool Expand = false;
2447 bool RetainExpansion = false;
2448 llvm::Optional<unsigned> NumExpansions
2449 = PackExpansion->getNumExpansions();
2450 if (SemaRef.CheckParameterPacksForExpansion(New->getLocation(),
2451 SourceRange(),
2452 Unexpanded,
2453 TemplateArgs,
2454 Expand,
2455 RetainExpansion,
2456 NumExpansions))
2457 break;
2458
2459 if (!Expand) {
2460 // We can't expand this pack expansion into separate arguments yet;
2461 // just substitute into the pattern and create a new pack expansion
2462 // type.
2463 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
2464 QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2465 TemplateArgs,
2466 New->getLocation(), New->getDeclName());
2467 if (T.isNull())
2468 break;
2469
2470 T = SemaRef.Context.getPackExpansionType(T, NumExpansions);
2471 Exceptions.push_back(T);
2472 continue;
2473 }
2474
2475 // Substitute into the pack expansion pattern for each template
2476 bool Invalid = false;
2477 for (unsigned ArgIdx = 0; ArgIdx != *NumExpansions; ++ArgIdx) {
2478 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, ArgIdx);
2479
2480 QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2481 TemplateArgs,
2482 New->getLocation(), New->getDeclName());
2483 if (T.isNull()) {
2484 Invalid = true;
2485 break;
2486 }
2487
2488 Exceptions.push_back(T);
2489 }
2490
2491 if (Invalid)
2492 break;
2493
2494 continue;
2495 }
2496
2497 QualType T
2498 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
2499 New->getLocation(), New->getDeclName());
2500 if (T.isNull() ||
2501 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
2502 continue;
2503
2504 Exceptions.push_back(T);
2505 }
2506 Expr *NoexceptExpr = 0;
2507 if (Expr *OldNoexceptExpr = Proto->getNoexceptExpr()) {
2508 EnterExpressionEvaluationContext Unevaluated(SemaRef,
2509 Sema::ConstantEvaluated);
2510 ExprResult E = SemaRef.SubstExpr(OldNoexceptExpr, TemplateArgs);
2511 if (E.isUsable())
2512 E = SemaRef.CheckBooleanCondition(E.get(), E.get()->getLocStart());
2513
2514 if (E.isUsable()) {
2515 NoexceptExpr = E.take();
2516 if (!NoexceptExpr->isTypeDependent() &&
2517 !NoexceptExpr->isValueDependent())
Douglas Gregorab41fe92012-05-04 22:38:52 +00002518 NoexceptExpr
2519 = SemaRef.VerifyIntegerConstantExpression(NoexceptExpr,
2520 0, diag::err_noexcept_needs_constant_expression,
2521 /*AllowFold*/ false).take();
Richard Smithe6975e92012-04-17 00:58:00 +00002522 }
2523 }
2524
2525 // Rebuild the function type
2526 const FunctionProtoType *NewProto
2527 = New->getType()->getAs<FunctionProtoType>();
2528 assert(NewProto && "Template instantiation without function prototype?");
2529
2530 FunctionProtoType::ExtProtoInfo EPI = NewProto->getExtProtoInfo();
2531 EPI.ExceptionSpecType = Proto->getExceptionSpecType();
2532 EPI.NumExceptions = Exceptions.size();
2533 EPI.Exceptions = Exceptions.data();
2534 EPI.NoexceptExpr = NoexceptExpr;
2535
2536 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
2537 NewProto->arg_type_begin(),
2538 NewProto->getNumArgs(),
2539 EPI));
2540}
2541
2542void Sema::InstantiateExceptionSpec(SourceLocation PointOfInstantiation,
2543 FunctionDecl *Decl) {
Richard Smith13bffc52012-04-19 00:08:28 +00002544 const FunctionProtoType *Proto = Decl->getType()->castAs<FunctionProtoType>();
2545 if (Proto->getExceptionSpecType() != EST_Uninstantiated)
Richard Smithe6975e92012-04-17 00:58:00 +00002546 return;
2547
2548 InstantiatingTemplate Inst(*this, PointOfInstantiation, Decl,
2549 InstantiatingTemplate::ExceptionSpecification());
Richard Smithb9d0b762012-07-27 04:22:15 +00002550 if (Inst) {
2551 // We hit the instantiation depth limit. Clear the exception specification
2552 // so that our callers don't have to cope with EST_Uninstantiated.
2553 FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
2554 EPI.ExceptionSpecType = EST_None;
2555 Decl->setType(Context.getFunctionType(Proto->getResultType(),
2556 Proto->arg_type_begin(),
2557 Proto->getNumArgs(),
2558 EPI));
Richard Smithe6975e92012-04-17 00:58:00 +00002559 return;
Richard Smithb9d0b762012-07-27 04:22:15 +00002560 }
Richard Smithe6975e92012-04-17 00:58:00 +00002561
2562 // Enter the scope of this instantiation. We don't use
2563 // PushDeclContext because we don't have a scope.
2564 Sema::ContextRAII savedContext(*this, Decl);
2565 LocalInstantiationScope Scope(*this);
2566
2567 MultiLevelTemplateArgumentList TemplateArgs =
2568 getTemplateInstantiationArgs(Decl, 0, /*RelativeToPrimary*/true);
2569
Richard Smith13bffc52012-04-19 00:08:28 +00002570 FunctionDecl *Template = Proto->getExceptionSpecTemplate();
2571 addInstantiatedParametersToScope(*this, Decl, Template, Scope, TemplateArgs);
Richard Smithe6975e92012-04-17 00:58:00 +00002572
Richard Smith13bffc52012-04-19 00:08:28 +00002573 ::InstantiateExceptionSpec(*this, Decl,
2574 Template->getType()->castAs<FunctionProtoType>(),
2575 TemplateArgs);
Richard Smithe6975e92012-04-17 00:58:00 +00002576}
2577
Mike Stump1eb44332009-09-09 15:08:12 +00002578/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00002579/// declaration (New) from the corresponding fields of its template (Tmpl).
2580///
2581/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00002582bool
2583TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00002584 FunctionDecl *Tmpl) {
David Blaikie85f485a2012-07-16 18:50:45 +00002585 if (Tmpl->isDeleted())
Sean Hunt10620eb2011-05-06 20:44:56 +00002586 New->setDeletedAsWritten();
Mike Stump1eb44332009-09-09 15:08:12 +00002587
Douglas Gregorcca9e962009-07-01 22:01:06 +00002588 // If we are performing substituting explicitly-specified template arguments
2589 // or deduced template arguments into a function template and we reach this
2590 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00002591 // to keeping the new function template specialization. We therefore
2592 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00002593 // into a template instantiation for this specific function template
2594 // specialization, which is not a SFINAE context, so that we diagnose any
2595 // further errors in the declaration itself.
2596 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
2597 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
2598 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
2599 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00002600 if (FunctionTemplateDecl *FunTmpl
Nick Lewycky4a9e60f2012-11-16 08:40:59 +00002601 = dyn_cast<FunctionTemplateDecl>(ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002602 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00002603 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00002604 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00002605 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
Nick Lewycky4a9e60f2012-11-16 08:40:59 +00002606 ActiveInst.Entity = New;
Douglas Gregorcca9e962009-07-01 22:01:06 +00002607 }
2608 }
Mike Stump1eb44332009-09-09 15:08:12 +00002609
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002610 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
2611 assert(Proto && "Function template without prototype?");
2612
Sebastian Redl60618fa2011-03-12 11:50:43 +00002613 if (Proto->hasExceptionSpec() || Proto->getNoReturnAttr()) {
John McCalle23cf432010-12-14 08:05:40 +00002614 FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
John McCalle23cf432010-12-14 08:05:40 +00002615
Richard Smithe6975e92012-04-17 00:58:00 +00002616 // DR1330: In C++11, defer instantiation of a non-trivial
2617 // exception specification.
Richard Smith80ad52f2013-01-02 11:42:31 +00002618 if (SemaRef.getLangOpts().CPlusPlus11 &&
Richard Smithe6975e92012-04-17 00:58:00 +00002619 EPI.ExceptionSpecType != EST_None &&
2620 EPI.ExceptionSpecType != EST_DynamicNone &&
2621 EPI.ExceptionSpecType != EST_BasicNoexcept) {
Richard Smith13bffc52012-04-19 00:08:28 +00002622 FunctionDecl *ExceptionSpecTemplate = Tmpl;
2623 if (EPI.ExceptionSpecType == EST_Uninstantiated)
2624 ExceptionSpecTemplate = EPI.ExceptionSpecTemplate;
Richard Smithb9d0b762012-07-27 04:22:15 +00002625 assert(EPI.ExceptionSpecType != EST_Unevaluated &&
2626 "instantiating implicitly-declared special member");
Richard Smith13bffc52012-04-19 00:08:28 +00002627
Richard Smithe6975e92012-04-17 00:58:00 +00002628 // Mark the function has having an uninstantiated exception specification.
2629 const FunctionProtoType *NewProto
2630 = New->getType()->getAs<FunctionProtoType>();
2631 assert(NewProto && "Template instantiation without function prototype?");
2632 EPI = NewProto->getExtProtoInfo();
2633 EPI.ExceptionSpecType = EST_Uninstantiated;
2634 EPI.ExceptionSpecDecl = New;
Richard Smith13bffc52012-04-19 00:08:28 +00002635 EPI.ExceptionSpecTemplate = ExceptionSpecTemplate;
Richard Smithe6975e92012-04-17 00:58:00 +00002636 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
2637 NewProto->arg_type_begin(),
2638 NewProto->getNumArgs(),
2639 EPI));
2640 } else {
2641 ::InstantiateExceptionSpec(SemaRef, New, Proto, TemplateArgs);
2642 }
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002643 }
2644
Rafael Espindola19f74ac2011-07-06 15:46:09 +00002645 // Get the definition. Leaves the variable unchanged if undefined.
Richard Smithe6975e92012-04-17 00:58:00 +00002646 const FunctionDecl *Definition = Tmpl;
Rafael Espindola19f74ac2011-07-06 15:46:09 +00002647 Tmpl->isDefined(Definition);
2648
DeLesley Hutchins23323e02012-01-20 22:50:54 +00002649 SemaRef.InstantiateAttrs(TemplateArgs, Definition, New,
2650 LateAttrs, StartingScope);
Douglas Gregor7cf84d62010-06-15 17:05:35 +00002651
Douglas Gregore53060f2009-06-25 22:08:12 +00002652 return false;
2653}
2654
Douglas Gregor5545e162009-03-24 00:38:23 +00002655/// \brief Initializes common fields of an instantiated method
2656/// declaration (New) from the corresponding fields of its template
2657/// (Tmpl).
2658///
2659/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00002660bool
2661TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00002662 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00002663 if (InitFunctionInstantiation(New, Tmpl))
2664 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002665
Douglas Gregor5545e162009-03-24 00:38:23 +00002666 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00002667 if (Tmpl->isVirtualAsWritten())
Douglas Gregor85606eb2010-09-28 20:50:54 +00002668 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +00002669
2670 // FIXME: attributes
2671 // FIXME: New needs a pointer to Tmpl
2672 return false;
2673}
Douglas Gregora58861f2009-05-13 20:28:22 +00002674
2675/// \brief Instantiate the definition of the given function from its
2676/// template.
2677///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002678/// \param PointOfInstantiation the point at which the instantiation was
2679/// required. Note that this is not precisely a "point of instantiation"
2680/// for the function, but it's close.
2681///
Douglas Gregora58861f2009-05-13 20:28:22 +00002682/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002683/// function template specialization or member function of a class template
2684/// specialization.
2685///
2686/// \param Recursive if true, recursively instantiates any functions that
2687/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002688///
2689/// \param DefinitionRequired if true, then we are performing an explicit
2690/// instantiation where the body of the function is required. Complain if
2691/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002692void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002693 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002694 bool Recursive,
2695 bool DefinitionRequired) {
Sean Hunt10620eb2011-05-06 20:44:56 +00002696 if (Function->isInvalidDecl() || Function->isDefined())
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002697 return;
2698
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002699 // Never instantiate an explicit specialization except if it is a class scope
2700 // explicit specialization.
2701 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization &&
2702 !Function->getClassScopeSpecializationPattern())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002703 return;
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002704
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002705 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00002706 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Sean Huntf996e052011-05-27 20:00:14 +00002707 assert(PatternDecl && "instantiating a non-template");
2708
2709 Stmt *Pattern = PatternDecl->getBody(PatternDecl);
2710 assert(PatternDecl && "template definition is not a template");
2711 if (!Pattern) {
2712 // Try to find a defaulted definition
2713 PatternDecl->isDefined(PatternDecl);
Sean Huntdfab8542011-05-25 22:02:25 +00002714 }
Sean Huntf996e052011-05-27 20:00:14 +00002715 assert(PatternDecl && "template definition is not a template");
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002716
Francois Pichet8387e2a2011-04-22 22:18:13 +00002717 // Postpone late parsed template instantiations.
Sean Huntf996e052011-05-27 20:00:14 +00002718 if (PatternDecl->isLateTemplateParsed() &&
Nick Lewycky8a29bc02011-05-12 03:51:24 +00002719 !LateTemplateParser) {
Francois Pichet8387e2a2011-04-22 22:18:13 +00002720 PendingInstantiations.push_back(
2721 std::make_pair(Function, PointOfInstantiation));
2722 return;
2723 }
2724
2725 // Call the LateTemplateParser callback if there a need to late parse
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002726 // a templated function definition.
Sean Huntf996e052011-05-27 20:00:14 +00002727 if (!Pattern && PatternDecl->isLateTemplateParsed() &&
Francois Pichet8387e2a2011-04-22 22:18:13 +00002728 LateTemplateParser) {
Francois Pichet4a47e8d2011-04-23 11:52:20 +00002729 LateTemplateParser(OpaqueParser, PatternDecl);
Francois Pichet8387e2a2011-04-22 22:18:13 +00002730 Pattern = PatternDecl->getBody(PatternDecl);
2731 }
2732
Sean Huntf996e052011-05-27 20:00:14 +00002733 if (!Pattern && !PatternDecl->isDefaulted()) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002734 if (DefinitionRequired) {
2735 if (Function->getPrimaryTemplate())
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002736 Diag(PointOfInstantiation,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002737 diag::err_explicit_instantiation_undefined_func_template)
2738 << Function->getPrimaryTemplate();
2739 else
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002740 Diag(PointOfInstantiation,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002741 diag::err_explicit_instantiation_undefined_member)
2742 << 1 << Function->getDeclName() << Function->getDeclContext();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002743
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002744 if (PatternDecl)
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002745 Diag(PatternDecl->getLocation(),
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002746 diag::note_explicit_instantiation_here);
Douglas Gregorcfe833b2010-05-17 17:57:54 +00002747 Function->setInvalidDecl();
Chandler Carruth58e390e2010-08-25 08:27:02 +00002748 } else if (Function->getTemplateSpecializationKind()
2749 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002750 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00002751 std::make_pair(Function, PointOfInstantiation));
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002752 }
Chandler Carruth58e390e2010-08-25 08:27:02 +00002753
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002754 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002755 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002756
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002757 // C++0x [temp.explicit]p9:
2758 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00002759 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002760 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00002761 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002762 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002763 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002764 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002765
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002766 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2767 if (Inst)
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002768 return;
2769
Abramo Bagnarae9946242011-11-18 08:08:52 +00002770 // Copy the inner loc start from the pattern.
2771 Function->setInnerLocStart(PatternDecl->getInnerLocStart());
2772
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002773 // If we're performing recursive template instantiation, create our own
2774 // queue of pending implicit instantiations that we will instantiate later,
2775 // while we're still within our own instantiation context.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002776 SmallVector<VTableUse, 16> SavedVTableUses;
Chandler Carruth62c78d52010-08-25 08:44:16 +00002777 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002778 if (Recursive) {
2779 VTableUses.swap(SavedVTableUses);
Chandler Carruth62c78d52010-08-25 08:44:16 +00002780 PendingInstantiations.swap(SavedPendingInstantiations);
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002781 }
Mike Stump1eb44332009-09-09 15:08:12 +00002782
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002783 EnterExpressionEvaluationContext EvalContext(*this,
John McCallf312b1e2010-08-26 23:41:50 +00002784 Sema::PotentiallyEvaluated);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002785
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002786 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00002787 // recorded, unless we're actually a member function within a local
2788 // class, in which case we need to merge our results with the parent
2789 // scope (of the enclosing function).
2790 bool MergeWithParentScope = false;
2791 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
2792 MergeWithParentScope = Rec->isLocalClass();
2793
2794 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002795
Richard Smith1d28caf2012-12-11 01:14:52 +00002796 if (PatternDecl->isDefaulted())
Sean Huntcd10dec2011-05-23 23:14:04 +00002797 SetDeclDefaulted(Function, PatternDecl->getLocation());
Richard Smith1d28caf2012-12-11 01:14:52 +00002798 else {
2799 ActOnStartOfFunctionDef(0, Function);
2800
2801 // Enter the scope of this instantiation. We don't use
2802 // PushDeclContext because we don't have a scope.
2803 Sema::ContextRAII savedContext(*this, Function);
2804
2805 MultiLevelTemplateArgumentList TemplateArgs =
2806 getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
2807
2808 addInstantiatedParametersToScope(*this, Function, PatternDecl, Scope,
2809 TemplateArgs);
2810
Sean Huntcd10dec2011-05-23 23:14:04 +00002811 // If this is a constructor, instantiate the member initializers.
2812 if (const CXXConstructorDecl *Ctor =
2813 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2814 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2815 TemplateArgs);
2816 }
2817
2818 // Instantiate the function body.
2819 StmtResult Body = SubstStmt(Pattern, TemplateArgs);
2820
2821 if (Body.isInvalid())
2822 Function->setInvalidDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002823
Sean Huntcd10dec2011-05-23 23:14:04 +00002824 ActOnFinishFunctionBody(Function, Body.get(),
2825 /*IsInstantiation=*/true);
Richard Smith1d28caf2012-12-11 01:14:52 +00002826
2827 PerformDependentDiagnostics(PatternDecl, TemplateArgs);
2828
2829 savedContext.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00002830 }
2831
Douglas Gregoraba43bb2009-05-26 20:50:29 +00002832 DeclGroupRef DG(Function);
2833 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00002834
Douglas Gregor60406be2010-01-16 22:29:39 +00002835 // This class may have local implicit instantiations that need to be
2836 // instantiation within this scope.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002837 PerformPendingInstantiations(/*LocalOnly=*/true);
Douglas Gregor60406be2010-01-16 22:29:39 +00002838 Scope.Exit();
2839
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002840 if (Recursive) {
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002841 // Define any pending vtables.
2842 DefineUsedVTables();
2843
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002844 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002845 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002846 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002847
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002848 // Restore the set of pending vtables.
Nick Lewycky81559102011-05-31 07:58:42 +00002849 assert(VTableUses.empty() &&
2850 "VTableUses should be empty before it is discarded.");
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002851 VTableUses.swap(SavedVTableUses);
2852
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002853 // Restore the set of pending implicit instantiations.
Nick Lewycky81559102011-05-31 07:58:42 +00002854 assert(PendingInstantiations.empty() &&
2855 "PendingInstantiations should be empty before it is discarded.");
Chandler Carruth62c78d52010-08-25 08:44:16 +00002856 PendingInstantiations.swap(SavedPendingInstantiations);
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002857 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002858}
2859
2860/// \brief Instantiate the definition of the given variable from its
2861/// template.
2862///
Douglas Gregor7caa6822009-07-24 20:34:43 +00002863/// \param PointOfInstantiation the point at which the instantiation was
2864/// required. Note that this is not precisely a "point of instantiation"
2865/// for the function, but it's close.
2866///
2867/// \param Var the already-instantiated declaration of a static member
2868/// variable of a class template specialization.
2869///
2870/// \param Recursive if true, recursively instantiates any functions that
2871/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002872///
2873/// \param DefinitionRequired if true, then we are performing an explicit
2874/// instantiation where an out-of-line definition of the member variable
2875/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002876void Sema::InstantiateStaticDataMemberDefinition(
2877 SourceLocation PointOfInstantiation,
2878 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002879 bool Recursive,
2880 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002881 if (Var->isInvalidDecl())
2882 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002883
Douglas Gregor7caa6822009-07-24 20:34:43 +00002884 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002885 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002886 assert(Def && "This data member was not instantiated from a template?");
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002887 assert(Def->isStaticDataMember() && "Not a static data member?");
Douglas Gregor0d035142009-10-27 18:42:08 +00002888 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002889
Douglas Gregor0d035142009-10-27 18:42:08 +00002890 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002891 // We did not find an out-of-line definition of this static data member,
2892 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00002893 // instantiate this definition (or provide a specialization for it) in
2894 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002895 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00002896 Def = Var->getInstantiatedFromStaticDataMember();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002897 Diag(PointOfInstantiation,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002898 diag::err_explicit_instantiation_undefined_member)
2899 << 2 << Var->getDeclName() << Var->getDeclContext();
2900 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
Chandler Carruth58e390e2010-08-25 08:27:02 +00002901 } else if (Var->getTemplateSpecializationKind()
2902 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002903 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00002904 std::make_pair(Var, PointOfInstantiation));
2905 }
2906
Douglas Gregor7caa6822009-07-24 20:34:43 +00002907 return;
2908 }
2909
Rafael Espindola234fe652012-03-05 10:54:55 +00002910 TemplateSpecializationKind TSK = Var->getTemplateSpecializationKind();
2911
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002912 // Never instantiate an explicit specialization.
Rafael Espindola234fe652012-03-05 10:54:55 +00002913 if (TSK == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002914 return;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002915
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002916 // C++0x [temp.explicit]p9:
2917 // Except for inline functions, other explicit instantiation declarations
2918 // have the effect of suppressing the implicit instantiation of the entity
2919 // to which they refer.
Rafael Espindola234fe652012-03-05 10:54:55 +00002920 if (TSK == TSK_ExplicitInstantiationDeclaration)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002921 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002922
Rafael Espindola02503932012-03-08 15:51:03 +00002923 Consumer.HandleCXXStaticMemberVarInstantiation(Var);
2924
Douglas Gregorf15748a2011-06-03 03:35:07 +00002925 // If we already have a definition, we're done.
Nick Lewycky95e38722012-04-04 02:38:36 +00002926 if (VarDecl *Def = Var->getDefinition()) {
2927 // We may be explicitly instantiating something we've already implicitly
2928 // instantiated.
2929 Def->setTemplateSpecializationKind(Var->getTemplateSpecializationKind(),
2930 PointOfInstantiation);
Douglas Gregorf15748a2011-06-03 03:35:07 +00002931 return;
Nick Lewycky95e38722012-04-04 02:38:36 +00002932 }
Douglas Gregorf15748a2011-06-03 03:35:07 +00002933
Douglas Gregor7caa6822009-07-24 20:34:43 +00002934 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
2935 if (Inst)
2936 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002937
Douglas Gregor7caa6822009-07-24 20:34:43 +00002938 // If we're performing recursive template instantiation, create our own
2939 // queue of pending implicit instantiations that we will instantiate later,
2940 // while we're still within our own instantiation context.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002941 SmallVector<VTableUse, 16> SavedVTableUses;
Chandler Carruth62c78d52010-08-25 08:44:16 +00002942 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Nick Lewycky81559102011-05-31 07:58:42 +00002943 if (Recursive) {
2944 VTableUses.swap(SavedVTableUses);
Chandler Carruth62c78d52010-08-25 08:44:16 +00002945 PendingInstantiations.swap(SavedPendingInstantiations);
Nick Lewycky81559102011-05-31 07:58:42 +00002946 }
Mike Stump1eb44332009-09-09 15:08:12 +00002947
Douglas Gregor7caa6822009-07-24 20:34:43 +00002948 // Enter the scope of this instantiation. We don't use
2949 // PushDeclContext because we don't have a scope.
John McCallf5ba7e02011-02-14 20:37:25 +00002950 ContextRAII previousContext(*this, Var->getDeclContext());
Douglas Gregor7bdc1522012-02-16 21:36:18 +00002951 LocalInstantiationScope Local(*this);
2952
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002953 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00002954 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Nico Weber6bb4dcb2010-11-28 22:53:37 +00002955 getTemplateInstantiationArgs(Var)));
John McCallf5ba7e02011-02-14 20:37:25 +00002956
2957 previousContext.pop();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002958
2959 if (Var) {
Douglas Gregor583f33b2009-10-15 18:07:02 +00002960 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2961 assert(MSInfo && "Missing member specialization information?");
2962 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2963 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00002964 DeclGroupRef DG(Var);
2965 Consumer.HandleTopLevelDecl(DG);
2966 }
Douglas Gregor7bdc1522012-02-16 21:36:18 +00002967 Local.Exit();
2968
Douglas Gregor7caa6822009-07-24 20:34:43 +00002969 if (Recursive) {
Nick Lewycky81559102011-05-31 07:58:42 +00002970 // Define any newly required vtables.
2971 DefineUsedVTables();
2972
Douglas Gregor7caa6822009-07-24 20:34:43 +00002973 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002974 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002975 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002976
Nick Lewycky81559102011-05-31 07:58:42 +00002977 // Restore the set of pending vtables.
2978 assert(VTableUses.empty() &&
2979 "VTableUses should be empty before it is discarded, "
2980 "while instantiating static data member.");
2981 VTableUses.swap(SavedVTableUses);
2982
Douglas Gregor7caa6822009-07-24 20:34:43 +00002983 // Restore the set of pending implicit instantiations.
Nick Lewycky81559102011-05-31 07:58:42 +00002984 assert(PendingInstantiations.empty() &&
2985 "PendingInstantiations should be empty before it is discarded, "
2986 "while instantiating static data member.");
Chandler Carruth62c78d52010-08-25 08:44:16 +00002987 PendingInstantiations.swap(SavedPendingInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002988 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002989}
Douglas Gregor815215d2009-05-27 05:35:12 +00002990
Anders Carlsson09025312009-08-29 05:16:22 +00002991void
2992Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
2993 const CXXConstructorDecl *Tmpl,
2994 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00002995
Richard Trieu90ab75b2011-09-09 03:18:59 +00002996 SmallVector<CXXCtorInitializer*, 4> NewInits;
Richard Smith54b3ba82012-09-25 00:23:05 +00002997 bool AnyErrors = Tmpl->isInvalidDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002998
Anders Carlsson09025312009-08-29 05:16:22 +00002999 // Instantiate all the initializers.
3000 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00003001 InitsEnd = Tmpl->init_end();
3002 Inits != InitsEnd; ++Inits) {
Sean Huntcbb67482011-01-08 20:30:50 +00003003 CXXCtorInitializer *Init = *Inits;
Anders Carlsson09025312009-08-29 05:16:22 +00003004
Chandler Carruth030ef472010-09-03 21:54:20 +00003005 // Only instantiate written initializers, let Sema re-construct implicit
3006 // ones.
3007 if (!Init->isWritten())
3008 continue;
3009
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003010 SourceLocation EllipsisLoc;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003011
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003012 if (Init->isPackExpansion()) {
3013 // This is a pack expansion. We should expand it now.
Douglas Gregor76852c22011-11-01 01:16:03 +00003014 TypeLoc BaseTL = Init->getTypeSourceInfo()->getTypeLoc();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003015 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003016 collectUnexpandedParameterPacks(BaseTL, Unexpanded);
3017 bool ShouldExpand = false;
Douglas Gregord3731192011-01-10 07:32:04 +00003018 bool RetainExpansion = false;
Douglas Gregorcded4f62011-01-14 17:04:44 +00003019 llvm::Optional<unsigned> NumExpansions;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003020 if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(),
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003021 BaseTL.getSourceRange(),
David Blaikiea71f9d02011-09-22 02:34:54 +00003022 Unexpanded,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003023 TemplateArgs, ShouldExpand,
Douglas Gregord3731192011-01-10 07:32:04 +00003024 RetainExpansion,
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003025 NumExpansions)) {
3026 AnyErrors = true;
3027 New->setInvalidDecl();
3028 continue;
3029 }
3030 assert(ShouldExpand && "Partial instantiation of base initializer?");
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003031
3032 // Loop over all of the arguments in the argument pack(s),
Douglas Gregorcded4f62011-01-14 17:04:44 +00003033 for (unsigned I = 0; I != *NumExpansions; ++I) {
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003034 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
3035
3036 // Instantiate the initializer.
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003037 ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs,
3038 /*CXXDirectInit=*/true);
3039 if (TempInit.isInvalid()) {
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003040 AnyErrors = true;
3041 break;
3042 }
3043
3044 // Instantiate the base type.
Douglas Gregor76852c22011-11-01 01:16:03 +00003045 TypeSourceInfo *BaseTInfo = SubstType(Init->getTypeSourceInfo(),
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003046 TemplateArgs,
3047 Init->getSourceLocation(),
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003048 New->getDeclName());
3049 if (!BaseTInfo) {
3050 AnyErrors = true;
3051 break;
3052 }
3053
3054 // Build the initializer.
Sebastian Redl6df65482011-09-24 17:48:25 +00003055 MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(),
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003056 BaseTInfo, TempInit.take(),
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003057 New->getParent(),
3058 SourceLocation());
3059 if (NewInit.isInvalid()) {
3060 AnyErrors = true;
3061 break;
3062 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003063
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003064 NewInits.push_back(NewInit.get());
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003065 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003066
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003067 continue;
3068 }
3069
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00003070 // Instantiate the initializer.
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003071 ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs,
3072 /*CXXDirectInit=*/true);
3073 if (TempInit.isInvalid()) {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00003074 AnyErrors = true;
3075 continue;
Anders Carlsson09025312009-08-29 05:16:22 +00003076 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003077
Anders Carlsson09025312009-08-29 05:16:22 +00003078 MemInitResult NewInit;
Douglas Gregor76852c22011-11-01 01:16:03 +00003079 if (Init->isDelegatingInitializer() || Init->isBaseInitializer()) {
3080 TypeSourceInfo *TInfo = SubstType(Init->getTypeSourceInfo(),
3081 TemplateArgs,
3082 Init->getSourceLocation(),
3083 New->getDeclName());
3084 if (!TInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00003085 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00003086 New->setInvalidDecl();
3087 continue;
3088 }
Sebastian Redl6df65482011-09-24 17:48:25 +00003089
Douglas Gregor76852c22011-11-01 01:16:03 +00003090 if (Init->isBaseInitializer())
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003091 NewInit = BuildBaseInitializer(TInfo->getType(), TInfo, TempInit.take(),
Douglas Gregor76852c22011-11-01 01:16:03 +00003092 New->getParent(), EllipsisLoc);
3093 else
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003094 NewInit = BuildDelegatingInitializer(TInfo, TempInit.take(),
Douglas Gregor76852c22011-11-01 01:16:03 +00003095 cast<CXXRecordDecl>(CurContext->getParent()));
Anders Carlsson09025312009-08-29 05:16:22 +00003096 } else if (Init->isMemberInitializer()) {
Douglas Gregorb7107222011-03-04 19:46:35 +00003097 FieldDecl *Member = cast_or_null<FieldDecl>(FindInstantiatedDecl(
Francois Pichet00eb3f92010-12-04 09:14:42 +00003098 Init->getMemberLocation(),
3099 Init->getMember(),
3100 TemplateArgs));
Douglas Gregorb7107222011-03-04 19:46:35 +00003101 if (!Member) {
3102 AnyErrors = true;
3103 New->setInvalidDecl();
3104 continue;
3105 }
Mike Stump1eb44332009-09-09 15:08:12 +00003106
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003107 NewInit = BuildMemberInitializer(Member, TempInit.take(),
Sebastian Redl6df65482011-09-24 17:48:25 +00003108 Init->getSourceLocation());
Francois Pichet00eb3f92010-12-04 09:14:42 +00003109 } else if (Init->isIndirectMemberInitializer()) {
3110 IndirectFieldDecl *IndirectMember =
Douglas Gregorb7107222011-03-04 19:46:35 +00003111 cast_or_null<IndirectFieldDecl>(FindInstantiatedDecl(
Francois Pichet00eb3f92010-12-04 09:14:42 +00003112 Init->getMemberLocation(),
3113 Init->getIndirectMember(), TemplateArgs));
3114
Douglas Gregorb7107222011-03-04 19:46:35 +00003115 if (!IndirectMember) {
3116 AnyErrors = true;
3117 New->setInvalidDecl();
Sebastian Redl6df65482011-09-24 17:48:25 +00003118 continue;
Douglas Gregorb7107222011-03-04 19:46:35 +00003119 }
Sebastian Redl6df65482011-09-24 17:48:25 +00003120
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003121 NewInit = BuildMemberInitializer(IndirectMember, TempInit.take(),
Sebastian Redl6df65482011-09-24 17:48:25 +00003122 Init->getSourceLocation());
Anders Carlsson09025312009-08-29 05:16:22 +00003123 }
3124
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00003125 if (NewInit.isInvalid()) {
3126 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00003127 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00003128 } else {
Richard Trieu90ab75b2011-09-09 03:18:59 +00003129 NewInits.push_back(NewInit.get());
Anders Carlsson09025312009-08-29 05:16:22 +00003130 }
3131 }
Mike Stump1eb44332009-09-09 15:08:12 +00003132
Anders Carlsson09025312009-08-29 05:16:22 +00003133 // Assign all the initializers to the new constructor.
John McCalld226f652010-08-21 09:40:31 +00003134 ActOnMemInitializers(New,
Anders Carlsson09025312009-08-29 05:16:22 +00003135 /*FIXME: ColonLoc */
3136 SourceLocation(),
David Blaikie93c86172013-01-17 05:26:25 +00003137 NewInits,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00003138 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00003139}
3140
John McCall52a575a2009-08-29 08:11:13 +00003141// TODO: this could be templated if the various decl types used the
3142// same method name.
3143static bool isInstantiationOf(ClassTemplateDecl *Pattern,
3144 ClassTemplateDecl *Instance) {
3145 Pattern = Pattern->getCanonicalDecl();
3146
3147 do {
3148 Instance = Instance->getCanonicalDecl();
3149 if (Pattern == Instance) return true;
3150 Instance = Instance->getInstantiatedFromMemberTemplate();
3151 } while (Instance);
3152
3153 return false;
3154}
3155
Douglas Gregor0d696532009-09-28 06:34:35 +00003156static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
3157 FunctionTemplateDecl *Instance) {
3158 Pattern = Pattern->getCanonicalDecl();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003159
Douglas Gregor0d696532009-09-28 06:34:35 +00003160 do {
3161 Instance = Instance->getCanonicalDecl();
3162 if (Pattern == Instance) return true;
3163 Instance = Instance->getInstantiatedFromMemberTemplate();
3164 } while (Instance);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003165
Douglas Gregor0d696532009-09-28 06:34:35 +00003166 return false;
3167}
3168
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003169static bool
Douglas Gregored9c0f92009-10-29 00:04:11 +00003170isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
3171 ClassTemplatePartialSpecializationDecl *Instance) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003172 Pattern
Douglas Gregored9c0f92009-10-29 00:04:11 +00003173 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
3174 do {
3175 Instance = cast<ClassTemplatePartialSpecializationDecl>(
3176 Instance->getCanonicalDecl());
3177 if (Pattern == Instance)
3178 return true;
3179 Instance = Instance->getInstantiatedFromMember();
3180 } while (Instance);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003181
Douglas Gregored9c0f92009-10-29 00:04:11 +00003182 return false;
3183}
3184
John McCall52a575a2009-08-29 08:11:13 +00003185static bool isInstantiationOf(CXXRecordDecl *Pattern,
3186 CXXRecordDecl *Instance) {
3187 Pattern = Pattern->getCanonicalDecl();
3188
3189 do {
3190 Instance = Instance->getCanonicalDecl();
3191 if (Pattern == Instance) return true;
3192 Instance = Instance->getInstantiatedFromMemberClass();
3193 } while (Instance);
3194
3195 return false;
3196}
3197
3198static bool isInstantiationOf(FunctionDecl *Pattern,
3199 FunctionDecl *Instance) {
3200 Pattern = Pattern->getCanonicalDecl();
3201
3202 do {
3203 Instance = Instance->getCanonicalDecl();
3204 if (Pattern == Instance) return true;
3205 Instance = Instance->getInstantiatedFromMemberFunction();
3206 } while (Instance);
3207
3208 return false;
3209}
3210
3211static bool isInstantiationOf(EnumDecl *Pattern,
3212 EnumDecl *Instance) {
3213 Pattern = Pattern->getCanonicalDecl();
3214
3215 do {
3216 Instance = Instance->getCanonicalDecl();
3217 if (Pattern == Instance) return true;
3218 Instance = Instance->getInstantiatedFromMemberEnum();
3219 } while (Instance);
3220
3221 return false;
3222}
3223
John McCalled976492009-12-04 22:46:56 +00003224static bool isInstantiationOf(UsingShadowDecl *Pattern,
3225 UsingShadowDecl *Instance,
3226 ASTContext &C) {
3227 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
3228}
3229
3230static bool isInstantiationOf(UsingDecl *Pattern,
3231 UsingDecl *Instance,
3232 ASTContext &C) {
3233 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
3234}
3235
John McCall7ba107a2009-11-18 02:36:19 +00003236static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
3237 UsingDecl *Instance,
3238 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00003239 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00003240}
3241
3242static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00003243 UsingDecl *Instance,
3244 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00003245 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00003246}
3247
John McCall52a575a2009-08-29 08:11:13 +00003248static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
3249 VarDecl *Instance) {
3250 assert(Instance->isStaticDataMember());
3251
3252 Pattern = Pattern->getCanonicalDecl();
3253
3254 do {
3255 Instance = Instance->getCanonicalDecl();
3256 if (Pattern == Instance) return true;
3257 Instance = Instance->getInstantiatedFromStaticDataMember();
3258 } while (Instance);
3259
3260 return false;
3261}
3262
John McCalled976492009-12-04 22:46:56 +00003263// Other is the prospective instantiation
3264// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00003265static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00003266 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00003267 if (UnresolvedUsingTypenameDecl *UUD
3268 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
3269 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
3270 return isInstantiationOf(UUD, UD, Ctx);
3271 }
3272 }
3273
3274 if (UnresolvedUsingValueDecl *UUD
3275 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00003276 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
3277 return isInstantiationOf(UUD, UD, Ctx);
3278 }
3279 }
Douglas Gregor815215d2009-05-27 05:35:12 +00003280
Anders Carlsson0d8df782009-08-29 19:37:28 +00003281 return false;
3282 }
Mike Stump1eb44332009-09-09 15:08:12 +00003283
John McCall52a575a2009-08-29 08:11:13 +00003284 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
3285 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00003286
John McCall52a575a2009-08-29 08:11:13 +00003287 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
3288 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00003289
John McCall52a575a2009-08-29 08:11:13 +00003290 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
3291 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00003292
Douglas Gregor7caa6822009-07-24 20:34:43 +00003293 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00003294 if (Var->isStaticDataMember())
3295 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
3296
3297 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
3298 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00003299
Douglas Gregor0d696532009-09-28 06:34:35 +00003300 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
3301 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
3302
Douglas Gregored9c0f92009-10-29 00:04:11 +00003303 if (ClassTemplatePartialSpecializationDecl *PartialSpec
3304 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
3305 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
3306 PartialSpec);
3307
Anders Carlssond8b285f2009-09-01 04:26:58 +00003308 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
3309 if (!Field->getDeclName()) {
3310 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00003311 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00003312 cast<FieldDecl>(D);
3313 }
3314 }
Mike Stump1eb44332009-09-09 15:08:12 +00003315
John McCalled976492009-12-04 22:46:56 +00003316 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
3317 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
3318
3319 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
3320 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
3321
Douglas Gregor815215d2009-05-27 05:35:12 +00003322 return D->getDeclName() && isa<NamedDecl>(Other) &&
3323 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
3324}
3325
3326template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00003327static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00003328 NamedDecl *D,
3329 ForwardIterator first,
3330 ForwardIterator last) {
3331 for (; first != last; ++first)
3332 if (isInstantiationOf(Ctx, D, *first))
3333 return cast<NamedDecl>(*first);
3334
3335 return 0;
3336}
3337
John McCall02cace72009-08-28 07:59:38 +00003338/// \brief Finds the instantiation of the given declaration context
3339/// within the current instantiation.
3340///
3341/// \returns NULL if there was an error
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003342DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
Douglas Gregore95b4092009-09-16 18:34:49 +00003343 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00003344 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003345 Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00003346 return cast_or_null<DeclContext>(ID);
3347 } else return DC;
3348}
3349
Douglas Gregored961e72009-05-27 17:54:46 +00003350/// \brief Find the instantiation of the given declaration within the
3351/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00003352///
3353/// This routine is intended to be used when \p D is a declaration
3354/// referenced from within a template, that needs to mapped into the
3355/// corresponding declaration within an instantiation. For example,
3356/// given:
3357///
3358/// \code
3359/// template<typename T>
3360/// struct X {
3361/// enum Kind {
3362/// KnownValue = sizeof(T)
3363/// };
3364///
3365/// bool getKind() const { return KnownValue; }
3366/// };
3367///
3368/// template struct X<int>;
3369/// \endcode
3370///
3371/// In the instantiation of X<int>::getKind(), we need to map the
3372/// EnumConstantDecl for KnownValue (which refers to
James Dennettf198d122012-06-17 03:36:08 +00003373/// X<T>::\<Kind>\::KnownValue) to its instantiation
James Dennettef2b5b32012-06-15 22:23:43 +00003374/// (X<int>::\<Kind>\::KnownValue). InstantiateCurrentDeclRef() performs
Douglas Gregored961e72009-05-27 17:54:46 +00003375/// this mapping from within the instantiation of X<int>.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003376NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
Douglas Gregore95b4092009-09-16 18:34:49 +00003377 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00003378 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00003379 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00003380 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
Douglas Gregor7bdc1522012-02-16 21:36:18 +00003381 (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext()) ||
3382 (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda())) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00003383 // D is a local of some kind. Look into the map of local
3384 // declarations to their instantiations.
Chris Lattnerd8e54992011-02-17 19:47:42 +00003385 typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
3386 llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found
3387 = CurrentInstantiationScope->findInstantiationOf(D);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003388
Chris Lattner57ad3782011-02-17 20:34:02 +00003389 if (Found) {
3390 if (Decl *FD = Found->dyn_cast<Decl *>())
3391 return cast<NamedDecl>(FD);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003392
Richard Smith9a4db032012-09-12 00:56:43 +00003393 int PackIdx = ArgumentPackSubstitutionIndex;
3394 assert(PackIdx != -1 && "found declaration pack but not pack expanding");
Chris Lattner57ad3782011-02-17 20:34:02 +00003395 return cast<NamedDecl>((*Found->get<DeclArgumentPack *>())[PackIdx]);
3396 }
3397
3398 // If we didn't find the decl, then we must have a label decl that hasn't
3399 // been found yet. Lazily instantiate it and return it now.
3400 assert(isa<LabelDecl>(D));
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003401
Chris Lattner57ad3782011-02-17 20:34:02 +00003402 Decl *Inst = SubstDecl(D, CurContext, TemplateArgs);
3403 assert(Inst && "Failed to instantiate label??");
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003404
Chris Lattner57ad3782011-02-17 20:34:02 +00003405 CurrentInstantiationScope->InstantiatedLocal(D, Inst);
3406 return cast<LabelDecl>(Inst);
Douglas Gregor2bba76b2009-05-27 17:07:49 +00003407 }
Douglas Gregor815215d2009-05-27 05:35:12 +00003408
Douglas Gregore95b4092009-09-16 18:34:49 +00003409 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
3410 if (!Record->isDependentContext())
3411 return D;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003412
Douglas Gregor2c1227c2011-11-07 17:43:18 +00003413 // Determine whether this record is the "templated" declaration describing
3414 // a class template or class template partial specialization.
Douglas Gregore95b4092009-09-16 18:34:49 +00003415 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
Douglas Gregor2c1227c2011-11-07 17:43:18 +00003416 if (ClassTemplate)
3417 ClassTemplate = ClassTemplate->getCanonicalDecl();
3418 else if (ClassTemplatePartialSpecializationDecl *PartialSpec
3419 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record))
3420 ClassTemplate = PartialSpec->getSpecializedTemplate()->getCanonicalDecl();
3421
3422 // Walk the current context to find either the record or an instantiation of
3423 // it.
3424 DeclContext *DC = CurContext;
3425 while (!DC->isFileContext()) {
3426 // If we're performing substitution while we're inside the template
3427 // definition, we'll find our own context. We're done.
3428 if (DC->Equals(Record))
3429 return Record;
3430
3431 if (CXXRecordDecl *InstRecord = dyn_cast<CXXRecordDecl>(DC)) {
3432 // Check whether we're in the process of instantiating a class template
3433 // specialization of the template we're mapping.
3434 if (ClassTemplateSpecializationDecl *InstSpec
3435 = dyn_cast<ClassTemplateSpecializationDecl>(InstRecord)){
3436 ClassTemplateDecl *SpecTemplate = InstSpec->getSpecializedTemplate();
3437 if (ClassTemplate && isInstantiationOf(ClassTemplate, SpecTemplate))
3438 return InstRecord;
3439 }
3440
3441 // Check whether we're in the process of instantiating a member class.
3442 if (isInstantiationOf(Record, InstRecord))
3443 return InstRecord;
Douglas Gregore95b4092009-09-16 18:34:49 +00003444 }
Douglas Gregor2c1227c2011-11-07 17:43:18 +00003445
3446
3447 // Move to the outer template scope.
3448 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) {
3449 if (FD->getFriendObjectKind() && FD->getDeclContext()->isFileContext()){
3450 DC = FD->getLexicalDeclContext();
3451 continue;
3452 }
John McCall52a575a2009-08-29 08:11:13 +00003453 }
Douglas Gregor2c1227c2011-11-07 17:43:18 +00003454
3455 DC = DC->getParent();
John McCall52a575a2009-08-29 08:11:13 +00003456 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00003457
Douglas Gregore95b4092009-09-16 18:34:49 +00003458 // Fall through to deal with other dependent record types (e.g.,
3459 // anonymous unions in class templates).
3460 }
John McCall52a575a2009-08-29 08:11:13 +00003461
Douglas Gregore95b4092009-09-16 18:34:49 +00003462 if (!ParentDC->isDependentContext())
3463 return D;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003464
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003465 ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00003466 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00003467 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003468
Douglas Gregor815215d2009-05-27 05:35:12 +00003469 if (ParentDC != D->getDeclContext()) {
3470 // We performed some kind of instantiation in the parent context,
3471 // so now we need to look into the instantiated parent context to
3472 // find the instantiation of the declaration D.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003473
John McCall3cb0ebd2010-03-10 03:28:59 +00003474 // If our context used to be dependent, we may need to instantiate
3475 // it before performing lookup into that context.
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00003476 bool IsBeingInstantiated = false;
John McCall3cb0ebd2010-03-10 03:28:59 +00003477 if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003478 if (!Spec->isDependentContext()) {
3479 QualType T = Context.getTypeDeclType(Spec);
John McCall3cb0ebd2010-03-10 03:28:59 +00003480 const RecordType *Tag = T->getAs<RecordType>();
3481 assert(Tag && "type of non-dependent record is not a RecordType");
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00003482 if (Tag->isBeingDefined())
3483 IsBeingInstantiated = true;
John McCall3cb0ebd2010-03-10 03:28:59 +00003484 if (!Tag->isBeingDefined() &&
3485 RequireCompleteType(Loc, T, diag::err_incomplete_type))
3486 return 0;
Douglas Gregora43064c2010-11-05 23:22:45 +00003487
3488 ParentDC = Tag->getDecl();
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003489 }
3490 }
3491
Douglas Gregor815215d2009-05-27 05:35:12 +00003492 NamedDecl *Result = 0;
3493 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003494 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
David Blaikie3bc93e32012-12-19 00:45:41 +00003495 Result = findInstantiationOf(Context, D, Found.begin(), Found.end());
Douglas Gregor815215d2009-05-27 05:35:12 +00003496 } else {
3497 // Since we don't have a name for the entity we're looking for,
3498 // our only option is to walk through all of the declarations to
3499 // find that name. This will occur in a few cases:
3500 //
3501 // - anonymous struct/union within a template
3502 // - unnamed class/struct/union/enum within a template
3503 //
3504 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00003505 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003506 ParentDC->decls_begin(),
3507 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00003508 }
Mike Stump1eb44332009-09-09 15:08:12 +00003509
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00003510 if (!Result) {
3511 if (isa<UsingShadowDecl>(D)) {
3512 // UsingShadowDecls can instantiate to nothing because of using hiding.
3513 } else if (Diags.hasErrorOccurred()) {
3514 // We've already complained about something, so most likely this
3515 // declaration failed to instantiate. There's no point in complaining
3516 // further, since this is normal in invalid code.
3517 } else if (IsBeingInstantiated) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003518 // The class in which this member exists is currently being
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00003519 // instantiated, and we haven't gotten around to instantiating this
3520 // member yet. This can happen when the code uses forward declarations
3521 // of member classes, and introduces ordering dependencies via
3522 // template instantiation.
3523 Diag(Loc, diag::err_member_not_yet_instantiated)
3524 << D->getDeclName()
3525 << Context.getTypeDeclType(cast<CXXRecordDecl>(ParentDC));
3526 Diag(D->getLocation(), diag::note_non_instantiated_member_here);
Richard Smith0724b7c2012-03-26 20:28:16 +00003527 } else if (EnumConstantDecl *ED = dyn_cast<EnumConstantDecl>(D)) {
3528 // This enumeration constant was found when the template was defined,
3529 // but can't be found in the instantiation. This can happen if an
3530 // unscoped enumeration member is explicitly specialized.
3531 EnumDecl *Enum = cast<EnumDecl>(ED->getLexicalDeclContext());
3532 EnumDecl *Spec = cast<EnumDecl>(FindInstantiatedDecl(Loc, Enum,
3533 TemplateArgs));
3534 assert(Spec->getTemplateSpecializationKind() ==
3535 TSK_ExplicitSpecialization);
3536 Diag(Loc, diag::err_enumerator_does_not_exist)
3537 << D->getDeclName()
3538 << Context.getTypeDeclType(cast<TypeDecl>(Spec->getDeclContext()));
3539 Diag(Spec->getLocation(), diag::note_enum_specialized_here)
3540 << Context.getTypeDeclType(Spec);
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00003541 } else {
3542 // We should have found something, but didn't.
3543 llvm_unreachable("Unable to find instantiation of declaration!");
3544 }
3545 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003546
Douglas Gregor815215d2009-05-27 05:35:12 +00003547 D = Result;
3548 }
3549
Douglas Gregor815215d2009-05-27 05:35:12 +00003550 return D;
3551}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003552
Mike Stump1eb44332009-09-09 15:08:12 +00003553/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003554/// instantiations we have seen until this point.
Nick Lewycky81559102011-05-31 07:58:42 +00003555void Sema::PerformPendingInstantiations(bool LocalOnly) {
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00003556 // Load pending instantiations from the external source.
3557 if (!LocalOnly && ExternalSource) {
Richard Smithb9d0b762012-07-27 04:22:15 +00003558 SmallVector<PendingImplicitInstantiation, 4> Pending;
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00003559 ExternalSource->ReadPendingInstantiations(Pending);
3560 PendingInstantiations.insert(PendingInstantiations.begin(),
3561 Pending.begin(), Pending.end());
3562 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00003563
Douglas Gregor60406be2010-01-16 22:29:39 +00003564 while (!PendingLocalImplicitInstantiations.empty() ||
Chandler Carruth62c78d52010-08-25 08:44:16 +00003565 (!LocalOnly && !PendingInstantiations.empty())) {
Douglas Gregor60406be2010-01-16 22:29:39 +00003566 PendingImplicitInstantiation Inst;
3567
3568 if (PendingLocalImplicitInstantiations.empty()) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00003569 Inst = PendingInstantiations.front();
3570 PendingInstantiations.pop_front();
Douglas Gregor60406be2010-01-16 22:29:39 +00003571 } else {
3572 Inst = PendingLocalImplicitInstantiations.front();
3573 PendingLocalImplicitInstantiations.pop_front();
3574 }
Mike Stump1eb44332009-09-09 15:08:12 +00003575
Douglas Gregor7caa6822009-07-24 20:34:43 +00003576 // Instantiate function definitions
3577 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
John McCallf312b1e2010-08-26 23:41:50 +00003578 PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(),
3579 "instantiating function definition");
Chandler Carruth58e390e2010-08-25 08:27:02 +00003580 bool DefinitionRequired = Function->getTemplateSpecializationKind() ==
3581 TSK_ExplicitInstantiationDefinition;
3582 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true,
3583 DefinitionRequired);
Douglas Gregor7caa6822009-07-24 20:34:43 +00003584 continue;
3585 }
Mike Stump1eb44332009-09-09 15:08:12 +00003586
Douglas Gregor7caa6822009-07-24 20:34:43 +00003587 // Instantiate static data member definitions.
3588 VarDecl *Var = cast<VarDecl>(Inst.first);
3589 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00003590
Chandler Carruth291b4412010-02-13 10:17:50 +00003591 // Don't try to instantiate declarations if the most recent redeclaration
3592 // is invalid.
Douglas Gregoref96ee02012-01-14 16:38:05 +00003593 if (Var->getMostRecentDecl()->isInvalidDecl())
Chandler Carruth291b4412010-02-13 10:17:50 +00003594 continue;
3595
3596 // Check if the most recent declaration has changed the specialization kind
3597 // and removed the need for implicit instantiation.
Douglas Gregoref96ee02012-01-14 16:38:05 +00003598 switch (Var->getMostRecentDecl()->getTemplateSpecializationKind()) {
Chandler Carruth291b4412010-02-13 10:17:50 +00003599 case TSK_Undeclared:
David Blaikieb219cfc2011-09-23 05:06:16 +00003600 llvm_unreachable("Cannot instantitiate an undeclared specialization.");
Chandler Carruth291b4412010-02-13 10:17:50 +00003601 case TSK_ExplicitInstantiationDeclaration:
Chandler Carruth291b4412010-02-13 10:17:50 +00003602 case TSK_ExplicitSpecialization:
Chandler Carruth58e390e2010-08-25 08:27:02 +00003603 continue; // No longer need to instantiate this type.
3604 case TSK_ExplicitInstantiationDefinition:
3605 // We only need an instantiation if the pending instantiation *is* the
3606 // explicit instantiation.
Douglas Gregoref96ee02012-01-14 16:38:05 +00003607 if (Var != Var->getMostRecentDecl()) continue;
Chandler Carruth291b4412010-02-13 10:17:50 +00003608 case TSK_ImplicitInstantiation:
3609 break;
3610 }
3611
John McCallf312b1e2010-08-26 23:41:50 +00003612 PrettyDeclStackTraceEntry CrashInfo(*this, Var, Var->getLocation(),
3613 "instantiating static data member "
3614 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00003615
Chandler Carruth58e390e2010-08-25 08:27:02 +00003616 bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
3617 TSK_ExplicitInstantiationDefinition;
3618 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true,
3619 DefinitionRequired);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003620 }
3621}
John McCall0c01d182010-03-24 05:22:00 +00003622
3623void Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
3624 const MultiLevelTemplateArgumentList &TemplateArgs) {
3625 for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
3626 E = Pattern->ddiag_end(); I != E; ++I) {
3627 DependentDiagnostic *DD = *I;
3628
3629 switch (DD->getKind()) {
3630 case DependentDiagnostic::Access:
3631 HandleDependentAccessCheck(*DD, TemplateArgs);
3632 break;
3633 }
3634 }
3635}