blob: 503d318c4c5ec74f8e6e2aa9c645592a771d38fc [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 Gregor251b4ff2009-10-08 07:24:58 +0000158 // Link instantiations of static data members back to the template from
159 // which they were instantiated.
160 if (Var->isStaticDataMember())
161 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
162 TSK_ImplicitInstantiation);
163
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000164 if (D->getInit()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000165 OwningExprResult Init
John McCallce3ff2b2009-08-25 22:02:44 +0000166 = SemaRef.SubstExpr(D->getInit(), TemplateArgs);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000167 if (Init.isInvalid())
168 Var->setInvalidDecl();
Douglas Gregor83ddad32009-08-26 21:14:46 +0000169 else if (ParenListExpr *PLE = dyn_cast<ParenListExpr>((Expr *)Init.get())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000170 // FIXME: We're faking all of the comma locations, which is suboptimal.
Douglas Gregor83ddad32009-08-26 21:14:46 +0000171 // Do we even need these comma locations?
172 llvm::SmallVector<SourceLocation, 4> FakeCommaLocs;
173 if (PLE->getNumExprs() > 0) {
174 FakeCommaLocs.reserve(PLE->getNumExprs() - 1);
175 for (unsigned I = 0, N = PLE->getNumExprs() - 1; I != N; ++I) {
176 Expr *E = PLE->getExpr(I)->Retain();
177 FakeCommaLocs.push_back(
178 SemaRef.PP.getLocForEndOfToken(E->getLocEnd()));
179 }
Douglas Gregore9f8eb62009-08-26 23:26:04 +0000180 PLE->getExpr(PLE->getNumExprs() - 1)->Retain();
Douglas Gregor83ddad32009-08-26 21:14:46 +0000181 }
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Douglas Gregor83ddad32009-08-26 21:14:46 +0000183 // Add the direct initializer to the declaration.
184 SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
Mike Stump1eb44332009-09-09 15:08:12 +0000185 PLE->getLParenLoc(),
Douglas Gregor83ddad32009-08-26 21:14:46 +0000186 Sema::MultiExprArg(SemaRef,
187 (void**)PLE->getExprs(),
188 PLE->getNumExprs()),
189 FakeCommaLocs.data(),
190 PLE->getRParenLoc());
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Douglas Gregor83ddad32009-08-26 21:14:46 +0000192 // When Init is destroyed, it will destroy the instantiated ParenListExpr;
193 // we've explicitly retained all of its subexpressions already.
194 } else
Chris Lattnerb28317a2009-03-28 19:18:32 +0000195 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init),
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000196 D->hasCXXDirectInitializer());
Douglas Gregor65b90052009-07-27 17:43:39 +0000197 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
198 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000199
200 return Var;
201}
202
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000203Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
204 bool Invalid = false;
205 QualType T = D->getType();
206 if (T->isDependentType()) {
John McCallce3ff2b2009-08-25 22:02:44 +0000207 T = SemaRef.SubstType(T, TemplateArgs,
208 D->getLocation(), D->getDeclName());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000209 if (!T.isNull() && T->isFunctionType()) {
210 // C++ [temp.arg.type]p3:
211 // If a declaration acquires a function type through a type
212 // dependent on a template-parameter and this causes a
213 // declaration that does not use the syntactic form of a
214 // function declarator to have function type, the program is
215 // ill-formed.
216 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
217 << T;
218 T = QualType();
219 Invalid = true;
220 }
221 }
222
223 Expr *BitWidth = D->getBitWidth();
224 if (Invalid)
225 BitWidth = 0;
226 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000227 // The bit-width expression is not potentially evaluated.
228 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000229
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000230 OwningExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000231 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000232 if (InstantiatedBitWidth.isInvalid()) {
233 Invalid = true;
234 BitWidth = 0;
235 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000236 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000237 }
238
239 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), T,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000240 D->getDeclaratorInfo(),
Mike Stump1eb44332009-09-09 15:08:12 +0000241 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000242 D->getLocation(),
243 D->isMutable(),
244 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000245 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000246 D->getAccess(),
247 0);
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000248 if (!Field)
249 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000251 if (Invalid)
252 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000253
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000254 if (!Field->getDeclName()) {
255 // Keep track of where this decl came from.
256 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000257 }
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000259 Field->setImplicit(D->isImplicit());
260 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000261
262 return Field;
263}
264
John McCall02cace72009-08-28 07:59:38 +0000265Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
266 FriendDecl::FriendUnion FU;
267
268 // Handle friend type expressions by simply substituting template
269 // parameters into the pattern type.
270 if (Type *Ty = D->getFriendType()) {
271 QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs,
272 D->getLocation(), DeclarationName());
273 if (T.isNull()) return 0;
274
275 assert(getLangOptions().CPlusPlus0x || T->isRecordType());
276 FU = T.getTypePtr();
277
278 // Handle everything else by appropriate substitution.
279 } else {
280 NamedDecl *ND = D->getFriendDecl();
281 assert(ND && "friend decl must be a decl or a type!");
282
283 Decl *NewND = Visit(ND);
284 if (!NewND) return 0;
285
286 FU = cast<NamedDecl>(NewND);
John McCallfd810b12009-08-14 02:03:10 +0000287 }
Mike Stump1eb44332009-09-09 15:08:12 +0000288
John McCall02cace72009-08-28 07:59:38 +0000289 FriendDecl *FD =
290 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU,
291 D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000292 FD->setAccess(AS_public);
John McCall02cace72009-08-28 07:59:38 +0000293 Owner->addDecl(FD);
294 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000295}
296
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000297Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
298 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Douglas Gregorac7610d2009-06-22 20:57:11 +0000300 // The expression in a static assertion is not potentially evaluated.
301 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000303 OwningExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000304 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000305 if (InstantiatedAssertExpr.isInvalid())
306 return 0;
307
Douglas Gregor43d9d922009-08-08 01:41:12 +0000308 OwningExprResult Message(SemaRef, D->getMessage());
309 D->getMessage()->Retain();
Mike Stump1eb44332009-09-09 15:08:12 +0000310 Decl *StaticAssert
311 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
Chris Lattnerb28317a2009-03-28 19:18:32 +0000312 move(InstantiatedAssertExpr),
313 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000314 return StaticAssert;
315}
316
317Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000318 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000319 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000320 D->getTagKeywordLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000321 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000322 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000323 Enum->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000324 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000325 Enum->startDefinition();
326
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000327 llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000328
329 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000330 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
331 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000332 EC != ECEnd; ++EC) {
333 // The specified value for the enumerator.
334 OwningExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000335 if (Expr *UninstValue = EC->getInitExpr()) {
336 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000337 EnterExpressionEvaluationContext Unevaluated(SemaRef,
Douglas Gregorac7610d2009-06-22 20:57:11 +0000338 Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000339
John McCallce3ff2b2009-08-25 22:02:44 +0000340 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000341 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000342
343 // Drop the initial value and continue.
344 bool isInvalid = false;
345 if (Value.isInvalid()) {
346 Value = SemaRef.Owned((Expr *)0);
347 isInvalid = true;
348 }
349
Mike Stump1eb44332009-09-09 15:08:12 +0000350 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000351 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
352 EC->getLocation(), EC->getIdentifier(),
353 move(Value));
354
355 if (isInvalid) {
356 if (EnumConst)
357 EnumConst->setInvalidDecl();
358 Enum->setInvalidDecl();
359 }
360
361 if (EnumConst) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000362 Enum->addDecl(EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000363 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000364 LastEnumConst = EnumConst;
365 }
366 }
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000368 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000369 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000370 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
371 Sema::DeclPtrTy::make(Enum),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000372 &Enumerators[0], Enumerators.size(),
373 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000374
375 return Enum;
376}
377
Douglas Gregor6477b692009-03-25 15:04:13 +0000378Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
379 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
380 return 0;
381}
382
John McCalle29ba202009-08-20 01:44:21 +0000383Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
384 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000385 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000386 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000387 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000388
389 CXXRecordDecl *Pattern = D->getTemplatedDecl();
390 CXXRecordDecl *RecordInst
391 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner,
392 Pattern->getLocation(), Pattern->getIdentifier(),
393 Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL);
394
395 ClassTemplateDecl *Inst
396 = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
397 D->getIdentifier(), InstParams, RecordInst, 0);
398 RecordInst->setDescribedClassTemplate(Inst);
399 Inst->setAccess(D->getAccess());
400 Inst->setInstantiatedFromMemberTemplate(D);
401
402 Owner->addDecl(Inst);
403 return Inst;
404}
405
Douglas Gregord60e1052009-08-27 16:57:43 +0000406Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000407TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
408 ClassTemplatePartialSpecializationDecl *D) {
409 assert(false &&"Partial specializations of member templates are unsupported");
410 return 0;
411}
412
413Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000414TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000415 // FIXME: Dig out the out-of-line definition of this function template?
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Douglas Gregord60e1052009-08-27 16:57:43 +0000417 TemplateParameterList *TempParams = D->getTemplateParameters();
418 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000419 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000420 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000421
422 // FIXME: Handle instantiation of nested function templates that aren't
Douglas Gregord60e1052009-08-27 16:57:43 +0000423 // member function templates. This could happen inside a FriendDecl.
424 assert(isa<CXXMethodDecl>(D->getTemplatedDecl()));
Mike Stump1eb44332009-09-09 15:08:12 +0000425 CXXMethodDecl *InstMethod
Douglas Gregord60e1052009-08-27 16:57:43 +0000426 = cast_or_null<CXXMethodDecl>(
Mike Stump1eb44332009-09-09 15:08:12 +0000427 VisitCXXMethodDecl(cast<CXXMethodDecl>(D->getTemplatedDecl()),
Douglas Gregord60e1052009-08-27 16:57:43 +0000428 InstParams));
429 if (!InstMethod)
430 return 0;
431
Mike Stump1eb44332009-09-09 15:08:12 +0000432 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000433 // template from which it was instantiated.
434 FunctionTemplateDecl *InstTemplate = InstMethod->getDescribedFunctionTemplate();
435 assert(InstTemplate && "VisitCXXMethodDecl didn't create a template!");
436 InstTemplate->setInstantiatedFromMemberTemplate(D);
437 Owner->addDecl(InstTemplate);
438 return InstTemplate;
439}
440
Douglas Gregord475b8d2009-03-25 21:17:03 +0000441Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
442 CXXRecordDecl *PrevDecl = 0;
443 if (D->isInjectedClassName())
444 PrevDecl = cast<CXXRecordDecl>(Owner);
445
446 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000447 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000448 D->getLocation(), D->getIdentifier(),
449 D->getTagKeywordLoc(), PrevDecl);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000450 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000451 // FIXME: Check against AS_none is an ugly hack to work around the issue that
452 // the tag decls introduced by friend class declarations don't have an access
453 // specifier. Remove once this area of the code gets sorted out.
454 if (D->getAccess() != AS_none)
455 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000456 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000457 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000458
John McCall02cace72009-08-28 07:59:38 +0000459 // If the original function was part of a friend declaration,
460 // inherit its namespace state.
461 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
462 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
463
Anders Carlssond8b285f2009-09-01 04:26:58 +0000464 Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
465
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000466 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000467 return Record;
468}
469
John McCall02cace72009-08-28 07:59:38 +0000470/// Normal class members are of more specific types and therefore
471/// don't make it here. This function serves two purposes:
472/// 1) instantiating function templates
473/// 2) substituting friend declarations
474/// FIXME: preserve function definitions in case #2
Douglas Gregore53060f2009-06-25 22:08:12 +0000475Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000476 // Check whether there is already a function template specialization for
477 // this declaration.
478 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
479 void *InsertPos = 0;
480 if (FunctionTemplate) {
481 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000482 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000483 TemplateArgs.getInnermost().getFlatArgumentList(),
484 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor828e2262009-07-29 16:09:57 +0000485 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000486
487 FunctionTemplateSpecializationInfo *Info
488 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor127102b2009-06-29 20:59:39 +0000489 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Douglas Gregor127102b2009-06-29 20:59:39 +0000491 // If we already have a function template specialization, return it.
492 if (Info)
493 return Info->Function;
494 }
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Douglas Gregore53060f2009-06-25 22:08:12 +0000496 Sema::LocalInstantiationScope Scope(SemaRef);
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Douglas Gregore53060f2009-06-25 22:08:12 +0000498 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000499 QualType T = SubstFunctionType(D, Params);
Douglas Gregore53060f2009-06-25 22:08:12 +0000500 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000501 return 0;
John McCallfd810b12009-08-14 02:03:10 +0000502
Douglas Gregore53060f2009-06-25 22:08:12 +0000503 // Build the instantiated method declaration.
Douglas Gregore95b4092009-09-16 18:34:49 +0000504 DeclContext *DC = SemaRef.FindInstantiatedContext(D->getDeclContext(),
505 TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +0000506 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +0000507 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000508 D->getDeclName(), T, D->getDeclaratorInfo(),
509 D->getStorageClass(),
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000510 D->isInline(), D->hasWrittenPrototype());
John McCall02cace72009-08-28 07:59:38 +0000511 Function->setLexicalDeclContext(Owner);
Mike Stump1eb44332009-09-09 15:08:12 +0000512
Douglas Gregore53060f2009-06-25 22:08:12 +0000513 // Attach the parameters
514 for (unsigned P = 0; P < Params.size(); ++P)
515 Params[P]->setOwningFunction(Function);
516 Function->setParams(SemaRef.Context, Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +0000517
518 // If the original function was part of a friend declaration,
519 // inherit its namespace state and add it to the owner.
520 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind()) {
521 bool WasDeclared = (FOK == Decl::FOK_Declared);
522 Function->setObjectOfFriendDecl(WasDeclared);
523 if (!Owner->isDependentContext())
John McCallab88d972009-08-31 22:39:49 +0000524 DC->makeDeclVisibleInContext(Function, /* Recoverable = */ false);
John McCallf181d8a2009-08-29 03:16:09 +0000525
Douglas Gregor2db32322009-10-07 23:56:10 +0000526 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
John McCall02cace72009-08-28 07:59:38 +0000527 }
Mike Stump1eb44332009-09-09 15:08:12 +0000528
Douglas Gregore53060f2009-06-25 22:08:12 +0000529 if (InitFunctionInstantiation(Function, D))
530 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000531
Douglas Gregore53060f2009-06-25 22:08:12 +0000532 bool Redeclaration = false;
533 bool OverloadableAttrRequired = false;
534 NamedDecl *PrevDecl = 0;
535 SemaRef.CheckFunctionDeclaration(Function, PrevDecl, Redeclaration,
536 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000537
Douglas Gregor127102b2009-06-29 20:59:39 +0000538 if (FunctionTemplate) {
539 // Record this function template specialization.
540 Function->setFunctionTemplateSpecialization(SemaRef.Context,
541 FunctionTemplate,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000542 &TemplateArgs.getInnermost(),
Douglas Gregor127102b2009-06-29 20:59:39 +0000543 InsertPos);
John McCallfd810b12009-08-14 02:03:10 +0000544 }
545
Douglas Gregore53060f2009-06-25 22:08:12 +0000546 return Function;
547}
548
Douglas Gregord60e1052009-08-27 16:57:43 +0000549Decl *
550TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
551 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +0000552 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
553 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +0000554 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +0000555 // We are creating a function template specialization from a function
556 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +0000557 // specialization for this particular set of template arguments.
Douglas Gregor6b906862009-08-21 00:16:32 +0000558 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000559 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000560 TemplateArgs.getInnermost().getFlatArgumentList(),
561 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000562 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000563
564 FunctionTemplateSpecializationInfo *Info
565 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor6b906862009-08-21 00:16:32 +0000566 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Douglas Gregor6b906862009-08-21 00:16:32 +0000568 // If we already have a function template specialization, return it.
569 if (Info)
570 return Info->Function;
571 }
572
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000573 Sema::LocalInstantiationScope Scope(SemaRef);
574
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000575 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000576 QualType T = SubstFunctionType(D, Params);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000577 if (T.isNull())
578 return 0;
579
580 // Build the instantiated method declaration.
581 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
Douglas Gregordec06662009-08-21 18:42:58 +0000582 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000583
Douglas Gregordec06662009-08-21 18:42:58 +0000584 DeclarationName Name = D->getDeclName();
Douglas Gregor17e32f32009-08-21 22:43:28 +0000585 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregordec06662009-08-21 18:42:58 +0000586 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
587 Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
588 SemaRef.Context.getCanonicalType(ClassTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000589 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
590 Constructor->getLocation(),
591 Name, T,
Douglas Gregor17e32f32009-08-21 22:43:28 +0000592 Constructor->getDeclaratorInfo(),
Mike Stump1eb44332009-09-09 15:08:12 +0000593 Constructor->isExplicit(),
Douglas Gregor17e32f32009-08-21 22:43:28 +0000594 Constructor->isInline(), false);
595 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
596 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
597 Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
598 SemaRef.Context.getCanonicalType(ClassTy));
599 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
600 Destructor->getLocation(), Name,
601 T, Destructor->isInline(), false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000602 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000603 CanQualType ConvTy
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000604 = SemaRef.Context.getCanonicalType(
John McCall183700f2009-09-21 23:43:11 +0000605 T->getAs<FunctionType>()->getResultType());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000606 Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
607 ConvTy);
608 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
609 Conversion->getLocation(), Name,
610 T, Conversion->getDeclaratorInfo(),
Mike Stump1eb44332009-09-09 15:08:12 +0000611 Conversion->isInline(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000612 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +0000613 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000614 Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
Douglas Gregordec06662009-08-21 18:42:58 +0000615 D->getDeclName(), T, D->getDeclaratorInfo(),
616 D->isStatic(), D->isInline());
617 }
Douglas Gregor6b906862009-08-21 00:16:32 +0000618
Douglas Gregord60e1052009-08-27 16:57:43 +0000619 if (TemplateParams) {
620 // Our resulting instantiation is actually a function template, since we
621 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +0000622 //
Douglas Gregord60e1052009-08-27 16:57:43 +0000623 // template<typename T>
624 // struct X {
625 // template<typename U> void f(T, U);
626 // };
627 //
628 // X<int> x;
629 //
630 // We are instantiating the member template "f" within X<int>, which means
631 // substituting int for T, but leaving "f" as a member function template.
632 // Build the function template itself.
633 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
634 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +0000635 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +0000636 TemplateParams, Method);
637 if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +0000638 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +0000639 Method->setDescribedFunctionTemplate(FunctionTemplate);
640 } else if (!FunctionTemplate)
Douglas Gregor2db32322009-10-07 23:56:10 +0000641 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000642
Mike Stump1eb44332009-09-09 15:08:12 +0000643 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000644 // out-of-line, the instantiation will have the same lexical
645 // context (which will be a namespace scope) as the template.
646 if (D->isOutOfLine())
647 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000648
Douglas Gregor5545e162009-03-24 00:38:23 +0000649 // Attach the parameters
650 for (unsigned P = 0; P < Params.size(); ++P)
651 Params[P]->setOwningFunction(Method);
Jay Foadbeaaccd2009-05-21 09:52:38 +0000652 Method->setParams(SemaRef.Context, Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +0000653
654 if (InitMethodInstantiation(Method, D))
655 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000656
Douglas Gregordec06662009-08-21 18:42:58 +0000657 NamedDecl *PrevDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000658
Douglas Gregord60e1052009-08-27 16:57:43 +0000659 if (!FunctionTemplate || TemplateParams) {
John McCallf36e02d2009-10-09 21:13:30 +0000660 Sema::LookupResult R;
661 SemaRef.LookupQualifiedName(R, Owner, Name, Sema::LookupOrdinaryName, true);
662 PrevDecl = R.getAsSingleDecl(SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000663
Douglas Gregordec06662009-08-21 18:42:58 +0000664 // In C++, the previous declaration we find might be a tag type
665 // (class or enum). In this case, the new declaration will hide the
666 // tag type. Note that this does does not apply if we're declaring a
667 // typedef (C++ [dcl.typedef]p4).
668 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
669 PrevDecl = 0;
670 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000671
Douglas Gregord60e1052009-08-27 16:57:43 +0000672 if (FunctionTemplate && !TemplateParams)
Douglas Gregor6b906862009-08-21 00:16:32 +0000673 // Record this function template specialization.
674 Method->setFunctionTemplateSpecialization(SemaRef.Context,
675 FunctionTemplate,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000676 &TemplateArgs.getInnermost(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000677 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000679 bool Redeclaration = false;
680 bool OverloadableAttrRequired = false;
681 SemaRef.CheckFunctionDeclaration(Method, PrevDecl, Redeclaration,
682 /*FIXME:*/OverloadableAttrRequired);
683
684 if (!FunctionTemplate && (!Method->isInvalidDecl() || !PrevDecl))
Douglas Gregordec06662009-08-21 18:42:58 +0000685 Owner->addDecl(Method);
Mike Stump1eb44332009-09-09 15:08:12 +0000686
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000687 return Method;
688}
689
Douglas Gregor615c5d42009-03-24 16:43:20 +0000690Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +0000691 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000692}
693
Douglas Gregor03b2b072009-03-24 00:15:49 +0000694Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +0000695 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +0000696}
697
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000698Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000699 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000700}
701
Douglas Gregor6477b692009-03-25 15:04:13 +0000702ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000703 QualType OrigT = SemaRef.SubstType(D->getOriginalType(), TemplateArgs,
Douglas Gregor7e063902009-05-11 23:53:27 +0000704 D->getLocation(), D->getDeclName());
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000705 if (OrigT.isNull())
706 return 0;
707
708 QualType T = SemaRef.adjustParameterType(OrigT);
709
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000710 // Allocate the parameter
711 ParmVarDecl *Param = 0;
712 if (T == OrigT)
713 Param = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000714 D->getIdentifier(), T, D->getDeclaratorInfo(),
715 D->getStorageClass(), 0);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000716 else
Mike Stump1eb44332009-09-09 15:08:12 +0000717 Param = OriginalParmVarDecl::Create(SemaRef.Context, Owner,
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000718 D->getLocation(), D->getIdentifier(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000719 T, D->getDeclaratorInfo(), OrigT,
720 D->getStorageClass(), 0);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000721
Anders Carlsson9351c172009-08-25 03:18:48 +0000722 // Mark the default argument as being uninstantiated.
Douglas Gregorf43d0b32009-09-25 06:56:31 +0000723 if (D->hasUninstantiatedDefaultArg())
724 Param->setUninstantiatedDefaultArg(D->getUninstantiatedDefaultArg());
Douglas Gregor0ed09302009-09-25 07:03:22 +0000725 else if (Expr *Arg = D->getDefaultArg())
726 Param->setUninstantiatedDefaultArg(Arg);
727
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000728 // Note: we don't try to instantiate function parameters until after
729 // we've instantiated the function's type. Therefore, we don't have
730 // to check for 'void' parameter types here.
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000731 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000732 return Param;
733}
734
735Decl *
736TemplateDeclInstantiator::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
737 // Since parameter types can decay either before or after
738 // instantiation, we simply treat OriginalParmVarDecls as
739 // ParmVarDecls the same way, and create one or the other depending
740 // on what happens after template instantiation.
741 return VisitParmVarDecl(D);
742}
743
John McCalle29ba202009-08-20 01:44:21 +0000744Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
745 TemplateTypeParmDecl *D) {
746 // TODO: don't always clone when decls are refcounted.
747 const Type* T = D->getTypeForDecl();
748 assert(T->isTemplateTypeParmType());
749 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000750
John McCalle29ba202009-08-20 01:44:21 +0000751 TemplateTypeParmDecl *Inst =
752 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
753 TTPT->getDepth(), TTPT->getIndex(),
754 TTPT->getName(),
755 D->wasDeclaredWithTypename(),
756 D->isParameterPack());
757
758 if (D->hasDefaultArgument()) {
759 QualType DefaultPattern = D->getDefaultArgument();
760 QualType DefaultInst
John McCallce3ff2b2009-08-25 22:02:44 +0000761 = SemaRef.SubstType(DefaultPattern, TemplateArgs,
762 D->getDefaultArgumentLoc(),
763 D->getDeclName());
Mike Stump1eb44332009-09-09 15:08:12 +0000764
John McCalle29ba202009-08-20 01:44:21 +0000765 Inst->setDefaultArgument(DefaultInst,
766 D->getDefaultArgumentLoc(),
767 D->defaultArgumentWasInherited() /* preserve? */);
768 }
769
770 return Inst;
771}
772
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000773Decl *
774TemplateDeclInstantiator::VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000775 NestedNameSpecifier *NNS =
776 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
777 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000778 TemplateArgs);
779 if (!NNS)
780 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000781
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000782 CXXScopeSpec SS;
783 SS.setRange(D->getTargetNestedNameRange());
784 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +0000785
786 NamedDecl *UD =
787 SemaRef.BuildUsingDeclaration(D->getLocation(), SS,
788 D->getTargetNameLocation(),
Anders Carlsson0d8df782009-08-29 19:37:28 +0000789 D->getTargetName(), 0, D->isTypeName());
790 if (UD)
Mike Stump1eb44332009-09-09 15:08:12 +0000791 SemaRef.Context.setInstantiatedFromUnresolvedUsingDecl(cast<UsingDecl>(UD),
Anders Carlsson0d8df782009-08-29 19:37:28 +0000792 D);
793 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000794}
795
John McCallce3ff2b2009-08-25 22:02:44 +0000796Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000797 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +0000798 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000799 return Instantiator.Visit(D);
800}
801
John McCalle29ba202009-08-20 01:44:21 +0000802/// \brief Instantiates a nested template parameter list in the current
803/// instantiation context.
804///
805/// \param L The parameter list to instantiate
806///
807/// \returns NULL if there was an error
808TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +0000809TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +0000810 // Get errors for all the parameters before bailing out.
811 bool Invalid = false;
812
813 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000814 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +0000815 ParamVector Params;
816 Params.reserve(N);
817 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
818 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000819 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +0000820 Params.push_back(D);
821 Invalid = Invalid || !D;
822 }
823
824 // Clean up if we had an error.
825 if (Invalid) {
826 for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
827 PI != PE; ++PI)
828 if (*PI)
829 (*PI)->Destroy(SemaRef.Context);
830 return NULL;
831 }
832
833 TemplateParameterList *InstL
834 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
835 L->getLAngleLoc(), &Params.front(), N,
836 L->getRAngleLoc());
837 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +0000838}
John McCalle29ba202009-08-20 01:44:21 +0000839
John McCallce3ff2b2009-08-25 22:02:44 +0000840/// \brief Does substitution on the type of the given function, including
841/// all of the function parameters.
Douglas Gregor5545e162009-03-24 00:38:23 +0000842///
John McCallce3ff2b2009-08-25 22:02:44 +0000843/// \param D The function whose type will be the basis of the substitution
Douglas Gregor5545e162009-03-24 00:38:23 +0000844///
845/// \param Params the instantiated parameter declarations
846
John McCallce3ff2b2009-08-25 22:02:44 +0000847/// \returns the instantiated function's type if successful, a NULL
Douglas Gregor5545e162009-03-24 00:38:23 +0000848/// type if there was an error.
Mike Stump1eb44332009-09-09 15:08:12 +0000849QualType
John McCallce3ff2b2009-08-25 22:02:44 +0000850TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +0000851 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
852 bool InvalidDecl = false;
853
John McCallce3ff2b2009-08-25 22:02:44 +0000854 // Substitute all of the function's formal parameter types.
Douglas Gregor7e063902009-05-11 23:53:27 +0000855 TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000856 llvm::SmallVector<QualType, 4> ParamTys;
Mike Stump1eb44332009-09-09 15:08:12 +0000857 for (FunctionDecl::param_iterator P = D->param_begin(),
Douglas Gregor5545e162009-03-24 00:38:23 +0000858 PEnd = D->param_end();
859 P != PEnd; ++P) {
Douglas Gregor6477b692009-03-25 15:04:13 +0000860 if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
Douglas Gregor5545e162009-03-24 00:38:23 +0000861 if (PInst->getType()->isVoidType()) {
862 SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
863 PInst->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000864 } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000865 PInst->getType(),
866 diag::err_abstract_type_in_decl,
867 Sema::AbstractParamType))
Douglas Gregor5545e162009-03-24 00:38:23 +0000868 PInst->setInvalidDecl();
869
870 Params.push_back(PInst);
871 ParamTys.push_back(PInst->getType());
872
873 if (PInst->isInvalidDecl())
874 InvalidDecl = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000875 } else
Douglas Gregor5545e162009-03-24 00:38:23 +0000876 InvalidDecl = true;
877 }
878
879 // FIXME: Deallocate dead declarations.
880 if (InvalidDecl)
881 return QualType();
882
John McCall183700f2009-09-21 23:43:11 +0000883 const FunctionProtoType *Proto = D->getType()->getAs<FunctionProtoType>();
Douglas Gregor5545e162009-03-24 00:38:23 +0000884 assert(Proto && "Missing prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +0000885 QualType ResultType
John McCallce3ff2b2009-08-25 22:02:44 +0000886 = SemaRef.SubstType(Proto->getResultType(), TemplateArgs,
887 D->getLocation(), D->getDeclName());
Douglas Gregor5545e162009-03-24 00:38:23 +0000888 if (ResultType.isNull())
889 return QualType();
890
Jay Foadbeaaccd2009-05-21 09:52:38 +0000891 return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
Douglas Gregor5545e162009-03-24 00:38:23 +0000892 Proto->isVariadic(), Proto->getTypeQuals(),
893 D->getLocation(), D->getDeclName());
894}
895
Mike Stump1eb44332009-09-09 15:08:12 +0000896/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +0000897/// declaration (New) from the corresponding fields of its template (Tmpl).
898///
899/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +0000900bool
901TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +0000902 FunctionDecl *Tmpl) {
903 if (Tmpl->isDeleted())
904 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +0000905
Douglas Gregorcca9e962009-07-01 22:01:06 +0000906 // If we are performing substituting explicitly-specified template arguments
907 // or deduced template arguments into a function template and we reach this
908 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +0000909 // to keeping the new function template specialization. We therefore
910 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +0000911 // into a template instantiation for this specific function template
912 // specialization, which is not a SFINAE context, so that we diagnose any
913 // further errors in the declaration itself.
914 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
915 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
916 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
917 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +0000918 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +0000919 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000920 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +0000921 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +0000922 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +0000923 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
924 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
925 }
926 }
Mike Stump1eb44332009-09-09 15:08:12 +0000927
Douglas Gregore53060f2009-06-25 22:08:12 +0000928 return false;
929}
930
Douglas Gregor5545e162009-03-24 00:38:23 +0000931/// \brief Initializes common fields of an instantiated method
932/// declaration (New) from the corresponding fields of its template
933/// (Tmpl).
934///
935/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +0000936bool
937TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +0000938 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +0000939 if (InitFunctionInstantiation(New, Tmpl))
940 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Douglas Gregor5545e162009-03-24 00:38:23 +0000942 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
943 New->setAccess(Tmpl->getAccess());
Anders Carlsson77b7f1d2009-05-14 22:15:41 +0000944 if (Tmpl->isVirtualAsWritten()) {
945 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +0000946 Record->setAggregate(false);
947 Record->setPOD(false);
Eli Friedman1d954f62009-08-15 21:55:26 +0000948 Record->setEmpty(false);
Douglas Gregor5545e162009-03-24 00:38:23 +0000949 Record->setPolymorphic(true);
950 }
Douglas Gregor5545e162009-03-24 00:38:23 +0000951 if (Tmpl->isPure()) {
952 New->setPure();
953 Record->setAbstract(true);
954 }
955
956 // FIXME: attributes
957 // FIXME: New needs a pointer to Tmpl
958 return false;
959}
Douglas Gregora58861f2009-05-13 20:28:22 +0000960
961/// \brief Instantiate the definition of the given function from its
962/// template.
963///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +0000964/// \param PointOfInstantiation the point at which the instantiation was
965/// required. Note that this is not precisely a "point of instantiation"
966/// for the function, but it's close.
967///
Douglas Gregora58861f2009-05-13 20:28:22 +0000968/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +0000969/// function template specialization or member function of a class template
970/// specialization.
971///
972/// \param Recursive if true, recursively instantiates any functions that
973/// are required by this instantiation.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000974void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +0000975 FunctionDecl *Function,
976 bool Recursive) {
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000977 if (Function->isInvalidDecl())
978 return;
979
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000980 assert(!Function->getBody() && "Already instantiated!");
Mike Stump1eb44332009-09-09 15:08:12 +0000981
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000982 // Never instantiate an explicit specialization.
983 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
984 return;
985
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000986 // Find the function body that we'll be substituting.
Douglas Gregor1637be72009-06-26 00:10:03 +0000987 const FunctionDecl *PatternDecl = 0;
Douglas Gregor5ec178f2009-08-28 21:09:48 +0000988 if (FunctionTemplateDecl *Primary = Function->getPrimaryTemplate()) {
989 while (Primary->getInstantiatedFromMemberTemplate())
990 Primary = Primary->getInstantiatedFromMemberTemplate();
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Douglas Gregor1637be72009-06-26 00:10:03 +0000992 PatternDecl = Primary->getTemplatedDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000993 } else
Douglas Gregor1637be72009-06-26 00:10:03 +0000994 PatternDecl = Function->getInstantiatedFromMemberFunction();
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000995 Stmt *Pattern = 0;
996 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000997 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000998
999 if (!Pattern)
1000 return;
1001
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001002 // C++0x [temp.explicit]p9:
1003 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00001004 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001005 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00001006 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001007 == TSK_ExplicitInstantiationDeclaration &&
1008 PatternDecl->isOutOfLine() && !PatternDecl->isInline())
1009 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001010
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001011 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
1012 if (Inst)
1013 return;
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001014
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001015 // If we're performing recursive template instantiation, create our own
1016 // queue of pending implicit instantiations that we will instantiate later,
1017 // while we're still within our own instantiation context.
1018 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1019 if (Recursive)
1020 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001021
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001022 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
1023
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001024 // Introduce a new scope where local variable instantiations will be
1025 // recorded.
1026 LocalInstantiationScope Scope(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001027
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001028 // Introduce the instantiated function parameters into the local
1029 // instantiation scope.
1030 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
1031 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
1032 Function->getParamDecl(I));
1033
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001034 // Enter the scope of this instantiation. We don't use
1035 // PushDeclContext because we don't have a scope.
1036 DeclContext *PreviousContext = CurContext;
1037 CurContext = Function;
1038
Mike Stump1eb44332009-09-09 15:08:12 +00001039 MultiLevelTemplateArgumentList TemplateArgs =
Anders Carlsson09025312009-08-29 05:16:22 +00001040 getTemplateInstantiationArgs(Function);
1041
1042 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001043 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00001044 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
1045 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
1046 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001047 }
1048
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001049 // Instantiate the function body.
Anders Carlsson09025312009-08-29 05:16:22 +00001050 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001051
Douglas Gregor52604ab2009-09-11 21:19:12 +00001052 if (Body.isInvalid())
1053 Function->setInvalidDecl();
1054
Mike Stump1eb44332009-09-09 15:08:12 +00001055 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001056 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001057
1058 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00001059
1060 DeclGroupRef DG(Function);
1061 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001062
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001063 if (Recursive) {
1064 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001065 // instantiation of this template.
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001066 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001067
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001068 // Restore the set of pending implicit instantiations.
1069 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1070 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001071}
1072
1073/// \brief Instantiate the definition of the given variable from its
1074/// template.
1075///
Douglas Gregor7caa6822009-07-24 20:34:43 +00001076/// \param PointOfInstantiation the point at which the instantiation was
1077/// required. Note that this is not precisely a "point of instantiation"
1078/// for the function, but it's close.
1079///
1080/// \param Var the already-instantiated declaration of a static member
1081/// variable of a class template specialization.
1082///
1083/// \param Recursive if true, recursively instantiates any functions that
1084/// are required by this instantiation.
1085void Sema::InstantiateStaticDataMemberDefinition(
1086 SourceLocation PointOfInstantiation,
1087 VarDecl *Var,
1088 bool Recursive) {
1089 if (Var->isInvalidDecl())
1090 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001091
Douglas Gregor7caa6822009-07-24 20:34:43 +00001092 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001093 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
1094 bool FoundOutOfLineDef = false;
1095 assert(Def && "This data member was not instantiated from a template?");
Mike Stump1eb44332009-09-09 15:08:12 +00001096 assert(Def->isStaticDataMember() && "Not a static data member?");
1097 for (VarDecl::redecl_iterator RD = Def->redecls_begin(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00001098 RDEnd = Def->redecls_end();
1099 RD != RDEnd; ++RD) {
1100 if (RD->getLexicalDeclContext()->isFileContext()) {
1101 Def = *RD;
1102 FoundOutOfLineDef = true;
1103 }
1104 }
Mike Stump1eb44332009-09-09 15:08:12 +00001105
Douglas Gregor7caa6822009-07-24 20:34:43 +00001106 if (!FoundOutOfLineDef) {
1107 // We did not find an out-of-line definition of this static data member,
1108 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00001109 // instantiate this definition (or provide a specialization for it) in
1110 // another translation unit.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001111 return;
1112 }
1113
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001114 // Never instantiate an explicit specialization.
1115 if (Def->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1116 return;
1117
1118 // C++0x [temp.explicit]p9:
1119 // Except for inline functions, other explicit instantiation declarations
1120 // have the effect of suppressing the implicit instantiation of the entity
1121 // to which they refer.
1122 if (Def->getTemplateSpecializationKind()
1123 == TSK_ExplicitInstantiationDeclaration)
1124 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001125
Douglas Gregor7caa6822009-07-24 20:34:43 +00001126 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
1127 if (Inst)
1128 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001129
Douglas Gregor7caa6822009-07-24 20:34:43 +00001130 // If we're performing recursive template instantiation, create our own
1131 // queue of pending implicit instantiations that we will instantiate later,
1132 // while we're still within our own instantiation context.
1133 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1134 if (Recursive)
1135 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001136
Douglas Gregor7caa6822009-07-24 20:34:43 +00001137 // Enter the scope of this instantiation. We don't use
1138 // PushDeclContext because we don't have a scope.
1139 DeclContext *PreviousContext = CurContext;
1140 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001141
John McCallce3ff2b2009-08-25 22:02:44 +00001142 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00001143 getTemplateInstantiationArgs(Var)));
Mike Stump1eb44332009-09-09 15:08:12 +00001144
Douglas Gregor7caa6822009-07-24 20:34:43 +00001145 CurContext = PreviousContext;
1146
1147 if (Var) {
1148 DeclGroupRef DG(Var);
1149 Consumer.HandleTopLevelDecl(DG);
1150 }
Mike Stump1eb44332009-09-09 15:08:12 +00001151
Douglas Gregor7caa6822009-07-24 20:34:43 +00001152 if (Recursive) {
1153 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001154 // instantiation of this template.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001155 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001156
Douglas Gregor7caa6822009-07-24 20:34:43 +00001157 // Restore the set of pending implicit instantiations.
1158 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001159 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001160}
Douglas Gregor815215d2009-05-27 05:35:12 +00001161
Anders Carlsson09025312009-08-29 05:16:22 +00001162void
1163Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
1164 const CXXConstructorDecl *Tmpl,
1165 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001166
Anders Carlsson09025312009-08-29 05:16:22 +00001167 llvm::SmallVector<MemInitTy*, 4> NewInits;
1168
1169 // Instantiate all the initializers.
1170 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00001171 InitsEnd = Tmpl->init_end();
1172 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00001173 CXXBaseOrMemberInitializer *Init = *Inits;
1174
1175 ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001176
Anders Carlsson09025312009-08-29 05:16:22 +00001177 // Instantiate all the arguments.
1178 for (ExprIterator Args = Init->arg_begin(), ArgsEnd = Init->arg_end();
1179 Args != ArgsEnd; ++Args) {
1180 OwningExprResult NewArg = SubstExpr(*Args, TemplateArgs);
1181
1182 if (NewArg.isInvalid())
1183 New->setInvalidDecl();
1184 else
1185 NewArgs.push_back(NewArg.takeAs<Expr>());
1186 }
1187
1188 MemInitResult NewInit;
1189
1190 if (Init->isBaseInitializer()) {
Eli Friedmanc5573a82009-08-29 22:22:07 +00001191 QualType BaseType(Init->getBaseClass(), 0);
1192 BaseType = SubstType(BaseType, TemplateArgs, Init->getSourceLocation(),
1193 New->getDeclName());
Anders Carlsson09025312009-08-29 05:16:22 +00001194
1195 NewInit = BuildBaseInitializer(BaseType,
Mike Stump1eb44332009-09-09 15:08:12 +00001196 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001197 NewArgs.size(),
1198 Init->getSourceLocation(),
1199 Init->getRParenLoc(),
1200 New->getParent());
1201 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001202 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00001203
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001204 // Is this an anonymous union?
1205 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregore95b4092009-09-16 18:34:49 +00001206 Member = cast<FieldDecl>(FindInstantiatedDecl(UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001207 else
Douglas Gregore95b4092009-09-16 18:34:49 +00001208 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMember(),
1209 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00001210
1211 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001212 NewArgs.size(),
1213 Init->getSourceLocation(),
1214 Init->getRParenLoc());
1215 }
1216
1217 if (NewInit.isInvalid())
1218 New->setInvalidDecl();
1219 else {
1220 // FIXME: It would be nice if ASTOwningVector had a release function.
1221 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001222
Anders Carlsson09025312009-08-29 05:16:22 +00001223 NewInits.push_back((MemInitTy *)NewInit.get());
1224 }
1225 }
Mike Stump1eb44332009-09-09 15:08:12 +00001226
Anders Carlsson09025312009-08-29 05:16:22 +00001227 // Assign all the initializers to the new constructor.
Mike Stump1eb44332009-09-09 15:08:12 +00001228 ActOnMemInitializers(DeclPtrTy::make(New),
Anders Carlsson09025312009-08-29 05:16:22 +00001229 /*FIXME: ColonLoc */
1230 SourceLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001231 NewInits.data(), NewInits.size());
Anders Carlsson09025312009-08-29 05:16:22 +00001232}
1233
John McCall52a575a2009-08-29 08:11:13 +00001234// TODO: this could be templated if the various decl types used the
1235// same method name.
1236static bool isInstantiationOf(ClassTemplateDecl *Pattern,
1237 ClassTemplateDecl *Instance) {
1238 Pattern = Pattern->getCanonicalDecl();
1239
1240 do {
1241 Instance = Instance->getCanonicalDecl();
1242 if (Pattern == Instance) return true;
1243 Instance = Instance->getInstantiatedFromMemberTemplate();
1244 } while (Instance);
1245
1246 return false;
1247}
1248
Douglas Gregor0d696532009-09-28 06:34:35 +00001249static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
1250 FunctionTemplateDecl *Instance) {
1251 Pattern = Pattern->getCanonicalDecl();
1252
1253 do {
1254 Instance = Instance->getCanonicalDecl();
1255 if (Pattern == Instance) return true;
1256 Instance = Instance->getInstantiatedFromMemberTemplate();
1257 } while (Instance);
1258
1259 return false;
1260}
1261
John McCall52a575a2009-08-29 08:11:13 +00001262static bool isInstantiationOf(CXXRecordDecl *Pattern,
1263 CXXRecordDecl *Instance) {
1264 Pattern = Pattern->getCanonicalDecl();
1265
1266 do {
1267 Instance = Instance->getCanonicalDecl();
1268 if (Pattern == Instance) return true;
1269 Instance = Instance->getInstantiatedFromMemberClass();
1270 } while (Instance);
1271
1272 return false;
1273}
1274
1275static bool isInstantiationOf(FunctionDecl *Pattern,
1276 FunctionDecl *Instance) {
1277 Pattern = Pattern->getCanonicalDecl();
1278
1279 do {
1280 Instance = Instance->getCanonicalDecl();
1281 if (Pattern == Instance) return true;
1282 Instance = Instance->getInstantiatedFromMemberFunction();
1283 } while (Instance);
1284
1285 return false;
1286}
1287
1288static bool isInstantiationOf(EnumDecl *Pattern,
1289 EnumDecl *Instance) {
1290 Pattern = Pattern->getCanonicalDecl();
1291
1292 do {
1293 Instance = Instance->getCanonicalDecl();
1294 if (Pattern == Instance) return true;
1295 Instance = Instance->getInstantiatedFromMemberEnum();
1296 } while (Instance);
1297
1298 return false;
1299}
1300
Anders Carlsson0d8df782009-08-29 19:37:28 +00001301static bool isInstantiationOf(UnresolvedUsingDecl *Pattern,
1302 UsingDecl *Instance,
1303 ASTContext &C) {
1304 return C.getInstantiatedFromUnresolvedUsingDecl(Instance) == Pattern;
1305}
1306
John McCall52a575a2009-08-29 08:11:13 +00001307static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
1308 VarDecl *Instance) {
1309 assert(Instance->isStaticDataMember());
1310
1311 Pattern = Pattern->getCanonicalDecl();
1312
1313 do {
1314 Instance = Instance->getCanonicalDecl();
1315 if (Pattern == Instance) return true;
1316 Instance = Instance->getInstantiatedFromStaticDataMember();
1317 } while (Instance);
1318
1319 return false;
1320}
1321
Douglas Gregor815215d2009-05-27 05:35:12 +00001322static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00001323 if (D->getKind() != Other->getKind()) {
1324 if (UnresolvedUsingDecl *UUD = dyn_cast<UnresolvedUsingDecl>(D)) {
1325 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
1326 return isInstantiationOf(UUD, UD, Ctx);
1327 }
1328 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001329
Anders Carlsson0d8df782009-08-29 19:37:28 +00001330 return false;
1331 }
Mike Stump1eb44332009-09-09 15:08:12 +00001332
John McCall52a575a2009-08-29 08:11:13 +00001333 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
1334 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001335
John McCall52a575a2009-08-29 08:11:13 +00001336 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
1337 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00001338
John McCall52a575a2009-08-29 08:11:13 +00001339 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
1340 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00001341
Douglas Gregor7caa6822009-07-24 20:34:43 +00001342 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00001343 if (Var->isStaticDataMember())
1344 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
1345
1346 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
1347 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00001348
Douglas Gregor0d696532009-09-28 06:34:35 +00001349 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
1350 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
1351
Anders Carlssond8b285f2009-09-01 04:26:58 +00001352 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
1353 if (!Field->getDeclName()) {
1354 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00001355 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00001356 cast<FieldDecl>(D);
1357 }
1358 }
Mike Stump1eb44332009-09-09 15:08:12 +00001359
Douglas Gregor815215d2009-05-27 05:35:12 +00001360 return D->getDeclName() && isa<NamedDecl>(Other) &&
1361 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
1362}
1363
1364template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00001365static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00001366 NamedDecl *D,
1367 ForwardIterator first,
1368 ForwardIterator last) {
1369 for (; first != last; ++first)
1370 if (isInstantiationOf(Ctx, D, *first))
1371 return cast<NamedDecl>(*first);
1372
1373 return 0;
1374}
1375
John McCall02cace72009-08-28 07:59:38 +00001376/// \brief Finds the instantiation of the given declaration context
1377/// within the current instantiation.
1378///
1379/// \returns NULL if there was an error
Douglas Gregore95b4092009-09-16 18:34:49 +00001380DeclContext *Sema::FindInstantiatedContext(DeclContext* DC,
1381 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00001382 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00001383 Decl* ID = FindInstantiatedDecl(D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00001384 return cast_or_null<DeclContext>(ID);
1385 } else return DC;
1386}
1387
Douglas Gregored961e72009-05-27 17:54:46 +00001388/// \brief Find the instantiation of the given declaration within the
1389/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00001390///
1391/// This routine is intended to be used when \p D is a declaration
1392/// referenced from within a template, that needs to mapped into the
1393/// corresponding declaration within an instantiation. For example,
1394/// given:
1395///
1396/// \code
1397/// template<typename T>
1398/// struct X {
1399/// enum Kind {
1400/// KnownValue = sizeof(T)
1401/// };
1402///
1403/// bool getKind() const { return KnownValue; }
1404/// };
1405///
1406/// template struct X<int>;
1407/// \endcode
1408///
1409/// In the instantiation of X<int>::getKind(), we need to map the
1410/// EnumConstantDecl for KnownValue (which refers to
1411/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00001412/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
1413/// this mapping from within the instantiation of X<int>.
Douglas Gregore95b4092009-09-16 18:34:49 +00001414NamedDecl *Sema::FindInstantiatedDecl(NamedDecl *D,
1415 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor44c73842009-09-01 17:53:10 +00001416 if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D)) {
1417 // Transform all of the elements of the overloaded function set.
Mike Stump1eb44332009-09-09 15:08:12 +00001418 OverloadedFunctionDecl *Result
Douglas Gregor44c73842009-09-01 17:53:10 +00001419 = OverloadedFunctionDecl::Create(Context, CurContext, Ovl->getDeclName());
Mike Stump1eb44332009-09-09 15:08:12 +00001420
Douglas Gregor44c73842009-09-01 17:53:10 +00001421 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
1422 FEnd = Ovl->function_end();
1423 F != FEnd; ++F) {
1424 Result->addOverload(
Douglas Gregore95b4092009-09-16 18:34:49 +00001425 AnyFunctionDecl::getFromNamedDecl(FindInstantiatedDecl(*F,
1426 TemplateArgs)));
Douglas Gregor44c73842009-09-01 17:53:10 +00001427 }
Mike Stump1eb44332009-09-09 15:08:12 +00001428
Douglas Gregor44c73842009-09-01 17:53:10 +00001429 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001430 }
1431
Douglas Gregor815215d2009-05-27 05:35:12 +00001432 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor2bba76b2009-05-27 17:07:49 +00001433 if (isa<ParmVarDecl>(D) || ParentDC->isFunctionOrMethod()) {
1434 // D is a local of some kind. Look into the map of local
1435 // declarations to their instantiations.
1436 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
1437 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001438
Douglas Gregore95b4092009-09-16 18:34:49 +00001439 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
1440 if (!Record->isDependentContext())
1441 return D;
1442
1443 // If the RecordDecl is actually the injected-class-name or a "templated"
1444 // declaration for a class template or class template partial
1445 // specialization, substitute into the injected-class-name of the
1446 // class template or partial specialization to find the new DeclContext.
1447 QualType T;
1448 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
1449
1450 if (ClassTemplate) {
1451 T = ClassTemplate->getInjectedClassNameType(Context);
1452 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
1453 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1454 T = Context.getTypeDeclType(Record);
1455 ClassTemplate = PartialSpec->getSpecializedTemplate();
1456 }
1457
1458 if (!T.isNull()) {
1459 // Substitute into the injected-class-name to get the type corresponding
1460 // to the instantiation we want. This substitution should never fail,
1461 // since we know we can instantiate the injected-class-name or we wouldn't
1462 // have gotten to the injected-class-name!
1463 // FIXME: Can we use the CurrentInstantiationScope to avoid this extra
1464 // instantiation in the common case?
1465 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
1466 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
1467
1468 if (!T->isDependentType()) {
1469 assert(T->isRecordType() && "Instantiation must produce a record type");
1470 return T->getAs<RecordType>()->getDecl();
1471 }
1472
1473 // We are performing "partial" template instantiation to create the
1474 // member declarations for the members of a class template
1475 // specialization. Therefore, D is actually referring to something in
1476 // the current instantiation. Look through the current context,
1477 // which contains actual instantiations, to find the instantiation of
1478 // the "current instantiation" that D refers to.
Mike Stump1eb44332009-09-09 15:08:12 +00001479 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00001480 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001481 if (ClassTemplateSpecializationDecl *Spec
John McCall52a575a2009-08-29 08:11:13 +00001482 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00001483 if (isInstantiationOf(ClassTemplate,
1484 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00001485 return Spec;
1486 }
1487
Mike Stump1eb44332009-09-09 15:08:12 +00001488 assert(false &&
John McCall52a575a2009-08-29 08:11:13 +00001489 "Unable to find declaration for the current instantiation");
Douglas Gregore95b4092009-09-16 18:34:49 +00001490 return Record;
John McCall52a575a2009-08-29 08:11:13 +00001491 }
Douglas Gregore95b4092009-09-16 18:34:49 +00001492
1493 // Fall through to deal with other dependent record types (e.g.,
1494 // anonymous unions in class templates).
1495 }
John McCall52a575a2009-08-29 08:11:13 +00001496
Douglas Gregore95b4092009-09-16 18:34:49 +00001497 if (!ParentDC->isDependentContext())
1498 return D;
1499
1500 ParentDC = FindInstantiatedContext(ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001501 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00001502 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001503
Douglas Gregor815215d2009-05-27 05:35:12 +00001504 if (ParentDC != D->getDeclContext()) {
1505 // We performed some kind of instantiation in the parent context,
1506 // so now we need to look into the instantiated parent context to
1507 // find the instantiation of the declaration D.
1508 NamedDecl *Result = 0;
1509 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001510 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00001511 Result = findInstantiationOf(Context, D, Found.first, Found.second);
1512 } else {
1513 // Since we don't have a name for the entity we're looking for,
1514 // our only option is to walk through all of the declarations to
1515 // find that name. This will occur in a few cases:
1516 //
1517 // - anonymous struct/union within a template
1518 // - unnamed class/struct/union/enum within a template
1519 //
1520 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00001521 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001522 ParentDC->decls_begin(),
1523 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00001524 }
Mike Stump1eb44332009-09-09 15:08:12 +00001525
Douglas Gregor815215d2009-05-27 05:35:12 +00001526 assert(Result && "Unable to find instantiation of declaration!");
1527 D = Result;
1528 }
1529
Douglas Gregor815215d2009-05-27 05:35:12 +00001530 return D;
1531}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001532
Mike Stump1eb44332009-09-09 15:08:12 +00001533/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001534/// instantiations we have seen until this point.
1535void Sema::PerformPendingImplicitInstantiations() {
1536 while (!PendingImplicitInstantiations.empty()) {
1537 PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front();
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001538 PendingImplicitInstantiations.pop_front();
Mike Stump1eb44332009-09-09 15:08:12 +00001539
Douglas Gregor7caa6822009-07-24 20:34:43 +00001540 // Instantiate function definitions
1541 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001542 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001543 Function->getLocation(), *this,
1544 Context.getSourceManager(),
1545 "instantiating function definition");
Mike Stump1eb44332009-09-09 15:08:12 +00001546
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001547 if (!Function->getBody())
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001548 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001549 continue;
1550 }
Mike Stump1eb44332009-09-09 15:08:12 +00001551
Douglas Gregor7caa6822009-07-24 20:34:43 +00001552 // Instantiate static data member definitions.
1553 VarDecl *Var = cast<VarDecl>(Inst.first);
1554 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001555
Mike Stump1eb44332009-09-09 15:08:12 +00001556 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001557 Var->getLocation(), *this,
1558 Context.getSourceManager(),
1559 "instantiating static data member "
1560 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00001561
Douglas Gregor7caa6822009-07-24 20:34:43 +00001562 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001563 }
1564}