blob: a5eb7793b844cc5d62ef26fbb027e8f863290782 [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"
18#include "llvm/Support/Compiler.h"
19
20using namespace clang;
21
22namespace {
23 class VISIBILITY_HIDDEN TemplateDeclInstantiator
Chris Lattnerb28317a2009-03-28 19:18:32 +000024 : public DeclVisitor<TemplateDeclInstantiator, Decl *> {
Douglas Gregor8dbc2692009-03-17 21:15:40 +000025 Sema &SemaRef;
26 DeclContext *Owner;
Douglas Gregor7e063902009-05-11 23:53:27 +000027 const TemplateArgumentList &TemplateArgs;
Douglas Gregor8dbc2692009-03-17 21:15:40 +000028
29 public:
30 typedef Sema::OwningExprResult OwningExprResult;
31
32 TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner,
Douglas Gregor7e063902009-05-11 23:53:27 +000033 const TemplateArgumentList &TemplateArgs)
34 : SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs) { }
Douglas Gregor8dbc2692009-03-17 21:15:40 +000035
Mike Stump390b4cc2009-05-16 07:39:55 +000036 // FIXME: Once we get closer to completion, replace these manually-written
37 // declarations with automatically-generated ones from
38 // clang/AST/DeclNodes.def.
Douglas Gregor4f722be2009-03-25 15:45:12 +000039 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
40 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000041 Decl *VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +000042 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000043 Decl *VisitFieldDecl(FieldDecl *D);
44 Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
45 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor6477b692009-03-25 15:04:13 +000046 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregord475b8d2009-03-25 21:17:03 +000047 Decl *VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregor2dc0e642009-03-23 23:06:20 +000048 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
Douglas Gregor615c5d42009-03-24 16:43:20 +000049 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
Douglas Gregor03b2b072009-03-24 00:15:49 +000050 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +000051 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor6477b692009-03-25 15:04:13 +000052 ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor2dc0e642009-03-23 23:06:20 +000053 Decl *VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
Douglas Gregor5545e162009-03-24 00:38:23 +000054
Douglas Gregor8dbc2692009-03-17 21:15:40 +000055 // Base case. FIXME: Remove once we can instantiate everything.
56 Decl *VisitDecl(Decl *) {
Douglas Gregor3d7a12a2009-03-25 23:32:15 +000057 assert(false && "Template instantiation of unknown declaration kind!");
Douglas Gregor8dbc2692009-03-17 21:15:40 +000058 return 0;
59 }
Douglas Gregor5545e162009-03-24 00:38:23 +000060
61 // Helper functions for instantiating methods.
62 QualType InstantiateFunctionType(FunctionDecl *D,
63 llvm::SmallVectorImpl<ParmVarDecl *> &Params);
64 bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000065 };
66}
67
Douglas Gregor4f722be2009-03-25 15:45:12 +000068Decl *
69TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
70 assert(false && "Translation units cannot be instantiated");
71 return D;
72}
73
74Decl *
75TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
76 assert(false && "Namespaces cannot be instantiated");
77 return D;
78}
79
Douglas Gregor8dbc2692009-03-17 21:15:40 +000080Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
81 bool Invalid = false;
82 QualType T = D->getUnderlyingType();
83 if (T->isDependentType()) {
Douglas Gregor1eee0e72009-05-14 21:06:31 +000084 T = SemaRef.InstantiateType(T, TemplateArgs,
85 D->getLocation(), D->getDeclName());
Douglas Gregor8dbc2692009-03-17 21:15:40 +000086 if (T.isNull()) {
87 Invalid = true;
88 T = SemaRef.Context.IntTy;
89 }
90 }
91
92 // Create the new typedef
93 TypedefDecl *Typedef
94 = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
95 D->getIdentifier(), T);
96 if (Invalid)
97 Typedef->setInvalidDecl();
98
Douglas Gregor6ab35242009-04-09 21:40:53 +000099 Owner->addDecl(SemaRef.Context, Typedef);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000100 return Typedef;
101}
102
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000103Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
104 // Instantiate the type of the declaration
105 QualType T = SemaRef.InstantiateType(D->getType(), TemplateArgs,
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000106 D->getTypeSpecStartLoc(),
107 D->getDeclName());
108 if (T.isNull())
109 return 0;
110
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000111 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000112 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
113 D->getLocation(), D->getIdentifier(),
114 T, D->getStorageClass(),
115 D->getTypeSpecStartLoc());
116 Var->setThreadSpecified(D->isThreadSpecified());
117 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
118 Var->setDeclaredInCondition(D->isDeclaredInCondition());
119
Mike Stump390b4cc2009-05-16 07:39:55 +0000120 // FIXME: In theory, we could have a previous declaration for variables that
121 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000122 bool Redeclaration = false;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000123 SemaRef.CheckVariableDeclaration(Var, 0, Redeclaration);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000124 Owner->addDecl(SemaRef.Context, Var);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000125
126 if (D->getInit()) {
127 OwningExprResult Init
Douglas Gregor7e063902009-05-11 23:53:27 +0000128 = SemaRef.InstantiateExpr(D->getInit(), TemplateArgs);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000129 if (Init.isInvalid())
130 Var->setInvalidDecl();
131 else
Chris Lattnerb28317a2009-03-28 19:18:32 +0000132 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init),
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000133 D->hasCXXDirectInitializer());
Douglas Gregord308e622009-05-18 20:51:54 +0000134 } else {
135 // FIXME: Call ActOnUninitializedDecl? (Not always)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000136 }
137
138 return Var;
139}
140
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000141Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
142 bool Invalid = false;
143 QualType T = D->getType();
144 if (T->isDependentType()) {
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000145 T = SemaRef.InstantiateType(T, TemplateArgs,
146 D->getLocation(), D->getDeclName());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000147 if (!T.isNull() && T->isFunctionType()) {
148 // C++ [temp.arg.type]p3:
149 // If a declaration acquires a function type through a type
150 // dependent on a template-parameter and this causes a
151 // declaration that does not use the syntactic form of a
152 // function declarator to have function type, the program is
153 // ill-formed.
154 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
155 << T;
156 T = QualType();
157 Invalid = true;
158 }
159 }
160
161 Expr *BitWidth = D->getBitWidth();
162 if (Invalid)
163 BitWidth = 0;
164 else if (BitWidth) {
165 OwningExprResult InstantiatedBitWidth
Douglas Gregor7e063902009-05-11 23:53:27 +0000166 = SemaRef.InstantiateExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000167 if (InstantiatedBitWidth.isInvalid()) {
168 Invalid = true;
169 BitWidth = 0;
170 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000171 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000172 }
173
174 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), T,
175 cast<RecordDecl>(Owner),
176 D->getLocation(),
177 D->isMutable(),
178 BitWidth,
179 D->getAccess(),
180 0);
181 if (Field) {
182 if (Invalid)
183 Field->setInvalidDecl();
184
Douglas Gregor6ab35242009-04-09 21:40:53 +0000185 Owner->addDecl(SemaRef.Context, Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000186 }
187
188 return Field;
189}
190
191Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
192 Expr *AssertExpr = D->getAssertExpr();
193
194 OwningExprResult InstantiatedAssertExpr
Douglas Gregor7e063902009-05-11 23:53:27 +0000195 = SemaRef.InstantiateExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000196 if (InstantiatedAssertExpr.isInvalid())
197 return 0;
198
199 OwningExprResult Message = SemaRef.Clone(D->getMessage());
200 Decl *StaticAssert
Chris Lattnerb28317a2009-03-28 19:18:32 +0000201 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
202 move(InstantiatedAssertExpr),
203 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000204 return StaticAssert;
205}
206
207Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
208 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
209 D->getLocation(), D->getIdentifier(),
210 /*PrevDecl=*/0);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000211 Enum->setAccess(D->getAccess());
Douglas Gregor6ab35242009-04-09 21:40:53 +0000212 Owner->addDecl(SemaRef.Context, Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000213 Enum->startDefinition();
214
Chris Lattnerb28317a2009-03-28 19:18:32 +0000215 llvm::SmallVector<Sema::DeclPtrTy, 16> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000216
217 EnumConstantDecl *LastEnumConst = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000218 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(SemaRef.Context),
219 ECEnd = D->enumerator_end(SemaRef.Context);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000220 EC != ECEnd; ++EC) {
221 // The specified value for the enumerator.
222 OwningExprResult Value = SemaRef.Owned((Expr *)0);
223 if (Expr *UninstValue = EC->getInitExpr())
Douglas Gregor7e063902009-05-11 23:53:27 +0000224 Value = SemaRef.InstantiateExpr(UninstValue, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000225
226 // Drop the initial value and continue.
227 bool isInvalid = false;
228 if (Value.isInvalid()) {
229 Value = SemaRef.Owned((Expr *)0);
230 isInvalid = true;
231 }
232
233 EnumConstantDecl *EnumConst
234 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
235 EC->getLocation(), EC->getIdentifier(),
236 move(Value));
237
238 if (isInvalid) {
239 if (EnumConst)
240 EnumConst->setInvalidDecl();
241 Enum->setInvalidDecl();
242 }
243
244 if (EnumConst) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000245 Enum->addDecl(SemaRef.Context, EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000246 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000247 LastEnumConst = EnumConst;
248 }
249 }
250
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000251 // FIXME: Fixup LBraceLoc and RBraceLoc
252 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
253 Sema::DeclPtrTy::make(Enum),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000254 &Enumerators[0], Enumerators.size());
255
256 return Enum;
257}
258
Douglas Gregor6477b692009-03-25 15:04:13 +0000259Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
260 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
261 return 0;
262}
263
Douglas Gregord475b8d2009-03-25 21:17:03 +0000264Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
265 CXXRecordDecl *PrevDecl = 0;
266 if (D->isInjectedClassName())
267 PrevDecl = cast<CXXRecordDecl>(Owner);
268
269 CXXRecordDecl *Record
270 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
271 D->getLocation(), D->getIdentifier(), PrevDecl);
272 Record->setImplicit(D->isImplicit());
273 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000274 if (!D->isInjectedClassName())
275 Record->setInstantiationOfMemberClass(D);
276
Douglas Gregor6ab35242009-04-09 21:40:53 +0000277 Owner->addDecl(SemaRef.Context, Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000278 return Record;
279}
280
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000281Decl *TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D) {
282 // Only handle actual methods; we'll deal with constructors,
283 // destructors, etc. separately.
284 if (D->getKind() != Decl::CXXMethod)
285 return 0;
286
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000287 Sema::LocalInstantiationScope Scope(SemaRef);
288
Douglas Gregor5545e162009-03-24 00:38:23 +0000289 llvm::SmallVector<ParmVarDecl *, 16> Params;
290 QualType T = InstantiateFunctionType(D, Params);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000291 if (T.isNull())
292 return 0;
293
294 // Build the instantiated method declaration.
295 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
296 CXXMethodDecl *Method
297 = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
298 D->getDeclName(), T, D->isStatic(),
299 D->isInline());
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000300 Method->setInstantiationOfMemberFunction(D);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000301
Douglas Gregor5545e162009-03-24 00:38:23 +0000302 // Attach the parameters
303 for (unsigned P = 0; P < Params.size(); ++P)
304 Params[P]->setOwningFunction(Method);
Jay Foadbeaaccd2009-05-21 09:52:38 +0000305 Method->setParams(SemaRef.Context, Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +0000306
307 if (InitMethodInstantiation(Method, D))
308 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000309
310 NamedDecl *PrevDecl
311 = SemaRef.LookupQualifiedName(Owner, Method->getDeclName(),
312 Sema::LookupOrdinaryName, true);
313 // In C++, the previous declaration we find might be a tag type
314 // (class or enum). In this case, the new declaration will hide the
315 // tag type. Note that this does does not apply if we're declaring a
316 // typedef (C++ [dcl.typedef]p4).
317 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
318 PrevDecl = 0;
319 bool Redeclaration = false;
320 bool OverloadableAttrRequired = false;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000321 SemaRef.CheckFunctionDeclaration(Method, PrevDecl, Redeclaration,
322 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000323
324 if (!Method->isInvalidDecl() || !PrevDecl)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000325 Owner->addDecl(SemaRef.Context, Method);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000326 return Method;
327}
328
Douglas Gregor615c5d42009-03-24 16:43:20 +0000329Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000330 Sema::LocalInstantiationScope Scope(SemaRef);
331
Douglas Gregor615c5d42009-03-24 16:43:20 +0000332 llvm::SmallVector<ParmVarDecl *, 16> Params;
333 QualType T = InstantiateFunctionType(D, Params);
334 if (T.isNull())
335 return 0;
336
337 // Build the instantiated method declaration.
338 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
339 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
340 DeclarationName Name
Douglas Gregor49f25ec2009-05-15 21:18:27 +0000341 = SemaRef.Context.DeclarationNames.getCXXConstructorName(
342 SemaRef.Context.getCanonicalType(ClassTy));
Douglas Gregor615c5d42009-03-24 16:43:20 +0000343 CXXConstructorDecl *Constructor
344 = CXXConstructorDecl::Create(SemaRef.Context, Record, D->getLocation(),
345 Name, T, D->isExplicit(), D->isInline(),
346 false);
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000347 Constructor->setInstantiationOfMemberFunction(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000348
349 // Attach the parameters
350 for (unsigned P = 0; P < Params.size(); ++P)
351 Params[P]->setOwningFunction(Constructor);
Jay Foadbeaaccd2009-05-21 09:52:38 +0000352 Constructor->setParams(SemaRef.Context, Params.data(), Params.size());
Douglas Gregor615c5d42009-03-24 16:43:20 +0000353
354 if (InitMethodInstantiation(Constructor, D))
355 Constructor->setInvalidDecl();
356
357 NamedDecl *PrevDecl
358 = SemaRef.LookupQualifiedName(Owner, Name, Sema::LookupOrdinaryName, true);
359
360 // In C++, the previous declaration we find might be a tag type
361 // (class or enum). In this case, the new declaration will hide the
362 // tag type. Note that this does does not apply if we're declaring a
363 // typedef (C++ [dcl.typedef]p4).
364 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
365 PrevDecl = 0;
366 bool Redeclaration = false;
367 bool OverloadableAttrRequired = false;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000368 SemaRef.CheckFunctionDeclaration(Constructor, PrevDecl, Redeclaration,
369 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000370
Douglas Gregor49f25ec2009-05-15 21:18:27 +0000371 Record->addedConstructor(SemaRef.Context, Constructor);
Chris Lattnereaaebc72009-04-25 08:06:05 +0000372 Owner->addDecl(SemaRef.Context, Constructor);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000373 return Constructor;
374}
375
Douglas Gregor03b2b072009-03-24 00:15:49 +0000376Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000377 Sema::LocalInstantiationScope Scope(SemaRef);
378
Douglas Gregor5545e162009-03-24 00:38:23 +0000379 llvm::SmallVector<ParmVarDecl *, 16> Params;
380 QualType T = InstantiateFunctionType(D, Params);
Douglas Gregor03b2b072009-03-24 00:15:49 +0000381 if (T.isNull())
382 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +0000383 assert(Params.size() == 0 && "Destructor with parameters?");
384
Douglas Gregor03b2b072009-03-24 00:15:49 +0000385 // Build the instantiated destructor declaration.
386 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
Douglas Gregor49f25ec2009-05-15 21:18:27 +0000387 QualType ClassTy =
388 SemaRef.Context.getCanonicalType(SemaRef.Context.getTypeDeclType(Record));
Douglas Gregor03b2b072009-03-24 00:15:49 +0000389 CXXDestructorDecl *Destructor
390 = CXXDestructorDecl::Create(SemaRef.Context, Record,
391 D->getLocation(),
392 SemaRef.Context.DeclarationNames.getCXXDestructorName(ClassTy),
393 T, D->isInline(), false);
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000394 Destructor->setInstantiationOfMemberFunction(D);
Douglas Gregor5545e162009-03-24 00:38:23 +0000395 if (InitMethodInstantiation(Destructor, D))
396 Destructor->setInvalidDecl();
Douglas Gregor03b2b072009-03-24 00:15:49 +0000397
398 bool Redeclaration = false;
399 bool OverloadableAttrRequired = false;
400 NamedDecl *PrevDecl = 0;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000401 SemaRef.CheckFunctionDeclaration(Destructor, PrevDecl, Redeclaration,
402 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000403 Owner->addDecl(SemaRef.Context, Destructor);
Douglas Gregor03b2b072009-03-24 00:15:49 +0000404 return Destructor;
405}
406
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000407Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000408 Sema::LocalInstantiationScope Scope(SemaRef);
409
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000410 llvm::SmallVector<ParmVarDecl *, 16> Params;
411 QualType T = InstantiateFunctionType(D, Params);
412 if (T.isNull())
413 return 0;
414 assert(Params.size() == 0 && "Destructor with parameters?");
415
416 // Build the instantiated conversion declaration.
417 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
418 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
419 QualType ConvTy
420 = SemaRef.Context.getCanonicalType(T->getAsFunctionType()->getResultType());
421 CXXConversionDecl *Conversion
422 = CXXConversionDecl::Create(SemaRef.Context, Record,
423 D->getLocation(),
424 SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(ConvTy),
425 T, D->isInline(), D->isExplicit());
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000426 Conversion->setInstantiationOfMemberFunction(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000427 if (InitMethodInstantiation(Conversion, D))
428 Conversion->setInvalidDecl();
429
430 bool Redeclaration = false;
431 bool OverloadableAttrRequired = false;
432 NamedDecl *PrevDecl = 0;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000433 SemaRef.CheckFunctionDeclaration(Conversion, PrevDecl, Redeclaration,
434 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000435 Owner->addDecl(SemaRef.Context, Conversion);
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000436 return Conversion;
437}
438
Douglas Gregor6477b692009-03-25 15:04:13 +0000439ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000440 QualType OrigT = SemaRef.InstantiateType(D->getOriginalType(), TemplateArgs,
Douglas Gregor7e063902009-05-11 23:53:27 +0000441 D->getLocation(), D->getDeclName());
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000442 if (OrigT.isNull())
443 return 0;
444
445 QualType T = SemaRef.adjustParameterType(OrigT);
446
447 if (D->getDefaultArg()) {
448 // FIXME: Leave a marker for "uninstantiated" default
449 // arguments. They only get instantiated on demand at the call
450 // site.
451 unsigned DiagID = SemaRef.Diags.getCustomDiagID(Diagnostic::Warning,
452 "sorry, dropping default argument during template instantiation");
453 SemaRef.Diag(D->getDefaultArg()->getSourceRange().getBegin(), DiagID)
454 << D->getDefaultArg()->getSourceRange();
455 }
456
457 // Allocate the parameter
458 ParmVarDecl *Param = 0;
459 if (T == OrigT)
460 Param = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
461 D->getIdentifier(), T, D->getStorageClass(),
462 0);
463 else
464 Param = OriginalParmVarDecl::Create(SemaRef.Context, Owner,
465 D->getLocation(), D->getIdentifier(),
466 T, OrigT, D->getStorageClass(), 0);
467
468 // Note: we don't try to instantiate function parameters until after
469 // we've instantiated the function's type. Therefore, we don't have
470 // to check for 'void' parameter types here.
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000471 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000472 return Param;
473}
474
475Decl *
476TemplateDeclInstantiator::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
477 // Since parameter types can decay either before or after
478 // instantiation, we simply treat OriginalParmVarDecls as
479 // ParmVarDecls the same way, and create one or the other depending
480 // on what happens after template instantiation.
481 return VisitParmVarDecl(D);
482}
483
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000484Decl *Sema::InstantiateDecl(Decl *D, DeclContext *Owner,
Douglas Gregor7e063902009-05-11 23:53:27 +0000485 const TemplateArgumentList &TemplateArgs) {
486 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000487 return Instantiator.Visit(D);
488}
489
Douglas Gregor5545e162009-03-24 00:38:23 +0000490/// \brief Instantiates the type of the given function, including
491/// instantiating all of the function parameters.
492///
493/// \param D The function that we will be instantiated
494///
495/// \param Params the instantiated parameter declarations
496
497/// \returns the instantiated function's type if successfull, a NULL
498/// type if there was an error.
499QualType
500TemplateDeclInstantiator::InstantiateFunctionType(FunctionDecl *D,
501 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
502 bool InvalidDecl = false;
503
504 // Instantiate the function parameters
Douglas Gregor7e063902009-05-11 23:53:27 +0000505 TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
Douglas Gregor5545e162009-03-24 00:38:23 +0000506 llvm::SmallVector<QualType, 16> ParamTys;
507 for (FunctionDecl::param_iterator P = D->param_begin(),
508 PEnd = D->param_end();
509 P != PEnd; ++P) {
Douglas Gregor6477b692009-03-25 15:04:13 +0000510 if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
Douglas Gregor5545e162009-03-24 00:38:23 +0000511 if (PInst->getType()->isVoidType()) {
512 SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
513 PInst->setInvalidDecl();
514 }
515 else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
516 PInst->getType(),
517 diag::err_abstract_type_in_decl,
Anders Carlsson8211eff2009-03-24 01:19:16 +0000518 Sema::AbstractParamType))
Douglas Gregor5545e162009-03-24 00:38:23 +0000519 PInst->setInvalidDecl();
520
521 Params.push_back(PInst);
522 ParamTys.push_back(PInst->getType());
523
524 if (PInst->isInvalidDecl())
525 InvalidDecl = true;
526 } else
527 InvalidDecl = true;
528 }
529
530 // FIXME: Deallocate dead declarations.
531 if (InvalidDecl)
532 return QualType();
533
534 const FunctionProtoType *Proto = D->getType()->getAsFunctionProtoType();
535 assert(Proto && "Missing prototype?");
536 QualType ResultType
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000537 = SemaRef.InstantiateType(Proto->getResultType(), TemplateArgs,
Douglas Gregor5545e162009-03-24 00:38:23 +0000538 D->getLocation(), D->getDeclName());
539 if (ResultType.isNull())
540 return QualType();
541
Jay Foadbeaaccd2009-05-21 09:52:38 +0000542 return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
Douglas Gregor5545e162009-03-24 00:38:23 +0000543 Proto->isVariadic(), Proto->getTypeQuals(),
544 D->getLocation(), D->getDeclName());
545}
546
547/// \brief Initializes common fields of an instantiated method
548/// declaration (New) from the corresponding fields of its template
549/// (Tmpl).
550///
551/// \returns true if there was an error
552bool
553TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
554 CXXMethodDecl *Tmpl) {
555 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
556 New->setAccess(Tmpl->getAccess());
Anders Carlsson77b7f1d2009-05-14 22:15:41 +0000557 if (Tmpl->isVirtualAsWritten()) {
558 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +0000559 Record->setAggregate(false);
560 Record->setPOD(false);
561 Record->setPolymorphic(true);
562 }
563 if (Tmpl->isDeleted())
564 New->setDeleted();
565 if (Tmpl->isPure()) {
566 New->setPure();
567 Record->setAbstract(true);
568 }
569
570 // FIXME: attributes
571 // FIXME: New needs a pointer to Tmpl
572 return false;
573}
Douglas Gregora58861f2009-05-13 20:28:22 +0000574
575/// \brief Instantiate the definition of the given function from its
576/// template.
577///
578/// \param Function the already-instantiated declaration of a
579/// function.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000580void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
581 FunctionDecl *Function) {
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000582 // FIXME: make this work for function template specializations, too.
583
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000584 if (Function->isInvalidDecl())
585 return;
586
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000587 // Find the function body that we'll be substituting.
588 const FunctionDecl *PatternDecl
589 = Function->getInstantiatedFromMemberFunction();
590 Stmt *Pattern = 0;
591 if (PatternDecl)
592 Pattern = PatternDecl->getBody(Context, PatternDecl);
593
594 if (!Pattern)
595 return;
596
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000597 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
598 if (Inst)
599 return;
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000600
Douglas Gregore2c31ff2009-05-15 17:59:04 +0000601 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
602
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000603 // Introduce a new scope where local variable instantiations will be
604 // recorded.
605 LocalInstantiationScope Scope(*this);
606
607 // Introduce the instantiated function parameters into the local
608 // instantiation scope.
609 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
610 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
611 Function->getParamDecl(I));
612
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000613 // Enter the scope of this instantiation. We don't use
614 // PushDeclContext because we don't have a scope.
615 DeclContext *PreviousContext = CurContext;
616 CurContext = Function;
617
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000618 // Instantiate the function body.
619 OwningStmtResult Body
620 = InstantiateStmt(Pattern, getTemplateInstantiationArgs(Function));
Douglas Gregore2c31ff2009-05-15 17:59:04 +0000621
622 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
623 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000624
625 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +0000626
627 DeclGroupRef DG(Function);
628 Consumer.HandleTopLevelDecl(DG);
Douglas Gregora58861f2009-05-13 20:28:22 +0000629}
630
631/// \brief Instantiate the definition of the given variable from its
632/// template.
633///
634/// \param Var the already-instantiated declaration of a variable.
635void Sema::InstantiateVariableDefinition(VarDecl *Var) {
636 // FIXME: Implement this!
637}
Douglas Gregor815215d2009-05-27 05:35:12 +0000638
639static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
640 if (D->getKind() != Other->getKind())
641 return false;
642
643 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
644 return Ctx.getCanonicalDecl(Record->getInstantiatedFromMemberClass())
645 == Ctx.getCanonicalDecl(D);
646
647 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
648 return Ctx.getCanonicalDecl(Function->getInstantiatedFromMemberFunction())
649 == Ctx.getCanonicalDecl(D);
650
651 // FIXME: Need something similar to the above for EnumDecls.
652
653 // FIXME: How can we find instantiations of anonymous unions?
654
655 return D->getDeclName() && isa<NamedDecl>(Other) &&
656 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
657}
658
659template<typename ForwardIterator>
660static NamedDecl *findInstantiationOf(ASTContext &Ctx,
661 NamedDecl *D,
662 ForwardIterator first,
663 ForwardIterator last) {
664 for (; first != last; ++first)
665 if (isInstantiationOf(Ctx, D, *first))
666 return cast<NamedDecl>(*first);
667
668 return 0;
669}
670
671/// \brief Find the instantiation of the given declaration with the
672/// given template arguments.
673///
674/// This routine is intended to be used when \p D is a declaration
675/// referenced from within a template, that needs to mapped into the
676/// corresponding declaration within an instantiation. For example,
677/// given:
678///
679/// \code
680/// template<typename T>
681/// struct X {
682/// enum Kind {
683/// KnownValue = sizeof(T)
684/// };
685///
686/// bool getKind() const { return KnownValue; }
687/// };
688///
689/// template struct X<int>;
690/// \endcode
691///
692/// In the instantiation of X<int>::getKind(), we need to map the
693/// EnumConstantDecl for KnownValue (which refers to
694/// X<T>::<Kind>::KnownValue) to its instantiation
695/// (X<int>::<Kind>::KnownValue). InstantiateDeclRef() performs this
696/// mapping, given the template arguments 'int'.
697NamedDecl *
698Sema::InstantiateDeclRef(NamedDecl *D, const TemplateArgumentList &TemplateArgs) {
699 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor2bba76b2009-05-27 17:07:49 +0000700 if (isa<ParmVarDecl>(D) || ParentDC->isFunctionOrMethod()) {
701 // D is a local of some kind. Look into the map of local
702 // declarations to their instantiations.
703 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
704 }
Douglas Gregor815215d2009-05-27 05:35:12 +0000705
Douglas Gregor2bba76b2009-05-27 17:07:49 +0000706 if (NamedDecl *ParentDecl = dyn_cast<NamedDecl>(ParentDC)) {
Douglas Gregor815215d2009-05-27 05:35:12 +0000707 ParentDecl = InstantiateDeclRef(ParentDecl, TemplateArgs);
708 if (!ParentDecl)
709 return 0;
710
711 ParentDC = cast<DeclContext>(ParentDecl);
712 }
713
Douglas Gregor815215d2009-05-27 05:35:12 +0000714 if (ParentDC != D->getDeclContext()) {
715 // We performed some kind of instantiation in the parent context,
716 // so now we need to look into the instantiated parent context to
717 // find the instantiation of the declaration D.
718 NamedDecl *Result = 0;
719 if (D->getDeclName()) {
720 DeclContext::lookup_result Found
721 = ParentDC->lookup(Context, D->getDeclName());
722 Result = findInstantiationOf(Context, D, Found.first, Found.second);
723 } else {
724 // Since we don't have a name for the entity we're looking for,
725 // our only option is to walk through all of the declarations to
726 // find that name. This will occur in a few cases:
727 //
728 // - anonymous struct/union within a template
729 // - unnamed class/struct/union/enum within a template
730 //
731 // FIXME: Find a better way to find these instantiations!
732 Result = findInstantiationOf(Context, D,
733 ParentDC->decls_begin(Context),
734 ParentDC->decls_end(Context));
735 }
736 assert(Result && "Unable to find instantiation of declaration!");
737 D = Result;
738 }
739
740 // D itself might be a class template that we need to instantiate
741 // with the given template arguments.
742 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
743 if (ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate()) {
744 QualType InjectedClassName
745 = ClassTemplate->getInjectedClassNameType(Context);
746 QualType InstantiatedType = InstantiateType(InjectedClassName,
747 TemplateArgs,
748 Record->getLocation(),
749 Record->getDeclName());
750 if (InstantiatedType.isNull())
751 return 0;
752 if (const RecordType *RT = InstantiatedType->getAsRecordType())
753 D = RT->getDecl();
754 }
755
756 return D;
757}