blob: 5ae7289ea56202f61028fd3a20b4c28e973af908 [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
Anders Carlssond8fe2d52009-11-07 06:07:58 +000031 void InstantiateAttrs(Decl *Tmpl, Decl *New);
32
Douglas Gregor8dbc2692009-03-17 21:15:40 +000033 public:
34 typedef Sema::OwningExprResult OwningExprResult;
35
36 TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +000037 const MultiLevelTemplateArgumentList &TemplateArgs)
Douglas Gregor7e063902009-05-11 23:53:27 +000038 : SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs) { }
Mike Stump1eb44332009-09-09 15:08:12 +000039
Mike Stump390b4cc2009-05-16 07:39:55 +000040 // FIXME: Once we get closer to completion, replace these manually-written
41 // declarations with automatically-generated ones from
42 // clang/AST/DeclNodes.def.
Douglas Gregor4f722be2009-03-25 15:45:12 +000043 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
44 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000045 Decl *VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +000046 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000047 Decl *VisitFieldDecl(FieldDecl *D);
48 Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
49 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor6477b692009-03-25 15:04:13 +000050 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
John McCall02cace72009-08-28 07:59:38 +000051 Decl *VisitFriendDecl(FriendDecl *D);
Douglas Gregora735b202009-10-13 14:39:41 +000052 Decl *VisitFunctionDecl(FunctionDecl *D,
53 TemplateParameterList *TemplateParams = 0);
Douglas Gregord475b8d2009-03-25 21:17:03 +000054 Decl *VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregord60e1052009-08-27 16:57:43 +000055 Decl *VisitCXXMethodDecl(CXXMethodDecl *D,
56 TemplateParameterList *TemplateParams = 0);
Douglas Gregor615c5d42009-03-24 16:43:20 +000057 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
Douglas Gregor03b2b072009-03-24 00:15:49 +000058 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +000059 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor6477b692009-03-25 15:04:13 +000060 ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D);
John McCalle29ba202009-08-20 01:44:21 +000061 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregor7974c3b2009-10-07 17:21:34 +000062 Decl *VisitClassTemplatePartialSpecializationDecl(
63 ClassTemplatePartialSpecializationDecl *D);
Douglas Gregord60e1052009-08-27 16:57:43 +000064 Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
John McCalle29ba202009-08-20 01:44:21 +000065 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Douglas Gregor33642df2009-10-23 23:25:44 +000066 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
Anders Carlsson0dde18e2009-08-28 15:18:15 +000067 Decl *VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D);
Mike Stump1eb44332009-09-09 15:08:12 +000068
Douglas Gregor8dbc2692009-03-17 21:15:40 +000069 // Base case. FIXME: Remove once we can instantiate everything.
Mike Stump1eb44332009-09-09 15:08:12 +000070 Decl *VisitDecl(Decl *) {
Douglas Gregor3d7a12a2009-03-25 23:32:15 +000071 assert(false && "Template instantiation of unknown declaration kind!");
Douglas Gregor8dbc2692009-03-17 21:15:40 +000072 return 0;
73 }
Douglas Gregor5545e162009-03-24 00:38:23 +000074
John McCallfd810b12009-08-14 02:03:10 +000075 const LangOptions &getLangOptions() {
76 return SemaRef.getLangOptions();
77 }
78
Douglas Gregor5545e162009-03-24 00:38:23 +000079 // Helper functions for instantiating methods.
John McCallce3ff2b2009-08-25 22:02:44 +000080 QualType SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +000081 llvm::SmallVectorImpl<ParmVarDecl *> &Params);
Douglas Gregore53060f2009-06-25 22:08:12 +000082 bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
Douglas Gregor5545e162009-03-24 00:38:23 +000083 bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
John McCalle29ba202009-08-20 01:44:21 +000084
85 TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +000086 SubstTemplateParams(TemplateParameterList *List);
Douglas Gregored9c0f92009-10-29 00:04:11 +000087
88 bool InstantiateClassTemplatePartialSpecialization(
89 ClassTemplateDecl *ClassTemplate,
90 ClassTemplatePartialSpecializationDecl *PartialSpec);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000091 };
92}
93
Anders Carlssond8fe2d52009-11-07 06:07:58 +000094// FIXME: Is this too simple?
95void TemplateDeclInstantiator::InstantiateAttrs(Decl *Tmpl, Decl *New) {
96 for (const Attr *TmplAttr = Tmpl->getAttrs(); TmplAttr;
97 TmplAttr = TmplAttr->getNext()) {
98
99 // FIXME: Is cloning correct for all attributes?
100 Attr *NewAttr = TmplAttr->clone(SemaRef.Context);
101
102 New->addAttr(NewAttr);
103 }
104}
105
Douglas Gregor4f722be2009-03-25 15:45:12 +0000106Decl *
107TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
108 assert(false && "Translation units cannot be instantiated");
109 return D;
110}
111
112Decl *
113TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
114 assert(false && "Namespaces cannot be instantiated");
115 return D;
116}
117
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000118Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
119 bool Invalid = false;
John McCallba6a9bd2009-10-24 08:00:42 +0000120 DeclaratorInfo *DI = D->getTypeDeclaratorInfo();
121 if (DI->getType()->isDependentType()) {
122 DI = SemaRef.SubstType(DI, TemplateArgs,
123 D->getLocation(), D->getDeclName());
124 if (!DI) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000125 Invalid = true;
John McCallba6a9bd2009-10-24 08:00:42 +0000126 DI = SemaRef.Context.getTrivialDeclaratorInfo(SemaRef.Context.IntTy);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000127 }
128 }
Mike Stump1eb44332009-09-09 15:08:12 +0000129
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000130 // Create the new typedef
131 TypedefDecl *Typedef
132 = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
John McCallba6a9bd2009-10-24 08:00:42 +0000133 D->getIdentifier(), DI);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000134 if (Invalid)
135 Typedef->setInvalidDecl();
136
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000137 Owner->addDecl(Typedef);
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000139 return Typedef;
140}
141
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000142Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000143 // Do substitution on the type of the declaration
John McCall0a5fa062009-10-21 02:39:02 +0000144 DeclaratorInfo *DI = SemaRef.SubstType(D->getDeclaratorInfo(),
145 TemplateArgs,
146 D->getTypeSpecStartLoc(),
147 D->getDeclName());
148 if (!DI)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000149 return 0;
150
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000151 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000152 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
153 D->getLocation(), D->getIdentifier(),
John McCall0a5fa062009-10-21 02:39:02 +0000154 DI->getType(), DI,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000155 D->getStorageClass());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000156 Var->setThreadSpecified(D->isThreadSpecified());
157 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
158 Var->setDeclaredInCondition(D->isDeclaredInCondition());
Mike Stump1eb44332009-09-09 15:08:12 +0000159
160 // If we are instantiating a static data member defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000161 // out-of-line, the instantiation will have the same lexical
162 // context (which will be a namespace scope) as the template.
163 if (D->isOutOfLine())
164 Var->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Mike Stump390b4cc2009-05-16 07:39:55 +0000166 // FIXME: In theory, we could have a previous declaration for variables that
167 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000168 bool Redeclaration = false;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000169 SemaRef.CheckVariableDeclaration(Var, 0, Redeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Douglas Gregor7caa6822009-07-24 20:34:43 +0000171 if (D->isOutOfLine()) {
172 D->getLexicalDeclContext()->addDecl(Var);
173 Owner->makeDeclVisibleInContext(Var);
174 } else {
175 Owner->addDecl(Var);
176 }
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000178 // Link instantiations of static data members back to the template from
179 // which they were instantiated.
180 if (Var->isStaticDataMember())
181 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000182 TSK_ImplicitInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000183
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000184 if (D->getInit()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000185 OwningExprResult Init
John McCallce3ff2b2009-08-25 22:02:44 +0000186 = SemaRef.SubstExpr(D->getInit(), TemplateArgs);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000187 if (Init.isInvalid())
188 Var->setInvalidDecl();
Douglas Gregor83ddad32009-08-26 21:14:46 +0000189 else if (ParenListExpr *PLE = dyn_cast<ParenListExpr>((Expr *)Init.get())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000190 // FIXME: We're faking all of the comma locations, which is suboptimal.
Douglas Gregor83ddad32009-08-26 21:14:46 +0000191 // Do we even need these comma locations?
192 llvm::SmallVector<SourceLocation, 4> FakeCommaLocs;
193 if (PLE->getNumExprs() > 0) {
194 FakeCommaLocs.reserve(PLE->getNumExprs() - 1);
195 for (unsigned I = 0, N = PLE->getNumExprs() - 1; I != N; ++I) {
196 Expr *E = PLE->getExpr(I)->Retain();
197 FakeCommaLocs.push_back(
198 SemaRef.PP.getLocForEndOfToken(E->getLocEnd()));
199 }
Douglas Gregore9f8eb62009-08-26 23:26:04 +0000200 PLE->getExpr(PLE->getNumExprs() - 1)->Retain();
Douglas Gregor83ddad32009-08-26 21:14:46 +0000201 }
Mike Stump1eb44332009-09-09 15:08:12 +0000202
Douglas Gregor83ddad32009-08-26 21:14:46 +0000203 // Add the direct initializer to the declaration.
204 SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
Mike Stump1eb44332009-09-09 15:08:12 +0000205 PLE->getLParenLoc(),
Douglas Gregor83ddad32009-08-26 21:14:46 +0000206 Sema::MultiExprArg(SemaRef,
207 (void**)PLE->getExprs(),
208 PLE->getNumExprs()),
209 FakeCommaLocs.data(),
210 PLE->getRParenLoc());
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Douglas Gregor83ddad32009-08-26 21:14:46 +0000212 // When Init is destroyed, it will destroy the instantiated ParenListExpr;
213 // we've explicitly retained all of its subexpressions already.
214 } else
Chris Lattnerb28317a2009-03-28 19:18:32 +0000215 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init),
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000216 D->hasCXXDirectInitializer());
Douglas Gregor65b90052009-07-27 17:43:39 +0000217 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
218 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000219
220 return Var;
221}
222
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000223Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
224 bool Invalid = false;
John McCall07fb6be2009-10-22 23:33:21 +0000225 DeclaratorInfo *DI = D->getDeclaratorInfo();
226 if (DI->getType()->isDependentType()) {
227 DI = SemaRef.SubstType(DI, TemplateArgs,
228 D->getLocation(), D->getDeclName());
229 if (!DI) {
230 DI = D->getDeclaratorInfo();
231 Invalid = true;
232 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000233 // C++ [temp.arg.type]p3:
234 // If a declaration acquires a function type through a type
235 // dependent on a template-parameter and this causes a
236 // declaration that does not use the syntactic form of a
237 // function declarator to have function type, the program is
238 // ill-formed.
239 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000240 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000241 Invalid = true;
242 }
243 }
244
245 Expr *BitWidth = D->getBitWidth();
246 if (Invalid)
247 BitWidth = 0;
248 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000249 // The bit-width expression is not potentially evaluated.
250 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000252 OwningExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000253 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000254 if (InstantiatedBitWidth.isInvalid()) {
255 Invalid = true;
256 BitWidth = 0;
257 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000258 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000259 }
260
John McCall07fb6be2009-10-22 23:33:21 +0000261 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
262 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000263 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000264 D->getLocation(),
265 D->isMutable(),
266 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000267 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000268 D->getAccess(),
269 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000270 if (!Field) {
271 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000272 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000273 }
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000275 InstantiateAttrs(D, Field);
276
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000277 if (Invalid)
278 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000280 if (!Field->getDeclName()) {
281 // Keep track of where this decl came from.
282 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000283 }
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000285 Field->setImplicit(D->isImplicit());
286 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000287
288 return Field;
289}
290
John McCall02cace72009-08-28 07:59:38 +0000291Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
292 FriendDecl::FriendUnion FU;
293
294 // Handle friend type expressions by simply substituting template
295 // parameters into the pattern type.
296 if (Type *Ty = D->getFriendType()) {
297 QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs,
298 D->getLocation(), DeclarationName());
299 if (T.isNull()) return 0;
300
301 assert(getLangOptions().CPlusPlus0x || T->isRecordType());
302 FU = T.getTypePtr();
303
304 // Handle everything else by appropriate substitution.
305 } else {
306 NamedDecl *ND = D->getFriendDecl();
307 assert(ND && "friend decl must be a decl or a type!");
308
Douglas Gregora735b202009-10-13 14:39:41 +0000309 // FIXME: We have a problem here, because the nested call to Visit(ND)
310 // will inject the thing that the friend references into the current
311 // owner, which is wrong.
John McCall02cace72009-08-28 07:59:38 +0000312 Decl *NewND = Visit(ND);
313 if (!NewND) return 0;
314
315 FU = cast<NamedDecl>(NewND);
John McCallfd810b12009-08-14 02:03:10 +0000316 }
Mike Stump1eb44332009-09-09 15:08:12 +0000317
John McCall02cace72009-08-28 07:59:38 +0000318 FriendDecl *FD =
319 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU,
320 D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000321 FD->setAccess(AS_public);
John McCall02cace72009-08-28 07:59:38 +0000322 Owner->addDecl(FD);
323 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000324}
325
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000326Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
327 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Douglas Gregorac7610d2009-06-22 20:57:11 +0000329 // The expression in a static assertion is not potentially evaluated.
330 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000332 OwningExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000333 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000334 if (InstantiatedAssertExpr.isInvalid())
335 return 0;
336
Douglas Gregor43d9d922009-08-08 01:41:12 +0000337 OwningExprResult Message(SemaRef, D->getMessage());
338 D->getMessage()->Retain();
Mike Stump1eb44332009-09-09 15:08:12 +0000339 Decl *StaticAssert
340 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
Chris Lattnerb28317a2009-03-28 19:18:32 +0000341 move(InstantiatedAssertExpr),
342 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000343 return StaticAssert;
344}
345
346Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000347 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000348 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000349 D->getTagKeywordLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000350 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000351 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000352 Enum->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000353 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000354 Enum->startDefinition();
355
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000356 llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000357
358 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000359 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
360 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000361 EC != ECEnd; ++EC) {
362 // The specified value for the enumerator.
363 OwningExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000364 if (Expr *UninstValue = EC->getInitExpr()) {
365 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000366 EnterExpressionEvaluationContext Unevaluated(SemaRef,
Douglas Gregorac7610d2009-06-22 20:57:11 +0000367 Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000368
John McCallce3ff2b2009-08-25 22:02:44 +0000369 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000370 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000371
372 // Drop the initial value and continue.
373 bool isInvalid = false;
374 if (Value.isInvalid()) {
375 Value = SemaRef.Owned((Expr *)0);
376 isInvalid = true;
377 }
378
Mike Stump1eb44332009-09-09 15:08:12 +0000379 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000380 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
381 EC->getLocation(), EC->getIdentifier(),
382 move(Value));
383
384 if (isInvalid) {
385 if (EnumConst)
386 EnumConst->setInvalidDecl();
387 Enum->setInvalidDecl();
388 }
389
390 if (EnumConst) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000391 Enum->addDecl(EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000392 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000393 LastEnumConst = EnumConst;
394 }
395 }
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000397 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000398 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000399 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
400 Sema::DeclPtrTy::make(Enum),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000401 &Enumerators[0], Enumerators.size(),
402 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000403
404 return Enum;
405}
406
Douglas Gregor6477b692009-03-25 15:04:13 +0000407Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
408 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
409 return 0;
410}
411
Douglas Gregored9c0f92009-10-29 00:04:11 +0000412namespace {
413 class SortDeclByLocation {
414 SourceManager &SourceMgr;
415
416 public:
417 explicit SortDeclByLocation(SourceManager &SourceMgr)
418 : SourceMgr(SourceMgr) { }
419
420 bool operator()(const Decl *X, const Decl *Y) const {
421 return SourceMgr.isBeforeInTranslationUnit(X->getLocation(),
422 Y->getLocation());
423 }
424 };
425}
426
John McCalle29ba202009-08-20 01:44:21 +0000427Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000428 // Create a local instantiation scope for this class template, which
429 // will contain the instantiations of the template parameters.
430 Sema::LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000431 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000432 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000433 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000434 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000435
436 CXXRecordDecl *Pattern = D->getTemplatedDecl();
437 CXXRecordDecl *RecordInst
438 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner,
439 Pattern->getLocation(), Pattern->getIdentifier(),
Douglas Gregorf0510d42009-10-12 23:11:44 +0000440 Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL,
441 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000442
443 ClassTemplateDecl *Inst
444 = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
445 D->getIdentifier(), InstParams, RecordInst, 0);
446 RecordInst->setDescribedClassTemplate(Inst);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000447 if (D->getFriendObjectKind())
448 Inst->setObjectOfFriendDecl(true);
449 else
450 Inst->setAccess(D->getAccess());
John McCalle29ba202009-08-20 01:44:21 +0000451 Inst->setInstantiatedFromMemberTemplate(D);
Douglas Gregorf0510d42009-10-12 23:11:44 +0000452
453 // Trigger creation of the type for the instantiation.
454 SemaRef.Context.getTypeDeclType(RecordInst);
455
Douglas Gregor259571e2009-10-30 22:42:42 +0000456 // Finish handling of friends.
457 if (Inst->getFriendObjectKind()) {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000458 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000459 }
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000460
John McCalle29ba202009-08-20 01:44:21 +0000461 Owner->addDecl(Inst);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000462
463 // First, we sort the partial specializations by location, so
464 // that we instantiate them in the order they were declared.
465 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
466 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
467 P = D->getPartialSpecializations().begin(),
468 PEnd = D->getPartialSpecializations().end();
469 P != PEnd; ++P)
470 PartialSpecs.push_back(&*P);
471 std::sort(PartialSpecs.begin(), PartialSpecs.end(),
472 SortDeclByLocation(SemaRef.SourceMgr));
473
474 // Instantiate all of the partial specializations of this member class
475 // template.
476 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
477 InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
478
John McCalle29ba202009-08-20 01:44:21 +0000479 return Inst;
480}
481
Douglas Gregord60e1052009-08-27 16:57:43 +0000482Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000483TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
484 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000485 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
486
487 // Lookup the already-instantiated declaration in the instantiation
488 // of the class template and return that.
489 DeclContext::lookup_result Found
490 = Owner->lookup(ClassTemplate->getDeclName());
491 if (Found.first == Found.second)
492 return 0;
493
494 ClassTemplateDecl *InstClassTemplate
495 = dyn_cast<ClassTemplateDecl>(*Found.first);
496 if (!InstClassTemplate)
497 return 0;
498
499 Decl *DCanon = D->getCanonicalDecl();
500 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
501 P = InstClassTemplate->getPartialSpecializations().begin(),
502 PEnd = InstClassTemplate->getPartialSpecializations().end();
503 P != PEnd; ++P) {
504 if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
505 return &*P;
506 }
507
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000508 return 0;
509}
510
511Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000512TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000513 // Create a local instantiation scope for this function template, which
514 // will contain the instantiations of the template parameters and then get
515 // merged with the local instantiation scope for the function template
516 // itself.
517 Sema::LocalInstantiationScope Scope(SemaRef);
518
Douglas Gregord60e1052009-08-27 16:57:43 +0000519 TemplateParameterList *TempParams = D->getTemplateParameters();
520 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000521 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000522 return NULL;
Douglas Gregored9c0f92009-10-29 00:04:11 +0000523
Douglas Gregora735b202009-10-13 14:39:41 +0000524 FunctionDecl *Instantiated = 0;
525 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
526 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
527 InstParams));
528 else
529 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
530 D->getTemplatedDecl(),
531 InstParams));
532
533 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000534 return 0;
535
Mike Stump1eb44332009-09-09 15:08:12 +0000536 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000537 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000538 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000539 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000540 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000541 assert(InstTemplate &&
542 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
543 if (!InstTemplate->getInstantiatedFromMemberTemplate())
544 InstTemplate->setInstantiatedFromMemberTemplate(D);
545
546 // Add non-friends into the owner.
547 if (!InstTemplate->getFriendObjectKind())
548 Owner->addDecl(InstTemplate);
Douglas Gregord60e1052009-08-27 16:57:43 +0000549 return InstTemplate;
550}
551
Douglas Gregord475b8d2009-03-25 21:17:03 +0000552Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
553 CXXRecordDecl *PrevDecl = 0;
554 if (D->isInjectedClassName())
555 PrevDecl = cast<CXXRecordDecl>(Owner);
556
557 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000558 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000559 D->getLocation(), D->getIdentifier(),
560 D->getTagKeywordLoc(), PrevDecl);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000561 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000562 // FIXME: Check against AS_none is an ugly hack to work around the issue that
563 // the tag decls introduced by friend class declarations don't have an access
564 // specifier. Remove once this area of the code gets sorted out.
565 if (D->getAccess() != AS_none)
566 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000567 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000568 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000569
John McCall02cace72009-08-28 07:59:38 +0000570 // If the original function was part of a friend declaration,
571 // inherit its namespace state.
572 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
573 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
574
Anders Carlssond8b285f2009-09-01 04:26:58 +0000575 Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
576
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000577 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000578 return Record;
579}
580
John McCall02cace72009-08-28 07:59:38 +0000581/// Normal class members are of more specific types and therefore
582/// don't make it here. This function serves two purposes:
583/// 1) instantiating function templates
584/// 2) substituting friend declarations
585/// FIXME: preserve function definitions in case #2
Douglas Gregora735b202009-10-13 14:39:41 +0000586 Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
587 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000588 // Check whether there is already a function template specialization for
589 // this declaration.
590 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
591 void *InsertPos = 0;
Douglas Gregora735b202009-10-13 14:39:41 +0000592 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000593 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000594 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000595 TemplateArgs.getInnermost().getFlatArgumentList(),
596 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor828e2262009-07-29 16:09:57 +0000597 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000598
599 FunctionTemplateSpecializationInfo *Info
600 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor127102b2009-06-29 20:59:39 +0000601 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000602
Douglas Gregor127102b2009-06-29 20:59:39 +0000603 // If we already have a function template specialization, return it.
604 if (Info)
605 return Info->Function;
606 }
Mike Stump1eb44332009-09-09 15:08:12 +0000607
Douglas Gregor550d9b22009-10-31 17:21:17 +0000608 Sema::LocalInstantiationScope Scope(SemaRef, TemplateParams != 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000609
Douglas Gregore53060f2009-06-25 22:08:12 +0000610 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000611 QualType T = SubstFunctionType(D, Params);
Douglas Gregore53060f2009-06-25 22:08:12 +0000612 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000613 return 0;
John McCallfd810b12009-08-14 02:03:10 +0000614
Douglas Gregore53060f2009-06-25 22:08:12 +0000615 // Build the instantiated method declaration.
Douglas Gregore95b4092009-09-16 18:34:49 +0000616 DeclContext *DC = SemaRef.FindInstantiatedContext(D->getDeclContext(),
617 TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +0000618 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +0000619 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000620 D->getDeclName(), T, D->getDeclaratorInfo(),
621 D->getStorageClass(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000622 D->isInlineSpecified(), D->hasWrittenPrototype());
John McCall02cace72009-08-28 07:59:38 +0000623 Function->setLexicalDeclContext(Owner);
Mike Stump1eb44332009-09-09 15:08:12 +0000624
Douglas Gregore53060f2009-06-25 22:08:12 +0000625 // Attach the parameters
626 for (unsigned P = 0; P < Params.size(); ++P)
627 Params[P]->setOwningFunction(Function);
628 Function->setParams(SemaRef.Context, Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +0000629
Douglas Gregora735b202009-10-13 14:39:41 +0000630 if (TemplateParams) {
631 // Our resulting instantiation is actually a function template, since we
632 // are substituting only the outer template parameters. For example, given
633 //
634 // template<typename T>
635 // struct X {
636 // template<typename U> friend void f(T, U);
637 // };
638 //
639 // X<int> x;
640 //
641 // We are instantiating the friend function template "f" within X<int>,
642 // which means substituting int for T, but leaving "f" as a friend function
643 // template.
644 // Build the function template itself.
645 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Owner,
646 Function->getLocation(),
647 Function->getDeclName(),
648 TemplateParams, Function);
649 Function->setDescribedFunctionTemplate(FunctionTemplate);
650 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
John McCall02cace72009-08-28 07:59:38 +0000651 }
Douglas Gregora735b202009-10-13 14:39:41 +0000652
Douglas Gregore53060f2009-06-25 22:08:12 +0000653 if (InitFunctionInstantiation(Function, D))
654 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Douglas Gregore53060f2009-06-25 22:08:12 +0000656 bool Redeclaration = false;
657 bool OverloadableAttrRequired = false;
Douglas Gregora735b202009-10-13 14:39:41 +0000658
Douglas Gregore53060f2009-06-25 22:08:12 +0000659 NamedDecl *PrevDecl = 0;
Douglas Gregora735b202009-10-13 14:39:41 +0000660 if (TemplateParams || !FunctionTemplate) {
661 // Look only into the namespace where the friend would be declared to
662 // find a previous declaration. This is the innermost enclosing namespace,
663 // as described in ActOnFriendFunctionDecl.
664 Sema::LookupResult R;
665 SemaRef.LookupQualifiedName(R, DC, Function->getDeclName(),
666 Sema::LookupOrdinaryName, true);
667
668 PrevDecl = R.getAsSingleDecl(SemaRef.Context);
669
670 // In C++, the previous declaration we find might be a tag type
671 // (class or enum). In this case, the new declaration will hide the
672 // tag type. Note that this does does not apply if we're declaring a
673 // typedef (C++ [dcl.typedef]p4).
674 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
675 PrevDecl = 0;
676 }
677
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000678 SemaRef.CheckFunctionDeclaration(Function, PrevDecl, false, Redeclaration,
Douglas Gregore53060f2009-06-25 22:08:12 +0000679 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000680
Douglas Gregora735b202009-10-13 14:39:41 +0000681 // If the original function was part of a friend declaration,
682 // inherit its namespace state and add it to the owner.
683 NamedDecl *FromFriendD
684 = TemplateParams? cast<NamedDecl>(D->getDescribedFunctionTemplate()) : D;
685 if (FromFriendD->getFriendObjectKind()) {
686 NamedDecl *ToFriendD = 0;
687 if (TemplateParams) {
688 ToFriendD = cast<NamedDecl>(FunctionTemplate);
689 PrevDecl = FunctionTemplate->getPreviousDeclaration();
690 } else {
691 ToFriendD = Function;
692 PrevDecl = Function->getPreviousDeclaration();
693 }
694 ToFriendD->setObjectOfFriendDecl(PrevDecl != NULL);
695 if (!Owner->isDependentContext() && !PrevDecl)
696 DC->makeDeclVisibleInContext(ToFriendD, /* Recoverable = */ false);
697
698 if (!TemplateParams)
699 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
700 }
701
702 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000703 // Record this function template specialization.
704 Function->setFunctionTemplateSpecialization(SemaRef.Context,
705 FunctionTemplate,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000706 &TemplateArgs.getInnermost(),
Douglas Gregor127102b2009-06-29 20:59:39 +0000707 InsertPos);
John McCallfd810b12009-08-14 02:03:10 +0000708 }
709
Douglas Gregore53060f2009-06-25 22:08:12 +0000710 return Function;
711}
712
Douglas Gregord60e1052009-08-27 16:57:43 +0000713Decl *
714TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
715 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +0000716 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
717 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +0000718 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +0000719 // We are creating a function template specialization from a function
720 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +0000721 // specialization for this particular set of template arguments.
Douglas Gregor6b906862009-08-21 00:16:32 +0000722 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000723 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000724 TemplateArgs.getInnermost().getFlatArgumentList(),
725 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000726 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000727
728 FunctionTemplateSpecializationInfo *Info
729 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor6b906862009-08-21 00:16:32 +0000730 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000731
Douglas Gregor6b906862009-08-21 00:16:32 +0000732 // If we already have a function template specialization, return it.
733 if (Info)
734 return Info->Function;
735 }
736
Douglas Gregor550d9b22009-10-31 17:21:17 +0000737 Sema::LocalInstantiationScope Scope(SemaRef, TemplateParams != 0);
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000738
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000739 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000740 QualType T = SubstFunctionType(D, Params);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000741 if (T.isNull())
742 return 0;
743
744 // Build the instantiated method declaration.
745 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
Douglas Gregordec06662009-08-21 18:42:58 +0000746 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000747
Douglas Gregordec06662009-08-21 18:42:58 +0000748 DeclarationName Name = D->getDeclName();
Douglas Gregor17e32f32009-08-21 22:43:28 +0000749 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregordec06662009-08-21 18:42:58 +0000750 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
751 Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
752 SemaRef.Context.getCanonicalType(ClassTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000753 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
754 Constructor->getLocation(),
755 Name, T,
Douglas Gregor17e32f32009-08-21 22:43:28 +0000756 Constructor->getDeclaratorInfo(),
Mike Stump1eb44332009-09-09 15:08:12 +0000757 Constructor->isExplicit(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000758 Constructor->isInlineSpecified(), false);
Douglas Gregor17e32f32009-08-21 22:43:28 +0000759 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
760 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
761 Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
762 SemaRef.Context.getCanonicalType(ClassTy));
763 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
764 Destructor->getLocation(), Name,
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000765 T, Destructor->isInlineSpecified(), false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000766 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000767 CanQualType ConvTy
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000768 = SemaRef.Context.getCanonicalType(
John McCall183700f2009-09-21 23:43:11 +0000769 T->getAs<FunctionType>()->getResultType());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000770 Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
771 ConvTy);
772 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
773 Conversion->getLocation(), Name,
774 T, Conversion->getDeclaratorInfo(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000775 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000776 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +0000777 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000778 Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
Douglas Gregordec06662009-08-21 18:42:58 +0000779 D->getDeclName(), T, D->getDeclaratorInfo(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000780 D->isStatic(), D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +0000781 }
Douglas Gregor6b906862009-08-21 00:16:32 +0000782
Douglas Gregord60e1052009-08-27 16:57:43 +0000783 if (TemplateParams) {
784 // Our resulting instantiation is actually a function template, since we
785 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +0000786 //
Douglas Gregord60e1052009-08-27 16:57:43 +0000787 // template<typename T>
788 // struct X {
789 // template<typename U> void f(T, U);
790 // };
791 //
792 // X<int> x;
793 //
794 // We are instantiating the member template "f" within X<int>, which means
795 // substituting int for T, but leaving "f" as a member function template.
796 // Build the function template itself.
797 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
798 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +0000799 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +0000800 TemplateParams, Method);
801 if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +0000802 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +0000803 Method->setDescribedFunctionTemplate(FunctionTemplate);
804 } else if (!FunctionTemplate)
Douglas Gregor2db32322009-10-07 23:56:10 +0000805 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000806
Mike Stump1eb44332009-09-09 15:08:12 +0000807 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000808 // out-of-line, the instantiation will have the same lexical
809 // context (which will be a namespace scope) as the template.
810 if (D->isOutOfLine())
811 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000812
Douglas Gregor5545e162009-03-24 00:38:23 +0000813 // Attach the parameters
814 for (unsigned P = 0; P < Params.size(); ++P)
815 Params[P]->setOwningFunction(Method);
Jay Foadbeaaccd2009-05-21 09:52:38 +0000816 Method->setParams(SemaRef.Context, Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +0000817
818 if (InitMethodInstantiation(Method, D))
819 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000820
Douglas Gregordec06662009-08-21 18:42:58 +0000821 NamedDecl *PrevDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000822
Douglas Gregord60e1052009-08-27 16:57:43 +0000823 if (!FunctionTemplate || TemplateParams) {
John McCallf36e02d2009-10-09 21:13:30 +0000824 Sema::LookupResult R;
825 SemaRef.LookupQualifiedName(R, Owner, Name, Sema::LookupOrdinaryName, true);
826 PrevDecl = R.getAsSingleDecl(SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000827
Douglas Gregordec06662009-08-21 18:42:58 +0000828 // In C++, the previous declaration we find might be a tag type
829 // (class or enum). In this case, the new declaration will hide the
830 // tag type. Note that this does does not apply if we're declaring a
831 // typedef (C++ [dcl.typedef]p4).
832 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
833 PrevDecl = 0;
834 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000835
Douglas Gregord60e1052009-08-27 16:57:43 +0000836 if (FunctionTemplate && !TemplateParams)
Douglas Gregor6b906862009-08-21 00:16:32 +0000837 // Record this function template specialization.
838 Method->setFunctionTemplateSpecialization(SemaRef.Context,
839 FunctionTemplate,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000840 &TemplateArgs.getInnermost(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000841 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000842
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000843 bool Redeclaration = false;
844 bool OverloadableAttrRequired = false;
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000845 SemaRef.CheckFunctionDeclaration(Method, PrevDecl, false, Redeclaration,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000846 /*FIXME:*/OverloadableAttrRequired);
847
Douglas Gregora735b202009-10-13 14:39:41 +0000848 if (!FunctionTemplate && (!Method->isInvalidDecl() || !PrevDecl) &&
849 !Method->getFriendObjectKind())
Douglas Gregordec06662009-08-21 18:42:58 +0000850 Owner->addDecl(Method);
Mike Stump1eb44332009-09-09 15:08:12 +0000851
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000852 return Method;
853}
854
Douglas Gregor615c5d42009-03-24 16:43:20 +0000855Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +0000856 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000857}
858
Douglas Gregor03b2b072009-03-24 00:15:49 +0000859Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +0000860 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +0000861}
862
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000863Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000864 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000865}
866
Douglas Gregor6477b692009-03-25 15:04:13 +0000867ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
John McCall58e46772009-10-23 21:48:59 +0000868 QualType T;
869 DeclaratorInfo *DI = D->getDeclaratorInfo();
870 if (DI) {
871 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
872 D->getDeclName());
873 if (DI) T = DI->getType();
874 } else {
875 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
876 D->getDeclName());
877 DI = 0;
878 }
879
880 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000881 return 0;
882
John McCall58e46772009-10-23 21:48:59 +0000883 T = SemaRef.adjustParameterType(T);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000884
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000885 // Allocate the parameter
John McCall58e46772009-10-23 21:48:59 +0000886 ParmVarDecl *Param
887 = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
888 D->getIdentifier(), T, DI, D->getStorageClass(), 0);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000889
Anders Carlsson9351c172009-08-25 03:18:48 +0000890 // Mark the default argument as being uninstantiated.
Douglas Gregorf43d0b32009-09-25 06:56:31 +0000891 if (D->hasUninstantiatedDefaultArg())
892 Param->setUninstantiatedDefaultArg(D->getUninstantiatedDefaultArg());
Douglas Gregor0ed09302009-09-25 07:03:22 +0000893 else if (Expr *Arg = D->getDefaultArg())
894 Param->setUninstantiatedDefaultArg(Arg);
895
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000896 // Note: we don't try to instantiate function parameters until after
897 // we've instantiated the function's type. Therefore, we don't have
898 // to check for 'void' parameter types here.
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000899 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000900 return Param;
901}
902
John McCalle29ba202009-08-20 01:44:21 +0000903Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
904 TemplateTypeParmDecl *D) {
905 // TODO: don't always clone when decls are refcounted.
906 const Type* T = D->getTypeForDecl();
907 assert(T->isTemplateTypeParmType());
908 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000909
John McCalle29ba202009-08-20 01:44:21 +0000910 TemplateTypeParmDecl *Inst =
911 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor550d9b22009-10-31 17:21:17 +0000912 TTPT->getDepth() - 1, TTPT->getIndex(),
John McCalle29ba202009-08-20 01:44:21 +0000913 TTPT->getName(),
914 D->wasDeclaredWithTypename(),
915 D->isParameterPack());
916
Douglas Gregor33642df2009-10-23 23:25:44 +0000917 // FIXME: Do we actually want to perform substitution here? I don't think
918 // we do.
John McCalle29ba202009-08-20 01:44:21 +0000919 if (D->hasDefaultArgument()) {
John McCall833ca992009-10-29 08:12:44 +0000920 DeclaratorInfo *DefaultPattern = D->getDefaultArgumentInfo();
921 DeclaratorInfo *DefaultInst
John McCallce3ff2b2009-08-25 22:02:44 +0000922 = SemaRef.SubstType(DefaultPattern, TemplateArgs,
923 D->getDefaultArgumentLoc(),
924 D->getDeclName());
Mike Stump1eb44332009-09-09 15:08:12 +0000925
John McCalle29ba202009-08-20 01:44:21 +0000926 Inst->setDefaultArgument(DefaultInst,
John McCalle29ba202009-08-20 01:44:21 +0000927 D->defaultArgumentWasInherited() /* preserve? */);
928 }
929
Douglas Gregor550d9b22009-10-31 17:21:17 +0000930 // Introduce this template parameter's instantiation into the instantiation
931 // scope.
932 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
933
John McCalle29ba202009-08-20 01:44:21 +0000934 return Inst;
935}
936
Douglas Gregor33642df2009-10-23 23:25:44 +0000937Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
938 NonTypeTemplateParmDecl *D) {
939 // Substitute into the type of the non-type template parameter.
940 QualType T;
941 DeclaratorInfo *DI = D->getDeclaratorInfo();
942 if (DI) {
943 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
944 D->getDeclName());
945 if (DI) T = DI->getType();
946 } else {
947 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
948 D->getDeclName());
949 DI = 0;
950 }
951 if (T.isNull())
952 return 0;
953
954 // Check that this type is acceptable for a non-type template parameter.
955 bool Invalid = false;
956 T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
957 if (T.isNull()) {
958 T = SemaRef.Context.IntTy;
959 Invalid = true;
960 }
961
962 NonTypeTemplateParmDecl *Param
963 = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
964 D->getDepth() - 1, D->getPosition(),
965 D->getIdentifier(), T, DI);
966 if (Invalid)
967 Param->setInvalidDecl();
968
969 Param->setDefaultArgument(D->getDefaultArgument());
Douglas Gregor550d9b22009-10-31 17:21:17 +0000970
971 // Introduce this template parameter's instantiation into the instantiation
972 // scope.
973 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +0000974 return Param;
975}
976
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000977Decl *
978TemplateDeclInstantiator::VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000979 NestedNameSpecifier *NNS =
980 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
981 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000982 TemplateArgs);
983 if (!NNS)
984 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000985
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000986 CXXScopeSpec SS;
987 SS.setRange(D->getTargetNestedNameRange());
988 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +0000989
990 NamedDecl *UD =
991 SemaRef.BuildUsingDeclaration(D->getLocation(), SS,
992 D->getTargetNameLocation(),
Anders Carlsson0d8df782009-08-29 19:37:28 +0000993 D->getTargetName(), 0, D->isTypeName());
994 if (UD)
Mike Stump1eb44332009-09-09 15:08:12 +0000995 SemaRef.Context.setInstantiatedFromUnresolvedUsingDecl(cast<UsingDecl>(UD),
Anders Carlsson0d8df782009-08-29 19:37:28 +0000996 D);
997 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000998}
999
John McCallce3ff2b2009-08-25 22:02:44 +00001000Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001001 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001002 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001003 return Instantiator.Visit(D);
1004}
1005
John McCalle29ba202009-08-20 01:44:21 +00001006/// \brief Instantiates a nested template parameter list in the current
1007/// instantiation context.
1008///
1009/// \param L The parameter list to instantiate
1010///
1011/// \returns NULL if there was an error
1012TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001013TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001014 // Get errors for all the parameters before bailing out.
1015 bool Invalid = false;
1016
1017 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001018 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001019 ParamVector Params;
1020 Params.reserve(N);
1021 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1022 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001023 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001024 Params.push_back(D);
1025 Invalid = Invalid || !D;
1026 }
1027
1028 // Clean up if we had an error.
1029 if (Invalid) {
1030 for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
1031 PI != PE; ++PI)
1032 if (*PI)
1033 (*PI)->Destroy(SemaRef.Context);
1034 return NULL;
1035 }
1036
1037 TemplateParameterList *InstL
1038 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1039 L->getLAngleLoc(), &Params.front(), N,
1040 L->getRAngleLoc());
1041 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001042}
John McCalle29ba202009-08-20 01:44:21 +00001043
Douglas Gregored9c0f92009-10-29 00:04:11 +00001044/// \brief Instantiate the declaration of a class template partial
1045/// specialization.
1046///
1047/// \param ClassTemplate the (instantiated) class template that is partially
1048// specialized by the instantiation of \p PartialSpec.
1049///
1050/// \param PartialSpec the (uninstantiated) class template partial
1051/// specialization that we are instantiating.
1052///
1053/// \returns true if there was an error, false otherwise.
1054bool
1055TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1056 ClassTemplateDecl *ClassTemplate,
1057 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001058 // Create a local instantiation scope for this class template partial
1059 // specialization, which will contain the instantiations of the template
1060 // parameters.
1061 Sema::LocalInstantiationScope Scope(SemaRef);
1062
Douglas Gregored9c0f92009-10-29 00:04:11 +00001063 // Substitute into the template parameters of the class template partial
1064 // specialization.
1065 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1066 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1067 if (!InstParams)
1068 return true;
1069
1070 // Substitute into the template arguments of the class template partial
1071 // specialization.
John McCall833ca992009-10-29 08:12:44 +00001072 const TemplateArgumentLoc *PartialSpecTemplateArgs
1073 = PartialSpec->getTemplateArgsAsWritten();
1074 unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1075
1076 llvm::SmallVector<TemplateArgumentLoc, 4> InstTemplateArgs(N);
1077 for (unsigned I = 0; I != N; ++I) {
1078 if (SemaRef.Subst(PartialSpecTemplateArgs[I], InstTemplateArgs[I],
1079 TemplateArgs))
Douglas Gregored9c0f92009-10-29 00:04:11 +00001080 return true;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001081 }
1082
1083
1084 // Check that the template argument list is well-formed for this
1085 // class template.
1086 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1087 InstTemplateArgs.size());
1088 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1089 PartialSpec->getLocation(),
1090 /*FIXME:*/PartialSpec->getLocation(),
1091 InstTemplateArgs.data(),
1092 InstTemplateArgs.size(),
1093 /*FIXME:*/PartialSpec->getLocation(),
1094 false,
1095 Converted))
1096 return true;
1097
1098 // Figure out where to insert this class template partial specialization
1099 // in the member template's set of class template partial specializations.
1100 llvm::FoldingSetNodeID ID;
1101 ClassTemplatePartialSpecializationDecl::Profile(ID,
1102 Converted.getFlatArguments(),
1103 Converted.flatSize(),
1104 SemaRef.Context);
1105 void *InsertPos = 0;
1106 ClassTemplateSpecializationDecl *PrevDecl
1107 = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
1108 InsertPos);
1109
1110 // Build the canonical type that describes the converted template
1111 // arguments of the class template partial specialization.
1112 QualType CanonType
1113 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1114 Converted.getFlatArguments(),
1115 Converted.flatSize());
1116
1117 // Build the fully-sugared type for this class template
1118 // specialization as the user wrote in the specialization
1119 // itself. This means that we'll pretty-print the type retrieved
1120 // from the specialization's declaration the way that the user
1121 // actually wrote the specialization, rather than formatting the
1122 // name based on the "canonical" representation used to store the
1123 // template arguments in the specialization.
1124 QualType WrittenTy
1125 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1126 InstTemplateArgs.data(),
1127 InstTemplateArgs.size(),
1128 CanonType);
1129
1130 if (PrevDecl) {
1131 // We've already seen a partial specialization with the same template
1132 // parameters and template arguments. This can happen, for example, when
1133 // substituting the outer template arguments ends up causing two
1134 // class template partial specializations of a member class template
1135 // to have identical forms, e.g.,
1136 //
1137 // template<typename T, typename U>
1138 // struct Outer {
1139 // template<typename X, typename Y> struct Inner;
1140 // template<typename Y> struct Inner<T, Y>;
1141 // template<typename Y> struct Inner<U, Y>;
1142 // };
1143 //
1144 // Outer<int, int> outer; // error: the partial specializations of Inner
1145 // // have the same signature.
1146 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1147 << WrittenTy;
1148 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1149 << SemaRef.Context.getTypeDeclType(PrevDecl);
1150 return true;
1151 }
1152
1153
1154 // Create the class template partial specialization declaration.
1155 ClassTemplatePartialSpecializationDecl *InstPartialSpec
1156 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context, Owner,
1157 PartialSpec->getLocation(),
1158 InstParams,
1159 ClassTemplate,
1160 Converted,
John McCall833ca992009-10-29 08:12:44 +00001161 InstTemplateArgs.data(),
1162 InstTemplateArgs.size(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00001163 0);
1164 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
1165 InstPartialSpec->setTypeAsWritten(WrittenTy);
1166
1167 // Add this partial specialization to the set of class template partial
1168 // specializations.
1169 ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec,
1170 InsertPos);
1171 return false;
1172}
1173
John McCallce3ff2b2009-08-25 22:02:44 +00001174/// \brief Does substitution on the type of the given function, including
1175/// all of the function parameters.
Douglas Gregor5545e162009-03-24 00:38:23 +00001176///
John McCallce3ff2b2009-08-25 22:02:44 +00001177/// \param D The function whose type will be the basis of the substitution
Douglas Gregor5545e162009-03-24 00:38:23 +00001178///
1179/// \param Params the instantiated parameter declarations
1180
John McCallce3ff2b2009-08-25 22:02:44 +00001181/// \returns the instantiated function's type if successful, a NULL
Douglas Gregor5545e162009-03-24 00:38:23 +00001182/// type if there was an error.
Mike Stump1eb44332009-09-09 15:08:12 +00001183QualType
John McCallce3ff2b2009-08-25 22:02:44 +00001184TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001185 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
1186 bool InvalidDecl = false;
1187
John McCallce3ff2b2009-08-25 22:02:44 +00001188 // Substitute all of the function's formal parameter types.
Douglas Gregor7e063902009-05-11 23:53:27 +00001189 TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001190 llvm::SmallVector<QualType, 4> ParamTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001191 for (FunctionDecl::param_iterator P = D->param_begin(),
Douglas Gregor5545e162009-03-24 00:38:23 +00001192 PEnd = D->param_end();
1193 P != PEnd; ++P) {
Douglas Gregor6477b692009-03-25 15:04:13 +00001194 if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
Douglas Gregor5545e162009-03-24 00:38:23 +00001195 if (PInst->getType()->isVoidType()) {
1196 SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
1197 PInst->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001198 } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001199 PInst->getType(),
1200 diag::err_abstract_type_in_decl,
1201 Sema::AbstractParamType))
Douglas Gregor5545e162009-03-24 00:38:23 +00001202 PInst->setInvalidDecl();
1203
1204 Params.push_back(PInst);
1205 ParamTys.push_back(PInst->getType());
1206
1207 if (PInst->isInvalidDecl())
1208 InvalidDecl = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001209 } else
Douglas Gregor5545e162009-03-24 00:38:23 +00001210 InvalidDecl = true;
1211 }
1212
1213 // FIXME: Deallocate dead declarations.
1214 if (InvalidDecl)
1215 return QualType();
1216
John McCall183700f2009-09-21 23:43:11 +00001217 const FunctionProtoType *Proto = D->getType()->getAs<FunctionProtoType>();
Douglas Gregor5545e162009-03-24 00:38:23 +00001218 assert(Proto && "Missing prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +00001219 QualType ResultType
John McCallce3ff2b2009-08-25 22:02:44 +00001220 = SemaRef.SubstType(Proto->getResultType(), TemplateArgs,
1221 D->getLocation(), D->getDeclName());
Douglas Gregor5545e162009-03-24 00:38:23 +00001222 if (ResultType.isNull())
1223 return QualType();
1224
Jay Foadbeaaccd2009-05-21 09:52:38 +00001225 return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
Douglas Gregor5545e162009-03-24 00:38:23 +00001226 Proto->isVariadic(), Proto->getTypeQuals(),
1227 D->getLocation(), D->getDeclName());
1228}
1229
Mike Stump1eb44332009-09-09 15:08:12 +00001230/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00001231/// declaration (New) from the corresponding fields of its template (Tmpl).
1232///
1233/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001234bool
1235TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00001236 FunctionDecl *Tmpl) {
1237 if (Tmpl->isDeleted())
1238 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00001239
Douglas Gregorcca9e962009-07-01 22:01:06 +00001240 // If we are performing substituting explicitly-specified template arguments
1241 // or deduced template arguments into a function template and we reach this
1242 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00001243 // to keeping the new function template specialization. We therefore
1244 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00001245 // into a template instantiation for this specific function template
1246 // specialization, which is not a SFINAE context, so that we diagnose any
1247 // further errors in the declaration itself.
1248 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1249 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1250 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1251 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00001252 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00001253 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001254 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00001255 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00001256 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001257 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1258 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
1259 }
1260 }
Mike Stump1eb44332009-09-09 15:08:12 +00001261
Douglas Gregore53060f2009-06-25 22:08:12 +00001262 return false;
1263}
1264
Douglas Gregor5545e162009-03-24 00:38:23 +00001265/// \brief Initializes common fields of an instantiated method
1266/// declaration (New) from the corresponding fields of its template
1267/// (Tmpl).
1268///
1269/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001270bool
1271TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00001272 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001273 if (InitFunctionInstantiation(New, Tmpl))
1274 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001275
Douglas Gregor5545e162009-03-24 00:38:23 +00001276 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
1277 New->setAccess(Tmpl->getAccess());
Anders Carlsson77b7f1d2009-05-14 22:15:41 +00001278 if (Tmpl->isVirtualAsWritten()) {
1279 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +00001280 Record->setAggregate(false);
1281 Record->setPOD(false);
Eli Friedman1d954f62009-08-15 21:55:26 +00001282 Record->setEmpty(false);
Douglas Gregor5545e162009-03-24 00:38:23 +00001283 Record->setPolymorphic(true);
1284 }
Douglas Gregor5545e162009-03-24 00:38:23 +00001285 if (Tmpl->isPure()) {
1286 New->setPure();
1287 Record->setAbstract(true);
1288 }
1289
1290 // FIXME: attributes
1291 // FIXME: New needs a pointer to Tmpl
1292 return false;
1293}
Douglas Gregora58861f2009-05-13 20:28:22 +00001294
1295/// \brief Instantiate the definition of the given function from its
1296/// template.
1297///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001298/// \param PointOfInstantiation the point at which the instantiation was
1299/// required. Note that this is not precisely a "point of instantiation"
1300/// for the function, but it's close.
1301///
Douglas Gregora58861f2009-05-13 20:28:22 +00001302/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001303/// function template specialization or member function of a class template
1304/// specialization.
1305///
1306/// \param Recursive if true, recursively instantiates any functions that
1307/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001308///
1309/// \param DefinitionRequired if true, then we are performing an explicit
1310/// instantiation where the body of the function is required. Complain if
1311/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001312void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001313 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001314 bool Recursive,
1315 bool DefinitionRequired) {
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001316 if (Function->isInvalidDecl())
1317 return;
1318
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001319 assert(!Function->getBody() && "Already instantiated!");
Mike Stump1eb44332009-09-09 15:08:12 +00001320
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001321 // Never instantiate an explicit specialization.
1322 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1323 return;
1324
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001325 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00001326 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001327 Stmt *Pattern = 0;
1328 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001329 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001330
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001331 if (!Pattern) {
1332 if (DefinitionRequired) {
1333 if (Function->getPrimaryTemplate())
1334 Diag(PointOfInstantiation,
1335 diag::err_explicit_instantiation_undefined_func_template)
1336 << Function->getPrimaryTemplate();
1337 else
1338 Diag(PointOfInstantiation,
1339 diag::err_explicit_instantiation_undefined_member)
1340 << 1 << Function->getDeclName() << Function->getDeclContext();
1341
1342 if (PatternDecl)
1343 Diag(PatternDecl->getLocation(),
1344 diag::note_explicit_instantiation_here);
1345 }
1346
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001347 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001348 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001349
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001350 // C++0x [temp.explicit]p9:
1351 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00001352 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001353 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00001354 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001355 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001356 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001357 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001358
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001359 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
1360 if (Inst)
1361 return;
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001362
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001363 // If we're performing recursive template instantiation, create our own
1364 // queue of pending implicit instantiations that we will instantiate later,
1365 // while we're still within our own instantiation context.
1366 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1367 if (Recursive)
1368 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001369
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001370 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
1371
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001372 // Introduce a new scope where local variable instantiations will be
1373 // recorded.
1374 LocalInstantiationScope Scope(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001375
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001376 // Introduce the instantiated function parameters into the local
1377 // instantiation scope.
1378 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
1379 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
1380 Function->getParamDecl(I));
1381
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001382 // Enter the scope of this instantiation. We don't use
1383 // PushDeclContext because we don't have a scope.
1384 DeclContext *PreviousContext = CurContext;
1385 CurContext = Function;
1386
Mike Stump1eb44332009-09-09 15:08:12 +00001387 MultiLevelTemplateArgumentList TemplateArgs =
Anders Carlsson09025312009-08-29 05:16:22 +00001388 getTemplateInstantiationArgs(Function);
1389
1390 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001391 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00001392 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
1393 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
1394 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001395 }
1396
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001397 // Instantiate the function body.
Anders Carlsson09025312009-08-29 05:16:22 +00001398 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001399
Douglas Gregor52604ab2009-09-11 21:19:12 +00001400 if (Body.isInvalid())
1401 Function->setInvalidDecl();
1402
Mike Stump1eb44332009-09-09 15:08:12 +00001403 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001404 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001405
1406 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00001407
1408 DeclGroupRef DG(Function);
1409 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001410
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001411 if (Recursive) {
1412 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001413 // instantiation of this template.
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001414 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001415
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001416 // Restore the set of pending implicit instantiations.
1417 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1418 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001419}
1420
1421/// \brief Instantiate the definition of the given variable from its
1422/// template.
1423///
Douglas Gregor7caa6822009-07-24 20:34:43 +00001424/// \param PointOfInstantiation the point at which the instantiation was
1425/// required. Note that this is not precisely a "point of instantiation"
1426/// for the function, but it's close.
1427///
1428/// \param Var the already-instantiated declaration of a static member
1429/// variable of a class template specialization.
1430///
1431/// \param Recursive if true, recursively instantiates any functions that
1432/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001433///
1434/// \param DefinitionRequired if true, then we are performing an explicit
1435/// instantiation where an out-of-line definition of the member variable
1436/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001437void Sema::InstantiateStaticDataMemberDefinition(
1438 SourceLocation PointOfInstantiation,
1439 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001440 bool Recursive,
1441 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001442 if (Var->isInvalidDecl())
1443 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001444
Douglas Gregor7caa6822009-07-24 20:34:43 +00001445 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001446 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00001447 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00001448 assert(Def->isStaticDataMember() && "Not a static data member?");
1449 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00001450
Douglas Gregor0d035142009-10-27 18:42:08 +00001451 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001452 // We did not find an out-of-line definition of this static data member,
1453 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00001454 // instantiate this definition (or provide a specialization for it) in
1455 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001456 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001457 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001458 Diag(PointOfInstantiation,
1459 diag::err_explicit_instantiation_undefined_member)
1460 << 2 << Var->getDeclName() << Var->getDeclContext();
1461 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
1462 }
1463
Douglas Gregor7caa6822009-07-24 20:34:43 +00001464 return;
1465 }
1466
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001467 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001468 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001469 return;
1470
1471 // C++0x [temp.explicit]p9:
1472 // Except for inline functions, other explicit instantiation declarations
1473 // have the effect of suppressing the implicit instantiation of the entity
1474 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001475 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001476 == TSK_ExplicitInstantiationDeclaration)
1477 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001478
Douglas Gregor7caa6822009-07-24 20:34:43 +00001479 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
1480 if (Inst)
1481 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001482
Douglas Gregor7caa6822009-07-24 20:34:43 +00001483 // If we're performing recursive template instantiation, create our own
1484 // queue of pending implicit instantiations that we will instantiate later,
1485 // while we're still within our own instantiation context.
1486 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1487 if (Recursive)
1488 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001489
Douglas Gregor7caa6822009-07-24 20:34:43 +00001490 // Enter the scope of this instantiation. We don't use
1491 // PushDeclContext because we don't have a scope.
1492 DeclContext *PreviousContext = CurContext;
1493 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001494
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001495 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00001496 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00001497 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00001498 CurContext = PreviousContext;
1499
1500 if (Var) {
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001501 Var->setPreviousDeclaration(OldVar);
Douglas Gregor583f33b2009-10-15 18:07:02 +00001502 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
1503 assert(MSInfo && "Missing member specialization information?");
1504 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
1505 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00001506 DeclGroupRef DG(Var);
1507 Consumer.HandleTopLevelDecl(DG);
1508 }
Mike Stump1eb44332009-09-09 15:08:12 +00001509
Douglas Gregor7caa6822009-07-24 20:34:43 +00001510 if (Recursive) {
1511 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001512 // instantiation of this template.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001513 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001514
Douglas Gregor7caa6822009-07-24 20:34:43 +00001515 // Restore the set of pending implicit instantiations.
1516 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001517 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001518}
Douglas Gregor815215d2009-05-27 05:35:12 +00001519
Anders Carlsson09025312009-08-29 05:16:22 +00001520void
1521Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
1522 const CXXConstructorDecl *Tmpl,
1523 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001524
Anders Carlsson09025312009-08-29 05:16:22 +00001525 llvm::SmallVector<MemInitTy*, 4> NewInits;
1526
1527 // Instantiate all the initializers.
1528 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00001529 InitsEnd = Tmpl->init_end();
1530 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00001531 CXXBaseOrMemberInitializer *Init = *Inits;
1532
1533 ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001534
Anders Carlsson09025312009-08-29 05:16:22 +00001535 // Instantiate all the arguments.
1536 for (ExprIterator Args = Init->arg_begin(), ArgsEnd = Init->arg_end();
1537 Args != ArgsEnd; ++Args) {
1538 OwningExprResult NewArg = SubstExpr(*Args, TemplateArgs);
1539
1540 if (NewArg.isInvalid())
1541 New->setInvalidDecl();
1542 else
1543 NewArgs.push_back(NewArg.takeAs<Expr>());
1544 }
1545
1546 MemInitResult NewInit;
1547
1548 if (Init->isBaseInitializer()) {
Eli Friedmanc5573a82009-08-29 22:22:07 +00001549 QualType BaseType(Init->getBaseClass(), 0);
1550 BaseType = SubstType(BaseType, TemplateArgs, Init->getSourceLocation(),
1551 New->getDeclName());
Anders Carlsson09025312009-08-29 05:16:22 +00001552
1553 NewInit = BuildBaseInitializer(BaseType,
Mike Stump1eb44332009-09-09 15:08:12 +00001554 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001555 NewArgs.size(),
1556 Init->getSourceLocation(),
1557 Init->getRParenLoc(),
1558 New->getParent());
1559 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001560 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00001561
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001562 // Is this an anonymous union?
1563 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregore95b4092009-09-16 18:34:49 +00001564 Member = cast<FieldDecl>(FindInstantiatedDecl(UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001565 else
Douglas Gregore95b4092009-09-16 18:34:49 +00001566 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMember(),
1567 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00001568
1569 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001570 NewArgs.size(),
1571 Init->getSourceLocation(),
1572 Init->getRParenLoc());
1573 }
1574
1575 if (NewInit.isInvalid())
1576 New->setInvalidDecl();
1577 else {
1578 // FIXME: It would be nice if ASTOwningVector had a release function.
1579 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001580
Anders Carlsson09025312009-08-29 05:16:22 +00001581 NewInits.push_back((MemInitTy *)NewInit.get());
1582 }
1583 }
Mike Stump1eb44332009-09-09 15:08:12 +00001584
Anders Carlsson09025312009-08-29 05:16:22 +00001585 // Assign all the initializers to the new constructor.
Mike Stump1eb44332009-09-09 15:08:12 +00001586 ActOnMemInitializers(DeclPtrTy::make(New),
Anders Carlsson09025312009-08-29 05:16:22 +00001587 /*FIXME: ColonLoc */
1588 SourceLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001589 NewInits.data(), NewInits.size());
Anders Carlsson09025312009-08-29 05:16:22 +00001590}
1591
John McCall52a575a2009-08-29 08:11:13 +00001592// TODO: this could be templated if the various decl types used the
1593// same method name.
1594static bool isInstantiationOf(ClassTemplateDecl *Pattern,
1595 ClassTemplateDecl *Instance) {
1596 Pattern = Pattern->getCanonicalDecl();
1597
1598 do {
1599 Instance = Instance->getCanonicalDecl();
1600 if (Pattern == Instance) return true;
1601 Instance = Instance->getInstantiatedFromMemberTemplate();
1602 } while (Instance);
1603
1604 return false;
1605}
1606
Douglas Gregor0d696532009-09-28 06:34:35 +00001607static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
1608 FunctionTemplateDecl *Instance) {
1609 Pattern = Pattern->getCanonicalDecl();
1610
1611 do {
1612 Instance = Instance->getCanonicalDecl();
1613 if (Pattern == Instance) return true;
1614 Instance = Instance->getInstantiatedFromMemberTemplate();
1615 } while (Instance);
1616
1617 return false;
1618}
1619
Douglas Gregored9c0f92009-10-29 00:04:11 +00001620static bool
1621isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
1622 ClassTemplatePartialSpecializationDecl *Instance) {
1623 Pattern
1624 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
1625 do {
1626 Instance = cast<ClassTemplatePartialSpecializationDecl>(
1627 Instance->getCanonicalDecl());
1628 if (Pattern == Instance)
1629 return true;
1630 Instance = Instance->getInstantiatedFromMember();
1631 } while (Instance);
1632
1633 return false;
1634}
1635
John McCall52a575a2009-08-29 08:11:13 +00001636static bool isInstantiationOf(CXXRecordDecl *Pattern,
1637 CXXRecordDecl *Instance) {
1638 Pattern = Pattern->getCanonicalDecl();
1639
1640 do {
1641 Instance = Instance->getCanonicalDecl();
1642 if (Pattern == Instance) return true;
1643 Instance = Instance->getInstantiatedFromMemberClass();
1644 } while (Instance);
1645
1646 return false;
1647}
1648
1649static bool isInstantiationOf(FunctionDecl *Pattern,
1650 FunctionDecl *Instance) {
1651 Pattern = Pattern->getCanonicalDecl();
1652
1653 do {
1654 Instance = Instance->getCanonicalDecl();
1655 if (Pattern == Instance) return true;
1656 Instance = Instance->getInstantiatedFromMemberFunction();
1657 } while (Instance);
1658
1659 return false;
1660}
1661
1662static bool isInstantiationOf(EnumDecl *Pattern,
1663 EnumDecl *Instance) {
1664 Pattern = Pattern->getCanonicalDecl();
1665
1666 do {
1667 Instance = Instance->getCanonicalDecl();
1668 if (Pattern == Instance) return true;
1669 Instance = Instance->getInstantiatedFromMemberEnum();
1670 } while (Instance);
1671
1672 return false;
1673}
1674
Anders Carlsson0d8df782009-08-29 19:37:28 +00001675static bool isInstantiationOf(UnresolvedUsingDecl *Pattern,
1676 UsingDecl *Instance,
1677 ASTContext &C) {
1678 return C.getInstantiatedFromUnresolvedUsingDecl(Instance) == Pattern;
1679}
1680
John McCall52a575a2009-08-29 08:11:13 +00001681static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
1682 VarDecl *Instance) {
1683 assert(Instance->isStaticDataMember());
1684
1685 Pattern = Pattern->getCanonicalDecl();
1686
1687 do {
1688 Instance = Instance->getCanonicalDecl();
1689 if (Pattern == Instance) return true;
1690 Instance = Instance->getInstantiatedFromStaticDataMember();
1691 } while (Instance);
1692
1693 return false;
1694}
1695
Douglas Gregor815215d2009-05-27 05:35:12 +00001696static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00001697 if (D->getKind() != Other->getKind()) {
1698 if (UnresolvedUsingDecl *UUD = dyn_cast<UnresolvedUsingDecl>(D)) {
1699 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
1700 return isInstantiationOf(UUD, UD, Ctx);
1701 }
1702 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001703
Anders Carlsson0d8df782009-08-29 19:37:28 +00001704 return false;
1705 }
Mike Stump1eb44332009-09-09 15:08:12 +00001706
John McCall52a575a2009-08-29 08:11:13 +00001707 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
1708 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001709
John McCall52a575a2009-08-29 08:11:13 +00001710 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
1711 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00001712
John McCall52a575a2009-08-29 08:11:13 +00001713 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
1714 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00001715
Douglas Gregor7caa6822009-07-24 20:34:43 +00001716 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00001717 if (Var->isStaticDataMember())
1718 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
1719
1720 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
1721 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00001722
Douglas Gregor0d696532009-09-28 06:34:35 +00001723 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
1724 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
1725
Douglas Gregored9c0f92009-10-29 00:04:11 +00001726 if (ClassTemplatePartialSpecializationDecl *PartialSpec
1727 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
1728 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
1729 PartialSpec);
1730
Anders Carlssond8b285f2009-09-01 04:26:58 +00001731 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
1732 if (!Field->getDeclName()) {
1733 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00001734 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00001735 cast<FieldDecl>(D);
1736 }
1737 }
Mike Stump1eb44332009-09-09 15:08:12 +00001738
Douglas Gregor815215d2009-05-27 05:35:12 +00001739 return D->getDeclName() && isa<NamedDecl>(Other) &&
1740 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
1741}
1742
1743template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00001744static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00001745 NamedDecl *D,
1746 ForwardIterator first,
1747 ForwardIterator last) {
1748 for (; first != last; ++first)
1749 if (isInstantiationOf(Ctx, D, *first))
1750 return cast<NamedDecl>(*first);
1751
1752 return 0;
1753}
1754
John McCall02cace72009-08-28 07:59:38 +00001755/// \brief Finds the instantiation of the given declaration context
1756/// within the current instantiation.
1757///
1758/// \returns NULL if there was an error
Douglas Gregore95b4092009-09-16 18:34:49 +00001759DeclContext *Sema::FindInstantiatedContext(DeclContext* DC,
1760 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00001761 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00001762 Decl* ID = FindInstantiatedDecl(D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00001763 return cast_or_null<DeclContext>(ID);
1764 } else return DC;
1765}
1766
Douglas Gregored961e72009-05-27 17:54:46 +00001767/// \brief Find the instantiation of the given declaration within the
1768/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00001769///
1770/// This routine is intended to be used when \p D is a declaration
1771/// referenced from within a template, that needs to mapped into the
1772/// corresponding declaration within an instantiation. For example,
1773/// given:
1774///
1775/// \code
1776/// template<typename T>
1777/// struct X {
1778/// enum Kind {
1779/// KnownValue = sizeof(T)
1780/// };
1781///
1782/// bool getKind() const { return KnownValue; }
1783/// };
1784///
1785/// template struct X<int>;
1786/// \endcode
1787///
1788/// In the instantiation of X<int>::getKind(), we need to map the
1789/// EnumConstantDecl for KnownValue (which refers to
1790/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00001791/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
1792/// this mapping from within the instantiation of X<int>.
Douglas Gregore95b4092009-09-16 18:34:49 +00001793NamedDecl *Sema::FindInstantiatedDecl(NamedDecl *D,
1794 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor44c73842009-09-01 17:53:10 +00001795 if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D)) {
1796 // Transform all of the elements of the overloaded function set.
Mike Stump1eb44332009-09-09 15:08:12 +00001797 OverloadedFunctionDecl *Result
Douglas Gregor44c73842009-09-01 17:53:10 +00001798 = OverloadedFunctionDecl::Create(Context, CurContext, Ovl->getDeclName());
Mike Stump1eb44332009-09-09 15:08:12 +00001799
Douglas Gregor44c73842009-09-01 17:53:10 +00001800 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
1801 FEnd = Ovl->function_end();
1802 F != FEnd; ++F) {
1803 Result->addOverload(
Douglas Gregore95b4092009-09-16 18:34:49 +00001804 AnyFunctionDecl::getFromNamedDecl(FindInstantiatedDecl(*F,
1805 TemplateArgs)));
Douglas Gregor44c73842009-09-01 17:53:10 +00001806 }
Mike Stump1eb44332009-09-09 15:08:12 +00001807
Douglas Gregor44c73842009-09-01 17:53:10 +00001808 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001809 }
1810
Douglas Gregor815215d2009-05-27 05:35:12 +00001811 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00001812 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
1813 isa<TemplateTypeParmDecl>(D) || isa<TemplateTypeParmDecl>(D) ||
1814 ParentDC->isFunctionOrMethod()) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00001815 // D is a local of some kind. Look into the map of local
1816 // declarations to their instantiations.
1817 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
1818 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001819
Douglas Gregore95b4092009-09-16 18:34:49 +00001820 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
1821 if (!Record->isDependentContext())
1822 return D;
1823
1824 // If the RecordDecl is actually the injected-class-name or a "templated"
1825 // declaration for a class template or class template partial
1826 // specialization, substitute into the injected-class-name of the
1827 // class template or partial specialization to find the new DeclContext.
1828 QualType T;
1829 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
1830
1831 if (ClassTemplate) {
1832 T = ClassTemplate->getInjectedClassNameType(Context);
1833 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
1834 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1835 T = Context.getTypeDeclType(Record);
1836 ClassTemplate = PartialSpec->getSpecializedTemplate();
1837 }
1838
1839 if (!T.isNull()) {
1840 // Substitute into the injected-class-name to get the type corresponding
1841 // to the instantiation we want. This substitution should never fail,
1842 // since we know we can instantiate the injected-class-name or we wouldn't
1843 // have gotten to the injected-class-name!
1844 // FIXME: Can we use the CurrentInstantiationScope to avoid this extra
1845 // instantiation in the common case?
1846 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
1847 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
1848
1849 if (!T->isDependentType()) {
1850 assert(T->isRecordType() && "Instantiation must produce a record type");
1851 return T->getAs<RecordType>()->getDecl();
1852 }
1853
1854 // We are performing "partial" template instantiation to create the
1855 // member declarations for the members of a class template
1856 // specialization. Therefore, D is actually referring to something in
1857 // the current instantiation. Look through the current context,
1858 // which contains actual instantiations, to find the instantiation of
1859 // the "current instantiation" that D refers to.
Mike Stump1eb44332009-09-09 15:08:12 +00001860 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00001861 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001862 if (ClassTemplateSpecializationDecl *Spec
John McCall52a575a2009-08-29 08:11:13 +00001863 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00001864 if (isInstantiationOf(ClassTemplate,
1865 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00001866 return Spec;
1867 }
1868
Mike Stump1eb44332009-09-09 15:08:12 +00001869 assert(false &&
John McCall52a575a2009-08-29 08:11:13 +00001870 "Unable to find declaration for the current instantiation");
Douglas Gregore95b4092009-09-16 18:34:49 +00001871 return Record;
John McCall52a575a2009-08-29 08:11:13 +00001872 }
Douglas Gregore95b4092009-09-16 18:34:49 +00001873
1874 // Fall through to deal with other dependent record types (e.g.,
1875 // anonymous unions in class templates).
1876 }
John McCall52a575a2009-08-29 08:11:13 +00001877
Douglas Gregore95b4092009-09-16 18:34:49 +00001878 if (!ParentDC->isDependentContext())
1879 return D;
1880
1881 ParentDC = FindInstantiatedContext(ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001882 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00001883 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001884
Douglas Gregor815215d2009-05-27 05:35:12 +00001885 if (ParentDC != D->getDeclContext()) {
1886 // We performed some kind of instantiation in the parent context,
1887 // so now we need to look into the instantiated parent context to
1888 // find the instantiation of the declaration D.
1889 NamedDecl *Result = 0;
1890 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001891 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00001892 Result = findInstantiationOf(Context, D, Found.first, Found.second);
1893 } else {
1894 // Since we don't have a name for the entity we're looking for,
1895 // our only option is to walk through all of the declarations to
1896 // find that name. This will occur in a few cases:
1897 //
1898 // - anonymous struct/union within a template
1899 // - unnamed class/struct/union/enum within a template
1900 //
1901 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00001902 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001903 ParentDC->decls_begin(),
1904 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00001905 }
Mike Stump1eb44332009-09-09 15:08:12 +00001906
Douglas Gregor815215d2009-05-27 05:35:12 +00001907 assert(Result && "Unable to find instantiation of declaration!");
1908 D = Result;
1909 }
1910
Douglas Gregor815215d2009-05-27 05:35:12 +00001911 return D;
1912}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001913
Mike Stump1eb44332009-09-09 15:08:12 +00001914/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001915/// instantiations we have seen until this point.
1916void Sema::PerformPendingImplicitInstantiations() {
1917 while (!PendingImplicitInstantiations.empty()) {
1918 PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front();
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001919 PendingImplicitInstantiations.pop_front();
Mike Stump1eb44332009-09-09 15:08:12 +00001920
Douglas Gregor7caa6822009-07-24 20:34:43 +00001921 // Instantiate function definitions
1922 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001923 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001924 Function->getLocation(), *this,
1925 Context.getSourceManager(),
1926 "instantiating function definition");
Mike Stump1eb44332009-09-09 15:08:12 +00001927
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001928 if (!Function->getBody())
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001929 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001930 continue;
1931 }
Mike Stump1eb44332009-09-09 15:08:12 +00001932
Douglas Gregor7caa6822009-07-24 20:34:43 +00001933 // Instantiate static data member definitions.
1934 VarDecl *Var = cast<VarDecl>(Inst.first);
1935 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001936
Mike Stump1eb44332009-09-09 15:08:12 +00001937 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001938 Var->getLocation(), *this,
1939 Context.getSourceManager(),
1940 "instantiating static data member "
1941 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00001942
Douglas Gregor7caa6822009-07-24 20:34:43 +00001943 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001944 }
1945}