blob: 4bbef29de0ba93d8f490e656dc1e20ec6e4a31b0 [file] [log] [blame]
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001//===--- SemaTemplateInstantiateDecl.cpp - C++ Template Decl Instantiation ===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//
9// This file implements C++ template instantiation for declarations.
10//
11//===----------------------------------------------------------------------===/
12#include "Sema.h"
Douglas Gregoraba43bb2009-05-26 20:50:29 +000013#include "clang/AST/ASTConsumer.h"
Douglas Gregor8dbc2692009-03-17 21:15:40 +000014#include "clang/AST/ASTContext.h"
15#include "clang/AST/DeclTemplate.h"
16#include "clang/AST/DeclVisitor.h"
17#include "clang/AST/Expr.h"
Anders Carlssonc17fb7b2009-09-01 05:12:24 +000018#include "clang/Basic/PrettyStackTrace.h"
Douglas Gregor83ddad32009-08-26 21:14:46 +000019#include "clang/Lex/Preprocessor.h"
Douglas Gregor8dbc2692009-03-17 21:15:40 +000020#include "llvm/Support/Compiler.h"
21
22using namespace clang;
23
24namespace {
Mike Stump1eb44332009-09-09 15:08:12 +000025 class VISIBILITY_HIDDEN TemplateDeclInstantiator
Chris Lattnerb28317a2009-03-28 19:18:32 +000026 : public DeclVisitor<TemplateDeclInstantiator, Decl *> {
Douglas Gregor8dbc2692009-03-17 21:15:40 +000027 Sema &SemaRef;
28 DeclContext *Owner;
Douglas Gregord6350ae2009-08-28 20:31:08 +000029 const MultiLevelTemplateArgumentList &TemplateArgs;
Mike Stump1eb44332009-09-09 15:08:12 +000030
Douglas Gregor8dbc2692009-03-17 21:15:40 +000031 public:
32 typedef Sema::OwningExprResult OwningExprResult;
33
34 TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +000035 const MultiLevelTemplateArgumentList &TemplateArgs)
Douglas Gregor7e063902009-05-11 23:53:27 +000036 : SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs) { }
Mike Stump1eb44332009-09-09 15:08:12 +000037
Mike Stump390b4cc2009-05-16 07:39:55 +000038 // FIXME: Once we get closer to completion, replace these manually-written
39 // declarations with automatically-generated ones from
40 // clang/AST/DeclNodes.def.
Douglas Gregor4f722be2009-03-25 15:45:12 +000041 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
42 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000043 Decl *VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +000044 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000045 Decl *VisitFieldDecl(FieldDecl *D);
46 Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
47 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor6477b692009-03-25 15:04:13 +000048 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
John McCall02cace72009-08-28 07:59:38 +000049 Decl *VisitFriendDecl(FriendDecl *D);
Douglas Gregora735b202009-10-13 14:39:41 +000050 Decl *VisitFunctionDecl(FunctionDecl *D,
51 TemplateParameterList *TemplateParams = 0);
Douglas Gregord475b8d2009-03-25 21:17:03 +000052 Decl *VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregord60e1052009-08-27 16:57:43 +000053 Decl *VisitCXXMethodDecl(CXXMethodDecl *D,
54 TemplateParameterList *TemplateParams = 0);
Douglas Gregor615c5d42009-03-24 16:43:20 +000055 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
Douglas Gregor03b2b072009-03-24 00:15:49 +000056 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +000057 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor6477b692009-03-25 15:04:13 +000058 ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor2dc0e642009-03-23 23:06:20 +000059 Decl *VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
John McCalle29ba202009-08-20 01:44:21 +000060 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregor7974c3b2009-10-07 17:21:34 +000061 Decl *VisitClassTemplatePartialSpecializationDecl(
62 ClassTemplatePartialSpecializationDecl *D);
Douglas Gregord60e1052009-08-27 16:57:43 +000063 Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
John McCalle29ba202009-08-20 01:44:21 +000064 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Anders Carlsson0dde18e2009-08-28 15:18:15 +000065 Decl *VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D);
Mike Stump1eb44332009-09-09 15:08:12 +000066
Douglas Gregor8dbc2692009-03-17 21:15:40 +000067 // Base case. FIXME: Remove once we can instantiate everything.
Mike Stump1eb44332009-09-09 15:08:12 +000068 Decl *VisitDecl(Decl *) {
Douglas Gregor3d7a12a2009-03-25 23:32:15 +000069 assert(false && "Template instantiation of unknown declaration kind!");
Douglas Gregor8dbc2692009-03-17 21:15:40 +000070 return 0;
71 }
Douglas Gregor5545e162009-03-24 00:38:23 +000072
John McCallfd810b12009-08-14 02:03:10 +000073 const LangOptions &getLangOptions() {
74 return SemaRef.getLangOptions();
75 }
76
Douglas Gregor5545e162009-03-24 00:38:23 +000077 // Helper functions for instantiating methods.
John McCallce3ff2b2009-08-25 22:02:44 +000078 QualType SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +000079 llvm::SmallVectorImpl<ParmVarDecl *> &Params);
Douglas Gregore53060f2009-06-25 22:08:12 +000080 bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
Douglas Gregor5545e162009-03-24 00:38:23 +000081 bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
John McCalle29ba202009-08-20 01:44:21 +000082
83 TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +000084 SubstTemplateParams(TemplateParameterList *List);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000085 };
86}
87
Douglas Gregor4f722be2009-03-25 15:45:12 +000088Decl *
89TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
90 assert(false && "Translation units cannot be instantiated");
91 return D;
92}
93
94Decl *
95TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
96 assert(false && "Namespaces cannot be instantiated");
97 return D;
98}
99
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000100Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
101 bool Invalid = false;
102 QualType T = D->getUnderlyingType();
103 if (T->isDependentType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000104 T = SemaRef.SubstType(T, TemplateArgs,
John McCallce3ff2b2009-08-25 22:02:44 +0000105 D->getLocation(), D->getDeclName());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000106 if (T.isNull()) {
107 Invalid = true;
108 T = SemaRef.Context.IntTy;
109 }
110 }
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000112 // Create the new typedef
113 TypedefDecl *Typedef
114 = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
115 D->getIdentifier(), T);
116 if (Invalid)
117 Typedef->setInvalidDecl();
118
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000119 Owner->addDecl(Typedef);
Mike Stump1eb44332009-09-09 15:08:12 +0000120
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000121 return Typedef;
122}
123
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000124Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000125 // Do substitution on the type of the declaration
126 QualType T = SemaRef.SubstType(D->getType(), TemplateArgs,
127 D->getTypeSpecStartLoc(),
128 D->getDeclName());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000129 if (T.isNull())
130 return 0;
131
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000132 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000133 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
134 D->getLocation(), D->getIdentifier(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000135 T, D->getDeclaratorInfo(),
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000136 D->getStorageClass());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000137 Var->setThreadSpecified(D->isThreadSpecified());
138 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
139 Var->setDeclaredInCondition(D->isDeclaredInCondition());
Mike Stump1eb44332009-09-09 15:08:12 +0000140
141 // If we are instantiating a static data member defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000142 // out-of-line, the instantiation will have the same lexical
143 // context (which will be a namespace scope) as the template.
144 if (D->isOutOfLine())
145 Var->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000146
Mike Stump390b4cc2009-05-16 07:39:55 +0000147 // FIXME: In theory, we could have a previous declaration for variables that
148 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000149 bool Redeclaration = false;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000150 SemaRef.CheckVariableDeclaration(Var, 0, Redeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Douglas Gregor7caa6822009-07-24 20:34:43 +0000152 if (D->isOutOfLine()) {
153 D->getLexicalDeclContext()->addDecl(Var);
154 Owner->makeDeclVisibleInContext(Var);
155 } else {
156 Owner->addDecl(Var);
157 }
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000159 // Link instantiations of static data members back to the template from
160 // which they were instantiated.
161 if (Var->isStaticDataMember())
162 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
163 TSK_ImplicitInstantiation);
164
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000165 if (D->getInit()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000166 OwningExprResult Init
John McCallce3ff2b2009-08-25 22:02:44 +0000167 = SemaRef.SubstExpr(D->getInit(), TemplateArgs);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000168 if (Init.isInvalid())
169 Var->setInvalidDecl();
Douglas Gregor83ddad32009-08-26 21:14:46 +0000170 else if (ParenListExpr *PLE = dyn_cast<ParenListExpr>((Expr *)Init.get())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000171 // FIXME: We're faking all of the comma locations, which is suboptimal.
Douglas Gregor83ddad32009-08-26 21:14:46 +0000172 // Do we even need these comma locations?
173 llvm::SmallVector<SourceLocation, 4> FakeCommaLocs;
174 if (PLE->getNumExprs() > 0) {
175 FakeCommaLocs.reserve(PLE->getNumExprs() - 1);
176 for (unsigned I = 0, N = PLE->getNumExprs() - 1; I != N; ++I) {
177 Expr *E = PLE->getExpr(I)->Retain();
178 FakeCommaLocs.push_back(
179 SemaRef.PP.getLocForEndOfToken(E->getLocEnd()));
180 }
Douglas Gregore9f8eb62009-08-26 23:26:04 +0000181 PLE->getExpr(PLE->getNumExprs() - 1)->Retain();
Douglas Gregor83ddad32009-08-26 21:14:46 +0000182 }
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Douglas Gregor83ddad32009-08-26 21:14:46 +0000184 // Add the direct initializer to the declaration.
185 SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
Mike Stump1eb44332009-09-09 15:08:12 +0000186 PLE->getLParenLoc(),
Douglas Gregor83ddad32009-08-26 21:14:46 +0000187 Sema::MultiExprArg(SemaRef,
188 (void**)PLE->getExprs(),
189 PLE->getNumExprs()),
190 FakeCommaLocs.data(),
191 PLE->getRParenLoc());
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Douglas Gregor83ddad32009-08-26 21:14:46 +0000193 // When Init is destroyed, it will destroy the instantiated ParenListExpr;
194 // we've explicitly retained all of its subexpressions already.
195 } else
Chris Lattnerb28317a2009-03-28 19:18:32 +0000196 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init),
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000197 D->hasCXXDirectInitializer());
Douglas Gregor65b90052009-07-27 17:43:39 +0000198 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
199 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000200
201 return Var;
202}
203
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000204Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
205 bool Invalid = false;
206 QualType T = D->getType();
207 if (T->isDependentType()) {
John McCallce3ff2b2009-08-25 22:02:44 +0000208 T = SemaRef.SubstType(T, TemplateArgs,
209 D->getLocation(), D->getDeclName());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000210 if (!T.isNull() && T->isFunctionType()) {
211 // C++ [temp.arg.type]p3:
212 // If a declaration acquires a function type through a type
213 // dependent on a template-parameter and this causes a
214 // declaration that does not use the syntactic form of a
215 // function declarator to have function type, the program is
216 // ill-formed.
217 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
218 << T;
219 T = QualType();
220 Invalid = true;
221 }
222 }
223
224 Expr *BitWidth = D->getBitWidth();
225 if (Invalid)
226 BitWidth = 0;
227 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000228 // The bit-width expression is not potentially evaluated.
229 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000230
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000231 OwningExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000232 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000233 if (InstantiatedBitWidth.isInvalid()) {
234 Invalid = true;
235 BitWidth = 0;
236 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000237 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000238 }
239
240 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), T,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000241 D->getDeclaratorInfo(),
Mike Stump1eb44332009-09-09 15:08:12 +0000242 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000243 D->getLocation(),
244 D->isMutable(),
245 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000246 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000247 D->getAccess(),
248 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000249 if (!Field) {
250 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000251 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000252 }
Mike Stump1eb44332009-09-09 15:08:12 +0000253
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000254 if (Invalid)
255 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000257 if (!Field->getDeclName()) {
258 // Keep track of where this decl came from.
259 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000260 }
Mike Stump1eb44332009-09-09 15:08:12 +0000261
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000262 Field->setImplicit(D->isImplicit());
263 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000264
265 return Field;
266}
267
John McCall02cace72009-08-28 07:59:38 +0000268Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
269 FriendDecl::FriendUnion FU;
270
271 // Handle friend type expressions by simply substituting template
272 // parameters into the pattern type.
273 if (Type *Ty = D->getFriendType()) {
274 QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs,
275 D->getLocation(), DeclarationName());
276 if (T.isNull()) return 0;
277
278 assert(getLangOptions().CPlusPlus0x || T->isRecordType());
279 FU = T.getTypePtr();
280
281 // Handle everything else by appropriate substitution.
282 } else {
283 NamedDecl *ND = D->getFriendDecl();
284 assert(ND && "friend decl must be a decl or a type!");
285
Douglas Gregora735b202009-10-13 14:39:41 +0000286 // FIXME: We have a problem here, because the nested call to Visit(ND)
287 // will inject the thing that the friend references into the current
288 // owner, which is wrong.
John McCall02cace72009-08-28 07:59:38 +0000289 Decl *NewND = Visit(ND);
290 if (!NewND) return 0;
291
292 FU = cast<NamedDecl>(NewND);
John McCallfd810b12009-08-14 02:03:10 +0000293 }
Mike Stump1eb44332009-09-09 15:08:12 +0000294
John McCall02cace72009-08-28 07:59:38 +0000295 FriendDecl *FD =
296 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU,
297 D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000298 FD->setAccess(AS_public);
John McCall02cace72009-08-28 07:59:38 +0000299 Owner->addDecl(FD);
300 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000301}
302
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000303Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
304 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Douglas Gregorac7610d2009-06-22 20:57:11 +0000306 // The expression in a static assertion is not potentially evaluated.
307 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000309 OwningExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000310 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000311 if (InstantiatedAssertExpr.isInvalid())
312 return 0;
313
Douglas Gregor43d9d922009-08-08 01:41:12 +0000314 OwningExprResult Message(SemaRef, D->getMessage());
315 D->getMessage()->Retain();
Mike Stump1eb44332009-09-09 15:08:12 +0000316 Decl *StaticAssert
317 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
Chris Lattnerb28317a2009-03-28 19:18:32 +0000318 move(InstantiatedAssertExpr),
319 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000320 return StaticAssert;
321}
322
323Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000324 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000325 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000326 D->getTagKeywordLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000327 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000328 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000329 Enum->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000330 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000331 Enum->startDefinition();
332
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000333 llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000334
335 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000336 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
337 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000338 EC != ECEnd; ++EC) {
339 // The specified value for the enumerator.
340 OwningExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000341 if (Expr *UninstValue = EC->getInitExpr()) {
342 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000343 EnterExpressionEvaluationContext Unevaluated(SemaRef,
Douglas Gregorac7610d2009-06-22 20:57:11 +0000344 Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000345
John McCallce3ff2b2009-08-25 22:02:44 +0000346 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000347 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000348
349 // Drop the initial value and continue.
350 bool isInvalid = false;
351 if (Value.isInvalid()) {
352 Value = SemaRef.Owned((Expr *)0);
353 isInvalid = true;
354 }
355
Mike Stump1eb44332009-09-09 15:08:12 +0000356 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000357 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
358 EC->getLocation(), EC->getIdentifier(),
359 move(Value));
360
361 if (isInvalid) {
362 if (EnumConst)
363 EnumConst->setInvalidDecl();
364 Enum->setInvalidDecl();
365 }
366
367 if (EnumConst) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000368 Enum->addDecl(EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000369 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000370 LastEnumConst = EnumConst;
371 }
372 }
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000374 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000375 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000376 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
377 Sema::DeclPtrTy::make(Enum),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000378 &Enumerators[0], Enumerators.size(),
379 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000380
381 return Enum;
382}
383
Douglas Gregor6477b692009-03-25 15:04:13 +0000384Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
385 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
386 return 0;
387}
388
John McCalle29ba202009-08-20 01:44:21 +0000389Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
390 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000391 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000392 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000393 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000394
395 CXXRecordDecl *Pattern = D->getTemplatedDecl();
396 CXXRecordDecl *RecordInst
397 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner,
398 Pattern->getLocation(), Pattern->getIdentifier(),
Douglas Gregorf0510d42009-10-12 23:11:44 +0000399 Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL,
400 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000401
402 ClassTemplateDecl *Inst
403 = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
404 D->getIdentifier(), InstParams, RecordInst, 0);
405 RecordInst->setDescribedClassTemplate(Inst);
406 Inst->setAccess(D->getAccess());
407 Inst->setInstantiatedFromMemberTemplate(D);
Douglas Gregorf0510d42009-10-12 23:11:44 +0000408
409 // Trigger creation of the type for the instantiation.
410 SemaRef.Context.getTypeDeclType(RecordInst);
411
John McCalle29ba202009-08-20 01:44:21 +0000412 Owner->addDecl(Inst);
413 return Inst;
414}
415
Douglas Gregord60e1052009-08-27 16:57:43 +0000416Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000417TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
418 ClassTemplatePartialSpecializationDecl *D) {
419 assert(false &&"Partial specializations of member templates are unsupported");
420 return 0;
421}
422
423Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000424TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000425 // FIXME: Dig out the out-of-line definition of this function template?
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Douglas Gregord60e1052009-08-27 16:57:43 +0000427 TemplateParameterList *TempParams = D->getTemplateParameters();
428 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000429 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000430 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Douglas Gregora735b202009-10-13 14:39:41 +0000432 FunctionDecl *Instantiated = 0;
433 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
434 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
435 InstParams));
436 else
437 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
438 D->getTemplatedDecl(),
439 InstParams));
440
441 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000442 return 0;
443
Mike Stump1eb44332009-09-09 15:08:12 +0000444 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000445 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000446 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000447 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000448 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000449 assert(InstTemplate &&
450 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
451 if (!InstTemplate->getInstantiatedFromMemberTemplate())
452 InstTemplate->setInstantiatedFromMemberTemplate(D);
453
454 // Add non-friends into the owner.
455 if (!InstTemplate->getFriendObjectKind())
456 Owner->addDecl(InstTemplate);
Douglas Gregord60e1052009-08-27 16:57:43 +0000457 return InstTemplate;
458}
459
Douglas Gregord475b8d2009-03-25 21:17:03 +0000460Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
461 CXXRecordDecl *PrevDecl = 0;
462 if (D->isInjectedClassName())
463 PrevDecl = cast<CXXRecordDecl>(Owner);
464
465 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000466 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000467 D->getLocation(), D->getIdentifier(),
468 D->getTagKeywordLoc(), PrevDecl);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000469 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000470 // FIXME: Check against AS_none is an ugly hack to work around the issue that
471 // the tag decls introduced by friend class declarations don't have an access
472 // specifier. Remove once this area of the code gets sorted out.
473 if (D->getAccess() != AS_none)
474 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000475 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000476 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000477
John McCall02cace72009-08-28 07:59:38 +0000478 // If the original function was part of a friend declaration,
479 // inherit its namespace state.
480 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
481 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
482
Anders Carlssond8b285f2009-09-01 04:26:58 +0000483 Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
484
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000485 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000486 return Record;
487}
488
John McCall02cace72009-08-28 07:59:38 +0000489/// Normal class members are of more specific types and therefore
490/// don't make it here. This function serves two purposes:
491/// 1) instantiating function templates
492/// 2) substituting friend declarations
493/// FIXME: preserve function definitions in case #2
Douglas Gregora735b202009-10-13 14:39:41 +0000494 Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
495 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000496 // Check whether there is already a function template specialization for
497 // this declaration.
498 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
499 void *InsertPos = 0;
Douglas Gregora735b202009-10-13 14:39:41 +0000500 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000501 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000502 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000503 TemplateArgs.getInnermost().getFlatArgumentList(),
504 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor828e2262009-07-29 16:09:57 +0000505 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000506
507 FunctionTemplateSpecializationInfo *Info
508 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor127102b2009-06-29 20:59:39 +0000509 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000510
Douglas Gregor127102b2009-06-29 20:59:39 +0000511 // If we already have a function template specialization, return it.
512 if (Info)
513 return Info->Function;
514 }
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Douglas Gregore53060f2009-06-25 22:08:12 +0000516 Sema::LocalInstantiationScope Scope(SemaRef);
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Douglas Gregore53060f2009-06-25 22:08:12 +0000518 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000519 QualType T = SubstFunctionType(D, Params);
Douglas Gregore53060f2009-06-25 22:08:12 +0000520 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000521 return 0;
John McCallfd810b12009-08-14 02:03:10 +0000522
Douglas Gregore53060f2009-06-25 22:08:12 +0000523 // Build the instantiated method declaration.
Douglas Gregore95b4092009-09-16 18:34:49 +0000524 DeclContext *DC = SemaRef.FindInstantiatedContext(D->getDeclContext(),
525 TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +0000526 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +0000527 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000528 D->getDeclName(), T, D->getDeclaratorInfo(),
529 D->getStorageClass(),
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000530 D->isInline(), D->hasWrittenPrototype());
John McCall02cace72009-08-28 07:59:38 +0000531 Function->setLexicalDeclContext(Owner);
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Douglas Gregore53060f2009-06-25 22:08:12 +0000533 // Attach the parameters
534 for (unsigned P = 0; P < Params.size(); ++P)
535 Params[P]->setOwningFunction(Function);
536 Function->setParams(SemaRef.Context, Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +0000537
Douglas Gregora735b202009-10-13 14:39:41 +0000538 if (TemplateParams) {
539 // Our resulting instantiation is actually a function template, since we
540 // are substituting only the outer template parameters. For example, given
541 //
542 // template<typename T>
543 // struct X {
544 // template<typename U> friend void f(T, U);
545 // };
546 //
547 // X<int> x;
548 //
549 // We are instantiating the friend function template "f" within X<int>,
550 // which means substituting int for T, but leaving "f" as a friend function
551 // template.
552 // Build the function template itself.
553 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Owner,
554 Function->getLocation(),
555 Function->getDeclName(),
556 TemplateParams, Function);
557 Function->setDescribedFunctionTemplate(FunctionTemplate);
558 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
John McCall02cace72009-08-28 07:59:38 +0000559 }
Douglas Gregora735b202009-10-13 14:39:41 +0000560
Douglas Gregore53060f2009-06-25 22:08:12 +0000561 if (InitFunctionInstantiation(Function, D))
562 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000563
Douglas Gregore53060f2009-06-25 22:08:12 +0000564 bool Redeclaration = false;
565 bool OverloadableAttrRequired = false;
Douglas Gregora735b202009-10-13 14:39:41 +0000566
Douglas Gregore53060f2009-06-25 22:08:12 +0000567 NamedDecl *PrevDecl = 0;
Douglas Gregora735b202009-10-13 14:39:41 +0000568 if (TemplateParams || !FunctionTemplate) {
569 // Look only into the namespace where the friend would be declared to
570 // find a previous declaration. This is the innermost enclosing namespace,
571 // as described in ActOnFriendFunctionDecl.
572 Sema::LookupResult R;
573 SemaRef.LookupQualifiedName(R, DC, Function->getDeclName(),
574 Sema::LookupOrdinaryName, true);
575
576 PrevDecl = R.getAsSingleDecl(SemaRef.Context);
577
578 // In C++, the previous declaration we find might be a tag type
579 // (class or enum). In this case, the new declaration will hide the
580 // tag type. Note that this does does not apply if we're declaring a
581 // typedef (C++ [dcl.typedef]p4).
582 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
583 PrevDecl = 0;
584 }
585
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000586 SemaRef.CheckFunctionDeclaration(Function, PrevDecl, false, Redeclaration,
Douglas Gregore53060f2009-06-25 22:08:12 +0000587 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000588
Douglas Gregora735b202009-10-13 14:39:41 +0000589 // If the original function was part of a friend declaration,
590 // inherit its namespace state and add it to the owner.
591 NamedDecl *FromFriendD
592 = TemplateParams? cast<NamedDecl>(D->getDescribedFunctionTemplate()) : D;
593 if (FromFriendD->getFriendObjectKind()) {
594 NamedDecl *ToFriendD = 0;
595 if (TemplateParams) {
596 ToFriendD = cast<NamedDecl>(FunctionTemplate);
597 PrevDecl = FunctionTemplate->getPreviousDeclaration();
598 } else {
599 ToFriendD = Function;
600 PrevDecl = Function->getPreviousDeclaration();
601 }
602 ToFriendD->setObjectOfFriendDecl(PrevDecl != NULL);
603 if (!Owner->isDependentContext() && !PrevDecl)
604 DC->makeDeclVisibleInContext(ToFriendD, /* Recoverable = */ false);
605
606 if (!TemplateParams)
607 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
608 }
609
610 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000611 // Record this function template specialization.
612 Function->setFunctionTemplateSpecialization(SemaRef.Context,
613 FunctionTemplate,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000614 &TemplateArgs.getInnermost(),
Douglas Gregor127102b2009-06-29 20:59:39 +0000615 InsertPos);
John McCallfd810b12009-08-14 02:03:10 +0000616 }
617
Douglas Gregore53060f2009-06-25 22:08:12 +0000618 return Function;
619}
620
Douglas Gregord60e1052009-08-27 16:57:43 +0000621Decl *
622TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
623 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +0000624 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
625 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +0000626 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +0000627 // We are creating a function template specialization from a function
628 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +0000629 // specialization for this particular set of template arguments.
Douglas Gregor6b906862009-08-21 00:16:32 +0000630 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000631 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000632 TemplateArgs.getInnermost().getFlatArgumentList(),
633 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000634 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000635
636 FunctionTemplateSpecializationInfo *Info
637 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor6b906862009-08-21 00:16:32 +0000638 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000639
Douglas Gregor6b906862009-08-21 00:16:32 +0000640 // If we already have a function template specialization, return it.
641 if (Info)
642 return Info->Function;
643 }
644
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000645 Sema::LocalInstantiationScope Scope(SemaRef);
646
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000647 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000648 QualType T = SubstFunctionType(D, Params);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000649 if (T.isNull())
650 return 0;
651
652 // Build the instantiated method declaration.
653 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
Douglas Gregordec06662009-08-21 18:42:58 +0000654 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Douglas Gregordec06662009-08-21 18:42:58 +0000656 DeclarationName Name = D->getDeclName();
Douglas Gregor17e32f32009-08-21 22:43:28 +0000657 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregordec06662009-08-21 18:42:58 +0000658 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
659 Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
660 SemaRef.Context.getCanonicalType(ClassTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000661 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
662 Constructor->getLocation(),
663 Name, T,
Douglas Gregor17e32f32009-08-21 22:43:28 +0000664 Constructor->getDeclaratorInfo(),
Mike Stump1eb44332009-09-09 15:08:12 +0000665 Constructor->isExplicit(),
Douglas Gregor17e32f32009-08-21 22:43:28 +0000666 Constructor->isInline(), false);
667 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
668 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
669 Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
670 SemaRef.Context.getCanonicalType(ClassTy));
671 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
672 Destructor->getLocation(), Name,
673 T, Destructor->isInline(), false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000674 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000675 CanQualType ConvTy
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000676 = SemaRef.Context.getCanonicalType(
John McCall183700f2009-09-21 23:43:11 +0000677 T->getAs<FunctionType>()->getResultType());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000678 Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
679 ConvTy);
680 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
681 Conversion->getLocation(), Name,
682 T, Conversion->getDeclaratorInfo(),
Mike Stump1eb44332009-09-09 15:08:12 +0000683 Conversion->isInline(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000684 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +0000685 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000686 Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
Douglas Gregordec06662009-08-21 18:42:58 +0000687 D->getDeclName(), T, D->getDeclaratorInfo(),
688 D->isStatic(), D->isInline());
689 }
Douglas Gregor6b906862009-08-21 00:16:32 +0000690
Douglas Gregord60e1052009-08-27 16:57:43 +0000691 if (TemplateParams) {
692 // Our resulting instantiation is actually a function template, since we
693 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +0000694 //
Douglas Gregord60e1052009-08-27 16:57:43 +0000695 // template<typename T>
696 // struct X {
697 // template<typename U> void f(T, U);
698 // };
699 //
700 // X<int> x;
701 //
702 // We are instantiating the member template "f" within X<int>, which means
703 // substituting int for T, but leaving "f" as a member function template.
704 // Build the function template itself.
705 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
706 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +0000707 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +0000708 TemplateParams, Method);
709 if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +0000710 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +0000711 Method->setDescribedFunctionTemplate(FunctionTemplate);
712 } else if (!FunctionTemplate)
Douglas Gregor2db32322009-10-07 23:56:10 +0000713 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000714
Mike Stump1eb44332009-09-09 15:08:12 +0000715 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000716 // out-of-line, the instantiation will have the same lexical
717 // context (which will be a namespace scope) as the template.
718 if (D->isOutOfLine())
719 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000720
Douglas Gregor5545e162009-03-24 00:38:23 +0000721 // Attach the parameters
722 for (unsigned P = 0; P < Params.size(); ++P)
723 Params[P]->setOwningFunction(Method);
Jay Foadbeaaccd2009-05-21 09:52:38 +0000724 Method->setParams(SemaRef.Context, Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +0000725
726 if (InitMethodInstantiation(Method, D))
727 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000728
Douglas Gregordec06662009-08-21 18:42:58 +0000729 NamedDecl *PrevDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000730
Douglas Gregord60e1052009-08-27 16:57:43 +0000731 if (!FunctionTemplate || TemplateParams) {
John McCallf36e02d2009-10-09 21:13:30 +0000732 Sema::LookupResult R;
733 SemaRef.LookupQualifiedName(R, Owner, Name, Sema::LookupOrdinaryName, true);
734 PrevDecl = R.getAsSingleDecl(SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000735
Douglas Gregordec06662009-08-21 18:42:58 +0000736 // In C++, the previous declaration we find might be a tag type
737 // (class or enum). In this case, the new declaration will hide the
738 // tag type. Note that this does does not apply if we're declaring a
739 // typedef (C++ [dcl.typedef]p4).
740 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
741 PrevDecl = 0;
742 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000743
Douglas Gregord60e1052009-08-27 16:57:43 +0000744 if (FunctionTemplate && !TemplateParams)
Douglas Gregor6b906862009-08-21 00:16:32 +0000745 // Record this function template specialization.
746 Method->setFunctionTemplateSpecialization(SemaRef.Context,
747 FunctionTemplate,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000748 &TemplateArgs.getInnermost(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000749 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000750
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000751 bool Redeclaration = false;
752 bool OverloadableAttrRequired = false;
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000753 SemaRef.CheckFunctionDeclaration(Method, PrevDecl, false, Redeclaration,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000754 /*FIXME:*/OverloadableAttrRequired);
755
Douglas Gregora735b202009-10-13 14:39:41 +0000756 if (!FunctionTemplate && (!Method->isInvalidDecl() || !PrevDecl) &&
757 !Method->getFriendObjectKind())
Douglas Gregordec06662009-08-21 18:42:58 +0000758 Owner->addDecl(Method);
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000760 return Method;
761}
762
Douglas Gregor615c5d42009-03-24 16:43:20 +0000763Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +0000764 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000765}
766
Douglas Gregor03b2b072009-03-24 00:15:49 +0000767Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +0000768 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +0000769}
770
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000771Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000772 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000773}
774
Douglas Gregor6477b692009-03-25 15:04:13 +0000775ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000776 QualType OrigT = SemaRef.SubstType(D->getOriginalType(), TemplateArgs,
Douglas Gregor7e063902009-05-11 23:53:27 +0000777 D->getLocation(), D->getDeclName());
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000778 if (OrigT.isNull())
779 return 0;
780
781 QualType T = SemaRef.adjustParameterType(OrigT);
782
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000783 // Allocate the parameter
784 ParmVarDecl *Param = 0;
785 if (T == OrigT)
786 Param = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000787 D->getIdentifier(), T, D->getDeclaratorInfo(),
788 D->getStorageClass(), 0);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000789 else
Mike Stump1eb44332009-09-09 15:08:12 +0000790 Param = OriginalParmVarDecl::Create(SemaRef.Context, Owner,
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000791 D->getLocation(), D->getIdentifier(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000792 T, D->getDeclaratorInfo(), OrigT,
793 D->getStorageClass(), 0);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000794
Anders Carlsson9351c172009-08-25 03:18:48 +0000795 // Mark the default argument as being uninstantiated.
Douglas Gregorf43d0b32009-09-25 06:56:31 +0000796 if (D->hasUninstantiatedDefaultArg())
797 Param->setUninstantiatedDefaultArg(D->getUninstantiatedDefaultArg());
Douglas Gregor0ed09302009-09-25 07:03:22 +0000798 else if (Expr *Arg = D->getDefaultArg())
799 Param->setUninstantiatedDefaultArg(Arg);
800
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000801 // Note: we don't try to instantiate function parameters until after
802 // we've instantiated the function's type. Therefore, we don't have
803 // to check for 'void' parameter types here.
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000804 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000805 return Param;
806}
807
808Decl *
809TemplateDeclInstantiator::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
810 // Since parameter types can decay either before or after
811 // instantiation, we simply treat OriginalParmVarDecls as
812 // ParmVarDecls the same way, and create one or the other depending
813 // on what happens after template instantiation.
814 return VisitParmVarDecl(D);
815}
816
John McCalle29ba202009-08-20 01:44:21 +0000817Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
818 TemplateTypeParmDecl *D) {
819 // TODO: don't always clone when decls are refcounted.
820 const Type* T = D->getTypeForDecl();
821 assert(T->isTemplateTypeParmType());
822 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000823
John McCalle29ba202009-08-20 01:44:21 +0000824 TemplateTypeParmDecl *Inst =
825 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
826 TTPT->getDepth(), TTPT->getIndex(),
827 TTPT->getName(),
828 D->wasDeclaredWithTypename(),
829 D->isParameterPack());
830
831 if (D->hasDefaultArgument()) {
832 QualType DefaultPattern = D->getDefaultArgument();
833 QualType DefaultInst
John McCallce3ff2b2009-08-25 22:02:44 +0000834 = SemaRef.SubstType(DefaultPattern, TemplateArgs,
835 D->getDefaultArgumentLoc(),
836 D->getDeclName());
Mike Stump1eb44332009-09-09 15:08:12 +0000837
John McCalle29ba202009-08-20 01:44:21 +0000838 Inst->setDefaultArgument(DefaultInst,
839 D->getDefaultArgumentLoc(),
840 D->defaultArgumentWasInherited() /* preserve? */);
841 }
842
843 return Inst;
844}
845
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000846Decl *
847TemplateDeclInstantiator::VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000848 NestedNameSpecifier *NNS =
849 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
850 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000851 TemplateArgs);
852 if (!NNS)
853 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000854
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000855 CXXScopeSpec SS;
856 SS.setRange(D->getTargetNestedNameRange());
857 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +0000858
859 NamedDecl *UD =
860 SemaRef.BuildUsingDeclaration(D->getLocation(), SS,
861 D->getTargetNameLocation(),
Anders Carlsson0d8df782009-08-29 19:37:28 +0000862 D->getTargetName(), 0, D->isTypeName());
863 if (UD)
Mike Stump1eb44332009-09-09 15:08:12 +0000864 SemaRef.Context.setInstantiatedFromUnresolvedUsingDecl(cast<UsingDecl>(UD),
Anders Carlsson0d8df782009-08-29 19:37:28 +0000865 D);
866 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000867}
868
John McCallce3ff2b2009-08-25 22:02:44 +0000869Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000870 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +0000871 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000872 return Instantiator.Visit(D);
873}
874
John McCalle29ba202009-08-20 01:44:21 +0000875/// \brief Instantiates a nested template parameter list in the current
876/// instantiation context.
877///
878/// \param L The parameter list to instantiate
879///
880/// \returns NULL if there was an error
881TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +0000882TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +0000883 // Get errors for all the parameters before bailing out.
884 bool Invalid = false;
885
886 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000887 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +0000888 ParamVector Params;
889 Params.reserve(N);
890 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
891 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000892 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +0000893 Params.push_back(D);
894 Invalid = Invalid || !D;
895 }
896
897 // Clean up if we had an error.
898 if (Invalid) {
899 for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
900 PI != PE; ++PI)
901 if (*PI)
902 (*PI)->Destroy(SemaRef.Context);
903 return NULL;
904 }
905
906 TemplateParameterList *InstL
907 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
908 L->getLAngleLoc(), &Params.front(), N,
909 L->getRAngleLoc());
910 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +0000911}
John McCalle29ba202009-08-20 01:44:21 +0000912
John McCallce3ff2b2009-08-25 22:02:44 +0000913/// \brief Does substitution on the type of the given function, including
914/// all of the function parameters.
Douglas Gregor5545e162009-03-24 00:38:23 +0000915///
John McCallce3ff2b2009-08-25 22:02:44 +0000916/// \param D The function whose type will be the basis of the substitution
Douglas Gregor5545e162009-03-24 00:38:23 +0000917///
918/// \param Params the instantiated parameter declarations
919
John McCallce3ff2b2009-08-25 22:02:44 +0000920/// \returns the instantiated function's type if successful, a NULL
Douglas Gregor5545e162009-03-24 00:38:23 +0000921/// type if there was an error.
Mike Stump1eb44332009-09-09 15:08:12 +0000922QualType
John McCallce3ff2b2009-08-25 22:02:44 +0000923TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +0000924 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
925 bool InvalidDecl = false;
926
John McCallce3ff2b2009-08-25 22:02:44 +0000927 // Substitute all of the function's formal parameter types.
Douglas Gregor7e063902009-05-11 23:53:27 +0000928 TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000929 llvm::SmallVector<QualType, 4> ParamTys;
Mike Stump1eb44332009-09-09 15:08:12 +0000930 for (FunctionDecl::param_iterator P = D->param_begin(),
Douglas Gregor5545e162009-03-24 00:38:23 +0000931 PEnd = D->param_end();
932 P != PEnd; ++P) {
Douglas Gregor6477b692009-03-25 15:04:13 +0000933 if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
Douglas Gregor5545e162009-03-24 00:38:23 +0000934 if (PInst->getType()->isVoidType()) {
935 SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
936 PInst->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000937 } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000938 PInst->getType(),
939 diag::err_abstract_type_in_decl,
940 Sema::AbstractParamType))
Douglas Gregor5545e162009-03-24 00:38:23 +0000941 PInst->setInvalidDecl();
942
943 Params.push_back(PInst);
944 ParamTys.push_back(PInst->getType());
945
946 if (PInst->isInvalidDecl())
947 InvalidDecl = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000948 } else
Douglas Gregor5545e162009-03-24 00:38:23 +0000949 InvalidDecl = true;
950 }
951
952 // FIXME: Deallocate dead declarations.
953 if (InvalidDecl)
954 return QualType();
955
John McCall183700f2009-09-21 23:43:11 +0000956 const FunctionProtoType *Proto = D->getType()->getAs<FunctionProtoType>();
Douglas Gregor5545e162009-03-24 00:38:23 +0000957 assert(Proto && "Missing prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +0000958 QualType ResultType
John McCallce3ff2b2009-08-25 22:02:44 +0000959 = SemaRef.SubstType(Proto->getResultType(), TemplateArgs,
960 D->getLocation(), D->getDeclName());
Douglas Gregor5545e162009-03-24 00:38:23 +0000961 if (ResultType.isNull())
962 return QualType();
963
Jay Foadbeaaccd2009-05-21 09:52:38 +0000964 return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
Douglas Gregor5545e162009-03-24 00:38:23 +0000965 Proto->isVariadic(), Proto->getTypeQuals(),
966 D->getLocation(), D->getDeclName());
967}
968
Mike Stump1eb44332009-09-09 15:08:12 +0000969/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +0000970/// declaration (New) from the corresponding fields of its template (Tmpl).
971///
972/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +0000973bool
974TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +0000975 FunctionDecl *Tmpl) {
976 if (Tmpl->isDeleted())
977 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Douglas Gregorcca9e962009-07-01 22:01:06 +0000979 // If we are performing substituting explicitly-specified template arguments
980 // or deduced template arguments into a function template and we reach this
981 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +0000982 // to keeping the new function template specialization. We therefore
983 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +0000984 // into a template instantiation for this specific function template
985 // specialization, which is not a SFINAE context, so that we diagnose any
986 // further errors in the declaration itself.
987 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
988 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
989 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
990 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +0000991 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +0000992 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000993 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +0000994 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +0000995 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +0000996 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
997 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
998 }
999 }
Mike Stump1eb44332009-09-09 15:08:12 +00001000
Douglas Gregore53060f2009-06-25 22:08:12 +00001001 return false;
1002}
1003
Douglas Gregor5545e162009-03-24 00:38:23 +00001004/// \brief Initializes common fields of an instantiated method
1005/// declaration (New) from the corresponding fields of its template
1006/// (Tmpl).
1007///
1008/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001009bool
1010TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00001011 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001012 if (InitFunctionInstantiation(New, Tmpl))
1013 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Douglas Gregor5545e162009-03-24 00:38:23 +00001015 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
1016 New->setAccess(Tmpl->getAccess());
Anders Carlsson77b7f1d2009-05-14 22:15:41 +00001017 if (Tmpl->isVirtualAsWritten()) {
1018 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +00001019 Record->setAggregate(false);
1020 Record->setPOD(false);
Eli Friedman1d954f62009-08-15 21:55:26 +00001021 Record->setEmpty(false);
Douglas Gregor5545e162009-03-24 00:38:23 +00001022 Record->setPolymorphic(true);
1023 }
Douglas Gregor5545e162009-03-24 00:38:23 +00001024 if (Tmpl->isPure()) {
1025 New->setPure();
1026 Record->setAbstract(true);
1027 }
1028
1029 // FIXME: attributes
1030 // FIXME: New needs a pointer to Tmpl
1031 return false;
1032}
Douglas Gregora58861f2009-05-13 20:28:22 +00001033
1034/// \brief Instantiate the definition of the given function from its
1035/// template.
1036///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001037/// \param PointOfInstantiation the point at which the instantiation was
1038/// required. Note that this is not precisely a "point of instantiation"
1039/// for the function, but it's close.
1040///
Douglas Gregora58861f2009-05-13 20:28:22 +00001041/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001042/// function template specialization or member function of a class template
1043/// specialization.
1044///
1045/// \param Recursive if true, recursively instantiates any functions that
1046/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001047///
1048/// \param DefinitionRequired if true, then we are performing an explicit
1049/// instantiation where the body of the function is required. Complain if
1050/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001051void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001052 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001053 bool Recursive,
1054 bool DefinitionRequired) {
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001055 if (Function->isInvalidDecl())
1056 return;
1057
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001058 assert(!Function->getBody() && "Already instantiated!");
Mike Stump1eb44332009-09-09 15:08:12 +00001059
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001060 // Never instantiate an explicit specialization.
1061 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1062 return;
1063
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001064 // Find the function body that we'll be substituting.
Douglas Gregor1637be72009-06-26 00:10:03 +00001065 const FunctionDecl *PatternDecl = 0;
Douglas Gregor5ec178f2009-08-28 21:09:48 +00001066 if (FunctionTemplateDecl *Primary = Function->getPrimaryTemplate()) {
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001067 while (Primary->getInstantiatedFromMemberTemplate()) {
1068 // If we have hit a point where the user provided a specialization of
1069 // this template, we're done looking.
1070 if (Primary->isMemberSpecialization())
1071 break;
1072
Douglas Gregor5ec178f2009-08-28 21:09:48 +00001073 Primary = Primary->getInstantiatedFromMemberTemplate();
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001074 }
1075
Douglas Gregor1637be72009-06-26 00:10:03 +00001076 PatternDecl = Primary->getTemplatedDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001077 } else
Douglas Gregor1637be72009-06-26 00:10:03 +00001078 PatternDecl = Function->getInstantiatedFromMemberFunction();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001079 Stmt *Pattern = 0;
1080 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001081 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001082
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001083 if (!Pattern) {
1084 if (DefinitionRequired) {
1085 if (Function->getPrimaryTemplate())
1086 Diag(PointOfInstantiation,
1087 diag::err_explicit_instantiation_undefined_func_template)
1088 << Function->getPrimaryTemplate();
1089 else
1090 Diag(PointOfInstantiation,
1091 diag::err_explicit_instantiation_undefined_member)
1092 << 1 << Function->getDeclName() << Function->getDeclContext();
1093
1094 if (PatternDecl)
1095 Diag(PatternDecl->getLocation(),
1096 diag::note_explicit_instantiation_here);
1097 }
1098
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001099 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001100 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001101
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001102 // C++0x [temp.explicit]p9:
1103 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00001104 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001105 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00001106 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001107 == TSK_ExplicitInstantiationDeclaration &&
1108 PatternDecl->isOutOfLine() && !PatternDecl->isInline())
1109 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001110
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001111 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
1112 if (Inst)
1113 return;
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001114
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001115 // If we're performing recursive template instantiation, create our own
1116 // queue of pending implicit instantiations that we will instantiate later,
1117 // while we're still within our own instantiation context.
1118 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1119 if (Recursive)
1120 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001121
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001122 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
1123
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001124 // Introduce a new scope where local variable instantiations will be
1125 // recorded.
1126 LocalInstantiationScope Scope(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001127
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001128 // Introduce the instantiated function parameters into the local
1129 // instantiation scope.
1130 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
1131 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
1132 Function->getParamDecl(I));
1133
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001134 // Enter the scope of this instantiation. We don't use
1135 // PushDeclContext because we don't have a scope.
1136 DeclContext *PreviousContext = CurContext;
1137 CurContext = Function;
1138
Mike Stump1eb44332009-09-09 15:08:12 +00001139 MultiLevelTemplateArgumentList TemplateArgs =
Anders Carlsson09025312009-08-29 05:16:22 +00001140 getTemplateInstantiationArgs(Function);
1141
1142 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001143 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00001144 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
1145 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
1146 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001147 }
1148
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001149 // Instantiate the function body.
Anders Carlsson09025312009-08-29 05:16:22 +00001150 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001151
Douglas Gregor52604ab2009-09-11 21:19:12 +00001152 if (Body.isInvalid())
1153 Function->setInvalidDecl();
1154
Mike Stump1eb44332009-09-09 15:08:12 +00001155 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001156 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001157
1158 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00001159
1160 DeclGroupRef DG(Function);
1161 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001162
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001163 if (Recursive) {
1164 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001165 // instantiation of this template.
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001166 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001167
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001168 // Restore the set of pending implicit instantiations.
1169 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1170 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001171}
1172
1173/// \brief Instantiate the definition of the given variable from its
1174/// template.
1175///
Douglas Gregor7caa6822009-07-24 20:34:43 +00001176/// \param PointOfInstantiation the point at which the instantiation was
1177/// required. Note that this is not precisely a "point of instantiation"
1178/// for the function, but it's close.
1179///
1180/// \param Var the already-instantiated declaration of a static member
1181/// variable of a class template specialization.
1182///
1183/// \param Recursive if true, recursively instantiates any functions that
1184/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001185///
1186/// \param DefinitionRequired if true, then we are performing an explicit
1187/// instantiation where an out-of-line definition of the member variable
1188/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001189void Sema::InstantiateStaticDataMemberDefinition(
1190 SourceLocation PointOfInstantiation,
1191 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001192 bool Recursive,
1193 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001194 if (Var->isInvalidDecl())
1195 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001196
Douglas Gregor7caa6822009-07-24 20:34:43 +00001197 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001198 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
1199 bool FoundOutOfLineDef = false;
1200 assert(Def && "This data member was not instantiated from a template?");
Mike Stump1eb44332009-09-09 15:08:12 +00001201 assert(Def->isStaticDataMember() && "Not a static data member?");
1202 for (VarDecl::redecl_iterator RD = Def->redecls_begin(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00001203 RDEnd = Def->redecls_end();
1204 RD != RDEnd; ++RD) {
1205 if (RD->getLexicalDeclContext()->isFileContext()) {
1206 Def = *RD;
1207 FoundOutOfLineDef = true;
1208 }
1209 }
Mike Stump1eb44332009-09-09 15:08:12 +00001210
Douglas Gregor7caa6822009-07-24 20:34:43 +00001211 if (!FoundOutOfLineDef) {
1212 // We did not find an out-of-line definition of this static data member,
1213 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00001214 // instantiate this definition (or provide a specialization for it) in
1215 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001216 if (DefinitionRequired) {
1217 Diag(PointOfInstantiation,
1218 diag::err_explicit_instantiation_undefined_member)
1219 << 2 << Var->getDeclName() << Var->getDeclContext();
1220 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
1221 }
1222
Douglas Gregor7caa6822009-07-24 20:34:43 +00001223 return;
1224 }
1225
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001226 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001227 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001228 return;
1229
1230 // C++0x [temp.explicit]p9:
1231 // Except for inline functions, other explicit instantiation declarations
1232 // have the effect of suppressing the implicit instantiation of the entity
1233 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001234 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001235 == TSK_ExplicitInstantiationDeclaration)
1236 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001237
Douglas Gregor7caa6822009-07-24 20:34:43 +00001238 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
1239 if (Inst)
1240 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001241
Douglas Gregor7caa6822009-07-24 20:34:43 +00001242 // If we're performing recursive template instantiation, create our own
1243 // queue of pending implicit instantiations that we will instantiate later,
1244 // while we're still within our own instantiation context.
1245 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1246 if (Recursive)
1247 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001248
Douglas Gregor7caa6822009-07-24 20:34:43 +00001249 // Enter the scope of this instantiation. We don't use
1250 // PushDeclContext because we don't have a scope.
1251 DeclContext *PreviousContext = CurContext;
1252 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001253
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001254 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00001255 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00001256 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00001257 CurContext = PreviousContext;
1258
1259 if (Var) {
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001260 Var->setPreviousDeclaration(OldVar);
Douglas Gregor583f33b2009-10-15 18:07:02 +00001261 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
1262 assert(MSInfo && "Missing member specialization information?");
1263 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
1264 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00001265 DeclGroupRef DG(Var);
1266 Consumer.HandleTopLevelDecl(DG);
1267 }
Mike Stump1eb44332009-09-09 15:08:12 +00001268
Douglas Gregor7caa6822009-07-24 20:34:43 +00001269 if (Recursive) {
1270 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001271 // instantiation of this template.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001272 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001273
Douglas Gregor7caa6822009-07-24 20:34:43 +00001274 // Restore the set of pending implicit instantiations.
1275 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001276 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001277}
Douglas Gregor815215d2009-05-27 05:35:12 +00001278
Anders Carlsson09025312009-08-29 05:16:22 +00001279void
1280Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
1281 const CXXConstructorDecl *Tmpl,
1282 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001283
Anders Carlsson09025312009-08-29 05:16:22 +00001284 llvm::SmallVector<MemInitTy*, 4> NewInits;
1285
1286 // Instantiate all the initializers.
1287 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00001288 InitsEnd = Tmpl->init_end();
1289 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00001290 CXXBaseOrMemberInitializer *Init = *Inits;
1291
1292 ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001293
Anders Carlsson09025312009-08-29 05:16:22 +00001294 // Instantiate all the arguments.
1295 for (ExprIterator Args = Init->arg_begin(), ArgsEnd = Init->arg_end();
1296 Args != ArgsEnd; ++Args) {
1297 OwningExprResult NewArg = SubstExpr(*Args, TemplateArgs);
1298
1299 if (NewArg.isInvalid())
1300 New->setInvalidDecl();
1301 else
1302 NewArgs.push_back(NewArg.takeAs<Expr>());
1303 }
1304
1305 MemInitResult NewInit;
1306
1307 if (Init->isBaseInitializer()) {
Eli Friedmanc5573a82009-08-29 22:22:07 +00001308 QualType BaseType(Init->getBaseClass(), 0);
1309 BaseType = SubstType(BaseType, TemplateArgs, Init->getSourceLocation(),
1310 New->getDeclName());
Anders Carlsson09025312009-08-29 05:16:22 +00001311
1312 NewInit = BuildBaseInitializer(BaseType,
Mike Stump1eb44332009-09-09 15:08:12 +00001313 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001314 NewArgs.size(),
1315 Init->getSourceLocation(),
1316 Init->getRParenLoc(),
1317 New->getParent());
1318 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001319 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00001320
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001321 // Is this an anonymous union?
1322 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregore95b4092009-09-16 18:34:49 +00001323 Member = cast<FieldDecl>(FindInstantiatedDecl(UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001324 else
Douglas Gregore95b4092009-09-16 18:34:49 +00001325 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMember(),
1326 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00001327
1328 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001329 NewArgs.size(),
1330 Init->getSourceLocation(),
1331 Init->getRParenLoc());
1332 }
1333
1334 if (NewInit.isInvalid())
1335 New->setInvalidDecl();
1336 else {
1337 // FIXME: It would be nice if ASTOwningVector had a release function.
1338 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001339
Anders Carlsson09025312009-08-29 05:16:22 +00001340 NewInits.push_back((MemInitTy *)NewInit.get());
1341 }
1342 }
Mike Stump1eb44332009-09-09 15:08:12 +00001343
Anders Carlsson09025312009-08-29 05:16:22 +00001344 // Assign all the initializers to the new constructor.
Mike Stump1eb44332009-09-09 15:08:12 +00001345 ActOnMemInitializers(DeclPtrTy::make(New),
Anders Carlsson09025312009-08-29 05:16:22 +00001346 /*FIXME: ColonLoc */
1347 SourceLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001348 NewInits.data(), NewInits.size());
Anders Carlsson09025312009-08-29 05:16:22 +00001349}
1350
John McCall52a575a2009-08-29 08:11:13 +00001351// TODO: this could be templated if the various decl types used the
1352// same method name.
1353static bool isInstantiationOf(ClassTemplateDecl *Pattern,
1354 ClassTemplateDecl *Instance) {
1355 Pattern = Pattern->getCanonicalDecl();
1356
1357 do {
1358 Instance = Instance->getCanonicalDecl();
1359 if (Pattern == Instance) return true;
1360 Instance = Instance->getInstantiatedFromMemberTemplate();
1361 } while (Instance);
1362
1363 return false;
1364}
1365
Douglas Gregor0d696532009-09-28 06:34:35 +00001366static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
1367 FunctionTemplateDecl *Instance) {
1368 Pattern = Pattern->getCanonicalDecl();
1369
1370 do {
1371 Instance = Instance->getCanonicalDecl();
1372 if (Pattern == Instance) return true;
1373 Instance = Instance->getInstantiatedFromMemberTemplate();
1374 } while (Instance);
1375
1376 return false;
1377}
1378
John McCall52a575a2009-08-29 08:11:13 +00001379static bool isInstantiationOf(CXXRecordDecl *Pattern,
1380 CXXRecordDecl *Instance) {
1381 Pattern = Pattern->getCanonicalDecl();
1382
1383 do {
1384 Instance = Instance->getCanonicalDecl();
1385 if (Pattern == Instance) return true;
1386 Instance = Instance->getInstantiatedFromMemberClass();
1387 } while (Instance);
1388
1389 return false;
1390}
1391
1392static bool isInstantiationOf(FunctionDecl *Pattern,
1393 FunctionDecl *Instance) {
1394 Pattern = Pattern->getCanonicalDecl();
1395
1396 do {
1397 Instance = Instance->getCanonicalDecl();
1398 if (Pattern == Instance) return true;
1399 Instance = Instance->getInstantiatedFromMemberFunction();
1400 } while (Instance);
1401
1402 return false;
1403}
1404
1405static bool isInstantiationOf(EnumDecl *Pattern,
1406 EnumDecl *Instance) {
1407 Pattern = Pattern->getCanonicalDecl();
1408
1409 do {
1410 Instance = Instance->getCanonicalDecl();
1411 if (Pattern == Instance) return true;
1412 Instance = Instance->getInstantiatedFromMemberEnum();
1413 } while (Instance);
1414
1415 return false;
1416}
1417
Anders Carlsson0d8df782009-08-29 19:37:28 +00001418static bool isInstantiationOf(UnresolvedUsingDecl *Pattern,
1419 UsingDecl *Instance,
1420 ASTContext &C) {
1421 return C.getInstantiatedFromUnresolvedUsingDecl(Instance) == Pattern;
1422}
1423
John McCall52a575a2009-08-29 08:11:13 +00001424static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
1425 VarDecl *Instance) {
1426 assert(Instance->isStaticDataMember());
1427
1428 Pattern = Pattern->getCanonicalDecl();
1429
1430 do {
1431 Instance = Instance->getCanonicalDecl();
1432 if (Pattern == Instance) return true;
1433 Instance = Instance->getInstantiatedFromStaticDataMember();
1434 } while (Instance);
1435
1436 return false;
1437}
1438
Douglas Gregor815215d2009-05-27 05:35:12 +00001439static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00001440 if (D->getKind() != Other->getKind()) {
1441 if (UnresolvedUsingDecl *UUD = dyn_cast<UnresolvedUsingDecl>(D)) {
1442 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
1443 return isInstantiationOf(UUD, UD, Ctx);
1444 }
1445 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001446
Anders Carlsson0d8df782009-08-29 19:37:28 +00001447 return false;
1448 }
Mike Stump1eb44332009-09-09 15:08:12 +00001449
John McCall52a575a2009-08-29 08:11:13 +00001450 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
1451 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001452
John McCall52a575a2009-08-29 08:11:13 +00001453 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
1454 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00001455
John McCall52a575a2009-08-29 08:11:13 +00001456 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
1457 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00001458
Douglas Gregor7caa6822009-07-24 20:34:43 +00001459 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00001460 if (Var->isStaticDataMember())
1461 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
1462
1463 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
1464 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00001465
Douglas Gregor0d696532009-09-28 06:34:35 +00001466 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
1467 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
1468
Anders Carlssond8b285f2009-09-01 04:26:58 +00001469 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
1470 if (!Field->getDeclName()) {
1471 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00001472 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00001473 cast<FieldDecl>(D);
1474 }
1475 }
Mike Stump1eb44332009-09-09 15:08:12 +00001476
Douglas Gregor815215d2009-05-27 05:35:12 +00001477 return D->getDeclName() && isa<NamedDecl>(Other) &&
1478 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
1479}
1480
1481template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00001482static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00001483 NamedDecl *D,
1484 ForwardIterator first,
1485 ForwardIterator last) {
1486 for (; first != last; ++first)
1487 if (isInstantiationOf(Ctx, D, *first))
1488 return cast<NamedDecl>(*first);
1489
1490 return 0;
1491}
1492
John McCall02cace72009-08-28 07:59:38 +00001493/// \brief Finds the instantiation of the given declaration context
1494/// within the current instantiation.
1495///
1496/// \returns NULL if there was an error
Douglas Gregore95b4092009-09-16 18:34:49 +00001497DeclContext *Sema::FindInstantiatedContext(DeclContext* DC,
1498 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00001499 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00001500 Decl* ID = FindInstantiatedDecl(D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00001501 return cast_or_null<DeclContext>(ID);
1502 } else return DC;
1503}
1504
Douglas Gregored961e72009-05-27 17:54:46 +00001505/// \brief Find the instantiation of the given declaration within the
1506/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00001507///
1508/// This routine is intended to be used when \p D is a declaration
1509/// referenced from within a template, that needs to mapped into the
1510/// corresponding declaration within an instantiation. For example,
1511/// given:
1512///
1513/// \code
1514/// template<typename T>
1515/// struct X {
1516/// enum Kind {
1517/// KnownValue = sizeof(T)
1518/// };
1519///
1520/// bool getKind() const { return KnownValue; }
1521/// };
1522///
1523/// template struct X<int>;
1524/// \endcode
1525///
1526/// In the instantiation of X<int>::getKind(), we need to map the
1527/// EnumConstantDecl for KnownValue (which refers to
1528/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00001529/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
1530/// this mapping from within the instantiation of X<int>.
Douglas Gregore95b4092009-09-16 18:34:49 +00001531NamedDecl *Sema::FindInstantiatedDecl(NamedDecl *D,
1532 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor44c73842009-09-01 17:53:10 +00001533 if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D)) {
1534 // Transform all of the elements of the overloaded function set.
Mike Stump1eb44332009-09-09 15:08:12 +00001535 OverloadedFunctionDecl *Result
Douglas Gregor44c73842009-09-01 17:53:10 +00001536 = OverloadedFunctionDecl::Create(Context, CurContext, Ovl->getDeclName());
Mike Stump1eb44332009-09-09 15:08:12 +00001537
Douglas Gregor44c73842009-09-01 17:53:10 +00001538 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
1539 FEnd = Ovl->function_end();
1540 F != FEnd; ++F) {
1541 Result->addOverload(
Douglas Gregore95b4092009-09-16 18:34:49 +00001542 AnyFunctionDecl::getFromNamedDecl(FindInstantiatedDecl(*F,
1543 TemplateArgs)));
Douglas Gregor44c73842009-09-01 17:53:10 +00001544 }
Mike Stump1eb44332009-09-09 15:08:12 +00001545
Douglas Gregor44c73842009-09-01 17:53:10 +00001546 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001547 }
1548
Douglas Gregor815215d2009-05-27 05:35:12 +00001549 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor2bba76b2009-05-27 17:07:49 +00001550 if (isa<ParmVarDecl>(D) || ParentDC->isFunctionOrMethod()) {
1551 // D is a local of some kind. Look into the map of local
1552 // declarations to their instantiations.
1553 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
1554 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001555
Douglas Gregore95b4092009-09-16 18:34:49 +00001556 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
1557 if (!Record->isDependentContext())
1558 return D;
1559
1560 // If the RecordDecl is actually the injected-class-name or a "templated"
1561 // declaration for a class template or class template partial
1562 // specialization, substitute into the injected-class-name of the
1563 // class template or partial specialization to find the new DeclContext.
1564 QualType T;
1565 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
1566
1567 if (ClassTemplate) {
1568 T = ClassTemplate->getInjectedClassNameType(Context);
1569 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
1570 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1571 T = Context.getTypeDeclType(Record);
1572 ClassTemplate = PartialSpec->getSpecializedTemplate();
1573 }
1574
1575 if (!T.isNull()) {
1576 // Substitute into the injected-class-name to get the type corresponding
1577 // to the instantiation we want. This substitution should never fail,
1578 // since we know we can instantiate the injected-class-name or we wouldn't
1579 // have gotten to the injected-class-name!
1580 // FIXME: Can we use the CurrentInstantiationScope to avoid this extra
1581 // instantiation in the common case?
1582 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
1583 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
1584
1585 if (!T->isDependentType()) {
1586 assert(T->isRecordType() && "Instantiation must produce a record type");
1587 return T->getAs<RecordType>()->getDecl();
1588 }
1589
1590 // We are performing "partial" template instantiation to create the
1591 // member declarations for the members of a class template
1592 // specialization. Therefore, D is actually referring to something in
1593 // the current instantiation. Look through the current context,
1594 // which contains actual instantiations, to find the instantiation of
1595 // the "current instantiation" that D refers to.
Mike Stump1eb44332009-09-09 15:08:12 +00001596 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00001597 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001598 if (ClassTemplateSpecializationDecl *Spec
John McCall52a575a2009-08-29 08:11:13 +00001599 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00001600 if (isInstantiationOf(ClassTemplate,
1601 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00001602 return Spec;
1603 }
1604
Mike Stump1eb44332009-09-09 15:08:12 +00001605 assert(false &&
John McCall52a575a2009-08-29 08:11:13 +00001606 "Unable to find declaration for the current instantiation");
Douglas Gregore95b4092009-09-16 18:34:49 +00001607 return Record;
John McCall52a575a2009-08-29 08:11:13 +00001608 }
Douglas Gregore95b4092009-09-16 18:34:49 +00001609
1610 // Fall through to deal with other dependent record types (e.g.,
1611 // anonymous unions in class templates).
1612 }
John McCall52a575a2009-08-29 08:11:13 +00001613
Douglas Gregore95b4092009-09-16 18:34:49 +00001614 if (!ParentDC->isDependentContext())
1615 return D;
1616
1617 ParentDC = FindInstantiatedContext(ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001618 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00001619 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001620
Douglas Gregor815215d2009-05-27 05:35:12 +00001621 if (ParentDC != D->getDeclContext()) {
1622 // We performed some kind of instantiation in the parent context,
1623 // so now we need to look into the instantiated parent context to
1624 // find the instantiation of the declaration D.
1625 NamedDecl *Result = 0;
1626 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001627 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00001628 Result = findInstantiationOf(Context, D, Found.first, Found.second);
1629 } else {
1630 // Since we don't have a name for the entity we're looking for,
1631 // our only option is to walk through all of the declarations to
1632 // find that name. This will occur in a few cases:
1633 //
1634 // - anonymous struct/union within a template
1635 // - unnamed class/struct/union/enum within a template
1636 //
1637 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00001638 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001639 ParentDC->decls_begin(),
1640 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00001641 }
Mike Stump1eb44332009-09-09 15:08:12 +00001642
Douglas Gregor815215d2009-05-27 05:35:12 +00001643 assert(Result && "Unable to find instantiation of declaration!");
1644 D = Result;
1645 }
1646
Douglas Gregor815215d2009-05-27 05:35:12 +00001647 return D;
1648}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001649
Mike Stump1eb44332009-09-09 15:08:12 +00001650/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001651/// instantiations we have seen until this point.
1652void Sema::PerformPendingImplicitInstantiations() {
1653 while (!PendingImplicitInstantiations.empty()) {
1654 PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front();
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001655 PendingImplicitInstantiations.pop_front();
Mike Stump1eb44332009-09-09 15:08:12 +00001656
Douglas Gregor7caa6822009-07-24 20:34:43 +00001657 // Instantiate function definitions
1658 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001659 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001660 Function->getLocation(), *this,
1661 Context.getSourceManager(),
1662 "instantiating function definition");
Mike Stump1eb44332009-09-09 15:08:12 +00001663
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001664 if (!Function->getBody())
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001665 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001666 continue;
1667 }
Mike Stump1eb44332009-09-09 15:08:12 +00001668
Douglas Gregor7caa6822009-07-24 20:34:43 +00001669 // Instantiate static data member definitions.
1670 VarDecl *Var = cast<VarDecl>(Inst.first);
1671 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001672
Mike Stump1eb44332009-09-09 15:08:12 +00001673 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00001674 Var->getLocation(), *this,
1675 Context.getSourceManager(),
1676 "instantiating static data member "
1677 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00001678
Douglas Gregor7caa6822009-07-24 20:34:43 +00001679 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001680 }
1681}