blob: 0e42bd65db22eeb4b976b2572ab457b46803149e [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);
Douglas Gregor9106ef72009-11-11 16:58:32 +000067 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
Anders Carlsson0dde18e2009-08-28 15:18:15 +000068 Decl *VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D);
Mike Stump1eb44332009-09-09 15:08:12 +000069
Douglas Gregor8dbc2692009-03-17 21:15:40 +000070 // Base case. FIXME: Remove once we can instantiate everything.
Mike Stump1eb44332009-09-09 15:08:12 +000071 Decl *VisitDecl(Decl *) {
Douglas Gregor3d7a12a2009-03-25 23:32:15 +000072 assert(false && "Template instantiation of unknown declaration kind!");
Douglas Gregor8dbc2692009-03-17 21:15:40 +000073 return 0;
74 }
Douglas Gregor5545e162009-03-24 00:38:23 +000075
John McCallfd810b12009-08-14 02:03:10 +000076 const LangOptions &getLangOptions() {
77 return SemaRef.getLangOptions();
78 }
79
Douglas Gregor5545e162009-03-24 00:38:23 +000080 // Helper functions for instantiating methods.
John McCallce3ff2b2009-08-25 22:02:44 +000081 QualType SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +000082 llvm::SmallVectorImpl<ParmVarDecl *> &Params);
Douglas Gregore53060f2009-06-25 22:08:12 +000083 bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
Douglas Gregor5545e162009-03-24 00:38:23 +000084 bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
John McCalle29ba202009-08-20 01:44:21 +000085
86 TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +000087 SubstTemplateParams(TemplateParameterList *List);
Douglas Gregored9c0f92009-10-29 00:04:11 +000088
89 bool InstantiateClassTemplatePartialSpecialization(
90 ClassTemplateDecl *ClassTemplate,
91 ClassTemplatePartialSpecializationDecl *PartialSpec);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000092 };
93}
94
Anders Carlssond8fe2d52009-11-07 06:07:58 +000095// FIXME: Is this too simple?
96void TemplateDeclInstantiator::InstantiateAttrs(Decl *Tmpl, Decl *New) {
97 for (const Attr *TmplAttr = Tmpl->getAttrs(); TmplAttr;
98 TmplAttr = TmplAttr->getNext()) {
99
100 // FIXME: Is cloning correct for all attributes?
101 Attr *NewAttr = TmplAttr->clone(SemaRef.Context);
102
103 New->addAttr(NewAttr);
104 }
105}
106
Douglas Gregor4f722be2009-03-25 15:45:12 +0000107Decl *
108TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
109 assert(false && "Translation units cannot be instantiated");
110 return D;
111}
112
113Decl *
114TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
115 assert(false && "Namespaces cannot be instantiated");
116 return D;
117}
118
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000119Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
120 bool Invalid = false;
John McCallba6a9bd2009-10-24 08:00:42 +0000121 DeclaratorInfo *DI = D->getTypeDeclaratorInfo();
122 if (DI->getType()->isDependentType()) {
123 DI = SemaRef.SubstType(DI, TemplateArgs,
124 D->getLocation(), D->getDeclName());
125 if (!DI) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000126 Invalid = true;
John McCallba6a9bd2009-10-24 08:00:42 +0000127 DI = SemaRef.Context.getTrivialDeclaratorInfo(SemaRef.Context.IntTy);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000128 }
129 }
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000131 // Create the new typedef
132 TypedefDecl *Typedef
133 = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
John McCallba6a9bd2009-10-24 08:00:42 +0000134 D->getIdentifier(), DI);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000135 if (Invalid)
136 Typedef->setInvalidDecl();
137
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000138 Owner->addDecl(Typedef);
Mike Stump1eb44332009-09-09 15:08:12 +0000139
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000140 return Typedef;
141}
142
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000143Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000144 // Do substitution on the type of the declaration
John McCall0a5fa062009-10-21 02:39:02 +0000145 DeclaratorInfo *DI = SemaRef.SubstType(D->getDeclaratorInfo(),
146 TemplateArgs,
147 D->getTypeSpecStartLoc(),
148 D->getDeclName());
149 if (!DI)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000150 return 0;
151
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000152 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000153 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
154 D->getLocation(), D->getIdentifier(),
John McCall0a5fa062009-10-21 02:39:02 +0000155 DI->getType(), DI,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000156 D->getStorageClass());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000157 Var->setThreadSpecified(D->isThreadSpecified());
158 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
159 Var->setDeclaredInCondition(D->isDeclaredInCondition());
Mike Stump1eb44332009-09-09 15:08:12 +0000160
161 // If we are instantiating a static data member defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000162 // out-of-line, the instantiation will have the same lexical
163 // context (which will be a namespace scope) as the template.
164 if (D->isOutOfLine())
165 Var->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Mike Stump390b4cc2009-05-16 07:39:55 +0000167 // FIXME: In theory, we could have a previous declaration for variables that
168 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000169 bool Redeclaration = false;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000170 SemaRef.CheckVariableDeclaration(Var, 0, Redeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Douglas Gregor7caa6822009-07-24 20:34:43 +0000172 if (D->isOutOfLine()) {
173 D->getLexicalDeclContext()->addDecl(Var);
174 Owner->makeDeclVisibleInContext(Var);
175 } else {
176 Owner->addDecl(Var);
177 }
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000179 // Link instantiations of static data members back to the template from
180 // which they were instantiated.
181 if (Var->isStaticDataMember())
182 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000183 TSK_ImplicitInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000184
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000185 if (D->getInit()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000186 OwningExprResult Init
John McCallce3ff2b2009-08-25 22:02:44 +0000187 = SemaRef.SubstExpr(D->getInit(), TemplateArgs);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000188 if (Init.isInvalid())
189 Var->setInvalidDecl();
Sebastian Redl42dddbe2009-11-08 10:16:43 +0000190 else if (!D->getType()->isDependentType() &&
191 !D->getInit()->isTypeDependent() &&
Douglas Gregore48319a2009-11-09 17:16:50 +0000192 !D->getInit()->isValueDependent()) {
Sebastian Redl42dddbe2009-11-08 10:16:43 +0000193 // If neither the declaration's type nor its initializer are dependent,
194 // we don't want to redo all the checking, especially since the
195 // initializer might have been wrapped by a CXXConstructExpr since we did
196 // it the first time.
197 Var->setInit(SemaRef.Context, Init.takeAs<Expr>());
198 }
Douglas Gregor83ddad32009-08-26 21:14:46 +0000199 else if (ParenListExpr *PLE = dyn_cast<ParenListExpr>((Expr *)Init.get())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000200 // FIXME: We're faking all of the comma locations, which is suboptimal.
Douglas Gregor83ddad32009-08-26 21:14:46 +0000201 // Do we even need these comma locations?
202 llvm::SmallVector<SourceLocation, 4> FakeCommaLocs;
203 if (PLE->getNumExprs() > 0) {
204 FakeCommaLocs.reserve(PLE->getNumExprs() - 1);
205 for (unsigned I = 0, N = PLE->getNumExprs() - 1; I != N; ++I) {
206 Expr *E = PLE->getExpr(I)->Retain();
207 FakeCommaLocs.push_back(
208 SemaRef.PP.getLocForEndOfToken(E->getLocEnd()));
209 }
Douglas Gregore9f8eb62009-08-26 23:26:04 +0000210 PLE->getExpr(PLE->getNumExprs() - 1)->Retain();
Douglas Gregor83ddad32009-08-26 21:14:46 +0000211 }
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Douglas Gregor83ddad32009-08-26 21:14:46 +0000213 // Add the direct initializer to the declaration.
214 SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
Mike Stump1eb44332009-09-09 15:08:12 +0000215 PLE->getLParenLoc(),
Douglas Gregor83ddad32009-08-26 21:14:46 +0000216 Sema::MultiExprArg(SemaRef,
217 (void**)PLE->getExprs(),
218 PLE->getNumExprs()),
219 FakeCommaLocs.data(),
220 PLE->getRParenLoc());
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Douglas Gregor83ddad32009-08-26 21:14:46 +0000222 // When Init is destroyed, it will destroy the instantiated ParenListExpr;
223 // we've explicitly retained all of its subexpressions already.
224 } else
Chris Lattnerb28317a2009-03-28 19:18:32 +0000225 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init),
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000226 D->hasCXXDirectInitializer());
Douglas Gregor65b90052009-07-27 17:43:39 +0000227 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
228 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000229
230 return Var;
231}
232
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000233Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
234 bool Invalid = false;
John McCall07fb6be2009-10-22 23:33:21 +0000235 DeclaratorInfo *DI = D->getDeclaratorInfo();
236 if (DI->getType()->isDependentType()) {
237 DI = SemaRef.SubstType(DI, TemplateArgs,
238 D->getLocation(), D->getDeclName());
239 if (!DI) {
240 DI = D->getDeclaratorInfo();
241 Invalid = true;
242 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000243 // C++ [temp.arg.type]p3:
244 // If a declaration acquires a function type through a type
245 // dependent on a template-parameter and this causes a
246 // declaration that does not use the syntactic form of a
247 // function declarator to have function type, the program is
248 // ill-formed.
249 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000250 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000251 Invalid = true;
252 }
253 }
254
255 Expr *BitWidth = D->getBitWidth();
256 if (Invalid)
257 BitWidth = 0;
258 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000259 // The bit-width expression is not potentially evaluated.
260 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000261
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000262 OwningExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000263 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000264 if (InstantiatedBitWidth.isInvalid()) {
265 Invalid = true;
266 BitWidth = 0;
267 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000268 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000269 }
270
John McCall07fb6be2009-10-22 23:33:21 +0000271 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
272 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000273 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000274 D->getLocation(),
275 D->isMutable(),
276 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000277 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000278 D->getAccess(),
279 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000280 if (!Field) {
281 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000282 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000283 }
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000285 InstantiateAttrs(D, Field);
286
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000287 if (Invalid)
288 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000290 if (!Field->getDeclName()) {
291 // Keep track of where this decl came from.
292 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000293 }
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000295 Field->setImplicit(D->isImplicit());
296 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000297
298 return Field;
299}
300
John McCall02cace72009-08-28 07:59:38 +0000301Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
302 FriendDecl::FriendUnion FU;
303
304 // Handle friend type expressions by simply substituting template
305 // parameters into the pattern type.
306 if (Type *Ty = D->getFriendType()) {
307 QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs,
308 D->getLocation(), DeclarationName());
309 if (T.isNull()) return 0;
310
311 assert(getLangOptions().CPlusPlus0x || T->isRecordType());
312 FU = T.getTypePtr();
313
314 // Handle everything else by appropriate substitution.
315 } else {
316 NamedDecl *ND = D->getFriendDecl();
317 assert(ND && "friend decl must be a decl or a type!");
318
Douglas Gregora735b202009-10-13 14:39:41 +0000319 // FIXME: We have a problem here, because the nested call to Visit(ND)
320 // will inject the thing that the friend references into the current
321 // owner, which is wrong.
John McCall02cace72009-08-28 07:59:38 +0000322 Decl *NewND = Visit(ND);
323 if (!NewND) return 0;
324
325 FU = cast<NamedDecl>(NewND);
John McCallfd810b12009-08-14 02:03:10 +0000326 }
Mike Stump1eb44332009-09-09 15:08:12 +0000327
John McCall02cace72009-08-28 07:59:38 +0000328 FriendDecl *FD =
329 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU,
330 D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000331 FD->setAccess(AS_public);
John McCall02cace72009-08-28 07:59:38 +0000332 Owner->addDecl(FD);
333 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000334}
335
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000336Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
337 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Douglas Gregorac7610d2009-06-22 20:57:11 +0000339 // The expression in a static assertion is not potentially evaluated.
340 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000342 OwningExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000343 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000344 if (InstantiatedAssertExpr.isInvalid())
345 return 0;
346
Douglas Gregor43d9d922009-08-08 01:41:12 +0000347 OwningExprResult Message(SemaRef, D->getMessage());
348 D->getMessage()->Retain();
Mike Stump1eb44332009-09-09 15:08:12 +0000349 Decl *StaticAssert
350 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
Chris Lattnerb28317a2009-03-28 19:18:32 +0000351 move(InstantiatedAssertExpr),
352 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000353 return StaticAssert;
354}
355
356Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000357 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000358 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000359 D->getTagKeywordLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000360 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000361 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000362 Enum->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000363 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000364 Enum->startDefinition();
365
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000366 llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000367
368 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000369 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
370 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000371 EC != ECEnd; ++EC) {
372 // The specified value for the enumerator.
373 OwningExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000374 if (Expr *UninstValue = EC->getInitExpr()) {
375 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000376 EnterExpressionEvaluationContext Unevaluated(SemaRef,
Douglas Gregorac7610d2009-06-22 20:57:11 +0000377 Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000378
John McCallce3ff2b2009-08-25 22:02:44 +0000379 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000380 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000381
382 // Drop the initial value and continue.
383 bool isInvalid = false;
384 if (Value.isInvalid()) {
385 Value = SemaRef.Owned((Expr *)0);
386 isInvalid = true;
387 }
388
Mike Stump1eb44332009-09-09 15:08:12 +0000389 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000390 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
391 EC->getLocation(), EC->getIdentifier(),
392 move(Value));
393
394 if (isInvalid) {
395 if (EnumConst)
396 EnumConst->setInvalidDecl();
397 Enum->setInvalidDecl();
398 }
399
400 if (EnumConst) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000401 Enum->addDecl(EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000402 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000403 LastEnumConst = EnumConst;
404 }
405 }
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000407 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000408 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000409 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
410 Sema::DeclPtrTy::make(Enum),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000411 &Enumerators[0], Enumerators.size(),
412 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000413
414 return Enum;
415}
416
Douglas Gregor6477b692009-03-25 15:04:13 +0000417Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
418 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
419 return 0;
420}
421
Douglas Gregored9c0f92009-10-29 00:04:11 +0000422namespace {
423 class SortDeclByLocation {
424 SourceManager &SourceMgr;
425
426 public:
427 explicit SortDeclByLocation(SourceManager &SourceMgr)
428 : SourceMgr(SourceMgr) { }
429
430 bool operator()(const Decl *X, const Decl *Y) const {
431 return SourceMgr.isBeforeInTranslationUnit(X->getLocation(),
432 Y->getLocation());
433 }
434 };
435}
436
John McCalle29ba202009-08-20 01:44:21 +0000437Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000438 // Create a local instantiation scope for this class template, which
439 // will contain the instantiations of the template parameters.
440 Sema::LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000441 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000442 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000443 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000444 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000445
446 CXXRecordDecl *Pattern = D->getTemplatedDecl();
447 CXXRecordDecl *RecordInst
448 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner,
449 Pattern->getLocation(), Pattern->getIdentifier(),
Douglas Gregorf0510d42009-10-12 23:11:44 +0000450 Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL,
451 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000452
453 ClassTemplateDecl *Inst
454 = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
455 D->getIdentifier(), InstParams, RecordInst, 0);
456 RecordInst->setDescribedClassTemplate(Inst);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000457 if (D->getFriendObjectKind())
458 Inst->setObjectOfFriendDecl(true);
459 else
460 Inst->setAccess(D->getAccess());
John McCalle29ba202009-08-20 01:44:21 +0000461 Inst->setInstantiatedFromMemberTemplate(D);
Douglas Gregorf0510d42009-10-12 23:11:44 +0000462
463 // Trigger creation of the type for the instantiation.
464 SemaRef.Context.getTypeDeclType(RecordInst);
465
Douglas Gregor259571e2009-10-30 22:42:42 +0000466 // Finish handling of friends.
467 if (Inst->getFriendObjectKind()) {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000468 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000469 }
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000470
John McCalle29ba202009-08-20 01:44:21 +0000471 Owner->addDecl(Inst);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000472
473 // First, we sort the partial specializations by location, so
474 // that we instantiate them in the order they were declared.
475 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
476 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
477 P = D->getPartialSpecializations().begin(),
478 PEnd = D->getPartialSpecializations().end();
479 P != PEnd; ++P)
480 PartialSpecs.push_back(&*P);
481 std::sort(PartialSpecs.begin(), PartialSpecs.end(),
482 SortDeclByLocation(SemaRef.SourceMgr));
483
484 // Instantiate all of the partial specializations of this member class
485 // template.
486 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
487 InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
488
John McCalle29ba202009-08-20 01:44:21 +0000489 return Inst;
490}
491
Douglas Gregord60e1052009-08-27 16:57:43 +0000492Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000493TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
494 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000495 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
496
497 // Lookup the already-instantiated declaration in the instantiation
498 // of the class template and return that.
499 DeclContext::lookup_result Found
500 = Owner->lookup(ClassTemplate->getDeclName());
501 if (Found.first == Found.second)
502 return 0;
503
504 ClassTemplateDecl *InstClassTemplate
505 = dyn_cast<ClassTemplateDecl>(*Found.first);
506 if (!InstClassTemplate)
507 return 0;
508
509 Decl *DCanon = D->getCanonicalDecl();
510 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
511 P = InstClassTemplate->getPartialSpecializations().begin(),
512 PEnd = InstClassTemplate->getPartialSpecializations().end();
513 P != PEnd; ++P) {
514 if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
515 return &*P;
516 }
517
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000518 return 0;
519}
520
521Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000522TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000523 // Create a local instantiation scope for this function template, which
524 // will contain the instantiations of the template parameters and then get
525 // merged with the local instantiation scope for the function template
526 // itself.
527 Sema::LocalInstantiationScope Scope(SemaRef);
528
Douglas Gregord60e1052009-08-27 16:57:43 +0000529 TemplateParameterList *TempParams = D->getTemplateParameters();
530 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000531 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000532 return NULL;
Douglas Gregored9c0f92009-10-29 00:04:11 +0000533
Douglas Gregora735b202009-10-13 14:39:41 +0000534 FunctionDecl *Instantiated = 0;
535 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
536 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
537 InstParams));
538 else
539 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
540 D->getTemplatedDecl(),
541 InstParams));
542
543 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000544 return 0;
545
Mike Stump1eb44332009-09-09 15:08:12 +0000546 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000547 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000548 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000549 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000550 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000551 assert(InstTemplate &&
552 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
553 if (!InstTemplate->getInstantiatedFromMemberTemplate())
554 InstTemplate->setInstantiatedFromMemberTemplate(D);
555
556 // Add non-friends into the owner.
557 if (!InstTemplate->getFriendObjectKind())
558 Owner->addDecl(InstTemplate);
Douglas Gregord60e1052009-08-27 16:57:43 +0000559 return InstTemplate;
560}
561
Douglas Gregord475b8d2009-03-25 21:17:03 +0000562Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
563 CXXRecordDecl *PrevDecl = 0;
564 if (D->isInjectedClassName())
565 PrevDecl = cast<CXXRecordDecl>(Owner);
566
567 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000568 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000569 D->getLocation(), D->getIdentifier(),
570 D->getTagKeywordLoc(), PrevDecl);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000571 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000572 // FIXME: Check against AS_none is an ugly hack to work around the issue that
573 // the tag decls introduced by friend class declarations don't have an access
574 // specifier. Remove once this area of the code gets sorted out.
575 if (D->getAccess() != AS_none)
576 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000577 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000578 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000579
John McCall02cace72009-08-28 07:59:38 +0000580 // If the original function was part of a friend declaration,
581 // inherit its namespace state.
582 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
583 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
584
Anders Carlssond8b285f2009-09-01 04:26:58 +0000585 Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
586
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000587 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000588 return Record;
589}
590
John McCall02cace72009-08-28 07:59:38 +0000591/// Normal class members are of more specific types and therefore
592/// don't make it here. This function serves two purposes:
593/// 1) instantiating function templates
594/// 2) substituting friend declarations
595/// FIXME: preserve function definitions in case #2
Douglas Gregora735b202009-10-13 14:39:41 +0000596 Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
597 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000598 // Check whether there is already a function template specialization for
599 // this declaration.
600 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
601 void *InsertPos = 0;
Douglas Gregora735b202009-10-13 14:39:41 +0000602 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000603 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000604 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000605 TemplateArgs.getInnermost().getFlatArgumentList(),
606 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor828e2262009-07-29 16:09:57 +0000607 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000608
609 FunctionTemplateSpecializationInfo *Info
610 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor127102b2009-06-29 20:59:39 +0000611 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000612
Douglas Gregor127102b2009-06-29 20:59:39 +0000613 // If we already have a function template specialization, return it.
614 if (Info)
615 return Info->Function;
616 }
Mike Stump1eb44332009-09-09 15:08:12 +0000617
Douglas Gregor550d9b22009-10-31 17:21:17 +0000618 Sema::LocalInstantiationScope Scope(SemaRef, TemplateParams != 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000619
Douglas Gregore53060f2009-06-25 22:08:12 +0000620 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000621 QualType T = SubstFunctionType(D, Params);
Douglas Gregore53060f2009-06-25 22:08:12 +0000622 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000623 return 0;
John McCallfd810b12009-08-14 02:03:10 +0000624
Douglas Gregore53060f2009-06-25 22:08:12 +0000625 // Build the instantiated method declaration.
Douglas Gregore95b4092009-09-16 18:34:49 +0000626 DeclContext *DC = SemaRef.FindInstantiatedContext(D->getDeclContext(),
627 TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +0000628 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +0000629 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000630 D->getDeclName(), T, D->getDeclaratorInfo(),
631 D->getStorageClass(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000632 D->isInlineSpecified(), D->hasWrittenPrototype());
John McCall02cace72009-08-28 07:59:38 +0000633 Function->setLexicalDeclContext(Owner);
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Douglas Gregore53060f2009-06-25 22:08:12 +0000635 // Attach the parameters
636 for (unsigned P = 0; P < Params.size(); ++P)
637 Params[P]->setOwningFunction(Function);
638 Function->setParams(SemaRef.Context, Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +0000639
Douglas Gregora735b202009-10-13 14:39:41 +0000640 if (TemplateParams) {
641 // Our resulting instantiation is actually a function template, since we
642 // are substituting only the outer template parameters. For example, given
643 //
644 // template<typename T>
645 // struct X {
646 // template<typename U> friend void f(T, U);
647 // };
648 //
649 // X<int> x;
650 //
651 // We are instantiating the friend function template "f" within X<int>,
652 // which means substituting int for T, but leaving "f" as a friend function
653 // template.
654 // Build the function template itself.
655 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Owner,
656 Function->getLocation(),
657 Function->getDeclName(),
658 TemplateParams, Function);
659 Function->setDescribedFunctionTemplate(FunctionTemplate);
660 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregorcad84b72009-11-13 23:14:53 +0000661 } else if (FunctionTemplate) {
662 // Record this function template specialization.
663 Function->setFunctionTemplateSpecialization(SemaRef.Context,
664 FunctionTemplate,
665 &TemplateArgs.getInnermost(),
666 InsertPos);
John McCall02cace72009-08-28 07:59:38 +0000667 }
Douglas Gregora735b202009-10-13 14:39:41 +0000668
Douglas Gregore53060f2009-06-25 22:08:12 +0000669 if (InitFunctionInstantiation(Function, D))
670 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000671
Douglas Gregore53060f2009-06-25 22:08:12 +0000672 bool Redeclaration = false;
673 bool OverloadableAttrRequired = false;
Douglas Gregora735b202009-10-13 14:39:41 +0000674
Douglas Gregore53060f2009-06-25 22:08:12 +0000675 NamedDecl *PrevDecl = 0;
Douglas Gregora735b202009-10-13 14:39:41 +0000676 if (TemplateParams || !FunctionTemplate) {
677 // Look only into the namespace where the friend would be declared to
678 // find a previous declaration. This is the innermost enclosing namespace,
679 // as described in ActOnFriendFunctionDecl.
680 Sema::LookupResult R;
681 SemaRef.LookupQualifiedName(R, DC, Function->getDeclName(),
682 Sema::LookupOrdinaryName, true);
683
684 PrevDecl = R.getAsSingleDecl(SemaRef.Context);
685
686 // In C++, the previous declaration we find might be a tag type
687 // (class or enum). In this case, the new declaration will hide the
688 // tag type. Note that this does does not apply if we're declaring a
689 // typedef (C++ [dcl.typedef]p4).
690 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
691 PrevDecl = 0;
692 }
693
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000694 SemaRef.CheckFunctionDeclaration(Function, PrevDecl, false, Redeclaration,
Douglas Gregore53060f2009-06-25 22:08:12 +0000695 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000696
Douglas Gregora735b202009-10-13 14:39:41 +0000697 // If the original function was part of a friend declaration,
698 // inherit its namespace state and add it to the owner.
699 NamedDecl *FromFriendD
700 = TemplateParams? cast<NamedDecl>(D->getDescribedFunctionTemplate()) : D;
701 if (FromFriendD->getFriendObjectKind()) {
702 NamedDecl *ToFriendD = 0;
703 if (TemplateParams) {
704 ToFriendD = cast<NamedDecl>(FunctionTemplate);
705 PrevDecl = FunctionTemplate->getPreviousDeclaration();
706 } else {
707 ToFriendD = Function;
708 PrevDecl = Function->getPreviousDeclaration();
709 }
710 ToFriendD->setObjectOfFriendDecl(PrevDecl != NULL);
711 if (!Owner->isDependentContext() && !PrevDecl)
712 DC->makeDeclVisibleInContext(ToFriendD, /* Recoverable = */ false);
713
714 if (!TemplateParams)
715 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
716 }
717
Douglas Gregore53060f2009-06-25 22:08:12 +0000718 return Function;
719}
720
Douglas Gregord60e1052009-08-27 16:57:43 +0000721Decl *
722TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
723 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +0000724 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
725 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +0000726 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +0000727 // We are creating a function template specialization from a function
728 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +0000729 // specialization for this particular set of template arguments.
Douglas Gregor6b906862009-08-21 00:16:32 +0000730 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000731 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000732 TemplateArgs.getInnermost().getFlatArgumentList(),
733 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000734 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000735
736 FunctionTemplateSpecializationInfo *Info
737 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor6b906862009-08-21 00:16:32 +0000738 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Douglas Gregor6b906862009-08-21 00:16:32 +0000740 // If we already have a function template specialization, return it.
741 if (Info)
742 return Info->Function;
743 }
744
Douglas Gregor550d9b22009-10-31 17:21:17 +0000745 Sema::LocalInstantiationScope Scope(SemaRef, TemplateParams != 0);
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000746
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000747 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000748 QualType T = SubstFunctionType(D, Params);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000749 if (T.isNull())
750 return 0;
751
752 // Build the instantiated method declaration.
753 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
Douglas Gregordec06662009-08-21 18:42:58 +0000754 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000755
Douglas Gregordec06662009-08-21 18:42:58 +0000756 DeclarationName Name = D->getDeclName();
Douglas Gregor17e32f32009-08-21 22:43:28 +0000757 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregordec06662009-08-21 18:42:58 +0000758 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
759 Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
760 SemaRef.Context.getCanonicalType(ClassTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000761 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
762 Constructor->getLocation(),
763 Name, T,
Douglas Gregor17e32f32009-08-21 22:43:28 +0000764 Constructor->getDeclaratorInfo(),
Mike Stump1eb44332009-09-09 15:08:12 +0000765 Constructor->isExplicit(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000766 Constructor->isInlineSpecified(), false);
Douglas Gregor17e32f32009-08-21 22:43:28 +0000767 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
768 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
769 Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
770 SemaRef.Context.getCanonicalType(ClassTy));
771 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
772 Destructor->getLocation(), Name,
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000773 T, Destructor->isInlineSpecified(), false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000774 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000775 CanQualType ConvTy
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000776 = SemaRef.Context.getCanonicalType(
John McCall183700f2009-09-21 23:43:11 +0000777 T->getAs<FunctionType>()->getResultType());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000778 Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
779 ConvTy);
780 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
781 Conversion->getLocation(), Name,
782 T, Conversion->getDeclaratorInfo(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000783 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000784 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +0000785 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000786 Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
Douglas Gregordec06662009-08-21 18:42:58 +0000787 D->getDeclName(), T, D->getDeclaratorInfo(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000788 D->isStatic(), D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +0000789 }
Douglas Gregor6b906862009-08-21 00:16:32 +0000790
Douglas Gregord60e1052009-08-27 16:57:43 +0000791 if (TemplateParams) {
792 // Our resulting instantiation is actually a function template, since we
793 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +0000794 //
Douglas Gregord60e1052009-08-27 16:57:43 +0000795 // template<typename T>
796 // struct X {
797 // template<typename U> void f(T, U);
798 // };
799 //
800 // X<int> x;
801 //
802 // We are instantiating the member template "f" within X<int>, which means
803 // substituting int for T, but leaving "f" as a member function template.
804 // Build the function template itself.
805 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
806 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +0000807 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +0000808 TemplateParams, Method);
809 if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +0000810 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +0000811 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregorcad84b72009-11-13 23:14:53 +0000812 } else if (FunctionTemplate) {
813 // Record this function template specialization.
814 Method->setFunctionTemplateSpecialization(SemaRef.Context,
815 FunctionTemplate,
816 &TemplateArgs.getInnermost(),
817 InsertPos);
818 } else {
819 // Record this instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +0000820 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregorcad84b72009-11-13 23:14:53 +0000821 }
822
Mike Stump1eb44332009-09-09 15:08:12 +0000823 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000824 // out-of-line, the instantiation will have the same lexical
825 // context (which will be a namespace scope) as the template.
826 if (D->isOutOfLine())
827 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000828
Douglas Gregor5545e162009-03-24 00:38:23 +0000829 // Attach the parameters
830 for (unsigned P = 0; P < Params.size(); ++P)
831 Params[P]->setOwningFunction(Method);
Jay Foadbeaaccd2009-05-21 09:52:38 +0000832 Method->setParams(SemaRef.Context, Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +0000833
Douglas Gregorcad84b72009-11-13 23:14:53 +0000834 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Method)) {
835 // C++ [class.copy]p3:
836 // [...] A member function template is never instantiated to perform the
837 // copy of a class object to an object of its class type.
838 if (FunctionTemplate && !TemplateParams &&
839 Constructor->isCopyConstructor(SemaRef.Context)) {
840 SemaRef.Diag(Constructor->getLocation(),
841 diag::err_constructor_template_is_copy_constructor)
842 << Constructor;
843 Method->setInvalidDecl();
844 return Method;
845 }
846 }
847
Douglas Gregor5545e162009-03-24 00:38:23 +0000848 if (InitMethodInstantiation(Method, D))
849 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000850
Douglas Gregordec06662009-08-21 18:42:58 +0000851 NamedDecl *PrevDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Douglas Gregord60e1052009-08-27 16:57:43 +0000853 if (!FunctionTemplate || TemplateParams) {
John McCallf36e02d2009-10-09 21:13:30 +0000854 Sema::LookupResult R;
855 SemaRef.LookupQualifiedName(R, Owner, Name, Sema::LookupOrdinaryName, true);
856 PrevDecl = R.getAsSingleDecl(SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000857
Douglas Gregordec06662009-08-21 18:42:58 +0000858 // In C++, the previous declaration we find might be a tag type
859 // (class or enum). In this case, the new declaration will hide the
860 // tag type. Note that this does does not apply if we're declaring a
861 // typedef (C++ [dcl.typedef]p4).
862 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
863 PrevDecl = 0;
864 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000865
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000866 bool Redeclaration = false;
867 bool OverloadableAttrRequired = false;
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000868 SemaRef.CheckFunctionDeclaration(Method, PrevDecl, false, Redeclaration,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000869 /*FIXME:*/OverloadableAttrRequired);
870
Douglas Gregora735b202009-10-13 14:39:41 +0000871 if (!FunctionTemplate && (!Method->isInvalidDecl() || !PrevDecl) &&
872 !Method->getFriendObjectKind())
Douglas Gregordec06662009-08-21 18:42:58 +0000873 Owner->addDecl(Method);
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000875 return Method;
876}
877
Douglas Gregor615c5d42009-03-24 16:43:20 +0000878Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +0000879 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000880}
881
Douglas Gregor03b2b072009-03-24 00:15:49 +0000882Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +0000883 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +0000884}
885
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000886Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000887 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000888}
889
Douglas Gregor6477b692009-03-25 15:04:13 +0000890ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
John McCall58e46772009-10-23 21:48:59 +0000891 QualType T;
892 DeclaratorInfo *DI = D->getDeclaratorInfo();
893 if (DI) {
894 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
895 D->getDeclName());
896 if (DI) T = DI->getType();
897 } else {
898 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
899 D->getDeclName());
900 DI = 0;
901 }
902
903 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000904 return 0;
905
John McCall58e46772009-10-23 21:48:59 +0000906 T = SemaRef.adjustParameterType(T);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000907
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000908 // Allocate the parameter
John McCall58e46772009-10-23 21:48:59 +0000909 ParmVarDecl *Param
910 = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
911 D->getIdentifier(), T, DI, D->getStorageClass(), 0);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000912
Anders Carlsson9351c172009-08-25 03:18:48 +0000913 // Mark the default argument as being uninstantiated.
Douglas Gregorf43d0b32009-09-25 06:56:31 +0000914 if (D->hasUninstantiatedDefaultArg())
915 Param->setUninstantiatedDefaultArg(D->getUninstantiatedDefaultArg());
Douglas Gregor0ed09302009-09-25 07:03:22 +0000916 else if (Expr *Arg = D->getDefaultArg())
917 Param->setUninstantiatedDefaultArg(Arg);
918
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000919 // Note: we don't try to instantiate function parameters until after
920 // we've instantiated the function's type. Therefore, we don't have
921 // to check for 'void' parameter types here.
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000922 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000923 return Param;
924}
925
John McCalle29ba202009-08-20 01:44:21 +0000926Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
927 TemplateTypeParmDecl *D) {
928 // TODO: don't always clone when decls are refcounted.
929 const Type* T = D->getTypeForDecl();
930 assert(T->isTemplateTypeParmType());
931 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000932
John McCalle29ba202009-08-20 01:44:21 +0000933 TemplateTypeParmDecl *Inst =
934 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor550d9b22009-10-31 17:21:17 +0000935 TTPT->getDepth() - 1, TTPT->getIndex(),
John McCalle29ba202009-08-20 01:44:21 +0000936 TTPT->getName(),
937 D->wasDeclaredWithTypename(),
938 D->isParameterPack());
939
Douglas Gregor0f8716b2009-11-09 19:17:50 +0000940 if (D->hasDefaultArgument())
941 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
John McCalle29ba202009-08-20 01:44:21 +0000942
Douglas Gregor550d9b22009-10-31 17:21:17 +0000943 // Introduce this template parameter's instantiation into the instantiation
944 // scope.
945 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
946
John McCalle29ba202009-08-20 01:44:21 +0000947 return Inst;
948}
949
Douglas Gregor33642df2009-10-23 23:25:44 +0000950Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
951 NonTypeTemplateParmDecl *D) {
952 // Substitute into the type of the non-type template parameter.
953 QualType T;
954 DeclaratorInfo *DI = D->getDeclaratorInfo();
955 if (DI) {
956 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
957 D->getDeclName());
958 if (DI) T = DI->getType();
959 } else {
960 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
961 D->getDeclName());
962 DI = 0;
963 }
964 if (T.isNull())
965 return 0;
966
967 // Check that this type is acceptable for a non-type template parameter.
968 bool Invalid = false;
969 T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
970 if (T.isNull()) {
971 T = SemaRef.Context.IntTy;
972 Invalid = true;
973 }
974
975 NonTypeTemplateParmDecl *Param
976 = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
977 D->getDepth() - 1, D->getPosition(),
978 D->getIdentifier(), T, DI);
979 if (Invalid)
980 Param->setInvalidDecl();
981
982 Param->setDefaultArgument(D->getDefaultArgument());
Douglas Gregor550d9b22009-10-31 17:21:17 +0000983
984 // Introduce this template parameter's instantiation into the instantiation
985 // scope.
986 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +0000987 return Param;
988}
989
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000990Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +0000991TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
992 TemplateTemplateParmDecl *D) {
993 // Instantiate the template parameter list of the template template parameter.
994 TemplateParameterList *TempParams = D->getTemplateParameters();
995 TemplateParameterList *InstParams;
996 {
997 // Perform the actual substitution of template parameters within a new,
998 // local instantiation scope.
999 Sema::LocalInstantiationScope Scope(SemaRef);
1000 InstParams = SubstTemplateParams(TempParams);
1001 if (!InstParams)
1002 return NULL;
1003 }
1004
1005 // Build the template template parameter.
1006 TemplateTemplateParmDecl *Param
1007 = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1008 D->getDepth() - 1, D->getPosition(),
1009 D->getIdentifier(), InstParams);
1010 Param->setDefaultArgument(D->getDefaultArgument());
1011
1012 // Introduce this template parameter's instantiation into the instantiation
1013 // scope.
1014 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1015
1016 return Param;
1017}
1018
1019Decl *
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001020TemplateDeclInstantiator::VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +00001021 NestedNameSpecifier *NNS =
1022 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1023 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001024 TemplateArgs);
1025 if (!NNS)
1026 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001027
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001028 CXXScopeSpec SS;
1029 SS.setRange(D->getTargetNestedNameRange());
1030 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +00001031
1032 NamedDecl *UD =
1033 SemaRef.BuildUsingDeclaration(D->getLocation(), SS,
1034 D->getTargetNameLocation(),
Anders Carlsson0d8df782009-08-29 19:37:28 +00001035 D->getTargetName(), 0, D->isTypeName());
1036 if (UD)
Mike Stump1eb44332009-09-09 15:08:12 +00001037 SemaRef.Context.setInstantiatedFromUnresolvedUsingDecl(cast<UsingDecl>(UD),
Anders Carlsson0d8df782009-08-29 19:37:28 +00001038 D);
1039 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001040}
1041
John McCallce3ff2b2009-08-25 22:02:44 +00001042Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001043 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001044 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001045 return Instantiator.Visit(D);
1046}
1047
John McCalle29ba202009-08-20 01:44:21 +00001048/// \brief Instantiates a nested template parameter list in the current
1049/// instantiation context.
1050///
1051/// \param L The parameter list to instantiate
1052///
1053/// \returns NULL if there was an error
1054TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001055TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001056 // Get errors for all the parameters before bailing out.
1057 bool Invalid = false;
1058
1059 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001060 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001061 ParamVector Params;
1062 Params.reserve(N);
1063 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1064 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001065 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001066 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001067 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00001068 }
1069
1070 // Clean up if we had an error.
1071 if (Invalid) {
1072 for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
1073 PI != PE; ++PI)
1074 if (*PI)
1075 (*PI)->Destroy(SemaRef.Context);
1076 return NULL;
1077 }
1078
1079 TemplateParameterList *InstL
1080 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1081 L->getLAngleLoc(), &Params.front(), N,
1082 L->getRAngleLoc());
1083 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001084}
John McCalle29ba202009-08-20 01:44:21 +00001085
Douglas Gregored9c0f92009-10-29 00:04:11 +00001086/// \brief Instantiate the declaration of a class template partial
1087/// specialization.
1088///
1089/// \param ClassTemplate the (instantiated) class template that is partially
1090// specialized by the instantiation of \p PartialSpec.
1091///
1092/// \param PartialSpec the (uninstantiated) class template partial
1093/// specialization that we are instantiating.
1094///
1095/// \returns true if there was an error, false otherwise.
1096bool
1097TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1098 ClassTemplateDecl *ClassTemplate,
1099 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001100 // Create a local instantiation scope for this class template partial
1101 // specialization, which will contain the instantiations of the template
1102 // parameters.
1103 Sema::LocalInstantiationScope Scope(SemaRef);
1104
Douglas Gregored9c0f92009-10-29 00:04:11 +00001105 // Substitute into the template parameters of the class template partial
1106 // specialization.
1107 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1108 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1109 if (!InstParams)
1110 return true;
1111
1112 // Substitute into the template arguments of the class template partial
1113 // specialization.
John McCall833ca992009-10-29 08:12:44 +00001114 const TemplateArgumentLoc *PartialSpecTemplateArgs
1115 = PartialSpec->getTemplateArgsAsWritten();
1116 unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1117
1118 llvm::SmallVector<TemplateArgumentLoc, 4> InstTemplateArgs(N);
1119 for (unsigned I = 0; I != N; ++I) {
1120 if (SemaRef.Subst(PartialSpecTemplateArgs[I], InstTemplateArgs[I],
1121 TemplateArgs))
Douglas Gregored9c0f92009-10-29 00:04:11 +00001122 return true;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001123 }
1124
1125
1126 // Check that the template argument list is well-formed for this
1127 // class template.
1128 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1129 InstTemplateArgs.size());
1130 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1131 PartialSpec->getLocation(),
1132 /*FIXME:*/PartialSpec->getLocation(),
1133 InstTemplateArgs.data(),
1134 InstTemplateArgs.size(),
1135 /*FIXME:*/PartialSpec->getLocation(),
1136 false,
1137 Converted))
1138 return true;
1139
1140 // Figure out where to insert this class template partial specialization
1141 // in the member template's set of class template partial specializations.
1142 llvm::FoldingSetNodeID ID;
1143 ClassTemplatePartialSpecializationDecl::Profile(ID,
1144 Converted.getFlatArguments(),
1145 Converted.flatSize(),
1146 SemaRef.Context);
1147 void *InsertPos = 0;
1148 ClassTemplateSpecializationDecl *PrevDecl
1149 = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
1150 InsertPos);
1151
1152 // Build the canonical type that describes the converted template
1153 // arguments of the class template partial specialization.
1154 QualType CanonType
1155 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1156 Converted.getFlatArguments(),
1157 Converted.flatSize());
1158
1159 // Build the fully-sugared type for this class template
1160 // specialization as the user wrote in the specialization
1161 // itself. This means that we'll pretty-print the type retrieved
1162 // from the specialization's declaration the way that the user
1163 // actually wrote the specialization, rather than formatting the
1164 // name based on the "canonical" representation used to store the
1165 // template arguments in the specialization.
1166 QualType WrittenTy
1167 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1168 InstTemplateArgs.data(),
1169 InstTemplateArgs.size(),
1170 CanonType);
1171
1172 if (PrevDecl) {
1173 // We've already seen a partial specialization with the same template
1174 // parameters and template arguments. This can happen, for example, when
1175 // substituting the outer template arguments ends up causing two
1176 // class template partial specializations of a member class template
1177 // to have identical forms, e.g.,
1178 //
1179 // template<typename T, typename U>
1180 // struct Outer {
1181 // template<typename X, typename Y> struct Inner;
1182 // template<typename Y> struct Inner<T, Y>;
1183 // template<typename Y> struct Inner<U, Y>;
1184 // };
1185 //
1186 // Outer<int, int> outer; // error: the partial specializations of Inner
1187 // // have the same signature.
1188 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1189 << WrittenTy;
1190 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1191 << SemaRef.Context.getTypeDeclType(PrevDecl);
1192 return true;
1193 }
1194
1195
1196 // Create the class template partial specialization declaration.
1197 ClassTemplatePartialSpecializationDecl *InstPartialSpec
1198 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context, Owner,
1199 PartialSpec->getLocation(),
1200 InstParams,
1201 ClassTemplate,
1202 Converted,
John McCall833ca992009-10-29 08:12:44 +00001203 InstTemplateArgs.data(),
1204 InstTemplateArgs.size(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00001205 0);
1206 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
1207 InstPartialSpec->setTypeAsWritten(WrittenTy);
1208
1209 // Add this partial specialization to the set of class template partial
1210 // specializations.
1211 ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec,
1212 InsertPos);
1213 return false;
1214}
1215
John McCallce3ff2b2009-08-25 22:02:44 +00001216/// \brief Does substitution on the type of the given function, including
1217/// all of the function parameters.
Douglas Gregor5545e162009-03-24 00:38:23 +00001218///
John McCallce3ff2b2009-08-25 22:02:44 +00001219/// \param D The function whose type will be the basis of the substitution
Douglas Gregor5545e162009-03-24 00:38:23 +00001220///
1221/// \param Params the instantiated parameter declarations
1222
John McCallce3ff2b2009-08-25 22:02:44 +00001223/// \returns the instantiated function's type if successful, a NULL
Douglas Gregor5545e162009-03-24 00:38:23 +00001224/// type if there was an error.
Mike Stump1eb44332009-09-09 15:08:12 +00001225QualType
John McCallce3ff2b2009-08-25 22:02:44 +00001226TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001227 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
1228 bool InvalidDecl = false;
1229
John McCallce3ff2b2009-08-25 22:02:44 +00001230 // Substitute all of the function's formal parameter types.
Douglas Gregor7e063902009-05-11 23:53:27 +00001231 TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001232 llvm::SmallVector<QualType, 4> ParamTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001233 for (FunctionDecl::param_iterator P = D->param_begin(),
Douglas Gregor5545e162009-03-24 00:38:23 +00001234 PEnd = D->param_end();
1235 P != PEnd; ++P) {
Douglas Gregor6477b692009-03-25 15:04:13 +00001236 if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
Douglas Gregor5545e162009-03-24 00:38:23 +00001237 if (PInst->getType()->isVoidType()) {
1238 SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
1239 PInst->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001240 } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001241 PInst->getType(),
1242 diag::err_abstract_type_in_decl,
1243 Sema::AbstractParamType))
Douglas Gregor5545e162009-03-24 00:38:23 +00001244 PInst->setInvalidDecl();
1245
1246 Params.push_back(PInst);
1247 ParamTys.push_back(PInst->getType());
1248
1249 if (PInst->isInvalidDecl())
1250 InvalidDecl = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001251 } else
Douglas Gregor5545e162009-03-24 00:38:23 +00001252 InvalidDecl = true;
1253 }
1254
1255 // FIXME: Deallocate dead declarations.
1256 if (InvalidDecl)
1257 return QualType();
1258
John McCall183700f2009-09-21 23:43:11 +00001259 const FunctionProtoType *Proto = D->getType()->getAs<FunctionProtoType>();
Douglas Gregor5545e162009-03-24 00:38:23 +00001260 assert(Proto && "Missing prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +00001261 QualType ResultType
John McCallce3ff2b2009-08-25 22:02:44 +00001262 = SemaRef.SubstType(Proto->getResultType(), TemplateArgs,
1263 D->getLocation(), D->getDeclName());
Douglas Gregor5545e162009-03-24 00:38:23 +00001264 if (ResultType.isNull())
1265 return QualType();
1266
Jay Foadbeaaccd2009-05-21 09:52:38 +00001267 return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
Douglas Gregor5545e162009-03-24 00:38:23 +00001268 Proto->isVariadic(), Proto->getTypeQuals(),
1269 D->getLocation(), D->getDeclName());
1270}
1271
Mike Stump1eb44332009-09-09 15:08:12 +00001272/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00001273/// declaration (New) from the corresponding fields of its template (Tmpl).
1274///
1275/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001276bool
1277TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00001278 FunctionDecl *Tmpl) {
1279 if (Tmpl->isDeleted())
1280 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00001281
Douglas Gregorcca9e962009-07-01 22:01:06 +00001282 // If we are performing substituting explicitly-specified template arguments
1283 // or deduced template arguments into a function template and we reach this
1284 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00001285 // to keeping the new function template specialization. We therefore
1286 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00001287 // into a template instantiation for this specific function template
1288 // specialization, which is not a SFINAE context, so that we diagnose any
1289 // further errors in the declaration itself.
1290 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1291 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1292 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1293 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00001294 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00001295 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001296 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00001297 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00001298 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001299 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1300 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
Douglas Gregorf35f8282009-11-11 21:54:23 +00001301 --SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001302 }
1303 }
Mike Stump1eb44332009-09-09 15:08:12 +00001304
Douglas Gregore53060f2009-06-25 22:08:12 +00001305 return false;
1306}
1307
Douglas Gregor5545e162009-03-24 00:38:23 +00001308/// \brief Initializes common fields of an instantiated method
1309/// declaration (New) from the corresponding fields of its template
1310/// (Tmpl).
1311///
1312/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001313bool
1314TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00001315 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001316 if (InitFunctionInstantiation(New, Tmpl))
1317 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001318
Douglas Gregor5545e162009-03-24 00:38:23 +00001319 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
1320 New->setAccess(Tmpl->getAccess());
Anders Carlsson77b7f1d2009-05-14 22:15:41 +00001321 if (Tmpl->isVirtualAsWritten()) {
1322 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +00001323 Record->setAggregate(false);
1324 Record->setPOD(false);
Eli Friedman1d954f62009-08-15 21:55:26 +00001325 Record->setEmpty(false);
Douglas Gregor5545e162009-03-24 00:38:23 +00001326 Record->setPolymorphic(true);
1327 }
Douglas Gregor5545e162009-03-24 00:38:23 +00001328 if (Tmpl->isPure()) {
1329 New->setPure();
1330 Record->setAbstract(true);
1331 }
1332
1333 // FIXME: attributes
1334 // FIXME: New needs a pointer to Tmpl
1335 return false;
1336}
Douglas Gregora58861f2009-05-13 20:28:22 +00001337
1338/// \brief Instantiate the definition of the given function from its
1339/// template.
1340///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001341/// \param PointOfInstantiation the point at which the instantiation was
1342/// required. Note that this is not precisely a "point of instantiation"
1343/// for the function, but it's close.
1344///
Douglas Gregora58861f2009-05-13 20:28:22 +00001345/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001346/// function template specialization or member function of a class template
1347/// specialization.
1348///
1349/// \param Recursive if true, recursively instantiates any functions that
1350/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001351///
1352/// \param DefinitionRequired if true, then we are performing an explicit
1353/// instantiation where the body of the function is required. Complain if
1354/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001355void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001356 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001357 bool Recursive,
1358 bool DefinitionRequired) {
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001359 if (Function->isInvalidDecl())
1360 return;
1361
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001362 assert(!Function->getBody() && "Already instantiated!");
Mike Stump1eb44332009-09-09 15:08:12 +00001363
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001364 // Never instantiate an explicit specialization.
1365 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1366 return;
1367
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001368 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00001369 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001370 Stmt *Pattern = 0;
1371 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001372 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001373
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001374 if (!Pattern) {
1375 if (DefinitionRequired) {
1376 if (Function->getPrimaryTemplate())
1377 Diag(PointOfInstantiation,
1378 diag::err_explicit_instantiation_undefined_func_template)
1379 << Function->getPrimaryTemplate();
1380 else
1381 Diag(PointOfInstantiation,
1382 diag::err_explicit_instantiation_undefined_member)
1383 << 1 << Function->getDeclName() << Function->getDeclContext();
1384
1385 if (PatternDecl)
1386 Diag(PatternDecl->getLocation(),
1387 diag::note_explicit_instantiation_here);
1388 }
1389
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001390 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001391 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001392
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001393 // C++0x [temp.explicit]p9:
1394 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00001395 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001396 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00001397 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001398 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001399 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001400 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001401
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001402 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
1403 if (Inst)
1404 return;
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001405
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001406 // If we're performing recursive template instantiation, create our own
1407 // queue of pending implicit instantiations that we will instantiate later,
1408 // while we're still within our own instantiation context.
1409 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1410 if (Recursive)
1411 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001412
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001413 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
1414
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001415 // Introduce a new scope where local variable instantiations will be
1416 // recorded.
1417 LocalInstantiationScope Scope(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001418
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001419 // Introduce the instantiated function parameters into the local
1420 // instantiation scope.
1421 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
1422 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
1423 Function->getParamDecl(I));
1424
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001425 // Enter the scope of this instantiation. We don't use
1426 // PushDeclContext because we don't have a scope.
1427 DeclContext *PreviousContext = CurContext;
1428 CurContext = Function;
1429
Mike Stump1eb44332009-09-09 15:08:12 +00001430 MultiLevelTemplateArgumentList TemplateArgs =
Anders Carlsson09025312009-08-29 05:16:22 +00001431 getTemplateInstantiationArgs(Function);
1432
1433 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001434 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00001435 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
1436 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
1437 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001438 }
1439
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001440 // Instantiate the function body.
Anders Carlsson09025312009-08-29 05:16:22 +00001441 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001442
Douglas Gregor52604ab2009-09-11 21:19:12 +00001443 if (Body.isInvalid())
1444 Function->setInvalidDecl();
1445
Mike Stump1eb44332009-09-09 15:08:12 +00001446 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001447 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001448
1449 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00001450
1451 DeclGroupRef DG(Function);
1452 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001453
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001454 if (Recursive) {
1455 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001456 // instantiation of this template.
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001457 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001458
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001459 // Restore the set of pending implicit instantiations.
1460 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1461 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001462}
1463
1464/// \brief Instantiate the definition of the given variable from its
1465/// template.
1466///
Douglas Gregor7caa6822009-07-24 20:34:43 +00001467/// \param PointOfInstantiation the point at which the instantiation was
1468/// required. Note that this is not precisely a "point of instantiation"
1469/// for the function, but it's close.
1470///
1471/// \param Var the already-instantiated declaration of a static member
1472/// variable of a class template specialization.
1473///
1474/// \param Recursive if true, recursively instantiates any functions that
1475/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001476///
1477/// \param DefinitionRequired if true, then we are performing an explicit
1478/// instantiation where an out-of-line definition of the member variable
1479/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001480void Sema::InstantiateStaticDataMemberDefinition(
1481 SourceLocation PointOfInstantiation,
1482 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001483 bool Recursive,
1484 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001485 if (Var->isInvalidDecl())
1486 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001487
Douglas Gregor7caa6822009-07-24 20:34:43 +00001488 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001489 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00001490 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00001491 assert(Def->isStaticDataMember() && "Not a static data member?");
1492 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00001493
Douglas Gregor0d035142009-10-27 18:42:08 +00001494 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001495 // We did not find an out-of-line definition of this static data member,
1496 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00001497 // instantiate this definition (or provide a specialization for it) in
1498 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001499 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001500 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001501 Diag(PointOfInstantiation,
1502 diag::err_explicit_instantiation_undefined_member)
1503 << 2 << Var->getDeclName() << Var->getDeclContext();
1504 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
1505 }
1506
Douglas Gregor7caa6822009-07-24 20:34:43 +00001507 return;
1508 }
1509
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001510 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001511 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001512 return;
1513
1514 // C++0x [temp.explicit]p9:
1515 // Except for inline functions, other explicit instantiation declarations
1516 // have the effect of suppressing the implicit instantiation of the entity
1517 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001518 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001519 == TSK_ExplicitInstantiationDeclaration)
1520 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001521
Douglas Gregor7caa6822009-07-24 20:34:43 +00001522 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
1523 if (Inst)
1524 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001525
Douglas Gregor7caa6822009-07-24 20:34:43 +00001526 // If we're performing recursive template instantiation, create our own
1527 // queue of pending implicit instantiations that we will instantiate later,
1528 // while we're still within our own instantiation context.
1529 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1530 if (Recursive)
1531 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001532
Douglas Gregor7caa6822009-07-24 20:34:43 +00001533 // Enter the scope of this instantiation. We don't use
1534 // PushDeclContext because we don't have a scope.
1535 DeclContext *PreviousContext = CurContext;
1536 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001537
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001538 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00001539 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00001540 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00001541 CurContext = PreviousContext;
1542
1543 if (Var) {
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001544 Var->setPreviousDeclaration(OldVar);
Douglas Gregor583f33b2009-10-15 18:07:02 +00001545 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
1546 assert(MSInfo && "Missing member specialization information?");
1547 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
1548 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00001549 DeclGroupRef DG(Var);
1550 Consumer.HandleTopLevelDecl(DG);
1551 }
Mike Stump1eb44332009-09-09 15:08:12 +00001552
Douglas Gregor7caa6822009-07-24 20:34:43 +00001553 if (Recursive) {
1554 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001555 // instantiation of this template.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001556 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001557
Douglas Gregor7caa6822009-07-24 20:34:43 +00001558 // Restore the set of pending implicit instantiations.
1559 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001560 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001561}
Douglas Gregor815215d2009-05-27 05:35:12 +00001562
Anders Carlsson09025312009-08-29 05:16:22 +00001563void
1564Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
1565 const CXXConstructorDecl *Tmpl,
1566 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001567
Anders Carlsson09025312009-08-29 05:16:22 +00001568 llvm::SmallVector<MemInitTy*, 4> NewInits;
1569
1570 // Instantiate all the initializers.
1571 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00001572 InitsEnd = Tmpl->init_end();
1573 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00001574 CXXBaseOrMemberInitializer *Init = *Inits;
1575
1576 ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001577
Anders Carlsson09025312009-08-29 05:16:22 +00001578 // Instantiate all the arguments.
1579 for (ExprIterator Args = Init->arg_begin(), ArgsEnd = Init->arg_end();
1580 Args != ArgsEnd; ++Args) {
1581 OwningExprResult NewArg = SubstExpr(*Args, TemplateArgs);
1582
1583 if (NewArg.isInvalid())
1584 New->setInvalidDecl();
1585 else
1586 NewArgs.push_back(NewArg.takeAs<Expr>());
1587 }
1588
1589 MemInitResult NewInit;
1590
1591 if (Init->isBaseInitializer()) {
Eli Friedmanc5573a82009-08-29 22:22:07 +00001592 QualType BaseType(Init->getBaseClass(), 0);
1593 BaseType = SubstType(BaseType, TemplateArgs, Init->getSourceLocation(),
1594 New->getDeclName());
Anders Carlsson09025312009-08-29 05:16:22 +00001595
1596 NewInit = BuildBaseInitializer(BaseType,
Mike Stump1eb44332009-09-09 15:08:12 +00001597 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001598 NewArgs.size(),
1599 Init->getSourceLocation(),
1600 Init->getRParenLoc(),
1601 New->getParent());
1602 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001603 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00001604
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001605 // Is this an anonymous union?
1606 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregore95b4092009-09-16 18:34:49 +00001607 Member = cast<FieldDecl>(FindInstantiatedDecl(UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001608 else
Douglas Gregore95b4092009-09-16 18:34:49 +00001609 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMember(),
1610 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00001611
1612 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001613 NewArgs.size(),
1614 Init->getSourceLocation(),
1615 Init->getRParenLoc());
1616 }
1617
1618 if (NewInit.isInvalid())
1619 New->setInvalidDecl();
1620 else {
1621 // FIXME: It would be nice if ASTOwningVector had a release function.
1622 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001623
Anders Carlsson09025312009-08-29 05:16:22 +00001624 NewInits.push_back((MemInitTy *)NewInit.get());
1625 }
1626 }
Mike Stump1eb44332009-09-09 15:08:12 +00001627
Anders Carlsson09025312009-08-29 05:16:22 +00001628 // Assign all the initializers to the new constructor.
Mike Stump1eb44332009-09-09 15:08:12 +00001629 ActOnMemInitializers(DeclPtrTy::make(New),
Anders Carlsson09025312009-08-29 05:16:22 +00001630 /*FIXME: ColonLoc */
1631 SourceLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001632 NewInits.data(), NewInits.size());
Anders Carlsson09025312009-08-29 05:16:22 +00001633}
1634
John McCall52a575a2009-08-29 08:11:13 +00001635// TODO: this could be templated if the various decl types used the
1636// same method name.
1637static bool isInstantiationOf(ClassTemplateDecl *Pattern,
1638 ClassTemplateDecl *Instance) {
1639 Pattern = Pattern->getCanonicalDecl();
1640
1641 do {
1642 Instance = Instance->getCanonicalDecl();
1643 if (Pattern == Instance) return true;
1644 Instance = Instance->getInstantiatedFromMemberTemplate();
1645 } while (Instance);
1646
1647 return false;
1648}
1649
Douglas Gregor0d696532009-09-28 06:34:35 +00001650static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
1651 FunctionTemplateDecl *Instance) {
1652 Pattern = Pattern->getCanonicalDecl();
1653
1654 do {
1655 Instance = Instance->getCanonicalDecl();
1656 if (Pattern == Instance) return true;
1657 Instance = Instance->getInstantiatedFromMemberTemplate();
1658 } while (Instance);
1659
1660 return false;
1661}
1662
Douglas Gregored9c0f92009-10-29 00:04:11 +00001663static bool
1664isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
1665 ClassTemplatePartialSpecializationDecl *Instance) {
1666 Pattern
1667 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
1668 do {
1669 Instance = cast<ClassTemplatePartialSpecializationDecl>(
1670 Instance->getCanonicalDecl());
1671 if (Pattern == Instance)
1672 return true;
1673 Instance = Instance->getInstantiatedFromMember();
1674 } while (Instance);
1675
1676 return false;
1677}
1678
John McCall52a575a2009-08-29 08:11:13 +00001679static bool isInstantiationOf(CXXRecordDecl *Pattern,
1680 CXXRecordDecl *Instance) {
1681 Pattern = Pattern->getCanonicalDecl();
1682
1683 do {
1684 Instance = Instance->getCanonicalDecl();
1685 if (Pattern == Instance) return true;
1686 Instance = Instance->getInstantiatedFromMemberClass();
1687 } while (Instance);
1688
1689 return false;
1690}
1691
1692static bool isInstantiationOf(FunctionDecl *Pattern,
1693 FunctionDecl *Instance) {
1694 Pattern = Pattern->getCanonicalDecl();
1695
1696 do {
1697 Instance = Instance->getCanonicalDecl();
1698 if (Pattern == Instance) return true;
1699 Instance = Instance->getInstantiatedFromMemberFunction();
1700 } while (Instance);
1701
1702 return false;
1703}
1704
1705static bool isInstantiationOf(EnumDecl *Pattern,
1706 EnumDecl *Instance) {
1707 Pattern = Pattern->getCanonicalDecl();
1708
1709 do {
1710 Instance = Instance->getCanonicalDecl();
1711 if (Pattern == Instance) return true;
1712 Instance = Instance->getInstantiatedFromMemberEnum();
1713 } while (Instance);
1714
1715 return false;
1716}
1717
Anders Carlsson0d8df782009-08-29 19:37:28 +00001718static bool isInstantiationOf(UnresolvedUsingDecl *Pattern,
1719 UsingDecl *Instance,
1720 ASTContext &C) {
1721 return C.getInstantiatedFromUnresolvedUsingDecl(Instance) == Pattern;
1722}
1723
John McCall52a575a2009-08-29 08:11:13 +00001724static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
1725 VarDecl *Instance) {
1726 assert(Instance->isStaticDataMember());
1727
1728 Pattern = Pattern->getCanonicalDecl();
1729
1730 do {
1731 Instance = Instance->getCanonicalDecl();
1732 if (Pattern == Instance) return true;
1733 Instance = Instance->getInstantiatedFromStaticDataMember();
1734 } while (Instance);
1735
1736 return false;
1737}
1738
Douglas Gregor815215d2009-05-27 05:35:12 +00001739static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00001740 if (D->getKind() != Other->getKind()) {
1741 if (UnresolvedUsingDecl *UUD = dyn_cast<UnresolvedUsingDecl>(D)) {
1742 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
1743 return isInstantiationOf(UUD, UD, Ctx);
1744 }
1745 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001746
Anders Carlsson0d8df782009-08-29 19:37:28 +00001747 return false;
1748 }
Mike Stump1eb44332009-09-09 15:08:12 +00001749
John McCall52a575a2009-08-29 08:11:13 +00001750 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
1751 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001752
John McCall52a575a2009-08-29 08:11:13 +00001753 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
1754 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00001755
John McCall52a575a2009-08-29 08:11:13 +00001756 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
1757 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00001758
Douglas Gregor7caa6822009-07-24 20:34:43 +00001759 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00001760 if (Var->isStaticDataMember())
1761 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
1762
1763 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
1764 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00001765
Douglas Gregor0d696532009-09-28 06:34:35 +00001766 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
1767 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
1768
Douglas Gregored9c0f92009-10-29 00:04:11 +00001769 if (ClassTemplatePartialSpecializationDecl *PartialSpec
1770 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
1771 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
1772 PartialSpec);
1773
Anders Carlssond8b285f2009-09-01 04:26:58 +00001774 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
1775 if (!Field->getDeclName()) {
1776 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00001777 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00001778 cast<FieldDecl>(D);
1779 }
1780 }
Mike Stump1eb44332009-09-09 15:08:12 +00001781
Douglas Gregor815215d2009-05-27 05:35:12 +00001782 return D->getDeclName() && isa<NamedDecl>(Other) &&
1783 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
1784}
1785
1786template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00001787static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00001788 NamedDecl *D,
1789 ForwardIterator first,
1790 ForwardIterator last) {
1791 for (; first != last; ++first)
1792 if (isInstantiationOf(Ctx, D, *first))
1793 return cast<NamedDecl>(*first);
1794
1795 return 0;
1796}
1797
John McCall02cace72009-08-28 07:59:38 +00001798/// \brief Finds the instantiation of the given declaration context
1799/// within the current instantiation.
1800///
1801/// \returns NULL if there was an error
Douglas Gregore95b4092009-09-16 18:34:49 +00001802DeclContext *Sema::FindInstantiatedContext(DeclContext* DC,
1803 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00001804 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00001805 Decl* ID = FindInstantiatedDecl(D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00001806 return cast_or_null<DeclContext>(ID);
1807 } else return DC;
1808}
1809
Douglas Gregored961e72009-05-27 17:54:46 +00001810/// \brief Find the instantiation of the given declaration within the
1811/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00001812///
1813/// This routine is intended to be used when \p D is a declaration
1814/// referenced from within a template, that needs to mapped into the
1815/// corresponding declaration within an instantiation. For example,
1816/// given:
1817///
1818/// \code
1819/// template<typename T>
1820/// struct X {
1821/// enum Kind {
1822/// KnownValue = sizeof(T)
1823/// };
1824///
1825/// bool getKind() const { return KnownValue; }
1826/// };
1827///
1828/// template struct X<int>;
1829/// \endcode
1830///
1831/// In the instantiation of X<int>::getKind(), we need to map the
1832/// EnumConstantDecl for KnownValue (which refers to
1833/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00001834/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
1835/// this mapping from within the instantiation of X<int>.
Douglas Gregore95b4092009-09-16 18:34:49 +00001836NamedDecl *Sema::FindInstantiatedDecl(NamedDecl *D,
1837 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor44c73842009-09-01 17:53:10 +00001838 if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D)) {
1839 // Transform all of the elements of the overloaded function set.
Mike Stump1eb44332009-09-09 15:08:12 +00001840 OverloadedFunctionDecl *Result
Douglas Gregor44c73842009-09-01 17:53:10 +00001841 = OverloadedFunctionDecl::Create(Context, CurContext, Ovl->getDeclName());
Mike Stump1eb44332009-09-09 15:08:12 +00001842
Douglas Gregor44c73842009-09-01 17:53:10 +00001843 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
1844 FEnd = Ovl->function_end();
1845 F != FEnd; ++F) {
1846 Result->addOverload(
Douglas Gregore95b4092009-09-16 18:34:49 +00001847 AnyFunctionDecl::getFromNamedDecl(FindInstantiatedDecl(*F,
1848 TemplateArgs)));
Douglas Gregor44c73842009-09-01 17:53:10 +00001849 }
Mike Stump1eb44332009-09-09 15:08:12 +00001850
Douglas Gregor44c73842009-09-01 17:53:10 +00001851 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001852 }
1853
Douglas Gregor815215d2009-05-27 05:35:12 +00001854 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00001855 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
1856 isa<TemplateTypeParmDecl>(D) || isa<TemplateTypeParmDecl>(D) ||
1857 ParentDC->isFunctionOrMethod()) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00001858 // D is a local of some kind. Look into the map of local
1859 // declarations to their instantiations.
1860 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
1861 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001862
Douglas Gregore95b4092009-09-16 18:34:49 +00001863 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
1864 if (!Record->isDependentContext())
1865 return D;
1866
1867 // If the RecordDecl is actually the injected-class-name or a "templated"
1868 // declaration for a class template or class template partial
1869 // specialization, substitute into the injected-class-name of the
1870 // class template or partial specialization to find the new DeclContext.
1871 QualType T;
1872 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
1873
1874 if (ClassTemplate) {
1875 T = ClassTemplate->getInjectedClassNameType(Context);
1876 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
1877 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1878 T = Context.getTypeDeclType(Record);
1879 ClassTemplate = PartialSpec->getSpecializedTemplate();
1880 }
1881
1882 if (!T.isNull()) {
1883 // Substitute into the injected-class-name to get the type corresponding
1884 // to the instantiation we want. This substitution should never fail,
1885 // since we know we can instantiate the injected-class-name or we wouldn't
1886 // have gotten to the injected-class-name!
1887 // FIXME: Can we use the CurrentInstantiationScope to avoid this extra
1888 // instantiation in the common case?
1889 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
1890 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
1891
1892 if (!T->isDependentType()) {
1893 assert(T->isRecordType() && "Instantiation must produce a record type");
1894 return T->getAs<RecordType>()->getDecl();
1895 }
1896
1897 // We are performing "partial" template instantiation to create the
1898 // member declarations for the members of a class template
1899 // specialization. Therefore, D is actually referring to something in
1900 // the current instantiation. Look through the current context,
1901 // which contains actual instantiations, to find the instantiation of
1902 // the "current instantiation" that D refers to.
Mike Stump1eb44332009-09-09 15:08:12 +00001903 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00001904 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001905 if (ClassTemplateSpecializationDecl *Spec
John McCall52a575a2009-08-29 08:11:13 +00001906 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00001907 if (isInstantiationOf(ClassTemplate,
1908 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00001909 return Spec;
1910 }
1911
Mike Stump1eb44332009-09-09 15:08:12 +00001912 assert(false &&
John McCall52a575a2009-08-29 08:11:13 +00001913 "Unable to find declaration for the current instantiation");
Douglas Gregore95b4092009-09-16 18:34:49 +00001914 return Record;
John McCall52a575a2009-08-29 08:11:13 +00001915 }
Douglas Gregore95b4092009-09-16 18:34:49 +00001916
1917 // Fall through to deal with other dependent record types (e.g.,
1918 // anonymous unions in class templates).
1919 }
John McCall52a575a2009-08-29 08:11:13 +00001920
Douglas Gregore95b4092009-09-16 18:34:49 +00001921 if (!ParentDC->isDependentContext())
1922 return D;
1923
1924 ParentDC = FindInstantiatedContext(ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001925 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00001926 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001927
Douglas Gregor815215d2009-05-27 05:35:12 +00001928 if (ParentDC != D->getDeclContext()) {
1929 // We performed some kind of instantiation in the parent context,
1930 // so now we need to look into the instantiated parent context to
1931 // find the instantiation of the declaration D.
1932 NamedDecl *Result = 0;
1933 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001934 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00001935 Result = findInstantiationOf(Context, D, Found.first, Found.second);
1936 } else {
1937 // Since we don't have a name for the entity we're looking for,
1938 // our only option is to walk through all of the declarations to
1939 // find that name. This will occur in a few cases:
1940 //
1941 // - anonymous struct/union within a template
1942 // - unnamed class/struct/union/enum within a template
1943 //
1944 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00001945 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001946 ParentDC->decls_begin(),
1947 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00001948 }
Mike Stump1eb44332009-09-09 15:08:12 +00001949
Douglas Gregor815215d2009-05-27 05:35:12 +00001950 assert(Result && "Unable to find instantiation of declaration!");
1951 D = Result;
1952 }
1953
Douglas Gregor815215d2009-05-27 05:35:12 +00001954 return D;
1955}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001956
Mike Stump1eb44332009-09-09 15:08:12 +00001957/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001958/// instantiations we have seen until this point.
1959void Sema::PerformPendingImplicitInstantiations() {
1960 while (!PendingImplicitInstantiations.empty()) {
1961 PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front();
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001962 PendingImplicitInstantiations.pop_front();
Mike Stump1eb44332009-09-09 15:08:12 +00001963
Douglas Gregor7caa6822009-07-24 20:34:43 +00001964 // Instantiate function definitions
1965 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001966 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001967 Function->getLocation(), *this,
1968 Context.getSourceManager(),
1969 "instantiating function definition");
Mike Stump1eb44332009-09-09 15:08:12 +00001970
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001971 if (!Function->getBody())
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001972 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001973 continue;
1974 }
Mike Stump1eb44332009-09-09 15:08:12 +00001975
Douglas Gregor7caa6822009-07-24 20:34:43 +00001976 // Instantiate static data member definitions.
1977 VarDecl *Var = cast<VarDecl>(Inst.first);
1978 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001979
Mike Stump1eb44332009-09-09 15:08:12 +00001980 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001981 Var->getLocation(), *this,
1982 Context.getSourceManager(),
1983 "instantiating static data member "
1984 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00001985
Douglas Gregor7caa6822009-07-24 20:34:43 +00001986 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001987 }
1988}