blob: 208352fb5322beac01acecd515c93b02186946ba [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 Gregor66724ea2009-11-14 01:20:54 +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.
John McCalla24dc2e2009-11-17 02:14:36 +0000680 Sema::LookupResult R(SemaRef, Function->getDeclName(), SourceLocation(),
681 Sema::LookupOrdinaryName,
682 Sema::LookupResult::ForRedeclaration);
683 SemaRef.LookupQualifiedName(R, DC);
Douglas Gregora735b202009-10-13 14:39:41 +0000684
685 PrevDecl = R.getAsSingleDecl(SemaRef.Context);
686
687 // In C++, the previous declaration we find might be a tag type
688 // (class or enum). In this case, the new declaration will hide the
689 // tag type. Note that this does does not apply if we're declaring a
690 // typedef (C++ [dcl.typedef]p4).
691 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
692 PrevDecl = 0;
693 }
694
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000695 SemaRef.CheckFunctionDeclaration(Function, PrevDecl, false, Redeclaration,
Douglas Gregore53060f2009-06-25 22:08:12 +0000696 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000697
Douglas Gregora735b202009-10-13 14:39:41 +0000698 // If the original function was part of a friend declaration,
699 // inherit its namespace state and add it to the owner.
700 NamedDecl *FromFriendD
701 = TemplateParams? cast<NamedDecl>(D->getDescribedFunctionTemplate()) : D;
702 if (FromFriendD->getFriendObjectKind()) {
703 NamedDecl *ToFriendD = 0;
704 if (TemplateParams) {
705 ToFriendD = cast<NamedDecl>(FunctionTemplate);
706 PrevDecl = FunctionTemplate->getPreviousDeclaration();
707 } else {
708 ToFriendD = Function;
709 PrevDecl = Function->getPreviousDeclaration();
710 }
711 ToFriendD->setObjectOfFriendDecl(PrevDecl != NULL);
712 if (!Owner->isDependentContext() && !PrevDecl)
713 DC->makeDeclVisibleInContext(ToFriendD, /* Recoverable = */ false);
714
715 if (!TemplateParams)
716 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
717 }
718
Douglas Gregore53060f2009-06-25 22:08:12 +0000719 return Function;
720}
721
Douglas Gregord60e1052009-08-27 16:57:43 +0000722Decl *
723TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
724 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +0000725 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
726 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +0000727 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +0000728 // We are creating a function template specialization from a function
729 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +0000730 // specialization for this particular set of template arguments.
Douglas Gregor6b906862009-08-21 00:16:32 +0000731 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000732 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000733 TemplateArgs.getInnermost().getFlatArgumentList(),
734 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000735 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000736
737 FunctionTemplateSpecializationInfo *Info
738 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor6b906862009-08-21 00:16:32 +0000739 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000740
Douglas Gregor6b906862009-08-21 00:16:32 +0000741 // If we already have a function template specialization, return it.
742 if (Info)
743 return Info->Function;
744 }
745
Douglas Gregor550d9b22009-10-31 17:21:17 +0000746 Sema::LocalInstantiationScope Scope(SemaRef, TemplateParams != 0);
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000747
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000748 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000749 QualType T = SubstFunctionType(D, Params);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000750 if (T.isNull())
751 return 0;
752
753 // Build the instantiated method declaration.
754 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
Douglas Gregordec06662009-08-21 18:42:58 +0000755 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Douglas Gregordec06662009-08-21 18:42:58 +0000757 DeclarationName Name = D->getDeclName();
Douglas Gregor17e32f32009-08-21 22:43:28 +0000758 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregordec06662009-08-21 18:42:58 +0000759 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
760 Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
761 SemaRef.Context.getCanonicalType(ClassTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000762 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
763 Constructor->getLocation(),
764 Name, T,
Douglas Gregor17e32f32009-08-21 22:43:28 +0000765 Constructor->getDeclaratorInfo(),
Mike Stump1eb44332009-09-09 15:08:12 +0000766 Constructor->isExplicit(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000767 Constructor->isInlineSpecified(), false);
Douglas Gregor17e32f32009-08-21 22:43:28 +0000768 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
769 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
770 Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
771 SemaRef.Context.getCanonicalType(ClassTy));
772 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
773 Destructor->getLocation(), Name,
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000774 T, Destructor->isInlineSpecified(), false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000775 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000776 CanQualType ConvTy
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000777 = SemaRef.Context.getCanonicalType(
John McCall183700f2009-09-21 23:43:11 +0000778 T->getAs<FunctionType>()->getResultType());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000779 Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
780 ConvTy);
781 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
782 Conversion->getLocation(), Name,
783 T, Conversion->getDeclaratorInfo(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000784 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000785 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +0000786 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000787 Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
Douglas Gregordec06662009-08-21 18:42:58 +0000788 D->getDeclName(), T, D->getDeclaratorInfo(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000789 D->isStatic(), D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +0000790 }
Douglas Gregor6b906862009-08-21 00:16:32 +0000791
Douglas Gregord60e1052009-08-27 16:57:43 +0000792 if (TemplateParams) {
793 // Our resulting instantiation is actually a function template, since we
794 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +0000795 //
Douglas Gregord60e1052009-08-27 16:57:43 +0000796 // template<typename T>
797 // struct X {
798 // template<typename U> void f(T, U);
799 // };
800 //
801 // X<int> x;
802 //
803 // We are instantiating the member template "f" within X<int>, which means
804 // substituting int for T, but leaving "f" as a member function template.
805 // Build the function template itself.
806 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
807 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +0000808 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +0000809 TemplateParams, Method);
810 if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +0000811 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +0000812 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +0000813 } else if (FunctionTemplate) {
814 // Record this function template specialization.
815 Method->setFunctionTemplateSpecialization(SemaRef.Context,
816 FunctionTemplate,
817 &TemplateArgs.getInnermost(),
818 InsertPos);
819 } else {
820 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +0000821 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +0000822 }
823
Mike Stump1eb44332009-09-09 15:08:12 +0000824 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000825 // out-of-line, the instantiation will have the same lexical
826 // context (which will be a namespace scope) as the template.
827 if (D->isOutOfLine())
828 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000829
Douglas Gregor5545e162009-03-24 00:38:23 +0000830 // Attach the parameters
831 for (unsigned P = 0; P < Params.size(); ++P)
832 Params[P]->setOwningFunction(Method);
Jay Foadbeaaccd2009-05-21 09:52:38 +0000833 Method->setParams(SemaRef.Context, Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +0000834
835 if (InitMethodInstantiation(Method, D))
836 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000837
Douglas Gregordec06662009-08-21 18:42:58 +0000838 NamedDecl *PrevDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000839
Douglas Gregord60e1052009-08-27 16:57:43 +0000840 if (!FunctionTemplate || TemplateParams) {
John McCalla24dc2e2009-11-17 02:14:36 +0000841 Sema::LookupResult R(SemaRef, Name, SourceLocation(),
842 Sema::LookupOrdinaryName,
843 Sema::LookupResult::ForRedeclaration);
844 SemaRef.LookupQualifiedName(R, Owner);
John McCallf36e02d2009-10-09 21:13:30 +0000845 PrevDecl = R.getAsSingleDecl(SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000846
Douglas Gregordec06662009-08-21 18:42:58 +0000847 // In C++, the previous declaration we find might be a tag type
848 // (class or enum). In this case, the new declaration will hide the
849 // tag type. Note that this does does not apply if we're declaring a
850 // typedef (C++ [dcl.typedef]p4).
851 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
852 PrevDecl = 0;
853 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000854
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000855 bool Redeclaration = false;
856 bool OverloadableAttrRequired = false;
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000857 SemaRef.CheckFunctionDeclaration(Method, PrevDecl, false, Redeclaration,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000858 /*FIXME:*/OverloadableAttrRequired);
859
Douglas Gregora735b202009-10-13 14:39:41 +0000860 if (!FunctionTemplate && (!Method->isInvalidDecl() || !PrevDecl) &&
861 !Method->getFriendObjectKind())
Douglas Gregordec06662009-08-21 18:42:58 +0000862 Owner->addDecl(Method);
Mike Stump1eb44332009-09-09 15:08:12 +0000863
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000864 return Method;
865}
866
Douglas Gregor615c5d42009-03-24 16:43:20 +0000867Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +0000868 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000869}
870
Douglas Gregor03b2b072009-03-24 00:15:49 +0000871Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +0000872 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +0000873}
874
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000875Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000876 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000877}
878
Douglas Gregor6477b692009-03-25 15:04:13 +0000879ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
John McCall58e46772009-10-23 21:48:59 +0000880 QualType T;
881 DeclaratorInfo *DI = D->getDeclaratorInfo();
882 if (DI) {
883 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
884 D->getDeclName());
885 if (DI) T = DI->getType();
886 } else {
887 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
888 D->getDeclName());
889 DI = 0;
890 }
891
892 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000893 return 0;
894
John McCall58e46772009-10-23 21:48:59 +0000895 T = SemaRef.adjustParameterType(T);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000896
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000897 // Allocate the parameter
John McCall58e46772009-10-23 21:48:59 +0000898 ParmVarDecl *Param
899 = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
900 D->getIdentifier(), T, DI, D->getStorageClass(), 0);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000901
Anders Carlsson9351c172009-08-25 03:18:48 +0000902 // Mark the default argument as being uninstantiated.
Douglas Gregorf43d0b32009-09-25 06:56:31 +0000903 if (D->hasUninstantiatedDefaultArg())
904 Param->setUninstantiatedDefaultArg(D->getUninstantiatedDefaultArg());
Douglas Gregor0ed09302009-09-25 07:03:22 +0000905 else if (Expr *Arg = D->getDefaultArg())
906 Param->setUninstantiatedDefaultArg(Arg);
907
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000908 // Note: we don't try to instantiate function parameters until after
909 // we've instantiated the function's type. Therefore, we don't have
910 // to check for 'void' parameter types here.
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000911 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000912 return Param;
913}
914
John McCalle29ba202009-08-20 01:44:21 +0000915Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
916 TemplateTypeParmDecl *D) {
917 // TODO: don't always clone when decls are refcounted.
918 const Type* T = D->getTypeForDecl();
919 assert(T->isTemplateTypeParmType());
920 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000921
John McCalle29ba202009-08-20 01:44:21 +0000922 TemplateTypeParmDecl *Inst =
923 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor550d9b22009-10-31 17:21:17 +0000924 TTPT->getDepth() - 1, TTPT->getIndex(),
John McCalle29ba202009-08-20 01:44:21 +0000925 TTPT->getName(),
926 D->wasDeclaredWithTypename(),
927 D->isParameterPack());
928
Douglas Gregor0f8716b2009-11-09 19:17:50 +0000929 if (D->hasDefaultArgument())
930 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
John McCalle29ba202009-08-20 01:44:21 +0000931
Douglas Gregor550d9b22009-10-31 17:21:17 +0000932 // Introduce this template parameter's instantiation into the instantiation
933 // scope.
934 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
935
John McCalle29ba202009-08-20 01:44:21 +0000936 return Inst;
937}
938
Douglas Gregor33642df2009-10-23 23:25:44 +0000939Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
940 NonTypeTemplateParmDecl *D) {
941 // Substitute into the type of the non-type template parameter.
942 QualType T;
943 DeclaratorInfo *DI = D->getDeclaratorInfo();
944 if (DI) {
945 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
946 D->getDeclName());
947 if (DI) T = DI->getType();
948 } else {
949 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
950 D->getDeclName());
951 DI = 0;
952 }
953 if (T.isNull())
954 return 0;
955
956 // Check that this type is acceptable for a non-type template parameter.
957 bool Invalid = false;
958 T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
959 if (T.isNull()) {
960 T = SemaRef.Context.IntTy;
961 Invalid = true;
962 }
963
964 NonTypeTemplateParmDecl *Param
965 = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
966 D->getDepth() - 1, D->getPosition(),
967 D->getIdentifier(), T, DI);
968 if (Invalid)
969 Param->setInvalidDecl();
970
971 Param->setDefaultArgument(D->getDefaultArgument());
Douglas Gregor550d9b22009-10-31 17:21:17 +0000972
973 // Introduce this template parameter's instantiation into the instantiation
974 // scope.
975 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +0000976 return Param;
977}
978
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000979Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +0000980TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
981 TemplateTemplateParmDecl *D) {
982 // Instantiate the template parameter list of the template template parameter.
983 TemplateParameterList *TempParams = D->getTemplateParameters();
984 TemplateParameterList *InstParams;
985 {
986 // Perform the actual substitution of template parameters within a new,
987 // local instantiation scope.
988 Sema::LocalInstantiationScope Scope(SemaRef);
989 InstParams = SubstTemplateParams(TempParams);
990 if (!InstParams)
991 return NULL;
992 }
993
994 // Build the template template parameter.
995 TemplateTemplateParmDecl *Param
996 = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
997 D->getDepth() - 1, D->getPosition(),
998 D->getIdentifier(), InstParams);
999 Param->setDefaultArgument(D->getDefaultArgument());
1000
1001 // Introduce this template parameter's instantiation into the instantiation
1002 // scope.
1003 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1004
1005 return Param;
1006}
1007
1008Decl *
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001009TemplateDeclInstantiator::VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +00001010 NestedNameSpecifier *NNS =
1011 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1012 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001013 TemplateArgs);
1014 if (!NNS)
1015 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001016
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001017 CXXScopeSpec SS;
1018 SS.setRange(D->getTargetNestedNameRange());
1019 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +00001020
1021 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00001022 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1023 D->getLocation(), SS, D->getTargetNameLocation(),
1024 D->getDeclName(), 0, D->isTypeName());
Anders Carlsson0d8df782009-08-29 19:37:28 +00001025 if (UD)
Mike Stump1eb44332009-09-09 15:08:12 +00001026 SemaRef.Context.setInstantiatedFromUnresolvedUsingDecl(cast<UsingDecl>(UD),
Anders Carlsson0d8df782009-08-29 19:37:28 +00001027 D);
1028 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001029}
1030
John McCallce3ff2b2009-08-25 22:02:44 +00001031Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001032 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001033 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001034 return Instantiator.Visit(D);
1035}
1036
John McCalle29ba202009-08-20 01:44:21 +00001037/// \brief Instantiates a nested template parameter list in the current
1038/// instantiation context.
1039///
1040/// \param L The parameter list to instantiate
1041///
1042/// \returns NULL if there was an error
1043TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001044TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001045 // Get errors for all the parameters before bailing out.
1046 bool Invalid = false;
1047
1048 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001049 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001050 ParamVector Params;
1051 Params.reserve(N);
1052 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1053 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001054 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001055 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001056 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00001057 }
1058
1059 // Clean up if we had an error.
1060 if (Invalid) {
1061 for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
1062 PI != PE; ++PI)
1063 if (*PI)
1064 (*PI)->Destroy(SemaRef.Context);
1065 return NULL;
1066 }
1067
1068 TemplateParameterList *InstL
1069 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1070 L->getLAngleLoc(), &Params.front(), N,
1071 L->getRAngleLoc());
1072 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001073}
John McCalle29ba202009-08-20 01:44:21 +00001074
Douglas Gregored9c0f92009-10-29 00:04:11 +00001075/// \brief Instantiate the declaration of a class template partial
1076/// specialization.
1077///
1078/// \param ClassTemplate the (instantiated) class template that is partially
1079// specialized by the instantiation of \p PartialSpec.
1080///
1081/// \param PartialSpec the (uninstantiated) class template partial
1082/// specialization that we are instantiating.
1083///
1084/// \returns true if there was an error, false otherwise.
1085bool
1086TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1087 ClassTemplateDecl *ClassTemplate,
1088 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001089 // Create a local instantiation scope for this class template partial
1090 // specialization, which will contain the instantiations of the template
1091 // parameters.
1092 Sema::LocalInstantiationScope Scope(SemaRef);
1093
Douglas Gregored9c0f92009-10-29 00:04:11 +00001094 // Substitute into the template parameters of the class template partial
1095 // specialization.
1096 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1097 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1098 if (!InstParams)
1099 return true;
1100
1101 // Substitute into the template arguments of the class template partial
1102 // specialization.
John McCall833ca992009-10-29 08:12:44 +00001103 const TemplateArgumentLoc *PartialSpecTemplateArgs
1104 = PartialSpec->getTemplateArgsAsWritten();
1105 unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1106
1107 llvm::SmallVector<TemplateArgumentLoc, 4> InstTemplateArgs(N);
1108 for (unsigned I = 0; I != N; ++I) {
1109 if (SemaRef.Subst(PartialSpecTemplateArgs[I], InstTemplateArgs[I],
1110 TemplateArgs))
Douglas Gregored9c0f92009-10-29 00:04:11 +00001111 return true;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001112 }
1113
1114
1115 // Check that the template argument list is well-formed for this
1116 // class template.
1117 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1118 InstTemplateArgs.size());
1119 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1120 PartialSpec->getLocation(),
1121 /*FIXME:*/PartialSpec->getLocation(),
1122 InstTemplateArgs.data(),
1123 InstTemplateArgs.size(),
1124 /*FIXME:*/PartialSpec->getLocation(),
1125 false,
1126 Converted))
1127 return true;
1128
1129 // Figure out where to insert this class template partial specialization
1130 // in the member template's set of class template partial specializations.
1131 llvm::FoldingSetNodeID ID;
1132 ClassTemplatePartialSpecializationDecl::Profile(ID,
1133 Converted.getFlatArguments(),
1134 Converted.flatSize(),
1135 SemaRef.Context);
1136 void *InsertPos = 0;
1137 ClassTemplateSpecializationDecl *PrevDecl
1138 = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
1139 InsertPos);
1140
1141 // Build the canonical type that describes the converted template
1142 // arguments of the class template partial specialization.
1143 QualType CanonType
1144 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1145 Converted.getFlatArguments(),
1146 Converted.flatSize());
1147
1148 // Build the fully-sugared type for this class template
1149 // specialization as the user wrote in the specialization
1150 // itself. This means that we'll pretty-print the type retrieved
1151 // from the specialization's declaration the way that the user
1152 // actually wrote the specialization, rather than formatting the
1153 // name based on the "canonical" representation used to store the
1154 // template arguments in the specialization.
1155 QualType WrittenTy
1156 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1157 InstTemplateArgs.data(),
1158 InstTemplateArgs.size(),
1159 CanonType);
1160
1161 if (PrevDecl) {
1162 // We've already seen a partial specialization with the same template
1163 // parameters and template arguments. This can happen, for example, when
1164 // substituting the outer template arguments ends up causing two
1165 // class template partial specializations of a member class template
1166 // to have identical forms, e.g.,
1167 //
1168 // template<typename T, typename U>
1169 // struct Outer {
1170 // template<typename X, typename Y> struct Inner;
1171 // template<typename Y> struct Inner<T, Y>;
1172 // template<typename Y> struct Inner<U, Y>;
1173 // };
1174 //
1175 // Outer<int, int> outer; // error: the partial specializations of Inner
1176 // // have the same signature.
1177 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1178 << WrittenTy;
1179 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1180 << SemaRef.Context.getTypeDeclType(PrevDecl);
1181 return true;
1182 }
1183
1184
1185 // Create the class template partial specialization declaration.
1186 ClassTemplatePartialSpecializationDecl *InstPartialSpec
1187 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context, Owner,
1188 PartialSpec->getLocation(),
1189 InstParams,
1190 ClassTemplate,
1191 Converted,
John McCall833ca992009-10-29 08:12:44 +00001192 InstTemplateArgs.data(),
1193 InstTemplateArgs.size(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00001194 0);
1195 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
1196 InstPartialSpec->setTypeAsWritten(WrittenTy);
1197
1198 // Add this partial specialization to the set of class template partial
1199 // specializations.
1200 ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec,
1201 InsertPos);
1202 return false;
1203}
1204
John McCallce3ff2b2009-08-25 22:02:44 +00001205/// \brief Does substitution on the type of the given function, including
1206/// all of the function parameters.
Douglas Gregor5545e162009-03-24 00:38:23 +00001207///
John McCallce3ff2b2009-08-25 22:02:44 +00001208/// \param D The function whose type will be the basis of the substitution
Douglas Gregor5545e162009-03-24 00:38:23 +00001209///
1210/// \param Params the instantiated parameter declarations
1211
John McCallce3ff2b2009-08-25 22:02:44 +00001212/// \returns the instantiated function's type if successful, a NULL
Douglas Gregor5545e162009-03-24 00:38:23 +00001213/// type if there was an error.
Mike Stump1eb44332009-09-09 15:08:12 +00001214QualType
John McCallce3ff2b2009-08-25 22:02:44 +00001215TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001216 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
1217 bool InvalidDecl = false;
1218
John McCallce3ff2b2009-08-25 22:02:44 +00001219 // Substitute all of the function's formal parameter types.
Douglas Gregor7e063902009-05-11 23:53:27 +00001220 TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001221 llvm::SmallVector<QualType, 4> ParamTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001222 for (FunctionDecl::param_iterator P = D->param_begin(),
Douglas Gregor5545e162009-03-24 00:38:23 +00001223 PEnd = D->param_end();
1224 P != PEnd; ++P) {
Douglas Gregor6477b692009-03-25 15:04:13 +00001225 if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
Douglas Gregor5545e162009-03-24 00:38:23 +00001226 if (PInst->getType()->isVoidType()) {
1227 SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
1228 PInst->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001229 } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001230 PInst->getType(),
1231 diag::err_abstract_type_in_decl,
1232 Sema::AbstractParamType))
Douglas Gregor5545e162009-03-24 00:38:23 +00001233 PInst->setInvalidDecl();
1234
1235 Params.push_back(PInst);
1236 ParamTys.push_back(PInst->getType());
1237
1238 if (PInst->isInvalidDecl())
1239 InvalidDecl = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001240 } else
Douglas Gregor5545e162009-03-24 00:38:23 +00001241 InvalidDecl = true;
1242 }
1243
1244 // FIXME: Deallocate dead declarations.
1245 if (InvalidDecl)
1246 return QualType();
1247
John McCall183700f2009-09-21 23:43:11 +00001248 const FunctionProtoType *Proto = D->getType()->getAs<FunctionProtoType>();
Douglas Gregor5545e162009-03-24 00:38:23 +00001249 assert(Proto && "Missing prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +00001250 QualType ResultType
John McCallce3ff2b2009-08-25 22:02:44 +00001251 = SemaRef.SubstType(Proto->getResultType(), TemplateArgs,
1252 D->getLocation(), D->getDeclName());
Douglas Gregor5545e162009-03-24 00:38:23 +00001253 if (ResultType.isNull())
1254 return QualType();
1255
Jay Foadbeaaccd2009-05-21 09:52:38 +00001256 return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
Douglas Gregor5545e162009-03-24 00:38:23 +00001257 Proto->isVariadic(), Proto->getTypeQuals(),
1258 D->getLocation(), D->getDeclName());
1259}
1260
Mike Stump1eb44332009-09-09 15:08:12 +00001261/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00001262/// declaration (New) from the corresponding fields of its template (Tmpl).
1263///
1264/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001265bool
1266TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00001267 FunctionDecl *Tmpl) {
1268 if (Tmpl->isDeleted())
1269 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00001270
Douglas Gregorcca9e962009-07-01 22:01:06 +00001271 // If we are performing substituting explicitly-specified template arguments
1272 // or deduced template arguments into a function template and we reach this
1273 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00001274 // to keeping the new function template specialization. We therefore
1275 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00001276 // into a template instantiation for this specific function template
1277 // specialization, which is not a SFINAE context, so that we diagnose any
1278 // further errors in the declaration itself.
1279 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1280 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1281 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1282 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00001283 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00001284 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001285 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00001286 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00001287 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001288 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1289 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
Douglas Gregorf35f8282009-11-11 21:54:23 +00001290 --SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001291 }
1292 }
Mike Stump1eb44332009-09-09 15:08:12 +00001293
Douglas Gregore53060f2009-06-25 22:08:12 +00001294 return false;
1295}
1296
Douglas Gregor5545e162009-03-24 00:38:23 +00001297/// \brief Initializes common fields of an instantiated method
1298/// declaration (New) from the corresponding fields of its template
1299/// (Tmpl).
1300///
1301/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001302bool
1303TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00001304 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001305 if (InitFunctionInstantiation(New, Tmpl))
1306 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001307
Douglas Gregor5545e162009-03-24 00:38:23 +00001308 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
1309 New->setAccess(Tmpl->getAccess());
Anders Carlsson77b7f1d2009-05-14 22:15:41 +00001310 if (Tmpl->isVirtualAsWritten()) {
1311 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +00001312 Record->setAggregate(false);
1313 Record->setPOD(false);
Eli Friedman1d954f62009-08-15 21:55:26 +00001314 Record->setEmpty(false);
Douglas Gregor5545e162009-03-24 00:38:23 +00001315 Record->setPolymorphic(true);
1316 }
Douglas Gregor5545e162009-03-24 00:38:23 +00001317 if (Tmpl->isPure()) {
1318 New->setPure();
1319 Record->setAbstract(true);
1320 }
1321
1322 // FIXME: attributes
1323 // FIXME: New needs a pointer to Tmpl
1324 return false;
1325}
Douglas Gregora58861f2009-05-13 20:28:22 +00001326
1327/// \brief Instantiate the definition of the given function from its
1328/// template.
1329///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001330/// \param PointOfInstantiation the point at which the instantiation was
1331/// required. Note that this is not precisely a "point of instantiation"
1332/// for the function, but it's close.
1333///
Douglas Gregora58861f2009-05-13 20:28:22 +00001334/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001335/// function template specialization or member function of a class template
1336/// specialization.
1337///
1338/// \param Recursive if true, recursively instantiates any functions that
1339/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001340///
1341/// \param DefinitionRequired if true, then we are performing an explicit
1342/// instantiation where the body of the function is required. Complain if
1343/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001344void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001345 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001346 bool Recursive,
1347 bool DefinitionRequired) {
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001348 if (Function->isInvalidDecl())
1349 return;
1350
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001351 assert(!Function->getBody() && "Already instantiated!");
Mike Stump1eb44332009-09-09 15:08:12 +00001352
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001353 // Never instantiate an explicit specialization.
1354 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1355 return;
1356
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001357 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00001358 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001359 Stmt *Pattern = 0;
1360 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001361 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001362
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001363 if (!Pattern) {
1364 if (DefinitionRequired) {
1365 if (Function->getPrimaryTemplate())
1366 Diag(PointOfInstantiation,
1367 diag::err_explicit_instantiation_undefined_func_template)
1368 << Function->getPrimaryTemplate();
1369 else
1370 Diag(PointOfInstantiation,
1371 diag::err_explicit_instantiation_undefined_member)
1372 << 1 << Function->getDeclName() << Function->getDeclContext();
1373
1374 if (PatternDecl)
1375 Diag(PatternDecl->getLocation(),
1376 diag::note_explicit_instantiation_here);
1377 }
1378
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001379 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001380 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001381
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001382 // C++0x [temp.explicit]p9:
1383 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00001384 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001385 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00001386 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001387 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001388 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001389 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001390
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001391 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
1392 if (Inst)
1393 return;
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001394
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001395 // If we're performing recursive template instantiation, create our own
1396 // queue of pending implicit instantiations that we will instantiate later,
1397 // while we're still within our own instantiation context.
1398 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1399 if (Recursive)
1400 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001401
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001402 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
1403
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001404 // Introduce a new scope where local variable instantiations will be
1405 // recorded.
1406 LocalInstantiationScope Scope(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001407
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001408 // Introduce the instantiated function parameters into the local
1409 // instantiation scope.
1410 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
1411 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
1412 Function->getParamDecl(I));
1413
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001414 // Enter the scope of this instantiation. We don't use
1415 // PushDeclContext because we don't have a scope.
1416 DeclContext *PreviousContext = CurContext;
1417 CurContext = Function;
1418
Mike Stump1eb44332009-09-09 15:08:12 +00001419 MultiLevelTemplateArgumentList TemplateArgs =
Anders Carlsson09025312009-08-29 05:16:22 +00001420 getTemplateInstantiationArgs(Function);
1421
1422 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001423 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00001424 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
1425 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
1426 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001427 }
1428
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001429 // Instantiate the function body.
Anders Carlsson09025312009-08-29 05:16:22 +00001430 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001431
Douglas Gregor52604ab2009-09-11 21:19:12 +00001432 if (Body.isInvalid())
1433 Function->setInvalidDecl();
1434
Mike Stump1eb44332009-09-09 15:08:12 +00001435 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001436 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001437
1438 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00001439
1440 DeclGroupRef DG(Function);
1441 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001442
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001443 if (Recursive) {
1444 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001445 // instantiation of this template.
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001446 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001447
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001448 // Restore the set of pending implicit instantiations.
1449 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1450 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001451}
1452
1453/// \brief Instantiate the definition of the given variable from its
1454/// template.
1455///
Douglas Gregor7caa6822009-07-24 20:34:43 +00001456/// \param PointOfInstantiation the point at which the instantiation was
1457/// required. Note that this is not precisely a "point of instantiation"
1458/// for the function, but it's close.
1459///
1460/// \param Var the already-instantiated declaration of a static member
1461/// variable of a class template specialization.
1462///
1463/// \param Recursive if true, recursively instantiates any functions that
1464/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001465///
1466/// \param DefinitionRequired if true, then we are performing an explicit
1467/// instantiation where an out-of-line definition of the member variable
1468/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001469void Sema::InstantiateStaticDataMemberDefinition(
1470 SourceLocation PointOfInstantiation,
1471 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001472 bool Recursive,
1473 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001474 if (Var->isInvalidDecl())
1475 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001476
Douglas Gregor7caa6822009-07-24 20:34:43 +00001477 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001478 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00001479 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00001480 assert(Def->isStaticDataMember() && "Not a static data member?");
1481 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00001482
Douglas Gregor0d035142009-10-27 18:42:08 +00001483 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001484 // We did not find an out-of-line definition of this static data member,
1485 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00001486 // instantiate this definition (or provide a specialization for it) in
1487 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001488 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001489 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001490 Diag(PointOfInstantiation,
1491 diag::err_explicit_instantiation_undefined_member)
1492 << 2 << Var->getDeclName() << Var->getDeclContext();
1493 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
1494 }
1495
Douglas Gregor7caa6822009-07-24 20:34:43 +00001496 return;
1497 }
1498
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001499 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001500 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001501 return;
1502
1503 // C++0x [temp.explicit]p9:
1504 // Except for inline functions, other explicit instantiation declarations
1505 // have the effect of suppressing the implicit instantiation of the entity
1506 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001507 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001508 == TSK_ExplicitInstantiationDeclaration)
1509 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001510
Douglas Gregor7caa6822009-07-24 20:34:43 +00001511 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
1512 if (Inst)
1513 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001514
Douglas Gregor7caa6822009-07-24 20:34:43 +00001515 // If we're performing recursive template instantiation, create our own
1516 // queue of pending implicit instantiations that we will instantiate later,
1517 // while we're still within our own instantiation context.
1518 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1519 if (Recursive)
1520 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001521
Douglas Gregor7caa6822009-07-24 20:34:43 +00001522 // Enter the scope of this instantiation. We don't use
1523 // PushDeclContext because we don't have a scope.
1524 DeclContext *PreviousContext = CurContext;
1525 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001526
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001527 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00001528 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00001529 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00001530 CurContext = PreviousContext;
1531
1532 if (Var) {
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001533 Var->setPreviousDeclaration(OldVar);
Douglas Gregor583f33b2009-10-15 18:07:02 +00001534 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
1535 assert(MSInfo && "Missing member specialization information?");
1536 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
1537 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00001538 DeclGroupRef DG(Var);
1539 Consumer.HandleTopLevelDecl(DG);
1540 }
Mike Stump1eb44332009-09-09 15:08:12 +00001541
Douglas Gregor7caa6822009-07-24 20:34:43 +00001542 if (Recursive) {
1543 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001544 // instantiation of this template.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001545 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001546
Douglas Gregor7caa6822009-07-24 20:34:43 +00001547 // Restore the set of pending implicit instantiations.
1548 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001549 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001550}
Douglas Gregor815215d2009-05-27 05:35:12 +00001551
Anders Carlsson09025312009-08-29 05:16:22 +00001552void
1553Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
1554 const CXXConstructorDecl *Tmpl,
1555 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001556
Anders Carlsson09025312009-08-29 05:16:22 +00001557 llvm::SmallVector<MemInitTy*, 4> NewInits;
1558
1559 // Instantiate all the initializers.
1560 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00001561 InitsEnd = Tmpl->init_end();
1562 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00001563 CXXBaseOrMemberInitializer *Init = *Inits;
1564
1565 ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001566
Anders Carlsson09025312009-08-29 05:16:22 +00001567 // Instantiate all the arguments.
1568 for (ExprIterator Args = Init->arg_begin(), ArgsEnd = Init->arg_end();
1569 Args != ArgsEnd; ++Args) {
1570 OwningExprResult NewArg = SubstExpr(*Args, TemplateArgs);
1571
1572 if (NewArg.isInvalid())
1573 New->setInvalidDecl();
1574 else
1575 NewArgs.push_back(NewArg.takeAs<Expr>());
1576 }
1577
1578 MemInitResult NewInit;
1579
1580 if (Init->isBaseInitializer()) {
Eli Friedmanc5573a82009-08-29 22:22:07 +00001581 QualType BaseType(Init->getBaseClass(), 0);
1582 BaseType = SubstType(BaseType, TemplateArgs, Init->getSourceLocation(),
1583 New->getDeclName());
Anders Carlsson09025312009-08-29 05:16:22 +00001584
1585 NewInit = BuildBaseInitializer(BaseType,
Mike Stump1eb44332009-09-09 15:08:12 +00001586 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001587 NewArgs.size(),
1588 Init->getSourceLocation(),
1589 Init->getRParenLoc(),
1590 New->getParent());
1591 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001592 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00001593
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001594 // Is this an anonymous union?
1595 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregore95b4092009-09-16 18:34:49 +00001596 Member = cast<FieldDecl>(FindInstantiatedDecl(UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001597 else
Douglas Gregore95b4092009-09-16 18:34:49 +00001598 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMember(),
1599 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00001600
1601 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001602 NewArgs.size(),
1603 Init->getSourceLocation(),
1604 Init->getRParenLoc());
1605 }
1606
1607 if (NewInit.isInvalid())
1608 New->setInvalidDecl();
1609 else {
1610 // FIXME: It would be nice if ASTOwningVector had a release function.
1611 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001612
Anders Carlsson09025312009-08-29 05:16:22 +00001613 NewInits.push_back((MemInitTy *)NewInit.get());
1614 }
1615 }
Mike Stump1eb44332009-09-09 15:08:12 +00001616
Anders Carlsson09025312009-08-29 05:16:22 +00001617 // Assign all the initializers to the new constructor.
Mike Stump1eb44332009-09-09 15:08:12 +00001618 ActOnMemInitializers(DeclPtrTy::make(New),
Anders Carlsson09025312009-08-29 05:16:22 +00001619 /*FIXME: ColonLoc */
1620 SourceLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001621 NewInits.data(), NewInits.size());
Anders Carlsson09025312009-08-29 05:16:22 +00001622}
1623
John McCall52a575a2009-08-29 08:11:13 +00001624// TODO: this could be templated if the various decl types used the
1625// same method name.
1626static bool isInstantiationOf(ClassTemplateDecl *Pattern,
1627 ClassTemplateDecl *Instance) {
1628 Pattern = Pattern->getCanonicalDecl();
1629
1630 do {
1631 Instance = Instance->getCanonicalDecl();
1632 if (Pattern == Instance) return true;
1633 Instance = Instance->getInstantiatedFromMemberTemplate();
1634 } while (Instance);
1635
1636 return false;
1637}
1638
Douglas Gregor0d696532009-09-28 06:34:35 +00001639static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
1640 FunctionTemplateDecl *Instance) {
1641 Pattern = Pattern->getCanonicalDecl();
1642
1643 do {
1644 Instance = Instance->getCanonicalDecl();
1645 if (Pattern == Instance) return true;
1646 Instance = Instance->getInstantiatedFromMemberTemplate();
1647 } while (Instance);
1648
1649 return false;
1650}
1651
Douglas Gregored9c0f92009-10-29 00:04:11 +00001652static bool
1653isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
1654 ClassTemplatePartialSpecializationDecl *Instance) {
1655 Pattern
1656 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
1657 do {
1658 Instance = cast<ClassTemplatePartialSpecializationDecl>(
1659 Instance->getCanonicalDecl());
1660 if (Pattern == Instance)
1661 return true;
1662 Instance = Instance->getInstantiatedFromMember();
1663 } while (Instance);
1664
1665 return false;
1666}
1667
John McCall52a575a2009-08-29 08:11:13 +00001668static bool isInstantiationOf(CXXRecordDecl *Pattern,
1669 CXXRecordDecl *Instance) {
1670 Pattern = Pattern->getCanonicalDecl();
1671
1672 do {
1673 Instance = Instance->getCanonicalDecl();
1674 if (Pattern == Instance) return true;
1675 Instance = Instance->getInstantiatedFromMemberClass();
1676 } while (Instance);
1677
1678 return false;
1679}
1680
1681static bool isInstantiationOf(FunctionDecl *Pattern,
1682 FunctionDecl *Instance) {
1683 Pattern = Pattern->getCanonicalDecl();
1684
1685 do {
1686 Instance = Instance->getCanonicalDecl();
1687 if (Pattern == Instance) return true;
1688 Instance = Instance->getInstantiatedFromMemberFunction();
1689 } while (Instance);
1690
1691 return false;
1692}
1693
1694static bool isInstantiationOf(EnumDecl *Pattern,
1695 EnumDecl *Instance) {
1696 Pattern = Pattern->getCanonicalDecl();
1697
1698 do {
1699 Instance = Instance->getCanonicalDecl();
1700 if (Pattern == Instance) return true;
1701 Instance = Instance->getInstantiatedFromMemberEnum();
1702 } while (Instance);
1703
1704 return false;
1705}
1706
Anders Carlsson0d8df782009-08-29 19:37:28 +00001707static bool isInstantiationOf(UnresolvedUsingDecl *Pattern,
1708 UsingDecl *Instance,
1709 ASTContext &C) {
1710 return C.getInstantiatedFromUnresolvedUsingDecl(Instance) == Pattern;
1711}
1712
John McCall52a575a2009-08-29 08:11:13 +00001713static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
1714 VarDecl *Instance) {
1715 assert(Instance->isStaticDataMember());
1716
1717 Pattern = Pattern->getCanonicalDecl();
1718
1719 do {
1720 Instance = Instance->getCanonicalDecl();
1721 if (Pattern == Instance) return true;
1722 Instance = Instance->getInstantiatedFromStaticDataMember();
1723 } while (Instance);
1724
1725 return false;
1726}
1727
Douglas Gregor815215d2009-05-27 05:35:12 +00001728static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00001729 if (D->getKind() != Other->getKind()) {
1730 if (UnresolvedUsingDecl *UUD = dyn_cast<UnresolvedUsingDecl>(D)) {
1731 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
1732 return isInstantiationOf(UUD, UD, Ctx);
1733 }
1734 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001735
Anders Carlsson0d8df782009-08-29 19:37:28 +00001736 return false;
1737 }
Mike Stump1eb44332009-09-09 15:08:12 +00001738
John McCall52a575a2009-08-29 08:11:13 +00001739 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
1740 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001741
John McCall52a575a2009-08-29 08:11:13 +00001742 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
1743 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00001744
John McCall52a575a2009-08-29 08:11:13 +00001745 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
1746 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00001747
Douglas Gregor7caa6822009-07-24 20:34:43 +00001748 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00001749 if (Var->isStaticDataMember())
1750 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
1751
1752 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
1753 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00001754
Douglas Gregor0d696532009-09-28 06:34:35 +00001755 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
1756 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
1757
Douglas Gregored9c0f92009-10-29 00:04:11 +00001758 if (ClassTemplatePartialSpecializationDecl *PartialSpec
1759 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
1760 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
1761 PartialSpec);
1762
Anders Carlssond8b285f2009-09-01 04:26:58 +00001763 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
1764 if (!Field->getDeclName()) {
1765 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00001766 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00001767 cast<FieldDecl>(D);
1768 }
1769 }
Mike Stump1eb44332009-09-09 15:08:12 +00001770
Douglas Gregor815215d2009-05-27 05:35:12 +00001771 return D->getDeclName() && isa<NamedDecl>(Other) &&
1772 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
1773}
1774
1775template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00001776static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00001777 NamedDecl *D,
1778 ForwardIterator first,
1779 ForwardIterator last) {
1780 for (; first != last; ++first)
1781 if (isInstantiationOf(Ctx, D, *first))
1782 return cast<NamedDecl>(*first);
1783
1784 return 0;
1785}
1786
John McCall02cace72009-08-28 07:59:38 +00001787/// \brief Finds the instantiation of the given declaration context
1788/// within the current instantiation.
1789///
1790/// \returns NULL if there was an error
Douglas Gregore95b4092009-09-16 18:34:49 +00001791DeclContext *Sema::FindInstantiatedContext(DeclContext* DC,
1792 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00001793 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00001794 Decl* ID = FindInstantiatedDecl(D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00001795 return cast_or_null<DeclContext>(ID);
1796 } else return DC;
1797}
1798
Douglas Gregored961e72009-05-27 17:54:46 +00001799/// \brief Find the instantiation of the given declaration within the
1800/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00001801///
1802/// This routine is intended to be used when \p D is a declaration
1803/// referenced from within a template, that needs to mapped into the
1804/// corresponding declaration within an instantiation. For example,
1805/// given:
1806///
1807/// \code
1808/// template<typename T>
1809/// struct X {
1810/// enum Kind {
1811/// KnownValue = sizeof(T)
1812/// };
1813///
1814/// bool getKind() const { return KnownValue; }
1815/// };
1816///
1817/// template struct X<int>;
1818/// \endcode
1819///
1820/// In the instantiation of X<int>::getKind(), we need to map the
1821/// EnumConstantDecl for KnownValue (which refers to
1822/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00001823/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
1824/// this mapping from within the instantiation of X<int>.
Douglas Gregore95b4092009-09-16 18:34:49 +00001825NamedDecl *Sema::FindInstantiatedDecl(NamedDecl *D,
1826 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor44c73842009-09-01 17:53:10 +00001827 if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D)) {
1828 // Transform all of the elements of the overloaded function set.
Mike Stump1eb44332009-09-09 15:08:12 +00001829 OverloadedFunctionDecl *Result
Douglas Gregor44c73842009-09-01 17:53:10 +00001830 = OverloadedFunctionDecl::Create(Context, CurContext, Ovl->getDeclName());
Mike Stump1eb44332009-09-09 15:08:12 +00001831
Douglas Gregor44c73842009-09-01 17:53:10 +00001832 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
1833 FEnd = Ovl->function_end();
1834 F != FEnd; ++F) {
1835 Result->addOverload(
Douglas Gregore95b4092009-09-16 18:34:49 +00001836 AnyFunctionDecl::getFromNamedDecl(FindInstantiatedDecl(*F,
1837 TemplateArgs)));
Douglas Gregor44c73842009-09-01 17:53:10 +00001838 }
Mike Stump1eb44332009-09-09 15:08:12 +00001839
Douglas Gregor44c73842009-09-01 17:53:10 +00001840 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001841 }
1842
Douglas Gregor815215d2009-05-27 05:35:12 +00001843 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00001844 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
1845 isa<TemplateTypeParmDecl>(D) || isa<TemplateTypeParmDecl>(D) ||
1846 ParentDC->isFunctionOrMethod()) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00001847 // D is a local of some kind. Look into the map of local
1848 // declarations to their instantiations.
1849 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
1850 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001851
Douglas Gregore95b4092009-09-16 18:34:49 +00001852 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
1853 if (!Record->isDependentContext())
1854 return D;
1855
1856 // If the RecordDecl is actually the injected-class-name or a "templated"
1857 // declaration for a class template or class template partial
1858 // specialization, substitute into the injected-class-name of the
1859 // class template or partial specialization to find the new DeclContext.
1860 QualType T;
1861 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
1862
1863 if (ClassTemplate) {
1864 T = ClassTemplate->getInjectedClassNameType(Context);
1865 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
1866 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1867 T = Context.getTypeDeclType(Record);
1868 ClassTemplate = PartialSpec->getSpecializedTemplate();
1869 }
1870
1871 if (!T.isNull()) {
1872 // Substitute into the injected-class-name to get the type corresponding
1873 // to the instantiation we want. This substitution should never fail,
1874 // since we know we can instantiate the injected-class-name or we wouldn't
1875 // have gotten to the injected-class-name!
1876 // FIXME: Can we use the CurrentInstantiationScope to avoid this extra
1877 // instantiation in the common case?
1878 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
1879 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
1880
1881 if (!T->isDependentType()) {
1882 assert(T->isRecordType() && "Instantiation must produce a record type");
1883 return T->getAs<RecordType>()->getDecl();
1884 }
1885
1886 // We are performing "partial" template instantiation to create the
1887 // member declarations for the members of a class template
1888 // specialization. Therefore, D is actually referring to something in
1889 // the current instantiation. Look through the current context,
1890 // which contains actual instantiations, to find the instantiation of
1891 // the "current instantiation" that D refers to.
Mike Stump1eb44332009-09-09 15:08:12 +00001892 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00001893 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001894 if (ClassTemplateSpecializationDecl *Spec
John McCall52a575a2009-08-29 08:11:13 +00001895 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00001896 if (isInstantiationOf(ClassTemplate,
1897 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00001898 return Spec;
1899 }
1900
Mike Stump1eb44332009-09-09 15:08:12 +00001901 assert(false &&
John McCall52a575a2009-08-29 08:11:13 +00001902 "Unable to find declaration for the current instantiation");
Douglas Gregore95b4092009-09-16 18:34:49 +00001903 return Record;
John McCall52a575a2009-08-29 08:11:13 +00001904 }
Douglas Gregore95b4092009-09-16 18:34:49 +00001905
1906 // Fall through to deal with other dependent record types (e.g.,
1907 // anonymous unions in class templates).
1908 }
John McCall52a575a2009-08-29 08:11:13 +00001909
Douglas Gregore95b4092009-09-16 18:34:49 +00001910 if (!ParentDC->isDependentContext())
1911 return D;
1912
1913 ParentDC = FindInstantiatedContext(ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001914 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00001915 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001916
Douglas Gregor815215d2009-05-27 05:35:12 +00001917 if (ParentDC != D->getDeclContext()) {
1918 // We performed some kind of instantiation in the parent context,
1919 // so now we need to look into the instantiated parent context to
1920 // find the instantiation of the declaration D.
1921 NamedDecl *Result = 0;
1922 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001923 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00001924 Result = findInstantiationOf(Context, D, Found.first, Found.second);
1925 } else {
1926 // Since we don't have a name for the entity we're looking for,
1927 // our only option is to walk through all of the declarations to
1928 // find that name. This will occur in a few cases:
1929 //
1930 // - anonymous struct/union within a template
1931 // - unnamed class/struct/union/enum within a template
1932 //
1933 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00001934 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001935 ParentDC->decls_begin(),
1936 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00001937 }
Mike Stump1eb44332009-09-09 15:08:12 +00001938
Douglas Gregor815215d2009-05-27 05:35:12 +00001939 assert(Result && "Unable to find instantiation of declaration!");
1940 D = Result;
1941 }
1942
Douglas Gregor815215d2009-05-27 05:35:12 +00001943 return D;
1944}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001945
Mike Stump1eb44332009-09-09 15:08:12 +00001946/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001947/// instantiations we have seen until this point.
1948void Sema::PerformPendingImplicitInstantiations() {
1949 while (!PendingImplicitInstantiations.empty()) {
1950 PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front();
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001951 PendingImplicitInstantiations.pop_front();
Mike Stump1eb44332009-09-09 15:08:12 +00001952
Douglas Gregor7caa6822009-07-24 20:34:43 +00001953 // Instantiate function definitions
1954 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001955 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001956 Function->getLocation(), *this,
1957 Context.getSourceManager(),
1958 "instantiating function definition");
Mike Stump1eb44332009-09-09 15:08:12 +00001959
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001960 if (!Function->getBody())
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001961 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001962 continue;
1963 }
Mike Stump1eb44332009-09-09 15:08:12 +00001964
Douglas Gregor7caa6822009-07-24 20:34:43 +00001965 // Instantiate static data member definitions.
1966 VarDecl *Var = cast<VarDecl>(Inst.first);
1967 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001968
Mike Stump1eb44332009-09-09 15:08:12 +00001969 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001970 Var->getLocation(), *this,
1971 Context.getSourceManager(),
1972 "instantiating static data member "
1973 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00001974
Douglas Gregor7caa6822009-07-24 20:34:43 +00001975 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001976 }
1977}