blob: f13bd69453d672976da4b8a4b8721ff13dcb7aa7 [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 Gregor2fa98002010-02-16 19:28:15 +00001307 if (D->isInvalidDecl())
1308 return 0;
1309
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001310 return Instantiator.Visit(D);
1311}
1312
John McCalle29ba202009-08-20 01:44:21 +00001313/// \brief Instantiates a nested template parameter list in the current
1314/// instantiation context.
1315///
1316/// \param L The parameter list to instantiate
1317///
1318/// \returns NULL if there was an error
1319TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001320TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001321 // Get errors for all the parameters before bailing out.
1322 bool Invalid = false;
1323
1324 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001325 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001326 ParamVector Params;
1327 Params.reserve(N);
1328 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1329 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001330 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001331 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001332 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00001333 }
1334
1335 // Clean up if we had an error.
1336 if (Invalid) {
1337 for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
1338 PI != PE; ++PI)
1339 if (*PI)
1340 (*PI)->Destroy(SemaRef.Context);
1341 return NULL;
1342 }
1343
1344 TemplateParameterList *InstL
1345 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1346 L->getLAngleLoc(), &Params.front(), N,
1347 L->getRAngleLoc());
1348 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001349}
John McCalle29ba202009-08-20 01:44:21 +00001350
Douglas Gregored9c0f92009-10-29 00:04:11 +00001351/// \brief Instantiate the declaration of a class template partial
1352/// specialization.
1353///
1354/// \param ClassTemplate the (instantiated) class template that is partially
1355// specialized by the instantiation of \p PartialSpec.
1356///
1357/// \param PartialSpec the (uninstantiated) class template partial
1358/// specialization that we are instantiating.
1359///
1360/// \returns true if there was an error, false otherwise.
1361bool
1362TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1363 ClassTemplateDecl *ClassTemplate,
1364 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001365 // Create a local instantiation scope for this class template partial
1366 // specialization, which will contain the instantiations of the template
1367 // parameters.
1368 Sema::LocalInstantiationScope Scope(SemaRef);
1369
Douglas Gregored9c0f92009-10-29 00:04:11 +00001370 // Substitute into the template parameters of the class template partial
1371 // specialization.
1372 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1373 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1374 if (!InstParams)
1375 return true;
1376
1377 // Substitute into the template arguments of the class template partial
1378 // specialization.
John McCall833ca992009-10-29 08:12:44 +00001379 const TemplateArgumentLoc *PartialSpecTemplateArgs
1380 = PartialSpec->getTemplateArgsAsWritten();
1381 unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1382
John McCalld5532b62009-11-23 01:53:49 +00001383 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
John McCall833ca992009-10-29 08:12:44 +00001384 for (unsigned I = 0; I != N; ++I) {
John McCalld5532b62009-11-23 01:53:49 +00001385 TemplateArgumentLoc Loc;
1386 if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs))
Douglas Gregored9c0f92009-10-29 00:04:11 +00001387 return true;
John McCalld5532b62009-11-23 01:53:49 +00001388 InstTemplateArgs.addArgument(Loc);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001389 }
1390
1391
1392 // Check that the template argument list is well-formed for this
1393 // class template.
1394 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1395 InstTemplateArgs.size());
1396 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1397 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001398 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001399 false,
1400 Converted))
1401 return true;
1402
1403 // Figure out where to insert this class template partial specialization
1404 // in the member template's set of class template partial specializations.
1405 llvm::FoldingSetNodeID ID;
1406 ClassTemplatePartialSpecializationDecl::Profile(ID,
1407 Converted.getFlatArguments(),
1408 Converted.flatSize(),
1409 SemaRef.Context);
1410 void *InsertPos = 0;
1411 ClassTemplateSpecializationDecl *PrevDecl
1412 = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
1413 InsertPos);
1414
1415 // Build the canonical type that describes the converted template
1416 // arguments of the class template partial specialization.
1417 QualType CanonType
1418 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1419 Converted.getFlatArguments(),
1420 Converted.flatSize());
1421
1422 // Build the fully-sugared type for this class template
1423 // specialization as the user wrote in the specialization
1424 // itself. This means that we'll pretty-print the type retrieved
1425 // from the specialization's declaration the way that the user
1426 // actually wrote the specialization, rather than formatting the
1427 // name based on the "canonical" representation used to store the
1428 // template arguments in the specialization.
1429 QualType WrittenTy
1430 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
John McCalld5532b62009-11-23 01:53:49 +00001431 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001432 CanonType);
1433
1434 if (PrevDecl) {
1435 // We've already seen a partial specialization with the same template
1436 // parameters and template arguments. This can happen, for example, when
1437 // substituting the outer template arguments ends up causing two
1438 // class template partial specializations of a member class template
1439 // to have identical forms, e.g.,
1440 //
1441 // template<typename T, typename U>
1442 // struct Outer {
1443 // template<typename X, typename Y> struct Inner;
1444 // template<typename Y> struct Inner<T, Y>;
1445 // template<typename Y> struct Inner<U, Y>;
1446 // };
1447 //
1448 // Outer<int, int> outer; // error: the partial specializations of Inner
1449 // // have the same signature.
1450 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1451 << WrittenTy;
1452 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1453 << SemaRef.Context.getTypeDeclType(PrevDecl);
1454 return true;
1455 }
1456
1457
1458 // Create the class template partial specialization declaration.
1459 ClassTemplatePartialSpecializationDecl *InstPartialSpec
1460 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context, Owner,
1461 PartialSpec->getLocation(),
1462 InstParams,
1463 ClassTemplate,
1464 Converted,
John McCalld5532b62009-11-23 01:53:49 +00001465 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001466 0);
1467 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
1468 InstPartialSpec->setTypeAsWritten(WrittenTy);
1469
1470 // Add this partial specialization to the set of class template partial
1471 // specializations.
1472 ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec,
1473 InsertPos);
1474 return false;
1475}
1476
John McCallce3ff2b2009-08-25 22:02:44 +00001477/// \brief Does substitution on the type of the given function, including
1478/// all of the function parameters.
Douglas Gregor5545e162009-03-24 00:38:23 +00001479///
John McCallce3ff2b2009-08-25 22:02:44 +00001480/// \param D The function whose type will be the basis of the substitution
Douglas Gregor5545e162009-03-24 00:38:23 +00001481///
1482/// \param Params the instantiated parameter declarations
1483
John McCallce3ff2b2009-08-25 22:02:44 +00001484/// \returns the instantiated function's type if successful, a NULL
Douglas Gregor5545e162009-03-24 00:38:23 +00001485/// type if there was an error.
Mike Stump1eb44332009-09-09 15:08:12 +00001486QualType
John McCallce3ff2b2009-08-25 22:02:44 +00001487TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001488 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
1489 bool InvalidDecl = false;
1490
John McCallce3ff2b2009-08-25 22:02:44 +00001491 // Substitute all of the function's formal parameter types.
Douglas Gregor7e063902009-05-11 23:53:27 +00001492 TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001493 llvm::SmallVector<QualType, 4> ParamTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001494 for (FunctionDecl::param_iterator P = D->param_begin(),
Douglas Gregor5545e162009-03-24 00:38:23 +00001495 PEnd = D->param_end();
1496 P != PEnd; ++P) {
Douglas Gregor6477b692009-03-25 15:04:13 +00001497 if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
Douglas Gregor5545e162009-03-24 00:38:23 +00001498 if (PInst->getType()->isVoidType()) {
1499 SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
1500 PInst->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001501 } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001502 PInst->getType(),
1503 diag::err_abstract_type_in_decl,
1504 Sema::AbstractParamType))
Douglas Gregor5545e162009-03-24 00:38:23 +00001505 PInst->setInvalidDecl();
1506
1507 Params.push_back(PInst);
1508 ParamTys.push_back(PInst->getType());
1509
1510 if (PInst->isInvalidDecl())
1511 InvalidDecl = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001512 } else
Douglas Gregor5545e162009-03-24 00:38:23 +00001513 InvalidDecl = true;
1514 }
1515
1516 // FIXME: Deallocate dead declarations.
1517 if (InvalidDecl)
1518 return QualType();
1519
John McCall183700f2009-09-21 23:43:11 +00001520 const FunctionProtoType *Proto = D->getType()->getAs<FunctionProtoType>();
Douglas Gregor5545e162009-03-24 00:38:23 +00001521 assert(Proto && "Missing prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +00001522 QualType ResultType
John McCallce3ff2b2009-08-25 22:02:44 +00001523 = SemaRef.SubstType(Proto->getResultType(), TemplateArgs,
1524 D->getLocation(), D->getDeclName());
Douglas Gregor5545e162009-03-24 00:38:23 +00001525 if (ResultType.isNull())
1526 return QualType();
1527
Jay Foadbeaaccd2009-05-21 09:52:38 +00001528 return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
Douglas Gregor5545e162009-03-24 00:38:23 +00001529 Proto->isVariadic(), Proto->getTypeQuals(),
1530 D->getLocation(), D->getDeclName());
1531}
1532
Mike Stump1eb44332009-09-09 15:08:12 +00001533/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00001534/// declaration (New) from the corresponding fields of its template (Tmpl).
1535///
1536/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001537bool
1538TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00001539 FunctionDecl *Tmpl) {
1540 if (Tmpl->isDeleted())
1541 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00001542
Douglas Gregorcca9e962009-07-01 22:01:06 +00001543 // If we are performing substituting explicitly-specified template arguments
1544 // or deduced template arguments into a function template and we reach this
1545 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00001546 // to keeping the new function template specialization. We therefore
1547 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00001548 // into a template instantiation for this specific function template
1549 // specialization, which is not a SFINAE context, so that we diagnose any
1550 // further errors in the declaration itself.
1551 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1552 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1553 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1554 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00001555 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00001556 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001557 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00001558 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00001559 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001560 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1561 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
Douglas Gregorf35f8282009-11-11 21:54:23 +00001562 --SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001563 }
1564 }
Mike Stump1eb44332009-09-09 15:08:12 +00001565
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001566 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
1567 assert(Proto && "Function template without prototype?");
1568
1569 if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
1570 Proto->getNoReturnAttr()) {
1571 // The function has an exception specification or a "noreturn"
1572 // attribute. Substitute into each of the exception types.
1573 llvm::SmallVector<QualType, 4> Exceptions;
1574 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
1575 // FIXME: Poor location information!
1576 QualType T
1577 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
1578 New->getLocation(), New->getDeclName());
1579 if (T.isNull() ||
1580 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
1581 continue;
1582
1583 Exceptions.push_back(T);
1584 }
1585
1586 // Rebuild the function type
1587
1588 const FunctionProtoType *NewProto
1589 = New->getType()->getAs<FunctionProtoType>();
1590 assert(NewProto && "Template instantiation without function prototype?");
1591 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
1592 NewProto->arg_type_begin(),
1593 NewProto->getNumArgs(),
1594 NewProto->isVariadic(),
1595 NewProto->getTypeQuals(),
1596 Proto->hasExceptionSpec(),
1597 Proto->hasAnyExceptionSpec(),
1598 Exceptions.size(),
1599 Exceptions.data(),
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001600 Proto->getNoReturnAttr(),
1601 Proto->getCallConv()));
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001602 }
1603
Douglas Gregore53060f2009-06-25 22:08:12 +00001604 return false;
1605}
1606
Douglas Gregor5545e162009-03-24 00:38:23 +00001607/// \brief Initializes common fields of an instantiated method
1608/// declaration (New) from the corresponding fields of its template
1609/// (Tmpl).
1610///
1611/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001612bool
1613TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00001614 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001615 if (InitFunctionInstantiation(New, Tmpl))
1616 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001617
Douglas Gregor5545e162009-03-24 00:38:23 +00001618 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
1619 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00001620 if (Tmpl->isVirtualAsWritten())
1621 Record->setMethodAsVirtual(New);
Douglas Gregor5545e162009-03-24 00:38:23 +00001622
1623 // FIXME: attributes
1624 // FIXME: New needs a pointer to Tmpl
1625 return false;
1626}
Douglas Gregora58861f2009-05-13 20:28:22 +00001627
1628/// \brief Instantiate the definition of the given function from its
1629/// template.
1630///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001631/// \param PointOfInstantiation the point at which the instantiation was
1632/// required. Note that this is not precisely a "point of instantiation"
1633/// for the function, but it's close.
1634///
Douglas Gregora58861f2009-05-13 20:28:22 +00001635/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001636/// function template specialization or member function of a class template
1637/// specialization.
1638///
1639/// \param Recursive if true, recursively instantiates any functions that
1640/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001641///
1642/// \param DefinitionRequired if true, then we are performing an explicit
1643/// instantiation where the body of the function is required. Complain if
1644/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001645void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001646 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001647 bool Recursive,
1648 bool DefinitionRequired) {
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001649 if (Function->isInvalidDecl())
1650 return;
1651
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001652 assert(!Function->getBody() && "Already instantiated!");
Mike Stump1eb44332009-09-09 15:08:12 +00001653
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001654 // Never instantiate an explicit specialization.
1655 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1656 return;
1657
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001658 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00001659 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001660 Stmt *Pattern = 0;
1661 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001662 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001663
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001664 if (!Pattern) {
1665 if (DefinitionRequired) {
1666 if (Function->getPrimaryTemplate())
1667 Diag(PointOfInstantiation,
1668 diag::err_explicit_instantiation_undefined_func_template)
1669 << Function->getPrimaryTemplate();
1670 else
1671 Diag(PointOfInstantiation,
1672 diag::err_explicit_instantiation_undefined_member)
1673 << 1 << Function->getDeclName() << Function->getDeclContext();
1674
1675 if (PatternDecl)
1676 Diag(PatternDecl->getLocation(),
1677 diag::note_explicit_instantiation_here);
1678 }
1679
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001680 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001681 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001682
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001683 // C++0x [temp.explicit]p9:
1684 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00001685 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001686 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00001687 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001688 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001689 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001690 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001691
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001692 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
1693 if (Inst)
1694 return;
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001695
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001696 // If we're performing recursive template instantiation, create our own
1697 // queue of pending implicit instantiations that we will instantiate later,
1698 // while we're still within our own instantiation context.
1699 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1700 if (Recursive)
1701 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001702
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001703 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
1704
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001705 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00001706 // recorded, unless we're actually a member function within a local
1707 // class, in which case we need to merge our results with the parent
1708 // scope (of the enclosing function).
1709 bool MergeWithParentScope = false;
1710 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
1711 MergeWithParentScope = Rec->isLocalClass();
1712
1713 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001714
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001715 // Introduce the instantiated function parameters into the local
1716 // instantiation scope.
1717 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
1718 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
1719 Function->getParamDecl(I));
1720
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001721 // Enter the scope of this instantiation. We don't use
1722 // PushDeclContext because we don't have a scope.
1723 DeclContext *PreviousContext = CurContext;
1724 CurContext = Function;
1725
Mike Stump1eb44332009-09-09 15:08:12 +00001726 MultiLevelTemplateArgumentList TemplateArgs =
Anders Carlsson09025312009-08-29 05:16:22 +00001727 getTemplateInstantiationArgs(Function);
1728
1729 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001730 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00001731 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
1732 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
1733 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001734 }
1735
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001736 // Instantiate the function body.
Anders Carlsson09025312009-08-29 05:16:22 +00001737 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001738
Douglas Gregor52604ab2009-09-11 21:19:12 +00001739 if (Body.isInvalid())
1740 Function->setInvalidDecl();
1741
Mike Stump1eb44332009-09-09 15:08:12 +00001742 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001743 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001744
1745 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00001746
1747 DeclGroupRef DG(Function);
1748 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001749
Douglas Gregor60406be2010-01-16 22:29:39 +00001750 // This class may have local implicit instantiations that need to be
1751 // instantiation within this scope.
1752 PerformPendingImplicitInstantiations(/*LocalOnly=*/true);
1753 Scope.Exit();
1754
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001755 if (Recursive) {
1756 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001757 // instantiation of this template.
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001758 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001759
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001760 // Restore the set of pending implicit instantiations.
1761 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1762 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001763}
1764
1765/// \brief Instantiate the definition of the given variable from its
1766/// template.
1767///
Douglas Gregor7caa6822009-07-24 20:34:43 +00001768/// \param PointOfInstantiation the point at which the instantiation was
1769/// required. Note that this is not precisely a "point of instantiation"
1770/// for the function, but it's close.
1771///
1772/// \param Var the already-instantiated declaration of a static member
1773/// variable of a class template specialization.
1774///
1775/// \param Recursive if true, recursively instantiates any functions that
1776/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001777///
1778/// \param DefinitionRequired if true, then we are performing an explicit
1779/// instantiation where an out-of-line definition of the member variable
1780/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001781void Sema::InstantiateStaticDataMemberDefinition(
1782 SourceLocation PointOfInstantiation,
1783 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001784 bool Recursive,
1785 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001786 if (Var->isInvalidDecl())
1787 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001788
Douglas Gregor7caa6822009-07-24 20:34:43 +00001789 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001790 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00001791 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00001792 assert(Def->isStaticDataMember() && "Not a static data member?");
1793 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00001794
Douglas Gregor0d035142009-10-27 18:42:08 +00001795 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001796 // We did not find an out-of-line definition of this static data member,
1797 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00001798 // instantiate this definition (or provide a specialization for it) in
1799 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001800 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001801 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001802 Diag(PointOfInstantiation,
1803 diag::err_explicit_instantiation_undefined_member)
1804 << 2 << Var->getDeclName() << Var->getDeclContext();
1805 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
1806 }
1807
Douglas Gregor7caa6822009-07-24 20:34:43 +00001808 return;
1809 }
1810
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001811 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001812 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001813 return;
1814
1815 // C++0x [temp.explicit]p9:
1816 // Except for inline functions, other explicit instantiation declarations
1817 // have the effect of suppressing the implicit instantiation of the entity
1818 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001819 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001820 == TSK_ExplicitInstantiationDeclaration)
1821 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001822
Douglas Gregor7caa6822009-07-24 20:34:43 +00001823 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
1824 if (Inst)
1825 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001826
Douglas Gregor7caa6822009-07-24 20:34:43 +00001827 // If we're performing recursive template instantiation, create our own
1828 // queue of pending implicit instantiations that we will instantiate later,
1829 // while we're still within our own instantiation context.
1830 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1831 if (Recursive)
1832 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001833
Douglas Gregor7caa6822009-07-24 20:34:43 +00001834 // Enter the scope of this instantiation. We don't use
1835 // PushDeclContext because we don't have a scope.
1836 DeclContext *PreviousContext = CurContext;
1837 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001838
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001839 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00001840 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00001841 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00001842 CurContext = PreviousContext;
1843
1844 if (Var) {
Douglas Gregor583f33b2009-10-15 18:07:02 +00001845 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
1846 assert(MSInfo && "Missing member specialization information?");
1847 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
1848 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00001849 DeclGroupRef DG(Var);
1850 Consumer.HandleTopLevelDecl(DG);
1851 }
Mike Stump1eb44332009-09-09 15:08:12 +00001852
Douglas Gregor7caa6822009-07-24 20:34:43 +00001853 if (Recursive) {
1854 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001855 // instantiation of this template.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001856 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001857
Douglas Gregor7caa6822009-07-24 20:34:43 +00001858 // Restore the set of pending implicit instantiations.
1859 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001860 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001861}
Douglas Gregor815215d2009-05-27 05:35:12 +00001862
Anders Carlsson09025312009-08-29 05:16:22 +00001863void
1864Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
1865 const CXXConstructorDecl *Tmpl,
1866 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001867
Anders Carlsson09025312009-08-29 05:16:22 +00001868 llvm::SmallVector<MemInitTy*, 4> NewInits;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001869 bool AnyErrors = false;
1870
Anders Carlsson09025312009-08-29 05:16:22 +00001871 // Instantiate all the initializers.
1872 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00001873 InitsEnd = Tmpl->init_end();
1874 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00001875 CXXBaseOrMemberInitializer *Init = *Inits;
1876
1877 ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001878 llvm::SmallVector<SourceLocation, 4> CommaLocs;
Mike Stump1eb44332009-09-09 15:08:12 +00001879
Anders Carlsson09025312009-08-29 05:16:22 +00001880 // Instantiate all the arguments.
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001881 Expr *InitE = Init->getInit();
1882 if (!InitE) {
1883 // Nothing to instantiate;
1884 } else if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(InitE)) {
1885 if (InstantiateInitializationArguments(*this, ParenList->getExprs(),
1886 ParenList->getNumExprs(),
1887 TemplateArgs, CommaLocs,
1888 NewArgs)) {
1889 AnyErrors = true;
1890 continue;
1891 }
1892 } else {
1893 OwningExprResult InitArg = SubstExpr(InitE, TemplateArgs);
1894 if (InitArg.isInvalid()) {
1895 AnyErrors = true;
1896 continue;
1897 }
1898
1899 NewArgs.push_back(InitArg.release());
Anders Carlsson09025312009-08-29 05:16:22 +00001900 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001901
Anders Carlsson09025312009-08-29 05:16:22 +00001902 MemInitResult NewInit;
Anders Carlsson09025312009-08-29 05:16:22 +00001903 if (Init->isBaseInitializer()) {
John McCalla93c9342009-12-07 02:54:59 +00001904 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
Douglas Gregor802ab452009-12-02 22:36:29 +00001905 TemplateArgs,
1906 Init->getSourceLocation(),
1907 New->getDeclName());
John McCalla93c9342009-12-07 02:54:59 +00001908 if (!BaseTInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001909 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00001910 New->setInvalidDecl();
1911 continue;
1912 }
1913
John McCalla93c9342009-12-07 02:54:59 +00001914 NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001915 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001916 NewArgs.size(),
Douglas Gregor802ab452009-12-02 22:36:29 +00001917 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00001918 Init->getRParenLoc(),
1919 New->getParent());
1920 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001921 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00001922
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001923 // Is this an anonymous union?
1924 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregore95b4092009-09-16 18:34:49 +00001925 Member = cast<FieldDecl>(FindInstantiatedDecl(UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001926 else
Douglas Gregore95b4092009-09-16 18:34:49 +00001927 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMember(),
1928 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00001929
1930 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001931 NewArgs.size(),
1932 Init->getSourceLocation(),
Douglas Gregor802ab452009-12-02 22:36:29 +00001933 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00001934 Init->getRParenLoc());
1935 }
1936
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001937 if (NewInit.isInvalid()) {
1938 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00001939 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001940 } else {
Anders Carlsson09025312009-08-29 05:16:22 +00001941 // FIXME: It would be nice if ASTOwningVector had a release function.
1942 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001943
Anders Carlsson09025312009-08-29 05:16:22 +00001944 NewInits.push_back((MemInitTy *)NewInit.get());
1945 }
1946 }
Mike Stump1eb44332009-09-09 15:08:12 +00001947
Anders Carlsson09025312009-08-29 05:16:22 +00001948 // Assign all the initializers to the new constructor.
Mike Stump1eb44332009-09-09 15:08:12 +00001949 ActOnMemInitializers(DeclPtrTy::make(New),
Anders Carlsson09025312009-08-29 05:16:22 +00001950 /*FIXME: ColonLoc */
1951 SourceLocation(),
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001952 NewInits.data(), NewInits.size(),
1953 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00001954}
1955
John McCall52a575a2009-08-29 08:11:13 +00001956// TODO: this could be templated if the various decl types used the
1957// same method name.
1958static bool isInstantiationOf(ClassTemplateDecl *Pattern,
1959 ClassTemplateDecl *Instance) {
1960 Pattern = Pattern->getCanonicalDecl();
1961
1962 do {
1963 Instance = Instance->getCanonicalDecl();
1964 if (Pattern == Instance) return true;
1965 Instance = Instance->getInstantiatedFromMemberTemplate();
1966 } while (Instance);
1967
1968 return false;
1969}
1970
Douglas Gregor0d696532009-09-28 06:34:35 +00001971static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
1972 FunctionTemplateDecl *Instance) {
1973 Pattern = Pattern->getCanonicalDecl();
1974
1975 do {
1976 Instance = Instance->getCanonicalDecl();
1977 if (Pattern == Instance) return true;
1978 Instance = Instance->getInstantiatedFromMemberTemplate();
1979 } while (Instance);
1980
1981 return false;
1982}
1983
Douglas Gregored9c0f92009-10-29 00:04:11 +00001984static bool
1985isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
1986 ClassTemplatePartialSpecializationDecl *Instance) {
1987 Pattern
1988 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
1989 do {
1990 Instance = cast<ClassTemplatePartialSpecializationDecl>(
1991 Instance->getCanonicalDecl());
1992 if (Pattern == Instance)
1993 return true;
1994 Instance = Instance->getInstantiatedFromMember();
1995 } while (Instance);
1996
1997 return false;
1998}
1999
John McCall52a575a2009-08-29 08:11:13 +00002000static bool isInstantiationOf(CXXRecordDecl *Pattern,
2001 CXXRecordDecl *Instance) {
2002 Pattern = Pattern->getCanonicalDecl();
2003
2004 do {
2005 Instance = Instance->getCanonicalDecl();
2006 if (Pattern == Instance) return true;
2007 Instance = Instance->getInstantiatedFromMemberClass();
2008 } while (Instance);
2009
2010 return false;
2011}
2012
2013static bool isInstantiationOf(FunctionDecl *Pattern,
2014 FunctionDecl *Instance) {
2015 Pattern = Pattern->getCanonicalDecl();
2016
2017 do {
2018 Instance = Instance->getCanonicalDecl();
2019 if (Pattern == Instance) return true;
2020 Instance = Instance->getInstantiatedFromMemberFunction();
2021 } while (Instance);
2022
2023 return false;
2024}
2025
2026static bool isInstantiationOf(EnumDecl *Pattern,
2027 EnumDecl *Instance) {
2028 Pattern = Pattern->getCanonicalDecl();
2029
2030 do {
2031 Instance = Instance->getCanonicalDecl();
2032 if (Pattern == Instance) return true;
2033 Instance = Instance->getInstantiatedFromMemberEnum();
2034 } while (Instance);
2035
2036 return false;
2037}
2038
John McCalled976492009-12-04 22:46:56 +00002039static bool isInstantiationOf(UsingShadowDecl *Pattern,
2040 UsingShadowDecl *Instance,
2041 ASTContext &C) {
2042 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2043}
2044
2045static bool isInstantiationOf(UsingDecl *Pattern,
2046 UsingDecl *Instance,
2047 ASTContext &C) {
2048 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2049}
2050
John McCall7ba107a2009-11-18 02:36:19 +00002051static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
2052 UsingDecl *Instance,
2053 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002054 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00002055}
2056
2057static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00002058 UsingDecl *Instance,
2059 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002060 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00002061}
2062
John McCall52a575a2009-08-29 08:11:13 +00002063static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
2064 VarDecl *Instance) {
2065 assert(Instance->isStaticDataMember());
2066
2067 Pattern = Pattern->getCanonicalDecl();
2068
2069 do {
2070 Instance = Instance->getCanonicalDecl();
2071 if (Pattern == Instance) return true;
2072 Instance = Instance->getInstantiatedFromStaticDataMember();
2073 } while (Instance);
2074
2075 return false;
2076}
2077
John McCalled976492009-12-04 22:46:56 +00002078// Other is the prospective instantiation
2079// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00002080static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002081 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00002082 if (UnresolvedUsingTypenameDecl *UUD
2083 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
2084 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2085 return isInstantiationOf(UUD, UD, Ctx);
2086 }
2087 }
2088
2089 if (UnresolvedUsingValueDecl *UUD
2090 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002091 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2092 return isInstantiationOf(UUD, UD, Ctx);
2093 }
2094 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002095
Anders Carlsson0d8df782009-08-29 19:37:28 +00002096 return false;
2097 }
Mike Stump1eb44332009-09-09 15:08:12 +00002098
John McCall52a575a2009-08-29 08:11:13 +00002099 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
2100 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002101
John McCall52a575a2009-08-29 08:11:13 +00002102 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
2103 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00002104
John McCall52a575a2009-08-29 08:11:13 +00002105 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
2106 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00002107
Douglas Gregor7caa6822009-07-24 20:34:43 +00002108 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00002109 if (Var->isStaticDataMember())
2110 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
2111
2112 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
2113 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00002114
Douglas Gregor0d696532009-09-28 06:34:35 +00002115 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
2116 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
2117
Douglas Gregored9c0f92009-10-29 00:04:11 +00002118 if (ClassTemplatePartialSpecializationDecl *PartialSpec
2119 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2120 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2121 PartialSpec);
2122
Anders Carlssond8b285f2009-09-01 04:26:58 +00002123 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2124 if (!Field->getDeclName()) {
2125 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00002126 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00002127 cast<FieldDecl>(D);
2128 }
2129 }
Mike Stump1eb44332009-09-09 15:08:12 +00002130
John McCalled976492009-12-04 22:46:56 +00002131 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2132 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2133
2134 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2135 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2136
Douglas Gregor815215d2009-05-27 05:35:12 +00002137 return D->getDeclName() && isa<NamedDecl>(Other) &&
2138 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2139}
2140
2141template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00002142static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00002143 NamedDecl *D,
2144 ForwardIterator first,
2145 ForwardIterator last) {
2146 for (; first != last; ++first)
2147 if (isInstantiationOf(Ctx, D, *first))
2148 return cast<NamedDecl>(*first);
2149
2150 return 0;
2151}
2152
John McCall02cace72009-08-28 07:59:38 +00002153/// \brief Finds the instantiation of the given declaration context
2154/// within the current instantiation.
2155///
2156/// \returns NULL if there was an error
Douglas Gregore95b4092009-09-16 18:34:49 +00002157DeclContext *Sema::FindInstantiatedContext(DeclContext* DC,
2158 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00002159 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00002160 Decl* ID = FindInstantiatedDecl(D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00002161 return cast_or_null<DeclContext>(ID);
2162 } else return DC;
2163}
2164
Douglas Gregored961e72009-05-27 17:54:46 +00002165/// \brief Find the instantiation of the given declaration within the
2166/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00002167///
2168/// This routine is intended to be used when \p D is a declaration
2169/// referenced from within a template, that needs to mapped into the
2170/// corresponding declaration within an instantiation. For example,
2171/// given:
2172///
2173/// \code
2174/// template<typename T>
2175/// struct X {
2176/// enum Kind {
2177/// KnownValue = sizeof(T)
2178/// };
2179///
2180/// bool getKind() const { return KnownValue; }
2181/// };
2182///
2183/// template struct X<int>;
2184/// \endcode
2185///
2186/// In the instantiation of X<int>::getKind(), we need to map the
2187/// EnumConstantDecl for KnownValue (which refers to
2188/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00002189/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2190/// this mapping from within the instantiation of X<int>.
Douglas Gregore95b4092009-09-16 18:34:49 +00002191NamedDecl *Sema::FindInstantiatedDecl(NamedDecl *D,
2192 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00002193 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00002194 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00002195 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
Douglas Gregor550d9b22009-10-31 17:21:17 +00002196 ParentDC->isFunctionOrMethod()) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002197 // D is a local of some kind. Look into the map of local
2198 // declarations to their instantiations.
2199 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
2200 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002201
Douglas Gregore95b4092009-09-16 18:34:49 +00002202 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2203 if (!Record->isDependentContext())
2204 return D;
2205
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002206 // If the RecordDecl is actually the injected-class-name or a
2207 // "templated" declaration for a class template, class template
2208 // partial specialization, or a member class of a class template,
2209 // substitute into the injected-class-name of the class template
2210 // or partial specialization to find the new DeclContext.
Douglas Gregore95b4092009-09-16 18:34:49 +00002211 QualType T;
2212 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2213
2214 if (ClassTemplate) {
2215 T = ClassTemplate->getInjectedClassNameType(Context);
2216 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2217 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
2218 T = Context.getTypeDeclType(Record);
2219 ClassTemplate = PartialSpec->getSpecializedTemplate();
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002220 }
Douglas Gregore95b4092009-09-16 18:34:49 +00002221
2222 if (!T.isNull()) {
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002223 // Substitute into the injected-class-name to get the type
2224 // corresponding to the instantiation we want, which may also be
2225 // the current instantiation (if we're in a template
2226 // definition). This substitution should never fail, since we
2227 // know we can instantiate the injected-class-name or we
2228 // wouldn't have gotten to the injected-class-name!
2229
2230 // FIXME: Can we use the CurrentInstantiationScope to avoid this
2231 // extra instantiation in the common case?
Douglas Gregore95b4092009-09-16 18:34:49 +00002232 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2233 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2234
2235 if (!T->isDependentType()) {
2236 assert(T->isRecordType() && "Instantiation must produce a record type");
2237 return T->getAs<RecordType>()->getDecl();
2238 }
2239
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002240 // We are performing "partial" template instantiation to create
2241 // the member declarations for the members of a class template
2242 // specialization. Therefore, D is actually referring to something
2243 // in the current instantiation. Look through the current
2244 // context, which contains actual instantiations, to find the
2245 // instantiation of the "current instantiation" that D refers
2246 // to.
2247 bool SawNonDependentContext = false;
Mike Stump1eb44332009-09-09 15:08:12 +00002248 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00002249 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002250 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002251 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00002252 if (isInstantiationOf(ClassTemplate,
2253 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00002254 return Spec;
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002255
2256 if (!DC->isDependentContext())
2257 SawNonDependentContext = true;
John McCall52a575a2009-08-29 08:11:13 +00002258 }
2259
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002260 // We're performing "instantiation" of a member of the current
2261 // instantiation while we are type-checking the
2262 // definition. Compute the declaration context and return that.
2263 assert(!SawNonDependentContext &&
2264 "No dependent context while instantiating record");
2265 DeclContext *DC = computeDeclContext(T);
2266 assert(DC &&
John McCall52a575a2009-08-29 08:11:13 +00002267 "Unable to find declaration for the current instantiation");
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002268 return cast<CXXRecordDecl>(DC);
John McCall52a575a2009-08-29 08:11:13 +00002269 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002270
Douglas Gregore95b4092009-09-16 18:34:49 +00002271 // Fall through to deal with other dependent record types (e.g.,
2272 // anonymous unions in class templates).
2273 }
John McCall52a575a2009-08-29 08:11:13 +00002274
Douglas Gregore95b4092009-09-16 18:34:49 +00002275 if (!ParentDC->isDependentContext())
2276 return D;
2277
2278 ParentDC = FindInstantiatedContext(ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002279 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00002280 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002281
Douglas Gregor815215d2009-05-27 05:35:12 +00002282 if (ParentDC != D->getDeclContext()) {
2283 // We performed some kind of instantiation in the parent context,
2284 // so now we need to look into the instantiated parent context to
2285 // find the instantiation of the declaration D.
2286 NamedDecl *Result = 0;
2287 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002288 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00002289 Result = findInstantiationOf(Context, D, Found.first, Found.second);
2290 } else {
2291 // Since we don't have a name for the entity we're looking for,
2292 // our only option is to walk through all of the declarations to
2293 // find that name. This will occur in a few cases:
2294 //
2295 // - anonymous struct/union within a template
2296 // - unnamed class/struct/union/enum within a template
2297 //
2298 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00002299 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002300 ParentDC->decls_begin(),
2301 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00002302 }
Mike Stump1eb44332009-09-09 15:08:12 +00002303
John McCall9f54ad42009-12-10 09:41:52 +00002304 // UsingShadowDecls can instantiate to nothing because of using hiding.
2305 assert((Result || isa<UsingShadowDecl>(D))
2306 && "Unable to find instantiation of declaration!");
2307
Douglas Gregor815215d2009-05-27 05:35:12 +00002308 D = Result;
2309 }
2310
Douglas Gregor815215d2009-05-27 05:35:12 +00002311 return D;
2312}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002313
Mike Stump1eb44332009-09-09 15:08:12 +00002314/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002315/// instantiations we have seen until this point.
Douglas Gregor60406be2010-01-16 22:29:39 +00002316void Sema::PerformPendingImplicitInstantiations(bool LocalOnly) {
2317 while (!PendingLocalImplicitInstantiations.empty() ||
2318 (!LocalOnly && !PendingImplicitInstantiations.empty())) {
2319 PendingImplicitInstantiation Inst;
2320
2321 if (PendingLocalImplicitInstantiations.empty()) {
2322 Inst = PendingImplicitInstantiations.front();
2323 PendingImplicitInstantiations.pop_front();
2324 } else {
2325 Inst = PendingLocalImplicitInstantiations.front();
2326 PendingLocalImplicitInstantiations.pop_front();
2327 }
Mike Stump1eb44332009-09-09 15:08:12 +00002328
Douglas Gregor7caa6822009-07-24 20:34:43 +00002329 // Instantiate function definitions
2330 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002331 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002332 Function->getLocation(), *this,
2333 Context.getSourceManager(),
2334 "instantiating function definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002335
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00002336 if (!Function->getBody())
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002337 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00002338 continue;
2339 }
Mike Stump1eb44332009-09-09 15:08:12 +00002340
Douglas Gregor7caa6822009-07-24 20:34:43 +00002341 // Instantiate static data member definitions.
2342 VarDecl *Var = cast<VarDecl>(Inst.first);
2343 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002344
Chandler Carruth291b4412010-02-13 10:17:50 +00002345 // Don't try to instantiate declarations if the most recent redeclaration
2346 // is invalid.
2347 if (Var->getMostRecentDeclaration()->isInvalidDecl())
2348 continue;
2349
2350 // Check if the most recent declaration has changed the specialization kind
2351 // and removed the need for implicit instantiation.
2352 switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
2353 case TSK_Undeclared:
2354 assert(false && "Cannot instantitiate an undeclared specialization.");
2355 case TSK_ExplicitInstantiationDeclaration:
2356 case TSK_ExplicitInstantiationDefinition:
2357 case TSK_ExplicitSpecialization:
2358 continue; // No longer need implicit instantiation.
2359 case TSK_ImplicitInstantiation:
2360 break;
2361 }
2362
Mike Stump1eb44332009-09-09 15:08:12 +00002363 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002364 Var->getLocation(), *this,
2365 Context.getSourceManager(),
2366 "instantiating static data member "
2367 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002368
Douglas Gregor7caa6822009-07-24 20:34:43 +00002369 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002370 }
2371}