blob: f8f22174645e4a0637768366456ee75b12f87194 [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"
Douglas Gregoraba43bb2009-05-26 20:50:29 +000013#include "clang/AST/ASTConsumer.h"
Douglas Gregor8dbc2692009-03-17 21:15:40 +000014#include "clang/AST/ASTContext.h"
15#include "clang/AST/DeclTemplate.h"
16#include "clang/AST/DeclVisitor.h"
17#include "clang/AST/Expr.h"
Anders Carlssonc17fb7b2009-09-01 05:12:24 +000018#include "clang/Basic/PrettyStackTrace.h"
Douglas Gregor83ddad32009-08-26 21:14:46 +000019#include "clang/Lex/Preprocessor.h"
Douglas Gregor8dbc2692009-03-17 21:15:40 +000020#include "llvm/Support/Compiler.h"
21
22using namespace clang;
23
24namespace {
Mike Stump1eb44332009-09-09 15:08:12 +000025 class VISIBILITY_HIDDEN TemplateDeclInstantiator
Chris Lattnerb28317a2009-03-28 19:18:32 +000026 : public DeclVisitor<TemplateDeclInstantiator, Decl *> {
Douglas Gregor8dbc2692009-03-17 21:15:40 +000027 Sema &SemaRef;
28 DeclContext *Owner;
Douglas Gregord6350ae2009-08-28 20:31:08 +000029 const MultiLevelTemplateArgumentList &TemplateArgs;
Mike Stump1eb44332009-09-09 15:08:12 +000030
Douglas Gregor8dbc2692009-03-17 21:15:40 +000031 public:
32 typedef Sema::OwningExprResult OwningExprResult;
33
34 TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +000035 const MultiLevelTemplateArgumentList &TemplateArgs)
Douglas Gregor7e063902009-05-11 23:53:27 +000036 : SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs) { }
Mike Stump1eb44332009-09-09 15:08:12 +000037
Mike Stump390b4cc2009-05-16 07:39:55 +000038 // FIXME: Once we get closer to completion, replace these manually-written
39 // declarations with automatically-generated ones from
40 // clang/AST/DeclNodes.def.
Douglas Gregor4f722be2009-03-25 15:45:12 +000041 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
42 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000043 Decl *VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +000044 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000045 Decl *VisitFieldDecl(FieldDecl *D);
46 Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
47 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor6477b692009-03-25 15:04:13 +000048 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
John McCall02cace72009-08-28 07:59:38 +000049 Decl *VisitFriendDecl(FriendDecl *D);
Douglas Gregora735b202009-10-13 14:39:41 +000050 Decl *VisitFunctionDecl(FunctionDecl *D,
51 TemplateParameterList *TemplateParams = 0);
Douglas Gregord475b8d2009-03-25 21:17:03 +000052 Decl *VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregord60e1052009-08-27 16:57:43 +000053 Decl *VisitCXXMethodDecl(CXXMethodDecl *D,
54 TemplateParameterList *TemplateParams = 0);
Douglas Gregor615c5d42009-03-24 16:43:20 +000055 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
Douglas Gregor03b2b072009-03-24 00:15:49 +000056 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +000057 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor6477b692009-03-25 15:04:13 +000058 ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D);
John McCalle29ba202009-08-20 01:44:21 +000059 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregor7974c3b2009-10-07 17:21:34 +000060 Decl *VisitClassTemplatePartialSpecializationDecl(
61 ClassTemplatePartialSpecializationDecl *D);
Douglas Gregord60e1052009-08-27 16:57:43 +000062 Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
John McCalle29ba202009-08-20 01:44:21 +000063 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Douglas Gregor33642df2009-10-23 23:25:44 +000064 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
Anders Carlsson0dde18e2009-08-28 15:18:15 +000065 Decl *VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D);
Mike Stump1eb44332009-09-09 15:08:12 +000066
Douglas Gregor8dbc2692009-03-17 21:15:40 +000067 // Base case. FIXME: Remove once we can instantiate everything.
Mike Stump1eb44332009-09-09 15:08:12 +000068 Decl *VisitDecl(Decl *) {
Douglas Gregor3d7a12a2009-03-25 23:32:15 +000069 assert(false && "Template instantiation of unknown declaration kind!");
Douglas Gregor8dbc2692009-03-17 21:15:40 +000070 return 0;
71 }
Douglas Gregor5545e162009-03-24 00:38:23 +000072
John McCallfd810b12009-08-14 02:03:10 +000073 const LangOptions &getLangOptions() {
74 return SemaRef.getLangOptions();
75 }
76
Douglas Gregor5545e162009-03-24 00:38:23 +000077 // Helper functions for instantiating methods.
John McCallce3ff2b2009-08-25 22:02:44 +000078 QualType SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +000079 llvm::SmallVectorImpl<ParmVarDecl *> &Params);
Douglas Gregore53060f2009-06-25 22:08:12 +000080 bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
Douglas Gregor5545e162009-03-24 00:38:23 +000081 bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
John McCalle29ba202009-08-20 01:44:21 +000082
83 TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +000084 SubstTemplateParams(TemplateParameterList *List);
Douglas Gregored9c0f92009-10-29 00:04:11 +000085
86 bool InstantiateClassTemplatePartialSpecialization(
87 ClassTemplateDecl *ClassTemplate,
88 ClassTemplatePartialSpecializationDecl *PartialSpec);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000089 };
90}
91
Douglas Gregor4f722be2009-03-25 15:45:12 +000092Decl *
93TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
94 assert(false && "Translation units cannot be instantiated");
95 return D;
96}
97
98Decl *
99TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
100 assert(false && "Namespaces cannot be instantiated");
101 return D;
102}
103
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000104Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
105 bool Invalid = false;
John McCallba6a9bd2009-10-24 08:00:42 +0000106 DeclaratorInfo *DI = D->getTypeDeclaratorInfo();
107 if (DI->getType()->isDependentType()) {
108 DI = SemaRef.SubstType(DI, TemplateArgs,
109 D->getLocation(), D->getDeclName());
110 if (!DI) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000111 Invalid = true;
John McCallba6a9bd2009-10-24 08:00:42 +0000112 DI = SemaRef.Context.getTrivialDeclaratorInfo(SemaRef.Context.IntTy);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000113 }
114 }
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000116 // Create the new typedef
117 TypedefDecl *Typedef
118 = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
John McCallba6a9bd2009-10-24 08:00:42 +0000119 D->getIdentifier(), DI);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000120 if (Invalid)
121 Typedef->setInvalidDecl();
122
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000123 Owner->addDecl(Typedef);
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000125 return Typedef;
126}
127
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000128Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000129 // Do substitution on the type of the declaration
John McCall0a5fa062009-10-21 02:39:02 +0000130 DeclaratorInfo *DI = SemaRef.SubstType(D->getDeclaratorInfo(),
131 TemplateArgs,
132 D->getTypeSpecStartLoc(),
133 D->getDeclName());
134 if (!DI)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000135 return 0;
136
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000137 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000138 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
139 D->getLocation(), D->getIdentifier(),
John McCall0a5fa062009-10-21 02:39:02 +0000140 DI->getType(), DI,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000141 D->getStorageClass());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000142 Var->setThreadSpecified(D->isThreadSpecified());
143 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
144 Var->setDeclaredInCondition(D->isDeclaredInCondition());
Mike Stump1eb44332009-09-09 15:08:12 +0000145
146 // If we are instantiating a static data member defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000147 // out-of-line, the instantiation will have the same lexical
148 // context (which will be a namespace scope) as the template.
149 if (D->isOutOfLine())
150 Var->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Mike Stump390b4cc2009-05-16 07:39:55 +0000152 // FIXME: In theory, we could have a previous declaration for variables that
153 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000154 bool Redeclaration = false;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000155 SemaRef.CheckVariableDeclaration(Var, 0, Redeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Douglas Gregor7caa6822009-07-24 20:34:43 +0000157 if (D->isOutOfLine()) {
158 D->getLexicalDeclContext()->addDecl(Var);
159 Owner->makeDeclVisibleInContext(Var);
160 } else {
161 Owner->addDecl(Var);
162 }
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000164 // Link instantiations of static data members back to the template from
165 // which they were instantiated.
166 if (Var->isStaticDataMember())
167 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
168 TSK_ImplicitInstantiation);
169
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000170 if (D->getInit()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000171 OwningExprResult Init
John McCallce3ff2b2009-08-25 22:02:44 +0000172 = SemaRef.SubstExpr(D->getInit(), TemplateArgs);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000173 if (Init.isInvalid())
174 Var->setInvalidDecl();
Douglas Gregor83ddad32009-08-26 21:14:46 +0000175 else if (ParenListExpr *PLE = dyn_cast<ParenListExpr>((Expr *)Init.get())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000176 // FIXME: We're faking all of the comma locations, which is suboptimal.
Douglas Gregor83ddad32009-08-26 21:14:46 +0000177 // Do we even need these comma locations?
178 llvm::SmallVector<SourceLocation, 4> FakeCommaLocs;
179 if (PLE->getNumExprs() > 0) {
180 FakeCommaLocs.reserve(PLE->getNumExprs() - 1);
181 for (unsigned I = 0, N = PLE->getNumExprs() - 1; I != N; ++I) {
182 Expr *E = PLE->getExpr(I)->Retain();
183 FakeCommaLocs.push_back(
184 SemaRef.PP.getLocForEndOfToken(E->getLocEnd()));
185 }
Douglas Gregore9f8eb62009-08-26 23:26:04 +0000186 PLE->getExpr(PLE->getNumExprs() - 1)->Retain();
Douglas Gregor83ddad32009-08-26 21:14:46 +0000187 }
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Douglas Gregor83ddad32009-08-26 21:14:46 +0000189 // Add the direct initializer to the declaration.
190 SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
Mike Stump1eb44332009-09-09 15:08:12 +0000191 PLE->getLParenLoc(),
Douglas Gregor83ddad32009-08-26 21:14:46 +0000192 Sema::MultiExprArg(SemaRef,
193 (void**)PLE->getExprs(),
194 PLE->getNumExprs()),
195 FakeCommaLocs.data(),
196 PLE->getRParenLoc());
Mike Stump1eb44332009-09-09 15:08:12 +0000197
Douglas Gregor83ddad32009-08-26 21:14:46 +0000198 // When Init is destroyed, it will destroy the instantiated ParenListExpr;
199 // we've explicitly retained all of its subexpressions already.
200 } else
Chris Lattnerb28317a2009-03-28 19:18:32 +0000201 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init),
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000202 D->hasCXXDirectInitializer());
Douglas Gregor65b90052009-07-27 17:43:39 +0000203 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
204 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000205
206 return Var;
207}
208
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000209Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
210 bool Invalid = false;
John McCall07fb6be2009-10-22 23:33:21 +0000211 DeclaratorInfo *DI = D->getDeclaratorInfo();
212 if (DI->getType()->isDependentType()) {
213 DI = SemaRef.SubstType(DI, TemplateArgs,
214 D->getLocation(), D->getDeclName());
215 if (!DI) {
216 DI = D->getDeclaratorInfo();
217 Invalid = true;
218 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000219 // C++ [temp.arg.type]p3:
220 // If a declaration acquires a function type through a type
221 // dependent on a template-parameter and this causes a
222 // declaration that does not use the syntactic form of a
223 // function declarator to have function type, the program is
224 // ill-formed.
225 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000226 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000227 Invalid = true;
228 }
229 }
230
231 Expr *BitWidth = D->getBitWidth();
232 if (Invalid)
233 BitWidth = 0;
234 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000235 // The bit-width expression is not potentially evaluated.
236 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000238 OwningExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000239 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000240 if (InstantiatedBitWidth.isInvalid()) {
241 Invalid = true;
242 BitWidth = 0;
243 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000244 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000245 }
246
John McCall07fb6be2009-10-22 23:33:21 +0000247 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
248 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000249 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000250 D->getLocation(),
251 D->isMutable(),
252 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000253 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000254 D->getAccess(),
255 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000256 if (!Field) {
257 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000258 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000259 }
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000261 if (Invalid)
262 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000264 if (!Field->getDeclName()) {
265 // Keep track of where this decl came from.
266 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000267 }
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000269 Field->setImplicit(D->isImplicit());
270 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000271
272 return Field;
273}
274
John McCall02cace72009-08-28 07:59:38 +0000275Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
276 FriendDecl::FriendUnion FU;
277
278 // Handle friend type expressions by simply substituting template
279 // parameters into the pattern type.
280 if (Type *Ty = D->getFriendType()) {
281 QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs,
282 D->getLocation(), DeclarationName());
283 if (T.isNull()) return 0;
284
285 assert(getLangOptions().CPlusPlus0x || T->isRecordType());
286 FU = T.getTypePtr();
287
288 // Handle everything else by appropriate substitution.
289 } else {
290 NamedDecl *ND = D->getFriendDecl();
291 assert(ND && "friend decl must be a decl or a type!");
292
Douglas Gregora735b202009-10-13 14:39:41 +0000293 // FIXME: We have a problem here, because the nested call to Visit(ND)
294 // will inject the thing that the friend references into the current
295 // owner, which is wrong.
John McCall02cace72009-08-28 07:59:38 +0000296 Decl *NewND = Visit(ND);
297 if (!NewND) return 0;
298
299 FU = cast<NamedDecl>(NewND);
John McCallfd810b12009-08-14 02:03:10 +0000300 }
Mike Stump1eb44332009-09-09 15:08:12 +0000301
John McCall02cace72009-08-28 07:59:38 +0000302 FriendDecl *FD =
303 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU,
304 D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000305 FD->setAccess(AS_public);
John McCall02cace72009-08-28 07:59:38 +0000306 Owner->addDecl(FD);
307 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000308}
309
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000310Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
311 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000312
Douglas Gregorac7610d2009-06-22 20:57:11 +0000313 // The expression in a static assertion is not potentially evaluated.
314 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000316 OwningExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000317 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000318 if (InstantiatedAssertExpr.isInvalid())
319 return 0;
320
Douglas Gregor43d9d922009-08-08 01:41:12 +0000321 OwningExprResult Message(SemaRef, D->getMessage());
322 D->getMessage()->Retain();
Mike Stump1eb44332009-09-09 15:08:12 +0000323 Decl *StaticAssert
324 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
Chris Lattnerb28317a2009-03-28 19:18:32 +0000325 move(InstantiatedAssertExpr),
326 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000327 return StaticAssert;
328}
329
330Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000331 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000332 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000333 D->getTagKeywordLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000334 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000335 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000336 Enum->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000337 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000338 Enum->startDefinition();
339
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000340 llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000341
342 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000343 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
344 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000345 EC != ECEnd; ++EC) {
346 // The specified value for the enumerator.
347 OwningExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000348 if (Expr *UninstValue = EC->getInitExpr()) {
349 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000350 EnterExpressionEvaluationContext Unevaluated(SemaRef,
Douglas Gregorac7610d2009-06-22 20:57:11 +0000351 Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000352
John McCallce3ff2b2009-08-25 22:02:44 +0000353 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000354 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000355
356 // Drop the initial value and continue.
357 bool isInvalid = false;
358 if (Value.isInvalid()) {
359 Value = SemaRef.Owned((Expr *)0);
360 isInvalid = true;
361 }
362
Mike Stump1eb44332009-09-09 15:08:12 +0000363 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000364 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
365 EC->getLocation(), EC->getIdentifier(),
366 move(Value));
367
368 if (isInvalid) {
369 if (EnumConst)
370 EnumConst->setInvalidDecl();
371 Enum->setInvalidDecl();
372 }
373
374 if (EnumConst) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000375 Enum->addDecl(EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000376 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000377 LastEnumConst = EnumConst;
378 }
379 }
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000381 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000382 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000383 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
384 Sema::DeclPtrTy::make(Enum),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000385 &Enumerators[0], Enumerators.size(),
386 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000387
388 return Enum;
389}
390
Douglas Gregor6477b692009-03-25 15:04:13 +0000391Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
392 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
393 return 0;
394}
395
Douglas Gregored9c0f92009-10-29 00:04:11 +0000396namespace {
397 class SortDeclByLocation {
398 SourceManager &SourceMgr;
399
400 public:
401 explicit SortDeclByLocation(SourceManager &SourceMgr)
402 : SourceMgr(SourceMgr) { }
403
404 bool operator()(const Decl *X, const Decl *Y) const {
405 return SourceMgr.isBeforeInTranslationUnit(X->getLocation(),
406 Y->getLocation());
407 }
408 };
409}
410
John McCalle29ba202009-08-20 01:44:21 +0000411Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
412 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000413 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000414 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000415 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000416
417 CXXRecordDecl *Pattern = D->getTemplatedDecl();
418 CXXRecordDecl *RecordInst
419 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner,
420 Pattern->getLocation(), Pattern->getIdentifier(),
Douglas Gregorf0510d42009-10-12 23:11:44 +0000421 Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL,
422 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000423
424 ClassTemplateDecl *Inst
425 = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
426 D->getIdentifier(), InstParams, RecordInst, 0);
427 RecordInst->setDescribedClassTemplate(Inst);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000428 if (D->getFriendObjectKind())
429 Inst->setObjectOfFriendDecl(true);
430 else
431 Inst->setAccess(D->getAccess());
John McCalle29ba202009-08-20 01:44:21 +0000432 Inst->setInstantiatedFromMemberTemplate(D);
Douglas Gregorf0510d42009-10-12 23:11:44 +0000433
434 // Trigger creation of the type for the instantiation.
435 SemaRef.Context.getTypeDeclType(RecordInst);
436
Douglas Gregor259571e2009-10-30 22:42:42 +0000437 // Finish handling of friends.
438 if (Inst->getFriendObjectKind()) {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000439 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000440 }
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000441
John McCalle29ba202009-08-20 01:44:21 +0000442 Owner->addDecl(Inst);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000443
444 // First, we sort the partial specializations by location, so
445 // that we instantiate them in the order they were declared.
446 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
447 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
448 P = D->getPartialSpecializations().begin(),
449 PEnd = D->getPartialSpecializations().end();
450 P != PEnd; ++P)
451 PartialSpecs.push_back(&*P);
452 std::sort(PartialSpecs.begin(), PartialSpecs.end(),
453 SortDeclByLocation(SemaRef.SourceMgr));
454
455 // Instantiate all of the partial specializations of this member class
456 // template.
457 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
458 InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
459
John McCalle29ba202009-08-20 01:44:21 +0000460 return Inst;
461}
462
Douglas Gregord60e1052009-08-27 16:57:43 +0000463Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000464TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
465 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000466 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
467
468 // Lookup the already-instantiated declaration in the instantiation
469 // of the class template and return that.
470 DeclContext::lookup_result Found
471 = Owner->lookup(ClassTemplate->getDeclName());
472 if (Found.first == Found.second)
473 return 0;
474
475 ClassTemplateDecl *InstClassTemplate
476 = dyn_cast<ClassTemplateDecl>(*Found.first);
477 if (!InstClassTemplate)
478 return 0;
479
480 Decl *DCanon = D->getCanonicalDecl();
481 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
482 P = InstClassTemplate->getPartialSpecializations().begin(),
483 PEnd = InstClassTemplate->getPartialSpecializations().end();
484 P != PEnd; ++P) {
485 if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
486 return &*P;
487 }
488
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000489 return 0;
490}
491
492Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000493TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000494 // FIXME: Dig out the out-of-line definition of this function template?
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Douglas Gregord60e1052009-08-27 16:57:43 +0000496 TemplateParameterList *TempParams = D->getTemplateParameters();
497 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000498 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000499 return NULL;
Douglas Gregored9c0f92009-10-29 00:04:11 +0000500
Douglas Gregora735b202009-10-13 14:39:41 +0000501 FunctionDecl *Instantiated = 0;
502 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
503 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
504 InstParams));
505 else
506 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
507 D->getTemplatedDecl(),
508 InstParams));
509
510 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000511 return 0;
512
Mike Stump1eb44332009-09-09 15:08:12 +0000513 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000514 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000515 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000516 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000517 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000518 assert(InstTemplate &&
519 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
520 if (!InstTemplate->getInstantiatedFromMemberTemplate())
521 InstTemplate->setInstantiatedFromMemberTemplate(D);
522
523 // Add non-friends into the owner.
524 if (!InstTemplate->getFriendObjectKind())
525 Owner->addDecl(InstTemplate);
Douglas Gregord60e1052009-08-27 16:57:43 +0000526 return InstTemplate;
527}
528
Douglas Gregord475b8d2009-03-25 21:17:03 +0000529Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
530 CXXRecordDecl *PrevDecl = 0;
531 if (D->isInjectedClassName())
532 PrevDecl = cast<CXXRecordDecl>(Owner);
533
534 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000535 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000536 D->getLocation(), D->getIdentifier(),
537 D->getTagKeywordLoc(), PrevDecl);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000538 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000539 // FIXME: Check against AS_none is an ugly hack to work around the issue that
540 // the tag decls introduced by friend class declarations don't have an access
541 // specifier. Remove once this area of the code gets sorted out.
542 if (D->getAccess() != AS_none)
543 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000544 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000545 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000546
John McCall02cace72009-08-28 07:59:38 +0000547 // If the original function was part of a friend declaration,
548 // inherit its namespace state.
549 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
550 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
551
Anders Carlssond8b285f2009-09-01 04:26:58 +0000552 Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
553
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000554 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000555 return Record;
556}
557
John McCall02cace72009-08-28 07:59:38 +0000558/// Normal class members are of more specific types and therefore
559/// don't make it here. This function serves two purposes:
560/// 1) instantiating function templates
561/// 2) substituting friend declarations
562/// FIXME: preserve function definitions in case #2
Douglas Gregora735b202009-10-13 14:39:41 +0000563 Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
564 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000565 // Check whether there is already a function template specialization for
566 // this declaration.
567 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
568 void *InsertPos = 0;
Douglas Gregora735b202009-10-13 14:39:41 +0000569 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000570 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000571 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000572 TemplateArgs.getInnermost().getFlatArgumentList(),
573 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor828e2262009-07-29 16:09:57 +0000574 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000575
576 FunctionTemplateSpecializationInfo *Info
577 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor127102b2009-06-29 20:59:39 +0000578 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000579
Douglas Gregor127102b2009-06-29 20:59:39 +0000580 // If we already have a function template specialization, return it.
581 if (Info)
582 return Info->Function;
583 }
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Douglas Gregore53060f2009-06-25 22:08:12 +0000585 Sema::LocalInstantiationScope Scope(SemaRef);
Mike Stump1eb44332009-09-09 15:08:12 +0000586
Douglas Gregore53060f2009-06-25 22:08:12 +0000587 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000588 QualType T = SubstFunctionType(D, Params);
Douglas Gregore53060f2009-06-25 22:08:12 +0000589 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000590 return 0;
John McCallfd810b12009-08-14 02:03:10 +0000591
Douglas Gregore53060f2009-06-25 22:08:12 +0000592 // Build the instantiated method declaration.
Douglas Gregore95b4092009-09-16 18:34:49 +0000593 DeclContext *DC = SemaRef.FindInstantiatedContext(D->getDeclContext(),
594 TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +0000595 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +0000596 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000597 D->getDeclName(), T, D->getDeclaratorInfo(),
598 D->getStorageClass(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000599 D->isInlineSpecified(), D->hasWrittenPrototype());
John McCall02cace72009-08-28 07:59:38 +0000600 Function->setLexicalDeclContext(Owner);
Mike Stump1eb44332009-09-09 15:08:12 +0000601
Douglas Gregore53060f2009-06-25 22:08:12 +0000602 // Attach the parameters
603 for (unsigned P = 0; P < Params.size(); ++P)
604 Params[P]->setOwningFunction(Function);
605 Function->setParams(SemaRef.Context, Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +0000606
Douglas Gregora735b202009-10-13 14:39:41 +0000607 if (TemplateParams) {
608 // Our resulting instantiation is actually a function template, since we
609 // are substituting only the outer template parameters. For example, given
610 //
611 // template<typename T>
612 // struct X {
613 // template<typename U> friend void f(T, U);
614 // };
615 //
616 // X<int> x;
617 //
618 // We are instantiating the friend function template "f" within X<int>,
619 // which means substituting int for T, but leaving "f" as a friend function
620 // template.
621 // Build the function template itself.
622 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Owner,
623 Function->getLocation(),
624 Function->getDeclName(),
625 TemplateParams, Function);
626 Function->setDescribedFunctionTemplate(FunctionTemplate);
627 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
John McCall02cace72009-08-28 07:59:38 +0000628 }
Douglas Gregora735b202009-10-13 14:39:41 +0000629
Douglas Gregore53060f2009-06-25 22:08:12 +0000630 if (InitFunctionInstantiation(Function, D))
631 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Douglas Gregore53060f2009-06-25 22:08:12 +0000633 bool Redeclaration = false;
634 bool OverloadableAttrRequired = false;
Douglas Gregora735b202009-10-13 14:39:41 +0000635
Douglas Gregore53060f2009-06-25 22:08:12 +0000636 NamedDecl *PrevDecl = 0;
Douglas Gregora735b202009-10-13 14:39:41 +0000637 if (TemplateParams || !FunctionTemplate) {
638 // Look only into the namespace where the friend would be declared to
639 // find a previous declaration. This is the innermost enclosing namespace,
640 // as described in ActOnFriendFunctionDecl.
641 Sema::LookupResult R;
642 SemaRef.LookupQualifiedName(R, DC, Function->getDeclName(),
643 Sema::LookupOrdinaryName, true);
644
645 PrevDecl = R.getAsSingleDecl(SemaRef.Context);
646
647 // In C++, the previous declaration we find might be a tag type
648 // (class or enum). In this case, the new declaration will hide the
649 // tag type. Note that this does does not apply if we're declaring a
650 // typedef (C++ [dcl.typedef]p4).
651 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
652 PrevDecl = 0;
653 }
654
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000655 SemaRef.CheckFunctionDeclaration(Function, PrevDecl, false, Redeclaration,
Douglas Gregore53060f2009-06-25 22:08:12 +0000656 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000657
Douglas Gregora735b202009-10-13 14:39:41 +0000658 // If the original function was part of a friend declaration,
659 // inherit its namespace state and add it to the owner.
660 NamedDecl *FromFriendD
661 = TemplateParams? cast<NamedDecl>(D->getDescribedFunctionTemplate()) : D;
662 if (FromFriendD->getFriendObjectKind()) {
663 NamedDecl *ToFriendD = 0;
664 if (TemplateParams) {
665 ToFriendD = cast<NamedDecl>(FunctionTemplate);
666 PrevDecl = FunctionTemplate->getPreviousDeclaration();
667 } else {
668 ToFriendD = Function;
669 PrevDecl = Function->getPreviousDeclaration();
670 }
671 ToFriendD->setObjectOfFriendDecl(PrevDecl != NULL);
672 if (!Owner->isDependentContext() && !PrevDecl)
673 DC->makeDeclVisibleInContext(ToFriendD, /* Recoverable = */ false);
674
675 if (!TemplateParams)
676 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
677 }
678
679 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000680 // Record this function template specialization.
681 Function->setFunctionTemplateSpecialization(SemaRef.Context,
682 FunctionTemplate,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000683 &TemplateArgs.getInnermost(),
Douglas Gregor127102b2009-06-29 20:59:39 +0000684 InsertPos);
John McCallfd810b12009-08-14 02:03:10 +0000685 }
686
Douglas Gregore53060f2009-06-25 22:08:12 +0000687 return Function;
688}
689
Douglas Gregord60e1052009-08-27 16:57:43 +0000690Decl *
691TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
692 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +0000693 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
694 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +0000695 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +0000696 // We are creating a function template specialization from a function
697 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +0000698 // specialization for this particular set of template arguments.
Douglas Gregor6b906862009-08-21 00:16:32 +0000699 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000700 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000701 TemplateArgs.getInnermost().getFlatArgumentList(),
702 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000703 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000704
705 FunctionTemplateSpecializationInfo *Info
706 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor6b906862009-08-21 00:16:32 +0000707 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000708
Douglas Gregor6b906862009-08-21 00:16:32 +0000709 // If we already have a function template specialization, return it.
710 if (Info)
711 return Info->Function;
712 }
713
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000714 Sema::LocalInstantiationScope Scope(SemaRef);
715
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000716 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000717 QualType T = SubstFunctionType(D, Params);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000718 if (T.isNull())
719 return 0;
720
721 // Build the instantiated method declaration.
722 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
Douglas Gregordec06662009-08-21 18:42:58 +0000723 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000724
Douglas Gregordec06662009-08-21 18:42:58 +0000725 DeclarationName Name = D->getDeclName();
Douglas Gregor17e32f32009-08-21 22:43:28 +0000726 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregordec06662009-08-21 18:42:58 +0000727 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
728 Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
729 SemaRef.Context.getCanonicalType(ClassTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000730 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
731 Constructor->getLocation(),
732 Name, T,
Douglas Gregor17e32f32009-08-21 22:43:28 +0000733 Constructor->getDeclaratorInfo(),
Mike Stump1eb44332009-09-09 15:08:12 +0000734 Constructor->isExplicit(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000735 Constructor->isInlineSpecified(), false);
Douglas Gregor17e32f32009-08-21 22:43:28 +0000736 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
737 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
738 Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
739 SemaRef.Context.getCanonicalType(ClassTy));
740 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
741 Destructor->getLocation(), Name,
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000742 T, Destructor->isInlineSpecified(), false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000743 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000744 CanQualType ConvTy
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000745 = SemaRef.Context.getCanonicalType(
John McCall183700f2009-09-21 23:43:11 +0000746 T->getAs<FunctionType>()->getResultType());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000747 Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
748 ConvTy);
749 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
750 Conversion->getLocation(), Name,
751 T, Conversion->getDeclaratorInfo(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000752 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000753 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +0000754 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000755 Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
Douglas Gregordec06662009-08-21 18:42:58 +0000756 D->getDeclName(), T, D->getDeclaratorInfo(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000757 D->isStatic(), D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +0000758 }
Douglas Gregor6b906862009-08-21 00:16:32 +0000759
Douglas Gregord60e1052009-08-27 16:57:43 +0000760 if (TemplateParams) {
761 // Our resulting instantiation is actually a function template, since we
762 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +0000763 //
Douglas Gregord60e1052009-08-27 16:57:43 +0000764 // template<typename T>
765 // struct X {
766 // template<typename U> void f(T, U);
767 // };
768 //
769 // X<int> x;
770 //
771 // We are instantiating the member template "f" within X<int>, which means
772 // substituting int for T, but leaving "f" as a member function template.
773 // Build the function template itself.
774 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
775 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +0000776 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +0000777 TemplateParams, Method);
778 if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +0000779 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +0000780 Method->setDescribedFunctionTemplate(FunctionTemplate);
781 } else if (!FunctionTemplate)
Douglas Gregor2db32322009-10-07 23:56:10 +0000782 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000783
Mike Stump1eb44332009-09-09 15:08:12 +0000784 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000785 // out-of-line, the instantiation will have the same lexical
786 // context (which will be a namespace scope) as the template.
787 if (D->isOutOfLine())
788 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000789
Douglas Gregor5545e162009-03-24 00:38:23 +0000790 // Attach the parameters
791 for (unsigned P = 0; P < Params.size(); ++P)
792 Params[P]->setOwningFunction(Method);
Jay Foadbeaaccd2009-05-21 09:52:38 +0000793 Method->setParams(SemaRef.Context, Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +0000794
795 if (InitMethodInstantiation(Method, D))
796 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000797
Douglas Gregordec06662009-08-21 18:42:58 +0000798 NamedDecl *PrevDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000799
Douglas Gregord60e1052009-08-27 16:57:43 +0000800 if (!FunctionTemplate || TemplateParams) {
John McCallf36e02d2009-10-09 21:13:30 +0000801 Sema::LookupResult R;
802 SemaRef.LookupQualifiedName(R, Owner, Name, Sema::LookupOrdinaryName, true);
803 PrevDecl = R.getAsSingleDecl(SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000804
Douglas Gregordec06662009-08-21 18:42:58 +0000805 // In C++, the previous declaration we find might be a tag type
806 // (class or enum). In this case, the new declaration will hide the
807 // tag type. Note that this does does not apply if we're declaring a
808 // typedef (C++ [dcl.typedef]p4).
809 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
810 PrevDecl = 0;
811 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000812
Douglas Gregord60e1052009-08-27 16:57:43 +0000813 if (FunctionTemplate && !TemplateParams)
Douglas Gregor6b906862009-08-21 00:16:32 +0000814 // Record this function template specialization.
815 Method->setFunctionTemplateSpecialization(SemaRef.Context,
816 FunctionTemplate,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000817 &TemplateArgs.getInnermost(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000818 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000819
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000820 bool Redeclaration = false;
821 bool OverloadableAttrRequired = false;
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000822 SemaRef.CheckFunctionDeclaration(Method, PrevDecl, false, Redeclaration,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000823 /*FIXME:*/OverloadableAttrRequired);
824
Douglas Gregora735b202009-10-13 14:39:41 +0000825 if (!FunctionTemplate && (!Method->isInvalidDecl() || !PrevDecl) &&
826 !Method->getFriendObjectKind())
Douglas Gregordec06662009-08-21 18:42:58 +0000827 Owner->addDecl(Method);
Mike Stump1eb44332009-09-09 15:08:12 +0000828
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000829 return Method;
830}
831
Douglas Gregor615c5d42009-03-24 16:43:20 +0000832Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +0000833 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000834}
835
Douglas Gregor03b2b072009-03-24 00:15:49 +0000836Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +0000837 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +0000838}
839
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000840Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000841 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000842}
843
Douglas Gregor6477b692009-03-25 15:04:13 +0000844ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
John McCall58e46772009-10-23 21:48:59 +0000845 QualType T;
846 DeclaratorInfo *DI = D->getDeclaratorInfo();
847 if (DI) {
848 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
849 D->getDeclName());
850 if (DI) T = DI->getType();
851 } else {
852 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
853 D->getDeclName());
854 DI = 0;
855 }
856
857 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000858 return 0;
859
John McCall58e46772009-10-23 21:48:59 +0000860 T = SemaRef.adjustParameterType(T);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000861
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000862 // Allocate the parameter
John McCall58e46772009-10-23 21:48:59 +0000863 ParmVarDecl *Param
864 = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
865 D->getIdentifier(), T, DI, D->getStorageClass(), 0);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000866
Anders Carlsson9351c172009-08-25 03:18:48 +0000867 // Mark the default argument as being uninstantiated.
Douglas Gregorf43d0b32009-09-25 06:56:31 +0000868 if (D->hasUninstantiatedDefaultArg())
869 Param->setUninstantiatedDefaultArg(D->getUninstantiatedDefaultArg());
Douglas Gregor0ed09302009-09-25 07:03:22 +0000870 else if (Expr *Arg = D->getDefaultArg())
871 Param->setUninstantiatedDefaultArg(Arg);
872
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000873 // Note: we don't try to instantiate function parameters until after
874 // we've instantiated the function's type. Therefore, we don't have
875 // to check for 'void' parameter types here.
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000876 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000877 return Param;
878}
879
John McCalle29ba202009-08-20 01:44:21 +0000880Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
881 TemplateTypeParmDecl *D) {
882 // TODO: don't always clone when decls are refcounted.
883 const Type* T = D->getTypeForDecl();
884 assert(T->isTemplateTypeParmType());
885 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000886
John McCalle29ba202009-08-20 01:44:21 +0000887 TemplateTypeParmDecl *Inst =
888 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
889 TTPT->getDepth(), TTPT->getIndex(),
890 TTPT->getName(),
891 D->wasDeclaredWithTypename(),
892 D->isParameterPack());
893
Douglas Gregor33642df2009-10-23 23:25:44 +0000894 // FIXME: Do we actually want to perform substitution here? I don't think
895 // we do.
John McCalle29ba202009-08-20 01:44:21 +0000896 if (D->hasDefaultArgument()) {
John McCall833ca992009-10-29 08:12:44 +0000897 DeclaratorInfo *DefaultPattern = D->getDefaultArgumentInfo();
898 DeclaratorInfo *DefaultInst
John McCallce3ff2b2009-08-25 22:02:44 +0000899 = SemaRef.SubstType(DefaultPattern, TemplateArgs,
900 D->getDefaultArgumentLoc(),
901 D->getDeclName());
Mike Stump1eb44332009-09-09 15:08:12 +0000902
John McCalle29ba202009-08-20 01:44:21 +0000903 Inst->setDefaultArgument(DefaultInst,
John McCalle29ba202009-08-20 01:44:21 +0000904 D->defaultArgumentWasInherited() /* preserve? */);
905 }
906
907 return Inst;
908}
909
Douglas Gregor33642df2009-10-23 23:25:44 +0000910Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
911 NonTypeTemplateParmDecl *D) {
912 // Substitute into the type of the non-type template parameter.
913 QualType T;
914 DeclaratorInfo *DI = D->getDeclaratorInfo();
915 if (DI) {
916 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
917 D->getDeclName());
918 if (DI) T = DI->getType();
919 } else {
920 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
921 D->getDeclName());
922 DI = 0;
923 }
924 if (T.isNull())
925 return 0;
926
927 // Check that this type is acceptable for a non-type template parameter.
928 bool Invalid = false;
929 T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
930 if (T.isNull()) {
931 T = SemaRef.Context.IntTy;
932 Invalid = true;
933 }
934
935 NonTypeTemplateParmDecl *Param
936 = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
937 D->getDepth() - 1, D->getPosition(),
938 D->getIdentifier(), T, DI);
939 if (Invalid)
940 Param->setInvalidDecl();
941
942 Param->setDefaultArgument(D->getDefaultArgument());
943 return Param;
944}
945
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000946Decl *
947TemplateDeclInstantiator::VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000948 NestedNameSpecifier *NNS =
949 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
950 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000951 TemplateArgs);
952 if (!NNS)
953 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000954
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000955 CXXScopeSpec SS;
956 SS.setRange(D->getTargetNestedNameRange());
957 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +0000958
959 NamedDecl *UD =
960 SemaRef.BuildUsingDeclaration(D->getLocation(), SS,
961 D->getTargetNameLocation(),
Anders Carlsson0d8df782009-08-29 19:37:28 +0000962 D->getTargetName(), 0, D->isTypeName());
963 if (UD)
Mike Stump1eb44332009-09-09 15:08:12 +0000964 SemaRef.Context.setInstantiatedFromUnresolvedUsingDecl(cast<UsingDecl>(UD),
Anders Carlsson0d8df782009-08-29 19:37:28 +0000965 D);
966 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000967}
968
John McCallce3ff2b2009-08-25 22:02:44 +0000969Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000970 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +0000971 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000972 return Instantiator.Visit(D);
973}
974
John McCalle29ba202009-08-20 01:44:21 +0000975/// \brief Instantiates a nested template parameter list in the current
976/// instantiation context.
977///
978/// \param L The parameter list to instantiate
979///
980/// \returns NULL if there was an error
981TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +0000982TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +0000983 // Get errors for all the parameters before bailing out.
984 bool Invalid = false;
985
986 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000987 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +0000988 ParamVector Params;
989 Params.reserve(N);
990 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
991 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000992 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +0000993 Params.push_back(D);
994 Invalid = Invalid || !D;
995 }
996
997 // Clean up if we had an error.
998 if (Invalid) {
999 for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
1000 PI != PE; ++PI)
1001 if (*PI)
1002 (*PI)->Destroy(SemaRef.Context);
1003 return NULL;
1004 }
1005
1006 TemplateParameterList *InstL
1007 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1008 L->getLAngleLoc(), &Params.front(), N,
1009 L->getRAngleLoc());
1010 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001011}
John McCalle29ba202009-08-20 01:44:21 +00001012
Douglas Gregored9c0f92009-10-29 00:04:11 +00001013/// \brief Instantiate the declaration of a class template partial
1014/// specialization.
1015///
1016/// \param ClassTemplate the (instantiated) class template that is partially
1017// specialized by the instantiation of \p PartialSpec.
1018///
1019/// \param PartialSpec the (uninstantiated) class template partial
1020/// specialization that we are instantiating.
1021///
1022/// \returns true if there was an error, false otherwise.
1023bool
1024TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1025 ClassTemplateDecl *ClassTemplate,
1026 ClassTemplatePartialSpecializationDecl *PartialSpec) {
1027 // Substitute into the template parameters of the class template partial
1028 // specialization.
1029 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1030 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1031 if (!InstParams)
1032 return true;
1033
1034 // Substitute into the template arguments of the class template partial
1035 // specialization.
John McCall833ca992009-10-29 08:12:44 +00001036 const TemplateArgumentLoc *PartialSpecTemplateArgs
1037 = PartialSpec->getTemplateArgsAsWritten();
1038 unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1039
1040 llvm::SmallVector<TemplateArgumentLoc, 4> InstTemplateArgs(N);
1041 for (unsigned I = 0; I != N; ++I) {
1042 if (SemaRef.Subst(PartialSpecTemplateArgs[I], InstTemplateArgs[I],
1043 TemplateArgs))
Douglas Gregored9c0f92009-10-29 00:04:11 +00001044 return true;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001045 }
1046
1047
1048 // Check that the template argument list is well-formed for this
1049 // class template.
1050 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1051 InstTemplateArgs.size());
1052 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1053 PartialSpec->getLocation(),
1054 /*FIXME:*/PartialSpec->getLocation(),
1055 InstTemplateArgs.data(),
1056 InstTemplateArgs.size(),
1057 /*FIXME:*/PartialSpec->getLocation(),
1058 false,
1059 Converted))
1060 return true;
1061
1062 // Figure out where to insert this class template partial specialization
1063 // in the member template's set of class template partial specializations.
1064 llvm::FoldingSetNodeID ID;
1065 ClassTemplatePartialSpecializationDecl::Profile(ID,
1066 Converted.getFlatArguments(),
1067 Converted.flatSize(),
1068 SemaRef.Context);
1069 void *InsertPos = 0;
1070 ClassTemplateSpecializationDecl *PrevDecl
1071 = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
1072 InsertPos);
1073
1074 // Build the canonical type that describes the converted template
1075 // arguments of the class template partial specialization.
1076 QualType CanonType
1077 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1078 Converted.getFlatArguments(),
1079 Converted.flatSize());
1080
1081 // Build the fully-sugared type for this class template
1082 // specialization as the user wrote in the specialization
1083 // itself. This means that we'll pretty-print the type retrieved
1084 // from the specialization's declaration the way that the user
1085 // actually wrote the specialization, rather than formatting the
1086 // name based on the "canonical" representation used to store the
1087 // template arguments in the specialization.
1088 QualType WrittenTy
1089 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1090 InstTemplateArgs.data(),
1091 InstTemplateArgs.size(),
1092 CanonType);
1093
1094 if (PrevDecl) {
1095 // We've already seen a partial specialization with the same template
1096 // parameters and template arguments. This can happen, for example, when
1097 // substituting the outer template arguments ends up causing two
1098 // class template partial specializations of a member class template
1099 // to have identical forms, e.g.,
1100 //
1101 // template<typename T, typename U>
1102 // struct Outer {
1103 // template<typename X, typename Y> struct Inner;
1104 // template<typename Y> struct Inner<T, Y>;
1105 // template<typename Y> struct Inner<U, Y>;
1106 // };
1107 //
1108 // Outer<int, int> outer; // error: the partial specializations of Inner
1109 // // have the same signature.
1110 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1111 << WrittenTy;
1112 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1113 << SemaRef.Context.getTypeDeclType(PrevDecl);
1114 return true;
1115 }
1116
1117
1118 // Create the class template partial specialization declaration.
1119 ClassTemplatePartialSpecializationDecl *InstPartialSpec
1120 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context, Owner,
1121 PartialSpec->getLocation(),
1122 InstParams,
1123 ClassTemplate,
1124 Converted,
John McCall833ca992009-10-29 08:12:44 +00001125 InstTemplateArgs.data(),
1126 InstTemplateArgs.size(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00001127 0);
1128 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
1129 InstPartialSpec->setTypeAsWritten(WrittenTy);
1130
1131 // Add this partial specialization to the set of class template partial
1132 // specializations.
1133 ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec,
1134 InsertPos);
1135 return false;
1136}
1137
John McCallce3ff2b2009-08-25 22:02:44 +00001138/// \brief Does substitution on the type of the given function, including
1139/// all of the function parameters.
Douglas Gregor5545e162009-03-24 00:38:23 +00001140///
John McCallce3ff2b2009-08-25 22:02:44 +00001141/// \param D The function whose type will be the basis of the substitution
Douglas Gregor5545e162009-03-24 00:38:23 +00001142///
1143/// \param Params the instantiated parameter declarations
1144
John McCallce3ff2b2009-08-25 22:02:44 +00001145/// \returns the instantiated function's type if successful, a NULL
Douglas Gregor5545e162009-03-24 00:38:23 +00001146/// type if there was an error.
Mike Stump1eb44332009-09-09 15:08:12 +00001147QualType
John McCallce3ff2b2009-08-25 22:02:44 +00001148TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001149 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
1150 bool InvalidDecl = false;
1151
John McCallce3ff2b2009-08-25 22:02:44 +00001152 // Substitute all of the function's formal parameter types.
Douglas Gregor7e063902009-05-11 23:53:27 +00001153 TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001154 llvm::SmallVector<QualType, 4> ParamTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001155 for (FunctionDecl::param_iterator P = D->param_begin(),
Douglas Gregor5545e162009-03-24 00:38:23 +00001156 PEnd = D->param_end();
1157 P != PEnd; ++P) {
Douglas Gregor6477b692009-03-25 15:04:13 +00001158 if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
Douglas Gregor5545e162009-03-24 00:38:23 +00001159 if (PInst->getType()->isVoidType()) {
1160 SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
1161 PInst->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001162 } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001163 PInst->getType(),
1164 diag::err_abstract_type_in_decl,
1165 Sema::AbstractParamType))
Douglas Gregor5545e162009-03-24 00:38:23 +00001166 PInst->setInvalidDecl();
1167
1168 Params.push_back(PInst);
1169 ParamTys.push_back(PInst->getType());
1170
1171 if (PInst->isInvalidDecl())
1172 InvalidDecl = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001173 } else
Douglas Gregor5545e162009-03-24 00:38:23 +00001174 InvalidDecl = true;
1175 }
1176
1177 // FIXME: Deallocate dead declarations.
1178 if (InvalidDecl)
1179 return QualType();
1180
John McCall183700f2009-09-21 23:43:11 +00001181 const FunctionProtoType *Proto = D->getType()->getAs<FunctionProtoType>();
Douglas Gregor5545e162009-03-24 00:38:23 +00001182 assert(Proto && "Missing prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +00001183 QualType ResultType
John McCallce3ff2b2009-08-25 22:02:44 +00001184 = SemaRef.SubstType(Proto->getResultType(), TemplateArgs,
1185 D->getLocation(), D->getDeclName());
Douglas Gregor5545e162009-03-24 00:38:23 +00001186 if (ResultType.isNull())
1187 return QualType();
1188
Jay Foadbeaaccd2009-05-21 09:52:38 +00001189 return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
Douglas Gregor5545e162009-03-24 00:38:23 +00001190 Proto->isVariadic(), Proto->getTypeQuals(),
1191 D->getLocation(), D->getDeclName());
1192}
1193
Mike Stump1eb44332009-09-09 15:08:12 +00001194/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00001195/// declaration (New) from the corresponding fields of its template (Tmpl).
1196///
1197/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001198bool
1199TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00001200 FunctionDecl *Tmpl) {
1201 if (Tmpl->isDeleted())
1202 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00001203
Douglas Gregorcca9e962009-07-01 22:01:06 +00001204 // If we are performing substituting explicitly-specified template arguments
1205 // or deduced template arguments into a function template and we reach this
1206 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00001207 // to keeping the new function template specialization. We therefore
1208 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00001209 // into a template instantiation for this specific function template
1210 // specialization, which is not a SFINAE context, so that we diagnose any
1211 // further errors in the declaration itself.
1212 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1213 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1214 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1215 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00001216 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00001217 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001218 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00001219 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00001220 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001221 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1222 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
1223 }
1224 }
Mike Stump1eb44332009-09-09 15:08:12 +00001225
Douglas Gregore53060f2009-06-25 22:08:12 +00001226 return false;
1227}
1228
Douglas Gregor5545e162009-03-24 00:38:23 +00001229/// \brief Initializes common fields of an instantiated method
1230/// declaration (New) from the corresponding fields of its template
1231/// (Tmpl).
1232///
1233/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001234bool
1235TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00001236 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001237 if (InitFunctionInstantiation(New, Tmpl))
1238 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001239
Douglas Gregor5545e162009-03-24 00:38:23 +00001240 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
1241 New->setAccess(Tmpl->getAccess());
Anders Carlsson77b7f1d2009-05-14 22:15:41 +00001242 if (Tmpl->isVirtualAsWritten()) {
1243 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +00001244 Record->setAggregate(false);
1245 Record->setPOD(false);
Eli Friedman1d954f62009-08-15 21:55:26 +00001246 Record->setEmpty(false);
Douglas Gregor5545e162009-03-24 00:38:23 +00001247 Record->setPolymorphic(true);
1248 }
Douglas Gregor5545e162009-03-24 00:38:23 +00001249 if (Tmpl->isPure()) {
1250 New->setPure();
1251 Record->setAbstract(true);
1252 }
1253
1254 // FIXME: attributes
1255 // FIXME: New needs a pointer to Tmpl
1256 return false;
1257}
Douglas Gregora58861f2009-05-13 20:28:22 +00001258
1259/// \brief Instantiate the definition of the given function from its
1260/// template.
1261///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001262/// \param PointOfInstantiation the point at which the instantiation was
1263/// required. Note that this is not precisely a "point of instantiation"
1264/// for the function, but it's close.
1265///
Douglas Gregora58861f2009-05-13 20:28:22 +00001266/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001267/// function template specialization or member function of a class template
1268/// specialization.
1269///
1270/// \param Recursive if true, recursively instantiates any functions that
1271/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001272///
1273/// \param DefinitionRequired if true, then we are performing an explicit
1274/// instantiation where the body of the function is required. Complain if
1275/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001276void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001277 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001278 bool Recursive,
1279 bool DefinitionRequired) {
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001280 if (Function->isInvalidDecl())
1281 return;
1282
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001283 assert(!Function->getBody() && "Already instantiated!");
Mike Stump1eb44332009-09-09 15:08:12 +00001284
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001285 // Never instantiate an explicit specialization.
1286 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1287 return;
1288
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001289 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00001290 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001291 Stmt *Pattern = 0;
1292 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001293 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001294
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001295 if (!Pattern) {
1296 if (DefinitionRequired) {
1297 if (Function->getPrimaryTemplate())
1298 Diag(PointOfInstantiation,
1299 diag::err_explicit_instantiation_undefined_func_template)
1300 << Function->getPrimaryTemplate();
1301 else
1302 Diag(PointOfInstantiation,
1303 diag::err_explicit_instantiation_undefined_member)
1304 << 1 << Function->getDeclName() << Function->getDeclContext();
1305
1306 if (PatternDecl)
1307 Diag(PatternDecl->getLocation(),
1308 diag::note_explicit_instantiation_here);
1309 }
1310
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001311 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001312 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001313
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001314 // C++0x [temp.explicit]p9:
1315 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00001316 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001317 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00001318 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001319 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001320 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001321 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001322
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001323 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
1324 if (Inst)
1325 return;
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001326
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001327 // If we're performing recursive template instantiation, create our own
1328 // queue of pending implicit instantiations that we will instantiate later,
1329 // while we're still within our own instantiation context.
1330 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1331 if (Recursive)
1332 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001333
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001334 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
1335
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001336 // Introduce a new scope where local variable instantiations will be
1337 // recorded.
1338 LocalInstantiationScope Scope(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001339
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001340 // Introduce the instantiated function parameters into the local
1341 // instantiation scope.
1342 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
1343 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
1344 Function->getParamDecl(I));
1345
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001346 // Enter the scope of this instantiation. We don't use
1347 // PushDeclContext because we don't have a scope.
1348 DeclContext *PreviousContext = CurContext;
1349 CurContext = Function;
1350
Mike Stump1eb44332009-09-09 15:08:12 +00001351 MultiLevelTemplateArgumentList TemplateArgs =
Anders Carlsson09025312009-08-29 05:16:22 +00001352 getTemplateInstantiationArgs(Function);
1353
1354 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001355 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00001356 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
1357 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
1358 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001359 }
1360
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001361 // Instantiate the function body.
Anders Carlsson09025312009-08-29 05:16:22 +00001362 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001363
Douglas Gregor52604ab2009-09-11 21:19:12 +00001364 if (Body.isInvalid())
1365 Function->setInvalidDecl();
1366
Mike Stump1eb44332009-09-09 15:08:12 +00001367 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001368 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001369
1370 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00001371
1372 DeclGroupRef DG(Function);
1373 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001374
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001375 if (Recursive) {
1376 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001377 // instantiation of this template.
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001378 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001379
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001380 // Restore the set of pending implicit instantiations.
1381 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1382 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001383}
1384
1385/// \brief Instantiate the definition of the given variable from its
1386/// template.
1387///
Douglas Gregor7caa6822009-07-24 20:34:43 +00001388/// \param PointOfInstantiation the point at which the instantiation was
1389/// required. Note that this is not precisely a "point of instantiation"
1390/// for the function, but it's close.
1391///
1392/// \param Var the already-instantiated declaration of a static member
1393/// variable of a class template specialization.
1394///
1395/// \param Recursive if true, recursively instantiates any functions that
1396/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001397///
1398/// \param DefinitionRequired if true, then we are performing an explicit
1399/// instantiation where an out-of-line definition of the member variable
1400/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001401void Sema::InstantiateStaticDataMemberDefinition(
1402 SourceLocation PointOfInstantiation,
1403 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001404 bool Recursive,
1405 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001406 if (Var->isInvalidDecl())
1407 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001408
Douglas Gregor7caa6822009-07-24 20:34:43 +00001409 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001410 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00001411 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00001412 assert(Def->isStaticDataMember() && "Not a static data member?");
1413 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00001414
Douglas Gregor0d035142009-10-27 18:42:08 +00001415 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001416 // We did not find an out-of-line definition of this static data member,
1417 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00001418 // instantiate this definition (or provide a specialization for it) in
1419 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001420 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001421 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001422 Diag(PointOfInstantiation,
1423 diag::err_explicit_instantiation_undefined_member)
1424 << 2 << Var->getDeclName() << Var->getDeclContext();
1425 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
1426 }
1427
Douglas Gregor7caa6822009-07-24 20:34:43 +00001428 return;
1429 }
1430
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001431 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001432 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001433 return;
1434
1435 // C++0x [temp.explicit]p9:
1436 // Except for inline functions, other explicit instantiation declarations
1437 // have the effect of suppressing the implicit instantiation of the entity
1438 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001439 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001440 == TSK_ExplicitInstantiationDeclaration)
1441 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001442
Douglas Gregor7caa6822009-07-24 20:34:43 +00001443 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
1444 if (Inst)
1445 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001446
Douglas Gregor7caa6822009-07-24 20:34:43 +00001447 // If we're performing recursive template instantiation, create our own
1448 // queue of pending implicit instantiations that we will instantiate later,
1449 // while we're still within our own instantiation context.
1450 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1451 if (Recursive)
1452 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001453
Douglas Gregor7caa6822009-07-24 20:34:43 +00001454 // Enter the scope of this instantiation. We don't use
1455 // PushDeclContext because we don't have a scope.
1456 DeclContext *PreviousContext = CurContext;
1457 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001458
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001459 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00001460 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00001461 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00001462 CurContext = PreviousContext;
1463
1464 if (Var) {
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001465 Var->setPreviousDeclaration(OldVar);
Douglas Gregor583f33b2009-10-15 18:07:02 +00001466 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
1467 assert(MSInfo && "Missing member specialization information?");
1468 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
1469 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00001470 DeclGroupRef DG(Var);
1471 Consumer.HandleTopLevelDecl(DG);
1472 }
Mike Stump1eb44332009-09-09 15:08:12 +00001473
Douglas Gregor7caa6822009-07-24 20:34:43 +00001474 if (Recursive) {
1475 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001476 // instantiation of this template.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001477 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001478
Douglas Gregor7caa6822009-07-24 20:34:43 +00001479 // Restore the set of pending implicit instantiations.
1480 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001481 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001482}
Douglas Gregor815215d2009-05-27 05:35:12 +00001483
Anders Carlsson09025312009-08-29 05:16:22 +00001484void
1485Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
1486 const CXXConstructorDecl *Tmpl,
1487 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001488
Anders Carlsson09025312009-08-29 05:16:22 +00001489 llvm::SmallVector<MemInitTy*, 4> NewInits;
1490
1491 // Instantiate all the initializers.
1492 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00001493 InitsEnd = Tmpl->init_end();
1494 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00001495 CXXBaseOrMemberInitializer *Init = *Inits;
1496
1497 ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001498
Anders Carlsson09025312009-08-29 05:16:22 +00001499 // Instantiate all the arguments.
1500 for (ExprIterator Args = Init->arg_begin(), ArgsEnd = Init->arg_end();
1501 Args != ArgsEnd; ++Args) {
1502 OwningExprResult NewArg = SubstExpr(*Args, TemplateArgs);
1503
1504 if (NewArg.isInvalid())
1505 New->setInvalidDecl();
1506 else
1507 NewArgs.push_back(NewArg.takeAs<Expr>());
1508 }
1509
1510 MemInitResult NewInit;
1511
1512 if (Init->isBaseInitializer()) {
Eli Friedmanc5573a82009-08-29 22:22:07 +00001513 QualType BaseType(Init->getBaseClass(), 0);
1514 BaseType = SubstType(BaseType, TemplateArgs, Init->getSourceLocation(),
1515 New->getDeclName());
Anders Carlsson09025312009-08-29 05:16:22 +00001516
1517 NewInit = BuildBaseInitializer(BaseType,
Mike Stump1eb44332009-09-09 15:08:12 +00001518 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001519 NewArgs.size(),
1520 Init->getSourceLocation(),
1521 Init->getRParenLoc(),
1522 New->getParent());
1523 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001524 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00001525
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001526 // Is this an anonymous union?
1527 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregore95b4092009-09-16 18:34:49 +00001528 Member = cast<FieldDecl>(FindInstantiatedDecl(UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001529 else
Douglas Gregore95b4092009-09-16 18:34:49 +00001530 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMember(),
1531 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00001532
1533 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001534 NewArgs.size(),
1535 Init->getSourceLocation(),
1536 Init->getRParenLoc());
1537 }
1538
1539 if (NewInit.isInvalid())
1540 New->setInvalidDecl();
1541 else {
1542 // FIXME: It would be nice if ASTOwningVector had a release function.
1543 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001544
Anders Carlsson09025312009-08-29 05:16:22 +00001545 NewInits.push_back((MemInitTy *)NewInit.get());
1546 }
1547 }
Mike Stump1eb44332009-09-09 15:08:12 +00001548
Anders Carlsson09025312009-08-29 05:16:22 +00001549 // Assign all the initializers to the new constructor.
Mike Stump1eb44332009-09-09 15:08:12 +00001550 ActOnMemInitializers(DeclPtrTy::make(New),
Anders Carlsson09025312009-08-29 05:16:22 +00001551 /*FIXME: ColonLoc */
1552 SourceLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001553 NewInits.data(), NewInits.size());
Anders Carlsson09025312009-08-29 05:16:22 +00001554}
1555
John McCall52a575a2009-08-29 08:11:13 +00001556// TODO: this could be templated if the various decl types used the
1557// same method name.
1558static bool isInstantiationOf(ClassTemplateDecl *Pattern,
1559 ClassTemplateDecl *Instance) {
1560 Pattern = Pattern->getCanonicalDecl();
1561
1562 do {
1563 Instance = Instance->getCanonicalDecl();
1564 if (Pattern == Instance) return true;
1565 Instance = Instance->getInstantiatedFromMemberTemplate();
1566 } while (Instance);
1567
1568 return false;
1569}
1570
Douglas Gregor0d696532009-09-28 06:34:35 +00001571static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
1572 FunctionTemplateDecl *Instance) {
1573 Pattern = Pattern->getCanonicalDecl();
1574
1575 do {
1576 Instance = Instance->getCanonicalDecl();
1577 if (Pattern == Instance) return true;
1578 Instance = Instance->getInstantiatedFromMemberTemplate();
1579 } while (Instance);
1580
1581 return false;
1582}
1583
Douglas Gregored9c0f92009-10-29 00:04:11 +00001584static bool
1585isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
1586 ClassTemplatePartialSpecializationDecl *Instance) {
1587 Pattern
1588 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
1589 do {
1590 Instance = cast<ClassTemplatePartialSpecializationDecl>(
1591 Instance->getCanonicalDecl());
1592 if (Pattern == Instance)
1593 return true;
1594 Instance = Instance->getInstantiatedFromMember();
1595 } while (Instance);
1596
1597 return false;
1598}
1599
John McCall52a575a2009-08-29 08:11:13 +00001600static bool isInstantiationOf(CXXRecordDecl *Pattern,
1601 CXXRecordDecl *Instance) {
1602 Pattern = Pattern->getCanonicalDecl();
1603
1604 do {
1605 Instance = Instance->getCanonicalDecl();
1606 if (Pattern == Instance) return true;
1607 Instance = Instance->getInstantiatedFromMemberClass();
1608 } while (Instance);
1609
1610 return false;
1611}
1612
1613static bool isInstantiationOf(FunctionDecl *Pattern,
1614 FunctionDecl *Instance) {
1615 Pattern = Pattern->getCanonicalDecl();
1616
1617 do {
1618 Instance = Instance->getCanonicalDecl();
1619 if (Pattern == Instance) return true;
1620 Instance = Instance->getInstantiatedFromMemberFunction();
1621 } while (Instance);
1622
1623 return false;
1624}
1625
1626static bool isInstantiationOf(EnumDecl *Pattern,
1627 EnumDecl *Instance) {
1628 Pattern = Pattern->getCanonicalDecl();
1629
1630 do {
1631 Instance = Instance->getCanonicalDecl();
1632 if (Pattern == Instance) return true;
1633 Instance = Instance->getInstantiatedFromMemberEnum();
1634 } while (Instance);
1635
1636 return false;
1637}
1638
Anders Carlsson0d8df782009-08-29 19:37:28 +00001639static bool isInstantiationOf(UnresolvedUsingDecl *Pattern,
1640 UsingDecl *Instance,
1641 ASTContext &C) {
1642 return C.getInstantiatedFromUnresolvedUsingDecl(Instance) == Pattern;
1643}
1644
John McCall52a575a2009-08-29 08:11:13 +00001645static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
1646 VarDecl *Instance) {
1647 assert(Instance->isStaticDataMember());
1648
1649 Pattern = Pattern->getCanonicalDecl();
1650
1651 do {
1652 Instance = Instance->getCanonicalDecl();
1653 if (Pattern == Instance) return true;
1654 Instance = Instance->getInstantiatedFromStaticDataMember();
1655 } while (Instance);
1656
1657 return false;
1658}
1659
Douglas Gregor815215d2009-05-27 05:35:12 +00001660static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00001661 if (D->getKind() != Other->getKind()) {
1662 if (UnresolvedUsingDecl *UUD = dyn_cast<UnresolvedUsingDecl>(D)) {
1663 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
1664 return isInstantiationOf(UUD, UD, Ctx);
1665 }
1666 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001667
Anders Carlsson0d8df782009-08-29 19:37:28 +00001668 return false;
1669 }
Mike Stump1eb44332009-09-09 15:08:12 +00001670
John McCall52a575a2009-08-29 08:11:13 +00001671 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
1672 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001673
John McCall52a575a2009-08-29 08:11:13 +00001674 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
1675 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00001676
John McCall52a575a2009-08-29 08:11:13 +00001677 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
1678 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00001679
Douglas Gregor7caa6822009-07-24 20:34:43 +00001680 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00001681 if (Var->isStaticDataMember())
1682 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
1683
1684 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
1685 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00001686
Douglas Gregor0d696532009-09-28 06:34:35 +00001687 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
1688 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
1689
Douglas Gregored9c0f92009-10-29 00:04:11 +00001690 if (ClassTemplatePartialSpecializationDecl *PartialSpec
1691 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
1692 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
1693 PartialSpec);
1694
Anders Carlssond8b285f2009-09-01 04:26:58 +00001695 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
1696 if (!Field->getDeclName()) {
1697 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00001698 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00001699 cast<FieldDecl>(D);
1700 }
1701 }
Mike Stump1eb44332009-09-09 15:08:12 +00001702
Douglas Gregor815215d2009-05-27 05:35:12 +00001703 return D->getDeclName() && isa<NamedDecl>(Other) &&
1704 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
1705}
1706
1707template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00001708static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00001709 NamedDecl *D,
1710 ForwardIterator first,
1711 ForwardIterator last) {
1712 for (; first != last; ++first)
1713 if (isInstantiationOf(Ctx, D, *first))
1714 return cast<NamedDecl>(*first);
1715
1716 return 0;
1717}
1718
John McCall02cace72009-08-28 07:59:38 +00001719/// \brief Finds the instantiation of the given declaration context
1720/// within the current instantiation.
1721///
1722/// \returns NULL if there was an error
Douglas Gregore95b4092009-09-16 18:34:49 +00001723DeclContext *Sema::FindInstantiatedContext(DeclContext* DC,
1724 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00001725 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00001726 Decl* ID = FindInstantiatedDecl(D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00001727 return cast_or_null<DeclContext>(ID);
1728 } else return DC;
1729}
1730
Douglas Gregored961e72009-05-27 17:54:46 +00001731/// \brief Find the instantiation of the given declaration within the
1732/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00001733///
1734/// This routine is intended to be used when \p D is a declaration
1735/// referenced from within a template, that needs to mapped into the
1736/// corresponding declaration within an instantiation. For example,
1737/// given:
1738///
1739/// \code
1740/// template<typename T>
1741/// struct X {
1742/// enum Kind {
1743/// KnownValue = sizeof(T)
1744/// };
1745///
1746/// bool getKind() const { return KnownValue; }
1747/// };
1748///
1749/// template struct X<int>;
1750/// \endcode
1751///
1752/// In the instantiation of X<int>::getKind(), we need to map the
1753/// EnumConstantDecl for KnownValue (which refers to
1754/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00001755/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
1756/// this mapping from within the instantiation of X<int>.
Douglas Gregore95b4092009-09-16 18:34:49 +00001757NamedDecl *Sema::FindInstantiatedDecl(NamedDecl *D,
1758 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor44c73842009-09-01 17:53:10 +00001759 if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D)) {
1760 // Transform all of the elements of the overloaded function set.
Mike Stump1eb44332009-09-09 15:08:12 +00001761 OverloadedFunctionDecl *Result
Douglas Gregor44c73842009-09-01 17:53:10 +00001762 = OverloadedFunctionDecl::Create(Context, CurContext, Ovl->getDeclName());
Mike Stump1eb44332009-09-09 15:08:12 +00001763
Douglas Gregor44c73842009-09-01 17:53:10 +00001764 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
1765 FEnd = Ovl->function_end();
1766 F != FEnd; ++F) {
1767 Result->addOverload(
Douglas Gregore95b4092009-09-16 18:34:49 +00001768 AnyFunctionDecl::getFromNamedDecl(FindInstantiatedDecl(*F,
1769 TemplateArgs)));
Douglas Gregor44c73842009-09-01 17:53:10 +00001770 }
Mike Stump1eb44332009-09-09 15:08:12 +00001771
Douglas Gregor44c73842009-09-01 17:53:10 +00001772 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001773 }
1774
Douglas Gregor815215d2009-05-27 05:35:12 +00001775 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor2bba76b2009-05-27 17:07:49 +00001776 if (isa<ParmVarDecl>(D) || ParentDC->isFunctionOrMethod()) {
1777 // D is a local of some kind. Look into the map of local
1778 // declarations to their instantiations.
1779 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
1780 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001781
Douglas Gregore95b4092009-09-16 18:34:49 +00001782 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
1783 if (!Record->isDependentContext())
1784 return D;
1785
1786 // If the RecordDecl is actually the injected-class-name or a "templated"
1787 // declaration for a class template or class template partial
1788 // specialization, substitute into the injected-class-name of the
1789 // class template or partial specialization to find the new DeclContext.
1790 QualType T;
1791 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
1792
1793 if (ClassTemplate) {
1794 T = ClassTemplate->getInjectedClassNameType(Context);
1795 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
1796 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1797 T = Context.getTypeDeclType(Record);
1798 ClassTemplate = PartialSpec->getSpecializedTemplate();
1799 }
1800
1801 if (!T.isNull()) {
1802 // Substitute into the injected-class-name to get the type corresponding
1803 // to the instantiation we want. This substitution should never fail,
1804 // since we know we can instantiate the injected-class-name or we wouldn't
1805 // have gotten to the injected-class-name!
1806 // FIXME: Can we use the CurrentInstantiationScope to avoid this extra
1807 // instantiation in the common case?
1808 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
1809 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
1810
1811 if (!T->isDependentType()) {
1812 assert(T->isRecordType() && "Instantiation must produce a record type");
1813 return T->getAs<RecordType>()->getDecl();
1814 }
1815
1816 // We are performing "partial" template instantiation to create the
1817 // member declarations for the members of a class template
1818 // specialization. Therefore, D is actually referring to something in
1819 // the current instantiation. Look through the current context,
1820 // which contains actual instantiations, to find the instantiation of
1821 // the "current instantiation" that D refers to.
Mike Stump1eb44332009-09-09 15:08:12 +00001822 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00001823 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001824 if (ClassTemplateSpecializationDecl *Spec
John McCall52a575a2009-08-29 08:11:13 +00001825 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00001826 if (isInstantiationOf(ClassTemplate,
1827 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00001828 return Spec;
1829 }
1830
Mike Stump1eb44332009-09-09 15:08:12 +00001831 assert(false &&
John McCall52a575a2009-08-29 08:11:13 +00001832 "Unable to find declaration for the current instantiation");
Douglas Gregore95b4092009-09-16 18:34:49 +00001833 return Record;
John McCall52a575a2009-08-29 08:11:13 +00001834 }
Douglas Gregore95b4092009-09-16 18:34:49 +00001835
1836 // Fall through to deal with other dependent record types (e.g.,
1837 // anonymous unions in class templates).
1838 }
John McCall52a575a2009-08-29 08:11:13 +00001839
Douglas Gregore95b4092009-09-16 18:34:49 +00001840 if (!ParentDC->isDependentContext())
1841 return D;
1842
1843 ParentDC = FindInstantiatedContext(ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001844 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00001845 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001846
Douglas Gregor815215d2009-05-27 05:35:12 +00001847 if (ParentDC != D->getDeclContext()) {
1848 // We performed some kind of instantiation in the parent context,
1849 // so now we need to look into the instantiated parent context to
1850 // find the instantiation of the declaration D.
1851 NamedDecl *Result = 0;
1852 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001853 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00001854 Result = findInstantiationOf(Context, D, Found.first, Found.second);
1855 } else {
1856 // Since we don't have a name for the entity we're looking for,
1857 // our only option is to walk through all of the declarations to
1858 // find that name. This will occur in a few cases:
1859 //
1860 // - anonymous struct/union within a template
1861 // - unnamed class/struct/union/enum within a template
1862 //
1863 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00001864 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001865 ParentDC->decls_begin(),
1866 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00001867 }
Mike Stump1eb44332009-09-09 15:08:12 +00001868
Douglas Gregor815215d2009-05-27 05:35:12 +00001869 assert(Result && "Unable to find instantiation of declaration!");
1870 D = Result;
1871 }
1872
Douglas Gregor815215d2009-05-27 05:35:12 +00001873 return D;
1874}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001875
Mike Stump1eb44332009-09-09 15:08:12 +00001876/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001877/// instantiations we have seen until this point.
1878void Sema::PerformPendingImplicitInstantiations() {
1879 while (!PendingImplicitInstantiations.empty()) {
1880 PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front();
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001881 PendingImplicitInstantiations.pop_front();
Mike Stump1eb44332009-09-09 15:08:12 +00001882
Douglas Gregor7caa6822009-07-24 20:34:43 +00001883 // Instantiate function definitions
1884 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001885 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001886 Function->getLocation(), *this,
1887 Context.getSourceManager(),
1888 "instantiating function definition");
Mike Stump1eb44332009-09-09 15:08:12 +00001889
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001890 if (!Function->getBody())
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001891 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001892 continue;
1893 }
Mike Stump1eb44332009-09-09 15:08:12 +00001894
Douglas Gregor7caa6822009-07-24 20:34:43 +00001895 // Instantiate static data member definitions.
1896 VarDecl *Var = cast<VarDecl>(Inst.first);
1897 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001898
Mike Stump1eb44332009-09-09 15:08:12 +00001899 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001900 Var->getLocation(), *this,
1901 Context.getSourceManager(),
1902 "instantiating static data member "
1903 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00001904
Douglas Gregor7caa6822009-07-24 20:34:43 +00001905 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001906 }
1907}