blob: 66a8b1f1e6120f75309b5462b39ab7027be7403d [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.
414 if (ND->getDeclContext()->isRecord())
415 NewND = SemaRef.FindInstantiatedDecl(ND, TemplateArgs);
Douglas Gregor7557a132009-12-24 20:56:24 +0000416 else if (D->wasSpecialization()) {
417 // Totally egregious hack to work around PR5866
418 return 0;
419 } else
John McCalle129d442009-12-17 23:21:11 +0000420 NewND = Visit(ND);
John McCall02cace72009-08-28 07:59:38 +0000421 if (!NewND) return 0;
422
423 FU = cast<NamedDecl>(NewND);
John McCallfd810b12009-08-14 02:03:10 +0000424 }
Mike Stump1eb44332009-09-09 15:08:12 +0000425
John McCall02cace72009-08-28 07:59:38 +0000426 FriendDecl *FD =
427 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU,
428 D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000429 FD->setAccess(AS_public);
John McCall02cace72009-08-28 07:59:38 +0000430 Owner->addDecl(FD);
431 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000432}
433
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000434Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
435 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Douglas Gregorac7610d2009-06-22 20:57:11 +0000437 // The expression in a static assertion is not potentially evaluated.
438 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000440 OwningExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000441 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000442 if (InstantiatedAssertExpr.isInvalid())
443 return 0;
444
Douglas Gregor43d9d922009-08-08 01:41:12 +0000445 OwningExprResult Message(SemaRef, D->getMessage());
446 D->getMessage()->Retain();
Mike Stump1eb44332009-09-09 15:08:12 +0000447 Decl *StaticAssert
448 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
Chris Lattnerb28317a2009-03-28 19:18:32 +0000449 move(InstantiatedAssertExpr),
450 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000451 return StaticAssert;
452}
453
454Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000455 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000456 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000457 D->getTagKeywordLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000458 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000459 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000460 Enum->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000461 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000462 Enum->startDefinition();
463
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000464 llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000465
466 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000467 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
468 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000469 EC != ECEnd; ++EC) {
470 // The specified value for the enumerator.
471 OwningExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000472 if (Expr *UninstValue = EC->getInitExpr()) {
473 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000474 EnterExpressionEvaluationContext Unevaluated(SemaRef,
Douglas Gregorac7610d2009-06-22 20:57:11 +0000475 Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000476
John McCallce3ff2b2009-08-25 22:02:44 +0000477 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000478 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000479
480 // Drop the initial value and continue.
481 bool isInvalid = false;
482 if (Value.isInvalid()) {
483 Value = SemaRef.Owned((Expr *)0);
484 isInvalid = true;
485 }
486
Mike Stump1eb44332009-09-09 15:08:12 +0000487 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000488 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
489 EC->getLocation(), EC->getIdentifier(),
490 move(Value));
491
492 if (isInvalid) {
493 if (EnumConst)
494 EnumConst->setInvalidDecl();
495 Enum->setInvalidDecl();
496 }
497
498 if (EnumConst) {
John McCall3b85ecf2010-01-23 22:37:59 +0000499 EnumConst->setAccess(Enum->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000500 Enum->addDecl(EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000501 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000502 LastEnumConst = EnumConst;
503 }
504 }
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000506 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000507 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000508 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
509 Sema::DeclPtrTy::make(Enum),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000510 &Enumerators[0], Enumerators.size(),
511 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000512
513 return Enum;
514}
515
Douglas Gregor6477b692009-03-25 15:04:13 +0000516Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
517 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
518 return 0;
519}
520
Douglas Gregored9c0f92009-10-29 00:04:11 +0000521namespace {
522 class SortDeclByLocation {
523 SourceManager &SourceMgr;
524
525 public:
526 explicit SortDeclByLocation(SourceManager &SourceMgr)
527 : SourceMgr(SourceMgr) { }
528
529 bool operator()(const Decl *X, const Decl *Y) const {
530 return SourceMgr.isBeforeInTranslationUnit(X->getLocation(),
531 Y->getLocation());
532 }
533 };
534}
535
John McCalle29ba202009-08-20 01:44:21 +0000536Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000537 // Create a local instantiation scope for this class template, which
538 // will contain the instantiations of the template parameters.
539 Sema::LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000540 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000541 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000542 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000543 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000544
545 CXXRecordDecl *Pattern = D->getTemplatedDecl();
546 CXXRecordDecl *RecordInst
547 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner,
548 Pattern->getLocation(), Pattern->getIdentifier(),
Douglas Gregorf0510d42009-10-12 23:11:44 +0000549 Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL,
550 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000551
552 ClassTemplateDecl *Inst
553 = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
554 D->getIdentifier(), InstParams, RecordInst, 0);
555 RecordInst->setDescribedClassTemplate(Inst);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000556 if (D->getFriendObjectKind())
557 Inst->setObjectOfFriendDecl(true);
558 else
559 Inst->setAccess(D->getAccess());
John McCalle29ba202009-08-20 01:44:21 +0000560 Inst->setInstantiatedFromMemberTemplate(D);
Douglas Gregorf0510d42009-10-12 23:11:44 +0000561
562 // Trigger creation of the type for the instantiation.
563 SemaRef.Context.getTypeDeclType(RecordInst);
564
Douglas Gregor259571e2009-10-30 22:42:42 +0000565 // Finish handling of friends.
566 if (Inst->getFriendObjectKind()) {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000567 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000568 }
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000569
John McCall46460a62010-01-20 21:53:11 +0000570 Inst->setAccess(D->getAccess());
John McCalle29ba202009-08-20 01:44:21 +0000571 Owner->addDecl(Inst);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000572
573 // First, we sort the partial specializations by location, so
574 // that we instantiate them in the order they were declared.
575 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
576 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
577 P = D->getPartialSpecializations().begin(),
578 PEnd = D->getPartialSpecializations().end();
579 P != PEnd; ++P)
580 PartialSpecs.push_back(&*P);
581 std::sort(PartialSpecs.begin(), PartialSpecs.end(),
582 SortDeclByLocation(SemaRef.SourceMgr));
583
584 // Instantiate all of the partial specializations of this member class
585 // template.
586 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
587 InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
588
John McCalle29ba202009-08-20 01:44:21 +0000589 return Inst;
590}
591
Douglas Gregord60e1052009-08-27 16:57:43 +0000592Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000593TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
594 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000595 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
596
597 // Lookup the already-instantiated declaration in the instantiation
598 // of the class template and return that.
599 DeclContext::lookup_result Found
600 = Owner->lookup(ClassTemplate->getDeclName());
601 if (Found.first == Found.second)
602 return 0;
603
604 ClassTemplateDecl *InstClassTemplate
605 = dyn_cast<ClassTemplateDecl>(*Found.first);
606 if (!InstClassTemplate)
607 return 0;
608
609 Decl *DCanon = D->getCanonicalDecl();
610 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
611 P = InstClassTemplate->getPartialSpecializations().begin(),
612 PEnd = InstClassTemplate->getPartialSpecializations().end();
613 P != PEnd; ++P) {
614 if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
615 return &*P;
616 }
617
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000618 return 0;
619}
620
621Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000622TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000623 // Create a local instantiation scope for this function template, which
624 // will contain the instantiations of the template parameters and then get
625 // merged with the local instantiation scope for the function template
626 // itself.
627 Sema::LocalInstantiationScope Scope(SemaRef);
628
Douglas Gregord60e1052009-08-27 16:57:43 +0000629 TemplateParameterList *TempParams = D->getTemplateParameters();
630 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000631 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000632 return NULL;
Douglas Gregored9c0f92009-10-29 00:04:11 +0000633
Douglas Gregora735b202009-10-13 14:39:41 +0000634 FunctionDecl *Instantiated = 0;
635 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
636 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
637 InstParams));
638 else
639 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
640 D->getTemplatedDecl(),
641 InstParams));
642
643 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000644 return 0;
645
John McCall46460a62010-01-20 21:53:11 +0000646 Instantiated->setAccess(D->getAccess());
647
Mike Stump1eb44332009-09-09 15:08:12 +0000648 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000649 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000650 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000651 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000652 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000653 assert(InstTemplate &&
654 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
John McCalle976ffe2009-12-14 23:19:40 +0000655
656 // Link the instantiation back to the pattern *unless* this is a
657 // non-definition friend declaration.
658 if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
659 !(InstTemplate->getFriendObjectKind() &&
660 !D->getTemplatedDecl()->isThisDeclarationADefinition()))
Douglas Gregora735b202009-10-13 14:39:41 +0000661 InstTemplate->setInstantiatedFromMemberTemplate(D);
662
663 // Add non-friends into the owner.
664 if (!InstTemplate->getFriendObjectKind())
665 Owner->addDecl(InstTemplate);
Douglas Gregord60e1052009-08-27 16:57:43 +0000666 return InstTemplate;
667}
668
Douglas Gregord475b8d2009-03-25 21:17:03 +0000669Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
670 CXXRecordDecl *PrevDecl = 0;
671 if (D->isInjectedClassName())
672 PrevDecl = cast<CXXRecordDecl>(Owner);
John McCall6c1c1b82009-12-15 22:29:06 +0000673 else if (D->getPreviousDeclaration()) {
674 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getPreviousDeclaration(),
675 TemplateArgs);
676 if (!Prev) return 0;
677 PrevDecl = cast<CXXRecordDecl>(Prev);
678 }
Douglas Gregord475b8d2009-03-25 21:17:03 +0000679
680 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000681 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000682 D->getLocation(), D->getIdentifier(),
683 D->getTagKeywordLoc(), PrevDecl);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000684 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000685 // FIXME: Check against AS_none is an ugly hack to work around the issue that
686 // the tag decls introduced by friend class declarations don't have an access
687 // specifier. Remove once this area of the code gets sorted out.
688 if (D->getAccess() != AS_none)
689 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000690 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000691 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000692
John McCall02cace72009-08-28 07:59:38 +0000693 // If the original function was part of a friend declaration,
694 // inherit its namespace state.
695 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
696 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
697
Anders Carlssond8b285f2009-09-01 04:26:58 +0000698 Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
699
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000700 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000701 return Record;
702}
703
John McCall02cace72009-08-28 07:59:38 +0000704/// Normal class members are of more specific types and therefore
705/// don't make it here. This function serves two purposes:
706/// 1) instantiating function templates
707/// 2) substituting friend declarations
708/// FIXME: preserve function definitions in case #2
Douglas Gregor7557a132009-12-24 20:56:24 +0000709Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
Douglas Gregora735b202009-10-13 14:39:41 +0000710 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000711 // Check whether there is already a function template specialization for
712 // this declaration.
713 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
714 void *InsertPos = 0;
Douglas Gregora735b202009-10-13 14:39:41 +0000715 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000716 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000717 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000718 TemplateArgs.getInnermost().getFlatArgumentList(),
719 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor828e2262009-07-29 16:09:57 +0000720 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000721
722 FunctionTemplateSpecializationInfo *Info
723 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor127102b2009-06-29 20:59:39 +0000724 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Douglas Gregor127102b2009-06-29 20:59:39 +0000726 // If we already have a function template specialization, return it.
727 if (Info)
728 return Info->Function;
729 }
Mike Stump1eb44332009-09-09 15:08:12 +0000730
Douglas Gregor79c22782010-01-16 20:21:20 +0000731 bool MergeWithParentScope = (TemplateParams != 0) ||
732 !(isa<Decl>(Owner) &&
733 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
734 Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000735
Douglas Gregore53060f2009-06-25 22:08:12 +0000736 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000737 QualType T = SubstFunctionType(D, Params);
Douglas Gregore53060f2009-06-25 22:08:12 +0000738 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000739 return 0;
John McCallfd810b12009-08-14 02:03:10 +0000740
Douglas Gregore53060f2009-06-25 22:08:12 +0000741 // Build the instantiated method declaration.
Douglas Gregore95b4092009-09-16 18:34:49 +0000742 DeclContext *DC = SemaRef.FindInstantiatedContext(D->getDeclContext(),
743 TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +0000744 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +0000745 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
John McCalla93c9342009-12-07 02:54:59 +0000746 D->getDeclName(), T, D->getTypeSourceInfo(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000747 D->getStorageClass(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000748 D->isInlineSpecified(), D->hasWrittenPrototype());
John McCall02cace72009-08-28 07:59:38 +0000749 Function->setLexicalDeclContext(Owner);
Mike Stump1eb44332009-09-09 15:08:12 +0000750
Douglas Gregore53060f2009-06-25 22:08:12 +0000751 // Attach the parameters
752 for (unsigned P = 0; P < Params.size(); ++P)
753 Params[P]->setOwningFunction(Function);
754 Function->setParams(SemaRef.Context, Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +0000755
Douglas Gregora735b202009-10-13 14:39:41 +0000756 if (TemplateParams) {
757 // Our resulting instantiation is actually a function template, since we
758 // are substituting only the outer template parameters. For example, given
759 //
760 // template<typename T>
761 // struct X {
762 // template<typename U> friend void f(T, U);
763 // };
764 //
765 // X<int> x;
766 //
767 // We are instantiating the friend function template "f" within X<int>,
768 // which means substituting int for T, but leaving "f" as a friend function
769 // template.
770 // Build the function template itself.
771 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Owner,
772 Function->getLocation(),
773 Function->getDeclName(),
774 TemplateParams, Function);
775 Function->setDescribedFunctionTemplate(FunctionTemplate);
776 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregor66724ea2009-11-14 01:20:54 +0000777 } else if (FunctionTemplate) {
778 // Record this function template specialization.
779 Function->setFunctionTemplateSpecialization(SemaRef.Context,
780 FunctionTemplate,
781 &TemplateArgs.getInnermost(),
782 InsertPos);
John McCall02cace72009-08-28 07:59:38 +0000783 }
Douglas Gregora735b202009-10-13 14:39:41 +0000784
Douglas Gregore53060f2009-06-25 22:08:12 +0000785 if (InitFunctionInstantiation(Function, D))
786 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000787
Douglas Gregore53060f2009-06-25 22:08:12 +0000788 bool Redeclaration = false;
789 bool OverloadableAttrRequired = false;
Douglas Gregora735b202009-10-13 14:39:41 +0000790
John McCall68263142009-11-18 22:49:29 +0000791 LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
792 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
793
Douglas Gregora735b202009-10-13 14:39:41 +0000794 if (TemplateParams || !FunctionTemplate) {
795 // Look only into the namespace where the friend would be declared to
796 // find a previous declaration. This is the innermost enclosing namespace,
797 // as described in ActOnFriendFunctionDecl.
John McCall68263142009-11-18 22:49:29 +0000798 SemaRef.LookupQualifiedName(Previous, DC);
Douglas Gregora735b202009-10-13 14:39:41 +0000799
Douglas Gregora735b202009-10-13 14:39:41 +0000800 // In C++, the previous declaration we find might be a tag type
801 // (class or enum). In this case, the new declaration will hide the
802 // tag type. Note that this does does not apply if we're declaring a
803 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +0000804 if (Previous.isSingleTagDecl())
805 Previous.clear();
Douglas Gregora735b202009-10-13 14:39:41 +0000806 }
807
John McCall9f54ad42009-12-10 09:41:52 +0000808 SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
809 false, Redeclaration,
Douglas Gregore53060f2009-06-25 22:08:12 +0000810 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000811
Douglas Gregora735b202009-10-13 14:39:41 +0000812 // If the original function was part of a friend declaration,
813 // inherit its namespace state and add it to the owner.
814 NamedDecl *FromFriendD
815 = TemplateParams? cast<NamedDecl>(D->getDescribedFunctionTemplate()) : D;
816 if (FromFriendD->getFriendObjectKind()) {
817 NamedDecl *ToFriendD = 0;
John McCall68263142009-11-18 22:49:29 +0000818 NamedDecl *PrevDecl;
Douglas Gregora735b202009-10-13 14:39:41 +0000819 if (TemplateParams) {
820 ToFriendD = cast<NamedDecl>(FunctionTemplate);
821 PrevDecl = FunctionTemplate->getPreviousDeclaration();
822 } else {
823 ToFriendD = Function;
824 PrevDecl = Function->getPreviousDeclaration();
825 }
826 ToFriendD->setObjectOfFriendDecl(PrevDecl != NULL);
827 if (!Owner->isDependentContext() && !PrevDecl)
828 DC->makeDeclVisibleInContext(ToFriendD, /* Recoverable = */ false);
829
830 if (!TemplateParams)
831 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
832 }
833
Douglas Gregore53060f2009-06-25 22:08:12 +0000834 return Function;
835}
836
Douglas Gregord60e1052009-08-27 16:57:43 +0000837Decl *
838TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
839 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +0000840 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
841 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +0000842 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +0000843 // We are creating a function template specialization from a function
844 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +0000845 // specialization for this particular set of template arguments.
Douglas Gregor6b906862009-08-21 00:16:32 +0000846 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000847 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000848 TemplateArgs.getInnermost().getFlatArgumentList(),
849 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000850 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000851
852 FunctionTemplateSpecializationInfo *Info
853 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor6b906862009-08-21 00:16:32 +0000854 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000855
Douglas Gregor6b906862009-08-21 00:16:32 +0000856 // If we already have a function template specialization, return it.
857 if (Info)
858 return Info->Function;
859 }
860
Douglas Gregor79c22782010-01-16 20:21:20 +0000861 bool MergeWithParentScope = (TemplateParams != 0) ||
862 !(isa<Decl>(Owner) &&
863 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
864 Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000865
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000866 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000867 QualType T = SubstFunctionType(D, Params);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000868 if (T.isNull())
869 return 0;
870
871 // Build the instantiated method declaration.
872 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
Douglas Gregordec06662009-08-21 18:42:58 +0000873 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Douglas Gregordec06662009-08-21 18:42:58 +0000875 DeclarationName Name = D->getDeclName();
Douglas Gregor17e32f32009-08-21 22:43:28 +0000876 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregordec06662009-08-21 18:42:58 +0000877 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
878 Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
879 SemaRef.Context.getCanonicalType(ClassTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000880 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
881 Constructor->getLocation(),
882 Name, T,
John McCalla93c9342009-12-07 02:54:59 +0000883 Constructor->getTypeSourceInfo(),
Mike Stump1eb44332009-09-09 15:08:12 +0000884 Constructor->isExplicit(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000885 Constructor->isInlineSpecified(), false);
Douglas Gregor17e32f32009-08-21 22:43:28 +0000886 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
887 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
888 Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
889 SemaRef.Context.getCanonicalType(ClassTy));
890 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
891 Destructor->getLocation(), Name,
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000892 T, Destructor->isInlineSpecified(), false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000893 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000894 CanQualType ConvTy
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000895 = SemaRef.Context.getCanonicalType(
John McCall183700f2009-09-21 23:43:11 +0000896 T->getAs<FunctionType>()->getResultType());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000897 Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
898 ConvTy);
899 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
900 Conversion->getLocation(), Name,
John McCalla93c9342009-12-07 02:54:59 +0000901 T, Conversion->getTypeSourceInfo(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000902 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000903 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +0000904 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000905 Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
John McCalla93c9342009-12-07 02:54:59 +0000906 D->getDeclName(), T, D->getTypeSourceInfo(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000907 D->isStatic(), D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +0000908 }
Douglas Gregor6b906862009-08-21 00:16:32 +0000909
Douglas Gregord60e1052009-08-27 16:57:43 +0000910 if (TemplateParams) {
911 // Our resulting instantiation is actually a function template, since we
912 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +0000913 //
Douglas Gregord60e1052009-08-27 16:57:43 +0000914 // template<typename T>
915 // struct X {
916 // template<typename U> void f(T, U);
917 // };
918 //
919 // X<int> x;
920 //
921 // We are instantiating the member template "f" within X<int>, which means
922 // substituting int for T, but leaving "f" as a member function template.
923 // Build the function template itself.
924 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
925 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +0000926 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +0000927 TemplateParams, Method);
928 if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +0000929 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +0000930 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +0000931 } else if (FunctionTemplate) {
932 // Record this function template specialization.
933 Method->setFunctionTemplateSpecialization(SemaRef.Context,
934 FunctionTemplate,
935 &TemplateArgs.getInnermost(),
936 InsertPos);
937 } else {
938 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +0000939 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +0000940 }
941
Mike Stump1eb44332009-09-09 15:08:12 +0000942 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000943 // out-of-line, the instantiation will have the same lexical
944 // context (which will be a namespace scope) as the template.
945 if (D->isOutOfLine())
946 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000947
Douglas Gregor5545e162009-03-24 00:38:23 +0000948 // Attach the parameters
949 for (unsigned P = 0; P < Params.size(); ++P)
950 Params[P]->setOwningFunction(Method);
Jay Foadbeaaccd2009-05-21 09:52:38 +0000951 Method->setParams(SemaRef.Context, Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +0000952
953 if (InitMethodInstantiation(Method, D))
954 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000955
John McCall68263142009-11-18 22:49:29 +0000956 LookupResult Previous(SemaRef, Name, SourceLocation(),
957 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000958
Douglas Gregord60e1052009-08-27 16:57:43 +0000959 if (!FunctionTemplate || TemplateParams) {
John McCall68263142009-11-18 22:49:29 +0000960 SemaRef.LookupQualifiedName(Previous, Owner);
Mike Stump1eb44332009-09-09 15:08:12 +0000961
Douglas Gregordec06662009-08-21 18:42:58 +0000962 // In C++, the previous declaration we find might be a tag type
963 // (class or enum). In this case, the new declaration will hide the
964 // tag type. Note that this does does not apply if we're declaring a
965 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +0000966 if (Previous.isSingleTagDecl())
967 Previous.clear();
Douglas Gregordec06662009-08-21 18:42:58 +0000968 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000969
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000970 bool Redeclaration = false;
971 bool OverloadableAttrRequired = false;
John McCall9f54ad42009-12-10 09:41:52 +0000972 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000973 /*FIXME:*/OverloadableAttrRequired);
974
Douglas Gregor4ba31362009-12-01 17:24:26 +0000975 if (D->isPure())
976 SemaRef.CheckPureMethod(Method, SourceRange());
977
John McCall46460a62010-01-20 21:53:11 +0000978 Method->setAccess(D->getAccess());
979
John McCall68263142009-11-18 22:49:29 +0000980 if (!FunctionTemplate && (!Method->isInvalidDecl() || Previous.empty()) &&
Douglas Gregora735b202009-10-13 14:39:41 +0000981 !Method->getFriendObjectKind())
Douglas Gregordec06662009-08-21 18:42:58 +0000982 Owner->addDecl(Method);
Mike Stump1eb44332009-09-09 15:08:12 +0000983
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000984 return Method;
985}
986
Douglas Gregor615c5d42009-03-24 16:43:20 +0000987Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +0000988 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000989}
990
Douglas Gregor03b2b072009-03-24 00:15:49 +0000991Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +0000992 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +0000993}
994
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000995Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000996 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000997}
998
Douglas Gregor6477b692009-03-25 15:04:13 +0000999ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
John McCall58e46772009-10-23 21:48:59 +00001000 QualType T;
John McCalla93c9342009-12-07 02:54:59 +00001001 TypeSourceInfo *DI = D->getTypeSourceInfo();
John McCall58e46772009-10-23 21:48:59 +00001002 if (DI) {
1003 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
1004 D->getDeclName());
1005 if (DI) T = DI->getType();
1006 } else {
1007 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
1008 D->getDeclName());
1009 DI = 0;
1010 }
1011
1012 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001013 return 0;
1014
John McCall58e46772009-10-23 21:48:59 +00001015 T = SemaRef.adjustParameterType(T);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001016
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001017 // Allocate the parameter
John McCall58e46772009-10-23 21:48:59 +00001018 ParmVarDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +00001019 = ParmVarDecl::Create(SemaRef.Context,
1020 SemaRef.Context.getTranslationUnitDecl(),
1021 D->getLocation(),
John McCall58e46772009-10-23 21:48:59 +00001022 D->getIdentifier(), T, DI, D->getStorageClass(), 0);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001023
Anders Carlsson9351c172009-08-25 03:18:48 +00001024 // Mark the default argument as being uninstantiated.
Douglas Gregorf43d0b32009-09-25 06:56:31 +00001025 if (D->hasUninstantiatedDefaultArg())
1026 Param->setUninstantiatedDefaultArg(D->getUninstantiatedDefaultArg());
Douglas Gregor0ed09302009-09-25 07:03:22 +00001027 else if (Expr *Arg = D->getDefaultArg())
1028 Param->setUninstantiatedDefaultArg(Arg);
1029
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001030 // Note: we don't try to instantiate function parameters until after
1031 // we've instantiated the function's type. Therefore, we don't have
1032 // to check for 'void' parameter types here.
Douglas Gregor48dd19b2009-05-14 21:44:34 +00001033 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001034 return Param;
1035}
1036
John McCalle29ba202009-08-20 01:44:21 +00001037Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1038 TemplateTypeParmDecl *D) {
1039 // TODO: don't always clone when decls are refcounted.
1040 const Type* T = D->getTypeForDecl();
1041 assert(T->isTemplateTypeParmType());
1042 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001043
John McCalle29ba202009-08-20 01:44:21 +00001044 TemplateTypeParmDecl *Inst =
1045 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor550d9b22009-10-31 17:21:17 +00001046 TTPT->getDepth() - 1, TTPT->getIndex(),
John McCalle29ba202009-08-20 01:44:21 +00001047 TTPT->getName(),
1048 D->wasDeclaredWithTypename(),
1049 D->isParameterPack());
1050
Douglas Gregor0f8716b2009-11-09 19:17:50 +00001051 if (D->hasDefaultArgument())
1052 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
John McCalle29ba202009-08-20 01:44:21 +00001053
Douglas Gregor550d9b22009-10-31 17:21:17 +00001054 // Introduce this template parameter's instantiation into the instantiation
1055 // scope.
1056 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1057
John McCalle29ba202009-08-20 01:44:21 +00001058 return Inst;
1059}
1060
Douglas Gregor33642df2009-10-23 23:25:44 +00001061Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1062 NonTypeTemplateParmDecl *D) {
1063 // Substitute into the type of the non-type template parameter.
1064 QualType T;
John McCalla93c9342009-12-07 02:54:59 +00001065 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor33642df2009-10-23 23:25:44 +00001066 if (DI) {
1067 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
1068 D->getDeclName());
1069 if (DI) T = DI->getType();
1070 } else {
1071 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
1072 D->getDeclName());
1073 DI = 0;
1074 }
1075 if (T.isNull())
1076 return 0;
1077
1078 // Check that this type is acceptable for a non-type template parameter.
1079 bool Invalid = false;
1080 T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
1081 if (T.isNull()) {
1082 T = SemaRef.Context.IntTy;
1083 Invalid = true;
1084 }
1085
1086 NonTypeTemplateParmDecl *Param
1087 = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1088 D->getDepth() - 1, D->getPosition(),
1089 D->getIdentifier(), T, DI);
1090 if (Invalid)
1091 Param->setInvalidDecl();
1092
1093 Param->setDefaultArgument(D->getDefaultArgument());
Douglas Gregor550d9b22009-10-31 17:21:17 +00001094
1095 // Introduce this template parameter's instantiation into the instantiation
1096 // scope.
1097 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001098 return Param;
1099}
1100
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001101Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001102TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1103 TemplateTemplateParmDecl *D) {
1104 // Instantiate the template parameter list of the template template parameter.
1105 TemplateParameterList *TempParams = D->getTemplateParameters();
1106 TemplateParameterList *InstParams;
1107 {
1108 // Perform the actual substitution of template parameters within a new,
1109 // local instantiation scope.
1110 Sema::LocalInstantiationScope Scope(SemaRef);
1111 InstParams = SubstTemplateParams(TempParams);
1112 if (!InstParams)
1113 return NULL;
1114 }
1115
1116 // Build the template template parameter.
1117 TemplateTemplateParmDecl *Param
1118 = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1119 D->getDepth() - 1, D->getPosition(),
1120 D->getIdentifier(), InstParams);
1121 Param->setDefaultArgument(D->getDefaultArgument());
1122
1123 // Introduce this template parameter's instantiation into the instantiation
1124 // scope.
1125 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1126
1127 return Param;
1128}
1129
Douglas Gregor48c32a72009-11-17 06:07:40 +00001130Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1131 // Using directives are never dependent, so they require no explicit
1132
1133 UsingDirectiveDecl *Inst
1134 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1135 D->getNamespaceKeyLocation(),
1136 D->getQualifierRange(), D->getQualifier(),
1137 D->getIdentLocation(),
1138 D->getNominatedNamespace(),
1139 D->getCommonAncestor());
1140 Owner->addDecl(Inst);
1141 return Inst;
1142}
1143
John McCalled976492009-12-04 22:46:56 +00001144Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
1145 // The nested name specifier is non-dependent, so no transformation
1146 // is required.
1147
John McCall9f54ad42009-12-10 09:41:52 +00001148 // We only need to do redeclaration lookups if we're in a class
1149 // scope (in fact, it's not really even possible in non-class
1150 // scopes).
1151 bool CheckRedeclaration = Owner->isRecord();
1152
1153 LookupResult Prev(SemaRef, D->getDeclName(), D->getLocation(),
1154 Sema::LookupUsingDeclName, Sema::ForRedeclaration);
1155
John McCalled976492009-12-04 22:46:56 +00001156 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
1157 D->getLocation(),
1158 D->getNestedNameRange(),
1159 D->getUsingLocation(),
1160 D->getTargetNestedNameDecl(),
1161 D->getDeclName(),
1162 D->isTypeName());
1163
1164 CXXScopeSpec SS;
1165 SS.setScopeRep(D->getTargetNestedNameDecl());
1166 SS.setRange(D->getNestedNameRange());
John McCall9f54ad42009-12-10 09:41:52 +00001167
1168 if (CheckRedeclaration) {
1169 Prev.setHideTags(false);
1170 SemaRef.LookupQualifiedName(Prev, Owner);
1171
1172 // Check for invalid redeclarations.
1173 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
1174 D->isTypeName(), SS,
1175 D->getLocation(), Prev))
1176 NewUD->setInvalidDecl();
1177
1178 }
1179
1180 if (!NewUD->isInvalidDecl() &&
1181 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
John McCalled976492009-12-04 22:46:56 +00001182 D->getLocation()))
1183 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00001184
John McCalled976492009-12-04 22:46:56 +00001185 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1186 NewUD->setAccess(D->getAccess());
1187 Owner->addDecl(NewUD);
1188
John McCall9f54ad42009-12-10 09:41:52 +00001189 // Don't process the shadow decls for an invalid decl.
1190 if (NewUD->isInvalidDecl())
1191 return NewUD;
1192
John McCall323c3102009-12-22 22:26:37 +00001193 bool isFunctionScope = Owner->isFunctionOrMethod();
1194
John McCall9f54ad42009-12-10 09:41:52 +00001195 // Process the shadow decls.
1196 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
1197 I != E; ++I) {
1198 UsingShadowDecl *Shadow = *I;
1199 NamedDecl *InstTarget =
1200 cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getTargetDecl(),
1201 TemplateArgs));
1202
1203 if (CheckRedeclaration &&
1204 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
1205 continue;
1206
1207 UsingShadowDecl *InstShadow
1208 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
1209 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
John McCall323c3102009-12-22 22:26:37 +00001210
1211 if (isFunctionScope)
1212 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
John McCall9f54ad42009-12-10 09:41:52 +00001213 }
John McCalled976492009-12-04 22:46:56 +00001214
1215 return NewUD;
1216}
1217
1218Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00001219 // Ignore these; we handle them in bulk when processing the UsingDecl.
1220 return 0;
John McCalled976492009-12-04 22:46:56 +00001221}
1222
John McCall7ba107a2009-11-18 02:36:19 +00001223Decl * TemplateDeclInstantiator
1224 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +00001225 NestedNameSpecifier *NNS =
1226 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1227 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001228 TemplateArgs);
1229 if (!NNS)
1230 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001231
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001232 CXXScopeSpec SS;
1233 SS.setRange(D->getTargetNestedNameRange());
1234 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +00001235
1236 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00001237 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
John McCall7ba107a2009-11-18 02:36:19 +00001238 D->getUsingLoc(), SS, D->getLocation(),
1239 D->getDeclName(), 0,
1240 /*instantiation*/ true,
1241 /*typename*/ true, D->getTypenameLoc());
1242 if (UD)
John McCalled976492009-12-04 22:46:56 +00001243 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1244
John McCall7ba107a2009-11-18 02:36:19 +00001245 return UD;
1246}
1247
1248Decl * TemplateDeclInstantiator
1249 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1250 NestedNameSpecifier *NNS =
1251 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1252 D->getTargetNestedNameRange(),
1253 TemplateArgs);
1254 if (!NNS)
1255 return 0;
1256
1257 CXXScopeSpec SS;
1258 SS.setRange(D->getTargetNestedNameRange());
1259 SS.setScopeRep(NNS);
1260
1261 NamedDecl *UD =
1262 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1263 D->getUsingLoc(), SS, D->getLocation(),
1264 D->getDeclName(), 0,
1265 /*instantiation*/ true,
1266 /*typename*/ false, SourceLocation());
Anders Carlsson0d8df782009-08-29 19:37:28 +00001267 if (UD)
John McCalled976492009-12-04 22:46:56 +00001268 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1269
Anders Carlsson0d8df782009-08-29 19:37:28 +00001270 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001271}
1272
John McCallce3ff2b2009-08-25 22:02:44 +00001273Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001274 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001275 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001276 return Instantiator.Visit(D);
1277}
1278
John McCalle29ba202009-08-20 01:44:21 +00001279/// \brief Instantiates a nested template parameter list in the current
1280/// instantiation context.
1281///
1282/// \param L The parameter list to instantiate
1283///
1284/// \returns NULL if there was an error
1285TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001286TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001287 // Get errors for all the parameters before bailing out.
1288 bool Invalid = false;
1289
1290 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001291 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001292 ParamVector Params;
1293 Params.reserve(N);
1294 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1295 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001296 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001297 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001298 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00001299 }
1300
1301 // Clean up if we had an error.
1302 if (Invalid) {
1303 for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
1304 PI != PE; ++PI)
1305 if (*PI)
1306 (*PI)->Destroy(SemaRef.Context);
1307 return NULL;
1308 }
1309
1310 TemplateParameterList *InstL
1311 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1312 L->getLAngleLoc(), &Params.front(), N,
1313 L->getRAngleLoc());
1314 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001315}
John McCalle29ba202009-08-20 01:44:21 +00001316
Douglas Gregored9c0f92009-10-29 00:04:11 +00001317/// \brief Instantiate the declaration of a class template partial
1318/// specialization.
1319///
1320/// \param ClassTemplate the (instantiated) class template that is partially
1321// specialized by the instantiation of \p PartialSpec.
1322///
1323/// \param PartialSpec the (uninstantiated) class template partial
1324/// specialization that we are instantiating.
1325///
1326/// \returns true if there was an error, false otherwise.
1327bool
1328TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1329 ClassTemplateDecl *ClassTemplate,
1330 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001331 // Create a local instantiation scope for this class template partial
1332 // specialization, which will contain the instantiations of the template
1333 // parameters.
1334 Sema::LocalInstantiationScope Scope(SemaRef);
1335
Douglas Gregored9c0f92009-10-29 00:04:11 +00001336 // Substitute into the template parameters of the class template partial
1337 // specialization.
1338 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1339 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1340 if (!InstParams)
1341 return true;
1342
1343 // Substitute into the template arguments of the class template partial
1344 // specialization.
John McCall833ca992009-10-29 08:12:44 +00001345 const TemplateArgumentLoc *PartialSpecTemplateArgs
1346 = PartialSpec->getTemplateArgsAsWritten();
1347 unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1348
John McCalld5532b62009-11-23 01:53:49 +00001349 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
John McCall833ca992009-10-29 08:12:44 +00001350 for (unsigned I = 0; I != N; ++I) {
John McCalld5532b62009-11-23 01:53:49 +00001351 TemplateArgumentLoc Loc;
1352 if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs))
Douglas Gregored9c0f92009-10-29 00:04:11 +00001353 return true;
John McCalld5532b62009-11-23 01:53:49 +00001354 InstTemplateArgs.addArgument(Loc);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001355 }
1356
1357
1358 // Check that the template argument list is well-formed for this
1359 // class template.
1360 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1361 InstTemplateArgs.size());
1362 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1363 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001364 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001365 false,
1366 Converted))
1367 return true;
1368
1369 // Figure out where to insert this class template partial specialization
1370 // in the member template's set of class template partial specializations.
1371 llvm::FoldingSetNodeID ID;
1372 ClassTemplatePartialSpecializationDecl::Profile(ID,
1373 Converted.getFlatArguments(),
1374 Converted.flatSize(),
1375 SemaRef.Context);
1376 void *InsertPos = 0;
1377 ClassTemplateSpecializationDecl *PrevDecl
1378 = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
1379 InsertPos);
1380
1381 // Build the canonical type that describes the converted template
1382 // arguments of the class template partial specialization.
1383 QualType CanonType
1384 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1385 Converted.getFlatArguments(),
1386 Converted.flatSize());
1387
1388 // Build the fully-sugared type for this class template
1389 // specialization as the user wrote in the specialization
1390 // itself. This means that we'll pretty-print the type retrieved
1391 // from the specialization's declaration the way that the user
1392 // actually wrote the specialization, rather than formatting the
1393 // name based on the "canonical" representation used to store the
1394 // template arguments in the specialization.
1395 QualType WrittenTy
1396 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
John McCalld5532b62009-11-23 01:53:49 +00001397 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001398 CanonType);
1399
1400 if (PrevDecl) {
1401 // We've already seen a partial specialization with the same template
1402 // parameters and template arguments. This can happen, for example, when
1403 // substituting the outer template arguments ends up causing two
1404 // class template partial specializations of a member class template
1405 // to have identical forms, e.g.,
1406 //
1407 // template<typename T, typename U>
1408 // struct Outer {
1409 // template<typename X, typename Y> struct Inner;
1410 // template<typename Y> struct Inner<T, Y>;
1411 // template<typename Y> struct Inner<U, Y>;
1412 // };
1413 //
1414 // Outer<int, int> outer; // error: the partial specializations of Inner
1415 // // have the same signature.
1416 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1417 << WrittenTy;
1418 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1419 << SemaRef.Context.getTypeDeclType(PrevDecl);
1420 return true;
1421 }
1422
1423
1424 // Create the class template partial specialization declaration.
1425 ClassTemplatePartialSpecializationDecl *InstPartialSpec
1426 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context, Owner,
1427 PartialSpec->getLocation(),
1428 InstParams,
1429 ClassTemplate,
1430 Converted,
John McCalld5532b62009-11-23 01:53:49 +00001431 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001432 0);
1433 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
1434 InstPartialSpec->setTypeAsWritten(WrittenTy);
1435
1436 // Add this partial specialization to the set of class template partial
1437 // specializations.
1438 ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec,
1439 InsertPos);
1440 return false;
1441}
1442
John McCallce3ff2b2009-08-25 22:02:44 +00001443/// \brief Does substitution on the type of the given function, including
1444/// all of the function parameters.
Douglas Gregor5545e162009-03-24 00:38:23 +00001445///
John McCallce3ff2b2009-08-25 22:02:44 +00001446/// \param D The function whose type will be the basis of the substitution
Douglas Gregor5545e162009-03-24 00:38:23 +00001447///
1448/// \param Params the instantiated parameter declarations
1449
John McCallce3ff2b2009-08-25 22:02:44 +00001450/// \returns the instantiated function's type if successful, a NULL
Douglas Gregor5545e162009-03-24 00:38:23 +00001451/// type if there was an error.
Mike Stump1eb44332009-09-09 15:08:12 +00001452QualType
John McCallce3ff2b2009-08-25 22:02:44 +00001453TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001454 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
1455 bool InvalidDecl = false;
1456
John McCallce3ff2b2009-08-25 22:02:44 +00001457 // Substitute all of the function's formal parameter types.
Douglas Gregor7e063902009-05-11 23:53:27 +00001458 TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001459 llvm::SmallVector<QualType, 4> ParamTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001460 for (FunctionDecl::param_iterator P = D->param_begin(),
Douglas Gregor5545e162009-03-24 00:38:23 +00001461 PEnd = D->param_end();
1462 P != PEnd; ++P) {
Douglas Gregor6477b692009-03-25 15:04:13 +00001463 if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
Douglas Gregor5545e162009-03-24 00:38:23 +00001464 if (PInst->getType()->isVoidType()) {
1465 SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
1466 PInst->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001467 } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001468 PInst->getType(),
1469 diag::err_abstract_type_in_decl,
1470 Sema::AbstractParamType))
Douglas Gregor5545e162009-03-24 00:38:23 +00001471 PInst->setInvalidDecl();
1472
1473 Params.push_back(PInst);
1474 ParamTys.push_back(PInst->getType());
1475
1476 if (PInst->isInvalidDecl())
1477 InvalidDecl = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001478 } else
Douglas Gregor5545e162009-03-24 00:38:23 +00001479 InvalidDecl = true;
1480 }
1481
1482 // FIXME: Deallocate dead declarations.
1483 if (InvalidDecl)
1484 return QualType();
1485
John McCall183700f2009-09-21 23:43:11 +00001486 const FunctionProtoType *Proto = D->getType()->getAs<FunctionProtoType>();
Douglas Gregor5545e162009-03-24 00:38:23 +00001487 assert(Proto && "Missing prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +00001488 QualType ResultType
John McCallce3ff2b2009-08-25 22:02:44 +00001489 = SemaRef.SubstType(Proto->getResultType(), TemplateArgs,
1490 D->getLocation(), D->getDeclName());
Douglas Gregor5545e162009-03-24 00:38:23 +00001491 if (ResultType.isNull())
1492 return QualType();
1493
Jay Foadbeaaccd2009-05-21 09:52:38 +00001494 return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
Douglas Gregor5545e162009-03-24 00:38:23 +00001495 Proto->isVariadic(), Proto->getTypeQuals(),
1496 D->getLocation(), D->getDeclName());
1497}
1498
Mike Stump1eb44332009-09-09 15:08:12 +00001499/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00001500/// declaration (New) from the corresponding fields of its template (Tmpl).
1501///
1502/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001503bool
1504TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00001505 FunctionDecl *Tmpl) {
1506 if (Tmpl->isDeleted())
1507 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00001508
Douglas Gregorcca9e962009-07-01 22:01:06 +00001509 // If we are performing substituting explicitly-specified template arguments
1510 // or deduced template arguments into a function template and we reach this
1511 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00001512 // to keeping the new function template specialization. We therefore
1513 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00001514 // into a template instantiation for this specific function template
1515 // specialization, which is not a SFINAE context, so that we diagnose any
1516 // further errors in the declaration itself.
1517 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1518 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1519 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1520 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00001521 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00001522 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001523 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00001524 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00001525 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001526 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1527 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
Douglas Gregorf35f8282009-11-11 21:54:23 +00001528 --SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001529 }
1530 }
Mike Stump1eb44332009-09-09 15:08:12 +00001531
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001532 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
1533 assert(Proto && "Function template without prototype?");
1534
1535 if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
1536 Proto->getNoReturnAttr()) {
1537 // The function has an exception specification or a "noreturn"
1538 // attribute. Substitute into each of the exception types.
1539 llvm::SmallVector<QualType, 4> Exceptions;
1540 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
1541 // FIXME: Poor location information!
1542 QualType T
1543 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
1544 New->getLocation(), New->getDeclName());
1545 if (T.isNull() ||
1546 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
1547 continue;
1548
1549 Exceptions.push_back(T);
1550 }
1551
1552 // Rebuild the function type
1553
1554 const FunctionProtoType *NewProto
1555 = New->getType()->getAs<FunctionProtoType>();
1556 assert(NewProto && "Template instantiation without function prototype?");
1557 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
1558 NewProto->arg_type_begin(),
1559 NewProto->getNumArgs(),
1560 NewProto->isVariadic(),
1561 NewProto->getTypeQuals(),
1562 Proto->hasExceptionSpec(),
1563 Proto->hasAnyExceptionSpec(),
1564 Exceptions.size(),
1565 Exceptions.data(),
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001566 Proto->getNoReturnAttr(),
1567 Proto->getCallConv()));
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001568 }
1569
Douglas Gregore53060f2009-06-25 22:08:12 +00001570 return false;
1571}
1572
Douglas Gregor5545e162009-03-24 00:38:23 +00001573/// \brief Initializes common fields of an instantiated method
1574/// declaration (New) from the corresponding fields of its template
1575/// (Tmpl).
1576///
1577/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001578bool
1579TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00001580 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001581 if (InitFunctionInstantiation(New, Tmpl))
1582 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001583
Douglas Gregor5545e162009-03-24 00:38:23 +00001584 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
1585 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00001586 if (Tmpl->isVirtualAsWritten())
1587 Record->setMethodAsVirtual(New);
Douglas Gregor5545e162009-03-24 00:38:23 +00001588
1589 // FIXME: attributes
1590 // FIXME: New needs a pointer to Tmpl
1591 return false;
1592}
Douglas Gregora58861f2009-05-13 20:28:22 +00001593
1594/// \brief Instantiate the definition of the given function from its
1595/// template.
1596///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001597/// \param PointOfInstantiation the point at which the instantiation was
1598/// required. Note that this is not precisely a "point of instantiation"
1599/// for the function, but it's close.
1600///
Douglas Gregora58861f2009-05-13 20:28:22 +00001601/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001602/// function template specialization or member function of a class template
1603/// specialization.
1604///
1605/// \param Recursive if true, recursively instantiates any functions that
1606/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001607///
1608/// \param DefinitionRequired if true, then we are performing an explicit
1609/// instantiation where the body of the function is required. Complain if
1610/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001611void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001612 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001613 bool Recursive,
1614 bool DefinitionRequired) {
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001615 if (Function->isInvalidDecl())
1616 return;
1617
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001618 assert(!Function->getBody() && "Already instantiated!");
Mike Stump1eb44332009-09-09 15:08:12 +00001619
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001620 // Never instantiate an explicit specialization.
1621 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1622 return;
1623
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001624 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00001625 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001626 Stmt *Pattern = 0;
1627 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001628 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001629
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001630 if (!Pattern) {
1631 if (DefinitionRequired) {
1632 if (Function->getPrimaryTemplate())
1633 Diag(PointOfInstantiation,
1634 diag::err_explicit_instantiation_undefined_func_template)
1635 << Function->getPrimaryTemplate();
1636 else
1637 Diag(PointOfInstantiation,
1638 diag::err_explicit_instantiation_undefined_member)
1639 << 1 << Function->getDeclName() << Function->getDeclContext();
1640
1641 if (PatternDecl)
1642 Diag(PatternDecl->getLocation(),
1643 diag::note_explicit_instantiation_here);
1644 }
1645
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001646 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001647 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001648
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001649 // C++0x [temp.explicit]p9:
1650 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00001651 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001652 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00001653 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001654 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001655 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001656 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001657
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001658 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
1659 if (Inst)
1660 return;
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001661
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001662 // If we're performing recursive template instantiation, create our own
1663 // queue of pending implicit instantiations that we will instantiate later,
1664 // while we're still within our own instantiation context.
1665 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1666 if (Recursive)
1667 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001668
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001669 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
1670
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001671 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00001672 // recorded, unless we're actually a member function within a local
1673 // class, in which case we need to merge our results with the parent
1674 // scope (of the enclosing function).
1675 bool MergeWithParentScope = false;
1676 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
1677 MergeWithParentScope = Rec->isLocalClass();
1678
1679 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001680
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001681 // Introduce the instantiated function parameters into the local
1682 // instantiation scope.
1683 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
1684 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
1685 Function->getParamDecl(I));
1686
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001687 // Enter the scope of this instantiation. We don't use
1688 // PushDeclContext because we don't have a scope.
1689 DeclContext *PreviousContext = CurContext;
1690 CurContext = Function;
1691
Mike Stump1eb44332009-09-09 15:08:12 +00001692 MultiLevelTemplateArgumentList TemplateArgs =
Anders Carlsson09025312009-08-29 05:16:22 +00001693 getTemplateInstantiationArgs(Function);
1694
1695 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001696 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00001697 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
1698 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
1699 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001700 }
1701
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001702 // Instantiate the function body.
Anders Carlsson09025312009-08-29 05:16:22 +00001703 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001704
Douglas Gregor52604ab2009-09-11 21:19:12 +00001705 if (Body.isInvalid())
1706 Function->setInvalidDecl();
1707
Mike Stump1eb44332009-09-09 15:08:12 +00001708 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001709 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001710
1711 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00001712
1713 DeclGroupRef DG(Function);
1714 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001715
Douglas Gregor60406be2010-01-16 22:29:39 +00001716 // This class may have local implicit instantiations that need to be
1717 // instantiation within this scope.
1718 PerformPendingImplicitInstantiations(/*LocalOnly=*/true);
1719 Scope.Exit();
1720
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001721 if (Recursive) {
1722 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001723 // instantiation of this template.
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001724 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001725
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001726 // Restore the set of pending implicit instantiations.
1727 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1728 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001729}
1730
1731/// \brief Instantiate the definition of the given variable from its
1732/// template.
1733///
Douglas Gregor7caa6822009-07-24 20:34:43 +00001734/// \param PointOfInstantiation the point at which the instantiation was
1735/// required. Note that this is not precisely a "point of instantiation"
1736/// for the function, but it's close.
1737///
1738/// \param Var the already-instantiated declaration of a static member
1739/// variable of a class template specialization.
1740///
1741/// \param Recursive if true, recursively instantiates any functions that
1742/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001743///
1744/// \param DefinitionRequired if true, then we are performing an explicit
1745/// instantiation where an out-of-line definition of the member variable
1746/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001747void Sema::InstantiateStaticDataMemberDefinition(
1748 SourceLocation PointOfInstantiation,
1749 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001750 bool Recursive,
1751 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001752 if (Var->isInvalidDecl())
1753 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001754
Douglas Gregor7caa6822009-07-24 20:34:43 +00001755 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001756 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00001757 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00001758 assert(Def->isStaticDataMember() && "Not a static data member?");
1759 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00001760
Douglas Gregor0d035142009-10-27 18:42:08 +00001761 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001762 // We did not find an out-of-line definition of this static data member,
1763 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00001764 // instantiate this definition (or provide a specialization for it) in
1765 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001766 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001767 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001768 Diag(PointOfInstantiation,
1769 diag::err_explicit_instantiation_undefined_member)
1770 << 2 << Var->getDeclName() << Var->getDeclContext();
1771 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
1772 }
1773
Douglas Gregor7caa6822009-07-24 20:34:43 +00001774 return;
1775 }
1776
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001777 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001778 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001779 return;
1780
1781 // C++0x [temp.explicit]p9:
1782 // Except for inline functions, other explicit instantiation declarations
1783 // have the effect of suppressing the implicit instantiation of the entity
1784 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001785 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001786 == TSK_ExplicitInstantiationDeclaration)
1787 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001788
Douglas Gregor7caa6822009-07-24 20:34:43 +00001789 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
1790 if (Inst)
1791 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001792
Douglas Gregor7caa6822009-07-24 20:34:43 +00001793 // If we're performing recursive template instantiation, create our own
1794 // queue of pending implicit instantiations that we will instantiate later,
1795 // while we're still within our own instantiation context.
1796 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1797 if (Recursive)
1798 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001799
Douglas Gregor7caa6822009-07-24 20:34:43 +00001800 // Enter the scope of this instantiation. We don't use
1801 // PushDeclContext because we don't have a scope.
1802 DeclContext *PreviousContext = CurContext;
1803 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001804
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001805 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00001806 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00001807 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00001808 CurContext = PreviousContext;
1809
1810 if (Var) {
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001811 Var->setPreviousDeclaration(OldVar);
Douglas Gregor583f33b2009-10-15 18:07:02 +00001812 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
1813 assert(MSInfo && "Missing member specialization information?");
1814 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
1815 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00001816 DeclGroupRef DG(Var);
1817 Consumer.HandleTopLevelDecl(DG);
1818 }
Mike Stump1eb44332009-09-09 15:08:12 +00001819
Douglas Gregor7caa6822009-07-24 20:34:43 +00001820 if (Recursive) {
1821 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001822 // instantiation of this template.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001823 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001824
Douglas Gregor7caa6822009-07-24 20:34:43 +00001825 // Restore the set of pending implicit instantiations.
1826 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001827 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001828}
Douglas Gregor815215d2009-05-27 05:35:12 +00001829
Anders Carlsson09025312009-08-29 05:16:22 +00001830void
1831Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
1832 const CXXConstructorDecl *Tmpl,
1833 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001834
Anders Carlsson09025312009-08-29 05:16:22 +00001835 llvm::SmallVector<MemInitTy*, 4> NewInits;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001836 bool AnyErrors = false;
1837
Anders Carlsson09025312009-08-29 05:16:22 +00001838 // Instantiate all the initializers.
1839 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00001840 InitsEnd = Tmpl->init_end();
1841 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00001842 CXXBaseOrMemberInitializer *Init = *Inits;
1843
1844 ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001845 llvm::SmallVector<SourceLocation, 4> CommaLocs;
Mike Stump1eb44332009-09-09 15:08:12 +00001846
Anders Carlsson09025312009-08-29 05:16:22 +00001847 // Instantiate all the arguments.
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001848 Expr *InitE = Init->getInit();
1849 if (!InitE) {
1850 // Nothing to instantiate;
1851 } else if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(InitE)) {
1852 if (InstantiateInitializationArguments(*this, ParenList->getExprs(),
1853 ParenList->getNumExprs(),
1854 TemplateArgs, CommaLocs,
1855 NewArgs)) {
1856 AnyErrors = true;
1857 continue;
1858 }
1859 } else {
1860 OwningExprResult InitArg = SubstExpr(InitE, TemplateArgs);
1861 if (InitArg.isInvalid()) {
1862 AnyErrors = true;
1863 continue;
1864 }
1865
1866 NewArgs.push_back(InitArg.release());
Anders Carlsson09025312009-08-29 05:16:22 +00001867 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001868
Anders Carlsson09025312009-08-29 05:16:22 +00001869 MemInitResult NewInit;
Anders Carlsson09025312009-08-29 05:16:22 +00001870 if (Init->isBaseInitializer()) {
John McCalla93c9342009-12-07 02:54:59 +00001871 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
Douglas Gregor802ab452009-12-02 22:36:29 +00001872 TemplateArgs,
1873 Init->getSourceLocation(),
1874 New->getDeclName());
John McCalla93c9342009-12-07 02:54:59 +00001875 if (!BaseTInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001876 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00001877 New->setInvalidDecl();
1878 continue;
1879 }
1880
John McCalla93c9342009-12-07 02:54:59 +00001881 NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001882 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001883 NewArgs.size(),
Douglas Gregor802ab452009-12-02 22:36:29 +00001884 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00001885 Init->getRParenLoc(),
1886 New->getParent());
1887 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001888 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00001889
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001890 // Is this an anonymous union?
1891 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregore95b4092009-09-16 18:34:49 +00001892 Member = cast<FieldDecl>(FindInstantiatedDecl(UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001893 else
Douglas Gregore95b4092009-09-16 18:34:49 +00001894 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMember(),
1895 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00001896
1897 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001898 NewArgs.size(),
1899 Init->getSourceLocation(),
Douglas Gregor802ab452009-12-02 22:36:29 +00001900 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00001901 Init->getRParenLoc());
1902 }
1903
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001904 if (NewInit.isInvalid()) {
1905 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00001906 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001907 } else {
Anders Carlsson09025312009-08-29 05:16:22 +00001908 // FIXME: It would be nice if ASTOwningVector had a release function.
1909 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001910
Anders Carlsson09025312009-08-29 05:16:22 +00001911 NewInits.push_back((MemInitTy *)NewInit.get());
1912 }
1913 }
Mike Stump1eb44332009-09-09 15:08:12 +00001914
Anders Carlsson09025312009-08-29 05:16:22 +00001915 // Assign all the initializers to the new constructor.
Mike Stump1eb44332009-09-09 15:08:12 +00001916 ActOnMemInitializers(DeclPtrTy::make(New),
Anders Carlsson09025312009-08-29 05:16:22 +00001917 /*FIXME: ColonLoc */
1918 SourceLocation(),
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001919 NewInits.data(), NewInits.size(),
1920 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00001921}
1922
John McCall52a575a2009-08-29 08:11:13 +00001923// TODO: this could be templated if the various decl types used the
1924// same method name.
1925static bool isInstantiationOf(ClassTemplateDecl *Pattern,
1926 ClassTemplateDecl *Instance) {
1927 Pattern = Pattern->getCanonicalDecl();
1928
1929 do {
1930 Instance = Instance->getCanonicalDecl();
1931 if (Pattern == Instance) return true;
1932 Instance = Instance->getInstantiatedFromMemberTemplate();
1933 } while (Instance);
1934
1935 return false;
1936}
1937
Douglas Gregor0d696532009-09-28 06:34:35 +00001938static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
1939 FunctionTemplateDecl *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 Gregored9c0f92009-10-29 00:04:11 +00001951static bool
1952isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
1953 ClassTemplatePartialSpecializationDecl *Instance) {
1954 Pattern
1955 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
1956 do {
1957 Instance = cast<ClassTemplatePartialSpecializationDecl>(
1958 Instance->getCanonicalDecl());
1959 if (Pattern == Instance)
1960 return true;
1961 Instance = Instance->getInstantiatedFromMember();
1962 } while (Instance);
1963
1964 return false;
1965}
1966
John McCall52a575a2009-08-29 08:11:13 +00001967static bool isInstantiationOf(CXXRecordDecl *Pattern,
1968 CXXRecordDecl *Instance) {
1969 Pattern = Pattern->getCanonicalDecl();
1970
1971 do {
1972 Instance = Instance->getCanonicalDecl();
1973 if (Pattern == Instance) return true;
1974 Instance = Instance->getInstantiatedFromMemberClass();
1975 } while (Instance);
1976
1977 return false;
1978}
1979
1980static bool isInstantiationOf(FunctionDecl *Pattern,
1981 FunctionDecl *Instance) {
1982 Pattern = Pattern->getCanonicalDecl();
1983
1984 do {
1985 Instance = Instance->getCanonicalDecl();
1986 if (Pattern == Instance) return true;
1987 Instance = Instance->getInstantiatedFromMemberFunction();
1988 } while (Instance);
1989
1990 return false;
1991}
1992
1993static bool isInstantiationOf(EnumDecl *Pattern,
1994 EnumDecl *Instance) {
1995 Pattern = Pattern->getCanonicalDecl();
1996
1997 do {
1998 Instance = Instance->getCanonicalDecl();
1999 if (Pattern == Instance) return true;
2000 Instance = Instance->getInstantiatedFromMemberEnum();
2001 } while (Instance);
2002
2003 return false;
2004}
2005
John McCalled976492009-12-04 22:46:56 +00002006static bool isInstantiationOf(UsingShadowDecl *Pattern,
2007 UsingShadowDecl *Instance,
2008 ASTContext &C) {
2009 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2010}
2011
2012static bool isInstantiationOf(UsingDecl *Pattern,
2013 UsingDecl *Instance,
2014 ASTContext &C) {
2015 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2016}
2017
John McCall7ba107a2009-11-18 02:36:19 +00002018static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
2019 UsingDecl *Instance,
2020 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002021 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00002022}
2023
2024static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00002025 UsingDecl *Instance,
2026 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002027 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00002028}
2029
John McCall52a575a2009-08-29 08:11:13 +00002030static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
2031 VarDecl *Instance) {
2032 assert(Instance->isStaticDataMember());
2033
2034 Pattern = Pattern->getCanonicalDecl();
2035
2036 do {
2037 Instance = Instance->getCanonicalDecl();
2038 if (Pattern == Instance) return true;
2039 Instance = Instance->getInstantiatedFromStaticDataMember();
2040 } while (Instance);
2041
2042 return false;
2043}
2044
John McCalled976492009-12-04 22:46:56 +00002045// Other is the prospective instantiation
2046// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00002047static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002048 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00002049 if (UnresolvedUsingTypenameDecl *UUD
2050 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
2051 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2052 return isInstantiationOf(UUD, UD, Ctx);
2053 }
2054 }
2055
2056 if (UnresolvedUsingValueDecl *UUD
2057 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002058 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2059 return isInstantiationOf(UUD, UD, Ctx);
2060 }
2061 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002062
Anders Carlsson0d8df782009-08-29 19:37:28 +00002063 return false;
2064 }
Mike Stump1eb44332009-09-09 15:08:12 +00002065
John McCall52a575a2009-08-29 08:11:13 +00002066 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
2067 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002068
John McCall52a575a2009-08-29 08:11:13 +00002069 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
2070 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00002071
John McCall52a575a2009-08-29 08:11:13 +00002072 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
2073 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00002074
Douglas Gregor7caa6822009-07-24 20:34:43 +00002075 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00002076 if (Var->isStaticDataMember())
2077 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
2078
2079 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
2080 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00002081
Douglas Gregor0d696532009-09-28 06:34:35 +00002082 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
2083 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
2084
Douglas Gregored9c0f92009-10-29 00:04:11 +00002085 if (ClassTemplatePartialSpecializationDecl *PartialSpec
2086 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2087 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2088 PartialSpec);
2089
Anders Carlssond8b285f2009-09-01 04:26:58 +00002090 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2091 if (!Field->getDeclName()) {
2092 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00002093 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00002094 cast<FieldDecl>(D);
2095 }
2096 }
Mike Stump1eb44332009-09-09 15:08:12 +00002097
John McCalled976492009-12-04 22:46:56 +00002098 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2099 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2100
2101 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2102 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2103
Douglas Gregor815215d2009-05-27 05:35:12 +00002104 return D->getDeclName() && isa<NamedDecl>(Other) &&
2105 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2106}
2107
2108template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00002109static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00002110 NamedDecl *D,
2111 ForwardIterator first,
2112 ForwardIterator last) {
2113 for (; first != last; ++first)
2114 if (isInstantiationOf(Ctx, D, *first))
2115 return cast<NamedDecl>(*first);
2116
2117 return 0;
2118}
2119
John McCall02cace72009-08-28 07:59:38 +00002120/// \brief Finds the instantiation of the given declaration context
2121/// within the current instantiation.
2122///
2123/// \returns NULL if there was an error
Douglas Gregore95b4092009-09-16 18:34:49 +00002124DeclContext *Sema::FindInstantiatedContext(DeclContext* DC,
2125 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00002126 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00002127 Decl* ID = FindInstantiatedDecl(D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00002128 return cast_or_null<DeclContext>(ID);
2129 } else return DC;
2130}
2131
Douglas Gregored961e72009-05-27 17:54:46 +00002132/// \brief Find the instantiation of the given declaration within the
2133/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00002134///
2135/// This routine is intended to be used when \p D is a declaration
2136/// referenced from within a template, that needs to mapped into the
2137/// corresponding declaration within an instantiation. For example,
2138/// given:
2139///
2140/// \code
2141/// template<typename T>
2142/// struct X {
2143/// enum Kind {
2144/// KnownValue = sizeof(T)
2145/// };
2146///
2147/// bool getKind() const { return KnownValue; }
2148/// };
2149///
2150/// template struct X<int>;
2151/// \endcode
2152///
2153/// In the instantiation of X<int>::getKind(), we need to map the
2154/// EnumConstantDecl for KnownValue (which refers to
2155/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00002156/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2157/// this mapping from within the instantiation of X<int>.
Douglas Gregore95b4092009-09-16 18:34:49 +00002158NamedDecl *Sema::FindInstantiatedDecl(NamedDecl *D,
2159 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00002160 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00002161 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00002162 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
Douglas Gregor550d9b22009-10-31 17:21:17 +00002163 ParentDC->isFunctionOrMethod()) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002164 // D is a local of some kind. Look into the map of local
2165 // declarations to their instantiations.
2166 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
2167 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002168
Douglas Gregore95b4092009-09-16 18:34:49 +00002169 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2170 if (!Record->isDependentContext())
2171 return D;
2172
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002173 // If the RecordDecl is actually the injected-class-name or a
2174 // "templated" declaration for a class template, class template
2175 // partial specialization, or a member class of a class template,
2176 // substitute into the injected-class-name of the class template
2177 // or partial specialization to find the new DeclContext.
Douglas Gregore95b4092009-09-16 18:34:49 +00002178 QualType T;
2179 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2180
2181 if (ClassTemplate) {
2182 T = ClassTemplate->getInjectedClassNameType(Context);
2183 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2184 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
2185 T = Context.getTypeDeclType(Record);
2186 ClassTemplate = PartialSpec->getSpecializedTemplate();
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002187 }
Douglas Gregore95b4092009-09-16 18:34:49 +00002188
2189 if (!T.isNull()) {
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002190 // Substitute into the injected-class-name to get the type
2191 // corresponding to the instantiation we want, which may also be
2192 // the current instantiation (if we're in a template
2193 // definition). This substitution should never fail, since we
2194 // know we can instantiate the injected-class-name or we
2195 // wouldn't have gotten to the injected-class-name!
2196
2197 // FIXME: Can we use the CurrentInstantiationScope to avoid this
2198 // extra instantiation in the common case?
Douglas Gregore95b4092009-09-16 18:34:49 +00002199 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2200 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2201
2202 if (!T->isDependentType()) {
2203 assert(T->isRecordType() && "Instantiation must produce a record type");
2204 return T->getAs<RecordType>()->getDecl();
2205 }
2206
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002207 // We are performing "partial" template instantiation to create
2208 // the member declarations for the members of a class template
2209 // specialization. Therefore, D is actually referring to something
2210 // in the current instantiation. Look through the current
2211 // context, which contains actual instantiations, to find the
2212 // instantiation of the "current instantiation" that D refers
2213 // to.
2214 bool SawNonDependentContext = false;
Mike Stump1eb44332009-09-09 15:08:12 +00002215 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00002216 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002217 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002218 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00002219 if (isInstantiationOf(ClassTemplate,
2220 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00002221 return Spec;
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002222
2223 if (!DC->isDependentContext())
2224 SawNonDependentContext = true;
John McCall52a575a2009-08-29 08:11:13 +00002225 }
2226
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002227 // We're performing "instantiation" of a member of the current
2228 // instantiation while we are type-checking the
2229 // definition. Compute the declaration context and return that.
2230 assert(!SawNonDependentContext &&
2231 "No dependent context while instantiating record");
2232 DeclContext *DC = computeDeclContext(T);
2233 assert(DC &&
John McCall52a575a2009-08-29 08:11:13 +00002234 "Unable to find declaration for the current instantiation");
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002235 return cast<CXXRecordDecl>(DC);
John McCall52a575a2009-08-29 08:11:13 +00002236 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002237
Douglas Gregore95b4092009-09-16 18:34:49 +00002238 // Fall through to deal with other dependent record types (e.g.,
2239 // anonymous unions in class templates).
2240 }
John McCall52a575a2009-08-29 08:11:13 +00002241
Douglas Gregore95b4092009-09-16 18:34:49 +00002242 if (!ParentDC->isDependentContext())
2243 return D;
2244
2245 ParentDC = FindInstantiatedContext(ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002246 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00002247 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002248
Douglas Gregor815215d2009-05-27 05:35:12 +00002249 if (ParentDC != D->getDeclContext()) {
2250 // We performed some kind of instantiation in the parent context,
2251 // so now we need to look into the instantiated parent context to
2252 // find the instantiation of the declaration D.
2253 NamedDecl *Result = 0;
2254 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002255 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00002256 Result = findInstantiationOf(Context, D, Found.first, Found.second);
2257 } else {
2258 // Since we don't have a name for the entity we're looking for,
2259 // our only option is to walk through all of the declarations to
2260 // find that name. This will occur in a few cases:
2261 //
2262 // - anonymous struct/union within a template
2263 // - unnamed class/struct/union/enum within a template
2264 //
2265 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00002266 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002267 ParentDC->decls_begin(),
2268 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00002269 }
Mike Stump1eb44332009-09-09 15:08:12 +00002270
John McCall9f54ad42009-12-10 09:41:52 +00002271 // UsingShadowDecls can instantiate to nothing because of using hiding.
2272 assert((Result || isa<UsingShadowDecl>(D))
2273 && "Unable to find instantiation of declaration!");
2274
Douglas Gregor815215d2009-05-27 05:35:12 +00002275 D = Result;
2276 }
2277
Douglas Gregor815215d2009-05-27 05:35:12 +00002278 return D;
2279}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002280
Mike Stump1eb44332009-09-09 15:08:12 +00002281/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002282/// instantiations we have seen until this point.
Douglas Gregor60406be2010-01-16 22:29:39 +00002283void Sema::PerformPendingImplicitInstantiations(bool LocalOnly) {
2284 while (!PendingLocalImplicitInstantiations.empty() ||
2285 (!LocalOnly && !PendingImplicitInstantiations.empty())) {
2286 PendingImplicitInstantiation Inst;
2287
2288 if (PendingLocalImplicitInstantiations.empty()) {
2289 Inst = PendingImplicitInstantiations.front();
2290 PendingImplicitInstantiations.pop_front();
2291 } else {
2292 Inst = PendingLocalImplicitInstantiations.front();
2293 PendingLocalImplicitInstantiations.pop_front();
2294 }
Mike Stump1eb44332009-09-09 15:08:12 +00002295
Douglas Gregor7caa6822009-07-24 20:34:43 +00002296 // Instantiate function definitions
2297 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002298 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002299 Function->getLocation(), *this,
2300 Context.getSourceManager(),
2301 "instantiating function definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002302
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00002303 if (!Function->getBody())
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002304 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00002305 continue;
2306 }
Mike Stump1eb44332009-09-09 15:08:12 +00002307
Douglas Gregor7caa6822009-07-24 20:34:43 +00002308 // Instantiate static data member definitions.
2309 VarDecl *Var = cast<VarDecl>(Inst.first);
2310 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002311
Mike Stump1eb44332009-09-09 15:08:12 +00002312 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002313 Var->getLocation(), *this,
2314 Context.getSourceManager(),
2315 "instantiating static data member "
2316 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002317
Douglas Gregor7caa6822009-07-24 20:34:43 +00002318 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002319 }
2320}