blob: 0b0efcb8332750857141fcc22b54d9d8aa4dd18f [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//===----------------------------------------------------------------------===/
12#include "Sema.h"
John McCall7d384dd2009-11-18 07:57:50 +000013#include "Lookup.h"
Douglas Gregoraba43bb2009-05-26 20:50:29 +000014#include "clang/AST/ASTConsumer.h"
Douglas Gregor8dbc2692009-03-17 21:15:40 +000015#include "clang/AST/ASTContext.h"
16#include "clang/AST/DeclTemplate.h"
17#include "clang/AST/DeclVisitor.h"
18#include "clang/AST/Expr.h"
Douglas Gregora88cfbf2009-12-12 18:16:41 +000019#include "clang/AST/ExprCXX.h"
Anders Carlssonc17fb7b2009-09-01 05:12:24 +000020#include "clang/Basic/PrettyStackTrace.h"
Douglas Gregor83ddad32009-08-26 21:14:46 +000021#include "clang/Lex/Preprocessor.h"
Douglas Gregor8dbc2692009-03-17 21:15:40 +000022
23using namespace clang;
24
25namespace {
Benjamin Kramer85b45212009-11-28 19:45:26 +000026 class TemplateDeclInstantiator
Chris Lattnerb28317a2009-03-28 19:18:32 +000027 : public DeclVisitor<TemplateDeclInstantiator, Decl *> {
Douglas Gregor8dbc2692009-03-17 21:15:40 +000028 Sema &SemaRef;
29 DeclContext *Owner;
Douglas Gregord6350ae2009-08-28 20:31:08 +000030 const MultiLevelTemplateArgumentList &TemplateArgs;
Mike Stump1eb44332009-09-09 15:08:12 +000031
Anders Carlssond8fe2d52009-11-07 06:07:58 +000032 void InstantiateAttrs(Decl *Tmpl, Decl *New);
33
Douglas Gregor8dbc2692009-03-17 21:15:40 +000034 public:
35 typedef Sema::OwningExprResult OwningExprResult;
36
37 TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +000038 const MultiLevelTemplateArgumentList &TemplateArgs)
Douglas Gregor7e063902009-05-11 23:53:27 +000039 : SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs) { }
Mike Stump1eb44332009-09-09 15:08:12 +000040
Mike Stump390b4cc2009-05-16 07:39:55 +000041 // FIXME: Once we get closer to completion, replace these manually-written
42 // declarations with automatically-generated ones from
43 // clang/AST/DeclNodes.def.
Douglas Gregor4f722be2009-03-25 15:45:12 +000044 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
45 Decl *VisitNamespaceDecl(NamespaceDecl *D);
John McCall3dbd3d52010-02-16 06:53:13 +000046 Decl *VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000047 Decl *VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +000048 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000049 Decl *VisitFieldDecl(FieldDecl *D);
50 Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
51 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor6477b692009-03-25 15:04:13 +000052 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
John McCall02cace72009-08-28 07:59:38 +000053 Decl *VisitFriendDecl(FriendDecl *D);
Douglas Gregora735b202009-10-13 14:39:41 +000054 Decl *VisitFunctionDecl(FunctionDecl *D,
55 TemplateParameterList *TemplateParams = 0);
Douglas Gregord475b8d2009-03-25 21:17:03 +000056 Decl *VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregord60e1052009-08-27 16:57:43 +000057 Decl *VisitCXXMethodDecl(CXXMethodDecl *D,
58 TemplateParameterList *TemplateParams = 0);
Douglas Gregor615c5d42009-03-24 16:43:20 +000059 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
Douglas Gregor03b2b072009-03-24 00:15:49 +000060 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +000061 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor6477b692009-03-25 15:04:13 +000062 ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D);
John McCalle29ba202009-08-20 01:44:21 +000063 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregor7974c3b2009-10-07 17:21:34 +000064 Decl *VisitClassTemplatePartialSpecializationDecl(
65 ClassTemplatePartialSpecializationDecl *D);
Douglas Gregord60e1052009-08-27 16:57:43 +000066 Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
John McCalle29ba202009-08-20 01:44:21 +000067 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Douglas Gregor33642df2009-10-23 23:25:44 +000068 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
Douglas Gregor9106ef72009-11-11 16:58:32 +000069 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
Douglas Gregor48c32a72009-11-17 06:07:40 +000070 Decl *VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
John McCalled976492009-12-04 22:46:56 +000071 Decl *VisitUsingDecl(UsingDecl *D);
72 Decl *VisitUsingShadowDecl(UsingShadowDecl *D);
John McCall7ba107a2009-11-18 02:36:19 +000073 Decl *VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
74 Decl *VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Mike Stump1eb44332009-09-09 15:08:12 +000075
Douglas Gregor8dbc2692009-03-17 21:15:40 +000076 // Base case. FIXME: Remove once we can instantiate everything.
Douglas Gregor48c32a72009-11-17 06:07:40 +000077 Decl *VisitDecl(Decl *D) {
78 unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID(
79 Diagnostic::Error,
80 "cannot instantiate %0 yet");
81 SemaRef.Diag(D->getLocation(), DiagID)
82 << D->getDeclKindName();
83
Douglas Gregor8dbc2692009-03-17 21:15:40 +000084 return 0;
85 }
Douglas Gregor5545e162009-03-24 00:38:23 +000086
John McCallfd810b12009-08-14 02:03:10 +000087 const LangOptions &getLangOptions() {
88 return SemaRef.getLangOptions();
89 }
90
Douglas Gregor5545e162009-03-24 00:38:23 +000091 // Helper functions for instantiating methods.
John McCallce3ff2b2009-08-25 22:02:44 +000092 QualType SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +000093 llvm::SmallVectorImpl<ParmVarDecl *> &Params);
Douglas Gregore53060f2009-06-25 22:08:12 +000094 bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
Douglas Gregor5545e162009-03-24 00:38:23 +000095 bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
John McCalle29ba202009-08-20 01:44:21 +000096
97 TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +000098 SubstTemplateParams(TemplateParameterList *List);
Douglas Gregored9c0f92009-10-29 00:04:11 +000099
100 bool InstantiateClassTemplatePartialSpecialization(
101 ClassTemplateDecl *ClassTemplate,
102 ClassTemplatePartialSpecializationDecl *PartialSpec);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000103 };
104}
105
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000106// FIXME: Is this too simple?
107void TemplateDeclInstantiator::InstantiateAttrs(Decl *Tmpl, Decl *New) {
108 for (const Attr *TmplAttr = Tmpl->getAttrs(); TmplAttr;
109 TmplAttr = TmplAttr->getNext()) {
110
111 // FIXME: Is cloning correct for all attributes?
112 Attr *NewAttr = TmplAttr->clone(SemaRef.Context);
113
114 New->addAttr(NewAttr);
115 }
116}
117
Douglas Gregor4f722be2009-03-25 15:45:12 +0000118Decl *
119TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
120 assert(false && "Translation units cannot be instantiated");
121 return D;
122}
123
124Decl *
125TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
126 assert(false && "Namespaces cannot be instantiated");
127 return D;
128}
129
John McCall3dbd3d52010-02-16 06:53:13 +0000130Decl *
131TemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
132 NamespaceAliasDecl *Inst
133 = NamespaceAliasDecl::Create(SemaRef.Context, Owner,
134 D->getNamespaceLoc(),
135 D->getAliasLoc(),
136 D->getNamespace()->getIdentifier(),
137 D->getQualifierRange(),
138 D->getQualifier(),
139 D->getTargetNameLoc(),
140 D->getNamespace());
141 Owner->addDecl(Inst);
142 return Inst;
143}
144
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000145Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
146 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000147 TypeSourceInfo *DI = D->getTypeSourceInfo();
John McCallba6a9bd2009-10-24 08:00:42 +0000148 if (DI->getType()->isDependentType()) {
149 DI = SemaRef.SubstType(DI, TemplateArgs,
150 D->getLocation(), D->getDeclName());
151 if (!DI) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000152 Invalid = true;
John McCalla93c9342009-12-07 02:54:59 +0000153 DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000154 }
155 }
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000157 // Create the new typedef
158 TypedefDecl *Typedef
159 = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
John McCallba6a9bd2009-10-24 08:00:42 +0000160 D->getIdentifier(), DI);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000161 if (Invalid)
162 Typedef->setInvalidDecl();
163
John McCall5126fd02009-12-30 00:31:22 +0000164 if (TypedefDecl *Prev = D->getPreviousDeclaration()) {
165 NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(Prev, TemplateArgs);
166 Typedef->setPreviousDeclaration(cast<TypedefDecl>(InstPrev));
167 }
168
John McCall46460a62010-01-20 21:53:11 +0000169 Typedef->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000170 Owner->addDecl(Typedef);
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000172 return Typedef;
173}
174
Douglas Gregor6eef5192009-12-14 19:27:10 +0000175/// \brief Instantiate the arguments provided as part of initialization.
176///
177/// \returns true if an error occurred, false otherwise.
178static bool InstantiateInitializationArguments(Sema &SemaRef,
179 Expr **Args, unsigned NumArgs,
180 const MultiLevelTemplateArgumentList &TemplateArgs,
181 llvm::SmallVectorImpl<SourceLocation> &FakeCommaLocs,
182 ASTOwningVector<&ActionBase::DeleteExpr> &InitArgs) {
183 for (unsigned I = 0; I != NumArgs; ++I) {
184 // When we hit the first defaulted argument, break out of the loop:
185 // we don't pass those default arguments on.
186 if (Args[I]->isDefaultArgument())
187 break;
188
189 Sema::OwningExprResult Arg = SemaRef.SubstExpr(Args[I], TemplateArgs);
190 if (Arg.isInvalid())
191 return true;
192
193 Expr *ArgExpr = (Expr *)Arg.get();
194 InitArgs.push_back(Arg.release());
195
196 // FIXME: We're faking all of the comma locations. Do we need them?
197 FakeCommaLocs.push_back(
198 SemaRef.PP.getLocForEndOfToken(ArgExpr->getLocEnd()));
199 }
200
201 return false;
202}
203
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000204Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000205 // Do substitution on the type of the declaration
John McCalla93c9342009-12-07 02:54:59 +0000206 TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
John McCall0a5fa062009-10-21 02:39:02 +0000207 TemplateArgs,
208 D->getTypeSpecStartLoc(),
209 D->getDeclName());
210 if (!DI)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000211 return 0;
212
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000213 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000214 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
215 D->getLocation(), D->getIdentifier(),
John McCall0a5fa062009-10-21 02:39:02 +0000216 DI->getType(), DI,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000217 D->getStorageClass());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000218 Var->setThreadSpecified(D->isThreadSpecified());
219 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
220 Var->setDeclaredInCondition(D->isDeclaredInCondition());
Mike Stump1eb44332009-09-09 15:08:12 +0000221
222 // If we are instantiating a static data member defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000223 // out-of-line, the instantiation will have the same lexical
224 // context (which will be a namespace scope) as the template.
225 if (D->isOutOfLine())
226 Var->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000227
John McCall46460a62010-01-20 21:53:11 +0000228 Var->setAccess(D->getAccess());
229
Mike Stump390b4cc2009-05-16 07:39:55 +0000230 // FIXME: In theory, we could have a previous declaration for variables that
231 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000232 bool Redeclaration = false;
John McCall68263142009-11-18 22:49:29 +0000233 // FIXME: having to fake up a LookupResult is dumb.
234 LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
235 Sema::LookupOrdinaryName);
Douglas Gregor60c93c92010-02-09 07:26:29 +0000236 if (D->isStaticDataMember())
237 SemaRef.LookupQualifiedName(Previous, Owner, false);
John McCall68263142009-11-18 22:49:29 +0000238 SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000239
Douglas Gregor7caa6822009-07-24 20:34:43 +0000240 if (D->isOutOfLine()) {
241 D->getLexicalDeclContext()->addDecl(Var);
242 Owner->makeDeclVisibleInContext(Var);
243 } else {
244 Owner->addDecl(Var);
245 }
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000247 // Link instantiations of static data members back to the template from
248 // which they were instantiated.
249 if (Var->isStaticDataMember())
250 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000251 TSK_ImplicitInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000252
Douglas Gregor60c93c92010-02-09 07:26:29 +0000253 if (Var->getAnyInitializer()) {
254 // We already have an initializer in the class.
255 } else if (D->getInit()) {
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000256 if (Var->isStaticDataMember() && !D->isOutOfLine())
257 SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
258 else
259 SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
260
Douglas Gregor6eef5192009-12-14 19:27:10 +0000261 // Extract the initializer, skipping through any temporary-binding
262 // expressions and look at the subexpression as it was written.
263 Expr *DInit = D->getInit();
Douglas Gregora89ebea2010-02-03 09:01:59 +0000264 if (CXXExprWithTemporaries *ExprTemp
265 = dyn_cast<CXXExprWithTemporaries>(DInit))
266 DInit = ExprTemp->getSubExpr();
Douglas Gregor6eef5192009-12-14 19:27:10 +0000267 while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(DInit))
268 DInit = Binder->getSubExpr();
269 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(DInit))
270 DInit = ICE->getSubExprAsWritten();
271
272 if (ParenListExpr *PLE = dyn_cast<ParenListExpr>(DInit)) {
273 // The initializer is a parenthesized list of expressions that is
274 // type-dependent. Instantiate each of the expressions; we'll be
275 // performing direct initialization with them.
276 llvm::SmallVector<SourceLocation, 4> CommaLocs;
277 ASTOwningVector<&ActionBase::DeleteExpr> InitArgs(SemaRef);
278 if (!InstantiateInitializationArguments(SemaRef,
279 PLE->getExprs(),
280 PLE->getNumExprs(),
281 TemplateArgs,
282 CommaLocs, InitArgs)) {
283 // Add the direct initializer to the declaration.
Douglas Gregora88cfbf2009-12-12 18:16:41 +0000284 SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
Douglas Gregor6eef5192009-12-14 19:27:10 +0000285 PLE->getLParenLoc(),
286 move_arg(InitArgs),
287 CommaLocs.data(),
288 PLE->getRParenLoc());
289 }
290 } else if (CXXConstructExpr *Construct =dyn_cast<CXXConstructExpr>(DInit)) {
291 // The initializer resolved to a constructor. Instantiate the constructor
292 // arguments.
293 llvm::SmallVector<SourceLocation, 4> CommaLocs;
294 ASTOwningVector<&ActionBase::DeleteExpr> InitArgs(SemaRef);
295
296 if (!InstantiateInitializationArguments(SemaRef,
297 Construct->getArgs(),
298 Construct->getNumArgs(),
299 TemplateArgs,
300 CommaLocs, InitArgs)) {
301 if (D->hasCXXDirectInitializer()) {
302 SourceLocation FakeLParenLoc =
303 SemaRef.PP.getLocForEndOfToken(D->getLocation());
304 SourceLocation FakeRParenLoc = CommaLocs.empty()? FakeLParenLoc
305 : CommaLocs.back();
306 SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
307 FakeLParenLoc,
308 move_arg(InitArgs),
309 CommaLocs.data(),
310 FakeRParenLoc);
311 } else if (InitArgs.size() == 1) {
312 Expr *Init = (Expr*)(InitArgs.take()[0]);
313 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var),
314 SemaRef.Owned(Init),
315 false);
316 } else {
317 assert(InitArgs.size() == 0);
318 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
319 }
320 }
321 } else {
322 OwningExprResult Init
323 = SemaRef.SubstExpr(D->getInit(), TemplateArgs);
324
325 // FIXME: Not happy about invalidating decls just because of a bad
326 // initializer, unless it affects the type.
327 if (Init.isInvalid())
328 Var->setInvalidDecl();
329 else
330 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init),
331 D->hasCXXDirectInitializer());
332 }
333
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000334 SemaRef.PopExpressionEvaluationContext();
Douglas Gregor65b90052009-07-27 17:43:39 +0000335 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
336 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000337
338 return Var;
339}
340
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000341Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
342 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000343 TypeSourceInfo *DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000344 if (DI->getType()->isDependentType()) {
345 DI = SemaRef.SubstType(DI, TemplateArgs,
346 D->getLocation(), D->getDeclName());
347 if (!DI) {
John McCalla93c9342009-12-07 02:54:59 +0000348 DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000349 Invalid = true;
350 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000351 // C++ [temp.arg.type]p3:
352 // If a declaration acquires a function type through a type
353 // dependent on a template-parameter and this causes a
354 // declaration that does not use the syntactic form of a
355 // function declarator to have function type, the program is
356 // ill-formed.
357 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000358 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000359 Invalid = true;
360 }
361 }
362
363 Expr *BitWidth = D->getBitWidth();
364 if (Invalid)
365 BitWidth = 0;
366 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000367 // The bit-width expression is not potentially evaluated.
368 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000370 OwningExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000371 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000372 if (InstantiatedBitWidth.isInvalid()) {
373 Invalid = true;
374 BitWidth = 0;
375 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000376 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000377 }
378
John McCall07fb6be2009-10-22 23:33:21 +0000379 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
380 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000381 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000382 D->getLocation(),
383 D->isMutable(),
384 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000385 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000386 D->getAccess(),
387 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000388 if (!Field) {
389 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000390 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000391 }
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000393 InstantiateAttrs(D, Field);
394
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000395 if (Invalid)
396 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000398 if (!Field->getDeclName()) {
399 // Keep track of where this decl came from.
400 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000401 }
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000403 Field->setImplicit(D->isImplicit());
John McCall46460a62010-01-20 21:53:11 +0000404 Field->setAccess(D->getAccess());
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000405 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000406
407 return Field;
408}
409
John McCall02cace72009-08-28 07:59:38 +0000410Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
411 FriendDecl::FriendUnion FU;
412
413 // Handle friend type expressions by simply substituting template
414 // parameters into the pattern type.
415 if (Type *Ty = D->getFriendType()) {
416 QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs,
417 D->getLocation(), DeclarationName());
418 if (T.isNull()) return 0;
419
420 assert(getLangOptions().CPlusPlus0x || T->isRecordType());
421 FU = T.getTypePtr();
422
423 // Handle everything else by appropriate substitution.
424 } else {
425 NamedDecl *ND = D->getFriendDecl();
426 assert(ND && "friend decl must be a decl or a type!");
427
Douglas Gregora735b202009-10-13 14:39:41 +0000428 // FIXME: We have a problem here, because the nested call to Visit(ND)
429 // will inject the thing that the friend references into the current
430 // owner, which is wrong.
John McCalle129d442009-12-17 23:21:11 +0000431 Decl *NewND;
432
433 // Hack to make this work almost well pending a rewrite.
Douglas Gregor63644fa2010-02-07 10:31:35 +0000434 if (ND->getDeclContext()->isRecord()) {
435 if (!ND->getDeclContext()->isDependentContext()) {
436 NewND = SemaRef.FindInstantiatedDecl(ND, TemplateArgs);
437 } else {
438 // FIXME: Hack to avoid crashing when incorrectly trying to instantiate
439 // templated friend declarations. This doesn't produce a correct AST;
440 // however this is sufficient for some AST analysis. The real solution
441 // must be put in place during the pending rewrite. See PR5848.
442 return 0;
443 }
444 } else if (D->wasSpecialization()) {
Douglas Gregor7557a132009-12-24 20:56:24 +0000445 // Totally egregious hack to work around PR5866
446 return 0;
447 } else
John McCalle129d442009-12-17 23:21:11 +0000448 NewND = Visit(ND);
John McCall02cace72009-08-28 07:59:38 +0000449 if (!NewND) return 0;
450
451 FU = cast<NamedDecl>(NewND);
John McCallfd810b12009-08-14 02:03:10 +0000452 }
Mike Stump1eb44332009-09-09 15:08:12 +0000453
John McCall02cace72009-08-28 07:59:38 +0000454 FriendDecl *FD =
455 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU,
456 D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000457 FD->setAccess(AS_public);
John McCall02cace72009-08-28 07:59:38 +0000458 Owner->addDecl(FD);
459 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000460}
461
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000462Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
463 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Douglas Gregorac7610d2009-06-22 20:57:11 +0000465 // The expression in a static assertion is not potentially evaluated.
466 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000468 OwningExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000469 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000470 if (InstantiatedAssertExpr.isInvalid())
471 return 0;
472
Douglas Gregor43d9d922009-08-08 01:41:12 +0000473 OwningExprResult Message(SemaRef, D->getMessage());
474 D->getMessage()->Retain();
Mike Stump1eb44332009-09-09 15:08:12 +0000475 Decl *StaticAssert
476 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
Chris Lattnerb28317a2009-03-28 19:18:32 +0000477 move(InstantiatedAssertExpr),
478 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000479 return StaticAssert;
480}
481
482Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000483 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000484 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000485 D->getTagKeywordLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000486 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000487 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000488 Enum->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000489 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000490 Enum->startDefinition();
491
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000492 llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000493
494 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000495 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
496 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000497 EC != ECEnd; ++EC) {
498 // The specified value for the enumerator.
499 OwningExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000500 if (Expr *UninstValue = EC->getInitExpr()) {
501 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000502 EnterExpressionEvaluationContext Unevaluated(SemaRef,
Douglas Gregorac7610d2009-06-22 20:57:11 +0000503 Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000504
John McCallce3ff2b2009-08-25 22:02:44 +0000505 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000506 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000507
508 // Drop the initial value and continue.
509 bool isInvalid = false;
510 if (Value.isInvalid()) {
511 Value = SemaRef.Owned((Expr *)0);
512 isInvalid = true;
513 }
514
Mike Stump1eb44332009-09-09 15:08:12 +0000515 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000516 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
517 EC->getLocation(), EC->getIdentifier(),
518 move(Value));
519
520 if (isInvalid) {
521 if (EnumConst)
522 EnumConst->setInvalidDecl();
523 Enum->setInvalidDecl();
524 }
525
526 if (EnumConst) {
John McCall3b85ecf2010-01-23 22:37:59 +0000527 EnumConst->setAccess(Enum->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000528 Enum->addDecl(EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000529 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000530 LastEnumConst = EnumConst;
531 }
532 }
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000534 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000535 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000536 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
537 Sema::DeclPtrTy::make(Enum),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000538 &Enumerators[0], Enumerators.size(),
539 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000540
541 return Enum;
542}
543
Douglas Gregor6477b692009-03-25 15:04:13 +0000544Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
545 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
546 return 0;
547}
548
Douglas Gregored9c0f92009-10-29 00:04:11 +0000549namespace {
550 class SortDeclByLocation {
551 SourceManager &SourceMgr;
552
553 public:
554 explicit SortDeclByLocation(SourceManager &SourceMgr)
555 : SourceMgr(SourceMgr) { }
556
557 bool operator()(const Decl *X, const Decl *Y) const {
558 return SourceMgr.isBeforeInTranslationUnit(X->getLocation(),
559 Y->getLocation());
560 }
561 };
562}
563
John McCalle29ba202009-08-20 01:44:21 +0000564Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000565 // Create a local instantiation scope for this class template, which
566 // will contain the instantiations of the template parameters.
567 Sema::LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000568 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000569 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000570 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000571 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000572
573 CXXRecordDecl *Pattern = D->getTemplatedDecl();
574 CXXRecordDecl *RecordInst
575 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner,
576 Pattern->getLocation(), Pattern->getIdentifier(),
Douglas Gregorf0510d42009-10-12 23:11:44 +0000577 Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL,
578 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000579
580 ClassTemplateDecl *Inst
581 = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
582 D->getIdentifier(), InstParams, RecordInst, 0);
583 RecordInst->setDescribedClassTemplate(Inst);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000584 if (D->getFriendObjectKind())
585 Inst->setObjectOfFriendDecl(true);
586 else
587 Inst->setAccess(D->getAccess());
John McCalle29ba202009-08-20 01:44:21 +0000588 Inst->setInstantiatedFromMemberTemplate(D);
Douglas Gregorf0510d42009-10-12 23:11:44 +0000589
590 // Trigger creation of the type for the instantiation.
591 SemaRef.Context.getTypeDeclType(RecordInst);
592
Douglas Gregor259571e2009-10-30 22:42:42 +0000593 // Finish handling of friends.
594 if (Inst->getFriendObjectKind()) {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000595 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000596 }
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000597
John McCall46460a62010-01-20 21:53:11 +0000598 Inst->setAccess(D->getAccess());
John McCalle29ba202009-08-20 01:44:21 +0000599 Owner->addDecl(Inst);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000600
601 // First, we sort the partial specializations by location, so
602 // that we instantiate them in the order they were declared.
603 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
604 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
605 P = D->getPartialSpecializations().begin(),
606 PEnd = D->getPartialSpecializations().end();
607 P != PEnd; ++P)
608 PartialSpecs.push_back(&*P);
609 std::sort(PartialSpecs.begin(), PartialSpecs.end(),
610 SortDeclByLocation(SemaRef.SourceMgr));
611
612 // Instantiate all of the partial specializations of this member class
613 // template.
614 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
615 InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
616
John McCalle29ba202009-08-20 01:44:21 +0000617 return Inst;
618}
619
Douglas Gregord60e1052009-08-27 16:57:43 +0000620Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000621TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
622 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000623 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
624
625 // Lookup the already-instantiated declaration in the instantiation
626 // of the class template and return that.
627 DeclContext::lookup_result Found
628 = Owner->lookup(ClassTemplate->getDeclName());
629 if (Found.first == Found.second)
630 return 0;
631
632 ClassTemplateDecl *InstClassTemplate
633 = dyn_cast<ClassTemplateDecl>(*Found.first);
634 if (!InstClassTemplate)
635 return 0;
636
637 Decl *DCanon = D->getCanonicalDecl();
638 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
639 P = InstClassTemplate->getPartialSpecializations().begin(),
640 PEnd = InstClassTemplate->getPartialSpecializations().end();
641 P != PEnd; ++P) {
642 if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
643 return &*P;
644 }
645
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000646 return 0;
647}
648
649Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000650TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000651 // Create a local instantiation scope for this function template, which
652 // will contain the instantiations of the template parameters and then get
653 // merged with the local instantiation scope for the function template
654 // itself.
655 Sema::LocalInstantiationScope Scope(SemaRef);
656
Douglas Gregord60e1052009-08-27 16:57:43 +0000657 TemplateParameterList *TempParams = D->getTemplateParameters();
658 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000659 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000660 return NULL;
Douglas Gregored9c0f92009-10-29 00:04:11 +0000661
Douglas Gregora735b202009-10-13 14:39:41 +0000662 FunctionDecl *Instantiated = 0;
663 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
664 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
665 InstParams));
666 else
667 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
668 D->getTemplatedDecl(),
669 InstParams));
670
671 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000672 return 0;
673
John McCall46460a62010-01-20 21:53:11 +0000674 Instantiated->setAccess(D->getAccess());
675
Mike Stump1eb44332009-09-09 15:08:12 +0000676 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000677 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000678 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000679 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000680 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000681 assert(InstTemplate &&
682 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
John McCalle976ffe2009-12-14 23:19:40 +0000683
684 // Link the instantiation back to the pattern *unless* this is a
685 // non-definition friend declaration.
686 if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
687 !(InstTemplate->getFriendObjectKind() &&
688 !D->getTemplatedDecl()->isThisDeclarationADefinition()))
Douglas Gregora735b202009-10-13 14:39:41 +0000689 InstTemplate->setInstantiatedFromMemberTemplate(D);
690
691 // Add non-friends into the owner.
692 if (!InstTemplate->getFriendObjectKind())
693 Owner->addDecl(InstTemplate);
Douglas Gregord60e1052009-08-27 16:57:43 +0000694 return InstTemplate;
695}
696
Douglas Gregord475b8d2009-03-25 21:17:03 +0000697Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
698 CXXRecordDecl *PrevDecl = 0;
699 if (D->isInjectedClassName())
700 PrevDecl = cast<CXXRecordDecl>(Owner);
John McCall6c1c1b82009-12-15 22:29:06 +0000701 else if (D->getPreviousDeclaration()) {
702 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getPreviousDeclaration(),
703 TemplateArgs);
704 if (!Prev) return 0;
705 PrevDecl = cast<CXXRecordDecl>(Prev);
706 }
Douglas Gregord475b8d2009-03-25 21:17:03 +0000707
708 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000709 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000710 D->getLocation(), D->getIdentifier(),
711 D->getTagKeywordLoc(), PrevDecl);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000712 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000713 // FIXME: Check against AS_none is an ugly hack to work around the issue that
714 // the tag decls introduced by friend class declarations don't have an access
715 // specifier. Remove once this area of the code gets sorted out.
716 if (D->getAccess() != AS_none)
717 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000718 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000719 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000720
John McCall02cace72009-08-28 07:59:38 +0000721 // If the original function was part of a friend declaration,
722 // inherit its namespace state.
723 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
724 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
725
Anders Carlssond8b285f2009-09-01 04:26:58 +0000726 Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
727
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000728 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000729 return Record;
730}
731
John McCall02cace72009-08-28 07:59:38 +0000732/// Normal class members are of more specific types and therefore
733/// don't make it here. This function serves two purposes:
734/// 1) instantiating function templates
735/// 2) substituting friend declarations
736/// FIXME: preserve function definitions in case #2
Douglas Gregor7557a132009-12-24 20:56:24 +0000737Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
Douglas Gregora735b202009-10-13 14:39:41 +0000738 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000739 // Check whether there is already a function template specialization for
740 // this declaration.
741 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
742 void *InsertPos = 0;
Douglas Gregora735b202009-10-13 14:39:41 +0000743 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000744 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000745 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000746 TemplateArgs.getInnermost().getFlatArgumentList(),
747 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor828e2262009-07-29 16:09:57 +0000748 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000749
750 FunctionTemplateSpecializationInfo *Info
751 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor127102b2009-06-29 20:59:39 +0000752 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000753
Douglas Gregor127102b2009-06-29 20:59:39 +0000754 // If we already have a function template specialization, return it.
755 if (Info)
756 return Info->Function;
757 }
Mike Stump1eb44332009-09-09 15:08:12 +0000758
Douglas Gregor79c22782010-01-16 20:21:20 +0000759 bool MergeWithParentScope = (TemplateParams != 0) ||
760 !(isa<Decl>(Owner) &&
761 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
762 Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000763
Douglas Gregore53060f2009-06-25 22:08:12 +0000764 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000765 QualType T = SubstFunctionType(D, Params);
Douglas Gregore53060f2009-06-25 22:08:12 +0000766 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000767 return 0;
John McCallfd810b12009-08-14 02:03:10 +0000768
John McCall68b6b872010-02-06 01:50:47 +0000769 // If we're instantiating a local function declaration, put the result
770 // in the owner; otherwise we need to find the instantiated context.
771 DeclContext *DC;
772 if (D->getDeclContext()->isFunctionOrMethod())
773 DC = Owner;
774 else
775 DC = SemaRef.FindInstantiatedContext(D->getDeclContext(), TemplateArgs);
776
John McCall02cace72009-08-28 07:59:38 +0000777 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +0000778 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
John McCalla93c9342009-12-07 02:54:59 +0000779 D->getDeclName(), T, D->getTypeSourceInfo(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000780 D->getStorageClass(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000781 D->isInlineSpecified(), D->hasWrittenPrototype());
John McCall02cace72009-08-28 07:59:38 +0000782 Function->setLexicalDeclContext(Owner);
Mike Stump1eb44332009-09-09 15:08:12 +0000783
Douglas Gregore53060f2009-06-25 22:08:12 +0000784 // Attach the parameters
785 for (unsigned P = 0; P < Params.size(); ++P)
786 Params[P]->setOwningFunction(Function);
Douglas Gregor838db382010-02-11 01:19:42 +0000787 Function->setParams(Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +0000788
Douglas Gregora735b202009-10-13 14:39:41 +0000789 if (TemplateParams) {
790 // Our resulting instantiation is actually a function template, since we
791 // are substituting only the outer template parameters. For example, given
792 //
793 // template<typename T>
794 // struct X {
795 // template<typename U> friend void f(T, U);
796 // };
797 //
798 // X<int> x;
799 //
800 // We are instantiating the friend function template "f" within X<int>,
801 // which means substituting int for T, but leaving "f" as a friend function
802 // template.
803 // Build the function template itself.
804 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Owner,
805 Function->getLocation(),
806 Function->getDeclName(),
807 TemplateParams, Function);
808 Function->setDescribedFunctionTemplate(FunctionTemplate);
809 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregor66724ea2009-11-14 01:20:54 +0000810 } else if (FunctionTemplate) {
811 // Record this function template specialization.
Douglas Gregor838db382010-02-11 01:19:42 +0000812 Function->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor66724ea2009-11-14 01:20:54 +0000813 &TemplateArgs.getInnermost(),
814 InsertPos);
John McCall02cace72009-08-28 07:59:38 +0000815 }
Douglas Gregora735b202009-10-13 14:39:41 +0000816
Douglas Gregore53060f2009-06-25 22:08:12 +0000817 if (InitFunctionInstantiation(Function, D))
818 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000819
Douglas Gregore53060f2009-06-25 22:08:12 +0000820 bool Redeclaration = false;
821 bool OverloadableAttrRequired = false;
Douglas Gregora735b202009-10-13 14:39:41 +0000822
John McCall68263142009-11-18 22:49:29 +0000823 LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
824 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
825
Douglas Gregora735b202009-10-13 14:39:41 +0000826 if (TemplateParams || !FunctionTemplate) {
827 // Look only into the namespace where the friend would be declared to
828 // find a previous declaration. This is the innermost enclosing namespace,
829 // as described in ActOnFriendFunctionDecl.
John McCall68263142009-11-18 22:49:29 +0000830 SemaRef.LookupQualifiedName(Previous, DC);
Douglas Gregora735b202009-10-13 14:39:41 +0000831
Douglas Gregora735b202009-10-13 14:39:41 +0000832 // In C++, the previous declaration we find might be a tag type
833 // (class or enum). In this case, the new declaration will hide the
834 // tag type. Note that this does does not apply if we're declaring a
835 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +0000836 if (Previous.isSingleTagDecl())
837 Previous.clear();
Douglas Gregora735b202009-10-13 14:39:41 +0000838 }
839
John McCall9f54ad42009-12-10 09:41:52 +0000840 SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
841 false, Redeclaration,
Douglas Gregore53060f2009-06-25 22:08:12 +0000842 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000843
Douglas Gregora735b202009-10-13 14:39:41 +0000844 // If the original function was part of a friend declaration,
845 // inherit its namespace state and add it to the owner.
846 NamedDecl *FromFriendD
847 = TemplateParams? cast<NamedDecl>(D->getDescribedFunctionTemplate()) : D;
848 if (FromFriendD->getFriendObjectKind()) {
849 NamedDecl *ToFriendD = 0;
John McCall68263142009-11-18 22:49:29 +0000850 NamedDecl *PrevDecl;
Douglas Gregora735b202009-10-13 14:39:41 +0000851 if (TemplateParams) {
852 ToFriendD = cast<NamedDecl>(FunctionTemplate);
853 PrevDecl = FunctionTemplate->getPreviousDeclaration();
854 } else {
855 ToFriendD = Function;
856 PrevDecl = Function->getPreviousDeclaration();
857 }
858 ToFriendD->setObjectOfFriendDecl(PrevDecl != NULL);
859 if (!Owner->isDependentContext() && !PrevDecl)
860 DC->makeDeclVisibleInContext(ToFriendD, /* Recoverable = */ false);
861
862 if (!TemplateParams)
863 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
864 }
865
Douglas Gregore53060f2009-06-25 22:08:12 +0000866 return Function;
867}
868
Douglas Gregord60e1052009-08-27 16:57:43 +0000869Decl *
870TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
871 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +0000872 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
873 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +0000874 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +0000875 // We are creating a function template specialization from a function
876 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +0000877 // specialization for this particular set of template arguments.
Douglas Gregor6b906862009-08-21 00:16:32 +0000878 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000879 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000880 TemplateArgs.getInnermost().getFlatArgumentList(),
881 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000882 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000883
884 FunctionTemplateSpecializationInfo *Info
885 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor6b906862009-08-21 00:16:32 +0000886 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000887
Douglas Gregor6b906862009-08-21 00:16:32 +0000888 // If we already have a function template specialization, return it.
889 if (Info)
890 return Info->Function;
891 }
892
Douglas Gregor79c22782010-01-16 20:21:20 +0000893 bool MergeWithParentScope = (TemplateParams != 0) ||
894 !(isa<Decl>(Owner) &&
895 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
896 Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000897
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000898 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000899 QualType T = SubstFunctionType(D, Params);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000900 if (T.isNull())
901 return 0;
902
903 // Build the instantiated method declaration.
904 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
Douglas Gregordec06662009-08-21 18:42:58 +0000905 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000906
Douglas Gregordec06662009-08-21 18:42:58 +0000907 DeclarationName Name = D->getDeclName();
Douglas Gregor17e32f32009-08-21 22:43:28 +0000908 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregordec06662009-08-21 18:42:58 +0000909 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
910 Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
911 SemaRef.Context.getCanonicalType(ClassTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000912 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
913 Constructor->getLocation(),
914 Name, T,
John McCalla93c9342009-12-07 02:54:59 +0000915 Constructor->getTypeSourceInfo(),
Mike Stump1eb44332009-09-09 15:08:12 +0000916 Constructor->isExplicit(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000917 Constructor->isInlineSpecified(), false);
Douglas Gregor17e32f32009-08-21 22:43:28 +0000918 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
919 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
920 Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
921 SemaRef.Context.getCanonicalType(ClassTy));
922 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
923 Destructor->getLocation(), Name,
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000924 T, Destructor->isInlineSpecified(), false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000925 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000926 CanQualType ConvTy
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000927 = SemaRef.Context.getCanonicalType(
John McCall183700f2009-09-21 23:43:11 +0000928 T->getAs<FunctionType>()->getResultType());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000929 Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
930 ConvTy);
931 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
932 Conversion->getLocation(), Name,
John McCalla93c9342009-12-07 02:54:59 +0000933 T, Conversion->getTypeSourceInfo(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000934 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000935 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +0000936 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000937 Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
John McCalla93c9342009-12-07 02:54:59 +0000938 D->getDeclName(), T, D->getTypeSourceInfo(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000939 D->isStatic(), D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +0000940 }
Douglas Gregor6b906862009-08-21 00:16:32 +0000941
Douglas Gregord60e1052009-08-27 16:57:43 +0000942 if (TemplateParams) {
943 // Our resulting instantiation is actually a function template, since we
944 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +0000945 //
Douglas Gregord60e1052009-08-27 16:57:43 +0000946 // template<typename T>
947 // struct X {
948 // template<typename U> void f(T, U);
949 // };
950 //
951 // X<int> x;
952 //
953 // We are instantiating the member template "f" within X<int>, which means
954 // substituting int for T, but leaving "f" as a member function template.
955 // Build the function template itself.
956 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
957 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +0000958 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +0000959 TemplateParams, Method);
960 if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +0000961 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +0000962 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +0000963 } else if (FunctionTemplate) {
964 // Record this function template specialization.
Douglas Gregor838db382010-02-11 01:19:42 +0000965 Method->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor66724ea2009-11-14 01:20:54 +0000966 &TemplateArgs.getInnermost(),
967 InsertPos);
968 } else {
969 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +0000970 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +0000971 }
972
Mike Stump1eb44332009-09-09 15:08:12 +0000973 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000974 // out-of-line, the instantiation will have the same lexical
975 // context (which will be a namespace scope) as the template.
976 if (D->isOutOfLine())
977 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Douglas Gregor5545e162009-03-24 00:38:23 +0000979 // Attach the parameters
980 for (unsigned P = 0; P < Params.size(); ++P)
981 Params[P]->setOwningFunction(Method);
Douglas Gregor838db382010-02-11 01:19:42 +0000982 Method->setParams(Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +0000983
984 if (InitMethodInstantiation(Method, D))
985 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000986
John McCall68263142009-11-18 22:49:29 +0000987 LookupResult Previous(SemaRef, Name, SourceLocation(),
988 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000989
Douglas Gregord60e1052009-08-27 16:57:43 +0000990 if (!FunctionTemplate || TemplateParams) {
John McCall68263142009-11-18 22:49:29 +0000991 SemaRef.LookupQualifiedName(Previous, Owner);
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Douglas Gregordec06662009-08-21 18:42:58 +0000993 // In C++, the previous declaration we find might be a tag type
994 // (class or enum). In this case, the new declaration will hide the
995 // tag type. Note that this does does not apply if we're declaring a
996 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +0000997 if (Previous.isSingleTagDecl())
998 Previous.clear();
Douglas Gregordec06662009-08-21 18:42:58 +0000999 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001000
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001001 bool Redeclaration = false;
1002 bool OverloadableAttrRequired = false;
John McCall9f54ad42009-12-10 09:41:52 +00001003 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001004 /*FIXME:*/OverloadableAttrRequired);
1005
Douglas Gregor4ba31362009-12-01 17:24:26 +00001006 if (D->isPure())
1007 SemaRef.CheckPureMethod(Method, SourceRange());
1008
John McCall46460a62010-01-20 21:53:11 +00001009 Method->setAccess(D->getAccess());
1010
John McCall68263142009-11-18 22:49:29 +00001011 if (!FunctionTemplate && (!Method->isInvalidDecl() || Previous.empty()) &&
Douglas Gregora735b202009-10-13 14:39:41 +00001012 !Method->getFriendObjectKind())
Douglas Gregordec06662009-08-21 18:42:58 +00001013 Owner->addDecl(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001015 return Method;
1016}
1017
Douglas Gregor615c5d42009-03-24 16:43:20 +00001018Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +00001019 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +00001020}
1021
Douglas Gregor03b2b072009-03-24 00:15:49 +00001022Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001023 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +00001024}
1025
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001026Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001027 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001028}
1029
Douglas Gregor6477b692009-03-25 15:04:13 +00001030ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
John McCall58e46772009-10-23 21:48:59 +00001031 QualType T;
John McCalla93c9342009-12-07 02:54:59 +00001032 TypeSourceInfo *DI = D->getTypeSourceInfo();
John McCall58e46772009-10-23 21:48:59 +00001033 if (DI) {
1034 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
1035 D->getDeclName());
1036 if (DI) T = DI->getType();
1037 } else {
1038 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
1039 D->getDeclName());
1040 DI = 0;
1041 }
1042
1043 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001044 return 0;
1045
John McCall58e46772009-10-23 21:48:59 +00001046 T = SemaRef.adjustParameterType(T);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001047
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001048 // Allocate the parameter
John McCall58e46772009-10-23 21:48:59 +00001049 ParmVarDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +00001050 = ParmVarDecl::Create(SemaRef.Context,
1051 SemaRef.Context.getTranslationUnitDecl(),
1052 D->getLocation(),
John McCall58e46772009-10-23 21:48:59 +00001053 D->getIdentifier(), T, DI, D->getStorageClass(), 0);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001054
Anders Carlsson9351c172009-08-25 03:18:48 +00001055 // Mark the default argument as being uninstantiated.
Douglas Gregorf43d0b32009-09-25 06:56:31 +00001056 if (D->hasUninstantiatedDefaultArg())
1057 Param->setUninstantiatedDefaultArg(D->getUninstantiatedDefaultArg());
Douglas Gregor0ed09302009-09-25 07:03:22 +00001058 else if (Expr *Arg = D->getDefaultArg())
1059 Param->setUninstantiatedDefaultArg(Arg);
1060
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001061 // Note: we don't try to instantiate function parameters until after
1062 // we've instantiated the function's type. Therefore, we don't have
1063 // to check for 'void' parameter types here.
Douglas Gregor48dd19b2009-05-14 21:44:34 +00001064 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001065 return Param;
1066}
1067
John McCalle29ba202009-08-20 01:44:21 +00001068Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1069 TemplateTypeParmDecl *D) {
1070 // TODO: don't always clone when decls are refcounted.
1071 const Type* T = D->getTypeForDecl();
1072 assert(T->isTemplateTypeParmType());
1073 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001074
John McCalle29ba202009-08-20 01:44:21 +00001075 TemplateTypeParmDecl *Inst =
1076 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor550d9b22009-10-31 17:21:17 +00001077 TTPT->getDepth() - 1, TTPT->getIndex(),
John McCalle29ba202009-08-20 01:44:21 +00001078 TTPT->getName(),
1079 D->wasDeclaredWithTypename(),
1080 D->isParameterPack());
1081
Douglas Gregor0f8716b2009-11-09 19:17:50 +00001082 if (D->hasDefaultArgument())
1083 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
John McCalle29ba202009-08-20 01:44:21 +00001084
Douglas Gregor550d9b22009-10-31 17:21:17 +00001085 // Introduce this template parameter's instantiation into the instantiation
1086 // scope.
1087 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1088
John McCalle29ba202009-08-20 01:44:21 +00001089 return Inst;
1090}
1091
Douglas Gregor33642df2009-10-23 23:25:44 +00001092Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1093 NonTypeTemplateParmDecl *D) {
1094 // Substitute into the type of the non-type template parameter.
1095 QualType T;
John McCalla93c9342009-12-07 02:54:59 +00001096 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor33642df2009-10-23 23:25:44 +00001097 if (DI) {
1098 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
1099 D->getDeclName());
1100 if (DI) T = DI->getType();
1101 } else {
1102 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
1103 D->getDeclName());
1104 DI = 0;
1105 }
1106 if (T.isNull())
1107 return 0;
1108
1109 // Check that this type is acceptable for a non-type template parameter.
1110 bool Invalid = false;
1111 T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
1112 if (T.isNull()) {
1113 T = SemaRef.Context.IntTy;
1114 Invalid = true;
1115 }
1116
1117 NonTypeTemplateParmDecl *Param
1118 = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1119 D->getDepth() - 1, D->getPosition(),
1120 D->getIdentifier(), T, DI);
1121 if (Invalid)
1122 Param->setInvalidDecl();
1123
1124 Param->setDefaultArgument(D->getDefaultArgument());
Douglas Gregor550d9b22009-10-31 17:21:17 +00001125
1126 // Introduce this template parameter's instantiation into the instantiation
1127 // scope.
1128 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001129 return Param;
1130}
1131
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001132Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001133TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1134 TemplateTemplateParmDecl *D) {
1135 // Instantiate the template parameter list of the template template parameter.
1136 TemplateParameterList *TempParams = D->getTemplateParameters();
1137 TemplateParameterList *InstParams;
1138 {
1139 // Perform the actual substitution of template parameters within a new,
1140 // local instantiation scope.
1141 Sema::LocalInstantiationScope Scope(SemaRef);
1142 InstParams = SubstTemplateParams(TempParams);
1143 if (!InstParams)
1144 return NULL;
1145 }
1146
1147 // Build the template template parameter.
1148 TemplateTemplateParmDecl *Param
1149 = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1150 D->getDepth() - 1, D->getPosition(),
1151 D->getIdentifier(), InstParams);
1152 Param->setDefaultArgument(D->getDefaultArgument());
1153
1154 // Introduce this template parameter's instantiation into the instantiation
1155 // scope.
1156 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1157
1158 return Param;
1159}
1160
Douglas Gregor48c32a72009-11-17 06:07:40 +00001161Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1162 // Using directives are never dependent, so they require no explicit
1163
1164 UsingDirectiveDecl *Inst
1165 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1166 D->getNamespaceKeyLocation(),
1167 D->getQualifierRange(), D->getQualifier(),
1168 D->getIdentLocation(),
1169 D->getNominatedNamespace(),
1170 D->getCommonAncestor());
1171 Owner->addDecl(Inst);
1172 return Inst;
1173}
1174
John McCalled976492009-12-04 22:46:56 +00001175Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
1176 // The nested name specifier is non-dependent, so no transformation
1177 // is required.
1178
John McCall9f54ad42009-12-10 09:41:52 +00001179 // We only need to do redeclaration lookups if we're in a class
1180 // scope (in fact, it's not really even possible in non-class
1181 // scopes).
1182 bool CheckRedeclaration = Owner->isRecord();
1183
1184 LookupResult Prev(SemaRef, D->getDeclName(), D->getLocation(),
1185 Sema::LookupUsingDeclName, Sema::ForRedeclaration);
1186
John McCalled976492009-12-04 22:46:56 +00001187 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
1188 D->getLocation(),
1189 D->getNestedNameRange(),
1190 D->getUsingLocation(),
1191 D->getTargetNestedNameDecl(),
1192 D->getDeclName(),
1193 D->isTypeName());
1194
1195 CXXScopeSpec SS;
1196 SS.setScopeRep(D->getTargetNestedNameDecl());
1197 SS.setRange(D->getNestedNameRange());
John McCall9f54ad42009-12-10 09:41:52 +00001198
1199 if (CheckRedeclaration) {
1200 Prev.setHideTags(false);
1201 SemaRef.LookupQualifiedName(Prev, Owner);
1202
1203 // Check for invalid redeclarations.
1204 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
1205 D->isTypeName(), SS,
1206 D->getLocation(), Prev))
1207 NewUD->setInvalidDecl();
1208
1209 }
1210
1211 if (!NewUD->isInvalidDecl() &&
1212 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
John McCalled976492009-12-04 22:46:56 +00001213 D->getLocation()))
1214 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00001215
John McCalled976492009-12-04 22:46:56 +00001216 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1217 NewUD->setAccess(D->getAccess());
1218 Owner->addDecl(NewUD);
1219
John McCall9f54ad42009-12-10 09:41:52 +00001220 // Don't process the shadow decls for an invalid decl.
1221 if (NewUD->isInvalidDecl())
1222 return NewUD;
1223
John McCall323c3102009-12-22 22:26:37 +00001224 bool isFunctionScope = Owner->isFunctionOrMethod();
1225
John McCall9f54ad42009-12-10 09:41:52 +00001226 // Process the shadow decls.
1227 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
1228 I != E; ++I) {
1229 UsingShadowDecl *Shadow = *I;
1230 NamedDecl *InstTarget =
1231 cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getTargetDecl(),
1232 TemplateArgs));
1233
1234 if (CheckRedeclaration &&
1235 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
1236 continue;
1237
1238 UsingShadowDecl *InstShadow
1239 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
1240 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
John McCall323c3102009-12-22 22:26:37 +00001241
1242 if (isFunctionScope)
1243 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
John McCall9f54ad42009-12-10 09:41:52 +00001244 }
John McCalled976492009-12-04 22:46:56 +00001245
1246 return NewUD;
1247}
1248
1249Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00001250 // Ignore these; we handle them in bulk when processing the UsingDecl.
1251 return 0;
John McCalled976492009-12-04 22:46:56 +00001252}
1253
John McCall7ba107a2009-11-18 02:36:19 +00001254Decl * TemplateDeclInstantiator
1255 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +00001256 NestedNameSpecifier *NNS =
1257 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1258 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001259 TemplateArgs);
1260 if (!NNS)
1261 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001262
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001263 CXXScopeSpec SS;
1264 SS.setRange(D->getTargetNestedNameRange());
1265 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +00001266
1267 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00001268 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
John McCall7ba107a2009-11-18 02:36:19 +00001269 D->getUsingLoc(), SS, D->getLocation(),
1270 D->getDeclName(), 0,
1271 /*instantiation*/ true,
1272 /*typename*/ true, D->getTypenameLoc());
1273 if (UD)
John McCalled976492009-12-04 22:46:56 +00001274 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1275
John McCall7ba107a2009-11-18 02:36:19 +00001276 return UD;
1277}
1278
1279Decl * TemplateDeclInstantiator
1280 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1281 NestedNameSpecifier *NNS =
1282 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1283 D->getTargetNestedNameRange(),
1284 TemplateArgs);
1285 if (!NNS)
1286 return 0;
1287
1288 CXXScopeSpec SS;
1289 SS.setRange(D->getTargetNestedNameRange());
1290 SS.setScopeRep(NNS);
1291
1292 NamedDecl *UD =
1293 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1294 D->getUsingLoc(), SS, D->getLocation(),
1295 D->getDeclName(), 0,
1296 /*instantiation*/ true,
1297 /*typename*/ false, SourceLocation());
Anders Carlsson0d8df782009-08-29 19:37:28 +00001298 if (UD)
John McCalled976492009-12-04 22:46:56 +00001299 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1300
Anders Carlsson0d8df782009-08-29 19:37:28 +00001301 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001302}
1303
John McCallce3ff2b2009-08-25 22:02:44 +00001304Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001305 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001306 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001307 return Instantiator.Visit(D);
1308}
1309
John McCalle29ba202009-08-20 01:44:21 +00001310/// \brief Instantiates a nested template parameter list in the current
1311/// instantiation context.
1312///
1313/// \param L The parameter list to instantiate
1314///
1315/// \returns NULL if there was an error
1316TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001317TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001318 // Get errors for all the parameters before bailing out.
1319 bool Invalid = false;
1320
1321 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001322 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001323 ParamVector Params;
1324 Params.reserve(N);
1325 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1326 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001327 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001328 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001329 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00001330 }
1331
1332 // Clean up if we had an error.
1333 if (Invalid) {
1334 for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
1335 PI != PE; ++PI)
1336 if (*PI)
1337 (*PI)->Destroy(SemaRef.Context);
1338 return NULL;
1339 }
1340
1341 TemplateParameterList *InstL
1342 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1343 L->getLAngleLoc(), &Params.front(), N,
1344 L->getRAngleLoc());
1345 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001346}
John McCalle29ba202009-08-20 01:44:21 +00001347
Douglas Gregored9c0f92009-10-29 00:04:11 +00001348/// \brief Instantiate the declaration of a class template partial
1349/// specialization.
1350///
1351/// \param ClassTemplate the (instantiated) class template that is partially
1352// specialized by the instantiation of \p PartialSpec.
1353///
1354/// \param PartialSpec the (uninstantiated) class template partial
1355/// specialization that we are instantiating.
1356///
1357/// \returns true if there was an error, false otherwise.
1358bool
1359TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1360 ClassTemplateDecl *ClassTemplate,
1361 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001362 // Create a local instantiation scope for this class template partial
1363 // specialization, which will contain the instantiations of the template
1364 // parameters.
1365 Sema::LocalInstantiationScope Scope(SemaRef);
1366
Douglas Gregored9c0f92009-10-29 00:04:11 +00001367 // Substitute into the template parameters of the class template partial
1368 // specialization.
1369 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1370 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1371 if (!InstParams)
1372 return true;
1373
1374 // Substitute into the template arguments of the class template partial
1375 // specialization.
John McCall833ca992009-10-29 08:12:44 +00001376 const TemplateArgumentLoc *PartialSpecTemplateArgs
1377 = PartialSpec->getTemplateArgsAsWritten();
1378 unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1379
John McCalld5532b62009-11-23 01:53:49 +00001380 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
John McCall833ca992009-10-29 08:12:44 +00001381 for (unsigned I = 0; I != N; ++I) {
John McCalld5532b62009-11-23 01:53:49 +00001382 TemplateArgumentLoc Loc;
1383 if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs))
Douglas Gregored9c0f92009-10-29 00:04:11 +00001384 return true;
John McCalld5532b62009-11-23 01:53:49 +00001385 InstTemplateArgs.addArgument(Loc);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001386 }
1387
1388
1389 // Check that the template argument list is well-formed for this
1390 // class template.
1391 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1392 InstTemplateArgs.size());
1393 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1394 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001395 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001396 false,
1397 Converted))
1398 return true;
1399
1400 // Figure out where to insert this class template partial specialization
1401 // in the member template's set of class template partial specializations.
1402 llvm::FoldingSetNodeID ID;
1403 ClassTemplatePartialSpecializationDecl::Profile(ID,
1404 Converted.getFlatArguments(),
1405 Converted.flatSize(),
1406 SemaRef.Context);
1407 void *InsertPos = 0;
1408 ClassTemplateSpecializationDecl *PrevDecl
1409 = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
1410 InsertPos);
1411
1412 // Build the canonical type that describes the converted template
1413 // arguments of the class template partial specialization.
1414 QualType CanonType
1415 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1416 Converted.getFlatArguments(),
1417 Converted.flatSize());
1418
1419 // Build the fully-sugared type for this class template
1420 // specialization as the user wrote in the specialization
1421 // itself. This means that we'll pretty-print the type retrieved
1422 // from the specialization's declaration the way that the user
1423 // actually wrote the specialization, rather than formatting the
1424 // name based on the "canonical" representation used to store the
1425 // template arguments in the specialization.
1426 QualType WrittenTy
1427 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
John McCalld5532b62009-11-23 01:53:49 +00001428 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001429 CanonType);
1430
1431 if (PrevDecl) {
1432 // We've already seen a partial specialization with the same template
1433 // parameters and template arguments. This can happen, for example, when
1434 // substituting the outer template arguments ends up causing two
1435 // class template partial specializations of a member class template
1436 // to have identical forms, e.g.,
1437 //
1438 // template<typename T, typename U>
1439 // struct Outer {
1440 // template<typename X, typename Y> struct Inner;
1441 // template<typename Y> struct Inner<T, Y>;
1442 // template<typename Y> struct Inner<U, Y>;
1443 // };
1444 //
1445 // Outer<int, int> outer; // error: the partial specializations of Inner
1446 // // have the same signature.
1447 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1448 << WrittenTy;
1449 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1450 << SemaRef.Context.getTypeDeclType(PrevDecl);
1451 return true;
1452 }
1453
1454
1455 // Create the class template partial specialization declaration.
1456 ClassTemplatePartialSpecializationDecl *InstPartialSpec
1457 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context, Owner,
1458 PartialSpec->getLocation(),
1459 InstParams,
1460 ClassTemplate,
1461 Converted,
John McCalld5532b62009-11-23 01:53:49 +00001462 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001463 0);
1464 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
1465 InstPartialSpec->setTypeAsWritten(WrittenTy);
1466
1467 // Add this partial specialization to the set of class template partial
1468 // specializations.
1469 ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec,
1470 InsertPos);
1471 return false;
1472}
1473
John McCallce3ff2b2009-08-25 22:02:44 +00001474/// \brief Does substitution on the type of the given function, including
1475/// all of the function parameters.
Douglas Gregor5545e162009-03-24 00:38:23 +00001476///
John McCallce3ff2b2009-08-25 22:02:44 +00001477/// \param D The function whose type will be the basis of the substitution
Douglas Gregor5545e162009-03-24 00:38:23 +00001478///
1479/// \param Params the instantiated parameter declarations
1480
John McCallce3ff2b2009-08-25 22:02:44 +00001481/// \returns the instantiated function's type if successful, a NULL
Douglas Gregor5545e162009-03-24 00:38:23 +00001482/// type if there was an error.
Mike Stump1eb44332009-09-09 15:08:12 +00001483QualType
John McCallce3ff2b2009-08-25 22:02:44 +00001484TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001485 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
1486 bool InvalidDecl = false;
1487
John McCallce3ff2b2009-08-25 22:02:44 +00001488 // Substitute all of the function's formal parameter types.
Douglas Gregor7e063902009-05-11 23:53:27 +00001489 TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001490 llvm::SmallVector<QualType, 4> ParamTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001491 for (FunctionDecl::param_iterator P = D->param_begin(),
Douglas Gregor5545e162009-03-24 00:38:23 +00001492 PEnd = D->param_end();
1493 P != PEnd; ++P) {
Douglas Gregor6477b692009-03-25 15:04:13 +00001494 if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
Douglas Gregor5545e162009-03-24 00:38:23 +00001495 if (PInst->getType()->isVoidType()) {
1496 SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
1497 PInst->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001498 } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001499 PInst->getType(),
1500 diag::err_abstract_type_in_decl,
1501 Sema::AbstractParamType))
Douglas Gregor5545e162009-03-24 00:38:23 +00001502 PInst->setInvalidDecl();
1503
1504 Params.push_back(PInst);
1505 ParamTys.push_back(PInst->getType());
1506
1507 if (PInst->isInvalidDecl())
1508 InvalidDecl = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001509 } else
Douglas Gregor5545e162009-03-24 00:38:23 +00001510 InvalidDecl = true;
1511 }
1512
1513 // FIXME: Deallocate dead declarations.
1514 if (InvalidDecl)
1515 return QualType();
1516
John McCall183700f2009-09-21 23:43:11 +00001517 const FunctionProtoType *Proto = D->getType()->getAs<FunctionProtoType>();
Douglas Gregor5545e162009-03-24 00:38:23 +00001518 assert(Proto && "Missing prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +00001519 QualType ResultType
John McCallce3ff2b2009-08-25 22:02:44 +00001520 = SemaRef.SubstType(Proto->getResultType(), TemplateArgs,
1521 D->getLocation(), D->getDeclName());
Douglas Gregor5545e162009-03-24 00:38:23 +00001522 if (ResultType.isNull())
1523 return QualType();
1524
Jay Foadbeaaccd2009-05-21 09:52:38 +00001525 return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
Douglas Gregor5545e162009-03-24 00:38:23 +00001526 Proto->isVariadic(), Proto->getTypeQuals(),
1527 D->getLocation(), D->getDeclName());
1528}
1529
Mike Stump1eb44332009-09-09 15:08:12 +00001530/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00001531/// declaration (New) from the corresponding fields of its template (Tmpl).
1532///
1533/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001534bool
1535TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00001536 FunctionDecl *Tmpl) {
1537 if (Tmpl->isDeleted())
1538 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00001539
Douglas Gregorcca9e962009-07-01 22:01:06 +00001540 // If we are performing substituting explicitly-specified template arguments
1541 // or deduced template arguments into a function template and we reach this
1542 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00001543 // to keeping the new function template specialization. We therefore
1544 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00001545 // into a template instantiation for this specific function template
1546 // specialization, which is not a SFINAE context, so that we diagnose any
1547 // further errors in the declaration itself.
1548 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1549 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1550 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1551 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00001552 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00001553 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001554 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00001555 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00001556 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001557 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1558 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
Douglas Gregorf35f8282009-11-11 21:54:23 +00001559 --SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001560 }
1561 }
Mike Stump1eb44332009-09-09 15:08:12 +00001562
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001563 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
1564 assert(Proto && "Function template without prototype?");
1565
1566 if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
1567 Proto->getNoReturnAttr()) {
1568 // The function has an exception specification or a "noreturn"
1569 // attribute. Substitute into each of the exception types.
1570 llvm::SmallVector<QualType, 4> Exceptions;
1571 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
1572 // FIXME: Poor location information!
1573 QualType T
1574 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
1575 New->getLocation(), New->getDeclName());
1576 if (T.isNull() ||
1577 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
1578 continue;
1579
1580 Exceptions.push_back(T);
1581 }
1582
1583 // Rebuild the function type
1584
1585 const FunctionProtoType *NewProto
1586 = New->getType()->getAs<FunctionProtoType>();
1587 assert(NewProto && "Template instantiation without function prototype?");
1588 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
1589 NewProto->arg_type_begin(),
1590 NewProto->getNumArgs(),
1591 NewProto->isVariadic(),
1592 NewProto->getTypeQuals(),
1593 Proto->hasExceptionSpec(),
1594 Proto->hasAnyExceptionSpec(),
1595 Exceptions.size(),
1596 Exceptions.data(),
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001597 Proto->getNoReturnAttr(),
1598 Proto->getCallConv()));
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001599 }
1600
Douglas Gregore53060f2009-06-25 22:08:12 +00001601 return false;
1602}
1603
Douglas Gregor5545e162009-03-24 00:38:23 +00001604/// \brief Initializes common fields of an instantiated method
1605/// declaration (New) from the corresponding fields of its template
1606/// (Tmpl).
1607///
1608/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001609bool
1610TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00001611 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001612 if (InitFunctionInstantiation(New, Tmpl))
1613 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001614
Douglas Gregor5545e162009-03-24 00:38:23 +00001615 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
1616 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00001617 if (Tmpl->isVirtualAsWritten())
1618 Record->setMethodAsVirtual(New);
Douglas Gregor5545e162009-03-24 00:38:23 +00001619
1620 // FIXME: attributes
1621 // FIXME: New needs a pointer to Tmpl
1622 return false;
1623}
Douglas Gregora58861f2009-05-13 20:28:22 +00001624
1625/// \brief Instantiate the definition of the given function from its
1626/// template.
1627///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001628/// \param PointOfInstantiation the point at which the instantiation was
1629/// required. Note that this is not precisely a "point of instantiation"
1630/// for the function, but it's close.
1631///
Douglas Gregora58861f2009-05-13 20:28:22 +00001632/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001633/// function template specialization or member function of a class template
1634/// specialization.
1635///
1636/// \param Recursive if true, recursively instantiates any functions that
1637/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001638///
1639/// \param DefinitionRequired if true, then we are performing an explicit
1640/// instantiation where the body of the function is required. Complain if
1641/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001642void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001643 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001644 bool Recursive,
1645 bool DefinitionRequired) {
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001646 if (Function->isInvalidDecl())
1647 return;
1648
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001649 assert(!Function->getBody() && "Already instantiated!");
Mike Stump1eb44332009-09-09 15:08:12 +00001650
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001651 // Never instantiate an explicit specialization.
1652 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1653 return;
1654
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001655 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00001656 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001657 Stmt *Pattern = 0;
1658 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001659 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001660
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001661 if (!Pattern) {
1662 if (DefinitionRequired) {
1663 if (Function->getPrimaryTemplate())
1664 Diag(PointOfInstantiation,
1665 diag::err_explicit_instantiation_undefined_func_template)
1666 << Function->getPrimaryTemplate();
1667 else
1668 Diag(PointOfInstantiation,
1669 diag::err_explicit_instantiation_undefined_member)
1670 << 1 << Function->getDeclName() << Function->getDeclContext();
1671
1672 if (PatternDecl)
1673 Diag(PatternDecl->getLocation(),
1674 diag::note_explicit_instantiation_here);
1675 }
1676
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001677 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001678 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001679
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001680 // C++0x [temp.explicit]p9:
1681 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00001682 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001683 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00001684 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001685 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001686 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001687 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001688
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001689 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
1690 if (Inst)
1691 return;
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001692
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001693 // If we're performing recursive template instantiation, create our own
1694 // queue of pending implicit instantiations that we will instantiate later,
1695 // while we're still within our own instantiation context.
1696 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1697 if (Recursive)
1698 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001699
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001700 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
1701
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001702 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00001703 // recorded, unless we're actually a member function within a local
1704 // class, in which case we need to merge our results with the parent
1705 // scope (of the enclosing function).
1706 bool MergeWithParentScope = false;
1707 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
1708 MergeWithParentScope = Rec->isLocalClass();
1709
1710 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001711
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001712 // Introduce the instantiated function parameters into the local
1713 // instantiation scope.
1714 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
1715 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
1716 Function->getParamDecl(I));
1717
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001718 // Enter the scope of this instantiation. We don't use
1719 // PushDeclContext because we don't have a scope.
1720 DeclContext *PreviousContext = CurContext;
1721 CurContext = Function;
1722
Mike Stump1eb44332009-09-09 15:08:12 +00001723 MultiLevelTemplateArgumentList TemplateArgs =
Anders Carlsson09025312009-08-29 05:16:22 +00001724 getTemplateInstantiationArgs(Function);
1725
1726 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001727 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00001728 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
1729 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
1730 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001731 }
1732
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001733 // Instantiate the function body.
Anders Carlsson09025312009-08-29 05:16:22 +00001734 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001735
Douglas Gregor52604ab2009-09-11 21:19:12 +00001736 if (Body.isInvalid())
1737 Function->setInvalidDecl();
1738
Mike Stump1eb44332009-09-09 15:08:12 +00001739 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001740 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001741
1742 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00001743
1744 DeclGroupRef DG(Function);
1745 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001746
Douglas Gregor60406be2010-01-16 22:29:39 +00001747 // This class may have local implicit instantiations that need to be
1748 // instantiation within this scope.
1749 PerformPendingImplicitInstantiations(/*LocalOnly=*/true);
1750 Scope.Exit();
1751
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001752 if (Recursive) {
1753 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001754 // instantiation of this template.
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001755 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001756
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001757 // Restore the set of pending implicit instantiations.
1758 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1759 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001760}
1761
1762/// \brief Instantiate the definition of the given variable from its
1763/// template.
1764///
Douglas Gregor7caa6822009-07-24 20:34:43 +00001765/// \param PointOfInstantiation the point at which the instantiation was
1766/// required. Note that this is not precisely a "point of instantiation"
1767/// for the function, but it's close.
1768///
1769/// \param Var the already-instantiated declaration of a static member
1770/// variable of a class template specialization.
1771///
1772/// \param Recursive if true, recursively instantiates any functions that
1773/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001774///
1775/// \param DefinitionRequired if true, then we are performing an explicit
1776/// instantiation where an out-of-line definition of the member variable
1777/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001778void Sema::InstantiateStaticDataMemberDefinition(
1779 SourceLocation PointOfInstantiation,
1780 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001781 bool Recursive,
1782 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001783 if (Var->isInvalidDecl())
1784 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001785
Douglas Gregor7caa6822009-07-24 20:34:43 +00001786 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001787 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00001788 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00001789 assert(Def->isStaticDataMember() && "Not a static data member?");
1790 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00001791
Douglas Gregor0d035142009-10-27 18:42:08 +00001792 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001793 // We did not find an out-of-line definition of this static data member,
1794 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00001795 // instantiate this definition (or provide a specialization for it) in
1796 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001797 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001798 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001799 Diag(PointOfInstantiation,
1800 diag::err_explicit_instantiation_undefined_member)
1801 << 2 << Var->getDeclName() << Var->getDeclContext();
1802 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
1803 }
1804
Douglas Gregor7caa6822009-07-24 20:34:43 +00001805 return;
1806 }
1807
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001808 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001809 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001810 return;
1811
1812 // C++0x [temp.explicit]p9:
1813 // Except for inline functions, other explicit instantiation declarations
1814 // have the effect of suppressing the implicit instantiation of the entity
1815 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001816 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001817 == TSK_ExplicitInstantiationDeclaration)
1818 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001819
Douglas Gregor7caa6822009-07-24 20:34:43 +00001820 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
1821 if (Inst)
1822 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001823
Douglas Gregor7caa6822009-07-24 20:34:43 +00001824 // If we're performing recursive template instantiation, create our own
1825 // queue of pending implicit instantiations that we will instantiate later,
1826 // while we're still within our own instantiation context.
1827 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1828 if (Recursive)
1829 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001830
Douglas Gregor7caa6822009-07-24 20:34:43 +00001831 // Enter the scope of this instantiation. We don't use
1832 // PushDeclContext because we don't have a scope.
1833 DeclContext *PreviousContext = CurContext;
1834 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001835
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001836 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00001837 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00001838 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00001839 CurContext = PreviousContext;
1840
1841 if (Var) {
Douglas Gregor583f33b2009-10-15 18:07:02 +00001842 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
1843 assert(MSInfo && "Missing member specialization information?");
1844 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
1845 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00001846 DeclGroupRef DG(Var);
1847 Consumer.HandleTopLevelDecl(DG);
1848 }
Mike Stump1eb44332009-09-09 15:08:12 +00001849
Douglas Gregor7caa6822009-07-24 20:34:43 +00001850 if (Recursive) {
1851 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001852 // instantiation of this template.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001853 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001854
Douglas Gregor7caa6822009-07-24 20:34:43 +00001855 // Restore the set of pending implicit instantiations.
1856 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001857 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001858}
Douglas Gregor815215d2009-05-27 05:35:12 +00001859
Anders Carlsson09025312009-08-29 05:16:22 +00001860void
1861Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
1862 const CXXConstructorDecl *Tmpl,
1863 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001864
Anders Carlsson09025312009-08-29 05:16:22 +00001865 llvm::SmallVector<MemInitTy*, 4> NewInits;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001866 bool AnyErrors = false;
1867
Anders Carlsson09025312009-08-29 05:16:22 +00001868 // Instantiate all the initializers.
1869 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00001870 InitsEnd = Tmpl->init_end();
1871 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00001872 CXXBaseOrMemberInitializer *Init = *Inits;
1873
1874 ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001875 llvm::SmallVector<SourceLocation, 4> CommaLocs;
Mike Stump1eb44332009-09-09 15:08:12 +00001876
Anders Carlsson09025312009-08-29 05:16:22 +00001877 // Instantiate all the arguments.
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001878 Expr *InitE = Init->getInit();
1879 if (!InitE) {
1880 // Nothing to instantiate;
1881 } else if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(InitE)) {
1882 if (InstantiateInitializationArguments(*this, ParenList->getExprs(),
1883 ParenList->getNumExprs(),
1884 TemplateArgs, CommaLocs,
1885 NewArgs)) {
1886 AnyErrors = true;
1887 continue;
1888 }
1889 } else {
1890 OwningExprResult InitArg = SubstExpr(InitE, TemplateArgs);
1891 if (InitArg.isInvalid()) {
1892 AnyErrors = true;
1893 continue;
1894 }
1895
1896 NewArgs.push_back(InitArg.release());
Anders Carlsson09025312009-08-29 05:16:22 +00001897 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001898
Anders Carlsson09025312009-08-29 05:16:22 +00001899 MemInitResult NewInit;
Anders Carlsson09025312009-08-29 05:16:22 +00001900 if (Init->isBaseInitializer()) {
John McCalla93c9342009-12-07 02:54:59 +00001901 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
Douglas Gregor802ab452009-12-02 22:36:29 +00001902 TemplateArgs,
1903 Init->getSourceLocation(),
1904 New->getDeclName());
John McCalla93c9342009-12-07 02:54:59 +00001905 if (!BaseTInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001906 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00001907 New->setInvalidDecl();
1908 continue;
1909 }
1910
John McCalla93c9342009-12-07 02:54:59 +00001911 NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001912 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001913 NewArgs.size(),
Douglas Gregor802ab452009-12-02 22:36:29 +00001914 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00001915 Init->getRParenLoc(),
1916 New->getParent());
1917 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001918 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00001919
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001920 // Is this an anonymous union?
1921 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregore95b4092009-09-16 18:34:49 +00001922 Member = cast<FieldDecl>(FindInstantiatedDecl(UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001923 else
Douglas Gregore95b4092009-09-16 18:34:49 +00001924 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMember(),
1925 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00001926
1927 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001928 NewArgs.size(),
1929 Init->getSourceLocation(),
Douglas Gregor802ab452009-12-02 22:36:29 +00001930 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00001931 Init->getRParenLoc());
1932 }
1933
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001934 if (NewInit.isInvalid()) {
1935 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00001936 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001937 } else {
Anders Carlsson09025312009-08-29 05:16:22 +00001938 // FIXME: It would be nice if ASTOwningVector had a release function.
1939 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001940
Anders Carlsson09025312009-08-29 05:16:22 +00001941 NewInits.push_back((MemInitTy *)NewInit.get());
1942 }
1943 }
Mike Stump1eb44332009-09-09 15:08:12 +00001944
Anders Carlsson09025312009-08-29 05:16:22 +00001945 // Assign all the initializers to the new constructor.
Mike Stump1eb44332009-09-09 15:08:12 +00001946 ActOnMemInitializers(DeclPtrTy::make(New),
Anders Carlsson09025312009-08-29 05:16:22 +00001947 /*FIXME: ColonLoc */
1948 SourceLocation(),
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001949 NewInits.data(), NewInits.size(),
1950 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00001951}
1952
John McCall52a575a2009-08-29 08:11:13 +00001953// TODO: this could be templated if the various decl types used the
1954// same method name.
1955static bool isInstantiationOf(ClassTemplateDecl *Pattern,
1956 ClassTemplateDecl *Instance) {
1957 Pattern = Pattern->getCanonicalDecl();
1958
1959 do {
1960 Instance = Instance->getCanonicalDecl();
1961 if (Pattern == Instance) return true;
1962 Instance = Instance->getInstantiatedFromMemberTemplate();
1963 } while (Instance);
1964
1965 return false;
1966}
1967
Douglas Gregor0d696532009-09-28 06:34:35 +00001968static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
1969 FunctionTemplateDecl *Instance) {
1970 Pattern = Pattern->getCanonicalDecl();
1971
1972 do {
1973 Instance = Instance->getCanonicalDecl();
1974 if (Pattern == Instance) return true;
1975 Instance = Instance->getInstantiatedFromMemberTemplate();
1976 } while (Instance);
1977
1978 return false;
1979}
1980
Douglas Gregored9c0f92009-10-29 00:04:11 +00001981static bool
1982isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
1983 ClassTemplatePartialSpecializationDecl *Instance) {
1984 Pattern
1985 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
1986 do {
1987 Instance = cast<ClassTemplatePartialSpecializationDecl>(
1988 Instance->getCanonicalDecl());
1989 if (Pattern == Instance)
1990 return true;
1991 Instance = Instance->getInstantiatedFromMember();
1992 } while (Instance);
1993
1994 return false;
1995}
1996
John McCall52a575a2009-08-29 08:11:13 +00001997static bool isInstantiationOf(CXXRecordDecl *Pattern,
1998 CXXRecordDecl *Instance) {
1999 Pattern = Pattern->getCanonicalDecl();
2000
2001 do {
2002 Instance = Instance->getCanonicalDecl();
2003 if (Pattern == Instance) return true;
2004 Instance = Instance->getInstantiatedFromMemberClass();
2005 } while (Instance);
2006
2007 return false;
2008}
2009
2010static bool isInstantiationOf(FunctionDecl *Pattern,
2011 FunctionDecl *Instance) {
2012 Pattern = Pattern->getCanonicalDecl();
2013
2014 do {
2015 Instance = Instance->getCanonicalDecl();
2016 if (Pattern == Instance) return true;
2017 Instance = Instance->getInstantiatedFromMemberFunction();
2018 } while (Instance);
2019
2020 return false;
2021}
2022
2023static bool isInstantiationOf(EnumDecl *Pattern,
2024 EnumDecl *Instance) {
2025 Pattern = Pattern->getCanonicalDecl();
2026
2027 do {
2028 Instance = Instance->getCanonicalDecl();
2029 if (Pattern == Instance) return true;
2030 Instance = Instance->getInstantiatedFromMemberEnum();
2031 } while (Instance);
2032
2033 return false;
2034}
2035
John McCalled976492009-12-04 22:46:56 +00002036static bool isInstantiationOf(UsingShadowDecl *Pattern,
2037 UsingShadowDecl *Instance,
2038 ASTContext &C) {
2039 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2040}
2041
2042static bool isInstantiationOf(UsingDecl *Pattern,
2043 UsingDecl *Instance,
2044 ASTContext &C) {
2045 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2046}
2047
John McCall7ba107a2009-11-18 02:36:19 +00002048static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
2049 UsingDecl *Instance,
2050 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002051 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00002052}
2053
2054static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00002055 UsingDecl *Instance,
2056 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002057 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00002058}
2059
John McCall52a575a2009-08-29 08:11:13 +00002060static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
2061 VarDecl *Instance) {
2062 assert(Instance->isStaticDataMember());
2063
2064 Pattern = Pattern->getCanonicalDecl();
2065
2066 do {
2067 Instance = Instance->getCanonicalDecl();
2068 if (Pattern == Instance) return true;
2069 Instance = Instance->getInstantiatedFromStaticDataMember();
2070 } while (Instance);
2071
2072 return false;
2073}
2074
John McCalled976492009-12-04 22:46:56 +00002075// Other is the prospective instantiation
2076// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00002077static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002078 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00002079 if (UnresolvedUsingTypenameDecl *UUD
2080 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
2081 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2082 return isInstantiationOf(UUD, UD, Ctx);
2083 }
2084 }
2085
2086 if (UnresolvedUsingValueDecl *UUD
2087 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002088 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2089 return isInstantiationOf(UUD, UD, Ctx);
2090 }
2091 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002092
Anders Carlsson0d8df782009-08-29 19:37:28 +00002093 return false;
2094 }
Mike Stump1eb44332009-09-09 15:08:12 +00002095
John McCall52a575a2009-08-29 08:11:13 +00002096 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
2097 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002098
John McCall52a575a2009-08-29 08:11:13 +00002099 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
2100 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00002101
John McCall52a575a2009-08-29 08:11:13 +00002102 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
2103 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00002104
Douglas Gregor7caa6822009-07-24 20:34:43 +00002105 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00002106 if (Var->isStaticDataMember())
2107 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
2108
2109 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
2110 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00002111
Douglas Gregor0d696532009-09-28 06:34:35 +00002112 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
2113 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
2114
Douglas Gregored9c0f92009-10-29 00:04:11 +00002115 if (ClassTemplatePartialSpecializationDecl *PartialSpec
2116 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2117 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2118 PartialSpec);
2119
Anders Carlssond8b285f2009-09-01 04:26:58 +00002120 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2121 if (!Field->getDeclName()) {
2122 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00002123 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00002124 cast<FieldDecl>(D);
2125 }
2126 }
Mike Stump1eb44332009-09-09 15:08:12 +00002127
John McCalled976492009-12-04 22:46:56 +00002128 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2129 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2130
2131 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2132 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2133
Douglas Gregor815215d2009-05-27 05:35:12 +00002134 return D->getDeclName() && isa<NamedDecl>(Other) &&
2135 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2136}
2137
2138template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00002139static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00002140 NamedDecl *D,
2141 ForwardIterator first,
2142 ForwardIterator last) {
2143 for (; first != last; ++first)
2144 if (isInstantiationOf(Ctx, D, *first))
2145 return cast<NamedDecl>(*first);
2146
2147 return 0;
2148}
2149
John McCall02cace72009-08-28 07:59:38 +00002150/// \brief Finds the instantiation of the given declaration context
2151/// within the current instantiation.
2152///
2153/// \returns NULL if there was an error
Douglas Gregore95b4092009-09-16 18:34:49 +00002154DeclContext *Sema::FindInstantiatedContext(DeclContext* DC,
2155 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00002156 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00002157 Decl* ID = FindInstantiatedDecl(D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00002158 return cast_or_null<DeclContext>(ID);
2159 } else return DC;
2160}
2161
Douglas Gregored961e72009-05-27 17:54:46 +00002162/// \brief Find the instantiation of the given declaration within the
2163/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00002164///
2165/// This routine is intended to be used when \p D is a declaration
2166/// referenced from within a template, that needs to mapped into the
2167/// corresponding declaration within an instantiation. For example,
2168/// given:
2169///
2170/// \code
2171/// template<typename T>
2172/// struct X {
2173/// enum Kind {
2174/// KnownValue = sizeof(T)
2175/// };
2176///
2177/// bool getKind() const { return KnownValue; }
2178/// };
2179///
2180/// template struct X<int>;
2181/// \endcode
2182///
2183/// In the instantiation of X<int>::getKind(), we need to map the
2184/// EnumConstantDecl for KnownValue (which refers to
2185/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00002186/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2187/// this mapping from within the instantiation of X<int>.
Douglas Gregore95b4092009-09-16 18:34:49 +00002188NamedDecl *Sema::FindInstantiatedDecl(NamedDecl *D,
2189 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00002190 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00002191 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00002192 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
Douglas Gregor550d9b22009-10-31 17:21:17 +00002193 ParentDC->isFunctionOrMethod()) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002194 // D is a local of some kind. Look into the map of local
2195 // declarations to their instantiations.
2196 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
2197 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002198
Douglas Gregore95b4092009-09-16 18:34:49 +00002199 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2200 if (!Record->isDependentContext())
2201 return D;
2202
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002203 // If the RecordDecl is actually the injected-class-name or a
2204 // "templated" declaration for a class template, class template
2205 // partial specialization, or a member class of a class template,
2206 // substitute into the injected-class-name of the class template
2207 // or partial specialization to find the new DeclContext.
Douglas Gregore95b4092009-09-16 18:34:49 +00002208 QualType T;
2209 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2210
2211 if (ClassTemplate) {
2212 T = ClassTemplate->getInjectedClassNameType(Context);
2213 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2214 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
2215 T = Context.getTypeDeclType(Record);
2216 ClassTemplate = PartialSpec->getSpecializedTemplate();
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002217 }
Douglas Gregore95b4092009-09-16 18:34:49 +00002218
2219 if (!T.isNull()) {
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002220 // Substitute into the injected-class-name to get the type
2221 // corresponding to the instantiation we want, which may also be
2222 // the current instantiation (if we're in a template
2223 // definition). This substitution should never fail, since we
2224 // know we can instantiate the injected-class-name or we
2225 // wouldn't have gotten to the injected-class-name!
2226
2227 // FIXME: Can we use the CurrentInstantiationScope to avoid this
2228 // extra instantiation in the common case?
Douglas Gregore95b4092009-09-16 18:34:49 +00002229 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2230 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2231
2232 if (!T->isDependentType()) {
2233 assert(T->isRecordType() && "Instantiation must produce a record type");
2234 return T->getAs<RecordType>()->getDecl();
2235 }
2236
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002237 // We are performing "partial" template instantiation to create
2238 // the member declarations for the members of a class template
2239 // specialization. Therefore, D is actually referring to something
2240 // in the current instantiation. Look through the current
2241 // context, which contains actual instantiations, to find the
2242 // instantiation of the "current instantiation" that D refers
2243 // to.
2244 bool SawNonDependentContext = false;
Mike Stump1eb44332009-09-09 15:08:12 +00002245 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00002246 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002247 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002248 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00002249 if (isInstantiationOf(ClassTemplate,
2250 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00002251 return Spec;
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002252
2253 if (!DC->isDependentContext())
2254 SawNonDependentContext = true;
John McCall52a575a2009-08-29 08:11:13 +00002255 }
2256
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002257 // We're performing "instantiation" of a member of the current
2258 // instantiation while we are type-checking the
2259 // definition. Compute the declaration context and return that.
2260 assert(!SawNonDependentContext &&
2261 "No dependent context while instantiating record");
2262 DeclContext *DC = computeDeclContext(T);
2263 assert(DC &&
John McCall52a575a2009-08-29 08:11:13 +00002264 "Unable to find declaration for the current instantiation");
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002265 return cast<CXXRecordDecl>(DC);
John McCall52a575a2009-08-29 08:11:13 +00002266 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002267
Douglas Gregore95b4092009-09-16 18:34:49 +00002268 // Fall through to deal with other dependent record types (e.g.,
2269 // anonymous unions in class templates).
2270 }
John McCall52a575a2009-08-29 08:11:13 +00002271
Douglas Gregore95b4092009-09-16 18:34:49 +00002272 if (!ParentDC->isDependentContext())
2273 return D;
2274
2275 ParentDC = FindInstantiatedContext(ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002276 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00002277 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002278
Douglas Gregor815215d2009-05-27 05:35:12 +00002279 if (ParentDC != D->getDeclContext()) {
2280 // We performed some kind of instantiation in the parent context,
2281 // so now we need to look into the instantiated parent context to
2282 // find the instantiation of the declaration D.
2283 NamedDecl *Result = 0;
2284 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002285 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00002286 Result = findInstantiationOf(Context, D, Found.first, Found.second);
2287 } else {
2288 // Since we don't have a name for the entity we're looking for,
2289 // our only option is to walk through all of the declarations to
2290 // find that name. This will occur in a few cases:
2291 //
2292 // - anonymous struct/union within a template
2293 // - unnamed class/struct/union/enum within a template
2294 //
2295 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00002296 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002297 ParentDC->decls_begin(),
2298 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00002299 }
Mike Stump1eb44332009-09-09 15:08:12 +00002300
John McCall9f54ad42009-12-10 09:41:52 +00002301 // UsingShadowDecls can instantiate to nothing because of using hiding.
2302 assert((Result || isa<UsingShadowDecl>(D))
2303 && "Unable to find instantiation of declaration!");
2304
Douglas Gregor815215d2009-05-27 05:35:12 +00002305 D = Result;
2306 }
2307
Douglas Gregor815215d2009-05-27 05:35:12 +00002308 return D;
2309}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002310
Mike Stump1eb44332009-09-09 15:08:12 +00002311/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002312/// instantiations we have seen until this point.
Douglas Gregor60406be2010-01-16 22:29:39 +00002313void Sema::PerformPendingImplicitInstantiations(bool LocalOnly) {
2314 while (!PendingLocalImplicitInstantiations.empty() ||
2315 (!LocalOnly && !PendingImplicitInstantiations.empty())) {
2316 PendingImplicitInstantiation Inst;
2317
2318 if (PendingLocalImplicitInstantiations.empty()) {
2319 Inst = PendingImplicitInstantiations.front();
2320 PendingImplicitInstantiations.pop_front();
2321 } else {
2322 Inst = PendingLocalImplicitInstantiations.front();
2323 PendingLocalImplicitInstantiations.pop_front();
2324 }
Mike Stump1eb44332009-09-09 15:08:12 +00002325
Douglas Gregor7caa6822009-07-24 20:34:43 +00002326 // Instantiate function definitions
2327 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002328 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002329 Function->getLocation(), *this,
2330 Context.getSourceManager(),
2331 "instantiating function definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002332
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00002333 if (!Function->getBody())
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002334 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00002335 continue;
2336 }
Mike Stump1eb44332009-09-09 15:08:12 +00002337
Douglas Gregor7caa6822009-07-24 20:34:43 +00002338 // Instantiate static data member definitions.
2339 VarDecl *Var = cast<VarDecl>(Inst.first);
2340 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002341
Chandler Carruth291b4412010-02-13 10:17:50 +00002342 // Don't try to instantiate declarations if the most recent redeclaration
2343 // is invalid.
2344 if (Var->getMostRecentDeclaration()->isInvalidDecl())
2345 continue;
2346
2347 // Check if the most recent declaration has changed the specialization kind
2348 // and removed the need for implicit instantiation.
2349 switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
2350 case TSK_Undeclared:
2351 assert(false && "Cannot instantitiate an undeclared specialization.");
2352 case TSK_ExplicitInstantiationDeclaration:
2353 case TSK_ExplicitInstantiationDefinition:
2354 case TSK_ExplicitSpecialization:
2355 continue; // No longer need implicit instantiation.
2356 case TSK_ImplicitInstantiation:
2357 break;
2358 }
2359
Mike Stump1eb44332009-09-09 15:08:12 +00002360 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002361 Var->getLocation(), *this,
2362 Context.getSourceManager(),
2363 "instantiating static data member "
2364 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002365
Douglas Gregor7caa6822009-07-24 20:34:43 +00002366 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002367 }
2368}