blob: 3a40b6fd77a78ecb9820d2a285bf8e90cd232258 [file] [log] [blame]
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001//===--- SemaTemplateInstantiateDecl.cpp - C++ Template Decl Instantiation ===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//
9// This file implements C++ template instantiation for declarations.
10//
11//===----------------------------------------------------------------------===/
John McCall2d887082010-08-25 22:03:47 +000012#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000013#include "clang/Sema/Lookup.h"
John McCallf312b1e2010-08-26 23:41:50 +000014#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall7cd088e2010-08-24 07:21:54 +000015#include "clang/Sema/Template.h"
Douglas Gregoraba43bb2009-05-26 20:50:29 +000016#include "clang/AST/ASTConsumer.h"
Douglas Gregor8dbc2692009-03-17 21:15:40 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/DeclTemplate.h"
19#include "clang/AST/DeclVisitor.h"
John McCall0c01d182010-03-24 05:22:00 +000020#include "clang/AST/DependentDiagnostic.h"
Douglas Gregor8dbc2692009-03-17 21:15:40 +000021#include "clang/AST/Expr.h"
Douglas Gregora88cfbf2009-12-12 18:16:41 +000022#include "clang/AST/ExprCXX.h"
John McCall21ef0fa2010-03-11 09:03:00 +000023#include "clang/AST/TypeLoc.h"
Douglas Gregor83ddad32009-08-26 21:14:46 +000024#include "clang/Lex/Preprocessor.h"
Douglas Gregor8dbc2692009-03-17 21:15:40 +000025
26using namespace clang;
27
John McCallb6217662010-03-15 10:12:16 +000028bool TemplateDeclInstantiator::SubstQualifier(const DeclaratorDecl *OldDecl,
29 DeclaratorDecl *NewDecl) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +000030 if (!OldDecl->getQualifierLoc())
31 return false;
32
33 NestedNameSpecifierLoc NewQualifierLoc
34 = SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(),
35 TemplateArgs);
36
37 if (!NewQualifierLoc)
John McCallb6217662010-03-15 10:12:16 +000038 return true;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +000039
40 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;
48
49 NestedNameSpecifierLoc NewQualifierLoc
50 = SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(),
51 TemplateArgs);
52
53 if (!NewQualifierLoc)
John McCallb6217662010-03-15 10:12:16 +000054 return true;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +000055
56 NewDecl->setQualifierInfo(NewQualifierLoc);
John McCallb6217662010-03-15 10:12:16 +000057 return false;
58}
59
Chandler Carruth4ced79f2010-06-25 03:22:07 +000060// FIXME: Is this still too simple?
John McCall1d8d1cc2010-08-01 02:01:53 +000061void Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs,
62 Decl *Tmpl, Decl *New) {
Sean Huntcf807c42010-08-18 23:23:40 +000063 for (AttrVec::const_iterator i = Tmpl->attr_begin(), e = Tmpl->attr_end();
64 i != e; ++i) {
65 const Attr *TmplAttr = *i;
Chandler Carruth4ced79f2010-06-25 03:22:07 +000066 // FIXME: This should be generalized to more than just the AlignedAttr.
67 if (const AlignedAttr *Aligned = dyn_cast<AlignedAttr>(TmplAttr)) {
Sean Huntcf807c42010-08-18 23:23:40 +000068 if (Aligned->isAlignmentDependent()) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +000069 // The alignment expression is not potentially evaluated.
John McCall1d8d1cc2010-08-01 02:01:53 +000070 EnterExpressionEvaluationContext Unevaluated(*this,
John McCallf312b1e2010-08-26 23:41:50 +000071 Sema::Unevaluated);
Chandler Carruth4ced79f2010-06-25 03:22:07 +000072
Sean Huntcf807c42010-08-18 23:23:40 +000073 if (Aligned->isAlignmentExpr()) {
John McCall60d7b3a2010-08-24 06:29:42 +000074 ExprResult Result = SubstExpr(Aligned->getAlignmentExpr(),
Nick Lewycky7663f392010-11-20 01:29:55 +000075 TemplateArgs);
Sean Huntcf807c42010-08-18 23:23:40 +000076 if (!Result.isInvalid())
77 AddAlignedAttr(Aligned->getLocation(), New, Result.takeAs<Expr>());
78 }
79 else {
80 TypeSourceInfo *Result = SubstType(Aligned->getAlignmentType(),
Nick Lewycky7663f392010-11-20 01:29:55 +000081 TemplateArgs,
82 Aligned->getLocation(),
83 DeclarationName());
Sean Huntcf807c42010-08-18 23:23:40 +000084 if (Result)
85 AddAlignedAttr(Aligned->getLocation(), New, Result);
86 }
Chandler Carruth4ced79f2010-06-25 03:22:07 +000087 continue;
88 }
89 }
90
Anders Carlssond8fe2d52009-11-07 06:07:58 +000091 // FIXME: Is cloning correct for all attributes?
John McCall1d8d1cc2010-08-01 02:01:53 +000092 Attr *NewAttr = TmplAttr->clone(Context);
Anders Carlssond8fe2d52009-11-07 06:07:58 +000093 New->addAttr(NewAttr);
94 }
95}
96
Douglas Gregor4f722be2009-03-25 15:45:12 +000097Decl *
98TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
99 assert(false && "Translation units cannot be instantiated");
100 return D;
101}
102
103Decl *
Chris Lattner57ad3782011-02-17 20:34:02 +0000104TemplateDeclInstantiator::VisitLabelDecl(LabelDecl *D) {
105 LabelDecl *Inst = LabelDecl::Create(SemaRef.Context, Owner, D->getLocation(),
106 D->getIdentifier());
107 Owner->addDecl(Inst);
108 return Inst;
109}
110
111Decl *
Douglas Gregor4f722be2009-03-25 15:45:12 +0000112TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
113 assert(false && "Namespaces cannot be instantiated");
114 return D;
115}
116
John McCall3dbd3d52010-02-16 06:53:13 +0000117Decl *
118TemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
119 NamespaceAliasDecl *Inst
120 = NamespaceAliasDecl::Create(SemaRef.Context, Owner,
121 D->getNamespaceLoc(),
122 D->getAliasLoc(),
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +0000123 D->getIdentifier(),
124 D->getQualifierLoc(),
John McCall3dbd3d52010-02-16 06:53:13 +0000125 D->getTargetNameLoc(),
126 D->getNamespace());
127 Owner->addDecl(Inst);
128 return Inst;
129}
130
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000131Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
132 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000133 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor836adf62010-05-24 17:22:01 +0000134 if (DI->getType()->isDependentType() ||
135 DI->getType()->isVariablyModifiedType()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000136 DI = SemaRef.SubstType(DI, TemplateArgs,
137 D->getLocation(), D->getDeclName());
138 if (!DI) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000139 Invalid = true;
John McCalla93c9342009-12-07 02:54:59 +0000140 DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000141 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000142 } else {
143 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000144 }
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000146 // Create the new typedef
147 TypedefDecl *Typedef
148 = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
John McCallba6a9bd2009-10-24 08:00:42 +0000149 D->getIdentifier(), DI);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000150 if (Invalid)
151 Typedef->setInvalidDecl();
152
John McCallcde5a402011-02-01 08:20:08 +0000153 // If the old typedef was the name for linkage purposes of an anonymous
154 // tag decl, re-establish that relationship for the new typedef.
155 if (const TagType *oldTagType = D->getUnderlyingType()->getAs<TagType>()) {
156 TagDecl *oldTag = oldTagType->getDecl();
157 if (oldTag->getTypedefForAnonDecl() == D) {
158 TagDecl *newTag = DI->getType()->castAs<TagType>()->getDecl();
159 assert(!newTag->getIdentifier() && !newTag->getTypedefForAnonDecl());
160 newTag->setTypedefForAnonDecl(Typedef);
161 }
Douglas Gregord57a38e2010-04-23 16:25:07 +0000162 }
163
John McCall5126fd02009-12-30 00:31:22 +0000164 if (TypedefDecl *Prev = D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000165 NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
166 TemplateArgs);
John McCall5126fd02009-12-30 00:31:22 +0000167 Typedef->setPreviousDeclaration(cast<TypedefDecl>(InstPrev));
168 }
169
John McCall1d8d1cc2010-08-01 02:01:53 +0000170 SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef);
Douglas Gregord57a38e2010-04-23 16:25:07 +0000171
John McCall46460a62010-01-20 21:53:11 +0000172 Typedef->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000173 Owner->addDecl(Typedef);
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000175 return Typedef;
176}
177
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000178/// \brief Instantiate an initializer, breaking it into separate
179/// initialization arguments.
180///
181/// \param S The semantic analysis object.
182///
183/// \param Init The initializer to instantiate.
184///
185/// \param TemplateArgs Template arguments to be substituted into the
186/// initializer.
187///
188/// \param NewArgs Will be filled in with the instantiation arguments.
189///
190/// \returns true if an error occurred, false otherwise
191static bool InstantiateInitializer(Sema &S, Expr *Init,
192 const MultiLevelTemplateArgumentList &TemplateArgs,
193 SourceLocation &LParenLoc,
Nick Lewycky7663f392010-11-20 01:29:55 +0000194 ASTOwningVector<Expr*> &NewArgs,
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000195 SourceLocation &RParenLoc) {
196 NewArgs.clear();
197 LParenLoc = SourceLocation();
198 RParenLoc = SourceLocation();
199
200 if (!Init)
201 return false;
202
John McCall4765fa02010-12-06 08:20:24 +0000203 if (ExprWithCleanups *ExprTemp = dyn_cast<ExprWithCleanups>(Init))
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000204 Init = ExprTemp->getSubExpr();
205
206 while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init))
207 Init = Binder->getSubExpr();
208
209 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init))
210 Init = ICE->getSubExprAsWritten();
211
212 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
213 LParenLoc = ParenList->getLParenLoc();
214 RParenLoc = ParenList->getRParenLoc();
Douglas Gregor91fc73e2011-01-07 19:35:17 +0000215 return S.SubstExprs(ParenList->getExprs(), ParenList->getNumExprs(),
216 true, TemplateArgs, NewArgs);
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000217 }
218
219 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) {
Douglas Gregor28329e52010-03-24 21:22:47 +0000220 if (!isa<CXXTemporaryObjectExpr>(Construct)) {
Douglas Gregor91fc73e2011-01-07 19:35:17 +0000221 if (S.SubstExprs(Construct->getArgs(), Construct->getNumArgs(), true,
222 TemplateArgs, NewArgs))
Douglas Gregor28329e52010-03-24 21:22:47 +0000223 return true;
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000224
Douglas Gregor28329e52010-03-24 21:22:47 +0000225 // FIXME: Fake locations!
226 LParenLoc = S.PP.getLocForEndOfToken(Init->getLocStart());
Douglas Gregora1a04782010-09-09 16:33:13 +0000227 RParenLoc = LParenLoc;
Douglas Gregor28329e52010-03-24 21:22:47 +0000228 return false;
229 }
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000230 }
231
John McCall60d7b3a2010-08-24 06:29:42 +0000232 ExprResult Result = S.SubstExpr(Init, TemplateArgs);
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000233 if (Result.isInvalid())
234 return true;
235
236 NewArgs.push_back(Result.takeAs<Expr>());
237 return false;
238}
239
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000240Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
Douglas Gregor9901c572010-05-21 00:31:19 +0000241 // If this is the variable for an anonymous struct or union,
242 // instantiate the anonymous struct/union type first.
243 if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
244 if (RecordTy->getDecl()->isAnonymousStructOrUnion())
245 if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
246 return 0;
247
John McCallce3ff2b2009-08-25 22:02:44 +0000248 // Do substitution on the type of the declaration
John McCalla93c9342009-12-07 02:54:59 +0000249 TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
John McCall0a5fa062009-10-21 02:39:02 +0000250 TemplateArgs,
251 D->getTypeSpecStartLoc(),
252 D->getDeclName());
253 if (!DI)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000254 return 0;
255
Douglas Gregorc6dbc3f2010-09-12 07:37:24 +0000256 if (DI->getType()->isFunctionType()) {
257 SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function)
258 << D->isStaticDataMember() << DI->getType();
259 return 0;
260 }
261
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000262 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000263 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
264 D->getLocation(), D->getIdentifier(),
John McCall0a5fa062009-10-21 02:39:02 +0000265 DI->getType(), DI,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000266 D->getStorageClass(),
267 D->getStorageClassAsWritten());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000268 Var->setThreadSpecified(D->isThreadSpecified());
269 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
Mike Stump1eb44332009-09-09 15:08:12 +0000270
John McCallb6217662010-03-15 10:12:16 +0000271 // Substitute the nested name specifier, if any.
272 if (SubstQualifier(D, Var))
273 return 0;
274
Mike Stump1eb44332009-09-09 15:08:12 +0000275 // If we are instantiating a static data member defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000276 // out-of-line, the instantiation will have the same lexical
277 // context (which will be a namespace scope) as the template.
278 if (D->isOutOfLine())
279 Var->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000280
John McCall46460a62010-01-20 21:53:11 +0000281 Var->setAccess(D->getAccess());
Douglas Gregorc070cc62010-06-17 23:14:26 +0000282
283 if (!D->isStaticDataMember())
284 Var->setUsed(D->isUsed(false));
Douglas Gregor4469e8a2010-05-19 17:02:24 +0000285
Mike Stump390b4cc2009-05-16 07:39:55 +0000286 // FIXME: In theory, we could have a previous declaration for variables that
287 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000288 bool Redeclaration = false;
John McCall68263142009-11-18 22:49:29 +0000289 // FIXME: having to fake up a LookupResult is dumb.
290 LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
Douglas Gregor449d0a82010-03-01 19:11:54 +0000291 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Douglas Gregor60c93c92010-02-09 07:26:29 +0000292 if (D->isStaticDataMember())
293 SemaRef.LookupQualifiedName(Previous, Owner, false);
John McCall68263142009-11-18 22:49:29 +0000294 SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000295
Douglas Gregor7caa6822009-07-24 20:34:43 +0000296 if (D->isOutOfLine()) {
Abramo Bagnaraea7b4882010-06-04 09:35:39 +0000297 if (!D->isStaticDataMember())
298 D->getLexicalDeclContext()->addDecl(Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000299 Owner->makeDeclVisibleInContext(Var);
300 } else {
301 Owner->addDecl(Var);
Douglas Gregorf7d72f52010-05-03 20:22:41 +0000302 if (Owner->isFunctionOrMethod())
303 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000304 }
John McCall1d8d1cc2010-08-01 02:01:53 +0000305 SemaRef.InstantiateAttrs(TemplateArgs, D, Var);
Fariborz Jahanian8dd0c562010-07-13 00:16:40 +0000306
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000307 // Link instantiations of static data members back to the template from
308 // which they were instantiated.
309 if (Var->isStaticDataMember())
310 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000311 TSK_ImplicitInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000312
Douglas Gregor60c93c92010-02-09 07:26:29 +0000313 if (Var->getAnyInitializer()) {
314 // We already have an initializer in the class.
315 } else if (D->getInit()) {
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000316 if (Var->isStaticDataMember() && !D->isOutOfLine())
317 SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
318 else
319 SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
320
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000321 // Instantiate the initializer.
322 SourceLocation LParenLoc, RParenLoc;
John McCallca0408f2010-08-23 06:44:23 +0000323 ASTOwningVector<Expr*> InitArgs(SemaRef);
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000324 if (!InstantiateInitializer(SemaRef, D->getInit(), TemplateArgs, LParenLoc,
Douglas Gregora1a04782010-09-09 16:33:13 +0000325 InitArgs, RParenLoc)) {
Richard Smith34b41d92011-02-20 03:19:35 +0000326 bool TypeMayContainAuto = true;
Douglas Gregor07a77b42011-01-14 17:12:22 +0000327 // Attach the initializer to the declaration, if we have one.
328 if (InitArgs.size() == 0)
Richard Smith34b41d92011-02-20 03:19:35 +0000329 SemaRef.ActOnUninitializedDecl(Var, TypeMayContainAuto);
Douglas Gregor07a77b42011-01-14 17:12:22 +0000330 else if (D->hasCXXDirectInitializer()) {
Douglas Gregor6eef5192009-12-14 19:27:10 +0000331 // Add the direct initializer to the declaration.
John McCalld226f652010-08-21 09:40:31 +0000332 SemaRef.AddCXXDirectInitializerToDecl(Var,
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000333 LParenLoc,
Douglas Gregor6eef5192009-12-14 19:27:10 +0000334 move_arg(InitArgs),
Richard Smith34b41d92011-02-20 03:19:35 +0000335 RParenLoc,
336 TypeMayContainAuto);
Douglas Gregor07a77b42011-01-14 17:12:22 +0000337 } else {
338 assert(InitArgs.size() == 1);
John McCall9ae2f072010-08-23 23:25:46 +0000339 Expr *Init = InitArgs.take()[0];
Richard Smith34b41d92011-02-20 03:19:35 +0000340 SemaRef.AddInitializerToDecl(Var, Init, false, TypeMayContainAuto);
Douglas Gregor6eef5192009-12-14 19:27:10 +0000341 }
Douglas Gregor6eef5192009-12-14 19:27:10 +0000342 } else {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000343 // FIXME: Not too happy about invalidating the declaration
344 // because of a bogus initializer.
345 Var->setInvalidDecl();
Douglas Gregor6eef5192009-12-14 19:27:10 +0000346 }
347
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000348 SemaRef.PopExpressionEvaluationContext();
Douglas Gregor65b90052009-07-27 17:43:39 +0000349 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
John McCalld226f652010-08-21 09:40:31 +0000350 SemaRef.ActOnUninitializedDecl(Var, false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000351
Douglas Gregor5764f612010-05-08 23:05:03 +0000352 // Diagnose unused local variables.
353 if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed())
354 SemaRef.DiagnoseUnusedDecl(Var);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +0000355
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000356 return Var;
357}
358
Abramo Bagnara6206d532010-06-05 05:09:32 +0000359Decl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
360 AccessSpecDecl* AD
361 = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
362 D->getAccessSpecifierLoc(), D->getColonLoc());
363 Owner->addHiddenDecl(AD);
364 return AD;
365}
366
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000367Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
368 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000369 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor836adf62010-05-24 17:22:01 +0000370 if (DI->getType()->isDependentType() ||
371 DI->getType()->isVariablyModifiedType()) {
John McCall07fb6be2009-10-22 23:33:21 +0000372 DI = SemaRef.SubstType(DI, TemplateArgs,
373 D->getLocation(), D->getDeclName());
374 if (!DI) {
John McCalla93c9342009-12-07 02:54:59 +0000375 DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000376 Invalid = true;
377 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000378 // C++ [temp.arg.type]p3:
379 // If a declaration acquires a function type through a type
380 // dependent on a template-parameter and this causes a
381 // declaration that does not use the syntactic form of a
382 // function declarator to have function type, the program is
383 // ill-formed.
384 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000385 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000386 Invalid = true;
387 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000388 } else {
389 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000390 }
391
392 Expr *BitWidth = D->getBitWidth();
393 if (Invalid)
394 BitWidth = 0;
395 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000396 // The bit-width expression is not potentially evaluated.
John McCallf312b1e2010-08-26 23:41:50 +0000397 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000398
John McCall60d7b3a2010-08-24 06:29:42 +0000399 ExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000400 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000401 if (InstantiatedBitWidth.isInvalid()) {
402 Invalid = true;
403 BitWidth = 0;
404 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000405 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000406 }
407
John McCall07fb6be2009-10-22 23:33:21 +0000408 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
409 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000410 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000411 D->getLocation(),
412 D->isMutable(),
413 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000414 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000415 D->getAccess(),
416 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000417 if (!Field) {
418 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000419 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000420 }
Mike Stump1eb44332009-09-09 15:08:12 +0000421
John McCall1d8d1cc2010-08-01 02:01:53 +0000422 SemaRef.InstantiateAttrs(TemplateArgs, D, Field);
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000423
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000424 if (Invalid)
425 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000427 if (!Field->getDeclName()) {
428 // Keep track of where this decl came from.
429 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor9901c572010-05-21 00:31:19 +0000430 }
431 if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
432 if (Parent->isAnonymousStructOrUnion() &&
Sebastian Redl7a126a42010-08-31 00:36:30 +0000433 Parent->getRedeclContext()->isFunctionOrMethod())
Douglas Gregor9901c572010-05-21 00:31:19 +0000434 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000435 }
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000437 Field->setImplicit(D->isImplicit());
John McCall46460a62010-01-20 21:53:11 +0000438 Field->setAccess(D->getAccess());
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000439 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000440
441 return Field;
442}
443
Francois Pichet87c2e122010-11-21 06:08:52 +0000444Decl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
445 NamedDecl **NamedChain =
446 new (SemaRef.Context)NamedDecl*[D->getChainingSize()];
447
448 int i = 0;
449 for (IndirectFieldDecl::chain_iterator PI =
450 D->chain_begin(), PE = D->chain_end();
451 PI != PE; ++PI)
452 NamedChain[i++] = (SemaRef.FindInstantiatedDecl(D->getLocation(),
453 *PI, TemplateArgs));
454
Francois Pichet40e17752010-12-09 10:07:54 +0000455 QualType T = cast<FieldDecl>(NamedChain[i-1])->getType();
Francois Pichet87c2e122010-11-21 06:08:52 +0000456 IndirectFieldDecl* IndirectField
457 = IndirectFieldDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Francois Pichet40e17752010-12-09 10:07:54 +0000458 D->getIdentifier(), T,
Francois Pichet87c2e122010-11-21 06:08:52 +0000459 NamedChain, D->getChainingSize());
460
461
462 IndirectField->setImplicit(D->isImplicit());
463 IndirectField->setAccess(D->getAccess());
464 Owner->addDecl(IndirectField);
465 return IndirectField;
466}
467
John McCall02cace72009-08-28 07:59:38 +0000468Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
John McCall02cace72009-08-28 07:59:38 +0000469 // Handle friend type expressions by simply substituting template
Douglas Gregor06245bf2010-04-07 17:57:12 +0000470 // parameters into the pattern type and checking the result.
John McCall32f2fb52010-03-25 18:04:51 +0000471 if (TypeSourceInfo *Ty = D->getFriendType()) {
472 TypeSourceInfo *InstTy =
473 SemaRef.SubstType(Ty, TemplateArgs,
474 D->getLocation(), DeclarationName());
Douglas Gregor06245bf2010-04-07 17:57:12 +0000475 if (!InstTy)
Douglas Gregor7557a132009-12-24 20:56:24 +0000476 return 0;
John McCall02cace72009-08-28 07:59:38 +0000477
Douglas Gregor06245bf2010-04-07 17:57:12 +0000478 FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getFriendLoc(), InstTy);
479 if (!FD)
480 return 0;
481
482 FD->setAccess(AS_public);
John McCall9a34edb2010-10-19 01:40:49 +0000483 FD->setUnsupportedFriend(D->isUnsupportedFriend());
Douglas Gregor06245bf2010-04-07 17:57:12 +0000484 Owner->addDecl(FD);
485 return FD;
486 }
487
488 NamedDecl *ND = D->getFriendDecl();
489 assert(ND && "friend decl must be a decl or a type!");
490
John McCallaf2094e2010-04-08 09:05:18 +0000491 // All of the Visit implementations for the various potential friend
492 // declarations have to be carefully written to work for friend
493 // objects, with the most important detail being that the target
494 // decl should almost certainly not be placed in Owner.
495 Decl *NewND = Visit(ND);
Douglas Gregor06245bf2010-04-07 17:57:12 +0000496 if (!NewND) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000497
John McCall02cace72009-08-28 07:59:38 +0000498 FriendDecl *FD =
Douglas Gregor06245bf2010-04-07 17:57:12 +0000499 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
500 cast<NamedDecl>(NewND), D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000501 FD->setAccess(AS_public);
John McCall9a34edb2010-10-19 01:40:49 +0000502 FD->setUnsupportedFriend(D->isUnsupportedFriend());
John McCall02cace72009-08-28 07:59:38 +0000503 Owner->addDecl(FD);
504 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000505}
506
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000507Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
508 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Douglas Gregorac7610d2009-06-22 20:57:11 +0000510 // The expression in a static assertion is not potentially evaluated.
John McCallf312b1e2010-08-26 23:41:50 +0000511 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000512
John McCall60d7b3a2010-08-24 06:29:42 +0000513 ExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000514 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000515 if (InstantiatedAssertExpr.isInvalid())
516 return 0;
517
John McCall60d7b3a2010-08-24 06:29:42 +0000518 ExprResult Message(D->getMessage());
John McCall3fa5cae2010-10-26 07:05:15 +0000519 D->getMessage();
John McCalld226f652010-08-21 09:40:31 +0000520 return SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
John McCall9ae2f072010-08-23 23:25:46 +0000521 InstantiatedAssertExpr.get(),
522 Message.get());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000523}
524
525Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000526 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000527 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000528 D->getTagKeywordLoc(),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +0000529 /*PrevDecl=*/0, D->isScoped(),
530 D->isScopedUsingClassTag(), D->isFixed());
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000531 if (D->isFixed()) {
532 if (TypeSourceInfo* TI = D->getIntegerTypeSourceInfo()) {
533 // If we have type source information for the underlying type, it means it
534 // has been explicitly set by the user. Perform substitution on it before
535 // moving on.
536 SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
537 Enum->setIntegerTypeSourceInfo(SemaRef.SubstType(TI,
538 TemplateArgs,
539 UnderlyingLoc,
540 DeclarationName()));
541
542 if (!Enum->getIntegerTypeSourceInfo())
543 Enum->setIntegerType(SemaRef.Context.IntTy);
544 }
545 else {
546 assert(!D->getIntegerType()->isDependentType()
547 && "Dependent type without type source info");
548 Enum->setIntegerType(D->getIntegerType());
549 }
550 }
551
John McCall5b629aa2010-10-22 23:36:17 +0000552 SemaRef.InstantiateAttrs(TemplateArgs, D, Enum);
553
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000554 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000555 Enum->setAccess(D->getAccess());
John McCallb6217662010-03-15 10:12:16 +0000556 if (SubstQualifier(D, Enum)) return 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000557 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000558 Enum->startDefinition();
559
Douglas Gregor96084f12010-03-01 19:00:07 +0000560 if (D->getDeclContext()->isFunctionOrMethod())
561 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
562
John McCalld226f652010-08-21 09:40:31 +0000563 llvm::SmallVector<Decl*, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000564
565 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000566 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
567 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000568 EC != ECEnd; ++EC) {
569 // The specified value for the enumerator.
John McCall60d7b3a2010-08-24 06:29:42 +0000570 ExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000571 if (Expr *UninstValue = EC->getInitExpr()) {
572 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000573 EnterExpressionEvaluationContext Unevaluated(SemaRef,
John McCallf312b1e2010-08-26 23:41:50 +0000574 Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000575
John McCallce3ff2b2009-08-25 22:02:44 +0000576 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000577 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000578
579 // Drop the initial value and continue.
580 bool isInvalid = false;
581 if (Value.isInvalid()) {
582 Value = SemaRef.Owned((Expr *)0);
583 isInvalid = true;
584 }
585
Mike Stump1eb44332009-09-09 15:08:12 +0000586 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000587 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
588 EC->getLocation(), EC->getIdentifier(),
John McCall9ae2f072010-08-23 23:25:46 +0000589 Value.get());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000590
591 if (isInvalid) {
592 if (EnumConst)
593 EnumConst->setInvalidDecl();
594 Enum->setInvalidDecl();
595 }
596
597 if (EnumConst) {
John McCall5b629aa2010-10-22 23:36:17 +0000598 SemaRef.InstantiateAttrs(TemplateArgs, *EC, EnumConst);
599
John McCall3b85ecf2010-01-23 22:37:59 +0000600 EnumConst->setAccess(Enum->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000601 Enum->addDecl(EnumConst);
John McCalld226f652010-08-21 09:40:31 +0000602 Enumerators.push_back(EnumConst);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000603 LastEnumConst = EnumConst;
Douglas Gregor96084f12010-03-01 19:00:07 +0000604
605 if (D->getDeclContext()->isFunctionOrMethod()) {
606 // If the enumeration is within a function or method, record the enum
607 // constant as a local.
608 SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
609 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000610 }
611 }
Mike Stump1eb44332009-09-09 15:08:12 +0000612
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000613 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000614 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000615 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
John McCalld226f652010-08-21 09:40:31 +0000616 Enum,
Eli Friedmande7a0fc2010-08-15 02:27:09 +0000617 Enumerators.data(), Enumerators.size(),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000618 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000619
620 return Enum;
621}
622
Douglas Gregor6477b692009-03-25 15:04:13 +0000623Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
624 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
625 return 0;
626}
627
John McCalle29ba202009-08-20 01:44:21 +0000628Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
John McCall93ba8572010-03-25 06:39:04 +0000629 bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
630
Douglas Gregor550d9b22009-10-31 17:21:17 +0000631 // Create a local instantiation scope for this class template, which
632 // will contain the instantiations of the template parameters.
John McCall2a7fb272010-08-25 05:32:35 +0000633 LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000634 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000635 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000636 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000637 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000638
639 CXXRecordDecl *Pattern = D->getTemplatedDecl();
John McCall93ba8572010-03-25 06:39:04 +0000640
641 // Instantiate the qualifier. We have to do this first in case
642 // we're a friend declaration, because if we are then we need to put
643 // the new declaration in the appropriate context.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000644 NestedNameSpecifierLoc QualifierLoc = Pattern->getQualifierLoc();
645 if (QualifierLoc) {
646 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
647 TemplateArgs);
648 if (!QualifierLoc)
649 return 0;
John McCall93ba8572010-03-25 06:39:04 +0000650 }
651
652 CXXRecordDecl *PrevDecl = 0;
653 ClassTemplateDecl *PrevClassTemplate = 0;
654
Nick Lewycky37574f52010-11-08 23:29:42 +0000655 if (!isFriend && Pattern->getPreviousDeclaration()) {
656 DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
657 if (Found.first != Found.second) {
658 PrevClassTemplate = dyn_cast<ClassTemplateDecl>(*Found.first);
659 if (PrevClassTemplate)
660 PrevDecl = PrevClassTemplate->getTemplatedDecl();
661 }
662 }
663
John McCall93ba8572010-03-25 06:39:04 +0000664 // If this isn't a friend, then it's a member template, in which
665 // case we just want to build the instantiation in the
666 // specialization. If it is a friend, we want to build it in
667 // the appropriate context.
668 DeclContext *DC = Owner;
669 if (isFriend) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000670 if (QualifierLoc) {
John McCall93ba8572010-03-25 06:39:04 +0000671 CXXScopeSpec SS;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000672 SS.Adopt(QualifierLoc);
John McCall93ba8572010-03-25 06:39:04 +0000673 DC = SemaRef.computeDeclContext(SS);
674 if (!DC) return 0;
675 } else {
676 DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
677 Pattern->getDeclContext(),
678 TemplateArgs);
679 }
680
681 // Look for a previous declaration of the template in the owning
682 // context.
683 LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
684 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
685 SemaRef.LookupQualifiedName(R, DC);
686
687 if (R.isSingleResult()) {
688 PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
689 if (PrevClassTemplate)
690 PrevDecl = PrevClassTemplate->getTemplatedDecl();
691 }
692
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000693 if (!PrevClassTemplate && QualifierLoc) {
John McCall93ba8572010-03-25 06:39:04 +0000694 SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
Douglas Gregor1eabb7d2010-03-31 23:17:41 +0000695 << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000696 << QualifierLoc.getSourceRange();
John McCall93ba8572010-03-25 06:39:04 +0000697 return 0;
698 }
699
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000700 bool AdoptedPreviousTemplateParams = false;
John McCall93ba8572010-03-25 06:39:04 +0000701 if (PrevClassTemplate) {
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000702 bool Complain = true;
703
704 // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
705 // template for struct std::tr1::__detail::_Map_base, where the
706 // template parameters of the friend declaration don't match the
707 // template parameters of the original declaration. In this one
708 // case, we don't complain about the ill-formed friend
709 // declaration.
710 if (isFriend && Pattern->getIdentifier() &&
711 Pattern->getIdentifier()->isStr("_Map_base") &&
712 DC->isNamespace() &&
713 cast<NamespaceDecl>(DC)->getIdentifier() &&
714 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
715 DeclContext *DCParent = DC->getParent();
716 if (DCParent->isNamespace() &&
717 cast<NamespaceDecl>(DCParent)->getIdentifier() &&
718 cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
719 DeclContext *DCParent2 = DCParent->getParent();
720 if (DCParent2->isNamespace() &&
721 cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
722 cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
723 DCParent2->getParent()->isTranslationUnit())
724 Complain = false;
725 }
726 }
727
John McCall93ba8572010-03-25 06:39:04 +0000728 TemplateParameterList *PrevParams
729 = PrevClassTemplate->getTemplateParameters();
730
731 // Make sure the parameter lists match.
732 if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000733 Complain,
734 Sema::TPL_TemplateMatch)) {
735 if (Complain)
736 return 0;
737
738 AdoptedPreviousTemplateParams = true;
739 InstParams = PrevParams;
740 }
John McCall93ba8572010-03-25 06:39:04 +0000741
742 // Do some additional validation, then merge default arguments
743 // from the existing declarations.
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000744 if (!AdoptedPreviousTemplateParams &&
745 SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
John McCall93ba8572010-03-25 06:39:04 +0000746 Sema::TPC_ClassTemplate))
747 return 0;
748 }
749 }
750
John McCalle29ba202009-08-20 01:44:21 +0000751 CXXRecordDecl *RecordInst
John McCall93ba8572010-03-25 06:39:04 +0000752 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
John McCalle29ba202009-08-20 01:44:21 +0000753 Pattern->getLocation(), Pattern->getIdentifier(),
John McCall93ba8572010-03-25 06:39:04 +0000754 Pattern->getTagKeywordLoc(), PrevDecl,
Douglas Gregorf0510d42009-10-12 23:11:44 +0000755 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000756
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000757 if (QualifierLoc)
758 RecordInst->setQualifierInfo(QualifierLoc);
John McCallb6217662010-03-15 10:12:16 +0000759
John McCalle29ba202009-08-20 01:44:21 +0000760 ClassTemplateDecl *Inst
John McCall93ba8572010-03-25 06:39:04 +0000761 = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
762 D->getIdentifier(), InstParams, RecordInst,
763 PrevClassTemplate);
John McCalle29ba202009-08-20 01:44:21 +0000764 RecordInst->setDescribedClassTemplate(Inst);
John McCallea7390c2010-04-08 20:25:50 +0000765
John McCall93ba8572010-03-25 06:39:04 +0000766 if (isFriend) {
John McCallea7390c2010-04-08 20:25:50 +0000767 if (PrevClassTemplate)
768 Inst->setAccess(PrevClassTemplate->getAccess());
769 else
770 Inst->setAccess(D->getAccess());
771
John McCall93ba8572010-03-25 06:39:04 +0000772 Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
773 // TODO: do we want to track the instantiation progeny of this
774 // friend target decl?
775 } else {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000776 Inst->setAccess(D->getAccess());
Nick Lewycky37574f52010-11-08 23:29:42 +0000777 if (!PrevClassTemplate)
778 Inst->setInstantiatedFromMemberTemplate(D);
John McCall93ba8572010-03-25 06:39:04 +0000779 }
Douglas Gregorf0510d42009-10-12 23:11:44 +0000780
781 // Trigger creation of the type for the instantiation.
John McCall3cb0ebd2010-03-10 03:28:59 +0000782 SemaRef.Context.getInjectedClassNameType(RecordInst,
Douglas Gregor24bae922010-07-08 18:37:38 +0000783 Inst->getInjectedClassNameSpecialization());
John McCallea7390c2010-04-08 20:25:50 +0000784
Douglas Gregor259571e2009-10-30 22:42:42 +0000785 // Finish handling of friends.
John McCall93ba8572010-03-25 06:39:04 +0000786 if (isFriend) {
787 DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000788 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000789 }
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000790
John McCalle29ba202009-08-20 01:44:21 +0000791 Owner->addDecl(Inst);
Douglas Gregord65587f2010-11-10 19:44:59 +0000792
793 if (!PrevClassTemplate) {
794 // Queue up any out-of-line partial specializations of this member
795 // class template; the client will force their instantiation once
796 // the enclosing class has been instantiated.
797 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
798 D->getPartialSpecializations(PartialSpecs);
799 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
800 if (PartialSpecs[I]->isOutOfLine())
801 OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I]));
802 }
803
John McCalle29ba202009-08-20 01:44:21 +0000804 return Inst;
805}
806
Douglas Gregord60e1052009-08-27 16:57:43 +0000807Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000808TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
809 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000810 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
811
812 // Lookup the already-instantiated declaration in the instantiation
813 // of the class template and return that.
814 DeclContext::lookup_result Found
815 = Owner->lookup(ClassTemplate->getDeclName());
816 if (Found.first == Found.second)
817 return 0;
818
819 ClassTemplateDecl *InstClassTemplate
820 = dyn_cast<ClassTemplateDecl>(*Found.first);
821 if (!InstClassTemplate)
822 return 0;
823
Douglas Gregord65587f2010-11-10 19:44:59 +0000824 if (ClassTemplatePartialSpecializationDecl *Result
825 = InstClassTemplate->findPartialSpecInstantiatedFromMember(D))
826 return Result;
827
828 return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D);
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000829}
830
831Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000832TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000833 // Create a local instantiation scope for this function template, which
834 // will contain the instantiations of the template parameters and then get
835 // merged with the local instantiation scope for the function template
836 // itself.
John McCall2a7fb272010-08-25 05:32:35 +0000837 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor895162d2010-04-30 18:55:50 +0000838
Douglas Gregord60e1052009-08-27 16:57:43 +0000839 TemplateParameterList *TempParams = D->getTemplateParameters();
840 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000841 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000842 return NULL;
Douglas Gregored9c0f92009-10-29 00:04:11 +0000843
Douglas Gregora735b202009-10-13 14:39:41 +0000844 FunctionDecl *Instantiated = 0;
845 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
846 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
847 InstParams));
848 else
849 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
850 D->getTemplatedDecl(),
851 InstParams));
852
853 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000854 return 0;
855
John McCall46460a62010-01-20 21:53:11 +0000856 Instantiated->setAccess(D->getAccess());
857
Mike Stump1eb44332009-09-09 15:08:12 +0000858 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000859 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000860 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000861 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000862 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000863 assert(InstTemplate &&
864 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
John McCalle976ffe2009-12-14 23:19:40 +0000865
John McCallb1a56e72010-03-26 23:10:15 +0000866 bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
867
John McCalle976ffe2009-12-14 23:19:40 +0000868 // Link the instantiation back to the pattern *unless* this is a
869 // non-definition friend declaration.
870 if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
John McCallb1a56e72010-03-26 23:10:15 +0000871 !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
Douglas Gregora735b202009-10-13 14:39:41 +0000872 InstTemplate->setInstantiatedFromMemberTemplate(D);
873
John McCallb1a56e72010-03-26 23:10:15 +0000874 // Make declarations visible in the appropriate context.
875 if (!isFriend)
Douglas Gregora735b202009-10-13 14:39:41 +0000876 Owner->addDecl(InstTemplate);
John McCallb1a56e72010-03-26 23:10:15 +0000877
Douglas Gregord60e1052009-08-27 16:57:43 +0000878 return InstTemplate;
879}
880
Douglas Gregord475b8d2009-03-25 21:17:03 +0000881Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
882 CXXRecordDecl *PrevDecl = 0;
883 if (D->isInjectedClassName())
884 PrevDecl = cast<CXXRecordDecl>(Owner);
John McCall6c1c1b82009-12-15 22:29:06 +0000885 else if (D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000886 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
887 D->getPreviousDeclaration(),
John McCall6c1c1b82009-12-15 22:29:06 +0000888 TemplateArgs);
889 if (!Prev) return 0;
890 PrevDecl = cast<CXXRecordDecl>(Prev);
891 }
Douglas Gregord475b8d2009-03-25 21:17:03 +0000892
893 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000894 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000895 D->getLocation(), D->getIdentifier(),
896 D->getTagKeywordLoc(), PrevDecl);
John McCallb6217662010-03-15 10:12:16 +0000897
898 // Substitute the nested name specifier, if any.
899 if (SubstQualifier(D, Record))
900 return 0;
901
Douglas Gregord475b8d2009-03-25 21:17:03 +0000902 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000903 // FIXME: Check against AS_none is an ugly hack to work around the issue that
904 // the tag decls introduced by friend class declarations don't have an access
905 // specifier. Remove once this area of the code gets sorted out.
906 if (D->getAccess() != AS_none)
907 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000908 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000909 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000910
John McCall02cace72009-08-28 07:59:38 +0000911 // If the original function was part of a friend declaration,
912 // inherit its namespace state.
913 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
914 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
915
Douglas Gregor9901c572010-05-21 00:31:19 +0000916 // Make sure that anonymous structs and unions are recorded.
917 if (D->isAnonymousStructOrUnion()) {
918 Record->setAnonymousStructOrUnion(true);
Sebastian Redl7a126a42010-08-31 00:36:30 +0000919 if (Record->getDeclContext()->getRedeclContext()->isFunctionOrMethod())
Douglas Gregor9901c572010-05-21 00:31:19 +0000920 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
921 }
Anders Carlssond8b285f2009-09-01 04:26:58 +0000922
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000923 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000924 return Record;
925}
926
John McCall02cace72009-08-28 07:59:38 +0000927/// Normal class members are of more specific types and therefore
928/// don't make it here. This function serves two purposes:
929/// 1) instantiating function templates
930/// 2) substituting friend declarations
931/// FIXME: preserve function definitions in case #2
Douglas Gregor7557a132009-12-24 20:56:24 +0000932Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
Douglas Gregora735b202009-10-13 14:39:41 +0000933 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000934 // Check whether there is already a function template specialization for
935 // this declaration.
936 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
937 void *InsertPos = 0;
John McCallb0cb0222010-03-27 05:57:59 +0000938 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor24bae922010-07-08 18:37:38 +0000939 std::pair<const TemplateArgument *, unsigned> Innermost
940 = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +0000942 FunctionDecl *SpecFunc
943 = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
944 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000945
Douglas Gregor127102b2009-06-29 20:59:39 +0000946 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +0000947 if (SpecFunc)
948 return SpecFunc;
Douglas Gregor127102b2009-06-29 20:59:39 +0000949 }
Mike Stump1eb44332009-09-09 15:08:12 +0000950
John McCallb0cb0222010-03-27 05:57:59 +0000951 bool isFriend;
952 if (FunctionTemplate)
953 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
954 else
955 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
956
Douglas Gregor79c22782010-01-16 20:21:20 +0000957 bool MergeWithParentScope = (TemplateParams != 0) ||
Douglas Gregorb212d9a2010-05-21 21:25:08 +0000958 Owner->isFunctionOrMethod() ||
Douglas Gregor79c22782010-01-16 20:21:20 +0000959 !(isa<Decl>(Owner) &&
960 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
John McCall2a7fb272010-08-25 05:32:35 +0000961 LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000962
Douglas Gregore53060f2009-06-25 22:08:12 +0000963 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +0000964 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
965 TInfo = SubstFunctionType(D, Params);
966 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000967 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +0000968 QualType T = TInfo->getType();
John McCallfd810b12009-08-14 02:03:10 +0000969
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000970 NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
971 if (QualifierLoc) {
972 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
973 TemplateArgs);
974 if (!QualifierLoc)
975 return 0;
John McCalld325daa2010-03-26 04:53:08 +0000976 }
977
John McCall68b6b872010-02-06 01:50:47 +0000978 // If we're instantiating a local function declaration, put the result
979 // in the owner; otherwise we need to find the instantiated context.
980 DeclContext *DC;
981 if (D->getDeclContext()->isFunctionOrMethod())
982 DC = Owner;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000983 else if (isFriend && QualifierLoc) {
John McCalld325daa2010-03-26 04:53:08 +0000984 CXXScopeSpec SS;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000985 SS.Adopt(QualifierLoc);
John McCalld325daa2010-03-26 04:53:08 +0000986 DC = SemaRef.computeDeclContext(SS);
987 if (!DC) return 0;
988 } else {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000989 DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
990 TemplateArgs);
John McCalld325daa2010-03-26 04:53:08 +0000991 }
John McCall68b6b872010-02-06 01:50:47 +0000992
John McCall02cace72009-08-28 07:59:38 +0000993 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +0000994 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +0000995 D->getDeclName(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000996 D->getStorageClass(), D->getStorageClassAsWritten(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000997 D->isInlineSpecified(), D->hasWrittenPrototype());
John McCallb6217662010-03-15 10:12:16 +0000998
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000999 if (QualifierLoc)
1000 Function->setQualifierInfo(QualifierLoc);
John McCallb6217662010-03-15 10:12:16 +00001001
John McCallb1a56e72010-03-26 23:10:15 +00001002 DeclContext *LexicalDC = Owner;
1003 if (!isFriend && D->isOutOfLine()) {
1004 assert(D->getDeclContext()->isFileContext());
1005 LexicalDC = D->getDeclContext();
1006 }
1007
1008 Function->setLexicalDeclContext(LexicalDC);
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Douglas Gregore53060f2009-06-25 22:08:12 +00001010 // Attach the parameters
1011 for (unsigned P = 0; P < Params.size(); ++P)
John McCall3019c442010-09-17 00:50:28 +00001012 if (Params[P])
1013 Params[P]->setOwningFunction(Function);
Douglas Gregor838db382010-02-11 01:19:42 +00001014 Function->setParams(Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +00001015
Douglas Gregorac7c2c82010-05-17 16:38:00 +00001016 SourceLocation InstantiateAtPOI;
Douglas Gregora735b202009-10-13 14:39:41 +00001017 if (TemplateParams) {
1018 // Our resulting instantiation is actually a function template, since we
1019 // are substituting only the outer template parameters. For example, given
1020 //
1021 // template<typename T>
1022 // struct X {
1023 // template<typename U> friend void f(T, U);
1024 // };
1025 //
1026 // X<int> x;
1027 //
1028 // We are instantiating the friend function template "f" within X<int>,
1029 // which means substituting int for T, but leaving "f" as a friend function
1030 // template.
1031 // Build the function template itself.
John McCalld325daa2010-03-26 04:53:08 +00001032 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
Douglas Gregora735b202009-10-13 14:39:41 +00001033 Function->getLocation(),
1034 Function->getDeclName(),
1035 TemplateParams, Function);
1036 Function->setDescribedFunctionTemplate(FunctionTemplate);
John McCallb1a56e72010-03-26 23:10:15 +00001037
1038 FunctionTemplate->setLexicalDeclContext(LexicalDC);
John McCalld325daa2010-03-26 04:53:08 +00001039
1040 if (isFriend && D->isThisDeclarationADefinition()) {
1041 // TODO: should we remember this connection regardless of whether
1042 // the friend declaration provided a body?
1043 FunctionTemplate->setInstantiatedFromMemberTemplate(
1044 D->getDescribedFunctionTemplate());
1045 }
Douglas Gregor66724ea2009-11-14 01:20:54 +00001046 } else if (FunctionTemplate) {
1047 // Record this function template specialization.
Douglas Gregor24bae922010-07-08 18:37:38 +00001048 std::pair<const TemplateArgument *, unsigned> Innermost
1049 = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001050 Function->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001051 TemplateArgumentList::CreateCopy(SemaRef.Context,
Douglas Gregor24bae922010-07-08 18:37:38 +00001052 Innermost.first,
1053 Innermost.second),
Douglas Gregor66724ea2009-11-14 01:20:54 +00001054 InsertPos);
John McCalld325daa2010-03-26 04:53:08 +00001055 } else if (isFriend && D->isThisDeclarationADefinition()) {
1056 // TODO: should we remember this connection regardless of whether
1057 // the friend declaration provided a body?
1058 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
John McCall02cace72009-08-28 07:59:38 +00001059 }
Douglas Gregora735b202009-10-13 14:39:41 +00001060
Douglas Gregore53060f2009-06-25 22:08:12 +00001061 if (InitFunctionInstantiation(Function, D))
1062 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001063
Douglas Gregore53060f2009-06-25 22:08:12 +00001064 bool Redeclaration = false;
John McCallaf2094e2010-04-08 09:05:18 +00001065 bool isExplicitSpecialization = false;
Douglas Gregora735b202009-10-13 14:39:41 +00001066
John McCall68263142009-11-18 22:49:29 +00001067 LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
1068 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
1069
John McCallaf2094e2010-04-08 09:05:18 +00001070 if (DependentFunctionTemplateSpecializationInfo *Info
1071 = D->getDependentSpecializationInfo()) {
1072 assert(isFriend && "non-friend has dependent specialization info?");
1073
1074 // This needs to be set now for future sanity.
1075 Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1076
1077 // Instantiate the explicit template arguments.
1078 TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1079 Info->getRAngleLoc());
Douglas Gregore02e2622010-12-22 21:19:48 +00001080 if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(),
1081 ExplicitArgs, TemplateArgs))
1082 return 0;
John McCallaf2094e2010-04-08 09:05:18 +00001083
1084 // Map the candidate templates to their instantiations.
1085 for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1086 Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1087 Info->getTemplate(I),
1088 TemplateArgs);
1089 if (!Temp) return 0;
1090
1091 Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1092 }
1093
1094 if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1095 &ExplicitArgs,
1096 Previous))
1097 Function->setInvalidDecl();
1098
1099 isExplicitSpecialization = true;
1100
1101 } else if (TemplateParams || !FunctionTemplate) {
Douglas Gregora735b202009-10-13 14:39:41 +00001102 // Look only into the namespace where the friend would be declared to
1103 // find a previous declaration. This is the innermost enclosing namespace,
1104 // as described in ActOnFriendFunctionDecl.
John McCall68263142009-11-18 22:49:29 +00001105 SemaRef.LookupQualifiedName(Previous, DC);
Douglas Gregora735b202009-10-13 14:39:41 +00001106
Douglas Gregora735b202009-10-13 14:39:41 +00001107 // In C++, the previous declaration we find might be a tag type
1108 // (class or enum). In this case, the new declaration will hide the
1109 // tag type. Note that this does does not apply if we're declaring a
1110 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001111 if (Previous.isSingleTagDecl())
1112 Previous.clear();
Douglas Gregora735b202009-10-13 14:39:41 +00001113 }
1114
John McCall9f54ad42009-12-10 09:41:52 +00001115 SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
Peter Collingbournec80e8112011-01-21 02:08:54 +00001116 isExplicitSpecialization, Redeclaration);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001117
John McCall76d32642010-04-24 01:30:58 +00001118 NamedDecl *PrincipalDecl = (TemplateParams
1119 ? cast<NamedDecl>(FunctionTemplate)
1120 : Function);
1121
Douglas Gregora735b202009-10-13 14:39:41 +00001122 // If the original function was part of a friend declaration,
1123 // inherit its namespace state and add it to the owner.
John McCalld325daa2010-03-26 04:53:08 +00001124 if (isFriend) {
John McCall68263142009-11-18 22:49:29 +00001125 NamedDecl *PrevDecl;
John McCall76d32642010-04-24 01:30:58 +00001126 if (TemplateParams)
Douglas Gregora735b202009-10-13 14:39:41 +00001127 PrevDecl = FunctionTemplate->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001128 else
Douglas Gregora735b202009-10-13 14:39:41 +00001129 PrevDecl = Function->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001130
1131 PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
1132 DC->makeDeclVisibleInContext(PrincipalDecl, /*Recoverable=*/ false);
Gabor Greifab297ac2010-08-30 21:10:05 +00001133
Gabor Greif77535df2010-08-30 22:25:56 +00001134 bool queuedInstantiation = false;
Gabor Greifab297ac2010-08-30 21:10:05 +00001135
Douglas Gregor238058c2010-05-18 05:45:02 +00001136 if (!SemaRef.getLangOptions().CPlusPlus0x &&
1137 D->isThisDeclarationADefinition()) {
1138 // Check for a function body.
1139 const FunctionDecl *Definition = 0;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001140 if (Function->hasBody(Definition) &&
Douglas Gregor238058c2010-05-18 05:45:02 +00001141 Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
1142 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1143 << Function->getDeclName();
1144 SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
1145 Function->setInvalidDecl();
1146 }
1147 // Check for redefinitions due to other instantiations of this or
1148 // a similar friend function.
1149 else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1150 REnd = Function->redecls_end();
1151 R != REnd; ++R) {
Gabor Greif13a8aff2010-08-28 15:42:30 +00001152 if (*R == Function)
1153 continue;
Gabor Greifab297ac2010-08-30 21:10:05 +00001154 switch (R->getFriendObjectKind()) {
1155 case Decl::FOK_None:
1156 if (!queuedInstantiation && R->isUsed(false)) {
1157 if (MemberSpecializationInfo *MSInfo
1158 = Function->getMemberSpecializationInfo()) {
1159 if (MSInfo->getPointOfInstantiation().isInvalid()) {
1160 SourceLocation Loc = R->getLocation(); // FIXME
1161 MSInfo->setPointOfInstantiation(Loc);
1162 SemaRef.PendingLocalImplicitInstantiations.push_back(
1163 std::make_pair(Function, Loc));
1164 queuedInstantiation = true;
1165 }
1166 }
1167 }
1168 break;
1169 default:
Douglas Gregor238058c2010-05-18 05:45:02 +00001170 if (const FunctionDecl *RPattern
Gabor Greif6a557d82010-08-28 15:46:56 +00001171 = R->getTemplateInstantiationPattern())
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001172 if (RPattern->hasBody(RPattern)) {
Douglas Gregor238058c2010-05-18 05:45:02 +00001173 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1174 << Function->getDeclName();
Gabor Greif6a557d82010-08-28 15:46:56 +00001175 SemaRef.Diag(R->getLocation(), diag::note_previous_definition);
Douglas Gregor238058c2010-05-18 05:45:02 +00001176 Function->setInvalidDecl();
1177 break;
1178 }
1179 }
1180 }
1181 }
Douglas Gregora735b202009-10-13 14:39:41 +00001182 }
1183
John McCall76d32642010-04-24 01:30:58 +00001184 if (Function->isOverloadedOperator() && !DC->isRecord() &&
1185 PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
1186 PrincipalDecl->setNonMemberOperator();
1187
Douglas Gregore53060f2009-06-25 22:08:12 +00001188 return Function;
1189}
1190
Douglas Gregord60e1052009-08-27 16:57:43 +00001191Decl *
1192TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1193 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +00001194 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1195 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +00001196 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +00001197 // We are creating a function template specialization from a function
1198 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +00001199 // specialization for this particular set of template arguments.
Douglas Gregor24bae922010-07-08 18:37:38 +00001200 std::pair<const TemplateArgument *, unsigned> Innermost
1201 = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +00001202
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001203 FunctionDecl *SpecFunc
1204 = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
1205 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001206
Douglas Gregor6b906862009-08-21 00:16:32 +00001207 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001208 if (SpecFunc)
1209 return SpecFunc;
Douglas Gregor6b906862009-08-21 00:16:32 +00001210 }
1211
John McCallb0cb0222010-03-27 05:57:59 +00001212 bool isFriend;
1213 if (FunctionTemplate)
1214 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1215 else
1216 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1217
Douglas Gregor79c22782010-01-16 20:21:20 +00001218 bool MergeWithParentScope = (TemplateParams != 0) ||
1219 !(isa<Decl>(Owner) &&
1220 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
John McCall2a7fb272010-08-25 05:32:35 +00001221 LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Douglas Gregor48dd19b2009-05-14 21:44:34 +00001222
John McCall4eab39f2010-10-19 02:26:41 +00001223 // Instantiate enclosing template arguments for friends.
1224 llvm::SmallVector<TemplateParameterList *, 4> TempParamLists;
1225 unsigned NumTempParamLists = 0;
1226 if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) {
1227 TempParamLists.set_size(NumTempParamLists);
1228 for (unsigned I = 0; I != NumTempParamLists; ++I) {
1229 TemplateParameterList *TempParams = D->getTemplateParameterList(I);
1230 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1231 if (!InstParams)
1232 return NULL;
1233 TempParamLists[I] = InstParams;
1234 }
1235 }
1236
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001237 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +00001238 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
1239 TInfo = SubstFunctionType(D, Params);
1240 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001241 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +00001242 QualType T = TInfo->getType();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001243
Abramo Bagnara723df242010-12-14 22:11:44 +00001244 // \brief If the type of this function, after ignoring parentheses,
1245 // is not *directly* a function type, then we're instantiating a function
1246 // that was declared via a typedef, e.g.,
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001247 //
1248 // typedef int functype(int, int);
1249 // functype func;
1250 //
1251 // In this case, we'll just go instantiate the ParmVarDecls that we
1252 // synthesized in the method declaration.
Abramo Bagnara723df242010-12-14 22:11:44 +00001253 if (!isa<FunctionProtoType>(T.IgnoreParens())) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001254 assert(!Params.size() && "Instantiating type could not yield parameters");
Douglas Gregor12c9c002011-01-07 16:43:16 +00001255 llvm::SmallVector<QualType, 4> ParamTypes;
1256 if (SemaRef.SubstParmTypes(D->getLocation(), D->param_begin(),
1257 D->getNumParams(), TemplateArgs, ParamTypes,
1258 &Params))
1259 return 0;
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001260 }
1261
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001262 NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
1263 if (QualifierLoc) {
1264 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
John McCallb0cb0222010-03-27 05:57:59 +00001265 TemplateArgs);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001266 if (!QualifierLoc)
1267 return 0;
John McCallb0cb0222010-03-27 05:57:59 +00001268 }
1269
1270 DeclContext *DC = Owner;
1271 if (isFriend) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001272 if (QualifierLoc) {
John McCallb0cb0222010-03-27 05:57:59 +00001273 CXXScopeSpec SS;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001274 SS.Adopt(QualifierLoc);
John McCallb0cb0222010-03-27 05:57:59 +00001275 DC = SemaRef.computeDeclContext(SS);
John McCallc54d6882010-10-19 05:01:53 +00001276
1277 if (DC && SemaRef.RequireCompleteDeclContext(SS, DC))
1278 return 0;
John McCallb0cb0222010-03-27 05:57:59 +00001279 } else {
1280 DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1281 D->getDeclContext(),
1282 TemplateArgs);
1283 }
1284 if (!DC) return 0;
1285 }
1286
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001287 // Build the instantiated method declaration.
John McCallb0cb0222010-03-27 05:57:59 +00001288 CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
Douglas Gregordec06662009-08-21 18:42:58 +00001289 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001290
Abramo Bagnara25777432010-08-11 22:01:17 +00001291 DeclarationNameInfo NameInfo
1292 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001293 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001294 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
Abramo Bagnara25777432010-08-11 22:01:17 +00001295 NameInfo, T, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001296 Constructor->isExplicit(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001297 Constructor->isInlineSpecified(),
1298 false);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001299 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001300 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001301 NameInfo, T, TInfo,
Abramo Bagnara25777432010-08-11 22:01:17 +00001302 Destructor->isInlineSpecified(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001303 false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001304 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001305 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
Abramo Bagnara25777432010-08-11 22:01:17 +00001306 NameInfo, T, TInfo,
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001307 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001308 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +00001309 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00001310 Method = CXXMethodDecl::Create(SemaRef.Context, Record,
1311 NameInfo, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001312 D->isStatic(),
1313 D->getStorageClassAsWritten(),
1314 D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +00001315 }
Douglas Gregor6b906862009-08-21 00:16:32 +00001316
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001317 if (QualifierLoc)
1318 Method->setQualifierInfo(QualifierLoc);
John McCallb6217662010-03-15 10:12:16 +00001319
Douglas Gregord60e1052009-08-27 16:57:43 +00001320 if (TemplateParams) {
1321 // Our resulting instantiation is actually a function template, since we
1322 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +00001323 //
Douglas Gregord60e1052009-08-27 16:57:43 +00001324 // template<typename T>
1325 // struct X {
1326 // template<typename U> void f(T, U);
1327 // };
1328 //
1329 // X<int> x;
1330 //
1331 // We are instantiating the member template "f" within X<int>, which means
1332 // substituting int for T, but leaving "f" as a member function template.
1333 // Build the function template itself.
1334 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1335 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001336 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +00001337 TemplateParams, Method);
John McCallb0cb0222010-03-27 05:57:59 +00001338 if (isFriend) {
1339 FunctionTemplate->setLexicalDeclContext(Owner);
1340 FunctionTemplate->setObjectOfFriendDecl(true);
1341 } else if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +00001342 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +00001343 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001344 } else if (FunctionTemplate) {
1345 // Record this function template specialization.
Douglas Gregor24bae922010-07-08 18:37:38 +00001346 std::pair<const TemplateArgument *, unsigned> Innermost
1347 = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001348 Method->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001349 TemplateArgumentList::CreateCopy(SemaRef.Context,
1350 Innermost.first,
1351 Innermost.second),
Douglas Gregor66724ea2009-11-14 01:20:54 +00001352 InsertPos);
John McCallb0cb0222010-03-27 05:57:59 +00001353 } else if (!isFriend) {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001354 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +00001355 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001356 }
1357
Mike Stump1eb44332009-09-09 15:08:12 +00001358 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +00001359 // out-of-line, the instantiation will have the same lexical
1360 // context (which will be a namespace scope) as the template.
John McCallb0cb0222010-03-27 05:57:59 +00001361 if (isFriend) {
John McCall4eab39f2010-10-19 02:26:41 +00001362 if (NumTempParamLists)
1363 Method->setTemplateParameterListsInfo(SemaRef.Context,
1364 NumTempParamLists,
1365 TempParamLists.data());
1366
John McCallb0cb0222010-03-27 05:57:59 +00001367 Method->setLexicalDeclContext(Owner);
1368 Method->setObjectOfFriendDecl(true);
1369 } else if (D->isOutOfLine())
Douglas Gregor7caa6822009-07-24 20:34:43 +00001370 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001371
Douglas Gregor5545e162009-03-24 00:38:23 +00001372 // Attach the parameters
1373 for (unsigned P = 0; P < Params.size(); ++P)
1374 Params[P]->setOwningFunction(Method);
Douglas Gregor838db382010-02-11 01:19:42 +00001375 Method->setParams(Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +00001376
1377 if (InitMethodInstantiation(Method, D))
1378 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001379
Abramo Bagnara25777432010-08-11 22:01:17 +00001380 LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
1381 Sema::ForRedeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001382
John McCallb0cb0222010-03-27 05:57:59 +00001383 if (!FunctionTemplate || TemplateParams || isFriend) {
1384 SemaRef.LookupQualifiedName(Previous, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001385
Douglas Gregordec06662009-08-21 18:42:58 +00001386 // In C++, the previous declaration we find might be a tag type
1387 // (class or enum). In this case, the new declaration will hide the
1388 // tag type. Note that this does does not apply if we're declaring a
1389 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001390 if (Previous.isSingleTagDecl())
1391 Previous.clear();
Douglas Gregordec06662009-08-21 18:42:58 +00001392 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001393
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001394 bool Redeclaration = false;
Peter Collingbournec80e8112011-01-21 02:08:54 +00001395 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001396
Douglas Gregor4ba31362009-12-01 17:24:26 +00001397 if (D->isPure())
1398 SemaRef.CheckPureMethod(Method, SourceRange());
1399
John McCall46460a62010-01-20 21:53:11 +00001400 Method->setAccess(D->getAccess());
1401
Anders Carlsson9eefa222011-01-20 06:52:44 +00001402 SemaRef.CheckOverrideControl(Method);
1403
John McCallb0cb0222010-03-27 05:57:59 +00001404 if (FunctionTemplate) {
1405 // If there's a function template, let our caller handle it.
1406 } else if (Method->isInvalidDecl() && !Previous.empty()) {
1407 // Don't hide a (potentially) valid declaration with an invalid one.
1408 } else {
1409 NamedDecl *DeclToAdd = (TemplateParams
1410 ? cast<NamedDecl>(FunctionTemplate)
1411 : Method);
1412 if (isFriend)
1413 Record->makeDeclVisibleInContext(DeclToAdd);
1414 else
1415 Owner->addDecl(DeclToAdd);
1416 }
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00001417
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001418 return Method;
1419}
1420
Douglas Gregor615c5d42009-03-24 16:43:20 +00001421Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +00001422 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +00001423}
1424
Douglas Gregor03b2b072009-03-24 00:15:49 +00001425Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001426 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +00001427}
1428
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001429Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001430 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001431}
1432
Douglas Gregor6477b692009-03-25 15:04:13 +00001433ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregor6a24bfd2011-01-14 22:40:04 +00001434 return SemaRef.SubstParmVarDecl(D, TemplateArgs, llvm::Optional<unsigned>());
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001435}
1436
John McCalle29ba202009-08-20 01:44:21 +00001437Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1438 TemplateTypeParmDecl *D) {
1439 // TODO: don't always clone when decls are refcounted.
Douglas Gregorefed5c82010-06-16 15:23:05 +00001440 const Type* T = D->getTypeForDecl();
1441 assert(T->isTemplateTypeParmType());
1442 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001443
John McCalle29ba202009-08-20 01:44:21 +00001444 TemplateTypeParmDecl *Inst =
1445 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor71b87e42010-08-30 23:23:59 +00001446 TTPT->getDepth() - TemplateArgs.getNumLevels(),
Nick Lewycky61139c52010-10-30 06:48:20 +00001447 TTPT->getIndex(), D->getIdentifier(),
John McCalle29ba202009-08-20 01:44:21 +00001448 D->wasDeclaredWithTypename(),
1449 D->isParameterPack());
1450
Douglas Gregor0f8716b2009-11-09 19:17:50 +00001451 if (D->hasDefaultArgument())
1452 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
John McCalle29ba202009-08-20 01:44:21 +00001453
Douglas Gregor550d9b22009-10-31 17:21:17 +00001454 // Introduce this template parameter's instantiation into the instantiation
1455 // scope.
1456 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1457
John McCalle29ba202009-08-20 01:44:21 +00001458 return Inst;
1459}
1460
Douglas Gregor33642df2009-10-23 23:25:44 +00001461Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1462 NonTypeTemplateParmDecl *D) {
1463 // Substitute into the type of the non-type template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001464 TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc();
1465 llvm::SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten;
1466 llvm::SmallVector<QualType, 4> ExpandedParameterPackTypes;
1467 bool IsExpandedParameterPack = false;
1468 TypeSourceInfo *DI;
Douglas Gregor33642df2009-10-23 23:25:44 +00001469 QualType T;
Douglas Gregor33642df2009-10-23 23:25:44 +00001470 bool Invalid = false;
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001471
1472 if (D->isExpandedParameterPack()) {
1473 // The non-type template parameter pack is an already-expanded pack
1474 // expansion of types. Substitute into each of the expanded types.
1475 ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes());
1476 ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes());
1477 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
1478 TypeSourceInfo *NewDI =SemaRef.SubstType(D->getExpansionTypeSourceInfo(I),
1479 TemplateArgs,
1480 D->getLocation(),
1481 D->getDeclName());
1482 if (!NewDI)
1483 return 0;
1484
1485 ExpandedParameterPackTypesAsWritten.push_back(NewDI);
1486 QualType NewT =SemaRef.CheckNonTypeTemplateParameterType(NewDI->getType(),
1487 D->getLocation());
1488 if (NewT.isNull())
1489 return 0;
1490 ExpandedParameterPackTypes.push_back(NewT);
1491 }
1492
1493 IsExpandedParameterPack = true;
1494 DI = D->getTypeSourceInfo();
1495 T = DI->getType();
1496 } else if (isa<PackExpansionTypeLoc>(TL)) {
1497 // The non-type template parameter pack's type is a pack expansion of types.
1498 // Determine whether we need to expand this parameter pack into separate
1499 // types.
1500 PackExpansionTypeLoc Expansion = cast<PackExpansionTypeLoc>(TL);
1501 TypeLoc Pattern = Expansion.getPatternLoc();
1502 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
1503 SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded);
1504
1505 // Determine whether the set of unexpanded parameter packs can and should
1506 // be expanded.
1507 bool Expand = true;
1508 bool RetainExpansion = false;
1509 llvm::Optional<unsigned> OrigNumExpansions
1510 = Expansion.getTypePtr()->getNumExpansions();
1511 llvm::Optional<unsigned> NumExpansions = OrigNumExpansions;
1512 if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(),
1513 Pattern.getSourceRange(),
1514 Unexpanded.data(),
1515 Unexpanded.size(),
1516 TemplateArgs,
1517 Expand, RetainExpansion,
1518 NumExpansions))
1519 return 0;
1520
1521 if (Expand) {
1522 for (unsigned I = 0; I != *NumExpansions; ++I) {
1523 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
1524 TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs,
1525 D->getLocation(),
1526 D->getDeclName());
1527 if (!NewDI)
1528 return 0;
1529
1530 ExpandedParameterPackTypesAsWritten.push_back(NewDI);
1531 QualType NewT = SemaRef.CheckNonTypeTemplateParameterType(
1532 NewDI->getType(),
1533 D->getLocation());
1534 if (NewT.isNull())
1535 return 0;
1536 ExpandedParameterPackTypes.push_back(NewT);
1537 }
1538
1539 // Note that we have an expanded parameter pack. The "type" of this
1540 // expanded parameter pack is the original expansion type, but callers
1541 // will end up using the expanded parameter pack types for type-checking.
1542 IsExpandedParameterPack = true;
1543 DI = D->getTypeSourceInfo();
1544 T = DI->getType();
1545 } else {
1546 // We cannot fully expand the pack expansion now, so substitute into the
1547 // pattern and create a new pack expansion type.
1548 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
1549 TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs,
1550 D->getLocation(),
1551 D->getDeclName());
1552 if (!NewPattern)
1553 return 0;
1554
1555 DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(),
1556 NumExpansions);
1557 if (!DI)
1558 return 0;
1559
1560 T = DI->getType();
1561 }
1562 } else {
1563 // Simple case: substitution into a parameter that is not a parameter pack.
1564 DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs,
1565 D->getLocation(), D->getDeclName());
1566 if (!DI)
1567 return 0;
1568
1569 // Check that this type is acceptable for a non-type template parameter.
1570 bool Invalid = false;
1571 T = SemaRef.CheckNonTypeTemplateParameterType(DI->getType(),
1572 D->getLocation());
1573 if (T.isNull()) {
1574 T = SemaRef.Context.IntTy;
1575 Invalid = true;
1576 }
Douglas Gregor33642df2009-10-23 23:25:44 +00001577 }
1578
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001579 NonTypeTemplateParmDecl *Param;
1580 if (IsExpandedParameterPack)
1581 Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
1582 D->getLocation(),
Douglas Gregor71b87e42010-08-30 23:23:59 +00001583 D->getDepth() - TemplateArgs.getNumLevels(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001584 D->getPosition(),
1585 D->getIdentifier(), T,
1586 DI,
1587 ExpandedParameterPackTypes.data(),
1588 ExpandedParameterPackTypes.size(),
1589 ExpandedParameterPackTypesAsWritten.data());
1590 else
1591 Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
1592 D->getLocation(),
1593 D->getDepth() - TemplateArgs.getNumLevels(),
1594 D->getPosition(),
1595 D->getIdentifier(), T,
1596 D->isParameterPack(), DI);
1597
Douglas Gregor33642df2009-10-23 23:25:44 +00001598 if (Invalid)
1599 Param->setInvalidDecl();
1600
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001601 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor550d9b22009-10-31 17:21:17 +00001602
1603 // Introduce this template parameter's instantiation into the instantiation
1604 // scope.
1605 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001606 return Param;
1607}
1608
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001609Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001610TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1611 TemplateTemplateParmDecl *D) {
1612 // Instantiate the template parameter list of the template template parameter.
1613 TemplateParameterList *TempParams = D->getTemplateParameters();
1614 TemplateParameterList *InstParams;
1615 {
1616 // Perform the actual substitution of template parameters within a new,
1617 // local instantiation scope.
John McCall2a7fb272010-08-25 05:32:35 +00001618 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor9106ef72009-11-11 16:58:32 +00001619 InstParams = SubstTemplateParams(TempParams);
1620 if (!InstParams)
1621 return NULL;
1622 }
1623
1624 // Build the template template parameter.
1625 TemplateTemplateParmDecl *Param
1626 = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor71b87e42010-08-30 23:23:59 +00001627 D->getDepth() - TemplateArgs.getNumLevels(),
Douglas Gregor61c4d282011-01-05 15:48:55 +00001628 D->getPosition(), D->isParameterPack(),
1629 D->getIdentifier(), InstParams);
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001630 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001631
Douglas Gregor9106ef72009-11-11 16:58:32 +00001632 // Introduce this template parameter's instantiation into the instantiation
1633 // scope.
1634 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1635
1636 return Param;
1637}
1638
Douglas Gregor48c32a72009-11-17 06:07:40 +00001639Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Douglas Gregordb992412011-02-25 16:33:46 +00001640 // Using directives are never dependent (and never contain any types or
1641 // expressions), so they require no explicit instantiation work.
Douglas Gregor48c32a72009-11-17 06:07:40 +00001642
1643 UsingDirectiveDecl *Inst
1644 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1645 D->getNamespaceKeyLocation(),
Douglas Gregordb992412011-02-25 16:33:46 +00001646 D->getQualifierLoc(),
Douglas Gregor48c32a72009-11-17 06:07:40 +00001647 D->getIdentLocation(),
1648 D->getNominatedNamespace(),
1649 D->getCommonAncestor());
1650 Owner->addDecl(Inst);
1651 return Inst;
1652}
1653
John McCalled976492009-12-04 22:46:56 +00001654Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
Douglas Gregor1b398202010-09-29 17:58:28 +00001655
1656 // The nested name specifier may be dependent, for example
1657 // template <typename T> struct t {
1658 // struct s1 { T f1(); };
1659 // struct s2 : s1 { using s1::f1; };
1660 // };
1661 // template struct t<int>;
1662 // Here, in using s1::f1, s1 refers to t<T>::s1;
1663 // we need to substitute for t<int>::s1.
Douglas Gregor5149f372011-02-25 15:54:31 +00001664 NestedNameSpecifierLoc QualifierLoc
1665 = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
1666 TemplateArgs);
1667 if (!QualifierLoc)
Douglas Gregordc355712011-02-25 00:36:19 +00001668 return 0;
Douglas Gregor1b398202010-09-29 17:58:28 +00001669
1670 // The name info is non-dependent, so no transformation
1671 // is required.
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001672 DeclarationNameInfo NameInfo = D->getNameInfo();
John McCalled976492009-12-04 22:46:56 +00001673
John McCall9f54ad42009-12-10 09:41:52 +00001674 // We only need to do redeclaration lookups if we're in a class
1675 // scope (in fact, it's not really even possible in non-class
1676 // scopes).
1677 bool CheckRedeclaration = Owner->isRecord();
1678
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001679 LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
1680 Sema::ForRedeclaration);
John McCall9f54ad42009-12-10 09:41:52 +00001681
John McCalled976492009-12-04 22:46:56 +00001682 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
John McCalled976492009-12-04 22:46:56 +00001683 D->getUsingLocation(),
Douglas Gregor5149f372011-02-25 15:54:31 +00001684 QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001685 NameInfo,
John McCalled976492009-12-04 22:46:56 +00001686 D->isTypeName());
1687
Douglas Gregor5149f372011-02-25 15:54:31 +00001688 CXXScopeSpec SS;
1689 SS.Adopt(QualifierLoc);
John McCall9f54ad42009-12-10 09:41:52 +00001690 if (CheckRedeclaration) {
1691 Prev.setHideTags(false);
1692 SemaRef.LookupQualifiedName(Prev, Owner);
1693
1694 // Check for invalid redeclarations.
1695 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
1696 D->isTypeName(), SS,
1697 D->getLocation(), Prev))
1698 NewUD->setInvalidDecl();
1699
1700 }
1701
1702 if (!NewUD->isInvalidDecl() &&
1703 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
John McCalled976492009-12-04 22:46:56 +00001704 D->getLocation()))
1705 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00001706
John McCalled976492009-12-04 22:46:56 +00001707 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1708 NewUD->setAccess(D->getAccess());
1709 Owner->addDecl(NewUD);
1710
John McCall9f54ad42009-12-10 09:41:52 +00001711 // Don't process the shadow decls for an invalid decl.
1712 if (NewUD->isInvalidDecl())
1713 return NewUD;
1714
John McCall323c3102009-12-22 22:26:37 +00001715 bool isFunctionScope = Owner->isFunctionOrMethod();
1716
John McCall9f54ad42009-12-10 09:41:52 +00001717 // Process the shadow decls.
1718 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
1719 I != E; ++I) {
1720 UsingShadowDecl *Shadow = *I;
1721 NamedDecl *InstTarget =
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001722 cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getLocation(),
1723 Shadow->getTargetDecl(),
John McCall9f54ad42009-12-10 09:41:52 +00001724 TemplateArgs));
1725
1726 if (CheckRedeclaration &&
1727 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
1728 continue;
1729
1730 UsingShadowDecl *InstShadow
1731 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
1732 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
John McCall323c3102009-12-22 22:26:37 +00001733
1734 if (isFunctionScope)
1735 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
John McCall9f54ad42009-12-10 09:41:52 +00001736 }
John McCalled976492009-12-04 22:46:56 +00001737
1738 return NewUD;
1739}
1740
1741Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00001742 // Ignore these; we handle them in bulk when processing the UsingDecl.
1743 return 0;
John McCalled976492009-12-04 22:46:56 +00001744}
1745
John McCall7ba107a2009-11-18 02:36:19 +00001746Decl * TemplateDeclInstantiator
1747 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Douglas Gregor5149f372011-02-25 15:54:31 +00001748 NestedNameSpecifierLoc QualifierLoc
1749 = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
1750 TemplateArgs);
1751 if (!QualifierLoc)
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001752 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001753
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001754 CXXScopeSpec SS;
Douglas Gregor5149f372011-02-25 15:54:31 +00001755 SS.Adopt(QualifierLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001756
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001757 // Since NameInfo refers to a typename, it cannot be a C++ special name.
1758 // Hence, no tranformation is required for it.
1759 DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001760 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00001761 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001762 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00001763 /*instantiation*/ true,
1764 /*typename*/ true, D->getTypenameLoc());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001765 if (UD)
John McCalled976492009-12-04 22:46:56 +00001766 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1767
John McCall7ba107a2009-11-18 02:36:19 +00001768 return UD;
1769}
1770
1771Decl * TemplateDeclInstantiator
1772 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Douglas Gregor5149f372011-02-25 15:54:31 +00001773 NestedNameSpecifierLoc QualifierLoc
1774 = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), TemplateArgs);
1775 if (!QualifierLoc)
John McCall7ba107a2009-11-18 02:36:19 +00001776 return 0;
Douglas Gregor5149f372011-02-25 15:54:31 +00001777
John McCall7ba107a2009-11-18 02:36:19 +00001778 CXXScopeSpec SS;
Douglas Gregor5149f372011-02-25 15:54:31 +00001779 SS.Adopt(QualifierLoc);
John McCall7ba107a2009-11-18 02:36:19 +00001780
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001781 DeclarationNameInfo NameInfo
1782 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
1783
John McCall7ba107a2009-11-18 02:36:19 +00001784 NamedDecl *UD =
1785 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001786 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00001787 /*instantiation*/ true,
1788 /*typename*/ false, SourceLocation());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001789 if (UD)
John McCalled976492009-12-04 22:46:56 +00001790 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1791
Anders Carlsson0d8df782009-08-29 19:37:28 +00001792 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001793}
1794
John McCallce3ff2b2009-08-25 22:02:44 +00001795Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001796 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001797 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor2fa98002010-02-16 19:28:15 +00001798 if (D->isInvalidDecl())
1799 return 0;
1800
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001801 return Instantiator.Visit(D);
1802}
1803
John McCalle29ba202009-08-20 01:44:21 +00001804/// \brief Instantiates a nested template parameter list in the current
1805/// instantiation context.
1806///
1807/// \param L The parameter list to instantiate
1808///
1809/// \returns NULL if there was an error
1810TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001811TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001812 // Get errors for all the parameters before bailing out.
1813 bool Invalid = false;
1814
1815 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001816 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001817 ParamVector Params;
1818 Params.reserve(N);
1819 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1820 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001821 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001822 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001823 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00001824 }
1825
1826 // Clean up if we had an error.
Douglas Gregorff331c12010-07-25 18:17:45 +00001827 if (Invalid)
John McCalle29ba202009-08-20 01:44:21 +00001828 return NULL;
John McCalle29ba202009-08-20 01:44:21 +00001829
1830 TemplateParameterList *InstL
1831 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1832 L->getLAngleLoc(), &Params.front(), N,
1833 L->getRAngleLoc());
1834 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001835}
John McCalle29ba202009-08-20 01:44:21 +00001836
Douglas Gregored9c0f92009-10-29 00:04:11 +00001837/// \brief Instantiate the declaration of a class template partial
1838/// specialization.
1839///
1840/// \param ClassTemplate the (instantiated) class template that is partially
1841// specialized by the instantiation of \p PartialSpec.
1842///
1843/// \param PartialSpec the (uninstantiated) class template partial
1844/// specialization that we are instantiating.
1845///
Douglas Gregord65587f2010-11-10 19:44:59 +00001846/// \returns The instantiated partial specialization, if successful; otherwise,
1847/// NULL to indicate an error.
1848ClassTemplatePartialSpecializationDecl *
Douglas Gregored9c0f92009-10-29 00:04:11 +00001849TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1850 ClassTemplateDecl *ClassTemplate,
1851 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001852 // Create a local instantiation scope for this class template partial
1853 // specialization, which will contain the instantiations of the template
1854 // parameters.
John McCall2a7fb272010-08-25 05:32:35 +00001855 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor550d9b22009-10-31 17:21:17 +00001856
Douglas Gregored9c0f92009-10-29 00:04:11 +00001857 // Substitute into the template parameters of the class template partial
1858 // specialization.
1859 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1860 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1861 if (!InstParams)
Douglas Gregord65587f2010-11-10 19:44:59 +00001862 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001863
1864 // Substitute into the template arguments of the class template partial
1865 // specialization.
John McCalld5532b62009-11-23 01:53:49 +00001866 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
Douglas Gregore02e2622010-12-22 21:19:48 +00001867 if (SemaRef.Subst(PartialSpec->getTemplateArgsAsWritten(),
1868 PartialSpec->getNumTemplateArgsAsWritten(),
1869 InstTemplateArgs, TemplateArgs))
1870 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001871
Douglas Gregored9c0f92009-10-29 00:04:11 +00001872 // Check that the template argument list is well-formed for this
1873 // class template.
Douglas Gregor910f8002010-11-07 23:05:16 +00001874 llvm::SmallVector<TemplateArgument, 4> Converted;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001875 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1876 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001877 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001878 false,
1879 Converted))
Douglas Gregord65587f2010-11-10 19:44:59 +00001880 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001881
1882 // Figure out where to insert this class template partial specialization
1883 // in the member template's set of class template partial specializations.
Douglas Gregored9c0f92009-10-29 00:04:11 +00001884 void *InsertPos = 0;
1885 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00001886 = ClassTemplate->findPartialSpecialization(Converted.data(),
1887 Converted.size(), InsertPos);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001888
1889 // Build the canonical type that describes the converted template
1890 // arguments of the class template partial specialization.
1891 QualType CanonType
1892 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
Douglas Gregor910f8002010-11-07 23:05:16 +00001893 Converted.data(),
1894 Converted.size());
Douglas Gregored9c0f92009-10-29 00:04:11 +00001895
1896 // Build the fully-sugared type for this class template
1897 // specialization as the user wrote in the specialization
1898 // itself. This means that we'll pretty-print the type retrieved
1899 // from the specialization's declaration the way that the user
1900 // actually wrote the specialization, rather than formatting the
1901 // name based on the "canonical" representation used to store the
1902 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00001903 TypeSourceInfo *WrittenTy
1904 = SemaRef.Context.getTemplateSpecializationTypeInfo(
1905 TemplateName(ClassTemplate),
1906 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001907 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001908 CanonType);
1909
1910 if (PrevDecl) {
1911 // We've already seen a partial specialization with the same template
1912 // parameters and template arguments. This can happen, for example, when
1913 // substituting the outer template arguments ends up causing two
1914 // class template partial specializations of a member class template
1915 // to have identical forms, e.g.,
1916 //
1917 // template<typename T, typename U>
1918 // struct Outer {
1919 // template<typename X, typename Y> struct Inner;
1920 // template<typename Y> struct Inner<T, Y>;
1921 // template<typename Y> struct Inner<U, Y>;
1922 // };
1923 //
1924 // Outer<int, int> outer; // error: the partial specializations of Inner
1925 // // have the same signature.
1926 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
Douglas Gregord65587f2010-11-10 19:44:59 +00001927 << WrittenTy->getType();
Douglas Gregored9c0f92009-10-29 00:04:11 +00001928 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1929 << SemaRef.Context.getTypeDeclType(PrevDecl);
Douglas Gregord65587f2010-11-10 19:44:59 +00001930 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001931 }
1932
1933
1934 // Create the class template partial specialization declaration.
1935 ClassTemplatePartialSpecializationDecl *InstPartialSpec
Douglas Gregor13c85772010-05-06 00:28:52 +00001936 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
1937 PartialSpec->getTagKind(),
1938 Owner,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001939 PartialSpec->getLocation(),
1940 InstParams,
1941 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001942 Converted.data(),
1943 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00001944 InstTemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00001945 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00001946 0,
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001947 ClassTemplate->getNextPartialSpecSequenceNumber());
John McCallb6217662010-03-15 10:12:16 +00001948 // Substitute the nested name specifier, if any.
1949 if (SubstQualifier(PartialSpec, InstPartialSpec))
1950 return 0;
1951
Douglas Gregored9c0f92009-10-29 00:04:11 +00001952 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001953 InstPartialSpec->setTypeAsWritten(WrittenTy);
1954
Douglas Gregored9c0f92009-10-29 00:04:11 +00001955 // Add this partial specialization to the set of class template partial
1956 // specializations.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001957 ClassTemplate->AddPartialSpecialization(InstPartialSpec, InsertPos);
Douglas Gregord65587f2010-11-10 19:44:59 +00001958 return InstPartialSpec;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001959}
1960
John McCall21ef0fa2010-03-11 09:03:00 +00001961TypeSourceInfo*
John McCallce3ff2b2009-08-25 22:02:44 +00001962TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001963 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
John McCall21ef0fa2010-03-11 09:03:00 +00001964 TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
1965 assert(OldTInfo && "substituting function without type source info");
1966 assert(Params.empty() && "parameter vector is non-empty at start");
John McCall6cd3b9f2010-04-09 17:38:44 +00001967 TypeSourceInfo *NewTInfo
1968 = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
1969 D->getTypeSpecStartLoc(),
1970 D->getDeclName());
John McCall21ef0fa2010-03-11 09:03:00 +00001971 if (!NewTInfo)
1972 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +00001973
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001974 if (NewTInfo != OldTInfo) {
1975 // Get parameters from the new type info.
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00001976 TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001977 if (FunctionProtoTypeLoc *OldProtoLoc
1978 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00001979 TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001980 FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
1981 assert(NewProtoLoc && "Missing prototype?");
Douglas Gregor12c9c002011-01-07 16:43:16 +00001982 unsigned NewIdx = 0, NumNewParams = NewProtoLoc->getNumArgs();
1983 for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc->getNumArgs();
1984 OldIdx != NumOldParams; ++OldIdx) {
1985 ParmVarDecl *OldParam = OldProtoLoc->getArg(OldIdx);
1986 if (!OldParam->isParameterPack() ||
1987 (NewIdx < NumNewParams &&
1988 NewProtoLoc->getArg(NewIdx)->isParameterPack())) {
1989 // Simple case: normal parameter, or a parameter pack that's
1990 // instantiated to a (still-dependent) parameter pack.
1991 ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++);
1992 Params.push_back(NewParam);
1993 SemaRef.CurrentInstantiationScope->InstantiatedLocal(OldParam,
1994 NewParam);
1995 continue;
1996 }
1997
1998 // Parameter pack: make the instantiation an argument pack.
1999 SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(
2000 OldParam);
Douglas Gregor21371ea2011-01-11 03:14:20 +00002001 unsigned NumArgumentsInExpansion
2002 = SemaRef.getNumArgumentsInExpansion(OldParam->getType(),
2003 TemplateArgs);
2004 while (NumArgumentsInExpansion--) {
Douglas Gregor12c9c002011-01-07 16:43:16 +00002005 ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++);
2006 Params.push_back(NewParam);
2007 SemaRef.CurrentInstantiationScope->InstantiatedLocalPackArg(OldParam,
2008 NewParam);
2009 }
Douglas Gregor6920cdc2010-05-03 15:32:18 +00002010 }
Douglas Gregor895162d2010-04-30 18:55:50 +00002011 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00002012 } else {
2013 // The function type itself was not dependent and therefore no
2014 // substitution occurred. However, we still need to instantiate
2015 // the function parameters themselves.
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00002016 TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00002017 if (FunctionProtoTypeLoc *OldProtoLoc
2018 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
2019 for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
2020 ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
2021 if (!Parm)
2022 return 0;
2023 Params.push_back(Parm);
2024 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00002025 }
2026 }
John McCall21ef0fa2010-03-11 09:03:00 +00002027 return NewTInfo;
Douglas Gregor5545e162009-03-24 00:38:23 +00002028}
2029
Mike Stump1eb44332009-09-09 15:08:12 +00002030/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00002031/// declaration (New) from the corresponding fields of its template (Tmpl).
2032///
2033/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00002034bool
2035TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00002036 FunctionDecl *Tmpl) {
2037 if (Tmpl->isDeleted())
2038 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00002039
Douglas Gregorcca9e962009-07-01 22:01:06 +00002040 // If we are performing substituting explicitly-specified template arguments
2041 // or deduced template arguments into a function template and we reach this
2042 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00002043 // to keeping the new function template specialization. We therefore
2044 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00002045 // into a template instantiation for this specific function template
2046 // specialization, which is not a SFINAE context, so that we diagnose any
2047 // further errors in the declaration itself.
2048 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
2049 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
2050 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
2051 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00002052 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00002053 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002054 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00002055 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00002056 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00002057 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
2058 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
Douglas Gregorf35f8282009-11-11 21:54:23 +00002059 --SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +00002060 }
2061 }
Mike Stump1eb44332009-09-09 15:08:12 +00002062
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002063 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
2064 assert(Proto && "Function template without prototype?");
2065
2066 if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
2067 Proto->getNoReturnAttr()) {
2068 // The function has an exception specification or a "noreturn"
2069 // attribute. Substitute into each of the exception types.
2070 llvm::SmallVector<QualType, 4> Exceptions;
2071 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
2072 // FIXME: Poor location information!
Douglas Gregorb99268b2010-12-21 00:52:54 +00002073 if (const PackExpansionType *PackExpansion
2074 = Proto->getExceptionType(I)->getAs<PackExpansionType>()) {
2075 // We have a pack expansion. Instantiate it.
2076 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2077 SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(),
2078 Unexpanded);
2079 assert(!Unexpanded.empty() &&
2080 "Pack expansion without parameter packs?");
2081
2082 bool Expand = false;
Douglas Gregord3731192011-01-10 07:32:04 +00002083 bool RetainExpansion = false;
Douglas Gregorcded4f62011-01-14 17:04:44 +00002084 llvm::Optional<unsigned> NumExpansions
2085 = PackExpansion->getNumExpansions();
Douglas Gregorb99268b2010-12-21 00:52:54 +00002086 if (SemaRef.CheckParameterPacksForExpansion(New->getLocation(),
2087 SourceRange(),
2088 Unexpanded.data(),
2089 Unexpanded.size(),
2090 TemplateArgs,
Douglas Gregord3731192011-01-10 07:32:04 +00002091 Expand,
2092 RetainExpansion,
2093 NumExpansions))
Douglas Gregorb99268b2010-12-21 00:52:54 +00002094 break;
2095
2096 if (!Expand) {
2097 // We can't expand this pack expansion into separate arguments yet;
Douglas Gregorcded4f62011-01-14 17:04:44 +00002098 // just substitute into the pattern and create a new pack expansion
2099 // type.
Douglas Gregorb99268b2010-12-21 00:52:54 +00002100 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
2101 QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2102 TemplateArgs,
2103 New->getLocation(), New->getDeclName());
2104 if (T.isNull())
2105 break;
2106
Douglas Gregorcded4f62011-01-14 17:04:44 +00002107 T = SemaRef.Context.getPackExpansionType(T, NumExpansions);
Douglas Gregorb99268b2010-12-21 00:52:54 +00002108 Exceptions.push_back(T);
2109 continue;
2110 }
2111
2112 // Substitute into the pack expansion pattern for each template
2113 bool Invalid = false;
Douglas Gregorcded4f62011-01-14 17:04:44 +00002114 for (unsigned ArgIdx = 0; ArgIdx != *NumExpansions; ++ArgIdx) {
Douglas Gregorb99268b2010-12-21 00:52:54 +00002115 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, ArgIdx);
2116
2117 QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2118 TemplateArgs,
2119 New->getLocation(), New->getDeclName());
2120 if (T.isNull()) {
2121 Invalid = true;
2122 break;
2123 }
2124
2125 Exceptions.push_back(T);
2126 }
2127
2128 if (Invalid)
2129 break;
2130
2131 continue;
2132 }
2133
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002134 QualType T
2135 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
2136 New->getLocation(), New->getDeclName());
2137 if (T.isNull() ||
2138 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
2139 continue;
2140
2141 Exceptions.push_back(T);
2142 }
2143
2144 // Rebuild the function type
2145
John McCalle23cf432010-12-14 08:05:40 +00002146 FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
2147 EPI.HasExceptionSpec = Proto->hasExceptionSpec();
2148 EPI.HasAnyExceptionSpec = Proto->hasAnyExceptionSpec();
2149 EPI.NumExceptions = Exceptions.size();
2150 EPI.Exceptions = Exceptions.data();
2151 EPI.ExtInfo = Proto->getExtInfo();
2152
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002153 const FunctionProtoType *NewProto
2154 = New->getType()->getAs<FunctionProtoType>();
2155 assert(NewProto && "Template instantiation without function prototype?");
2156 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
2157 NewProto->arg_type_begin(),
2158 NewProto->getNumArgs(),
John McCalle23cf432010-12-14 08:05:40 +00002159 EPI));
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002160 }
2161
John McCall1d8d1cc2010-08-01 02:01:53 +00002162 SemaRef.InstantiateAttrs(TemplateArgs, Tmpl, New);
Douglas Gregor7cf84d62010-06-15 17:05:35 +00002163
Douglas Gregore53060f2009-06-25 22:08:12 +00002164 return false;
2165}
2166
Douglas Gregor5545e162009-03-24 00:38:23 +00002167/// \brief Initializes common fields of an instantiated method
2168/// declaration (New) from the corresponding fields of its template
2169/// (Tmpl).
2170///
2171/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00002172bool
2173TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00002174 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00002175 if (InitFunctionInstantiation(New, Tmpl))
2176 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002177
Douglas Gregor5545e162009-03-24 00:38:23 +00002178 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00002179 if (Tmpl->isVirtualAsWritten())
Douglas Gregor85606eb2010-09-28 20:50:54 +00002180 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +00002181
2182 // FIXME: attributes
2183 // FIXME: New needs a pointer to Tmpl
2184 return false;
2185}
Douglas Gregora58861f2009-05-13 20:28:22 +00002186
2187/// \brief Instantiate the definition of the given function from its
2188/// template.
2189///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002190/// \param PointOfInstantiation the point at which the instantiation was
2191/// required. Note that this is not precisely a "point of instantiation"
2192/// for the function, but it's close.
2193///
Douglas Gregora58861f2009-05-13 20:28:22 +00002194/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002195/// function template specialization or member function of a class template
2196/// specialization.
2197///
2198/// \param Recursive if true, recursively instantiates any functions that
2199/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002200///
2201/// \param DefinitionRequired if true, then we are performing an explicit
2202/// instantiation where the body of the function is required. Complain if
2203/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002204void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002205 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002206 bool Recursive,
2207 bool DefinitionRequired) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002208 if (Function->isInvalidDecl() || Function->hasBody())
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002209 return;
2210
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002211 // Never instantiate an explicit specialization.
2212 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2213 return;
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002214
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002215 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00002216 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002217 Stmt *Pattern = 0;
2218 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00002219 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002220
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002221 if (!Pattern) {
2222 if (DefinitionRequired) {
2223 if (Function->getPrimaryTemplate())
2224 Diag(PointOfInstantiation,
2225 diag::err_explicit_instantiation_undefined_func_template)
2226 << Function->getPrimaryTemplate();
2227 else
2228 Diag(PointOfInstantiation,
2229 diag::err_explicit_instantiation_undefined_member)
2230 << 1 << Function->getDeclName() << Function->getDeclContext();
2231
2232 if (PatternDecl)
2233 Diag(PatternDecl->getLocation(),
2234 diag::note_explicit_instantiation_here);
Douglas Gregorcfe833b2010-05-17 17:57:54 +00002235 Function->setInvalidDecl();
Chandler Carruth58e390e2010-08-25 08:27:02 +00002236 } else if (Function->getTemplateSpecializationKind()
2237 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002238 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00002239 std::make_pair(Function, PointOfInstantiation));
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002240 }
Chandler Carruth58e390e2010-08-25 08:27:02 +00002241
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002242 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002243 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002244
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002245 // C++0x [temp.explicit]p9:
2246 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00002247 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002248 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00002249 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002250 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002251 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002252 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002253
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002254 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2255 if (Inst)
Douglas Gregore7089b02010-05-03 23:29:10 +00002256 return;
2257
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002258 // If we're performing recursive template instantiation, create our own
2259 // queue of pending implicit instantiations that we will instantiate later,
2260 // while we're still within our own instantiation context.
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002261 llvm::SmallVector<VTableUse, 16> SavedVTableUses;
Chandler Carruth62c78d52010-08-25 08:44:16 +00002262 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002263 if (Recursive) {
2264 VTableUses.swap(SavedVTableUses);
Chandler Carruth62c78d52010-08-25 08:44:16 +00002265 PendingInstantiations.swap(SavedPendingInstantiations);
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002266 }
Mike Stump1eb44332009-09-09 15:08:12 +00002267
Douglas Gregor9679caf2010-05-12 17:27:19 +00002268 EnterExpressionEvaluationContext EvalContext(*this,
John McCallf312b1e2010-08-26 23:41:50 +00002269 Sema::PotentiallyEvaluated);
John McCalld226f652010-08-21 09:40:31 +00002270 ActOnStartOfFunctionDef(0, Function);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002271
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002272 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00002273 // recorded, unless we're actually a member function within a local
2274 // class, in which case we need to merge our results with the parent
2275 // scope (of the enclosing function).
2276 bool MergeWithParentScope = false;
2277 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
2278 MergeWithParentScope = Rec->isLocalClass();
2279
2280 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002281
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002282 // Introduce the instantiated function parameters into the local
Peter Collingbourne8a6c0f12010-07-18 16:45:46 +00002283 // instantiation scope, and set the parameter names to those used
2284 // in the template.
Douglas Gregor12c9c002011-01-07 16:43:16 +00002285 unsigned FParamIdx = 0;
Peter Collingbourne8a6c0f12010-07-18 16:45:46 +00002286 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
2287 const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
Douglas Gregor12c9c002011-01-07 16:43:16 +00002288 if (!PatternParam->isParameterPack()) {
2289 // Simple case: not a parameter pack.
2290 assert(FParamIdx < Function->getNumParams());
2291 ParmVarDecl *FunctionParam = Function->getParamDecl(I);
2292 FunctionParam->setDeclName(PatternParam->getDeclName());
2293 Scope.InstantiatedLocal(PatternParam, FunctionParam);
2294 ++FParamIdx;
2295 continue;
2296 }
2297
2298 // Expand the parameter pack.
2299 Scope.MakeInstantiatedLocalArgPack(PatternParam);
2300 for (unsigned NumFParams = Function->getNumParams();
2301 FParamIdx < NumFParams;
2302 ++FParamIdx) {
2303 ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
2304 FunctionParam->setDeclName(PatternParam->getDeclName());
2305 Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam);
2306 }
Peter Collingbourne8a6c0f12010-07-18 16:45:46 +00002307 }
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002308
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002309 // Enter the scope of this instantiation. We don't use
2310 // PushDeclContext because we don't have a scope.
John McCalleee1d542011-02-14 07:13:47 +00002311 Sema::ContextRAII savedContext(*this, Function);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002312
Mike Stump1eb44332009-09-09 15:08:12 +00002313 MultiLevelTemplateArgumentList TemplateArgs =
Douglas Gregore7089b02010-05-03 23:29:10 +00002314 getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
Anders Carlsson09025312009-08-29 05:16:22 +00002315
2316 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00002317 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00002318 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2319 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2320 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002321 }
2322
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002323 // Instantiate the function body.
John McCall60d7b3a2010-08-24 06:29:42 +00002324 StmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002325
Douglas Gregor52604ab2009-09-11 21:19:12 +00002326 if (Body.isInvalid())
2327 Function->setInvalidDecl();
2328
John McCall9ae2f072010-08-23 23:25:46 +00002329 ActOnFinishFunctionBody(Function, Body.get(),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002330 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002331
John McCall0c01d182010-03-24 05:22:00 +00002332 PerformDependentDiagnostics(PatternDecl, TemplateArgs);
2333
John McCalleee1d542011-02-14 07:13:47 +00002334 savedContext.pop();
Douglas Gregoraba43bb2009-05-26 20:50:29 +00002335
2336 DeclGroupRef DG(Function);
2337 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00002338
Douglas Gregor60406be2010-01-16 22:29:39 +00002339 // This class may have local implicit instantiations that need to be
2340 // instantiation within this scope.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002341 PerformPendingInstantiations(/*LocalOnly=*/true);
Douglas Gregor60406be2010-01-16 22:29:39 +00002342 Scope.Exit();
2343
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002344 if (Recursive) {
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002345 // Define any pending vtables.
2346 DefineUsedVTables();
2347
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002348 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002349 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002350 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002351
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002352 // Restore the set of pending vtables.
2353 VTableUses.swap(SavedVTableUses);
2354
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002355 // Restore the set of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002356 PendingInstantiations.swap(SavedPendingInstantiations);
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002357 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002358}
2359
2360/// \brief Instantiate the definition of the given variable from its
2361/// template.
2362///
Douglas Gregor7caa6822009-07-24 20:34:43 +00002363/// \param PointOfInstantiation the point at which the instantiation was
2364/// required. Note that this is not precisely a "point of instantiation"
2365/// for the function, but it's close.
2366///
2367/// \param Var the already-instantiated declaration of a static member
2368/// variable of a class template specialization.
2369///
2370/// \param Recursive if true, recursively instantiates any functions that
2371/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002372///
2373/// \param DefinitionRequired if true, then we are performing an explicit
2374/// instantiation where an out-of-line definition of the member variable
2375/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002376void Sema::InstantiateStaticDataMemberDefinition(
2377 SourceLocation PointOfInstantiation,
2378 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002379 bool Recursive,
2380 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002381 if (Var->isInvalidDecl())
2382 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002383
Douglas Gregor7caa6822009-07-24 20:34:43 +00002384 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002385 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002386 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00002387 assert(Def->isStaticDataMember() && "Not a static data member?");
2388 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002389
Douglas Gregor0d035142009-10-27 18:42:08 +00002390 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002391 // We did not find an out-of-line definition of this static data member,
2392 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00002393 // instantiate this definition (or provide a specialization for it) in
2394 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002395 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00002396 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002397 Diag(PointOfInstantiation,
2398 diag::err_explicit_instantiation_undefined_member)
2399 << 2 << Var->getDeclName() << Var->getDeclContext();
2400 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
Chandler Carruth58e390e2010-08-25 08:27:02 +00002401 } else if (Var->getTemplateSpecializationKind()
2402 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002403 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00002404 std::make_pair(Var, PointOfInstantiation));
2405 }
2406
Douglas Gregor7caa6822009-07-24 20:34:43 +00002407 return;
2408 }
2409
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002410 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002411 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002412 return;
2413
2414 // C++0x [temp.explicit]p9:
2415 // Except for inline functions, other explicit instantiation declarations
2416 // have the effect of suppressing the implicit instantiation of the entity
2417 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002418 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002419 == TSK_ExplicitInstantiationDeclaration)
2420 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002421
Douglas Gregor7caa6822009-07-24 20:34:43 +00002422 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
2423 if (Inst)
2424 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002425
Douglas Gregor7caa6822009-07-24 20:34:43 +00002426 // If we're performing recursive template instantiation, create our own
2427 // queue of pending implicit instantiations that we will instantiate later,
2428 // while we're still within our own instantiation context.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002429 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Douglas Gregor7caa6822009-07-24 20:34:43 +00002430 if (Recursive)
Chandler Carruth62c78d52010-08-25 08:44:16 +00002431 PendingInstantiations.swap(SavedPendingInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002432
Douglas Gregor7caa6822009-07-24 20:34:43 +00002433 // Enter the scope of this instantiation. We don't use
2434 // PushDeclContext because we don't have a scope.
John McCallf5ba7e02011-02-14 20:37:25 +00002435 ContextRAII previousContext(*this, Var->getDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +00002436
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002437 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00002438 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Nico Weber6bb4dcb2010-11-28 22:53:37 +00002439 getTemplateInstantiationArgs(Var)));
John McCallf5ba7e02011-02-14 20:37:25 +00002440
2441 previousContext.pop();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002442
2443 if (Var) {
Douglas Gregor583f33b2009-10-15 18:07:02 +00002444 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2445 assert(MSInfo && "Missing member specialization information?");
2446 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2447 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00002448 DeclGroupRef DG(Var);
2449 Consumer.HandleTopLevelDecl(DG);
2450 }
Mike Stump1eb44332009-09-09 15:08:12 +00002451
Douglas Gregor7caa6822009-07-24 20:34:43 +00002452 if (Recursive) {
2453 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002454 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002455 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002456
Douglas Gregor7caa6822009-07-24 20:34:43 +00002457 // Restore the set of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002458 PendingInstantiations.swap(SavedPendingInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002459 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002460}
Douglas Gregor815215d2009-05-27 05:35:12 +00002461
Anders Carlsson09025312009-08-29 05:16:22 +00002462void
2463Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
2464 const CXXConstructorDecl *Tmpl,
2465 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00002466
Anders Carlsson09025312009-08-29 05:16:22 +00002467 llvm::SmallVector<MemInitTy*, 4> NewInits;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002468 bool AnyErrors = false;
2469
Anders Carlsson09025312009-08-29 05:16:22 +00002470 // Instantiate all the initializers.
2471 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00002472 InitsEnd = Tmpl->init_end();
2473 Inits != InitsEnd; ++Inits) {
Sean Huntcbb67482011-01-08 20:30:50 +00002474 CXXCtorInitializer *Init = *Inits;
Anders Carlsson09025312009-08-29 05:16:22 +00002475
Chandler Carruth030ef472010-09-03 21:54:20 +00002476 // Only instantiate written initializers, let Sema re-construct implicit
2477 // ones.
2478 if (!Init->isWritten())
2479 continue;
2480
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002481 SourceLocation LParenLoc, RParenLoc;
John McCallca0408f2010-08-23 06:44:23 +00002482 ASTOwningVector<Expr*> NewArgs(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002483
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002484 SourceLocation EllipsisLoc;
2485
2486 if (Init->isPackExpansion()) {
2487 // This is a pack expansion. We should expand it now.
2488 TypeLoc BaseTL = Init->getBaseClassInfo()->getTypeLoc();
2489 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2490 collectUnexpandedParameterPacks(BaseTL, Unexpanded);
2491 bool ShouldExpand = false;
Douglas Gregord3731192011-01-10 07:32:04 +00002492 bool RetainExpansion = false;
Douglas Gregorcded4f62011-01-14 17:04:44 +00002493 llvm::Optional<unsigned> NumExpansions;
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002494 if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(),
2495 BaseTL.getSourceRange(),
2496 Unexpanded.data(),
2497 Unexpanded.size(),
2498 TemplateArgs, ShouldExpand,
Douglas Gregord3731192011-01-10 07:32:04 +00002499 RetainExpansion,
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002500 NumExpansions)) {
2501 AnyErrors = true;
2502 New->setInvalidDecl();
2503 continue;
2504 }
2505 assert(ShouldExpand && "Partial instantiation of base initializer?");
2506
2507 // Loop over all of the arguments in the argument pack(s),
Douglas Gregorcded4f62011-01-14 17:04:44 +00002508 for (unsigned I = 0; I != *NumExpansions; ++I) {
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002509 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
2510
2511 // Instantiate the initializer.
2512 if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
2513 LParenLoc, NewArgs, RParenLoc)) {
2514 AnyErrors = true;
2515 break;
2516 }
2517
2518 // Instantiate the base type.
2519 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
2520 TemplateArgs,
2521 Init->getSourceLocation(),
2522 New->getDeclName());
2523 if (!BaseTInfo) {
2524 AnyErrors = true;
2525 break;
2526 }
2527
2528 // Build the initializer.
2529 MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(),
2530 BaseTInfo,
2531 (Expr **)NewArgs.data(),
2532 NewArgs.size(),
2533 Init->getLParenLoc(),
2534 Init->getRParenLoc(),
2535 New->getParent(),
2536 SourceLocation());
2537 if (NewInit.isInvalid()) {
2538 AnyErrors = true;
2539 break;
2540 }
2541
2542 NewInits.push_back(NewInit.get());
2543 NewArgs.clear();
2544 }
2545
2546 continue;
2547 }
2548
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002549 // Instantiate the initializer.
2550 if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
Douglas Gregora1a04782010-09-09 16:33:13 +00002551 LParenLoc, NewArgs, RParenLoc)) {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002552 AnyErrors = true;
2553 continue;
Anders Carlsson09025312009-08-29 05:16:22 +00002554 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002555
Anders Carlsson09025312009-08-29 05:16:22 +00002556 MemInitResult NewInit;
Anders Carlsson09025312009-08-29 05:16:22 +00002557 if (Init->isBaseInitializer()) {
John McCalla93c9342009-12-07 02:54:59 +00002558 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002559 TemplateArgs,
2560 Init->getSourceLocation(),
2561 New->getDeclName());
John McCalla93c9342009-12-07 02:54:59 +00002562 if (!BaseTInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002563 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00002564 New->setInvalidDecl();
2565 continue;
2566 }
2567
John McCalla93c9342009-12-07 02:54:59 +00002568 NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00002569 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002570 NewArgs.size(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002571 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002572 Init->getRParenLoc(),
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002573 New->getParent(),
2574 EllipsisLoc);
Anders Carlsson09025312009-08-29 05:16:22 +00002575 } else if (Init->isMemberInitializer()) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00002576 FieldDecl *Member = cast<FieldDecl>(FindInstantiatedDecl(
2577 Init->getMemberLocation(),
2578 Init->getMember(),
2579 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00002580
2581 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002582 NewArgs.size(),
2583 Init->getSourceLocation(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002584 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002585 Init->getRParenLoc());
Francois Pichet00eb3f92010-12-04 09:14:42 +00002586 } else if (Init->isIndirectMemberInitializer()) {
2587 IndirectFieldDecl *IndirectMember =
2588 cast<IndirectFieldDecl>(FindInstantiatedDecl(
2589 Init->getMemberLocation(),
2590 Init->getIndirectMember(), TemplateArgs));
2591
2592 NewInit = BuildMemberInitializer(IndirectMember, (Expr **)NewArgs.data(),
2593 NewArgs.size(),
2594 Init->getSourceLocation(),
2595 Init->getLParenLoc(),
2596 Init->getRParenLoc());
Anders Carlsson09025312009-08-29 05:16:22 +00002597 }
2598
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002599 if (NewInit.isInvalid()) {
2600 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00002601 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002602 } else {
Anders Carlsson09025312009-08-29 05:16:22 +00002603 // FIXME: It would be nice if ASTOwningVector had a release function.
2604 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00002605
Anders Carlsson09025312009-08-29 05:16:22 +00002606 NewInits.push_back((MemInitTy *)NewInit.get());
2607 }
2608 }
Mike Stump1eb44332009-09-09 15:08:12 +00002609
Anders Carlsson09025312009-08-29 05:16:22 +00002610 // Assign all the initializers to the new constructor.
John McCalld226f652010-08-21 09:40:31 +00002611 ActOnMemInitializers(New,
Anders Carlsson09025312009-08-29 05:16:22 +00002612 /*FIXME: ColonLoc */
2613 SourceLocation(),
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002614 NewInits.data(), NewInits.size(),
2615 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00002616}
2617
John McCall52a575a2009-08-29 08:11:13 +00002618// TODO: this could be templated if the various decl types used the
2619// same method name.
2620static bool isInstantiationOf(ClassTemplateDecl *Pattern,
2621 ClassTemplateDecl *Instance) {
2622 Pattern = Pattern->getCanonicalDecl();
2623
2624 do {
2625 Instance = Instance->getCanonicalDecl();
2626 if (Pattern == Instance) return true;
2627 Instance = Instance->getInstantiatedFromMemberTemplate();
2628 } while (Instance);
2629
2630 return false;
2631}
2632
Douglas Gregor0d696532009-09-28 06:34:35 +00002633static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
2634 FunctionTemplateDecl *Instance) {
2635 Pattern = Pattern->getCanonicalDecl();
2636
2637 do {
2638 Instance = Instance->getCanonicalDecl();
2639 if (Pattern == Instance) return true;
2640 Instance = Instance->getInstantiatedFromMemberTemplate();
2641 } while (Instance);
2642
2643 return false;
2644}
2645
Douglas Gregored9c0f92009-10-29 00:04:11 +00002646static bool
2647isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2648 ClassTemplatePartialSpecializationDecl *Instance) {
2649 Pattern
2650 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2651 do {
2652 Instance = cast<ClassTemplatePartialSpecializationDecl>(
2653 Instance->getCanonicalDecl());
2654 if (Pattern == Instance)
2655 return true;
2656 Instance = Instance->getInstantiatedFromMember();
2657 } while (Instance);
2658
2659 return false;
2660}
2661
John McCall52a575a2009-08-29 08:11:13 +00002662static bool isInstantiationOf(CXXRecordDecl *Pattern,
2663 CXXRecordDecl *Instance) {
2664 Pattern = Pattern->getCanonicalDecl();
2665
2666 do {
2667 Instance = Instance->getCanonicalDecl();
2668 if (Pattern == Instance) return true;
2669 Instance = Instance->getInstantiatedFromMemberClass();
2670 } while (Instance);
2671
2672 return false;
2673}
2674
2675static bool isInstantiationOf(FunctionDecl *Pattern,
2676 FunctionDecl *Instance) {
2677 Pattern = Pattern->getCanonicalDecl();
2678
2679 do {
2680 Instance = Instance->getCanonicalDecl();
2681 if (Pattern == Instance) return true;
2682 Instance = Instance->getInstantiatedFromMemberFunction();
2683 } while (Instance);
2684
2685 return false;
2686}
2687
2688static bool isInstantiationOf(EnumDecl *Pattern,
2689 EnumDecl *Instance) {
2690 Pattern = Pattern->getCanonicalDecl();
2691
2692 do {
2693 Instance = Instance->getCanonicalDecl();
2694 if (Pattern == Instance) return true;
2695 Instance = Instance->getInstantiatedFromMemberEnum();
2696 } while (Instance);
2697
2698 return false;
2699}
2700
John McCalled976492009-12-04 22:46:56 +00002701static bool isInstantiationOf(UsingShadowDecl *Pattern,
2702 UsingShadowDecl *Instance,
2703 ASTContext &C) {
2704 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2705}
2706
2707static bool isInstantiationOf(UsingDecl *Pattern,
2708 UsingDecl *Instance,
2709 ASTContext &C) {
2710 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2711}
2712
John McCall7ba107a2009-11-18 02:36:19 +00002713static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
2714 UsingDecl *Instance,
2715 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002716 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00002717}
2718
2719static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00002720 UsingDecl *Instance,
2721 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002722 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00002723}
2724
John McCall52a575a2009-08-29 08:11:13 +00002725static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
2726 VarDecl *Instance) {
2727 assert(Instance->isStaticDataMember());
2728
2729 Pattern = Pattern->getCanonicalDecl();
2730
2731 do {
2732 Instance = Instance->getCanonicalDecl();
2733 if (Pattern == Instance) return true;
2734 Instance = Instance->getInstantiatedFromStaticDataMember();
2735 } while (Instance);
2736
2737 return false;
2738}
2739
John McCalled976492009-12-04 22:46:56 +00002740// Other is the prospective instantiation
2741// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00002742static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002743 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00002744 if (UnresolvedUsingTypenameDecl *UUD
2745 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
2746 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2747 return isInstantiationOf(UUD, UD, Ctx);
2748 }
2749 }
2750
2751 if (UnresolvedUsingValueDecl *UUD
2752 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002753 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2754 return isInstantiationOf(UUD, UD, Ctx);
2755 }
2756 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002757
Anders Carlsson0d8df782009-08-29 19:37:28 +00002758 return false;
2759 }
Mike Stump1eb44332009-09-09 15:08:12 +00002760
John McCall52a575a2009-08-29 08:11:13 +00002761 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
2762 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002763
John McCall52a575a2009-08-29 08:11:13 +00002764 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
2765 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00002766
John McCall52a575a2009-08-29 08:11:13 +00002767 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
2768 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00002769
Douglas Gregor7caa6822009-07-24 20:34:43 +00002770 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00002771 if (Var->isStaticDataMember())
2772 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
2773
2774 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
2775 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00002776
Douglas Gregor0d696532009-09-28 06:34:35 +00002777 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
2778 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
2779
Douglas Gregored9c0f92009-10-29 00:04:11 +00002780 if (ClassTemplatePartialSpecializationDecl *PartialSpec
2781 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2782 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2783 PartialSpec);
2784
Anders Carlssond8b285f2009-09-01 04:26:58 +00002785 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2786 if (!Field->getDeclName()) {
2787 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00002788 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00002789 cast<FieldDecl>(D);
2790 }
2791 }
Mike Stump1eb44332009-09-09 15:08:12 +00002792
John McCalled976492009-12-04 22:46:56 +00002793 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2794 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2795
2796 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2797 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2798
Douglas Gregor815215d2009-05-27 05:35:12 +00002799 return D->getDeclName() && isa<NamedDecl>(Other) &&
2800 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2801}
2802
2803template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00002804static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00002805 NamedDecl *D,
2806 ForwardIterator first,
2807 ForwardIterator last) {
2808 for (; first != last; ++first)
2809 if (isInstantiationOf(Ctx, D, *first))
2810 return cast<NamedDecl>(*first);
2811
2812 return 0;
2813}
2814
John McCall02cace72009-08-28 07:59:38 +00002815/// \brief Finds the instantiation of the given declaration context
2816/// within the current instantiation.
2817///
2818/// \returns NULL if there was an error
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002819DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
Douglas Gregore95b4092009-09-16 18:34:49 +00002820 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00002821 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002822 Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00002823 return cast_or_null<DeclContext>(ID);
2824 } else return DC;
2825}
2826
Douglas Gregored961e72009-05-27 17:54:46 +00002827/// \brief Find the instantiation of the given declaration within the
2828/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00002829///
2830/// This routine is intended to be used when \p D is a declaration
2831/// referenced from within a template, that needs to mapped into the
2832/// corresponding declaration within an instantiation. For example,
2833/// given:
2834///
2835/// \code
2836/// template<typename T>
2837/// struct X {
2838/// enum Kind {
2839/// KnownValue = sizeof(T)
2840/// };
2841///
2842/// bool getKind() const { return KnownValue; }
2843/// };
2844///
2845/// template struct X<int>;
2846/// \endcode
2847///
2848/// In the instantiation of X<int>::getKind(), we need to map the
2849/// EnumConstantDecl for KnownValue (which refers to
2850/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00002851/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2852/// this mapping from within the instantiation of X<int>.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002853NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
Douglas Gregore95b4092009-09-16 18:34:49 +00002854 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00002855 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00002856 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00002857 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
John McCall76672452010-08-19 23:06:02 +00002858 (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext())) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002859 // D is a local of some kind. Look into the map of local
2860 // declarations to their instantiations.
Chris Lattnerd8e54992011-02-17 19:47:42 +00002861 typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
2862 llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found
2863 = CurrentInstantiationScope->findInstantiationOf(D);
Chris Lattnerd8e54992011-02-17 19:47:42 +00002864
Chris Lattner57ad3782011-02-17 20:34:02 +00002865 if (Found) {
2866 if (Decl *FD = Found->dyn_cast<Decl *>())
2867 return cast<NamedDecl>(FD);
2868
2869 unsigned PackIdx = ArgumentPackSubstitutionIndex;
2870 return cast<NamedDecl>((*Found->get<DeclArgumentPack *>())[PackIdx]);
2871 }
2872
2873 // If we didn't find the decl, then we must have a label decl that hasn't
2874 // been found yet. Lazily instantiate it and return it now.
2875 assert(isa<LabelDecl>(D));
Chris Lattnerd8e54992011-02-17 19:47:42 +00002876
Chris Lattner57ad3782011-02-17 20:34:02 +00002877 Decl *Inst = SubstDecl(D, CurContext, TemplateArgs);
2878 assert(Inst && "Failed to instantiate label??");
2879
2880 CurrentInstantiationScope->InstantiatedLocal(D, Inst);
2881 return cast<LabelDecl>(Inst);
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002882 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002883
Douglas Gregore95b4092009-09-16 18:34:49 +00002884 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2885 if (!Record->isDependentContext())
2886 return D;
2887
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002888 // If the RecordDecl is actually the injected-class-name or a
2889 // "templated" declaration for a class template, class template
2890 // partial specialization, or a member class of a class template,
2891 // substitute into the injected-class-name of the class template
2892 // or partial specialization to find the new DeclContext.
Douglas Gregore95b4092009-09-16 18:34:49 +00002893 QualType T;
2894 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2895
2896 if (ClassTemplate) {
Douglas Gregor24bae922010-07-08 18:37:38 +00002897 T = ClassTemplate->getInjectedClassNameSpecialization();
Douglas Gregore95b4092009-09-16 18:34:49 +00002898 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2899 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00002900 ClassTemplate = PartialSpec->getSpecializedTemplate();
John McCall3cb0ebd2010-03-10 03:28:59 +00002901
2902 // If we call SubstType with an InjectedClassNameType here we
2903 // can end up in an infinite loop.
2904 T = Context.getTypeDeclType(Record);
2905 assert(isa<InjectedClassNameType>(T) &&
2906 "type of partial specialization is not an InjectedClassNameType");
John McCall31f17ec2010-04-27 00:57:59 +00002907 T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
John McCall3cb0ebd2010-03-10 03:28:59 +00002908 }
Douglas Gregore95b4092009-09-16 18:34:49 +00002909
2910 if (!T.isNull()) {
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002911 // Substitute into the injected-class-name to get the type
2912 // corresponding to the instantiation we want, which may also be
2913 // the current instantiation (if we're in a template
2914 // definition). This substitution should never fail, since we
2915 // know we can instantiate the injected-class-name or we
2916 // wouldn't have gotten to the injected-class-name!
2917
2918 // FIXME: Can we use the CurrentInstantiationScope to avoid this
2919 // extra instantiation in the common case?
Douglas Gregore95b4092009-09-16 18:34:49 +00002920 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2921 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2922
2923 if (!T->isDependentType()) {
2924 assert(T->isRecordType() && "Instantiation must produce a record type");
2925 return T->getAs<RecordType>()->getDecl();
2926 }
2927
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002928 // We are performing "partial" template instantiation to create
2929 // the member declarations for the members of a class template
2930 // specialization. Therefore, D is actually referring to something
2931 // in the current instantiation. Look through the current
2932 // context, which contains actual instantiations, to find the
2933 // instantiation of the "current instantiation" that D refers
2934 // to.
2935 bool SawNonDependentContext = false;
Mike Stump1eb44332009-09-09 15:08:12 +00002936 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00002937 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002938 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002939 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00002940 if (isInstantiationOf(ClassTemplate,
2941 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00002942 return Spec;
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002943
2944 if (!DC->isDependentContext())
2945 SawNonDependentContext = true;
John McCall52a575a2009-08-29 08:11:13 +00002946 }
2947
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002948 // We're performing "instantiation" of a member of the current
2949 // instantiation while we are type-checking the
2950 // definition. Compute the declaration context and return that.
2951 assert(!SawNonDependentContext &&
2952 "No dependent context while instantiating record");
2953 DeclContext *DC = computeDeclContext(T);
2954 assert(DC &&
John McCall52a575a2009-08-29 08:11:13 +00002955 "Unable to find declaration for the current instantiation");
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002956 return cast<CXXRecordDecl>(DC);
John McCall52a575a2009-08-29 08:11:13 +00002957 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002958
Douglas Gregore95b4092009-09-16 18:34:49 +00002959 // Fall through to deal with other dependent record types (e.g.,
2960 // anonymous unions in class templates).
2961 }
John McCall52a575a2009-08-29 08:11:13 +00002962
Douglas Gregore95b4092009-09-16 18:34:49 +00002963 if (!ParentDC->isDependentContext())
2964 return D;
2965
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002966 ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002967 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00002968 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002969
Douglas Gregor815215d2009-05-27 05:35:12 +00002970 if (ParentDC != D->getDeclContext()) {
2971 // We performed some kind of instantiation in the parent context,
2972 // so now we need to look into the instantiated parent context to
2973 // find the instantiation of the declaration D.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002974
John McCall3cb0ebd2010-03-10 03:28:59 +00002975 // If our context used to be dependent, we may need to instantiate
2976 // it before performing lookup into that context.
2977 if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002978 if (!Spec->isDependentContext()) {
2979 QualType T = Context.getTypeDeclType(Spec);
John McCall3cb0ebd2010-03-10 03:28:59 +00002980 const RecordType *Tag = T->getAs<RecordType>();
2981 assert(Tag && "type of non-dependent record is not a RecordType");
2982 if (!Tag->isBeingDefined() &&
2983 RequireCompleteType(Loc, T, diag::err_incomplete_type))
2984 return 0;
Douglas Gregora43064c2010-11-05 23:22:45 +00002985
2986 ParentDC = Tag->getDecl();
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002987 }
2988 }
2989
Douglas Gregor815215d2009-05-27 05:35:12 +00002990 NamedDecl *Result = 0;
2991 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002992 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00002993 Result = findInstantiationOf(Context, D, Found.first, Found.second);
2994 } else {
2995 // Since we don't have a name for the entity we're looking for,
2996 // our only option is to walk through all of the declarations to
2997 // find that name. This will occur in a few cases:
2998 //
2999 // - anonymous struct/union within a template
3000 // - unnamed class/struct/union/enum within a template
3001 //
3002 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00003003 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003004 ParentDC->decls_begin(),
3005 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00003006 }
Mike Stump1eb44332009-09-09 15:08:12 +00003007
John McCall9f54ad42009-12-10 09:41:52 +00003008 // UsingShadowDecls can instantiate to nothing because of using hiding.
Douglas Gregor00225542010-03-01 18:27:54 +00003009 assert((Result || isa<UsingShadowDecl>(D) || D->isInvalidDecl() ||
3010 cast<Decl>(ParentDC)->isInvalidDecl())
John McCall9f54ad42009-12-10 09:41:52 +00003011 && "Unable to find instantiation of declaration!");
3012
Douglas Gregor815215d2009-05-27 05:35:12 +00003013 D = Result;
3014 }
3015
Douglas Gregor815215d2009-05-27 05:35:12 +00003016 return D;
3017}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003018
Mike Stump1eb44332009-09-09 15:08:12 +00003019/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003020/// instantiations we have seen until this point.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003021void Sema::PerformPendingInstantiations(bool LocalOnly) {
Douglas Gregor60406be2010-01-16 22:29:39 +00003022 while (!PendingLocalImplicitInstantiations.empty() ||
Chandler Carruth62c78d52010-08-25 08:44:16 +00003023 (!LocalOnly && !PendingInstantiations.empty())) {
Douglas Gregor60406be2010-01-16 22:29:39 +00003024 PendingImplicitInstantiation Inst;
3025
3026 if (PendingLocalImplicitInstantiations.empty()) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00003027 Inst = PendingInstantiations.front();
3028 PendingInstantiations.pop_front();
Douglas Gregor60406be2010-01-16 22:29:39 +00003029 } else {
3030 Inst = PendingLocalImplicitInstantiations.front();
3031 PendingLocalImplicitInstantiations.pop_front();
3032 }
Mike Stump1eb44332009-09-09 15:08:12 +00003033
Douglas Gregor7caa6822009-07-24 20:34:43 +00003034 // Instantiate function definitions
3035 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
John McCallf312b1e2010-08-26 23:41:50 +00003036 PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(),
3037 "instantiating function definition");
Chandler Carruth58e390e2010-08-25 08:27:02 +00003038 bool DefinitionRequired = Function->getTemplateSpecializationKind() ==
3039 TSK_ExplicitInstantiationDefinition;
3040 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true,
3041 DefinitionRequired);
Douglas Gregor7caa6822009-07-24 20:34:43 +00003042 continue;
3043 }
Mike Stump1eb44332009-09-09 15:08:12 +00003044
Douglas Gregor7caa6822009-07-24 20:34:43 +00003045 // Instantiate static data member definitions.
3046 VarDecl *Var = cast<VarDecl>(Inst.first);
3047 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00003048
Chandler Carruth291b4412010-02-13 10:17:50 +00003049 // Don't try to instantiate declarations if the most recent redeclaration
3050 // is invalid.
3051 if (Var->getMostRecentDeclaration()->isInvalidDecl())
3052 continue;
3053
3054 // Check if the most recent declaration has changed the specialization kind
3055 // and removed the need for implicit instantiation.
3056 switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
3057 case TSK_Undeclared:
3058 assert(false && "Cannot instantitiate an undeclared specialization.");
3059 case TSK_ExplicitInstantiationDeclaration:
Chandler Carruth291b4412010-02-13 10:17:50 +00003060 case TSK_ExplicitSpecialization:
Chandler Carruth58e390e2010-08-25 08:27:02 +00003061 continue; // No longer need to instantiate this type.
3062 case TSK_ExplicitInstantiationDefinition:
3063 // We only need an instantiation if the pending instantiation *is* the
3064 // explicit instantiation.
3065 if (Var != Var->getMostRecentDeclaration()) continue;
Chandler Carruth291b4412010-02-13 10:17:50 +00003066 case TSK_ImplicitInstantiation:
3067 break;
3068 }
3069
John McCallf312b1e2010-08-26 23:41:50 +00003070 PrettyDeclStackTraceEntry CrashInfo(*this, Var, Var->getLocation(),
3071 "instantiating static data member "
3072 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00003073
Chandler Carruth58e390e2010-08-25 08:27:02 +00003074 bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
3075 TSK_ExplicitInstantiationDefinition;
3076 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true,
3077 DefinitionRequired);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003078 }
3079}
John McCall0c01d182010-03-24 05:22:00 +00003080
3081void Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
3082 const MultiLevelTemplateArgumentList &TemplateArgs) {
3083 for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
3084 E = Pattern->ddiag_end(); I != E; ++I) {
3085 DependentDiagnostic *DD = *I;
3086
3087 switch (DD->getKind()) {
3088 case DependentDiagnostic::Access:
3089 HandleDependentAccessCheck(*DD, TemplateArgs);
3090 break;
3091 }
3092 }
3093}