blob: b03734346c4bc9014a74d540defdfda66b2c088e [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);
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000249 if (!Field)
250 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000252 if (Invalid)
253 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000255 if (!Field->getDeclName()) {
256 // Keep track of where this decl came from.
257 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000258 }
Mike Stump1eb44332009-09-09 15:08:12 +0000259
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000260 Field->setImplicit(D->isImplicit());
261 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000262
263 return Field;
264}
265
John McCall02cace72009-08-28 07:59:38 +0000266Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
267 FriendDecl::FriendUnion FU;
268
269 // Handle friend type expressions by simply substituting template
270 // parameters into the pattern type.
271 if (Type *Ty = D->getFriendType()) {
272 QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs,
273 D->getLocation(), DeclarationName());
274 if (T.isNull()) return 0;
275
276 assert(getLangOptions().CPlusPlus0x || T->isRecordType());
277 FU = T.getTypePtr();
278
279 // Handle everything else by appropriate substitution.
280 } else {
281 NamedDecl *ND = D->getFriendDecl();
282 assert(ND && "friend decl must be a decl or a type!");
283
Douglas Gregora735b202009-10-13 14:39:41 +0000284 // FIXME: We have a problem here, because the nested call to Visit(ND)
285 // will inject the thing that the friend references into the current
286 // owner, which is wrong.
John McCall02cace72009-08-28 07:59:38 +0000287 Decl *NewND = Visit(ND);
288 if (!NewND) return 0;
289
290 FU = cast<NamedDecl>(NewND);
John McCallfd810b12009-08-14 02:03:10 +0000291 }
Mike Stump1eb44332009-09-09 15:08:12 +0000292
John McCall02cace72009-08-28 07:59:38 +0000293 FriendDecl *FD =
294 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU,
295 D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000296 FD->setAccess(AS_public);
John McCall02cace72009-08-28 07:59:38 +0000297 Owner->addDecl(FD);
298 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000299}
300
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000301Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
302 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000303
Douglas Gregorac7610d2009-06-22 20:57:11 +0000304 // The expression in a static assertion is not potentially evaluated.
305 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000306
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000307 OwningExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000308 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000309 if (InstantiatedAssertExpr.isInvalid())
310 return 0;
311
Douglas Gregor43d9d922009-08-08 01:41:12 +0000312 OwningExprResult Message(SemaRef, D->getMessage());
313 D->getMessage()->Retain();
Mike Stump1eb44332009-09-09 15:08:12 +0000314 Decl *StaticAssert
315 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
Chris Lattnerb28317a2009-03-28 19:18:32 +0000316 move(InstantiatedAssertExpr),
317 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000318 return StaticAssert;
319}
320
321Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000322 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000323 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000324 D->getTagKeywordLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000325 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000326 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000327 Enum->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000328 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000329 Enum->startDefinition();
330
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000331 llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000332
333 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000334 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
335 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000336 EC != ECEnd; ++EC) {
337 // The specified value for the enumerator.
338 OwningExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000339 if (Expr *UninstValue = EC->getInitExpr()) {
340 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000341 EnterExpressionEvaluationContext Unevaluated(SemaRef,
Douglas Gregorac7610d2009-06-22 20:57:11 +0000342 Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000343
John McCallce3ff2b2009-08-25 22:02:44 +0000344 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000345 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000346
347 // Drop the initial value and continue.
348 bool isInvalid = false;
349 if (Value.isInvalid()) {
350 Value = SemaRef.Owned((Expr *)0);
351 isInvalid = true;
352 }
353
Mike Stump1eb44332009-09-09 15:08:12 +0000354 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000355 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
356 EC->getLocation(), EC->getIdentifier(),
357 move(Value));
358
359 if (isInvalid) {
360 if (EnumConst)
361 EnumConst->setInvalidDecl();
362 Enum->setInvalidDecl();
363 }
364
365 if (EnumConst) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000366 Enum->addDecl(EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000367 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000368 LastEnumConst = EnumConst;
369 }
370 }
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000372 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000373 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000374 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
375 Sema::DeclPtrTy::make(Enum),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000376 &Enumerators[0], Enumerators.size(),
377 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000378
379 return Enum;
380}
381
Douglas Gregor6477b692009-03-25 15:04:13 +0000382Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
383 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
384 return 0;
385}
386
John McCalle29ba202009-08-20 01:44:21 +0000387Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
388 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000389 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000390 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000391 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000392
393 CXXRecordDecl *Pattern = D->getTemplatedDecl();
394 CXXRecordDecl *RecordInst
395 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner,
396 Pattern->getLocation(), Pattern->getIdentifier(),
Douglas Gregorf0510d42009-10-12 23:11:44 +0000397 Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL,
398 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000399
400 ClassTemplateDecl *Inst
401 = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
402 D->getIdentifier(), InstParams, RecordInst, 0);
403 RecordInst->setDescribedClassTemplate(Inst);
404 Inst->setAccess(D->getAccess());
405 Inst->setInstantiatedFromMemberTemplate(D);
Douglas Gregorf0510d42009-10-12 23:11:44 +0000406
407 // Trigger creation of the type for the instantiation.
408 SemaRef.Context.getTypeDeclType(RecordInst);
409
John McCalle29ba202009-08-20 01:44:21 +0000410 Owner->addDecl(Inst);
411 return Inst;
412}
413
Douglas Gregord60e1052009-08-27 16:57:43 +0000414Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000415TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
416 ClassTemplatePartialSpecializationDecl *D) {
417 assert(false &&"Partial specializations of member templates are unsupported");
418 return 0;
419}
420
421Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000422TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000423 // FIXME: Dig out the out-of-line definition of this function template?
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Douglas Gregord60e1052009-08-27 16:57:43 +0000425 TemplateParameterList *TempParams = D->getTemplateParameters();
426 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000427 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000428 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Douglas Gregora735b202009-10-13 14:39:41 +0000430 FunctionDecl *Instantiated = 0;
431 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
432 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
433 InstParams));
434 else
435 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
436 D->getTemplatedDecl(),
437 InstParams));
438
439 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000440 return 0;
441
Mike Stump1eb44332009-09-09 15:08:12 +0000442 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000443 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000444 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000445 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000446 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000447 assert(InstTemplate &&
448 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
449 if (!InstTemplate->getInstantiatedFromMemberTemplate())
450 InstTemplate->setInstantiatedFromMemberTemplate(D);
451
452 // Add non-friends into the owner.
453 if (!InstTemplate->getFriendObjectKind())
454 Owner->addDecl(InstTemplate);
Douglas Gregord60e1052009-08-27 16:57:43 +0000455 return InstTemplate;
456}
457
Douglas Gregord475b8d2009-03-25 21:17:03 +0000458Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
459 CXXRecordDecl *PrevDecl = 0;
460 if (D->isInjectedClassName())
461 PrevDecl = cast<CXXRecordDecl>(Owner);
462
463 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000464 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000465 D->getLocation(), D->getIdentifier(),
466 D->getTagKeywordLoc(), PrevDecl);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000467 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000468 // FIXME: Check against AS_none is an ugly hack to work around the issue that
469 // the tag decls introduced by friend class declarations don't have an access
470 // specifier. Remove once this area of the code gets sorted out.
471 if (D->getAccess() != AS_none)
472 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000473 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000474 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000475
John McCall02cace72009-08-28 07:59:38 +0000476 // If the original function was part of a friend declaration,
477 // inherit its namespace state.
478 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
479 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
480
Anders Carlssond8b285f2009-09-01 04:26:58 +0000481 Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
482
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000483 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000484 return Record;
485}
486
John McCall02cace72009-08-28 07:59:38 +0000487/// Normal class members are of more specific types and therefore
488/// don't make it here. This function serves two purposes:
489/// 1) instantiating function templates
490/// 2) substituting friend declarations
491/// FIXME: preserve function definitions in case #2
Douglas Gregora735b202009-10-13 14:39:41 +0000492 Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
493 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000494 // Check whether there is already a function template specialization for
495 // this declaration.
496 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
497 void *InsertPos = 0;
Douglas Gregora735b202009-10-13 14:39:41 +0000498 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000499 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000500 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000501 TemplateArgs.getInnermost().getFlatArgumentList(),
502 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor828e2262009-07-29 16:09:57 +0000503 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000504
505 FunctionTemplateSpecializationInfo *Info
506 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor127102b2009-06-29 20:59:39 +0000507 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Douglas Gregor127102b2009-06-29 20:59:39 +0000509 // If we already have a function template specialization, return it.
510 if (Info)
511 return Info->Function;
512 }
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Douglas Gregore53060f2009-06-25 22:08:12 +0000514 Sema::LocalInstantiationScope Scope(SemaRef);
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Douglas Gregore53060f2009-06-25 22:08:12 +0000516 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000517 QualType T = SubstFunctionType(D, Params);
Douglas Gregore53060f2009-06-25 22:08:12 +0000518 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000519 return 0;
John McCallfd810b12009-08-14 02:03:10 +0000520
Douglas Gregore53060f2009-06-25 22:08:12 +0000521 // Build the instantiated method declaration.
Douglas Gregore95b4092009-09-16 18:34:49 +0000522 DeclContext *DC = SemaRef.FindInstantiatedContext(D->getDeclContext(),
523 TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +0000524 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +0000525 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000526 D->getDeclName(), T, D->getDeclaratorInfo(),
527 D->getStorageClass(),
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000528 D->isInline(), D->hasWrittenPrototype());
John McCall02cace72009-08-28 07:59:38 +0000529 Function->setLexicalDeclContext(Owner);
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Douglas Gregore53060f2009-06-25 22:08:12 +0000531 // Attach the parameters
532 for (unsigned P = 0; P < Params.size(); ++P)
533 Params[P]->setOwningFunction(Function);
534 Function->setParams(SemaRef.Context, Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +0000535
Douglas Gregora735b202009-10-13 14:39:41 +0000536 if (TemplateParams) {
537 // Our resulting instantiation is actually a function template, since we
538 // are substituting only the outer template parameters. For example, given
539 //
540 // template<typename T>
541 // struct X {
542 // template<typename U> friend void f(T, U);
543 // };
544 //
545 // X<int> x;
546 //
547 // We are instantiating the friend function template "f" within X<int>,
548 // which means substituting int for T, but leaving "f" as a friend function
549 // template.
550 // Build the function template itself.
551 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Owner,
552 Function->getLocation(),
553 Function->getDeclName(),
554 TemplateParams, Function);
555 Function->setDescribedFunctionTemplate(FunctionTemplate);
556 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
John McCall02cace72009-08-28 07:59:38 +0000557 }
Douglas Gregora735b202009-10-13 14:39:41 +0000558
Douglas Gregore53060f2009-06-25 22:08:12 +0000559 if (InitFunctionInstantiation(Function, D))
560 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000561
Douglas Gregore53060f2009-06-25 22:08:12 +0000562 bool Redeclaration = false;
563 bool OverloadableAttrRequired = false;
Douglas Gregora735b202009-10-13 14:39:41 +0000564
Douglas Gregore53060f2009-06-25 22:08:12 +0000565 NamedDecl *PrevDecl = 0;
Douglas Gregora735b202009-10-13 14:39:41 +0000566 if (TemplateParams || !FunctionTemplate) {
567 // Look only into the namespace where the friend would be declared to
568 // find a previous declaration. This is the innermost enclosing namespace,
569 // as described in ActOnFriendFunctionDecl.
570 Sema::LookupResult R;
571 SemaRef.LookupQualifiedName(R, DC, Function->getDeclName(),
572 Sema::LookupOrdinaryName, true);
573
574 PrevDecl = R.getAsSingleDecl(SemaRef.Context);
575
576 // In C++, the previous declaration we find might be a tag type
577 // (class or enum). In this case, the new declaration will hide the
578 // tag type. Note that this does does not apply if we're declaring a
579 // typedef (C++ [dcl.typedef]p4).
580 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
581 PrevDecl = 0;
582 }
583
Douglas Gregore53060f2009-06-25 22:08:12 +0000584 SemaRef.CheckFunctionDeclaration(Function, PrevDecl, Redeclaration,
585 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000586
Douglas Gregora735b202009-10-13 14:39:41 +0000587 // If the original function was part of a friend declaration,
588 // inherit its namespace state and add it to the owner.
589 NamedDecl *FromFriendD
590 = TemplateParams? cast<NamedDecl>(D->getDescribedFunctionTemplate()) : D;
591 if (FromFriendD->getFriendObjectKind()) {
592 NamedDecl *ToFriendD = 0;
593 if (TemplateParams) {
594 ToFriendD = cast<NamedDecl>(FunctionTemplate);
595 PrevDecl = FunctionTemplate->getPreviousDeclaration();
596 } else {
597 ToFriendD = Function;
598 PrevDecl = Function->getPreviousDeclaration();
599 }
600 ToFriendD->setObjectOfFriendDecl(PrevDecl != NULL);
601 if (!Owner->isDependentContext() && !PrevDecl)
602 DC->makeDeclVisibleInContext(ToFriendD, /* Recoverable = */ false);
603
604 if (!TemplateParams)
605 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
606 }
607
608 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000609 // Record this function template specialization.
610 Function->setFunctionTemplateSpecialization(SemaRef.Context,
611 FunctionTemplate,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000612 &TemplateArgs.getInnermost(),
Douglas Gregor127102b2009-06-29 20:59:39 +0000613 InsertPos);
John McCallfd810b12009-08-14 02:03:10 +0000614 }
615
Douglas Gregore53060f2009-06-25 22:08:12 +0000616 return Function;
617}
618
Douglas Gregord60e1052009-08-27 16:57:43 +0000619Decl *
620TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
621 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +0000622 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
623 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +0000624 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +0000625 // We are creating a function template specialization from a function
626 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +0000627 // specialization for this particular set of template arguments.
Douglas Gregor6b906862009-08-21 00:16:32 +0000628 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000629 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000630 TemplateArgs.getInnermost().getFlatArgumentList(),
631 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000632 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000633
634 FunctionTemplateSpecializationInfo *Info
635 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor6b906862009-08-21 00:16:32 +0000636 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000637
Douglas Gregor6b906862009-08-21 00:16:32 +0000638 // If we already have a function template specialization, return it.
639 if (Info)
640 return Info->Function;
641 }
642
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000643 Sema::LocalInstantiationScope Scope(SemaRef);
644
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000645 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000646 QualType T = SubstFunctionType(D, Params);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000647 if (T.isNull())
648 return 0;
649
650 // Build the instantiated method declaration.
651 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
Douglas Gregordec06662009-08-21 18:42:58 +0000652 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Douglas Gregordec06662009-08-21 18:42:58 +0000654 DeclarationName Name = D->getDeclName();
Douglas Gregor17e32f32009-08-21 22:43:28 +0000655 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregordec06662009-08-21 18:42:58 +0000656 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
657 Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
658 SemaRef.Context.getCanonicalType(ClassTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000659 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
660 Constructor->getLocation(),
661 Name, T,
Douglas Gregor17e32f32009-08-21 22:43:28 +0000662 Constructor->getDeclaratorInfo(),
Mike Stump1eb44332009-09-09 15:08:12 +0000663 Constructor->isExplicit(),
Douglas Gregor17e32f32009-08-21 22:43:28 +0000664 Constructor->isInline(), false);
665 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
666 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
667 Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
668 SemaRef.Context.getCanonicalType(ClassTy));
669 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
670 Destructor->getLocation(), Name,
671 T, Destructor->isInline(), false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000672 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000673 CanQualType ConvTy
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000674 = SemaRef.Context.getCanonicalType(
John McCall183700f2009-09-21 23:43:11 +0000675 T->getAs<FunctionType>()->getResultType());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000676 Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
677 ConvTy);
678 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
679 Conversion->getLocation(), Name,
680 T, Conversion->getDeclaratorInfo(),
Mike Stump1eb44332009-09-09 15:08:12 +0000681 Conversion->isInline(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000682 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +0000683 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000684 Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
Douglas Gregordec06662009-08-21 18:42:58 +0000685 D->getDeclName(), T, D->getDeclaratorInfo(),
686 D->isStatic(), D->isInline());
687 }
Douglas Gregor6b906862009-08-21 00:16:32 +0000688
Douglas Gregord60e1052009-08-27 16:57:43 +0000689 if (TemplateParams) {
690 // Our resulting instantiation is actually a function template, since we
691 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +0000692 //
Douglas Gregord60e1052009-08-27 16:57:43 +0000693 // template<typename T>
694 // struct X {
695 // template<typename U> void f(T, U);
696 // };
697 //
698 // X<int> x;
699 //
700 // We are instantiating the member template "f" within X<int>, which means
701 // substituting int for T, but leaving "f" as a member function template.
702 // Build the function template itself.
703 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
704 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +0000705 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +0000706 TemplateParams, Method);
707 if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +0000708 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +0000709 Method->setDescribedFunctionTemplate(FunctionTemplate);
710 } else if (!FunctionTemplate)
Douglas Gregor2db32322009-10-07 23:56:10 +0000711 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000712
Mike Stump1eb44332009-09-09 15:08:12 +0000713 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000714 // out-of-line, the instantiation will have the same lexical
715 // context (which will be a namespace scope) as the template.
716 if (D->isOutOfLine())
717 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Douglas Gregor5545e162009-03-24 00:38:23 +0000719 // Attach the parameters
720 for (unsigned P = 0; P < Params.size(); ++P)
721 Params[P]->setOwningFunction(Method);
Jay Foadbeaaccd2009-05-21 09:52:38 +0000722 Method->setParams(SemaRef.Context, Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +0000723
724 if (InitMethodInstantiation(Method, D))
725 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000726
Douglas Gregordec06662009-08-21 18:42:58 +0000727 NamedDecl *PrevDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000728
Douglas Gregord60e1052009-08-27 16:57:43 +0000729 if (!FunctionTemplate || TemplateParams) {
John McCallf36e02d2009-10-09 21:13:30 +0000730 Sema::LookupResult R;
731 SemaRef.LookupQualifiedName(R, Owner, Name, Sema::LookupOrdinaryName, true);
732 PrevDecl = R.getAsSingleDecl(SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000733
Douglas Gregordec06662009-08-21 18:42:58 +0000734 // In C++, the previous declaration we find might be a tag type
735 // (class or enum). In this case, the new declaration will hide the
736 // tag type. Note that this does does not apply if we're declaring a
737 // typedef (C++ [dcl.typedef]p4).
738 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
739 PrevDecl = 0;
740 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000741
Douglas Gregord60e1052009-08-27 16:57:43 +0000742 if (FunctionTemplate && !TemplateParams)
Douglas Gregor6b906862009-08-21 00:16:32 +0000743 // Record this function template specialization.
744 Method->setFunctionTemplateSpecialization(SemaRef.Context,
745 FunctionTemplate,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000746 &TemplateArgs.getInnermost(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000747 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000748
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000749 bool Redeclaration = false;
750 bool OverloadableAttrRequired = false;
751 SemaRef.CheckFunctionDeclaration(Method, PrevDecl, Redeclaration,
752 /*FIXME:*/OverloadableAttrRequired);
753
Douglas Gregora735b202009-10-13 14:39:41 +0000754 if (!FunctionTemplate && (!Method->isInvalidDecl() || !PrevDecl) &&
755 !Method->getFriendObjectKind())
Douglas Gregordec06662009-08-21 18:42:58 +0000756 Owner->addDecl(Method);
Mike Stump1eb44332009-09-09 15:08:12 +0000757
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000758 return Method;
759}
760
Douglas Gregor615c5d42009-03-24 16:43:20 +0000761Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +0000762 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000763}
764
Douglas Gregor03b2b072009-03-24 00:15:49 +0000765Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +0000766 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +0000767}
768
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000769Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000770 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000771}
772
Douglas Gregor6477b692009-03-25 15:04:13 +0000773ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000774 QualType OrigT = SemaRef.SubstType(D->getOriginalType(), TemplateArgs,
Douglas Gregor7e063902009-05-11 23:53:27 +0000775 D->getLocation(), D->getDeclName());
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000776 if (OrigT.isNull())
777 return 0;
778
779 QualType T = SemaRef.adjustParameterType(OrigT);
780
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000781 // Allocate the parameter
782 ParmVarDecl *Param = 0;
783 if (T == OrigT)
784 Param = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000785 D->getIdentifier(), T, D->getDeclaratorInfo(),
786 D->getStorageClass(), 0);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000787 else
Mike Stump1eb44332009-09-09 15:08:12 +0000788 Param = OriginalParmVarDecl::Create(SemaRef.Context, Owner,
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000789 D->getLocation(), D->getIdentifier(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000790 T, D->getDeclaratorInfo(), OrigT,
791 D->getStorageClass(), 0);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000792
Anders Carlsson9351c172009-08-25 03:18:48 +0000793 // Mark the default argument as being uninstantiated.
Douglas Gregorf43d0b32009-09-25 06:56:31 +0000794 if (D->hasUninstantiatedDefaultArg())
795 Param->setUninstantiatedDefaultArg(D->getUninstantiatedDefaultArg());
Douglas Gregor0ed09302009-09-25 07:03:22 +0000796 else if (Expr *Arg = D->getDefaultArg())
797 Param->setUninstantiatedDefaultArg(Arg);
798
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000799 // Note: we don't try to instantiate function parameters until after
800 // we've instantiated the function's type. Therefore, we don't have
801 // to check for 'void' parameter types here.
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000802 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000803 return Param;
804}
805
806Decl *
807TemplateDeclInstantiator::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
808 // Since parameter types can decay either before or after
809 // instantiation, we simply treat OriginalParmVarDecls as
810 // ParmVarDecls the same way, and create one or the other depending
811 // on what happens after template instantiation.
812 return VisitParmVarDecl(D);
813}
814
John McCalle29ba202009-08-20 01:44:21 +0000815Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
816 TemplateTypeParmDecl *D) {
817 // TODO: don't always clone when decls are refcounted.
818 const Type* T = D->getTypeForDecl();
819 assert(T->isTemplateTypeParmType());
820 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000821
John McCalle29ba202009-08-20 01:44:21 +0000822 TemplateTypeParmDecl *Inst =
823 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
824 TTPT->getDepth(), TTPT->getIndex(),
825 TTPT->getName(),
826 D->wasDeclaredWithTypename(),
827 D->isParameterPack());
828
829 if (D->hasDefaultArgument()) {
830 QualType DefaultPattern = D->getDefaultArgument();
831 QualType DefaultInst
John McCallce3ff2b2009-08-25 22:02:44 +0000832 = SemaRef.SubstType(DefaultPattern, TemplateArgs,
833 D->getDefaultArgumentLoc(),
834 D->getDeclName());
Mike Stump1eb44332009-09-09 15:08:12 +0000835
John McCalle29ba202009-08-20 01:44:21 +0000836 Inst->setDefaultArgument(DefaultInst,
837 D->getDefaultArgumentLoc(),
838 D->defaultArgumentWasInherited() /* preserve? */);
839 }
840
841 return Inst;
842}
843
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000844Decl *
845TemplateDeclInstantiator::VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000846 NestedNameSpecifier *NNS =
847 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
848 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000849 TemplateArgs);
850 if (!NNS)
851 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000853 CXXScopeSpec SS;
854 SS.setRange(D->getTargetNestedNameRange());
855 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +0000856
857 NamedDecl *UD =
858 SemaRef.BuildUsingDeclaration(D->getLocation(), SS,
859 D->getTargetNameLocation(),
Anders Carlsson0d8df782009-08-29 19:37:28 +0000860 D->getTargetName(), 0, D->isTypeName());
861 if (UD)
Mike Stump1eb44332009-09-09 15:08:12 +0000862 SemaRef.Context.setInstantiatedFromUnresolvedUsingDecl(cast<UsingDecl>(UD),
Anders Carlsson0d8df782009-08-29 19:37:28 +0000863 D);
864 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000865}
866
John McCallce3ff2b2009-08-25 22:02:44 +0000867Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000868 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +0000869 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000870 return Instantiator.Visit(D);
871}
872
John McCalle29ba202009-08-20 01:44:21 +0000873/// \brief Instantiates a nested template parameter list in the current
874/// instantiation context.
875///
876/// \param L The parameter list to instantiate
877///
878/// \returns NULL if there was an error
879TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +0000880TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +0000881 // Get errors for all the parameters before bailing out.
882 bool Invalid = false;
883
884 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000885 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +0000886 ParamVector Params;
887 Params.reserve(N);
888 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
889 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000890 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +0000891 Params.push_back(D);
892 Invalid = Invalid || !D;
893 }
894
895 // Clean up if we had an error.
896 if (Invalid) {
897 for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
898 PI != PE; ++PI)
899 if (*PI)
900 (*PI)->Destroy(SemaRef.Context);
901 return NULL;
902 }
903
904 TemplateParameterList *InstL
905 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
906 L->getLAngleLoc(), &Params.front(), N,
907 L->getRAngleLoc());
908 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +0000909}
John McCalle29ba202009-08-20 01:44:21 +0000910
John McCallce3ff2b2009-08-25 22:02:44 +0000911/// \brief Does substitution on the type of the given function, including
912/// all of the function parameters.
Douglas Gregor5545e162009-03-24 00:38:23 +0000913///
John McCallce3ff2b2009-08-25 22:02:44 +0000914/// \param D The function whose type will be the basis of the substitution
Douglas Gregor5545e162009-03-24 00:38:23 +0000915///
916/// \param Params the instantiated parameter declarations
917
John McCallce3ff2b2009-08-25 22:02:44 +0000918/// \returns the instantiated function's type if successful, a NULL
Douglas Gregor5545e162009-03-24 00:38:23 +0000919/// type if there was an error.
Mike Stump1eb44332009-09-09 15:08:12 +0000920QualType
John McCallce3ff2b2009-08-25 22:02:44 +0000921TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +0000922 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
923 bool InvalidDecl = false;
924
John McCallce3ff2b2009-08-25 22:02:44 +0000925 // Substitute all of the function's formal parameter types.
Douglas Gregor7e063902009-05-11 23:53:27 +0000926 TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000927 llvm::SmallVector<QualType, 4> ParamTys;
Mike Stump1eb44332009-09-09 15:08:12 +0000928 for (FunctionDecl::param_iterator P = D->param_begin(),
Douglas Gregor5545e162009-03-24 00:38:23 +0000929 PEnd = D->param_end();
930 P != PEnd; ++P) {
Douglas Gregor6477b692009-03-25 15:04:13 +0000931 if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
Douglas Gregor5545e162009-03-24 00:38:23 +0000932 if (PInst->getType()->isVoidType()) {
933 SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
934 PInst->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000935 } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000936 PInst->getType(),
937 diag::err_abstract_type_in_decl,
938 Sema::AbstractParamType))
Douglas Gregor5545e162009-03-24 00:38:23 +0000939 PInst->setInvalidDecl();
940
941 Params.push_back(PInst);
942 ParamTys.push_back(PInst->getType());
943
944 if (PInst->isInvalidDecl())
945 InvalidDecl = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000946 } else
Douglas Gregor5545e162009-03-24 00:38:23 +0000947 InvalidDecl = true;
948 }
949
950 // FIXME: Deallocate dead declarations.
951 if (InvalidDecl)
952 return QualType();
953
John McCall183700f2009-09-21 23:43:11 +0000954 const FunctionProtoType *Proto = D->getType()->getAs<FunctionProtoType>();
Douglas Gregor5545e162009-03-24 00:38:23 +0000955 assert(Proto && "Missing prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +0000956 QualType ResultType
John McCallce3ff2b2009-08-25 22:02:44 +0000957 = SemaRef.SubstType(Proto->getResultType(), TemplateArgs,
958 D->getLocation(), D->getDeclName());
Douglas Gregor5545e162009-03-24 00:38:23 +0000959 if (ResultType.isNull())
960 return QualType();
961
Jay Foadbeaaccd2009-05-21 09:52:38 +0000962 return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
Douglas Gregor5545e162009-03-24 00:38:23 +0000963 Proto->isVariadic(), Proto->getTypeQuals(),
964 D->getLocation(), D->getDeclName());
965}
966
Mike Stump1eb44332009-09-09 15:08:12 +0000967/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +0000968/// declaration (New) from the corresponding fields of its template (Tmpl).
969///
970/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +0000971bool
972TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +0000973 FunctionDecl *Tmpl) {
974 if (Tmpl->isDeleted())
975 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Douglas Gregorcca9e962009-07-01 22:01:06 +0000977 // If we are performing substituting explicitly-specified template arguments
978 // or deduced template arguments into a function template and we reach this
979 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +0000980 // to keeping the new function template specialization. We therefore
981 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +0000982 // into a template instantiation for this specific function template
983 // specialization, which is not a SFINAE context, so that we diagnose any
984 // further errors in the declaration itself.
985 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
986 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
987 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
988 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +0000989 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +0000990 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000991 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +0000992 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +0000993 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +0000994 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
995 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
996 }
997 }
Mike Stump1eb44332009-09-09 15:08:12 +0000998
Douglas Gregore53060f2009-06-25 22:08:12 +0000999 return false;
1000}
1001
Douglas Gregor5545e162009-03-24 00:38:23 +00001002/// \brief Initializes common fields of an instantiated method
1003/// declaration (New) from the corresponding fields of its template
1004/// (Tmpl).
1005///
1006/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001007bool
1008TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00001009 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001010 if (InitFunctionInstantiation(New, Tmpl))
1011 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001012
Douglas Gregor5545e162009-03-24 00:38:23 +00001013 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
1014 New->setAccess(Tmpl->getAccess());
Anders Carlsson77b7f1d2009-05-14 22:15:41 +00001015 if (Tmpl->isVirtualAsWritten()) {
1016 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +00001017 Record->setAggregate(false);
1018 Record->setPOD(false);
Eli Friedman1d954f62009-08-15 21:55:26 +00001019 Record->setEmpty(false);
Douglas Gregor5545e162009-03-24 00:38:23 +00001020 Record->setPolymorphic(true);
1021 }
Douglas Gregor5545e162009-03-24 00:38:23 +00001022 if (Tmpl->isPure()) {
1023 New->setPure();
1024 Record->setAbstract(true);
1025 }
1026
1027 // FIXME: attributes
1028 // FIXME: New needs a pointer to Tmpl
1029 return false;
1030}
Douglas Gregora58861f2009-05-13 20:28:22 +00001031
1032/// \brief Instantiate the definition of the given function from its
1033/// template.
1034///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001035/// \param PointOfInstantiation the point at which the instantiation was
1036/// required. Note that this is not precisely a "point of instantiation"
1037/// for the function, but it's close.
1038///
Douglas Gregora58861f2009-05-13 20:28:22 +00001039/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001040/// function template specialization or member function of a class template
1041/// specialization.
1042///
1043/// \param Recursive if true, recursively instantiates any functions that
1044/// are required by this instantiation.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001045void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001046 FunctionDecl *Function,
1047 bool Recursive) {
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001048 if (Function->isInvalidDecl())
1049 return;
1050
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001051 assert(!Function->getBody() && "Already instantiated!");
Mike Stump1eb44332009-09-09 15:08:12 +00001052
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001053 // Never instantiate an explicit specialization.
1054 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1055 return;
1056
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001057 // Find the function body that we'll be substituting.
Douglas Gregor1637be72009-06-26 00:10:03 +00001058 const FunctionDecl *PatternDecl = 0;
Douglas Gregor5ec178f2009-08-28 21:09:48 +00001059 if (FunctionTemplateDecl *Primary = Function->getPrimaryTemplate()) {
1060 while (Primary->getInstantiatedFromMemberTemplate())
1061 Primary = Primary->getInstantiatedFromMemberTemplate();
Mike Stump1eb44332009-09-09 15:08:12 +00001062
Douglas Gregor1637be72009-06-26 00:10:03 +00001063 PatternDecl = Primary->getTemplatedDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001064 } else
Douglas Gregor1637be72009-06-26 00:10:03 +00001065 PatternDecl = Function->getInstantiatedFromMemberFunction();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001066 Stmt *Pattern = 0;
1067 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001068 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001069
1070 if (!Pattern)
1071 return;
1072
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001073 // C++0x [temp.explicit]p9:
1074 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00001075 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001076 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00001077 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001078 == TSK_ExplicitInstantiationDeclaration &&
1079 PatternDecl->isOutOfLine() && !PatternDecl->isInline())
1080 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001081
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001082 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
1083 if (Inst)
1084 return;
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001085
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001086 // If we're performing recursive template instantiation, create our own
1087 // queue of pending implicit instantiations that we will instantiate later,
1088 // while we're still within our own instantiation context.
1089 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1090 if (Recursive)
1091 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001092
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001093 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
1094
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001095 // Introduce a new scope where local variable instantiations will be
1096 // recorded.
1097 LocalInstantiationScope Scope(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001098
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001099 // Introduce the instantiated function parameters into the local
1100 // instantiation scope.
1101 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
1102 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
1103 Function->getParamDecl(I));
1104
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001105 // Enter the scope of this instantiation. We don't use
1106 // PushDeclContext because we don't have a scope.
1107 DeclContext *PreviousContext = CurContext;
1108 CurContext = Function;
1109
Mike Stump1eb44332009-09-09 15:08:12 +00001110 MultiLevelTemplateArgumentList TemplateArgs =
Anders Carlsson09025312009-08-29 05:16:22 +00001111 getTemplateInstantiationArgs(Function);
1112
1113 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001114 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00001115 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
1116 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
1117 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001118 }
1119
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001120 // Instantiate the function body.
Anders Carlsson09025312009-08-29 05:16:22 +00001121 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001122
Douglas Gregor52604ab2009-09-11 21:19:12 +00001123 if (Body.isInvalid())
1124 Function->setInvalidDecl();
1125
Mike Stump1eb44332009-09-09 15:08:12 +00001126 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001127 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001128
1129 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00001130
1131 DeclGroupRef DG(Function);
1132 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001133
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001134 if (Recursive) {
1135 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001136 // instantiation of this template.
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001137 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001138
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001139 // Restore the set of pending implicit instantiations.
1140 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1141 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001142}
1143
1144/// \brief Instantiate the definition of the given variable from its
1145/// template.
1146///
Douglas Gregor7caa6822009-07-24 20:34:43 +00001147/// \param PointOfInstantiation the point at which the instantiation was
1148/// required. Note that this is not precisely a "point of instantiation"
1149/// for the function, but it's close.
1150///
1151/// \param Var the already-instantiated declaration of a static member
1152/// variable of a class template specialization.
1153///
1154/// \param Recursive if true, recursively instantiates any functions that
1155/// are required by this instantiation.
1156void Sema::InstantiateStaticDataMemberDefinition(
1157 SourceLocation PointOfInstantiation,
1158 VarDecl *Var,
1159 bool Recursive) {
1160 if (Var->isInvalidDecl())
1161 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001162
Douglas Gregor7caa6822009-07-24 20:34:43 +00001163 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001164 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
1165 bool FoundOutOfLineDef = false;
1166 assert(Def && "This data member was not instantiated from a template?");
Mike Stump1eb44332009-09-09 15:08:12 +00001167 assert(Def->isStaticDataMember() && "Not a static data member?");
1168 for (VarDecl::redecl_iterator RD = Def->redecls_begin(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00001169 RDEnd = Def->redecls_end();
1170 RD != RDEnd; ++RD) {
1171 if (RD->getLexicalDeclContext()->isFileContext()) {
1172 Def = *RD;
1173 FoundOutOfLineDef = true;
1174 }
1175 }
Mike Stump1eb44332009-09-09 15:08:12 +00001176
Douglas Gregor7caa6822009-07-24 20:34:43 +00001177 if (!FoundOutOfLineDef) {
1178 // We did not find an out-of-line definition of this static data member,
1179 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00001180 // instantiate this definition (or provide a specialization for it) in
1181 // another translation unit.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001182 return;
1183 }
1184
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001185 // Never instantiate an explicit specialization.
1186 if (Def->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1187 return;
1188
1189 // C++0x [temp.explicit]p9:
1190 // Except for inline functions, other explicit instantiation declarations
1191 // have the effect of suppressing the implicit instantiation of the entity
1192 // to which they refer.
1193 if (Def->getTemplateSpecializationKind()
1194 == TSK_ExplicitInstantiationDeclaration)
1195 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001196
Douglas Gregor7caa6822009-07-24 20:34:43 +00001197 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
1198 if (Inst)
1199 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001200
Douglas Gregor7caa6822009-07-24 20:34:43 +00001201 // If we're performing recursive template instantiation, create our own
1202 // queue of pending implicit instantiations that we will instantiate later,
1203 // while we're still within our own instantiation context.
1204 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1205 if (Recursive)
1206 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001207
Douglas Gregor7caa6822009-07-24 20:34:43 +00001208 // Enter the scope of this instantiation. We don't use
1209 // PushDeclContext because we don't have a scope.
1210 DeclContext *PreviousContext = CurContext;
1211 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001212
John McCallce3ff2b2009-08-25 22:02:44 +00001213 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00001214 getTemplateInstantiationArgs(Var)));
Mike Stump1eb44332009-09-09 15:08:12 +00001215
Douglas Gregor7caa6822009-07-24 20:34:43 +00001216 CurContext = PreviousContext;
1217
1218 if (Var) {
1219 DeclGroupRef DG(Var);
1220 Consumer.HandleTopLevelDecl(DG);
1221 }
Mike Stump1eb44332009-09-09 15:08:12 +00001222
Douglas Gregor7caa6822009-07-24 20:34:43 +00001223 if (Recursive) {
1224 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001225 // instantiation of this template.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001226 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001227
Douglas Gregor7caa6822009-07-24 20:34:43 +00001228 // Restore the set of pending implicit instantiations.
1229 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001230 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001231}
Douglas Gregor815215d2009-05-27 05:35:12 +00001232
Anders Carlsson09025312009-08-29 05:16:22 +00001233void
1234Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
1235 const CXXConstructorDecl *Tmpl,
1236 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001237
Anders Carlsson09025312009-08-29 05:16:22 +00001238 llvm::SmallVector<MemInitTy*, 4> NewInits;
1239
1240 // Instantiate all the initializers.
1241 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00001242 InitsEnd = Tmpl->init_end();
1243 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00001244 CXXBaseOrMemberInitializer *Init = *Inits;
1245
1246 ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001247
Anders Carlsson09025312009-08-29 05:16:22 +00001248 // Instantiate all the arguments.
1249 for (ExprIterator Args = Init->arg_begin(), ArgsEnd = Init->arg_end();
1250 Args != ArgsEnd; ++Args) {
1251 OwningExprResult NewArg = SubstExpr(*Args, TemplateArgs);
1252
1253 if (NewArg.isInvalid())
1254 New->setInvalidDecl();
1255 else
1256 NewArgs.push_back(NewArg.takeAs<Expr>());
1257 }
1258
1259 MemInitResult NewInit;
1260
1261 if (Init->isBaseInitializer()) {
Eli Friedmanc5573a82009-08-29 22:22:07 +00001262 QualType BaseType(Init->getBaseClass(), 0);
1263 BaseType = SubstType(BaseType, TemplateArgs, Init->getSourceLocation(),
1264 New->getDeclName());
Anders Carlsson09025312009-08-29 05:16:22 +00001265
1266 NewInit = BuildBaseInitializer(BaseType,
Mike Stump1eb44332009-09-09 15:08:12 +00001267 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001268 NewArgs.size(),
1269 Init->getSourceLocation(),
1270 Init->getRParenLoc(),
1271 New->getParent());
1272 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001273 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00001274
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001275 // Is this an anonymous union?
1276 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregore95b4092009-09-16 18:34:49 +00001277 Member = cast<FieldDecl>(FindInstantiatedDecl(UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001278 else
Douglas Gregore95b4092009-09-16 18:34:49 +00001279 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMember(),
1280 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00001281
1282 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001283 NewArgs.size(),
1284 Init->getSourceLocation(),
1285 Init->getRParenLoc());
1286 }
1287
1288 if (NewInit.isInvalid())
1289 New->setInvalidDecl();
1290 else {
1291 // FIXME: It would be nice if ASTOwningVector had a release function.
1292 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001293
Anders Carlsson09025312009-08-29 05:16:22 +00001294 NewInits.push_back((MemInitTy *)NewInit.get());
1295 }
1296 }
Mike Stump1eb44332009-09-09 15:08:12 +00001297
Anders Carlsson09025312009-08-29 05:16:22 +00001298 // Assign all the initializers to the new constructor.
Mike Stump1eb44332009-09-09 15:08:12 +00001299 ActOnMemInitializers(DeclPtrTy::make(New),
Anders Carlsson09025312009-08-29 05:16:22 +00001300 /*FIXME: ColonLoc */
1301 SourceLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001302 NewInits.data(), NewInits.size());
Anders Carlsson09025312009-08-29 05:16:22 +00001303}
1304
John McCall52a575a2009-08-29 08:11:13 +00001305// TODO: this could be templated if the various decl types used the
1306// same method name.
1307static bool isInstantiationOf(ClassTemplateDecl *Pattern,
1308 ClassTemplateDecl *Instance) {
1309 Pattern = Pattern->getCanonicalDecl();
1310
1311 do {
1312 Instance = Instance->getCanonicalDecl();
1313 if (Pattern == Instance) return true;
1314 Instance = Instance->getInstantiatedFromMemberTemplate();
1315 } while (Instance);
1316
1317 return false;
1318}
1319
Douglas Gregor0d696532009-09-28 06:34:35 +00001320static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
1321 FunctionTemplateDecl *Instance) {
1322 Pattern = Pattern->getCanonicalDecl();
1323
1324 do {
1325 Instance = Instance->getCanonicalDecl();
1326 if (Pattern == Instance) return true;
1327 Instance = Instance->getInstantiatedFromMemberTemplate();
1328 } while (Instance);
1329
1330 return false;
1331}
1332
John McCall52a575a2009-08-29 08:11:13 +00001333static bool isInstantiationOf(CXXRecordDecl *Pattern,
1334 CXXRecordDecl *Instance) {
1335 Pattern = Pattern->getCanonicalDecl();
1336
1337 do {
1338 Instance = Instance->getCanonicalDecl();
1339 if (Pattern == Instance) return true;
1340 Instance = Instance->getInstantiatedFromMemberClass();
1341 } while (Instance);
1342
1343 return false;
1344}
1345
1346static bool isInstantiationOf(FunctionDecl *Pattern,
1347 FunctionDecl *Instance) {
1348 Pattern = Pattern->getCanonicalDecl();
1349
1350 do {
1351 Instance = Instance->getCanonicalDecl();
1352 if (Pattern == Instance) return true;
1353 Instance = Instance->getInstantiatedFromMemberFunction();
1354 } while (Instance);
1355
1356 return false;
1357}
1358
1359static bool isInstantiationOf(EnumDecl *Pattern,
1360 EnumDecl *Instance) {
1361 Pattern = Pattern->getCanonicalDecl();
1362
1363 do {
1364 Instance = Instance->getCanonicalDecl();
1365 if (Pattern == Instance) return true;
1366 Instance = Instance->getInstantiatedFromMemberEnum();
1367 } while (Instance);
1368
1369 return false;
1370}
1371
Anders Carlsson0d8df782009-08-29 19:37:28 +00001372static bool isInstantiationOf(UnresolvedUsingDecl *Pattern,
1373 UsingDecl *Instance,
1374 ASTContext &C) {
1375 return C.getInstantiatedFromUnresolvedUsingDecl(Instance) == Pattern;
1376}
1377
John McCall52a575a2009-08-29 08:11:13 +00001378static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
1379 VarDecl *Instance) {
1380 assert(Instance->isStaticDataMember());
1381
1382 Pattern = Pattern->getCanonicalDecl();
1383
1384 do {
1385 Instance = Instance->getCanonicalDecl();
1386 if (Pattern == Instance) return true;
1387 Instance = Instance->getInstantiatedFromStaticDataMember();
1388 } while (Instance);
1389
1390 return false;
1391}
1392
Douglas Gregor815215d2009-05-27 05:35:12 +00001393static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00001394 if (D->getKind() != Other->getKind()) {
1395 if (UnresolvedUsingDecl *UUD = dyn_cast<UnresolvedUsingDecl>(D)) {
1396 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
1397 return isInstantiationOf(UUD, UD, Ctx);
1398 }
1399 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001400
Anders Carlsson0d8df782009-08-29 19:37:28 +00001401 return false;
1402 }
Mike Stump1eb44332009-09-09 15:08:12 +00001403
John McCall52a575a2009-08-29 08:11:13 +00001404 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
1405 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001406
John McCall52a575a2009-08-29 08:11:13 +00001407 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
1408 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00001409
John McCall52a575a2009-08-29 08:11:13 +00001410 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
1411 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00001412
Douglas Gregor7caa6822009-07-24 20:34:43 +00001413 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00001414 if (Var->isStaticDataMember())
1415 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
1416
1417 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
1418 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00001419
Douglas Gregor0d696532009-09-28 06:34:35 +00001420 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
1421 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
1422
Anders Carlssond8b285f2009-09-01 04:26:58 +00001423 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
1424 if (!Field->getDeclName()) {
1425 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00001426 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00001427 cast<FieldDecl>(D);
1428 }
1429 }
Mike Stump1eb44332009-09-09 15:08:12 +00001430
Douglas Gregor815215d2009-05-27 05:35:12 +00001431 return D->getDeclName() && isa<NamedDecl>(Other) &&
1432 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
1433}
1434
1435template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00001436static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00001437 NamedDecl *D,
1438 ForwardIterator first,
1439 ForwardIterator last) {
1440 for (; first != last; ++first)
1441 if (isInstantiationOf(Ctx, D, *first))
1442 return cast<NamedDecl>(*first);
1443
1444 return 0;
1445}
1446
John McCall02cace72009-08-28 07:59:38 +00001447/// \brief Finds the instantiation of the given declaration context
1448/// within the current instantiation.
1449///
1450/// \returns NULL if there was an error
Douglas Gregore95b4092009-09-16 18:34:49 +00001451DeclContext *Sema::FindInstantiatedContext(DeclContext* DC,
1452 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00001453 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00001454 Decl* ID = FindInstantiatedDecl(D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00001455 return cast_or_null<DeclContext>(ID);
1456 } else return DC;
1457}
1458
Douglas Gregored961e72009-05-27 17:54:46 +00001459/// \brief Find the instantiation of the given declaration within the
1460/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00001461///
1462/// This routine is intended to be used when \p D is a declaration
1463/// referenced from within a template, that needs to mapped into the
1464/// corresponding declaration within an instantiation. For example,
1465/// given:
1466///
1467/// \code
1468/// template<typename T>
1469/// struct X {
1470/// enum Kind {
1471/// KnownValue = sizeof(T)
1472/// };
1473///
1474/// bool getKind() const { return KnownValue; }
1475/// };
1476///
1477/// template struct X<int>;
1478/// \endcode
1479///
1480/// In the instantiation of X<int>::getKind(), we need to map the
1481/// EnumConstantDecl for KnownValue (which refers to
1482/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00001483/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
1484/// this mapping from within the instantiation of X<int>.
Douglas Gregore95b4092009-09-16 18:34:49 +00001485NamedDecl *Sema::FindInstantiatedDecl(NamedDecl *D,
1486 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor44c73842009-09-01 17:53:10 +00001487 if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D)) {
1488 // Transform all of the elements of the overloaded function set.
Mike Stump1eb44332009-09-09 15:08:12 +00001489 OverloadedFunctionDecl *Result
Douglas Gregor44c73842009-09-01 17:53:10 +00001490 = OverloadedFunctionDecl::Create(Context, CurContext, Ovl->getDeclName());
Mike Stump1eb44332009-09-09 15:08:12 +00001491
Douglas Gregor44c73842009-09-01 17:53:10 +00001492 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
1493 FEnd = Ovl->function_end();
1494 F != FEnd; ++F) {
1495 Result->addOverload(
Douglas Gregore95b4092009-09-16 18:34:49 +00001496 AnyFunctionDecl::getFromNamedDecl(FindInstantiatedDecl(*F,
1497 TemplateArgs)));
Douglas Gregor44c73842009-09-01 17:53:10 +00001498 }
Mike Stump1eb44332009-09-09 15:08:12 +00001499
Douglas Gregor44c73842009-09-01 17:53:10 +00001500 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001501 }
1502
Douglas Gregor815215d2009-05-27 05:35:12 +00001503 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor2bba76b2009-05-27 17:07:49 +00001504 if (isa<ParmVarDecl>(D) || ParentDC->isFunctionOrMethod()) {
1505 // D is a local of some kind. Look into the map of local
1506 // declarations to their instantiations.
1507 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
1508 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001509
Douglas Gregore95b4092009-09-16 18:34:49 +00001510 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
1511 if (!Record->isDependentContext())
1512 return D;
1513
1514 // If the RecordDecl is actually the injected-class-name or a "templated"
1515 // declaration for a class template or class template partial
1516 // specialization, substitute into the injected-class-name of the
1517 // class template or partial specialization to find the new DeclContext.
1518 QualType T;
1519 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
1520
1521 if (ClassTemplate) {
1522 T = ClassTemplate->getInjectedClassNameType(Context);
1523 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
1524 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1525 T = Context.getTypeDeclType(Record);
1526 ClassTemplate = PartialSpec->getSpecializedTemplate();
1527 }
1528
1529 if (!T.isNull()) {
1530 // Substitute into the injected-class-name to get the type corresponding
1531 // to the instantiation we want. This substitution should never fail,
1532 // since we know we can instantiate the injected-class-name or we wouldn't
1533 // have gotten to the injected-class-name!
1534 // FIXME: Can we use the CurrentInstantiationScope to avoid this extra
1535 // instantiation in the common case?
1536 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
1537 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
1538
1539 if (!T->isDependentType()) {
1540 assert(T->isRecordType() && "Instantiation must produce a record type");
1541 return T->getAs<RecordType>()->getDecl();
1542 }
1543
1544 // We are performing "partial" template instantiation to create the
1545 // member declarations for the members of a class template
1546 // specialization. Therefore, D is actually referring to something in
1547 // the current instantiation. Look through the current context,
1548 // which contains actual instantiations, to find the instantiation of
1549 // the "current instantiation" that D refers to.
Mike Stump1eb44332009-09-09 15:08:12 +00001550 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00001551 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001552 if (ClassTemplateSpecializationDecl *Spec
John McCall52a575a2009-08-29 08:11:13 +00001553 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00001554 if (isInstantiationOf(ClassTemplate,
1555 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00001556 return Spec;
1557 }
1558
Mike Stump1eb44332009-09-09 15:08:12 +00001559 assert(false &&
John McCall52a575a2009-08-29 08:11:13 +00001560 "Unable to find declaration for the current instantiation");
Douglas Gregore95b4092009-09-16 18:34:49 +00001561 return Record;
John McCall52a575a2009-08-29 08:11:13 +00001562 }
Douglas Gregore95b4092009-09-16 18:34:49 +00001563
1564 // Fall through to deal with other dependent record types (e.g.,
1565 // anonymous unions in class templates).
1566 }
John McCall52a575a2009-08-29 08:11:13 +00001567
Douglas Gregore95b4092009-09-16 18:34:49 +00001568 if (!ParentDC->isDependentContext())
1569 return D;
1570
1571 ParentDC = FindInstantiatedContext(ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001572 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00001573 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001574
Douglas Gregor815215d2009-05-27 05:35:12 +00001575 if (ParentDC != D->getDeclContext()) {
1576 // We performed some kind of instantiation in the parent context,
1577 // so now we need to look into the instantiated parent context to
1578 // find the instantiation of the declaration D.
1579 NamedDecl *Result = 0;
1580 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001581 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00001582 Result = findInstantiationOf(Context, D, Found.first, Found.second);
1583 } else {
1584 // Since we don't have a name for the entity we're looking for,
1585 // our only option is to walk through all of the declarations to
1586 // find that name. This will occur in a few cases:
1587 //
1588 // - anonymous struct/union within a template
1589 // - unnamed class/struct/union/enum within a template
1590 //
1591 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00001592 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001593 ParentDC->decls_begin(),
1594 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00001595 }
Mike Stump1eb44332009-09-09 15:08:12 +00001596
Douglas Gregor815215d2009-05-27 05:35:12 +00001597 assert(Result && "Unable to find instantiation of declaration!");
1598 D = Result;
1599 }
1600
Douglas Gregor815215d2009-05-27 05:35:12 +00001601 return D;
1602}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001603
Mike Stump1eb44332009-09-09 15:08:12 +00001604/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001605/// instantiations we have seen until this point.
1606void Sema::PerformPendingImplicitInstantiations() {
1607 while (!PendingImplicitInstantiations.empty()) {
1608 PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front();
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001609 PendingImplicitInstantiations.pop_front();
Mike Stump1eb44332009-09-09 15:08:12 +00001610
Douglas Gregor7caa6822009-07-24 20:34:43 +00001611 // Instantiate function definitions
1612 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001613 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001614 Function->getLocation(), *this,
1615 Context.getSourceManager(),
1616 "instantiating function definition");
Mike Stump1eb44332009-09-09 15:08:12 +00001617
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001618 if (!Function->getBody())
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001619 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001620 continue;
1621 }
Mike Stump1eb44332009-09-09 15:08:12 +00001622
Douglas Gregor7caa6822009-07-24 20:34:43 +00001623 // Instantiate static data member definitions.
1624 VarDecl *Var = cast<VarDecl>(Inst.first);
1625 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001626
Mike Stump1eb44332009-09-09 15:08:12 +00001627 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001628 Var->getLocation(), *this,
1629 Context.getSourceManager(),
1630 "instantiating static data member "
1631 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00001632
Douglas Gregor7caa6822009-07-24 20:34:43 +00001633 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001634 }
1635}