blob: 33fa28866e52e595a08fb424397e83a15a1982a4 [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 Gregorfd056bc2009-10-13 16:30:37 +0000584 SemaRef.CheckFunctionDeclaration(Function, PrevDecl, false, Redeclaration,
Douglas Gregore53060f2009-06-25 22:08:12 +0000585 /*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;
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000751 SemaRef.CheckFunctionDeclaration(Method, PrevDecl, false, Redeclaration,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000752 /*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()) {
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001060 while (Primary->getInstantiatedFromMemberTemplate()) {
1061 // If we have hit a point where the user provided a specialization of
1062 // this template, we're done looking.
1063 if (Primary->isMemberSpecialization())
1064 break;
1065
Douglas Gregor5ec178f2009-08-28 21:09:48 +00001066 Primary = Primary->getInstantiatedFromMemberTemplate();
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001067 }
1068
Douglas Gregor1637be72009-06-26 00:10:03 +00001069 PatternDecl = Primary->getTemplatedDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001070 } else
Douglas Gregor1637be72009-06-26 00:10:03 +00001071 PatternDecl = Function->getInstantiatedFromMemberFunction();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001072 Stmt *Pattern = 0;
1073 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001074 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001075
1076 if (!Pattern)
1077 return;
1078
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001079 // C++0x [temp.explicit]p9:
1080 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00001081 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001082 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00001083 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001084 == TSK_ExplicitInstantiationDeclaration &&
1085 PatternDecl->isOutOfLine() && !PatternDecl->isInline())
1086 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001087
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001088 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
1089 if (Inst)
1090 return;
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001091
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001092 // If we're performing recursive template instantiation, create our own
1093 // queue of pending implicit instantiations that we will instantiate later,
1094 // while we're still within our own instantiation context.
1095 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1096 if (Recursive)
1097 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001098
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001099 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
1100
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001101 // Introduce a new scope where local variable instantiations will be
1102 // recorded.
1103 LocalInstantiationScope Scope(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001104
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001105 // Introduce the instantiated function parameters into the local
1106 // instantiation scope.
1107 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
1108 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
1109 Function->getParamDecl(I));
1110
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001111 // Enter the scope of this instantiation. We don't use
1112 // PushDeclContext because we don't have a scope.
1113 DeclContext *PreviousContext = CurContext;
1114 CurContext = Function;
1115
Mike Stump1eb44332009-09-09 15:08:12 +00001116 MultiLevelTemplateArgumentList TemplateArgs =
Anders Carlsson09025312009-08-29 05:16:22 +00001117 getTemplateInstantiationArgs(Function);
1118
1119 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001120 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00001121 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
1122 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
1123 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001124 }
1125
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001126 // Instantiate the function body.
Anders Carlsson09025312009-08-29 05:16:22 +00001127 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001128
Douglas Gregor52604ab2009-09-11 21:19:12 +00001129 if (Body.isInvalid())
1130 Function->setInvalidDecl();
1131
Mike Stump1eb44332009-09-09 15:08:12 +00001132 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001133 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001134
1135 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00001136
1137 DeclGroupRef DG(Function);
1138 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001139
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001140 if (Recursive) {
1141 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001142 // instantiation of this template.
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001143 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001144
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001145 // Restore the set of pending implicit instantiations.
1146 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1147 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001148}
1149
1150/// \brief Instantiate the definition of the given variable from its
1151/// template.
1152///
Douglas Gregor7caa6822009-07-24 20:34:43 +00001153/// \param PointOfInstantiation the point at which the instantiation was
1154/// required. Note that this is not precisely a "point of instantiation"
1155/// for the function, but it's close.
1156///
1157/// \param Var the already-instantiated declaration of a static member
1158/// variable of a class template specialization.
1159///
1160/// \param Recursive if true, recursively instantiates any functions that
1161/// are required by this instantiation.
1162void Sema::InstantiateStaticDataMemberDefinition(
1163 SourceLocation PointOfInstantiation,
1164 VarDecl *Var,
1165 bool Recursive) {
1166 if (Var->isInvalidDecl())
1167 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001168
Douglas Gregor7caa6822009-07-24 20:34:43 +00001169 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001170 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
1171 bool FoundOutOfLineDef = false;
1172 assert(Def && "This data member was not instantiated from a template?");
Mike Stump1eb44332009-09-09 15:08:12 +00001173 assert(Def->isStaticDataMember() && "Not a static data member?");
1174 for (VarDecl::redecl_iterator RD = Def->redecls_begin(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00001175 RDEnd = Def->redecls_end();
1176 RD != RDEnd; ++RD) {
1177 if (RD->getLexicalDeclContext()->isFileContext()) {
1178 Def = *RD;
1179 FoundOutOfLineDef = true;
1180 }
1181 }
Mike Stump1eb44332009-09-09 15:08:12 +00001182
Douglas Gregor7caa6822009-07-24 20:34:43 +00001183 if (!FoundOutOfLineDef) {
1184 // We did not find an out-of-line definition of this static data member,
1185 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00001186 // instantiate this definition (or provide a specialization for it) in
1187 // another translation unit.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001188 return;
1189 }
1190
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001191 // Never instantiate an explicit specialization.
1192 if (Def->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1193 return;
1194
1195 // C++0x [temp.explicit]p9:
1196 // Except for inline functions, other explicit instantiation declarations
1197 // have the effect of suppressing the implicit instantiation of the entity
1198 // to which they refer.
1199 if (Def->getTemplateSpecializationKind()
1200 == TSK_ExplicitInstantiationDeclaration)
1201 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001202
Douglas Gregor7caa6822009-07-24 20:34:43 +00001203 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
1204 if (Inst)
1205 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001206
Douglas Gregor7caa6822009-07-24 20:34:43 +00001207 // If we're performing recursive template instantiation, create our own
1208 // queue of pending implicit instantiations that we will instantiate later,
1209 // while we're still within our own instantiation context.
1210 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1211 if (Recursive)
1212 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001213
Douglas Gregor7caa6822009-07-24 20:34:43 +00001214 // Enter the scope of this instantiation. We don't use
1215 // PushDeclContext because we don't have a scope.
1216 DeclContext *PreviousContext = CurContext;
1217 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001218
John McCallce3ff2b2009-08-25 22:02:44 +00001219 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00001220 getTemplateInstantiationArgs(Var)));
Mike Stump1eb44332009-09-09 15:08:12 +00001221
Douglas Gregor7caa6822009-07-24 20:34:43 +00001222 CurContext = PreviousContext;
1223
1224 if (Var) {
1225 DeclGroupRef DG(Var);
1226 Consumer.HandleTopLevelDecl(DG);
1227 }
Mike Stump1eb44332009-09-09 15:08:12 +00001228
Douglas Gregor7caa6822009-07-24 20:34:43 +00001229 if (Recursive) {
1230 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001231 // instantiation of this template.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001232 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001233
Douglas Gregor7caa6822009-07-24 20:34:43 +00001234 // Restore the set of pending implicit instantiations.
1235 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001236 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001237}
Douglas Gregor815215d2009-05-27 05:35:12 +00001238
Anders Carlsson09025312009-08-29 05:16:22 +00001239void
1240Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
1241 const CXXConstructorDecl *Tmpl,
1242 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001243
Anders Carlsson09025312009-08-29 05:16:22 +00001244 llvm::SmallVector<MemInitTy*, 4> NewInits;
1245
1246 // Instantiate all the initializers.
1247 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00001248 InitsEnd = Tmpl->init_end();
1249 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00001250 CXXBaseOrMemberInitializer *Init = *Inits;
1251
1252 ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001253
Anders Carlsson09025312009-08-29 05:16:22 +00001254 // Instantiate all the arguments.
1255 for (ExprIterator Args = Init->arg_begin(), ArgsEnd = Init->arg_end();
1256 Args != ArgsEnd; ++Args) {
1257 OwningExprResult NewArg = SubstExpr(*Args, TemplateArgs);
1258
1259 if (NewArg.isInvalid())
1260 New->setInvalidDecl();
1261 else
1262 NewArgs.push_back(NewArg.takeAs<Expr>());
1263 }
1264
1265 MemInitResult NewInit;
1266
1267 if (Init->isBaseInitializer()) {
Eli Friedmanc5573a82009-08-29 22:22:07 +00001268 QualType BaseType(Init->getBaseClass(), 0);
1269 BaseType = SubstType(BaseType, TemplateArgs, Init->getSourceLocation(),
1270 New->getDeclName());
Anders Carlsson09025312009-08-29 05:16:22 +00001271
1272 NewInit = BuildBaseInitializer(BaseType,
Mike Stump1eb44332009-09-09 15:08:12 +00001273 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001274 NewArgs.size(),
1275 Init->getSourceLocation(),
1276 Init->getRParenLoc(),
1277 New->getParent());
1278 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001279 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00001280
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001281 // Is this an anonymous union?
1282 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregore95b4092009-09-16 18:34:49 +00001283 Member = cast<FieldDecl>(FindInstantiatedDecl(UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001284 else
Douglas Gregore95b4092009-09-16 18:34:49 +00001285 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMember(),
1286 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00001287
1288 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001289 NewArgs.size(),
1290 Init->getSourceLocation(),
1291 Init->getRParenLoc());
1292 }
1293
1294 if (NewInit.isInvalid())
1295 New->setInvalidDecl();
1296 else {
1297 // FIXME: It would be nice if ASTOwningVector had a release function.
1298 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001299
Anders Carlsson09025312009-08-29 05:16:22 +00001300 NewInits.push_back((MemInitTy *)NewInit.get());
1301 }
1302 }
Mike Stump1eb44332009-09-09 15:08:12 +00001303
Anders Carlsson09025312009-08-29 05:16:22 +00001304 // Assign all the initializers to the new constructor.
Mike Stump1eb44332009-09-09 15:08:12 +00001305 ActOnMemInitializers(DeclPtrTy::make(New),
Anders Carlsson09025312009-08-29 05:16:22 +00001306 /*FIXME: ColonLoc */
1307 SourceLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001308 NewInits.data(), NewInits.size());
Anders Carlsson09025312009-08-29 05:16:22 +00001309}
1310
John McCall52a575a2009-08-29 08:11:13 +00001311// TODO: this could be templated if the various decl types used the
1312// same method name.
1313static bool isInstantiationOf(ClassTemplateDecl *Pattern,
1314 ClassTemplateDecl *Instance) {
1315 Pattern = Pattern->getCanonicalDecl();
1316
1317 do {
1318 Instance = Instance->getCanonicalDecl();
1319 if (Pattern == Instance) return true;
1320 Instance = Instance->getInstantiatedFromMemberTemplate();
1321 } while (Instance);
1322
1323 return false;
1324}
1325
Douglas Gregor0d696532009-09-28 06:34:35 +00001326static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
1327 FunctionTemplateDecl *Instance) {
1328 Pattern = Pattern->getCanonicalDecl();
1329
1330 do {
1331 Instance = Instance->getCanonicalDecl();
1332 if (Pattern == Instance) return true;
1333 Instance = Instance->getInstantiatedFromMemberTemplate();
1334 } while (Instance);
1335
1336 return false;
1337}
1338
John McCall52a575a2009-08-29 08:11:13 +00001339static bool isInstantiationOf(CXXRecordDecl *Pattern,
1340 CXXRecordDecl *Instance) {
1341 Pattern = Pattern->getCanonicalDecl();
1342
1343 do {
1344 Instance = Instance->getCanonicalDecl();
1345 if (Pattern == Instance) return true;
1346 Instance = Instance->getInstantiatedFromMemberClass();
1347 } while (Instance);
1348
1349 return false;
1350}
1351
1352static bool isInstantiationOf(FunctionDecl *Pattern,
1353 FunctionDecl *Instance) {
1354 Pattern = Pattern->getCanonicalDecl();
1355
1356 do {
1357 Instance = Instance->getCanonicalDecl();
1358 if (Pattern == Instance) return true;
1359 Instance = Instance->getInstantiatedFromMemberFunction();
1360 } while (Instance);
1361
1362 return false;
1363}
1364
1365static bool isInstantiationOf(EnumDecl *Pattern,
1366 EnumDecl *Instance) {
1367 Pattern = Pattern->getCanonicalDecl();
1368
1369 do {
1370 Instance = Instance->getCanonicalDecl();
1371 if (Pattern == Instance) return true;
1372 Instance = Instance->getInstantiatedFromMemberEnum();
1373 } while (Instance);
1374
1375 return false;
1376}
1377
Anders Carlsson0d8df782009-08-29 19:37:28 +00001378static bool isInstantiationOf(UnresolvedUsingDecl *Pattern,
1379 UsingDecl *Instance,
1380 ASTContext &C) {
1381 return C.getInstantiatedFromUnresolvedUsingDecl(Instance) == Pattern;
1382}
1383
John McCall52a575a2009-08-29 08:11:13 +00001384static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
1385 VarDecl *Instance) {
1386 assert(Instance->isStaticDataMember());
1387
1388 Pattern = Pattern->getCanonicalDecl();
1389
1390 do {
1391 Instance = Instance->getCanonicalDecl();
1392 if (Pattern == Instance) return true;
1393 Instance = Instance->getInstantiatedFromStaticDataMember();
1394 } while (Instance);
1395
1396 return false;
1397}
1398
Douglas Gregor815215d2009-05-27 05:35:12 +00001399static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00001400 if (D->getKind() != Other->getKind()) {
1401 if (UnresolvedUsingDecl *UUD = dyn_cast<UnresolvedUsingDecl>(D)) {
1402 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
1403 return isInstantiationOf(UUD, UD, Ctx);
1404 }
1405 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001406
Anders Carlsson0d8df782009-08-29 19:37:28 +00001407 return false;
1408 }
Mike Stump1eb44332009-09-09 15:08:12 +00001409
John McCall52a575a2009-08-29 08:11:13 +00001410 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
1411 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001412
John McCall52a575a2009-08-29 08:11:13 +00001413 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
1414 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00001415
John McCall52a575a2009-08-29 08:11:13 +00001416 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
1417 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00001418
Douglas Gregor7caa6822009-07-24 20:34:43 +00001419 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00001420 if (Var->isStaticDataMember())
1421 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
1422
1423 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
1424 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00001425
Douglas Gregor0d696532009-09-28 06:34:35 +00001426 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
1427 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
1428
Anders Carlssond8b285f2009-09-01 04:26:58 +00001429 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
1430 if (!Field->getDeclName()) {
1431 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00001432 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00001433 cast<FieldDecl>(D);
1434 }
1435 }
Mike Stump1eb44332009-09-09 15:08:12 +00001436
Douglas Gregor815215d2009-05-27 05:35:12 +00001437 return D->getDeclName() && isa<NamedDecl>(Other) &&
1438 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
1439}
1440
1441template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00001442static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00001443 NamedDecl *D,
1444 ForwardIterator first,
1445 ForwardIterator last) {
1446 for (; first != last; ++first)
1447 if (isInstantiationOf(Ctx, D, *first))
1448 return cast<NamedDecl>(*first);
1449
1450 return 0;
1451}
1452
John McCall02cace72009-08-28 07:59:38 +00001453/// \brief Finds the instantiation of the given declaration context
1454/// within the current instantiation.
1455///
1456/// \returns NULL if there was an error
Douglas Gregore95b4092009-09-16 18:34:49 +00001457DeclContext *Sema::FindInstantiatedContext(DeclContext* DC,
1458 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00001459 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00001460 Decl* ID = FindInstantiatedDecl(D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00001461 return cast_or_null<DeclContext>(ID);
1462 } else return DC;
1463}
1464
Douglas Gregored961e72009-05-27 17:54:46 +00001465/// \brief Find the instantiation of the given declaration within the
1466/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00001467///
1468/// This routine is intended to be used when \p D is a declaration
1469/// referenced from within a template, that needs to mapped into the
1470/// corresponding declaration within an instantiation. For example,
1471/// given:
1472///
1473/// \code
1474/// template<typename T>
1475/// struct X {
1476/// enum Kind {
1477/// KnownValue = sizeof(T)
1478/// };
1479///
1480/// bool getKind() const { return KnownValue; }
1481/// };
1482///
1483/// template struct X<int>;
1484/// \endcode
1485///
1486/// In the instantiation of X<int>::getKind(), we need to map the
1487/// EnumConstantDecl for KnownValue (which refers to
1488/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00001489/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
1490/// this mapping from within the instantiation of X<int>.
Douglas Gregore95b4092009-09-16 18:34:49 +00001491NamedDecl *Sema::FindInstantiatedDecl(NamedDecl *D,
1492 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor44c73842009-09-01 17:53:10 +00001493 if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D)) {
1494 // Transform all of the elements of the overloaded function set.
Mike Stump1eb44332009-09-09 15:08:12 +00001495 OverloadedFunctionDecl *Result
Douglas Gregor44c73842009-09-01 17:53:10 +00001496 = OverloadedFunctionDecl::Create(Context, CurContext, Ovl->getDeclName());
Mike Stump1eb44332009-09-09 15:08:12 +00001497
Douglas Gregor44c73842009-09-01 17:53:10 +00001498 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
1499 FEnd = Ovl->function_end();
1500 F != FEnd; ++F) {
1501 Result->addOverload(
Douglas Gregore95b4092009-09-16 18:34:49 +00001502 AnyFunctionDecl::getFromNamedDecl(FindInstantiatedDecl(*F,
1503 TemplateArgs)));
Douglas Gregor44c73842009-09-01 17:53:10 +00001504 }
Mike Stump1eb44332009-09-09 15:08:12 +00001505
Douglas Gregor44c73842009-09-01 17:53:10 +00001506 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001507 }
1508
Douglas Gregor815215d2009-05-27 05:35:12 +00001509 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor2bba76b2009-05-27 17:07:49 +00001510 if (isa<ParmVarDecl>(D) || ParentDC->isFunctionOrMethod()) {
1511 // D is a local of some kind. Look into the map of local
1512 // declarations to their instantiations.
1513 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
1514 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001515
Douglas Gregore95b4092009-09-16 18:34:49 +00001516 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
1517 if (!Record->isDependentContext())
1518 return D;
1519
1520 // If the RecordDecl is actually the injected-class-name or a "templated"
1521 // declaration for a class template or class template partial
1522 // specialization, substitute into the injected-class-name of the
1523 // class template or partial specialization to find the new DeclContext.
1524 QualType T;
1525 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
1526
1527 if (ClassTemplate) {
1528 T = ClassTemplate->getInjectedClassNameType(Context);
1529 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
1530 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1531 T = Context.getTypeDeclType(Record);
1532 ClassTemplate = PartialSpec->getSpecializedTemplate();
1533 }
1534
1535 if (!T.isNull()) {
1536 // Substitute into the injected-class-name to get the type corresponding
1537 // to the instantiation we want. This substitution should never fail,
1538 // since we know we can instantiate the injected-class-name or we wouldn't
1539 // have gotten to the injected-class-name!
1540 // FIXME: Can we use the CurrentInstantiationScope to avoid this extra
1541 // instantiation in the common case?
1542 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
1543 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
1544
1545 if (!T->isDependentType()) {
1546 assert(T->isRecordType() && "Instantiation must produce a record type");
1547 return T->getAs<RecordType>()->getDecl();
1548 }
1549
1550 // We are performing "partial" template instantiation to create the
1551 // member declarations for the members of a class template
1552 // specialization. Therefore, D is actually referring to something in
1553 // the current instantiation. Look through the current context,
1554 // which contains actual instantiations, to find the instantiation of
1555 // the "current instantiation" that D refers to.
Mike Stump1eb44332009-09-09 15:08:12 +00001556 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00001557 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001558 if (ClassTemplateSpecializationDecl *Spec
John McCall52a575a2009-08-29 08:11:13 +00001559 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00001560 if (isInstantiationOf(ClassTemplate,
1561 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00001562 return Spec;
1563 }
1564
Mike Stump1eb44332009-09-09 15:08:12 +00001565 assert(false &&
John McCall52a575a2009-08-29 08:11:13 +00001566 "Unable to find declaration for the current instantiation");
Douglas Gregore95b4092009-09-16 18:34:49 +00001567 return Record;
John McCall52a575a2009-08-29 08:11:13 +00001568 }
Douglas Gregore95b4092009-09-16 18:34:49 +00001569
1570 // Fall through to deal with other dependent record types (e.g.,
1571 // anonymous unions in class templates).
1572 }
John McCall52a575a2009-08-29 08:11:13 +00001573
Douglas Gregore95b4092009-09-16 18:34:49 +00001574 if (!ParentDC->isDependentContext())
1575 return D;
1576
1577 ParentDC = FindInstantiatedContext(ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001578 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00001579 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001580
Douglas Gregor815215d2009-05-27 05:35:12 +00001581 if (ParentDC != D->getDeclContext()) {
1582 // We performed some kind of instantiation in the parent context,
1583 // so now we need to look into the instantiated parent context to
1584 // find the instantiation of the declaration D.
1585 NamedDecl *Result = 0;
1586 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001587 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00001588 Result = findInstantiationOf(Context, D, Found.first, Found.second);
1589 } else {
1590 // Since we don't have a name for the entity we're looking for,
1591 // our only option is to walk through all of the declarations to
1592 // find that name. This will occur in a few cases:
1593 //
1594 // - anonymous struct/union within a template
1595 // - unnamed class/struct/union/enum within a template
1596 //
1597 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00001598 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001599 ParentDC->decls_begin(),
1600 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00001601 }
Mike Stump1eb44332009-09-09 15:08:12 +00001602
Douglas Gregor815215d2009-05-27 05:35:12 +00001603 assert(Result && "Unable to find instantiation of declaration!");
1604 D = Result;
1605 }
1606
Douglas Gregor815215d2009-05-27 05:35:12 +00001607 return D;
1608}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001609
Mike Stump1eb44332009-09-09 15:08:12 +00001610/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001611/// instantiations we have seen until this point.
1612void Sema::PerformPendingImplicitInstantiations() {
1613 while (!PendingImplicitInstantiations.empty()) {
1614 PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front();
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001615 PendingImplicitInstantiations.pop_front();
Mike Stump1eb44332009-09-09 15:08:12 +00001616
Douglas Gregor7caa6822009-07-24 20:34:43 +00001617 // Instantiate function definitions
1618 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001619 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001620 Function->getLocation(), *this,
1621 Context.getSourceManager(),
1622 "instantiating function definition");
Mike Stump1eb44332009-09-09 15:08:12 +00001623
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001624 if (!Function->getBody())
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001625 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001626 continue;
1627 }
Mike Stump1eb44332009-09-09 15:08:12 +00001628
Douglas Gregor7caa6822009-07-24 20:34:43 +00001629 // Instantiate static data member definitions.
1630 VarDecl *Var = cast<VarDecl>(Inst.first);
1631 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001632
Mike Stump1eb44332009-09-09 15:08:12 +00001633 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001634 Var->getLocation(), *this,
1635 Context.getSourceManager(),
1636 "instantiating static data member "
1637 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00001638
Douglas Gregor7caa6822009-07-24 20:34:43 +00001639 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001640 }
1641}