blob: d7820bbc2e66ddf17a1500e0c1a5cacd11ef1317 [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);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000046 Decl *VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +000047 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000048 Decl *VisitFieldDecl(FieldDecl *D);
49 Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
50 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor6477b692009-03-25 15:04:13 +000051 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
John McCall02cace72009-08-28 07:59:38 +000052 Decl *VisitFriendDecl(FriendDecl *D);
Douglas Gregora735b202009-10-13 14:39:41 +000053 Decl *VisitFunctionDecl(FunctionDecl *D,
54 TemplateParameterList *TemplateParams = 0);
Douglas Gregord475b8d2009-03-25 21:17:03 +000055 Decl *VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregord60e1052009-08-27 16:57:43 +000056 Decl *VisitCXXMethodDecl(CXXMethodDecl *D,
57 TemplateParameterList *TemplateParams = 0);
Douglas Gregor615c5d42009-03-24 16:43:20 +000058 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
Douglas Gregor03b2b072009-03-24 00:15:49 +000059 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +000060 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor6477b692009-03-25 15:04:13 +000061 ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D);
John McCalle29ba202009-08-20 01:44:21 +000062 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregor7974c3b2009-10-07 17:21:34 +000063 Decl *VisitClassTemplatePartialSpecializationDecl(
64 ClassTemplatePartialSpecializationDecl *D);
Douglas Gregord60e1052009-08-27 16:57:43 +000065 Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
John McCalle29ba202009-08-20 01:44:21 +000066 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Douglas Gregor33642df2009-10-23 23:25:44 +000067 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
Douglas Gregor9106ef72009-11-11 16:58:32 +000068 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
Douglas Gregor48c32a72009-11-17 06:07:40 +000069 Decl *VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
John McCalled976492009-12-04 22:46:56 +000070 Decl *VisitUsingDecl(UsingDecl *D);
71 Decl *VisitUsingShadowDecl(UsingShadowDecl *D);
John McCall7ba107a2009-11-18 02:36:19 +000072 Decl *VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
73 Decl *VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Mike Stump1eb44332009-09-09 15:08:12 +000074
Douglas Gregor8dbc2692009-03-17 21:15:40 +000075 // Base case. FIXME: Remove once we can instantiate everything.
Douglas Gregor48c32a72009-11-17 06:07:40 +000076 Decl *VisitDecl(Decl *D) {
77 unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID(
78 Diagnostic::Error,
79 "cannot instantiate %0 yet");
80 SemaRef.Diag(D->getLocation(), DiagID)
81 << D->getDeclKindName();
82
Douglas Gregor8dbc2692009-03-17 21:15:40 +000083 return 0;
84 }
Douglas Gregor5545e162009-03-24 00:38:23 +000085
John McCallfd810b12009-08-14 02:03:10 +000086 const LangOptions &getLangOptions() {
87 return SemaRef.getLangOptions();
88 }
89
Douglas Gregor5545e162009-03-24 00:38:23 +000090 // Helper functions for instantiating methods.
John McCallce3ff2b2009-08-25 22:02:44 +000091 QualType SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +000092 llvm::SmallVectorImpl<ParmVarDecl *> &Params);
Douglas Gregore53060f2009-06-25 22:08:12 +000093 bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
Douglas Gregor5545e162009-03-24 00:38:23 +000094 bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
John McCalle29ba202009-08-20 01:44:21 +000095
96 TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +000097 SubstTemplateParams(TemplateParameterList *List);
Douglas Gregored9c0f92009-10-29 00:04:11 +000098
99 bool InstantiateClassTemplatePartialSpecialization(
100 ClassTemplateDecl *ClassTemplate,
101 ClassTemplatePartialSpecializationDecl *PartialSpec);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000102 };
103}
104
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000105// FIXME: Is this too simple?
106void TemplateDeclInstantiator::InstantiateAttrs(Decl *Tmpl, Decl *New) {
107 for (const Attr *TmplAttr = Tmpl->getAttrs(); TmplAttr;
108 TmplAttr = TmplAttr->getNext()) {
109
110 // FIXME: Is cloning correct for all attributes?
111 Attr *NewAttr = TmplAttr->clone(SemaRef.Context);
112
113 New->addAttr(NewAttr);
114 }
115}
116
Douglas Gregor4f722be2009-03-25 15:45:12 +0000117Decl *
118TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
119 assert(false && "Translation units cannot be instantiated");
120 return D;
121}
122
123Decl *
124TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
125 assert(false && "Namespaces cannot be instantiated");
126 return D;
127}
128
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000129Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
130 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000131 TypeSourceInfo *DI = D->getTypeSourceInfo();
John McCallba6a9bd2009-10-24 08:00:42 +0000132 if (DI->getType()->isDependentType()) {
133 DI = SemaRef.SubstType(DI, TemplateArgs,
134 D->getLocation(), D->getDeclName());
135 if (!DI) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000136 Invalid = true;
John McCalla93c9342009-12-07 02:54:59 +0000137 DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000138 }
139 }
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000141 // Create the new typedef
142 TypedefDecl *Typedef
143 = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
John McCallba6a9bd2009-10-24 08:00:42 +0000144 D->getIdentifier(), DI);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000145 if (Invalid)
146 Typedef->setInvalidDecl();
147
John McCall5126fd02009-12-30 00:31:22 +0000148 if (TypedefDecl *Prev = D->getPreviousDeclaration()) {
149 NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(Prev, TemplateArgs);
150 Typedef->setPreviousDeclaration(cast<TypedefDecl>(InstPrev));
151 }
152
John McCall46460a62010-01-20 21:53:11 +0000153 Typedef->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000154 Owner->addDecl(Typedef);
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000156 return Typedef;
157}
158
Douglas Gregor6eef5192009-12-14 19:27:10 +0000159/// \brief Instantiate the arguments provided as part of initialization.
160///
161/// \returns true if an error occurred, false otherwise.
162static bool InstantiateInitializationArguments(Sema &SemaRef,
163 Expr **Args, unsigned NumArgs,
164 const MultiLevelTemplateArgumentList &TemplateArgs,
165 llvm::SmallVectorImpl<SourceLocation> &FakeCommaLocs,
166 ASTOwningVector<&ActionBase::DeleteExpr> &InitArgs) {
167 for (unsigned I = 0; I != NumArgs; ++I) {
168 // When we hit the first defaulted argument, break out of the loop:
169 // we don't pass those default arguments on.
170 if (Args[I]->isDefaultArgument())
171 break;
172
173 Sema::OwningExprResult Arg = SemaRef.SubstExpr(Args[I], TemplateArgs);
174 if (Arg.isInvalid())
175 return true;
176
177 Expr *ArgExpr = (Expr *)Arg.get();
178 InitArgs.push_back(Arg.release());
179
180 // FIXME: We're faking all of the comma locations. Do we need them?
181 FakeCommaLocs.push_back(
182 SemaRef.PP.getLocForEndOfToken(ArgExpr->getLocEnd()));
183 }
184
185 return false;
186}
187
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000188Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000189 // Do substitution on the type of the declaration
John McCalla93c9342009-12-07 02:54:59 +0000190 TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
John McCall0a5fa062009-10-21 02:39:02 +0000191 TemplateArgs,
192 D->getTypeSpecStartLoc(),
193 D->getDeclName());
194 if (!DI)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000195 return 0;
196
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000197 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000198 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
199 D->getLocation(), D->getIdentifier(),
John McCall0a5fa062009-10-21 02:39:02 +0000200 DI->getType(), DI,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000201 D->getStorageClass());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000202 Var->setThreadSpecified(D->isThreadSpecified());
203 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
204 Var->setDeclaredInCondition(D->isDeclaredInCondition());
Mike Stump1eb44332009-09-09 15:08:12 +0000205
206 // If we are instantiating a static data member defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000207 // out-of-line, the instantiation will have the same lexical
208 // context (which will be a namespace scope) as the template.
209 if (D->isOutOfLine())
210 Var->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000211
John McCall46460a62010-01-20 21:53:11 +0000212 Var->setAccess(D->getAccess());
213
Mike Stump390b4cc2009-05-16 07:39:55 +0000214 // FIXME: In theory, we could have a previous declaration for variables that
215 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000216 bool Redeclaration = false;
John McCall68263142009-11-18 22:49:29 +0000217 // FIXME: having to fake up a LookupResult is dumb.
218 LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
219 Sema::LookupOrdinaryName);
220 SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Douglas Gregor7caa6822009-07-24 20:34:43 +0000222 if (D->isOutOfLine()) {
223 D->getLexicalDeclContext()->addDecl(Var);
224 Owner->makeDeclVisibleInContext(Var);
225 } else {
226 Owner->addDecl(Var);
227 }
Mike Stump1eb44332009-09-09 15:08:12 +0000228
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000229 // Link instantiations of static data members back to the template from
230 // which they were instantiated.
231 if (Var->isStaticDataMember())
232 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000233 TSK_ImplicitInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000234
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000235 if (D->getInit()) {
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000236 if (Var->isStaticDataMember() && !D->isOutOfLine())
237 SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
238 else
239 SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
240
Douglas Gregor6eef5192009-12-14 19:27:10 +0000241 // Extract the initializer, skipping through any temporary-binding
242 // expressions and look at the subexpression as it was written.
243 Expr *DInit = D->getInit();
Douglas Gregora89ebea2010-02-03 09:01:59 +0000244 if (CXXExprWithTemporaries *ExprTemp
245 = dyn_cast<CXXExprWithTemporaries>(DInit))
246 DInit = ExprTemp->getSubExpr();
Douglas Gregor6eef5192009-12-14 19:27:10 +0000247 while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(DInit))
248 DInit = Binder->getSubExpr();
249 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(DInit))
250 DInit = ICE->getSubExprAsWritten();
251
252 if (ParenListExpr *PLE = dyn_cast<ParenListExpr>(DInit)) {
253 // The initializer is a parenthesized list of expressions that is
254 // type-dependent. Instantiate each of the expressions; we'll be
255 // performing direct initialization with them.
256 llvm::SmallVector<SourceLocation, 4> CommaLocs;
257 ASTOwningVector<&ActionBase::DeleteExpr> InitArgs(SemaRef);
258 if (!InstantiateInitializationArguments(SemaRef,
259 PLE->getExprs(),
260 PLE->getNumExprs(),
261 TemplateArgs,
262 CommaLocs, InitArgs)) {
263 // Add the direct initializer to the declaration.
Douglas Gregora88cfbf2009-12-12 18:16:41 +0000264 SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
Douglas Gregor6eef5192009-12-14 19:27:10 +0000265 PLE->getLParenLoc(),
266 move_arg(InitArgs),
267 CommaLocs.data(),
268 PLE->getRParenLoc());
269 }
270 } else if (CXXConstructExpr *Construct =dyn_cast<CXXConstructExpr>(DInit)) {
271 // The initializer resolved to a constructor. Instantiate the constructor
272 // arguments.
273 llvm::SmallVector<SourceLocation, 4> CommaLocs;
274 ASTOwningVector<&ActionBase::DeleteExpr> InitArgs(SemaRef);
275
276 if (!InstantiateInitializationArguments(SemaRef,
277 Construct->getArgs(),
278 Construct->getNumArgs(),
279 TemplateArgs,
280 CommaLocs, InitArgs)) {
281 if (D->hasCXXDirectInitializer()) {
282 SourceLocation FakeLParenLoc =
283 SemaRef.PP.getLocForEndOfToken(D->getLocation());
284 SourceLocation FakeRParenLoc = CommaLocs.empty()? FakeLParenLoc
285 : CommaLocs.back();
286 SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
287 FakeLParenLoc,
288 move_arg(InitArgs),
289 CommaLocs.data(),
290 FakeRParenLoc);
291 } else if (InitArgs.size() == 1) {
292 Expr *Init = (Expr*)(InitArgs.take()[0]);
293 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var),
294 SemaRef.Owned(Init),
295 false);
296 } else {
297 assert(InitArgs.size() == 0);
298 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
299 }
300 }
301 } else {
302 OwningExprResult Init
303 = SemaRef.SubstExpr(D->getInit(), TemplateArgs);
304
305 // FIXME: Not happy about invalidating decls just because of a bad
306 // initializer, unless it affects the type.
307 if (Init.isInvalid())
308 Var->setInvalidDecl();
309 else
310 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init),
311 D->hasCXXDirectInitializer());
312 }
313
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000314 SemaRef.PopExpressionEvaluationContext();
Douglas Gregor65b90052009-07-27 17:43:39 +0000315 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
316 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000317
318 return Var;
319}
320
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000321Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
322 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000323 TypeSourceInfo *DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000324 if (DI->getType()->isDependentType()) {
325 DI = SemaRef.SubstType(DI, TemplateArgs,
326 D->getLocation(), D->getDeclName());
327 if (!DI) {
John McCalla93c9342009-12-07 02:54:59 +0000328 DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000329 Invalid = true;
330 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000331 // C++ [temp.arg.type]p3:
332 // If a declaration acquires a function type through a type
333 // dependent on a template-parameter and this causes a
334 // declaration that does not use the syntactic form of a
335 // function declarator to have function type, the program is
336 // ill-formed.
337 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000338 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000339 Invalid = true;
340 }
341 }
342
343 Expr *BitWidth = D->getBitWidth();
344 if (Invalid)
345 BitWidth = 0;
346 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000347 // The bit-width expression is not potentially evaluated.
348 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000350 OwningExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000351 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000352 if (InstantiatedBitWidth.isInvalid()) {
353 Invalid = true;
354 BitWidth = 0;
355 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000356 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000357 }
358
John McCall07fb6be2009-10-22 23:33:21 +0000359 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
360 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000361 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000362 D->getLocation(),
363 D->isMutable(),
364 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000365 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000366 D->getAccess(),
367 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000368 if (!Field) {
369 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000370 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000371 }
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000373 InstantiateAttrs(D, Field);
374
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000375 if (Invalid)
376 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000378 if (!Field->getDeclName()) {
379 // Keep track of where this decl came from.
380 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000381 }
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000383 Field->setImplicit(D->isImplicit());
John McCall46460a62010-01-20 21:53:11 +0000384 Field->setAccess(D->getAccess());
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000385 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000386
387 return Field;
388}
389
John McCall02cace72009-08-28 07:59:38 +0000390Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
391 FriendDecl::FriendUnion FU;
392
393 // Handle friend type expressions by simply substituting template
394 // parameters into the pattern type.
395 if (Type *Ty = D->getFriendType()) {
396 QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs,
397 D->getLocation(), DeclarationName());
398 if (T.isNull()) return 0;
399
400 assert(getLangOptions().CPlusPlus0x || T->isRecordType());
401 FU = T.getTypePtr();
402
403 // Handle everything else by appropriate substitution.
404 } else {
405 NamedDecl *ND = D->getFriendDecl();
406 assert(ND && "friend decl must be a decl or a type!");
407
Douglas Gregora735b202009-10-13 14:39:41 +0000408 // FIXME: We have a problem here, because the nested call to Visit(ND)
409 // will inject the thing that the friend references into the current
410 // owner, which is wrong.
John McCalle129d442009-12-17 23:21:11 +0000411 Decl *NewND;
412
413 // Hack to make this work almost well pending a rewrite.
Douglas Gregor63644fa2010-02-07 10:31:35 +0000414 if (ND->getDeclContext()->isRecord()) {
415 if (!ND->getDeclContext()->isDependentContext()) {
416 NewND = SemaRef.FindInstantiatedDecl(ND, TemplateArgs);
417 } else {
418 // FIXME: Hack to avoid crashing when incorrectly trying to instantiate
419 // templated friend declarations. This doesn't produce a correct AST;
420 // however this is sufficient for some AST analysis. The real solution
421 // must be put in place during the pending rewrite. See PR5848.
422 return 0;
423 }
424 } else if (D->wasSpecialization()) {
Douglas Gregor7557a132009-12-24 20:56:24 +0000425 // Totally egregious hack to work around PR5866
426 return 0;
427 } else
John McCalle129d442009-12-17 23:21:11 +0000428 NewND = Visit(ND);
John McCall02cace72009-08-28 07:59:38 +0000429 if (!NewND) return 0;
430
431 FU = cast<NamedDecl>(NewND);
John McCallfd810b12009-08-14 02:03:10 +0000432 }
Mike Stump1eb44332009-09-09 15:08:12 +0000433
John McCall02cace72009-08-28 07:59:38 +0000434 FriendDecl *FD =
435 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU,
436 D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000437 FD->setAccess(AS_public);
John McCall02cace72009-08-28 07:59:38 +0000438 Owner->addDecl(FD);
439 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000440}
441
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000442Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
443 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Douglas Gregorac7610d2009-06-22 20:57:11 +0000445 // The expression in a static assertion is not potentially evaluated.
446 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000448 OwningExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000449 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000450 if (InstantiatedAssertExpr.isInvalid())
451 return 0;
452
Douglas Gregor43d9d922009-08-08 01:41:12 +0000453 OwningExprResult Message(SemaRef, D->getMessage());
454 D->getMessage()->Retain();
Mike Stump1eb44332009-09-09 15:08:12 +0000455 Decl *StaticAssert
456 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
Chris Lattnerb28317a2009-03-28 19:18:32 +0000457 move(InstantiatedAssertExpr),
458 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000459 return StaticAssert;
460}
461
462Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000463 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000464 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000465 D->getTagKeywordLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000466 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000467 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000468 Enum->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000469 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000470 Enum->startDefinition();
471
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000472 llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000473
474 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000475 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
476 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000477 EC != ECEnd; ++EC) {
478 // The specified value for the enumerator.
479 OwningExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000480 if (Expr *UninstValue = EC->getInitExpr()) {
481 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000482 EnterExpressionEvaluationContext Unevaluated(SemaRef,
Douglas Gregorac7610d2009-06-22 20:57:11 +0000483 Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000484
John McCallce3ff2b2009-08-25 22:02:44 +0000485 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000486 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000487
488 // Drop the initial value and continue.
489 bool isInvalid = false;
490 if (Value.isInvalid()) {
491 Value = SemaRef.Owned((Expr *)0);
492 isInvalid = true;
493 }
494
Mike Stump1eb44332009-09-09 15:08:12 +0000495 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000496 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
497 EC->getLocation(), EC->getIdentifier(),
498 move(Value));
499
500 if (isInvalid) {
501 if (EnumConst)
502 EnumConst->setInvalidDecl();
503 Enum->setInvalidDecl();
504 }
505
506 if (EnumConst) {
John McCall3b85ecf2010-01-23 22:37:59 +0000507 EnumConst->setAccess(Enum->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000508 Enum->addDecl(EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000509 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000510 LastEnumConst = EnumConst;
511 }
512 }
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000514 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000515 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000516 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
517 Sema::DeclPtrTy::make(Enum),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000518 &Enumerators[0], Enumerators.size(),
519 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000520
521 return Enum;
522}
523
Douglas Gregor6477b692009-03-25 15:04:13 +0000524Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
525 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
526 return 0;
527}
528
Douglas Gregored9c0f92009-10-29 00:04:11 +0000529namespace {
530 class SortDeclByLocation {
531 SourceManager &SourceMgr;
532
533 public:
534 explicit SortDeclByLocation(SourceManager &SourceMgr)
535 : SourceMgr(SourceMgr) { }
536
537 bool operator()(const Decl *X, const Decl *Y) const {
538 return SourceMgr.isBeforeInTranslationUnit(X->getLocation(),
539 Y->getLocation());
540 }
541 };
542}
543
John McCalle29ba202009-08-20 01:44:21 +0000544Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000545 // Create a local instantiation scope for this class template, which
546 // will contain the instantiations of the template parameters.
547 Sema::LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000548 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000549 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000550 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000551 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000552
553 CXXRecordDecl *Pattern = D->getTemplatedDecl();
554 CXXRecordDecl *RecordInst
555 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner,
556 Pattern->getLocation(), Pattern->getIdentifier(),
Douglas Gregorf0510d42009-10-12 23:11:44 +0000557 Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL,
558 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000559
560 ClassTemplateDecl *Inst
561 = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
562 D->getIdentifier(), InstParams, RecordInst, 0);
563 RecordInst->setDescribedClassTemplate(Inst);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000564 if (D->getFriendObjectKind())
565 Inst->setObjectOfFriendDecl(true);
566 else
567 Inst->setAccess(D->getAccess());
John McCalle29ba202009-08-20 01:44:21 +0000568 Inst->setInstantiatedFromMemberTemplate(D);
Douglas Gregorf0510d42009-10-12 23:11:44 +0000569
570 // Trigger creation of the type for the instantiation.
571 SemaRef.Context.getTypeDeclType(RecordInst);
572
Douglas Gregor259571e2009-10-30 22:42:42 +0000573 // Finish handling of friends.
574 if (Inst->getFriendObjectKind()) {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000575 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000576 }
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000577
John McCall46460a62010-01-20 21:53:11 +0000578 Inst->setAccess(D->getAccess());
John McCalle29ba202009-08-20 01:44:21 +0000579 Owner->addDecl(Inst);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000580
581 // First, we sort the partial specializations by location, so
582 // that we instantiate them in the order they were declared.
583 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
584 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
585 P = D->getPartialSpecializations().begin(),
586 PEnd = D->getPartialSpecializations().end();
587 P != PEnd; ++P)
588 PartialSpecs.push_back(&*P);
589 std::sort(PartialSpecs.begin(), PartialSpecs.end(),
590 SortDeclByLocation(SemaRef.SourceMgr));
591
592 // Instantiate all of the partial specializations of this member class
593 // template.
594 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
595 InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
596
John McCalle29ba202009-08-20 01:44:21 +0000597 return Inst;
598}
599
Douglas Gregord60e1052009-08-27 16:57:43 +0000600Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000601TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
602 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000603 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
604
605 // Lookup the already-instantiated declaration in the instantiation
606 // of the class template and return that.
607 DeclContext::lookup_result Found
608 = Owner->lookup(ClassTemplate->getDeclName());
609 if (Found.first == Found.second)
610 return 0;
611
612 ClassTemplateDecl *InstClassTemplate
613 = dyn_cast<ClassTemplateDecl>(*Found.first);
614 if (!InstClassTemplate)
615 return 0;
616
617 Decl *DCanon = D->getCanonicalDecl();
618 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
619 P = InstClassTemplate->getPartialSpecializations().begin(),
620 PEnd = InstClassTemplate->getPartialSpecializations().end();
621 P != PEnd; ++P) {
622 if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
623 return &*P;
624 }
625
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000626 return 0;
627}
628
629Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000630TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000631 // Create a local instantiation scope for this function template, which
632 // will contain the instantiations of the template parameters and then get
633 // merged with the local instantiation scope for the function template
634 // itself.
635 Sema::LocalInstantiationScope Scope(SemaRef);
636
Douglas Gregord60e1052009-08-27 16:57:43 +0000637 TemplateParameterList *TempParams = D->getTemplateParameters();
638 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000639 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000640 return NULL;
Douglas Gregored9c0f92009-10-29 00:04:11 +0000641
Douglas Gregora735b202009-10-13 14:39:41 +0000642 FunctionDecl *Instantiated = 0;
643 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
644 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
645 InstParams));
646 else
647 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
648 D->getTemplatedDecl(),
649 InstParams));
650
651 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000652 return 0;
653
John McCall46460a62010-01-20 21:53:11 +0000654 Instantiated->setAccess(D->getAccess());
655
Mike Stump1eb44332009-09-09 15:08:12 +0000656 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000657 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000658 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000659 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000660 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000661 assert(InstTemplate &&
662 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
John McCalle976ffe2009-12-14 23:19:40 +0000663
664 // Link the instantiation back to the pattern *unless* this is a
665 // non-definition friend declaration.
666 if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
667 !(InstTemplate->getFriendObjectKind() &&
668 !D->getTemplatedDecl()->isThisDeclarationADefinition()))
Douglas Gregora735b202009-10-13 14:39:41 +0000669 InstTemplate->setInstantiatedFromMemberTemplate(D);
670
671 // Add non-friends into the owner.
672 if (!InstTemplate->getFriendObjectKind())
673 Owner->addDecl(InstTemplate);
Douglas Gregord60e1052009-08-27 16:57:43 +0000674 return InstTemplate;
675}
676
Douglas Gregord475b8d2009-03-25 21:17:03 +0000677Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
678 CXXRecordDecl *PrevDecl = 0;
679 if (D->isInjectedClassName())
680 PrevDecl = cast<CXXRecordDecl>(Owner);
John McCall6c1c1b82009-12-15 22:29:06 +0000681 else if (D->getPreviousDeclaration()) {
682 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getPreviousDeclaration(),
683 TemplateArgs);
684 if (!Prev) return 0;
685 PrevDecl = cast<CXXRecordDecl>(Prev);
686 }
Douglas Gregord475b8d2009-03-25 21:17:03 +0000687
688 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000689 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000690 D->getLocation(), D->getIdentifier(),
691 D->getTagKeywordLoc(), PrevDecl);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000692 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000693 // FIXME: Check against AS_none is an ugly hack to work around the issue that
694 // the tag decls introduced by friend class declarations don't have an access
695 // specifier. Remove once this area of the code gets sorted out.
696 if (D->getAccess() != AS_none)
697 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000698 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000699 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000700
John McCall02cace72009-08-28 07:59:38 +0000701 // If the original function was part of a friend declaration,
702 // inherit its namespace state.
703 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
704 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
705
Anders Carlssond8b285f2009-09-01 04:26:58 +0000706 Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
707
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000708 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000709 return Record;
710}
711
John McCall02cace72009-08-28 07:59:38 +0000712/// Normal class members are of more specific types and therefore
713/// don't make it here. This function serves two purposes:
714/// 1) instantiating function templates
715/// 2) substituting friend declarations
716/// FIXME: preserve function definitions in case #2
Douglas Gregor7557a132009-12-24 20:56:24 +0000717Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
Douglas Gregora735b202009-10-13 14:39:41 +0000718 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000719 // Check whether there is already a function template specialization for
720 // this declaration.
721 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
722 void *InsertPos = 0;
Douglas Gregora735b202009-10-13 14:39:41 +0000723 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000724 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000725 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000726 TemplateArgs.getInnermost().getFlatArgumentList(),
727 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor828e2262009-07-29 16:09:57 +0000728 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000729
730 FunctionTemplateSpecializationInfo *Info
731 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor127102b2009-06-29 20:59:39 +0000732 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000733
Douglas Gregor127102b2009-06-29 20:59:39 +0000734 // If we already have a function template specialization, return it.
735 if (Info)
736 return Info->Function;
737 }
Mike Stump1eb44332009-09-09 15:08:12 +0000738
Douglas Gregor79c22782010-01-16 20:21:20 +0000739 bool MergeWithParentScope = (TemplateParams != 0) ||
740 !(isa<Decl>(Owner) &&
741 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
742 Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000743
Douglas Gregore53060f2009-06-25 22:08:12 +0000744 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000745 QualType T = SubstFunctionType(D, Params);
Douglas Gregore53060f2009-06-25 22:08:12 +0000746 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000747 return 0;
John McCallfd810b12009-08-14 02:03:10 +0000748
John McCall68b6b872010-02-06 01:50:47 +0000749 // If we're instantiating a local function declaration, put the result
750 // in the owner; otherwise we need to find the instantiated context.
751 DeclContext *DC;
752 if (D->getDeclContext()->isFunctionOrMethod())
753 DC = Owner;
754 else
755 DC = SemaRef.FindInstantiatedContext(D->getDeclContext(), TemplateArgs);
756
John McCall02cace72009-08-28 07:59:38 +0000757 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +0000758 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
John McCalla93c9342009-12-07 02:54:59 +0000759 D->getDeclName(), T, D->getTypeSourceInfo(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000760 D->getStorageClass(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000761 D->isInlineSpecified(), D->hasWrittenPrototype());
John McCall02cace72009-08-28 07:59:38 +0000762 Function->setLexicalDeclContext(Owner);
Mike Stump1eb44332009-09-09 15:08:12 +0000763
Douglas Gregore53060f2009-06-25 22:08:12 +0000764 // Attach the parameters
765 for (unsigned P = 0; P < Params.size(); ++P)
766 Params[P]->setOwningFunction(Function);
767 Function->setParams(SemaRef.Context, Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +0000768
Douglas Gregora735b202009-10-13 14:39:41 +0000769 if (TemplateParams) {
770 // Our resulting instantiation is actually a function template, since we
771 // are substituting only the outer template parameters. For example, given
772 //
773 // template<typename T>
774 // struct X {
775 // template<typename U> friend void f(T, U);
776 // };
777 //
778 // X<int> x;
779 //
780 // We are instantiating the friend function template "f" within X<int>,
781 // which means substituting int for T, but leaving "f" as a friend function
782 // template.
783 // Build the function template itself.
784 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Owner,
785 Function->getLocation(),
786 Function->getDeclName(),
787 TemplateParams, Function);
788 Function->setDescribedFunctionTemplate(FunctionTemplate);
789 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregor66724ea2009-11-14 01:20:54 +0000790 } else if (FunctionTemplate) {
791 // Record this function template specialization.
792 Function->setFunctionTemplateSpecialization(SemaRef.Context,
793 FunctionTemplate,
794 &TemplateArgs.getInnermost(),
795 InsertPos);
John McCall02cace72009-08-28 07:59:38 +0000796 }
Douglas Gregora735b202009-10-13 14:39:41 +0000797
Douglas Gregore53060f2009-06-25 22:08:12 +0000798 if (InitFunctionInstantiation(Function, D))
799 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000800
Douglas Gregore53060f2009-06-25 22:08:12 +0000801 bool Redeclaration = false;
802 bool OverloadableAttrRequired = false;
Douglas Gregora735b202009-10-13 14:39:41 +0000803
John McCall68263142009-11-18 22:49:29 +0000804 LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
805 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
806
Douglas Gregora735b202009-10-13 14:39:41 +0000807 if (TemplateParams || !FunctionTemplate) {
808 // Look only into the namespace where the friend would be declared to
809 // find a previous declaration. This is the innermost enclosing namespace,
810 // as described in ActOnFriendFunctionDecl.
John McCall68263142009-11-18 22:49:29 +0000811 SemaRef.LookupQualifiedName(Previous, DC);
Douglas Gregora735b202009-10-13 14:39:41 +0000812
Douglas Gregora735b202009-10-13 14:39:41 +0000813 // In C++, the previous declaration we find might be a tag type
814 // (class or enum). In this case, the new declaration will hide the
815 // tag type. Note that this does does not apply if we're declaring a
816 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +0000817 if (Previous.isSingleTagDecl())
818 Previous.clear();
Douglas Gregora735b202009-10-13 14:39:41 +0000819 }
820
John McCall9f54ad42009-12-10 09:41:52 +0000821 SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
822 false, Redeclaration,
Douglas Gregore53060f2009-06-25 22:08:12 +0000823 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000824
Douglas Gregora735b202009-10-13 14:39:41 +0000825 // If the original function was part of a friend declaration,
826 // inherit its namespace state and add it to the owner.
827 NamedDecl *FromFriendD
828 = TemplateParams? cast<NamedDecl>(D->getDescribedFunctionTemplate()) : D;
829 if (FromFriendD->getFriendObjectKind()) {
830 NamedDecl *ToFriendD = 0;
John McCall68263142009-11-18 22:49:29 +0000831 NamedDecl *PrevDecl;
Douglas Gregora735b202009-10-13 14:39:41 +0000832 if (TemplateParams) {
833 ToFriendD = cast<NamedDecl>(FunctionTemplate);
834 PrevDecl = FunctionTemplate->getPreviousDeclaration();
835 } else {
836 ToFriendD = Function;
837 PrevDecl = Function->getPreviousDeclaration();
838 }
839 ToFriendD->setObjectOfFriendDecl(PrevDecl != NULL);
840 if (!Owner->isDependentContext() && !PrevDecl)
841 DC->makeDeclVisibleInContext(ToFriendD, /* Recoverable = */ false);
842
843 if (!TemplateParams)
844 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
845 }
846
Douglas Gregore53060f2009-06-25 22:08:12 +0000847 return Function;
848}
849
Douglas Gregord60e1052009-08-27 16:57:43 +0000850Decl *
851TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
852 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +0000853 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
854 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +0000855 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +0000856 // We are creating a function template specialization from a function
857 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +0000858 // specialization for this particular set of template arguments.
Douglas Gregor6b906862009-08-21 00:16:32 +0000859 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000860 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000861 TemplateArgs.getInnermost().getFlatArgumentList(),
862 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000863 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000864
865 FunctionTemplateSpecializationInfo *Info
866 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor6b906862009-08-21 00:16:32 +0000867 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000868
Douglas Gregor6b906862009-08-21 00:16:32 +0000869 // If we already have a function template specialization, return it.
870 if (Info)
871 return Info->Function;
872 }
873
Douglas Gregor79c22782010-01-16 20:21:20 +0000874 bool MergeWithParentScope = (TemplateParams != 0) ||
875 !(isa<Decl>(Owner) &&
876 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
877 Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000878
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000879 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000880 QualType T = SubstFunctionType(D, Params);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000881 if (T.isNull())
882 return 0;
883
884 // Build the instantiated method declaration.
885 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
Douglas Gregordec06662009-08-21 18:42:58 +0000886 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000887
Douglas Gregordec06662009-08-21 18:42:58 +0000888 DeclarationName Name = D->getDeclName();
Douglas Gregor17e32f32009-08-21 22:43:28 +0000889 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregordec06662009-08-21 18:42:58 +0000890 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
891 Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
892 SemaRef.Context.getCanonicalType(ClassTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000893 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
894 Constructor->getLocation(),
895 Name, T,
John McCalla93c9342009-12-07 02:54:59 +0000896 Constructor->getTypeSourceInfo(),
Mike Stump1eb44332009-09-09 15:08:12 +0000897 Constructor->isExplicit(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000898 Constructor->isInlineSpecified(), false);
Douglas Gregor17e32f32009-08-21 22:43:28 +0000899 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
900 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
901 Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
902 SemaRef.Context.getCanonicalType(ClassTy));
903 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
904 Destructor->getLocation(), Name,
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000905 T, Destructor->isInlineSpecified(), false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000906 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000907 CanQualType ConvTy
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000908 = SemaRef.Context.getCanonicalType(
John McCall183700f2009-09-21 23:43:11 +0000909 T->getAs<FunctionType>()->getResultType());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000910 Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
911 ConvTy);
912 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
913 Conversion->getLocation(), Name,
John McCalla93c9342009-12-07 02:54:59 +0000914 T, Conversion->getTypeSourceInfo(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000915 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000916 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +0000917 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000918 Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
John McCalla93c9342009-12-07 02:54:59 +0000919 D->getDeclName(), T, D->getTypeSourceInfo(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000920 D->isStatic(), D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +0000921 }
Douglas Gregor6b906862009-08-21 00:16:32 +0000922
Douglas Gregord60e1052009-08-27 16:57:43 +0000923 if (TemplateParams) {
924 // Our resulting instantiation is actually a function template, since we
925 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +0000926 //
Douglas Gregord60e1052009-08-27 16:57:43 +0000927 // template<typename T>
928 // struct X {
929 // template<typename U> void f(T, U);
930 // };
931 //
932 // X<int> x;
933 //
934 // We are instantiating the member template "f" within X<int>, which means
935 // substituting int for T, but leaving "f" as a member function template.
936 // Build the function template itself.
937 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
938 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +0000939 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +0000940 TemplateParams, Method);
941 if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +0000942 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +0000943 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +0000944 } else if (FunctionTemplate) {
945 // Record this function template specialization.
946 Method->setFunctionTemplateSpecialization(SemaRef.Context,
947 FunctionTemplate,
948 &TemplateArgs.getInnermost(),
949 InsertPos);
950 } else {
951 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +0000952 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +0000953 }
954
Mike Stump1eb44332009-09-09 15:08:12 +0000955 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000956 // out-of-line, the instantiation will have the same lexical
957 // context (which will be a namespace scope) as the template.
958 if (D->isOutOfLine())
959 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000960
Douglas Gregor5545e162009-03-24 00:38:23 +0000961 // Attach the parameters
962 for (unsigned P = 0; P < Params.size(); ++P)
963 Params[P]->setOwningFunction(Method);
Jay Foadbeaaccd2009-05-21 09:52:38 +0000964 Method->setParams(SemaRef.Context, Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +0000965
966 if (InitMethodInstantiation(Method, D))
967 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000968
John McCall68263142009-11-18 22:49:29 +0000969 LookupResult Previous(SemaRef, Name, SourceLocation(),
970 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000971
Douglas Gregord60e1052009-08-27 16:57:43 +0000972 if (!FunctionTemplate || TemplateParams) {
John McCall68263142009-11-18 22:49:29 +0000973 SemaRef.LookupQualifiedName(Previous, Owner);
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Douglas Gregordec06662009-08-21 18:42:58 +0000975 // In C++, the previous declaration we find might be a tag type
976 // (class or enum). In this case, the new declaration will hide the
977 // tag type. Note that this does does not apply if we're declaring a
978 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +0000979 if (Previous.isSingleTagDecl())
980 Previous.clear();
Douglas Gregordec06662009-08-21 18:42:58 +0000981 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000982
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000983 bool Redeclaration = false;
984 bool OverloadableAttrRequired = false;
John McCall9f54ad42009-12-10 09:41:52 +0000985 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000986 /*FIXME:*/OverloadableAttrRequired);
987
Douglas Gregor4ba31362009-12-01 17:24:26 +0000988 if (D->isPure())
989 SemaRef.CheckPureMethod(Method, SourceRange());
990
John McCall46460a62010-01-20 21:53:11 +0000991 Method->setAccess(D->getAccess());
992
John McCall68263142009-11-18 22:49:29 +0000993 if (!FunctionTemplate && (!Method->isInvalidDecl() || Previous.empty()) &&
Douglas Gregora735b202009-10-13 14:39:41 +0000994 !Method->getFriendObjectKind())
Douglas Gregordec06662009-08-21 18:42:58 +0000995 Owner->addDecl(Method);
Mike Stump1eb44332009-09-09 15:08:12 +0000996
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000997 return Method;
998}
999
Douglas Gregor615c5d42009-03-24 16:43:20 +00001000Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +00001001 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +00001002}
1003
Douglas Gregor03b2b072009-03-24 00:15:49 +00001004Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001005 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +00001006}
1007
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001008Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001009 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001010}
1011
Douglas Gregor6477b692009-03-25 15:04:13 +00001012ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
John McCall58e46772009-10-23 21:48:59 +00001013 QualType T;
John McCalla93c9342009-12-07 02:54:59 +00001014 TypeSourceInfo *DI = D->getTypeSourceInfo();
John McCall58e46772009-10-23 21:48:59 +00001015 if (DI) {
1016 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
1017 D->getDeclName());
1018 if (DI) T = DI->getType();
1019 } else {
1020 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
1021 D->getDeclName());
1022 DI = 0;
1023 }
1024
1025 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001026 return 0;
1027
John McCall58e46772009-10-23 21:48:59 +00001028 T = SemaRef.adjustParameterType(T);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001029
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001030 // Allocate the parameter
John McCall58e46772009-10-23 21:48:59 +00001031 ParmVarDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +00001032 = ParmVarDecl::Create(SemaRef.Context,
1033 SemaRef.Context.getTranslationUnitDecl(),
1034 D->getLocation(),
John McCall58e46772009-10-23 21:48:59 +00001035 D->getIdentifier(), T, DI, D->getStorageClass(), 0);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001036
Anders Carlsson9351c172009-08-25 03:18:48 +00001037 // Mark the default argument as being uninstantiated.
Douglas Gregorf43d0b32009-09-25 06:56:31 +00001038 if (D->hasUninstantiatedDefaultArg())
1039 Param->setUninstantiatedDefaultArg(D->getUninstantiatedDefaultArg());
Douglas Gregor0ed09302009-09-25 07:03:22 +00001040 else if (Expr *Arg = D->getDefaultArg())
1041 Param->setUninstantiatedDefaultArg(Arg);
1042
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001043 // Note: we don't try to instantiate function parameters until after
1044 // we've instantiated the function's type. Therefore, we don't have
1045 // to check for 'void' parameter types here.
Douglas Gregor48dd19b2009-05-14 21:44:34 +00001046 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001047 return Param;
1048}
1049
John McCalle29ba202009-08-20 01:44:21 +00001050Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1051 TemplateTypeParmDecl *D) {
1052 // TODO: don't always clone when decls are refcounted.
1053 const Type* T = D->getTypeForDecl();
1054 assert(T->isTemplateTypeParmType());
1055 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001056
John McCalle29ba202009-08-20 01:44:21 +00001057 TemplateTypeParmDecl *Inst =
1058 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor550d9b22009-10-31 17:21:17 +00001059 TTPT->getDepth() - 1, TTPT->getIndex(),
John McCalle29ba202009-08-20 01:44:21 +00001060 TTPT->getName(),
1061 D->wasDeclaredWithTypename(),
1062 D->isParameterPack());
1063
Douglas Gregor0f8716b2009-11-09 19:17:50 +00001064 if (D->hasDefaultArgument())
1065 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
John McCalle29ba202009-08-20 01:44:21 +00001066
Douglas Gregor550d9b22009-10-31 17:21:17 +00001067 // Introduce this template parameter's instantiation into the instantiation
1068 // scope.
1069 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1070
John McCalle29ba202009-08-20 01:44:21 +00001071 return Inst;
1072}
1073
Douglas Gregor33642df2009-10-23 23:25:44 +00001074Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1075 NonTypeTemplateParmDecl *D) {
1076 // Substitute into the type of the non-type template parameter.
1077 QualType T;
John McCalla93c9342009-12-07 02:54:59 +00001078 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor33642df2009-10-23 23:25:44 +00001079 if (DI) {
1080 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
1081 D->getDeclName());
1082 if (DI) T = DI->getType();
1083 } else {
1084 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
1085 D->getDeclName());
1086 DI = 0;
1087 }
1088 if (T.isNull())
1089 return 0;
1090
1091 // Check that this type is acceptable for a non-type template parameter.
1092 bool Invalid = false;
1093 T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
1094 if (T.isNull()) {
1095 T = SemaRef.Context.IntTy;
1096 Invalid = true;
1097 }
1098
1099 NonTypeTemplateParmDecl *Param
1100 = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1101 D->getDepth() - 1, D->getPosition(),
1102 D->getIdentifier(), T, DI);
1103 if (Invalid)
1104 Param->setInvalidDecl();
1105
1106 Param->setDefaultArgument(D->getDefaultArgument());
Douglas Gregor550d9b22009-10-31 17:21:17 +00001107
1108 // Introduce this template parameter's instantiation into the instantiation
1109 // scope.
1110 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001111 return Param;
1112}
1113
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001114Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001115TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1116 TemplateTemplateParmDecl *D) {
1117 // Instantiate the template parameter list of the template template parameter.
1118 TemplateParameterList *TempParams = D->getTemplateParameters();
1119 TemplateParameterList *InstParams;
1120 {
1121 // Perform the actual substitution of template parameters within a new,
1122 // local instantiation scope.
1123 Sema::LocalInstantiationScope Scope(SemaRef);
1124 InstParams = SubstTemplateParams(TempParams);
1125 if (!InstParams)
1126 return NULL;
1127 }
1128
1129 // Build the template template parameter.
1130 TemplateTemplateParmDecl *Param
1131 = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1132 D->getDepth() - 1, D->getPosition(),
1133 D->getIdentifier(), InstParams);
1134 Param->setDefaultArgument(D->getDefaultArgument());
1135
1136 // Introduce this template parameter's instantiation into the instantiation
1137 // scope.
1138 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1139
1140 return Param;
1141}
1142
Douglas Gregor48c32a72009-11-17 06:07:40 +00001143Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1144 // Using directives are never dependent, so they require no explicit
1145
1146 UsingDirectiveDecl *Inst
1147 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1148 D->getNamespaceKeyLocation(),
1149 D->getQualifierRange(), D->getQualifier(),
1150 D->getIdentLocation(),
1151 D->getNominatedNamespace(),
1152 D->getCommonAncestor());
1153 Owner->addDecl(Inst);
1154 return Inst;
1155}
1156
John McCalled976492009-12-04 22:46:56 +00001157Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
1158 // The nested name specifier is non-dependent, so no transformation
1159 // is required.
1160
John McCall9f54ad42009-12-10 09:41:52 +00001161 // We only need to do redeclaration lookups if we're in a class
1162 // scope (in fact, it's not really even possible in non-class
1163 // scopes).
1164 bool CheckRedeclaration = Owner->isRecord();
1165
1166 LookupResult Prev(SemaRef, D->getDeclName(), D->getLocation(),
1167 Sema::LookupUsingDeclName, Sema::ForRedeclaration);
1168
John McCalled976492009-12-04 22:46:56 +00001169 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
1170 D->getLocation(),
1171 D->getNestedNameRange(),
1172 D->getUsingLocation(),
1173 D->getTargetNestedNameDecl(),
1174 D->getDeclName(),
1175 D->isTypeName());
1176
1177 CXXScopeSpec SS;
1178 SS.setScopeRep(D->getTargetNestedNameDecl());
1179 SS.setRange(D->getNestedNameRange());
John McCall9f54ad42009-12-10 09:41:52 +00001180
1181 if (CheckRedeclaration) {
1182 Prev.setHideTags(false);
1183 SemaRef.LookupQualifiedName(Prev, Owner);
1184
1185 // Check for invalid redeclarations.
1186 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
1187 D->isTypeName(), SS,
1188 D->getLocation(), Prev))
1189 NewUD->setInvalidDecl();
1190
1191 }
1192
1193 if (!NewUD->isInvalidDecl() &&
1194 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
John McCalled976492009-12-04 22:46:56 +00001195 D->getLocation()))
1196 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00001197
John McCalled976492009-12-04 22:46:56 +00001198 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1199 NewUD->setAccess(D->getAccess());
1200 Owner->addDecl(NewUD);
1201
John McCall9f54ad42009-12-10 09:41:52 +00001202 // Don't process the shadow decls for an invalid decl.
1203 if (NewUD->isInvalidDecl())
1204 return NewUD;
1205
John McCall323c3102009-12-22 22:26:37 +00001206 bool isFunctionScope = Owner->isFunctionOrMethod();
1207
John McCall9f54ad42009-12-10 09:41:52 +00001208 // Process the shadow decls.
1209 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
1210 I != E; ++I) {
1211 UsingShadowDecl *Shadow = *I;
1212 NamedDecl *InstTarget =
1213 cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getTargetDecl(),
1214 TemplateArgs));
1215
1216 if (CheckRedeclaration &&
1217 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
1218 continue;
1219
1220 UsingShadowDecl *InstShadow
1221 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
1222 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
John McCall323c3102009-12-22 22:26:37 +00001223
1224 if (isFunctionScope)
1225 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
John McCall9f54ad42009-12-10 09:41:52 +00001226 }
John McCalled976492009-12-04 22:46:56 +00001227
1228 return NewUD;
1229}
1230
1231Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00001232 // Ignore these; we handle them in bulk when processing the UsingDecl.
1233 return 0;
John McCalled976492009-12-04 22:46:56 +00001234}
1235
John McCall7ba107a2009-11-18 02:36:19 +00001236Decl * TemplateDeclInstantiator
1237 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +00001238 NestedNameSpecifier *NNS =
1239 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1240 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001241 TemplateArgs);
1242 if (!NNS)
1243 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001244
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001245 CXXScopeSpec SS;
1246 SS.setRange(D->getTargetNestedNameRange());
1247 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +00001248
1249 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00001250 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
John McCall7ba107a2009-11-18 02:36:19 +00001251 D->getUsingLoc(), SS, D->getLocation(),
1252 D->getDeclName(), 0,
1253 /*instantiation*/ true,
1254 /*typename*/ true, D->getTypenameLoc());
1255 if (UD)
John McCalled976492009-12-04 22:46:56 +00001256 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1257
John McCall7ba107a2009-11-18 02:36:19 +00001258 return UD;
1259}
1260
1261Decl * TemplateDeclInstantiator
1262 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1263 NestedNameSpecifier *NNS =
1264 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1265 D->getTargetNestedNameRange(),
1266 TemplateArgs);
1267 if (!NNS)
1268 return 0;
1269
1270 CXXScopeSpec SS;
1271 SS.setRange(D->getTargetNestedNameRange());
1272 SS.setScopeRep(NNS);
1273
1274 NamedDecl *UD =
1275 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1276 D->getUsingLoc(), SS, D->getLocation(),
1277 D->getDeclName(), 0,
1278 /*instantiation*/ true,
1279 /*typename*/ false, SourceLocation());
Anders Carlsson0d8df782009-08-29 19:37:28 +00001280 if (UD)
John McCalled976492009-12-04 22:46:56 +00001281 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1282
Anders Carlsson0d8df782009-08-29 19:37:28 +00001283 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001284}
1285
John McCallce3ff2b2009-08-25 22:02:44 +00001286Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001287 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001288 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001289 return Instantiator.Visit(D);
1290}
1291
John McCalle29ba202009-08-20 01:44:21 +00001292/// \brief Instantiates a nested template parameter list in the current
1293/// instantiation context.
1294///
1295/// \param L The parameter list to instantiate
1296///
1297/// \returns NULL if there was an error
1298TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001299TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001300 // Get errors for all the parameters before bailing out.
1301 bool Invalid = false;
1302
1303 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001304 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001305 ParamVector Params;
1306 Params.reserve(N);
1307 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1308 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001309 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001310 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001311 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00001312 }
1313
1314 // Clean up if we had an error.
1315 if (Invalid) {
1316 for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
1317 PI != PE; ++PI)
1318 if (*PI)
1319 (*PI)->Destroy(SemaRef.Context);
1320 return NULL;
1321 }
1322
1323 TemplateParameterList *InstL
1324 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1325 L->getLAngleLoc(), &Params.front(), N,
1326 L->getRAngleLoc());
1327 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001328}
John McCalle29ba202009-08-20 01:44:21 +00001329
Douglas Gregored9c0f92009-10-29 00:04:11 +00001330/// \brief Instantiate the declaration of a class template partial
1331/// specialization.
1332///
1333/// \param ClassTemplate the (instantiated) class template that is partially
1334// specialized by the instantiation of \p PartialSpec.
1335///
1336/// \param PartialSpec the (uninstantiated) class template partial
1337/// specialization that we are instantiating.
1338///
1339/// \returns true if there was an error, false otherwise.
1340bool
1341TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1342 ClassTemplateDecl *ClassTemplate,
1343 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001344 // Create a local instantiation scope for this class template partial
1345 // specialization, which will contain the instantiations of the template
1346 // parameters.
1347 Sema::LocalInstantiationScope Scope(SemaRef);
1348
Douglas Gregored9c0f92009-10-29 00:04:11 +00001349 // Substitute into the template parameters of the class template partial
1350 // specialization.
1351 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1352 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1353 if (!InstParams)
1354 return true;
1355
1356 // Substitute into the template arguments of the class template partial
1357 // specialization.
John McCall833ca992009-10-29 08:12:44 +00001358 const TemplateArgumentLoc *PartialSpecTemplateArgs
1359 = PartialSpec->getTemplateArgsAsWritten();
1360 unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1361
John McCalld5532b62009-11-23 01:53:49 +00001362 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
John McCall833ca992009-10-29 08:12:44 +00001363 for (unsigned I = 0; I != N; ++I) {
John McCalld5532b62009-11-23 01:53:49 +00001364 TemplateArgumentLoc Loc;
1365 if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs))
Douglas Gregored9c0f92009-10-29 00:04:11 +00001366 return true;
John McCalld5532b62009-11-23 01:53:49 +00001367 InstTemplateArgs.addArgument(Loc);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001368 }
1369
1370
1371 // Check that the template argument list is well-formed for this
1372 // class template.
1373 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1374 InstTemplateArgs.size());
1375 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1376 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001377 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001378 false,
1379 Converted))
1380 return true;
1381
1382 // Figure out where to insert this class template partial specialization
1383 // in the member template's set of class template partial specializations.
1384 llvm::FoldingSetNodeID ID;
1385 ClassTemplatePartialSpecializationDecl::Profile(ID,
1386 Converted.getFlatArguments(),
1387 Converted.flatSize(),
1388 SemaRef.Context);
1389 void *InsertPos = 0;
1390 ClassTemplateSpecializationDecl *PrevDecl
1391 = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
1392 InsertPos);
1393
1394 // Build the canonical type that describes the converted template
1395 // arguments of the class template partial specialization.
1396 QualType CanonType
1397 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1398 Converted.getFlatArguments(),
1399 Converted.flatSize());
1400
1401 // Build the fully-sugared type for this class template
1402 // specialization as the user wrote in the specialization
1403 // itself. This means that we'll pretty-print the type retrieved
1404 // from the specialization's declaration the way that the user
1405 // actually wrote the specialization, rather than formatting the
1406 // name based on the "canonical" representation used to store the
1407 // template arguments in the specialization.
1408 QualType WrittenTy
1409 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
John McCalld5532b62009-11-23 01:53:49 +00001410 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001411 CanonType);
1412
1413 if (PrevDecl) {
1414 // We've already seen a partial specialization with the same template
1415 // parameters and template arguments. This can happen, for example, when
1416 // substituting the outer template arguments ends up causing two
1417 // class template partial specializations of a member class template
1418 // to have identical forms, e.g.,
1419 //
1420 // template<typename T, typename U>
1421 // struct Outer {
1422 // template<typename X, typename Y> struct Inner;
1423 // template<typename Y> struct Inner<T, Y>;
1424 // template<typename Y> struct Inner<U, Y>;
1425 // };
1426 //
1427 // Outer<int, int> outer; // error: the partial specializations of Inner
1428 // // have the same signature.
1429 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1430 << WrittenTy;
1431 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1432 << SemaRef.Context.getTypeDeclType(PrevDecl);
1433 return true;
1434 }
1435
1436
1437 // Create the class template partial specialization declaration.
1438 ClassTemplatePartialSpecializationDecl *InstPartialSpec
1439 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context, Owner,
1440 PartialSpec->getLocation(),
1441 InstParams,
1442 ClassTemplate,
1443 Converted,
John McCalld5532b62009-11-23 01:53:49 +00001444 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001445 0);
1446 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
1447 InstPartialSpec->setTypeAsWritten(WrittenTy);
1448
1449 // Add this partial specialization to the set of class template partial
1450 // specializations.
1451 ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec,
1452 InsertPos);
1453 return false;
1454}
1455
John McCallce3ff2b2009-08-25 22:02:44 +00001456/// \brief Does substitution on the type of the given function, including
1457/// all of the function parameters.
Douglas Gregor5545e162009-03-24 00:38:23 +00001458///
John McCallce3ff2b2009-08-25 22:02:44 +00001459/// \param D The function whose type will be the basis of the substitution
Douglas Gregor5545e162009-03-24 00:38:23 +00001460///
1461/// \param Params the instantiated parameter declarations
1462
John McCallce3ff2b2009-08-25 22:02:44 +00001463/// \returns the instantiated function's type if successful, a NULL
Douglas Gregor5545e162009-03-24 00:38:23 +00001464/// type if there was an error.
Mike Stump1eb44332009-09-09 15:08:12 +00001465QualType
John McCallce3ff2b2009-08-25 22:02:44 +00001466TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001467 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
1468 bool InvalidDecl = false;
1469
John McCallce3ff2b2009-08-25 22:02:44 +00001470 // Substitute all of the function's formal parameter types.
Douglas Gregor7e063902009-05-11 23:53:27 +00001471 TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001472 llvm::SmallVector<QualType, 4> ParamTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001473 for (FunctionDecl::param_iterator P = D->param_begin(),
Douglas Gregor5545e162009-03-24 00:38:23 +00001474 PEnd = D->param_end();
1475 P != PEnd; ++P) {
Douglas Gregor6477b692009-03-25 15:04:13 +00001476 if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
Douglas Gregor5545e162009-03-24 00:38:23 +00001477 if (PInst->getType()->isVoidType()) {
1478 SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
1479 PInst->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001480 } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001481 PInst->getType(),
1482 diag::err_abstract_type_in_decl,
1483 Sema::AbstractParamType))
Douglas Gregor5545e162009-03-24 00:38:23 +00001484 PInst->setInvalidDecl();
1485
1486 Params.push_back(PInst);
1487 ParamTys.push_back(PInst->getType());
1488
1489 if (PInst->isInvalidDecl())
1490 InvalidDecl = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001491 } else
Douglas Gregor5545e162009-03-24 00:38:23 +00001492 InvalidDecl = true;
1493 }
1494
1495 // FIXME: Deallocate dead declarations.
1496 if (InvalidDecl)
1497 return QualType();
1498
John McCall183700f2009-09-21 23:43:11 +00001499 const FunctionProtoType *Proto = D->getType()->getAs<FunctionProtoType>();
Douglas Gregor5545e162009-03-24 00:38:23 +00001500 assert(Proto && "Missing prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +00001501 QualType ResultType
John McCallce3ff2b2009-08-25 22:02:44 +00001502 = SemaRef.SubstType(Proto->getResultType(), TemplateArgs,
1503 D->getLocation(), D->getDeclName());
Douglas Gregor5545e162009-03-24 00:38:23 +00001504 if (ResultType.isNull())
1505 return QualType();
1506
Jay Foadbeaaccd2009-05-21 09:52:38 +00001507 return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
Douglas Gregor5545e162009-03-24 00:38:23 +00001508 Proto->isVariadic(), Proto->getTypeQuals(),
1509 D->getLocation(), D->getDeclName());
1510}
1511
Mike Stump1eb44332009-09-09 15:08:12 +00001512/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00001513/// declaration (New) from the corresponding fields of its template (Tmpl).
1514///
1515/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001516bool
1517TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00001518 FunctionDecl *Tmpl) {
1519 if (Tmpl->isDeleted())
1520 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00001521
Douglas Gregorcca9e962009-07-01 22:01:06 +00001522 // If we are performing substituting explicitly-specified template arguments
1523 // or deduced template arguments into a function template and we reach this
1524 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00001525 // to keeping the new function template specialization. We therefore
1526 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00001527 // into a template instantiation for this specific function template
1528 // specialization, which is not a SFINAE context, so that we diagnose any
1529 // further errors in the declaration itself.
1530 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1531 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1532 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1533 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00001534 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00001535 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001536 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00001537 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00001538 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001539 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1540 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
Douglas Gregorf35f8282009-11-11 21:54:23 +00001541 --SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001542 }
1543 }
Mike Stump1eb44332009-09-09 15:08:12 +00001544
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001545 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
1546 assert(Proto && "Function template without prototype?");
1547
1548 if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
1549 Proto->getNoReturnAttr()) {
1550 // The function has an exception specification or a "noreturn"
1551 // attribute. Substitute into each of the exception types.
1552 llvm::SmallVector<QualType, 4> Exceptions;
1553 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
1554 // FIXME: Poor location information!
1555 QualType T
1556 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
1557 New->getLocation(), New->getDeclName());
1558 if (T.isNull() ||
1559 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
1560 continue;
1561
1562 Exceptions.push_back(T);
1563 }
1564
1565 // Rebuild the function type
1566
1567 const FunctionProtoType *NewProto
1568 = New->getType()->getAs<FunctionProtoType>();
1569 assert(NewProto && "Template instantiation without function prototype?");
1570 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
1571 NewProto->arg_type_begin(),
1572 NewProto->getNumArgs(),
1573 NewProto->isVariadic(),
1574 NewProto->getTypeQuals(),
1575 Proto->hasExceptionSpec(),
1576 Proto->hasAnyExceptionSpec(),
1577 Exceptions.size(),
1578 Exceptions.data(),
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001579 Proto->getNoReturnAttr(),
1580 Proto->getCallConv()));
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001581 }
1582
Douglas Gregore53060f2009-06-25 22:08:12 +00001583 return false;
1584}
1585
Douglas Gregor5545e162009-03-24 00:38:23 +00001586/// \brief Initializes common fields of an instantiated method
1587/// declaration (New) from the corresponding fields of its template
1588/// (Tmpl).
1589///
1590/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001591bool
1592TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00001593 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001594 if (InitFunctionInstantiation(New, Tmpl))
1595 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001596
Douglas Gregor5545e162009-03-24 00:38:23 +00001597 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
1598 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00001599 if (Tmpl->isVirtualAsWritten())
1600 Record->setMethodAsVirtual(New);
Douglas Gregor5545e162009-03-24 00:38:23 +00001601
1602 // FIXME: attributes
1603 // FIXME: New needs a pointer to Tmpl
1604 return false;
1605}
Douglas Gregora58861f2009-05-13 20:28:22 +00001606
1607/// \brief Instantiate the definition of the given function from its
1608/// template.
1609///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001610/// \param PointOfInstantiation the point at which the instantiation was
1611/// required. Note that this is not precisely a "point of instantiation"
1612/// for the function, but it's close.
1613///
Douglas Gregora58861f2009-05-13 20:28:22 +00001614/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001615/// function template specialization or member function of a class template
1616/// specialization.
1617///
1618/// \param Recursive if true, recursively instantiates any functions that
1619/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001620///
1621/// \param DefinitionRequired if true, then we are performing an explicit
1622/// instantiation where the body of the function is required. Complain if
1623/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001624void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001625 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001626 bool Recursive,
1627 bool DefinitionRequired) {
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001628 if (Function->isInvalidDecl())
1629 return;
1630
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001631 assert(!Function->getBody() && "Already instantiated!");
Mike Stump1eb44332009-09-09 15:08:12 +00001632
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001633 // Never instantiate an explicit specialization.
1634 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1635 return;
1636
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001637 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00001638 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001639 Stmt *Pattern = 0;
1640 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001641 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001642
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001643 if (!Pattern) {
1644 if (DefinitionRequired) {
1645 if (Function->getPrimaryTemplate())
1646 Diag(PointOfInstantiation,
1647 diag::err_explicit_instantiation_undefined_func_template)
1648 << Function->getPrimaryTemplate();
1649 else
1650 Diag(PointOfInstantiation,
1651 diag::err_explicit_instantiation_undefined_member)
1652 << 1 << Function->getDeclName() << Function->getDeclContext();
1653
1654 if (PatternDecl)
1655 Diag(PatternDecl->getLocation(),
1656 diag::note_explicit_instantiation_here);
1657 }
1658
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001659 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001660 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001661
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001662 // C++0x [temp.explicit]p9:
1663 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00001664 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001665 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00001666 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001667 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001668 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001669 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001670
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001671 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
1672 if (Inst)
1673 return;
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001674
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001675 // If we're performing recursive template instantiation, create our own
1676 // queue of pending implicit instantiations that we will instantiate later,
1677 // while we're still within our own instantiation context.
1678 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1679 if (Recursive)
1680 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001681
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001682 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
1683
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001684 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00001685 // recorded, unless we're actually a member function within a local
1686 // class, in which case we need to merge our results with the parent
1687 // scope (of the enclosing function).
1688 bool MergeWithParentScope = false;
1689 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
1690 MergeWithParentScope = Rec->isLocalClass();
1691
1692 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001693
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001694 // Introduce the instantiated function parameters into the local
1695 // instantiation scope.
1696 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
1697 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
1698 Function->getParamDecl(I));
1699
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001700 // Enter the scope of this instantiation. We don't use
1701 // PushDeclContext because we don't have a scope.
1702 DeclContext *PreviousContext = CurContext;
1703 CurContext = Function;
1704
Mike Stump1eb44332009-09-09 15:08:12 +00001705 MultiLevelTemplateArgumentList TemplateArgs =
Anders Carlsson09025312009-08-29 05:16:22 +00001706 getTemplateInstantiationArgs(Function);
1707
1708 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001709 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00001710 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
1711 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
1712 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001713 }
1714
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001715 // Instantiate the function body.
Anders Carlsson09025312009-08-29 05:16:22 +00001716 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001717
Douglas Gregor52604ab2009-09-11 21:19:12 +00001718 if (Body.isInvalid())
1719 Function->setInvalidDecl();
1720
Mike Stump1eb44332009-09-09 15:08:12 +00001721 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001722 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001723
1724 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00001725
1726 DeclGroupRef DG(Function);
1727 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001728
Douglas Gregor60406be2010-01-16 22:29:39 +00001729 // This class may have local implicit instantiations that need to be
1730 // instantiation within this scope.
1731 PerformPendingImplicitInstantiations(/*LocalOnly=*/true);
1732 Scope.Exit();
1733
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001734 if (Recursive) {
1735 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001736 // instantiation of this template.
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001737 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001738
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001739 // Restore the set of pending implicit instantiations.
1740 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1741 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001742}
1743
1744/// \brief Instantiate the definition of the given variable from its
1745/// template.
1746///
Douglas Gregor7caa6822009-07-24 20:34:43 +00001747/// \param PointOfInstantiation the point at which the instantiation was
1748/// required. Note that this is not precisely a "point of instantiation"
1749/// for the function, but it's close.
1750///
1751/// \param Var the already-instantiated declaration of a static member
1752/// variable of a class template specialization.
1753///
1754/// \param Recursive if true, recursively instantiates any functions that
1755/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001756///
1757/// \param DefinitionRequired if true, then we are performing an explicit
1758/// instantiation where an out-of-line definition of the member variable
1759/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001760void Sema::InstantiateStaticDataMemberDefinition(
1761 SourceLocation PointOfInstantiation,
1762 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001763 bool Recursive,
1764 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001765 if (Var->isInvalidDecl())
1766 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001767
Douglas Gregor7caa6822009-07-24 20:34:43 +00001768 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001769 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00001770 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00001771 assert(Def->isStaticDataMember() && "Not a static data member?");
1772 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00001773
Douglas Gregor0d035142009-10-27 18:42:08 +00001774 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001775 // We did not find an out-of-line definition of this static data member,
1776 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00001777 // instantiate this definition (or provide a specialization for it) in
1778 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001779 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001780 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001781 Diag(PointOfInstantiation,
1782 diag::err_explicit_instantiation_undefined_member)
1783 << 2 << Var->getDeclName() << Var->getDeclContext();
1784 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
1785 }
1786
Douglas Gregor7caa6822009-07-24 20:34:43 +00001787 return;
1788 }
1789
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001790 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001791 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001792 return;
1793
1794 // C++0x [temp.explicit]p9:
1795 // Except for inline functions, other explicit instantiation declarations
1796 // have the effect of suppressing the implicit instantiation of the entity
1797 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001798 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001799 == TSK_ExplicitInstantiationDeclaration)
1800 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001801
Douglas Gregor7caa6822009-07-24 20:34:43 +00001802 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
1803 if (Inst)
1804 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001805
Douglas Gregor7caa6822009-07-24 20:34:43 +00001806 // If we're performing recursive template instantiation, create our own
1807 // queue of pending implicit instantiations that we will instantiate later,
1808 // while we're still within our own instantiation context.
1809 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1810 if (Recursive)
1811 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001812
Douglas Gregor7caa6822009-07-24 20:34:43 +00001813 // Enter the scope of this instantiation. We don't use
1814 // PushDeclContext because we don't have a scope.
1815 DeclContext *PreviousContext = CurContext;
1816 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001817
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001818 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00001819 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00001820 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00001821 CurContext = PreviousContext;
1822
1823 if (Var) {
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001824 Var->setPreviousDeclaration(OldVar);
Douglas Gregor583f33b2009-10-15 18:07:02 +00001825 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
1826 assert(MSInfo && "Missing member specialization information?");
1827 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
1828 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00001829 DeclGroupRef DG(Var);
1830 Consumer.HandleTopLevelDecl(DG);
1831 }
Mike Stump1eb44332009-09-09 15:08:12 +00001832
Douglas Gregor7caa6822009-07-24 20:34:43 +00001833 if (Recursive) {
1834 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001835 // instantiation of this template.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001836 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001837
Douglas Gregor7caa6822009-07-24 20:34:43 +00001838 // Restore the set of pending implicit instantiations.
1839 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001840 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001841}
Douglas Gregor815215d2009-05-27 05:35:12 +00001842
Anders Carlsson09025312009-08-29 05:16:22 +00001843void
1844Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
1845 const CXXConstructorDecl *Tmpl,
1846 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001847
Anders Carlsson09025312009-08-29 05:16:22 +00001848 llvm::SmallVector<MemInitTy*, 4> NewInits;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001849 bool AnyErrors = false;
1850
Anders Carlsson09025312009-08-29 05:16:22 +00001851 // Instantiate all the initializers.
1852 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00001853 InitsEnd = Tmpl->init_end();
1854 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00001855 CXXBaseOrMemberInitializer *Init = *Inits;
1856
1857 ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001858 llvm::SmallVector<SourceLocation, 4> CommaLocs;
Mike Stump1eb44332009-09-09 15:08:12 +00001859
Anders Carlsson09025312009-08-29 05:16:22 +00001860 // Instantiate all the arguments.
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001861 Expr *InitE = Init->getInit();
1862 if (!InitE) {
1863 // Nothing to instantiate;
1864 } else if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(InitE)) {
1865 if (InstantiateInitializationArguments(*this, ParenList->getExprs(),
1866 ParenList->getNumExprs(),
1867 TemplateArgs, CommaLocs,
1868 NewArgs)) {
1869 AnyErrors = true;
1870 continue;
1871 }
1872 } else {
1873 OwningExprResult InitArg = SubstExpr(InitE, TemplateArgs);
1874 if (InitArg.isInvalid()) {
1875 AnyErrors = true;
1876 continue;
1877 }
1878
1879 NewArgs.push_back(InitArg.release());
Anders Carlsson09025312009-08-29 05:16:22 +00001880 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001881
Anders Carlsson09025312009-08-29 05:16:22 +00001882 MemInitResult NewInit;
Anders Carlsson09025312009-08-29 05:16:22 +00001883 if (Init->isBaseInitializer()) {
John McCalla93c9342009-12-07 02:54:59 +00001884 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
Douglas Gregor802ab452009-12-02 22:36:29 +00001885 TemplateArgs,
1886 Init->getSourceLocation(),
1887 New->getDeclName());
John McCalla93c9342009-12-07 02:54:59 +00001888 if (!BaseTInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001889 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00001890 New->setInvalidDecl();
1891 continue;
1892 }
1893
John McCalla93c9342009-12-07 02:54:59 +00001894 NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001895 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001896 NewArgs.size(),
Douglas Gregor802ab452009-12-02 22:36:29 +00001897 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00001898 Init->getRParenLoc(),
1899 New->getParent());
1900 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001901 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00001902
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001903 // Is this an anonymous union?
1904 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregore95b4092009-09-16 18:34:49 +00001905 Member = cast<FieldDecl>(FindInstantiatedDecl(UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001906 else
Douglas Gregore95b4092009-09-16 18:34:49 +00001907 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMember(),
1908 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00001909
1910 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001911 NewArgs.size(),
1912 Init->getSourceLocation(),
Douglas Gregor802ab452009-12-02 22:36:29 +00001913 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00001914 Init->getRParenLoc());
1915 }
1916
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001917 if (NewInit.isInvalid()) {
1918 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00001919 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001920 } else {
Anders Carlsson09025312009-08-29 05:16:22 +00001921 // FIXME: It would be nice if ASTOwningVector had a release function.
1922 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001923
Anders Carlsson09025312009-08-29 05:16:22 +00001924 NewInits.push_back((MemInitTy *)NewInit.get());
1925 }
1926 }
Mike Stump1eb44332009-09-09 15:08:12 +00001927
Anders Carlsson09025312009-08-29 05:16:22 +00001928 // Assign all the initializers to the new constructor.
Mike Stump1eb44332009-09-09 15:08:12 +00001929 ActOnMemInitializers(DeclPtrTy::make(New),
Anders Carlsson09025312009-08-29 05:16:22 +00001930 /*FIXME: ColonLoc */
1931 SourceLocation(),
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001932 NewInits.data(), NewInits.size(),
1933 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00001934}
1935
John McCall52a575a2009-08-29 08:11:13 +00001936// TODO: this could be templated if the various decl types used the
1937// same method name.
1938static bool isInstantiationOf(ClassTemplateDecl *Pattern,
1939 ClassTemplateDecl *Instance) {
1940 Pattern = Pattern->getCanonicalDecl();
1941
1942 do {
1943 Instance = Instance->getCanonicalDecl();
1944 if (Pattern == Instance) return true;
1945 Instance = Instance->getInstantiatedFromMemberTemplate();
1946 } while (Instance);
1947
1948 return false;
1949}
1950
Douglas Gregor0d696532009-09-28 06:34:35 +00001951static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
1952 FunctionTemplateDecl *Instance) {
1953 Pattern = Pattern->getCanonicalDecl();
1954
1955 do {
1956 Instance = Instance->getCanonicalDecl();
1957 if (Pattern == Instance) return true;
1958 Instance = Instance->getInstantiatedFromMemberTemplate();
1959 } while (Instance);
1960
1961 return false;
1962}
1963
Douglas Gregored9c0f92009-10-29 00:04:11 +00001964static bool
1965isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
1966 ClassTemplatePartialSpecializationDecl *Instance) {
1967 Pattern
1968 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
1969 do {
1970 Instance = cast<ClassTemplatePartialSpecializationDecl>(
1971 Instance->getCanonicalDecl());
1972 if (Pattern == Instance)
1973 return true;
1974 Instance = Instance->getInstantiatedFromMember();
1975 } while (Instance);
1976
1977 return false;
1978}
1979
John McCall52a575a2009-08-29 08:11:13 +00001980static bool isInstantiationOf(CXXRecordDecl *Pattern,
1981 CXXRecordDecl *Instance) {
1982 Pattern = Pattern->getCanonicalDecl();
1983
1984 do {
1985 Instance = Instance->getCanonicalDecl();
1986 if (Pattern == Instance) return true;
1987 Instance = Instance->getInstantiatedFromMemberClass();
1988 } while (Instance);
1989
1990 return false;
1991}
1992
1993static bool isInstantiationOf(FunctionDecl *Pattern,
1994 FunctionDecl *Instance) {
1995 Pattern = Pattern->getCanonicalDecl();
1996
1997 do {
1998 Instance = Instance->getCanonicalDecl();
1999 if (Pattern == Instance) return true;
2000 Instance = Instance->getInstantiatedFromMemberFunction();
2001 } while (Instance);
2002
2003 return false;
2004}
2005
2006static bool isInstantiationOf(EnumDecl *Pattern,
2007 EnumDecl *Instance) {
2008 Pattern = Pattern->getCanonicalDecl();
2009
2010 do {
2011 Instance = Instance->getCanonicalDecl();
2012 if (Pattern == Instance) return true;
2013 Instance = Instance->getInstantiatedFromMemberEnum();
2014 } while (Instance);
2015
2016 return false;
2017}
2018
John McCalled976492009-12-04 22:46:56 +00002019static bool isInstantiationOf(UsingShadowDecl *Pattern,
2020 UsingShadowDecl *Instance,
2021 ASTContext &C) {
2022 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2023}
2024
2025static bool isInstantiationOf(UsingDecl *Pattern,
2026 UsingDecl *Instance,
2027 ASTContext &C) {
2028 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2029}
2030
John McCall7ba107a2009-11-18 02:36:19 +00002031static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
2032 UsingDecl *Instance,
2033 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002034 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00002035}
2036
2037static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00002038 UsingDecl *Instance,
2039 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002040 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00002041}
2042
John McCall52a575a2009-08-29 08:11:13 +00002043static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
2044 VarDecl *Instance) {
2045 assert(Instance->isStaticDataMember());
2046
2047 Pattern = Pattern->getCanonicalDecl();
2048
2049 do {
2050 Instance = Instance->getCanonicalDecl();
2051 if (Pattern == Instance) return true;
2052 Instance = Instance->getInstantiatedFromStaticDataMember();
2053 } while (Instance);
2054
2055 return false;
2056}
2057
John McCalled976492009-12-04 22:46:56 +00002058// Other is the prospective instantiation
2059// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00002060static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002061 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00002062 if (UnresolvedUsingTypenameDecl *UUD
2063 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
2064 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2065 return isInstantiationOf(UUD, UD, Ctx);
2066 }
2067 }
2068
2069 if (UnresolvedUsingValueDecl *UUD
2070 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002071 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2072 return isInstantiationOf(UUD, UD, Ctx);
2073 }
2074 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002075
Anders Carlsson0d8df782009-08-29 19:37:28 +00002076 return false;
2077 }
Mike Stump1eb44332009-09-09 15:08:12 +00002078
John McCall52a575a2009-08-29 08:11:13 +00002079 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
2080 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002081
John McCall52a575a2009-08-29 08:11:13 +00002082 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
2083 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00002084
John McCall52a575a2009-08-29 08:11:13 +00002085 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
2086 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00002087
Douglas Gregor7caa6822009-07-24 20:34:43 +00002088 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00002089 if (Var->isStaticDataMember())
2090 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
2091
2092 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
2093 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00002094
Douglas Gregor0d696532009-09-28 06:34:35 +00002095 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
2096 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
2097
Douglas Gregored9c0f92009-10-29 00:04:11 +00002098 if (ClassTemplatePartialSpecializationDecl *PartialSpec
2099 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2100 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2101 PartialSpec);
2102
Anders Carlssond8b285f2009-09-01 04:26:58 +00002103 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2104 if (!Field->getDeclName()) {
2105 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00002106 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00002107 cast<FieldDecl>(D);
2108 }
2109 }
Mike Stump1eb44332009-09-09 15:08:12 +00002110
John McCalled976492009-12-04 22:46:56 +00002111 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2112 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2113
2114 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2115 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2116
Douglas Gregor815215d2009-05-27 05:35:12 +00002117 return D->getDeclName() && isa<NamedDecl>(Other) &&
2118 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2119}
2120
2121template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00002122static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00002123 NamedDecl *D,
2124 ForwardIterator first,
2125 ForwardIterator last) {
2126 for (; first != last; ++first)
2127 if (isInstantiationOf(Ctx, D, *first))
2128 return cast<NamedDecl>(*first);
2129
2130 return 0;
2131}
2132
John McCall02cace72009-08-28 07:59:38 +00002133/// \brief Finds the instantiation of the given declaration context
2134/// within the current instantiation.
2135///
2136/// \returns NULL if there was an error
Douglas Gregore95b4092009-09-16 18:34:49 +00002137DeclContext *Sema::FindInstantiatedContext(DeclContext* DC,
2138 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00002139 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00002140 Decl* ID = FindInstantiatedDecl(D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00002141 return cast_or_null<DeclContext>(ID);
2142 } else return DC;
2143}
2144
Douglas Gregored961e72009-05-27 17:54:46 +00002145/// \brief Find the instantiation of the given declaration within the
2146/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00002147///
2148/// This routine is intended to be used when \p D is a declaration
2149/// referenced from within a template, that needs to mapped into the
2150/// corresponding declaration within an instantiation. For example,
2151/// given:
2152///
2153/// \code
2154/// template<typename T>
2155/// struct X {
2156/// enum Kind {
2157/// KnownValue = sizeof(T)
2158/// };
2159///
2160/// bool getKind() const { return KnownValue; }
2161/// };
2162///
2163/// template struct X<int>;
2164/// \endcode
2165///
2166/// In the instantiation of X<int>::getKind(), we need to map the
2167/// EnumConstantDecl for KnownValue (which refers to
2168/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00002169/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2170/// this mapping from within the instantiation of X<int>.
Douglas Gregore95b4092009-09-16 18:34:49 +00002171NamedDecl *Sema::FindInstantiatedDecl(NamedDecl *D,
2172 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00002173 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00002174 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00002175 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
Douglas Gregor550d9b22009-10-31 17:21:17 +00002176 ParentDC->isFunctionOrMethod()) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002177 // D is a local of some kind. Look into the map of local
2178 // declarations to their instantiations.
2179 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
2180 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002181
Douglas Gregore95b4092009-09-16 18:34:49 +00002182 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2183 if (!Record->isDependentContext())
2184 return D;
2185
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002186 // If the RecordDecl is actually the injected-class-name or a
2187 // "templated" declaration for a class template, class template
2188 // partial specialization, or a member class of a class template,
2189 // substitute into the injected-class-name of the class template
2190 // or partial specialization to find the new DeclContext.
Douglas Gregore95b4092009-09-16 18:34:49 +00002191 QualType T;
2192 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2193
2194 if (ClassTemplate) {
2195 T = ClassTemplate->getInjectedClassNameType(Context);
2196 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2197 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
2198 T = Context.getTypeDeclType(Record);
2199 ClassTemplate = PartialSpec->getSpecializedTemplate();
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002200 }
Douglas Gregore95b4092009-09-16 18:34:49 +00002201
2202 if (!T.isNull()) {
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002203 // Substitute into the injected-class-name to get the type
2204 // corresponding to the instantiation we want, which may also be
2205 // the current instantiation (if we're in a template
2206 // definition). This substitution should never fail, since we
2207 // know we can instantiate the injected-class-name or we
2208 // wouldn't have gotten to the injected-class-name!
2209
2210 // FIXME: Can we use the CurrentInstantiationScope to avoid this
2211 // extra instantiation in the common case?
Douglas Gregore95b4092009-09-16 18:34:49 +00002212 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2213 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2214
2215 if (!T->isDependentType()) {
2216 assert(T->isRecordType() && "Instantiation must produce a record type");
2217 return T->getAs<RecordType>()->getDecl();
2218 }
2219
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002220 // We are performing "partial" template instantiation to create
2221 // the member declarations for the members of a class template
2222 // specialization. Therefore, D is actually referring to something
2223 // in the current instantiation. Look through the current
2224 // context, which contains actual instantiations, to find the
2225 // instantiation of the "current instantiation" that D refers
2226 // to.
2227 bool SawNonDependentContext = false;
Mike Stump1eb44332009-09-09 15:08:12 +00002228 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00002229 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002230 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002231 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00002232 if (isInstantiationOf(ClassTemplate,
2233 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00002234 return Spec;
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002235
2236 if (!DC->isDependentContext())
2237 SawNonDependentContext = true;
John McCall52a575a2009-08-29 08:11:13 +00002238 }
2239
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002240 // We're performing "instantiation" of a member of the current
2241 // instantiation while we are type-checking the
2242 // definition. Compute the declaration context and return that.
2243 assert(!SawNonDependentContext &&
2244 "No dependent context while instantiating record");
2245 DeclContext *DC = computeDeclContext(T);
2246 assert(DC &&
John McCall52a575a2009-08-29 08:11:13 +00002247 "Unable to find declaration for the current instantiation");
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002248 return cast<CXXRecordDecl>(DC);
John McCall52a575a2009-08-29 08:11:13 +00002249 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002250
Douglas Gregore95b4092009-09-16 18:34:49 +00002251 // Fall through to deal with other dependent record types (e.g.,
2252 // anonymous unions in class templates).
2253 }
John McCall52a575a2009-08-29 08:11:13 +00002254
Douglas Gregore95b4092009-09-16 18:34:49 +00002255 if (!ParentDC->isDependentContext())
2256 return D;
2257
2258 ParentDC = FindInstantiatedContext(ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002259 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00002260 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002261
Douglas Gregor815215d2009-05-27 05:35:12 +00002262 if (ParentDC != D->getDeclContext()) {
2263 // We performed some kind of instantiation in the parent context,
2264 // so now we need to look into the instantiated parent context to
2265 // find the instantiation of the declaration D.
2266 NamedDecl *Result = 0;
2267 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002268 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00002269 Result = findInstantiationOf(Context, D, Found.first, Found.second);
2270 } else {
2271 // Since we don't have a name for the entity we're looking for,
2272 // our only option is to walk through all of the declarations to
2273 // find that name. This will occur in a few cases:
2274 //
2275 // - anonymous struct/union within a template
2276 // - unnamed class/struct/union/enum within a template
2277 //
2278 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00002279 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002280 ParentDC->decls_begin(),
2281 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00002282 }
Mike Stump1eb44332009-09-09 15:08:12 +00002283
John McCall9f54ad42009-12-10 09:41:52 +00002284 // UsingShadowDecls can instantiate to nothing because of using hiding.
2285 assert((Result || isa<UsingShadowDecl>(D))
2286 && "Unable to find instantiation of declaration!");
2287
Douglas Gregor815215d2009-05-27 05:35:12 +00002288 D = Result;
2289 }
2290
Douglas Gregor815215d2009-05-27 05:35:12 +00002291 return D;
2292}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002293
Mike Stump1eb44332009-09-09 15:08:12 +00002294/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002295/// instantiations we have seen until this point.
Douglas Gregor60406be2010-01-16 22:29:39 +00002296void Sema::PerformPendingImplicitInstantiations(bool LocalOnly) {
2297 while (!PendingLocalImplicitInstantiations.empty() ||
2298 (!LocalOnly && !PendingImplicitInstantiations.empty())) {
2299 PendingImplicitInstantiation Inst;
2300
2301 if (PendingLocalImplicitInstantiations.empty()) {
2302 Inst = PendingImplicitInstantiations.front();
2303 PendingImplicitInstantiations.pop_front();
2304 } else {
2305 Inst = PendingLocalImplicitInstantiations.front();
2306 PendingLocalImplicitInstantiations.pop_front();
2307 }
Mike Stump1eb44332009-09-09 15:08:12 +00002308
Douglas Gregor7caa6822009-07-24 20:34:43 +00002309 // Instantiate function definitions
2310 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002311 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002312 Function->getLocation(), *this,
2313 Context.getSourceManager(),
2314 "instantiating function definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002315
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00002316 if (!Function->getBody())
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002317 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00002318 continue;
2319 }
Mike Stump1eb44332009-09-09 15:08:12 +00002320
Douglas Gregor7caa6822009-07-24 20:34:43 +00002321 // Instantiate static data member definitions.
2322 VarDecl *Var = cast<VarDecl>(Inst.first);
2323 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002324
Mike Stump1eb44332009-09-09 15:08:12 +00002325 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002326 Var->getLocation(), *this,
2327 Context.getSourceManager(),
2328 "instantiating static data member "
2329 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002330
Douglas Gregor7caa6822009-07-24 20:34:43 +00002331 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002332 }
2333}