blob: 08cb681960054b9ec63c806e7b160d2d67560e4b [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);
Douglas Gregor60c93c92010-02-09 07:26:29 +0000220 if (D->isStaticDataMember())
221 SemaRef.LookupQualifiedName(Previous, Owner, false);
John McCall68263142009-11-18 22:49:29 +0000222 SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000223
Douglas Gregor7caa6822009-07-24 20:34:43 +0000224 if (D->isOutOfLine()) {
225 D->getLexicalDeclContext()->addDecl(Var);
226 Owner->makeDeclVisibleInContext(Var);
227 } else {
228 Owner->addDecl(Var);
229 }
Mike Stump1eb44332009-09-09 15:08:12 +0000230
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000231 // Link instantiations of static data members back to the template from
232 // which they were instantiated.
233 if (Var->isStaticDataMember())
234 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000235 TSK_ImplicitInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000236
Douglas Gregor60c93c92010-02-09 07:26:29 +0000237 if (Var->getAnyInitializer()) {
238 // We already have an initializer in the class.
239 } else if (D->getInit()) {
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000240 if (Var->isStaticDataMember() && !D->isOutOfLine())
241 SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
242 else
243 SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
244
Douglas Gregor6eef5192009-12-14 19:27:10 +0000245 // Extract the initializer, skipping through any temporary-binding
246 // expressions and look at the subexpression as it was written.
247 Expr *DInit = D->getInit();
Douglas Gregora89ebea2010-02-03 09:01:59 +0000248 if (CXXExprWithTemporaries *ExprTemp
249 = dyn_cast<CXXExprWithTemporaries>(DInit))
250 DInit = ExprTemp->getSubExpr();
Douglas Gregor6eef5192009-12-14 19:27:10 +0000251 while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(DInit))
252 DInit = Binder->getSubExpr();
253 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(DInit))
254 DInit = ICE->getSubExprAsWritten();
255
256 if (ParenListExpr *PLE = dyn_cast<ParenListExpr>(DInit)) {
257 // The initializer is a parenthesized list of expressions that is
258 // type-dependent. Instantiate each of the expressions; we'll be
259 // performing direct initialization with them.
260 llvm::SmallVector<SourceLocation, 4> CommaLocs;
261 ASTOwningVector<&ActionBase::DeleteExpr> InitArgs(SemaRef);
262 if (!InstantiateInitializationArguments(SemaRef,
263 PLE->getExprs(),
264 PLE->getNumExprs(),
265 TemplateArgs,
266 CommaLocs, InitArgs)) {
267 // Add the direct initializer to the declaration.
Douglas Gregora88cfbf2009-12-12 18:16:41 +0000268 SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
Douglas Gregor6eef5192009-12-14 19:27:10 +0000269 PLE->getLParenLoc(),
270 move_arg(InitArgs),
271 CommaLocs.data(),
272 PLE->getRParenLoc());
273 }
274 } else if (CXXConstructExpr *Construct =dyn_cast<CXXConstructExpr>(DInit)) {
275 // The initializer resolved to a constructor. Instantiate the constructor
276 // arguments.
277 llvm::SmallVector<SourceLocation, 4> CommaLocs;
278 ASTOwningVector<&ActionBase::DeleteExpr> InitArgs(SemaRef);
279
280 if (!InstantiateInitializationArguments(SemaRef,
281 Construct->getArgs(),
282 Construct->getNumArgs(),
283 TemplateArgs,
284 CommaLocs, InitArgs)) {
285 if (D->hasCXXDirectInitializer()) {
286 SourceLocation FakeLParenLoc =
287 SemaRef.PP.getLocForEndOfToken(D->getLocation());
288 SourceLocation FakeRParenLoc = CommaLocs.empty()? FakeLParenLoc
289 : CommaLocs.back();
290 SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
291 FakeLParenLoc,
292 move_arg(InitArgs),
293 CommaLocs.data(),
294 FakeRParenLoc);
295 } else if (InitArgs.size() == 1) {
296 Expr *Init = (Expr*)(InitArgs.take()[0]);
297 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var),
298 SemaRef.Owned(Init),
299 false);
300 } else {
301 assert(InitArgs.size() == 0);
302 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
303 }
304 }
305 } else {
306 OwningExprResult Init
307 = SemaRef.SubstExpr(D->getInit(), TemplateArgs);
308
309 // FIXME: Not happy about invalidating decls just because of a bad
310 // initializer, unless it affects the type.
311 if (Init.isInvalid())
312 Var->setInvalidDecl();
313 else
314 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init),
315 D->hasCXXDirectInitializer());
316 }
317
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000318 SemaRef.PopExpressionEvaluationContext();
Douglas Gregor65b90052009-07-27 17:43:39 +0000319 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
320 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000321
322 return Var;
323}
324
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000325Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
326 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000327 TypeSourceInfo *DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000328 if (DI->getType()->isDependentType()) {
329 DI = SemaRef.SubstType(DI, TemplateArgs,
330 D->getLocation(), D->getDeclName());
331 if (!DI) {
John McCalla93c9342009-12-07 02:54:59 +0000332 DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000333 Invalid = true;
334 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000335 // C++ [temp.arg.type]p3:
336 // If a declaration acquires a function type through a type
337 // dependent on a template-parameter and this causes a
338 // declaration that does not use the syntactic form of a
339 // function declarator to have function type, the program is
340 // ill-formed.
341 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000342 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000343 Invalid = true;
344 }
345 }
346
347 Expr *BitWidth = D->getBitWidth();
348 if (Invalid)
349 BitWidth = 0;
350 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000351 // The bit-width expression is not potentially evaluated.
352 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000354 OwningExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000355 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000356 if (InstantiatedBitWidth.isInvalid()) {
357 Invalid = true;
358 BitWidth = 0;
359 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000360 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000361 }
362
John McCall07fb6be2009-10-22 23:33:21 +0000363 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
364 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000365 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000366 D->getLocation(),
367 D->isMutable(),
368 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000369 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000370 D->getAccess(),
371 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000372 if (!Field) {
373 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000374 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000375 }
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000377 InstantiateAttrs(D, Field);
378
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000379 if (Invalid)
380 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000382 if (!Field->getDeclName()) {
383 // Keep track of where this decl came from.
384 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000385 }
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000387 Field->setImplicit(D->isImplicit());
John McCall46460a62010-01-20 21:53:11 +0000388 Field->setAccess(D->getAccess());
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000389 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000390
391 return Field;
392}
393
John McCall02cace72009-08-28 07:59:38 +0000394Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
395 FriendDecl::FriendUnion FU;
396
397 // Handle friend type expressions by simply substituting template
398 // parameters into the pattern type.
399 if (Type *Ty = D->getFriendType()) {
400 QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs,
401 D->getLocation(), DeclarationName());
402 if (T.isNull()) return 0;
403
404 assert(getLangOptions().CPlusPlus0x || T->isRecordType());
405 FU = T.getTypePtr();
406
407 // Handle everything else by appropriate substitution.
408 } else {
409 NamedDecl *ND = D->getFriendDecl();
410 assert(ND && "friend decl must be a decl or a type!");
411
Douglas Gregora735b202009-10-13 14:39:41 +0000412 // FIXME: We have a problem here, because the nested call to Visit(ND)
413 // will inject the thing that the friend references into the current
414 // owner, which is wrong.
John McCalle129d442009-12-17 23:21:11 +0000415 Decl *NewND;
416
417 // Hack to make this work almost well pending a rewrite.
Douglas Gregor63644fa2010-02-07 10:31:35 +0000418 if (ND->getDeclContext()->isRecord()) {
419 if (!ND->getDeclContext()->isDependentContext()) {
420 NewND = SemaRef.FindInstantiatedDecl(ND, TemplateArgs);
421 } else {
422 // FIXME: Hack to avoid crashing when incorrectly trying to instantiate
423 // templated friend declarations. This doesn't produce a correct AST;
424 // however this is sufficient for some AST analysis. The real solution
425 // must be put in place during the pending rewrite. See PR5848.
426 return 0;
427 }
428 } else if (D->wasSpecialization()) {
Douglas Gregor7557a132009-12-24 20:56:24 +0000429 // Totally egregious hack to work around PR5866
430 return 0;
431 } else
John McCalle129d442009-12-17 23:21:11 +0000432 NewND = Visit(ND);
John McCall02cace72009-08-28 07:59:38 +0000433 if (!NewND) return 0;
434
435 FU = cast<NamedDecl>(NewND);
John McCallfd810b12009-08-14 02:03:10 +0000436 }
Mike Stump1eb44332009-09-09 15:08:12 +0000437
John McCall02cace72009-08-28 07:59:38 +0000438 FriendDecl *FD =
439 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU,
440 D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000441 FD->setAccess(AS_public);
John McCall02cace72009-08-28 07:59:38 +0000442 Owner->addDecl(FD);
443 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000444}
445
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000446Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
447 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000448
Douglas Gregorac7610d2009-06-22 20:57:11 +0000449 // The expression in a static assertion is not potentially evaluated.
450 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000452 OwningExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000453 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000454 if (InstantiatedAssertExpr.isInvalid())
455 return 0;
456
Douglas Gregor43d9d922009-08-08 01:41:12 +0000457 OwningExprResult Message(SemaRef, D->getMessage());
458 D->getMessage()->Retain();
Mike Stump1eb44332009-09-09 15:08:12 +0000459 Decl *StaticAssert
460 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
Chris Lattnerb28317a2009-03-28 19:18:32 +0000461 move(InstantiatedAssertExpr),
462 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000463 return StaticAssert;
464}
465
466Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000467 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000468 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000469 D->getTagKeywordLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000470 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000471 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000472 Enum->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000473 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000474 Enum->startDefinition();
475
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000476 llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000477
478 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000479 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
480 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000481 EC != ECEnd; ++EC) {
482 // The specified value for the enumerator.
483 OwningExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000484 if (Expr *UninstValue = EC->getInitExpr()) {
485 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000486 EnterExpressionEvaluationContext Unevaluated(SemaRef,
Douglas Gregorac7610d2009-06-22 20:57:11 +0000487 Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000488
John McCallce3ff2b2009-08-25 22:02:44 +0000489 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000490 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000491
492 // Drop the initial value and continue.
493 bool isInvalid = false;
494 if (Value.isInvalid()) {
495 Value = SemaRef.Owned((Expr *)0);
496 isInvalid = true;
497 }
498
Mike Stump1eb44332009-09-09 15:08:12 +0000499 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000500 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
501 EC->getLocation(), EC->getIdentifier(),
502 move(Value));
503
504 if (isInvalid) {
505 if (EnumConst)
506 EnumConst->setInvalidDecl();
507 Enum->setInvalidDecl();
508 }
509
510 if (EnumConst) {
John McCall3b85ecf2010-01-23 22:37:59 +0000511 EnumConst->setAccess(Enum->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000512 Enum->addDecl(EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000513 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000514 LastEnumConst = EnumConst;
515 }
516 }
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000518 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000519 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000520 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
521 Sema::DeclPtrTy::make(Enum),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000522 &Enumerators[0], Enumerators.size(),
523 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000524
525 return Enum;
526}
527
Douglas Gregor6477b692009-03-25 15:04:13 +0000528Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
529 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
530 return 0;
531}
532
Douglas Gregored9c0f92009-10-29 00:04:11 +0000533namespace {
534 class SortDeclByLocation {
535 SourceManager &SourceMgr;
536
537 public:
538 explicit SortDeclByLocation(SourceManager &SourceMgr)
539 : SourceMgr(SourceMgr) { }
540
541 bool operator()(const Decl *X, const Decl *Y) const {
542 return SourceMgr.isBeforeInTranslationUnit(X->getLocation(),
543 Y->getLocation());
544 }
545 };
546}
547
John McCalle29ba202009-08-20 01:44:21 +0000548Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000549 // Create a local instantiation scope for this class template, which
550 // will contain the instantiations of the template parameters.
551 Sema::LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000552 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000553 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000554 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000555 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000556
557 CXXRecordDecl *Pattern = D->getTemplatedDecl();
558 CXXRecordDecl *RecordInst
559 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner,
560 Pattern->getLocation(), Pattern->getIdentifier(),
Douglas Gregorf0510d42009-10-12 23:11:44 +0000561 Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL,
562 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000563
564 ClassTemplateDecl *Inst
565 = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
566 D->getIdentifier(), InstParams, RecordInst, 0);
567 RecordInst->setDescribedClassTemplate(Inst);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000568 if (D->getFriendObjectKind())
569 Inst->setObjectOfFriendDecl(true);
570 else
571 Inst->setAccess(D->getAccess());
John McCalle29ba202009-08-20 01:44:21 +0000572 Inst->setInstantiatedFromMemberTemplate(D);
Douglas Gregorf0510d42009-10-12 23:11:44 +0000573
574 // Trigger creation of the type for the instantiation.
575 SemaRef.Context.getTypeDeclType(RecordInst);
576
Douglas Gregor259571e2009-10-30 22:42:42 +0000577 // Finish handling of friends.
578 if (Inst->getFriendObjectKind()) {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000579 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000580 }
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000581
John McCall46460a62010-01-20 21:53:11 +0000582 Inst->setAccess(D->getAccess());
John McCalle29ba202009-08-20 01:44:21 +0000583 Owner->addDecl(Inst);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000584
585 // First, we sort the partial specializations by location, so
586 // that we instantiate them in the order they were declared.
587 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
588 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
589 P = D->getPartialSpecializations().begin(),
590 PEnd = D->getPartialSpecializations().end();
591 P != PEnd; ++P)
592 PartialSpecs.push_back(&*P);
593 std::sort(PartialSpecs.begin(), PartialSpecs.end(),
594 SortDeclByLocation(SemaRef.SourceMgr));
595
596 // Instantiate all of the partial specializations of this member class
597 // template.
598 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
599 InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
600
John McCalle29ba202009-08-20 01:44:21 +0000601 return Inst;
602}
603
Douglas Gregord60e1052009-08-27 16:57:43 +0000604Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000605TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
606 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000607 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
608
609 // Lookup the already-instantiated declaration in the instantiation
610 // of the class template and return that.
611 DeclContext::lookup_result Found
612 = Owner->lookup(ClassTemplate->getDeclName());
613 if (Found.first == Found.second)
614 return 0;
615
616 ClassTemplateDecl *InstClassTemplate
617 = dyn_cast<ClassTemplateDecl>(*Found.first);
618 if (!InstClassTemplate)
619 return 0;
620
621 Decl *DCanon = D->getCanonicalDecl();
622 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
623 P = InstClassTemplate->getPartialSpecializations().begin(),
624 PEnd = InstClassTemplate->getPartialSpecializations().end();
625 P != PEnd; ++P) {
626 if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
627 return &*P;
628 }
629
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000630 return 0;
631}
632
633Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000634TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000635 // Create a local instantiation scope for this function template, which
636 // will contain the instantiations of the template parameters and then get
637 // merged with the local instantiation scope for the function template
638 // itself.
639 Sema::LocalInstantiationScope Scope(SemaRef);
640
Douglas Gregord60e1052009-08-27 16:57:43 +0000641 TemplateParameterList *TempParams = D->getTemplateParameters();
642 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000643 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000644 return NULL;
Douglas Gregored9c0f92009-10-29 00:04:11 +0000645
Douglas Gregora735b202009-10-13 14:39:41 +0000646 FunctionDecl *Instantiated = 0;
647 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
648 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
649 InstParams));
650 else
651 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
652 D->getTemplatedDecl(),
653 InstParams));
654
655 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000656 return 0;
657
John McCall46460a62010-01-20 21:53:11 +0000658 Instantiated->setAccess(D->getAccess());
659
Mike Stump1eb44332009-09-09 15:08:12 +0000660 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000661 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000662 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000663 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000664 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000665 assert(InstTemplate &&
666 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
John McCalle976ffe2009-12-14 23:19:40 +0000667
668 // Link the instantiation back to the pattern *unless* this is a
669 // non-definition friend declaration.
670 if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
671 !(InstTemplate->getFriendObjectKind() &&
672 !D->getTemplatedDecl()->isThisDeclarationADefinition()))
Douglas Gregora735b202009-10-13 14:39:41 +0000673 InstTemplate->setInstantiatedFromMemberTemplate(D);
674
675 // Add non-friends into the owner.
676 if (!InstTemplate->getFriendObjectKind())
677 Owner->addDecl(InstTemplate);
Douglas Gregord60e1052009-08-27 16:57:43 +0000678 return InstTemplate;
679}
680
Douglas Gregord475b8d2009-03-25 21:17:03 +0000681Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
682 CXXRecordDecl *PrevDecl = 0;
683 if (D->isInjectedClassName())
684 PrevDecl = cast<CXXRecordDecl>(Owner);
John McCall6c1c1b82009-12-15 22:29:06 +0000685 else if (D->getPreviousDeclaration()) {
686 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getPreviousDeclaration(),
687 TemplateArgs);
688 if (!Prev) return 0;
689 PrevDecl = cast<CXXRecordDecl>(Prev);
690 }
Douglas Gregord475b8d2009-03-25 21:17:03 +0000691
692 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000693 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000694 D->getLocation(), D->getIdentifier(),
695 D->getTagKeywordLoc(), PrevDecl);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000696 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000697 // FIXME: Check against AS_none is an ugly hack to work around the issue that
698 // the tag decls introduced by friend class declarations don't have an access
699 // specifier. Remove once this area of the code gets sorted out.
700 if (D->getAccess() != AS_none)
701 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000702 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000703 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000704
John McCall02cace72009-08-28 07:59:38 +0000705 // If the original function was part of a friend declaration,
706 // inherit its namespace state.
707 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
708 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
709
Anders Carlssond8b285f2009-09-01 04:26:58 +0000710 Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
711
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000712 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000713 return Record;
714}
715
John McCall02cace72009-08-28 07:59:38 +0000716/// Normal class members are of more specific types and therefore
717/// don't make it here. This function serves two purposes:
718/// 1) instantiating function templates
719/// 2) substituting friend declarations
720/// FIXME: preserve function definitions in case #2
Douglas Gregor7557a132009-12-24 20:56:24 +0000721Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
Douglas Gregora735b202009-10-13 14:39:41 +0000722 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000723 // Check whether there is already a function template specialization for
724 // this declaration.
725 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
726 void *InsertPos = 0;
Douglas Gregora735b202009-10-13 14:39:41 +0000727 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000728 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000729 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000730 TemplateArgs.getInnermost().getFlatArgumentList(),
731 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor828e2262009-07-29 16:09:57 +0000732 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000733
734 FunctionTemplateSpecializationInfo *Info
735 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor127102b2009-06-29 20:59:39 +0000736 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000737
Douglas Gregor127102b2009-06-29 20:59:39 +0000738 // If we already have a function template specialization, return it.
739 if (Info)
740 return Info->Function;
741 }
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Douglas Gregor79c22782010-01-16 20:21:20 +0000743 bool MergeWithParentScope = (TemplateParams != 0) ||
744 !(isa<Decl>(Owner) &&
745 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
746 Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000747
Douglas Gregore53060f2009-06-25 22:08:12 +0000748 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000749 QualType T = SubstFunctionType(D, Params);
Douglas Gregore53060f2009-06-25 22:08:12 +0000750 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000751 return 0;
John McCallfd810b12009-08-14 02:03:10 +0000752
John McCall68b6b872010-02-06 01:50:47 +0000753 // If we're instantiating a local function declaration, put the result
754 // in the owner; otherwise we need to find the instantiated context.
755 DeclContext *DC;
756 if (D->getDeclContext()->isFunctionOrMethod())
757 DC = Owner;
758 else
759 DC = SemaRef.FindInstantiatedContext(D->getDeclContext(), TemplateArgs);
760
John McCall02cace72009-08-28 07:59:38 +0000761 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +0000762 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
John McCalla93c9342009-12-07 02:54:59 +0000763 D->getDeclName(), T, D->getTypeSourceInfo(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000764 D->getStorageClass(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000765 D->isInlineSpecified(), D->hasWrittenPrototype());
John McCall02cace72009-08-28 07:59:38 +0000766 Function->setLexicalDeclContext(Owner);
Mike Stump1eb44332009-09-09 15:08:12 +0000767
Douglas Gregore53060f2009-06-25 22:08:12 +0000768 // Attach the parameters
769 for (unsigned P = 0; P < Params.size(); ++P)
770 Params[P]->setOwningFunction(Function);
771 Function->setParams(SemaRef.Context, Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +0000772
Douglas Gregora735b202009-10-13 14:39:41 +0000773 if (TemplateParams) {
774 // Our resulting instantiation is actually a function template, since we
775 // are substituting only the outer template parameters. For example, given
776 //
777 // template<typename T>
778 // struct X {
779 // template<typename U> friend void f(T, U);
780 // };
781 //
782 // X<int> x;
783 //
784 // We are instantiating the friend function template "f" within X<int>,
785 // which means substituting int for T, but leaving "f" as a friend function
786 // template.
787 // Build the function template itself.
788 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Owner,
789 Function->getLocation(),
790 Function->getDeclName(),
791 TemplateParams, Function);
792 Function->setDescribedFunctionTemplate(FunctionTemplate);
793 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregor66724ea2009-11-14 01:20:54 +0000794 } else if (FunctionTemplate) {
795 // Record this function template specialization.
796 Function->setFunctionTemplateSpecialization(SemaRef.Context,
797 FunctionTemplate,
798 &TemplateArgs.getInnermost(),
799 InsertPos);
John McCall02cace72009-08-28 07:59:38 +0000800 }
Douglas Gregora735b202009-10-13 14:39:41 +0000801
Douglas Gregore53060f2009-06-25 22:08:12 +0000802 if (InitFunctionInstantiation(Function, D))
803 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000804
Douglas Gregore53060f2009-06-25 22:08:12 +0000805 bool Redeclaration = false;
806 bool OverloadableAttrRequired = false;
Douglas Gregora735b202009-10-13 14:39:41 +0000807
John McCall68263142009-11-18 22:49:29 +0000808 LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
809 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
810
Douglas Gregora735b202009-10-13 14:39:41 +0000811 if (TemplateParams || !FunctionTemplate) {
812 // Look only into the namespace where the friend would be declared to
813 // find a previous declaration. This is the innermost enclosing namespace,
814 // as described in ActOnFriendFunctionDecl.
John McCall68263142009-11-18 22:49:29 +0000815 SemaRef.LookupQualifiedName(Previous, DC);
Douglas Gregora735b202009-10-13 14:39:41 +0000816
Douglas Gregora735b202009-10-13 14:39:41 +0000817 // In C++, the previous declaration we find might be a tag type
818 // (class or enum). In this case, the new declaration will hide the
819 // tag type. Note that this does does not apply if we're declaring a
820 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +0000821 if (Previous.isSingleTagDecl())
822 Previous.clear();
Douglas Gregora735b202009-10-13 14:39:41 +0000823 }
824
John McCall9f54ad42009-12-10 09:41:52 +0000825 SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
826 false, Redeclaration,
Douglas Gregore53060f2009-06-25 22:08:12 +0000827 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000828
Douglas Gregora735b202009-10-13 14:39:41 +0000829 // If the original function was part of a friend declaration,
830 // inherit its namespace state and add it to the owner.
831 NamedDecl *FromFriendD
832 = TemplateParams? cast<NamedDecl>(D->getDescribedFunctionTemplate()) : D;
833 if (FromFriendD->getFriendObjectKind()) {
834 NamedDecl *ToFriendD = 0;
John McCall68263142009-11-18 22:49:29 +0000835 NamedDecl *PrevDecl;
Douglas Gregora735b202009-10-13 14:39:41 +0000836 if (TemplateParams) {
837 ToFriendD = cast<NamedDecl>(FunctionTemplate);
838 PrevDecl = FunctionTemplate->getPreviousDeclaration();
839 } else {
840 ToFriendD = Function;
841 PrevDecl = Function->getPreviousDeclaration();
842 }
843 ToFriendD->setObjectOfFriendDecl(PrevDecl != NULL);
844 if (!Owner->isDependentContext() && !PrevDecl)
845 DC->makeDeclVisibleInContext(ToFriendD, /* Recoverable = */ false);
846
847 if (!TemplateParams)
848 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
849 }
850
Douglas Gregore53060f2009-06-25 22:08:12 +0000851 return Function;
852}
853
Douglas Gregord60e1052009-08-27 16:57:43 +0000854Decl *
855TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
856 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +0000857 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
858 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +0000859 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +0000860 // We are creating a function template specialization from a function
861 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +0000862 // specialization for this particular set of template arguments.
Douglas Gregor6b906862009-08-21 00:16:32 +0000863 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000864 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000865 TemplateArgs.getInnermost().getFlatArgumentList(),
866 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000867 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000868
869 FunctionTemplateSpecializationInfo *Info
870 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor6b906862009-08-21 00:16:32 +0000871 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000872
Douglas Gregor6b906862009-08-21 00:16:32 +0000873 // If we already have a function template specialization, return it.
874 if (Info)
875 return Info->Function;
876 }
877
Douglas Gregor79c22782010-01-16 20:21:20 +0000878 bool MergeWithParentScope = (TemplateParams != 0) ||
879 !(isa<Decl>(Owner) &&
880 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
881 Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000882
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000883 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000884 QualType T = SubstFunctionType(D, Params);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000885 if (T.isNull())
886 return 0;
887
888 // Build the instantiated method declaration.
889 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
Douglas Gregordec06662009-08-21 18:42:58 +0000890 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000891
Douglas Gregordec06662009-08-21 18:42:58 +0000892 DeclarationName Name = D->getDeclName();
Douglas Gregor17e32f32009-08-21 22:43:28 +0000893 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregordec06662009-08-21 18:42:58 +0000894 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
895 Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
896 SemaRef.Context.getCanonicalType(ClassTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000897 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
898 Constructor->getLocation(),
899 Name, T,
John McCalla93c9342009-12-07 02:54:59 +0000900 Constructor->getTypeSourceInfo(),
Mike Stump1eb44332009-09-09 15:08:12 +0000901 Constructor->isExplicit(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000902 Constructor->isInlineSpecified(), false);
Douglas Gregor17e32f32009-08-21 22:43:28 +0000903 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
904 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
905 Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
906 SemaRef.Context.getCanonicalType(ClassTy));
907 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
908 Destructor->getLocation(), Name,
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000909 T, Destructor->isInlineSpecified(), false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000910 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000911 CanQualType ConvTy
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000912 = SemaRef.Context.getCanonicalType(
John McCall183700f2009-09-21 23:43:11 +0000913 T->getAs<FunctionType>()->getResultType());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000914 Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
915 ConvTy);
916 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
917 Conversion->getLocation(), Name,
John McCalla93c9342009-12-07 02:54:59 +0000918 T, Conversion->getTypeSourceInfo(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000919 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000920 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +0000921 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000922 Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
John McCalla93c9342009-12-07 02:54:59 +0000923 D->getDeclName(), T, D->getTypeSourceInfo(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000924 D->isStatic(), D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +0000925 }
Douglas Gregor6b906862009-08-21 00:16:32 +0000926
Douglas Gregord60e1052009-08-27 16:57:43 +0000927 if (TemplateParams) {
928 // Our resulting instantiation is actually a function template, since we
929 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +0000930 //
Douglas Gregord60e1052009-08-27 16:57:43 +0000931 // template<typename T>
932 // struct X {
933 // template<typename U> void f(T, U);
934 // };
935 //
936 // X<int> x;
937 //
938 // We are instantiating the member template "f" within X<int>, which means
939 // substituting int for T, but leaving "f" as a member function template.
940 // Build the function template itself.
941 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
942 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +0000943 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +0000944 TemplateParams, Method);
945 if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +0000946 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +0000947 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +0000948 } else if (FunctionTemplate) {
949 // Record this function template specialization.
950 Method->setFunctionTemplateSpecialization(SemaRef.Context,
951 FunctionTemplate,
952 &TemplateArgs.getInnermost(),
953 InsertPos);
954 } else {
955 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +0000956 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +0000957 }
958
Mike Stump1eb44332009-09-09 15:08:12 +0000959 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000960 // out-of-line, the instantiation will have the same lexical
961 // context (which will be a namespace scope) as the template.
962 if (D->isOutOfLine())
963 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000964
Douglas Gregor5545e162009-03-24 00:38:23 +0000965 // Attach the parameters
966 for (unsigned P = 0; P < Params.size(); ++P)
967 Params[P]->setOwningFunction(Method);
Jay Foadbeaaccd2009-05-21 09:52:38 +0000968 Method->setParams(SemaRef.Context, Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +0000969
970 if (InitMethodInstantiation(Method, D))
971 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000972
John McCall68263142009-11-18 22:49:29 +0000973 LookupResult Previous(SemaRef, Name, SourceLocation(),
974 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Douglas Gregord60e1052009-08-27 16:57:43 +0000976 if (!FunctionTemplate || TemplateParams) {
John McCall68263142009-11-18 22:49:29 +0000977 SemaRef.LookupQualifiedName(Previous, Owner);
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Douglas Gregordec06662009-08-21 18:42:58 +0000979 // In C++, the previous declaration we find might be a tag type
980 // (class or enum). In this case, the new declaration will hide the
981 // tag type. Note that this does does not apply if we're declaring a
982 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +0000983 if (Previous.isSingleTagDecl())
984 Previous.clear();
Douglas Gregordec06662009-08-21 18:42:58 +0000985 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000986
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000987 bool Redeclaration = false;
988 bool OverloadableAttrRequired = false;
John McCall9f54ad42009-12-10 09:41:52 +0000989 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000990 /*FIXME:*/OverloadableAttrRequired);
991
Douglas Gregor4ba31362009-12-01 17:24:26 +0000992 if (D->isPure())
993 SemaRef.CheckPureMethod(Method, SourceRange());
994
John McCall46460a62010-01-20 21:53:11 +0000995 Method->setAccess(D->getAccess());
996
John McCall68263142009-11-18 22:49:29 +0000997 if (!FunctionTemplate && (!Method->isInvalidDecl() || Previous.empty()) &&
Douglas Gregora735b202009-10-13 14:39:41 +0000998 !Method->getFriendObjectKind())
Douglas Gregordec06662009-08-21 18:42:58 +0000999 Owner->addDecl(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001000
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001001 return Method;
1002}
1003
Douglas Gregor615c5d42009-03-24 16:43:20 +00001004Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +00001005 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +00001006}
1007
Douglas Gregor03b2b072009-03-24 00:15:49 +00001008Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001009 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +00001010}
1011
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001012Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001013 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001014}
1015
Douglas Gregor6477b692009-03-25 15:04:13 +00001016ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
John McCall58e46772009-10-23 21:48:59 +00001017 QualType T;
John McCalla93c9342009-12-07 02:54:59 +00001018 TypeSourceInfo *DI = D->getTypeSourceInfo();
John McCall58e46772009-10-23 21:48:59 +00001019 if (DI) {
1020 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
1021 D->getDeclName());
1022 if (DI) T = DI->getType();
1023 } else {
1024 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
1025 D->getDeclName());
1026 DI = 0;
1027 }
1028
1029 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001030 return 0;
1031
John McCall58e46772009-10-23 21:48:59 +00001032 T = SemaRef.adjustParameterType(T);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001033
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001034 // Allocate the parameter
John McCall58e46772009-10-23 21:48:59 +00001035 ParmVarDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +00001036 = ParmVarDecl::Create(SemaRef.Context,
1037 SemaRef.Context.getTranslationUnitDecl(),
1038 D->getLocation(),
John McCall58e46772009-10-23 21:48:59 +00001039 D->getIdentifier(), T, DI, D->getStorageClass(), 0);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001040
Anders Carlsson9351c172009-08-25 03:18:48 +00001041 // Mark the default argument as being uninstantiated.
Douglas Gregorf43d0b32009-09-25 06:56:31 +00001042 if (D->hasUninstantiatedDefaultArg())
1043 Param->setUninstantiatedDefaultArg(D->getUninstantiatedDefaultArg());
Douglas Gregor0ed09302009-09-25 07:03:22 +00001044 else if (Expr *Arg = D->getDefaultArg())
1045 Param->setUninstantiatedDefaultArg(Arg);
1046
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001047 // Note: we don't try to instantiate function parameters until after
1048 // we've instantiated the function's type. Therefore, we don't have
1049 // to check for 'void' parameter types here.
Douglas Gregor48dd19b2009-05-14 21:44:34 +00001050 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001051 return Param;
1052}
1053
John McCalle29ba202009-08-20 01:44:21 +00001054Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1055 TemplateTypeParmDecl *D) {
1056 // TODO: don't always clone when decls are refcounted.
1057 const Type* T = D->getTypeForDecl();
1058 assert(T->isTemplateTypeParmType());
1059 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001060
John McCalle29ba202009-08-20 01:44:21 +00001061 TemplateTypeParmDecl *Inst =
1062 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor550d9b22009-10-31 17:21:17 +00001063 TTPT->getDepth() - 1, TTPT->getIndex(),
John McCalle29ba202009-08-20 01:44:21 +00001064 TTPT->getName(),
1065 D->wasDeclaredWithTypename(),
1066 D->isParameterPack());
1067
Douglas Gregor0f8716b2009-11-09 19:17:50 +00001068 if (D->hasDefaultArgument())
1069 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
John McCalle29ba202009-08-20 01:44:21 +00001070
Douglas Gregor550d9b22009-10-31 17:21:17 +00001071 // Introduce this template parameter's instantiation into the instantiation
1072 // scope.
1073 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1074
John McCalle29ba202009-08-20 01:44:21 +00001075 return Inst;
1076}
1077
Douglas Gregor33642df2009-10-23 23:25:44 +00001078Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1079 NonTypeTemplateParmDecl *D) {
1080 // Substitute into the type of the non-type template parameter.
1081 QualType T;
John McCalla93c9342009-12-07 02:54:59 +00001082 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor33642df2009-10-23 23:25:44 +00001083 if (DI) {
1084 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
1085 D->getDeclName());
1086 if (DI) T = DI->getType();
1087 } else {
1088 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
1089 D->getDeclName());
1090 DI = 0;
1091 }
1092 if (T.isNull())
1093 return 0;
1094
1095 // Check that this type is acceptable for a non-type template parameter.
1096 bool Invalid = false;
1097 T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
1098 if (T.isNull()) {
1099 T = SemaRef.Context.IntTy;
1100 Invalid = true;
1101 }
1102
1103 NonTypeTemplateParmDecl *Param
1104 = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1105 D->getDepth() - 1, D->getPosition(),
1106 D->getIdentifier(), T, DI);
1107 if (Invalid)
1108 Param->setInvalidDecl();
1109
1110 Param->setDefaultArgument(D->getDefaultArgument());
Douglas Gregor550d9b22009-10-31 17:21:17 +00001111
1112 // Introduce this template parameter's instantiation into the instantiation
1113 // scope.
1114 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001115 return Param;
1116}
1117
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001118Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001119TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1120 TemplateTemplateParmDecl *D) {
1121 // Instantiate the template parameter list of the template template parameter.
1122 TemplateParameterList *TempParams = D->getTemplateParameters();
1123 TemplateParameterList *InstParams;
1124 {
1125 // Perform the actual substitution of template parameters within a new,
1126 // local instantiation scope.
1127 Sema::LocalInstantiationScope Scope(SemaRef);
1128 InstParams = SubstTemplateParams(TempParams);
1129 if (!InstParams)
1130 return NULL;
1131 }
1132
1133 // Build the template template parameter.
1134 TemplateTemplateParmDecl *Param
1135 = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1136 D->getDepth() - 1, D->getPosition(),
1137 D->getIdentifier(), InstParams);
1138 Param->setDefaultArgument(D->getDefaultArgument());
1139
1140 // Introduce this template parameter's instantiation into the instantiation
1141 // scope.
1142 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1143
1144 return Param;
1145}
1146
Douglas Gregor48c32a72009-11-17 06:07:40 +00001147Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1148 // Using directives are never dependent, so they require no explicit
1149
1150 UsingDirectiveDecl *Inst
1151 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1152 D->getNamespaceKeyLocation(),
1153 D->getQualifierRange(), D->getQualifier(),
1154 D->getIdentLocation(),
1155 D->getNominatedNamespace(),
1156 D->getCommonAncestor());
1157 Owner->addDecl(Inst);
1158 return Inst;
1159}
1160
John McCalled976492009-12-04 22:46:56 +00001161Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
1162 // The nested name specifier is non-dependent, so no transformation
1163 // is required.
1164
John McCall9f54ad42009-12-10 09:41:52 +00001165 // We only need to do redeclaration lookups if we're in a class
1166 // scope (in fact, it's not really even possible in non-class
1167 // scopes).
1168 bool CheckRedeclaration = Owner->isRecord();
1169
1170 LookupResult Prev(SemaRef, D->getDeclName(), D->getLocation(),
1171 Sema::LookupUsingDeclName, Sema::ForRedeclaration);
1172
John McCalled976492009-12-04 22:46:56 +00001173 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
1174 D->getLocation(),
1175 D->getNestedNameRange(),
1176 D->getUsingLocation(),
1177 D->getTargetNestedNameDecl(),
1178 D->getDeclName(),
1179 D->isTypeName());
1180
1181 CXXScopeSpec SS;
1182 SS.setScopeRep(D->getTargetNestedNameDecl());
1183 SS.setRange(D->getNestedNameRange());
John McCall9f54ad42009-12-10 09:41:52 +00001184
1185 if (CheckRedeclaration) {
1186 Prev.setHideTags(false);
1187 SemaRef.LookupQualifiedName(Prev, Owner);
1188
1189 // Check for invalid redeclarations.
1190 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
1191 D->isTypeName(), SS,
1192 D->getLocation(), Prev))
1193 NewUD->setInvalidDecl();
1194
1195 }
1196
1197 if (!NewUD->isInvalidDecl() &&
1198 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
John McCalled976492009-12-04 22:46:56 +00001199 D->getLocation()))
1200 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00001201
John McCalled976492009-12-04 22:46:56 +00001202 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1203 NewUD->setAccess(D->getAccess());
1204 Owner->addDecl(NewUD);
1205
John McCall9f54ad42009-12-10 09:41:52 +00001206 // Don't process the shadow decls for an invalid decl.
1207 if (NewUD->isInvalidDecl())
1208 return NewUD;
1209
John McCall323c3102009-12-22 22:26:37 +00001210 bool isFunctionScope = Owner->isFunctionOrMethod();
1211
John McCall9f54ad42009-12-10 09:41:52 +00001212 // Process the shadow decls.
1213 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
1214 I != E; ++I) {
1215 UsingShadowDecl *Shadow = *I;
1216 NamedDecl *InstTarget =
1217 cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getTargetDecl(),
1218 TemplateArgs));
1219
1220 if (CheckRedeclaration &&
1221 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
1222 continue;
1223
1224 UsingShadowDecl *InstShadow
1225 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
1226 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
John McCall323c3102009-12-22 22:26:37 +00001227
1228 if (isFunctionScope)
1229 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
John McCall9f54ad42009-12-10 09:41:52 +00001230 }
John McCalled976492009-12-04 22:46:56 +00001231
1232 return NewUD;
1233}
1234
1235Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00001236 // Ignore these; we handle them in bulk when processing the UsingDecl.
1237 return 0;
John McCalled976492009-12-04 22:46:56 +00001238}
1239
John McCall7ba107a2009-11-18 02:36:19 +00001240Decl * TemplateDeclInstantiator
1241 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +00001242 NestedNameSpecifier *NNS =
1243 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1244 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001245 TemplateArgs);
1246 if (!NNS)
1247 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001248
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001249 CXXScopeSpec SS;
1250 SS.setRange(D->getTargetNestedNameRange());
1251 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +00001252
1253 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00001254 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
John McCall7ba107a2009-11-18 02:36:19 +00001255 D->getUsingLoc(), SS, D->getLocation(),
1256 D->getDeclName(), 0,
1257 /*instantiation*/ true,
1258 /*typename*/ true, D->getTypenameLoc());
1259 if (UD)
John McCalled976492009-12-04 22:46:56 +00001260 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1261
John McCall7ba107a2009-11-18 02:36:19 +00001262 return UD;
1263}
1264
1265Decl * TemplateDeclInstantiator
1266 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1267 NestedNameSpecifier *NNS =
1268 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1269 D->getTargetNestedNameRange(),
1270 TemplateArgs);
1271 if (!NNS)
1272 return 0;
1273
1274 CXXScopeSpec SS;
1275 SS.setRange(D->getTargetNestedNameRange());
1276 SS.setScopeRep(NNS);
1277
1278 NamedDecl *UD =
1279 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1280 D->getUsingLoc(), SS, D->getLocation(),
1281 D->getDeclName(), 0,
1282 /*instantiation*/ true,
1283 /*typename*/ false, SourceLocation());
Anders Carlsson0d8df782009-08-29 19:37:28 +00001284 if (UD)
John McCalled976492009-12-04 22:46:56 +00001285 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1286
Anders Carlsson0d8df782009-08-29 19:37:28 +00001287 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001288}
1289
John McCallce3ff2b2009-08-25 22:02:44 +00001290Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001291 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001292 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001293 return Instantiator.Visit(D);
1294}
1295
John McCalle29ba202009-08-20 01:44:21 +00001296/// \brief Instantiates a nested template parameter list in the current
1297/// instantiation context.
1298///
1299/// \param L The parameter list to instantiate
1300///
1301/// \returns NULL if there was an error
1302TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001303TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001304 // Get errors for all the parameters before bailing out.
1305 bool Invalid = false;
1306
1307 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001308 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001309 ParamVector Params;
1310 Params.reserve(N);
1311 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1312 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001313 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001314 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001315 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00001316 }
1317
1318 // Clean up if we had an error.
1319 if (Invalid) {
1320 for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
1321 PI != PE; ++PI)
1322 if (*PI)
1323 (*PI)->Destroy(SemaRef.Context);
1324 return NULL;
1325 }
1326
1327 TemplateParameterList *InstL
1328 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1329 L->getLAngleLoc(), &Params.front(), N,
1330 L->getRAngleLoc());
1331 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001332}
John McCalle29ba202009-08-20 01:44:21 +00001333
Douglas Gregored9c0f92009-10-29 00:04:11 +00001334/// \brief Instantiate the declaration of a class template partial
1335/// specialization.
1336///
1337/// \param ClassTemplate the (instantiated) class template that is partially
1338// specialized by the instantiation of \p PartialSpec.
1339///
1340/// \param PartialSpec the (uninstantiated) class template partial
1341/// specialization that we are instantiating.
1342///
1343/// \returns true if there was an error, false otherwise.
1344bool
1345TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1346 ClassTemplateDecl *ClassTemplate,
1347 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001348 // Create a local instantiation scope for this class template partial
1349 // specialization, which will contain the instantiations of the template
1350 // parameters.
1351 Sema::LocalInstantiationScope Scope(SemaRef);
1352
Douglas Gregored9c0f92009-10-29 00:04:11 +00001353 // Substitute into the template parameters of the class template partial
1354 // specialization.
1355 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1356 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1357 if (!InstParams)
1358 return true;
1359
1360 // Substitute into the template arguments of the class template partial
1361 // specialization.
John McCall833ca992009-10-29 08:12:44 +00001362 const TemplateArgumentLoc *PartialSpecTemplateArgs
1363 = PartialSpec->getTemplateArgsAsWritten();
1364 unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1365
John McCalld5532b62009-11-23 01:53:49 +00001366 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
John McCall833ca992009-10-29 08:12:44 +00001367 for (unsigned I = 0; I != N; ++I) {
John McCalld5532b62009-11-23 01:53:49 +00001368 TemplateArgumentLoc Loc;
1369 if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs))
Douglas Gregored9c0f92009-10-29 00:04:11 +00001370 return true;
John McCalld5532b62009-11-23 01:53:49 +00001371 InstTemplateArgs.addArgument(Loc);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001372 }
1373
1374
1375 // Check that the template argument list is well-formed for this
1376 // class template.
1377 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1378 InstTemplateArgs.size());
1379 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1380 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001381 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001382 false,
1383 Converted))
1384 return true;
1385
1386 // Figure out where to insert this class template partial specialization
1387 // in the member template's set of class template partial specializations.
1388 llvm::FoldingSetNodeID ID;
1389 ClassTemplatePartialSpecializationDecl::Profile(ID,
1390 Converted.getFlatArguments(),
1391 Converted.flatSize(),
1392 SemaRef.Context);
1393 void *InsertPos = 0;
1394 ClassTemplateSpecializationDecl *PrevDecl
1395 = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
1396 InsertPos);
1397
1398 // Build the canonical type that describes the converted template
1399 // arguments of the class template partial specialization.
1400 QualType CanonType
1401 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1402 Converted.getFlatArguments(),
1403 Converted.flatSize());
1404
1405 // Build the fully-sugared type for this class template
1406 // specialization as the user wrote in the specialization
1407 // itself. This means that we'll pretty-print the type retrieved
1408 // from the specialization's declaration the way that the user
1409 // actually wrote the specialization, rather than formatting the
1410 // name based on the "canonical" representation used to store the
1411 // template arguments in the specialization.
1412 QualType WrittenTy
1413 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
John McCalld5532b62009-11-23 01:53:49 +00001414 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001415 CanonType);
1416
1417 if (PrevDecl) {
1418 // We've already seen a partial specialization with the same template
1419 // parameters and template arguments. This can happen, for example, when
1420 // substituting the outer template arguments ends up causing two
1421 // class template partial specializations of a member class template
1422 // to have identical forms, e.g.,
1423 //
1424 // template<typename T, typename U>
1425 // struct Outer {
1426 // template<typename X, typename Y> struct Inner;
1427 // template<typename Y> struct Inner<T, Y>;
1428 // template<typename Y> struct Inner<U, Y>;
1429 // };
1430 //
1431 // Outer<int, int> outer; // error: the partial specializations of Inner
1432 // // have the same signature.
1433 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1434 << WrittenTy;
1435 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1436 << SemaRef.Context.getTypeDeclType(PrevDecl);
1437 return true;
1438 }
1439
1440
1441 // Create the class template partial specialization declaration.
1442 ClassTemplatePartialSpecializationDecl *InstPartialSpec
1443 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context, Owner,
1444 PartialSpec->getLocation(),
1445 InstParams,
1446 ClassTemplate,
1447 Converted,
John McCalld5532b62009-11-23 01:53:49 +00001448 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001449 0);
1450 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
1451 InstPartialSpec->setTypeAsWritten(WrittenTy);
1452
1453 // Add this partial specialization to the set of class template partial
1454 // specializations.
1455 ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec,
1456 InsertPos);
1457 return false;
1458}
1459
John McCallce3ff2b2009-08-25 22:02:44 +00001460/// \brief Does substitution on the type of the given function, including
1461/// all of the function parameters.
Douglas Gregor5545e162009-03-24 00:38:23 +00001462///
John McCallce3ff2b2009-08-25 22:02:44 +00001463/// \param D The function whose type will be the basis of the substitution
Douglas Gregor5545e162009-03-24 00:38:23 +00001464///
1465/// \param Params the instantiated parameter declarations
1466
John McCallce3ff2b2009-08-25 22:02:44 +00001467/// \returns the instantiated function's type if successful, a NULL
Douglas Gregor5545e162009-03-24 00:38:23 +00001468/// type if there was an error.
Mike Stump1eb44332009-09-09 15:08:12 +00001469QualType
John McCallce3ff2b2009-08-25 22:02:44 +00001470TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001471 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
1472 bool InvalidDecl = false;
1473
John McCallce3ff2b2009-08-25 22:02:44 +00001474 // Substitute all of the function's formal parameter types.
Douglas Gregor7e063902009-05-11 23:53:27 +00001475 TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001476 llvm::SmallVector<QualType, 4> ParamTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001477 for (FunctionDecl::param_iterator P = D->param_begin(),
Douglas Gregor5545e162009-03-24 00:38:23 +00001478 PEnd = D->param_end();
1479 P != PEnd; ++P) {
Douglas Gregor6477b692009-03-25 15:04:13 +00001480 if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
Douglas Gregor5545e162009-03-24 00:38:23 +00001481 if (PInst->getType()->isVoidType()) {
1482 SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
1483 PInst->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001484 } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001485 PInst->getType(),
1486 diag::err_abstract_type_in_decl,
1487 Sema::AbstractParamType))
Douglas Gregor5545e162009-03-24 00:38:23 +00001488 PInst->setInvalidDecl();
1489
1490 Params.push_back(PInst);
1491 ParamTys.push_back(PInst->getType());
1492
1493 if (PInst->isInvalidDecl())
1494 InvalidDecl = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001495 } else
Douglas Gregor5545e162009-03-24 00:38:23 +00001496 InvalidDecl = true;
1497 }
1498
1499 // FIXME: Deallocate dead declarations.
1500 if (InvalidDecl)
1501 return QualType();
1502
John McCall183700f2009-09-21 23:43:11 +00001503 const FunctionProtoType *Proto = D->getType()->getAs<FunctionProtoType>();
Douglas Gregor5545e162009-03-24 00:38:23 +00001504 assert(Proto && "Missing prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +00001505 QualType ResultType
John McCallce3ff2b2009-08-25 22:02:44 +00001506 = SemaRef.SubstType(Proto->getResultType(), TemplateArgs,
1507 D->getLocation(), D->getDeclName());
Douglas Gregor5545e162009-03-24 00:38:23 +00001508 if (ResultType.isNull())
1509 return QualType();
1510
Jay Foadbeaaccd2009-05-21 09:52:38 +00001511 return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
Douglas Gregor5545e162009-03-24 00:38:23 +00001512 Proto->isVariadic(), Proto->getTypeQuals(),
1513 D->getLocation(), D->getDeclName());
1514}
1515
Mike Stump1eb44332009-09-09 15:08:12 +00001516/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00001517/// declaration (New) from the corresponding fields of its template (Tmpl).
1518///
1519/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001520bool
1521TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00001522 FunctionDecl *Tmpl) {
1523 if (Tmpl->isDeleted())
1524 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00001525
Douglas Gregorcca9e962009-07-01 22:01:06 +00001526 // If we are performing substituting explicitly-specified template arguments
1527 // or deduced template arguments into a function template and we reach this
1528 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00001529 // to keeping the new function template specialization. We therefore
1530 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00001531 // into a template instantiation for this specific function template
1532 // specialization, which is not a SFINAE context, so that we diagnose any
1533 // further errors in the declaration itself.
1534 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1535 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1536 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1537 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00001538 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00001539 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001540 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00001541 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00001542 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001543 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1544 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
Douglas Gregorf35f8282009-11-11 21:54:23 +00001545 --SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001546 }
1547 }
Mike Stump1eb44332009-09-09 15:08:12 +00001548
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001549 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
1550 assert(Proto && "Function template without prototype?");
1551
1552 if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
1553 Proto->getNoReturnAttr()) {
1554 // The function has an exception specification or a "noreturn"
1555 // attribute. Substitute into each of the exception types.
1556 llvm::SmallVector<QualType, 4> Exceptions;
1557 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
1558 // FIXME: Poor location information!
1559 QualType T
1560 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
1561 New->getLocation(), New->getDeclName());
1562 if (T.isNull() ||
1563 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
1564 continue;
1565
1566 Exceptions.push_back(T);
1567 }
1568
1569 // Rebuild the function type
1570
1571 const FunctionProtoType *NewProto
1572 = New->getType()->getAs<FunctionProtoType>();
1573 assert(NewProto && "Template instantiation without function prototype?");
1574 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
1575 NewProto->arg_type_begin(),
1576 NewProto->getNumArgs(),
1577 NewProto->isVariadic(),
1578 NewProto->getTypeQuals(),
1579 Proto->hasExceptionSpec(),
1580 Proto->hasAnyExceptionSpec(),
1581 Exceptions.size(),
1582 Exceptions.data(),
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001583 Proto->getNoReturnAttr(),
1584 Proto->getCallConv()));
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001585 }
1586
Douglas Gregore53060f2009-06-25 22:08:12 +00001587 return false;
1588}
1589
Douglas Gregor5545e162009-03-24 00:38:23 +00001590/// \brief Initializes common fields of an instantiated method
1591/// declaration (New) from the corresponding fields of its template
1592/// (Tmpl).
1593///
1594/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001595bool
1596TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00001597 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001598 if (InitFunctionInstantiation(New, Tmpl))
1599 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001600
Douglas Gregor5545e162009-03-24 00:38:23 +00001601 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
1602 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00001603 if (Tmpl->isVirtualAsWritten())
1604 Record->setMethodAsVirtual(New);
Douglas Gregor5545e162009-03-24 00:38:23 +00001605
1606 // FIXME: attributes
1607 // FIXME: New needs a pointer to Tmpl
1608 return false;
1609}
Douglas Gregora58861f2009-05-13 20:28:22 +00001610
1611/// \brief Instantiate the definition of the given function from its
1612/// template.
1613///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001614/// \param PointOfInstantiation the point at which the instantiation was
1615/// required. Note that this is not precisely a "point of instantiation"
1616/// for the function, but it's close.
1617///
Douglas Gregora58861f2009-05-13 20:28:22 +00001618/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001619/// function template specialization or member function of a class template
1620/// specialization.
1621///
1622/// \param Recursive if true, recursively instantiates any functions that
1623/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001624///
1625/// \param DefinitionRequired if true, then we are performing an explicit
1626/// instantiation where the body of the function is required. Complain if
1627/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001628void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001629 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001630 bool Recursive,
1631 bool DefinitionRequired) {
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001632 if (Function->isInvalidDecl())
1633 return;
1634
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001635 assert(!Function->getBody() && "Already instantiated!");
Mike Stump1eb44332009-09-09 15:08:12 +00001636
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001637 // Never instantiate an explicit specialization.
1638 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1639 return;
1640
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001641 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00001642 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001643 Stmt *Pattern = 0;
1644 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001645 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001646
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001647 if (!Pattern) {
1648 if (DefinitionRequired) {
1649 if (Function->getPrimaryTemplate())
1650 Diag(PointOfInstantiation,
1651 diag::err_explicit_instantiation_undefined_func_template)
1652 << Function->getPrimaryTemplate();
1653 else
1654 Diag(PointOfInstantiation,
1655 diag::err_explicit_instantiation_undefined_member)
1656 << 1 << Function->getDeclName() << Function->getDeclContext();
1657
1658 if (PatternDecl)
1659 Diag(PatternDecl->getLocation(),
1660 diag::note_explicit_instantiation_here);
1661 }
1662
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001663 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001664 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001665
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001666 // C++0x [temp.explicit]p9:
1667 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00001668 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001669 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00001670 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001671 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001672 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001673 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001674
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001675 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
1676 if (Inst)
1677 return;
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001678
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001679 // If we're performing recursive template instantiation, create our own
1680 // queue of pending implicit instantiations that we will instantiate later,
1681 // while we're still within our own instantiation context.
1682 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1683 if (Recursive)
1684 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001685
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001686 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
1687
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001688 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00001689 // recorded, unless we're actually a member function within a local
1690 // class, in which case we need to merge our results with the parent
1691 // scope (of the enclosing function).
1692 bool MergeWithParentScope = false;
1693 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
1694 MergeWithParentScope = Rec->isLocalClass();
1695
1696 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001697
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001698 // Introduce the instantiated function parameters into the local
1699 // instantiation scope.
1700 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
1701 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
1702 Function->getParamDecl(I));
1703
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001704 // Enter the scope of this instantiation. We don't use
1705 // PushDeclContext because we don't have a scope.
1706 DeclContext *PreviousContext = CurContext;
1707 CurContext = Function;
1708
Mike Stump1eb44332009-09-09 15:08:12 +00001709 MultiLevelTemplateArgumentList TemplateArgs =
Anders Carlsson09025312009-08-29 05:16:22 +00001710 getTemplateInstantiationArgs(Function);
1711
1712 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001713 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00001714 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
1715 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
1716 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001717 }
1718
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001719 // Instantiate the function body.
Anders Carlsson09025312009-08-29 05:16:22 +00001720 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001721
Douglas Gregor52604ab2009-09-11 21:19:12 +00001722 if (Body.isInvalid())
1723 Function->setInvalidDecl();
1724
Mike Stump1eb44332009-09-09 15:08:12 +00001725 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001726 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001727
1728 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00001729
1730 DeclGroupRef DG(Function);
1731 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001732
Douglas Gregor60406be2010-01-16 22:29:39 +00001733 // This class may have local implicit instantiations that need to be
1734 // instantiation within this scope.
1735 PerformPendingImplicitInstantiations(/*LocalOnly=*/true);
1736 Scope.Exit();
1737
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001738 if (Recursive) {
1739 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001740 // instantiation of this template.
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001741 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001742
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001743 // Restore the set of pending implicit instantiations.
1744 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1745 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001746}
1747
1748/// \brief Instantiate the definition of the given variable from its
1749/// template.
1750///
Douglas Gregor7caa6822009-07-24 20:34:43 +00001751/// \param PointOfInstantiation the point at which the instantiation was
1752/// required. Note that this is not precisely a "point of instantiation"
1753/// for the function, but it's close.
1754///
1755/// \param Var the already-instantiated declaration of a static member
1756/// variable of a class template specialization.
1757///
1758/// \param Recursive if true, recursively instantiates any functions that
1759/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001760///
1761/// \param DefinitionRequired if true, then we are performing an explicit
1762/// instantiation where an out-of-line definition of the member variable
1763/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001764void Sema::InstantiateStaticDataMemberDefinition(
1765 SourceLocation PointOfInstantiation,
1766 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001767 bool Recursive,
1768 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001769 if (Var->isInvalidDecl())
1770 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001771
Douglas Gregor7caa6822009-07-24 20:34:43 +00001772 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001773 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00001774 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00001775 assert(Def->isStaticDataMember() && "Not a static data member?");
1776 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00001777
Douglas Gregor0d035142009-10-27 18:42:08 +00001778 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001779 // We did not find an out-of-line definition of this static data member,
1780 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00001781 // instantiate this definition (or provide a specialization for it) in
1782 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001783 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001784 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001785 Diag(PointOfInstantiation,
1786 diag::err_explicit_instantiation_undefined_member)
1787 << 2 << Var->getDeclName() << Var->getDeclContext();
1788 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
1789 }
1790
Douglas Gregor7caa6822009-07-24 20:34:43 +00001791 return;
1792 }
1793
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001794 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001795 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001796 return;
1797
1798 // C++0x [temp.explicit]p9:
1799 // Except for inline functions, other explicit instantiation declarations
1800 // have the effect of suppressing the implicit instantiation of the entity
1801 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001802 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001803 == TSK_ExplicitInstantiationDeclaration)
1804 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001805
Douglas Gregor7caa6822009-07-24 20:34:43 +00001806 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
1807 if (Inst)
1808 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001809
Douglas Gregor7caa6822009-07-24 20:34:43 +00001810 // If we're performing recursive template instantiation, create our own
1811 // queue of pending implicit instantiations that we will instantiate later,
1812 // while we're still within our own instantiation context.
1813 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1814 if (Recursive)
1815 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001816
Douglas Gregor7caa6822009-07-24 20:34:43 +00001817 // Enter the scope of this instantiation. We don't use
1818 // PushDeclContext because we don't have a scope.
1819 DeclContext *PreviousContext = CurContext;
1820 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001821
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001822 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00001823 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00001824 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00001825 CurContext = PreviousContext;
1826
1827 if (Var) {
Douglas Gregor583f33b2009-10-15 18:07:02 +00001828 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
1829 assert(MSInfo && "Missing member specialization information?");
1830 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
1831 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00001832 DeclGroupRef DG(Var);
1833 Consumer.HandleTopLevelDecl(DG);
1834 }
Mike Stump1eb44332009-09-09 15:08:12 +00001835
Douglas Gregor7caa6822009-07-24 20:34:43 +00001836 if (Recursive) {
1837 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001838 // instantiation of this template.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001839 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001840
Douglas Gregor7caa6822009-07-24 20:34:43 +00001841 // Restore the set of pending implicit instantiations.
1842 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001843 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001844}
Douglas Gregor815215d2009-05-27 05:35:12 +00001845
Anders Carlsson09025312009-08-29 05:16:22 +00001846void
1847Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
1848 const CXXConstructorDecl *Tmpl,
1849 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001850
Anders Carlsson09025312009-08-29 05:16:22 +00001851 llvm::SmallVector<MemInitTy*, 4> NewInits;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001852 bool AnyErrors = false;
1853
Anders Carlsson09025312009-08-29 05:16:22 +00001854 // Instantiate all the initializers.
1855 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00001856 InitsEnd = Tmpl->init_end();
1857 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00001858 CXXBaseOrMemberInitializer *Init = *Inits;
1859
1860 ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001861 llvm::SmallVector<SourceLocation, 4> CommaLocs;
Mike Stump1eb44332009-09-09 15:08:12 +00001862
Anders Carlsson09025312009-08-29 05:16:22 +00001863 // Instantiate all the arguments.
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001864 Expr *InitE = Init->getInit();
1865 if (!InitE) {
1866 // Nothing to instantiate;
1867 } else if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(InitE)) {
1868 if (InstantiateInitializationArguments(*this, ParenList->getExprs(),
1869 ParenList->getNumExprs(),
1870 TemplateArgs, CommaLocs,
1871 NewArgs)) {
1872 AnyErrors = true;
1873 continue;
1874 }
1875 } else {
1876 OwningExprResult InitArg = SubstExpr(InitE, TemplateArgs);
1877 if (InitArg.isInvalid()) {
1878 AnyErrors = true;
1879 continue;
1880 }
1881
1882 NewArgs.push_back(InitArg.release());
Anders Carlsson09025312009-08-29 05:16:22 +00001883 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001884
Anders Carlsson09025312009-08-29 05:16:22 +00001885 MemInitResult NewInit;
Anders Carlsson09025312009-08-29 05:16:22 +00001886 if (Init->isBaseInitializer()) {
John McCalla93c9342009-12-07 02:54:59 +00001887 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
Douglas Gregor802ab452009-12-02 22:36:29 +00001888 TemplateArgs,
1889 Init->getSourceLocation(),
1890 New->getDeclName());
John McCalla93c9342009-12-07 02:54:59 +00001891 if (!BaseTInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001892 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00001893 New->setInvalidDecl();
1894 continue;
1895 }
1896
John McCalla93c9342009-12-07 02:54:59 +00001897 NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001898 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001899 NewArgs.size(),
Douglas Gregor802ab452009-12-02 22:36:29 +00001900 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00001901 Init->getRParenLoc(),
1902 New->getParent());
1903 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001904 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00001905
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001906 // Is this an anonymous union?
1907 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregore95b4092009-09-16 18:34:49 +00001908 Member = cast<FieldDecl>(FindInstantiatedDecl(UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001909 else
Douglas Gregore95b4092009-09-16 18:34:49 +00001910 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMember(),
1911 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00001912
1913 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001914 NewArgs.size(),
1915 Init->getSourceLocation(),
Douglas Gregor802ab452009-12-02 22:36:29 +00001916 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00001917 Init->getRParenLoc());
1918 }
1919
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001920 if (NewInit.isInvalid()) {
1921 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00001922 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001923 } else {
Anders Carlsson09025312009-08-29 05:16:22 +00001924 // FIXME: It would be nice if ASTOwningVector had a release function.
1925 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001926
Anders Carlsson09025312009-08-29 05:16:22 +00001927 NewInits.push_back((MemInitTy *)NewInit.get());
1928 }
1929 }
Mike Stump1eb44332009-09-09 15:08:12 +00001930
Anders Carlsson09025312009-08-29 05:16:22 +00001931 // Assign all the initializers to the new constructor.
Mike Stump1eb44332009-09-09 15:08:12 +00001932 ActOnMemInitializers(DeclPtrTy::make(New),
Anders Carlsson09025312009-08-29 05:16:22 +00001933 /*FIXME: ColonLoc */
1934 SourceLocation(),
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001935 NewInits.data(), NewInits.size(),
1936 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00001937}
1938
John McCall52a575a2009-08-29 08:11:13 +00001939// TODO: this could be templated if the various decl types used the
1940// same method name.
1941static bool isInstantiationOf(ClassTemplateDecl *Pattern,
1942 ClassTemplateDecl *Instance) {
1943 Pattern = Pattern->getCanonicalDecl();
1944
1945 do {
1946 Instance = Instance->getCanonicalDecl();
1947 if (Pattern == Instance) return true;
1948 Instance = Instance->getInstantiatedFromMemberTemplate();
1949 } while (Instance);
1950
1951 return false;
1952}
1953
Douglas Gregor0d696532009-09-28 06:34:35 +00001954static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
1955 FunctionTemplateDecl *Instance) {
1956 Pattern = Pattern->getCanonicalDecl();
1957
1958 do {
1959 Instance = Instance->getCanonicalDecl();
1960 if (Pattern == Instance) return true;
1961 Instance = Instance->getInstantiatedFromMemberTemplate();
1962 } while (Instance);
1963
1964 return false;
1965}
1966
Douglas Gregored9c0f92009-10-29 00:04:11 +00001967static bool
1968isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
1969 ClassTemplatePartialSpecializationDecl *Instance) {
1970 Pattern
1971 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
1972 do {
1973 Instance = cast<ClassTemplatePartialSpecializationDecl>(
1974 Instance->getCanonicalDecl());
1975 if (Pattern == Instance)
1976 return true;
1977 Instance = Instance->getInstantiatedFromMember();
1978 } while (Instance);
1979
1980 return false;
1981}
1982
John McCall52a575a2009-08-29 08:11:13 +00001983static bool isInstantiationOf(CXXRecordDecl *Pattern,
1984 CXXRecordDecl *Instance) {
1985 Pattern = Pattern->getCanonicalDecl();
1986
1987 do {
1988 Instance = Instance->getCanonicalDecl();
1989 if (Pattern == Instance) return true;
1990 Instance = Instance->getInstantiatedFromMemberClass();
1991 } while (Instance);
1992
1993 return false;
1994}
1995
1996static bool isInstantiationOf(FunctionDecl *Pattern,
1997 FunctionDecl *Instance) {
1998 Pattern = Pattern->getCanonicalDecl();
1999
2000 do {
2001 Instance = Instance->getCanonicalDecl();
2002 if (Pattern == Instance) return true;
2003 Instance = Instance->getInstantiatedFromMemberFunction();
2004 } while (Instance);
2005
2006 return false;
2007}
2008
2009static bool isInstantiationOf(EnumDecl *Pattern,
2010 EnumDecl *Instance) {
2011 Pattern = Pattern->getCanonicalDecl();
2012
2013 do {
2014 Instance = Instance->getCanonicalDecl();
2015 if (Pattern == Instance) return true;
2016 Instance = Instance->getInstantiatedFromMemberEnum();
2017 } while (Instance);
2018
2019 return false;
2020}
2021
John McCalled976492009-12-04 22:46:56 +00002022static bool isInstantiationOf(UsingShadowDecl *Pattern,
2023 UsingShadowDecl *Instance,
2024 ASTContext &C) {
2025 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2026}
2027
2028static bool isInstantiationOf(UsingDecl *Pattern,
2029 UsingDecl *Instance,
2030 ASTContext &C) {
2031 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2032}
2033
John McCall7ba107a2009-11-18 02:36:19 +00002034static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
2035 UsingDecl *Instance,
2036 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002037 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00002038}
2039
2040static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00002041 UsingDecl *Instance,
2042 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002043 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00002044}
2045
John McCall52a575a2009-08-29 08:11:13 +00002046static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
2047 VarDecl *Instance) {
2048 assert(Instance->isStaticDataMember());
2049
2050 Pattern = Pattern->getCanonicalDecl();
2051
2052 do {
2053 Instance = Instance->getCanonicalDecl();
2054 if (Pattern == Instance) return true;
2055 Instance = Instance->getInstantiatedFromStaticDataMember();
2056 } while (Instance);
2057
2058 return false;
2059}
2060
John McCalled976492009-12-04 22:46:56 +00002061// Other is the prospective instantiation
2062// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00002063static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002064 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00002065 if (UnresolvedUsingTypenameDecl *UUD
2066 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
2067 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2068 return isInstantiationOf(UUD, UD, Ctx);
2069 }
2070 }
2071
2072 if (UnresolvedUsingValueDecl *UUD
2073 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002074 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2075 return isInstantiationOf(UUD, UD, Ctx);
2076 }
2077 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002078
Anders Carlsson0d8df782009-08-29 19:37:28 +00002079 return false;
2080 }
Mike Stump1eb44332009-09-09 15:08:12 +00002081
John McCall52a575a2009-08-29 08:11:13 +00002082 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
2083 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002084
John McCall52a575a2009-08-29 08:11:13 +00002085 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
2086 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00002087
John McCall52a575a2009-08-29 08:11:13 +00002088 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
2089 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00002090
Douglas Gregor7caa6822009-07-24 20:34:43 +00002091 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00002092 if (Var->isStaticDataMember())
2093 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
2094
2095 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
2096 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00002097
Douglas Gregor0d696532009-09-28 06:34:35 +00002098 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
2099 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
2100
Douglas Gregored9c0f92009-10-29 00:04:11 +00002101 if (ClassTemplatePartialSpecializationDecl *PartialSpec
2102 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2103 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2104 PartialSpec);
2105
Anders Carlssond8b285f2009-09-01 04:26:58 +00002106 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2107 if (!Field->getDeclName()) {
2108 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00002109 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00002110 cast<FieldDecl>(D);
2111 }
2112 }
Mike Stump1eb44332009-09-09 15:08:12 +00002113
John McCalled976492009-12-04 22:46:56 +00002114 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2115 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2116
2117 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2118 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2119
Douglas Gregor815215d2009-05-27 05:35:12 +00002120 return D->getDeclName() && isa<NamedDecl>(Other) &&
2121 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2122}
2123
2124template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00002125static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00002126 NamedDecl *D,
2127 ForwardIterator first,
2128 ForwardIterator last) {
2129 for (; first != last; ++first)
2130 if (isInstantiationOf(Ctx, D, *first))
2131 return cast<NamedDecl>(*first);
2132
2133 return 0;
2134}
2135
John McCall02cace72009-08-28 07:59:38 +00002136/// \brief Finds the instantiation of the given declaration context
2137/// within the current instantiation.
2138///
2139/// \returns NULL if there was an error
Douglas Gregore95b4092009-09-16 18:34:49 +00002140DeclContext *Sema::FindInstantiatedContext(DeclContext* DC,
2141 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00002142 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00002143 Decl* ID = FindInstantiatedDecl(D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00002144 return cast_or_null<DeclContext>(ID);
2145 } else return DC;
2146}
2147
Douglas Gregored961e72009-05-27 17:54:46 +00002148/// \brief Find the instantiation of the given declaration within the
2149/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00002150///
2151/// This routine is intended to be used when \p D is a declaration
2152/// referenced from within a template, that needs to mapped into the
2153/// corresponding declaration within an instantiation. For example,
2154/// given:
2155///
2156/// \code
2157/// template<typename T>
2158/// struct X {
2159/// enum Kind {
2160/// KnownValue = sizeof(T)
2161/// };
2162///
2163/// bool getKind() const { return KnownValue; }
2164/// };
2165///
2166/// template struct X<int>;
2167/// \endcode
2168///
2169/// In the instantiation of X<int>::getKind(), we need to map the
2170/// EnumConstantDecl for KnownValue (which refers to
2171/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00002172/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2173/// this mapping from within the instantiation of X<int>.
Douglas Gregore95b4092009-09-16 18:34:49 +00002174NamedDecl *Sema::FindInstantiatedDecl(NamedDecl *D,
2175 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00002176 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00002177 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00002178 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
Douglas Gregor550d9b22009-10-31 17:21:17 +00002179 ParentDC->isFunctionOrMethod()) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002180 // D is a local of some kind. Look into the map of local
2181 // declarations to their instantiations.
2182 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
2183 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002184
Douglas Gregore95b4092009-09-16 18:34:49 +00002185 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2186 if (!Record->isDependentContext())
2187 return D;
2188
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002189 // If the RecordDecl is actually the injected-class-name or a
2190 // "templated" declaration for a class template, class template
2191 // partial specialization, or a member class of a class template,
2192 // substitute into the injected-class-name of the class template
2193 // or partial specialization to find the new DeclContext.
Douglas Gregore95b4092009-09-16 18:34:49 +00002194 QualType T;
2195 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2196
2197 if (ClassTemplate) {
2198 T = ClassTemplate->getInjectedClassNameType(Context);
2199 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2200 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
2201 T = Context.getTypeDeclType(Record);
2202 ClassTemplate = PartialSpec->getSpecializedTemplate();
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002203 }
Douglas Gregore95b4092009-09-16 18:34:49 +00002204
2205 if (!T.isNull()) {
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002206 // Substitute into the injected-class-name to get the type
2207 // corresponding to the instantiation we want, which may also be
2208 // the current instantiation (if we're in a template
2209 // definition). This substitution should never fail, since we
2210 // know we can instantiate the injected-class-name or we
2211 // wouldn't have gotten to the injected-class-name!
2212
2213 // FIXME: Can we use the CurrentInstantiationScope to avoid this
2214 // extra instantiation in the common case?
Douglas Gregore95b4092009-09-16 18:34:49 +00002215 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2216 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2217
2218 if (!T->isDependentType()) {
2219 assert(T->isRecordType() && "Instantiation must produce a record type");
2220 return T->getAs<RecordType>()->getDecl();
2221 }
2222
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002223 // We are performing "partial" template instantiation to create
2224 // the member declarations for the members of a class template
2225 // specialization. Therefore, D is actually referring to something
2226 // in the current instantiation. Look through the current
2227 // context, which contains actual instantiations, to find the
2228 // instantiation of the "current instantiation" that D refers
2229 // to.
2230 bool SawNonDependentContext = false;
Mike Stump1eb44332009-09-09 15:08:12 +00002231 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00002232 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002233 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002234 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00002235 if (isInstantiationOf(ClassTemplate,
2236 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00002237 return Spec;
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002238
2239 if (!DC->isDependentContext())
2240 SawNonDependentContext = true;
John McCall52a575a2009-08-29 08:11:13 +00002241 }
2242
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002243 // We're performing "instantiation" of a member of the current
2244 // instantiation while we are type-checking the
2245 // definition. Compute the declaration context and return that.
2246 assert(!SawNonDependentContext &&
2247 "No dependent context while instantiating record");
2248 DeclContext *DC = computeDeclContext(T);
2249 assert(DC &&
John McCall52a575a2009-08-29 08:11:13 +00002250 "Unable to find declaration for the current instantiation");
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002251 return cast<CXXRecordDecl>(DC);
John McCall52a575a2009-08-29 08:11:13 +00002252 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002253
Douglas Gregore95b4092009-09-16 18:34:49 +00002254 // Fall through to deal with other dependent record types (e.g.,
2255 // anonymous unions in class templates).
2256 }
John McCall52a575a2009-08-29 08:11:13 +00002257
Douglas Gregore95b4092009-09-16 18:34:49 +00002258 if (!ParentDC->isDependentContext())
2259 return D;
2260
2261 ParentDC = FindInstantiatedContext(ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002262 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00002263 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002264
Douglas Gregor815215d2009-05-27 05:35:12 +00002265 if (ParentDC != D->getDeclContext()) {
2266 // We performed some kind of instantiation in the parent context,
2267 // so now we need to look into the instantiated parent context to
2268 // find the instantiation of the declaration D.
2269 NamedDecl *Result = 0;
2270 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002271 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00002272 Result = findInstantiationOf(Context, D, Found.first, Found.second);
2273 } else {
2274 // Since we don't have a name for the entity we're looking for,
2275 // our only option is to walk through all of the declarations to
2276 // find that name. This will occur in a few cases:
2277 //
2278 // - anonymous struct/union within a template
2279 // - unnamed class/struct/union/enum within a template
2280 //
2281 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00002282 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002283 ParentDC->decls_begin(),
2284 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00002285 }
Mike Stump1eb44332009-09-09 15:08:12 +00002286
John McCall9f54ad42009-12-10 09:41:52 +00002287 // UsingShadowDecls can instantiate to nothing because of using hiding.
2288 assert((Result || isa<UsingShadowDecl>(D))
2289 && "Unable to find instantiation of declaration!");
2290
Douglas Gregor815215d2009-05-27 05:35:12 +00002291 D = Result;
2292 }
2293
Douglas Gregor815215d2009-05-27 05:35:12 +00002294 return D;
2295}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002296
Mike Stump1eb44332009-09-09 15:08:12 +00002297/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002298/// instantiations we have seen until this point.
Douglas Gregor60406be2010-01-16 22:29:39 +00002299void Sema::PerformPendingImplicitInstantiations(bool LocalOnly) {
2300 while (!PendingLocalImplicitInstantiations.empty() ||
2301 (!LocalOnly && !PendingImplicitInstantiations.empty())) {
2302 PendingImplicitInstantiation Inst;
2303
2304 if (PendingLocalImplicitInstantiations.empty()) {
2305 Inst = PendingImplicitInstantiations.front();
2306 PendingImplicitInstantiations.pop_front();
2307 } else {
2308 Inst = PendingLocalImplicitInstantiations.front();
2309 PendingLocalImplicitInstantiations.pop_front();
2310 }
Mike Stump1eb44332009-09-09 15:08:12 +00002311
Douglas Gregor7caa6822009-07-24 20:34:43 +00002312 // Instantiate function definitions
2313 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002314 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002315 Function->getLocation(), *this,
2316 Context.getSourceManager(),
2317 "instantiating function definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002318
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00002319 if (!Function->getBody())
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002320 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00002321 continue;
2322 }
Mike Stump1eb44332009-09-09 15:08:12 +00002323
Douglas Gregor7caa6822009-07-24 20:34:43 +00002324 // Instantiate static data member definitions.
2325 VarDecl *Var = cast<VarDecl>(Inst.first);
2326 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002327
Mike Stump1eb44332009-09-09 15:08:12 +00002328 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002329 Var->getLocation(), *this,
2330 Context.getSourceManager(),
2331 "instantiating static data member "
2332 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002333
Douglas Gregor7caa6822009-07-24 20:34:43 +00002334 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002335 }
2336}