blob: 73b01271b21e7117cfff14e4d3be6c527c3291b0 [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) {
30 NestedNameSpecifier *OldQual = OldDecl->getQualifier();
31 if (!OldQual) return false;
32
33 SourceRange QualRange = OldDecl->getQualifierRange();
34
35 NestedNameSpecifier *NewQual
36 = SemaRef.SubstNestedNameSpecifier(OldQual, QualRange, TemplateArgs);
37 if (!NewQual)
38 return true;
39
40 NewDecl->setQualifierInfo(NewQual, QualRange);
41 return false;
42}
43
44bool TemplateDeclInstantiator::SubstQualifier(const TagDecl *OldDecl,
45 TagDecl *NewDecl) {
46 NestedNameSpecifier *OldQual = OldDecl->getQualifier();
47 if (!OldQual) return false;
48
49 SourceRange QualRange = OldDecl->getQualifierRange();
50
51 NestedNameSpecifier *NewQual
52 = SemaRef.SubstNestedNameSpecifier(OldQual, QualRange, TemplateArgs);
53 if (!NewQual)
54 return true;
55
56 NewDecl->setQualifierInfo(NewQual, QualRange);
57 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 *
104TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
105 assert(false && "Namespaces cannot be instantiated");
106 return D;
107}
108
John McCall3dbd3d52010-02-16 06:53:13 +0000109Decl *
110TemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
111 NamespaceAliasDecl *Inst
112 = NamespaceAliasDecl::Create(SemaRef.Context, Owner,
113 D->getNamespaceLoc(),
114 D->getAliasLoc(),
115 D->getNamespace()->getIdentifier(),
116 D->getQualifierRange(),
117 D->getQualifier(),
118 D->getTargetNameLoc(),
119 D->getNamespace());
120 Owner->addDecl(Inst);
121 return Inst;
122}
123
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000124Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
125 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000126 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor836adf62010-05-24 17:22:01 +0000127 if (DI->getType()->isDependentType() ||
128 DI->getType()->isVariablyModifiedType()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000129 DI = SemaRef.SubstType(DI, TemplateArgs,
130 D->getLocation(), D->getDeclName());
131 if (!DI) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000132 Invalid = true;
John McCalla93c9342009-12-07 02:54:59 +0000133 DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000134 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000135 } else {
136 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000137 }
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000139 // Create the new typedef
140 TypedefDecl *Typedef
141 = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
John McCallba6a9bd2009-10-24 08:00:42 +0000142 D->getIdentifier(), DI);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000143 if (Invalid)
144 Typedef->setInvalidDecl();
145
John McCallcde5a402011-02-01 08:20:08 +0000146 // If the old typedef was the name for linkage purposes of an anonymous
147 // tag decl, re-establish that relationship for the new typedef.
148 if (const TagType *oldTagType = D->getUnderlyingType()->getAs<TagType>()) {
149 TagDecl *oldTag = oldTagType->getDecl();
150 if (oldTag->getTypedefForAnonDecl() == D) {
151 TagDecl *newTag = DI->getType()->castAs<TagType>()->getDecl();
152 assert(!newTag->getIdentifier() && !newTag->getTypedefForAnonDecl());
153 newTag->setTypedefForAnonDecl(Typedef);
154 }
Douglas Gregord57a38e2010-04-23 16:25:07 +0000155 }
156
John McCall5126fd02009-12-30 00:31:22 +0000157 if (TypedefDecl *Prev = D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000158 NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
159 TemplateArgs);
John McCall5126fd02009-12-30 00:31:22 +0000160 Typedef->setPreviousDeclaration(cast<TypedefDecl>(InstPrev));
161 }
162
John McCall1d8d1cc2010-08-01 02:01:53 +0000163 SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef);
Douglas Gregord57a38e2010-04-23 16:25:07 +0000164
John McCall46460a62010-01-20 21:53:11 +0000165 Typedef->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000166 Owner->addDecl(Typedef);
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000168 return Typedef;
169}
170
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000171/// \brief Instantiate an initializer, breaking it into separate
172/// initialization arguments.
173///
174/// \param S The semantic analysis object.
175///
176/// \param Init The initializer to instantiate.
177///
178/// \param TemplateArgs Template arguments to be substituted into the
179/// initializer.
180///
181/// \param NewArgs Will be filled in with the instantiation arguments.
182///
183/// \returns true if an error occurred, false otherwise
184static bool InstantiateInitializer(Sema &S, Expr *Init,
185 const MultiLevelTemplateArgumentList &TemplateArgs,
186 SourceLocation &LParenLoc,
Nick Lewycky7663f392010-11-20 01:29:55 +0000187 ASTOwningVector<Expr*> &NewArgs,
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000188 SourceLocation &RParenLoc) {
189 NewArgs.clear();
190 LParenLoc = SourceLocation();
191 RParenLoc = SourceLocation();
192
193 if (!Init)
194 return false;
195
John McCall4765fa02010-12-06 08:20:24 +0000196 if (ExprWithCleanups *ExprTemp = dyn_cast<ExprWithCleanups>(Init))
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000197 Init = ExprTemp->getSubExpr();
198
199 while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init))
200 Init = Binder->getSubExpr();
201
202 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init))
203 Init = ICE->getSubExprAsWritten();
204
205 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
206 LParenLoc = ParenList->getLParenLoc();
207 RParenLoc = ParenList->getRParenLoc();
Douglas Gregor91fc73e2011-01-07 19:35:17 +0000208 return S.SubstExprs(ParenList->getExprs(), ParenList->getNumExprs(),
209 true, TemplateArgs, NewArgs);
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000210 }
211
212 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) {
Douglas Gregor28329e52010-03-24 21:22:47 +0000213 if (!isa<CXXTemporaryObjectExpr>(Construct)) {
Douglas Gregor91fc73e2011-01-07 19:35:17 +0000214 if (S.SubstExprs(Construct->getArgs(), Construct->getNumArgs(), true,
215 TemplateArgs, NewArgs))
Douglas Gregor28329e52010-03-24 21:22:47 +0000216 return true;
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000217
Douglas Gregor28329e52010-03-24 21:22:47 +0000218 // FIXME: Fake locations!
219 LParenLoc = S.PP.getLocForEndOfToken(Init->getLocStart());
Douglas Gregora1a04782010-09-09 16:33:13 +0000220 RParenLoc = LParenLoc;
Douglas Gregor28329e52010-03-24 21:22:47 +0000221 return false;
222 }
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000223 }
224
John McCall60d7b3a2010-08-24 06:29:42 +0000225 ExprResult Result = S.SubstExpr(Init, TemplateArgs);
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000226 if (Result.isInvalid())
227 return true;
228
229 NewArgs.push_back(Result.takeAs<Expr>());
230 return false;
231}
232
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000233Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
Douglas Gregor9901c572010-05-21 00:31:19 +0000234 // If this is the variable for an anonymous struct or union,
235 // instantiate the anonymous struct/union type first.
236 if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
237 if (RecordTy->getDecl()->isAnonymousStructOrUnion())
238 if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
239 return 0;
240
John McCallce3ff2b2009-08-25 22:02:44 +0000241 // Do substitution on the type of the declaration
John McCalla93c9342009-12-07 02:54:59 +0000242 TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
John McCall0a5fa062009-10-21 02:39:02 +0000243 TemplateArgs,
244 D->getTypeSpecStartLoc(),
245 D->getDeclName());
246 if (!DI)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000247 return 0;
248
Douglas Gregorc6dbc3f2010-09-12 07:37:24 +0000249 if (DI->getType()->isFunctionType()) {
250 SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function)
251 << D->isStaticDataMember() << DI->getType();
252 return 0;
253 }
254
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000255 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000256 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
257 D->getLocation(), D->getIdentifier(),
John McCall0a5fa062009-10-21 02:39:02 +0000258 DI->getType(), DI,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000259 D->getStorageClass(),
260 D->getStorageClassAsWritten());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000261 Var->setThreadSpecified(D->isThreadSpecified());
262 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
Mike Stump1eb44332009-09-09 15:08:12 +0000263
John McCallb6217662010-03-15 10:12:16 +0000264 // Substitute the nested name specifier, if any.
265 if (SubstQualifier(D, Var))
266 return 0;
267
Mike Stump1eb44332009-09-09 15:08:12 +0000268 // If we are instantiating a static data member defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000269 // out-of-line, the instantiation will have the same lexical
270 // context (which will be a namespace scope) as the template.
271 if (D->isOutOfLine())
272 Var->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000273
John McCall46460a62010-01-20 21:53:11 +0000274 Var->setAccess(D->getAccess());
Douglas Gregorc070cc62010-06-17 23:14:26 +0000275
276 if (!D->isStaticDataMember())
277 Var->setUsed(D->isUsed(false));
Douglas Gregor4469e8a2010-05-19 17:02:24 +0000278
Mike Stump390b4cc2009-05-16 07:39:55 +0000279 // FIXME: In theory, we could have a previous declaration for variables that
280 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000281 bool Redeclaration = false;
John McCall68263142009-11-18 22:49:29 +0000282 // FIXME: having to fake up a LookupResult is dumb.
283 LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
Douglas Gregor449d0a82010-03-01 19:11:54 +0000284 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Douglas Gregor60c93c92010-02-09 07:26:29 +0000285 if (D->isStaticDataMember())
286 SemaRef.LookupQualifiedName(Previous, Owner, false);
John McCall68263142009-11-18 22:49:29 +0000287 SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Douglas Gregor7caa6822009-07-24 20:34:43 +0000289 if (D->isOutOfLine()) {
Abramo Bagnaraea7b4882010-06-04 09:35:39 +0000290 if (!D->isStaticDataMember())
291 D->getLexicalDeclContext()->addDecl(Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000292 Owner->makeDeclVisibleInContext(Var);
293 } else {
294 Owner->addDecl(Var);
Douglas Gregorf7d72f52010-05-03 20:22:41 +0000295 if (Owner->isFunctionOrMethod())
296 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000297 }
John McCall1d8d1cc2010-08-01 02:01:53 +0000298 SemaRef.InstantiateAttrs(TemplateArgs, D, Var);
Fariborz Jahanian8dd0c562010-07-13 00:16:40 +0000299
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000300 // Link instantiations of static data members back to the template from
301 // which they were instantiated.
302 if (Var->isStaticDataMember())
303 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000304 TSK_ImplicitInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000305
Douglas Gregor60c93c92010-02-09 07:26:29 +0000306 if (Var->getAnyInitializer()) {
307 // We already have an initializer in the class.
308 } else if (D->getInit()) {
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000309 if (Var->isStaticDataMember() && !D->isOutOfLine())
310 SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
311 else
312 SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
313
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000314 // Instantiate the initializer.
315 SourceLocation LParenLoc, RParenLoc;
John McCallca0408f2010-08-23 06:44:23 +0000316 ASTOwningVector<Expr*> InitArgs(SemaRef);
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000317 if (!InstantiateInitializer(SemaRef, D->getInit(), TemplateArgs, LParenLoc,
Douglas Gregora1a04782010-09-09 16:33:13 +0000318 InitArgs, RParenLoc)) {
Douglas Gregor07a77b42011-01-14 17:12:22 +0000319 // Attach the initializer to the declaration, if we have one.
320 if (InitArgs.size() == 0)
321 SemaRef.ActOnUninitializedDecl(Var, false);
322 else if (D->hasCXXDirectInitializer()) {
Douglas Gregor6eef5192009-12-14 19:27:10 +0000323 // Add the direct initializer to the declaration.
John McCalld226f652010-08-21 09:40:31 +0000324 SemaRef.AddCXXDirectInitializerToDecl(Var,
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000325 LParenLoc,
Douglas Gregor6eef5192009-12-14 19:27:10 +0000326 move_arg(InitArgs),
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000327 RParenLoc);
Douglas Gregor07a77b42011-01-14 17:12:22 +0000328 } else {
329 assert(InitArgs.size() == 1);
John McCall9ae2f072010-08-23 23:25:46 +0000330 Expr *Init = InitArgs.take()[0];
331 SemaRef.AddInitializerToDecl(Var, Init, false);
Douglas Gregor6eef5192009-12-14 19:27:10 +0000332 }
Douglas Gregor6eef5192009-12-14 19:27:10 +0000333 } else {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000334 // FIXME: Not too happy about invalidating the declaration
335 // because of a bogus initializer.
336 Var->setInvalidDecl();
Douglas Gregor6eef5192009-12-14 19:27:10 +0000337 }
338
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000339 SemaRef.PopExpressionEvaluationContext();
Douglas Gregor65b90052009-07-27 17:43:39 +0000340 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
John McCalld226f652010-08-21 09:40:31 +0000341 SemaRef.ActOnUninitializedDecl(Var, false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000342
Douglas Gregor5764f612010-05-08 23:05:03 +0000343 // Diagnose unused local variables.
344 if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed())
345 SemaRef.DiagnoseUnusedDecl(Var);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +0000346
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000347 return Var;
348}
349
Abramo Bagnara6206d532010-06-05 05:09:32 +0000350Decl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
351 AccessSpecDecl* AD
352 = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
353 D->getAccessSpecifierLoc(), D->getColonLoc());
354 Owner->addHiddenDecl(AD);
355 return AD;
356}
357
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000358Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
359 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000360 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor836adf62010-05-24 17:22:01 +0000361 if (DI->getType()->isDependentType() ||
362 DI->getType()->isVariablyModifiedType()) {
John McCall07fb6be2009-10-22 23:33:21 +0000363 DI = SemaRef.SubstType(DI, TemplateArgs,
364 D->getLocation(), D->getDeclName());
365 if (!DI) {
John McCalla93c9342009-12-07 02:54:59 +0000366 DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000367 Invalid = true;
368 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000369 // C++ [temp.arg.type]p3:
370 // If a declaration acquires a function type through a type
371 // dependent on a template-parameter and this causes a
372 // declaration that does not use the syntactic form of a
373 // function declarator to have function type, the program is
374 // ill-formed.
375 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000376 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000377 Invalid = true;
378 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000379 } else {
380 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000381 }
382
383 Expr *BitWidth = D->getBitWidth();
384 if (Invalid)
385 BitWidth = 0;
386 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000387 // The bit-width expression is not potentially evaluated.
John McCallf312b1e2010-08-26 23:41:50 +0000388 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000389
John McCall60d7b3a2010-08-24 06:29:42 +0000390 ExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000391 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000392 if (InstantiatedBitWidth.isInvalid()) {
393 Invalid = true;
394 BitWidth = 0;
395 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000396 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000397 }
398
John McCall07fb6be2009-10-22 23:33:21 +0000399 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
400 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000401 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000402 D->getLocation(),
403 D->isMutable(),
404 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000405 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000406 D->getAccess(),
407 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000408 if (!Field) {
409 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000410 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000411 }
Mike Stump1eb44332009-09-09 15:08:12 +0000412
John McCall1d8d1cc2010-08-01 02:01:53 +0000413 SemaRef.InstantiateAttrs(TemplateArgs, D, Field);
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000414
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000415 if (Invalid)
416 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000418 if (!Field->getDeclName()) {
419 // Keep track of where this decl came from.
420 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor9901c572010-05-21 00:31:19 +0000421 }
422 if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
423 if (Parent->isAnonymousStructOrUnion() &&
Sebastian Redl7a126a42010-08-31 00:36:30 +0000424 Parent->getRedeclContext()->isFunctionOrMethod())
Douglas Gregor9901c572010-05-21 00:31:19 +0000425 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000426 }
Mike Stump1eb44332009-09-09 15:08:12 +0000427
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000428 Field->setImplicit(D->isImplicit());
John McCall46460a62010-01-20 21:53:11 +0000429 Field->setAccess(D->getAccess());
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000430 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000431
432 return Field;
433}
434
Francois Pichet87c2e122010-11-21 06:08:52 +0000435Decl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
436 NamedDecl **NamedChain =
437 new (SemaRef.Context)NamedDecl*[D->getChainingSize()];
438
439 int i = 0;
440 for (IndirectFieldDecl::chain_iterator PI =
441 D->chain_begin(), PE = D->chain_end();
442 PI != PE; ++PI)
443 NamedChain[i++] = (SemaRef.FindInstantiatedDecl(D->getLocation(),
444 *PI, TemplateArgs));
445
Francois Pichet40e17752010-12-09 10:07:54 +0000446 QualType T = cast<FieldDecl>(NamedChain[i-1])->getType();
Francois Pichet87c2e122010-11-21 06:08:52 +0000447 IndirectFieldDecl* IndirectField
448 = IndirectFieldDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Francois Pichet40e17752010-12-09 10:07:54 +0000449 D->getIdentifier(), T,
Francois Pichet87c2e122010-11-21 06:08:52 +0000450 NamedChain, D->getChainingSize());
451
452
453 IndirectField->setImplicit(D->isImplicit());
454 IndirectField->setAccess(D->getAccess());
455 Owner->addDecl(IndirectField);
456 return IndirectField;
457}
458
John McCall02cace72009-08-28 07:59:38 +0000459Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
John McCall02cace72009-08-28 07:59:38 +0000460 // Handle friend type expressions by simply substituting template
Douglas Gregor06245bf2010-04-07 17:57:12 +0000461 // parameters into the pattern type and checking the result.
John McCall32f2fb52010-03-25 18:04:51 +0000462 if (TypeSourceInfo *Ty = D->getFriendType()) {
463 TypeSourceInfo *InstTy =
464 SemaRef.SubstType(Ty, TemplateArgs,
465 D->getLocation(), DeclarationName());
Douglas Gregor06245bf2010-04-07 17:57:12 +0000466 if (!InstTy)
Douglas Gregor7557a132009-12-24 20:56:24 +0000467 return 0;
John McCall02cace72009-08-28 07:59:38 +0000468
Douglas Gregor06245bf2010-04-07 17:57:12 +0000469 FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getFriendLoc(), InstTy);
470 if (!FD)
471 return 0;
472
473 FD->setAccess(AS_public);
John McCall9a34edb2010-10-19 01:40:49 +0000474 FD->setUnsupportedFriend(D->isUnsupportedFriend());
Douglas Gregor06245bf2010-04-07 17:57:12 +0000475 Owner->addDecl(FD);
476 return FD;
477 }
478
479 NamedDecl *ND = D->getFriendDecl();
480 assert(ND && "friend decl must be a decl or a type!");
481
John McCallaf2094e2010-04-08 09:05:18 +0000482 // All of the Visit implementations for the various potential friend
483 // declarations have to be carefully written to work for friend
484 // objects, with the most important detail being that the target
485 // decl should almost certainly not be placed in Owner.
486 Decl *NewND = Visit(ND);
Douglas Gregor06245bf2010-04-07 17:57:12 +0000487 if (!NewND) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000488
John McCall02cace72009-08-28 07:59:38 +0000489 FriendDecl *FD =
Douglas Gregor06245bf2010-04-07 17:57:12 +0000490 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
491 cast<NamedDecl>(NewND), D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000492 FD->setAccess(AS_public);
John McCall9a34edb2010-10-19 01:40:49 +0000493 FD->setUnsupportedFriend(D->isUnsupportedFriend());
John McCall02cace72009-08-28 07:59:38 +0000494 Owner->addDecl(FD);
495 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000496}
497
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000498Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
499 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Douglas Gregorac7610d2009-06-22 20:57:11 +0000501 // The expression in a static assertion is not potentially evaluated.
John McCallf312b1e2010-08-26 23:41:50 +0000502 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000503
John McCall60d7b3a2010-08-24 06:29:42 +0000504 ExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000505 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000506 if (InstantiatedAssertExpr.isInvalid())
507 return 0;
508
John McCall60d7b3a2010-08-24 06:29:42 +0000509 ExprResult Message(D->getMessage());
John McCall3fa5cae2010-10-26 07:05:15 +0000510 D->getMessage();
John McCalld226f652010-08-21 09:40:31 +0000511 return SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
John McCall9ae2f072010-08-23 23:25:46 +0000512 InstantiatedAssertExpr.get(),
513 Message.get());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000514}
515
516Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000517 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000518 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000519 D->getTagKeywordLoc(),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +0000520 /*PrevDecl=*/0, D->isScoped(),
521 D->isScopedUsingClassTag(), D->isFixed());
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000522 if (D->isFixed()) {
523 if (TypeSourceInfo* TI = D->getIntegerTypeSourceInfo()) {
524 // If we have type source information for the underlying type, it means it
525 // has been explicitly set by the user. Perform substitution on it before
526 // moving on.
527 SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
528 Enum->setIntegerTypeSourceInfo(SemaRef.SubstType(TI,
529 TemplateArgs,
530 UnderlyingLoc,
531 DeclarationName()));
532
533 if (!Enum->getIntegerTypeSourceInfo())
534 Enum->setIntegerType(SemaRef.Context.IntTy);
535 }
536 else {
537 assert(!D->getIntegerType()->isDependentType()
538 && "Dependent type without type source info");
539 Enum->setIntegerType(D->getIntegerType());
540 }
541 }
542
John McCall5b629aa2010-10-22 23:36:17 +0000543 SemaRef.InstantiateAttrs(TemplateArgs, D, Enum);
544
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000545 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000546 Enum->setAccess(D->getAccess());
John McCallb6217662010-03-15 10:12:16 +0000547 if (SubstQualifier(D, Enum)) return 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000548 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000549 Enum->startDefinition();
550
Douglas Gregor96084f12010-03-01 19:00:07 +0000551 if (D->getDeclContext()->isFunctionOrMethod())
552 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
553
John McCalld226f652010-08-21 09:40:31 +0000554 llvm::SmallVector<Decl*, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000555
556 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000557 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
558 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000559 EC != ECEnd; ++EC) {
560 // The specified value for the enumerator.
John McCall60d7b3a2010-08-24 06:29:42 +0000561 ExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000562 if (Expr *UninstValue = EC->getInitExpr()) {
563 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000564 EnterExpressionEvaluationContext Unevaluated(SemaRef,
John McCallf312b1e2010-08-26 23:41:50 +0000565 Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000566
John McCallce3ff2b2009-08-25 22:02:44 +0000567 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000568 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000569
570 // Drop the initial value and continue.
571 bool isInvalid = false;
572 if (Value.isInvalid()) {
573 Value = SemaRef.Owned((Expr *)0);
574 isInvalid = true;
575 }
576
Mike Stump1eb44332009-09-09 15:08:12 +0000577 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000578 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
579 EC->getLocation(), EC->getIdentifier(),
John McCall9ae2f072010-08-23 23:25:46 +0000580 Value.get());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000581
582 if (isInvalid) {
583 if (EnumConst)
584 EnumConst->setInvalidDecl();
585 Enum->setInvalidDecl();
586 }
587
588 if (EnumConst) {
John McCall5b629aa2010-10-22 23:36:17 +0000589 SemaRef.InstantiateAttrs(TemplateArgs, *EC, EnumConst);
590
John McCall3b85ecf2010-01-23 22:37:59 +0000591 EnumConst->setAccess(Enum->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000592 Enum->addDecl(EnumConst);
John McCalld226f652010-08-21 09:40:31 +0000593 Enumerators.push_back(EnumConst);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000594 LastEnumConst = EnumConst;
Douglas Gregor96084f12010-03-01 19:00:07 +0000595
596 if (D->getDeclContext()->isFunctionOrMethod()) {
597 // If the enumeration is within a function or method, record the enum
598 // constant as a local.
599 SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
600 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000601 }
602 }
Mike Stump1eb44332009-09-09 15:08:12 +0000603
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000604 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000605 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000606 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
John McCalld226f652010-08-21 09:40:31 +0000607 Enum,
Eli Friedmande7a0fc2010-08-15 02:27:09 +0000608 Enumerators.data(), Enumerators.size(),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000609 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000610
611 return Enum;
612}
613
Douglas Gregor6477b692009-03-25 15:04:13 +0000614Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
615 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
616 return 0;
617}
618
John McCalle29ba202009-08-20 01:44:21 +0000619Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
John McCall93ba8572010-03-25 06:39:04 +0000620 bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
621
Douglas Gregor550d9b22009-10-31 17:21:17 +0000622 // Create a local instantiation scope for this class template, which
623 // will contain the instantiations of the template parameters.
John McCall2a7fb272010-08-25 05:32:35 +0000624 LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000625 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000626 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000627 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000628 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000629
630 CXXRecordDecl *Pattern = D->getTemplatedDecl();
John McCall93ba8572010-03-25 06:39:04 +0000631
632 // Instantiate the qualifier. We have to do this first in case
633 // we're a friend declaration, because if we are then we need to put
634 // the new declaration in the appropriate context.
635 NestedNameSpecifier *Qualifier = Pattern->getQualifier();
636 if (Qualifier) {
637 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
638 Pattern->getQualifierRange(),
639 TemplateArgs);
640 if (!Qualifier) return 0;
641 }
642
643 CXXRecordDecl *PrevDecl = 0;
644 ClassTemplateDecl *PrevClassTemplate = 0;
645
Nick Lewycky37574f52010-11-08 23:29:42 +0000646 if (!isFriend && Pattern->getPreviousDeclaration()) {
647 DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
648 if (Found.first != Found.second) {
649 PrevClassTemplate = dyn_cast<ClassTemplateDecl>(*Found.first);
650 if (PrevClassTemplate)
651 PrevDecl = PrevClassTemplate->getTemplatedDecl();
652 }
653 }
654
John McCall93ba8572010-03-25 06:39:04 +0000655 // If this isn't a friend, then it's a member template, in which
656 // case we just want to build the instantiation in the
657 // specialization. If it is a friend, we want to build it in
658 // the appropriate context.
659 DeclContext *DC = Owner;
660 if (isFriend) {
661 if (Qualifier) {
662 CXXScopeSpec SS;
663 SS.setScopeRep(Qualifier);
664 SS.setRange(Pattern->getQualifierRange());
665 DC = SemaRef.computeDeclContext(SS);
666 if (!DC) return 0;
667 } else {
668 DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
669 Pattern->getDeclContext(),
670 TemplateArgs);
671 }
672
673 // Look for a previous declaration of the template in the owning
674 // context.
675 LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
676 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
677 SemaRef.LookupQualifiedName(R, DC);
678
679 if (R.isSingleResult()) {
680 PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
681 if (PrevClassTemplate)
682 PrevDecl = PrevClassTemplate->getTemplatedDecl();
683 }
684
685 if (!PrevClassTemplate && Qualifier) {
686 SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
Douglas Gregor1eabb7d2010-03-31 23:17:41 +0000687 << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
688 << Pattern->getQualifierRange();
John McCall93ba8572010-03-25 06:39:04 +0000689 return 0;
690 }
691
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000692 bool AdoptedPreviousTemplateParams = false;
John McCall93ba8572010-03-25 06:39:04 +0000693 if (PrevClassTemplate) {
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000694 bool Complain = true;
695
696 // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
697 // template for struct std::tr1::__detail::_Map_base, where the
698 // template parameters of the friend declaration don't match the
699 // template parameters of the original declaration. In this one
700 // case, we don't complain about the ill-formed friend
701 // declaration.
702 if (isFriend && Pattern->getIdentifier() &&
703 Pattern->getIdentifier()->isStr("_Map_base") &&
704 DC->isNamespace() &&
705 cast<NamespaceDecl>(DC)->getIdentifier() &&
706 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
707 DeclContext *DCParent = DC->getParent();
708 if (DCParent->isNamespace() &&
709 cast<NamespaceDecl>(DCParent)->getIdentifier() &&
710 cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
711 DeclContext *DCParent2 = DCParent->getParent();
712 if (DCParent2->isNamespace() &&
713 cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
714 cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
715 DCParent2->getParent()->isTranslationUnit())
716 Complain = false;
717 }
718 }
719
John McCall93ba8572010-03-25 06:39:04 +0000720 TemplateParameterList *PrevParams
721 = PrevClassTemplate->getTemplateParameters();
722
723 // Make sure the parameter lists match.
724 if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000725 Complain,
726 Sema::TPL_TemplateMatch)) {
727 if (Complain)
728 return 0;
729
730 AdoptedPreviousTemplateParams = true;
731 InstParams = PrevParams;
732 }
John McCall93ba8572010-03-25 06:39:04 +0000733
734 // Do some additional validation, then merge default arguments
735 // from the existing declarations.
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000736 if (!AdoptedPreviousTemplateParams &&
737 SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
John McCall93ba8572010-03-25 06:39:04 +0000738 Sema::TPC_ClassTemplate))
739 return 0;
740 }
741 }
742
John McCalle29ba202009-08-20 01:44:21 +0000743 CXXRecordDecl *RecordInst
John McCall93ba8572010-03-25 06:39:04 +0000744 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
John McCalle29ba202009-08-20 01:44:21 +0000745 Pattern->getLocation(), Pattern->getIdentifier(),
John McCall93ba8572010-03-25 06:39:04 +0000746 Pattern->getTagKeywordLoc(), PrevDecl,
Douglas Gregorf0510d42009-10-12 23:11:44 +0000747 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000748
John McCall93ba8572010-03-25 06:39:04 +0000749 if (Qualifier)
750 RecordInst->setQualifierInfo(Qualifier, Pattern->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +0000751
John McCalle29ba202009-08-20 01:44:21 +0000752 ClassTemplateDecl *Inst
John McCall93ba8572010-03-25 06:39:04 +0000753 = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
754 D->getIdentifier(), InstParams, RecordInst,
755 PrevClassTemplate);
John McCalle29ba202009-08-20 01:44:21 +0000756 RecordInst->setDescribedClassTemplate(Inst);
John McCallea7390c2010-04-08 20:25:50 +0000757
John McCall93ba8572010-03-25 06:39:04 +0000758 if (isFriend) {
John McCallea7390c2010-04-08 20:25:50 +0000759 if (PrevClassTemplate)
760 Inst->setAccess(PrevClassTemplate->getAccess());
761 else
762 Inst->setAccess(D->getAccess());
763
John McCall93ba8572010-03-25 06:39:04 +0000764 Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
765 // TODO: do we want to track the instantiation progeny of this
766 // friend target decl?
767 } else {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000768 Inst->setAccess(D->getAccess());
Nick Lewycky37574f52010-11-08 23:29:42 +0000769 if (!PrevClassTemplate)
770 Inst->setInstantiatedFromMemberTemplate(D);
John McCall93ba8572010-03-25 06:39:04 +0000771 }
Douglas Gregorf0510d42009-10-12 23:11:44 +0000772
773 // Trigger creation of the type for the instantiation.
John McCall3cb0ebd2010-03-10 03:28:59 +0000774 SemaRef.Context.getInjectedClassNameType(RecordInst,
Douglas Gregor24bae922010-07-08 18:37:38 +0000775 Inst->getInjectedClassNameSpecialization());
John McCallea7390c2010-04-08 20:25:50 +0000776
Douglas Gregor259571e2009-10-30 22:42:42 +0000777 // Finish handling of friends.
John McCall93ba8572010-03-25 06:39:04 +0000778 if (isFriend) {
779 DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000780 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000781 }
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000782
John McCalle29ba202009-08-20 01:44:21 +0000783 Owner->addDecl(Inst);
Douglas Gregord65587f2010-11-10 19:44:59 +0000784
785 if (!PrevClassTemplate) {
786 // Queue up any out-of-line partial specializations of this member
787 // class template; the client will force their instantiation once
788 // the enclosing class has been instantiated.
789 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
790 D->getPartialSpecializations(PartialSpecs);
791 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
792 if (PartialSpecs[I]->isOutOfLine())
793 OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I]));
794 }
795
John McCalle29ba202009-08-20 01:44:21 +0000796 return Inst;
797}
798
Douglas Gregord60e1052009-08-27 16:57:43 +0000799Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000800TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
801 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000802 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
803
804 // Lookup the already-instantiated declaration in the instantiation
805 // of the class template and return that.
806 DeclContext::lookup_result Found
807 = Owner->lookup(ClassTemplate->getDeclName());
808 if (Found.first == Found.second)
809 return 0;
810
811 ClassTemplateDecl *InstClassTemplate
812 = dyn_cast<ClassTemplateDecl>(*Found.first);
813 if (!InstClassTemplate)
814 return 0;
815
Douglas Gregord65587f2010-11-10 19:44:59 +0000816 if (ClassTemplatePartialSpecializationDecl *Result
817 = InstClassTemplate->findPartialSpecInstantiatedFromMember(D))
818 return Result;
819
820 return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D);
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000821}
822
823Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000824TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000825 // Create a local instantiation scope for this function template, which
826 // will contain the instantiations of the template parameters and then get
827 // merged with the local instantiation scope for the function template
828 // itself.
John McCall2a7fb272010-08-25 05:32:35 +0000829 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor895162d2010-04-30 18:55:50 +0000830
Douglas Gregord60e1052009-08-27 16:57:43 +0000831 TemplateParameterList *TempParams = D->getTemplateParameters();
832 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000833 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000834 return NULL;
Douglas Gregored9c0f92009-10-29 00:04:11 +0000835
Douglas Gregora735b202009-10-13 14:39:41 +0000836 FunctionDecl *Instantiated = 0;
837 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
838 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
839 InstParams));
840 else
841 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
842 D->getTemplatedDecl(),
843 InstParams));
844
845 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000846 return 0;
847
John McCall46460a62010-01-20 21:53:11 +0000848 Instantiated->setAccess(D->getAccess());
849
Mike Stump1eb44332009-09-09 15:08:12 +0000850 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000851 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000852 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000853 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000854 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000855 assert(InstTemplate &&
856 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
John McCalle976ffe2009-12-14 23:19:40 +0000857
John McCallb1a56e72010-03-26 23:10:15 +0000858 bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
859
John McCalle976ffe2009-12-14 23:19:40 +0000860 // Link the instantiation back to the pattern *unless* this is a
861 // non-definition friend declaration.
862 if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
John McCallb1a56e72010-03-26 23:10:15 +0000863 !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
Douglas Gregora735b202009-10-13 14:39:41 +0000864 InstTemplate->setInstantiatedFromMemberTemplate(D);
865
John McCallb1a56e72010-03-26 23:10:15 +0000866 // Make declarations visible in the appropriate context.
867 if (!isFriend)
Douglas Gregora735b202009-10-13 14:39:41 +0000868 Owner->addDecl(InstTemplate);
John McCallb1a56e72010-03-26 23:10:15 +0000869
Douglas Gregord60e1052009-08-27 16:57:43 +0000870 return InstTemplate;
871}
872
Douglas Gregord475b8d2009-03-25 21:17:03 +0000873Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
874 CXXRecordDecl *PrevDecl = 0;
875 if (D->isInjectedClassName())
876 PrevDecl = cast<CXXRecordDecl>(Owner);
John McCall6c1c1b82009-12-15 22:29:06 +0000877 else if (D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000878 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
879 D->getPreviousDeclaration(),
John McCall6c1c1b82009-12-15 22:29:06 +0000880 TemplateArgs);
881 if (!Prev) return 0;
882 PrevDecl = cast<CXXRecordDecl>(Prev);
883 }
Douglas Gregord475b8d2009-03-25 21:17:03 +0000884
885 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000886 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000887 D->getLocation(), D->getIdentifier(),
888 D->getTagKeywordLoc(), PrevDecl);
John McCallb6217662010-03-15 10:12:16 +0000889
890 // Substitute the nested name specifier, if any.
891 if (SubstQualifier(D, Record))
892 return 0;
893
Douglas Gregord475b8d2009-03-25 21:17:03 +0000894 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000895 // FIXME: Check against AS_none is an ugly hack to work around the issue that
896 // the tag decls introduced by friend class declarations don't have an access
897 // specifier. Remove once this area of the code gets sorted out.
898 if (D->getAccess() != AS_none)
899 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000900 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000901 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000902
John McCall02cace72009-08-28 07:59:38 +0000903 // If the original function was part of a friend declaration,
904 // inherit its namespace state.
905 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
906 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
907
Douglas Gregor9901c572010-05-21 00:31:19 +0000908 // Make sure that anonymous structs and unions are recorded.
909 if (D->isAnonymousStructOrUnion()) {
910 Record->setAnonymousStructOrUnion(true);
Sebastian Redl7a126a42010-08-31 00:36:30 +0000911 if (Record->getDeclContext()->getRedeclContext()->isFunctionOrMethod())
Douglas Gregor9901c572010-05-21 00:31:19 +0000912 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
913 }
Anders Carlssond8b285f2009-09-01 04:26:58 +0000914
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000915 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000916 return Record;
917}
918
John McCall02cace72009-08-28 07:59:38 +0000919/// Normal class members are of more specific types and therefore
920/// don't make it here. This function serves two purposes:
921/// 1) instantiating function templates
922/// 2) substituting friend declarations
923/// FIXME: preserve function definitions in case #2
Douglas Gregor7557a132009-12-24 20:56:24 +0000924Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
Douglas Gregora735b202009-10-13 14:39:41 +0000925 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000926 // Check whether there is already a function template specialization for
927 // this declaration.
928 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
929 void *InsertPos = 0;
John McCallb0cb0222010-03-27 05:57:59 +0000930 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor24bae922010-07-08 18:37:38 +0000931 std::pair<const TemplateArgument *, unsigned> Innermost
932 = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +0000934 FunctionDecl *SpecFunc
935 = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
936 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000937
Douglas Gregor127102b2009-06-29 20:59:39 +0000938 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +0000939 if (SpecFunc)
940 return SpecFunc;
Douglas Gregor127102b2009-06-29 20:59:39 +0000941 }
Mike Stump1eb44332009-09-09 15:08:12 +0000942
John McCallb0cb0222010-03-27 05:57:59 +0000943 bool isFriend;
944 if (FunctionTemplate)
945 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
946 else
947 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
948
Douglas Gregor79c22782010-01-16 20:21:20 +0000949 bool MergeWithParentScope = (TemplateParams != 0) ||
Douglas Gregorb212d9a2010-05-21 21:25:08 +0000950 Owner->isFunctionOrMethod() ||
Douglas Gregor79c22782010-01-16 20:21:20 +0000951 !(isa<Decl>(Owner) &&
952 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
John McCall2a7fb272010-08-25 05:32:35 +0000953 LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000954
Douglas Gregore53060f2009-06-25 22:08:12 +0000955 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +0000956 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
957 TInfo = SubstFunctionType(D, Params);
958 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000959 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +0000960 QualType T = TInfo->getType();
John McCallfd810b12009-08-14 02:03:10 +0000961
John McCalld325daa2010-03-26 04:53:08 +0000962 NestedNameSpecifier *Qualifier = D->getQualifier();
963 if (Qualifier) {
964 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
965 D->getQualifierRange(),
966 TemplateArgs);
967 if (!Qualifier) return 0;
968 }
969
John McCall68b6b872010-02-06 01:50:47 +0000970 // If we're instantiating a local function declaration, put the result
971 // in the owner; otherwise we need to find the instantiated context.
972 DeclContext *DC;
973 if (D->getDeclContext()->isFunctionOrMethod())
974 DC = Owner;
John McCalld325daa2010-03-26 04:53:08 +0000975 else if (isFriend && Qualifier) {
976 CXXScopeSpec SS;
977 SS.setScopeRep(Qualifier);
978 SS.setRange(D->getQualifierRange());
979 DC = SemaRef.computeDeclContext(SS);
980 if (!DC) return 0;
981 } else {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000982 DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
983 TemplateArgs);
John McCalld325daa2010-03-26 04:53:08 +0000984 }
John McCall68b6b872010-02-06 01:50:47 +0000985
John McCall02cace72009-08-28 07:59:38 +0000986 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +0000987 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +0000988 D->getDeclName(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000989 D->getStorageClass(), D->getStorageClassAsWritten(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000990 D->isInlineSpecified(), D->hasWrittenPrototype());
John McCallb6217662010-03-15 10:12:16 +0000991
John McCalld325daa2010-03-26 04:53:08 +0000992 if (Qualifier)
993 Function->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +0000994
John McCallb1a56e72010-03-26 23:10:15 +0000995 DeclContext *LexicalDC = Owner;
996 if (!isFriend && D->isOutOfLine()) {
997 assert(D->getDeclContext()->isFileContext());
998 LexicalDC = D->getDeclContext();
999 }
1000
1001 Function->setLexicalDeclContext(LexicalDC);
Mike Stump1eb44332009-09-09 15:08:12 +00001002
Douglas Gregore53060f2009-06-25 22:08:12 +00001003 // Attach the parameters
1004 for (unsigned P = 0; P < Params.size(); ++P)
John McCall3019c442010-09-17 00:50:28 +00001005 if (Params[P])
1006 Params[P]->setOwningFunction(Function);
Douglas Gregor838db382010-02-11 01:19:42 +00001007 Function->setParams(Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +00001008
Douglas Gregorac7c2c82010-05-17 16:38:00 +00001009 SourceLocation InstantiateAtPOI;
Douglas Gregora735b202009-10-13 14:39:41 +00001010 if (TemplateParams) {
1011 // Our resulting instantiation is actually a function template, since we
1012 // are substituting only the outer template parameters. For example, given
1013 //
1014 // template<typename T>
1015 // struct X {
1016 // template<typename U> friend void f(T, U);
1017 // };
1018 //
1019 // X<int> x;
1020 //
1021 // We are instantiating the friend function template "f" within X<int>,
1022 // which means substituting int for T, but leaving "f" as a friend function
1023 // template.
1024 // Build the function template itself.
John McCalld325daa2010-03-26 04:53:08 +00001025 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
Douglas Gregora735b202009-10-13 14:39:41 +00001026 Function->getLocation(),
1027 Function->getDeclName(),
1028 TemplateParams, Function);
1029 Function->setDescribedFunctionTemplate(FunctionTemplate);
John McCallb1a56e72010-03-26 23:10:15 +00001030
1031 FunctionTemplate->setLexicalDeclContext(LexicalDC);
John McCalld325daa2010-03-26 04:53:08 +00001032
1033 if (isFriend && D->isThisDeclarationADefinition()) {
1034 // TODO: should we remember this connection regardless of whether
1035 // the friend declaration provided a body?
1036 FunctionTemplate->setInstantiatedFromMemberTemplate(
1037 D->getDescribedFunctionTemplate());
1038 }
Douglas Gregor66724ea2009-11-14 01:20:54 +00001039 } else if (FunctionTemplate) {
1040 // Record this function template specialization.
Douglas Gregor24bae922010-07-08 18:37:38 +00001041 std::pair<const TemplateArgument *, unsigned> Innermost
1042 = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001043 Function->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001044 TemplateArgumentList::CreateCopy(SemaRef.Context,
Douglas Gregor24bae922010-07-08 18:37:38 +00001045 Innermost.first,
1046 Innermost.second),
Douglas Gregor66724ea2009-11-14 01:20:54 +00001047 InsertPos);
John McCalld325daa2010-03-26 04:53:08 +00001048 } else if (isFriend && D->isThisDeclarationADefinition()) {
1049 // TODO: should we remember this connection regardless of whether
1050 // the friend declaration provided a body?
1051 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
John McCall02cace72009-08-28 07:59:38 +00001052 }
Douglas Gregora735b202009-10-13 14:39:41 +00001053
Douglas Gregore53060f2009-06-25 22:08:12 +00001054 if (InitFunctionInstantiation(Function, D))
1055 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001056
Douglas Gregore53060f2009-06-25 22:08:12 +00001057 bool Redeclaration = false;
John McCallaf2094e2010-04-08 09:05:18 +00001058 bool isExplicitSpecialization = false;
Douglas Gregora735b202009-10-13 14:39:41 +00001059
John McCall68263142009-11-18 22:49:29 +00001060 LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
1061 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
1062
John McCallaf2094e2010-04-08 09:05:18 +00001063 if (DependentFunctionTemplateSpecializationInfo *Info
1064 = D->getDependentSpecializationInfo()) {
1065 assert(isFriend && "non-friend has dependent specialization info?");
1066
1067 // This needs to be set now for future sanity.
1068 Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1069
1070 // Instantiate the explicit template arguments.
1071 TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1072 Info->getRAngleLoc());
Douglas Gregore02e2622010-12-22 21:19:48 +00001073 if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(),
1074 ExplicitArgs, TemplateArgs))
1075 return 0;
John McCallaf2094e2010-04-08 09:05:18 +00001076
1077 // Map the candidate templates to their instantiations.
1078 for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1079 Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1080 Info->getTemplate(I),
1081 TemplateArgs);
1082 if (!Temp) return 0;
1083
1084 Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1085 }
1086
1087 if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1088 &ExplicitArgs,
1089 Previous))
1090 Function->setInvalidDecl();
1091
1092 isExplicitSpecialization = true;
1093
1094 } else if (TemplateParams || !FunctionTemplate) {
Douglas Gregora735b202009-10-13 14:39:41 +00001095 // Look only into the namespace where the friend would be declared to
1096 // find a previous declaration. This is the innermost enclosing namespace,
1097 // as described in ActOnFriendFunctionDecl.
John McCall68263142009-11-18 22:49:29 +00001098 SemaRef.LookupQualifiedName(Previous, DC);
Douglas Gregora735b202009-10-13 14:39:41 +00001099
Douglas Gregora735b202009-10-13 14:39:41 +00001100 // In C++, the previous declaration we find might be a tag type
1101 // (class or enum). In this case, the new declaration will hide the
1102 // tag type. Note that this does does not apply if we're declaring a
1103 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001104 if (Previous.isSingleTagDecl())
1105 Previous.clear();
Douglas Gregora735b202009-10-13 14:39:41 +00001106 }
1107
John McCall9f54ad42009-12-10 09:41:52 +00001108 SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
Peter Collingbournec80e8112011-01-21 02:08:54 +00001109 isExplicitSpecialization, Redeclaration);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001110
John McCall76d32642010-04-24 01:30:58 +00001111 NamedDecl *PrincipalDecl = (TemplateParams
1112 ? cast<NamedDecl>(FunctionTemplate)
1113 : Function);
1114
Douglas Gregora735b202009-10-13 14:39:41 +00001115 // If the original function was part of a friend declaration,
1116 // inherit its namespace state and add it to the owner.
John McCalld325daa2010-03-26 04:53:08 +00001117 if (isFriend) {
John McCall68263142009-11-18 22:49:29 +00001118 NamedDecl *PrevDecl;
John McCall76d32642010-04-24 01:30:58 +00001119 if (TemplateParams)
Douglas Gregora735b202009-10-13 14:39:41 +00001120 PrevDecl = FunctionTemplate->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001121 else
Douglas Gregora735b202009-10-13 14:39:41 +00001122 PrevDecl = Function->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001123
1124 PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
1125 DC->makeDeclVisibleInContext(PrincipalDecl, /*Recoverable=*/ false);
Gabor Greifab297ac2010-08-30 21:10:05 +00001126
Gabor Greif77535df2010-08-30 22:25:56 +00001127 bool queuedInstantiation = false;
Gabor Greifab297ac2010-08-30 21:10:05 +00001128
Douglas Gregor238058c2010-05-18 05:45:02 +00001129 if (!SemaRef.getLangOptions().CPlusPlus0x &&
1130 D->isThisDeclarationADefinition()) {
1131 // Check for a function body.
1132 const FunctionDecl *Definition = 0;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001133 if (Function->hasBody(Definition) &&
Douglas Gregor238058c2010-05-18 05:45:02 +00001134 Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
1135 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1136 << Function->getDeclName();
1137 SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
1138 Function->setInvalidDecl();
1139 }
1140 // Check for redefinitions due to other instantiations of this or
1141 // a similar friend function.
1142 else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1143 REnd = Function->redecls_end();
1144 R != REnd; ++R) {
Gabor Greif13a8aff2010-08-28 15:42:30 +00001145 if (*R == Function)
1146 continue;
Gabor Greifab297ac2010-08-30 21:10:05 +00001147 switch (R->getFriendObjectKind()) {
1148 case Decl::FOK_None:
1149 if (!queuedInstantiation && R->isUsed(false)) {
1150 if (MemberSpecializationInfo *MSInfo
1151 = Function->getMemberSpecializationInfo()) {
1152 if (MSInfo->getPointOfInstantiation().isInvalid()) {
1153 SourceLocation Loc = R->getLocation(); // FIXME
1154 MSInfo->setPointOfInstantiation(Loc);
1155 SemaRef.PendingLocalImplicitInstantiations.push_back(
1156 std::make_pair(Function, Loc));
1157 queuedInstantiation = true;
1158 }
1159 }
1160 }
1161 break;
1162 default:
Douglas Gregor238058c2010-05-18 05:45:02 +00001163 if (const FunctionDecl *RPattern
Gabor Greif6a557d82010-08-28 15:46:56 +00001164 = R->getTemplateInstantiationPattern())
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001165 if (RPattern->hasBody(RPattern)) {
Douglas Gregor238058c2010-05-18 05:45:02 +00001166 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1167 << Function->getDeclName();
Gabor Greif6a557d82010-08-28 15:46:56 +00001168 SemaRef.Diag(R->getLocation(), diag::note_previous_definition);
Douglas Gregor238058c2010-05-18 05:45:02 +00001169 Function->setInvalidDecl();
1170 break;
1171 }
1172 }
1173 }
1174 }
Douglas Gregora735b202009-10-13 14:39:41 +00001175 }
1176
John McCall76d32642010-04-24 01:30:58 +00001177 if (Function->isOverloadedOperator() && !DC->isRecord() &&
1178 PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
1179 PrincipalDecl->setNonMemberOperator();
1180
Douglas Gregore53060f2009-06-25 22:08:12 +00001181 return Function;
1182}
1183
Douglas Gregord60e1052009-08-27 16:57:43 +00001184Decl *
1185TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1186 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +00001187 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1188 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +00001189 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +00001190 // We are creating a function template specialization from a function
1191 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +00001192 // specialization for this particular set of template arguments.
Douglas Gregor24bae922010-07-08 18:37:38 +00001193 std::pair<const TemplateArgument *, unsigned> Innermost
1194 = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +00001195
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001196 FunctionDecl *SpecFunc
1197 = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
1198 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001199
Douglas Gregor6b906862009-08-21 00:16:32 +00001200 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001201 if (SpecFunc)
1202 return SpecFunc;
Douglas Gregor6b906862009-08-21 00:16:32 +00001203 }
1204
John McCallb0cb0222010-03-27 05:57:59 +00001205 bool isFriend;
1206 if (FunctionTemplate)
1207 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1208 else
1209 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1210
Douglas Gregor79c22782010-01-16 20:21:20 +00001211 bool MergeWithParentScope = (TemplateParams != 0) ||
1212 !(isa<Decl>(Owner) &&
1213 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
John McCall2a7fb272010-08-25 05:32:35 +00001214 LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Douglas Gregor48dd19b2009-05-14 21:44:34 +00001215
John McCall4eab39f2010-10-19 02:26:41 +00001216 // Instantiate enclosing template arguments for friends.
1217 llvm::SmallVector<TemplateParameterList *, 4> TempParamLists;
1218 unsigned NumTempParamLists = 0;
1219 if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) {
1220 TempParamLists.set_size(NumTempParamLists);
1221 for (unsigned I = 0; I != NumTempParamLists; ++I) {
1222 TemplateParameterList *TempParams = D->getTemplateParameterList(I);
1223 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1224 if (!InstParams)
1225 return NULL;
1226 TempParamLists[I] = InstParams;
1227 }
1228 }
1229
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001230 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +00001231 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
1232 TInfo = SubstFunctionType(D, Params);
1233 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001234 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +00001235 QualType T = TInfo->getType();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001236
Abramo Bagnara723df242010-12-14 22:11:44 +00001237 // \brief If the type of this function, after ignoring parentheses,
1238 // is not *directly* a function type, then we're instantiating a function
1239 // that was declared via a typedef, e.g.,
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001240 //
1241 // typedef int functype(int, int);
1242 // functype func;
1243 //
1244 // In this case, we'll just go instantiate the ParmVarDecls that we
1245 // synthesized in the method declaration.
Abramo Bagnara723df242010-12-14 22:11:44 +00001246 if (!isa<FunctionProtoType>(T.IgnoreParens())) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001247 assert(!Params.size() && "Instantiating type could not yield parameters");
Douglas Gregor12c9c002011-01-07 16:43:16 +00001248 llvm::SmallVector<QualType, 4> ParamTypes;
1249 if (SemaRef.SubstParmTypes(D->getLocation(), D->param_begin(),
1250 D->getNumParams(), TemplateArgs, ParamTypes,
1251 &Params))
1252 return 0;
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001253 }
1254
John McCallb0cb0222010-03-27 05:57:59 +00001255 NestedNameSpecifier *Qualifier = D->getQualifier();
1256 if (Qualifier) {
1257 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
1258 D->getQualifierRange(),
1259 TemplateArgs);
1260 if (!Qualifier) return 0;
1261 }
1262
1263 DeclContext *DC = Owner;
1264 if (isFriend) {
1265 if (Qualifier) {
1266 CXXScopeSpec SS;
1267 SS.setScopeRep(Qualifier);
1268 SS.setRange(D->getQualifierRange());
1269 DC = SemaRef.computeDeclContext(SS);
John McCallc54d6882010-10-19 05:01:53 +00001270
1271 if (DC && SemaRef.RequireCompleteDeclContext(SS, DC))
1272 return 0;
John McCallb0cb0222010-03-27 05:57:59 +00001273 } else {
1274 DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1275 D->getDeclContext(),
1276 TemplateArgs);
1277 }
1278 if (!DC) return 0;
1279 }
1280
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001281 // Build the instantiated method declaration.
John McCallb0cb0222010-03-27 05:57:59 +00001282 CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
Douglas Gregordec06662009-08-21 18:42:58 +00001283 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001284
Abramo Bagnara25777432010-08-11 22:01:17 +00001285 DeclarationNameInfo NameInfo
1286 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001287 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001288 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
Abramo Bagnara25777432010-08-11 22:01:17 +00001289 NameInfo, T, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001290 Constructor->isExplicit(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001291 Constructor->isInlineSpecified(),
1292 false);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001293 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001294 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001295 NameInfo, T, TInfo,
Abramo Bagnara25777432010-08-11 22:01:17 +00001296 Destructor->isInlineSpecified(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001297 false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001298 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001299 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
Abramo Bagnara25777432010-08-11 22:01:17 +00001300 NameInfo, T, TInfo,
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001301 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001302 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +00001303 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00001304 Method = CXXMethodDecl::Create(SemaRef.Context, Record,
1305 NameInfo, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001306 D->isStatic(),
1307 D->getStorageClassAsWritten(),
1308 D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +00001309 }
Douglas Gregor6b906862009-08-21 00:16:32 +00001310
John McCallb0cb0222010-03-27 05:57:59 +00001311 if (Qualifier)
1312 Method->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +00001313
Douglas Gregord60e1052009-08-27 16:57:43 +00001314 if (TemplateParams) {
1315 // Our resulting instantiation is actually a function template, since we
1316 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +00001317 //
Douglas Gregord60e1052009-08-27 16:57:43 +00001318 // template<typename T>
1319 // struct X {
1320 // template<typename U> void f(T, U);
1321 // };
1322 //
1323 // X<int> x;
1324 //
1325 // We are instantiating the member template "f" within X<int>, which means
1326 // substituting int for T, but leaving "f" as a member function template.
1327 // Build the function template itself.
1328 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1329 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001330 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +00001331 TemplateParams, Method);
John McCallb0cb0222010-03-27 05:57:59 +00001332 if (isFriend) {
1333 FunctionTemplate->setLexicalDeclContext(Owner);
1334 FunctionTemplate->setObjectOfFriendDecl(true);
1335 } else if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +00001336 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +00001337 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001338 } else if (FunctionTemplate) {
1339 // Record this function template specialization.
Douglas Gregor24bae922010-07-08 18:37:38 +00001340 std::pair<const TemplateArgument *, unsigned> Innermost
1341 = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001342 Method->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001343 TemplateArgumentList::CreateCopy(SemaRef.Context,
1344 Innermost.first,
1345 Innermost.second),
Douglas Gregor66724ea2009-11-14 01:20:54 +00001346 InsertPos);
John McCallb0cb0222010-03-27 05:57:59 +00001347 } else if (!isFriend) {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001348 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +00001349 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001350 }
1351
Mike Stump1eb44332009-09-09 15:08:12 +00001352 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +00001353 // out-of-line, the instantiation will have the same lexical
1354 // context (which will be a namespace scope) as the template.
John McCallb0cb0222010-03-27 05:57:59 +00001355 if (isFriend) {
John McCall4eab39f2010-10-19 02:26:41 +00001356 if (NumTempParamLists)
1357 Method->setTemplateParameterListsInfo(SemaRef.Context,
1358 NumTempParamLists,
1359 TempParamLists.data());
1360
John McCallb0cb0222010-03-27 05:57:59 +00001361 Method->setLexicalDeclContext(Owner);
1362 Method->setObjectOfFriendDecl(true);
1363 } else if (D->isOutOfLine())
Douglas Gregor7caa6822009-07-24 20:34:43 +00001364 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001365
Douglas Gregor5545e162009-03-24 00:38:23 +00001366 // Attach the parameters
1367 for (unsigned P = 0; P < Params.size(); ++P)
1368 Params[P]->setOwningFunction(Method);
Douglas Gregor838db382010-02-11 01:19:42 +00001369 Method->setParams(Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +00001370
1371 if (InitMethodInstantiation(Method, D))
1372 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001373
Abramo Bagnara25777432010-08-11 22:01:17 +00001374 LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
1375 Sema::ForRedeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001376
John McCallb0cb0222010-03-27 05:57:59 +00001377 if (!FunctionTemplate || TemplateParams || isFriend) {
1378 SemaRef.LookupQualifiedName(Previous, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001379
Douglas Gregordec06662009-08-21 18:42:58 +00001380 // In C++, the previous declaration we find might be a tag type
1381 // (class or enum). In this case, the new declaration will hide the
1382 // tag type. Note that this does does not apply if we're declaring a
1383 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001384 if (Previous.isSingleTagDecl())
1385 Previous.clear();
Douglas Gregordec06662009-08-21 18:42:58 +00001386 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001387
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001388 bool Redeclaration = false;
Peter Collingbournec80e8112011-01-21 02:08:54 +00001389 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001390
Douglas Gregor4ba31362009-12-01 17:24:26 +00001391 if (D->isPure())
1392 SemaRef.CheckPureMethod(Method, SourceRange());
1393
John McCall46460a62010-01-20 21:53:11 +00001394 Method->setAccess(D->getAccess());
1395
Anders Carlsson9eefa222011-01-20 06:52:44 +00001396 SemaRef.CheckOverrideControl(Method);
1397
John McCallb0cb0222010-03-27 05:57:59 +00001398 if (FunctionTemplate) {
1399 // If there's a function template, let our caller handle it.
1400 } else if (Method->isInvalidDecl() && !Previous.empty()) {
1401 // Don't hide a (potentially) valid declaration with an invalid one.
1402 } else {
1403 NamedDecl *DeclToAdd = (TemplateParams
1404 ? cast<NamedDecl>(FunctionTemplate)
1405 : Method);
1406 if (isFriend)
1407 Record->makeDeclVisibleInContext(DeclToAdd);
1408 else
1409 Owner->addDecl(DeclToAdd);
1410 }
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00001411
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001412 return Method;
1413}
1414
Douglas Gregor615c5d42009-03-24 16:43:20 +00001415Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +00001416 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +00001417}
1418
Douglas Gregor03b2b072009-03-24 00:15:49 +00001419Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001420 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +00001421}
1422
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001423Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001424 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001425}
1426
Douglas Gregor6477b692009-03-25 15:04:13 +00001427ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregor6a24bfd2011-01-14 22:40:04 +00001428 return SemaRef.SubstParmVarDecl(D, TemplateArgs, llvm::Optional<unsigned>());
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001429}
1430
John McCalle29ba202009-08-20 01:44:21 +00001431Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1432 TemplateTypeParmDecl *D) {
1433 // TODO: don't always clone when decls are refcounted.
Douglas Gregorefed5c82010-06-16 15:23:05 +00001434 const Type* T = D->getTypeForDecl();
1435 assert(T->isTemplateTypeParmType());
1436 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001437
John McCalle29ba202009-08-20 01:44:21 +00001438 TemplateTypeParmDecl *Inst =
1439 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor71b87e42010-08-30 23:23:59 +00001440 TTPT->getDepth() - TemplateArgs.getNumLevels(),
Nick Lewycky61139c52010-10-30 06:48:20 +00001441 TTPT->getIndex(), D->getIdentifier(),
John McCalle29ba202009-08-20 01:44:21 +00001442 D->wasDeclaredWithTypename(),
1443 D->isParameterPack());
1444
Douglas Gregor0f8716b2009-11-09 19:17:50 +00001445 if (D->hasDefaultArgument())
1446 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
John McCalle29ba202009-08-20 01:44:21 +00001447
Douglas Gregor550d9b22009-10-31 17:21:17 +00001448 // Introduce this template parameter's instantiation into the instantiation
1449 // scope.
1450 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1451
John McCalle29ba202009-08-20 01:44:21 +00001452 return Inst;
1453}
1454
Douglas Gregor33642df2009-10-23 23:25:44 +00001455Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1456 NonTypeTemplateParmDecl *D) {
1457 // Substitute into the type of the non-type template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001458 TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc();
1459 llvm::SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten;
1460 llvm::SmallVector<QualType, 4> ExpandedParameterPackTypes;
1461 bool IsExpandedParameterPack = false;
1462 TypeSourceInfo *DI;
Douglas Gregor33642df2009-10-23 23:25:44 +00001463 QualType T;
Douglas Gregor33642df2009-10-23 23:25:44 +00001464 bool Invalid = false;
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001465
1466 if (D->isExpandedParameterPack()) {
1467 // The non-type template parameter pack is an already-expanded pack
1468 // expansion of types. Substitute into each of the expanded types.
1469 ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes());
1470 ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes());
1471 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
1472 TypeSourceInfo *NewDI =SemaRef.SubstType(D->getExpansionTypeSourceInfo(I),
1473 TemplateArgs,
1474 D->getLocation(),
1475 D->getDeclName());
1476 if (!NewDI)
1477 return 0;
1478
1479 ExpandedParameterPackTypesAsWritten.push_back(NewDI);
1480 QualType NewT =SemaRef.CheckNonTypeTemplateParameterType(NewDI->getType(),
1481 D->getLocation());
1482 if (NewT.isNull())
1483 return 0;
1484 ExpandedParameterPackTypes.push_back(NewT);
1485 }
1486
1487 IsExpandedParameterPack = true;
1488 DI = D->getTypeSourceInfo();
1489 T = DI->getType();
1490 } else if (isa<PackExpansionTypeLoc>(TL)) {
1491 // The non-type template parameter pack's type is a pack expansion of types.
1492 // Determine whether we need to expand this parameter pack into separate
1493 // types.
1494 PackExpansionTypeLoc Expansion = cast<PackExpansionTypeLoc>(TL);
1495 TypeLoc Pattern = Expansion.getPatternLoc();
1496 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
1497 SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded);
1498
1499 // Determine whether the set of unexpanded parameter packs can and should
1500 // be expanded.
1501 bool Expand = true;
1502 bool RetainExpansion = false;
1503 llvm::Optional<unsigned> OrigNumExpansions
1504 = Expansion.getTypePtr()->getNumExpansions();
1505 llvm::Optional<unsigned> NumExpansions = OrigNumExpansions;
1506 if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(),
1507 Pattern.getSourceRange(),
1508 Unexpanded.data(),
1509 Unexpanded.size(),
1510 TemplateArgs,
1511 Expand, RetainExpansion,
1512 NumExpansions))
1513 return 0;
1514
1515 if (Expand) {
1516 for (unsigned I = 0; I != *NumExpansions; ++I) {
1517 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
1518 TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs,
1519 D->getLocation(),
1520 D->getDeclName());
1521 if (!NewDI)
1522 return 0;
1523
1524 ExpandedParameterPackTypesAsWritten.push_back(NewDI);
1525 QualType NewT = SemaRef.CheckNonTypeTemplateParameterType(
1526 NewDI->getType(),
1527 D->getLocation());
1528 if (NewT.isNull())
1529 return 0;
1530 ExpandedParameterPackTypes.push_back(NewT);
1531 }
1532
1533 // Note that we have an expanded parameter pack. The "type" of this
1534 // expanded parameter pack is the original expansion type, but callers
1535 // will end up using the expanded parameter pack types for type-checking.
1536 IsExpandedParameterPack = true;
1537 DI = D->getTypeSourceInfo();
1538 T = DI->getType();
1539 } else {
1540 // We cannot fully expand the pack expansion now, so substitute into the
1541 // pattern and create a new pack expansion type.
1542 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
1543 TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs,
1544 D->getLocation(),
1545 D->getDeclName());
1546 if (!NewPattern)
1547 return 0;
1548
1549 DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(),
1550 NumExpansions);
1551 if (!DI)
1552 return 0;
1553
1554 T = DI->getType();
1555 }
1556 } else {
1557 // Simple case: substitution into a parameter that is not a parameter pack.
1558 DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs,
1559 D->getLocation(), D->getDeclName());
1560 if (!DI)
1561 return 0;
1562
1563 // Check that this type is acceptable for a non-type template parameter.
1564 bool Invalid = false;
1565 T = SemaRef.CheckNonTypeTemplateParameterType(DI->getType(),
1566 D->getLocation());
1567 if (T.isNull()) {
1568 T = SemaRef.Context.IntTy;
1569 Invalid = true;
1570 }
Douglas Gregor33642df2009-10-23 23:25:44 +00001571 }
1572
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001573 NonTypeTemplateParmDecl *Param;
1574 if (IsExpandedParameterPack)
1575 Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
1576 D->getLocation(),
Douglas Gregor71b87e42010-08-30 23:23:59 +00001577 D->getDepth() - TemplateArgs.getNumLevels(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001578 D->getPosition(),
1579 D->getIdentifier(), T,
1580 DI,
1581 ExpandedParameterPackTypes.data(),
1582 ExpandedParameterPackTypes.size(),
1583 ExpandedParameterPackTypesAsWritten.data());
1584 else
1585 Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
1586 D->getLocation(),
1587 D->getDepth() - TemplateArgs.getNumLevels(),
1588 D->getPosition(),
1589 D->getIdentifier(), T,
1590 D->isParameterPack(), DI);
1591
Douglas Gregor33642df2009-10-23 23:25:44 +00001592 if (Invalid)
1593 Param->setInvalidDecl();
1594
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001595 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor550d9b22009-10-31 17:21:17 +00001596
1597 // Introduce this template parameter's instantiation into the instantiation
1598 // scope.
1599 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001600 return Param;
1601}
1602
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001603Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001604TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1605 TemplateTemplateParmDecl *D) {
1606 // Instantiate the template parameter list of the template template parameter.
1607 TemplateParameterList *TempParams = D->getTemplateParameters();
1608 TemplateParameterList *InstParams;
1609 {
1610 // Perform the actual substitution of template parameters within a new,
1611 // local instantiation scope.
John McCall2a7fb272010-08-25 05:32:35 +00001612 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor9106ef72009-11-11 16:58:32 +00001613 InstParams = SubstTemplateParams(TempParams);
1614 if (!InstParams)
1615 return NULL;
1616 }
1617
1618 // Build the template template parameter.
1619 TemplateTemplateParmDecl *Param
1620 = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor71b87e42010-08-30 23:23:59 +00001621 D->getDepth() - TemplateArgs.getNumLevels(),
Douglas Gregor61c4d282011-01-05 15:48:55 +00001622 D->getPosition(), D->isParameterPack(),
1623 D->getIdentifier(), InstParams);
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001624 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001625
Douglas Gregor9106ef72009-11-11 16:58:32 +00001626 // Introduce this template parameter's instantiation into the instantiation
1627 // scope.
1628 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1629
1630 return Param;
1631}
1632
Douglas Gregor48c32a72009-11-17 06:07:40 +00001633Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1634 // Using directives are never dependent, so they require no explicit
1635
1636 UsingDirectiveDecl *Inst
1637 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1638 D->getNamespaceKeyLocation(),
1639 D->getQualifierRange(), D->getQualifier(),
1640 D->getIdentLocation(),
1641 D->getNominatedNamespace(),
1642 D->getCommonAncestor());
1643 Owner->addDecl(Inst);
1644 return Inst;
1645}
1646
John McCalled976492009-12-04 22:46:56 +00001647Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
Douglas Gregor1b398202010-09-29 17:58:28 +00001648
1649 // The nested name specifier may be dependent, for example
1650 // template <typename T> struct t {
1651 // struct s1 { T f1(); };
1652 // struct s2 : s1 { using s1::f1; };
1653 // };
1654 // template struct t<int>;
1655 // Here, in using s1::f1, s1 refers to t<T>::s1;
1656 // we need to substitute for t<int>::s1.
1657 NestedNameSpecifier *NNS =
1658 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameDecl(),
1659 D->getNestedNameRange(),
1660 TemplateArgs);
1661 if (!NNS)
1662 return 0;
1663
1664 // The name info is non-dependent, so no transformation
1665 // is required.
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001666 DeclarationNameInfo NameInfo = D->getNameInfo();
John McCalled976492009-12-04 22:46:56 +00001667
John McCall9f54ad42009-12-10 09:41:52 +00001668 // We only need to do redeclaration lookups if we're in a class
1669 // scope (in fact, it's not really even possible in non-class
1670 // scopes).
1671 bool CheckRedeclaration = Owner->isRecord();
1672
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001673 LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
1674 Sema::ForRedeclaration);
John McCall9f54ad42009-12-10 09:41:52 +00001675
John McCalled976492009-12-04 22:46:56 +00001676 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
John McCalled976492009-12-04 22:46:56 +00001677 D->getNestedNameRange(),
1678 D->getUsingLocation(),
Douglas Gregor1b398202010-09-29 17:58:28 +00001679 NNS,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001680 NameInfo,
John McCalled976492009-12-04 22:46:56 +00001681 D->isTypeName());
1682
1683 CXXScopeSpec SS;
Douglas Gregor1b398202010-09-29 17:58:28 +00001684 SS.setScopeRep(NNS);
John McCalled976492009-12-04 22:46:56 +00001685 SS.setRange(D->getNestedNameRange());
John McCall9f54ad42009-12-10 09:41:52 +00001686
1687 if (CheckRedeclaration) {
1688 Prev.setHideTags(false);
1689 SemaRef.LookupQualifiedName(Prev, Owner);
1690
1691 // Check for invalid redeclarations.
1692 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
1693 D->isTypeName(), SS,
1694 D->getLocation(), Prev))
1695 NewUD->setInvalidDecl();
1696
1697 }
1698
1699 if (!NewUD->isInvalidDecl() &&
1700 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
John McCalled976492009-12-04 22:46:56 +00001701 D->getLocation()))
1702 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00001703
John McCalled976492009-12-04 22:46:56 +00001704 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1705 NewUD->setAccess(D->getAccess());
1706 Owner->addDecl(NewUD);
1707
John McCall9f54ad42009-12-10 09:41:52 +00001708 // Don't process the shadow decls for an invalid decl.
1709 if (NewUD->isInvalidDecl())
1710 return NewUD;
1711
John McCall323c3102009-12-22 22:26:37 +00001712 bool isFunctionScope = Owner->isFunctionOrMethod();
1713
John McCall9f54ad42009-12-10 09:41:52 +00001714 // Process the shadow decls.
1715 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
1716 I != E; ++I) {
1717 UsingShadowDecl *Shadow = *I;
1718 NamedDecl *InstTarget =
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001719 cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getLocation(),
1720 Shadow->getTargetDecl(),
John McCall9f54ad42009-12-10 09:41:52 +00001721 TemplateArgs));
1722
1723 if (CheckRedeclaration &&
1724 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
1725 continue;
1726
1727 UsingShadowDecl *InstShadow
1728 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
1729 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
John McCall323c3102009-12-22 22:26:37 +00001730
1731 if (isFunctionScope)
1732 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
John McCall9f54ad42009-12-10 09:41:52 +00001733 }
John McCalled976492009-12-04 22:46:56 +00001734
1735 return NewUD;
1736}
1737
1738Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00001739 // Ignore these; we handle them in bulk when processing the UsingDecl.
1740 return 0;
John McCalled976492009-12-04 22:46:56 +00001741}
1742
John McCall7ba107a2009-11-18 02:36:19 +00001743Decl * TemplateDeclInstantiator
1744 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +00001745 NestedNameSpecifier *NNS =
1746 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1747 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001748 TemplateArgs);
1749 if (!NNS)
1750 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001751
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001752 CXXScopeSpec SS;
1753 SS.setRange(D->getTargetNestedNameRange());
1754 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +00001755
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001756 // Since NameInfo refers to a typename, it cannot be a C++ special name.
1757 // Hence, no tranformation is required for it.
1758 DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001759 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00001760 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001761 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00001762 /*instantiation*/ true,
1763 /*typename*/ true, D->getTypenameLoc());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001764 if (UD)
John McCalled976492009-12-04 22:46:56 +00001765 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1766
John McCall7ba107a2009-11-18 02:36:19 +00001767 return UD;
1768}
1769
1770Decl * TemplateDeclInstantiator
1771 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1772 NestedNameSpecifier *NNS =
1773 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1774 D->getTargetNestedNameRange(),
1775 TemplateArgs);
1776 if (!NNS)
1777 return 0;
1778
1779 CXXScopeSpec SS;
1780 SS.setRange(D->getTargetNestedNameRange());
1781 SS.setScopeRep(NNS);
1782
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001783 DeclarationNameInfo NameInfo
1784 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
1785
John McCall7ba107a2009-11-18 02:36:19 +00001786 NamedDecl *UD =
1787 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001788 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00001789 /*instantiation*/ true,
1790 /*typename*/ false, SourceLocation());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001791 if (UD)
John McCalled976492009-12-04 22:46:56 +00001792 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1793
Anders Carlsson0d8df782009-08-29 19:37:28 +00001794 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001795}
1796
John McCallce3ff2b2009-08-25 22:02:44 +00001797Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001798 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001799 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor2fa98002010-02-16 19:28:15 +00001800 if (D->isInvalidDecl())
1801 return 0;
1802
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001803 return Instantiator.Visit(D);
1804}
1805
John McCalle29ba202009-08-20 01:44:21 +00001806/// \brief Instantiates a nested template parameter list in the current
1807/// instantiation context.
1808///
1809/// \param L The parameter list to instantiate
1810///
1811/// \returns NULL if there was an error
1812TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001813TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001814 // Get errors for all the parameters before bailing out.
1815 bool Invalid = false;
1816
1817 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001818 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001819 ParamVector Params;
1820 Params.reserve(N);
1821 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1822 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001823 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001824 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001825 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00001826 }
1827
1828 // Clean up if we had an error.
Douglas Gregorff331c12010-07-25 18:17:45 +00001829 if (Invalid)
John McCalle29ba202009-08-20 01:44:21 +00001830 return NULL;
John McCalle29ba202009-08-20 01:44:21 +00001831
1832 TemplateParameterList *InstL
1833 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1834 L->getLAngleLoc(), &Params.front(), N,
1835 L->getRAngleLoc());
1836 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001837}
John McCalle29ba202009-08-20 01:44:21 +00001838
Douglas Gregored9c0f92009-10-29 00:04:11 +00001839/// \brief Instantiate the declaration of a class template partial
1840/// specialization.
1841///
1842/// \param ClassTemplate the (instantiated) class template that is partially
1843// specialized by the instantiation of \p PartialSpec.
1844///
1845/// \param PartialSpec the (uninstantiated) class template partial
1846/// specialization that we are instantiating.
1847///
Douglas Gregord65587f2010-11-10 19:44:59 +00001848/// \returns The instantiated partial specialization, if successful; otherwise,
1849/// NULL to indicate an error.
1850ClassTemplatePartialSpecializationDecl *
Douglas Gregored9c0f92009-10-29 00:04:11 +00001851TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1852 ClassTemplateDecl *ClassTemplate,
1853 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001854 // Create a local instantiation scope for this class template partial
1855 // specialization, which will contain the instantiations of the template
1856 // parameters.
John McCall2a7fb272010-08-25 05:32:35 +00001857 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor550d9b22009-10-31 17:21:17 +00001858
Douglas Gregored9c0f92009-10-29 00:04:11 +00001859 // Substitute into the template parameters of the class template partial
1860 // specialization.
1861 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1862 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1863 if (!InstParams)
Douglas Gregord65587f2010-11-10 19:44:59 +00001864 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001865
1866 // Substitute into the template arguments of the class template partial
1867 // specialization.
John McCalld5532b62009-11-23 01:53:49 +00001868 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
Douglas Gregore02e2622010-12-22 21:19:48 +00001869 if (SemaRef.Subst(PartialSpec->getTemplateArgsAsWritten(),
1870 PartialSpec->getNumTemplateArgsAsWritten(),
1871 InstTemplateArgs, TemplateArgs))
1872 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001873
Douglas Gregored9c0f92009-10-29 00:04:11 +00001874 // Check that the template argument list is well-formed for this
1875 // class template.
Douglas Gregor910f8002010-11-07 23:05:16 +00001876 llvm::SmallVector<TemplateArgument, 4> Converted;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001877 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1878 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001879 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001880 false,
1881 Converted))
Douglas Gregord65587f2010-11-10 19:44:59 +00001882 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001883
1884 // Figure out where to insert this class template partial specialization
1885 // in the member template's set of class template partial specializations.
Douglas Gregored9c0f92009-10-29 00:04:11 +00001886 void *InsertPos = 0;
1887 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00001888 = ClassTemplate->findPartialSpecialization(Converted.data(),
1889 Converted.size(), InsertPos);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001890
1891 // Build the canonical type that describes the converted template
1892 // arguments of the class template partial specialization.
1893 QualType CanonType
1894 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
Douglas Gregor910f8002010-11-07 23:05:16 +00001895 Converted.data(),
1896 Converted.size());
Douglas Gregored9c0f92009-10-29 00:04:11 +00001897
1898 // Build the fully-sugared type for this class template
1899 // specialization as the user wrote in the specialization
1900 // itself. This means that we'll pretty-print the type retrieved
1901 // from the specialization's declaration the way that the user
1902 // actually wrote the specialization, rather than formatting the
1903 // name based on the "canonical" representation used to store the
1904 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00001905 TypeSourceInfo *WrittenTy
1906 = SemaRef.Context.getTemplateSpecializationTypeInfo(
1907 TemplateName(ClassTemplate),
1908 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001909 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001910 CanonType);
1911
1912 if (PrevDecl) {
1913 // We've already seen a partial specialization with the same template
1914 // parameters and template arguments. This can happen, for example, when
1915 // substituting the outer template arguments ends up causing two
1916 // class template partial specializations of a member class template
1917 // to have identical forms, e.g.,
1918 //
1919 // template<typename T, typename U>
1920 // struct Outer {
1921 // template<typename X, typename Y> struct Inner;
1922 // template<typename Y> struct Inner<T, Y>;
1923 // template<typename Y> struct Inner<U, Y>;
1924 // };
1925 //
1926 // Outer<int, int> outer; // error: the partial specializations of Inner
1927 // // have the same signature.
1928 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
Douglas Gregord65587f2010-11-10 19:44:59 +00001929 << WrittenTy->getType();
Douglas Gregored9c0f92009-10-29 00:04:11 +00001930 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1931 << SemaRef.Context.getTypeDeclType(PrevDecl);
Douglas Gregord65587f2010-11-10 19:44:59 +00001932 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001933 }
1934
1935
1936 // Create the class template partial specialization declaration.
1937 ClassTemplatePartialSpecializationDecl *InstPartialSpec
Douglas Gregor13c85772010-05-06 00:28:52 +00001938 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
1939 PartialSpec->getTagKind(),
1940 Owner,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001941 PartialSpec->getLocation(),
1942 InstParams,
1943 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001944 Converted.data(),
1945 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00001946 InstTemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00001947 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00001948 0,
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001949 ClassTemplate->getNextPartialSpecSequenceNumber());
John McCallb6217662010-03-15 10:12:16 +00001950 // Substitute the nested name specifier, if any.
1951 if (SubstQualifier(PartialSpec, InstPartialSpec))
1952 return 0;
1953
Douglas Gregored9c0f92009-10-29 00:04:11 +00001954 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001955 InstPartialSpec->setTypeAsWritten(WrittenTy);
1956
Douglas Gregored9c0f92009-10-29 00:04:11 +00001957 // Add this partial specialization to the set of class template partial
1958 // specializations.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001959 ClassTemplate->AddPartialSpecialization(InstPartialSpec, InsertPos);
Douglas Gregord65587f2010-11-10 19:44:59 +00001960 return InstPartialSpec;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001961}
1962
John McCall21ef0fa2010-03-11 09:03:00 +00001963TypeSourceInfo*
John McCallce3ff2b2009-08-25 22:02:44 +00001964TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001965 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
John McCall21ef0fa2010-03-11 09:03:00 +00001966 TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
1967 assert(OldTInfo && "substituting function without type source info");
1968 assert(Params.empty() && "parameter vector is non-empty at start");
John McCall6cd3b9f2010-04-09 17:38:44 +00001969 TypeSourceInfo *NewTInfo
1970 = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
1971 D->getTypeSpecStartLoc(),
1972 D->getDeclName());
John McCall21ef0fa2010-03-11 09:03:00 +00001973 if (!NewTInfo)
1974 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +00001975
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001976 if (NewTInfo != OldTInfo) {
1977 // Get parameters from the new type info.
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00001978 TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001979 if (FunctionProtoTypeLoc *OldProtoLoc
1980 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00001981 TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001982 FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
1983 assert(NewProtoLoc && "Missing prototype?");
Douglas Gregor12c9c002011-01-07 16:43:16 +00001984 unsigned NewIdx = 0, NumNewParams = NewProtoLoc->getNumArgs();
1985 for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc->getNumArgs();
1986 OldIdx != NumOldParams; ++OldIdx) {
1987 ParmVarDecl *OldParam = OldProtoLoc->getArg(OldIdx);
1988 if (!OldParam->isParameterPack() ||
1989 (NewIdx < NumNewParams &&
1990 NewProtoLoc->getArg(NewIdx)->isParameterPack())) {
1991 // Simple case: normal parameter, or a parameter pack that's
1992 // instantiated to a (still-dependent) parameter pack.
1993 ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++);
1994 Params.push_back(NewParam);
1995 SemaRef.CurrentInstantiationScope->InstantiatedLocal(OldParam,
1996 NewParam);
1997 continue;
1998 }
1999
2000 // Parameter pack: make the instantiation an argument pack.
2001 SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(
2002 OldParam);
Douglas Gregor21371ea2011-01-11 03:14:20 +00002003 unsigned NumArgumentsInExpansion
2004 = SemaRef.getNumArgumentsInExpansion(OldParam->getType(),
2005 TemplateArgs);
2006 while (NumArgumentsInExpansion--) {
Douglas Gregor12c9c002011-01-07 16:43:16 +00002007 ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++);
2008 Params.push_back(NewParam);
2009 SemaRef.CurrentInstantiationScope->InstantiatedLocalPackArg(OldParam,
2010 NewParam);
2011 }
Douglas Gregor6920cdc2010-05-03 15:32:18 +00002012 }
Douglas Gregor895162d2010-04-30 18:55:50 +00002013 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00002014 } else {
2015 // The function type itself was not dependent and therefore no
2016 // substitution occurred. However, we still need to instantiate
2017 // the function parameters themselves.
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00002018 TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00002019 if (FunctionProtoTypeLoc *OldProtoLoc
2020 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
2021 for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
2022 ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
2023 if (!Parm)
2024 return 0;
2025 Params.push_back(Parm);
2026 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00002027 }
2028 }
John McCall21ef0fa2010-03-11 09:03:00 +00002029 return NewTInfo;
Douglas Gregor5545e162009-03-24 00:38:23 +00002030}
2031
Mike Stump1eb44332009-09-09 15:08:12 +00002032/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00002033/// declaration (New) from the corresponding fields of its template (Tmpl).
2034///
2035/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00002036bool
2037TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00002038 FunctionDecl *Tmpl) {
2039 if (Tmpl->isDeleted())
2040 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00002041
Douglas Gregorcca9e962009-07-01 22:01:06 +00002042 // If we are performing substituting explicitly-specified template arguments
2043 // or deduced template arguments into a function template and we reach this
2044 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00002045 // to keeping the new function template specialization. We therefore
2046 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00002047 // into a template instantiation for this specific function template
2048 // specialization, which is not a SFINAE context, so that we diagnose any
2049 // further errors in the declaration itself.
2050 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
2051 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
2052 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
2053 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00002054 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00002055 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002056 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00002057 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00002058 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00002059 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
2060 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
Douglas Gregorf35f8282009-11-11 21:54:23 +00002061 --SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +00002062 }
2063 }
Mike Stump1eb44332009-09-09 15:08:12 +00002064
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002065 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
2066 assert(Proto && "Function template without prototype?");
2067
2068 if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
2069 Proto->getNoReturnAttr()) {
2070 // The function has an exception specification or a "noreturn"
2071 // attribute. Substitute into each of the exception types.
2072 llvm::SmallVector<QualType, 4> Exceptions;
2073 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
2074 // FIXME: Poor location information!
Douglas Gregorb99268b2010-12-21 00:52:54 +00002075 if (const PackExpansionType *PackExpansion
2076 = Proto->getExceptionType(I)->getAs<PackExpansionType>()) {
2077 // We have a pack expansion. Instantiate it.
2078 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2079 SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(),
2080 Unexpanded);
2081 assert(!Unexpanded.empty() &&
2082 "Pack expansion without parameter packs?");
2083
2084 bool Expand = false;
Douglas Gregord3731192011-01-10 07:32:04 +00002085 bool RetainExpansion = false;
Douglas Gregorcded4f62011-01-14 17:04:44 +00002086 llvm::Optional<unsigned> NumExpansions
2087 = PackExpansion->getNumExpansions();
Douglas Gregorb99268b2010-12-21 00:52:54 +00002088 if (SemaRef.CheckParameterPacksForExpansion(New->getLocation(),
2089 SourceRange(),
2090 Unexpanded.data(),
2091 Unexpanded.size(),
2092 TemplateArgs,
Douglas Gregord3731192011-01-10 07:32:04 +00002093 Expand,
2094 RetainExpansion,
2095 NumExpansions))
Douglas Gregorb99268b2010-12-21 00:52:54 +00002096 break;
2097
2098 if (!Expand) {
2099 // We can't expand this pack expansion into separate arguments yet;
Douglas Gregorcded4f62011-01-14 17:04:44 +00002100 // just substitute into the pattern and create a new pack expansion
2101 // type.
Douglas Gregorb99268b2010-12-21 00:52:54 +00002102 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
2103 QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2104 TemplateArgs,
2105 New->getLocation(), New->getDeclName());
2106 if (T.isNull())
2107 break;
2108
Douglas Gregorcded4f62011-01-14 17:04:44 +00002109 T = SemaRef.Context.getPackExpansionType(T, NumExpansions);
Douglas Gregorb99268b2010-12-21 00:52:54 +00002110 Exceptions.push_back(T);
2111 continue;
2112 }
2113
2114 // Substitute into the pack expansion pattern for each template
2115 bool Invalid = false;
Douglas Gregorcded4f62011-01-14 17:04:44 +00002116 for (unsigned ArgIdx = 0; ArgIdx != *NumExpansions; ++ArgIdx) {
Douglas Gregorb99268b2010-12-21 00:52:54 +00002117 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, ArgIdx);
2118
2119 QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2120 TemplateArgs,
2121 New->getLocation(), New->getDeclName());
2122 if (T.isNull()) {
2123 Invalid = true;
2124 break;
2125 }
2126
2127 Exceptions.push_back(T);
2128 }
2129
2130 if (Invalid)
2131 break;
2132
2133 continue;
2134 }
2135
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002136 QualType T
2137 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
2138 New->getLocation(), New->getDeclName());
2139 if (T.isNull() ||
2140 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
2141 continue;
2142
2143 Exceptions.push_back(T);
2144 }
2145
2146 // Rebuild the function type
2147
John McCalle23cf432010-12-14 08:05:40 +00002148 FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
2149 EPI.HasExceptionSpec = Proto->hasExceptionSpec();
2150 EPI.HasAnyExceptionSpec = Proto->hasAnyExceptionSpec();
2151 EPI.NumExceptions = Exceptions.size();
2152 EPI.Exceptions = Exceptions.data();
2153 EPI.ExtInfo = Proto->getExtInfo();
2154
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002155 const FunctionProtoType *NewProto
2156 = New->getType()->getAs<FunctionProtoType>();
2157 assert(NewProto && "Template instantiation without function prototype?");
2158 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
2159 NewProto->arg_type_begin(),
2160 NewProto->getNumArgs(),
John McCalle23cf432010-12-14 08:05:40 +00002161 EPI));
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002162 }
2163
John McCall1d8d1cc2010-08-01 02:01:53 +00002164 SemaRef.InstantiateAttrs(TemplateArgs, Tmpl, New);
Douglas Gregor7cf84d62010-06-15 17:05:35 +00002165
Douglas Gregore53060f2009-06-25 22:08:12 +00002166 return false;
2167}
2168
Douglas Gregor5545e162009-03-24 00:38:23 +00002169/// \brief Initializes common fields of an instantiated method
2170/// declaration (New) from the corresponding fields of its template
2171/// (Tmpl).
2172///
2173/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00002174bool
2175TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00002176 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00002177 if (InitFunctionInstantiation(New, Tmpl))
2178 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002179
Douglas Gregor5545e162009-03-24 00:38:23 +00002180 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00002181 if (Tmpl->isVirtualAsWritten())
Douglas Gregor85606eb2010-09-28 20:50:54 +00002182 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +00002183
2184 // FIXME: attributes
2185 // FIXME: New needs a pointer to Tmpl
2186 return false;
2187}
Douglas Gregora58861f2009-05-13 20:28:22 +00002188
2189/// \brief Instantiate the definition of the given function from its
2190/// template.
2191///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002192/// \param PointOfInstantiation the point at which the instantiation was
2193/// required. Note that this is not precisely a "point of instantiation"
2194/// for the function, but it's close.
2195///
Douglas Gregora58861f2009-05-13 20:28:22 +00002196/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002197/// function template specialization or member function of a class template
2198/// specialization.
2199///
2200/// \param Recursive if true, recursively instantiates any functions that
2201/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002202///
2203/// \param DefinitionRequired if true, then we are performing an explicit
2204/// instantiation where the body of the function is required. Complain if
2205/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002206void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002207 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002208 bool Recursive,
2209 bool DefinitionRequired) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002210 if (Function->isInvalidDecl() || Function->hasBody())
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002211 return;
2212
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002213 // Never instantiate an explicit specialization.
2214 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2215 return;
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002216
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002217 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00002218 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002219 Stmt *Pattern = 0;
2220 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00002221 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002222
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002223 if (!Pattern) {
2224 if (DefinitionRequired) {
2225 if (Function->getPrimaryTemplate())
2226 Diag(PointOfInstantiation,
2227 diag::err_explicit_instantiation_undefined_func_template)
2228 << Function->getPrimaryTemplate();
2229 else
2230 Diag(PointOfInstantiation,
2231 diag::err_explicit_instantiation_undefined_member)
2232 << 1 << Function->getDeclName() << Function->getDeclContext();
2233
2234 if (PatternDecl)
2235 Diag(PatternDecl->getLocation(),
2236 diag::note_explicit_instantiation_here);
Douglas Gregorcfe833b2010-05-17 17:57:54 +00002237 Function->setInvalidDecl();
Chandler Carruth58e390e2010-08-25 08:27:02 +00002238 } else if (Function->getTemplateSpecializationKind()
2239 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002240 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00002241 std::make_pair(Function, PointOfInstantiation));
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002242 }
Chandler Carruth58e390e2010-08-25 08:27:02 +00002243
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002244 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002245 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002246
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002247 // C++0x [temp.explicit]p9:
2248 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00002249 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002250 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00002251 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002252 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002253 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002254 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002255
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002256 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2257 if (Inst)
Douglas Gregore7089b02010-05-03 23:29:10 +00002258 return;
2259
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002260 // If we're performing recursive template instantiation, create our own
2261 // queue of pending implicit instantiations that we will instantiate later,
2262 // while we're still within our own instantiation context.
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002263 llvm::SmallVector<VTableUse, 16> SavedVTableUses;
Chandler Carruth62c78d52010-08-25 08:44:16 +00002264 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002265 if (Recursive) {
2266 VTableUses.swap(SavedVTableUses);
Chandler Carruth62c78d52010-08-25 08:44:16 +00002267 PendingInstantiations.swap(SavedPendingInstantiations);
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002268 }
Mike Stump1eb44332009-09-09 15:08:12 +00002269
Douglas Gregor9679caf2010-05-12 17:27:19 +00002270 EnterExpressionEvaluationContext EvalContext(*this,
John McCallf312b1e2010-08-26 23:41:50 +00002271 Sema::PotentiallyEvaluated);
John McCalld226f652010-08-21 09:40:31 +00002272 ActOnStartOfFunctionDef(0, Function);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002273
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002274 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00002275 // recorded, unless we're actually a member function within a local
2276 // class, in which case we need to merge our results with the parent
2277 // scope (of the enclosing function).
2278 bool MergeWithParentScope = false;
2279 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
2280 MergeWithParentScope = Rec->isLocalClass();
2281
2282 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002283
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002284 // Introduce the instantiated function parameters into the local
Peter Collingbourne8a6c0f12010-07-18 16:45:46 +00002285 // instantiation scope, and set the parameter names to those used
2286 // in the template.
Douglas Gregor12c9c002011-01-07 16:43:16 +00002287 unsigned FParamIdx = 0;
Peter Collingbourne8a6c0f12010-07-18 16:45:46 +00002288 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
2289 const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
Douglas Gregor12c9c002011-01-07 16:43:16 +00002290 if (!PatternParam->isParameterPack()) {
2291 // Simple case: not a parameter pack.
2292 assert(FParamIdx < Function->getNumParams());
2293 ParmVarDecl *FunctionParam = Function->getParamDecl(I);
2294 FunctionParam->setDeclName(PatternParam->getDeclName());
2295 Scope.InstantiatedLocal(PatternParam, FunctionParam);
2296 ++FParamIdx;
2297 continue;
2298 }
2299
2300 // Expand the parameter pack.
2301 Scope.MakeInstantiatedLocalArgPack(PatternParam);
2302 for (unsigned NumFParams = Function->getNumParams();
2303 FParamIdx < NumFParams;
2304 ++FParamIdx) {
2305 ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
2306 FunctionParam->setDeclName(PatternParam->getDeclName());
2307 Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam);
2308 }
Peter Collingbourne8a6c0f12010-07-18 16:45:46 +00002309 }
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002310
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002311 // Enter the scope of this instantiation. We don't use
2312 // PushDeclContext because we don't have a scope.
2313 DeclContext *PreviousContext = CurContext;
2314 CurContext = Function;
2315
Mike Stump1eb44332009-09-09 15:08:12 +00002316 MultiLevelTemplateArgumentList TemplateArgs =
Douglas Gregore7089b02010-05-03 23:29:10 +00002317 getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
Anders Carlsson09025312009-08-29 05:16:22 +00002318
2319 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00002320 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00002321 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2322 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2323 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002324 }
2325
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002326 // Instantiate the function body.
John McCall60d7b3a2010-08-24 06:29:42 +00002327 StmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002328
Douglas Gregor52604ab2009-09-11 21:19:12 +00002329 if (Body.isInvalid())
2330 Function->setInvalidDecl();
2331
John McCall9ae2f072010-08-23 23:25:46 +00002332 ActOnFinishFunctionBody(Function, Body.get(),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002333 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002334
John McCall0c01d182010-03-24 05:22:00 +00002335 PerformDependentDiagnostics(PatternDecl, TemplateArgs);
2336
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002337 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00002338
2339 DeclGroupRef DG(Function);
2340 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00002341
Douglas Gregor60406be2010-01-16 22:29:39 +00002342 // This class may have local implicit instantiations that need to be
2343 // instantiation within this scope.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002344 PerformPendingInstantiations(/*LocalOnly=*/true);
Douglas Gregor60406be2010-01-16 22:29:39 +00002345 Scope.Exit();
2346
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002347 if (Recursive) {
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002348 // Define any pending vtables.
2349 DefineUsedVTables();
2350
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002351 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002352 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002353 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002354
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002355 // Restore the set of pending vtables.
2356 VTableUses.swap(SavedVTableUses);
2357
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002358 // Restore the set of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002359 PendingInstantiations.swap(SavedPendingInstantiations);
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002360 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002361}
2362
2363/// \brief Instantiate the definition of the given variable from its
2364/// template.
2365///
Douglas Gregor7caa6822009-07-24 20:34:43 +00002366/// \param PointOfInstantiation the point at which the instantiation was
2367/// required. Note that this is not precisely a "point of instantiation"
2368/// for the function, but it's close.
2369///
2370/// \param Var the already-instantiated declaration of a static member
2371/// variable of a class template specialization.
2372///
2373/// \param Recursive if true, recursively instantiates any functions that
2374/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002375///
2376/// \param DefinitionRequired if true, then we are performing an explicit
2377/// instantiation where an out-of-line definition of the member variable
2378/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002379void Sema::InstantiateStaticDataMemberDefinition(
2380 SourceLocation PointOfInstantiation,
2381 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002382 bool Recursive,
2383 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002384 if (Var->isInvalidDecl())
2385 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002386
Douglas Gregor7caa6822009-07-24 20:34:43 +00002387 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002388 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002389 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00002390 assert(Def->isStaticDataMember() && "Not a static data member?");
2391 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002392
Douglas Gregor0d035142009-10-27 18:42:08 +00002393 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002394 // We did not find an out-of-line definition of this static data member,
2395 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00002396 // instantiate this definition (or provide a specialization for it) in
2397 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002398 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00002399 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002400 Diag(PointOfInstantiation,
2401 diag::err_explicit_instantiation_undefined_member)
2402 << 2 << Var->getDeclName() << Var->getDeclContext();
2403 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
Chandler Carruth58e390e2010-08-25 08:27:02 +00002404 } else if (Var->getTemplateSpecializationKind()
2405 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002406 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00002407 std::make_pair(Var, PointOfInstantiation));
2408 }
2409
Douglas Gregor7caa6822009-07-24 20:34:43 +00002410 return;
2411 }
2412
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002413 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002414 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002415 return;
2416
2417 // C++0x [temp.explicit]p9:
2418 // Except for inline functions, other explicit instantiation declarations
2419 // have the effect of suppressing the implicit instantiation of the entity
2420 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002421 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002422 == TSK_ExplicitInstantiationDeclaration)
2423 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002424
Douglas Gregor7caa6822009-07-24 20:34:43 +00002425 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
2426 if (Inst)
2427 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002428
Douglas Gregor7caa6822009-07-24 20:34:43 +00002429 // If we're performing recursive template instantiation, create our own
2430 // queue of pending implicit instantiations that we will instantiate later,
2431 // while we're still within our own instantiation context.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002432 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Douglas Gregor7caa6822009-07-24 20:34:43 +00002433 if (Recursive)
Chandler Carruth62c78d52010-08-25 08:44:16 +00002434 PendingInstantiations.swap(SavedPendingInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002435
Douglas Gregor7caa6822009-07-24 20:34:43 +00002436 // Enter the scope of this instantiation. We don't use
2437 // PushDeclContext because we don't have a scope.
2438 DeclContext *PreviousContext = CurContext;
2439 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00002440
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002441 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00002442 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Nico Weber6bb4dcb2010-11-28 22:53:37 +00002443 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00002444 CurContext = PreviousContext;
2445
2446 if (Var) {
Douglas Gregor583f33b2009-10-15 18:07:02 +00002447 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2448 assert(MSInfo && "Missing member specialization information?");
2449 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2450 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00002451 DeclGroupRef DG(Var);
2452 Consumer.HandleTopLevelDecl(DG);
2453 }
Mike Stump1eb44332009-09-09 15:08:12 +00002454
Douglas Gregor7caa6822009-07-24 20:34:43 +00002455 if (Recursive) {
2456 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002457 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002458 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002459
Douglas Gregor7caa6822009-07-24 20:34:43 +00002460 // Restore the set of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002461 PendingInstantiations.swap(SavedPendingInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002462 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002463}
Douglas Gregor815215d2009-05-27 05:35:12 +00002464
Anders Carlsson09025312009-08-29 05:16:22 +00002465void
2466Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
2467 const CXXConstructorDecl *Tmpl,
2468 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00002469
Anders Carlsson09025312009-08-29 05:16:22 +00002470 llvm::SmallVector<MemInitTy*, 4> NewInits;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002471 bool AnyErrors = false;
2472
Anders Carlsson09025312009-08-29 05:16:22 +00002473 // Instantiate all the initializers.
2474 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00002475 InitsEnd = Tmpl->init_end();
2476 Inits != InitsEnd; ++Inits) {
Sean Huntcbb67482011-01-08 20:30:50 +00002477 CXXCtorInitializer *Init = *Inits;
Anders Carlsson09025312009-08-29 05:16:22 +00002478
Chandler Carruth030ef472010-09-03 21:54:20 +00002479 // Only instantiate written initializers, let Sema re-construct implicit
2480 // ones.
2481 if (!Init->isWritten())
2482 continue;
2483
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002484 SourceLocation LParenLoc, RParenLoc;
John McCallca0408f2010-08-23 06:44:23 +00002485 ASTOwningVector<Expr*> NewArgs(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002486
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002487 SourceLocation EllipsisLoc;
2488
2489 if (Init->isPackExpansion()) {
2490 // This is a pack expansion. We should expand it now.
2491 TypeLoc BaseTL = Init->getBaseClassInfo()->getTypeLoc();
2492 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2493 collectUnexpandedParameterPacks(BaseTL, Unexpanded);
2494 bool ShouldExpand = false;
Douglas Gregord3731192011-01-10 07:32:04 +00002495 bool RetainExpansion = false;
Douglas Gregorcded4f62011-01-14 17:04:44 +00002496 llvm::Optional<unsigned> NumExpansions;
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002497 if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(),
2498 BaseTL.getSourceRange(),
2499 Unexpanded.data(),
2500 Unexpanded.size(),
2501 TemplateArgs, ShouldExpand,
Douglas Gregord3731192011-01-10 07:32:04 +00002502 RetainExpansion,
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002503 NumExpansions)) {
2504 AnyErrors = true;
2505 New->setInvalidDecl();
2506 continue;
2507 }
2508 assert(ShouldExpand && "Partial instantiation of base initializer?");
2509
2510 // Loop over all of the arguments in the argument pack(s),
Douglas Gregorcded4f62011-01-14 17:04:44 +00002511 for (unsigned I = 0; I != *NumExpansions; ++I) {
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002512 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
2513
2514 // Instantiate the initializer.
2515 if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
2516 LParenLoc, NewArgs, RParenLoc)) {
2517 AnyErrors = true;
2518 break;
2519 }
2520
2521 // Instantiate the base type.
2522 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
2523 TemplateArgs,
2524 Init->getSourceLocation(),
2525 New->getDeclName());
2526 if (!BaseTInfo) {
2527 AnyErrors = true;
2528 break;
2529 }
2530
2531 // Build the initializer.
2532 MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(),
2533 BaseTInfo,
2534 (Expr **)NewArgs.data(),
2535 NewArgs.size(),
2536 Init->getLParenLoc(),
2537 Init->getRParenLoc(),
2538 New->getParent(),
2539 SourceLocation());
2540 if (NewInit.isInvalid()) {
2541 AnyErrors = true;
2542 break;
2543 }
2544
2545 NewInits.push_back(NewInit.get());
2546 NewArgs.clear();
2547 }
2548
2549 continue;
2550 }
2551
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002552 // Instantiate the initializer.
2553 if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
Douglas Gregora1a04782010-09-09 16:33:13 +00002554 LParenLoc, NewArgs, RParenLoc)) {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002555 AnyErrors = true;
2556 continue;
Anders Carlsson09025312009-08-29 05:16:22 +00002557 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002558
Anders Carlsson09025312009-08-29 05:16:22 +00002559 MemInitResult NewInit;
Anders Carlsson09025312009-08-29 05:16:22 +00002560 if (Init->isBaseInitializer()) {
John McCalla93c9342009-12-07 02:54:59 +00002561 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002562 TemplateArgs,
2563 Init->getSourceLocation(),
2564 New->getDeclName());
John McCalla93c9342009-12-07 02:54:59 +00002565 if (!BaseTInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002566 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00002567 New->setInvalidDecl();
2568 continue;
2569 }
2570
John McCalla93c9342009-12-07 02:54:59 +00002571 NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00002572 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002573 NewArgs.size(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002574 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002575 Init->getRParenLoc(),
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002576 New->getParent(),
2577 EllipsisLoc);
Anders Carlsson09025312009-08-29 05:16:22 +00002578 } else if (Init->isMemberInitializer()) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00002579 FieldDecl *Member = cast<FieldDecl>(FindInstantiatedDecl(
2580 Init->getMemberLocation(),
2581 Init->getMember(),
2582 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00002583
2584 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002585 NewArgs.size(),
2586 Init->getSourceLocation(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002587 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002588 Init->getRParenLoc());
Francois Pichet00eb3f92010-12-04 09:14:42 +00002589 } else if (Init->isIndirectMemberInitializer()) {
2590 IndirectFieldDecl *IndirectMember =
2591 cast<IndirectFieldDecl>(FindInstantiatedDecl(
2592 Init->getMemberLocation(),
2593 Init->getIndirectMember(), TemplateArgs));
2594
2595 NewInit = BuildMemberInitializer(IndirectMember, (Expr **)NewArgs.data(),
2596 NewArgs.size(),
2597 Init->getSourceLocation(),
2598 Init->getLParenLoc(),
2599 Init->getRParenLoc());
Anders Carlsson09025312009-08-29 05:16:22 +00002600 }
2601
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002602 if (NewInit.isInvalid()) {
2603 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00002604 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002605 } else {
Anders Carlsson09025312009-08-29 05:16:22 +00002606 // FIXME: It would be nice if ASTOwningVector had a release function.
2607 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00002608
Anders Carlsson09025312009-08-29 05:16:22 +00002609 NewInits.push_back((MemInitTy *)NewInit.get());
2610 }
2611 }
Mike Stump1eb44332009-09-09 15:08:12 +00002612
Anders Carlsson09025312009-08-29 05:16:22 +00002613 // Assign all the initializers to the new constructor.
John McCalld226f652010-08-21 09:40:31 +00002614 ActOnMemInitializers(New,
Anders Carlsson09025312009-08-29 05:16:22 +00002615 /*FIXME: ColonLoc */
2616 SourceLocation(),
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002617 NewInits.data(), NewInits.size(),
2618 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00002619}
2620
John McCall52a575a2009-08-29 08:11:13 +00002621// TODO: this could be templated if the various decl types used the
2622// same method name.
2623static bool isInstantiationOf(ClassTemplateDecl *Pattern,
2624 ClassTemplateDecl *Instance) {
2625 Pattern = Pattern->getCanonicalDecl();
2626
2627 do {
2628 Instance = Instance->getCanonicalDecl();
2629 if (Pattern == Instance) return true;
2630 Instance = Instance->getInstantiatedFromMemberTemplate();
2631 } while (Instance);
2632
2633 return false;
2634}
2635
Douglas Gregor0d696532009-09-28 06:34:35 +00002636static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
2637 FunctionTemplateDecl *Instance) {
2638 Pattern = Pattern->getCanonicalDecl();
2639
2640 do {
2641 Instance = Instance->getCanonicalDecl();
2642 if (Pattern == Instance) return true;
2643 Instance = Instance->getInstantiatedFromMemberTemplate();
2644 } while (Instance);
2645
2646 return false;
2647}
2648
Douglas Gregored9c0f92009-10-29 00:04:11 +00002649static bool
2650isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2651 ClassTemplatePartialSpecializationDecl *Instance) {
2652 Pattern
2653 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2654 do {
2655 Instance = cast<ClassTemplatePartialSpecializationDecl>(
2656 Instance->getCanonicalDecl());
2657 if (Pattern == Instance)
2658 return true;
2659 Instance = Instance->getInstantiatedFromMember();
2660 } while (Instance);
2661
2662 return false;
2663}
2664
John McCall52a575a2009-08-29 08:11:13 +00002665static bool isInstantiationOf(CXXRecordDecl *Pattern,
2666 CXXRecordDecl *Instance) {
2667 Pattern = Pattern->getCanonicalDecl();
2668
2669 do {
2670 Instance = Instance->getCanonicalDecl();
2671 if (Pattern == Instance) return true;
2672 Instance = Instance->getInstantiatedFromMemberClass();
2673 } while (Instance);
2674
2675 return false;
2676}
2677
2678static bool isInstantiationOf(FunctionDecl *Pattern,
2679 FunctionDecl *Instance) {
2680 Pattern = Pattern->getCanonicalDecl();
2681
2682 do {
2683 Instance = Instance->getCanonicalDecl();
2684 if (Pattern == Instance) return true;
2685 Instance = Instance->getInstantiatedFromMemberFunction();
2686 } while (Instance);
2687
2688 return false;
2689}
2690
2691static bool isInstantiationOf(EnumDecl *Pattern,
2692 EnumDecl *Instance) {
2693 Pattern = Pattern->getCanonicalDecl();
2694
2695 do {
2696 Instance = Instance->getCanonicalDecl();
2697 if (Pattern == Instance) return true;
2698 Instance = Instance->getInstantiatedFromMemberEnum();
2699 } while (Instance);
2700
2701 return false;
2702}
2703
John McCalled976492009-12-04 22:46:56 +00002704static bool isInstantiationOf(UsingShadowDecl *Pattern,
2705 UsingShadowDecl *Instance,
2706 ASTContext &C) {
2707 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2708}
2709
2710static bool isInstantiationOf(UsingDecl *Pattern,
2711 UsingDecl *Instance,
2712 ASTContext &C) {
2713 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2714}
2715
John McCall7ba107a2009-11-18 02:36:19 +00002716static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
2717 UsingDecl *Instance,
2718 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002719 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00002720}
2721
2722static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00002723 UsingDecl *Instance,
2724 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002725 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00002726}
2727
John McCall52a575a2009-08-29 08:11:13 +00002728static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
2729 VarDecl *Instance) {
2730 assert(Instance->isStaticDataMember());
2731
2732 Pattern = Pattern->getCanonicalDecl();
2733
2734 do {
2735 Instance = Instance->getCanonicalDecl();
2736 if (Pattern == Instance) return true;
2737 Instance = Instance->getInstantiatedFromStaticDataMember();
2738 } while (Instance);
2739
2740 return false;
2741}
2742
John McCalled976492009-12-04 22:46:56 +00002743// Other is the prospective instantiation
2744// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00002745static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002746 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00002747 if (UnresolvedUsingTypenameDecl *UUD
2748 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
2749 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2750 return isInstantiationOf(UUD, UD, Ctx);
2751 }
2752 }
2753
2754 if (UnresolvedUsingValueDecl *UUD
2755 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002756 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2757 return isInstantiationOf(UUD, UD, Ctx);
2758 }
2759 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002760
Anders Carlsson0d8df782009-08-29 19:37:28 +00002761 return false;
2762 }
Mike Stump1eb44332009-09-09 15:08:12 +00002763
John McCall52a575a2009-08-29 08:11:13 +00002764 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
2765 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002766
John McCall52a575a2009-08-29 08:11:13 +00002767 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
2768 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00002769
John McCall52a575a2009-08-29 08:11:13 +00002770 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
2771 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00002772
Douglas Gregor7caa6822009-07-24 20:34:43 +00002773 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00002774 if (Var->isStaticDataMember())
2775 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
2776
2777 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
2778 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00002779
Douglas Gregor0d696532009-09-28 06:34:35 +00002780 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
2781 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
2782
Douglas Gregored9c0f92009-10-29 00:04:11 +00002783 if (ClassTemplatePartialSpecializationDecl *PartialSpec
2784 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2785 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2786 PartialSpec);
2787
Anders Carlssond8b285f2009-09-01 04:26:58 +00002788 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2789 if (!Field->getDeclName()) {
2790 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00002791 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00002792 cast<FieldDecl>(D);
2793 }
2794 }
Mike Stump1eb44332009-09-09 15:08:12 +00002795
John McCalled976492009-12-04 22:46:56 +00002796 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2797 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2798
2799 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2800 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2801
Douglas Gregor815215d2009-05-27 05:35:12 +00002802 return D->getDeclName() && isa<NamedDecl>(Other) &&
2803 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2804}
2805
2806template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00002807static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00002808 NamedDecl *D,
2809 ForwardIterator first,
2810 ForwardIterator last) {
2811 for (; first != last; ++first)
2812 if (isInstantiationOf(Ctx, D, *first))
2813 return cast<NamedDecl>(*first);
2814
2815 return 0;
2816}
2817
John McCall02cace72009-08-28 07:59:38 +00002818/// \brief Finds the instantiation of the given declaration context
2819/// within the current instantiation.
2820///
2821/// \returns NULL if there was an error
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002822DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
Douglas Gregore95b4092009-09-16 18:34:49 +00002823 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00002824 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002825 Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00002826 return cast_or_null<DeclContext>(ID);
2827 } else return DC;
2828}
2829
Douglas Gregored961e72009-05-27 17:54:46 +00002830/// \brief Find the instantiation of the given declaration within the
2831/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00002832///
2833/// This routine is intended to be used when \p D is a declaration
2834/// referenced from within a template, that needs to mapped into the
2835/// corresponding declaration within an instantiation. For example,
2836/// given:
2837///
2838/// \code
2839/// template<typename T>
2840/// struct X {
2841/// enum Kind {
2842/// KnownValue = sizeof(T)
2843/// };
2844///
2845/// bool getKind() const { return KnownValue; }
2846/// };
2847///
2848/// template struct X<int>;
2849/// \endcode
2850///
2851/// In the instantiation of X<int>::getKind(), we need to map the
2852/// EnumConstantDecl for KnownValue (which refers to
2853/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00002854/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2855/// this mapping from within the instantiation of X<int>.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002856NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
Douglas Gregore95b4092009-09-16 18:34:49 +00002857 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00002858 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00002859 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00002860 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
John McCall76672452010-08-19 23:06:02 +00002861 (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext())) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002862 // D is a local of some kind. Look into the map of local
2863 // declarations to their instantiations.
Fariborz Jahanian8dd0c562010-07-13 00:16:40 +00002864 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002865 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002866
Douglas Gregore95b4092009-09-16 18:34:49 +00002867 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2868 if (!Record->isDependentContext())
2869 return D;
2870
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002871 // If the RecordDecl is actually the injected-class-name or a
2872 // "templated" declaration for a class template, class template
2873 // partial specialization, or a member class of a class template,
2874 // substitute into the injected-class-name of the class template
2875 // or partial specialization to find the new DeclContext.
Douglas Gregore95b4092009-09-16 18:34:49 +00002876 QualType T;
2877 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2878
2879 if (ClassTemplate) {
Douglas Gregor24bae922010-07-08 18:37:38 +00002880 T = ClassTemplate->getInjectedClassNameSpecialization();
Douglas Gregore95b4092009-09-16 18:34:49 +00002881 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2882 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00002883 ClassTemplate = PartialSpec->getSpecializedTemplate();
John McCall3cb0ebd2010-03-10 03:28:59 +00002884
2885 // If we call SubstType with an InjectedClassNameType here we
2886 // can end up in an infinite loop.
2887 T = Context.getTypeDeclType(Record);
2888 assert(isa<InjectedClassNameType>(T) &&
2889 "type of partial specialization is not an InjectedClassNameType");
John McCall31f17ec2010-04-27 00:57:59 +00002890 T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
John McCall3cb0ebd2010-03-10 03:28:59 +00002891 }
Douglas Gregore95b4092009-09-16 18:34:49 +00002892
2893 if (!T.isNull()) {
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002894 // Substitute into the injected-class-name to get the type
2895 // corresponding to the instantiation we want, which may also be
2896 // the current instantiation (if we're in a template
2897 // definition). This substitution should never fail, since we
2898 // know we can instantiate the injected-class-name or we
2899 // wouldn't have gotten to the injected-class-name!
2900
2901 // FIXME: Can we use the CurrentInstantiationScope to avoid this
2902 // extra instantiation in the common case?
Douglas Gregore95b4092009-09-16 18:34:49 +00002903 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2904 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2905
2906 if (!T->isDependentType()) {
2907 assert(T->isRecordType() && "Instantiation must produce a record type");
2908 return T->getAs<RecordType>()->getDecl();
2909 }
2910
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002911 // We are performing "partial" template instantiation to create
2912 // the member declarations for the members of a class template
2913 // specialization. Therefore, D is actually referring to something
2914 // in the current instantiation. Look through the current
2915 // context, which contains actual instantiations, to find the
2916 // instantiation of the "current instantiation" that D refers
2917 // to.
2918 bool SawNonDependentContext = false;
Mike Stump1eb44332009-09-09 15:08:12 +00002919 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00002920 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002921 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002922 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00002923 if (isInstantiationOf(ClassTemplate,
2924 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00002925 return Spec;
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002926
2927 if (!DC->isDependentContext())
2928 SawNonDependentContext = true;
John McCall52a575a2009-08-29 08:11:13 +00002929 }
2930
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002931 // We're performing "instantiation" of a member of the current
2932 // instantiation while we are type-checking the
2933 // definition. Compute the declaration context and return that.
2934 assert(!SawNonDependentContext &&
2935 "No dependent context while instantiating record");
2936 DeclContext *DC = computeDeclContext(T);
2937 assert(DC &&
John McCall52a575a2009-08-29 08:11:13 +00002938 "Unable to find declaration for the current instantiation");
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002939 return cast<CXXRecordDecl>(DC);
John McCall52a575a2009-08-29 08:11:13 +00002940 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002941
Douglas Gregore95b4092009-09-16 18:34:49 +00002942 // Fall through to deal with other dependent record types (e.g.,
2943 // anonymous unions in class templates).
2944 }
John McCall52a575a2009-08-29 08:11:13 +00002945
Douglas Gregore95b4092009-09-16 18:34:49 +00002946 if (!ParentDC->isDependentContext())
2947 return D;
2948
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002949 ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002950 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00002951 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002952
Douglas Gregor815215d2009-05-27 05:35:12 +00002953 if (ParentDC != D->getDeclContext()) {
2954 // We performed some kind of instantiation in the parent context,
2955 // so now we need to look into the instantiated parent context to
2956 // find the instantiation of the declaration D.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002957
John McCall3cb0ebd2010-03-10 03:28:59 +00002958 // If our context used to be dependent, we may need to instantiate
2959 // it before performing lookup into that context.
2960 if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002961 if (!Spec->isDependentContext()) {
2962 QualType T = Context.getTypeDeclType(Spec);
John McCall3cb0ebd2010-03-10 03:28:59 +00002963 const RecordType *Tag = T->getAs<RecordType>();
2964 assert(Tag && "type of non-dependent record is not a RecordType");
2965 if (!Tag->isBeingDefined() &&
2966 RequireCompleteType(Loc, T, diag::err_incomplete_type))
2967 return 0;
Douglas Gregora43064c2010-11-05 23:22:45 +00002968
2969 ParentDC = Tag->getDecl();
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002970 }
2971 }
2972
Douglas Gregor815215d2009-05-27 05:35:12 +00002973 NamedDecl *Result = 0;
2974 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002975 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00002976 Result = findInstantiationOf(Context, D, Found.first, Found.second);
2977 } else {
2978 // Since we don't have a name for the entity we're looking for,
2979 // our only option is to walk through all of the declarations to
2980 // find that name. This will occur in a few cases:
2981 //
2982 // - anonymous struct/union within a template
2983 // - unnamed class/struct/union/enum within a template
2984 //
2985 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00002986 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002987 ParentDC->decls_begin(),
2988 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00002989 }
Mike Stump1eb44332009-09-09 15:08:12 +00002990
John McCall9f54ad42009-12-10 09:41:52 +00002991 // UsingShadowDecls can instantiate to nothing because of using hiding.
Douglas Gregor00225542010-03-01 18:27:54 +00002992 assert((Result || isa<UsingShadowDecl>(D) || D->isInvalidDecl() ||
2993 cast<Decl>(ParentDC)->isInvalidDecl())
John McCall9f54ad42009-12-10 09:41:52 +00002994 && "Unable to find instantiation of declaration!");
2995
Douglas Gregor815215d2009-05-27 05:35:12 +00002996 D = Result;
2997 }
2998
Douglas Gregor815215d2009-05-27 05:35:12 +00002999 return D;
3000}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003001
Mike Stump1eb44332009-09-09 15:08:12 +00003002/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003003/// instantiations we have seen until this point.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003004void Sema::PerformPendingInstantiations(bool LocalOnly) {
Douglas Gregor60406be2010-01-16 22:29:39 +00003005 while (!PendingLocalImplicitInstantiations.empty() ||
Chandler Carruth62c78d52010-08-25 08:44:16 +00003006 (!LocalOnly && !PendingInstantiations.empty())) {
Douglas Gregor60406be2010-01-16 22:29:39 +00003007 PendingImplicitInstantiation Inst;
3008
3009 if (PendingLocalImplicitInstantiations.empty()) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00003010 Inst = PendingInstantiations.front();
3011 PendingInstantiations.pop_front();
Douglas Gregor60406be2010-01-16 22:29:39 +00003012 } else {
3013 Inst = PendingLocalImplicitInstantiations.front();
3014 PendingLocalImplicitInstantiations.pop_front();
3015 }
Mike Stump1eb44332009-09-09 15:08:12 +00003016
Douglas Gregor7caa6822009-07-24 20:34:43 +00003017 // Instantiate function definitions
3018 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
John McCallf312b1e2010-08-26 23:41:50 +00003019 PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(),
3020 "instantiating function definition");
Chandler Carruth58e390e2010-08-25 08:27:02 +00003021 bool DefinitionRequired = Function->getTemplateSpecializationKind() ==
3022 TSK_ExplicitInstantiationDefinition;
3023 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true,
3024 DefinitionRequired);
Douglas Gregor7caa6822009-07-24 20:34:43 +00003025 continue;
3026 }
Mike Stump1eb44332009-09-09 15:08:12 +00003027
Douglas Gregor7caa6822009-07-24 20:34:43 +00003028 // Instantiate static data member definitions.
3029 VarDecl *Var = cast<VarDecl>(Inst.first);
3030 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00003031
Chandler Carruth291b4412010-02-13 10:17:50 +00003032 // Don't try to instantiate declarations if the most recent redeclaration
3033 // is invalid.
3034 if (Var->getMostRecentDeclaration()->isInvalidDecl())
3035 continue;
3036
3037 // Check if the most recent declaration has changed the specialization kind
3038 // and removed the need for implicit instantiation.
3039 switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
3040 case TSK_Undeclared:
3041 assert(false && "Cannot instantitiate an undeclared specialization.");
3042 case TSK_ExplicitInstantiationDeclaration:
Chandler Carruth291b4412010-02-13 10:17:50 +00003043 case TSK_ExplicitSpecialization:
Chandler Carruth58e390e2010-08-25 08:27:02 +00003044 continue; // No longer need to instantiate this type.
3045 case TSK_ExplicitInstantiationDefinition:
3046 // We only need an instantiation if the pending instantiation *is* the
3047 // explicit instantiation.
3048 if (Var != Var->getMostRecentDeclaration()) continue;
Chandler Carruth291b4412010-02-13 10:17:50 +00003049 case TSK_ImplicitInstantiation:
3050 break;
3051 }
3052
John McCallf312b1e2010-08-26 23:41:50 +00003053 PrettyDeclStackTraceEntry CrashInfo(*this, Var, Var->getLocation(),
3054 "instantiating static data member "
3055 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00003056
Chandler Carruth58e390e2010-08-25 08:27:02 +00003057 bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
3058 TSK_ExplicitInstantiationDefinition;
3059 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true,
3060 DefinitionRequired);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003061 }
3062}
John McCall0c01d182010-03-24 05:22:00 +00003063
3064void Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
3065 const MultiLevelTemplateArgumentList &TemplateArgs) {
3066 for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
3067 E = Pattern->ddiag_end(); I != E; ++I) {
3068 DependentDiagnostic *DD = *I;
3069
3070 switch (DD->getKind()) {
3071 case DependentDiagnostic::Access:
3072 HandleDependentAccessCheck(*DD, TemplateArgs);
3073 break;
3074 }
3075 }
3076}