blob: 00f148df6e6b2e0ff384de0ddff7c7182a1acc0c [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
Douglas Gregor8dbc2692009-03-17 21:15:40 +000031 public:
32 typedef Sema::OwningExprResult OwningExprResult;
33
34 TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +000035 const MultiLevelTemplateArgumentList &TemplateArgs)
Douglas Gregor7e063902009-05-11 23:53:27 +000036 : SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs) { }
Mike Stump1eb44332009-09-09 15:08:12 +000037
Mike Stump390b4cc2009-05-16 07:39:55 +000038 // FIXME: Once we get closer to completion, replace these manually-written
39 // declarations with automatically-generated ones from
40 // clang/AST/DeclNodes.def.
Douglas Gregor4f722be2009-03-25 15:45:12 +000041 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
42 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000043 Decl *VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +000044 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000045 Decl *VisitFieldDecl(FieldDecl *D);
46 Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
47 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor6477b692009-03-25 15:04:13 +000048 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
John McCall02cace72009-08-28 07:59:38 +000049 Decl *VisitFriendDecl(FriendDecl *D);
Douglas Gregora735b202009-10-13 14:39:41 +000050 Decl *VisitFunctionDecl(FunctionDecl *D,
51 TemplateParameterList *TemplateParams = 0);
Douglas Gregord475b8d2009-03-25 21:17:03 +000052 Decl *VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregord60e1052009-08-27 16:57:43 +000053 Decl *VisitCXXMethodDecl(CXXMethodDecl *D,
54 TemplateParameterList *TemplateParams = 0);
Douglas Gregor615c5d42009-03-24 16:43:20 +000055 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
Douglas Gregor03b2b072009-03-24 00:15:49 +000056 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +000057 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor6477b692009-03-25 15:04:13 +000058 ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D);
John McCalle29ba202009-08-20 01:44:21 +000059 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregor7974c3b2009-10-07 17:21:34 +000060 Decl *VisitClassTemplatePartialSpecializationDecl(
61 ClassTemplatePartialSpecializationDecl *D);
Douglas Gregord60e1052009-08-27 16:57:43 +000062 Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
John McCalle29ba202009-08-20 01:44:21 +000063 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Anders Carlsson0dde18e2009-08-28 15:18:15 +000064 Decl *VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D);
Mike Stump1eb44332009-09-09 15:08:12 +000065
Douglas Gregor8dbc2692009-03-17 21:15:40 +000066 // Base case. FIXME: Remove once we can instantiate everything.
Mike Stump1eb44332009-09-09 15:08:12 +000067 Decl *VisitDecl(Decl *) {
Douglas Gregor3d7a12a2009-03-25 23:32:15 +000068 assert(false && "Template instantiation of unknown declaration kind!");
Douglas Gregor8dbc2692009-03-17 21:15:40 +000069 return 0;
70 }
Douglas Gregor5545e162009-03-24 00:38:23 +000071
John McCallfd810b12009-08-14 02:03:10 +000072 const LangOptions &getLangOptions() {
73 return SemaRef.getLangOptions();
74 }
75
Douglas Gregor5545e162009-03-24 00:38:23 +000076 // Helper functions for instantiating methods.
John McCallce3ff2b2009-08-25 22:02:44 +000077 QualType SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +000078 llvm::SmallVectorImpl<ParmVarDecl *> &Params);
Douglas Gregore53060f2009-06-25 22:08:12 +000079 bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
Douglas Gregor5545e162009-03-24 00:38:23 +000080 bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
John McCalle29ba202009-08-20 01:44:21 +000081
82 TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +000083 SubstTemplateParams(TemplateParameterList *List);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000084 };
85}
86
Douglas Gregor4f722be2009-03-25 15:45:12 +000087Decl *
88TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
89 assert(false && "Translation units cannot be instantiated");
90 return D;
91}
92
93Decl *
94TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
95 assert(false && "Namespaces cannot be instantiated");
96 return D;
97}
98
Douglas Gregor8dbc2692009-03-17 21:15:40 +000099Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
100 bool Invalid = false;
101 QualType T = D->getUnderlyingType();
102 if (T->isDependentType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000103 T = SemaRef.SubstType(T, TemplateArgs,
John McCallce3ff2b2009-08-25 22:02:44 +0000104 D->getLocation(), D->getDeclName());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000105 if (T.isNull()) {
106 Invalid = true;
107 T = SemaRef.Context.IntTy;
108 }
109 }
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000111 // Create the new typedef
112 TypedefDecl *Typedef
113 = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
114 D->getIdentifier(), T);
115 if (Invalid)
116 Typedef->setInvalidDecl();
117
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000118 Owner->addDecl(Typedef);
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000120 return Typedef;
121}
122
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000123Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000124 // Do substitution on the type of the declaration
John McCall0a5fa062009-10-21 02:39:02 +0000125 DeclaratorInfo *DI = SemaRef.SubstType(D->getDeclaratorInfo(),
126 TemplateArgs,
127 D->getTypeSpecStartLoc(),
128 D->getDeclName());
129 if (!DI)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000130 return 0;
131
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000132 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000133 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
134 D->getLocation(), D->getIdentifier(),
John McCall0a5fa062009-10-21 02:39:02 +0000135 DI->getType(), DI,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000136 D->getStorageClass());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000137 Var->setThreadSpecified(D->isThreadSpecified());
138 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
139 Var->setDeclaredInCondition(D->isDeclaredInCondition());
Mike Stump1eb44332009-09-09 15:08:12 +0000140
141 // If we are instantiating a static data member defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000142 // out-of-line, the instantiation will have the same lexical
143 // context (which will be a namespace scope) as the template.
144 if (D->isOutOfLine())
145 Var->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000146
Mike Stump390b4cc2009-05-16 07:39:55 +0000147 // FIXME: In theory, we could have a previous declaration for variables that
148 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000149 bool Redeclaration = false;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000150 SemaRef.CheckVariableDeclaration(Var, 0, Redeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Douglas Gregor7caa6822009-07-24 20:34:43 +0000152 if (D->isOutOfLine()) {
153 D->getLexicalDeclContext()->addDecl(Var);
154 Owner->makeDeclVisibleInContext(Var);
155 } else {
156 Owner->addDecl(Var);
157 }
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000159 // Link instantiations of static data members back to the template from
160 // which they were instantiated.
161 if (Var->isStaticDataMember())
162 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
163 TSK_ImplicitInstantiation);
164
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000165 if (D->getInit()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000166 OwningExprResult Init
John McCallce3ff2b2009-08-25 22:02:44 +0000167 = SemaRef.SubstExpr(D->getInit(), TemplateArgs);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000168 if (Init.isInvalid())
169 Var->setInvalidDecl();
Douglas Gregor83ddad32009-08-26 21:14:46 +0000170 else if (ParenListExpr *PLE = dyn_cast<ParenListExpr>((Expr *)Init.get())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000171 // FIXME: We're faking all of the comma locations, which is suboptimal.
Douglas Gregor83ddad32009-08-26 21:14:46 +0000172 // Do we even need these comma locations?
173 llvm::SmallVector<SourceLocation, 4> FakeCommaLocs;
174 if (PLE->getNumExprs() > 0) {
175 FakeCommaLocs.reserve(PLE->getNumExprs() - 1);
176 for (unsigned I = 0, N = PLE->getNumExprs() - 1; I != N; ++I) {
177 Expr *E = PLE->getExpr(I)->Retain();
178 FakeCommaLocs.push_back(
179 SemaRef.PP.getLocForEndOfToken(E->getLocEnd()));
180 }
Douglas Gregore9f8eb62009-08-26 23:26:04 +0000181 PLE->getExpr(PLE->getNumExprs() - 1)->Retain();
Douglas Gregor83ddad32009-08-26 21:14:46 +0000182 }
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Douglas Gregor83ddad32009-08-26 21:14:46 +0000184 // Add the direct initializer to the declaration.
185 SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
Mike Stump1eb44332009-09-09 15:08:12 +0000186 PLE->getLParenLoc(),
Douglas Gregor83ddad32009-08-26 21:14:46 +0000187 Sema::MultiExprArg(SemaRef,
188 (void**)PLE->getExprs(),
189 PLE->getNumExprs()),
190 FakeCommaLocs.data(),
191 PLE->getRParenLoc());
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Douglas Gregor83ddad32009-08-26 21:14:46 +0000193 // When Init is destroyed, it will destroy the instantiated ParenListExpr;
194 // we've explicitly retained all of its subexpressions already.
195 } else
Chris Lattnerb28317a2009-03-28 19:18:32 +0000196 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init),
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000197 D->hasCXXDirectInitializer());
Douglas Gregor65b90052009-07-27 17:43:39 +0000198 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
199 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000200
201 return Var;
202}
203
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000204Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
205 bool Invalid = false;
John McCall07fb6be2009-10-22 23:33:21 +0000206 DeclaratorInfo *DI = D->getDeclaratorInfo();
207 if (DI->getType()->isDependentType()) {
208 DI = SemaRef.SubstType(DI, TemplateArgs,
209 D->getLocation(), D->getDeclName());
210 if (!DI) {
211 DI = D->getDeclaratorInfo();
212 Invalid = true;
213 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000214 // C++ [temp.arg.type]p3:
215 // If a declaration acquires a function type through a type
216 // dependent on a template-parameter and this causes a
217 // declaration that does not use the syntactic form of a
218 // function declarator to have function type, the program is
219 // ill-formed.
220 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000221 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000222 Invalid = true;
223 }
224 }
225
226 Expr *BitWidth = D->getBitWidth();
227 if (Invalid)
228 BitWidth = 0;
229 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000230 // The bit-width expression is not potentially evaluated.
231 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000232
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000233 OwningExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000234 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000235 if (InstantiatedBitWidth.isInvalid()) {
236 Invalid = true;
237 BitWidth = 0;
238 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000239 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000240 }
241
John McCall07fb6be2009-10-22 23:33:21 +0000242 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
243 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000244 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000245 D->getLocation(),
246 D->isMutable(),
247 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000248 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000249 D->getAccess(),
250 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000251 if (!Field) {
252 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000253 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000254 }
Mike Stump1eb44332009-09-09 15:08:12 +0000255
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000256 if (Invalid)
257 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000259 if (!Field->getDeclName()) {
260 // Keep track of where this decl came from.
261 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000262 }
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000264 Field->setImplicit(D->isImplicit());
265 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000266
267 return Field;
268}
269
John McCall02cace72009-08-28 07:59:38 +0000270Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
271 FriendDecl::FriendUnion FU;
272
273 // Handle friend type expressions by simply substituting template
274 // parameters into the pattern type.
275 if (Type *Ty = D->getFriendType()) {
276 QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs,
277 D->getLocation(), DeclarationName());
278 if (T.isNull()) return 0;
279
280 assert(getLangOptions().CPlusPlus0x || T->isRecordType());
281 FU = T.getTypePtr();
282
283 // Handle everything else by appropriate substitution.
284 } else {
285 NamedDecl *ND = D->getFriendDecl();
286 assert(ND && "friend decl must be a decl or a type!");
287
Douglas Gregora735b202009-10-13 14:39:41 +0000288 // FIXME: We have a problem here, because the nested call to Visit(ND)
289 // will inject the thing that the friend references into the current
290 // owner, which is wrong.
John McCall02cace72009-08-28 07:59:38 +0000291 Decl *NewND = Visit(ND);
292 if (!NewND) return 0;
293
294 FU = cast<NamedDecl>(NewND);
John McCallfd810b12009-08-14 02:03:10 +0000295 }
Mike Stump1eb44332009-09-09 15:08:12 +0000296
John McCall02cace72009-08-28 07:59:38 +0000297 FriendDecl *FD =
298 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU,
299 D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000300 FD->setAccess(AS_public);
John McCall02cace72009-08-28 07:59:38 +0000301 Owner->addDecl(FD);
302 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000303}
304
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000305Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
306 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Douglas Gregorac7610d2009-06-22 20:57:11 +0000308 // The expression in a static assertion is not potentially evaluated.
309 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000310
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000311 OwningExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000312 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000313 if (InstantiatedAssertExpr.isInvalid())
314 return 0;
315
Douglas Gregor43d9d922009-08-08 01:41:12 +0000316 OwningExprResult Message(SemaRef, D->getMessage());
317 D->getMessage()->Retain();
Mike Stump1eb44332009-09-09 15:08:12 +0000318 Decl *StaticAssert
319 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
Chris Lattnerb28317a2009-03-28 19:18:32 +0000320 move(InstantiatedAssertExpr),
321 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000322 return StaticAssert;
323}
324
325Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000326 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000327 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000328 D->getTagKeywordLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000329 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000330 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000331 Enum->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000332 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000333 Enum->startDefinition();
334
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000335 llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000336
337 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000338 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
339 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000340 EC != ECEnd; ++EC) {
341 // The specified value for the enumerator.
342 OwningExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000343 if (Expr *UninstValue = EC->getInitExpr()) {
344 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000345 EnterExpressionEvaluationContext Unevaluated(SemaRef,
Douglas Gregorac7610d2009-06-22 20:57:11 +0000346 Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000347
John McCallce3ff2b2009-08-25 22:02:44 +0000348 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000349 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000350
351 // Drop the initial value and continue.
352 bool isInvalid = false;
353 if (Value.isInvalid()) {
354 Value = SemaRef.Owned((Expr *)0);
355 isInvalid = true;
356 }
357
Mike Stump1eb44332009-09-09 15:08:12 +0000358 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000359 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
360 EC->getLocation(), EC->getIdentifier(),
361 move(Value));
362
363 if (isInvalid) {
364 if (EnumConst)
365 EnumConst->setInvalidDecl();
366 Enum->setInvalidDecl();
367 }
368
369 if (EnumConst) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000370 Enum->addDecl(EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000371 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000372 LastEnumConst = EnumConst;
373 }
374 }
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000376 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000377 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000378 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
379 Sema::DeclPtrTy::make(Enum),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000380 &Enumerators[0], Enumerators.size(),
381 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000382
383 return Enum;
384}
385
Douglas Gregor6477b692009-03-25 15:04:13 +0000386Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
387 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
388 return 0;
389}
390
John McCalle29ba202009-08-20 01:44:21 +0000391Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
392 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000393 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000394 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000395 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000396
397 CXXRecordDecl *Pattern = D->getTemplatedDecl();
398 CXXRecordDecl *RecordInst
399 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner,
400 Pattern->getLocation(), Pattern->getIdentifier(),
Douglas Gregorf0510d42009-10-12 23:11:44 +0000401 Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL,
402 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000403
404 ClassTemplateDecl *Inst
405 = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
406 D->getIdentifier(), InstParams, RecordInst, 0);
407 RecordInst->setDescribedClassTemplate(Inst);
408 Inst->setAccess(D->getAccess());
409 Inst->setInstantiatedFromMemberTemplate(D);
Douglas Gregorf0510d42009-10-12 23:11:44 +0000410
411 // Trigger creation of the type for the instantiation.
412 SemaRef.Context.getTypeDeclType(RecordInst);
413
John McCalle29ba202009-08-20 01:44:21 +0000414 Owner->addDecl(Inst);
415 return Inst;
416}
417
Douglas Gregord60e1052009-08-27 16:57:43 +0000418Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000419TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
420 ClassTemplatePartialSpecializationDecl *D) {
421 assert(false &&"Partial specializations of member templates are unsupported");
422 return 0;
423}
424
425Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000426TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000427 // FIXME: Dig out the out-of-line definition of this function template?
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Douglas Gregord60e1052009-08-27 16:57:43 +0000429 TemplateParameterList *TempParams = D->getTemplateParameters();
430 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000431 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000432 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Douglas Gregora735b202009-10-13 14:39:41 +0000434 FunctionDecl *Instantiated = 0;
435 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
436 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
437 InstParams));
438 else
439 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
440 D->getTemplatedDecl(),
441 InstParams));
442
443 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000444 return 0;
445
Mike Stump1eb44332009-09-09 15:08:12 +0000446 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000447 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000448 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000449 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000450 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000451 assert(InstTemplate &&
452 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
453 if (!InstTemplate->getInstantiatedFromMemberTemplate())
454 InstTemplate->setInstantiatedFromMemberTemplate(D);
455
456 // Add non-friends into the owner.
457 if (!InstTemplate->getFriendObjectKind())
458 Owner->addDecl(InstTemplate);
Douglas Gregord60e1052009-08-27 16:57:43 +0000459 return InstTemplate;
460}
461
Douglas Gregord475b8d2009-03-25 21:17:03 +0000462Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
463 CXXRecordDecl *PrevDecl = 0;
464 if (D->isInjectedClassName())
465 PrevDecl = cast<CXXRecordDecl>(Owner);
466
467 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000468 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000469 D->getLocation(), D->getIdentifier(),
470 D->getTagKeywordLoc(), PrevDecl);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000471 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000472 // FIXME: Check against AS_none is an ugly hack to work around the issue that
473 // the tag decls introduced by friend class declarations don't have an access
474 // specifier. Remove once this area of the code gets sorted out.
475 if (D->getAccess() != AS_none)
476 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000477 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000478 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000479
John McCall02cace72009-08-28 07:59:38 +0000480 // If the original function was part of a friend declaration,
481 // inherit its namespace state.
482 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
483 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
484
Anders Carlssond8b285f2009-09-01 04:26:58 +0000485 Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
486
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000487 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000488 return Record;
489}
490
John McCall02cace72009-08-28 07:59:38 +0000491/// Normal class members are of more specific types and therefore
492/// don't make it here. This function serves two purposes:
493/// 1) instantiating function templates
494/// 2) substituting friend declarations
495/// FIXME: preserve function definitions in case #2
Douglas Gregora735b202009-10-13 14:39:41 +0000496 Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
497 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000498 // Check whether there is already a function template specialization for
499 // this declaration.
500 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
501 void *InsertPos = 0;
Douglas Gregora735b202009-10-13 14:39:41 +0000502 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000503 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000504 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000505 TemplateArgs.getInnermost().getFlatArgumentList(),
506 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor828e2262009-07-29 16:09:57 +0000507 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000508
509 FunctionTemplateSpecializationInfo *Info
510 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor127102b2009-06-29 20:59:39 +0000511 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000512
Douglas Gregor127102b2009-06-29 20:59:39 +0000513 // If we already have a function template specialization, return it.
514 if (Info)
515 return Info->Function;
516 }
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Douglas Gregore53060f2009-06-25 22:08:12 +0000518 Sema::LocalInstantiationScope Scope(SemaRef);
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Douglas Gregore53060f2009-06-25 22:08:12 +0000520 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000521 QualType T = SubstFunctionType(D, Params);
Douglas Gregore53060f2009-06-25 22:08:12 +0000522 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000523 return 0;
John McCallfd810b12009-08-14 02:03:10 +0000524
Douglas Gregore53060f2009-06-25 22:08:12 +0000525 // Build the instantiated method declaration.
Douglas Gregore95b4092009-09-16 18:34:49 +0000526 DeclContext *DC = SemaRef.FindInstantiatedContext(D->getDeclContext(),
527 TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +0000528 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +0000529 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000530 D->getDeclName(), T, D->getDeclaratorInfo(),
531 D->getStorageClass(),
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000532 D->isInline(), D->hasWrittenPrototype());
John McCall02cace72009-08-28 07:59:38 +0000533 Function->setLexicalDeclContext(Owner);
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Douglas Gregore53060f2009-06-25 22:08:12 +0000535 // Attach the parameters
536 for (unsigned P = 0; P < Params.size(); ++P)
537 Params[P]->setOwningFunction(Function);
538 Function->setParams(SemaRef.Context, Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +0000539
Douglas Gregora735b202009-10-13 14:39:41 +0000540 if (TemplateParams) {
541 // Our resulting instantiation is actually a function template, since we
542 // are substituting only the outer template parameters. For example, given
543 //
544 // template<typename T>
545 // struct X {
546 // template<typename U> friend void f(T, U);
547 // };
548 //
549 // X<int> x;
550 //
551 // We are instantiating the friend function template "f" within X<int>,
552 // which means substituting int for T, but leaving "f" as a friend function
553 // template.
554 // Build the function template itself.
555 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Owner,
556 Function->getLocation(),
557 Function->getDeclName(),
558 TemplateParams, Function);
559 Function->setDescribedFunctionTemplate(FunctionTemplate);
560 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
John McCall02cace72009-08-28 07:59:38 +0000561 }
Douglas Gregora735b202009-10-13 14:39:41 +0000562
Douglas Gregore53060f2009-06-25 22:08:12 +0000563 if (InitFunctionInstantiation(Function, D))
564 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000565
Douglas Gregore53060f2009-06-25 22:08:12 +0000566 bool Redeclaration = false;
567 bool OverloadableAttrRequired = false;
Douglas Gregora735b202009-10-13 14:39:41 +0000568
Douglas Gregore53060f2009-06-25 22:08:12 +0000569 NamedDecl *PrevDecl = 0;
Douglas Gregora735b202009-10-13 14:39:41 +0000570 if (TemplateParams || !FunctionTemplate) {
571 // Look only into the namespace where the friend would be declared to
572 // find a previous declaration. This is the innermost enclosing namespace,
573 // as described in ActOnFriendFunctionDecl.
574 Sema::LookupResult R;
575 SemaRef.LookupQualifiedName(R, DC, Function->getDeclName(),
576 Sema::LookupOrdinaryName, true);
577
578 PrevDecl = R.getAsSingleDecl(SemaRef.Context);
579
580 // In C++, the previous declaration we find might be a tag type
581 // (class or enum). In this case, the new declaration will hide the
582 // tag type. Note that this does does not apply if we're declaring a
583 // typedef (C++ [dcl.typedef]p4).
584 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
585 PrevDecl = 0;
586 }
587
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000588 SemaRef.CheckFunctionDeclaration(Function, PrevDecl, false, Redeclaration,
Douglas Gregore53060f2009-06-25 22:08:12 +0000589 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000590
Douglas Gregora735b202009-10-13 14:39:41 +0000591 // If the original function was part of a friend declaration,
592 // inherit its namespace state and add it to the owner.
593 NamedDecl *FromFriendD
594 = TemplateParams? cast<NamedDecl>(D->getDescribedFunctionTemplate()) : D;
595 if (FromFriendD->getFriendObjectKind()) {
596 NamedDecl *ToFriendD = 0;
597 if (TemplateParams) {
598 ToFriendD = cast<NamedDecl>(FunctionTemplate);
599 PrevDecl = FunctionTemplate->getPreviousDeclaration();
600 } else {
601 ToFriendD = Function;
602 PrevDecl = Function->getPreviousDeclaration();
603 }
604 ToFriendD->setObjectOfFriendDecl(PrevDecl != NULL);
605 if (!Owner->isDependentContext() && !PrevDecl)
606 DC->makeDeclVisibleInContext(ToFriendD, /* Recoverable = */ false);
607
608 if (!TemplateParams)
609 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
610 }
611
612 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000613 // Record this function template specialization.
614 Function->setFunctionTemplateSpecialization(SemaRef.Context,
615 FunctionTemplate,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000616 &TemplateArgs.getInnermost(),
Douglas Gregor127102b2009-06-29 20:59:39 +0000617 InsertPos);
John McCallfd810b12009-08-14 02:03:10 +0000618 }
619
Douglas Gregore53060f2009-06-25 22:08:12 +0000620 return Function;
621}
622
Douglas Gregord60e1052009-08-27 16:57:43 +0000623Decl *
624TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
625 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +0000626 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
627 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +0000628 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +0000629 // We are creating a function template specialization from a function
630 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +0000631 // specialization for this particular set of template arguments.
Douglas Gregor6b906862009-08-21 00:16:32 +0000632 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000633 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000634 TemplateArgs.getInnermost().getFlatArgumentList(),
635 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000636 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000637
638 FunctionTemplateSpecializationInfo *Info
639 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor6b906862009-08-21 00:16:32 +0000640 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000641
Douglas Gregor6b906862009-08-21 00:16:32 +0000642 // If we already have a function template specialization, return it.
643 if (Info)
644 return Info->Function;
645 }
646
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000647 Sema::LocalInstantiationScope Scope(SemaRef);
648
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000649 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000650 QualType T = SubstFunctionType(D, Params);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000651 if (T.isNull())
652 return 0;
653
654 // Build the instantiated method declaration.
655 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
Douglas Gregordec06662009-08-21 18:42:58 +0000656 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Douglas Gregordec06662009-08-21 18:42:58 +0000658 DeclarationName Name = D->getDeclName();
Douglas Gregor17e32f32009-08-21 22:43:28 +0000659 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregordec06662009-08-21 18:42:58 +0000660 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
661 Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
662 SemaRef.Context.getCanonicalType(ClassTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000663 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
664 Constructor->getLocation(),
665 Name, T,
Douglas Gregor17e32f32009-08-21 22:43:28 +0000666 Constructor->getDeclaratorInfo(),
Mike Stump1eb44332009-09-09 15:08:12 +0000667 Constructor->isExplicit(),
Douglas Gregor17e32f32009-08-21 22:43:28 +0000668 Constructor->isInline(), false);
669 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
670 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
671 Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
672 SemaRef.Context.getCanonicalType(ClassTy));
673 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
674 Destructor->getLocation(), Name,
675 T, Destructor->isInline(), false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000676 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000677 CanQualType ConvTy
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000678 = SemaRef.Context.getCanonicalType(
John McCall183700f2009-09-21 23:43:11 +0000679 T->getAs<FunctionType>()->getResultType());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000680 Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
681 ConvTy);
682 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
683 Conversion->getLocation(), Name,
684 T, Conversion->getDeclaratorInfo(),
Mike Stump1eb44332009-09-09 15:08:12 +0000685 Conversion->isInline(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000686 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +0000687 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000688 Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
Douglas Gregordec06662009-08-21 18:42:58 +0000689 D->getDeclName(), T, D->getDeclaratorInfo(),
690 D->isStatic(), D->isInline());
691 }
Douglas Gregor6b906862009-08-21 00:16:32 +0000692
Douglas Gregord60e1052009-08-27 16:57:43 +0000693 if (TemplateParams) {
694 // Our resulting instantiation is actually a function template, since we
695 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +0000696 //
Douglas Gregord60e1052009-08-27 16:57:43 +0000697 // template<typename T>
698 // struct X {
699 // template<typename U> void f(T, U);
700 // };
701 //
702 // X<int> x;
703 //
704 // We are instantiating the member template "f" within X<int>, which means
705 // substituting int for T, but leaving "f" as a member function template.
706 // Build the function template itself.
707 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
708 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +0000709 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +0000710 TemplateParams, Method);
711 if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +0000712 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +0000713 Method->setDescribedFunctionTemplate(FunctionTemplate);
714 } else if (!FunctionTemplate)
Douglas Gregor2db32322009-10-07 23:56:10 +0000715 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000716
Mike Stump1eb44332009-09-09 15:08:12 +0000717 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000718 // out-of-line, the instantiation will have the same lexical
719 // context (which will be a namespace scope) as the template.
720 if (D->isOutOfLine())
721 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000722
Douglas Gregor5545e162009-03-24 00:38:23 +0000723 // Attach the parameters
724 for (unsigned P = 0; P < Params.size(); ++P)
725 Params[P]->setOwningFunction(Method);
Jay Foadbeaaccd2009-05-21 09:52:38 +0000726 Method->setParams(SemaRef.Context, Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +0000727
728 if (InitMethodInstantiation(Method, D))
729 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000730
Douglas Gregordec06662009-08-21 18:42:58 +0000731 NamedDecl *PrevDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000732
Douglas Gregord60e1052009-08-27 16:57:43 +0000733 if (!FunctionTemplate || TemplateParams) {
John McCallf36e02d2009-10-09 21:13:30 +0000734 Sema::LookupResult R;
735 SemaRef.LookupQualifiedName(R, Owner, Name, Sema::LookupOrdinaryName, true);
736 PrevDecl = R.getAsSingleDecl(SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000737
Douglas Gregordec06662009-08-21 18:42:58 +0000738 // In C++, the previous declaration we find might be a tag type
739 // (class or enum). In this case, the new declaration will hide the
740 // tag type. Note that this does does not apply if we're declaring a
741 // typedef (C++ [dcl.typedef]p4).
742 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
743 PrevDecl = 0;
744 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000745
Douglas Gregord60e1052009-08-27 16:57:43 +0000746 if (FunctionTemplate && !TemplateParams)
Douglas Gregor6b906862009-08-21 00:16:32 +0000747 // Record this function template specialization.
748 Method->setFunctionTemplateSpecialization(SemaRef.Context,
749 FunctionTemplate,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000750 &TemplateArgs.getInnermost(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000751 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000752
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000753 bool Redeclaration = false;
754 bool OverloadableAttrRequired = false;
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000755 SemaRef.CheckFunctionDeclaration(Method, PrevDecl, false, Redeclaration,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000756 /*FIXME:*/OverloadableAttrRequired);
757
Douglas Gregora735b202009-10-13 14:39:41 +0000758 if (!FunctionTemplate && (!Method->isInvalidDecl() || !PrevDecl) &&
759 !Method->getFriendObjectKind())
Douglas Gregordec06662009-08-21 18:42:58 +0000760 Owner->addDecl(Method);
Mike Stump1eb44332009-09-09 15:08:12 +0000761
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000762 return Method;
763}
764
Douglas Gregor615c5d42009-03-24 16:43:20 +0000765Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +0000766 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000767}
768
Douglas Gregor03b2b072009-03-24 00:15:49 +0000769Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +0000770 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +0000771}
772
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000773Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000774 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000775}
776
Douglas Gregor6477b692009-03-25 15:04:13 +0000777ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
John McCall58e46772009-10-23 21:48:59 +0000778 QualType T;
779 DeclaratorInfo *DI = D->getDeclaratorInfo();
780 if (DI) {
781 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
782 D->getDeclName());
783 if (DI) T = DI->getType();
784 } else {
785 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
786 D->getDeclName());
787 DI = 0;
788 }
789
790 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000791 return 0;
792
John McCall58e46772009-10-23 21:48:59 +0000793 T = SemaRef.adjustParameterType(T);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000794
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000795 // Allocate the parameter
John McCall58e46772009-10-23 21:48:59 +0000796 ParmVarDecl *Param
797 = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
798 D->getIdentifier(), T, DI, D->getStorageClass(), 0);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000799
Anders Carlsson9351c172009-08-25 03:18:48 +0000800 // Mark the default argument as being uninstantiated.
Douglas Gregorf43d0b32009-09-25 06:56:31 +0000801 if (D->hasUninstantiatedDefaultArg())
802 Param->setUninstantiatedDefaultArg(D->getUninstantiatedDefaultArg());
Douglas Gregor0ed09302009-09-25 07:03:22 +0000803 else if (Expr *Arg = D->getDefaultArg())
804 Param->setUninstantiatedDefaultArg(Arg);
805
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000806 // Note: we don't try to instantiate function parameters until after
807 // we've instantiated the function's type. Therefore, we don't have
808 // to check for 'void' parameter types here.
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000809 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000810 return Param;
811}
812
John McCalle29ba202009-08-20 01:44:21 +0000813Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
814 TemplateTypeParmDecl *D) {
815 // TODO: don't always clone when decls are refcounted.
816 const Type* T = D->getTypeForDecl();
817 assert(T->isTemplateTypeParmType());
818 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000819
John McCalle29ba202009-08-20 01:44:21 +0000820 TemplateTypeParmDecl *Inst =
821 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
822 TTPT->getDepth(), TTPT->getIndex(),
823 TTPT->getName(),
824 D->wasDeclaredWithTypename(),
825 D->isParameterPack());
826
827 if (D->hasDefaultArgument()) {
828 QualType DefaultPattern = D->getDefaultArgument();
829 QualType DefaultInst
John McCallce3ff2b2009-08-25 22:02:44 +0000830 = SemaRef.SubstType(DefaultPattern, TemplateArgs,
831 D->getDefaultArgumentLoc(),
832 D->getDeclName());
Mike Stump1eb44332009-09-09 15:08:12 +0000833
John McCalle29ba202009-08-20 01:44:21 +0000834 Inst->setDefaultArgument(DefaultInst,
835 D->getDefaultArgumentLoc(),
836 D->defaultArgumentWasInherited() /* preserve? */);
837 }
838
839 return Inst;
840}
841
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000842Decl *
843TemplateDeclInstantiator::VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000844 NestedNameSpecifier *NNS =
845 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
846 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000847 TemplateArgs);
848 if (!NNS)
849 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000851 CXXScopeSpec SS;
852 SS.setRange(D->getTargetNestedNameRange());
853 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +0000854
855 NamedDecl *UD =
856 SemaRef.BuildUsingDeclaration(D->getLocation(), SS,
857 D->getTargetNameLocation(),
Anders Carlsson0d8df782009-08-29 19:37:28 +0000858 D->getTargetName(), 0, D->isTypeName());
859 if (UD)
Mike Stump1eb44332009-09-09 15:08:12 +0000860 SemaRef.Context.setInstantiatedFromUnresolvedUsingDecl(cast<UsingDecl>(UD),
Anders Carlsson0d8df782009-08-29 19:37:28 +0000861 D);
862 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000863}
864
John McCallce3ff2b2009-08-25 22:02:44 +0000865Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000866 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +0000867 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000868 return Instantiator.Visit(D);
869}
870
John McCalle29ba202009-08-20 01:44:21 +0000871/// \brief Instantiates a nested template parameter list in the current
872/// instantiation context.
873///
874/// \param L The parameter list to instantiate
875///
876/// \returns NULL if there was an error
877TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +0000878TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +0000879 // Get errors for all the parameters before bailing out.
880 bool Invalid = false;
881
882 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000883 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +0000884 ParamVector Params;
885 Params.reserve(N);
886 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
887 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000888 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +0000889 Params.push_back(D);
890 Invalid = Invalid || !D;
891 }
892
893 // Clean up if we had an error.
894 if (Invalid) {
895 for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
896 PI != PE; ++PI)
897 if (*PI)
898 (*PI)->Destroy(SemaRef.Context);
899 return NULL;
900 }
901
902 TemplateParameterList *InstL
903 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
904 L->getLAngleLoc(), &Params.front(), N,
905 L->getRAngleLoc());
906 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +0000907}
John McCalle29ba202009-08-20 01:44:21 +0000908
John McCallce3ff2b2009-08-25 22:02:44 +0000909/// \brief Does substitution on the type of the given function, including
910/// all of the function parameters.
Douglas Gregor5545e162009-03-24 00:38:23 +0000911///
John McCallce3ff2b2009-08-25 22:02:44 +0000912/// \param D The function whose type will be the basis of the substitution
Douglas Gregor5545e162009-03-24 00:38:23 +0000913///
914/// \param Params the instantiated parameter declarations
915
John McCallce3ff2b2009-08-25 22:02:44 +0000916/// \returns the instantiated function's type if successful, a NULL
Douglas Gregor5545e162009-03-24 00:38:23 +0000917/// type if there was an error.
Mike Stump1eb44332009-09-09 15:08:12 +0000918QualType
John McCallce3ff2b2009-08-25 22:02:44 +0000919TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +0000920 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
921 bool InvalidDecl = false;
922
John McCallce3ff2b2009-08-25 22:02:44 +0000923 // Substitute all of the function's formal parameter types.
Douglas Gregor7e063902009-05-11 23:53:27 +0000924 TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000925 llvm::SmallVector<QualType, 4> ParamTys;
Mike Stump1eb44332009-09-09 15:08:12 +0000926 for (FunctionDecl::param_iterator P = D->param_begin(),
Douglas Gregor5545e162009-03-24 00:38:23 +0000927 PEnd = D->param_end();
928 P != PEnd; ++P) {
Douglas Gregor6477b692009-03-25 15:04:13 +0000929 if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
Douglas Gregor5545e162009-03-24 00:38:23 +0000930 if (PInst->getType()->isVoidType()) {
931 SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
932 PInst->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000933 } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000934 PInst->getType(),
935 diag::err_abstract_type_in_decl,
936 Sema::AbstractParamType))
Douglas Gregor5545e162009-03-24 00:38:23 +0000937 PInst->setInvalidDecl();
938
939 Params.push_back(PInst);
940 ParamTys.push_back(PInst->getType());
941
942 if (PInst->isInvalidDecl())
943 InvalidDecl = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000944 } else
Douglas Gregor5545e162009-03-24 00:38:23 +0000945 InvalidDecl = true;
946 }
947
948 // FIXME: Deallocate dead declarations.
949 if (InvalidDecl)
950 return QualType();
951
John McCall183700f2009-09-21 23:43:11 +0000952 const FunctionProtoType *Proto = D->getType()->getAs<FunctionProtoType>();
Douglas Gregor5545e162009-03-24 00:38:23 +0000953 assert(Proto && "Missing prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +0000954 QualType ResultType
John McCallce3ff2b2009-08-25 22:02:44 +0000955 = SemaRef.SubstType(Proto->getResultType(), TemplateArgs,
956 D->getLocation(), D->getDeclName());
Douglas Gregor5545e162009-03-24 00:38:23 +0000957 if (ResultType.isNull())
958 return QualType();
959
Jay Foadbeaaccd2009-05-21 09:52:38 +0000960 return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
Douglas Gregor5545e162009-03-24 00:38:23 +0000961 Proto->isVariadic(), Proto->getTypeQuals(),
962 D->getLocation(), D->getDeclName());
963}
964
Mike Stump1eb44332009-09-09 15:08:12 +0000965/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +0000966/// declaration (New) from the corresponding fields of its template (Tmpl).
967///
968/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +0000969bool
970TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +0000971 FunctionDecl *Tmpl) {
972 if (Tmpl->isDeleted())
973 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Douglas Gregorcca9e962009-07-01 22:01:06 +0000975 // If we are performing substituting explicitly-specified template arguments
976 // or deduced template arguments into a function template and we reach this
977 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +0000978 // to keeping the new function template specialization. We therefore
979 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +0000980 // into a template instantiation for this specific function template
981 // specialization, which is not a SFINAE context, so that we diagnose any
982 // further errors in the declaration itself.
983 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
984 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
985 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
986 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +0000987 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +0000988 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000989 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +0000990 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +0000991 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +0000992 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
993 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
994 }
995 }
Mike Stump1eb44332009-09-09 15:08:12 +0000996
Douglas Gregore53060f2009-06-25 22:08:12 +0000997 return false;
998}
999
Douglas Gregor5545e162009-03-24 00:38:23 +00001000/// \brief Initializes common fields of an instantiated method
1001/// declaration (New) from the corresponding fields of its template
1002/// (Tmpl).
1003///
1004/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001005bool
1006TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00001007 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001008 if (InitFunctionInstantiation(New, Tmpl))
1009 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001010
Douglas Gregor5545e162009-03-24 00:38:23 +00001011 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
1012 New->setAccess(Tmpl->getAccess());
Anders Carlsson77b7f1d2009-05-14 22:15:41 +00001013 if (Tmpl->isVirtualAsWritten()) {
1014 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +00001015 Record->setAggregate(false);
1016 Record->setPOD(false);
Eli Friedman1d954f62009-08-15 21:55:26 +00001017 Record->setEmpty(false);
Douglas Gregor5545e162009-03-24 00:38:23 +00001018 Record->setPolymorphic(true);
1019 }
Douglas Gregor5545e162009-03-24 00:38:23 +00001020 if (Tmpl->isPure()) {
1021 New->setPure();
1022 Record->setAbstract(true);
1023 }
1024
1025 // FIXME: attributes
1026 // FIXME: New needs a pointer to Tmpl
1027 return false;
1028}
Douglas Gregora58861f2009-05-13 20:28:22 +00001029
1030/// \brief Instantiate the definition of the given function from its
1031/// template.
1032///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001033/// \param PointOfInstantiation the point at which the instantiation was
1034/// required. Note that this is not precisely a "point of instantiation"
1035/// for the function, but it's close.
1036///
Douglas Gregora58861f2009-05-13 20:28:22 +00001037/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001038/// function template specialization or member function of a class template
1039/// specialization.
1040///
1041/// \param Recursive if true, recursively instantiates any functions that
1042/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001043///
1044/// \param DefinitionRequired if true, then we are performing an explicit
1045/// instantiation where the body of the function is required. Complain if
1046/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001047void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001048 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001049 bool Recursive,
1050 bool DefinitionRequired) {
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001051 if (Function->isInvalidDecl())
1052 return;
1053
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001054 assert(!Function->getBody() && "Already instantiated!");
Mike Stump1eb44332009-09-09 15:08:12 +00001055
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001056 // Never instantiate an explicit specialization.
1057 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1058 return;
1059
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001060 // Find the function body that we'll be substituting.
Douglas Gregor1637be72009-06-26 00:10:03 +00001061 const FunctionDecl *PatternDecl = 0;
Douglas Gregor5ec178f2009-08-28 21:09:48 +00001062 if (FunctionTemplateDecl *Primary = Function->getPrimaryTemplate()) {
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001063 while (Primary->getInstantiatedFromMemberTemplate()) {
1064 // If we have hit a point where the user provided a specialization of
1065 // this template, we're done looking.
1066 if (Primary->isMemberSpecialization())
1067 break;
1068
Douglas Gregor5ec178f2009-08-28 21:09:48 +00001069 Primary = Primary->getInstantiatedFromMemberTemplate();
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001070 }
1071
Douglas Gregor1637be72009-06-26 00:10:03 +00001072 PatternDecl = Primary->getTemplatedDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001073 } else
Douglas Gregor1637be72009-06-26 00:10:03 +00001074 PatternDecl = Function->getInstantiatedFromMemberFunction();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001075 Stmt *Pattern = 0;
1076 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001077 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001078
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001079 if (!Pattern) {
1080 if (DefinitionRequired) {
1081 if (Function->getPrimaryTemplate())
1082 Diag(PointOfInstantiation,
1083 diag::err_explicit_instantiation_undefined_func_template)
1084 << Function->getPrimaryTemplate();
1085 else
1086 Diag(PointOfInstantiation,
1087 diag::err_explicit_instantiation_undefined_member)
1088 << 1 << Function->getDeclName() << Function->getDeclContext();
1089
1090 if (PatternDecl)
1091 Diag(PatternDecl->getLocation(),
1092 diag::note_explicit_instantiation_here);
1093 }
1094
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001095 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001096 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001097
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001098 // C++0x [temp.explicit]p9:
1099 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00001100 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001101 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00001102 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001103 == TSK_ExplicitInstantiationDeclaration &&
1104 PatternDecl->isOutOfLine() && !PatternDecl->isInline())
1105 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001106
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001107 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
1108 if (Inst)
1109 return;
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001110
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001111 // If we're performing recursive template instantiation, create our own
1112 // queue of pending implicit instantiations that we will instantiate later,
1113 // while we're still within our own instantiation context.
1114 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1115 if (Recursive)
1116 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001117
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001118 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
1119
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001120 // Introduce a new scope where local variable instantiations will be
1121 // recorded.
1122 LocalInstantiationScope Scope(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001123
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001124 // Introduce the instantiated function parameters into the local
1125 // instantiation scope.
1126 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
1127 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
1128 Function->getParamDecl(I));
1129
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001130 // Enter the scope of this instantiation. We don't use
1131 // PushDeclContext because we don't have a scope.
1132 DeclContext *PreviousContext = CurContext;
1133 CurContext = Function;
1134
Mike Stump1eb44332009-09-09 15:08:12 +00001135 MultiLevelTemplateArgumentList TemplateArgs =
Anders Carlsson09025312009-08-29 05:16:22 +00001136 getTemplateInstantiationArgs(Function);
1137
1138 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001139 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00001140 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
1141 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
1142 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001143 }
1144
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001145 // Instantiate the function body.
Anders Carlsson09025312009-08-29 05:16:22 +00001146 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001147
Douglas Gregor52604ab2009-09-11 21:19:12 +00001148 if (Body.isInvalid())
1149 Function->setInvalidDecl();
1150
Mike Stump1eb44332009-09-09 15:08:12 +00001151 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001152 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001153
1154 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00001155
1156 DeclGroupRef DG(Function);
1157 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001158
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001159 if (Recursive) {
1160 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001161 // instantiation of this template.
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001162 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001163
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001164 // Restore the set of pending implicit instantiations.
1165 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1166 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001167}
1168
1169/// \brief Instantiate the definition of the given variable from its
1170/// template.
1171///
Douglas Gregor7caa6822009-07-24 20:34:43 +00001172/// \param PointOfInstantiation the point at which the instantiation was
1173/// required. Note that this is not precisely a "point of instantiation"
1174/// for the function, but it's close.
1175///
1176/// \param Var the already-instantiated declaration of a static member
1177/// variable of a class template specialization.
1178///
1179/// \param Recursive if true, recursively instantiates any functions that
1180/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001181///
1182/// \param DefinitionRequired if true, then we are performing an explicit
1183/// instantiation where an out-of-line definition of the member variable
1184/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001185void Sema::InstantiateStaticDataMemberDefinition(
1186 SourceLocation PointOfInstantiation,
1187 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001188 bool Recursive,
1189 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001190 if (Var->isInvalidDecl())
1191 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001192
Douglas Gregor7caa6822009-07-24 20:34:43 +00001193 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001194 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
1195 bool FoundOutOfLineDef = false;
1196 assert(Def && "This data member was not instantiated from a template?");
Mike Stump1eb44332009-09-09 15:08:12 +00001197 assert(Def->isStaticDataMember() && "Not a static data member?");
1198 for (VarDecl::redecl_iterator RD = Def->redecls_begin(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00001199 RDEnd = Def->redecls_end();
1200 RD != RDEnd; ++RD) {
1201 if (RD->getLexicalDeclContext()->isFileContext()) {
1202 Def = *RD;
1203 FoundOutOfLineDef = true;
1204 }
1205 }
Mike Stump1eb44332009-09-09 15:08:12 +00001206
Douglas Gregor7caa6822009-07-24 20:34:43 +00001207 if (!FoundOutOfLineDef) {
1208 // We did not find an out-of-line definition of this static data member,
1209 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00001210 // instantiate this definition (or provide a specialization for it) in
1211 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001212 if (DefinitionRequired) {
1213 Diag(PointOfInstantiation,
1214 diag::err_explicit_instantiation_undefined_member)
1215 << 2 << Var->getDeclName() << Var->getDeclContext();
1216 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
1217 }
1218
Douglas Gregor7caa6822009-07-24 20:34:43 +00001219 return;
1220 }
1221
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001222 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001223 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001224 return;
1225
1226 // C++0x [temp.explicit]p9:
1227 // Except for inline functions, other explicit instantiation declarations
1228 // have the effect of suppressing the implicit instantiation of the entity
1229 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001230 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001231 == TSK_ExplicitInstantiationDeclaration)
1232 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001233
Douglas Gregor7caa6822009-07-24 20:34:43 +00001234 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
1235 if (Inst)
1236 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001237
Douglas Gregor7caa6822009-07-24 20:34:43 +00001238 // If we're performing recursive template instantiation, create our own
1239 // queue of pending implicit instantiations that we will instantiate later,
1240 // while we're still within our own instantiation context.
1241 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1242 if (Recursive)
1243 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001244
Douglas Gregor7caa6822009-07-24 20:34:43 +00001245 // Enter the scope of this instantiation. We don't use
1246 // PushDeclContext because we don't have a scope.
1247 DeclContext *PreviousContext = CurContext;
1248 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001249
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001250 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00001251 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00001252 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00001253 CurContext = PreviousContext;
1254
1255 if (Var) {
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001256 Var->setPreviousDeclaration(OldVar);
Douglas Gregor583f33b2009-10-15 18:07:02 +00001257 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
1258 assert(MSInfo && "Missing member specialization information?");
1259 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
1260 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00001261 DeclGroupRef DG(Var);
1262 Consumer.HandleTopLevelDecl(DG);
1263 }
Mike Stump1eb44332009-09-09 15:08:12 +00001264
Douglas Gregor7caa6822009-07-24 20:34:43 +00001265 if (Recursive) {
1266 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001267 // instantiation of this template.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001268 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001269
Douglas Gregor7caa6822009-07-24 20:34:43 +00001270 // Restore the set of pending implicit instantiations.
1271 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001272 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001273}
Douglas Gregor815215d2009-05-27 05:35:12 +00001274
Anders Carlsson09025312009-08-29 05:16:22 +00001275void
1276Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
1277 const CXXConstructorDecl *Tmpl,
1278 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001279
Anders Carlsson09025312009-08-29 05:16:22 +00001280 llvm::SmallVector<MemInitTy*, 4> NewInits;
1281
1282 // Instantiate all the initializers.
1283 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00001284 InitsEnd = Tmpl->init_end();
1285 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00001286 CXXBaseOrMemberInitializer *Init = *Inits;
1287
1288 ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001289
Anders Carlsson09025312009-08-29 05:16:22 +00001290 // Instantiate all the arguments.
1291 for (ExprIterator Args = Init->arg_begin(), ArgsEnd = Init->arg_end();
1292 Args != ArgsEnd; ++Args) {
1293 OwningExprResult NewArg = SubstExpr(*Args, TemplateArgs);
1294
1295 if (NewArg.isInvalid())
1296 New->setInvalidDecl();
1297 else
1298 NewArgs.push_back(NewArg.takeAs<Expr>());
1299 }
1300
1301 MemInitResult NewInit;
1302
1303 if (Init->isBaseInitializer()) {
Eli Friedmanc5573a82009-08-29 22:22:07 +00001304 QualType BaseType(Init->getBaseClass(), 0);
1305 BaseType = SubstType(BaseType, TemplateArgs, Init->getSourceLocation(),
1306 New->getDeclName());
Anders Carlsson09025312009-08-29 05:16:22 +00001307
1308 NewInit = BuildBaseInitializer(BaseType,
Mike Stump1eb44332009-09-09 15:08:12 +00001309 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001310 NewArgs.size(),
1311 Init->getSourceLocation(),
1312 Init->getRParenLoc(),
1313 New->getParent());
1314 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001315 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00001316
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001317 // Is this an anonymous union?
1318 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregore95b4092009-09-16 18:34:49 +00001319 Member = cast<FieldDecl>(FindInstantiatedDecl(UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001320 else
Douglas Gregore95b4092009-09-16 18:34:49 +00001321 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMember(),
1322 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00001323
1324 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001325 NewArgs.size(),
1326 Init->getSourceLocation(),
1327 Init->getRParenLoc());
1328 }
1329
1330 if (NewInit.isInvalid())
1331 New->setInvalidDecl();
1332 else {
1333 // FIXME: It would be nice if ASTOwningVector had a release function.
1334 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001335
Anders Carlsson09025312009-08-29 05:16:22 +00001336 NewInits.push_back((MemInitTy *)NewInit.get());
1337 }
1338 }
Mike Stump1eb44332009-09-09 15:08:12 +00001339
Anders Carlsson09025312009-08-29 05:16:22 +00001340 // Assign all the initializers to the new constructor.
Mike Stump1eb44332009-09-09 15:08:12 +00001341 ActOnMemInitializers(DeclPtrTy::make(New),
Anders Carlsson09025312009-08-29 05:16:22 +00001342 /*FIXME: ColonLoc */
1343 SourceLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001344 NewInits.data(), NewInits.size());
Anders Carlsson09025312009-08-29 05:16:22 +00001345}
1346
John McCall52a575a2009-08-29 08:11:13 +00001347// TODO: this could be templated if the various decl types used the
1348// same method name.
1349static bool isInstantiationOf(ClassTemplateDecl *Pattern,
1350 ClassTemplateDecl *Instance) {
1351 Pattern = Pattern->getCanonicalDecl();
1352
1353 do {
1354 Instance = Instance->getCanonicalDecl();
1355 if (Pattern == Instance) return true;
1356 Instance = Instance->getInstantiatedFromMemberTemplate();
1357 } while (Instance);
1358
1359 return false;
1360}
1361
Douglas Gregor0d696532009-09-28 06:34:35 +00001362static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
1363 FunctionTemplateDecl *Instance) {
1364 Pattern = Pattern->getCanonicalDecl();
1365
1366 do {
1367 Instance = Instance->getCanonicalDecl();
1368 if (Pattern == Instance) return true;
1369 Instance = Instance->getInstantiatedFromMemberTemplate();
1370 } while (Instance);
1371
1372 return false;
1373}
1374
John McCall52a575a2009-08-29 08:11:13 +00001375static bool isInstantiationOf(CXXRecordDecl *Pattern,
1376 CXXRecordDecl *Instance) {
1377 Pattern = Pattern->getCanonicalDecl();
1378
1379 do {
1380 Instance = Instance->getCanonicalDecl();
1381 if (Pattern == Instance) return true;
1382 Instance = Instance->getInstantiatedFromMemberClass();
1383 } while (Instance);
1384
1385 return false;
1386}
1387
1388static bool isInstantiationOf(FunctionDecl *Pattern,
1389 FunctionDecl *Instance) {
1390 Pattern = Pattern->getCanonicalDecl();
1391
1392 do {
1393 Instance = Instance->getCanonicalDecl();
1394 if (Pattern == Instance) return true;
1395 Instance = Instance->getInstantiatedFromMemberFunction();
1396 } while (Instance);
1397
1398 return false;
1399}
1400
1401static bool isInstantiationOf(EnumDecl *Pattern,
1402 EnumDecl *Instance) {
1403 Pattern = Pattern->getCanonicalDecl();
1404
1405 do {
1406 Instance = Instance->getCanonicalDecl();
1407 if (Pattern == Instance) return true;
1408 Instance = Instance->getInstantiatedFromMemberEnum();
1409 } while (Instance);
1410
1411 return false;
1412}
1413
Anders Carlsson0d8df782009-08-29 19:37:28 +00001414static bool isInstantiationOf(UnresolvedUsingDecl *Pattern,
1415 UsingDecl *Instance,
1416 ASTContext &C) {
1417 return C.getInstantiatedFromUnresolvedUsingDecl(Instance) == Pattern;
1418}
1419
John McCall52a575a2009-08-29 08:11:13 +00001420static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
1421 VarDecl *Instance) {
1422 assert(Instance->isStaticDataMember());
1423
1424 Pattern = Pattern->getCanonicalDecl();
1425
1426 do {
1427 Instance = Instance->getCanonicalDecl();
1428 if (Pattern == Instance) return true;
1429 Instance = Instance->getInstantiatedFromStaticDataMember();
1430 } while (Instance);
1431
1432 return false;
1433}
1434
Douglas Gregor815215d2009-05-27 05:35:12 +00001435static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00001436 if (D->getKind() != Other->getKind()) {
1437 if (UnresolvedUsingDecl *UUD = dyn_cast<UnresolvedUsingDecl>(D)) {
1438 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
1439 return isInstantiationOf(UUD, UD, Ctx);
1440 }
1441 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001442
Anders Carlsson0d8df782009-08-29 19:37:28 +00001443 return false;
1444 }
Mike Stump1eb44332009-09-09 15:08:12 +00001445
John McCall52a575a2009-08-29 08:11:13 +00001446 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
1447 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001448
John McCall52a575a2009-08-29 08:11:13 +00001449 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
1450 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00001451
John McCall52a575a2009-08-29 08:11:13 +00001452 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
1453 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00001454
Douglas Gregor7caa6822009-07-24 20:34:43 +00001455 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00001456 if (Var->isStaticDataMember())
1457 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
1458
1459 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
1460 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00001461
Douglas Gregor0d696532009-09-28 06:34:35 +00001462 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
1463 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
1464
Anders Carlssond8b285f2009-09-01 04:26:58 +00001465 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
1466 if (!Field->getDeclName()) {
1467 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00001468 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00001469 cast<FieldDecl>(D);
1470 }
1471 }
Mike Stump1eb44332009-09-09 15:08:12 +00001472
Douglas Gregor815215d2009-05-27 05:35:12 +00001473 return D->getDeclName() && isa<NamedDecl>(Other) &&
1474 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
1475}
1476
1477template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00001478static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00001479 NamedDecl *D,
1480 ForwardIterator first,
1481 ForwardIterator last) {
1482 for (; first != last; ++first)
1483 if (isInstantiationOf(Ctx, D, *first))
1484 return cast<NamedDecl>(*first);
1485
1486 return 0;
1487}
1488
John McCall02cace72009-08-28 07:59:38 +00001489/// \brief Finds the instantiation of the given declaration context
1490/// within the current instantiation.
1491///
1492/// \returns NULL if there was an error
Douglas Gregore95b4092009-09-16 18:34:49 +00001493DeclContext *Sema::FindInstantiatedContext(DeclContext* DC,
1494 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00001495 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00001496 Decl* ID = FindInstantiatedDecl(D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00001497 return cast_or_null<DeclContext>(ID);
1498 } else return DC;
1499}
1500
Douglas Gregored961e72009-05-27 17:54:46 +00001501/// \brief Find the instantiation of the given declaration within the
1502/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00001503///
1504/// This routine is intended to be used when \p D is a declaration
1505/// referenced from within a template, that needs to mapped into the
1506/// corresponding declaration within an instantiation. For example,
1507/// given:
1508///
1509/// \code
1510/// template<typename T>
1511/// struct X {
1512/// enum Kind {
1513/// KnownValue = sizeof(T)
1514/// };
1515///
1516/// bool getKind() const { return KnownValue; }
1517/// };
1518///
1519/// template struct X<int>;
1520/// \endcode
1521///
1522/// In the instantiation of X<int>::getKind(), we need to map the
1523/// EnumConstantDecl for KnownValue (which refers to
1524/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00001525/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
1526/// this mapping from within the instantiation of X<int>.
Douglas Gregore95b4092009-09-16 18:34:49 +00001527NamedDecl *Sema::FindInstantiatedDecl(NamedDecl *D,
1528 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor44c73842009-09-01 17:53:10 +00001529 if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D)) {
1530 // Transform all of the elements of the overloaded function set.
Mike Stump1eb44332009-09-09 15:08:12 +00001531 OverloadedFunctionDecl *Result
Douglas Gregor44c73842009-09-01 17:53:10 +00001532 = OverloadedFunctionDecl::Create(Context, CurContext, Ovl->getDeclName());
Mike Stump1eb44332009-09-09 15:08:12 +00001533
Douglas Gregor44c73842009-09-01 17:53:10 +00001534 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
1535 FEnd = Ovl->function_end();
1536 F != FEnd; ++F) {
1537 Result->addOverload(
Douglas Gregore95b4092009-09-16 18:34:49 +00001538 AnyFunctionDecl::getFromNamedDecl(FindInstantiatedDecl(*F,
1539 TemplateArgs)));
Douglas Gregor44c73842009-09-01 17:53:10 +00001540 }
Mike Stump1eb44332009-09-09 15:08:12 +00001541
Douglas Gregor44c73842009-09-01 17:53:10 +00001542 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001543 }
1544
Douglas Gregor815215d2009-05-27 05:35:12 +00001545 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor2bba76b2009-05-27 17:07:49 +00001546 if (isa<ParmVarDecl>(D) || ParentDC->isFunctionOrMethod()) {
1547 // D is a local of some kind. Look into the map of local
1548 // declarations to their instantiations.
1549 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
1550 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001551
Douglas Gregore95b4092009-09-16 18:34:49 +00001552 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
1553 if (!Record->isDependentContext())
1554 return D;
1555
1556 // If the RecordDecl is actually the injected-class-name or a "templated"
1557 // declaration for a class template or class template partial
1558 // specialization, substitute into the injected-class-name of the
1559 // class template or partial specialization to find the new DeclContext.
1560 QualType T;
1561 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
1562
1563 if (ClassTemplate) {
1564 T = ClassTemplate->getInjectedClassNameType(Context);
1565 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
1566 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1567 T = Context.getTypeDeclType(Record);
1568 ClassTemplate = PartialSpec->getSpecializedTemplate();
1569 }
1570
1571 if (!T.isNull()) {
1572 // Substitute into the injected-class-name to get the type corresponding
1573 // to the instantiation we want. This substitution should never fail,
1574 // since we know we can instantiate the injected-class-name or we wouldn't
1575 // have gotten to the injected-class-name!
1576 // FIXME: Can we use the CurrentInstantiationScope to avoid this extra
1577 // instantiation in the common case?
1578 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
1579 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
1580
1581 if (!T->isDependentType()) {
1582 assert(T->isRecordType() && "Instantiation must produce a record type");
1583 return T->getAs<RecordType>()->getDecl();
1584 }
1585
1586 // We are performing "partial" template instantiation to create the
1587 // member declarations for the members of a class template
1588 // specialization. Therefore, D is actually referring to something in
1589 // the current instantiation. Look through the current context,
1590 // which contains actual instantiations, to find the instantiation of
1591 // the "current instantiation" that D refers to.
Mike Stump1eb44332009-09-09 15:08:12 +00001592 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00001593 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001594 if (ClassTemplateSpecializationDecl *Spec
John McCall52a575a2009-08-29 08:11:13 +00001595 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00001596 if (isInstantiationOf(ClassTemplate,
1597 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00001598 return Spec;
1599 }
1600
Mike Stump1eb44332009-09-09 15:08:12 +00001601 assert(false &&
John McCall52a575a2009-08-29 08:11:13 +00001602 "Unable to find declaration for the current instantiation");
Douglas Gregore95b4092009-09-16 18:34:49 +00001603 return Record;
John McCall52a575a2009-08-29 08:11:13 +00001604 }
Douglas Gregore95b4092009-09-16 18:34:49 +00001605
1606 // Fall through to deal with other dependent record types (e.g.,
1607 // anonymous unions in class templates).
1608 }
John McCall52a575a2009-08-29 08:11:13 +00001609
Douglas Gregore95b4092009-09-16 18:34:49 +00001610 if (!ParentDC->isDependentContext())
1611 return D;
1612
1613 ParentDC = FindInstantiatedContext(ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001614 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00001615 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001616
Douglas Gregor815215d2009-05-27 05:35:12 +00001617 if (ParentDC != D->getDeclContext()) {
1618 // We performed some kind of instantiation in the parent context,
1619 // so now we need to look into the instantiated parent context to
1620 // find the instantiation of the declaration D.
1621 NamedDecl *Result = 0;
1622 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001623 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00001624 Result = findInstantiationOf(Context, D, Found.first, Found.second);
1625 } else {
1626 // Since we don't have a name for the entity we're looking for,
1627 // our only option is to walk through all of the declarations to
1628 // find that name. This will occur in a few cases:
1629 //
1630 // - anonymous struct/union within a template
1631 // - unnamed class/struct/union/enum within a template
1632 //
1633 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00001634 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001635 ParentDC->decls_begin(),
1636 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00001637 }
Mike Stump1eb44332009-09-09 15:08:12 +00001638
Douglas Gregor815215d2009-05-27 05:35:12 +00001639 assert(Result && "Unable to find instantiation of declaration!");
1640 D = Result;
1641 }
1642
Douglas Gregor815215d2009-05-27 05:35:12 +00001643 return D;
1644}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001645
Mike Stump1eb44332009-09-09 15:08:12 +00001646/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001647/// instantiations we have seen until this point.
1648void Sema::PerformPendingImplicitInstantiations() {
1649 while (!PendingImplicitInstantiations.empty()) {
1650 PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front();
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001651 PendingImplicitInstantiations.pop_front();
Mike Stump1eb44332009-09-09 15:08:12 +00001652
Douglas Gregor7caa6822009-07-24 20:34:43 +00001653 // Instantiate function definitions
1654 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001655 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001656 Function->getLocation(), *this,
1657 Context.getSourceManager(),
1658 "instantiating function definition");
Mike Stump1eb44332009-09-09 15:08:12 +00001659
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001660 if (!Function->getBody())
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001661 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001662 continue;
1663 }
Mike Stump1eb44332009-09-09 15:08:12 +00001664
Douglas Gregor7caa6822009-07-24 20:34:43 +00001665 // Instantiate static data member definitions.
1666 VarDecl *Var = cast<VarDecl>(Inst.first);
1667 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001668
Mike Stump1eb44332009-09-09 15:08:12 +00001669 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001670 Var->getLocation(), *this,
1671 Context.getSourceManager(),
1672 "instantiating static data member "
1673 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00001674
Douglas Gregor7caa6822009-07-24 20:34:43 +00001675 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001676 }
1677}