blob: a43413e275212b3c8081a9f9698320901467b65e [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 Gregore53060f2009-06-25 22:08:12 +000050 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregord475b8d2009-03-25 21:17:03 +000051 Decl *VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregord60e1052009-08-27 16:57:43 +000052 Decl *VisitCXXMethodDecl(CXXMethodDecl *D,
53 TemplateParameterList *TemplateParams = 0);
Douglas Gregor615c5d42009-03-24 16:43:20 +000054 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
Douglas Gregor03b2b072009-03-24 00:15:49 +000055 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +000056 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor6477b692009-03-25 15:04:13 +000057 ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor2dc0e642009-03-23 23:06:20 +000058 Decl *VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
John McCalle29ba202009-08-20 01:44:21 +000059 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregord60e1052009-08-27 16:57:43 +000060 Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
John McCalle29ba202009-08-20 01:44:21 +000061 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Anders Carlsson0dde18e2009-08-28 15:18:15 +000062 Decl *VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D);
Mike Stump1eb44332009-09-09 15:08:12 +000063
Douglas Gregor8dbc2692009-03-17 21:15:40 +000064 // Base case. FIXME: Remove once we can instantiate everything.
Mike Stump1eb44332009-09-09 15:08:12 +000065 Decl *VisitDecl(Decl *) {
Douglas Gregor3d7a12a2009-03-25 23:32:15 +000066 assert(false && "Template instantiation of unknown declaration kind!");
Douglas Gregor8dbc2692009-03-17 21:15:40 +000067 return 0;
68 }
Douglas Gregor5545e162009-03-24 00:38:23 +000069
John McCallfd810b12009-08-14 02:03:10 +000070 const LangOptions &getLangOptions() {
71 return SemaRef.getLangOptions();
72 }
73
Douglas Gregor5545e162009-03-24 00:38:23 +000074 // Helper functions for instantiating methods.
John McCallce3ff2b2009-08-25 22:02:44 +000075 QualType SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +000076 llvm::SmallVectorImpl<ParmVarDecl *> &Params);
Douglas Gregore53060f2009-06-25 22:08:12 +000077 bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
Douglas Gregor5545e162009-03-24 00:38:23 +000078 bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
John McCalle29ba202009-08-20 01:44:21 +000079
80 TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +000081 SubstTemplateParams(TemplateParameterList *List);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000082 };
83}
84
Douglas Gregor4f722be2009-03-25 15:45:12 +000085Decl *
86TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
87 assert(false && "Translation units cannot be instantiated");
88 return D;
89}
90
91Decl *
92TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
93 assert(false && "Namespaces cannot be instantiated");
94 return D;
95}
96
Douglas Gregor8dbc2692009-03-17 21:15:40 +000097Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
98 bool Invalid = false;
99 QualType T = D->getUnderlyingType();
100 if (T->isDependentType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000101 T = SemaRef.SubstType(T, TemplateArgs,
John McCallce3ff2b2009-08-25 22:02:44 +0000102 D->getLocation(), D->getDeclName());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000103 if (T.isNull()) {
104 Invalid = true;
105 T = SemaRef.Context.IntTy;
106 }
107 }
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000109 // Create the new typedef
110 TypedefDecl *Typedef
111 = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
112 D->getIdentifier(), T);
113 if (Invalid)
114 Typedef->setInvalidDecl();
115
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000116 Owner->addDecl(Typedef);
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000118 return Typedef;
119}
120
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000121Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000122 // Do substitution on the type of the declaration
123 QualType T = SemaRef.SubstType(D->getType(), TemplateArgs,
124 D->getTypeSpecStartLoc(),
125 D->getDeclName());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000126 if (T.isNull())
127 return 0;
128
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000129 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000130 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
131 D->getLocation(), D->getIdentifier(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000132 T, D->getDeclaratorInfo(),
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000133 D->getStorageClass());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000134 Var->setThreadSpecified(D->isThreadSpecified());
135 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
136 Var->setDeclaredInCondition(D->isDeclaredInCondition());
Mike Stump1eb44332009-09-09 15:08:12 +0000137
138 // If we are instantiating a static data member defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000139 // out-of-line, the instantiation will have the same lexical
140 // context (which will be a namespace scope) as the template.
141 if (D->isOutOfLine())
142 Var->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Mike Stump390b4cc2009-05-16 07:39:55 +0000144 // FIXME: In theory, we could have a previous declaration for variables that
145 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000146 bool Redeclaration = false;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000147 SemaRef.CheckVariableDeclaration(Var, 0, Redeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000148
Douglas Gregor7caa6822009-07-24 20:34:43 +0000149 if (D->isOutOfLine()) {
150 D->getLexicalDeclContext()->addDecl(Var);
151 Owner->makeDeclVisibleInContext(Var);
152 } else {
153 Owner->addDecl(Var);
154 }
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000156 if (D->getInit()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000157 OwningExprResult Init
John McCallce3ff2b2009-08-25 22:02:44 +0000158 = SemaRef.SubstExpr(D->getInit(), TemplateArgs);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000159 if (Init.isInvalid())
160 Var->setInvalidDecl();
Douglas Gregor83ddad32009-08-26 21:14:46 +0000161 else if (ParenListExpr *PLE = dyn_cast<ParenListExpr>((Expr *)Init.get())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000162 // FIXME: We're faking all of the comma locations, which is suboptimal.
Douglas Gregor83ddad32009-08-26 21:14:46 +0000163 // Do we even need these comma locations?
164 llvm::SmallVector<SourceLocation, 4> FakeCommaLocs;
165 if (PLE->getNumExprs() > 0) {
166 FakeCommaLocs.reserve(PLE->getNumExprs() - 1);
167 for (unsigned I = 0, N = PLE->getNumExprs() - 1; I != N; ++I) {
168 Expr *E = PLE->getExpr(I)->Retain();
169 FakeCommaLocs.push_back(
170 SemaRef.PP.getLocForEndOfToken(E->getLocEnd()));
171 }
Douglas Gregore9f8eb62009-08-26 23:26:04 +0000172 PLE->getExpr(PLE->getNumExprs() - 1)->Retain();
Douglas Gregor83ddad32009-08-26 21:14:46 +0000173 }
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Douglas Gregor83ddad32009-08-26 21:14:46 +0000175 // Add the direct initializer to the declaration.
176 SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
Mike Stump1eb44332009-09-09 15:08:12 +0000177 PLE->getLParenLoc(),
Douglas Gregor83ddad32009-08-26 21:14:46 +0000178 Sema::MultiExprArg(SemaRef,
179 (void**)PLE->getExprs(),
180 PLE->getNumExprs()),
181 FakeCommaLocs.data(),
182 PLE->getRParenLoc());
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Douglas Gregor83ddad32009-08-26 21:14:46 +0000184 // When Init is destroyed, it will destroy the instantiated ParenListExpr;
185 // we've explicitly retained all of its subexpressions already.
186 } else
Chris Lattnerb28317a2009-03-28 19:18:32 +0000187 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init),
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000188 D->hasCXXDirectInitializer());
Douglas Gregor65b90052009-07-27 17:43:39 +0000189 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
190 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000191
Douglas Gregor7caa6822009-07-24 20:34:43 +0000192 // Link instantiations of static data members back to the template from
193 // which they were instantiated.
194 if (Var->isStaticDataMember())
195 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D);
Mike Stump1eb44332009-09-09 15:08:12 +0000196
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000197 return Var;
198}
199
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000200Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
201 bool Invalid = false;
202 QualType T = D->getType();
203 if (T->isDependentType()) {
John McCallce3ff2b2009-08-25 22:02:44 +0000204 T = SemaRef.SubstType(T, TemplateArgs,
205 D->getLocation(), D->getDeclName());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000206 if (!T.isNull() && T->isFunctionType()) {
207 // C++ [temp.arg.type]p3:
208 // If a declaration acquires a function type through a type
209 // dependent on a template-parameter and this causes a
210 // declaration that does not use the syntactic form of a
211 // function declarator to have function type, the program is
212 // ill-formed.
213 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
214 << T;
215 T = QualType();
216 Invalid = true;
217 }
218 }
219
220 Expr *BitWidth = D->getBitWidth();
221 if (Invalid)
222 BitWidth = 0;
223 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000224 // The bit-width expression is not potentially evaluated.
225 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000226
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000227 OwningExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000228 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000229 if (InstantiatedBitWidth.isInvalid()) {
230 Invalid = true;
231 BitWidth = 0;
232 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000233 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000234 }
235
236 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), T,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000237 D->getDeclaratorInfo(),
Mike Stump1eb44332009-09-09 15:08:12 +0000238 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000239 D->getLocation(),
240 D->isMutable(),
241 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000242 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000243 D->getAccess(),
244 0);
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000245 if (!Field)
246 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000247
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000248 if (Invalid)
249 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000251 if (!Field->getDeclName()) {
252 // Keep track of where this decl came from.
253 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000254 }
Mike Stump1eb44332009-09-09 15:08:12 +0000255
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000256 Field->setImplicit(D->isImplicit());
257 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000258
259 return Field;
260}
261
John McCall02cace72009-08-28 07:59:38 +0000262Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
263 FriendDecl::FriendUnion FU;
264
265 // Handle friend type expressions by simply substituting template
266 // parameters into the pattern type.
267 if (Type *Ty = D->getFriendType()) {
268 QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs,
269 D->getLocation(), DeclarationName());
270 if (T.isNull()) return 0;
271
272 assert(getLangOptions().CPlusPlus0x || T->isRecordType());
273 FU = T.getTypePtr();
274
275 // Handle everything else by appropriate substitution.
276 } else {
277 NamedDecl *ND = D->getFriendDecl();
278 assert(ND && "friend decl must be a decl or a type!");
279
280 Decl *NewND = Visit(ND);
281 if (!NewND) return 0;
282
283 FU = cast<NamedDecl>(NewND);
John McCallfd810b12009-08-14 02:03:10 +0000284 }
Mike Stump1eb44332009-09-09 15:08:12 +0000285
John McCall02cace72009-08-28 07:59:38 +0000286 FriendDecl *FD =
287 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU,
288 D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000289 FD->setAccess(AS_public);
John McCall02cace72009-08-28 07:59:38 +0000290 Owner->addDecl(FD);
291 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000292}
293
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000294Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
295 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Douglas Gregorac7610d2009-06-22 20:57:11 +0000297 // The expression in a static assertion is not potentially evaluated.
298 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000300 OwningExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000301 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000302 if (InstantiatedAssertExpr.isInvalid())
303 return 0;
304
Douglas Gregor43d9d922009-08-08 01:41:12 +0000305 OwningExprResult Message(SemaRef, D->getMessage());
306 D->getMessage()->Retain();
Mike Stump1eb44332009-09-09 15:08:12 +0000307 Decl *StaticAssert
308 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
Chris Lattnerb28317a2009-03-28 19:18:32 +0000309 move(InstantiatedAssertExpr),
310 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000311 return StaticAssert;
312}
313
314Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000315 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000316 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000317 D->getTagKeywordLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000318 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000319 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000320 Enum->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000321 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000322 Enum->startDefinition();
323
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000324 llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000325
326 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000327 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
328 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000329 EC != ECEnd; ++EC) {
330 // The specified value for the enumerator.
331 OwningExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000332 if (Expr *UninstValue = EC->getInitExpr()) {
333 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000334 EnterExpressionEvaluationContext Unevaluated(SemaRef,
Douglas Gregorac7610d2009-06-22 20:57:11 +0000335 Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000336
John McCallce3ff2b2009-08-25 22:02:44 +0000337 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000338 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000339
340 // Drop the initial value and continue.
341 bool isInvalid = false;
342 if (Value.isInvalid()) {
343 Value = SemaRef.Owned((Expr *)0);
344 isInvalid = true;
345 }
346
Mike Stump1eb44332009-09-09 15:08:12 +0000347 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000348 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
349 EC->getLocation(), EC->getIdentifier(),
350 move(Value));
351
352 if (isInvalid) {
353 if (EnumConst)
354 EnumConst->setInvalidDecl();
355 Enum->setInvalidDecl();
356 }
357
358 if (EnumConst) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000359 Enum->addDecl(EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000360 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000361 LastEnumConst = EnumConst;
362 }
363 }
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000365 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000366 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000367 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
368 Sema::DeclPtrTy::make(Enum),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000369 &Enumerators[0], Enumerators.size(),
370 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000371
372 return Enum;
373}
374
Douglas Gregor6477b692009-03-25 15:04:13 +0000375Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
376 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
377 return 0;
378}
379
John McCalle29ba202009-08-20 01:44:21 +0000380Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
381 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000382 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000383 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000384 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000385
386 CXXRecordDecl *Pattern = D->getTemplatedDecl();
387 CXXRecordDecl *RecordInst
388 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner,
389 Pattern->getLocation(), Pattern->getIdentifier(),
390 Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL);
391
392 ClassTemplateDecl *Inst
393 = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
394 D->getIdentifier(), InstParams, RecordInst, 0);
395 RecordInst->setDescribedClassTemplate(Inst);
396 Inst->setAccess(D->getAccess());
397 Inst->setInstantiatedFromMemberTemplate(D);
398
399 Owner->addDecl(Inst);
400 return Inst;
401}
402
Douglas Gregord60e1052009-08-27 16:57:43 +0000403Decl *
404TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000405 // FIXME: Dig out the out-of-line definition of this function template?
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Douglas Gregord60e1052009-08-27 16:57:43 +0000407 TemplateParameterList *TempParams = D->getTemplateParameters();
408 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000409 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000410 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000411
412 // FIXME: Handle instantiation of nested function templates that aren't
Douglas Gregord60e1052009-08-27 16:57:43 +0000413 // member function templates. This could happen inside a FriendDecl.
414 assert(isa<CXXMethodDecl>(D->getTemplatedDecl()));
Mike Stump1eb44332009-09-09 15:08:12 +0000415 CXXMethodDecl *InstMethod
Douglas Gregord60e1052009-08-27 16:57:43 +0000416 = cast_or_null<CXXMethodDecl>(
Mike Stump1eb44332009-09-09 15:08:12 +0000417 VisitCXXMethodDecl(cast<CXXMethodDecl>(D->getTemplatedDecl()),
Douglas Gregord60e1052009-08-27 16:57:43 +0000418 InstParams));
419 if (!InstMethod)
420 return 0;
421
Mike Stump1eb44332009-09-09 15:08:12 +0000422 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000423 // template from which it was instantiated.
424 FunctionTemplateDecl *InstTemplate = InstMethod->getDescribedFunctionTemplate();
425 assert(InstTemplate && "VisitCXXMethodDecl didn't create a template!");
426 InstTemplate->setInstantiatedFromMemberTemplate(D);
427 Owner->addDecl(InstTemplate);
428 return InstTemplate;
429}
430
Douglas Gregord475b8d2009-03-25 21:17:03 +0000431Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
432 CXXRecordDecl *PrevDecl = 0;
433 if (D->isInjectedClassName())
434 PrevDecl = cast<CXXRecordDecl>(Owner);
435
436 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000437 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000438 D->getLocation(), D->getIdentifier(),
439 D->getTagKeywordLoc(), PrevDecl);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000440 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000441 // FIXME: Check against AS_none is an ugly hack to work around the issue that
442 // the tag decls introduced by friend class declarations don't have an access
443 // specifier. Remove once this area of the code gets sorted out.
444 if (D->getAccess() != AS_none)
445 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000446 if (!D->isInjectedClassName())
447 Record->setInstantiationOfMemberClass(D);
448
John McCall02cace72009-08-28 07:59:38 +0000449 // If the original function was part of a friend declaration,
450 // inherit its namespace state.
451 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
452 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
453
Anders Carlssond8b285f2009-09-01 04:26:58 +0000454 Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
455
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000456 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000457 return Record;
458}
459
John McCall02cace72009-08-28 07:59:38 +0000460/// Normal class members are of more specific types and therefore
461/// don't make it here. This function serves two purposes:
462/// 1) instantiating function templates
463/// 2) substituting friend declarations
464/// FIXME: preserve function definitions in case #2
Douglas Gregore53060f2009-06-25 22:08:12 +0000465Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000466 // Check whether there is already a function template specialization for
467 // this declaration.
468 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
469 void *InsertPos = 0;
470 if (FunctionTemplate) {
471 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000472 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000473 TemplateArgs.getInnermost().getFlatArgumentList(),
474 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor828e2262009-07-29 16:09:57 +0000475 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000476
477 FunctionTemplateSpecializationInfo *Info
478 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor127102b2009-06-29 20:59:39 +0000479 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Douglas Gregor127102b2009-06-29 20:59:39 +0000481 // If we already have a function template specialization, return it.
482 if (Info)
483 return Info->Function;
484 }
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Douglas Gregore53060f2009-06-25 22:08:12 +0000486 Sema::LocalInstantiationScope Scope(SemaRef);
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Douglas Gregore53060f2009-06-25 22:08:12 +0000488 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000489 QualType T = SubstFunctionType(D, Params);
Douglas Gregore53060f2009-06-25 22:08:12 +0000490 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000491 return 0;
John McCallfd810b12009-08-14 02:03:10 +0000492
Douglas Gregore53060f2009-06-25 22:08:12 +0000493 // Build the instantiated method declaration.
Douglas Gregore95b4092009-09-16 18:34:49 +0000494 DeclContext *DC = SemaRef.FindInstantiatedContext(D->getDeclContext(),
495 TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +0000496 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +0000497 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000498 D->getDeclName(), T, D->getDeclaratorInfo(),
499 D->getStorageClass(),
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000500 D->isInline(), D->hasWrittenPrototype());
John McCall02cace72009-08-28 07:59:38 +0000501 Function->setLexicalDeclContext(Owner);
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Douglas Gregore53060f2009-06-25 22:08:12 +0000503 // Attach the parameters
504 for (unsigned P = 0; P < Params.size(); ++P)
505 Params[P]->setOwningFunction(Function);
506 Function->setParams(SemaRef.Context, Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +0000507
508 // If the original function was part of a friend declaration,
509 // inherit its namespace state and add it to the owner.
510 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind()) {
511 bool WasDeclared = (FOK == Decl::FOK_Declared);
512 Function->setObjectOfFriendDecl(WasDeclared);
513 if (!Owner->isDependentContext())
John McCallab88d972009-08-31 22:39:49 +0000514 DC->makeDeclVisibleInContext(Function, /* Recoverable = */ false);
John McCallf181d8a2009-08-29 03:16:09 +0000515
516 Function->setInstantiationOfMemberFunction(D);
John McCall02cace72009-08-28 07:59:38 +0000517 }
Mike Stump1eb44332009-09-09 15:08:12 +0000518
Douglas Gregore53060f2009-06-25 22:08:12 +0000519 if (InitFunctionInstantiation(Function, D))
520 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000521
Douglas Gregore53060f2009-06-25 22:08:12 +0000522 bool Redeclaration = false;
523 bool OverloadableAttrRequired = false;
524 NamedDecl *PrevDecl = 0;
525 SemaRef.CheckFunctionDeclaration(Function, PrevDecl, Redeclaration,
526 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000527
Douglas Gregor127102b2009-06-29 20:59:39 +0000528 if (FunctionTemplate) {
529 // Record this function template specialization.
530 Function->setFunctionTemplateSpecialization(SemaRef.Context,
531 FunctionTemplate,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000532 &TemplateArgs.getInnermost(),
Douglas Gregor127102b2009-06-29 20:59:39 +0000533 InsertPos);
John McCallfd810b12009-08-14 02:03:10 +0000534 }
535
Douglas Gregore53060f2009-06-25 22:08:12 +0000536 return Function;
537}
538
Douglas Gregord60e1052009-08-27 16:57:43 +0000539Decl *
540TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
541 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +0000542 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
543 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +0000544 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +0000545 // We are creating a function template specialization from a function
546 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +0000547 // specialization for this particular set of template arguments.
Douglas Gregor6b906862009-08-21 00:16:32 +0000548 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000549 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000550 TemplateArgs.getInnermost().getFlatArgumentList(),
551 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000552 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000553
554 FunctionTemplateSpecializationInfo *Info
555 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor6b906862009-08-21 00:16:32 +0000556 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Douglas Gregor6b906862009-08-21 00:16:32 +0000558 // If we already have a function template specialization, return it.
559 if (Info)
560 return Info->Function;
561 }
562
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000563 Sema::LocalInstantiationScope Scope(SemaRef);
564
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000565 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000566 QualType T = SubstFunctionType(D, Params);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000567 if (T.isNull())
568 return 0;
569
570 // Build the instantiated method declaration.
571 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
Douglas Gregordec06662009-08-21 18:42:58 +0000572 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000573
Douglas Gregordec06662009-08-21 18:42:58 +0000574 DeclarationName Name = D->getDeclName();
Douglas Gregor17e32f32009-08-21 22:43:28 +0000575 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregordec06662009-08-21 18:42:58 +0000576 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
577 Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
578 SemaRef.Context.getCanonicalType(ClassTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000579 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
580 Constructor->getLocation(),
581 Name, T,
Douglas Gregor17e32f32009-08-21 22:43:28 +0000582 Constructor->getDeclaratorInfo(),
Mike Stump1eb44332009-09-09 15:08:12 +0000583 Constructor->isExplicit(),
Douglas Gregor17e32f32009-08-21 22:43:28 +0000584 Constructor->isInline(), false);
585 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
586 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
587 Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
588 SemaRef.Context.getCanonicalType(ClassTy));
589 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
590 Destructor->getLocation(), Name,
591 T, Destructor->isInline(), false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000592 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000593 CanQualType ConvTy
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000594 = SemaRef.Context.getCanonicalType(
595 T->getAsFunctionType()->getResultType());
596 Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
597 ConvTy);
598 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
599 Conversion->getLocation(), Name,
600 T, Conversion->getDeclaratorInfo(),
Mike Stump1eb44332009-09-09 15:08:12 +0000601 Conversion->isInline(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000602 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +0000603 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000604 Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
Douglas Gregordec06662009-08-21 18:42:58 +0000605 D->getDeclName(), T, D->getDeclaratorInfo(),
606 D->isStatic(), D->isInline());
607 }
Douglas Gregor6b906862009-08-21 00:16:32 +0000608
Douglas Gregord60e1052009-08-27 16:57:43 +0000609 if (TemplateParams) {
610 // Our resulting instantiation is actually a function template, since we
611 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +0000612 //
Douglas Gregord60e1052009-08-27 16:57:43 +0000613 // template<typename T>
614 // struct X {
615 // template<typename U> void f(T, U);
616 // };
617 //
618 // X<int> x;
619 //
620 // We are instantiating the member template "f" within X<int>, which means
621 // substituting int for T, but leaving "f" as a member function template.
622 // Build the function template itself.
623 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
624 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +0000625 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +0000626 TemplateParams, Method);
627 if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +0000628 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +0000629 Method->setDescribedFunctionTemplate(FunctionTemplate);
630 } else if (!FunctionTemplate)
Douglas Gregor6b906862009-08-21 00:16:32 +0000631 Method->setInstantiationOfMemberFunction(D);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000632
Mike Stump1eb44332009-09-09 15:08:12 +0000633 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000634 // out-of-line, the instantiation will have the same lexical
635 // context (which will be a namespace scope) as the template.
636 if (D->isOutOfLine())
637 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Douglas Gregor5545e162009-03-24 00:38:23 +0000639 // Attach the parameters
640 for (unsigned P = 0; P < Params.size(); ++P)
641 Params[P]->setOwningFunction(Method);
Jay Foadbeaaccd2009-05-21 09:52:38 +0000642 Method->setParams(SemaRef.Context, Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +0000643
644 if (InitMethodInstantiation(Method, D))
645 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000646
Douglas Gregordec06662009-08-21 18:42:58 +0000647 NamedDecl *PrevDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000648
Douglas Gregord60e1052009-08-27 16:57:43 +0000649 if (!FunctionTemplate || TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +0000650 PrevDecl = SemaRef.LookupQualifiedName(Owner, Name,
Douglas Gregordec06662009-08-21 18:42:58 +0000651 Sema::LookupOrdinaryName, true);
Mike Stump1eb44332009-09-09 15:08:12 +0000652
Douglas Gregordec06662009-08-21 18:42:58 +0000653 // In C++, the previous declaration we find might be a tag type
654 // (class or enum). In this case, the new declaration will hide the
655 // tag type. Note that this does does not apply if we're declaring a
656 // typedef (C++ [dcl.typedef]p4).
657 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
658 PrevDecl = 0;
659 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000660
Douglas Gregord60e1052009-08-27 16:57:43 +0000661 if (FunctionTemplate && !TemplateParams)
Douglas Gregor6b906862009-08-21 00:16:32 +0000662 // Record this function template specialization.
663 Method->setFunctionTemplateSpecialization(SemaRef.Context,
664 FunctionTemplate,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000665 &TemplateArgs.getInnermost(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000666 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000667
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000668 bool Redeclaration = false;
669 bool OverloadableAttrRequired = false;
670 SemaRef.CheckFunctionDeclaration(Method, PrevDecl, Redeclaration,
671 /*FIXME:*/OverloadableAttrRequired);
672
673 if (!FunctionTemplate && (!Method->isInvalidDecl() || !PrevDecl))
Douglas Gregordec06662009-08-21 18:42:58 +0000674 Owner->addDecl(Method);
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000676 return Method;
677}
678
Douglas Gregor615c5d42009-03-24 16:43:20 +0000679Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +0000680 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000681}
682
Douglas Gregor03b2b072009-03-24 00:15:49 +0000683Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +0000684 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +0000685}
686
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000687Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000688 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000689}
690
Douglas Gregor6477b692009-03-25 15:04:13 +0000691ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000692 QualType OrigT = SemaRef.SubstType(D->getOriginalType(), TemplateArgs,
Douglas Gregor7e063902009-05-11 23:53:27 +0000693 D->getLocation(), D->getDeclName());
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000694 if (OrigT.isNull())
695 return 0;
696
697 QualType T = SemaRef.adjustParameterType(OrigT);
698
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000699 // Allocate the parameter
700 ParmVarDecl *Param = 0;
701 if (T == OrigT)
702 Param = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000703 D->getIdentifier(), T, D->getDeclaratorInfo(),
704 D->getStorageClass(), 0);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000705 else
Mike Stump1eb44332009-09-09 15:08:12 +0000706 Param = OriginalParmVarDecl::Create(SemaRef.Context, Owner,
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000707 D->getLocation(), D->getIdentifier(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000708 T, D->getDeclaratorInfo(), OrigT,
709 D->getStorageClass(), 0);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000710
Anders Carlsson9351c172009-08-25 03:18:48 +0000711 // Mark the default argument as being uninstantiated.
712 if (Expr *Arg = D->getDefaultArg())
713 Param->setUninstantiatedDefaultArg(Arg);
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000715 // Note: we don't try to instantiate function parameters until after
716 // we've instantiated the function's type. Therefore, we don't have
717 // to check for 'void' parameter types here.
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000718 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000719 return Param;
720}
721
722Decl *
723TemplateDeclInstantiator::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
724 // Since parameter types can decay either before or after
725 // instantiation, we simply treat OriginalParmVarDecls as
726 // ParmVarDecls the same way, and create one or the other depending
727 // on what happens after template instantiation.
728 return VisitParmVarDecl(D);
729}
730
John McCalle29ba202009-08-20 01:44:21 +0000731Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
732 TemplateTypeParmDecl *D) {
733 // TODO: don't always clone when decls are refcounted.
734 const Type* T = D->getTypeForDecl();
735 assert(T->isTemplateTypeParmType());
736 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000737
John McCalle29ba202009-08-20 01:44:21 +0000738 TemplateTypeParmDecl *Inst =
739 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
740 TTPT->getDepth(), TTPT->getIndex(),
741 TTPT->getName(),
742 D->wasDeclaredWithTypename(),
743 D->isParameterPack());
744
745 if (D->hasDefaultArgument()) {
746 QualType DefaultPattern = D->getDefaultArgument();
747 QualType DefaultInst
John McCallce3ff2b2009-08-25 22:02:44 +0000748 = SemaRef.SubstType(DefaultPattern, TemplateArgs,
749 D->getDefaultArgumentLoc(),
750 D->getDeclName());
Mike Stump1eb44332009-09-09 15:08:12 +0000751
John McCalle29ba202009-08-20 01:44:21 +0000752 Inst->setDefaultArgument(DefaultInst,
753 D->getDefaultArgumentLoc(),
754 D->defaultArgumentWasInherited() /* preserve? */);
755 }
756
757 return Inst;
758}
759
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000760Decl *
761TemplateDeclInstantiator::VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000762 NestedNameSpecifier *NNS =
763 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
764 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000765 TemplateArgs);
766 if (!NNS)
767 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000769 CXXScopeSpec SS;
770 SS.setRange(D->getTargetNestedNameRange());
771 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +0000772
773 NamedDecl *UD =
774 SemaRef.BuildUsingDeclaration(D->getLocation(), SS,
775 D->getTargetNameLocation(),
Anders Carlsson0d8df782009-08-29 19:37:28 +0000776 D->getTargetName(), 0, D->isTypeName());
777 if (UD)
Mike Stump1eb44332009-09-09 15:08:12 +0000778 SemaRef.Context.setInstantiatedFromUnresolvedUsingDecl(cast<UsingDecl>(UD),
Anders Carlsson0d8df782009-08-29 19:37:28 +0000779 D);
780 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000781}
782
John McCallce3ff2b2009-08-25 22:02:44 +0000783Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000784 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +0000785 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000786 return Instantiator.Visit(D);
787}
788
John McCalle29ba202009-08-20 01:44:21 +0000789/// \brief Instantiates a nested template parameter list in the current
790/// instantiation context.
791///
792/// \param L The parameter list to instantiate
793///
794/// \returns NULL if there was an error
795TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +0000796TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +0000797 // Get errors for all the parameters before bailing out.
798 bool Invalid = false;
799
800 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000801 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +0000802 ParamVector Params;
803 Params.reserve(N);
804 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
805 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000806 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +0000807 Params.push_back(D);
808 Invalid = Invalid || !D;
809 }
810
811 // Clean up if we had an error.
812 if (Invalid) {
813 for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
814 PI != PE; ++PI)
815 if (*PI)
816 (*PI)->Destroy(SemaRef.Context);
817 return NULL;
818 }
819
820 TemplateParameterList *InstL
821 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
822 L->getLAngleLoc(), &Params.front(), N,
823 L->getRAngleLoc());
824 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +0000825}
John McCalle29ba202009-08-20 01:44:21 +0000826
John McCallce3ff2b2009-08-25 22:02:44 +0000827/// \brief Does substitution on the type of the given function, including
828/// all of the function parameters.
Douglas Gregor5545e162009-03-24 00:38:23 +0000829///
John McCallce3ff2b2009-08-25 22:02:44 +0000830/// \param D The function whose type will be the basis of the substitution
Douglas Gregor5545e162009-03-24 00:38:23 +0000831///
832/// \param Params the instantiated parameter declarations
833
John McCallce3ff2b2009-08-25 22:02:44 +0000834/// \returns the instantiated function's type if successful, a NULL
Douglas Gregor5545e162009-03-24 00:38:23 +0000835/// type if there was an error.
Mike Stump1eb44332009-09-09 15:08:12 +0000836QualType
John McCallce3ff2b2009-08-25 22:02:44 +0000837TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +0000838 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
839 bool InvalidDecl = false;
840
John McCallce3ff2b2009-08-25 22:02:44 +0000841 // Substitute all of the function's formal parameter types.
Douglas Gregor7e063902009-05-11 23:53:27 +0000842 TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000843 llvm::SmallVector<QualType, 4> ParamTys;
Mike Stump1eb44332009-09-09 15:08:12 +0000844 for (FunctionDecl::param_iterator P = D->param_begin(),
Douglas Gregor5545e162009-03-24 00:38:23 +0000845 PEnd = D->param_end();
846 P != PEnd; ++P) {
Douglas Gregor6477b692009-03-25 15:04:13 +0000847 if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
Douglas Gregor5545e162009-03-24 00:38:23 +0000848 if (PInst->getType()->isVoidType()) {
849 SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
850 PInst->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000851 } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000852 PInst->getType(),
853 diag::err_abstract_type_in_decl,
854 Sema::AbstractParamType))
Douglas Gregor5545e162009-03-24 00:38:23 +0000855 PInst->setInvalidDecl();
856
857 Params.push_back(PInst);
858 ParamTys.push_back(PInst->getType());
859
860 if (PInst->isInvalidDecl())
861 InvalidDecl = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000862 } else
Douglas Gregor5545e162009-03-24 00:38:23 +0000863 InvalidDecl = true;
864 }
865
866 // FIXME: Deallocate dead declarations.
867 if (InvalidDecl)
868 return QualType();
869
870 const FunctionProtoType *Proto = D->getType()->getAsFunctionProtoType();
871 assert(Proto && "Missing prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +0000872 QualType ResultType
John McCallce3ff2b2009-08-25 22:02:44 +0000873 = SemaRef.SubstType(Proto->getResultType(), TemplateArgs,
874 D->getLocation(), D->getDeclName());
Douglas Gregor5545e162009-03-24 00:38:23 +0000875 if (ResultType.isNull())
876 return QualType();
877
Jay Foadbeaaccd2009-05-21 09:52:38 +0000878 return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
Douglas Gregor5545e162009-03-24 00:38:23 +0000879 Proto->isVariadic(), Proto->getTypeQuals(),
880 D->getLocation(), D->getDeclName());
881}
882
Mike Stump1eb44332009-09-09 15:08:12 +0000883/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +0000884/// declaration (New) from the corresponding fields of its template (Tmpl).
885///
886/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +0000887bool
888TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +0000889 FunctionDecl *Tmpl) {
890 if (Tmpl->isDeleted())
891 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +0000892
Douglas Gregorcca9e962009-07-01 22:01:06 +0000893 // If we are performing substituting explicitly-specified template arguments
894 // or deduced template arguments into a function template and we reach this
895 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +0000896 // to keeping the new function template specialization. We therefore
897 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +0000898 // into a template instantiation for this specific function template
899 // specialization, which is not a SFINAE context, so that we diagnose any
900 // further errors in the declaration itself.
901 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
902 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
903 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
904 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +0000905 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +0000906 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000907 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +0000908 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +0000909 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +0000910 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
911 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
912 }
913 }
Mike Stump1eb44332009-09-09 15:08:12 +0000914
Douglas Gregore53060f2009-06-25 22:08:12 +0000915 return false;
916}
917
Douglas Gregor5545e162009-03-24 00:38:23 +0000918/// \brief Initializes common fields of an instantiated method
919/// declaration (New) from the corresponding fields of its template
920/// (Tmpl).
921///
922/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +0000923bool
924TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +0000925 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +0000926 if (InitFunctionInstantiation(New, Tmpl))
927 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000928
Douglas Gregor5545e162009-03-24 00:38:23 +0000929 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
930 New->setAccess(Tmpl->getAccess());
Anders Carlsson77b7f1d2009-05-14 22:15:41 +0000931 if (Tmpl->isVirtualAsWritten()) {
932 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +0000933 Record->setAggregate(false);
934 Record->setPOD(false);
Eli Friedman1d954f62009-08-15 21:55:26 +0000935 Record->setEmpty(false);
Douglas Gregor5545e162009-03-24 00:38:23 +0000936 Record->setPolymorphic(true);
937 }
Douglas Gregor5545e162009-03-24 00:38:23 +0000938 if (Tmpl->isPure()) {
939 New->setPure();
940 Record->setAbstract(true);
941 }
942
943 // FIXME: attributes
944 // FIXME: New needs a pointer to Tmpl
945 return false;
946}
Douglas Gregora58861f2009-05-13 20:28:22 +0000947
948/// \brief Instantiate the definition of the given function from its
949/// template.
950///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +0000951/// \param PointOfInstantiation the point at which the instantiation was
952/// required. Note that this is not precisely a "point of instantiation"
953/// for the function, but it's close.
954///
Douglas Gregora58861f2009-05-13 20:28:22 +0000955/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +0000956/// function template specialization or member function of a class template
957/// specialization.
958///
959/// \param Recursive if true, recursively instantiates any functions that
960/// are required by this instantiation.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000961void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +0000962 FunctionDecl *Function,
963 bool Recursive) {
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000964 if (Function->isInvalidDecl())
965 return;
966
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000967 assert(!Function->getBody() && "Already instantiated!");
Mike Stump1eb44332009-09-09 15:08:12 +0000968
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000969 // Find the function body that we'll be substituting.
Douglas Gregor1637be72009-06-26 00:10:03 +0000970 const FunctionDecl *PatternDecl = 0;
Douglas Gregor5ec178f2009-08-28 21:09:48 +0000971 if (FunctionTemplateDecl *Primary = Function->getPrimaryTemplate()) {
972 while (Primary->getInstantiatedFromMemberTemplate())
973 Primary = Primary->getInstantiatedFromMemberTemplate();
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Douglas Gregor1637be72009-06-26 00:10:03 +0000975 PatternDecl = Primary->getTemplatedDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000976 } else
Douglas Gregor1637be72009-06-26 00:10:03 +0000977 PatternDecl = Function->getInstantiatedFromMemberFunction();
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000978 Stmt *Pattern = 0;
979 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000980 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000981
982 if (!Pattern)
983 return;
984
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000985 // C++0x [temp.explicit]p9:
986 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +0000987 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000988 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +0000989 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000990 == TSK_ExplicitInstantiationDeclaration &&
991 PatternDecl->isOutOfLine() && !PatternDecl->isInline())
992 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000993
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000994 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
995 if (Inst)
996 return;
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000997
Douglas Gregorb33fe2f2009-06-30 17:20:14 +0000998 // If we're performing recursive template instantiation, create our own
999 // queue of pending implicit instantiations that we will instantiate later,
1000 // while we're still within our own instantiation context.
1001 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1002 if (Recursive)
1003 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001004
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001005 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
1006
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001007 // Introduce a new scope where local variable instantiations will be
1008 // recorded.
1009 LocalInstantiationScope Scope(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001010
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001011 // Introduce the instantiated function parameters into the local
1012 // instantiation scope.
1013 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
1014 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
1015 Function->getParamDecl(I));
1016
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001017 // Enter the scope of this instantiation. We don't use
1018 // PushDeclContext because we don't have a scope.
1019 DeclContext *PreviousContext = CurContext;
1020 CurContext = Function;
1021
Mike Stump1eb44332009-09-09 15:08:12 +00001022 MultiLevelTemplateArgumentList TemplateArgs =
Anders Carlsson09025312009-08-29 05:16:22 +00001023 getTemplateInstantiationArgs(Function);
1024
1025 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001026 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00001027 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
1028 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
1029 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001030 }
1031
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001032 // Instantiate the function body.
Anders Carlsson09025312009-08-29 05:16:22 +00001033 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001034
Douglas Gregor52604ab2009-09-11 21:19:12 +00001035 if (Body.isInvalid())
1036 Function->setInvalidDecl();
1037
Mike Stump1eb44332009-09-09 15:08:12 +00001038 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001039 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001040
1041 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00001042
1043 DeclGroupRef DG(Function);
1044 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001045
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001046 if (Recursive) {
1047 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001048 // instantiation of this template.
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001049 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001050
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001051 // Restore the set of pending implicit instantiations.
1052 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1053 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001054}
1055
1056/// \brief Instantiate the definition of the given variable from its
1057/// template.
1058///
Douglas Gregor7caa6822009-07-24 20:34:43 +00001059/// \param PointOfInstantiation the point at which the instantiation was
1060/// required. Note that this is not precisely a "point of instantiation"
1061/// for the function, but it's close.
1062///
1063/// \param Var the already-instantiated declaration of a static member
1064/// variable of a class template specialization.
1065///
1066/// \param Recursive if true, recursively instantiates any functions that
1067/// are required by this instantiation.
1068void Sema::InstantiateStaticDataMemberDefinition(
1069 SourceLocation PointOfInstantiation,
1070 VarDecl *Var,
1071 bool Recursive) {
1072 if (Var->isInvalidDecl())
1073 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001074
Douglas Gregor7caa6822009-07-24 20:34:43 +00001075 // Find the out-of-line definition of this static data member.
1076 // FIXME: Do we have to look for specializations separately?
1077 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
1078 bool FoundOutOfLineDef = false;
1079 assert(Def && "This data member was not instantiated from a template?");
Mike Stump1eb44332009-09-09 15:08:12 +00001080 assert(Def->isStaticDataMember() && "Not a static data member?");
1081 for (VarDecl::redecl_iterator RD = Def->redecls_begin(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00001082 RDEnd = Def->redecls_end();
1083 RD != RDEnd; ++RD) {
1084 if (RD->getLexicalDeclContext()->isFileContext()) {
1085 Def = *RD;
1086 FoundOutOfLineDef = true;
1087 }
1088 }
Mike Stump1eb44332009-09-09 15:08:12 +00001089
Douglas Gregor7caa6822009-07-24 20:34:43 +00001090 if (!FoundOutOfLineDef) {
1091 // We did not find an out-of-line definition of this static data member,
1092 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00001093 // instantiate this definition (or provide a specialization for it) in
1094 // another translation unit.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001095 return;
1096 }
1097
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001098 // FIXME: extern templates
Mike Stump1eb44332009-09-09 15:08:12 +00001099
Douglas Gregor7caa6822009-07-24 20:34:43 +00001100 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
1101 if (Inst)
1102 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001103
Douglas Gregor7caa6822009-07-24 20:34:43 +00001104 // If we're performing recursive template instantiation, create our own
1105 // queue of pending implicit instantiations that we will instantiate later,
1106 // while we're still within our own instantiation context.
1107 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1108 if (Recursive)
1109 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001110
Douglas Gregor7caa6822009-07-24 20:34:43 +00001111 // Enter the scope of this instantiation. We don't use
1112 // PushDeclContext because we don't have a scope.
1113 DeclContext *PreviousContext = CurContext;
1114 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001115
John McCallce3ff2b2009-08-25 22:02:44 +00001116 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00001117 getTemplateInstantiationArgs(Var)));
Mike Stump1eb44332009-09-09 15:08:12 +00001118
Douglas Gregor7caa6822009-07-24 20:34:43 +00001119 CurContext = PreviousContext;
1120
1121 if (Var) {
1122 DeclGroupRef DG(Var);
1123 Consumer.HandleTopLevelDecl(DG);
1124 }
Mike Stump1eb44332009-09-09 15:08:12 +00001125
Douglas Gregor7caa6822009-07-24 20:34:43 +00001126 if (Recursive) {
1127 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001128 // instantiation of this template.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001129 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001130
Douglas Gregor7caa6822009-07-24 20:34:43 +00001131 // Restore the set of pending implicit instantiations.
1132 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001133 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001134}
Douglas Gregor815215d2009-05-27 05:35:12 +00001135
Anders Carlsson09025312009-08-29 05:16:22 +00001136void
1137Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
1138 const CXXConstructorDecl *Tmpl,
1139 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001140
Anders Carlsson09025312009-08-29 05:16:22 +00001141 llvm::SmallVector<MemInitTy*, 4> NewInits;
1142
1143 // Instantiate all the initializers.
1144 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00001145 InitsEnd = Tmpl->init_end();
1146 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00001147 CXXBaseOrMemberInitializer *Init = *Inits;
1148
1149 ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001150
Anders Carlsson09025312009-08-29 05:16:22 +00001151 // Instantiate all the arguments.
1152 for (ExprIterator Args = Init->arg_begin(), ArgsEnd = Init->arg_end();
1153 Args != ArgsEnd; ++Args) {
1154 OwningExprResult NewArg = SubstExpr(*Args, TemplateArgs);
1155
1156 if (NewArg.isInvalid())
1157 New->setInvalidDecl();
1158 else
1159 NewArgs.push_back(NewArg.takeAs<Expr>());
1160 }
1161
1162 MemInitResult NewInit;
1163
1164 if (Init->isBaseInitializer()) {
Eli Friedmanc5573a82009-08-29 22:22:07 +00001165 QualType BaseType(Init->getBaseClass(), 0);
1166 BaseType = SubstType(BaseType, TemplateArgs, Init->getSourceLocation(),
1167 New->getDeclName());
Anders Carlsson09025312009-08-29 05:16:22 +00001168
1169 NewInit = BuildBaseInitializer(BaseType,
Mike Stump1eb44332009-09-09 15:08:12 +00001170 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001171 NewArgs.size(),
1172 Init->getSourceLocation(),
1173 Init->getRParenLoc(),
1174 New->getParent());
1175 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001176 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00001177
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001178 // Is this an anonymous union?
1179 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregore95b4092009-09-16 18:34:49 +00001180 Member = cast<FieldDecl>(FindInstantiatedDecl(UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001181 else
Douglas Gregore95b4092009-09-16 18:34:49 +00001182 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMember(),
1183 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00001184
1185 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001186 NewArgs.size(),
1187 Init->getSourceLocation(),
1188 Init->getRParenLoc());
1189 }
1190
1191 if (NewInit.isInvalid())
1192 New->setInvalidDecl();
1193 else {
1194 // FIXME: It would be nice if ASTOwningVector had a release function.
1195 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001196
Anders Carlsson09025312009-08-29 05:16:22 +00001197 NewInits.push_back((MemInitTy *)NewInit.get());
1198 }
1199 }
Mike Stump1eb44332009-09-09 15:08:12 +00001200
Anders Carlsson09025312009-08-29 05:16:22 +00001201 // Assign all the initializers to the new constructor.
Mike Stump1eb44332009-09-09 15:08:12 +00001202 ActOnMemInitializers(DeclPtrTy::make(New),
Anders Carlsson09025312009-08-29 05:16:22 +00001203 /*FIXME: ColonLoc */
1204 SourceLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001205 NewInits.data(), NewInits.size());
Anders Carlsson09025312009-08-29 05:16:22 +00001206}
1207
John McCall52a575a2009-08-29 08:11:13 +00001208// TODO: this could be templated if the various decl types used the
1209// same method name.
1210static bool isInstantiationOf(ClassTemplateDecl *Pattern,
1211 ClassTemplateDecl *Instance) {
1212 Pattern = Pattern->getCanonicalDecl();
1213
1214 do {
1215 Instance = Instance->getCanonicalDecl();
1216 if (Pattern == Instance) return true;
1217 Instance = Instance->getInstantiatedFromMemberTemplate();
1218 } while (Instance);
1219
1220 return false;
1221}
1222
1223static bool isInstantiationOf(CXXRecordDecl *Pattern,
1224 CXXRecordDecl *Instance) {
1225 Pattern = Pattern->getCanonicalDecl();
1226
1227 do {
1228 Instance = Instance->getCanonicalDecl();
1229 if (Pattern == Instance) return true;
1230 Instance = Instance->getInstantiatedFromMemberClass();
1231 } while (Instance);
1232
1233 return false;
1234}
1235
1236static bool isInstantiationOf(FunctionDecl *Pattern,
1237 FunctionDecl *Instance) {
1238 Pattern = Pattern->getCanonicalDecl();
1239
1240 do {
1241 Instance = Instance->getCanonicalDecl();
1242 if (Pattern == Instance) return true;
1243 Instance = Instance->getInstantiatedFromMemberFunction();
1244 } while (Instance);
1245
1246 return false;
1247}
1248
1249static bool isInstantiationOf(EnumDecl *Pattern,
1250 EnumDecl *Instance) {
1251 Pattern = Pattern->getCanonicalDecl();
1252
1253 do {
1254 Instance = Instance->getCanonicalDecl();
1255 if (Pattern == Instance) return true;
1256 Instance = Instance->getInstantiatedFromMemberEnum();
1257 } while (Instance);
1258
1259 return false;
1260}
1261
Anders Carlsson0d8df782009-08-29 19:37:28 +00001262static bool isInstantiationOf(UnresolvedUsingDecl *Pattern,
1263 UsingDecl *Instance,
1264 ASTContext &C) {
1265 return C.getInstantiatedFromUnresolvedUsingDecl(Instance) == Pattern;
1266}
1267
John McCall52a575a2009-08-29 08:11:13 +00001268static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
1269 VarDecl *Instance) {
1270 assert(Instance->isStaticDataMember());
1271
1272 Pattern = Pattern->getCanonicalDecl();
1273
1274 do {
1275 Instance = Instance->getCanonicalDecl();
1276 if (Pattern == Instance) return true;
1277 Instance = Instance->getInstantiatedFromStaticDataMember();
1278 } while (Instance);
1279
1280 return false;
1281}
1282
Douglas Gregor815215d2009-05-27 05:35:12 +00001283static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00001284 if (D->getKind() != Other->getKind()) {
1285 if (UnresolvedUsingDecl *UUD = dyn_cast<UnresolvedUsingDecl>(D)) {
1286 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
1287 return isInstantiationOf(UUD, UD, Ctx);
1288 }
1289 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001290
Anders Carlsson0d8df782009-08-29 19:37:28 +00001291 return false;
1292 }
Mike Stump1eb44332009-09-09 15:08:12 +00001293
John McCall52a575a2009-08-29 08:11:13 +00001294 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
1295 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001296
John McCall52a575a2009-08-29 08:11:13 +00001297 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
1298 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00001299
John McCall52a575a2009-08-29 08:11:13 +00001300 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
1301 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00001302
Douglas Gregor7caa6822009-07-24 20:34:43 +00001303 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00001304 if (Var->isStaticDataMember())
1305 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
1306
1307 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
1308 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00001309
Anders Carlssond8b285f2009-09-01 04:26:58 +00001310 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
1311 if (!Field->getDeclName()) {
1312 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00001313 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00001314 cast<FieldDecl>(D);
1315 }
1316 }
Mike Stump1eb44332009-09-09 15:08:12 +00001317
Douglas Gregor815215d2009-05-27 05:35:12 +00001318 return D->getDeclName() && isa<NamedDecl>(Other) &&
1319 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
1320}
1321
1322template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00001323static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00001324 NamedDecl *D,
1325 ForwardIterator first,
1326 ForwardIterator last) {
1327 for (; first != last; ++first)
1328 if (isInstantiationOf(Ctx, D, *first))
1329 return cast<NamedDecl>(*first);
1330
1331 return 0;
1332}
1333
John McCall02cace72009-08-28 07:59:38 +00001334/// \brief Finds the instantiation of the given declaration context
1335/// within the current instantiation.
1336///
1337/// \returns NULL if there was an error
Douglas Gregore95b4092009-09-16 18:34:49 +00001338DeclContext *Sema::FindInstantiatedContext(DeclContext* DC,
1339 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00001340 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00001341 Decl* ID = FindInstantiatedDecl(D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00001342 return cast_or_null<DeclContext>(ID);
1343 } else return DC;
1344}
1345
Douglas Gregored961e72009-05-27 17:54:46 +00001346/// \brief Find the instantiation of the given declaration within the
1347/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00001348///
1349/// This routine is intended to be used when \p D is a declaration
1350/// referenced from within a template, that needs to mapped into the
1351/// corresponding declaration within an instantiation. For example,
1352/// given:
1353///
1354/// \code
1355/// template<typename T>
1356/// struct X {
1357/// enum Kind {
1358/// KnownValue = sizeof(T)
1359/// };
1360///
1361/// bool getKind() const { return KnownValue; }
1362/// };
1363///
1364/// template struct X<int>;
1365/// \endcode
1366///
1367/// In the instantiation of X<int>::getKind(), we need to map the
1368/// EnumConstantDecl for KnownValue (which refers to
1369/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00001370/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
1371/// this mapping from within the instantiation of X<int>.
Douglas Gregore95b4092009-09-16 18:34:49 +00001372NamedDecl *Sema::FindInstantiatedDecl(NamedDecl *D,
1373 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor44c73842009-09-01 17:53:10 +00001374 if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D)) {
1375 // Transform all of the elements of the overloaded function set.
Mike Stump1eb44332009-09-09 15:08:12 +00001376 OverloadedFunctionDecl *Result
Douglas Gregor44c73842009-09-01 17:53:10 +00001377 = OverloadedFunctionDecl::Create(Context, CurContext, Ovl->getDeclName());
Mike Stump1eb44332009-09-09 15:08:12 +00001378
Douglas Gregor44c73842009-09-01 17:53:10 +00001379 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
1380 FEnd = Ovl->function_end();
1381 F != FEnd; ++F) {
1382 Result->addOverload(
Douglas Gregore95b4092009-09-16 18:34:49 +00001383 AnyFunctionDecl::getFromNamedDecl(FindInstantiatedDecl(*F,
1384 TemplateArgs)));
Douglas Gregor44c73842009-09-01 17:53:10 +00001385 }
Mike Stump1eb44332009-09-09 15:08:12 +00001386
Douglas Gregor44c73842009-09-01 17:53:10 +00001387 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001388 }
1389
Douglas Gregor815215d2009-05-27 05:35:12 +00001390 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor2bba76b2009-05-27 17:07:49 +00001391 if (isa<ParmVarDecl>(D) || ParentDC->isFunctionOrMethod()) {
1392 // D is a local of some kind. Look into the map of local
1393 // declarations to their instantiations.
1394 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
1395 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001396
Douglas Gregore95b4092009-09-16 18:34:49 +00001397 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
1398 if (!Record->isDependentContext())
1399 return D;
1400
1401 // If the RecordDecl is actually the injected-class-name or a "templated"
1402 // declaration for a class template or class template partial
1403 // specialization, substitute into the injected-class-name of the
1404 // class template or partial specialization to find the new DeclContext.
1405 QualType T;
1406 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
1407
1408 if (ClassTemplate) {
1409 T = ClassTemplate->getInjectedClassNameType(Context);
1410 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
1411 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1412 T = Context.getTypeDeclType(Record);
1413 ClassTemplate = PartialSpec->getSpecializedTemplate();
1414 }
1415
1416 if (!T.isNull()) {
1417 // Substitute into the injected-class-name to get the type corresponding
1418 // to the instantiation we want. This substitution should never fail,
1419 // since we know we can instantiate the injected-class-name or we wouldn't
1420 // have gotten to the injected-class-name!
1421 // FIXME: Can we use the CurrentInstantiationScope to avoid this extra
1422 // instantiation in the common case?
1423 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
1424 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
1425
1426 if (!T->isDependentType()) {
1427 assert(T->isRecordType() && "Instantiation must produce a record type");
1428 return T->getAs<RecordType>()->getDecl();
1429 }
1430
1431 // We are performing "partial" template instantiation to create the
1432 // member declarations for the members of a class template
1433 // specialization. Therefore, D is actually referring to something in
1434 // the current instantiation. Look through the current context,
1435 // which contains actual instantiations, to find the instantiation of
1436 // the "current instantiation" that D refers to.
Mike Stump1eb44332009-09-09 15:08:12 +00001437 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00001438 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001439 if (ClassTemplateSpecializationDecl *Spec
John McCall52a575a2009-08-29 08:11:13 +00001440 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00001441 if (isInstantiationOf(ClassTemplate,
1442 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00001443 return Spec;
1444 }
1445
Mike Stump1eb44332009-09-09 15:08:12 +00001446 assert(false &&
John McCall52a575a2009-08-29 08:11:13 +00001447 "Unable to find declaration for the current instantiation");
Douglas Gregore95b4092009-09-16 18:34:49 +00001448 return Record;
John McCall52a575a2009-08-29 08:11:13 +00001449 }
Douglas Gregore95b4092009-09-16 18:34:49 +00001450
1451 // Fall through to deal with other dependent record types (e.g.,
1452 // anonymous unions in class templates).
1453 }
John McCall52a575a2009-08-29 08:11:13 +00001454
Douglas Gregore95b4092009-09-16 18:34:49 +00001455 if (!ParentDC->isDependentContext())
1456 return D;
1457
1458 ParentDC = FindInstantiatedContext(ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001459 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00001460 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001461
Douglas Gregor815215d2009-05-27 05:35:12 +00001462 if (ParentDC != D->getDeclContext()) {
1463 // We performed some kind of instantiation in the parent context,
1464 // so now we need to look into the instantiated parent context to
1465 // find the instantiation of the declaration D.
1466 NamedDecl *Result = 0;
1467 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001468 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00001469 Result = findInstantiationOf(Context, D, Found.first, Found.second);
1470 } else {
1471 // Since we don't have a name for the entity we're looking for,
1472 // our only option is to walk through all of the declarations to
1473 // find that name. This will occur in a few cases:
1474 //
1475 // - anonymous struct/union within a template
1476 // - unnamed class/struct/union/enum within a template
1477 //
1478 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00001479 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001480 ParentDC->decls_begin(),
1481 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00001482 }
Mike Stump1eb44332009-09-09 15:08:12 +00001483
Douglas Gregor815215d2009-05-27 05:35:12 +00001484 assert(Result && "Unable to find instantiation of declaration!");
1485 D = Result;
1486 }
1487
Douglas Gregor815215d2009-05-27 05:35:12 +00001488 return D;
1489}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001490
Mike Stump1eb44332009-09-09 15:08:12 +00001491/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001492/// instantiations we have seen until this point.
1493void Sema::PerformPendingImplicitInstantiations() {
1494 while (!PendingImplicitInstantiations.empty()) {
1495 PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front();
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001496 PendingImplicitInstantiations.pop_front();
Mike Stump1eb44332009-09-09 15:08:12 +00001497
Douglas Gregor7caa6822009-07-24 20:34:43 +00001498 // Instantiate function definitions
1499 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001500 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001501 Function->getLocation(), *this,
1502 Context.getSourceManager(),
1503 "instantiating function definition");
Mike Stump1eb44332009-09-09 15:08:12 +00001504
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001505 if (!Function->getBody())
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001506 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001507 continue;
1508 }
Mike Stump1eb44332009-09-09 15:08:12 +00001509
Douglas Gregor7caa6822009-07-24 20:34:43 +00001510 // Instantiate static data member definitions.
1511 VarDecl *Var = cast<VarDecl>(Inst.first);
1512 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001513
Mike Stump1eb44332009-09-09 15:08:12 +00001514 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001515 Var->getLocation(), *this,
1516 Context.getSourceManager(),
1517 "instantiating static data member "
1518 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00001519
Douglas Gregor7caa6822009-07-24 20:34:43 +00001520 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001521 }
1522}