blob: 641ea244278d70fd3a91d109f1f90364f7b6d1ee [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 Gregor33642df2009-10-23 23:25:44 +0000926 // FIXME: Do we actually want to perform substitution here? I don't think
927 // we do.
John McCalle29ba202009-08-20 01:44:21 +0000928 if (D->hasDefaultArgument()) {
John McCall833ca992009-10-29 08:12:44 +0000929 DeclaratorInfo *DefaultPattern = D->getDefaultArgumentInfo();
930 DeclaratorInfo *DefaultInst
John McCallce3ff2b2009-08-25 22:02:44 +0000931 = SemaRef.SubstType(DefaultPattern, TemplateArgs,
932 D->getDefaultArgumentLoc(),
933 D->getDeclName());
Mike Stump1eb44332009-09-09 15:08:12 +0000934
John McCalle29ba202009-08-20 01:44:21 +0000935 Inst->setDefaultArgument(DefaultInst,
John McCalle29ba202009-08-20 01:44:21 +0000936 D->defaultArgumentWasInherited() /* preserve? */);
937 }
938
Douglas Gregor550d9b22009-10-31 17:21:17 +0000939 // Introduce this template parameter's instantiation into the instantiation
940 // scope.
941 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
942
John McCalle29ba202009-08-20 01:44:21 +0000943 return Inst;
944}
945
Douglas Gregor33642df2009-10-23 23:25:44 +0000946Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
947 NonTypeTemplateParmDecl *D) {
948 // Substitute into the type of the non-type template parameter.
949 QualType T;
950 DeclaratorInfo *DI = D->getDeclaratorInfo();
951 if (DI) {
952 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
953 D->getDeclName());
954 if (DI) T = DI->getType();
955 } else {
956 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
957 D->getDeclName());
958 DI = 0;
959 }
960 if (T.isNull())
961 return 0;
962
963 // Check that this type is acceptable for a non-type template parameter.
964 bool Invalid = false;
965 T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
966 if (T.isNull()) {
967 T = SemaRef.Context.IntTy;
968 Invalid = true;
969 }
970
971 NonTypeTemplateParmDecl *Param
972 = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
973 D->getDepth() - 1, D->getPosition(),
974 D->getIdentifier(), T, DI);
975 if (Invalid)
976 Param->setInvalidDecl();
977
978 Param->setDefaultArgument(D->getDefaultArgument());
Douglas Gregor550d9b22009-10-31 17:21:17 +0000979
980 // Introduce this template parameter's instantiation into the instantiation
981 // scope.
982 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +0000983 return Param;
984}
985
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000986Decl *
987TemplateDeclInstantiator::VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000988 NestedNameSpecifier *NNS =
989 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
990 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000991 TemplateArgs);
992 if (!NNS)
993 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000994
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000995 CXXScopeSpec SS;
996 SS.setRange(D->getTargetNestedNameRange());
997 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +0000998
999 NamedDecl *UD =
1000 SemaRef.BuildUsingDeclaration(D->getLocation(), SS,
1001 D->getTargetNameLocation(),
Anders Carlsson0d8df782009-08-29 19:37:28 +00001002 D->getTargetName(), 0, D->isTypeName());
1003 if (UD)
Mike Stump1eb44332009-09-09 15:08:12 +00001004 SemaRef.Context.setInstantiatedFromUnresolvedUsingDecl(cast<UsingDecl>(UD),
Anders Carlsson0d8df782009-08-29 19:37:28 +00001005 D);
1006 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001007}
1008
John McCallce3ff2b2009-08-25 22:02:44 +00001009Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001010 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001011 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001012 return Instantiator.Visit(D);
1013}
1014
John McCalle29ba202009-08-20 01:44:21 +00001015/// \brief Instantiates a nested template parameter list in the current
1016/// instantiation context.
1017///
1018/// \param L The parameter list to instantiate
1019///
1020/// \returns NULL if there was an error
1021TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001022TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001023 // Get errors for all the parameters before bailing out.
1024 bool Invalid = false;
1025
1026 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001027 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001028 ParamVector Params;
1029 Params.reserve(N);
1030 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1031 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001032 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001033 Params.push_back(D);
1034 Invalid = Invalid || !D;
1035 }
1036
1037 // Clean up if we had an error.
1038 if (Invalid) {
1039 for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
1040 PI != PE; ++PI)
1041 if (*PI)
1042 (*PI)->Destroy(SemaRef.Context);
1043 return NULL;
1044 }
1045
1046 TemplateParameterList *InstL
1047 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1048 L->getLAngleLoc(), &Params.front(), N,
1049 L->getRAngleLoc());
1050 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001051}
John McCalle29ba202009-08-20 01:44:21 +00001052
Douglas Gregored9c0f92009-10-29 00:04:11 +00001053/// \brief Instantiate the declaration of a class template partial
1054/// specialization.
1055///
1056/// \param ClassTemplate the (instantiated) class template that is partially
1057// specialized by the instantiation of \p PartialSpec.
1058///
1059/// \param PartialSpec the (uninstantiated) class template partial
1060/// specialization that we are instantiating.
1061///
1062/// \returns true if there was an error, false otherwise.
1063bool
1064TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1065 ClassTemplateDecl *ClassTemplate,
1066 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001067 // Create a local instantiation scope for this class template partial
1068 // specialization, which will contain the instantiations of the template
1069 // parameters.
1070 Sema::LocalInstantiationScope Scope(SemaRef);
1071
Douglas Gregored9c0f92009-10-29 00:04:11 +00001072 // Substitute into the template parameters of the class template partial
1073 // specialization.
1074 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1075 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1076 if (!InstParams)
1077 return true;
1078
1079 // Substitute into the template arguments of the class template partial
1080 // specialization.
John McCall833ca992009-10-29 08:12:44 +00001081 const TemplateArgumentLoc *PartialSpecTemplateArgs
1082 = PartialSpec->getTemplateArgsAsWritten();
1083 unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1084
1085 llvm::SmallVector<TemplateArgumentLoc, 4> InstTemplateArgs(N);
1086 for (unsigned I = 0; I != N; ++I) {
1087 if (SemaRef.Subst(PartialSpecTemplateArgs[I], InstTemplateArgs[I],
1088 TemplateArgs))
Douglas Gregored9c0f92009-10-29 00:04:11 +00001089 return true;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001090 }
1091
1092
1093 // Check that the template argument list is well-formed for this
1094 // class template.
1095 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1096 InstTemplateArgs.size());
1097 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1098 PartialSpec->getLocation(),
1099 /*FIXME:*/PartialSpec->getLocation(),
1100 InstTemplateArgs.data(),
1101 InstTemplateArgs.size(),
1102 /*FIXME:*/PartialSpec->getLocation(),
1103 false,
1104 Converted))
1105 return true;
1106
1107 // Figure out where to insert this class template partial specialization
1108 // in the member template's set of class template partial specializations.
1109 llvm::FoldingSetNodeID ID;
1110 ClassTemplatePartialSpecializationDecl::Profile(ID,
1111 Converted.getFlatArguments(),
1112 Converted.flatSize(),
1113 SemaRef.Context);
1114 void *InsertPos = 0;
1115 ClassTemplateSpecializationDecl *PrevDecl
1116 = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
1117 InsertPos);
1118
1119 // Build the canonical type that describes the converted template
1120 // arguments of the class template partial specialization.
1121 QualType CanonType
1122 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1123 Converted.getFlatArguments(),
1124 Converted.flatSize());
1125
1126 // Build the fully-sugared type for this class template
1127 // specialization as the user wrote in the specialization
1128 // itself. This means that we'll pretty-print the type retrieved
1129 // from the specialization's declaration the way that the user
1130 // actually wrote the specialization, rather than formatting the
1131 // name based on the "canonical" representation used to store the
1132 // template arguments in the specialization.
1133 QualType WrittenTy
1134 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1135 InstTemplateArgs.data(),
1136 InstTemplateArgs.size(),
1137 CanonType);
1138
1139 if (PrevDecl) {
1140 // We've already seen a partial specialization with the same template
1141 // parameters and template arguments. This can happen, for example, when
1142 // substituting the outer template arguments ends up causing two
1143 // class template partial specializations of a member class template
1144 // to have identical forms, e.g.,
1145 //
1146 // template<typename T, typename U>
1147 // struct Outer {
1148 // template<typename X, typename Y> struct Inner;
1149 // template<typename Y> struct Inner<T, Y>;
1150 // template<typename Y> struct Inner<U, Y>;
1151 // };
1152 //
1153 // Outer<int, int> outer; // error: the partial specializations of Inner
1154 // // have the same signature.
1155 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1156 << WrittenTy;
1157 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1158 << SemaRef.Context.getTypeDeclType(PrevDecl);
1159 return true;
1160 }
1161
1162
1163 // Create the class template partial specialization declaration.
1164 ClassTemplatePartialSpecializationDecl *InstPartialSpec
1165 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context, Owner,
1166 PartialSpec->getLocation(),
1167 InstParams,
1168 ClassTemplate,
1169 Converted,
John McCall833ca992009-10-29 08:12:44 +00001170 InstTemplateArgs.data(),
1171 InstTemplateArgs.size(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00001172 0);
1173 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
1174 InstPartialSpec->setTypeAsWritten(WrittenTy);
1175
1176 // Add this partial specialization to the set of class template partial
1177 // specializations.
1178 ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec,
1179 InsertPos);
1180 return false;
1181}
1182
John McCallce3ff2b2009-08-25 22:02:44 +00001183/// \brief Does substitution on the type of the given function, including
1184/// all of the function parameters.
Douglas Gregor5545e162009-03-24 00:38:23 +00001185///
John McCallce3ff2b2009-08-25 22:02:44 +00001186/// \param D The function whose type will be the basis of the substitution
Douglas Gregor5545e162009-03-24 00:38:23 +00001187///
1188/// \param Params the instantiated parameter declarations
1189
John McCallce3ff2b2009-08-25 22:02:44 +00001190/// \returns the instantiated function's type if successful, a NULL
Douglas Gregor5545e162009-03-24 00:38:23 +00001191/// type if there was an error.
Mike Stump1eb44332009-09-09 15:08:12 +00001192QualType
John McCallce3ff2b2009-08-25 22:02:44 +00001193TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001194 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
1195 bool InvalidDecl = false;
1196
John McCallce3ff2b2009-08-25 22:02:44 +00001197 // Substitute all of the function's formal parameter types.
Douglas Gregor7e063902009-05-11 23:53:27 +00001198 TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001199 llvm::SmallVector<QualType, 4> ParamTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001200 for (FunctionDecl::param_iterator P = D->param_begin(),
Douglas Gregor5545e162009-03-24 00:38:23 +00001201 PEnd = D->param_end();
1202 P != PEnd; ++P) {
Douglas Gregor6477b692009-03-25 15:04:13 +00001203 if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
Douglas Gregor5545e162009-03-24 00:38:23 +00001204 if (PInst->getType()->isVoidType()) {
1205 SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
1206 PInst->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001207 } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001208 PInst->getType(),
1209 diag::err_abstract_type_in_decl,
1210 Sema::AbstractParamType))
Douglas Gregor5545e162009-03-24 00:38:23 +00001211 PInst->setInvalidDecl();
1212
1213 Params.push_back(PInst);
1214 ParamTys.push_back(PInst->getType());
1215
1216 if (PInst->isInvalidDecl())
1217 InvalidDecl = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001218 } else
Douglas Gregor5545e162009-03-24 00:38:23 +00001219 InvalidDecl = true;
1220 }
1221
1222 // FIXME: Deallocate dead declarations.
1223 if (InvalidDecl)
1224 return QualType();
1225
John McCall183700f2009-09-21 23:43:11 +00001226 const FunctionProtoType *Proto = D->getType()->getAs<FunctionProtoType>();
Douglas Gregor5545e162009-03-24 00:38:23 +00001227 assert(Proto && "Missing prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +00001228 QualType ResultType
John McCallce3ff2b2009-08-25 22:02:44 +00001229 = SemaRef.SubstType(Proto->getResultType(), TemplateArgs,
1230 D->getLocation(), D->getDeclName());
Douglas Gregor5545e162009-03-24 00:38:23 +00001231 if (ResultType.isNull())
1232 return QualType();
1233
Jay Foadbeaaccd2009-05-21 09:52:38 +00001234 return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
Douglas Gregor5545e162009-03-24 00:38:23 +00001235 Proto->isVariadic(), Proto->getTypeQuals(),
1236 D->getLocation(), D->getDeclName());
1237}
1238
Mike Stump1eb44332009-09-09 15:08:12 +00001239/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00001240/// declaration (New) from the corresponding fields of its template (Tmpl).
1241///
1242/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001243bool
1244TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00001245 FunctionDecl *Tmpl) {
1246 if (Tmpl->isDeleted())
1247 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00001248
Douglas Gregorcca9e962009-07-01 22:01:06 +00001249 // If we are performing substituting explicitly-specified template arguments
1250 // or deduced template arguments into a function template and we reach this
1251 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00001252 // to keeping the new function template specialization. We therefore
1253 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00001254 // into a template instantiation for this specific function template
1255 // specialization, which is not a SFINAE context, so that we diagnose any
1256 // further errors in the declaration itself.
1257 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1258 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1259 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1260 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00001261 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00001262 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001263 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00001264 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00001265 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001266 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1267 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
1268 }
1269 }
Mike Stump1eb44332009-09-09 15:08:12 +00001270
Douglas Gregore53060f2009-06-25 22:08:12 +00001271 return false;
1272}
1273
Douglas Gregor5545e162009-03-24 00:38:23 +00001274/// \brief Initializes common fields of an instantiated method
1275/// declaration (New) from the corresponding fields of its template
1276/// (Tmpl).
1277///
1278/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001279bool
1280TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00001281 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001282 if (InitFunctionInstantiation(New, Tmpl))
1283 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001284
Douglas Gregor5545e162009-03-24 00:38:23 +00001285 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
1286 New->setAccess(Tmpl->getAccess());
Anders Carlsson77b7f1d2009-05-14 22:15:41 +00001287 if (Tmpl->isVirtualAsWritten()) {
1288 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +00001289 Record->setAggregate(false);
1290 Record->setPOD(false);
Eli Friedman1d954f62009-08-15 21:55:26 +00001291 Record->setEmpty(false);
Douglas Gregor5545e162009-03-24 00:38:23 +00001292 Record->setPolymorphic(true);
1293 }
Douglas Gregor5545e162009-03-24 00:38:23 +00001294 if (Tmpl->isPure()) {
1295 New->setPure();
1296 Record->setAbstract(true);
1297 }
1298
1299 // FIXME: attributes
1300 // FIXME: New needs a pointer to Tmpl
1301 return false;
1302}
Douglas Gregora58861f2009-05-13 20:28:22 +00001303
1304/// \brief Instantiate the definition of the given function from its
1305/// template.
1306///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001307/// \param PointOfInstantiation the point at which the instantiation was
1308/// required. Note that this is not precisely a "point of instantiation"
1309/// for the function, but it's close.
1310///
Douglas Gregora58861f2009-05-13 20:28:22 +00001311/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001312/// function template specialization or member function of a class template
1313/// specialization.
1314///
1315/// \param Recursive if true, recursively instantiates any functions that
1316/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001317///
1318/// \param DefinitionRequired if true, then we are performing an explicit
1319/// instantiation where the body of the function is required. Complain if
1320/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001321void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001322 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001323 bool Recursive,
1324 bool DefinitionRequired) {
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001325 if (Function->isInvalidDecl())
1326 return;
1327
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001328 assert(!Function->getBody() && "Already instantiated!");
Mike Stump1eb44332009-09-09 15:08:12 +00001329
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001330 // Never instantiate an explicit specialization.
1331 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1332 return;
1333
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001334 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00001335 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001336 Stmt *Pattern = 0;
1337 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001338 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001339
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001340 if (!Pattern) {
1341 if (DefinitionRequired) {
1342 if (Function->getPrimaryTemplate())
1343 Diag(PointOfInstantiation,
1344 diag::err_explicit_instantiation_undefined_func_template)
1345 << Function->getPrimaryTemplate();
1346 else
1347 Diag(PointOfInstantiation,
1348 diag::err_explicit_instantiation_undefined_member)
1349 << 1 << Function->getDeclName() << Function->getDeclContext();
1350
1351 if (PatternDecl)
1352 Diag(PatternDecl->getLocation(),
1353 diag::note_explicit_instantiation_here);
1354 }
1355
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001356 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001357 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001358
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001359 // C++0x [temp.explicit]p9:
1360 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00001361 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001362 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00001363 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001364 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001365 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001366 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001367
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001368 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
1369 if (Inst)
1370 return;
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001371
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001372 // If we're performing recursive template instantiation, create our own
1373 // queue of pending implicit instantiations that we will instantiate later,
1374 // while we're still within our own instantiation context.
1375 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1376 if (Recursive)
1377 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001378
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001379 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
1380
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001381 // Introduce a new scope where local variable instantiations will be
1382 // recorded.
1383 LocalInstantiationScope Scope(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001384
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001385 // Introduce the instantiated function parameters into the local
1386 // instantiation scope.
1387 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
1388 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
1389 Function->getParamDecl(I));
1390
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001391 // Enter the scope of this instantiation. We don't use
1392 // PushDeclContext because we don't have a scope.
1393 DeclContext *PreviousContext = CurContext;
1394 CurContext = Function;
1395
Mike Stump1eb44332009-09-09 15:08:12 +00001396 MultiLevelTemplateArgumentList TemplateArgs =
Anders Carlsson09025312009-08-29 05:16:22 +00001397 getTemplateInstantiationArgs(Function);
1398
1399 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001400 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00001401 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
1402 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
1403 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001404 }
1405
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001406 // Instantiate the function body.
Anders Carlsson09025312009-08-29 05:16:22 +00001407 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001408
Douglas Gregor52604ab2009-09-11 21:19:12 +00001409 if (Body.isInvalid())
1410 Function->setInvalidDecl();
1411
Mike Stump1eb44332009-09-09 15:08:12 +00001412 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001413 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001414
1415 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00001416
1417 DeclGroupRef DG(Function);
1418 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001419
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001420 if (Recursive) {
1421 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001422 // instantiation of this template.
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001423 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001424
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001425 // Restore the set of pending implicit instantiations.
1426 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1427 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001428}
1429
1430/// \brief Instantiate the definition of the given variable from its
1431/// template.
1432///
Douglas Gregor7caa6822009-07-24 20:34:43 +00001433/// \param PointOfInstantiation the point at which the instantiation was
1434/// required. Note that this is not precisely a "point of instantiation"
1435/// for the function, but it's close.
1436///
1437/// \param Var the already-instantiated declaration of a static member
1438/// variable of a class template specialization.
1439///
1440/// \param Recursive if true, recursively instantiates any functions that
1441/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001442///
1443/// \param DefinitionRequired if true, then we are performing an explicit
1444/// instantiation where an out-of-line definition of the member variable
1445/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001446void Sema::InstantiateStaticDataMemberDefinition(
1447 SourceLocation PointOfInstantiation,
1448 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001449 bool Recursive,
1450 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001451 if (Var->isInvalidDecl())
1452 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001453
Douglas Gregor7caa6822009-07-24 20:34:43 +00001454 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001455 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00001456 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00001457 assert(Def->isStaticDataMember() && "Not a static data member?");
1458 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00001459
Douglas Gregor0d035142009-10-27 18:42:08 +00001460 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001461 // We did not find an out-of-line definition of this static data member,
1462 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00001463 // instantiate this definition (or provide a specialization for it) in
1464 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001465 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001466 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001467 Diag(PointOfInstantiation,
1468 diag::err_explicit_instantiation_undefined_member)
1469 << 2 << Var->getDeclName() << Var->getDeclContext();
1470 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
1471 }
1472
Douglas Gregor7caa6822009-07-24 20:34:43 +00001473 return;
1474 }
1475
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001476 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001477 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001478 return;
1479
1480 // C++0x [temp.explicit]p9:
1481 // Except for inline functions, other explicit instantiation declarations
1482 // have the effect of suppressing the implicit instantiation of the entity
1483 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001484 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001485 == TSK_ExplicitInstantiationDeclaration)
1486 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001487
Douglas Gregor7caa6822009-07-24 20:34:43 +00001488 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
1489 if (Inst)
1490 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001491
Douglas Gregor7caa6822009-07-24 20:34:43 +00001492 // If we're performing recursive template instantiation, create our own
1493 // queue of pending implicit instantiations that we will instantiate later,
1494 // while we're still within our own instantiation context.
1495 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1496 if (Recursive)
1497 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001498
Douglas Gregor7caa6822009-07-24 20:34:43 +00001499 // Enter the scope of this instantiation. We don't use
1500 // PushDeclContext because we don't have a scope.
1501 DeclContext *PreviousContext = CurContext;
1502 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001503
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001504 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00001505 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00001506 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00001507 CurContext = PreviousContext;
1508
1509 if (Var) {
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001510 Var->setPreviousDeclaration(OldVar);
Douglas Gregor583f33b2009-10-15 18:07:02 +00001511 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
1512 assert(MSInfo && "Missing member specialization information?");
1513 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
1514 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00001515 DeclGroupRef DG(Var);
1516 Consumer.HandleTopLevelDecl(DG);
1517 }
Mike Stump1eb44332009-09-09 15:08:12 +00001518
Douglas Gregor7caa6822009-07-24 20:34:43 +00001519 if (Recursive) {
1520 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001521 // instantiation of this template.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001522 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001523
Douglas Gregor7caa6822009-07-24 20:34:43 +00001524 // Restore the set of pending implicit instantiations.
1525 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001526 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001527}
Douglas Gregor815215d2009-05-27 05:35:12 +00001528
Anders Carlsson09025312009-08-29 05:16:22 +00001529void
1530Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
1531 const CXXConstructorDecl *Tmpl,
1532 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001533
Anders Carlsson09025312009-08-29 05:16:22 +00001534 llvm::SmallVector<MemInitTy*, 4> NewInits;
1535
1536 // Instantiate all the initializers.
1537 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00001538 InitsEnd = Tmpl->init_end();
1539 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00001540 CXXBaseOrMemberInitializer *Init = *Inits;
1541
1542 ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001543
Anders Carlsson09025312009-08-29 05:16:22 +00001544 // Instantiate all the arguments.
1545 for (ExprIterator Args = Init->arg_begin(), ArgsEnd = Init->arg_end();
1546 Args != ArgsEnd; ++Args) {
1547 OwningExprResult NewArg = SubstExpr(*Args, TemplateArgs);
1548
1549 if (NewArg.isInvalid())
1550 New->setInvalidDecl();
1551 else
1552 NewArgs.push_back(NewArg.takeAs<Expr>());
1553 }
1554
1555 MemInitResult NewInit;
1556
1557 if (Init->isBaseInitializer()) {
Eli Friedmanc5573a82009-08-29 22:22:07 +00001558 QualType BaseType(Init->getBaseClass(), 0);
1559 BaseType = SubstType(BaseType, TemplateArgs, Init->getSourceLocation(),
1560 New->getDeclName());
Anders Carlsson09025312009-08-29 05:16:22 +00001561
1562 NewInit = BuildBaseInitializer(BaseType,
Mike Stump1eb44332009-09-09 15:08:12 +00001563 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001564 NewArgs.size(),
1565 Init->getSourceLocation(),
1566 Init->getRParenLoc(),
1567 New->getParent());
1568 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001569 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00001570
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001571 // Is this an anonymous union?
1572 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregore95b4092009-09-16 18:34:49 +00001573 Member = cast<FieldDecl>(FindInstantiatedDecl(UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001574 else
Douglas Gregore95b4092009-09-16 18:34:49 +00001575 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMember(),
1576 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00001577
1578 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001579 NewArgs.size(),
1580 Init->getSourceLocation(),
1581 Init->getRParenLoc());
1582 }
1583
1584 if (NewInit.isInvalid())
1585 New->setInvalidDecl();
1586 else {
1587 // FIXME: It would be nice if ASTOwningVector had a release function.
1588 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001589
Anders Carlsson09025312009-08-29 05:16:22 +00001590 NewInits.push_back((MemInitTy *)NewInit.get());
1591 }
1592 }
Mike Stump1eb44332009-09-09 15:08:12 +00001593
Anders Carlsson09025312009-08-29 05:16:22 +00001594 // Assign all the initializers to the new constructor.
Mike Stump1eb44332009-09-09 15:08:12 +00001595 ActOnMemInitializers(DeclPtrTy::make(New),
Anders Carlsson09025312009-08-29 05:16:22 +00001596 /*FIXME: ColonLoc */
1597 SourceLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001598 NewInits.data(), NewInits.size());
Anders Carlsson09025312009-08-29 05:16:22 +00001599}
1600
John McCall52a575a2009-08-29 08:11:13 +00001601// TODO: this could be templated if the various decl types used the
1602// same method name.
1603static bool isInstantiationOf(ClassTemplateDecl *Pattern,
1604 ClassTemplateDecl *Instance) {
1605 Pattern = Pattern->getCanonicalDecl();
1606
1607 do {
1608 Instance = Instance->getCanonicalDecl();
1609 if (Pattern == Instance) return true;
1610 Instance = Instance->getInstantiatedFromMemberTemplate();
1611 } while (Instance);
1612
1613 return false;
1614}
1615
Douglas Gregor0d696532009-09-28 06:34:35 +00001616static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
1617 FunctionTemplateDecl *Instance) {
1618 Pattern = Pattern->getCanonicalDecl();
1619
1620 do {
1621 Instance = Instance->getCanonicalDecl();
1622 if (Pattern == Instance) return true;
1623 Instance = Instance->getInstantiatedFromMemberTemplate();
1624 } while (Instance);
1625
1626 return false;
1627}
1628
Douglas Gregored9c0f92009-10-29 00:04:11 +00001629static bool
1630isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
1631 ClassTemplatePartialSpecializationDecl *Instance) {
1632 Pattern
1633 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
1634 do {
1635 Instance = cast<ClassTemplatePartialSpecializationDecl>(
1636 Instance->getCanonicalDecl());
1637 if (Pattern == Instance)
1638 return true;
1639 Instance = Instance->getInstantiatedFromMember();
1640 } while (Instance);
1641
1642 return false;
1643}
1644
John McCall52a575a2009-08-29 08:11:13 +00001645static bool isInstantiationOf(CXXRecordDecl *Pattern,
1646 CXXRecordDecl *Instance) {
1647 Pattern = Pattern->getCanonicalDecl();
1648
1649 do {
1650 Instance = Instance->getCanonicalDecl();
1651 if (Pattern == Instance) return true;
1652 Instance = Instance->getInstantiatedFromMemberClass();
1653 } while (Instance);
1654
1655 return false;
1656}
1657
1658static bool isInstantiationOf(FunctionDecl *Pattern,
1659 FunctionDecl *Instance) {
1660 Pattern = Pattern->getCanonicalDecl();
1661
1662 do {
1663 Instance = Instance->getCanonicalDecl();
1664 if (Pattern == Instance) return true;
1665 Instance = Instance->getInstantiatedFromMemberFunction();
1666 } while (Instance);
1667
1668 return false;
1669}
1670
1671static bool isInstantiationOf(EnumDecl *Pattern,
1672 EnumDecl *Instance) {
1673 Pattern = Pattern->getCanonicalDecl();
1674
1675 do {
1676 Instance = Instance->getCanonicalDecl();
1677 if (Pattern == Instance) return true;
1678 Instance = Instance->getInstantiatedFromMemberEnum();
1679 } while (Instance);
1680
1681 return false;
1682}
1683
Anders Carlsson0d8df782009-08-29 19:37:28 +00001684static bool isInstantiationOf(UnresolvedUsingDecl *Pattern,
1685 UsingDecl *Instance,
1686 ASTContext &C) {
1687 return C.getInstantiatedFromUnresolvedUsingDecl(Instance) == Pattern;
1688}
1689
John McCall52a575a2009-08-29 08:11:13 +00001690static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
1691 VarDecl *Instance) {
1692 assert(Instance->isStaticDataMember());
1693
1694 Pattern = Pattern->getCanonicalDecl();
1695
1696 do {
1697 Instance = Instance->getCanonicalDecl();
1698 if (Pattern == Instance) return true;
1699 Instance = Instance->getInstantiatedFromStaticDataMember();
1700 } while (Instance);
1701
1702 return false;
1703}
1704
Douglas Gregor815215d2009-05-27 05:35:12 +00001705static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00001706 if (D->getKind() != Other->getKind()) {
1707 if (UnresolvedUsingDecl *UUD = dyn_cast<UnresolvedUsingDecl>(D)) {
1708 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
1709 return isInstantiationOf(UUD, UD, Ctx);
1710 }
1711 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001712
Anders Carlsson0d8df782009-08-29 19:37:28 +00001713 return false;
1714 }
Mike Stump1eb44332009-09-09 15:08:12 +00001715
John McCall52a575a2009-08-29 08:11:13 +00001716 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
1717 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001718
John McCall52a575a2009-08-29 08:11:13 +00001719 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
1720 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00001721
John McCall52a575a2009-08-29 08:11:13 +00001722 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
1723 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00001724
Douglas Gregor7caa6822009-07-24 20:34:43 +00001725 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00001726 if (Var->isStaticDataMember())
1727 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
1728
1729 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
1730 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00001731
Douglas Gregor0d696532009-09-28 06:34:35 +00001732 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
1733 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
1734
Douglas Gregored9c0f92009-10-29 00:04:11 +00001735 if (ClassTemplatePartialSpecializationDecl *PartialSpec
1736 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
1737 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
1738 PartialSpec);
1739
Anders Carlssond8b285f2009-09-01 04:26:58 +00001740 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
1741 if (!Field->getDeclName()) {
1742 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00001743 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00001744 cast<FieldDecl>(D);
1745 }
1746 }
Mike Stump1eb44332009-09-09 15:08:12 +00001747
Douglas Gregor815215d2009-05-27 05:35:12 +00001748 return D->getDeclName() && isa<NamedDecl>(Other) &&
1749 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
1750}
1751
1752template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00001753static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00001754 NamedDecl *D,
1755 ForwardIterator first,
1756 ForwardIterator last) {
1757 for (; first != last; ++first)
1758 if (isInstantiationOf(Ctx, D, *first))
1759 return cast<NamedDecl>(*first);
1760
1761 return 0;
1762}
1763
John McCall02cace72009-08-28 07:59:38 +00001764/// \brief Finds the instantiation of the given declaration context
1765/// within the current instantiation.
1766///
1767/// \returns NULL if there was an error
Douglas Gregore95b4092009-09-16 18:34:49 +00001768DeclContext *Sema::FindInstantiatedContext(DeclContext* DC,
1769 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00001770 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00001771 Decl* ID = FindInstantiatedDecl(D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00001772 return cast_or_null<DeclContext>(ID);
1773 } else return DC;
1774}
1775
Douglas Gregored961e72009-05-27 17:54:46 +00001776/// \brief Find the instantiation of the given declaration within the
1777/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00001778///
1779/// This routine is intended to be used when \p D is a declaration
1780/// referenced from within a template, that needs to mapped into the
1781/// corresponding declaration within an instantiation. For example,
1782/// given:
1783///
1784/// \code
1785/// template<typename T>
1786/// struct X {
1787/// enum Kind {
1788/// KnownValue = sizeof(T)
1789/// };
1790///
1791/// bool getKind() const { return KnownValue; }
1792/// };
1793///
1794/// template struct X<int>;
1795/// \endcode
1796///
1797/// In the instantiation of X<int>::getKind(), we need to map the
1798/// EnumConstantDecl for KnownValue (which refers to
1799/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00001800/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
1801/// this mapping from within the instantiation of X<int>.
Douglas Gregore95b4092009-09-16 18:34:49 +00001802NamedDecl *Sema::FindInstantiatedDecl(NamedDecl *D,
1803 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor44c73842009-09-01 17:53:10 +00001804 if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D)) {
1805 // Transform all of the elements of the overloaded function set.
Mike Stump1eb44332009-09-09 15:08:12 +00001806 OverloadedFunctionDecl *Result
Douglas Gregor44c73842009-09-01 17:53:10 +00001807 = OverloadedFunctionDecl::Create(Context, CurContext, Ovl->getDeclName());
Mike Stump1eb44332009-09-09 15:08:12 +00001808
Douglas Gregor44c73842009-09-01 17:53:10 +00001809 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
1810 FEnd = Ovl->function_end();
1811 F != FEnd; ++F) {
1812 Result->addOverload(
Douglas Gregore95b4092009-09-16 18:34:49 +00001813 AnyFunctionDecl::getFromNamedDecl(FindInstantiatedDecl(*F,
1814 TemplateArgs)));
Douglas Gregor44c73842009-09-01 17:53:10 +00001815 }
Mike Stump1eb44332009-09-09 15:08:12 +00001816
Douglas Gregor44c73842009-09-01 17:53:10 +00001817 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001818 }
1819
Douglas Gregor815215d2009-05-27 05:35:12 +00001820 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00001821 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
1822 isa<TemplateTypeParmDecl>(D) || isa<TemplateTypeParmDecl>(D) ||
1823 ParentDC->isFunctionOrMethod()) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00001824 // D is a local of some kind. Look into the map of local
1825 // declarations to their instantiations.
1826 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
1827 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001828
Douglas Gregore95b4092009-09-16 18:34:49 +00001829 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
1830 if (!Record->isDependentContext())
1831 return D;
1832
1833 // If the RecordDecl is actually the injected-class-name or a "templated"
1834 // declaration for a class template or class template partial
1835 // specialization, substitute into the injected-class-name of the
1836 // class template or partial specialization to find the new DeclContext.
1837 QualType T;
1838 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
1839
1840 if (ClassTemplate) {
1841 T = ClassTemplate->getInjectedClassNameType(Context);
1842 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
1843 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1844 T = Context.getTypeDeclType(Record);
1845 ClassTemplate = PartialSpec->getSpecializedTemplate();
1846 }
1847
1848 if (!T.isNull()) {
1849 // Substitute into the injected-class-name to get the type corresponding
1850 // to the instantiation we want. This substitution should never fail,
1851 // since we know we can instantiate the injected-class-name or we wouldn't
1852 // have gotten to the injected-class-name!
1853 // FIXME: Can we use the CurrentInstantiationScope to avoid this extra
1854 // instantiation in the common case?
1855 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
1856 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
1857
1858 if (!T->isDependentType()) {
1859 assert(T->isRecordType() && "Instantiation must produce a record type");
1860 return T->getAs<RecordType>()->getDecl();
1861 }
1862
1863 // We are performing "partial" template instantiation to create the
1864 // member declarations for the members of a class template
1865 // specialization. Therefore, D is actually referring to something in
1866 // the current instantiation. Look through the current context,
1867 // which contains actual instantiations, to find the instantiation of
1868 // the "current instantiation" that D refers to.
Mike Stump1eb44332009-09-09 15:08:12 +00001869 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00001870 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001871 if (ClassTemplateSpecializationDecl *Spec
John McCall52a575a2009-08-29 08:11:13 +00001872 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00001873 if (isInstantiationOf(ClassTemplate,
1874 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00001875 return Spec;
1876 }
1877
Mike Stump1eb44332009-09-09 15:08:12 +00001878 assert(false &&
John McCall52a575a2009-08-29 08:11:13 +00001879 "Unable to find declaration for the current instantiation");
Douglas Gregore95b4092009-09-16 18:34:49 +00001880 return Record;
John McCall52a575a2009-08-29 08:11:13 +00001881 }
Douglas Gregore95b4092009-09-16 18:34:49 +00001882
1883 // Fall through to deal with other dependent record types (e.g.,
1884 // anonymous unions in class templates).
1885 }
John McCall52a575a2009-08-29 08:11:13 +00001886
Douglas Gregore95b4092009-09-16 18:34:49 +00001887 if (!ParentDC->isDependentContext())
1888 return D;
1889
1890 ParentDC = FindInstantiatedContext(ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001891 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00001892 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001893
Douglas Gregor815215d2009-05-27 05:35:12 +00001894 if (ParentDC != D->getDeclContext()) {
1895 // We performed some kind of instantiation in the parent context,
1896 // so now we need to look into the instantiated parent context to
1897 // find the instantiation of the declaration D.
1898 NamedDecl *Result = 0;
1899 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001900 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00001901 Result = findInstantiationOf(Context, D, Found.first, Found.second);
1902 } else {
1903 // Since we don't have a name for the entity we're looking for,
1904 // our only option is to walk through all of the declarations to
1905 // find that name. This will occur in a few cases:
1906 //
1907 // - anonymous struct/union within a template
1908 // - unnamed class/struct/union/enum within a template
1909 //
1910 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00001911 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001912 ParentDC->decls_begin(),
1913 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00001914 }
Mike Stump1eb44332009-09-09 15:08:12 +00001915
Douglas Gregor815215d2009-05-27 05:35:12 +00001916 assert(Result && "Unable to find instantiation of declaration!");
1917 D = Result;
1918 }
1919
Douglas Gregor815215d2009-05-27 05:35:12 +00001920 return D;
1921}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001922
Mike Stump1eb44332009-09-09 15:08:12 +00001923/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001924/// instantiations we have seen until this point.
1925void Sema::PerformPendingImplicitInstantiations() {
1926 while (!PendingImplicitInstantiations.empty()) {
1927 PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front();
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001928 PendingImplicitInstantiations.pop_front();
Mike Stump1eb44332009-09-09 15:08:12 +00001929
Douglas Gregor7caa6822009-07-24 20:34:43 +00001930 // Instantiate function definitions
1931 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001932 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001933 Function->getLocation(), *this,
1934 Context.getSourceManager(),
1935 "instantiating function definition");
Mike Stump1eb44332009-09-09 15:08:12 +00001936
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001937 if (!Function->getBody())
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001938 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001939 continue;
1940 }
Mike Stump1eb44332009-09-09 15:08:12 +00001941
Douglas Gregor7caa6822009-07-24 20:34:43 +00001942 // Instantiate static data member definitions.
1943 VarDecl *Var = cast<VarDecl>(Inst.first);
1944 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001945
Mike Stump1eb44332009-09-09 15:08:12 +00001946 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001947 Var->getLocation(), *this,
1948 Context.getSourceManager(),
1949 "instantiating static data member "
1950 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00001951
Douglas Gregor7caa6822009-07-24 20:34:43 +00001952 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001953 }
1954}