blob: d37476259e0298d26134a181cf408e2548bfe826 [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();
Sebastian Redl42dddbe2009-11-08 10:16:43 +0000189 else if (!D->getType()->isDependentType() &&
190 !D->getInit()->isTypeDependent() &&
Douglas Gregore48319a2009-11-09 17:16:50 +0000191 !D->getInit()->isValueDependent()) {
Sebastian Redl42dddbe2009-11-08 10:16:43 +0000192 // If neither the declaration's type nor its initializer are dependent,
193 // we don't want to redo all the checking, especially since the
194 // initializer might have been wrapped by a CXXConstructExpr since we did
195 // it the first time.
196 Var->setInit(SemaRef.Context, Init.takeAs<Expr>());
197 }
Douglas Gregor83ddad32009-08-26 21:14:46 +0000198 else if (ParenListExpr *PLE = dyn_cast<ParenListExpr>((Expr *)Init.get())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000199 // FIXME: We're faking all of the comma locations, which is suboptimal.
Douglas Gregor83ddad32009-08-26 21:14:46 +0000200 // Do we even need these comma locations?
201 llvm::SmallVector<SourceLocation, 4> FakeCommaLocs;
202 if (PLE->getNumExprs() > 0) {
203 FakeCommaLocs.reserve(PLE->getNumExprs() - 1);
204 for (unsigned I = 0, N = PLE->getNumExprs() - 1; I != N; ++I) {
205 Expr *E = PLE->getExpr(I)->Retain();
206 FakeCommaLocs.push_back(
207 SemaRef.PP.getLocForEndOfToken(E->getLocEnd()));
208 }
Douglas Gregore9f8eb62009-08-26 23:26:04 +0000209 PLE->getExpr(PLE->getNumExprs() - 1)->Retain();
Douglas Gregor83ddad32009-08-26 21:14:46 +0000210 }
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Douglas Gregor83ddad32009-08-26 21:14:46 +0000212 // Add the direct initializer to the declaration.
213 SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
Mike Stump1eb44332009-09-09 15:08:12 +0000214 PLE->getLParenLoc(),
Douglas Gregor83ddad32009-08-26 21:14:46 +0000215 Sema::MultiExprArg(SemaRef,
216 (void**)PLE->getExprs(),
217 PLE->getNumExprs()),
218 FakeCommaLocs.data(),
219 PLE->getRParenLoc());
Mike Stump1eb44332009-09-09 15:08:12 +0000220
Douglas Gregor83ddad32009-08-26 21:14:46 +0000221 // When Init is destroyed, it will destroy the instantiated ParenListExpr;
222 // we've explicitly retained all of its subexpressions already.
223 } else
Chris Lattnerb28317a2009-03-28 19:18:32 +0000224 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init),
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000225 D->hasCXXDirectInitializer());
Douglas Gregor65b90052009-07-27 17:43:39 +0000226 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
227 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000228
229 return Var;
230}
231
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000232Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
233 bool Invalid = false;
John McCall07fb6be2009-10-22 23:33:21 +0000234 DeclaratorInfo *DI = D->getDeclaratorInfo();
235 if (DI->getType()->isDependentType()) {
236 DI = SemaRef.SubstType(DI, TemplateArgs,
237 D->getLocation(), D->getDeclName());
238 if (!DI) {
239 DI = D->getDeclaratorInfo();
240 Invalid = true;
241 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000242 // C++ [temp.arg.type]p3:
243 // If a declaration acquires a function type through a type
244 // dependent on a template-parameter and this causes a
245 // declaration that does not use the syntactic form of a
246 // function declarator to have function type, the program is
247 // ill-formed.
248 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000249 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000250 Invalid = true;
251 }
252 }
253
254 Expr *BitWidth = D->getBitWidth();
255 if (Invalid)
256 BitWidth = 0;
257 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000258 // The bit-width expression is not potentially evaluated.
259 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000261 OwningExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000262 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000263 if (InstantiatedBitWidth.isInvalid()) {
264 Invalid = true;
265 BitWidth = 0;
266 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000267 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000268 }
269
John McCall07fb6be2009-10-22 23:33:21 +0000270 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
271 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000272 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000273 D->getLocation(),
274 D->isMutable(),
275 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000276 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000277 D->getAccess(),
278 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000279 if (!Field) {
280 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000281 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000282 }
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000284 InstantiateAttrs(D, Field);
285
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000286 if (Invalid)
287 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000289 if (!Field->getDeclName()) {
290 // Keep track of where this decl came from.
291 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000292 }
Mike Stump1eb44332009-09-09 15:08:12 +0000293
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000294 Field->setImplicit(D->isImplicit());
295 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000296
297 return Field;
298}
299
John McCall02cace72009-08-28 07:59:38 +0000300Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
301 FriendDecl::FriendUnion FU;
302
303 // Handle friend type expressions by simply substituting template
304 // parameters into the pattern type.
305 if (Type *Ty = D->getFriendType()) {
306 QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs,
307 D->getLocation(), DeclarationName());
308 if (T.isNull()) return 0;
309
310 assert(getLangOptions().CPlusPlus0x || T->isRecordType());
311 FU = T.getTypePtr();
312
313 // Handle everything else by appropriate substitution.
314 } else {
315 NamedDecl *ND = D->getFriendDecl();
316 assert(ND && "friend decl must be a decl or a type!");
317
Douglas Gregora735b202009-10-13 14:39:41 +0000318 // FIXME: We have a problem here, because the nested call to Visit(ND)
319 // will inject the thing that the friend references into the current
320 // owner, which is wrong.
John McCall02cace72009-08-28 07:59:38 +0000321 Decl *NewND = Visit(ND);
322 if (!NewND) return 0;
323
324 FU = cast<NamedDecl>(NewND);
John McCallfd810b12009-08-14 02:03:10 +0000325 }
Mike Stump1eb44332009-09-09 15:08:12 +0000326
John McCall02cace72009-08-28 07:59:38 +0000327 FriendDecl *FD =
328 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU,
329 D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000330 FD->setAccess(AS_public);
John McCall02cace72009-08-28 07:59:38 +0000331 Owner->addDecl(FD);
332 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000333}
334
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000335Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
336 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Douglas Gregorac7610d2009-06-22 20:57:11 +0000338 // The expression in a static assertion is not potentially evaluated.
339 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000341 OwningExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000342 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000343 if (InstantiatedAssertExpr.isInvalid())
344 return 0;
345
Douglas Gregor43d9d922009-08-08 01:41:12 +0000346 OwningExprResult Message(SemaRef, D->getMessage());
347 D->getMessage()->Retain();
Mike Stump1eb44332009-09-09 15:08:12 +0000348 Decl *StaticAssert
349 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
Chris Lattnerb28317a2009-03-28 19:18:32 +0000350 move(InstantiatedAssertExpr),
351 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000352 return StaticAssert;
353}
354
355Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000356 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000357 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000358 D->getTagKeywordLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000359 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000360 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000361 Enum->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000362 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000363 Enum->startDefinition();
364
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000365 llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000366
367 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000368 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
369 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000370 EC != ECEnd; ++EC) {
371 // The specified value for the enumerator.
372 OwningExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000373 if (Expr *UninstValue = EC->getInitExpr()) {
374 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000375 EnterExpressionEvaluationContext Unevaluated(SemaRef,
Douglas Gregorac7610d2009-06-22 20:57:11 +0000376 Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000377
John McCallce3ff2b2009-08-25 22:02:44 +0000378 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000379 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000380
381 // Drop the initial value and continue.
382 bool isInvalid = false;
383 if (Value.isInvalid()) {
384 Value = SemaRef.Owned((Expr *)0);
385 isInvalid = true;
386 }
387
Mike Stump1eb44332009-09-09 15:08:12 +0000388 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000389 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
390 EC->getLocation(), EC->getIdentifier(),
391 move(Value));
392
393 if (isInvalid) {
394 if (EnumConst)
395 EnumConst->setInvalidDecl();
396 Enum->setInvalidDecl();
397 }
398
399 if (EnumConst) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000400 Enum->addDecl(EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000401 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000402 LastEnumConst = EnumConst;
403 }
404 }
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000406 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000407 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000408 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
409 Sema::DeclPtrTy::make(Enum),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000410 &Enumerators[0], Enumerators.size(),
411 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000412
413 return Enum;
414}
415
Douglas Gregor6477b692009-03-25 15:04:13 +0000416Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
417 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
418 return 0;
419}
420
Douglas Gregored9c0f92009-10-29 00:04:11 +0000421namespace {
422 class SortDeclByLocation {
423 SourceManager &SourceMgr;
424
425 public:
426 explicit SortDeclByLocation(SourceManager &SourceMgr)
427 : SourceMgr(SourceMgr) { }
428
429 bool operator()(const Decl *X, const Decl *Y) const {
430 return SourceMgr.isBeforeInTranslationUnit(X->getLocation(),
431 Y->getLocation());
432 }
433 };
434}
435
John McCalle29ba202009-08-20 01:44:21 +0000436Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000437 // Create a local instantiation scope for this class template, which
438 // will contain the instantiations of the template parameters.
439 Sema::LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000440 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000441 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000442 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000443 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000444
445 CXXRecordDecl *Pattern = D->getTemplatedDecl();
446 CXXRecordDecl *RecordInst
447 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner,
448 Pattern->getLocation(), Pattern->getIdentifier(),
Douglas Gregorf0510d42009-10-12 23:11:44 +0000449 Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL,
450 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000451
452 ClassTemplateDecl *Inst
453 = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
454 D->getIdentifier(), InstParams, RecordInst, 0);
455 RecordInst->setDescribedClassTemplate(Inst);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000456 if (D->getFriendObjectKind())
457 Inst->setObjectOfFriendDecl(true);
458 else
459 Inst->setAccess(D->getAccess());
John McCalle29ba202009-08-20 01:44:21 +0000460 Inst->setInstantiatedFromMemberTemplate(D);
Douglas Gregorf0510d42009-10-12 23:11:44 +0000461
462 // Trigger creation of the type for the instantiation.
463 SemaRef.Context.getTypeDeclType(RecordInst);
464
Douglas Gregor259571e2009-10-30 22:42:42 +0000465 // Finish handling of friends.
466 if (Inst->getFriendObjectKind()) {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000467 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000468 }
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000469
John McCalle29ba202009-08-20 01:44:21 +0000470 Owner->addDecl(Inst);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000471
472 // First, we sort the partial specializations by location, so
473 // that we instantiate them in the order they were declared.
474 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
475 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
476 P = D->getPartialSpecializations().begin(),
477 PEnd = D->getPartialSpecializations().end();
478 P != PEnd; ++P)
479 PartialSpecs.push_back(&*P);
480 std::sort(PartialSpecs.begin(), PartialSpecs.end(),
481 SortDeclByLocation(SemaRef.SourceMgr));
482
483 // Instantiate all of the partial specializations of this member class
484 // template.
485 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
486 InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
487
John McCalle29ba202009-08-20 01:44:21 +0000488 return Inst;
489}
490
Douglas Gregord60e1052009-08-27 16:57:43 +0000491Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000492TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
493 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000494 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
495
496 // Lookup the already-instantiated declaration in the instantiation
497 // of the class template and return that.
498 DeclContext::lookup_result Found
499 = Owner->lookup(ClassTemplate->getDeclName());
500 if (Found.first == Found.second)
501 return 0;
502
503 ClassTemplateDecl *InstClassTemplate
504 = dyn_cast<ClassTemplateDecl>(*Found.first);
505 if (!InstClassTemplate)
506 return 0;
507
508 Decl *DCanon = D->getCanonicalDecl();
509 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
510 P = InstClassTemplate->getPartialSpecializations().begin(),
511 PEnd = InstClassTemplate->getPartialSpecializations().end();
512 P != PEnd; ++P) {
513 if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
514 return &*P;
515 }
516
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000517 return 0;
518}
519
520Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000521TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000522 // Create a local instantiation scope for this function template, which
523 // will contain the instantiations of the template parameters and then get
524 // merged with the local instantiation scope for the function template
525 // itself.
526 Sema::LocalInstantiationScope Scope(SemaRef);
527
Douglas Gregord60e1052009-08-27 16:57:43 +0000528 TemplateParameterList *TempParams = D->getTemplateParameters();
529 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000530 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000531 return NULL;
Douglas Gregored9c0f92009-10-29 00:04:11 +0000532
Douglas Gregora735b202009-10-13 14:39:41 +0000533 FunctionDecl *Instantiated = 0;
534 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
535 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
536 InstParams));
537 else
538 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
539 D->getTemplatedDecl(),
540 InstParams));
541
542 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000543 return 0;
544
Mike Stump1eb44332009-09-09 15:08:12 +0000545 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000546 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000547 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000548 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000549 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000550 assert(InstTemplate &&
551 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
552 if (!InstTemplate->getInstantiatedFromMemberTemplate())
553 InstTemplate->setInstantiatedFromMemberTemplate(D);
554
555 // Add non-friends into the owner.
556 if (!InstTemplate->getFriendObjectKind())
557 Owner->addDecl(InstTemplate);
Douglas Gregord60e1052009-08-27 16:57:43 +0000558 return InstTemplate;
559}
560
Douglas Gregord475b8d2009-03-25 21:17:03 +0000561Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
562 CXXRecordDecl *PrevDecl = 0;
563 if (D->isInjectedClassName())
564 PrevDecl = cast<CXXRecordDecl>(Owner);
565
566 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000567 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000568 D->getLocation(), D->getIdentifier(),
569 D->getTagKeywordLoc(), PrevDecl);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000570 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000571 // FIXME: Check against AS_none is an ugly hack to work around the issue that
572 // the tag decls introduced by friend class declarations don't have an access
573 // specifier. Remove once this area of the code gets sorted out.
574 if (D->getAccess() != AS_none)
575 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000576 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000577 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000578
John McCall02cace72009-08-28 07:59:38 +0000579 // If the original function was part of a friend declaration,
580 // inherit its namespace state.
581 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
582 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
583
Anders Carlssond8b285f2009-09-01 04:26:58 +0000584 Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
585
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000586 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000587 return Record;
588}
589
John McCall02cace72009-08-28 07:59:38 +0000590/// Normal class members are of more specific types and therefore
591/// don't make it here. This function serves two purposes:
592/// 1) instantiating function templates
593/// 2) substituting friend declarations
594/// FIXME: preserve function definitions in case #2
Douglas Gregora735b202009-10-13 14:39:41 +0000595 Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
596 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000597 // Check whether there is already a function template specialization for
598 // this declaration.
599 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
600 void *InsertPos = 0;
Douglas Gregora735b202009-10-13 14:39:41 +0000601 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000602 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000603 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000604 TemplateArgs.getInnermost().getFlatArgumentList(),
605 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor828e2262009-07-29 16:09:57 +0000606 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000607
608 FunctionTemplateSpecializationInfo *Info
609 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor127102b2009-06-29 20:59:39 +0000610 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Douglas Gregor127102b2009-06-29 20:59:39 +0000612 // If we already have a function template specialization, return it.
613 if (Info)
614 return Info->Function;
615 }
Mike Stump1eb44332009-09-09 15:08:12 +0000616
Douglas Gregor550d9b22009-10-31 17:21:17 +0000617 Sema::LocalInstantiationScope Scope(SemaRef, TemplateParams != 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000618
Douglas Gregore53060f2009-06-25 22:08:12 +0000619 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000620 QualType T = SubstFunctionType(D, Params);
Douglas Gregore53060f2009-06-25 22:08:12 +0000621 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000622 return 0;
John McCallfd810b12009-08-14 02:03:10 +0000623
Douglas Gregore53060f2009-06-25 22:08:12 +0000624 // Build the instantiated method declaration.
Douglas Gregore95b4092009-09-16 18:34:49 +0000625 DeclContext *DC = SemaRef.FindInstantiatedContext(D->getDeclContext(),
626 TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +0000627 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +0000628 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000629 D->getDeclName(), T, D->getDeclaratorInfo(),
630 D->getStorageClass(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000631 D->isInlineSpecified(), D->hasWrittenPrototype());
John McCall02cace72009-08-28 07:59:38 +0000632 Function->setLexicalDeclContext(Owner);
Mike Stump1eb44332009-09-09 15:08:12 +0000633
Douglas Gregore53060f2009-06-25 22:08:12 +0000634 // Attach the parameters
635 for (unsigned P = 0; P < Params.size(); ++P)
636 Params[P]->setOwningFunction(Function);
637 Function->setParams(SemaRef.Context, Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +0000638
Douglas Gregora735b202009-10-13 14:39:41 +0000639 if (TemplateParams) {
640 // Our resulting instantiation is actually a function template, since we
641 // are substituting only the outer template parameters. For example, given
642 //
643 // template<typename T>
644 // struct X {
645 // template<typename U> friend void f(T, U);
646 // };
647 //
648 // X<int> x;
649 //
650 // We are instantiating the friend function template "f" within X<int>,
651 // which means substituting int for T, but leaving "f" as a friend function
652 // template.
653 // Build the function template itself.
654 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Owner,
655 Function->getLocation(),
656 Function->getDeclName(),
657 TemplateParams, Function);
658 Function->setDescribedFunctionTemplate(FunctionTemplate);
659 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
John McCall02cace72009-08-28 07:59:38 +0000660 }
Douglas Gregora735b202009-10-13 14:39:41 +0000661
Douglas Gregore53060f2009-06-25 22:08:12 +0000662 if (InitFunctionInstantiation(Function, D))
663 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000664
Douglas Gregore53060f2009-06-25 22:08:12 +0000665 bool Redeclaration = false;
666 bool OverloadableAttrRequired = false;
Douglas Gregora735b202009-10-13 14:39:41 +0000667
Douglas Gregore53060f2009-06-25 22:08:12 +0000668 NamedDecl *PrevDecl = 0;
Douglas Gregora735b202009-10-13 14:39:41 +0000669 if (TemplateParams || !FunctionTemplate) {
670 // Look only into the namespace where the friend would be declared to
671 // find a previous declaration. This is the innermost enclosing namespace,
672 // as described in ActOnFriendFunctionDecl.
673 Sema::LookupResult R;
674 SemaRef.LookupQualifiedName(R, DC, Function->getDeclName(),
675 Sema::LookupOrdinaryName, true);
676
677 PrevDecl = R.getAsSingleDecl(SemaRef.Context);
678
679 // In C++, the previous declaration we find might be a tag type
680 // (class or enum). In this case, the new declaration will hide the
681 // tag type. Note that this does does not apply if we're declaring a
682 // typedef (C++ [dcl.typedef]p4).
683 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
684 PrevDecl = 0;
685 }
686
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000687 SemaRef.CheckFunctionDeclaration(Function, PrevDecl, false, Redeclaration,
Douglas Gregore53060f2009-06-25 22:08:12 +0000688 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000689
Douglas Gregora735b202009-10-13 14:39:41 +0000690 // If the original function was part of a friend declaration,
691 // inherit its namespace state and add it to the owner.
692 NamedDecl *FromFriendD
693 = TemplateParams? cast<NamedDecl>(D->getDescribedFunctionTemplate()) : D;
694 if (FromFriendD->getFriendObjectKind()) {
695 NamedDecl *ToFriendD = 0;
696 if (TemplateParams) {
697 ToFriendD = cast<NamedDecl>(FunctionTemplate);
698 PrevDecl = FunctionTemplate->getPreviousDeclaration();
699 } else {
700 ToFriendD = Function;
701 PrevDecl = Function->getPreviousDeclaration();
702 }
703 ToFriendD->setObjectOfFriendDecl(PrevDecl != NULL);
704 if (!Owner->isDependentContext() && !PrevDecl)
705 DC->makeDeclVisibleInContext(ToFriendD, /* Recoverable = */ false);
706
707 if (!TemplateParams)
708 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
709 }
710
711 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000712 // Record this function template specialization.
713 Function->setFunctionTemplateSpecialization(SemaRef.Context,
714 FunctionTemplate,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000715 &TemplateArgs.getInnermost(),
Douglas Gregor127102b2009-06-29 20:59:39 +0000716 InsertPos);
John McCallfd810b12009-08-14 02:03:10 +0000717 }
718
Douglas Gregore53060f2009-06-25 22:08:12 +0000719 return Function;
720}
721
Douglas Gregord60e1052009-08-27 16:57:43 +0000722Decl *
723TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
724 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +0000725 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
726 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +0000727 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +0000728 // We are creating a function template specialization from a function
729 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +0000730 // specialization for this particular set of template arguments.
Douglas Gregor6b906862009-08-21 00:16:32 +0000731 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000732 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000733 TemplateArgs.getInnermost().getFlatArgumentList(),
734 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000735 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000736
737 FunctionTemplateSpecializationInfo *Info
738 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor6b906862009-08-21 00:16:32 +0000739 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000740
Douglas Gregor6b906862009-08-21 00:16:32 +0000741 // If we already have a function template specialization, return it.
742 if (Info)
743 return Info->Function;
744 }
745
Douglas Gregor550d9b22009-10-31 17:21:17 +0000746 Sema::LocalInstantiationScope Scope(SemaRef, TemplateParams != 0);
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000747
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000748 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000749 QualType T = SubstFunctionType(D, Params);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000750 if (T.isNull())
751 return 0;
752
753 // Build the instantiated method declaration.
754 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
Douglas Gregordec06662009-08-21 18:42:58 +0000755 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Douglas Gregordec06662009-08-21 18:42:58 +0000757 DeclarationName Name = D->getDeclName();
Douglas Gregor17e32f32009-08-21 22:43:28 +0000758 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregordec06662009-08-21 18:42:58 +0000759 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
760 Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
761 SemaRef.Context.getCanonicalType(ClassTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000762 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
763 Constructor->getLocation(),
764 Name, T,
Douglas Gregor17e32f32009-08-21 22:43:28 +0000765 Constructor->getDeclaratorInfo(),
Mike Stump1eb44332009-09-09 15:08:12 +0000766 Constructor->isExplicit(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000767 Constructor->isInlineSpecified(), false);
Douglas Gregor17e32f32009-08-21 22:43:28 +0000768 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
769 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
770 Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
771 SemaRef.Context.getCanonicalType(ClassTy));
772 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
773 Destructor->getLocation(), Name,
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000774 T, Destructor->isInlineSpecified(), false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000775 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000776 CanQualType ConvTy
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000777 = SemaRef.Context.getCanonicalType(
John McCall183700f2009-09-21 23:43:11 +0000778 T->getAs<FunctionType>()->getResultType());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000779 Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
780 ConvTy);
781 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
782 Conversion->getLocation(), Name,
783 T, Conversion->getDeclaratorInfo(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000784 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000785 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +0000786 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000787 Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
Douglas Gregordec06662009-08-21 18:42:58 +0000788 D->getDeclName(), T, D->getDeclaratorInfo(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000789 D->isStatic(), D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +0000790 }
Douglas Gregor6b906862009-08-21 00:16:32 +0000791
Douglas Gregord60e1052009-08-27 16:57:43 +0000792 if (TemplateParams) {
793 // Our resulting instantiation is actually a function template, since we
794 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +0000795 //
Douglas Gregord60e1052009-08-27 16:57:43 +0000796 // template<typename T>
797 // struct X {
798 // template<typename U> void f(T, U);
799 // };
800 //
801 // X<int> x;
802 //
803 // We are instantiating the member template "f" within X<int>, which means
804 // substituting int for T, but leaving "f" as a member function template.
805 // Build the function template itself.
806 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
807 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +0000808 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +0000809 TemplateParams, Method);
810 if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +0000811 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +0000812 Method->setDescribedFunctionTemplate(FunctionTemplate);
813 } else if (!FunctionTemplate)
Douglas Gregor2db32322009-10-07 23:56:10 +0000814 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000815
Mike Stump1eb44332009-09-09 15:08:12 +0000816 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000817 // out-of-line, the instantiation will have the same lexical
818 // context (which will be a namespace scope) as the template.
819 if (D->isOutOfLine())
820 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000821
Douglas Gregor5545e162009-03-24 00:38:23 +0000822 // Attach the parameters
823 for (unsigned P = 0; P < Params.size(); ++P)
824 Params[P]->setOwningFunction(Method);
Jay Foadbeaaccd2009-05-21 09:52:38 +0000825 Method->setParams(SemaRef.Context, Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +0000826
827 if (InitMethodInstantiation(Method, D))
828 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000829
Douglas Gregordec06662009-08-21 18:42:58 +0000830 NamedDecl *PrevDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Douglas Gregord60e1052009-08-27 16:57:43 +0000832 if (!FunctionTemplate || TemplateParams) {
John McCallf36e02d2009-10-09 21:13:30 +0000833 Sema::LookupResult R;
834 SemaRef.LookupQualifiedName(R, Owner, Name, Sema::LookupOrdinaryName, true);
835 PrevDecl = R.getAsSingleDecl(SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000836
Douglas Gregordec06662009-08-21 18:42:58 +0000837 // In C++, the previous declaration we find might be a tag type
838 // (class or enum). In this case, the new declaration will hide the
839 // tag type. Note that this does does not apply if we're declaring a
840 // typedef (C++ [dcl.typedef]p4).
841 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
842 PrevDecl = 0;
843 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000844
Douglas Gregord60e1052009-08-27 16:57:43 +0000845 if (FunctionTemplate && !TemplateParams)
Douglas Gregor6b906862009-08-21 00:16:32 +0000846 // Record this function template specialization.
847 Method->setFunctionTemplateSpecialization(SemaRef.Context,
848 FunctionTemplate,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000849 &TemplateArgs.getInnermost(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000850 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000851
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000852 bool Redeclaration = false;
853 bool OverloadableAttrRequired = false;
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000854 SemaRef.CheckFunctionDeclaration(Method, PrevDecl, false, Redeclaration,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000855 /*FIXME:*/OverloadableAttrRequired);
856
Douglas Gregora735b202009-10-13 14:39:41 +0000857 if (!FunctionTemplate && (!Method->isInvalidDecl() || !PrevDecl) &&
858 !Method->getFriendObjectKind())
Douglas Gregordec06662009-08-21 18:42:58 +0000859 Owner->addDecl(Method);
Mike Stump1eb44332009-09-09 15:08:12 +0000860
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000861 return Method;
862}
863
Douglas Gregor615c5d42009-03-24 16:43:20 +0000864Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +0000865 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000866}
867
Douglas Gregor03b2b072009-03-24 00:15:49 +0000868Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +0000869 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +0000870}
871
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000872Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000873 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000874}
875
Douglas Gregor6477b692009-03-25 15:04:13 +0000876ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
John McCall58e46772009-10-23 21:48:59 +0000877 QualType T;
878 DeclaratorInfo *DI = D->getDeclaratorInfo();
879 if (DI) {
880 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
881 D->getDeclName());
882 if (DI) T = DI->getType();
883 } else {
884 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
885 D->getDeclName());
886 DI = 0;
887 }
888
889 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000890 return 0;
891
John McCall58e46772009-10-23 21:48:59 +0000892 T = SemaRef.adjustParameterType(T);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000893
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000894 // Allocate the parameter
John McCall58e46772009-10-23 21:48:59 +0000895 ParmVarDecl *Param
896 = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
897 D->getIdentifier(), T, DI, D->getStorageClass(), 0);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000898
Anders Carlsson9351c172009-08-25 03:18:48 +0000899 // Mark the default argument as being uninstantiated.
Douglas Gregorf43d0b32009-09-25 06:56:31 +0000900 if (D->hasUninstantiatedDefaultArg())
901 Param->setUninstantiatedDefaultArg(D->getUninstantiatedDefaultArg());
Douglas Gregor0ed09302009-09-25 07:03:22 +0000902 else if (Expr *Arg = D->getDefaultArg())
903 Param->setUninstantiatedDefaultArg(Arg);
904
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000905 // Note: we don't try to instantiate function parameters until after
906 // we've instantiated the function's type. Therefore, we don't have
907 // to check for 'void' parameter types here.
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000908 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000909 return Param;
910}
911
John McCalle29ba202009-08-20 01:44:21 +0000912Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
913 TemplateTypeParmDecl *D) {
914 // TODO: don't always clone when decls are refcounted.
915 const Type* T = D->getTypeForDecl();
916 assert(T->isTemplateTypeParmType());
917 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000918
John McCalle29ba202009-08-20 01:44:21 +0000919 TemplateTypeParmDecl *Inst =
920 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor550d9b22009-10-31 17:21:17 +0000921 TTPT->getDepth() - 1, TTPT->getIndex(),
John McCalle29ba202009-08-20 01:44:21 +0000922 TTPT->getName(),
923 D->wasDeclaredWithTypename(),
924 D->isParameterPack());
925
Douglas Gregor0f8716b2009-11-09 19:17:50 +0000926 if (D->hasDefaultArgument())
927 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
John McCalle29ba202009-08-20 01:44:21 +0000928
Douglas Gregor550d9b22009-10-31 17:21:17 +0000929 // Introduce this template parameter's instantiation into the instantiation
930 // scope.
931 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
932
John McCalle29ba202009-08-20 01:44:21 +0000933 return Inst;
934}
935
Douglas Gregor33642df2009-10-23 23:25:44 +0000936Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
937 NonTypeTemplateParmDecl *D) {
938 // Substitute into the type of the non-type template parameter.
939 QualType T;
940 DeclaratorInfo *DI = D->getDeclaratorInfo();
941 if (DI) {
942 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
943 D->getDeclName());
944 if (DI) T = DI->getType();
945 } else {
946 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
947 D->getDeclName());
948 DI = 0;
949 }
950 if (T.isNull())
951 return 0;
952
953 // Check that this type is acceptable for a non-type template parameter.
954 bool Invalid = false;
955 T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
956 if (T.isNull()) {
957 T = SemaRef.Context.IntTy;
958 Invalid = true;
959 }
960
961 NonTypeTemplateParmDecl *Param
962 = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
963 D->getDepth() - 1, D->getPosition(),
964 D->getIdentifier(), T, DI);
965 if (Invalid)
966 Param->setInvalidDecl();
967
968 Param->setDefaultArgument(D->getDefaultArgument());
Douglas Gregor550d9b22009-10-31 17:21:17 +0000969
970 // Introduce this template parameter's instantiation into the instantiation
971 // scope.
972 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +0000973 return Param;
974}
975
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000976Decl *
977TemplateDeclInstantiator::VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000978 NestedNameSpecifier *NNS =
979 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
980 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000981 TemplateArgs);
982 if (!NNS)
983 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000985 CXXScopeSpec SS;
986 SS.setRange(D->getTargetNestedNameRange());
987 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +0000988
989 NamedDecl *UD =
990 SemaRef.BuildUsingDeclaration(D->getLocation(), SS,
991 D->getTargetNameLocation(),
Anders Carlsson0d8df782009-08-29 19:37:28 +0000992 D->getTargetName(), 0, D->isTypeName());
993 if (UD)
Mike Stump1eb44332009-09-09 15:08:12 +0000994 SemaRef.Context.setInstantiatedFromUnresolvedUsingDecl(cast<UsingDecl>(UD),
Anders Carlsson0d8df782009-08-29 19:37:28 +0000995 D);
996 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000997}
998
John McCallce3ff2b2009-08-25 22:02:44 +0000999Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001000 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001001 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001002 return Instantiator.Visit(D);
1003}
1004
John McCalle29ba202009-08-20 01:44:21 +00001005/// \brief Instantiates a nested template parameter list in the current
1006/// instantiation context.
1007///
1008/// \param L The parameter list to instantiate
1009///
1010/// \returns NULL if there was an error
1011TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001012TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001013 // Get errors for all the parameters before bailing out.
1014 bool Invalid = false;
1015
1016 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001017 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001018 ParamVector Params;
1019 Params.reserve(N);
1020 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1021 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001022 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001023 Params.push_back(D);
1024 Invalid = Invalid || !D;
1025 }
1026
1027 // Clean up if we had an error.
1028 if (Invalid) {
1029 for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
1030 PI != PE; ++PI)
1031 if (*PI)
1032 (*PI)->Destroy(SemaRef.Context);
1033 return NULL;
1034 }
1035
1036 TemplateParameterList *InstL
1037 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1038 L->getLAngleLoc(), &Params.front(), N,
1039 L->getRAngleLoc());
1040 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001041}
John McCalle29ba202009-08-20 01:44:21 +00001042
Douglas Gregored9c0f92009-10-29 00:04:11 +00001043/// \brief Instantiate the declaration of a class template partial
1044/// specialization.
1045///
1046/// \param ClassTemplate the (instantiated) class template that is partially
1047// specialized by the instantiation of \p PartialSpec.
1048///
1049/// \param PartialSpec the (uninstantiated) class template partial
1050/// specialization that we are instantiating.
1051///
1052/// \returns true if there was an error, false otherwise.
1053bool
1054TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1055 ClassTemplateDecl *ClassTemplate,
1056 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001057 // Create a local instantiation scope for this class template partial
1058 // specialization, which will contain the instantiations of the template
1059 // parameters.
1060 Sema::LocalInstantiationScope Scope(SemaRef);
1061
Douglas Gregored9c0f92009-10-29 00:04:11 +00001062 // Substitute into the template parameters of the class template partial
1063 // specialization.
1064 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1065 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1066 if (!InstParams)
1067 return true;
1068
1069 // Substitute into the template arguments of the class template partial
1070 // specialization.
John McCall833ca992009-10-29 08:12:44 +00001071 const TemplateArgumentLoc *PartialSpecTemplateArgs
1072 = PartialSpec->getTemplateArgsAsWritten();
1073 unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1074
1075 llvm::SmallVector<TemplateArgumentLoc, 4> InstTemplateArgs(N);
1076 for (unsigned I = 0; I != N; ++I) {
1077 if (SemaRef.Subst(PartialSpecTemplateArgs[I], InstTemplateArgs[I],
1078 TemplateArgs))
Douglas Gregored9c0f92009-10-29 00:04:11 +00001079 return true;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001080 }
1081
1082
1083 // Check that the template argument list is well-formed for this
1084 // class template.
1085 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1086 InstTemplateArgs.size());
1087 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1088 PartialSpec->getLocation(),
1089 /*FIXME:*/PartialSpec->getLocation(),
1090 InstTemplateArgs.data(),
1091 InstTemplateArgs.size(),
1092 /*FIXME:*/PartialSpec->getLocation(),
1093 false,
1094 Converted))
1095 return true;
1096
1097 // Figure out where to insert this class template partial specialization
1098 // in the member template's set of class template partial specializations.
1099 llvm::FoldingSetNodeID ID;
1100 ClassTemplatePartialSpecializationDecl::Profile(ID,
1101 Converted.getFlatArguments(),
1102 Converted.flatSize(),
1103 SemaRef.Context);
1104 void *InsertPos = 0;
1105 ClassTemplateSpecializationDecl *PrevDecl
1106 = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
1107 InsertPos);
1108
1109 // Build the canonical type that describes the converted template
1110 // arguments of the class template partial specialization.
1111 QualType CanonType
1112 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1113 Converted.getFlatArguments(),
1114 Converted.flatSize());
1115
1116 // Build the fully-sugared type for this class template
1117 // specialization as the user wrote in the specialization
1118 // itself. This means that we'll pretty-print the type retrieved
1119 // from the specialization's declaration the way that the user
1120 // actually wrote the specialization, rather than formatting the
1121 // name based on the "canonical" representation used to store the
1122 // template arguments in the specialization.
1123 QualType WrittenTy
1124 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1125 InstTemplateArgs.data(),
1126 InstTemplateArgs.size(),
1127 CanonType);
1128
1129 if (PrevDecl) {
1130 // We've already seen a partial specialization with the same template
1131 // parameters and template arguments. This can happen, for example, when
1132 // substituting the outer template arguments ends up causing two
1133 // class template partial specializations of a member class template
1134 // to have identical forms, e.g.,
1135 //
1136 // template<typename T, typename U>
1137 // struct Outer {
1138 // template<typename X, typename Y> struct Inner;
1139 // template<typename Y> struct Inner<T, Y>;
1140 // template<typename Y> struct Inner<U, Y>;
1141 // };
1142 //
1143 // Outer<int, int> outer; // error: the partial specializations of Inner
1144 // // have the same signature.
1145 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1146 << WrittenTy;
1147 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1148 << SemaRef.Context.getTypeDeclType(PrevDecl);
1149 return true;
1150 }
1151
1152
1153 // Create the class template partial specialization declaration.
1154 ClassTemplatePartialSpecializationDecl *InstPartialSpec
1155 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context, Owner,
1156 PartialSpec->getLocation(),
1157 InstParams,
1158 ClassTemplate,
1159 Converted,
John McCall833ca992009-10-29 08:12:44 +00001160 InstTemplateArgs.data(),
1161 InstTemplateArgs.size(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00001162 0);
1163 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
1164 InstPartialSpec->setTypeAsWritten(WrittenTy);
1165
1166 // Add this partial specialization to the set of class template partial
1167 // specializations.
1168 ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec,
1169 InsertPos);
1170 return false;
1171}
1172
John McCallce3ff2b2009-08-25 22:02:44 +00001173/// \brief Does substitution on the type of the given function, including
1174/// all of the function parameters.
Douglas Gregor5545e162009-03-24 00:38:23 +00001175///
John McCallce3ff2b2009-08-25 22:02:44 +00001176/// \param D The function whose type will be the basis of the substitution
Douglas Gregor5545e162009-03-24 00:38:23 +00001177///
1178/// \param Params the instantiated parameter declarations
1179
John McCallce3ff2b2009-08-25 22:02:44 +00001180/// \returns the instantiated function's type if successful, a NULL
Douglas Gregor5545e162009-03-24 00:38:23 +00001181/// type if there was an error.
Mike Stump1eb44332009-09-09 15:08:12 +00001182QualType
John McCallce3ff2b2009-08-25 22:02:44 +00001183TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001184 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
1185 bool InvalidDecl = false;
1186
John McCallce3ff2b2009-08-25 22:02:44 +00001187 // Substitute all of the function's formal parameter types.
Douglas Gregor7e063902009-05-11 23:53:27 +00001188 TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001189 llvm::SmallVector<QualType, 4> ParamTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001190 for (FunctionDecl::param_iterator P = D->param_begin(),
Douglas Gregor5545e162009-03-24 00:38:23 +00001191 PEnd = D->param_end();
1192 P != PEnd; ++P) {
Douglas Gregor6477b692009-03-25 15:04:13 +00001193 if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
Douglas Gregor5545e162009-03-24 00:38:23 +00001194 if (PInst->getType()->isVoidType()) {
1195 SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
1196 PInst->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001197 } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001198 PInst->getType(),
1199 diag::err_abstract_type_in_decl,
1200 Sema::AbstractParamType))
Douglas Gregor5545e162009-03-24 00:38:23 +00001201 PInst->setInvalidDecl();
1202
1203 Params.push_back(PInst);
1204 ParamTys.push_back(PInst->getType());
1205
1206 if (PInst->isInvalidDecl())
1207 InvalidDecl = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001208 } else
Douglas Gregor5545e162009-03-24 00:38:23 +00001209 InvalidDecl = true;
1210 }
1211
1212 // FIXME: Deallocate dead declarations.
1213 if (InvalidDecl)
1214 return QualType();
1215
John McCall183700f2009-09-21 23:43:11 +00001216 const FunctionProtoType *Proto = D->getType()->getAs<FunctionProtoType>();
Douglas Gregor5545e162009-03-24 00:38:23 +00001217 assert(Proto && "Missing prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +00001218 QualType ResultType
John McCallce3ff2b2009-08-25 22:02:44 +00001219 = SemaRef.SubstType(Proto->getResultType(), TemplateArgs,
1220 D->getLocation(), D->getDeclName());
Douglas Gregor5545e162009-03-24 00:38:23 +00001221 if (ResultType.isNull())
1222 return QualType();
1223
Jay Foadbeaaccd2009-05-21 09:52:38 +00001224 return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
Douglas Gregor5545e162009-03-24 00:38:23 +00001225 Proto->isVariadic(), Proto->getTypeQuals(),
1226 D->getLocation(), D->getDeclName());
1227}
1228
Mike Stump1eb44332009-09-09 15:08:12 +00001229/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00001230/// declaration (New) from the corresponding fields of its template (Tmpl).
1231///
1232/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001233bool
1234TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00001235 FunctionDecl *Tmpl) {
1236 if (Tmpl->isDeleted())
1237 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00001238
Douglas Gregorcca9e962009-07-01 22:01:06 +00001239 // If we are performing substituting explicitly-specified template arguments
1240 // or deduced template arguments into a function template and we reach this
1241 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00001242 // to keeping the new function template specialization. We therefore
1243 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00001244 // into a template instantiation for this specific function template
1245 // specialization, which is not a SFINAE context, so that we diagnose any
1246 // further errors in the declaration itself.
1247 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1248 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1249 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1250 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00001251 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00001252 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001253 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00001254 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00001255 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001256 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1257 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
1258 }
1259 }
Mike Stump1eb44332009-09-09 15:08:12 +00001260
Douglas Gregore53060f2009-06-25 22:08:12 +00001261 return false;
1262}
1263
Douglas Gregor5545e162009-03-24 00:38:23 +00001264/// \brief Initializes common fields of an instantiated method
1265/// declaration (New) from the corresponding fields of its template
1266/// (Tmpl).
1267///
1268/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001269bool
1270TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00001271 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001272 if (InitFunctionInstantiation(New, Tmpl))
1273 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001274
Douglas Gregor5545e162009-03-24 00:38:23 +00001275 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
1276 New->setAccess(Tmpl->getAccess());
Anders Carlsson77b7f1d2009-05-14 22:15:41 +00001277 if (Tmpl->isVirtualAsWritten()) {
1278 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +00001279 Record->setAggregate(false);
1280 Record->setPOD(false);
Eli Friedman1d954f62009-08-15 21:55:26 +00001281 Record->setEmpty(false);
Douglas Gregor5545e162009-03-24 00:38:23 +00001282 Record->setPolymorphic(true);
1283 }
Douglas Gregor5545e162009-03-24 00:38:23 +00001284 if (Tmpl->isPure()) {
1285 New->setPure();
1286 Record->setAbstract(true);
1287 }
1288
1289 // FIXME: attributes
1290 // FIXME: New needs a pointer to Tmpl
1291 return false;
1292}
Douglas Gregora58861f2009-05-13 20:28:22 +00001293
1294/// \brief Instantiate the definition of the given function from its
1295/// template.
1296///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001297/// \param PointOfInstantiation the point at which the instantiation was
1298/// required. Note that this is not precisely a "point of instantiation"
1299/// for the function, but it's close.
1300///
Douglas Gregora58861f2009-05-13 20:28:22 +00001301/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001302/// function template specialization or member function of a class template
1303/// specialization.
1304///
1305/// \param Recursive if true, recursively instantiates any functions that
1306/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001307///
1308/// \param DefinitionRequired if true, then we are performing an explicit
1309/// instantiation where the body of the function is required. Complain if
1310/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001311void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001312 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001313 bool Recursive,
1314 bool DefinitionRequired) {
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001315 if (Function->isInvalidDecl())
1316 return;
1317
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001318 assert(!Function->getBody() && "Already instantiated!");
Mike Stump1eb44332009-09-09 15:08:12 +00001319
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001320 // Never instantiate an explicit specialization.
1321 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1322 return;
1323
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001324 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00001325 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001326 Stmt *Pattern = 0;
1327 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001328 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001329
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001330 if (!Pattern) {
1331 if (DefinitionRequired) {
1332 if (Function->getPrimaryTemplate())
1333 Diag(PointOfInstantiation,
1334 diag::err_explicit_instantiation_undefined_func_template)
1335 << Function->getPrimaryTemplate();
1336 else
1337 Diag(PointOfInstantiation,
1338 diag::err_explicit_instantiation_undefined_member)
1339 << 1 << Function->getDeclName() << Function->getDeclContext();
1340
1341 if (PatternDecl)
1342 Diag(PatternDecl->getLocation(),
1343 diag::note_explicit_instantiation_here);
1344 }
1345
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001346 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001347 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001348
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001349 // C++0x [temp.explicit]p9:
1350 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00001351 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001352 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00001353 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001354 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001355 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001356 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001357
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001358 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
1359 if (Inst)
1360 return;
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001361
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001362 // If we're performing recursive template instantiation, create our own
1363 // queue of pending implicit instantiations that we will instantiate later,
1364 // while we're still within our own instantiation context.
1365 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1366 if (Recursive)
1367 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001368
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001369 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
1370
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001371 // Introduce a new scope where local variable instantiations will be
1372 // recorded.
1373 LocalInstantiationScope Scope(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001374
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001375 // Introduce the instantiated function parameters into the local
1376 // instantiation scope.
1377 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
1378 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
1379 Function->getParamDecl(I));
1380
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001381 // Enter the scope of this instantiation. We don't use
1382 // PushDeclContext because we don't have a scope.
1383 DeclContext *PreviousContext = CurContext;
1384 CurContext = Function;
1385
Mike Stump1eb44332009-09-09 15:08:12 +00001386 MultiLevelTemplateArgumentList TemplateArgs =
Anders Carlsson09025312009-08-29 05:16:22 +00001387 getTemplateInstantiationArgs(Function);
1388
1389 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001390 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00001391 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
1392 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
1393 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001394 }
1395
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001396 // Instantiate the function body.
Anders Carlsson09025312009-08-29 05:16:22 +00001397 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001398
Douglas Gregor52604ab2009-09-11 21:19:12 +00001399 if (Body.isInvalid())
1400 Function->setInvalidDecl();
1401
Mike Stump1eb44332009-09-09 15:08:12 +00001402 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001403 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001404
1405 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00001406
1407 DeclGroupRef DG(Function);
1408 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001409
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001410 if (Recursive) {
1411 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001412 // instantiation of this template.
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001413 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001414
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001415 // Restore the set of pending implicit instantiations.
1416 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1417 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001418}
1419
1420/// \brief Instantiate the definition of the given variable from its
1421/// template.
1422///
Douglas Gregor7caa6822009-07-24 20:34:43 +00001423/// \param PointOfInstantiation the point at which the instantiation was
1424/// required. Note that this is not precisely a "point of instantiation"
1425/// for the function, but it's close.
1426///
1427/// \param Var the already-instantiated declaration of a static member
1428/// variable of a class template specialization.
1429///
1430/// \param Recursive if true, recursively instantiates any functions that
1431/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001432///
1433/// \param DefinitionRequired if true, then we are performing an explicit
1434/// instantiation where an out-of-line definition of the member variable
1435/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001436void Sema::InstantiateStaticDataMemberDefinition(
1437 SourceLocation PointOfInstantiation,
1438 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001439 bool Recursive,
1440 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001441 if (Var->isInvalidDecl())
1442 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001443
Douglas Gregor7caa6822009-07-24 20:34:43 +00001444 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001445 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00001446 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00001447 assert(Def->isStaticDataMember() && "Not a static data member?");
1448 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00001449
Douglas Gregor0d035142009-10-27 18:42:08 +00001450 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001451 // We did not find an out-of-line definition of this static data member,
1452 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00001453 // instantiate this definition (or provide a specialization for it) in
1454 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001455 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001456 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001457 Diag(PointOfInstantiation,
1458 diag::err_explicit_instantiation_undefined_member)
1459 << 2 << Var->getDeclName() << Var->getDeclContext();
1460 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
1461 }
1462
Douglas Gregor7caa6822009-07-24 20:34:43 +00001463 return;
1464 }
1465
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001466 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001467 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001468 return;
1469
1470 // C++0x [temp.explicit]p9:
1471 // Except for inline functions, other explicit instantiation declarations
1472 // have the effect of suppressing the implicit instantiation of the entity
1473 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001474 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001475 == TSK_ExplicitInstantiationDeclaration)
1476 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001477
Douglas Gregor7caa6822009-07-24 20:34:43 +00001478 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
1479 if (Inst)
1480 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001481
Douglas Gregor7caa6822009-07-24 20:34:43 +00001482 // If we're performing recursive template instantiation, create our own
1483 // queue of pending implicit instantiations that we will instantiate later,
1484 // while we're still within our own instantiation context.
1485 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1486 if (Recursive)
1487 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001488
Douglas Gregor7caa6822009-07-24 20:34:43 +00001489 // Enter the scope of this instantiation. We don't use
1490 // PushDeclContext because we don't have a scope.
1491 DeclContext *PreviousContext = CurContext;
1492 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001493
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001494 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00001495 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00001496 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00001497 CurContext = PreviousContext;
1498
1499 if (Var) {
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001500 Var->setPreviousDeclaration(OldVar);
Douglas Gregor583f33b2009-10-15 18:07:02 +00001501 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
1502 assert(MSInfo && "Missing member specialization information?");
1503 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
1504 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00001505 DeclGroupRef DG(Var);
1506 Consumer.HandleTopLevelDecl(DG);
1507 }
Mike Stump1eb44332009-09-09 15:08:12 +00001508
Douglas Gregor7caa6822009-07-24 20:34:43 +00001509 if (Recursive) {
1510 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001511 // instantiation of this template.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001512 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001513
Douglas Gregor7caa6822009-07-24 20:34:43 +00001514 // Restore the set of pending implicit instantiations.
1515 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001516 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001517}
Douglas Gregor815215d2009-05-27 05:35:12 +00001518
Anders Carlsson09025312009-08-29 05:16:22 +00001519void
1520Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
1521 const CXXConstructorDecl *Tmpl,
1522 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001523
Anders Carlsson09025312009-08-29 05:16:22 +00001524 llvm::SmallVector<MemInitTy*, 4> NewInits;
1525
1526 // Instantiate all the initializers.
1527 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00001528 InitsEnd = Tmpl->init_end();
1529 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00001530 CXXBaseOrMemberInitializer *Init = *Inits;
1531
1532 ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001533
Anders Carlsson09025312009-08-29 05:16:22 +00001534 // Instantiate all the arguments.
1535 for (ExprIterator Args = Init->arg_begin(), ArgsEnd = Init->arg_end();
1536 Args != ArgsEnd; ++Args) {
1537 OwningExprResult NewArg = SubstExpr(*Args, TemplateArgs);
1538
1539 if (NewArg.isInvalid())
1540 New->setInvalidDecl();
1541 else
1542 NewArgs.push_back(NewArg.takeAs<Expr>());
1543 }
1544
1545 MemInitResult NewInit;
1546
1547 if (Init->isBaseInitializer()) {
Eli Friedmanc5573a82009-08-29 22:22:07 +00001548 QualType BaseType(Init->getBaseClass(), 0);
1549 BaseType = SubstType(BaseType, TemplateArgs, Init->getSourceLocation(),
1550 New->getDeclName());
Anders Carlsson09025312009-08-29 05:16:22 +00001551
1552 NewInit = BuildBaseInitializer(BaseType,
Mike Stump1eb44332009-09-09 15:08:12 +00001553 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001554 NewArgs.size(),
1555 Init->getSourceLocation(),
1556 Init->getRParenLoc(),
1557 New->getParent());
1558 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001559 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00001560
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001561 // Is this an anonymous union?
1562 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregore95b4092009-09-16 18:34:49 +00001563 Member = cast<FieldDecl>(FindInstantiatedDecl(UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001564 else
Douglas Gregore95b4092009-09-16 18:34:49 +00001565 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMember(),
1566 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00001567
1568 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001569 NewArgs.size(),
1570 Init->getSourceLocation(),
1571 Init->getRParenLoc());
1572 }
1573
1574 if (NewInit.isInvalid())
1575 New->setInvalidDecl();
1576 else {
1577 // FIXME: It would be nice if ASTOwningVector had a release function.
1578 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001579
Anders Carlsson09025312009-08-29 05:16:22 +00001580 NewInits.push_back((MemInitTy *)NewInit.get());
1581 }
1582 }
Mike Stump1eb44332009-09-09 15:08:12 +00001583
Anders Carlsson09025312009-08-29 05:16:22 +00001584 // Assign all the initializers to the new constructor.
Mike Stump1eb44332009-09-09 15:08:12 +00001585 ActOnMemInitializers(DeclPtrTy::make(New),
Anders Carlsson09025312009-08-29 05:16:22 +00001586 /*FIXME: ColonLoc */
1587 SourceLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001588 NewInits.data(), NewInits.size());
Anders Carlsson09025312009-08-29 05:16:22 +00001589}
1590
John McCall52a575a2009-08-29 08:11:13 +00001591// TODO: this could be templated if the various decl types used the
1592// same method name.
1593static bool isInstantiationOf(ClassTemplateDecl *Pattern,
1594 ClassTemplateDecl *Instance) {
1595 Pattern = Pattern->getCanonicalDecl();
1596
1597 do {
1598 Instance = Instance->getCanonicalDecl();
1599 if (Pattern == Instance) return true;
1600 Instance = Instance->getInstantiatedFromMemberTemplate();
1601 } while (Instance);
1602
1603 return false;
1604}
1605
Douglas Gregor0d696532009-09-28 06:34:35 +00001606static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
1607 FunctionTemplateDecl *Instance) {
1608 Pattern = Pattern->getCanonicalDecl();
1609
1610 do {
1611 Instance = Instance->getCanonicalDecl();
1612 if (Pattern == Instance) return true;
1613 Instance = Instance->getInstantiatedFromMemberTemplate();
1614 } while (Instance);
1615
1616 return false;
1617}
1618
Douglas Gregored9c0f92009-10-29 00:04:11 +00001619static bool
1620isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
1621 ClassTemplatePartialSpecializationDecl *Instance) {
1622 Pattern
1623 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
1624 do {
1625 Instance = cast<ClassTemplatePartialSpecializationDecl>(
1626 Instance->getCanonicalDecl());
1627 if (Pattern == Instance)
1628 return true;
1629 Instance = Instance->getInstantiatedFromMember();
1630 } while (Instance);
1631
1632 return false;
1633}
1634
John McCall52a575a2009-08-29 08:11:13 +00001635static bool isInstantiationOf(CXXRecordDecl *Pattern,
1636 CXXRecordDecl *Instance) {
1637 Pattern = Pattern->getCanonicalDecl();
1638
1639 do {
1640 Instance = Instance->getCanonicalDecl();
1641 if (Pattern == Instance) return true;
1642 Instance = Instance->getInstantiatedFromMemberClass();
1643 } while (Instance);
1644
1645 return false;
1646}
1647
1648static bool isInstantiationOf(FunctionDecl *Pattern,
1649 FunctionDecl *Instance) {
1650 Pattern = Pattern->getCanonicalDecl();
1651
1652 do {
1653 Instance = Instance->getCanonicalDecl();
1654 if (Pattern == Instance) return true;
1655 Instance = Instance->getInstantiatedFromMemberFunction();
1656 } while (Instance);
1657
1658 return false;
1659}
1660
1661static bool isInstantiationOf(EnumDecl *Pattern,
1662 EnumDecl *Instance) {
1663 Pattern = Pattern->getCanonicalDecl();
1664
1665 do {
1666 Instance = Instance->getCanonicalDecl();
1667 if (Pattern == Instance) return true;
1668 Instance = Instance->getInstantiatedFromMemberEnum();
1669 } while (Instance);
1670
1671 return false;
1672}
1673
Anders Carlsson0d8df782009-08-29 19:37:28 +00001674static bool isInstantiationOf(UnresolvedUsingDecl *Pattern,
1675 UsingDecl *Instance,
1676 ASTContext &C) {
1677 return C.getInstantiatedFromUnresolvedUsingDecl(Instance) == Pattern;
1678}
1679
John McCall52a575a2009-08-29 08:11:13 +00001680static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
1681 VarDecl *Instance) {
1682 assert(Instance->isStaticDataMember());
1683
1684 Pattern = Pattern->getCanonicalDecl();
1685
1686 do {
1687 Instance = Instance->getCanonicalDecl();
1688 if (Pattern == Instance) return true;
1689 Instance = Instance->getInstantiatedFromStaticDataMember();
1690 } while (Instance);
1691
1692 return false;
1693}
1694
Douglas Gregor815215d2009-05-27 05:35:12 +00001695static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00001696 if (D->getKind() != Other->getKind()) {
1697 if (UnresolvedUsingDecl *UUD = dyn_cast<UnresolvedUsingDecl>(D)) {
1698 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
1699 return isInstantiationOf(UUD, UD, Ctx);
1700 }
1701 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001702
Anders Carlsson0d8df782009-08-29 19:37:28 +00001703 return false;
1704 }
Mike Stump1eb44332009-09-09 15:08:12 +00001705
John McCall52a575a2009-08-29 08:11:13 +00001706 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
1707 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001708
John McCall52a575a2009-08-29 08:11:13 +00001709 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
1710 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00001711
John McCall52a575a2009-08-29 08:11:13 +00001712 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
1713 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00001714
Douglas Gregor7caa6822009-07-24 20:34:43 +00001715 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00001716 if (Var->isStaticDataMember())
1717 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
1718
1719 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
1720 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00001721
Douglas Gregor0d696532009-09-28 06:34:35 +00001722 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
1723 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
1724
Douglas Gregored9c0f92009-10-29 00:04:11 +00001725 if (ClassTemplatePartialSpecializationDecl *PartialSpec
1726 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
1727 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
1728 PartialSpec);
1729
Anders Carlssond8b285f2009-09-01 04:26:58 +00001730 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
1731 if (!Field->getDeclName()) {
1732 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00001733 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00001734 cast<FieldDecl>(D);
1735 }
1736 }
Mike Stump1eb44332009-09-09 15:08:12 +00001737
Douglas Gregor815215d2009-05-27 05:35:12 +00001738 return D->getDeclName() && isa<NamedDecl>(Other) &&
1739 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
1740}
1741
1742template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00001743static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00001744 NamedDecl *D,
1745 ForwardIterator first,
1746 ForwardIterator last) {
1747 for (; first != last; ++first)
1748 if (isInstantiationOf(Ctx, D, *first))
1749 return cast<NamedDecl>(*first);
1750
1751 return 0;
1752}
1753
John McCall02cace72009-08-28 07:59:38 +00001754/// \brief Finds the instantiation of the given declaration context
1755/// within the current instantiation.
1756///
1757/// \returns NULL if there was an error
Douglas Gregore95b4092009-09-16 18:34:49 +00001758DeclContext *Sema::FindInstantiatedContext(DeclContext* DC,
1759 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00001760 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00001761 Decl* ID = FindInstantiatedDecl(D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00001762 return cast_or_null<DeclContext>(ID);
1763 } else return DC;
1764}
1765
Douglas Gregored961e72009-05-27 17:54:46 +00001766/// \brief Find the instantiation of the given declaration within the
1767/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00001768///
1769/// This routine is intended to be used when \p D is a declaration
1770/// referenced from within a template, that needs to mapped into the
1771/// corresponding declaration within an instantiation. For example,
1772/// given:
1773///
1774/// \code
1775/// template<typename T>
1776/// struct X {
1777/// enum Kind {
1778/// KnownValue = sizeof(T)
1779/// };
1780///
1781/// bool getKind() const { return KnownValue; }
1782/// };
1783///
1784/// template struct X<int>;
1785/// \endcode
1786///
1787/// In the instantiation of X<int>::getKind(), we need to map the
1788/// EnumConstantDecl for KnownValue (which refers to
1789/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00001790/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
1791/// this mapping from within the instantiation of X<int>.
Douglas Gregore95b4092009-09-16 18:34:49 +00001792NamedDecl *Sema::FindInstantiatedDecl(NamedDecl *D,
1793 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor44c73842009-09-01 17:53:10 +00001794 if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D)) {
1795 // Transform all of the elements of the overloaded function set.
Mike Stump1eb44332009-09-09 15:08:12 +00001796 OverloadedFunctionDecl *Result
Douglas Gregor44c73842009-09-01 17:53:10 +00001797 = OverloadedFunctionDecl::Create(Context, CurContext, Ovl->getDeclName());
Mike Stump1eb44332009-09-09 15:08:12 +00001798
Douglas Gregor44c73842009-09-01 17:53:10 +00001799 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
1800 FEnd = Ovl->function_end();
1801 F != FEnd; ++F) {
1802 Result->addOverload(
Douglas Gregore95b4092009-09-16 18:34:49 +00001803 AnyFunctionDecl::getFromNamedDecl(FindInstantiatedDecl(*F,
1804 TemplateArgs)));
Douglas Gregor44c73842009-09-01 17:53:10 +00001805 }
Mike Stump1eb44332009-09-09 15:08:12 +00001806
Douglas Gregor44c73842009-09-01 17:53:10 +00001807 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001808 }
1809
Douglas Gregor815215d2009-05-27 05:35:12 +00001810 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00001811 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
1812 isa<TemplateTypeParmDecl>(D) || isa<TemplateTypeParmDecl>(D) ||
1813 ParentDC->isFunctionOrMethod()) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00001814 // D is a local of some kind. Look into the map of local
1815 // declarations to their instantiations.
1816 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
1817 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001818
Douglas Gregore95b4092009-09-16 18:34:49 +00001819 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
1820 if (!Record->isDependentContext())
1821 return D;
1822
1823 // If the RecordDecl is actually the injected-class-name or a "templated"
1824 // declaration for a class template or class template partial
1825 // specialization, substitute into the injected-class-name of the
1826 // class template or partial specialization to find the new DeclContext.
1827 QualType T;
1828 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
1829
1830 if (ClassTemplate) {
1831 T = ClassTemplate->getInjectedClassNameType(Context);
1832 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
1833 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1834 T = Context.getTypeDeclType(Record);
1835 ClassTemplate = PartialSpec->getSpecializedTemplate();
1836 }
1837
1838 if (!T.isNull()) {
1839 // Substitute into the injected-class-name to get the type corresponding
1840 // to the instantiation we want. This substitution should never fail,
1841 // since we know we can instantiate the injected-class-name or we wouldn't
1842 // have gotten to the injected-class-name!
1843 // FIXME: Can we use the CurrentInstantiationScope to avoid this extra
1844 // instantiation in the common case?
1845 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
1846 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
1847
1848 if (!T->isDependentType()) {
1849 assert(T->isRecordType() && "Instantiation must produce a record type");
1850 return T->getAs<RecordType>()->getDecl();
1851 }
1852
1853 // We are performing "partial" template instantiation to create the
1854 // member declarations for the members of a class template
1855 // specialization. Therefore, D is actually referring to something in
1856 // the current instantiation. Look through the current context,
1857 // which contains actual instantiations, to find the instantiation of
1858 // the "current instantiation" that D refers to.
Mike Stump1eb44332009-09-09 15:08:12 +00001859 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00001860 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001861 if (ClassTemplateSpecializationDecl *Spec
John McCall52a575a2009-08-29 08:11:13 +00001862 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00001863 if (isInstantiationOf(ClassTemplate,
1864 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00001865 return Spec;
1866 }
1867
Mike Stump1eb44332009-09-09 15:08:12 +00001868 assert(false &&
John McCall52a575a2009-08-29 08:11:13 +00001869 "Unable to find declaration for the current instantiation");
Douglas Gregore95b4092009-09-16 18:34:49 +00001870 return Record;
John McCall52a575a2009-08-29 08:11:13 +00001871 }
Douglas Gregore95b4092009-09-16 18:34:49 +00001872
1873 // Fall through to deal with other dependent record types (e.g.,
1874 // anonymous unions in class templates).
1875 }
John McCall52a575a2009-08-29 08:11:13 +00001876
Douglas Gregore95b4092009-09-16 18:34:49 +00001877 if (!ParentDC->isDependentContext())
1878 return D;
1879
1880 ParentDC = FindInstantiatedContext(ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001881 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00001882 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001883
Douglas Gregor815215d2009-05-27 05:35:12 +00001884 if (ParentDC != D->getDeclContext()) {
1885 // We performed some kind of instantiation in the parent context,
1886 // so now we need to look into the instantiated parent context to
1887 // find the instantiation of the declaration D.
1888 NamedDecl *Result = 0;
1889 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001890 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00001891 Result = findInstantiationOf(Context, D, Found.first, Found.second);
1892 } else {
1893 // Since we don't have a name for the entity we're looking for,
1894 // our only option is to walk through all of the declarations to
1895 // find that name. This will occur in a few cases:
1896 //
1897 // - anonymous struct/union within a template
1898 // - unnamed class/struct/union/enum within a template
1899 //
1900 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00001901 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001902 ParentDC->decls_begin(),
1903 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00001904 }
Mike Stump1eb44332009-09-09 15:08:12 +00001905
Douglas Gregor815215d2009-05-27 05:35:12 +00001906 assert(Result && "Unable to find instantiation of declaration!");
1907 D = Result;
1908 }
1909
Douglas Gregor815215d2009-05-27 05:35:12 +00001910 return D;
1911}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001912
Mike Stump1eb44332009-09-09 15:08:12 +00001913/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001914/// instantiations we have seen until this point.
1915void Sema::PerformPendingImplicitInstantiations() {
1916 while (!PendingImplicitInstantiations.empty()) {
1917 PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front();
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001918 PendingImplicitInstantiations.pop_front();
Mike Stump1eb44332009-09-09 15:08:12 +00001919
Douglas Gregor7caa6822009-07-24 20:34:43 +00001920 // Instantiate function definitions
1921 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001922 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001923 Function->getLocation(), *this,
1924 Context.getSourceManager(),
1925 "instantiating function definition");
Mike Stump1eb44332009-09-09 15:08:12 +00001926
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001927 if (!Function->getBody())
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001928 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001929 continue;
1930 }
Mike Stump1eb44332009-09-09 15:08:12 +00001931
Douglas Gregor7caa6822009-07-24 20:34:43 +00001932 // Instantiate static data member definitions.
1933 VarDecl *Var = cast<VarDecl>(Inst.first);
1934 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001935
Mike Stump1eb44332009-09-09 15:08:12 +00001936 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001937 Var->getLocation(), *this,
1938 Context.getSourceManager(),
1939 "instantiating static data member "
1940 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00001941
Douglas Gregor7caa6822009-07-24 20:34:43 +00001942 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001943 }
1944}