blob: 00ef407566f187826865c301c4c4acd43c691d5c [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);
Douglas Gregor48c32a72009-11-17 06:07:40 +000068 Decl *VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
Anders Carlsson0dde18e2009-08-28 15:18:15 +000069 Decl *VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D);
Mike Stump1eb44332009-09-09 15:08:12 +000070
Douglas Gregor8dbc2692009-03-17 21:15:40 +000071 // Base case. FIXME: Remove once we can instantiate everything.
Douglas Gregor48c32a72009-11-17 06:07:40 +000072 Decl *VisitDecl(Decl *D) {
73 unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID(
74 Diagnostic::Error,
75 "cannot instantiate %0 yet");
76 SemaRef.Diag(D->getLocation(), DiagID)
77 << D->getDeclKindName();
78
Douglas Gregor8dbc2692009-03-17 21:15:40 +000079 return 0;
80 }
Douglas Gregor5545e162009-03-24 00:38:23 +000081
John McCallfd810b12009-08-14 02:03:10 +000082 const LangOptions &getLangOptions() {
83 return SemaRef.getLangOptions();
84 }
85
Douglas Gregor5545e162009-03-24 00:38:23 +000086 // Helper functions for instantiating methods.
John McCallce3ff2b2009-08-25 22:02:44 +000087 QualType SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +000088 llvm::SmallVectorImpl<ParmVarDecl *> &Params);
Douglas Gregore53060f2009-06-25 22:08:12 +000089 bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
Douglas Gregor5545e162009-03-24 00:38:23 +000090 bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
John McCalle29ba202009-08-20 01:44:21 +000091
92 TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +000093 SubstTemplateParams(TemplateParameterList *List);
Douglas Gregored9c0f92009-10-29 00:04:11 +000094
95 bool InstantiateClassTemplatePartialSpecialization(
96 ClassTemplateDecl *ClassTemplate,
97 ClassTemplatePartialSpecializationDecl *PartialSpec);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000098 };
99}
100
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000101// FIXME: Is this too simple?
102void TemplateDeclInstantiator::InstantiateAttrs(Decl *Tmpl, Decl *New) {
103 for (const Attr *TmplAttr = Tmpl->getAttrs(); TmplAttr;
104 TmplAttr = TmplAttr->getNext()) {
105
106 // FIXME: Is cloning correct for all attributes?
107 Attr *NewAttr = TmplAttr->clone(SemaRef.Context);
108
109 New->addAttr(NewAttr);
110 }
111}
112
Douglas Gregor4f722be2009-03-25 15:45:12 +0000113Decl *
114TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
115 assert(false && "Translation units cannot be instantiated");
116 return D;
117}
118
119Decl *
120TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
121 assert(false && "Namespaces cannot be instantiated");
122 return D;
123}
124
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000125Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
126 bool Invalid = false;
John McCallba6a9bd2009-10-24 08:00:42 +0000127 DeclaratorInfo *DI = D->getTypeDeclaratorInfo();
128 if (DI->getType()->isDependentType()) {
129 DI = SemaRef.SubstType(DI, TemplateArgs,
130 D->getLocation(), D->getDeclName());
131 if (!DI) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000132 Invalid = true;
John McCallba6a9bd2009-10-24 08:00:42 +0000133 DI = SemaRef.Context.getTrivialDeclaratorInfo(SemaRef.Context.IntTy);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000134 }
135 }
Mike Stump1eb44332009-09-09 15:08:12 +0000136
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000137 // Create the new typedef
138 TypedefDecl *Typedef
139 = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
John McCallba6a9bd2009-10-24 08:00:42 +0000140 D->getIdentifier(), DI);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000141 if (Invalid)
142 Typedef->setInvalidDecl();
143
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000144 Owner->addDecl(Typedef);
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000146 return Typedef;
147}
148
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000149Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000150 // Do substitution on the type of the declaration
John McCall0a5fa062009-10-21 02:39:02 +0000151 DeclaratorInfo *DI = SemaRef.SubstType(D->getDeclaratorInfo(),
152 TemplateArgs,
153 D->getTypeSpecStartLoc(),
154 D->getDeclName());
155 if (!DI)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000156 return 0;
157
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000158 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000159 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
160 D->getLocation(), D->getIdentifier(),
John McCall0a5fa062009-10-21 02:39:02 +0000161 DI->getType(), DI,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000162 D->getStorageClass());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000163 Var->setThreadSpecified(D->isThreadSpecified());
164 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
165 Var->setDeclaredInCondition(D->isDeclaredInCondition());
Mike Stump1eb44332009-09-09 15:08:12 +0000166
167 // If we are instantiating a static data member defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000168 // out-of-line, the instantiation will have the same lexical
169 // context (which will be a namespace scope) as the template.
170 if (D->isOutOfLine())
171 Var->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Mike Stump390b4cc2009-05-16 07:39:55 +0000173 // FIXME: In theory, we could have a previous declaration for variables that
174 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000175 bool Redeclaration = false;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000176 SemaRef.CheckVariableDeclaration(Var, 0, Redeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Douglas Gregor7caa6822009-07-24 20:34:43 +0000178 if (D->isOutOfLine()) {
179 D->getLexicalDeclContext()->addDecl(Var);
180 Owner->makeDeclVisibleInContext(Var);
181 } else {
182 Owner->addDecl(Var);
183 }
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000185 // Link instantiations of static data members back to the template from
186 // which they were instantiated.
187 if (Var->isStaticDataMember())
188 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000189 TSK_ImplicitInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000190
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000191 if (D->getInit()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000192 OwningExprResult Init
John McCallce3ff2b2009-08-25 22:02:44 +0000193 = SemaRef.SubstExpr(D->getInit(), TemplateArgs);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000194 if (Init.isInvalid())
195 Var->setInvalidDecl();
Sebastian Redl42dddbe2009-11-08 10:16:43 +0000196 else if (!D->getType()->isDependentType() &&
197 !D->getInit()->isTypeDependent() &&
Douglas Gregore48319a2009-11-09 17:16:50 +0000198 !D->getInit()->isValueDependent()) {
Sebastian Redl42dddbe2009-11-08 10:16:43 +0000199 // If neither the declaration's type nor its initializer are dependent,
200 // we don't want to redo all the checking, especially since the
201 // initializer might have been wrapped by a CXXConstructExpr since we did
202 // it the first time.
203 Var->setInit(SemaRef.Context, Init.takeAs<Expr>());
204 }
Douglas Gregor83ddad32009-08-26 21:14:46 +0000205 else if (ParenListExpr *PLE = dyn_cast<ParenListExpr>((Expr *)Init.get())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000206 // FIXME: We're faking all of the comma locations, which is suboptimal.
Douglas Gregor83ddad32009-08-26 21:14:46 +0000207 // Do we even need these comma locations?
208 llvm::SmallVector<SourceLocation, 4> FakeCommaLocs;
209 if (PLE->getNumExprs() > 0) {
210 FakeCommaLocs.reserve(PLE->getNumExprs() - 1);
211 for (unsigned I = 0, N = PLE->getNumExprs() - 1; I != N; ++I) {
212 Expr *E = PLE->getExpr(I)->Retain();
213 FakeCommaLocs.push_back(
214 SemaRef.PP.getLocForEndOfToken(E->getLocEnd()));
215 }
Douglas Gregore9f8eb62009-08-26 23:26:04 +0000216 PLE->getExpr(PLE->getNumExprs() - 1)->Retain();
Douglas Gregor83ddad32009-08-26 21:14:46 +0000217 }
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Douglas Gregor83ddad32009-08-26 21:14:46 +0000219 // Add the direct initializer to the declaration.
220 SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
Mike Stump1eb44332009-09-09 15:08:12 +0000221 PLE->getLParenLoc(),
Douglas Gregor83ddad32009-08-26 21:14:46 +0000222 Sema::MultiExprArg(SemaRef,
223 (void**)PLE->getExprs(),
224 PLE->getNumExprs()),
225 FakeCommaLocs.data(),
226 PLE->getRParenLoc());
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Douglas Gregor83ddad32009-08-26 21:14:46 +0000228 // When Init is destroyed, it will destroy the instantiated ParenListExpr;
229 // we've explicitly retained all of its subexpressions already.
230 } else
Chris Lattnerb28317a2009-03-28 19:18:32 +0000231 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init),
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000232 D->hasCXXDirectInitializer());
Douglas Gregor65b90052009-07-27 17:43:39 +0000233 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
234 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000235
236 return Var;
237}
238
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000239Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
240 bool Invalid = false;
John McCall07fb6be2009-10-22 23:33:21 +0000241 DeclaratorInfo *DI = D->getDeclaratorInfo();
242 if (DI->getType()->isDependentType()) {
243 DI = SemaRef.SubstType(DI, TemplateArgs,
244 D->getLocation(), D->getDeclName());
245 if (!DI) {
246 DI = D->getDeclaratorInfo();
247 Invalid = true;
248 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000249 // C++ [temp.arg.type]p3:
250 // If a declaration acquires a function type through a type
251 // dependent on a template-parameter and this causes a
252 // declaration that does not use the syntactic form of a
253 // function declarator to have function type, the program is
254 // ill-formed.
255 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000256 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000257 Invalid = true;
258 }
259 }
260
261 Expr *BitWidth = D->getBitWidth();
262 if (Invalid)
263 BitWidth = 0;
264 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000265 // The bit-width expression is not potentially evaluated.
266 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000268 OwningExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000269 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000270 if (InstantiatedBitWidth.isInvalid()) {
271 Invalid = true;
272 BitWidth = 0;
273 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000274 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000275 }
276
John McCall07fb6be2009-10-22 23:33:21 +0000277 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
278 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000279 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000280 D->getLocation(),
281 D->isMutable(),
282 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000283 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000284 D->getAccess(),
285 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000286 if (!Field) {
287 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000288 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000289 }
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000291 InstantiateAttrs(D, Field);
292
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000293 if (Invalid)
294 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000295
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000296 if (!Field->getDeclName()) {
297 // Keep track of where this decl came from.
298 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000299 }
Mike Stump1eb44332009-09-09 15:08:12 +0000300
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000301 Field->setImplicit(D->isImplicit());
302 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000303
304 return Field;
305}
306
John McCall02cace72009-08-28 07:59:38 +0000307Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
308 FriendDecl::FriendUnion FU;
309
310 // Handle friend type expressions by simply substituting template
311 // parameters into the pattern type.
312 if (Type *Ty = D->getFriendType()) {
313 QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs,
314 D->getLocation(), DeclarationName());
315 if (T.isNull()) return 0;
316
317 assert(getLangOptions().CPlusPlus0x || T->isRecordType());
318 FU = T.getTypePtr();
319
320 // Handle everything else by appropriate substitution.
321 } else {
322 NamedDecl *ND = D->getFriendDecl();
323 assert(ND && "friend decl must be a decl or a type!");
324
Douglas Gregora735b202009-10-13 14:39:41 +0000325 // FIXME: We have a problem here, because the nested call to Visit(ND)
326 // will inject the thing that the friend references into the current
327 // owner, which is wrong.
John McCall02cace72009-08-28 07:59:38 +0000328 Decl *NewND = Visit(ND);
329 if (!NewND) return 0;
330
331 FU = cast<NamedDecl>(NewND);
John McCallfd810b12009-08-14 02:03:10 +0000332 }
Mike Stump1eb44332009-09-09 15:08:12 +0000333
John McCall02cace72009-08-28 07:59:38 +0000334 FriendDecl *FD =
335 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU,
336 D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000337 FD->setAccess(AS_public);
John McCall02cace72009-08-28 07:59:38 +0000338 Owner->addDecl(FD);
339 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000340}
341
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000342Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
343 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Douglas Gregorac7610d2009-06-22 20:57:11 +0000345 // The expression in a static assertion is not potentially evaluated.
346 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000348 OwningExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000349 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000350 if (InstantiatedAssertExpr.isInvalid())
351 return 0;
352
Douglas Gregor43d9d922009-08-08 01:41:12 +0000353 OwningExprResult Message(SemaRef, D->getMessage());
354 D->getMessage()->Retain();
Mike Stump1eb44332009-09-09 15:08:12 +0000355 Decl *StaticAssert
356 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
Chris Lattnerb28317a2009-03-28 19:18:32 +0000357 move(InstantiatedAssertExpr),
358 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000359 return StaticAssert;
360}
361
362Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000363 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000364 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000365 D->getTagKeywordLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000366 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000367 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000368 Enum->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000369 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000370 Enum->startDefinition();
371
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000372 llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000373
374 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000375 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
376 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000377 EC != ECEnd; ++EC) {
378 // The specified value for the enumerator.
379 OwningExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000380 if (Expr *UninstValue = EC->getInitExpr()) {
381 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000382 EnterExpressionEvaluationContext Unevaluated(SemaRef,
Douglas Gregorac7610d2009-06-22 20:57:11 +0000383 Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000384
John McCallce3ff2b2009-08-25 22:02:44 +0000385 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000386 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000387
388 // Drop the initial value and continue.
389 bool isInvalid = false;
390 if (Value.isInvalid()) {
391 Value = SemaRef.Owned((Expr *)0);
392 isInvalid = true;
393 }
394
Mike Stump1eb44332009-09-09 15:08:12 +0000395 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000396 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
397 EC->getLocation(), EC->getIdentifier(),
398 move(Value));
399
400 if (isInvalid) {
401 if (EnumConst)
402 EnumConst->setInvalidDecl();
403 Enum->setInvalidDecl();
404 }
405
406 if (EnumConst) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000407 Enum->addDecl(EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000408 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000409 LastEnumConst = EnumConst;
410 }
411 }
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000413 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000414 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000415 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
416 Sema::DeclPtrTy::make(Enum),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000417 &Enumerators[0], Enumerators.size(),
418 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000419
420 return Enum;
421}
422
Douglas Gregor6477b692009-03-25 15:04:13 +0000423Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
424 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
425 return 0;
426}
427
Douglas Gregored9c0f92009-10-29 00:04:11 +0000428namespace {
429 class SortDeclByLocation {
430 SourceManager &SourceMgr;
431
432 public:
433 explicit SortDeclByLocation(SourceManager &SourceMgr)
434 : SourceMgr(SourceMgr) { }
435
436 bool operator()(const Decl *X, const Decl *Y) const {
437 return SourceMgr.isBeforeInTranslationUnit(X->getLocation(),
438 Y->getLocation());
439 }
440 };
441}
442
John McCalle29ba202009-08-20 01:44:21 +0000443Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000444 // Create a local instantiation scope for this class template, which
445 // will contain the instantiations of the template parameters.
446 Sema::LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000447 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000448 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000449 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000450 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000451
452 CXXRecordDecl *Pattern = D->getTemplatedDecl();
453 CXXRecordDecl *RecordInst
454 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner,
455 Pattern->getLocation(), Pattern->getIdentifier(),
Douglas Gregorf0510d42009-10-12 23:11:44 +0000456 Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL,
457 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000458
459 ClassTemplateDecl *Inst
460 = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
461 D->getIdentifier(), InstParams, RecordInst, 0);
462 RecordInst->setDescribedClassTemplate(Inst);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000463 if (D->getFriendObjectKind())
464 Inst->setObjectOfFriendDecl(true);
465 else
466 Inst->setAccess(D->getAccess());
John McCalle29ba202009-08-20 01:44:21 +0000467 Inst->setInstantiatedFromMemberTemplate(D);
Douglas Gregorf0510d42009-10-12 23:11:44 +0000468
469 // Trigger creation of the type for the instantiation.
470 SemaRef.Context.getTypeDeclType(RecordInst);
471
Douglas Gregor259571e2009-10-30 22:42:42 +0000472 // Finish handling of friends.
473 if (Inst->getFriendObjectKind()) {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000474 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000475 }
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000476
John McCalle29ba202009-08-20 01:44:21 +0000477 Owner->addDecl(Inst);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000478
479 // First, we sort the partial specializations by location, so
480 // that we instantiate them in the order they were declared.
481 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
482 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
483 P = D->getPartialSpecializations().begin(),
484 PEnd = D->getPartialSpecializations().end();
485 P != PEnd; ++P)
486 PartialSpecs.push_back(&*P);
487 std::sort(PartialSpecs.begin(), PartialSpecs.end(),
488 SortDeclByLocation(SemaRef.SourceMgr));
489
490 // Instantiate all of the partial specializations of this member class
491 // template.
492 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
493 InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
494
John McCalle29ba202009-08-20 01:44:21 +0000495 return Inst;
496}
497
Douglas Gregord60e1052009-08-27 16:57:43 +0000498Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000499TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
500 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000501 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
502
503 // Lookup the already-instantiated declaration in the instantiation
504 // of the class template and return that.
505 DeclContext::lookup_result Found
506 = Owner->lookup(ClassTemplate->getDeclName());
507 if (Found.first == Found.second)
508 return 0;
509
510 ClassTemplateDecl *InstClassTemplate
511 = dyn_cast<ClassTemplateDecl>(*Found.first);
512 if (!InstClassTemplate)
513 return 0;
514
515 Decl *DCanon = D->getCanonicalDecl();
516 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
517 P = InstClassTemplate->getPartialSpecializations().begin(),
518 PEnd = InstClassTemplate->getPartialSpecializations().end();
519 P != PEnd; ++P) {
520 if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
521 return &*P;
522 }
523
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000524 return 0;
525}
526
527Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000528TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000529 // Create a local instantiation scope for this function template, which
530 // will contain the instantiations of the template parameters and then get
531 // merged with the local instantiation scope for the function template
532 // itself.
533 Sema::LocalInstantiationScope Scope(SemaRef);
534
Douglas Gregord60e1052009-08-27 16:57:43 +0000535 TemplateParameterList *TempParams = D->getTemplateParameters();
536 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000537 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000538 return NULL;
Douglas Gregored9c0f92009-10-29 00:04:11 +0000539
Douglas Gregora735b202009-10-13 14:39:41 +0000540 FunctionDecl *Instantiated = 0;
541 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
542 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
543 InstParams));
544 else
545 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
546 D->getTemplatedDecl(),
547 InstParams));
548
549 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000550 return 0;
551
Mike Stump1eb44332009-09-09 15:08:12 +0000552 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000553 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000554 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000555 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000556 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000557 assert(InstTemplate &&
558 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
559 if (!InstTemplate->getInstantiatedFromMemberTemplate())
560 InstTemplate->setInstantiatedFromMemberTemplate(D);
561
562 // Add non-friends into the owner.
563 if (!InstTemplate->getFriendObjectKind())
564 Owner->addDecl(InstTemplate);
Douglas Gregord60e1052009-08-27 16:57:43 +0000565 return InstTemplate;
566}
567
Douglas Gregord475b8d2009-03-25 21:17:03 +0000568Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
569 CXXRecordDecl *PrevDecl = 0;
570 if (D->isInjectedClassName())
571 PrevDecl = cast<CXXRecordDecl>(Owner);
572
573 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000574 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000575 D->getLocation(), D->getIdentifier(),
576 D->getTagKeywordLoc(), PrevDecl);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000577 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000578 // FIXME: Check against AS_none is an ugly hack to work around the issue that
579 // the tag decls introduced by friend class declarations don't have an access
580 // specifier. Remove once this area of the code gets sorted out.
581 if (D->getAccess() != AS_none)
582 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000583 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000584 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000585
John McCall02cace72009-08-28 07:59:38 +0000586 // If the original function was part of a friend declaration,
587 // inherit its namespace state.
588 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
589 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
590
Anders Carlssond8b285f2009-09-01 04:26:58 +0000591 Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
592
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000593 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000594 return Record;
595}
596
John McCall02cace72009-08-28 07:59:38 +0000597/// Normal class members are of more specific types and therefore
598/// don't make it here. This function serves two purposes:
599/// 1) instantiating function templates
600/// 2) substituting friend declarations
601/// FIXME: preserve function definitions in case #2
Douglas Gregora735b202009-10-13 14:39:41 +0000602 Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
603 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000604 // Check whether there is already a function template specialization for
605 // this declaration.
606 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
607 void *InsertPos = 0;
Douglas Gregora735b202009-10-13 14:39:41 +0000608 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000609 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000610 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000611 TemplateArgs.getInnermost().getFlatArgumentList(),
612 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor828e2262009-07-29 16:09:57 +0000613 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000614
615 FunctionTemplateSpecializationInfo *Info
616 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor127102b2009-06-29 20:59:39 +0000617 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000618
Douglas Gregor127102b2009-06-29 20:59:39 +0000619 // If we already have a function template specialization, return it.
620 if (Info)
621 return Info->Function;
622 }
Mike Stump1eb44332009-09-09 15:08:12 +0000623
Douglas Gregor550d9b22009-10-31 17:21:17 +0000624 Sema::LocalInstantiationScope Scope(SemaRef, TemplateParams != 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000625
Douglas Gregore53060f2009-06-25 22:08:12 +0000626 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000627 QualType T = SubstFunctionType(D, Params);
Douglas Gregore53060f2009-06-25 22:08:12 +0000628 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000629 return 0;
John McCallfd810b12009-08-14 02:03:10 +0000630
Douglas Gregore53060f2009-06-25 22:08:12 +0000631 // Build the instantiated method declaration.
Douglas Gregore95b4092009-09-16 18:34:49 +0000632 DeclContext *DC = SemaRef.FindInstantiatedContext(D->getDeclContext(),
633 TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +0000634 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +0000635 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000636 D->getDeclName(), T, D->getDeclaratorInfo(),
637 D->getStorageClass(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000638 D->isInlineSpecified(), D->hasWrittenPrototype());
John McCall02cace72009-08-28 07:59:38 +0000639 Function->setLexicalDeclContext(Owner);
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Douglas Gregore53060f2009-06-25 22:08:12 +0000641 // Attach the parameters
642 for (unsigned P = 0; P < Params.size(); ++P)
643 Params[P]->setOwningFunction(Function);
644 Function->setParams(SemaRef.Context, Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +0000645
Douglas Gregora735b202009-10-13 14:39:41 +0000646 if (TemplateParams) {
647 // Our resulting instantiation is actually a function template, since we
648 // are substituting only the outer template parameters. For example, given
649 //
650 // template<typename T>
651 // struct X {
652 // template<typename U> friend void f(T, U);
653 // };
654 //
655 // X<int> x;
656 //
657 // We are instantiating the friend function template "f" within X<int>,
658 // which means substituting int for T, but leaving "f" as a friend function
659 // template.
660 // Build the function template itself.
661 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Owner,
662 Function->getLocation(),
663 Function->getDeclName(),
664 TemplateParams, Function);
665 Function->setDescribedFunctionTemplate(FunctionTemplate);
666 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregor66724ea2009-11-14 01:20:54 +0000667 } else if (FunctionTemplate) {
668 // Record this function template specialization.
669 Function->setFunctionTemplateSpecialization(SemaRef.Context,
670 FunctionTemplate,
671 &TemplateArgs.getInnermost(),
672 InsertPos);
John McCall02cace72009-08-28 07:59:38 +0000673 }
Douglas Gregora735b202009-10-13 14:39:41 +0000674
Douglas Gregore53060f2009-06-25 22:08:12 +0000675 if (InitFunctionInstantiation(Function, D))
676 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000677
Douglas Gregore53060f2009-06-25 22:08:12 +0000678 bool Redeclaration = false;
679 bool OverloadableAttrRequired = false;
Douglas Gregora735b202009-10-13 14:39:41 +0000680
Douglas Gregore53060f2009-06-25 22:08:12 +0000681 NamedDecl *PrevDecl = 0;
Douglas Gregora735b202009-10-13 14:39:41 +0000682 if (TemplateParams || !FunctionTemplate) {
683 // Look only into the namespace where the friend would be declared to
684 // find a previous declaration. This is the innermost enclosing namespace,
685 // as described in ActOnFriendFunctionDecl.
John McCalla24dc2e2009-11-17 02:14:36 +0000686 Sema::LookupResult R(SemaRef, Function->getDeclName(), SourceLocation(),
687 Sema::LookupOrdinaryName,
688 Sema::LookupResult::ForRedeclaration);
689 SemaRef.LookupQualifiedName(R, DC);
Douglas Gregora735b202009-10-13 14:39:41 +0000690
691 PrevDecl = R.getAsSingleDecl(SemaRef.Context);
692
693 // In C++, the previous declaration we find might be a tag type
694 // (class or enum). In this case, the new declaration will hide the
695 // tag type. Note that this does does not apply if we're declaring a
696 // typedef (C++ [dcl.typedef]p4).
697 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
698 PrevDecl = 0;
699 }
700
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000701 SemaRef.CheckFunctionDeclaration(Function, PrevDecl, false, Redeclaration,
Douglas Gregore53060f2009-06-25 22:08:12 +0000702 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000703
Douglas Gregora735b202009-10-13 14:39:41 +0000704 // If the original function was part of a friend declaration,
705 // inherit its namespace state and add it to the owner.
706 NamedDecl *FromFriendD
707 = TemplateParams? cast<NamedDecl>(D->getDescribedFunctionTemplate()) : D;
708 if (FromFriendD->getFriendObjectKind()) {
709 NamedDecl *ToFriendD = 0;
710 if (TemplateParams) {
711 ToFriendD = cast<NamedDecl>(FunctionTemplate);
712 PrevDecl = FunctionTemplate->getPreviousDeclaration();
713 } else {
714 ToFriendD = Function;
715 PrevDecl = Function->getPreviousDeclaration();
716 }
717 ToFriendD->setObjectOfFriendDecl(PrevDecl != NULL);
718 if (!Owner->isDependentContext() && !PrevDecl)
719 DC->makeDeclVisibleInContext(ToFriendD, /* Recoverable = */ false);
720
721 if (!TemplateParams)
722 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
723 }
724
Douglas Gregore53060f2009-06-25 22:08:12 +0000725 return Function;
726}
727
Douglas Gregord60e1052009-08-27 16:57:43 +0000728Decl *
729TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
730 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +0000731 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
732 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +0000733 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +0000734 // We are creating a function template specialization from a function
735 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +0000736 // specialization for this particular set of template arguments.
Douglas Gregor6b906862009-08-21 00:16:32 +0000737 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000738 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000739 TemplateArgs.getInnermost().getFlatArgumentList(),
740 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000741 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000742
743 FunctionTemplateSpecializationInfo *Info
744 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor6b906862009-08-21 00:16:32 +0000745 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000746
Douglas Gregor6b906862009-08-21 00:16:32 +0000747 // If we already have a function template specialization, return it.
748 if (Info)
749 return Info->Function;
750 }
751
Douglas Gregor550d9b22009-10-31 17:21:17 +0000752 Sema::LocalInstantiationScope Scope(SemaRef, TemplateParams != 0);
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000753
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000754 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000755 QualType T = SubstFunctionType(D, Params);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000756 if (T.isNull())
757 return 0;
758
759 // Build the instantiated method declaration.
760 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
Douglas Gregordec06662009-08-21 18:42:58 +0000761 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000762
Douglas Gregordec06662009-08-21 18:42:58 +0000763 DeclarationName Name = D->getDeclName();
Douglas Gregor17e32f32009-08-21 22:43:28 +0000764 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregordec06662009-08-21 18:42:58 +0000765 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
766 Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
767 SemaRef.Context.getCanonicalType(ClassTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000768 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
769 Constructor->getLocation(),
770 Name, T,
Douglas Gregor17e32f32009-08-21 22:43:28 +0000771 Constructor->getDeclaratorInfo(),
Mike Stump1eb44332009-09-09 15:08:12 +0000772 Constructor->isExplicit(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000773 Constructor->isInlineSpecified(), false);
Douglas Gregor17e32f32009-08-21 22:43:28 +0000774 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
775 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
776 Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
777 SemaRef.Context.getCanonicalType(ClassTy));
778 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
779 Destructor->getLocation(), Name,
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000780 T, Destructor->isInlineSpecified(), false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000781 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000782 CanQualType ConvTy
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000783 = SemaRef.Context.getCanonicalType(
John McCall183700f2009-09-21 23:43:11 +0000784 T->getAs<FunctionType>()->getResultType());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000785 Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
786 ConvTy);
787 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
788 Conversion->getLocation(), Name,
789 T, Conversion->getDeclaratorInfo(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000790 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000791 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +0000792 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000793 Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
Douglas Gregordec06662009-08-21 18:42:58 +0000794 D->getDeclName(), T, D->getDeclaratorInfo(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000795 D->isStatic(), D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +0000796 }
Douglas Gregor6b906862009-08-21 00:16:32 +0000797
Douglas Gregord60e1052009-08-27 16:57:43 +0000798 if (TemplateParams) {
799 // Our resulting instantiation is actually a function template, since we
800 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +0000801 //
Douglas Gregord60e1052009-08-27 16:57:43 +0000802 // template<typename T>
803 // struct X {
804 // template<typename U> void f(T, U);
805 // };
806 //
807 // X<int> x;
808 //
809 // We are instantiating the member template "f" within X<int>, which means
810 // substituting int for T, but leaving "f" as a member function template.
811 // Build the function template itself.
812 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
813 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +0000814 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +0000815 TemplateParams, Method);
816 if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +0000817 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +0000818 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +0000819 } else if (FunctionTemplate) {
820 // Record this function template specialization.
821 Method->setFunctionTemplateSpecialization(SemaRef.Context,
822 FunctionTemplate,
823 &TemplateArgs.getInnermost(),
824 InsertPos);
825 } else {
826 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +0000827 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +0000828 }
829
Mike Stump1eb44332009-09-09 15:08:12 +0000830 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000831 // out-of-line, the instantiation will have the same lexical
832 // context (which will be a namespace scope) as the template.
833 if (D->isOutOfLine())
834 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000835
Douglas Gregor5545e162009-03-24 00:38:23 +0000836 // Attach the parameters
837 for (unsigned P = 0; P < Params.size(); ++P)
838 Params[P]->setOwningFunction(Method);
Jay Foadbeaaccd2009-05-21 09:52:38 +0000839 Method->setParams(SemaRef.Context, Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +0000840
841 if (InitMethodInstantiation(Method, D))
842 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000843
Douglas Gregordec06662009-08-21 18:42:58 +0000844 NamedDecl *PrevDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000845
Douglas Gregord60e1052009-08-27 16:57:43 +0000846 if (!FunctionTemplate || TemplateParams) {
John McCalla24dc2e2009-11-17 02:14:36 +0000847 Sema::LookupResult R(SemaRef, Name, SourceLocation(),
848 Sema::LookupOrdinaryName,
849 Sema::LookupResult::ForRedeclaration);
850 SemaRef.LookupQualifiedName(R, Owner);
John McCallf36e02d2009-10-09 21:13:30 +0000851 PrevDecl = R.getAsSingleDecl(SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Douglas Gregordec06662009-08-21 18:42:58 +0000853 // In C++, the previous declaration we find might be a tag type
854 // (class or enum). In this case, the new declaration will hide the
855 // tag type. Note that this does does not apply if we're declaring a
856 // typedef (C++ [dcl.typedef]p4).
857 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
858 PrevDecl = 0;
859 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000860
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000861 bool Redeclaration = false;
862 bool OverloadableAttrRequired = false;
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000863 SemaRef.CheckFunctionDeclaration(Method, PrevDecl, false, Redeclaration,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000864 /*FIXME:*/OverloadableAttrRequired);
865
Douglas Gregora735b202009-10-13 14:39:41 +0000866 if (!FunctionTemplate && (!Method->isInvalidDecl() || !PrevDecl) &&
867 !Method->getFriendObjectKind())
Douglas Gregordec06662009-08-21 18:42:58 +0000868 Owner->addDecl(Method);
Mike Stump1eb44332009-09-09 15:08:12 +0000869
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000870 return Method;
871}
872
Douglas Gregor615c5d42009-03-24 16:43:20 +0000873Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +0000874 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000875}
876
Douglas Gregor03b2b072009-03-24 00:15:49 +0000877Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +0000878 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +0000879}
880
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000881Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000882 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000883}
884
Douglas Gregor6477b692009-03-25 15:04:13 +0000885ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
John McCall58e46772009-10-23 21:48:59 +0000886 QualType T;
887 DeclaratorInfo *DI = D->getDeclaratorInfo();
888 if (DI) {
889 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
890 D->getDeclName());
891 if (DI) T = DI->getType();
892 } else {
893 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
894 D->getDeclName());
895 DI = 0;
896 }
897
898 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000899 return 0;
900
John McCall58e46772009-10-23 21:48:59 +0000901 T = SemaRef.adjustParameterType(T);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000902
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000903 // Allocate the parameter
John McCall58e46772009-10-23 21:48:59 +0000904 ParmVarDecl *Param
905 = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
906 D->getIdentifier(), T, DI, D->getStorageClass(), 0);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000907
Anders Carlsson9351c172009-08-25 03:18:48 +0000908 // Mark the default argument as being uninstantiated.
Douglas Gregorf43d0b32009-09-25 06:56:31 +0000909 if (D->hasUninstantiatedDefaultArg())
910 Param->setUninstantiatedDefaultArg(D->getUninstantiatedDefaultArg());
Douglas Gregor0ed09302009-09-25 07:03:22 +0000911 else if (Expr *Arg = D->getDefaultArg())
912 Param->setUninstantiatedDefaultArg(Arg);
913
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000914 // Note: we don't try to instantiate function parameters until after
915 // we've instantiated the function's type. Therefore, we don't have
916 // to check for 'void' parameter types here.
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000917 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000918 return Param;
919}
920
John McCalle29ba202009-08-20 01:44:21 +0000921Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
922 TemplateTypeParmDecl *D) {
923 // TODO: don't always clone when decls are refcounted.
924 const Type* T = D->getTypeForDecl();
925 assert(T->isTemplateTypeParmType());
926 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000927
John McCalle29ba202009-08-20 01:44:21 +0000928 TemplateTypeParmDecl *Inst =
929 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor550d9b22009-10-31 17:21:17 +0000930 TTPT->getDepth() - 1, TTPT->getIndex(),
John McCalle29ba202009-08-20 01:44:21 +0000931 TTPT->getName(),
932 D->wasDeclaredWithTypename(),
933 D->isParameterPack());
934
Douglas Gregor0f8716b2009-11-09 19:17:50 +0000935 if (D->hasDefaultArgument())
936 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
John McCalle29ba202009-08-20 01:44:21 +0000937
Douglas Gregor550d9b22009-10-31 17:21:17 +0000938 // Introduce this template parameter's instantiation into the instantiation
939 // scope.
940 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
941
John McCalle29ba202009-08-20 01:44:21 +0000942 return Inst;
943}
944
Douglas Gregor33642df2009-10-23 23:25:44 +0000945Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
946 NonTypeTemplateParmDecl *D) {
947 // Substitute into the type of the non-type template parameter.
948 QualType T;
949 DeclaratorInfo *DI = D->getDeclaratorInfo();
950 if (DI) {
951 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
952 D->getDeclName());
953 if (DI) T = DI->getType();
954 } else {
955 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
956 D->getDeclName());
957 DI = 0;
958 }
959 if (T.isNull())
960 return 0;
961
962 // Check that this type is acceptable for a non-type template parameter.
963 bool Invalid = false;
964 T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
965 if (T.isNull()) {
966 T = SemaRef.Context.IntTy;
967 Invalid = true;
968 }
969
970 NonTypeTemplateParmDecl *Param
971 = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
972 D->getDepth() - 1, D->getPosition(),
973 D->getIdentifier(), T, DI);
974 if (Invalid)
975 Param->setInvalidDecl();
976
977 Param->setDefaultArgument(D->getDefaultArgument());
Douglas Gregor550d9b22009-10-31 17:21:17 +0000978
979 // Introduce this template parameter's instantiation into the instantiation
980 // scope.
981 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +0000982 return Param;
983}
984
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000985Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +0000986TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
987 TemplateTemplateParmDecl *D) {
988 // Instantiate the template parameter list of the template template parameter.
989 TemplateParameterList *TempParams = D->getTemplateParameters();
990 TemplateParameterList *InstParams;
991 {
992 // Perform the actual substitution of template parameters within a new,
993 // local instantiation scope.
994 Sema::LocalInstantiationScope Scope(SemaRef);
995 InstParams = SubstTemplateParams(TempParams);
996 if (!InstParams)
997 return NULL;
998 }
999
1000 // Build the template template parameter.
1001 TemplateTemplateParmDecl *Param
1002 = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1003 D->getDepth() - 1, D->getPosition(),
1004 D->getIdentifier(), InstParams);
1005 Param->setDefaultArgument(D->getDefaultArgument());
1006
1007 // Introduce this template parameter's instantiation into the instantiation
1008 // scope.
1009 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1010
1011 return Param;
1012}
1013
Douglas Gregor48c32a72009-11-17 06:07:40 +00001014Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1015 // Using directives are never dependent, so they require no explicit
1016
1017 UsingDirectiveDecl *Inst
1018 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1019 D->getNamespaceKeyLocation(),
1020 D->getQualifierRange(), D->getQualifier(),
1021 D->getIdentLocation(),
1022 D->getNominatedNamespace(),
1023 D->getCommonAncestor());
1024 Owner->addDecl(Inst);
1025 return Inst;
1026}
1027
Douglas Gregor9106ef72009-11-11 16:58:32 +00001028Decl *
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001029TemplateDeclInstantiator::VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +00001030 NestedNameSpecifier *NNS =
1031 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1032 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001033 TemplateArgs);
1034 if (!NNS)
1035 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001036
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001037 CXXScopeSpec SS;
1038 SS.setRange(D->getTargetNestedNameRange());
1039 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +00001040
1041 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00001042 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1043 D->getLocation(), SS, D->getTargetNameLocation(),
1044 D->getDeclName(), 0, D->isTypeName());
Anders Carlsson0d8df782009-08-29 19:37:28 +00001045 if (UD)
Mike Stump1eb44332009-09-09 15:08:12 +00001046 SemaRef.Context.setInstantiatedFromUnresolvedUsingDecl(cast<UsingDecl>(UD),
Anders Carlsson0d8df782009-08-29 19:37:28 +00001047 D);
1048 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001049}
1050
John McCallce3ff2b2009-08-25 22:02:44 +00001051Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001052 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001053 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001054 return Instantiator.Visit(D);
1055}
1056
John McCalle29ba202009-08-20 01:44:21 +00001057/// \brief Instantiates a nested template parameter list in the current
1058/// instantiation context.
1059///
1060/// \param L The parameter list to instantiate
1061///
1062/// \returns NULL if there was an error
1063TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001064TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001065 // Get errors for all the parameters before bailing out.
1066 bool Invalid = false;
1067
1068 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001069 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001070 ParamVector Params;
1071 Params.reserve(N);
1072 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1073 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001074 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001075 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001076 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00001077 }
1078
1079 // Clean up if we had an error.
1080 if (Invalid) {
1081 for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
1082 PI != PE; ++PI)
1083 if (*PI)
1084 (*PI)->Destroy(SemaRef.Context);
1085 return NULL;
1086 }
1087
1088 TemplateParameterList *InstL
1089 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1090 L->getLAngleLoc(), &Params.front(), N,
1091 L->getRAngleLoc());
1092 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001093}
John McCalle29ba202009-08-20 01:44:21 +00001094
Douglas Gregored9c0f92009-10-29 00:04:11 +00001095/// \brief Instantiate the declaration of a class template partial
1096/// specialization.
1097///
1098/// \param ClassTemplate the (instantiated) class template that is partially
1099// specialized by the instantiation of \p PartialSpec.
1100///
1101/// \param PartialSpec the (uninstantiated) class template partial
1102/// specialization that we are instantiating.
1103///
1104/// \returns true if there was an error, false otherwise.
1105bool
1106TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1107 ClassTemplateDecl *ClassTemplate,
1108 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001109 // Create a local instantiation scope for this class template partial
1110 // specialization, which will contain the instantiations of the template
1111 // parameters.
1112 Sema::LocalInstantiationScope Scope(SemaRef);
1113
Douglas Gregored9c0f92009-10-29 00:04:11 +00001114 // Substitute into the template parameters of the class template partial
1115 // specialization.
1116 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1117 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1118 if (!InstParams)
1119 return true;
1120
1121 // Substitute into the template arguments of the class template partial
1122 // specialization.
John McCall833ca992009-10-29 08:12:44 +00001123 const TemplateArgumentLoc *PartialSpecTemplateArgs
1124 = PartialSpec->getTemplateArgsAsWritten();
1125 unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1126
1127 llvm::SmallVector<TemplateArgumentLoc, 4> InstTemplateArgs(N);
1128 for (unsigned I = 0; I != N; ++I) {
1129 if (SemaRef.Subst(PartialSpecTemplateArgs[I], InstTemplateArgs[I],
1130 TemplateArgs))
Douglas Gregored9c0f92009-10-29 00:04:11 +00001131 return true;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001132 }
1133
1134
1135 // Check that the template argument list is well-formed for this
1136 // class template.
1137 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1138 InstTemplateArgs.size());
1139 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1140 PartialSpec->getLocation(),
1141 /*FIXME:*/PartialSpec->getLocation(),
1142 InstTemplateArgs.data(),
1143 InstTemplateArgs.size(),
1144 /*FIXME:*/PartialSpec->getLocation(),
1145 false,
1146 Converted))
1147 return true;
1148
1149 // Figure out where to insert this class template partial specialization
1150 // in the member template's set of class template partial specializations.
1151 llvm::FoldingSetNodeID ID;
1152 ClassTemplatePartialSpecializationDecl::Profile(ID,
1153 Converted.getFlatArguments(),
1154 Converted.flatSize(),
1155 SemaRef.Context);
1156 void *InsertPos = 0;
1157 ClassTemplateSpecializationDecl *PrevDecl
1158 = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
1159 InsertPos);
1160
1161 // Build the canonical type that describes the converted template
1162 // arguments of the class template partial specialization.
1163 QualType CanonType
1164 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1165 Converted.getFlatArguments(),
1166 Converted.flatSize());
1167
1168 // Build the fully-sugared type for this class template
1169 // specialization as the user wrote in the specialization
1170 // itself. This means that we'll pretty-print the type retrieved
1171 // from the specialization's declaration the way that the user
1172 // actually wrote the specialization, rather than formatting the
1173 // name based on the "canonical" representation used to store the
1174 // template arguments in the specialization.
1175 QualType WrittenTy
1176 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1177 InstTemplateArgs.data(),
1178 InstTemplateArgs.size(),
1179 CanonType);
1180
1181 if (PrevDecl) {
1182 // We've already seen a partial specialization with the same template
1183 // parameters and template arguments. This can happen, for example, when
1184 // substituting the outer template arguments ends up causing two
1185 // class template partial specializations of a member class template
1186 // to have identical forms, e.g.,
1187 //
1188 // template<typename T, typename U>
1189 // struct Outer {
1190 // template<typename X, typename Y> struct Inner;
1191 // template<typename Y> struct Inner<T, Y>;
1192 // template<typename Y> struct Inner<U, Y>;
1193 // };
1194 //
1195 // Outer<int, int> outer; // error: the partial specializations of Inner
1196 // // have the same signature.
1197 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1198 << WrittenTy;
1199 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1200 << SemaRef.Context.getTypeDeclType(PrevDecl);
1201 return true;
1202 }
1203
1204
1205 // Create the class template partial specialization declaration.
1206 ClassTemplatePartialSpecializationDecl *InstPartialSpec
1207 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context, Owner,
1208 PartialSpec->getLocation(),
1209 InstParams,
1210 ClassTemplate,
1211 Converted,
John McCall833ca992009-10-29 08:12:44 +00001212 InstTemplateArgs.data(),
1213 InstTemplateArgs.size(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00001214 0);
1215 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
1216 InstPartialSpec->setTypeAsWritten(WrittenTy);
1217
1218 // Add this partial specialization to the set of class template partial
1219 // specializations.
1220 ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec,
1221 InsertPos);
1222 return false;
1223}
1224
John McCallce3ff2b2009-08-25 22:02:44 +00001225/// \brief Does substitution on the type of the given function, including
1226/// all of the function parameters.
Douglas Gregor5545e162009-03-24 00:38:23 +00001227///
John McCallce3ff2b2009-08-25 22:02:44 +00001228/// \param D The function whose type will be the basis of the substitution
Douglas Gregor5545e162009-03-24 00:38:23 +00001229///
1230/// \param Params the instantiated parameter declarations
1231
John McCallce3ff2b2009-08-25 22:02:44 +00001232/// \returns the instantiated function's type if successful, a NULL
Douglas Gregor5545e162009-03-24 00:38:23 +00001233/// type if there was an error.
Mike Stump1eb44332009-09-09 15:08:12 +00001234QualType
John McCallce3ff2b2009-08-25 22:02:44 +00001235TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001236 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
1237 bool InvalidDecl = false;
1238
John McCallce3ff2b2009-08-25 22:02:44 +00001239 // Substitute all of the function's formal parameter types.
Douglas Gregor7e063902009-05-11 23:53:27 +00001240 TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001241 llvm::SmallVector<QualType, 4> ParamTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001242 for (FunctionDecl::param_iterator P = D->param_begin(),
Douglas Gregor5545e162009-03-24 00:38:23 +00001243 PEnd = D->param_end();
1244 P != PEnd; ++P) {
Douglas Gregor6477b692009-03-25 15:04:13 +00001245 if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
Douglas Gregor5545e162009-03-24 00:38:23 +00001246 if (PInst->getType()->isVoidType()) {
1247 SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
1248 PInst->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001249 } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001250 PInst->getType(),
1251 diag::err_abstract_type_in_decl,
1252 Sema::AbstractParamType))
Douglas Gregor5545e162009-03-24 00:38:23 +00001253 PInst->setInvalidDecl();
1254
1255 Params.push_back(PInst);
1256 ParamTys.push_back(PInst->getType());
1257
1258 if (PInst->isInvalidDecl())
1259 InvalidDecl = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001260 } else
Douglas Gregor5545e162009-03-24 00:38:23 +00001261 InvalidDecl = true;
1262 }
1263
1264 // FIXME: Deallocate dead declarations.
1265 if (InvalidDecl)
1266 return QualType();
1267
John McCall183700f2009-09-21 23:43:11 +00001268 const FunctionProtoType *Proto = D->getType()->getAs<FunctionProtoType>();
Douglas Gregor5545e162009-03-24 00:38:23 +00001269 assert(Proto && "Missing prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +00001270 QualType ResultType
John McCallce3ff2b2009-08-25 22:02:44 +00001271 = SemaRef.SubstType(Proto->getResultType(), TemplateArgs,
1272 D->getLocation(), D->getDeclName());
Douglas Gregor5545e162009-03-24 00:38:23 +00001273 if (ResultType.isNull())
1274 return QualType();
1275
Jay Foadbeaaccd2009-05-21 09:52:38 +00001276 return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
Douglas Gregor5545e162009-03-24 00:38:23 +00001277 Proto->isVariadic(), Proto->getTypeQuals(),
1278 D->getLocation(), D->getDeclName());
1279}
1280
Mike Stump1eb44332009-09-09 15:08:12 +00001281/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00001282/// declaration (New) from the corresponding fields of its template (Tmpl).
1283///
1284/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001285bool
1286TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00001287 FunctionDecl *Tmpl) {
1288 if (Tmpl->isDeleted())
1289 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00001290
Douglas Gregorcca9e962009-07-01 22:01:06 +00001291 // If we are performing substituting explicitly-specified template arguments
1292 // or deduced template arguments into a function template and we reach this
1293 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00001294 // to keeping the new function template specialization. We therefore
1295 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00001296 // into a template instantiation for this specific function template
1297 // specialization, which is not a SFINAE context, so that we diagnose any
1298 // further errors in the declaration itself.
1299 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1300 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1301 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1302 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00001303 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00001304 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001305 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00001306 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00001307 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001308 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1309 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
Douglas Gregorf35f8282009-11-11 21:54:23 +00001310 --SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001311 }
1312 }
Mike Stump1eb44332009-09-09 15:08:12 +00001313
Douglas Gregore53060f2009-06-25 22:08:12 +00001314 return false;
1315}
1316
Douglas Gregor5545e162009-03-24 00:38:23 +00001317/// \brief Initializes common fields of an instantiated method
1318/// declaration (New) from the corresponding fields of its template
1319/// (Tmpl).
1320///
1321/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001322bool
1323TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00001324 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001325 if (InitFunctionInstantiation(New, Tmpl))
1326 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001327
Douglas Gregor5545e162009-03-24 00:38:23 +00001328 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
1329 New->setAccess(Tmpl->getAccess());
Anders Carlsson77b7f1d2009-05-14 22:15:41 +00001330 if (Tmpl->isVirtualAsWritten()) {
1331 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +00001332 Record->setAggregate(false);
1333 Record->setPOD(false);
Eli Friedman1d954f62009-08-15 21:55:26 +00001334 Record->setEmpty(false);
Douglas Gregor5545e162009-03-24 00:38:23 +00001335 Record->setPolymorphic(true);
1336 }
Douglas Gregor5545e162009-03-24 00:38:23 +00001337 if (Tmpl->isPure()) {
1338 New->setPure();
1339 Record->setAbstract(true);
1340 }
1341
1342 // FIXME: attributes
1343 // FIXME: New needs a pointer to Tmpl
1344 return false;
1345}
Douglas Gregora58861f2009-05-13 20:28:22 +00001346
1347/// \brief Instantiate the definition of the given function from its
1348/// template.
1349///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001350/// \param PointOfInstantiation the point at which the instantiation was
1351/// required. Note that this is not precisely a "point of instantiation"
1352/// for the function, but it's close.
1353///
Douglas Gregora58861f2009-05-13 20:28:22 +00001354/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001355/// function template specialization or member function of a class template
1356/// specialization.
1357///
1358/// \param Recursive if true, recursively instantiates any functions that
1359/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001360///
1361/// \param DefinitionRequired if true, then we are performing an explicit
1362/// instantiation where the body of the function is required. Complain if
1363/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001364void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001365 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001366 bool Recursive,
1367 bool DefinitionRequired) {
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001368 if (Function->isInvalidDecl())
1369 return;
1370
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001371 assert(!Function->getBody() && "Already instantiated!");
Mike Stump1eb44332009-09-09 15:08:12 +00001372
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001373 // Never instantiate an explicit specialization.
1374 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1375 return;
1376
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001377 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00001378 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001379 Stmt *Pattern = 0;
1380 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001381 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001382
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001383 if (!Pattern) {
1384 if (DefinitionRequired) {
1385 if (Function->getPrimaryTemplate())
1386 Diag(PointOfInstantiation,
1387 diag::err_explicit_instantiation_undefined_func_template)
1388 << Function->getPrimaryTemplate();
1389 else
1390 Diag(PointOfInstantiation,
1391 diag::err_explicit_instantiation_undefined_member)
1392 << 1 << Function->getDeclName() << Function->getDeclContext();
1393
1394 if (PatternDecl)
1395 Diag(PatternDecl->getLocation(),
1396 diag::note_explicit_instantiation_here);
1397 }
1398
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001399 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001400 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001401
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001402 // C++0x [temp.explicit]p9:
1403 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00001404 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001405 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00001406 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001407 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001408 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001409 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001410
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001411 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
1412 if (Inst)
1413 return;
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001414
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001415 // If we're performing recursive template instantiation, create our own
1416 // queue of pending implicit instantiations that we will instantiate later,
1417 // while we're still within our own instantiation context.
1418 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1419 if (Recursive)
1420 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001421
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001422 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
1423
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001424 // Introduce a new scope where local variable instantiations will be
1425 // recorded.
1426 LocalInstantiationScope Scope(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001427
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001428 // Introduce the instantiated function parameters into the local
1429 // instantiation scope.
1430 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
1431 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
1432 Function->getParamDecl(I));
1433
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001434 // Enter the scope of this instantiation. We don't use
1435 // PushDeclContext because we don't have a scope.
1436 DeclContext *PreviousContext = CurContext;
1437 CurContext = Function;
1438
Mike Stump1eb44332009-09-09 15:08:12 +00001439 MultiLevelTemplateArgumentList TemplateArgs =
Anders Carlsson09025312009-08-29 05:16:22 +00001440 getTemplateInstantiationArgs(Function);
1441
1442 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001443 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00001444 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
1445 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
1446 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001447 }
1448
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001449 // Instantiate the function body.
Anders Carlsson09025312009-08-29 05:16:22 +00001450 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001451
Douglas Gregor52604ab2009-09-11 21:19:12 +00001452 if (Body.isInvalid())
1453 Function->setInvalidDecl();
1454
Mike Stump1eb44332009-09-09 15:08:12 +00001455 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001456 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001457
1458 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00001459
1460 DeclGroupRef DG(Function);
1461 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001462
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001463 if (Recursive) {
1464 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001465 // instantiation of this template.
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001466 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001467
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001468 // Restore the set of pending implicit instantiations.
1469 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1470 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001471}
1472
1473/// \brief Instantiate the definition of the given variable from its
1474/// template.
1475///
Douglas Gregor7caa6822009-07-24 20:34:43 +00001476/// \param PointOfInstantiation the point at which the instantiation was
1477/// required. Note that this is not precisely a "point of instantiation"
1478/// for the function, but it's close.
1479///
1480/// \param Var the already-instantiated declaration of a static member
1481/// variable of a class template specialization.
1482///
1483/// \param Recursive if true, recursively instantiates any functions that
1484/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001485///
1486/// \param DefinitionRequired if true, then we are performing an explicit
1487/// instantiation where an out-of-line definition of the member variable
1488/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001489void Sema::InstantiateStaticDataMemberDefinition(
1490 SourceLocation PointOfInstantiation,
1491 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001492 bool Recursive,
1493 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001494 if (Var->isInvalidDecl())
1495 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001496
Douglas Gregor7caa6822009-07-24 20:34:43 +00001497 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001498 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00001499 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00001500 assert(Def->isStaticDataMember() && "Not a static data member?");
1501 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00001502
Douglas Gregor0d035142009-10-27 18:42:08 +00001503 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001504 // We did not find an out-of-line definition of this static data member,
1505 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00001506 // instantiate this definition (or provide a specialization for it) in
1507 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001508 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001509 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001510 Diag(PointOfInstantiation,
1511 diag::err_explicit_instantiation_undefined_member)
1512 << 2 << Var->getDeclName() << Var->getDeclContext();
1513 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
1514 }
1515
Douglas Gregor7caa6822009-07-24 20:34:43 +00001516 return;
1517 }
1518
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001519 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001520 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001521 return;
1522
1523 // C++0x [temp.explicit]p9:
1524 // Except for inline functions, other explicit instantiation declarations
1525 // have the effect of suppressing the implicit instantiation of the entity
1526 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001527 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001528 == TSK_ExplicitInstantiationDeclaration)
1529 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001530
Douglas Gregor7caa6822009-07-24 20:34:43 +00001531 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
1532 if (Inst)
1533 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001534
Douglas Gregor7caa6822009-07-24 20:34:43 +00001535 // If we're performing recursive template instantiation, create our own
1536 // queue of pending implicit instantiations that we will instantiate later,
1537 // while we're still within our own instantiation context.
1538 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1539 if (Recursive)
1540 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001541
Douglas Gregor7caa6822009-07-24 20:34:43 +00001542 // Enter the scope of this instantiation. We don't use
1543 // PushDeclContext because we don't have a scope.
1544 DeclContext *PreviousContext = CurContext;
1545 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001546
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001547 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00001548 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00001549 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00001550 CurContext = PreviousContext;
1551
1552 if (Var) {
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001553 Var->setPreviousDeclaration(OldVar);
Douglas Gregor583f33b2009-10-15 18:07:02 +00001554 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
1555 assert(MSInfo && "Missing member specialization information?");
1556 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
1557 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00001558 DeclGroupRef DG(Var);
1559 Consumer.HandleTopLevelDecl(DG);
1560 }
Mike Stump1eb44332009-09-09 15:08:12 +00001561
Douglas Gregor7caa6822009-07-24 20:34:43 +00001562 if (Recursive) {
1563 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001564 // instantiation of this template.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001565 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001566
Douglas Gregor7caa6822009-07-24 20:34:43 +00001567 // Restore the set of pending implicit instantiations.
1568 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001569 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001570}
Douglas Gregor815215d2009-05-27 05:35:12 +00001571
Anders Carlsson09025312009-08-29 05:16:22 +00001572void
1573Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
1574 const CXXConstructorDecl *Tmpl,
1575 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001576
Anders Carlsson09025312009-08-29 05:16:22 +00001577 llvm::SmallVector<MemInitTy*, 4> NewInits;
1578
1579 // Instantiate all the initializers.
1580 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00001581 InitsEnd = Tmpl->init_end();
1582 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00001583 CXXBaseOrMemberInitializer *Init = *Inits;
1584
1585 ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001586
Anders Carlsson09025312009-08-29 05:16:22 +00001587 // Instantiate all the arguments.
1588 for (ExprIterator Args = Init->arg_begin(), ArgsEnd = Init->arg_end();
1589 Args != ArgsEnd; ++Args) {
1590 OwningExprResult NewArg = SubstExpr(*Args, TemplateArgs);
1591
1592 if (NewArg.isInvalid())
1593 New->setInvalidDecl();
1594 else
1595 NewArgs.push_back(NewArg.takeAs<Expr>());
1596 }
1597
1598 MemInitResult NewInit;
1599
1600 if (Init->isBaseInitializer()) {
Eli Friedmanc5573a82009-08-29 22:22:07 +00001601 QualType BaseType(Init->getBaseClass(), 0);
1602 BaseType = SubstType(BaseType, TemplateArgs, Init->getSourceLocation(),
1603 New->getDeclName());
Anders Carlsson09025312009-08-29 05:16:22 +00001604
1605 NewInit = BuildBaseInitializer(BaseType,
Mike Stump1eb44332009-09-09 15:08:12 +00001606 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001607 NewArgs.size(),
1608 Init->getSourceLocation(),
1609 Init->getRParenLoc(),
1610 New->getParent());
1611 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001612 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00001613
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001614 // Is this an anonymous union?
1615 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregore95b4092009-09-16 18:34:49 +00001616 Member = cast<FieldDecl>(FindInstantiatedDecl(UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001617 else
Douglas Gregore95b4092009-09-16 18:34:49 +00001618 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMember(),
1619 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00001620
1621 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001622 NewArgs.size(),
1623 Init->getSourceLocation(),
1624 Init->getRParenLoc());
1625 }
1626
1627 if (NewInit.isInvalid())
1628 New->setInvalidDecl();
1629 else {
1630 // FIXME: It would be nice if ASTOwningVector had a release function.
1631 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001632
Anders Carlsson09025312009-08-29 05:16:22 +00001633 NewInits.push_back((MemInitTy *)NewInit.get());
1634 }
1635 }
Mike Stump1eb44332009-09-09 15:08:12 +00001636
Anders Carlsson09025312009-08-29 05:16:22 +00001637 // Assign all the initializers to the new constructor.
Mike Stump1eb44332009-09-09 15:08:12 +00001638 ActOnMemInitializers(DeclPtrTy::make(New),
Anders Carlsson09025312009-08-29 05:16:22 +00001639 /*FIXME: ColonLoc */
1640 SourceLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001641 NewInits.data(), NewInits.size());
Anders Carlsson09025312009-08-29 05:16:22 +00001642}
1643
John McCall52a575a2009-08-29 08:11:13 +00001644// TODO: this could be templated if the various decl types used the
1645// same method name.
1646static bool isInstantiationOf(ClassTemplateDecl *Pattern,
1647 ClassTemplateDecl *Instance) {
1648 Pattern = Pattern->getCanonicalDecl();
1649
1650 do {
1651 Instance = Instance->getCanonicalDecl();
1652 if (Pattern == Instance) return true;
1653 Instance = Instance->getInstantiatedFromMemberTemplate();
1654 } while (Instance);
1655
1656 return false;
1657}
1658
Douglas Gregor0d696532009-09-28 06:34:35 +00001659static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
1660 FunctionTemplateDecl *Instance) {
1661 Pattern = Pattern->getCanonicalDecl();
1662
1663 do {
1664 Instance = Instance->getCanonicalDecl();
1665 if (Pattern == Instance) return true;
1666 Instance = Instance->getInstantiatedFromMemberTemplate();
1667 } while (Instance);
1668
1669 return false;
1670}
1671
Douglas Gregored9c0f92009-10-29 00:04:11 +00001672static bool
1673isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
1674 ClassTemplatePartialSpecializationDecl *Instance) {
1675 Pattern
1676 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
1677 do {
1678 Instance = cast<ClassTemplatePartialSpecializationDecl>(
1679 Instance->getCanonicalDecl());
1680 if (Pattern == Instance)
1681 return true;
1682 Instance = Instance->getInstantiatedFromMember();
1683 } while (Instance);
1684
1685 return false;
1686}
1687
John McCall52a575a2009-08-29 08:11:13 +00001688static bool isInstantiationOf(CXXRecordDecl *Pattern,
1689 CXXRecordDecl *Instance) {
1690 Pattern = Pattern->getCanonicalDecl();
1691
1692 do {
1693 Instance = Instance->getCanonicalDecl();
1694 if (Pattern == Instance) return true;
1695 Instance = Instance->getInstantiatedFromMemberClass();
1696 } while (Instance);
1697
1698 return false;
1699}
1700
1701static bool isInstantiationOf(FunctionDecl *Pattern,
1702 FunctionDecl *Instance) {
1703 Pattern = Pattern->getCanonicalDecl();
1704
1705 do {
1706 Instance = Instance->getCanonicalDecl();
1707 if (Pattern == Instance) return true;
1708 Instance = Instance->getInstantiatedFromMemberFunction();
1709 } while (Instance);
1710
1711 return false;
1712}
1713
1714static bool isInstantiationOf(EnumDecl *Pattern,
1715 EnumDecl *Instance) {
1716 Pattern = Pattern->getCanonicalDecl();
1717
1718 do {
1719 Instance = Instance->getCanonicalDecl();
1720 if (Pattern == Instance) return true;
1721 Instance = Instance->getInstantiatedFromMemberEnum();
1722 } while (Instance);
1723
1724 return false;
1725}
1726
Anders Carlsson0d8df782009-08-29 19:37:28 +00001727static bool isInstantiationOf(UnresolvedUsingDecl *Pattern,
1728 UsingDecl *Instance,
1729 ASTContext &C) {
1730 return C.getInstantiatedFromUnresolvedUsingDecl(Instance) == Pattern;
1731}
1732
John McCall52a575a2009-08-29 08:11:13 +00001733static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
1734 VarDecl *Instance) {
1735 assert(Instance->isStaticDataMember());
1736
1737 Pattern = Pattern->getCanonicalDecl();
1738
1739 do {
1740 Instance = Instance->getCanonicalDecl();
1741 if (Pattern == Instance) return true;
1742 Instance = Instance->getInstantiatedFromStaticDataMember();
1743 } while (Instance);
1744
1745 return false;
1746}
1747
Douglas Gregor815215d2009-05-27 05:35:12 +00001748static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00001749 if (D->getKind() != Other->getKind()) {
1750 if (UnresolvedUsingDecl *UUD = dyn_cast<UnresolvedUsingDecl>(D)) {
1751 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
1752 return isInstantiationOf(UUD, UD, Ctx);
1753 }
1754 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001755
Anders Carlsson0d8df782009-08-29 19:37:28 +00001756 return false;
1757 }
Mike Stump1eb44332009-09-09 15:08:12 +00001758
John McCall52a575a2009-08-29 08:11:13 +00001759 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
1760 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001761
John McCall52a575a2009-08-29 08:11:13 +00001762 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
1763 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00001764
John McCall52a575a2009-08-29 08:11:13 +00001765 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
1766 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00001767
Douglas Gregor7caa6822009-07-24 20:34:43 +00001768 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00001769 if (Var->isStaticDataMember())
1770 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
1771
1772 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
1773 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00001774
Douglas Gregor0d696532009-09-28 06:34:35 +00001775 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
1776 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
1777
Douglas Gregored9c0f92009-10-29 00:04:11 +00001778 if (ClassTemplatePartialSpecializationDecl *PartialSpec
1779 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
1780 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
1781 PartialSpec);
1782
Anders Carlssond8b285f2009-09-01 04:26:58 +00001783 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
1784 if (!Field->getDeclName()) {
1785 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00001786 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00001787 cast<FieldDecl>(D);
1788 }
1789 }
Mike Stump1eb44332009-09-09 15:08:12 +00001790
Douglas Gregor815215d2009-05-27 05:35:12 +00001791 return D->getDeclName() && isa<NamedDecl>(Other) &&
1792 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
1793}
1794
1795template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00001796static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00001797 NamedDecl *D,
1798 ForwardIterator first,
1799 ForwardIterator last) {
1800 for (; first != last; ++first)
1801 if (isInstantiationOf(Ctx, D, *first))
1802 return cast<NamedDecl>(*first);
1803
1804 return 0;
1805}
1806
John McCall02cace72009-08-28 07:59:38 +00001807/// \brief Finds the instantiation of the given declaration context
1808/// within the current instantiation.
1809///
1810/// \returns NULL if there was an error
Douglas Gregore95b4092009-09-16 18:34:49 +00001811DeclContext *Sema::FindInstantiatedContext(DeclContext* DC,
1812 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00001813 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00001814 Decl* ID = FindInstantiatedDecl(D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00001815 return cast_or_null<DeclContext>(ID);
1816 } else return DC;
1817}
1818
Douglas Gregored961e72009-05-27 17:54:46 +00001819/// \brief Find the instantiation of the given declaration within the
1820/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00001821///
1822/// This routine is intended to be used when \p D is a declaration
1823/// referenced from within a template, that needs to mapped into the
1824/// corresponding declaration within an instantiation. For example,
1825/// given:
1826///
1827/// \code
1828/// template<typename T>
1829/// struct X {
1830/// enum Kind {
1831/// KnownValue = sizeof(T)
1832/// };
1833///
1834/// bool getKind() const { return KnownValue; }
1835/// };
1836///
1837/// template struct X<int>;
1838/// \endcode
1839///
1840/// In the instantiation of X<int>::getKind(), we need to map the
1841/// EnumConstantDecl for KnownValue (which refers to
1842/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00001843/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
1844/// this mapping from within the instantiation of X<int>.
Douglas Gregore95b4092009-09-16 18:34:49 +00001845NamedDecl *Sema::FindInstantiatedDecl(NamedDecl *D,
1846 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor44c73842009-09-01 17:53:10 +00001847 if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D)) {
1848 // Transform all of the elements of the overloaded function set.
Mike Stump1eb44332009-09-09 15:08:12 +00001849 OverloadedFunctionDecl *Result
Douglas Gregor44c73842009-09-01 17:53:10 +00001850 = OverloadedFunctionDecl::Create(Context, CurContext, Ovl->getDeclName());
Mike Stump1eb44332009-09-09 15:08:12 +00001851
Douglas Gregor44c73842009-09-01 17:53:10 +00001852 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
1853 FEnd = Ovl->function_end();
1854 F != FEnd; ++F) {
1855 Result->addOverload(
Douglas Gregore95b4092009-09-16 18:34:49 +00001856 AnyFunctionDecl::getFromNamedDecl(FindInstantiatedDecl(*F,
1857 TemplateArgs)));
Douglas Gregor44c73842009-09-01 17:53:10 +00001858 }
Mike Stump1eb44332009-09-09 15:08:12 +00001859
Douglas Gregor44c73842009-09-01 17:53:10 +00001860 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001861 }
1862
Douglas Gregor815215d2009-05-27 05:35:12 +00001863 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00001864 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
1865 isa<TemplateTypeParmDecl>(D) || isa<TemplateTypeParmDecl>(D) ||
1866 ParentDC->isFunctionOrMethod()) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00001867 // D is a local of some kind. Look into the map of local
1868 // declarations to their instantiations.
1869 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
1870 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001871
Douglas Gregore95b4092009-09-16 18:34:49 +00001872 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
1873 if (!Record->isDependentContext())
1874 return D;
1875
1876 // If the RecordDecl is actually the injected-class-name or a "templated"
1877 // declaration for a class template or class template partial
1878 // specialization, substitute into the injected-class-name of the
1879 // class template or partial specialization to find the new DeclContext.
1880 QualType T;
1881 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
1882
1883 if (ClassTemplate) {
1884 T = ClassTemplate->getInjectedClassNameType(Context);
1885 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
1886 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1887 T = Context.getTypeDeclType(Record);
1888 ClassTemplate = PartialSpec->getSpecializedTemplate();
1889 }
1890
1891 if (!T.isNull()) {
1892 // Substitute into the injected-class-name to get the type corresponding
1893 // to the instantiation we want. This substitution should never fail,
1894 // since we know we can instantiate the injected-class-name or we wouldn't
1895 // have gotten to the injected-class-name!
1896 // FIXME: Can we use the CurrentInstantiationScope to avoid this extra
1897 // instantiation in the common case?
1898 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
1899 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
1900
1901 if (!T->isDependentType()) {
1902 assert(T->isRecordType() && "Instantiation must produce a record type");
1903 return T->getAs<RecordType>()->getDecl();
1904 }
1905
1906 // We are performing "partial" template instantiation to create the
1907 // member declarations for the members of a class template
1908 // specialization. Therefore, D is actually referring to something in
1909 // the current instantiation. Look through the current context,
1910 // which contains actual instantiations, to find the instantiation of
1911 // the "current instantiation" that D refers to.
Mike Stump1eb44332009-09-09 15:08:12 +00001912 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00001913 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001914 if (ClassTemplateSpecializationDecl *Spec
John McCall52a575a2009-08-29 08:11:13 +00001915 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00001916 if (isInstantiationOf(ClassTemplate,
1917 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00001918 return Spec;
1919 }
1920
Mike Stump1eb44332009-09-09 15:08:12 +00001921 assert(false &&
John McCall52a575a2009-08-29 08:11:13 +00001922 "Unable to find declaration for the current instantiation");
Douglas Gregore95b4092009-09-16 18:34:49 +00001923 return Record;
John McCall52a575a2009-08-29 08:11:13 +00001924 }
Douglas Gregore95b4092009-09-16 18:34:49 +00001925
1926 // Fall through to deal with other dependent record types (e.g.,
1927 // anonymous unions in class templates).
1928 }
John McCall52a575a2009-08-29 08:11:13 +00001929
Douglas Gregore95b4092009-09-16 18:34:49 +00001930 if (!ParentDC->isDependentContext())
1931 return D;
1932
1933 ParentDC = FindInstantiatedContext(ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001934 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00001935 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001936
Douglas Gregor815215d2009-05-27 05:35:12 +00001937 if (ParentDC != D->getDeclContext()) {
1938 // We performed some kind of instantiation in the parent context,
1939 // so now we need to look into the instantiated parent context to
1940 // find the instantiation of the declaration D.
1941 NamedDecl *Result = 0;
1942 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001943 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00001944 Result = findInstantiationOf(Context, D, Found.first, Found.second);
1945 } else {
1946 // Since we don't have a name for the entity we're looking for,
1947 // our only option is to walk through all of the declarations to
1948 // find that name. This will occur in a few cases:
1949 //
1950 // - anonymous struct/union within a template
1951 // - unnamed class/struct/union/enum within a template
1952 //
1953 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00001954 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001955 ParentDC->decls_begin(),
1956 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00001957 }
Mike Stump1eb44332009-09-09 15:08:12 +00001958
Douglas Gregor815215d2009-05-27 05:35:12 +00001959 assert(Result && "Unable to find instantiation of declaration!");
1960 D = Result;
1961 }
1962
Douglas Gregor815215d2009-05-27 05:35:12 +00001963 return D;
1964}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001965
Mike Stump1eb44332009-09-09 15:08:12 +00001966/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001967/// instantiations we have seen until this point.
1968void Sema::PerformPendingImplicitInstantiations() {
1969 while (!PendingImplicitInstantiations.empty()) {
1970 PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front();
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001971 PendingImplicitInstantiations.pop_front();
Mike Stump1eb44332009-09-09 15:08:12 +00001972
Douglas Gregor7caa6822009-07-24 20:34:43 +00001973 // Instantiate function definitions
1974 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001975 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001976 Function->getLocation(), *this,
1977 Context.getSourceManager(),
1978 "instantiating function definition");
Mike Stump1eb44332009-09-09 15:08:12 +00001979
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001980 if (!Function->getBody())
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001981 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001982 continue;
1983 }
Mike Stump1eb44332009-09-09 15:08:12 +00001984
Douglas Gregor7caa6822009-07-24 20:34:43 +00001985 // Instantiate static data member definitions.
1986 VarDecl *Var = cast<VarDecl>(Inst.first);
1987 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001988
Mike Stump1eb44332009-09-09 15:08:12 +00001989 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001990 Var->getLocation(), *this,
1991 Context.getSourceManager(),
1992 "instantiating static data member "
1993 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00001994
Douglas Gregor7caa6822009-07-24 20:34:43 +00001995 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001996 }
1997}