blob: 060cc559833fd2dc83acf35f2ca8055087d580c0 [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
126 QualType T = SemaRef.SubstType(D->getType(), TemplateArgs,
127 D->getTypeSpecStartLoc(),
128 D->getDeclName());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000129 if (T.isNull())
130 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(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000135 T, D->getDeclaratorInfo(),
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;
206 QualType T = D->getType();
207 if (T->isDependentType()) {
John McCallce3ff2b2009-08-25 22:02:44 +0000208 T = SemaRef.SubstType(T, TemplateArgs,
209 D->getLocation(), D->getDeclName());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000210 if (!T.isNull() && T->isFunctionType()) {
211 // C++ [temp.arg.type]p3:
212 // If a declaration acquires a function type through a type
213 // dependent on a template-parameter and this causes a
214 // declaration that does not use the syntactic form of a
215 // function declarator to have function type, the program is
216 // ill-formed.
217 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
218 << T;
219 T = QualType();
220 Invalid = true;
221 }
222 }
223
224 Expr *BitWidth = D->getBitWidth();
225 if (Invalid)
226 BitWidth = 0;
227 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000228 // The bit-width expression is not potentially evaluated.
229 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000230
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000231 OwningExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000232 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000233 if (InstantiatedBitWidth.isInvalid()) {
234 Invalid = true;
235 BitWidth = 0;
236 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000237 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000238 }
239
240 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), T,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000241 D->getDeclaratorInfo(),
Mike Stump1eb44332009-09-09 15:08:12 +0000242 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000243 D->getLocation(),
244 D->isMutable(),
245 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000246 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000247 D->getAccess(),
248 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000249 if (!Field) {
250 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000251 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000252 }
Mike Stump1eb44332009-09-09 15:08:12 +0000253
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000254 if (Invalid)
255 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000257 if (!Field->getDeclName()) {
258 // Keep track of where this decl came from.
259 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000260 }
Mike Stump1eb44332009-09-09 15:08:12 +0000261
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000262 Field->setImplicit(D->isImplicit());
263 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000264
265 return Field;
266}
267
John McCall02cace72009-08-28 07:59:38 +0000268Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
269 FriendDecl::FriendUnion FU;
270
271 // Handle friend type expressions by simply substituting template
272 // parameters into the pattern type.
273 if (Type *Ty = D->getFriendType()) {
274 QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs,
275 D->getLocation(), DeclarationName());
276 if (T.isNull()) return 0;
277
278 assert(getLangOptions().CPlusPlus0x || T->isRecordType());
279 FU = T.getTypePtr();
280
281 // Handle everything else by appropriate substitution.
282 } else {
283 NamedDecl *ND = D->getFriendDecl();
284 assert(ND && "friend decl must be a decl or a type!");
285
Douglas Gregora735b202009-10-13 14:39:41 +0000286 // FIXME: We have a problem here, because the nested call to Visit(ND)
287 // will inject the thing that the friend references into the current
288 // owner, which is wrong.
John McCall02cace72009-08-28 07:59:38 +0000289 Decl *NewND = Visit(ND);
290 if (!NewND) return 0;
291
292 FU = cast<NamedDecl>(NewND);
John McCallfd810b12009-08-14 02:03:10 +0000293 }
Mike Stump1eb44332009-09-09 15:08:12 +0000294
John McCall02cace72009-08-28 07:59:38 +0000295 FriendDecl *FD =
296 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU,
297 D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000298 FD->setAccess(AS_public);
John McCall02cace72009-08-28 07:59:38 +0000299 Owner->addDecl(FD);
300 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000301}
302
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000303Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
304 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Douglas Gregorac7610d2009-06-22 20:57:11 +0000306 // The expression in a static assertion is not potentially evaluated.
307 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000309 OwningExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000310 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000311 if (InstantiatedAssertExpr.isInvalid())
312 return 0;
313
Douglas Gregor43d9d922009-08-08 01:41:12 +0000314 OwningExprResult Message(SemaRef, D->getMessage());
315 D->getMessage()->Retain();
Mike Stump1eb44332009-09-09 15:08:12 +0000316 Decl *StaticAssert
317 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
Chris Lattnerb28317a2009-03-28 19:18:32 +0000318 move(InstantiatedAssertExpr),
319 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000320 return StaticAssert;
321}
322
323Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000324 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000325 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000326 D->getTagKeywordLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000327 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000328 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000329 Enum->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000330 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000331 Enum->startDefinition();
332
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000333 llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000334
335 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000336 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
337 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000338 EC != ECEnd; ++EC) {
339 // The specified value for the enumerator.
340 OwningExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000341 if (Expr *UninstValue = EC->getInitExpr()) {
342 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000343 EnterExpressionEvaluationContext Unevaluated(SemaRef,
Douglas Gregorac7610d2009-06-22 20:57:11 +0000344 Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000345
John McCallce3ff2b2009-08-25 22:02:44 +0000346 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000347 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000348
349 // Drop the initial value and continue.
350 bool isInvalid = false;
351 if (Value.isInvalid()) {
352 Value = SemaRef.Owned((Expr *)0);
353 isInvalid = true;
354 }
355
Mike Stump1eb44332009-09-09 15:08:12 +0000356 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000357 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
358 EC->getLocation(), EC->getIdentifier(),
359 move(Value));
360
361 if (isInvalid) {
362 if (EnumConst)
363 EnumConst->setInvalidDecl();
364 Enum->setInvalidDecl();
365 }
366
367 if (EnumConst) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000368 Enum->addDecl(EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000369 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000370 LastEnumConst = EnumConst;
371 }
372 }
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000374 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000375 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000376 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
377 Sema::DeclPtrTy::make(Enum),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000378 &Enumerators[0], Enumerators.size(),
379 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000380
381 return Enum;
382}
383
Douglas Gregor6477b692009-03-25 15:04:13 +0000384Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
385 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
386 return 0;
387}
388
John McCalle29ba202009-08-20 01:44:21 +0000389Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
390 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000391 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000392 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000393 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000394
395 CXXRecordDecl *Pattern = D->getTemplatedDecl();
396 CXXRecordDecl *RecordInst
397 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner,
398 Pattern->getLocation(), Pattern->getIdentifier(),
Douglas Gregorf0510d42009-10-12 23:11:44 +0000399 Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL,
400 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000401
402 ClassTemplateDecl *Inst
403 = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
404 D->getIdentifier(), InstParams, RecordInst, 0);
405 RecordInst->setDescribedClassTemplate(Inst);
406 Inst->setAccess(D->getAccess());
407 Inst->setInstantiatedFromMemberTemplate(D);
Douglas Gregorf0510d42009-10-12 23:11:44 +0000408
409 // Trigger creation of the type for the instantiation.
410 SemaRef.Context.getTypeDeclType(RecordInst);
411
John McCalle29ba202009-08-20 01:44:21 +0000412 Owner->addDecl(Inst);
413 return Inst;
414}
415
Douglas Gregord60e1052009-08-27 16:57:43 +0000416Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000417TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
418 ClassTemplatePartialSpecializationDecl *D) {
419 assert(false &&"Partial specializations of member templates are unsupported");
420 return 0;
421}
422
423Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000424TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000425 // FIXME: Dig out the out-of-line definition of this function template?
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Douglas Gregord60e1052009-08-27 16:57:43 +0000427 TemplateParameterList *TempParams = D->getTemplateParameters();
428 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000429 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000430 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Douglas Gregora735b202009-10-13 14:39:41 +0000432 FunctionDecl *Instantiated = 0;
433 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
434 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
435 InstParams));
436 else
437 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
438 D->getTemplatedDecl(),
439 InstParams));
440
441 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000442 return 0;
443
Mike Stump1eb44332009-09-09 15:08:12 +0000444 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000445 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000446 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000447 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000448 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000449 assert(InstTemplate &&
450 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
451 if (!InstTemplate->getInstantiatedFromMemberTemplate())
452 InstTemplate->setInstantiatedFromMemberTemplate(D);
453
454 // Add non-friends into the owner.
455 if (!InstTemplate->getFriendObjectKind())
456 Owner->addDecl(InstTemplate);
Douglas Gregord60e1052009-08-27 16:57:43 +0000457 return InstTemplate;
458}
459
Douglas Gregord475b8d2009-03-25 21:17:03 +0000460Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
461 CXXRecordDecl *PrevDecl = 0;
462 if (D->isInjectedClassName())
463 PrevDecl = cast<CXXRecordDecl>(Owner);
464
465 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000466 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000467 D->getLocation(), D->getIdentifier(),
468 D->getTagKeywordLoc(), PrevDecl);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000469 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000470 // FIXME: Check against AS_none is an ugly hack to work around the issue that
471 // the tag decls introduced by friend class declarations don't have an access
472 // specifier. Remove once this area of the code gets sorted out.
473 if (D->getAccess() != AS_none)
474 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000475 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000476 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000477
John McCall02cace72009-08-28 07:59:38 +0000478 // If the original function was part of a friend declaration,
479 // inherit its namespace state.
480 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
481 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
482
Anders Carlssond8b285f2009-09-01 04:26:58 +0000483 Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
484
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000485 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000486 return Record;
487}
488
John McCall02cace72009-08-28 07:59:38 +0000489/// Normal class members are of more specific types and therefore
490/// don't make it here. This function serves two purposes:
491/// 1) instantiating function templates
492/// 2) substituting friend declarations
493/// FIXME: preserve function definitions in case #2
Douglas Gregora735b202009-10-13 14:39:41 +0000494 Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
495 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000496 // Check whether there is already a function template specialization for
497 // this declaration.
498 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
499 void *InsertPos = 0;
Douglas Gregora735b202009-10-13 14:39:41 +0000500 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000501 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000502 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000503 TemplateArgs.getInnermost().getFlatArgumentList(),
504 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor828e2262009-07-29 16:09:57 +0000505 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000506
507 FunctionTemplateSpecializationInfo *Info
508 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor127102b2009-06-29 20:59:39 +0000509 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000510
Douglas Gregor127102b2009-06-29 20:59:39 +0000511 // If we already have a function template specialization, return it.
512 if (Info)
513 return Info->Function;
514 }
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Douglas Gregore53060f2009-06-25 22:08:12 +0000516 Sema::LocalInstantiationScope Scope(SemaRef);
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Douglas Gregore53060f2009-06-25 22:08:12 +0000518 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000519 QualType T = SubstFunctionType(D, Params);
Douglas Gregore53060f2009-06-25 22:08:12 +0000520 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000521 return 0;
John McCallfd810b12009-08-14 02:03:10 +0000522
Douglas Gregore53060f2009-06-25 22:08:12 +0000523 // Build the instantiated method declaration.
Douglas Gregore95b4092009-09-16 18:34:49 +0000524 DeclContext *DC = SemaRef.FindInstantiatedContext(D->getDeclContext(),
525 TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +0000526 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +0000527 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000528 D->getDeclName(), T, D->getDeclaratorInfo(),
529 D->getStorageClass(),
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000530 D->isInline(), D->hasWrittenPrototype());
John McCall02cace72009-08-28 07:59:38 +0000531 Function->setLexicalDeclContext(Owner);
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Douglas Gregore53060f2009-06-25 22:08:12 +0000533 // Attach the parameters
534 for (unsigned P = 0; P < Params.size(); ++P)
535 Params[P]->setOwningFunction(Function);
536 Function->setParams(SemaRef.Context, Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +0000537
Douglas Gregora735b202009-10-13 14:39:41 +0000538 if (TemplateParams) {
539 // Our resulting instantiation is actually a function template, since we
540 // are substituting only the outer template parameters. For example, given
541 //
542 // template<typename T>
543 // struct X {
544 // template<typename U> friend void f(T, U);
545 // };
546 //
547 // X<int> x;
548 //
549 // We are instantiating the friend function template "f" within X<int>,
550 // which means substituting int for T, but leaving "f" as a friend function
551 // template.
552 // Build the function template itself.
553 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Owner,
554 Function->getLocation(),
555 Function->getDeclName(),
556 TemplateParams, Function);
557 Function->setDescribedFunctionTemplate(FunctionTemplate);
558 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
John McCall02cace72009-08-28 07:59:38 +0000559 }
Douglas Gregora735b202009-10-13 14:39:41 +0000560
Douglas Gregore53060f2009-06-25 22:08:12 +0000561 if (InitFunctionInstantiation(Function, D))
562 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000563
Douglas Gregore53060f2009-06-25 22:08:12 +0000564 bool Redeclaration = false;
565 bool OverloadableAttrRequired = false;
Douglas Gregora735b202009-10-13 14:39:41 +0000566
Douglas Gregore53060f2009-06-25 22:08:12 +0000567 NamedDecl *PrevDecl = 0;
Douglas Gregora735b202009-10-13 14:39:41 +0000568 if (TemplateParams || !FunctionTemplate) {
569 // Look only into the namespace where the friend would be declared to
570 // find a previous declaration. This is the innermost enclosing namespace,
571 // as described in ActOnFriendFunctionDecl.
572 Sema::LookupResult R;
573 SemaRef.LookupQualifiedName(R, DC, Function->getDeclName(),
574 Sema::LookupOrdinaryName, true);
575
576 PrevDecl = R.getAsSingleDecl(SemaRef.Context);
577
578 // In C++, the previous declaration we find might be a tag type
579 // (class or enum). In this case, the new declaration will hide the
580 // tag type. Note that this does does not apply if we're declaring a
581 // typedef (C++ [dcl.typedef]p4).
582 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
583 PrevDecl = 0;
584 }
585
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000586 SemaRef.CheckFunctionDeclaration(Function, PrevDecl, false, Redeclaration,
Douglas Gregore53060f2009-06-25 22:08:12 +0000587 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000588
Douglas Gregora735b202009-10-13 14:39:41 +0000589 // If the original function was part of a friend declaration,
590 // inherit its namespace state and add it to the owner.
591 NamedDecl *FromFriendD
592 = TemplateParams? cast<NamedDecl>(D->getDescribedFunctionTemplate()) : D;
593 if (FromFriendD->getFriendObjectKind()) {
594 NamedDecl *ToFriendD = 0;
595 if (TemplateParams) {
596 ToFriendD = cast<NamedDecl>(FunctionTemplate);
597 PrevDecl = FunctionTemplate->getPreviousDeclaration();
598 } else {
599 ToFriendD = Function;
600 PrevDecl = Function->getPreviousDeclaration();
601 }
602 ToFriendD->setObjectOfFriendDecl(PrevDecl != NULL);
603 if (!Owner->isDependentContext() && !PrevDecl)
604 DC->makeDeclVisibleInContext(ToFriendD, /* Recoverable = */ false);
605
606 if (!TemplateParams)
607 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
608 }
609
610 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000611 // Record this function template specialization.
612 Function->setFunctionTemplateSpecialization(SemaRef.Context,
613 FunctionTemplate,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000614 &TemplateArgs.getInnermost(),
Douglas Gregor127102b2009-06-29 20:59:39 +0000615 InsertPos);
John McCallfd810b12009-08-14 02:03:10 +0000616 }
617
Douglas Gregore53060f2009-06-25 22:08:12 +0000618 return Function;
619}
620
Douglas Gregord60e1052009-08-27 16:57:43 +0000621Decl *
622TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
623 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +0000624 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
625 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +0000626 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +0000627 // We are creating a function template specialization from a function
628 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +0000629 // specialization for this particular set of template arguments.
Douglas Gregor6b906862009-08-21 00:16:32 +0000630 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000631 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000632 TemplateArgs.getInnermost().getFlatArgumentList(),
633 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000634 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000635
636 FunctionTemplateSpecializationInfo *Info
637 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor6b906862009-08-21 00:16:32 +0000638 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000639
Douglas Gregor6b906862009-08-21 00:16:32 +0000640 // If we already have a function template specialization, return it.
641 if (Info)
642 return Info->Function;
643 }
644
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000645 Sema::LocalInstantiationScope Scope(SemaRef);
646
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000647 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000648 QualType T = SubstFunctionType(D, Params);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000649 if (T.isNull())
650 return 0;
651
652 // Build the instantiated method declaration.
653 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
Douglas Gregordec06662009-08-21 18:42:58 +0000654 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Douglas Gregordec06662009-08-21 18:42:58 +0000656 DeclarationName Name = D->getDeclName();
Douglas Gregor17e32f32009-08-21 22:43:28 +0000657 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregordec06662009-08-21 18:42:58 +0000658 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
659 Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
660 SemaRef.Context.getCanonicalType(ClassTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000661 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
662 Constructor->getLocation(),
663 Name, T,
Douglas Gregor17e32f32009-08-21 22:43:28 +0000664 Constructor->getDeclaratorInfo(),
Mike Stump1eb44332009-09-09 15:08:12 +0000665 Constructor->isExplicit(),
Douglas Gregor17e32f32009-08-21 22:43:28 +0000666 Constructor->isInline(), false);
667 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
668 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
669 Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
670 SemaRef.Context.getCanonicalType(ClassTy));
671 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
672 Destructor->getLocation(), Name,
673 T, Destructor->isInline(), false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000674 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000675 CanQualType ConvTy
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000676 = SemaRef.Context.getCanonicalType(
John McCall183700f2009-09-21 23:43:11 +0000677 T->getAs<FunctionType>()->getResultType());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000678 Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
679 ConvTy);
680 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
681 Conversion->getLocation(), Name,
682 T, Conversion->getDeclaratorInfo(),
Mike Stump1eb44332009-09-09 15:08:12 +0000683 Conversion->isInline(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000684 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +0000685 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000686 Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
Douglas Gregordec06662009-08-21 18:42:58 +0000687 D->getDeclName(), T, D->getDeclaratorInfo(),
688 D->isStatic(), D->isInline());
689 }
Douglas Gregor6b906862009-08-21 00:16:32 +0000690
Douglas Gregord60e1052009-08-27 16:57:43 +0000691 if (TemplateParams) {
692 // Our resulting instantiation is actually a function template, since we
693 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +0000694 //
Douglas Gregord60e1052009-08-27 16:57:43 +0000695 // template<typename T>
696 // struct X {
697 // template<typename U> void f(T, U);
698 // };
699 //
700 // X<int> x;
701 //
702 // We are instantiating the member template "f" within X<int>, which means
703 // substituting int for T, but leaving "f" as a member function template.
704 // Build the function template itself.
705 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
706 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +0000707 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +0000708 TemplateParams, Method);
709 if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +0000710 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +0000711 Method->setDescribedFunctionTemplate(FunctionTemplate);
712 } else if (!FunctionTemplate)
Douglas Gregor2db32322009-10-07 23:56:10 +0000713 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000714
Mike Stump1eb44332009-09-09 15:08:12 +0000715 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000716 // out-of-line, the instantiation will have the same lexical
717 // context (which will be a namespace scope) as the template.
718 if (D->isOutOfLine())
719 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000720
Douglas Gregor5545e162009-03-24 00:38:23 +0000721 // Attach the parameters
722 for (unsigned P = 0; P < Params.size(); ++P)
723 Params[P]->setOwningFunction(Method);
Jay Foadbeaaccd2009-05-21 09:52:38 +0000724 Method->setParams(SemaRef.Context, Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +0000725
726 if (InitMethodInstantiation(Method, D))
727 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000728
Douglas Gregordec06662009-08-21 18:42:58 +0000729 NamedDecl *PrevDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000730
Douglas Gregord60e1052009-08-27 16:57:43 +0000731 if (!FunctionTemplate || TemplateParams) {
John McCallf36e02d2009-10-09 21:13:30 +0000732 Sema::LookupResult R;
733 SemaRef.LookupQualifiedName(R, Owner, Name, Sema::LookupOrdinaryName, true);
734 PrevDecl = R.getAsSingleDecl(SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000735
Douglas Gregordec06662009-08-21 18:42:58 +0000736 // In C++, the previous declaration we find might be a tag type
737 // (class or enum). In this case, the new declaration will hide the
738 // tag type. Note that this does does not apply if we're declaring a
739 // typedef (C++ [dcl.typedef]p4).
740 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
741 PrevDecl = 0;
742 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000743
Douglas Gregord60e1052009-08-27 16:57:43 +0000744 if (FunctionTemplate && !TemplateParams)
Douglas Gregor6b906862009-08-21 00:16:32 +0000745 // Record this function template specialization.
746 Method->setFunctionTemplateSpecialization(SemaRef.Context,
747 FunctionTemplate,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000748 &TemplateArgs.getInnermost(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000749 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000750
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000751 bool Redeclaration = false;
752 bool OverloadableAttrRequired = false;
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000753 SemaRef.CheckFunctionDeclaration(Method, PrevDecl, false, Redeclaration,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000754 /*FIXME:*/OverloadableAttrRequired);
755
Douglas Gregora735b202009-10-13 14:39:41 +0000756 if (!FunctionTemplate && (!Method->isInvalidDecl() || !PrevDecl) &&
757 !Method->getFriendObjectKind())
Douglas Gregordec06662009-08-21 18:42:58 +0000758 Owner->addDecl(Method);
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000760 return Method;
761}
762
Douglas Gregor615c5d42009-03-24 16:43:20 +0000763Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +0000764 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000765}
766
Douglas Gregor03b2b072009-03-24 00:15:49 +0000767Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +0000768 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +0000769}
770
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000771Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000772 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000773}
774
Douglas Gregor6477b692009-03-25 15:04:13 +0000775ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000776 QualType OrigT = SemaRef.SubstType(D->getOriginalType(), TemplateArgs,
Douglas Gregor7e063902009-05-11 23:53:27 +0000777 D->getLocation(), D->getDeclName());
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000778 if (OrigT.isNull())
779 return 0;
780
781 QualType T = SemaRef.adjustParameterType(OrigT);
782
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000783 // Allocate the parameter
784 ParmVarDecl *Param = 0;
785 if (T == OrigT)
786 Param = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000787 D->getIdentifier(), T, D->getDeclaratorInfo(),
788 D->getStorageClass(), 0);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000789 else
Mike Stump1eb44332009-09-09 15:08:12 +0000790 Param = OriginalParmVarDecl::Create(SemaRef.Context, Owner,
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000791 D->getLocation(), D->getIdentifier(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000792 T, D->getDeclaratorInfo(), OrigT,
793 D->getStorageClass(), 0);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000794
Anders Carlsson9351c172009-08-25 03:18:48 +0000795 // Mark the default argument as being uninstantiated.
Douglas Gregorf43d0b32009-09-25 06:56:31 +0000796 if (D->hasUninstantiatedDefaultArg())
797 Param->setUninstantiatedDefaultArg(D->getUninstantiatedDefaultArg());
Douglas Gregor0ed09302009-09-25 07:03:22 +0000798 else if (Expr *Arg = D->getDefaultArg())
799 Param->setUninstantiatedDefaultArg(Arg);
800
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000801 // Note: we don't try to instantiate function parameters until after
802 // we've instantiated the function's type. Therefore, we don't have
803 // to check for 'void' parameter types here.
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000804 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000805 return Param;
806}
807
808Decl *
809TemplateDeclInstantiator::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
810 // Since parameter types can decay either before or after
811 // instantiation, we simply treat OriginalParmVarDecls as
812 // ParmVarDecls the same way, and create one or the other depending
813 // on what happens after template instantiation.
814 return VisitParmVarDecl(D);
815}
816
John McCalle29ba202009-08-20 01:44:21 +0000817Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
818 TemplateTypeParmDecl *D) {
819 // TODO: don't always clone when decls are refcounted.
820 const Type* T = D->getTypeForDecl();
821 assert(T->isTemplateTypeParmType());
822 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000823
John McCalle29ba202009-08-20 01:44:21 +0000824 TemplateTypeParmDecl *Inst =
825 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
826 TTPT->getDepth(), TTPT->getIndex(),
827 TTPT->getName(),
828 D->wasDeclaredWithTypename(),
829 D->isParameterPack());
830
831 if (D->hasDefaultArgument()) {
832 QualType DefaultPattern = D->getDefaultArgument();
833 QualType DefaultInst
John McCallce3ff2b2009-08-25 22:02:44 +0000834 = SemaRef.SubstType(DefaultPattern, TemplateArgs,
835 D->getDefaultArgumentLoc(),
836 D->getDeclName());
Mike Stump1eb44332009-09-09 15:08:12 +0000837
John McCalle29ba202009-08-20 01:44:21 +0000838 Inst->setDefaultArgument(DefaultInst,
839 D->getDefaultArgumentLoc(),
840 D->defaultArgumentWasInherited() /* preserve? */);
841 }
842
843 return Inst;
844}
845
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000846Decl *
847TemplateDeclInstantiator::VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000848 NestedNameSpecifier *NNS =
849 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
850 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000851 TemplateArgs);
852 if (!NNS)
853 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000854
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000855 CXXScopeSpec SS;
856 SS.setRange(D->getTargetNestedNameRange());
857 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +0000858
859 NamedDecl *UD =
860 SemaRef.BuildUsingDeclaration(D->getLocation(), SS,
861 D->getTargetNameLocation(),
Anders Carlsson0d8df782009-08-29 19:37:28 +0000862 D->getTargetName(), 0, D->isTypeName());
863 if (UD)
Mike Stump1eb44332009-09-09 15:08:12 +0000864 SemaRef.Context.setInstantiatedFromUnresolvedUsingDecl(cast<UsingDecl>(UD),
Anders Carlsson0d8df782009-08-29 19:37:28 +0000865 D);
866 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000867}
868
John McCallce3ff2b2009-08-25 22:02:44 +0000869Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000870 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +0000871 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000872 return Instantiator.Visit(D);
873}
874
John McCalle29ba202009-08-20 01:44:21 +0000875/// \brief Instantiates a nested template parameter list in the current
876/// instantiation context.
877///
878/// \param L The parameter list to instantiate
879///
880/// \returns NULL if there was an error
881TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +0000882TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +0000883 // Get errors for all the parameters before bailing out.
884 bool Invalid = false;
885
886 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000887 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +0000888 ParamVector Params;
889 Params.reserve(N);
890 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
891 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000892 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +0000893 Params.push_back(D);
894 Invalid = Invalid || !D;
895 }
896
897 // Clean up if we had an error.
898 if (Invalid) {
899 for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
900 PI != PE; ++PI)
901 if (*PI)
902 (*PI)->Destroy(SemaRef.Context);
903 return NULL;
904 }
905
906 TemplateParameterList *InstL
907 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
908 L->getLAngleLoc(), &Params.front(), N,
909 L->getRAngleLoc());
910 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +0000911}
John McCalle29ba202009-08-20 01:44:21 +0000912
John McCallce3ff2b2009-08-25 22:02:44 +0000913/// \brief Does substitution on the type of the given function, including
914/// all of the function parameters.
Douglas Gregor5545e162009-03-24 00:38:23 +0000915///
John McCallce3ff2b2009-08-25 22:02:44 +0000916/// \param D The function whose type will be the basis of the substitution
Douglas Gregor5545e162009-03-24 00:38:23 +0000917///
918/// \param Params the instantiated parameter declarations
919
John McCallce3ff2b2009-08-25 22:02:44 +0000920/// \returns the instantiated function's type if successful, a NULL
Douglas Gregor5545e162009-03-24 00:38:23 +0000921/// type if there was an error.
Mike Stump1eb44332009-09-09 15:08:12 +0000922QualType
John McCallce3ff2b2009-08-25 22:02:44 +0000923TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +0000924 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
925 bool InvalidDecl = false;
926
John McCallce3ff2b2009-08-25 22:02:44 +0000927 // Substitute all of the function's formal parameter types.
Douglas Gregor7e063902009-05-11 23:53:27 +0000928 TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000929 llvm::SmallVector<QualType, 4> ParamTys;
Mike Stump1eb44332009-09-09 15:08:12 +0000930 for (FunctionDecl::param_iterator P = D->param_begin(),
Douglas Gregor5545e162009-03-24 00:38:23 +0000931 PEnd = D->param_end();
932 P != PEnd; ++P) {
Douglas Gregor6477b692009-03-25 15:04:13 +0000933 if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
Douglas Gregor5545e162009-03-24 00:38:23 +0000934 if (PInst->getType()->isVoidType()) {
935 SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
936 PInst->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000937 } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000938 PInst->getType(),
939 diag::err_abstract_type_in_decl,
940 Sema::AbstractParamType))
Douglas Gregor5545e162009-03-24 00:38:23 +0000941 PInst->setInvalidDecl();
942
943 Params.push_back(PInst);
944 ParamTys.push_back(PInst->getType());
945
946 if (PInst->isInvalidDecl())
947 InvalidDecl = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000948 } else
Douglas Gregor5545e162009-03-24 00:38:23 +0000949 InvalidDecl = true;
950 }
951
952 // FIXME: Deallocate dead declarations.
953 if (InvalidDecl)
954 return QualType();
955
John McCall183700f2009-09-21 23:43:11 +0000956 const FunctionProtoType *Proto = D->getType()->getAs<FunctionProtoType>();
Douglas Gregor5545e162009-03-24 00:38:23 +0000957 assert(Proto && "Missing prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +0000958 QualType ResultType
John McCallce3ff2b2009-08-25 22:02:44 +0000959 = SemaRef.SubstType(Proto->getResultType(), TemplateArgs,
960 D->getLocation(), D->getDeclName());
Douglas Gregor5545e162009-03-24 00:38:23 +0000961 if (ResultType.isNull())
962 return QualType();
963
Jay Foadbeaaccd2009-05-21 09:52:38 +0000964 return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
Douglas Gregor5545e162009-03-24 00:38:23 +0000965 Proto->isVariadic(), Proto->getTypeQuals(),
966 D->getLocation(), D->getDeclName());
967}
968
Mike Stump1eb44332009-09-09 15:08:12 +0000969/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +0000970/// declaration (New) from the corresponding fields of its template (Tmpl).
971///
972/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +0000973bool
974TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +0000975 FunctionDecl *Tmpl) {
976 if (Tmpl->isDeleted())
977 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Douglas Gregorcca9e962009-07-01 22:01:06 +0000979 // If we are performing substituting explicitly-specified template arguments
980 // or deduced template arguments into a function template and we reach this
981 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +0000982 // to keeping the new function template specialization. We therefore
983 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +0000984 // into a template instantiation for this specific function template
985 // specialization, which is not a SFINAE context, so that we diagnose any
986 // further errors in the declaration itself.
987 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
988 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
989 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
990 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +0000991 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +0000992 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000993 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +0000994 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +0000995 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +0000996 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
997 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
998 }
999 }
Mike Stump1eb44332009-09-09 15:08:12 +00001000
Douglas Gregore53060f2009-06-25 22:08:12 +00001001 return false;
1002}
1003
Douglas Gregor5545e162009-03-24 00:38:23 +00001004/// \brief Initializes common fields of an instantiated method
1005/// declaration (New) from the corresponding fields of its template
1006/// (Tmpl).
1007///
1008/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001009bool
1010TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00001011 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001012 if (InitFunctionInstantiation(New, Tmpl))
1013 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Douglas Gregor5545e162009-03-24 00:38:23 +00001015 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
1016 New->setAccess(Tmpl->getAccess());
Anders Carlsson77b7f1d2009-05-14 22:15:41 +00001017 if (Tmpl->isVirtualAsWritten()) {
1018 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +00001019 Record->setAggregate(false);
1020 Record->setPOD(false);
Eli Friedman1d954f62009-08-15 21:55:26 +00001021 Record->setEmpty(false);
Douglas Gregor5545e162009-03-24 00:38:23 +00001022 Record->setPolymorphic(true);
1023 }
Douglas Gregor5545e162009-03-24 00:38:23 +00001024 if (Tmpl->isPure()) {
1025 New->setPure();
1026 Record->setAbstract(true);
1027 }
1028
1029 // FIXME: attributes
1030 // FIXME: New needs a pointer to Tmpl
1031 return false;
1032}
Douglas Gregora58861f2009-05-13 20:28:22 +00001033
1034/// \brief Instantiate the definition of the given function from its
1035/// template.
1036///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001037/// \param PointOfInstantiation the point at which the instantiation was
1038/// required. Note that this is not precisely a "point of instantiation"
1039/// for the function, but it's close.
1040///
Douglas Gregora58861f2009-05-13 20:28:22 +00001041/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001042/// function template specialization or member function of a class template
1043/// specialization.
1044///
1045/// \param Recursive if true, recursively instantiates any functions that
1046/// are required by this instantiation.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001047void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001048 FunctionDecl *Function,
1049 bool Recursive) {
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001050 if (Function->isInvalidDecl())
1051 return;
1052
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001053 assert(!Function->getBody() && "Already instantiated!");
Mike Stump1eb44332009-09-09 15:08:12 +00001054
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001055 // Never instantiate an explicit specialization.
1056 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1057 return;
1058
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001059 // Find the function body that we'll be substituting.
Douglas Gregor1637be72009-06-26 00:10:03 +00001060 const FunctionDecl *PatternDecl = 0;
Douglas Gregor5ec178f2009-08-28 21:09:48 +00001061 if (FunctionTemplateDecl *Primary = Function->getPrimaryTemplate()) {
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001062 while (Primary->getInstantiatedFromMemberTemplate()) {
1063 // If we have hit a point where the user provided a specialization of
1064 // this template, we're done looking.
1065 if (Primary->isMemberSpecialization())
1066 break;
1067
Douglas Gregor5ec178f2009-08-28 21:09:48 +00001068 Primary = Primary->getInstantiatedFromMemberTemplate();
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001069 }
1070
Douglas Gregor1637be72009-06-26 00:10:03 +00001071 PatternDecl = Primary->getTemplatedDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001072 } else
Douglas Gregor1637be72009-06-26 00:10:03 +00001073 PatternDecl = Function->getInstantiatedFromMemberFunction();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001074 Stmt *Pattern = 0;
1075 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001076 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001077
1078 if (!Pattern)
1079 return;
1080
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001081 // C++0x [temp.explicit]p9:
1082 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00001083 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001084 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00001085 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001086 == TSK_ExplicitInstantiationDeclaration &&
1087 PatternDecl->isOutOfLine() && !PatternDecl->isInline())
1088 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001089
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001090 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
1091 if (Inst)
1092 return;
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001093
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001094 // If we're performing recursive template instantiation, create our own
1095 // queue of pending implicit instantiations that we will instantiate later,
1096 // while we're still within our own instantiation context.
1097 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1098 if (Recursive)
1099 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001100
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001101 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
1102
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001103 // Introduce a new scope where local variable instantiations will be
1104 // recorded.
1105 LocalInstantiationScope Scope(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001106
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001107 // Introduce the instantiated function parameters into the local
1108 // instantiation scope.
1109 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
1110 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
1111 Function->getParamDecl(I));
1112
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001113 // Enter the scope of this instantiation. We don't use
1114 // PushDeclContext because we don't have a scope.
1115 DeclContext *PreviousContext = CurContext;
1116 CurContext = Function;
1117
Mike Stump1eb44332009-09-09 15:08:12 +00001118 MultiLevelTemplateArgumentList TemplateArgs =
Anders Carlsson09025312009-08-29 05:16:22 +00001119 getTemplateInstantiationArgs(Function);
1120
1121 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001122 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00001123 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
1124 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
1125 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001126 }
1127
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001128 // Instantiate the function body.
Anders Carlsson09025312009-08-29 05:16:22 +00001129 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001130
Douglas Gregor52604ab2009-09-11 21:19:12 +00001131 if (Body.isInvalid())
1132 Function->setInvalidDecl();
1133
Mike Stump1eb44332009-09-09 15:08:12 +00001134 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001135 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001136
1137 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00001138
1139 DeclGroupRef DG(Function);
1140 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001141
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001142 if (Recursive) {
1143 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001144 // instantiation of this template.
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001145 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001146
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001147 // Restore the set of pending implicit instantiations.
1148 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1149 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001150}
1151
1152/// \brief Instantiate the definition of the given variable from its
1153/// template.
1154///
Douglas Gregor7caa6822009-07-24 20:34:43 +00001155/// \param PointOfInstantiation the point at which the instantiation was
1156/// required. Note that this is not precisely a "point of instantiation"
1157/// for the function, but it's close.
1158///
1159/// \param Var the already-instantiated declaration of a static member
1160/// variable of a class template specialization.
1161///
1162/// \param Recursive if true, recursively instantiates any functions that
1163/// are required by this instantiation.
1164void Sema::InstantiateStaticDataMemberDefinition(
1165 SourceLocation PointOfInstantiation,
1166 VarDecl *Var,
1167 bool Recursive) {
1168 if (Var->isInvalidDecl())
1169 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001170
Douglas Gregor7caa6822009-07-24 20:34:43 +00001171 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001172 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
1173 bool FoundOutOfLineDef = false;
1174 assert(Def && "This data member was not instantiated from a template?");
Mike Stump1eb44332009-09-09 15:08:12 +00001175 assert(Def->isStaticDataMember() && "Not a static data member?");
1176 for (VarDecl::redecl_iterator RD = Def->redecls_begin(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00001177 RDEnd = Def->redecls_end();
1178 RD != RDEnd; ++RD) {
1179 if (RD->getLexicalDeclContext()->isFileContext()) {
1180 Def = *RD;
1181 FoundOutOfLineDef = true;
1182 }
1183 }
Mike Stump1eb44332009-09-09 15:08:12 +00001184
Douglas Gregor7caa6822009-07-24 20:34:43 +00001185 if (!FoundOutOfLineDef) {
1186 // We did not find an out-of-line definition of this static data member,
1187 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00001188 // instantiate this definition (or provide a specialization for it) in
1189 // another translation unit.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001190 return;
1191 }
1192
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001193 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001194 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001195 return;
1196
1197 // C++0x [temp.explicit]p9:
1198 // Except for inline functions, other explicit instantiation declarations
1199 // have the effect of suppressing the implicit instantiation of the entity
1200 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001201 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001202 == TSK_ExplicitInstantiationDeclaration)
1203 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001204
Douglas Gregor7caa6822009-07-24 20:34:43 +00001205 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
1206 if (Inst)
1207 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001208
Douglas Gregor7caa6822009-07-24 20:34:43 +00001209 // If we're performing recursive template instantiation, create our own
1210 // queue of pending implicit instantiations that we will instantiate later,
1211 // while we're still within our own instantiation context.
1212 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1213 if (Recursive)
1214 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001215
Douglas Gregor7caa6822009-07-24 20:34:43 +00001216 // Enter the scope of this instantiation. We don't use
1217 // PushDeclContext because we don't have a scope.
1218 DeclContext *PreviousContext = CurContext;
1219 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001220
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001221 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00001222 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00001223 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00001224 CurContext = PreviousContext;
1225
1226 if (Var) {
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001227 Var->setPreviousDeclaration(OldVar);
1228 Var->setTemplateSpecializationKind(OldVar->getTemplateSpecializationKind());
Douglas Gregor7caa6822009-07-24 20:34:43 +00001229 DeclGroupRef DG(Var);
1230 Consumer.HandleTopLevelDecl(DG);
1231 }
Mike Stump1eb44332009-09-09 15:08:12 +00001232
Douglas Gregor7caa6822009-07-24 20:34:43 +00001233 if (Recursive) {
1234 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001235 // instantiation of this template.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001236 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001237
Douglas Gregor7caa6822009-07-24 20:34:43 +00001238 // Restore the set of pending implicit instantiations.
1239 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001240 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001241}
Douglas Gregor815215d2009-05-27 05:35:12 +00001242
Anders Carlsson09025312009-08-29 05:16:22 +00001243void
1244Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
1245 const CXXConstructorDecl *Tmpl,
1246 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001247
Anders Carlsson09025312009-08-29 05:16:22 +00001248 llvm::SmallVector<MemInitTy*, 4> NewInits;
1249
1250 // Instantiate all the initializers.
1251 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00001252 InitsEnd = Tmpl->init_end();
1253 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00001254 CXXBaseOrMemberInitializer *Init = *Inits;
1255
1256 ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001257
Anders Carlsson09025312009-08-29 05:16:22 +00001258 // Instantiate all the arguments.
1259 for (ExprIterator Args = Init->arg_begin(), ArgsEnd = Init->arg_end();
1260 Args != ArgsEnd; ++Args) {
1261 OwningExprResult NewArg = SubstExpr(*Args, TemplateArgs);
1262
1263 if (NewArg.isInvalid())
1264 New->setInvalidDecl();
1265 else
1266 NewArgs.push_back(NewArg.takeAs<Expr>());
1267 }
1268
1269 MemInitResult NewInit;
1270
1271 if (Init->isBaseInitializer()) {
Eli Friedmanc5573a82009-08-29 22:22:07 +00001272 QualType BaseType(Init->getBaseClass(), 0);
1273 BaseType = SubstType(BaseType, TemplateArgs, Init->getSourceLocation(),
1274 New->getDeclName());
Anders Carlsson09025312009-08-29 05:16:22 +00001275
1276 NewInit = BuildBaseInitializer(BaseType,
Mike Stump1eb44332009-09-09 15:08:12 +00001277 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001278 NewArgs.size(),
1279 Init->getSourceLocation(),
1280 Init->getRParenLoc(),
1281 New->getParent());
1282 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001283 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00001284
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001285 // Is this an anonymous union?
1286 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregore95b4092009-09-16 18:34:49 +00001287 Member = cast<FieldDecl>(FindInstantiatedDecl(UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001288 else
Douglas Gregore95b4092009-09-16 18:34:49 +00001289 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMember(),
1290 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00001291
1292 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001293 NewArgs.size(),
1294 Init->getSourceLocation(),
1295 Init->getRParenLoc());
1296 }
1297
1298 if (NewInit.isInvalid())
1299 New->setInvalidDecl();
1300 else {
1301 // FIXME: It would be nice if ASTOwningVector had a release function.
1302 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001303
Anders Carlsson09025312009-08-29 05:16:22 +00001304 NewInits.push_back((MemInitTy *)NewInit.get());
1305 }
1306 }
Mike Stump1eb44332009-09-09 15:08:12 +00001307
Anders Carlsson09025312009-08-29 05:16:22 +00001308 // Assign all the initializers to the new constructor.
Mike Stump1eb44332009-09-09 15:08:12 +00001309 ActOnMemInitializers(DeclPtrTy::make(New),
Anders Carlsson09025312009-08-29 05:16:22 +00001310 /*FIXME: ColonLoc */
1311 SourceLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001312 NewInits.data(), NewInits.size());
Anders Carlsson09025312009-08-29 05:16:22 +00001313}
1314
John McCall52a575a2009-08-29 08:11:13 +00001315// TODO: this could be templated if the various decl types used the
1316// same method name.
1317static bool isInstantiationOf(ClassTemplateDecl *Pattern,
1318 ClassTemplateDecl *Instance) {
1319 Pattern = Pattern->getCanonicalDecl();
1320
1321 do {
1322 Instance = Instance->getCanonicalDecl();
1323 if (Pattern == Instance) return true;
1324 Instance = Instance->getInstantiatedFromMemberTemplate();
1325 } while (Instance);
1326
1327 return false;
1328}
1329
Douglas Gregor0d696532009-09-28 06:34:35 +00001330static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
1331 FunctionTemplateDecl *Instance) {
1332 Pattern = Pattern->getCanonicalDecl();
1333
1334 do {
1335 Instance = Instance->getCanonicalDecl();
1336 if (Pattern == Instance) return true;
1337 Instance = Instance->getInstantiatedFromMemberTemplate();
1338 } while (Instance);
1339
1340 return false;
1341}
1342
John McCall52a575a2009-08-29 08:11:13 +00001343static bool isInstantiationOf(CXXRecordDecl *Pattern,
1344 CXXRecordDecl *Instance) {
1345 Pattern = Pattern->getCanonicalDecl();
1346
1347 do {
1348 Instance = Instance->getCanonicalDecl();
1349 if (Pattern == Instance) return true;
1350 Instance = Instance->getInstantiatedFromMemberClass();
1351 } while (Instance);
1352
1353 return false;
1354}
1355
1356static bool isInstantiationOf(FunctionDecl *Pattern,
1357 FunctionDecl *Instance) {
1358 Pattern = Pattern->getCanonicalDecl();
1359
1360 do {
1361 Instance = Instance->getCanonicalDecl();
1362 if (Pattern == Instance) return true;
1363 Instance = Instance->getInstantiatedFromMemberFunction();
1364 } while (Instance);
1365
1366 return false;
1367}
1368
1369static bool isInstantiationOf(EnumDecl *Pattern,
1370 EnumDecl *Instance) {
1371 Pattern = Pattern->getCanonicalDecl();
1372
1373 do {
1374 Instance = Instance->getCanonicalDecl();
1375 if (Pattern == Instance) return true;
1376 Instance = Instance->getInstantiatedFromMemberEnum();
1377 } while (Instance);
1378
1379 return false;
1380}
1381
Anders Carlsson0d8df782009-08-29 19:37:28 +00001382static bool isInstantiationOf(UnresolvedUsingDecl *Pattern,
1383 UsingDecl *Instance,
1384 ASTContext &C) {
1385 return C.getInstantiatedFromUnresolvedUsingDecl(Instance) == Pattern;
1386}
1387
John McCall52a575a2009-08-29 08:11:13 +00001388static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
1389 VarDecl *Instance) {
1390 assert(Instance->isStaticDataMember());
1391
1392 Pattern = Pattern->getCanonicalDecl();
1393
1394 do {
1395 Instance = Instance->getCanonicalDecl();
1396 if (Pattern == Instance) return true;
1397 Instance = Instance->getInstantiatedFromStaticDataMember();
1398 } while (Instance);
1399
1400 return false;
1401}
1402
Douglas Gregor815215d2009-05-27 05:35:12 +00001403static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00001404 if (D->getKind() != Other->getKind()) {
1405 if (UnresolvedUsingDecl *UUD = dyn_cast<UnresolvedUsingDecl>(D)) {
1406 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
1407 return isInstantiationOf(UUD, UD, Ctx);
1408 }
1409 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001410
Anders Carlsson0d8df782009-08-29 19:37:28 +00001411 return false;
1412 }
Mike Stump1eb44332009-09-09 15:08:12 +00001413
John McCall52a575a2009-08-29 08:11:13 +00001414 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
1415 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001416
John McCall52a575a2009-08-29 08:11:13 +00001417 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
1418 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00001419
John McCall52a575a2009-08-29 08:11:13 +00001420 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
1421 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00001422
Douglas Gregor7caa6822009-07-24 20:34:43 +00001423 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00001424 if (Var->isStaticDataMember())
1425 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
1426
1427 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
1428 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00001429
Douglas Gregor0d696532009-09-28 06:34:35 +00001430 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
1431 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
1432
Anders Carlssond8b285f2009-09-01 04:26:58 +00001433 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
1434 if (!Field->getDeclName()) {
1435 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00001436 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00001437 cast<FieldDecl>(D);
1438 }
1439 }
Mike Stump1eb44332009-09-09 15:08:12 +00001440
Douglas Gregor815215d2009-05-27 05:35:12 +00001441 return D->getDeclName() && isa<NamedDecl>(Other) &&
1442 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
1443}
1444
1445template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00001446static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00001447 NamedDecl *D,
1448 ForwardIterator first,
1449 ForwardIterator last) {
1450 for (; first != last; ++first)
1451 if (isInstantiationOf(Ctx, D, *first))
1452 return cast<NamedDecl>(*first);
1453
1454 return 0;
1455}
1456
John McCall02cace72009-08-28 07:59:38 +00001457/// \brief Finds the instantiation of the given declaration context
1458/// within the current instantiation.
1459///
1460/// \returns NULL if there was an error
Douglas Gregore95b4092009-09-16 18:34:49 +00001461DeclContext *Sema::FindInstantiatedContext(DeclContext* DC,
1462 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00001463 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00001464 Decl* ID = FindInstantiatedDecl(D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00001465 return cast_or_null<DeclContext>(ID);
1466 } else return DC;
1467}
1468
Douglas Gregored961e72009-05-27 17:54:46 +00001469/// \brief Find the instantiation of the given declaration within the
1470/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00001471///
1472/// This routine is intended to be used when \p D is a declaration
1473/// referenced from within a template, that needs to mapped into the
1474/// corresponding declaration within an instantiation. For example,
1475/// given:
1476///
1477/// \code
1478/// template<typename T>
1479/// struct X {
1480/// enum Kind {
1481/// KnownValue = sizeof(T)
1482/// };
1483///
1484/// bool getKind() const { return KnownValue; }
1485/// };
1486///
1487/// template struct X<int>;
1488/// \endcode
1489///
1490/// In the instantiation of X<int>::getKind(), we need to map the
1491/// EnumConstantDecl for KnownValue (which refers to
1492/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00001493/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
1494/// this mapping from within the instantiation of X<int>.
Douglas Gregore95b4092009-09-16 18:34:49 +00001495NamedDecl *Sema::FindInstantiatedDecl(NamedDecl *D,
1496 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor44c73842009-09-01 17:53:10 +00001497 if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D)) {
1498 // Transform all of the elements of the overloaded function set.
Mike Stump1eb44332009-09-09 15:08:12 +00001499 OverloadedFunctionDecl *Result
Douglas Gregor44c73842009-09-01 17:53:10 +00001500 = OverloadedFunctionDecl::Create(Context, CurContext, Ovl->getDeclName());
Mike Stump1eb44332009-09-09 15:08:12 +00001501
Douglas Gregor44c73842009-09-01 17:53:10 +00001502 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
1503 FEnd = Ovl->function_end();
1504 F != FEnd; ++F) {
1505 Result->addOverload(
Douglas Gregore95b4092009-09-16 18:34:49 +00001506 AnyFunctionDecl::getFromNamedDecl(FindInstantiatedDecl(*F,
1507 TemplateArgs)));
Douglas Gregor44c73842009-09-01 17:53:10 +00001508 }
Mike Stump1eb44332009-09-09 15:08:12 +00001509
Douglas Gregor44c73842009-09-01 17:53:10 +00001510 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001511 }
1512
Douglas Gregor815215d2009-05-27 05:35:12 +00001513 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor2bba76b2009-05-27 17:07:49 +00001514 if (isa<ParmVarDecl>(D) || ParentDC->isFunctionOrMethod()) {
1515 // D is a local of some kind. Look into the map of local
1516 // declarations to their instantiations.
1517 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
1518 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001519
Douglas Gregore95b4092009-09-16 18:34:49 +00001520 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
1521 if (!Record->isDependentContext())
1522 return D;
1523
1524 // If the RecordDecl is actually the injected-class-name or a "templated"
1525 // declaration for a class template or class template partial
1526 // specialization, substitute into the injected-class-name of the
1527 // class template or partial specialization to find the new DeclContext.
1528 QualType T;
1529 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
1530
1531 if (ClassTemplate) {
1532 T = ClassTemplate->getInjectedClassNameType(Context);
1533 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
1534 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1535 T = Context.getTypeDeclType(Record);
1536 ClassTemplate = PartialSpec->getSpecializedTemplate();
1537 }
1538
1539 if (!T.isNull()) {
1540 // Substitute into the injected-class-name to get the type corresponding
1541 // to the instantiation we want. This substitution should never fail,
1542 // since we know we can instantiate the injected-class-name or we wouldn't
1543 // have gotten to the injected-class-name!
1544 // FIXME: Can we use the CurrentInstantiationScope to avoid this extra
1545 // instantiation in the common case?
1546 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
1547 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
1548
1549 if (!T->isDependentType()) {
1550 assert(T->isRecordType() && "Instantiation must produce a record type");
1551 return T->getAs<RecordType>()->getDecl();
1552 }
1553
1554 // We are performing "partial" template instantiation to create the
1555 // member declarations for the members of a class template
1556 // specialization. Therefore, D is actually referring to something in
1557 // the current instantiation. Look through the current context,
1558 // which contains actual instantiations, to find the instantiation of
1559 // the "current instantiation" that D refers to.
Mike Stump1eb44332009-09-09 15:08:12 +00001560 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00001561 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001562 if (ClassTemplateSpecializationDecl *Spec
John McCall52a575a2009-08-29 08:11:13 +00001563 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00001564 if (isInstantiationOf(ClassTemplate,
1565 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00001566 return Spec;
1567 }
1568
Mike Stump1eb44332009-09-09 15:08:12 +00001569 assert(false &&
John McCall52a575a2009-08-29 08:11:13 +00001570 "Unable to find declaration for the current instantiation");
Douglas Gregore95b4092009-09-16 18:34:49 +00001571 return Record;
John McCall52a575a2009-08-29 08:11:13 +00001572 }
Douglas Gregore95b4092009-09-16 18:34:49 +00001573
1574 // Fall through to deal with other dependent record types (e.g.,
1575 // anonymous unions in class templates).
1576 }
John McCall52a575a2009-08-29 08:11:13 +00001577
Douglas Gregore95b4092009-09-16 18:34:49 +00001578 if (!ParentDC->isDependentContext())
1579 return D;
1580
1581 ParentDC = FindInstantiatedContext(ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001582 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00001583 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001584
Douglas Gregor815215d2009-05-27 05:35:12 +00001585 if (ParentDC != D->getDeclContext()) {
1586 // We performed some kind of instantiation in the parent context,
1587 // so now we need to look into the instantiated parent context to
1588 // find the instantiation of the declaration D.
1589 NamedDecl *Result = 0;
1590 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001591 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00001592 Result = findInstantiationOf(Context, D, Found.first, Found.second);
1593 } else {
1594 // Since we don't have a name for the entity we're looking for,
1595 // our only option is to walk through all of the declarations to
1596 // find that name. This will occur in a few cases:
1597 //
1598 // - anonymous struct/union within a template
1599 // - unnamed class/struct/union/enum within a template
1600 //
1601 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00001602 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001603 ParentDC->decls_begin(),
1604 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00001605 }
Mike Stump1eb44332009-09-09 15:08:12 +00001606
Douglas Gregor815215d2009-05-27 05:35:12 +00001607 assert(Result && "Unable to find instantiation of declaration!");
1608 D = Result;
1609 }
1610
Douglas Gregor815215d2009-05-27 05:35:12 +00001611 return D;
1612}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001613
Mike Stump1eb44332009-09-09 15:08:12 +00001614/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001615/// instantiations we have seen until this point.
1616void Sema::PerformPendingImplicitInstantiations() {
1617 while (!PendingImplicitInstantiations.empty()) {
1618 PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front();
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001619 PendingImplicitInstantiations.pop_front();
Mike Stump1eb44332009-09-09 15:08:12 +00001620
Douglas Gregor7caa6822009-07-24 20:34:43 +00001621 // Instantiate function definitions
1622 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001623 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001624 Function->getLocation(), *this,
1625 Context.getSourceManager(),
1626 "instantiating function definition");
Mike Stump1eb44332009-09-09 15:08:12 +00001627
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001628 if (!Function->getBody())
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001629 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001630 continue;
1631 }
Mike Stump1eb44332009-09-09 15:08:12 +00001632
Douglas Gregor7caa6822009-07-24 20:34:43 +00001633 // Instantiate static data member definitions.
1634 VarDecl *Var = cast<VarDecl>(Inst.first);
1635 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001636
Mike Stump1eb44332009-09-09 15:08:12 +00001637 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001638 Var->getLocation(), *this,
1639 Context.getSourceManager(),
1640 "instantiating static data member "
1641 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00001642
Douglas Gregor7caa6822009-07-24 20:34:43 +00001643 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001644 }
1645}