blob: 0a75383842545f2514e3c3cf13a9d80c46294b7c [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);
Douglas Gregor2dc0e642009-03-23 23:06:20 +000059 Decl *VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
John McCalle29ba202009-08-20 01:44:21 +000060 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregor7974c3b2009-10-07 17:21:34 +000061 Decl *VisitClassTemplatePartialSpecializationDecl(
62 ClassTemplatePartialSpecializationDecl *D);
Douglas Gregord60e1052009-08-27 16:57:43 +000063 Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
John McCalle29ba202009-08-20 01:44:21 +000064 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Anders Carlsson0dde18e2009-08-28 15:18:15 +000065 Decl *VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D);
Mike Stump1eb44332009-09-09 15:08:12 +000066
Douglas Gregor8dbc2692009-03-17 21:15:40 +000067 // Base case. FIXME: Remove once we can instantiate everything.
Mike Stump1eb44332009-09-09 15:08:12 +000068 Decl *VisitDecl(Decl *) {
Douglas Gregor3d7a12a2009-03-25 23:32:15 +000069 assert(false && "Template instantiation of unknown declaration kind!");
Douglas Gregor8dbc2692009-03-17 21:15:40 +000070 return 0;
71 }
Douglas Gregor5545e162009-03-24 00:38:23 +000072
John McCallfd810b12009-08-14 02:03:10 +000073 const LangOptions &getLangOptions() {
74 return SemaRef.getLangOptions();
75 }
76
Douglas Gregor5545e162009-03-24 00:38:23 +000077 // Helper functions for instantiating methods.
John McCallce3ff2b2009-08-25 22:02:44 +000078 QualType SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +000079 llvm::SmallVectorImpl<ParmVarDecl *> &Params);
Douglas Gregore53060f2009-06-25 22:08:12 +000080 bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
Douglas Gregor5545e162009-03-24 00:38:23 +000081 bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
John McCalle29ba202009-08-20 01:44:21 +000082
83 TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +000084 SubstTemplateParams(TemplateParameterList *List);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000085 };
86}
87
Douglas Gregor4f722be2009-03-25 15:45:12 +000088Decl *
89TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
90 assert(false && "Translation units cannot be instantiated");
91 return D;
92}
93
94Decl *
95TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
96 assert(false && "Namespaces cannot be instantiated");
97 return D;
98}
99
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000100Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
101 bool Invalid = false;
102 QualType T = D->getUnderlyingType();
103 if (T->isDependentType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000104 T = SemaRef.SubstType(T, TemplateArgs,
John McCallce3ff2b2009-08-25 22:02:44 +0000105 D->getLocation(), D->getDeclName());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000106 if (T.isNull()) {
107 Invalid = true;
108 T = SemaRef.Context.IntTy;
109 }
110 }
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000112 // Create the new typedef
113 TypedefDecl *Typedef
114 = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
115 D->getIdentifier(), T);
116 if (Invalid)
117 Typedef->setInvalidDecl();
118
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000119 Owner->addDecl(Typedef);
Mike Stump1eb44332009-09-09 15:08:12 +0000120
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000121 return Typedef;
122}
123
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000124Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000125 // Do substitution on the type of the declaration
John McCall0a5fa062009-10-21 02:39:02 +0000126 DeclaratorInfo *DI = SemaRef.SubstType(D->getDeclaratorInfo(),
127 TemplateArgs,
128 D->getTypeSpecStartLoc(),
129 D->getDeclName());
130 if (!DI)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000131 return 0;
132
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000133 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000134 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
135 D->getLocation(), D->getIdentifier(),
John McCall0a5fa062009-10-21 02:39:02 +0000136 DI->getType(), DI,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000137 D->getStorageClass());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000138 Var->setThreadSpecified(D->isThreadSpecified());
139 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
140 Var->setDeclaredInCondition(D->isDeclaredInCondition());
Mike Stump1eb44332009-09-09 15:08:12 +0000141
142 // If we are instantiating a static data member defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000143 // out-of-line, the instantiation will have the same lexical
144 // context (which will be a namespace scope) as the template.
145 if (D->isOutOfLine())
146 Var->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Mike Stump390b4cc2009-05-16 07:39:55 +0000148 // FIXME: In theory, we could have a previous declaration for variables that
149 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000150 bool Redeclaration = false;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000151 SemaRef.CheckVariableDeclaration(Var, 0, Redeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Douglas Gregor7caa6822009-07-24 20:34:43 +0000153 if (D->isOutOfLine()) {
154 D->getLexicalDeclContext()->addDecl(Var);
155 Owner->makeDeclVisibleInContext(Var);
156 } else {
157 Owner->addDecl(Var);
158 }
Mike Stump1eb44332009-09-09 15:08:12 +0000159
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000160 // Link instantiations of static data members back to the template from
161 // which they were instantiated.
162 if (Var->isStaticDataMember())
163 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
164 TSK_ImplicitInstantiation);
165
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000166 if (D->getInit()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000167 OwningExprResult Init
John McCallce3ff2b2009-08-25 22:02:44 +0000168 = SemaRef.SubstExpr(D->getInit(), TemplateArgs);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000169 if (Init.isInvalid())
170 Var->setInvalidDecl();
Douglas Gregor83ddad32009-08-26 21:14:46 +0000171 else if (ParenListExpr *PLE = dyn_cast<ParenListExpr>((Expr *)Init.get())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000172 // FIXME: We're faking all of the comma locations, which is suboptimal.
Douglas Gregor83ddad32009-08-26 21:14:46 +0000173 // Do we even need these comma locations?
174 llvm::SmallVector<SourceLocation, 4> FakeCommaLocs;
175 if (PLE->getNumExprs() > 0) {
176 FakeCommaLocs.reserve(PLE->getNumExprs() - 1);
177 for (unsigned I = 0, N = PLE->getNumExprs() - 1; I != N; ++I) {
178 Expr *E = PLE->getExpr(I)->Retain();
179 FakeCommaLocs.push_back(
180 SemaRef.PP.getLocForEndOfToken(E->getLocEnd()));
181 }
Douglas Gregore9f8eb62009-08-26 23:26:04 +0000182 PLE->getExpr(PLE->getNumExprs() - 1)->Retain();
Douglas Gregor83ddad32009-08-26 21:14:46 +0000183 }
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Douglas Gregor83ddad32009-08-26 21:14:46 +0000185 // Add the direct initializer to the declaration.
186 SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
Mike Stump1eb44332009-09-09 15:08:12 +0000187 PLE->getLParenLoc(),
Douglas Gregor83ddad32009-08-26 21:14:46 +0000188 Sema::MultiExprArg(SemaRef,
189 (void**)PLE->getExprs(),
190 PLE->getNumExprs()),
191 FakeCommaLocs.data(),
192 PLE->getRParenLoc());
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Douglas Gregor83ddad32009-08-26 21:14:46 +0000194 // When Init is destroyed, it will destroy the instantiated ParenListExpr;
195 // we've explicitly retained all of its subexpressions already.
196 } else
Chris Lattnerb28317a2009-03-28 19:18:32 +0000197 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init),
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000198 D->hasCXXDirectInitializer());
Douglas Gregor65b90052009-07-27 17:43:39 +0000199 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
200 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000201
202 return Var;
203}
204
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000205Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
206 bool Invalid = false;
207 QualType T = D->getType();
208 if (T->isDependentType()) {
John McCallce3ff2b2009-08-25 22:02:44 +0000209 T = SemaRef.SubstType(T, TemplateArgs,
210 D->getLocation(), D->getDeclName());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000211 if (!T.isNull() && T->isFunctionType()) {
212 // C++ [temp.arg.type]p3:
213 // If a declaration acquires a function type through a type
214 // dependent on a template-parameter and this causes a
215 // declaration that does not use the syntactic form of a
216 // function declarator to have function type, the program is
217 // ill-formed.
218 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
219 << T;
220 T = QualType();
221 Invalid = true;
222 }
223 }
224
225 Expr *BitWidth = D->getBitWidth();
226 if (Invalid)
227 BitWidth = 0;
228 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000229 // The bit-width expression is not potentially evaluated.
230 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000231
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000232 OwningExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000233 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000234 if (InstantiatedBitWidth.isInvalid()) {
235 Invalid = true;
236 BitWidth = 0;
237 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000238 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000239 }
240
241 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), T,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000242 D->getDeclaratorInfo(),
Mike Stump1eb44332009-09-09 15:08:12 +0000243 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000244 D->getLocation(),
245 D->isMutable(),
246 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000247 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000248 D->getAccess(),
249 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000250 if (!Field) {
251 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000252 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000253 }
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000255 if (Invalid)
256 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000258 if (!Field->getDeclName()) {
259 // Keep track of where this decl came from.
260 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000261 }
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000263 Field->setImplicit(D->isImplicit());
264 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000265
266 return Field;
267}
268
John McCall02cace72009-08-28 07:59:38 +0000269Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
270 FriendDecl::FriendUnion FU;
271
272 // Handle friend type expressions by simply substituting template
273 // parameters into the pattern type.
274 if (Type *Ty = D->getFriendType()) {
275 QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs,
276 D->getLocation(), DeclarationName());
277 if (T.isNull()) return 0;
278
279 assert(getLangOptions().CPlusPlus0x || T->isRecordType());
280 FU = T.getTypePtr();
281
282 // Handle everything else by appropriate substitution.
283 } else {
284 NamedDecl *ND = D->getFriendDecl();
285 assert(ND && "friend decl must be a decl or a type!");
286
Douglas Gregora735b202009-10-13 14:39:41 +0000287 // FIXME: We have a problem here, because the nested call to Visit(ND)
288 // will inject the thing that the friend references into the current
289 // owner, which is wrong.
John McCall02cace72009-08-28 07:59:38 +0000290 Decl *NewND = Visit(ND);
291 if (!NewND) return 0;
292
293 FU = cast<NamedDecl>(NewND);
John McCallfd810b12009-08-14 02:03:10 +0000294 }
Mike Stump1eb44332009-09-09 15:08:12 +0000295
John McCall02cace72009-08-28 07:59:38 +0000296 FriendDecl *FD =
297 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU,
298 D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000299 FD->setAccess(AS_public);
John McCall02cace72009-08-28 07:59:38 +0000300 Owner->addDecl(FD);
301 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000302}
303
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000304Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
305 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000306
Douglas Gregorac7610d2009-06-22 20:57:11 +0000307 // The expression in a static assertion is not potentially evaluated.
308 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000309
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000310 OwningExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000311 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000312 if (InstantiatedAssertExpr.isInvalid())
313 return 0;
314
Douglas Gregor43d9d922009-08-08 01:41:12 +0000315 OwningExprResult Message(SemaRef, D->getMessage());
316 D->getMessage()->Retain();
Mike Stump1eb44332009-09-09 15:08:12 +0000317 Decl *StaticAssert
318 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
Chris Lattnerb28317a2009-03-28 19:18:32 +0000319 move(InstantiatedAssertExpr),
320 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000321 return StaticAssert;
322}
323
324Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000325 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000326 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000327 D->getTagKeywordLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000328 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000329 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000330 Enum->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000331 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000332 Enum->startDefinition();
333
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000334 llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000335
336 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000337 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
338 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000339 EC != ECEnd; ++EC) {
340 // The specified value for the enumerator.
341 OwningExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000342 if (Expr *UninstValue = EC->getInitExpr()) {
343 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000344 EnterExpressionEvaluationContext Unevaluated(SemaRef,
Douglas Gregorac7610d2009-06-22 20:57:11 +0000345 Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000346
John McCallce3ff2b2009-08-25 22:02:44 +0000347 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000348 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000349
350 // Drop the initial value and continue.
351 bool isInvalid = false;
352 if (Value.isInvalid()) {
353 Value = SemaRef.Owned((Expr *)0);
354 isInvalid = true;
355 }
356
Mike Stump1eb44332009-09-09 15:08:12 +0000357 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000358 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
359 EC->getLocation(), EC->getIdentifier(),
360 move(Value));
361
362 if (isInvalid) {
363 if (EnumConst)
364 EnumConst->setInvalidDecl();
365 Enum->setInvalidDecl();
366 }
367
368 if (EnumConst) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000369 Enum->addDecl(EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000370 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000371 LastEnumConst = EnumConst;
372 }
373 }
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000375 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000376 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000377 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
378 Sema::DeclPtrTy::make(Enum),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000379 &Enumerators[0], Enumerators.size(),
380 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000381
382 return Enum;
383}
384
Douglas Gregor6477b692009-03-25 15:04:13 +0000385Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
386 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
387 return 0;
388}
389
John McCalle29ba202009-08-20 01:44:21 +0000390Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
391 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000392 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000393 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000394 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000395
396 CXXRecordDecl *Pattern = D->getTemplatedDecl();
397 CXXRecordDecl *RecordInst
398 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner,
399 Pattern->getLocation(), Pattern->getIdentifier(),
Douglas Gregorf0510d42009-10-12 23:11:44 +0000400 Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL,
401 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000402
403 ClassTemplateDecl *Inst
404 = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
405 D->getIdentifier(), InstParams, RecordInst, 0);
406 RecordInst->setDescribedClassTemplate(Inst);
407 Inst->setAccess(D->getAccess());
408 Inst->setInstantiatedFromMemberTemplate(D);
Douglas Gregorf0510d42009-10-12 23:11:44 +0000409
410 // Trigger creation of the type for the instantiation.
411 SemaRef.Context.getTypeDeclType(RecordInst);
412
John McCalle29ba202009-08-20 01:44:21 +0000413 Owner->addDecl(Inst);
414 return Inst;
415}
416
Douglas Gregord60e1052009-08-27 16:57:43 +0000417Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000418TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
419 ClassTemplatePartialSpecializationDecl *D) {
420 assert(false &&"Partial specializations of member templates are unsupported");
421 return 0;
422}
423
424Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000425TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000426 // FIXME: Dig out the out-of-line definition of this function template?
Mike Stump1eb44332009-09-09 15:08:12 +0000427
Douglas Gregord60e1052009-08-27 16:57:43 +0000428 TemplateParameterList *TempParams = D->getTemplateParameters();
429 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000430 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000431 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Douglas Gregora735b202009-10-13 14:39:41 +0000433 FunctionDecl *Instantiated = 0;
434 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
435 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
436 InstParams));
437 else
438 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
439 D->getTemplatedDecl(),
440 InstParams));
441
442 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000443 return 0;
444
Mike Stump1eb44332009-09-09 15:08:12 +0000445 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000446 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000447 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000448 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000449 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000450 assert(InstTemplate &&
451 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
452 if (!InstTemplate->getInstantiatedFromMemberTemplate())
453 InstTemplate->setInstantiatedFromMemberTemplate(D);
454
455 // Add non-friends into the owner.
456 if (!InstTemplate->getFriendObjectKind())
457 Owner->addDecl(InstTemplate);
Douglas Gregord60e1052009-08-27 16:57:43 +0000458 return InstTemplate;
459}
460
Douglas Gregord475b8d2009-03-25 21:17:03 +0000461Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
462 CXXRecordDecl *PrevDecl = 0;
463 if (D->isInjectedClassName())
464 PrevDecl = cast<CXXRecordDecl>(Owner);
465
466 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000467 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000468 D->getLocation(), D->getIdentifier(),
469 D->getTagKeywordLoc(), PrevDecl);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000470 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000471 // FIXME: Check against AS_none is an ugly hack to work around the issue that
472 // the tag decls introduced by friend class declarations don't have an access
473 // specifier. Remove once this area of the code gets sorted out.
474 if (D->getAccess() != AS_none)
475 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000476 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000477 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000478
John McCall02cace72009-08-28 07:59:38 +0000479 // If the original function was part of a friend declaration,
480 // inherit its namespace state.
481 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
482 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
483
Anders Carlssond8b285f2009-09-01 04:26:58 +0000484 Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
485
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000486 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000487 return Record;
488}
489
John McCall02cace72009-08-28 07:59:38 +0000490/// Normal class members are of more specific types and therefore
491/// don't make it here. This function serves two purposes:
492/// 1) instantiating function templates
493/// 2) substituting friend declarations
494/// FIXME: preserve function definitions in case #2
Douglas Gregora735b202009-10-13 14:39:41 +0000495 Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
496 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000497 // Check whether there is already a function template specialization for
498 // this declaration.
499 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
500 void *InsertPos = 0;
Douglas Gregora735b202009-10-13 14:39:41 +0000501 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000502 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000503 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000504 TemplateArgs.getInnermost().getFlatArgumentList(),
505 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor828e2262009-07-29 16:09:57 +0000506 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000507
508 FunctionTemplateSpecializationInfo *Info
509 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor127102b2009-06-29 20:59:39 +0000510 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Douglas Gregor127102b2009-06-29 20:59:39 +0000512 // If we already have a function template specialization, return it.
513 if (Info)
514 return Info->Function;
515 }
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Douglas Gregore53060f2009-06-25 22:08:12 +0000517 Sema::LocalInstantiationScope Scope(SemaRef);
Mike Stump1eb44332009-09-09 15:08:12 +0000518
Douglas Gregore53060f2009-06-25 22:08:12 +0000519 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000520 QualType T = SubstFunctionType(D, Params);
Douglas Gregore53060f2009-06-25 22:08:12 +0000521 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000522 return 0;
John McCallfd810b12009-08-14 02:03:10 +0000523
Douglas Gregore53060f2009-06-25 22:08:12 +0000524 // Build the instantiated method declaration.
Douglas Gregore95b4092009-09-16 18:34:49 +0000525 DeclContext *DC = SemaRef.FindInstantiatedContext(D->getDeclContext(),
526 TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +0000527 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +0000528 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000529 D->getDeclName(), T, D->getDeclaratorInfo(),
530 D->getStorageClass(),
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000531 D->isInline(), D->hasWrittenPrototype());
John McCall02cace72009-08-28 07:59:38 +0000532 Function->setLexicalDeclContext(Owner);
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Douglas Gregore53060f2009-06-25 22:08:12 +0000534 // Attach the parameters
535 for (unsigned P = 0; P < Params.size(); ++P)
536 Params[P]->setOwningFunction(Function);
537 Function->setParams(SemaRef.Context, Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +0000538
Douglas Gregora735b202009-10-13 14:39:41 +0000539 if (TemplateParams) {
540 // Our resulting instantiation is actually a function template, since we
541 // are substituting only the outer template parameters. For example, given
542 //
543 // template<typename T>
544 // struct X {
545 // template<typename U> friend void f(T, U);
546 // };
547 //
548 // X<int> x;
549 //
550 // We are instantiating the friend function template "f" within X<int>,
551 // which means substituting int for T, but leaving "f" as a friend function
552 // template.
553 // Build the function template itself.
554 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Owner,
555 Function->getLocation(),
556 Function->getDeclName(),
557 TemplateParams, Function);
558 Function->setDescribedFunctionTemplate(FunctionTemplate);
559 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
John McCall02cace72009-08-28 07:59:38 +0000560 }
Douglas Gregora735b202009-10-13 14:39:41 +0000561
Douglas Gregore53060f2009-06-25 22:08:12 +0000562 if (InitFunctionInstantiation(Function, D))
563 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000564
Douglas Gregore53060f2009-06-25 22:08:12 +0000565 bool Redeclaration = false;
566 bool OverloadableAttrRequired = false;
Douglas Gregora735b202009-10-13 14:39:41 +0000567
Douglas Gregore53060f2009-06-25 22:08:12 +0000568 NamedDecl *PrevDecl = 0;
Douglas Gregora735b202009-10-13 14:39:41 +0000569 if (TemplateParams || !FunctionTemplate) {
570 // Look only into the namespace where the friend would be declared to
571 // find a previous declaration. This is the innermost enclosing namespace,
572 // as described in ActOnFriendFunctionDecl.
573 Sema::LookupResult R;
574 SemaRef.LookupQualifiedName(R, DC, Function->getDeclName(),
575 Sema::LookupOrdinaryName, true);
576
577 PrevDecl = R.getAsSingleDecl(SemaRef.Context);
578
579 // In C++, the previous declaration we find might be a tag type
580 // (class or enum). In this case, the new declaration will hide the
581 // tag type. Note that this does does not apply if we're declaring a
582 // typedef (C++ [dcl.typedef]p4).
583 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
584 PrevDecl = 0;
585 }
586
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000587 SemaRef.CheckFunctionDeclaration(Function, PrevDecl, false, Redeclaration,
Douglas Gregore53060f2009-06-25 22:08:12 +0000588 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000589
Douglas Gregora735b202009-10-13 14:39:41 +0000590 // If the original function was part of a friend declaration,
591 // inherit its namespace state and add it to the owner.
592 NamedDecl *FromFriendD
593 = TemplateParams? cast<NamedDecl>(D->getDescribedFunctionTemplate()) : D;
594 if (FromFriendD->getFriendObjectKind()) {
595 NamedDecl *ToFriendD = 0;
596 if (TemplateParams) {
597 ToFriendD = cast<NamedDecl>(FunctionTemplate);
598 PrevDecl = FunctionTemplate->getPreviousDeclaration();
599 } else {
600 ToFriendD = Function;
601 PrevDecl = Function->getPreviousDeclaration();
602 }
603 ToFriendD->setObjectOfFriendDecl(PrevDecl != NULL);
604 if (!Owner->isDependentContext() && !PrevDecl)
605 DC->makeDeclVisibleInContext(ToFriendD, /* Recoverable = */ false);
606
607 if (!TemplateParams)
608 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
609 }
610
611 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000612 // Record this function template specialization.
613 Function->setFunctionTemplateSpecialization(SemaRef.Context,
614 FunctionTemplate,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000615 &TemplateArgs.getInnermost(),
Douglas Gregor127102b2009-06-29 20:59:39 +0000616 InsertPos);
John McCallfd810b12009-08-14 02:03:10 +0000617 }
618
Douglas Gregore53060f2009-06-25 22:08:12 +0000619 return Function;
620}
621
Douglas Gregord60e1052009-08-27 16:57:43 +0000622Decl *
623TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
624 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +0000625 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
626 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +0000627 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +0000628 // We are creating a function template specialization from a function
629 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +0000630 // specialization for this particular set of template arguments.
Douglas Gregor6b906862009-08-21 00:16:32 +0000631 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000632 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000633 TemplateArgs.getInnermost().getFlatArgumentList(),
634 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000635 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000636
637 FunctionTemplateSpecializationInfo *Info
638 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor6b906862009-08-21 00:16:32 +0000639 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Douglas Gregor6b906862009-08-21 00:16:32 +0000641 // If we already have a function template specialization, return it.
642 if (Info)
643 return Info->Function;
644 }
645
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000646 Sema::LocalInstantiationScope Scope(SemaRef);
647
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000648 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000649 QualType T = SubstFunctionType(D, Params);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000650 if (T.isNull())
651 return 0;
652
653 // Build the instantiated method declaration.
654 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
Douglas Gregordec06662009-08-21 18:42:58 +0000655 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000656
Douglas Gregordec06662009-08-21 18:42:58 +0000657 DeclarationName Name = D->getDeclName();
Douglas Gregor17e32f32009-08-21 22:43:28 +0000658 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregordec06662009-08-21 18:42:58 +0000659 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
660 Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
661 SemaRef.Context.getCanonicalType(ClassTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000662 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
663 Constructor->getLocation(),
664 Name, T,
Douglas Gregor17e32f32009-08-21 22:43:28 +0000665 Constructor->getDeclaratorInfo(),
Mike Stump1eb44332009-09-09 15:08:12 +0000666 Constructor->isExplicit(),
Douglas Gregor17e32f32009-08-21 22:43:28 +0000667 Constructor->isInline(), false);
668 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
669 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
670 Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
671 SemaRef.Context.getCanonicalType(ClassTy));
672 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
673 Destructor->getLocation(), Name,
674 T, Destructor->isInline(), false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000675 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000676 CanQualType ConvTy
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000677 = SemaRef.Context.getCanonicalType(
John McCall183700f2009-09-21 23:43:11 +0000678 T->getAs<FunctionType>()->getResultType());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000679 Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
680 ConvTy);
681 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
682 Conversion->getLocation(), Name,
683 T, Conversion->getDeclaratorInfo(),
Mike Stump1eb44332009-09-09 15:08:12 +0000684 Conversion->isInline(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000685 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +0000686 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000687 Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
Douglas Gregordec06662009-08-21 18:42:58 +0000688 D->getDeclName(), T, D->getDeclaratorInfo(),
689 D->isStatic(), D->isInline());
690 }
Douglas Gregor6b906862009-08-21 00:16:32 +0000691
Douglas Gregord60e1052009-08-27 16:57:43 +0000692 if (TemplateParams) {
693 // Our resulting instantiation is actually a function template, since we
694 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +0000695 //
Douglas Gregord60e1052009-08-27 16:57:43 +0000696 // template<typename T>
697 // struct X {
698 // template<typename U> void f(T, U);
699 // };
700 //
701 // X<int> x;
702 //
703 // We are instantiating the member template "f" within X<int>, which means
704 // substituting int for T, but leaving "f" as a member function template.
705 // Build the function template itself.
706 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
707 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +0000708 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +0000709 TemplateParams, Method);
710 if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +0000711 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +0000712 Method->setDescribedFunctionTemplate(FunctionTemplate);
713 } else if (!FunctionTemplate)
Douglas Gregor2db32322009-10-07 23:56:10 +0000714 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000715
Mike Stump1eb44332009-09-09 15:08:12 +0000716 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000717 // out-of-line, the instantiation will have the same lexical
718 // context (which will be a namespace scope) as the template.
719 if (D->isOutOfLine())
720 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000721
Douglas Gregor5545e162009-03-24 00:38:23 +0000722 // Attach the parameters
723 for (unsigned P = 0; P < Params.size(); ++P)
724 Params[P]->setOwningFunction(Method);
Jay Foadbeaaccd2009-05-21 09:52:38 +0000725 Method->setParams(SemaRef.Context, Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +0000726
727 if (InitMethodInstantiation(Method, D))
728 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000729
Douglas Gregordec06662009-08-21 18:42:58 +0000730 NamedDecl *PrevDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000731
Douglas Gregord60e1052009-08-27 16:57:43 +0000732 if (!FunctionTemplate || TemplateParams) {
John McCallf36e02d2009-10-09 21:13:30 +0000733 Sema::LookupResult R;
734 SemaRef.LookupQualifiedName(R, Owner, Name, Sema::LookupOrdinaryName, true);
735 PrevDecl = R.getAsSingleDecl(SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000736
Douglas Gregordec06662009-08-21 18:42:58 +0000737 // In C++, the previous declaration we find might be a tag type
738 // (class or enum). In this case, the new declaration will hide the
739 // tag type. Note that this does does not apply if we're declaring a
740 // typedef (C++ [dcl.typedef]p4).
741 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
742 PrevDecl = 0;
743 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000744
Douglas Gregord60e1052009-08-27 16:57:43 +0000745 if (FunctionTemplate && !TemplateParams)
Douglas Gregor6b906862009-08-21 00:16:32 +0000746 // Record this function template specialization.
747 Method->setFunctionTemplateSpecialization(SemaRef.Context,
748 FunctionTemplate,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000749 &TemplateArgs.getInnermost(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000750 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000751
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000752 bool Redeclaration = false;
753 bool OverloadableAttrRequired = false;
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000754 SemaRef.CheckFunctionDeclaration(Method, PrevDecl, false, Redeclaration,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000755 /*FIXME:*/OverloadableAttrRequired);
756
Douglas Gregora735b202009-10-13 14:39:41 +0000757 if (!FunctionTemplate && (!Method->isInvalidDecl() || !PrevDecl) &&
758 !Method->getFriendObjectKind())
Douglas Gregordec06662009-08-21 18:42:58 +0000759 Owner->addDecl(Method);
Mike Stump1eb44332009-09-09 15:08:12 +0000760
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000761 return Method;
762}
763
Douglas Gregor615c5d42009-03-24 16:43:20 +0000764Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +0000765 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000766}
767
Douglas Gregor03b2b072009-03-24 00:15:49 +0000768Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +0000769 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +0000770}
771
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000772Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000773 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000774}
775
Douglas Gregor6477b692009-03-25 15:04:13 +0000776ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000777 QualType OrigT = SemaRef.SubstType(D->getOriginalType(), TemplateArgs,
Douglas Gregor7e063902009-05-11 23:53:27 +0000778 D->getLocation(), D->getDeclName());
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000779 if (OrigT.isNull())
780 return 0;
781
782 QualType T = SemaRef.adjustParameterType(OrigT);
783
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000784 // Allocate the parameter
785 ParmVarDecl *Param = 0;
786 if (T == OrigT)
787 Param = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000788 D->getIdentifier(), T, D->getDeclaratorInfo(),
789 D->getStorageClass(), 0);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000790 else
Mike Stump1eb44332009-09-09 15:08:12 +0000791 Param = OriginalParmVarDecl::Create(SemaRef.Context, Owner,
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000792 D->getLocation(), D->getIdentifier(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000793 T, D->getDeclaratorInfo(), OrigT,
794 D->getStorageClass(), 0);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000795
Anders Carlsson9351c172009-08-25 03:18:48 +0000796 // Mark the default argument as being uninstantiated.
Douglas Gregorf43d0b32009-09-25 06:56:31 +0000797 if (D->hasUninstantiatedDefaultArg())
798 Param->setUninstantiatedDefaultArg(D->getUninstantiatedDefaultArg());
Douglas Gregor0ed09302009-09-25 07:03:22 +0000799 else if (Expr *Arg = D->getDefaultArg())
800 Param->setUninstantiatedDefaultArg(Arg);
801
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000802 // Note: we don't try to instantiate function parameters until after
803 // we've instantiated the function's type. Therefore, we don't have
804 // to check for 'void' parameter types here.
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000805 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000806 return Param;
807}
808
809Decl *
810TemplateDeclInstantiator::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
811 // Since parameter types can decay either before or after
812 // instantiation, we simply treat OriginalParmVarDecls as
813 // ParmVarDecls the same way, and create one or the other depending
814 // on what happens after template instantiation.
815 return VisitParmVarDecl(D);
816}
817
John McCalle29ba202009-08-20 01:44:21 +0000818Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
819 TemplateTypeParmDecl *D) {
820 // TODO: don't always clone when decls are refcounted.
821 const Type* T = D->getTypeForDecl();
822 assert(T->isTemplateTypeParmType());
823 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000824
John McCalle29ba202009-08-20 01:44:21 +0000825 TemplateTypeParmDecl *Inst =
826 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
827 TTPT->getDepth(), TTPT->getIndex(),
828 TTPT->getName(),
829 D->wasDeclaredWithTypename(),
830 D->isParameterPack());
831
832 if (D->hasDefaultArgument()) {
833 QualType DefaultPattern = D->getDefaultArgument();
834 QualType DefaultInst
John McCallce3ff2b2009-08-25 22:02:44 +0000835 = SemaRef.SubstType(DefaultPattern, TemplateArgs,
836 D->getDefaultArgumentLoc(),
837 D->getDeclName());
Mike Stump1eb44332009-09-09 15:08:12 +0000838
John McCalle29ba202009-08-20 01:44:21 +0000839 Inst->setDefaultArgument(DefaultInst,
840 D->getDefaultArgumentLoc(),
841 D->defaultArgumentWasInherited() /* preserve? */);
842 }
843
844 return Inst;
845}
846
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000847Decl *
848TemplateDeclInstantiator::VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000849 NestedNameSpecifier *NNS =
850 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
851 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000852 TemplateArgs);
853 if (!NNS)
854 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000855
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000856 CXXScopeSpec SS;
857 SS.setRange(D->getTargetNestedNameRange());
858 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +0000859
860 NamedDecl *UD =
861 SemaRef.BuildUsingDeclaration(D->getLocation(), SS,
862 D->getTargetNameLocation(),
Anders Carlsson0d8df782009-08-29 19:37:28 +0000863 D->getTargetName(), 0, D->isTypeName());
864 if (UD)
Mike Stump1eb44332009-09-09 15:08:12 +0000865 SemaRef.Context.setInstantiatedFromUnresolvedUsingDecl(cast<UsingDecl>(UD),
Anders Carlsson0d8df782009-08-29 19:37:28 +0000866 D);
867 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000868}
869
John McCallce3ff2b2009-08-25 22:02:44 +0000870Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000871 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +0000872 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000873 return Instantiator.Visit(D);
874}
875
John McCalle29ba202009-08-20 01:44:21 +0000876/// \brief Instantiates a nested template parameter list in the current
877/// instantiation context.
878///
879/// \param L The parameter list to instantiate
880///
881/// \returns NULL if there was an error
882TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +0000883TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +0000884 // Get errors for all the parameters before bailing out.
885 bool Invalid = false;
886
887 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000888 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +0000889 ParamVector Params;
890 Params.reserve(N);
891 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
892 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000893 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +0000894 Params.push_back(D);
895 Invalid = Invalid || !D;
896 }
897
898 // Clean up if we had an error.
899 if (Invalid) {
900 for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
901 PI != PE; ++PI)
902 if (*PI)
903 (*PI)->Destroy(SemaRef.Context);
904 return NULL;
905 }
906
907 TemplateParameterList *InstL
908 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
909 L->getLAngleLoc(), &Params.front(), N,
910 L->getRAngleLoc());
911 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +0000912}
John McCalle29ba202009-08-20 01:44:21 +0000913
John McCallce3ff2b2009-08-25 22:02:44 +0000914/// \brief Does substitution on the type of the given function, including
915/// all of the function parameters.
Douglas Gregor5545e162009-03-24 00:38:23 +0000916///
John McCallce3ff2b2009-08-25 22:02:44 +0000917/// \param D The function whose type will be the basis of the substitution
Douglas Gregor5545e162009-03-24 00:38:23 +0000918///
919/// \param Params the instantiated parameter declarations
920
John McCallce3ff2b2009-08-25 22:02:44 +0000921/// \returns the instantiated function's type if successful, a NULL
Douglas Gregor5545e162009-03-24 00:38:23 +0000922/// type if there was an error.
Mike Stump1eb44332009-09-09 15:08:12 +0000923QualType
John McCallce3ff2b2009-08-25 22:02:44 +0000924TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +0000925 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
926 bool InvalidDecl = false;
927
John McCallce3ff2b2009-08-25 22:02:44 +0000928 // Substitute all of the function's formal parameter types.
Douglas Gregor7e063902009-05-11 23:53:27 +0000929 TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000930 llvm::SmallVector<QualType, 4> ParamTys;
Mike Stump1eb44332009-09-09 15:08:12 +0000931 for (FunctionDecl::param_iterator P = D->param_begin(),
Douglas Gregor5545e162009-03-24 00:38:23 +0000932 PEnd = D->param_end();
933 P != PEnd; ++P) {
Douglas Gregor6477b692009-03-25 15:04:13 +0000934 if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
Douglas Gregor5545e162009-03-24 00:38:23 +0000935 if (PInst->getType()->isVoidType()) {
936 SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
937 PInst->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000938 } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000939 PInst->getType(),
940 diag::err_abstract_type_in_decl,
941 Sema::AbstractParamType))
Douglas Gregor5545e162009-03-24 00:38:23 +0000942 PInst->setInvalidDecl();
943
944 Params.push_back(PInst);
945 ParamTys.push_back(PInst->getType());
946
947 if (PInst->isInvalidDecl())
948 InvalidDecl = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000949 } else
Douglas Gregor5545e162009-03-24 00:38:23 +0000950 InvalidDecl = true;
951 }
952
953 // FIXME: Deallocate dead declarations.
954 if (InvalidDecl)
955 return QualType();
956
John McCall183700f2009-09-21 23:43:11 +0000957 const FunctionProtoType *Proto = D->getType()->getAs<FunctionProtoType>();
Douglas Gregor5545e162009-03-24 00:38:23 +0000958 assert(Proto && "Missing prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +0000959 QualType ResultType
John McCallce3ff2b2009-08-25 22:02:44 +0000960 = SemaRef.SubstType(Proto->getResultType(), TemplateArgs,
961 D->getLocation(), D->getDeclName());
Douglas Gregor5545e162009-03-24 00:38:23 +0000962 if (ResultType.isNull())
963 return QualType();
964
Jay Foadbeaaccd2009-05-21 09:52:38 +0000965 return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
Douglas Gregor5545e162009-03-24 00:38:23 +0000966 Proto->isVariadic(), Proto->getTypeQuals(),
967 D->getLocation(), D->getDeclName());
968}
969
Mike Stump1eb44332009-09-09 15:08:12 +0000970/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +0000971/// declaration (New) from the corresponding fields of its template (Tmpl).
972///
973/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +0000974bool
975TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +0000976 FunctionDecl *Tmpl) {
977 if (Tmpl->isDeleted())
978 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +0000979
Douglas Gregorcca9e962009-07-01 22:01:06 +0000980 // If we are performing substituting explicitly-specified template arguments
981 // or deduced template arguments into a function template and we reach this
982 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +0000983 // to keeping the new function template specialization. We therefore
984 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +0000985 // into a template instantiation for this specific function template
986 // specialization, which is not a SFINAE context, so that we diagnose any
987 // further errors in the declaration itself.
988 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
989 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
990 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
991 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +0000992 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +0000993 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000994 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +0000995 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +0000996 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +0000997 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
998 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
999 }
1000 }
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Douglas Gregore53060f2009-06-25 22:08:12 +00001002 return false;
1003}
1004
Douglas Gregor5545e162009-03-24 00:38:23 +00001005/// \brief Initializes common fields of an instantiated method
1006/// declaration (New) from the corresponding fields of its template
1007/// (Tmpl).
1008///
1009/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001010bool
1011TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00001012 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001013 if (InitFunctionInstantiation(New, Tmpl))
1014 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001015
Douglas Gregor5545e162009-03-24 00:38:23 +00001016 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
1017 New->setAccess(Tmpl->getAccess());
Anders Carlsson77b7f1d2009-05-14 22:15:41 +00001018 if (Tmpl->isVirtualAsWritten()) {
1019 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +00001020 Record->setAggregate(false);
1021 Record->setPOD(false);
Eli Friedman1d954f62009-08-15 21:55:26 +00001022 Record->setEmpty(false);
Douglas Gregor5545e162009-03-24 00:38:23 +00001023 Record->setPolymorphic(true);
1024 }
Douglas Gregor5545e162009-03-24 00:38:23 +00001025 if (Tmpl->isPure()) {
1026 New->setPure();
1027 Record->setAbstract(true);
1028 }
1029
1030 // FIXME: attributes
1031 // FIXME: New needs a pointer to Tmpl
1032 return false;
1033}
Douglas Gregora58861f2009-05-13 20:28:22 +00001034
1035/// \brief Instantiate the definition of the given function from its
1036/// template.
1037///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001038/// \param PointOfInstantiation the point at which the instantiation was
1039/// required. Note that this is not precisely a "point of instantiation"
1040/// for the function, but it's close.
1041///
Douglas Gregora58861f2009-05-13 20:28:22 +00001042/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001043/// function template specialization or member function of a class template
1044/// specialization.
1045///
1046/// \param Recursive if true, recursively instantiates any functions that
1047/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001048///
1049/// \param DefinitionRequired if true, then we are performing an explicit
1050/// instantiation where the body of the function is required. Complain if
1051/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001052void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001053 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001054 bool Recursive,
1055 bool DefinitionRequired) {
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001056 if (Function->isInvalidDecl())
1057 return;
1058
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001059 assert(!Function->getBody() && "Already instantiated!");
Mike Stump1eb44332009-09-09 15:08:12 +00001060
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001061 // Never instantiate an explicit specialization.
1062 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1063 return;
1064
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001065 // Find the function body that we'll be substituting.
Douglas Gregor1637be72009-06-26 00:10:03 +00001066 const FunctionDecl *PatternDecl = 0;
Douglas Gregor5ec178f2009-08-28 21:09:48 +00001067 if (FunctionTemplateDecl *Primary = Function->getPrimaryTemplate()) {
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001068 while (Primary->getInstantiatedFromMemberTemplate()) {
1069 // If we have hit a point where the user provided a specialization of
1070 // this template, we're done looking.
1071 if (Primary->isMemberSpecialization())
1072 break;
1073
Douglas Gregor5ec178f2009-08-28 21:09:48 +00001074 Primary = Primary->getInstantiatedFromMemberTemplate();
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001075 }
1076
Douglas Gregor1637be72009-06-26 00:10:03 +00001077 PatternDecl = Primary->getTemplatedDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001078 } else
Douglas Gregor1637be72009-06-26 00:10:03 +00001079 PatternDecl = Function->getInstantiatedFromMemberFunction();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001080 Stmt *Pattern = 0;
1081 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001082 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001083
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001084 if (!Pattern) {
1085 if (DefinitionRequired) {
1086 if (Function->getPrimaryTemplate())
1087 Diag(PointOfInstantiation,
1088 diag::err_explicit_instantiation_undefined_func_template)
1089 << Function->getPrimaryTemplate();
1090 else
1091 Diag(PointOfInstantiation,
1092 diag::err_explicit_instantiation_undefined_member)
1093 << 1 << Function->getDeclName() << Function->getDeclContext();
1094
1095 if (PatternDecl)
1096 Diag(PatternDecl->getLocation(),
1097 diag::note_explicit_instantiation_here);
1098 }
1099
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001100 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001101 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001102
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001103 // C++0x [temp.explicit]p9:
1104 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00001105 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001106 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00001107 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001108 == TSK_ExplicitInstantiationDeclaration &&
1109 PatternDecl->isOutOfLine() && !PatternDecl->isInline())
1110 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001111
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001112 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
1113 if (Inst)
1114 return;
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001115
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001116 // If we're performing recursive template instantiation, create our own
1117 // queue of pending implicit instantiations that we will instantiate later,
1118 // while we're still within our own instantiation context.
1119 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1120 if (Recursive)
1121 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001122
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001123 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
1124
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001125 // Introduce a new scope where local variable instantiations will be
1126 // recorded.
1127 LocalInstantiationScope Scope(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001128
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001129 // Introduce the instantiated function parameters into the local
1130 // instantiation scope.
1131 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
1132 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
1133 Function->getParamDecl(I));
1134
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001135 // Enter the scope of this instantiation. We don't use
1136 // PushDeclContext because we don't have a scope.
1137 DeclContext *PreviousContext = CurContext;
1138 CurContext = Function;
1139
Mike Stump1eb44332009-09-09 15:08:12 +00001140 MultiLevelTemplateArgumentList TemplateArgs =
Anders Carlsson09025312009-08-29 05:16:22 +00001141 getTemplateInstantiationArgs(Function);
1142
1143 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001144 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00001145 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
1146 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
1147 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001148 }
1149
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001150 // Instantiate the function body.
Anders Carlsson09025312009-08-29 05:16:22 +00001151 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001152
Douglas Gregor52604ab2009-09-11 21:19:12 +00001153 if (Body.isInvalid())
1154 Function->setInvalidDecl();
1155
Mike Stump1eb44332009-09-09 15:08:12 +00001156 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001157 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001158
1159 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00001160
1161 DeclGroupRef DG(Function);
1162 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001163
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001164 if (Recursive) {
1165 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001166 // instantiation of this template.
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001167 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001168
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001169 // Restore the set of pending implicit instantiations.
1170 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1171 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001172}
1173
1174/// \brief Instantiate the definition of the given variable from its
1175/// template.
1176///
Douglas Gregor7caa6822009-07-24 20:34:43 +00001177/// \param PointOfInstantiation the point at which the instantiation was
1178/// required. Note that this is not precisely a "point of instantiation"
1179/// for the function, but it's close.
1180///
1181/// \param Var the already-instantiated declaration of a static member
1182/// variable of a class template specialization.
1183///
1184/// \param Recursive if true, recursively instantiates any functions that
1185/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001186///
1187/// \param DefinitionRequired if true, then we are performing an explicit
1188/// instantiation where an out-of-line definition of the member variable
1189/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001190void Sema::InstantiateStaticDataMemberDefinition(
1191 SourceLocation PointOfInstantiation,
1192 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001193 bool Recursive,
1194 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001195 if (Var->isInvalidDecl())
1196 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001197
Douglas Gregor7caa6822009-07-24 20:34:43 +00001198 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001199 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
1200 bool FoundOutOfLineDef = false;
1201 assert(Def && "This data member was not instantiated from a template?");
Mike Stump1eb44332009-09-09 15:08:12 +00001202 assert(Def->isStaticDataMember() && "Not a static data member?");
1203 for (VarDecl::redecl_iterator RD = Def->redecls_begin(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00001204 RDEnd = Def->redecls_end();
1205 RD != RDEnd; ++RD) {
1206 if (RD->getLexicalDeclContext()->isFileContext()) {
1207 Def = *RD;
1208 FoundOutOfLineDef = true;
1209 }
1210 }
Mike Stump1eb44332009-09-09 15:08:12 +00001211
Douglas Gregor7caa6822009-07-24 20:34:43 +00001212 if (!FoundOutOfLineDef) {
1213 // We did not find an out-of-line definition of this static data member,
1214 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00001215 // instantiate this definition (or provide a specialization for it) in
1216 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001217 if (DefinitionRequired) {
1218 Diag(PointOfInstantiation,
1219 diag::err_explicit_instantiation_undefined_member)
1220 << 2 << Var->getDeclName() << Var->getDeclContext();
1221 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
1222 }
1223
Douglas Gregor7caa6822009-07-24 20:34:43 +00001224 return;
1225 }
1226
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001227 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001228 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001229 return;
1230
1231 // C++0x [temp.explicit]p9:
1232 // Except for inline functions, other explicit instantiation declarations
1233 // have the effect of suppressing the implicit instantiation of the entity
1234 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001235 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001236 == TSK_ExplicitInstantiationDeclaration)
1237 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001238
Douglas Gregor7caa6822009-07-24 20:34:43 +00001239 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
1240 if (Inst)
1241 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001242
Douglas Gregor7caa6822009-07-24 20:34:43 +00001243 // If we're performing recursive template instantiation, create our own
1244 // queue of pending implicit instantiations that we will instantiate later,
1245 // while we're still within our own instantiation context.
1246 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1247 if (Recursive)
1248 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001249
Douglas Gregor7caa6822009-07-24 20:34:43 +00001250 // Enter the scope of this instantiation. We don't use
1251 // PushDeclContext because we don't have a scope.
1252 DeclContext *PreviousContext = CurContext;
1253 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001254
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001255 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00001256 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00001257 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00001258 CurContext = PreviousContext;
1259
1260 if (Var) {
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001261 Var->setPreviousDeclaration(OldVar);
Douglas Gregor583f33b2009-10-15 18:07:02 +00001262 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
1263 assert(MSInfo && "Missing member specialization information?");
1264 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
1265 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00001266 DeclGroupRef DG(Var);
1267 Consumer.HandleTopLevelDecl(DG);
1268 }
Mike Stump1eb44332009-09-09 15:08:12 +00001269
Douglas Gregor7caa6822009-07-24 20:34:43 +00001270 if (Recursive) {
1271 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001272 // instantiation of this template.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001273 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001274
Douglas Gregor7caa6822009-07-24 20:34:43 +00001275 // Restore the set of pending implicit instantiations.
1276 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001277 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001278}
Douglas Gregor815215d2009-05-27 05:35:12 +00001279
Anders Carlsson09025312009-08-29 05:16:22 +00001280void
1281Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
1282 const CXXConstructorDecl *Tmpl,
1283 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001284
Anders Carlsson09025312009-08-29 05:16:22 +00001285 llvm::SmallVector<MemInitTy*, 4> NewInits;
1286
1287 // Instantiate all the initializers.
1288 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00001289 InitsEnd = Tmpl->init_end();
1290 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00001291 CXXBaseOrMemberInitializer *Init = *Inits;
1292
1293 ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001294
Anders Carlsson09025312009-08-29 05:16:22 +00001295 // Instantiate all the arguments.
1296 for (ExprIterator Args = Init->arg_begin(), ArgsEnd = Init->arg_end();
1297 Args != ArgsEnd; ++Args) {
1298 OwningExprResult NewArg = SubstExpr(*Args, TemplateArgs);
1299
1300 if (NewArg.isInvalid())
1301 New->setInvalidDecl();
1302 else
1303 NewArgs.push_back(NewArg.takeAs<Expr>());
1304 }
1305
1306 MemInitResult NewInit;
1307
1308 if (Init->isBaseInitializer()) {
Eli Friedmanc5573a82009-08-29 22:22:07 +00001309 QualType BaseType(Init->getBaseClass(), 0);
1310 BaseType = SubstType(BaseType, TemplateArgs, Init->getSourceLocation(),
1311 New->getDeclName());
Anders Carlsson09025312009-08-29 05:16:22 +00001312
1313 NewInit = BuildBaseInitializer(BaseType,
Mike Stump1eb44332009-09-09 15:08:12 +00001314 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001315 NewArgs.size(),
1316 Init->getSourceLocation(),
1317 Init->getRParenLoc(),
1318 New->getParent());
1319 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001320 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00001321
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001322 // Is this an anonymous union?
1323 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregore95b4092009-09-16 18:34:49 +00001324 Member = cast<FieldDecl>(FindInstantiatedDecl(UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001325 else
Douglas Gregore95b4092009-09-16 18:34:49 +00001326 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMember(),
1327 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00001328
1329 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001330 NewArgs.size(),
1331 Init->getSourceLocation(),
1332 Init->getRParenLoc());
1333 }
1334
1335 if (NewInit.isInvalid())
1336 New->setInvalidDecl();
1337 else {
1338 // FIXME: It would be nice if ASTOwningVector had a release function.
1339 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001340
Anders Carlsson09025312009-08-29 05:16:22 +00001341 NewInits.push_back((MemInitTy *)NewInit.get());
1342 }
1343 }
Mike Stump1eb44332009-09-09 15:08:12 +00001344
Anders Carlsson09025312009-08-29 05:16:22 +00001345 // Assign all the initializers to the new constructor.
Mike Stump1eb44332009-09-09 15:08:12 +00001346 ActOnMemInitializers(DeclPtrTy::make(New),
Anders Carlsson09025312009-08-29 05:16:22 +00001347 /*FIXME: ColonLoc */
1348 SourceLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001349 NewInits.data(), NewInits.size());
Anders Carlsson09025312009-08-29 05:16:22 +00001350}
1351
John McCall52a575a2009-08-29 08:11:13 +00001352// TODO: this could be templated if the various decl types used the
1353// same method name.
1354static bool isInstantiationOf(ClassTemplateDecl *Pattern,
1355 ClassTemplateDecl *Instance) {
1356 Pattern = Pattern->getCanonicalDecl();
1357
1358 do {
1359 Instance = Instance->getCanonicalDecl();
1360 if (Pattern == Instance) return true;
1361 Instance = Instance->getInstantiatedFromMemberTemplate();
1362 } while (Instance);
1363
1364 return false;
1365}
1366
Douglas Gregor0d696532009-09-28 06:34:35 +00001367static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
1368 FunctionTemplateDecl *Instance) {
1369 Pattern = Pattern->getCanonicalDecl();
1370
1371 do {
1372 Instance = Instance->getCanonicalDecl();
1373 if (Pattern == Instance) return true;
1374 Instance = Instance->getInstantiatedFromMemberTemplate();
1375 } while (Instance);
1376
1377 return false;
1378}
1379
John McCall52a575a2009-08-29 08:11:13 +00001380static bool isInstantiationOf(CXXRecordDecl *Pattern,
1381 CXXRecordDecl *Instance) {
1382 Pattern = Pattern->getCanonicalDecl();
1383
1384 do {
1385 Instance = Instance->getCanonicalDecl();
1386 if (Pattern == Instance) return true;
1387 Instance = Instance->getInstantiatedFromMemberClass();
1388 } while (Instance);
1389
1390 return false;
1391}
1392
1393static bool isInstantiationOf(FunctionDecl *Pattern,
1394 FunctionDecl *Instance) {
1395 Pattern = Pattern->getCanonicalDecl();
1396
1397 do {
1398 Instance = Instance->getCanonicalDecl();
1399 if (Pattern == Instance) return true;
1400 Instance = Instance->getInstantiatedFromMemberFunction();
1401 } while (Instance);
1402
1403 return false;
1404}
1405
1406static bool isInstantiationOf(EnumDecl *Pattern,
1407 EnumDecl *Instance) {
1408 Pattern = Pattern->getCanonicalDecl();
1409
1410 do {
1411 Instance = Instance->getCanonicalDecl();
1412 if (Pattern == Instance) return true;
1413 Instance = Instance->getInstantiatedFromMemberEnum();
1414 } while (Instance);
1415
1416 return false;
1417}
1418
Anders Carlsson0d8df782009-08-29 19:37:28 +00001419static bool isInstantiationOf(UnresolvedUsingDecl *Pattern,
1420 UsingDecl *Instance,
1421 ASTContext &C) {
1422 return C.getInstantiatedFromUnresolvedUsingDecl(Instance) == Pattern;
1423}
1424
John McCall52a575a2009-08-29 08:11:13 +00001425static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
1426 VarDecl *Instance) {
1427 assert(Instance->isStaticDataMember());
1428
1429 Pattern = Pattern->getCanonicalDecl();
1430
1431 do {
1432 Instance = Instance->getCanonicalDecl();
1433 if (Pattern == Instance) return true;
1434 Instance = Instance->getInstantiatedFromStaticDataMember();
1435 } while (Instance);
1436
1437 return false;
1438}
1439
Douglas Gregor815215d2009-05-27 05:35:12 +00001440static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00001441 if (D->getKind() != Other->getKind()) {
1442 if (UnresolvedUsingDecl *UUD = dyn_cast<UnresolvedUsingDecl>(D)) {
1443 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
1444 return isInstantiationOf(UUD, UD, Ctx);
1445 }
1446 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001447
Anders Carlsson0d8df782009-08-29 19:37:28 +00001448 return false;
1449 }
Mike Stump1eb44332009-09-09 15:08:12 +00001450
John McCall52a575a2009-08-29 08:11:13 +00001451 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
1452 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001453
John McCall52a575a2009-08-29 08:11:13 +00001454 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
1455 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00001456
John McCall52a575a2009-08-29 08:11:13 +00001457 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
1458 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00001459
Douglas Gregor7caa6822009-07-24 20:34:43 +00001460 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00001461 if (Var->isStaticDataMember())
1462 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
1463
1464 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
1465 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00001466
Douglas Gregor0d696532009-09-28 06:34:35 +00001467 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
1468 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
1469
Anders Carlssond8b285f2009-09-01 04:26:58 +00001470 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
1471 if (!Field->getDeclName()) {
1472 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00001473 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00001474 cast<FieldDecl>(D);
1475 }
1476 }
Mike Stump1eb44332009-09-09 15:08:12 +00001477
Douglas Gregor815215d2009-05-27 05:35:12 +00001478 return D->getDeclName() && isa<NamedDecl>(Other) &&
1479 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
1480}
1481
1482template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00001483static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00001484 NamedDecl *D,
1485 ForwardIterator first,
1486 ForwardIterator last) {
1487 for (; first != last; ++first)
1488 if (isInstantiationOf(Ctx, D, *first))
1489 return cast<NamedDecl>(*first);
1490
1491 return 0;
1492}
1493
John McCall02cace72009-08-28 07:59:38 +00001494/// \brief Finds the instantiation of the given declaration context
1495/// within the current instantiation.
1496///
1497/// \returns NULL if there was an error
Douglas Gregore95b4092009-09-16 18:34:49 +00001498DeclContext *Sema::FindInstantiatedContext(DeclContext* DC,
1499 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00001500 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00001501 Decl* ID = FindInstantiatedDecl(D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00001502 return cast_or_null<DeclContext>(ID);
1503 } else return DC;
1504}
1505
Douglas Gregored961e72009-05-27 17:54:46 +00001506/// \brief Find the instantiation of the given declaration within the
1507/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00001508///
1509/// This routine is intended to be used when \p D is a declaration
1510/// referenced from within a template, that needs to mapped into the
1511/// corresponding declaration within an instantiation. For example,
1512/// given:
1513///
1514/// \code
1515/// template<typename T>
1516/// struct X {
1517/// enum Kind {
1518/// KnownValue = sizeof(T)
1519/// };
1520///
1521/// bool getKind() const { return KnownValue; }
1522/// };
1523///
1524/// template struct X<int>;
1525/// \endcode
1526///
1527/// In the instantiation of X<int>::getKind(), we need to map the
1528/// EnumConstantDecl for KnownValue (which refers to
1529/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00001530/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
1531/// this mapping from within the instantiation of X<int>.
Douglas Gregore95b4092009-09-16 18:34:49 +00001532NamedDecl *Sema::FindInstantiatedDecl(NamedDecl *D,
1533 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor44c73842009-09-01 17:53:10 +00001534 if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D)) {
1535 // Transform all of the elements of the overloaded function set.
Mike Stump1eb44332009-09-09 15:08:12 +00001536 OverloadedFunctionDecl *Result
Douglas Gregor44c73842009-09-01 17:53:10 +00001537 = OverloadedFunctionDecl::Create(Context, CurContext, Ovl->getDeclName());
Mike Stump1eb44332009-09-09 15:08:12 +00001538
Douglas Gregor44c73842009-09-01 17:53:10 +00001539 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
1540 FEnd = Ovl->function_end();
1541 F != FEnd; ++F) {
1542 Result->addOverload(
Douglas Gregore95b4092009-09-16 18:34:49 +00001543 AnyFunctionDecl::getFromNamedDecl(FindInstantiatedDecl(*F,
1544 TemplateArgs)));
Douglas Gregor44c73842009-09-01 17:53:10 +00001545 }
Mike Stump1eb44332009-09-09 15:08:12 +00001546
Douglas Gregor44c73842009-09-01 17:53:10 +00001547 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001548 }
1549
Douglas Gregor815215d2009-05-27 05:35:12 +00001550 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor2bba76b2009-05-27 17:07:49 +00001551 if (isa<ParmVarDecl>(D) || ParentDC->isFunctionOrMethod()) {
1552 // D is a local of some kind. Look into the map of local
1553 // declarations to their instantiations.
1554 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
1555 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001556
Douglas Gregore95b4092009-09-16 18:34:49 +00001557 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
1558 if (!Record->isDependentContext())
1559 return D;
1560
1561 // If the RecordDecl is actually the injected-class-name or a "templated"
1562 // declaration for a class template or class template partial
1563 // specialization, substitute into the injected-class-name of the
1564 // class template or partial specialization to find the new DeclContext.
1565 QualType T;
1566 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
1567
1568 if (ClassTemplate) {
1569 T = ClassTemplate->getInjectedClassNameType(Context);
1570 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
1571 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1572 T = Context.getTypeDeclType(Record);
1573 ClassTemplate = PartialSpec->getSpecializedTemplate();
1574 }
1575
1576 if (!T.isNull()) {
1577 // Substitute into the injected-class-name to get the type corresponding
1578 // to the instantiation we want. This substitution should never fail,
1579 // since we know we can instantiate the injected-class-name or we wouldn't
1580 // have gotten to the injected-class-name!
1581 // FIXME: Can we use the CurrentInstantiationScope to avoid this extra
1582 // instantiation in the common case?
1583 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
1584 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
1585
1586 if (!T->isDependentType()) {
1587 assert(T->isRecordType() && "Instantiation must produce a record type");
1588 return T->getAs<RecordType>()->getDecl();
1589 }
1590
1591 // We are performing "partial" template instantiation to create the
1592 // member declarations for the members of a class template
1593 // specialization. Therefore, D is actually referring to something in
1594 // the current instantiation. Look through the current context,
1595 // which contains actual instantiations, to find the instantiation of
1596 // the "current instantiation" that D refers to.
Mike Stump1eb44332009-09-09 15:08:12 +00001597 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00001598 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001599 if (ClassTemplateSpecializationDecl *Spec
John McCall52a575a2009-08-29 08:11:13 +00001600 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00001601 if (isInstantiationOf(ClassTemplate,
1602 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00001603 return Spec;
1604 }
1605
Mike Stump1eb44332009-09-09 15:08:12 +00001606 assert(false &&
John McCall52a575a2009-08-29 08:11:13 +00001607 "Unable to find declaration for the current instantiation");
Douglas Gregore95b4092009-09-16 18:34:49 +00001608 return Record;
John McCall52a575a2009-08-29 08:11:13 +00001609 }
Douglas Gregore95b4092009-09-16 18:34:49 +00001610
1611 // Fall through to deal with other dependent record types (e.g.,
1612 // anonymous unions in class templates).
1613 }
John McCall52a575a2009-08-29 08:11:13 +00001614
Douglas Gregore95b4092009-09-16 18:34:49 +00001615 if (!ParentDC->isDependentContext())
1616 return D;
1617
1618 ParentDC = FindInstantiatedContext(ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001619 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00001620 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001621
Douglas Gregor815215d2009-05-27 05:35:12 +00001622 if (ParentDC != D->getDeclContext()) {
1623 // We performed some kind of instantiation in the parent context,
1624 // so now we need to look into the instantiated parent context to
1625 // find the instantiation of the declaration D.
1626 NamedDecl *Result = 0;
1627 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001628 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00001629 Result = findInstantiationOf(Context, D, Found.first, Found.second);
1630 } else {
1631 // Since we don't have a name for the entity we're looking for,
1632 // our only option is to walk through all of the declarations to
1633 // find that name. This will occur in a few cases:
1634 //
1635 // - anonymous struct/union within a template
1636 // - unnamed class/struct/union/enum within a template
1637 //
1638 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00001639 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001640 ParentDC->decls_begin(),
1641 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00001642 }
Mike Stump1eb44332009-09-09 15:08:12 +00001643
Douglas Gregor815215d2009-05-27 05:35:12 +00001644 assert(Result && "Unable to find instantiation of declaration!");
1645 D = Result;
1646 }
1647
Douglas Gregor815215d2009-05-27 05:35:12 +00001648 return D;
1649}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001650
Mike Stump1eb44332009-09-09 15:08:12 +00001651/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001652/// instantiations we have seen until this point.
1653void Sema::PerformPendingImplicitInstantiations() {
1654 while (!PendingImplicitInstantiations.empty()) {
1655 PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front();
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001656 PendingImplicitInstantiations.pop_front();
Mike Stump1eb44332009-09-09 15:08:12 +00001657
Douglas Gregor7caa6822009-07-24 20:34:43 +00001658 // Instantiate function definitions
1659 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001660 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001661 Function->getLocation(), *this,
1662 Context.getSourceManager(),
1663 "instantiating function definition");
Mike Stump1eb44332009-09-09 15:08:12 +00001664
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001665 if (!Function->getBody())
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001666 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001667 continue;
1668 }
Mike Stump1eb44332009-09-09 15:08:12 +00001669
Douglas Gregor7caa6822009-07-24 20:34:43 +00001670 // Instantiate static data member definitions.
1671 VarDecl *Var = cast<VarDecl>(Inst.first);
1672 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001673
Mike Stump1eb44332009-09-09 15:08:12 +00001674 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001675 Var->getLocation(), *this,
1676 Context.getSourceManager(),
1677 "instantiating static data member "
1678 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00001679
Douglas Gregor7caa6822009-07-24 20:34:43 +00001680 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001681 }
1682}