blob: fcacb4a3f000cdde70176bba5d138a13127ccafb [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 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);
Anders Carlsson0dde18e2009-08-28 15:18:15 +000064 Decl *VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D);
Mike Stump1eb44332009-09-09 15:08:12 +000065
Douglas Gregor8dbc2692009-03-17 21:15:40 +000066 // Base case. FIXME: Remove once we can instantiate everything.
Mike Stump1eb44332009-09-09 15:08:12 +000067 Decl *VisitDecl(Decl *) {
Douglas Gregor3d7a12a2009-03-25 23:32:15 +000068 assert(false && "Template instantiation of unknown declaration kind!");
Douglas Gregor8dbc2692009-03-17 21:15:40 +000069 return 0;
70 }
Douglas Gregor5545e162009-03-24 00:38:23 +000071
John McCallfd810b12009-08-14 02:03:10 +000072 const LangOptions &getLangOptions() {
73 return SemaRef.getLangOptions();
74 }
75
Douglas Gregor5545e162009-03-24 00:38:23 +000076 // Helper functions for instantiating methods.
John McCallce3ff2b2009-08-25 22:02:44 +000077 QualType SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +000078 llvm::SmallVectorImpl<ParmVarDecl *> &Params);
Douglas Gregore53060f2009-06-25 22:08:12 +000079 bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
Douglas Gregor5545e162009-03-24 00:38:23 +000080 bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
John McCalle29ba202009-08-20 01:44:21 +000081
82 TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +000083 SubstTemplateParams(TemplateParameterList *List);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000084 };
85}
86
Douglas Gregor4f722be2009-03-25 15:45:12 +000087Decl *
88TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
89 assert(false && "Translation units cannot be instantiated");
90 return D;
91}
92
93Decl *
94TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
95 assert(false && "Namespaces cannot be instantiated");
96 return D;
97}
98
Douglas Gregor8dbc2692009-03-17 21:15:40 +000099Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
100 bool Invalid = false;
101 QualType T = D->getUnderlyingType();
102 if (T->isDependentType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000103 T = SemaRef.SubstType(T, TemplateArgs,
John McCallce3ff2b2009-08-25 22:02:44 +0000104 D->getLocation(), D->getDeclName());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000105 if (T.isNull()) {
106 Invalid = true;
107 T = SemaRef.Context.IntTy;
108 }
109 }
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000111 // Create the new typedef
112 TypedefDecl *Typedef
113 = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
114 D->getIdentifier(), T);
115 if (Invalid)
116 Typedef->setInvalidDecl();
117
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000118 Owner->addDecl(Typedef);
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000120 return Typedef;
121}
122
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000123Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000124 // Do substitution on the type of the declaration
125 QualType T = SemaRef.SubstType(D->getType(), TemplateArgs,
126 D->getTypeSpecStartLoc(),
127 D->getDeclName());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000128 if (T.isNull())
129 return 0;
130
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000131 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000132 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
133 D->getLocation(), D->getIdentifier(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000134 T, D->getDeclaratorInfo(),
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000135 D->getStorageClass());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000136 Var->setThreadSpecified(D->isThreadSpecified());
137 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
138 Var->setDeclaredInCondition(D->isDeclaredInCondition());
Mike Stump1eb44332009-09-09 15:08:12 +0000139
140 // If we are instantiating a static data member defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000141 // out-of-line, the instantiation will have the same lexical
142 // context (which will be a namespace scope) as the template.
143 if (D->isOutOfLine())
144 Var->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Mike Stump390b4cc2009-05-16 07:39:55 +0000146 // FIXME: In theory, we could have a previous declaration for variables that
147 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000148 bool Redeclaration = false;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000149 SemaRef.CheckVariableDeclaration(Var, 0, Redeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Douglas Gregor7caa6822009-07-24 20:34:43 +0000151 if (D->isOutOfLine()) {
152 D->getLexicalDeclContext()->addDecl(Var);
153 Owner->makeDeclVisibleInContext(Var);
154 } else {
155 Owner->addDecl(Var);
156 }
Mike Stump1eb44332009-09-09 15:08:12 +0000157
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000158 if (D->getInit()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000159 OwningExprResult Init
John McCallce3ff2b2009-08-25 22:02:44 +0000160 = SemaRef.SubstExpr(D->getInit(), TemplateArgs);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000161 if (Init.isInvalid())
162 Var->setInvalidDecl();
Douglas Gregor83ddad32009-08-26 21:14:46 +0000163 else if (ParenListExpr *PLE = dyn_cast<ParenListExpr>((Expr *)Init.get())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000164 // FIXME: We're faking all of the comma locations, which is suboptimal.
Douglas Gregor83ddad32009-08-26 21:14:46 +0000165 // Do we even need these comma locations?
166 llvm::SmallVector<SourceLocation, 4> FakeCommaLocs;
167 if (PLE->getNumExprs() > 0) {
168 FakeCommaLocs.reserve(PLE->getNumExprs() - 1);
169 for (unsigned I = 0, N = PLE->getNumExprs() - 1; I != N; ++I) {
170 Expr *E = PLE->getExpr(I)->Retain();
171 FakeCommaLocs.push_back(
172 SemaRef.PP.getLocForEndOfToken(E->getLocEnd()));
173 }
Douglas Gregore9f8eb62009-08-26 23:26:04 +0000174 PLE->getExpr(PLE->getNumExprs() - 1)->Retain();
Douglas Gregor83ddad32009-08-26 21:14:46 +0000175 }
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Douglas Gregor83ddad32009-08-26 21:14:46 +0000177 // Add the direct initializer to the declaration.
178 SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
Mike Stump1eb44332009-09-09 15:08:12 +0000179 PLE->getLParenLoc(),
Douglas Gregor83ddad32009-08-26 21:14:46 +0000180 Sema::MultiExprArg(SemaRef,
181 (void**)PLE->getExprs(),
182 PLE->getNumExprs()),
183 FakeCommaLocs.data(),
184 PLE->getRParenLoc());
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Douglas Gregor83ddad32009-08-26 21:14:46 +0000186 // When Init is destroyed, it will destroy the instantiated ParenListExpr;
187 // we've explicitly retained all of its subexpressions already.
188 } else
Chris Lattnerb28317a2009-03-28 19:18:32 +0000189 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init),
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000190 D->hasCXXDirectInitializer());
Douglas Gregor65b90052009-07-27 17:43:39 +0000191 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
192 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000193
Douglas Gregor7caa6822009-07-24 20:34:43 +0000194 // Link instantiations of static data members back to the template from
195 // which they were instantiated.
196 if (Var->isStaticDataMember())
197 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D);
Mike Stump1eb44332009-09-09 15:08:12 +0000198
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000199 return Var;
200}
201
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000202Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
203 bool Invalid = false;
204 QualType T = D->getType();
205 if (T->isDependentType()) {
John McCallce3ff2b2009-08-25 22:02:44 +0000206 T = SemaRef.SubstType(T, TemplateArgs,
207 D->getLocation(), D->getDeclName());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000208 if (!T.isNull() && T->isFunctionType()) {
209 // C++ [temp.arg.type]p3:
210 // If a declaration acquires a function type through a type
211 // dependent on a template-parameter and this causes a
212 // declaration that does not use the syntactic form of a
213 // function declarator to have function type, the program is
214 // ill-formed.
215 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
216 << T;
217 T = QualType();
218 Invalid = true;
219 }
220 }
221
222 Expr *BitWidth = D->getBitWidth();
223 if (Invalid)
224 BitWidth = 0;
225 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000226 // The bit-width expression is not potentially evaluated.
227 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000228
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000229 OwningExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000230 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000231 if (InstantiatedBitWidth.isInvalid()) {
232 Invalid = true;
233 BitWidth = 0;
234 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000235 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000236 }
237
238 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), T,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000239 D->getDeclaratorInfo(),
Mike Stump1eb44332009-09-09 15:08:12 +0000240 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000241 D->getLocation(),
242 D->isMutable(),
243 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000244 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000245 D->getAccess(),
246 0);
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000247 if (!Field)
248 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000249
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000250 if (Invalid)
251 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000252
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000253 if (!Field->getDeclName()) {
254 // Keep track of where this decl came from.
255 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000256 }
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000258 Field->setImplicit(D->isImplicit());
259 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000260
261 return Field;
262}
263
John McCall02cace72009-08-28 07:59:38 +0000264Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
265 FriendDecl::FriendUnion FU;
266
267 // Handle friend type expressions by simply substituting template
268 // parameters into the pattern type.
269 if (Type *Ty = D->getFriendType()) {
270 QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs,
271 D->getLocation(), DeclarationName());
272 if (T.isNull()) return 0;
273
274 assert(getLangOptions().CPlusPlus0x || T->isRecordType());
275 FU = T.getTypePtr();
276
277 // Handle everything else by appropriate substitution.
278 } else {
279 NamedDecl *ND = D->getFriendDecl();
280 assert(ND && "friend decl must be a decl or a type!");
281
282 Decl *NewND = Visit(ND);
283 if (!NewND) return 0;
284
285 FU = cast<NamedDecl>(NewND);
John McCallfd810b12009-08-14 02:03:10 +0000286 }
Mike Stump1eb44332009-09-09 15:08:12 +0000287
John McCall02cace72009-08-28 07:59:38 +0000288 FriendDecl *FD =
289 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU,
290 D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000291 FD->setAccess(AS_public);
John McCall02cace72009-08-28 07:59:38 +0000292 Owner->addDecl(FD);
293 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000294}
295
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000296Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
297 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000298
Douglas Gregorac7610d2009-06-22 20:57:11 +0000299 // The expression in a static assertion is not potentially evaluated.
300 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000302 OwningExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000303 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000304 if (InstantiatedAssertExpr.isInvalid())
305 return 0;
306
Douglas Gregor43d9d922009-08-08 01:41:12 +0000307 OwningExprResult Message(SemaRef, D->getMessage());
308 D->getMessage()->Retain();
Mike Stump1eb44332009-09-09 15:08:12 +0000309 Decl *StaticAssert
310 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
Chris Lattnerb28317a2009-03-28 19:18:32 +0000311 move(InstantiatedAssertExpr),
312 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000313 return StaticAssert;
314}
315
316Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000317 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000318 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000319 D->getTagKeywordLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000320 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000321 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000322 Enum->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000323 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000324 Enum->startDefinition();
325
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000326 llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000327
328 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000329 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
330 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000331 EC != ECEnd; ++EC) {
332 // The specified value for the enumerator.
333 OwningExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000334 if (Expr *UninstValue = EC->getInitExpr()) {
335 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000336 EnterExpressionEvaluationContext Unevaluated(SemaRef,
Douglas Gregorac7610d2009-06-22 20:57:11 +0000337 Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000338
John McCallce3ff2b2009-08-25 22:02:44 +0000339 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000340 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000341
342 // Drop the initial value and continue.
343 bool isInvalid = false;
344 if (Value.isInvalid()) {
345 Value = SemaRef.Owned((Expr *)0);
346 isInvalid = true;
347 }
348
Mike Stump1eb44332009-09-09 15:08:12 +0000349 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000350 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
351 EC->getLocation(), EC->getIdentifier(),
352 move(Value));
353
354 if (isInvalid) {
355 if (EnumConst)
356 EnumConst->setInvalidDecl();
357 Enum->setInvalidDecl();
358 }
359
360 if (EnumConst) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000361 Enum->addDecl(EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000362 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000363 LastEnumConst = EnumConst;
364 }
365 }
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000367 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000368 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000369 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
370 Sema::DeclPtrTy::make(Enum),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000371 &Enumerators[0], Enumerators.size(),
372 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000373
374 return Enum;
375}
376
Douglas Gregor6477b692009-03-25 15:04:13 +0000377Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
378 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
379 return 0;
380}
381
John McCalle29ba202009-08-20 01:44:21 +0000382Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
383 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000384 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000385 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000386 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000387
388 CXXRecordDecl *Pattern = D->getTemplatedDecl();
389 CXXRecordDecl *RecordInst
390 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner,
391 Pattern->getLocation(), Pattern->getIdentifier(),
392 Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL);
393
394 ClassTemplateDecl *Inst
395 = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
396 D->getIdentifier(), InstParams, RecordInst, 0);
397 RecordInst->setDescribedClassTemplate(Inst);
398 Inst->setAccess(D->getAccess());
399 Inst->setInstantiatedFromMemberTemplate(D);
400
401 Owner->addDecl(Inst);
402 return Inst;
403}
404
Douglas Gregord60e1052009-08-27 16:57:43 +0000405Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000406TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
407 ClassTemplatePartialSpecializationDecl *D) {
408 assert(false &&"Partial specializations of member templates are unsupported");
409 return 0;
410}
411
412Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000413TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000414 // FIXME: Dig out the out-of-line definition of this function template?
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Douglas Gregord60e1052009-08-27 16:57:43 +0000416 TemplateParameterList *TempParams = D->getTemplateParameters();
417 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000418 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000419 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000420
421 // FIXME: Handle instantiation of nested function templates that aren't
Douglas Gregord60e1052009-08-27 16:57:43 +0000422 // member function templates. This could happen inside a FriendDecl.
423 assert(isa<CXXMethodDecl>(D->getTemplatedDecl()));
Mike Stump1eb44332009-09-09 15:08:12 +0000424 CXXMethodDecl *InstMethod
Douglas Gregord60e1052009-08-27 16:57:43 +0000425 = cast_or_null<CXXMethodDecl>(
Mike Stump1eb44332009-09-09 15:08:12 +0000426 VisitCXXMethodDecl(cast<CXXMethodDecl>(D->getTemplatedDecl()),
Douglas Gregord60e1052009-08-27 16:57:43 +0000427 InstParams));
428 if (!InstMethod)
429 return 0;
430
Mike Stump1eb44332009-09-09 15:08:12 +0000431 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000432 // template from which it was instantiated.
433 FunctionTemplateDecl *InstTemplate = InstMethod->getDescribedFunctionTemplate();
434 assert(InstTemplate && "VisitCXXMethodDecl didn't create a template!");
435 InstTemplate->setInstantiatedFromMemberTemplate(D);
436 Owner->addDecl(InstTemplate);
437 return InstTemplate;
438}
439
Douglas Gregord475b8d2009-03-25 21:17:03 +0000440Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
441 CXXRecordDecl *PrevDecl = 0;
442 if (D->isInjectedClassName())
443 PrevDecl = cast<CXXRecordDecl>(Owner);
444
445 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000446 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000447 D->getLocation(), D->getIdentifier(),
448 D->getTagKeywordLoc(), PrevDecl);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000449 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000450 // FIXME: Check against AS_none is an ugly hack to work around the issue that
451 // the tag decls introduced by friend class declarations don't have an access
452 // specifier. Remove once this area of the code gets sorted out.
453 if (D->getAccess() != AS_none)
454 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000455 if (!D->isInjectedClassName())
456 Record->setInstantiationOfMemberClass(D);
457
John McCall02cace72009-08-28 07:59:38 +0000458 // If the original function was part of a friend declaration,
459 // inherit its namespace state.
460 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
461 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
462
Anders Carlssond8b285f2009-09-01 04:26:58 +0000463 Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
464
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000465 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000466 return Record;
467}
468
John McCall02cace72009-08-28 07:59:38 +0000469/// Normal class members are of more specific types and therefore
470/// don't make it here. This function serves two purposes:
471/// 1) instantiating function templates
472/// 2) substituting friend declarations
473/// FIXME: preserve function definitions in case #2
Douglas Gregore53060f2009-06-25 22:08:12 +0000474Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000475 // Check whether there is already a function template specialization for
476 // this declaration.
477 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
478 void *InsertPos = 0;
479 if (FunctionTemplate) {
480 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000481 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000482 TemplateArgs.getInnermost().getFlatArgumentList(),
483 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor828e2262009-07-29 16:09:57 +0000484 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000485
486 FunctionTemplateSpecializationInfo *Info
487 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor127102b2009-06-29 20:59:39 +0000488 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Douglas Gregor127102b2009-06-29 20:59:39 +0000490 // If we already have a function template specialization, return it.
491 if (Info)
492 return Info->Function;
493 }
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Douglas Gregore53060f2009-06-25 22:08:12 +0000495 Sema::LocalInstantiationScope Scope(SemaRef);
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Douglas Gregore53060f2009-06-25 22:08:12 +0000497 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000498 QualType T = SubstFunctionType(D, Params);
Douglas Gregore53060f2009-06-25 22:08:12 +0000499 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000500 return 0;
John McCallfd810b12009-08-14 02:03:10 +0000501
Douglas Gregore53060f2009-06-25 22:08:12 +0000502 // Build the instantiated method declaration.
Douglas Gregore95b4092009-09-16 18:34:49 +0000503 DeclContext *DC = SemaRef.FindInstantiatedContext(D->getDeclContext(),
504 TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +0000505 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +0000506 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000507 D->getDeclName(), T, D->getDeclaratorInfo(),
508 D->getStorageClass(),
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000509 D->isInline(), D->hasWrittenPrototype());
John McCall02cace72009-08-28 07:59:38 +0000510 Function->setLexicalDeclContext(Owner);
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Douglas Gregore53060f2009-06-25 22:08:12 +0000512 // Attach the parameters
513 for (unsigned P = 0; P < Params.size(); ++P)
514 Params[P]->setOwningFunction(Function);
515 Function->setParams(SemaRef.Context, Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +0000516
517 // If the original function was part of a friend declaration,
518 // inherit its namespace state and add it to the owner.
519 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind()) {
520 bool WasDeclared = (FOK == Decl::FOK_Declared);
521 Function->setObjectOfFriendDecl(WasDeclared);
522 if (!Owner->isDependentContext())
John McCallab88d972009-08-31 22:39:49 +0000523 DC->makeDeclVisibleInContext(Function, /* Recoverable = */ false);
John McCallf181d8a2009-08-29 03:16:09 +0000524
Douglas Gregor2db32322009-10-07 23:56:10 +0000525 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
John McCall02cace72009-08-28 07:59:38 +0000526 }
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Douglas Gregore53060f2009-06-25 22:08:12 +0000528 if (InitFunctionInstantiation(Function, D))
529 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Douglas Gregore53060f2009-06-25 22:08:12 +0000531 bool Redeclaration = false;
532 bool OverloadableAttrRequired = false;
533 NamedDecl *PrevDecl = 0;
534 SemaRef.CheckFunctionDeclaration(Function, PrevDecl, Redeclaration,
535 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000536
Douglas Gregor127102b2009-06-29 20:59:39 +0000537 if (FunctionTemplate) {
538 // Record this function template specialization.
539 Function->setFunctionTemplateSpecialization(SemaRef.Context,
540 FunctionTemplate,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000541 &TemplateArgs.getInnermost(),
Douglas Gregor127102b2009-06-29 20:59:39 +0000542 InsertPos);
John McCallfd810b12009-08-14 02:03:10 +0000543 }
544
Douglas Gregore53060f2009-06-25 22:08:12 +0000545 return Function;
546}
547
Douglas Gregord60e1052009-08-27 16:57:43 +0000548Decl *
549TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
550 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +0000551 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
552 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +0000553 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +0000554 // We are creating a function template specialization from a function
555 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +0000556 // specialization for this particular set of template arguments.
Douglas Gregor6b906862009-08-21 00:16:32 +0000557 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000558 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000559 TemplateArgs.getInnermost().getFlatArgumentList(),
560 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000561 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000562
563 FunctionTemplateSpecializationInfo *Info
564 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor6b906862009-08-21 00:16:32 +0000565 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Douglas Gregor6b906862009-08-21 00:16:32 +0000567 // If we already have a function template specialization, return it.
568 if (Info)
569 return Info->Function;
570 }
571
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000572 Sema::LocalInstantiationScope Scope(SemaRef);
573
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000574 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000575 QualType T = SubstFunctionType(D, Params);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000576 if (T.isNull())
577 return 0;
578
579 // Build the instantiated method declaration.
580 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
Douglas Gregordec06662009-08-21 18:42:58 +0000581 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000582
Douglas Gregordec06662009-08-21 18:42:58 +0000583 DeclarationName Name = D->getDeclName();
Douglas Gregor17e32f32009-08-21 22:43:28 +0000584 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregordec06662009-08-21 18:42:58 +0000585 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
586 Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
587 SemaRef.Context.getCanonicalType(ClassTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000588 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
589 Constructor->getLocation(),
590 Name, T,
Douglas Gregor17e32f32009-08-21 22:43:28 +0000591 Constructor->getDeclaratorInfo(),
Mike Stump1eb44332009-09-09 15:08:12 +0000592 Constructor->isExplicit(),
Douglas Gregor17e32f32009-08-21 22:43:28 +0000593 Constructor->isInline(), false);
594 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
595 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
596 Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
597 SemaRef.Context.getCanonicalType(ClassTy));
598 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
599 Destructor->getLocation(), Name,
600 T, Destructor->isInline(), false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000601 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000602 CanQualType ConvTy
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000603 = SemaRef.Context.getCanonicalType(
John McCall183700f2009-09-21 23:43:11 +0000604 T->getAs<FunctionType>()->getResultType());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000605 Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
606 ConvTy);
607 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
608 Conversion->getLocation(), Name,
609 T, Conversion->getDeclaratorInfo(),
Mike Stump1eb44332009-09-09 15:08:12 +0000610 Conversion->isInline(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000611 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +0000612 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000613 Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
Douglas Gregordec06662009-08-21 18:42:58 +0000614 D->getDeclName(), T, D->getDeclaratorInfo(),
615 D->isStatic(), D->isInline());
616 }
Douglas Gregor6b906862009-08-21 00:16:32 +0000617
Douglas Gregord60e1052009-08-27 16:57:43 +0000618 if (TemplateParams) {
619 // Our resulting instantiation is actually a function template, since we
620 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +0000621 //
Douglas Gregord60e1052009-08-27 16:57:43 +0000622 // template<typename T>
623 // struct X {
624 // template<typename U> void f(T, U);
625 // };
626 //
627 // X<int> x;
628 //
629 // We are instantiating the member template "f" within X<int>, which means
630 // substituting int for T, but leaving "f" as a member function template.
631 // Build the function template itself.
632 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
633 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +0000634 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +0000635 TemplateParams, Method);
636 if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +0000637 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +0000638 Method->setDescribedFunctionTemplate(FunctionTemplate);
639 } else if (!FunctionTemplate)
Douglas Gregor2db32322009-10-07 23:56:10 +0000640 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000641
Mike Stump1eb44332009-09-09 15:08:12 +0000642 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000643 // out-of-line, the instantiation will have the same lexical
644 // context (which will be a namespace scope) as the template.
645 if (D->isOutOfLine())
646 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000647
Douglas Gregor5545e162009-03-24 00:38:23 +0000648 // Attach the parameters
649 for (unsigned P = 0; P < Params.size(); ++P)
650 Params[P]->setOwningFunction(Method);
Jay Foadbeaaccd2009-05-21 09:52:38 +0000651 Method->setParams(SemaRef.Context, Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +0000652
653 if (InitMethodInstantiation(Method, D))
654 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000655
Douglas Gregordec06662009-08-21 18:42:58 +0000656 NamedDecl *PrevDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Douglas Gregord60e1052009-08-27 16:57:43 +0000658 if (!FunctionTemplate || TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +0000659 PrevDecl = SemaRef.LookupQualifiedName(Owner, Name,
Douglas Gregordec06662009-08-21 18:42:58 +0000660 Sema::LookupOrdinaryName, true);
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Douglas Gregordec06662009-08-21 18:42:58 +0000662 // In C++, the previous declaration we find might be a tag type
663 // (class or enum). In this case, the new declaration will hide the
664 // tag type. Note that this does does not apply if we're declaring a
665 // typedef (C++ [dcl.typedef]p4).
666 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
667 PrevDecl = 0;
668 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000669
Douglas Gregord60e1052009-08-27 16:57:43 +0000670 if (FunctionTemplate && !TemplateParams)
Douglas Gregor6b906862009-08-21 00:16:32 +0000671 // Record this function template specialization.
672 Method->setFunctionTemplateSpecialization(SemaRef.Context,
673 FunctionTemplate,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000674 &TemplateArgs.getInnermost(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000675 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000676
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000677 bool Redeclaration = false;
678 bool OverloadableAttrRequired = false;
679 SemaRef.CheckFunctionDeclaration(Method, PrevDecl, Redeclaration,
680 /*FIXME:*/OverloadableAttrRequired);
681
682 if (!FunctionTemplate && (!Method->isInvalidDecl() || !PrevDecl))
Douglas Gregordec06662009-08-21 18:42:58 +0000683 Owner->addDecl(Method);
Mike Stump1eb44332009-09-09 15:08:12 +0000684
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000685 return Method;
686}
687
Douglas Gregor615c5d42009-03-24 16:43:20 +0000688Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +0000689 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000690}
691
Douglas Gregor03b2b072009-03-24 00:15:49 +0000692Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +0000693 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +0000694}
695
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000696Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000697 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000698}
699
Douglas Gregor6477b692009-03-25 15:04:13 +0000700ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000701 QualType OrigT = SemaRef.SubstType(D->getOriginalType(), TemplateArgs,
Douglas Gregor7e063902009-05-11 23:53:27 +0000702 D->getLocation(), D->getDeclName());
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000703 if (OrigT.isNull())
704 return 0;
705
706 QualType T = SemaRef.adjustParameterType(OrigT);
707
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000708 // Allocate the parameter
709 ParmVarDecl *Param = 0;
710 if (T == OrigT)
711 Param = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000712 D->getIdentifier(), T, D->getDeclaratorInfo(),
713 D->getStorageClass(), 0);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000714 else
Mike Stump1eb44332009-09-09 15:08:12 +0000715 Param = OriginalParmVarDecl::Create(SemaRef.Context, Owner,
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000716 D->getLocation(), D->getIdentifier(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000717 T, D->getDeclaratorInfo(), OrigT,
718 D->getStorageClass(), 0);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000719
Anders Carlsson9351c172009-08-25 03:18:48 +0000720 // Mark the default argument as being uninstantiated.
Douglas Gregorf43d0b32009-09-25 06:56:31 +0000721 if (D->hasUninstantiatedDefaultArg())
722 Param->setUninstantiatedDefaultArg(D->getUninstantiatedDefaultArg());
Douglas Gregor0ed09302009-09-25 07:03:22 +0000723 else if (Expr *Arg = D->getDefaultArg())
724 Param->setUninstantiatedDefaultArg(Arg);
725
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000726 // Note: we don't try to instantiate function parameters until after
727 // we've instantiated the function's type. Therefore, we don't have
728 // to check for 'void' parameter types here.
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000729 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000730 return Param;
731}
732
733Decl *
734TemplateDeclInstantiator::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
735 // Since parameter types can decay either before or after
736 // instantiation, we simply treat OriginalParmVarDecls as
737 // ParmVarDecls the same way, and create one or the other depending
738 // on what happens after template instantiation.
739 return VisitParmVarDecl(D);
740}
741
John McCalle29ba202009-08-20 01:44:21 +0000742Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
743 TemplateTypeParmDecl *D) {
744 // TODO: don't always clone when decls are refcounted.
745 const Type* T = D->getTypeForDecl();
746 assert(T->isTemplateTypeParmType());
747 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000748
John McCalle29ba202009-08-20 01:44:21 +0000749 TemplateTypeParmDecl *Inst =
750 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
751 TTPT->getDepth(), TTPT->getIndex(),
752 TTPT->getName(),
753 D->wasDeclaredWithTypename(),
754 D->isParameterPack());
755
756 if (D->hasDefaultArgument()) {
757 QualType DefaultPattern = D->getDefaultArgument();
758 QualType DefaultInst
John McCallce3ff2b2009-08-25 22:02:44 +0000759 = SemaRef.SubstType(DefaultPattern, TemplateArgs,
760 D->getDefaultArgumentLoc(),
761 D->getDeclName());
Mike Stump1eb44332009-09-09 15:08:12 +0000762
John McCalle29ba202009-08-20 01:44:21 +0000763 Inst->setDefaultArgument(DefaultInst,
764 D->getDefaultArgumentLoc(),
765 D->defaultArgumentWasInherited() /* preserve? */);
766 }
767
768 return Inst;
769}
770
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000771Decl *
772TemplateDeclInstantiator::VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000773 NestedNameSpecifier *NNS =
774 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
775 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000776 TemplateArgs);
777 if (!NNS)
778 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000779
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000780 CXXScopeSpec SS;
781 SS.setRange(D->getTargetNestedNameRange());
782 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +0000783
784 NamedDecl *UD =
785 SemaRef.BuildUsingDeclaration(D->getLocation(), SS,
786 D->getTargetNameLocation(),
Anders Carlsson0d8df782009-08-29 19:37:28 +0000787 D->getTargetName(), 0, D->isTypeName());
788 if (UD)
Mike Stump1eb44332009-09-09 15:08:12 +0000789 SemaRef.Context.setInstantiatedFromUnresolvedUsingDecl(cast<UsingDecl>(UD),
Anders Carlsson0d8df782009-08-29 19:37:28 +0000790 D);
791 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000792}
793
John McCallce3ff2b2009-08-25 22:02:44 +0000794Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000795 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +0000796 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000797 return Instantiator.Visit(D);
798}
799
John McCalle29ba202009-08-20 01:44:21 +0000800/// \brief Instantiates a nested template parameter list in the current
801/// instantiation context.
802///
803/// \param L The parameter list to instantiate
804///
805/// \returns NULL if there was an error
806TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +0000807TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +0000808 // Get errors for all the parameters before bailing out.
809 bool Invalid = false;
810
811 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000812 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +0000813 ParamVector Params;
814 Params.reserve(N);
815 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
816 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000817 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +0000818 Params.push_back(D);
819 Invalid = Invalid || !D;
820 }
821
822 // Clean up if we had an error.
823 if (Invalid) {
824 for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
825 PI != PE; ++PI)
826 if (*PI)
827 (*PI)->Destroy(SemaRef.Context);
828 return NULL;
829 }
830
831 TemplateParameterList *InstL
832 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
833 L->getLAngleLoc(), &Params.front(), N,
834 L->getRAngleLoc());
835 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +0000836}
John McCalle29ba202009-08-20 01:44:21 +0000837
John McCallce3ff2b2009-08-25 22:02:44 +0000838/// \brief Does substitution on the type of the given function, including
839/// all of the function parameters.
Douglas Gregor5545e162009-03-24 00:38:23 +0000840///
John McCallce3ff2b2009-08-25 22:02:44 +0000841/// \param D The function whose type will be the basis of the substitution
Douglas Gregor5545e162009-03-24 00:38:23 +0000842///
843/// \param Params the instantiated parameter declarations
844
John McCallce3ff2b2009-08-25 22:02:44 +0000845/// \returns the instantiated function's type if successful, a NULL
Douglas Gregor5545e162009-03-24 00:38:23 +0000846/// type if there was an error.
Mike Stump1eb44332009-09-09 15:08:12 +0000847QualType
John McCallce3ff2b2009-08-25 22:02:44 +0000848TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +0000849 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
850 bool InvalidDecl = false;
851
John McCallce3ff2b2009-08-25 22:02:44 +0000852 // Substitute all of the function's formal parameter types.
Douglas Gregor7e063902009-05-11 23:53:27 +0000853 TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000854 llvm::SmallVector<QualType, 4> ParamTys;
Mike Stump1eb44332009-09-09 15:08:12 +0000855 for (FunctionDecl::param_iterator P = D->param_begin(),
Douglas Gregor5545e162009-03-24 00:38:23 +0000856 PEnd = D->param_end();
857 P != PEnd; ++P) {
Douglas Gregor6477b692009-03-25 15:04:13 +0000858 if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
Douglas Gregor5545e162009-03-24 00:38:23 +0000859 if (PInst->getType()->isVoidType()) {
860 SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
861 PInst->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000862 } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000863 PInst->getType(),
864 diag::err_abstract_type_in_decl,
865 Sema::AbstractParamType))
Douglas Gregor5545e162009-03-24 00:38:23 +0000866 PInst->setInvalidDecl();
867
868 Params.push_back(PInst);
869 ParamTys.push_back(PInst->getType());
870
871 if (PInst->isInvalidDecl())
872 InvalidDecl = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000873 } else
Douglas Gregor5545e162009-03-24 00:38:23 +0000874 InvalidDecl = true;
875 }
876
877 // FIXME: Deallocate dead declarations.
878 if (InvalidDecl)
879 return QualType();
880
John McCall183700f2009-09-21 23:43:11 +0000881 const FunctionProtoType *Proto = D->getType()->getAs<FunctionProtoType>();
Douglas Gregor5545e162009-03-24 00:38:23 +0000882 assert(Proto && "Missing prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +0000883 QualType ResultType
John McCallce3ff2b2009-08-25 22:02:44 +0000884 = SemaRef.SubstType(Proto->getResultType(), TemplateArgs,
885 D->getLocation(), D->getDeclName());
Douglas Gregor5545e162009-03-24 00:38:23 +0000886 if (ResultType.isNull())
887 return QualType();
888
Jay Foadbeaaccd2009-05-21 09:52:38 +0000889 return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
Douglas Gregor5545e162009-03-24 00:38:23 +0000890 Proto->isVariadic(), Proto->getTypeQuals(),
891 D->getLocation(), D->getDeclName());
892}
893
Mike Stump1eb44332009-09-09 15:08:12 +0000894/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +0000895/// declaration (New) from the corresponding fields of its template (Tmpl).
896///
897/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +0000898bool
899TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +0000900 FunctionDecl *Tmpl) {
901 if (Tmpl->isDeleted())
902 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +0000903
Douglas Gregorcca9e962009-07-01 22:01:06 +0000904 // If we are performing substituting explicitly-specified template arguments
905 // or deduced template arguments into a function template and we reach this
906 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +0000907 // to keeping the new function template specialization. We therefore
908 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +0000909 // into a template instantiation for this specific function template
910 // specialization, which is not a SFINAE context, so that we diagnose any
911 // further errors in the declaration itself.
912 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
913 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
914 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
915 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +0000916 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +0000917 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000918 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +0000919 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +0000920 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +0000921 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
922 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
923 }
924 }
Mike Stump1eb44332009-09-09 15:08:12 +0000925
Douglas Gregore53060f2009-06-25 22:08:12 +0000926 return false;
927}
928
Douglas Gregor5545e162009-03-24 00:38:23 +0000929/// \brief Initializes common fields of an instantiated method
930/// declaration (New) from the corresponding fields of its template
931/// (Tmpl).
932///
933/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +0000934bool
935TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +0000936 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +0000937 if (InitFunctionInstantiation(New, Tmpl))
938 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000939
Douglas Gregor5545e162009-03-24 00:38:23 +0000940 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
941 New->setAccess(Tmpl->getAccess());
Anders Carlsson77b7f1d2009-05-14 22:15:41 +0000942 if (Tmpl->isVirtualAsWritten()) {
943 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +0000944 Record->setAggregate(false);
945 Record->setPOD(false);
Eli Friedman1d954f62009-08-15 21:55:26 +0000946 Record->setEmpty(false);
Douglas Gregor5545e162009-03-24 00:38:23 +0000947 Record->setPolymorphic(true);
948 }
Douglas Gregor5545e162009-03-24 00:38:23 +0000949 if (Tmpl->isPure()) {
950 New->setPure();
951 Record->setAbstract(true);
952 }
953
954 // FIXME: attributes
955 // FIXME: New needs a pointer to Tmpl
956 return false;
957}
Douglas Gregora58861f2009-05-13 20:28:22 +0000958
959/// \brief Instantiate the definition of the given function from its
960/// template.
961///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +0000962/// \param PointOfInstantiation the point at which the instantiation was
963/// required. Note that this is not precisely a "point of instantiation"
964/// for the function, but it's close.
965///
Douglas Gregora58861f2009-05-13 20:28:22 +0000966/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +0000967/// function template specialization or member function of a class template
968/// specialization.
969///
970/// \param Recursive if true, recursively instantiates any functions that
971/// are required by this instantiation.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000972void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +0000973 FunctionDecl *Function,
974 bool Recursive) {
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000975 if (Function->isInvalidDecl())
976 return;
977
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000978 assert(!Function->getBody() && "Already instantiated!");
Mike Stump1eb44332009-09-09 15:08:12 +0000979
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000980 // Find the function body that we'll be substituting.
Douglas Gregor1637be72009-06-26 00:10:03 +0000981 const FunctionDecl *PatternDecl = 0;
Douglas Gregor5ec178f2009-08-28 21:09:48 +0000982 if (FunctionTemplateDecl *Primary = Function->getPrimaryTemplate()) {
983 while (Primary->getInstantiatedFromMemberTemplate())
984 Primary = Primary->getInstantiatedFromMemberTemplate();
Mike Stump1eb44332009-09-09 15:08:12 +0000985
Douglas Gregor1637be72009-06-26 00:10:03 +0000986 PatternDecl = Primary->getTemplatedDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000987 } else
Douglas Gregor1637be72009-06-26 00:10:03 +0000988 PatternDecl = Function->getInstantiatedFromMemberFunction();
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000989 Stmt *Pattern = 0;
990 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000991 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000992
993 if (!Pattern)
994 return;
995
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000996 // C++0x [temp.explicit]p9:
997 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +0000998 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000999 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00001000 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001001 == TSK_ExplicitInstantiationDeclaration &&
1002 PatternDecl->isOutOfLine() && !PatternDecl->isInline())
1003 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001004
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001005 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
1006 if (Inst)
1007 return;
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001008
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001009 // If we're performing recursive template instantiation, create our own
1010 // queue of pending implicit instantiations that we will instantiate later,
1011 // while we're still within our own instantiation context.
1012 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1013 if (Recursive)
1014 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001015
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001016 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
1017
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001018 // Introduce a new scope where local variable instantiations will be
1019 // recorded.
1020 LocalInstantiationScope Scope(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001021
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001022 // Introduce the instantiated function parameters into the local
1023 // instantiation scope.
1024 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
1025 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
1026 Function->getParamDecl(I));
1027
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001028 // Enter the scope of this instantiation. We don't use
1029 // PushDeclContext because we don't have a scope.
1030 DeclContext *PreviousContext = CurContext;
1031 CurContext = Function;
1032
Mike Stump1eb44332009-09-09 15:08:12 +00001033 MultiLevelTemplateArgumentList TemplateArgs =
Anders Carlsson09025312009-08-29 05:16:22 +00001034 getTemplateInstantiationArgs(Function);
1035
1036 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001037 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00001038 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
1039 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
1040 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001041 }
1042
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001043 // Instantiate the function body.
Anders Carlsson09025312009-08-29 05:16:22 +00001044 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001045
Douglas Gregor52604ab2009-09-11 21:19:12 +00001046 if (Body.isInvalid())
1047 Function->setInvalidDecl();
1048
Mike Stump1eb44332009-09-09 15:08:12 +00001049 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001050 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001051
1052 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00001053
1054 DeclGroupRef DG(Function);
1055 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001056
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001057 if (Recursive) {
1058 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001059 // instantiation of this template.
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001060 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001061
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001062 // Restore the set of pending implicit instantiations.
1063 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1064 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001065}
1066
1067/// \brief Instantiate the definition of the given variable from its
1068/// template.
1069///
Douglas Gregor7caa6822009-07-24 20:34:43 +00001070/// \param PointOfInstantiation the point at which the instantiation was
1071/// required. Note that this is not precisely a "point of instantiation"
1072/// for the function, but it's close.
1073///
1074/// \param Var the already-instantiated declaration of a static member
1075/// variable of a class template specialization.
1076///
1077/// \param Recursive if true, recursively instantiates any functions that
1078/// are required by this instantiation.
1079void Sema::InstantiateStaticDataMemberDefinition(
1080 SourceLocation PointOfInstantiation,
1081 VarDecl *Var,
1082 bool Recursive) {
1083 if (Var->isInvalidDecl())
1084 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001085
Douglas Gregor7caa6822009-07-24 20:34:43 +00001086 // Find the out-of-line definition of this static data member.
1087 // FIXME: Do we have to look for specializations separately?
1088 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
1089 bool FoundOutOfLineDef = false;
1090 assert(Def && "This data member was not instantiated from a template?");
Mike Stump1eb44332009-09-09 15:08:12 +00001091 assert(Def->isStaticDataMember() && "Not a static data member?");
1092 for (VarDecl::redecl_iterator RD = Def->redecls_begin(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00001093 RDEnd = Def->redecls_end();
1094 RD != RDEnd; ++RD) {
1095 if (RD->getLexicalDeclContext()->isFileContext()) {
1096 Def = *RD;
1097 FoundOutOfLineDef = true;
1098 }
1099 }
Mike Stump1eb44332009-09-09 15:08:12 +00001100
Douglas Gregor7caa6822009-07-24 20:34:43 +00001101 if (!FoundOutOfLineDef) {
1102 // We did not find an out-of-line definition of this static data member,
1103 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00001104 // instantiate this definition (or provide a specialization for it) in
1105 // another translation unit.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001106 return;
1107 }
1108
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001109 // FIXME: extern templates
Mike Stump1eb44332009-09-09 15:08:12 +00001110
Douglas Gregor7caa6822009-07-24 20:34:43 +00001111 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
1112 if (Inst)
1113 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001114
Douglas Gregor7caa6822009-07-24 20:34:43 +00001115 // If we're performing recursive template instantiation, create our own
1116 // queue of pending implicit instantiations that we will instantiate later,
1117 // while we're still within our own instantiation context.
1118 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1119 if (Recursive)
1120 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001121
Douglas Gregor7caa6822009-07-24 20:34:43 +00001122 // Enter the scope of this instantiation. We don't use
1123 // PushDeclContext because we don't have a scope.
1124 DeclContext *PreviousContext = CurContext;
1125 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001126
John McCallce3ff2b2009-08-25 22:02:44 +00001127 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00001128 getTemplateInstantiationArgs(Var)));
Mike Stump1eb44332009-09-09 15:08:12 +00001129
Douglas Gregor7caa6822009-07-24 20:34:43 +00001130 CurContext = PreviousContext;
1131
1132 if (Var) {
1133 DeclGroupRef DG(Var);
1134 Consumer.HandleTopLevelDecl(DG);
1135 }
Mike Stump1eb44332009-09-09 15:08:12 +00001136
Douglas Gregor7caa6822009-07-24 20:34:43 +00001137 if (Recursive) {
1138 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001139 // instantiation of this template.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001140 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001141
Douglas Gregor7caa6822009-07-24 20:34:43 +00001142 // Restore the set of pending implicit instantiations.
1143 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001144 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001145}
Douglas Gregor815215d2009-05-27 05:35:12 +00001146
Anders Carlsson09025312009-08-29 05:16:22 +00001147void
1148Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
1149 const CXXConstructorDecl *Tmpl,
1150 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001151
Anders Carlsson09025312009-08-29 05:16:22 +00001152 llvm::SmallVector<MemInitTy*, 4> NewInits;
1153
1154 // Instantiate all the initializers.
1155 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00001156 InitsEnd = Tmpl->init_end();
1157 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00001158 CXXBaseOrMemberInitializer *Init = *Inits;
1159
1160 ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001161
Anders Carlsson09025312009-08-29 05:16:22 +00001162 // Instantiate all the arguments.
1163 for (ExprIterator Args = Init->arg_begin(), ArgsEnd = Init->arg_end();
1164 Args != ArgsEnd; ++Args) {
1165 OwningExprResult NewArg = SubstExpr(*Args, TemplateArgs);
1166
1167 if (NewArg.isInvalid())
1168 New->setInvalidDecl();
1169 else
1170 NewArgs.push_back(NewArg.takeAs<Expr>());
1171 }
1172
1173 MemInitResult NewInit;
1174
1175 if (Init->isBaseInitializer()) {
Eli Friedmanc5573a82009-08-29 22:22:07 +00001176 QualType BaseType(Init->getBaseClass(), 0);
1177 BaseType = SubstType(BaseType, TemplateArgs, Init->getSourceLocation(),
1178 New->getDeclName());
Anders Carlsson09025312009-08-29 05:16:22 +00001179
1180 NewInit = BuildBaseInitializer(BaseType,
Mike Stump1eb44332009-09-09 15:08:12 +00001181 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001182 NewArgs.size(),
1183 Init->getSourceLocation(),
1184 Init->getRParenLoc(),
1185 New->getParent());
1186 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001187 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00001188
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001189 // Is this an anonymous union?
1190 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregore95b4092009-09-16 18:34:49 +00001191 Member = cast<FieldDecl>(FindInstantiatedDecl(UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001192 else
Douglas Gregore95b4092009-09-16 18:34:49 +00001193 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMember(),
1194 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00001195
1196 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001197 NewArgs.size(),
1198 Init->getSourceLocation(),
1199 Init->getRParenLoc());
1200 }
1201
1202 if (NewInit.isInvalid())
1203 New->setInvalidDecl();
1204 else {
1205 // FIXME: It would be nice if ASTOwningVector had a release function.
1206 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001207
Anders Carlsson09025312009-08-29 05:16:22 +00001208 NewInits.push_back((MemInitTy *)NewInit.get());
1209 }
1210 }
Mike Stump1eb44332009-09-09 15:08:12 +00001211
Anders Carlsson09025312009-08-29 05:16:22 +00001212 // Assign all the initializers to the new constructor.
Mike Stump1eb44332009-09-09 15:08:12 +00001213 ActOnMemInitializers(DeclPtrTy::make(New),
Anders Carlsson09025312009-08-29 05:16:22 +00001214 /*FIXME: ColonLoc */
1215 SourceLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001216 NewInits.data(), NewInits.size());
Anders Carlsson09025312009-08-29 05:16:22 +00001217}
1218
John McCall52a575a2009-08-29 08:11:13 +00001219// TODO: this could be templated if the various decl types used the
1220// same method name.
1221static bool isInstantiationOf(ClassTemplateDecl *Pattern,
1222 ClassTemplateDecl *Instance) {
1223 Pattern = Pattern->getCanonicalDecl();
1224
1225 do {
1226 Instance = Instance->getCanonicalDecl();
1227 if (Pattern == Instance) return true;
1228 Instance = Instance->getInstantiatedFromMemberTemplate();
1229 } while (Instance);
1230
1231 return false;
1232}
1233
Douglas Gregor0d696532009-09-28 06:34:35 +00001234static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
1235 FunctionTemplateDecl *Instance) {
1236 Pattern = Pattern->getCanonicalDecl();
1237
1238 do {
1239 Instance = Instance->getCanonicalDecl();
1240 if (Pattern == Instance) return true;
1241 Instance = Instance->getInstantiatedFromMemberTemplate();
1242 } while (Instance);
1243
1244 return false;
1245}
1246
John McCall52a575a2009-08-29 08:11:13 +00001247static bool isInstantiationOf(CXXRecordDecl *Pattern,
1248 CXXRecordDecl *Instance) {
1249 Pattern = Pattern->getCanonicalDecl();
1250
1251 do {
1252 Instance = Instance->getCanonicalDecl();
1253 if (Pattern == Instance) return true;
1254 Instance = Instance->getInstantiatedFromMemberClass();
1255 } while (Instance);
1256
1257 return false;
1258}
1259
1260static bool isInstantiationOf(FunctionDecl *Pattern,
1261 FunctionDecl *Instance) {
1262 Pattern = Pattern->getCanonicalDecl();
1263
1264 do {
1265 Instance = Instance->getCanonicalDecl();
1266 if (Pattern == Instance) return true;
1267 Instance = Instance->getInstantiatedFromMemberFunction();
1268 } while (Instance);
1269
1270 return false;
1271}
1272
1273static bool isInstantiationOf(EnumDecl *Pattern,
1274 EnumDecl *Instance) {
1275 Pattern = Pattern->getCanonicalDecl();
1276
1277 do {
1278 Instance = Instance->getCanonicalDecl();
1279 if (Pattern == Instance) return true;
1280 Instance = Instance->getInstantiatedFromMemberEnum();
1281 } while (Instance);
1282
1283 return false;
1284}
1285
Anders Carlsson0d8df782009-08-29 19:37:28 +00001286static bool isInstantiationOf(UnresolvedUsingDecl *Pattern,
1287 UsingDecl *Instance,
1288 ASTContext &C) {
1289 return C.getInstantiatedFromUnresolvedUsingDecl(Instance) == Pattern;
1290}
1291
John McCall52a575a2009-08-29 08:11:13 +00001292static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
1293 VarDecl *Instance) {
1294 assert(Instance->isStaticDataMember());
1295
1296 Pattern = Pattern->getCanonicalDecl();
1297
1298 do {
1299 Instance = Instance->getCanonicalDecl();
1300 if (Pattern == Instance) return true;
1301 Instance = Instance->getInstantiatedFromStaticDataMember();
1302 } while (Instance);
1303
1304 return false;
1305}
1306
Douglas Gregor815215d2009-05-27 05:35:12 +00001307static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00001308 if (D->getKind() != Other->getKind()) {
1309 if (UnresolvedUsingDecl *UUD = dyn_cast<UnresolvedUsingDecl>(D)) {
1310 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
1311 return isInstantiationOf(UUD, UD, Ctx);
1312 }
1313 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001314
Anders Carlsson0d8df782009-08-29 19:37:28 +00001315 return false;
1316 }
Mike Stump1eb44332009-09-09 15:08:12 +00001317
John McCall52a575a2009-08-29 08:11:13 +00001318 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
1319 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001320
John McCall52a575a2009-08-29 08:11:13 +00001321 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
1322 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00001323
John McCall52a575a2009-08-29 08:11:13 +00001324 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
1325 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00001326
Douglas Gregor7caa6822009-07-24 20:34:43 +00001327 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00001328 if (Var->isStaticDataMember())
1329 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
1330
1331 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
1332 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00001333
Douglas Gregor0d696532009-09-28 06:34:35 +00001334 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
1335 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
1336
Anders Carlssond8b285f2009-09-01 04:26:58 +00001337 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
1338 if (!Field->getDeclName()) {
1339 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00001340 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00001341 cast<FieldDecl>(D);
1342 }
1343 }
Mike Stump1eb44332009-09-09 15:08:12 +00001344
Douglas Gregor815215d2009-05-27 05:35:12 +00001345 return D->getDeclName() && isa<NamedDecl>(Other) &&
1346 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
1347}
1348
1349template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00001350static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00001351 NamedDecl *D,
1352 ForwardIterator first,
1353 ForwardIterator last) {
1354 for (; first != last; ++first)
1355 if (isInstantiationOf(Ctx, D, *first))
1356 return cast<NamedDecl>(*first);
1357
1358 return 0;
1359}
1360
John McCall02cace72009-08-28 07:59:38 +00001361/// \brief Finds the instantiation of the given declaration context
1362/// within the current instantiation.
1363///
1364/// \returns NULL if there was an error
Douglas Gregore95b4092009-09-16 18:34:49 +00001365DeclContext *Sema::FindInstantiatedContext(DeclContext* DC,
1366 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00001367 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00001368 Decl* ID = FindInstantiatedDecl(D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00001369 return cast_or_null<DeclContext>(ID);
1370 } else return DC;
1371}
1372
Douglas Gregored961e72009-05-27 17:54:46 +00001373/// \brief Find the instantiation of the given declaration within the
1374/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00001375///
1376/// This routine is intended to be used when \p D is a declaration
1377/// referenced from within a template, that needs to mapped into the
1378/// corresponding declaration within an instantiation. For example,
1379/// given:
1380///
1381/// \code
1382/// template<typename T>
1383/// struct X {
1384/// enum Kind {
1385/// KnownValue = sizeof(T)
1386/// };
1387///
1388/// bool getKind() const { return KnownValue; }
1389/// };
1390///
1391/// template struct X<int>;
1392/// \endcode
1393///
1394/// In the instantiation of X<int>::getKind(), we need to map the
1395/// EnumConstantDecl for KnownValue (which refers to
1396/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00001397/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
1398/// this mapping from within the instantiation of X<int>.
Douglas Gregore95b4092009-09-16 18:34:49 +00001399NamedDecl *Sema::FindInstantiatedDecl(NamedDecl *D,
1400 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor44c73842009-09-01 17:53:10 +00001401 if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D)) {
1402 // Transform all of the elements of the overloaded function set.
Mike Stump1eb44332009-09-09 15:08:12 +00001403 OverloadedFunctionDecl *Result
Douglas Gregor44c73842009-09-01 17:53:10 +00001404 = OverloadedFunctionDecl::Create(Context, CurContext, Ovl->getDeclName());
Mike Stump1eb44332009-09-09 15:08:12 +00001405
Douglas Gregor44c73842009-09-01 17:53:10 +00001406 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
1407 FEnd = Ovl->function_end();
1408 F != FEnd; ++F) {
1409 Result->addOverload(
Douglas Gregore95b4092009-09-16 18:34:49 +00001410 AnyFunctionDecl::getFromNamedDecl(FindInstantiatedDecl(*F,
1411 TemplateArgs)));
Douglas Gregor44c73842009-09-01 17:53:10 +00001412 }
Mike Stump1eb44332009-09-09 15:08:12 +00001413
Douglas Gregor44c73842009-09-01 17:53:10 +00001414 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001415 }
1416
Douglas Gregor815215d2009-05-27 05:35:12 +00001417 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor2bba76b2009-05-27 17:07:49 +00001418 if (isa<ParmVarDecl>(D) || ParentDC->isFunctionOrMethod()) {
1419 // D is a local of some kind. Look into the map of local
1420 // declarations to their instantiations.
1421 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
1422 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001423
Douglas Gregore95b4092009-09-16 18:34:49 +00001424 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
1425 if (!Record->isDependentContext())
1426 return D;
1427
1428 // If the RecordDecl is actually the injected-class-name or a "templated"
1429 // declaration for a class template or class template partial
1430 // specialization, substitute into the injected-class-name of the
1431 // class template or partial specialization to find the new DeclContext.
1432 QualType T;
1433 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
1434
1435 if (ClassTemplate) {
1436 T = ClassTemplate->getInjectedClassNameType(Context);
1437 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
1438 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1439 T = Context.getTypeDeclType(Record);
1440 ClassTemplate = PartialSpec->getSpecializedTemplate();
1441 }
1442
1443 if (!T.isNull()) {
1444 // Substitute into the injected-class-name to get the type corresponding
1445 // to the instantiation we want. This substitution should never fail,
1446 // since we know we can instantiate the injected-class-name or we wouldn't
1447 // have gotten to the injected-class-name!
1448 // FIXME: Can we use the CurrentInstantiationScope to avoid this extra
1449 // instantiation in the common case?
1450 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
1451 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
1452
1453 if (!T->isDependentType()) {
1454 assert(T->isRecordType() && "Instantiation must produce a record type");
1455 return T->getAs<RecordType>()->getDecl();
1456 }
1457
1458 // We are performing "partial" template instantiation to create the
1459 // member declarations for the members of a class template
1460 // specialization. Therefore, D is actually referring to something in
1461 // the current instantiation. Look through the current context,
1462 // which contains actual instantiations, to find the instantiation of
1463 // the "current instantiation" that D refers to.
Mike Stump1eb44332009-09-09 15:08:12 +00001464 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00001465 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001466 if (ClassTemplateSpecializationDecl *Spec
John McCall52a575a2009-08-29 08:11:13 +00001467 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00001468 if (isInstantiationOf(ClassTemplate,
1469 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00001470 return Spec;
1471 }
1472
Mike Stump1eb44332009-09-09 15:08:12 +00001473 assert(false &&
John McCall52a575a2009-08-29 08:11:13 +00001474 "Unable to find declaration for the current instantiation");
Douglas Gregore95b4092009-09-16 18:34:49 +00001475 return Record;
John McCall52a575a2009-08-29 08:11:13 +00001476 }
Douglas Gregore95b4092009-09-16 18:34:49 +00001477
1478 // Fall through to deal with other dependent record types (e.g.,
1479 // anonymous unions in class templates).
1480 }
John McCall52a575a2009-08-29 08:11:13 +00001481
Douglas Gregore95b4092009-09-16 18:34:49 +00001482 if (!ParentDC->isDependentContext())
1483 return D;
1484
1485 ParentDC = FindInstantiatedContext(ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001486 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00001487 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001488
Douglas Gregor815215d2009-05-27 05:35:12 +00001489 if (ParentDC != D->getDeclContext()) {
1490 // We performed some kind of instantiation in the parent context,
1491 // so now we need to look into the instantiated parent context to
1492 // find the instantiation of the declaration D.
1493 NamedDecl *Result = 0;
1494 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001495 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00001496 Result = findInstantiationOf(Context, D, Found.first, Found.second);
1497 } else {
1498 // Since we don't have a name for the entity we're looking for,
1499 // our only option is to walk through all of the declarations to
1500 // find that name. This will occur in a few cases:
1501 //
1502 // - anonymous struct/union within a template
1503 // - unnamed class/struct/union/enum within a template
1504 //
1505 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00001506 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001507 ParentDC->decls_begin(),
1508 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00001509 }
Mike Stump1eb44332009-09-09 15:08:12 +00001510
Douglas Gregor815215d2009-05-27 05:35:12 +00001511 assert(Result && "Unable to find instantiation of declaration!");
1512 D = Result;
1513 }
1514
Douglas Gregor815215d2009-05-27 05:35:12 +00001515 return D;
1516}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001517
Mike Stump1eb44332009-09-09 15:08:12 +00001518/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001519/// instantiations we have seen until this point.
1520void Sema::PerformPendingImplicitInstantiations() {
1521 while (!PendingImplicitInstantiations.empty()) {
1522 PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front();
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001523 PendingImplicitInstantiations.pop_front();
Mike Stump1eb44332009-09-09 15:08:12 +00001524
Douglas Gregor7caa6822009-07-24 20:34:43 +00001525 // Instantiate function definitions
1526 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001527 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001528 Function->getLocation(), *this,
1529 Context.getSourceManager(),
1530 "instantiating function definition");
Mike Stump1eb44332009-09-09 15:08:12 +00001531
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001532 if (!Function->getBody())
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001533 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001534 continue;
1535 }
Mike Stump1eb44332009-09-09 15:08:12 +00001536
Douglas Gregor7caa6822009-07-24 20:34:43 +00001537 // Instantiate static data member definitions.
1538 VarDecl *Var = cast<VarDecl>(Inst.first);
1539 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001540
Mike Stump1eb44332009-09-09 15:08:12 +00001541 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001542 Var->getLocation(), *this,
1543 Context.getSourceManager(),
1544 "instantiating static data member "
1545 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00001546
Douglas Gregor7caa6822009-07-24 20:34:43 +00001547 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001548 }
1549}