blob: 39e455a6ec7d91722660e6f0d31b7e3f244f069f [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 Gregorbc221632009-05-28 16:34:51 +0000100 if (Owner->isFunctionOrMethod())
101 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Typedef);
102
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000103 return Typedef;
104}
105
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000106Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
107 // Instantiate the type of the declaration
108 QualType T = SemaRef.InstantiateType(D->getType(), TemplateArgs,
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000109 D->getTypeSpecStartLoc(),
110 D->getDeclName());
111 if (T.isNull())
112 return 0;
113
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000114 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000115 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
116 D->getLocation(), D->getIdentifier(),
117 T, D->getStorageClass(),
118 D->getTypeSpecStartLoc());
119 Var->setThreadSpecified(D->isThreadSpecified());
120 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
121 Var->setDeclaredInCondition(D->isDeclaredInCondition());
122
Mike Stump390b4cc2009-05-16 07:39:55 +0000123 // FIXME: In theory, we could have a previous declaration for variables that
124 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000125 bool Redeclaration = false;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000126 SemaRef.CheckVariableDeclaration(Var, 0, Redeclaration);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000127 Owner->addDecl(SemaRef.Context, Var);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000128
129 if (D->getInit()) {
130 OwningExprResult Init
Douglas Gregor7e063902009-05-11 23:53:27 +0000131 = SemaRef.InstantiateExpr(D->getInit(), TemplateArgs);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000132 if (Init.isInvalid())
133 Var->setInvalidDecl();
134 else
Chris Lattnerb28317a2009-03-28 19:18:32 +0000135 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init),
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000136 D->hasCXXDirectInitializer());
Douglas Gregord308e622009-05-18 20:51:54 +0000137 } else {
138 // FIXME: Call ActOnUninitializedDecl? (Not always)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000139 }
140
141 return Var;
142}
143
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000144Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
145 bool Invalid = false;
146 QualType T = D->getType();
147 if (T->isDependentType()) {
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000148 T = SemaRef.InstantiateType(T, TemplateArgs,
149 D->getLocation(), D->getDeclName());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000150 if (!T.isNull() && T->isFunctionType()) {
151 // C++ [temp.arg.type]p3:
152 // If a declaration acquires a function type through a type
153 // dependent on a template-parameter and this causes a
154 // declaration that does not use the syntactic form of a
155 // function declarator to have function type, the program is
156 // ill-formed.
157 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
158 << T;
159 T = QualType();
160 Invalid = true;
161 }
162 }
163
164 Expr *BitWidth = D->getBitWidth();
165 if (Invalid)
166 BitWidth = 0;
167 else if (BitWidth) {
168 OwningExprResult InstantiatedBitWidth
Douglas Gregor7e063902009-05-11 23:53:27 +0000169 = SemaRef.InstantiateExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000170 if (InstantiatedBitWidth.isInvalid()) {
171 Invalid = true;
172 BitWidth = 0;
173 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000174 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000175 }
176
177 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), T,
178 cast<RecordDecl>(Owner),
179 D->getLocation(),
180 D->isMutable(),
181 BitWidth,
182 D->getAccess(),
183 0);
184 if (Field) {
185 if (Invalid)
186 Field->setInvalidDecl();
187
Douglas Gregor6ab35242009-04-09 21:40:53 +0000188 Owner->addDecl(SemaRef.Context, Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000189 }
190
191 return Field;
192}
193
194Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
195 Expr *AssertExpr = D->getAssertExpr();
196
197 OwningExprResult InstantiatedAssertExpr
Douglas Gregor7e063902009-05-11 23:53:27 +0000198 = SemaRef.InstantiateExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000199 if (InstantiatedAssertExpr.isInvalid())
200 return 0;
201
202 OwningExprResult Message = SemaRef.Clone(D->getMessage());
203 Decl *StaticAssert
Chris Lattnerb28317a2009-03-28 19:18:32 +0000204 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
205 move(InstantiatedAssertExpr),
206 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000207 return StaticAssert;
208}
209
210Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
211 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
212 D->getLocation(), D->getIdentifier(),
213 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000214 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000215 Enum->setAccess(D->getAccess());
Douglas Gregor6ab35242009-04-09 21:40:53 +0000216 Owner->addDecl(SemaRef.Context, Enum);
Douglas Gregorbc221632009-05-28 16:34:51 +0000217 if (Owner->isFunctionOrMethod())
218 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000219 Enum->startDefinition();
220
Chris Lattnerb28317a2009-03-28 19:18:32 +0000221 llvm::SmallVector<Sema::DeclPtrTy, 16> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000222
223 EnumConstantDecl *LastEnumConst = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000224 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(SemaRef.Context),
225 ECEnd = D->enumerator_end(SemaRef.Context);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000226 EC != ECEnd; ++EC) {
227 // The specified value for the enumerator.
228 OwningExprResult Value = SemaRef.Owned((Expr *)0);
229 if (Expr *UninstValue = EC->getInitExpr())
Douglas Gregor7e063902009-05-11 23:53:27 +0000230 Value = SemaRef.InstantiateExpr(UninstValue, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000231
232 // Drop the initial value and continue.
233 bool isInvalid = false;
234 if (Value.isInvalid()) {
235 Value = SemaRef.Owned((Expr *)0);
236 isInvalid = true;
237 }
238
239 EnumConstantDecl *EnumConst
240 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
241 EC->getLocation(), EC->getIdentifier(),
242 move(Value));
243
244 if (isInvalid) {
245 if (EnumConst)
246 EnumConst->setInvalidDecl();
247 Enum->setInvalidDecl();
248 }
249
250 if (EnumConst) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000251 Enum->addDecl(SemaRef.Context, EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000252 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000253 LastEnumConst = EnumConst;
254 }
255 }
256
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000257 // FIXME: Fixup LBraceLoc and RBraceLoc
258 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
259 Sema::DeclPtrTy::make(Enum),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000260 &Enumerators[0], Enumerators.size());
261
262 return Enum;
263}
264
Douglas Gregor6477b692009-03-25 15:04:13 +0000265Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
266 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
267 return 0;
268}
269
Douglas Gregord475b8d2009-03-25 21:17:03 +0000270Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
271 CXXRecordDecl *PrevDecl = 0;
272 if (D->isInjectedClassName())
273 PrevDecl = cast<CXXRecordDecl>(Owner);
274
275 CXXRecordDecl *Record
276 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
277 D->getLocation(), D->getIdentifier(), PrevDecl);
278 Record->setImplicit(D->isImplicit());
279 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000280 if (!D->isInjectedClassName())
281 Record->setInstantiationOfMemberClass(D);
282
Douglas Gregor6ab35242009-04-09 21:40:53 +0000283 Owner->addDecl(SemaRef.Context, Record);
Douglas Gregorbc221632009-05-28 16:34:51 +0000284 if (Owner->isFunctionOrMethod())
285 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000286 return Record;
287}
288
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000289Decl *TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D) {
290 // Only handle actual methods; we'll deal with constructors,
291 // destructors, etc. separately.
292 if (D->getKind() != Decl::CXXMethod)
293 return 0;
294
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000295 Sema::LocalInstantiationScope Scope(SemaRef);
296
Douglas Gregor5545e162009-03-24 00:38:23 +0000297 llvm::SmallVector<ParmVarDecl *, 16> Params;
298 QualType T = InstantiateFunctionType(D, Params);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000299 if (T.isNull())
300 return 0;
301
302 // Build the instantiated method declaration.
303 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
304 CXXMethodDecl *Method
305 = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
306 D->getDeclName(), T, D->isStatic(),
307 D->isInline());
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000308 Method->setInstantiationOfMemberFunction(D);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000309
Douglas Gregor5545e162009-03-24 00:38:23 +0000310 // Attach the parameters
311 for (unsigned P = 0; P < Params.size(); ++P)
312 Params[P]->setOwningFunction(Method);
Jay Foadbeaaccd2009-05-21 09:52:38 +0000313 Method->setParams(SemaRef.Context, Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +0000314
315 if (InitMethodInstantiation(Method, D))
316 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000317
318 NamedDecl *PrevDecl
319 = SemaRef.LookupQualifiedName(Owner, Method->getDeclName(),
320 Sema::LookupOrdinaryName, true);
321 // In C++, the previous declaration we find might be a tag type
322 // (class or enum). In this case, the new declaration will hide the
323 // tag type. Note that this does does not apply if we're declaring a
324 // typedef (C++ [dcl.typedef]p4).
325 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
326 PrevDecl = 0;
327 bool Redeclaration = false;
328 bool OverloadableAttrRequired = false;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000329 SemaRef.CheckFunctionDeclaration(Method, PrevDecl, Redeclaration,
330 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000331
332 if (!Method->isInvalidDecl() || !PrevDecl)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000333 Owner->addDecl(SemaRef.Context, Method);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000334 return Method;
335}
336
Douglas Gregor615c5d42009-03-24 16:43:20 +0000337Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000338 Sema::LocalInstantiationScope Scope(SemaRef);
339
Douglas Gregor615c5d42009-03-24 16:43:20 +0000340 llvm::SmallVector<ParmVarDecl *, 16> Params;
341 QualType T = InstantiateFunctionType(D, Params);
342 if (T.isNull())
343 return 0;
344
345 // Build the instantiated method declaration.
346 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
347 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
348 DeclarationName Name
Douglas Gregor49f25ec2009-05-15 21:18:27 +0000349 = SemaRef.Context.DeclarationNames.getCXXConstructorName(
350 SemaRef.Context.getCanonicalType(ClassTy));
Douglas Gregor615c5d42009-03-24 16:43:20 +0000351 CXXConstructorDecl *Constructor
352 = CXXConstructorDecl::Create(SemaRef.Context, Record, D->getLocation(),
353 Name, T, D->isExplicit(), D->isInline(),
354 false);
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000355 Constructor->setInstantiationOfMemberFunction(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000356
357 // Attach the parameters
358 for (unsigned P = 0; P < Params.size(); ++P)
359 Params[P]->setOwningFunction(Constructor);
Jay Foadbeaaccd2009-05-21 09:52:38 +0000360 Constructor->setParams(SemaRef.Context, Params.data(), Params.size());
Douglas Gregor615c5d42009-03-24 16:43:20 +0000361
362 if (InitMethodInstantiation(Constructor, D))
363 Constructor->setInvalidDecl();
364
365 NamedDecl *PrevDecl
366 = SemaRef.LookupQualifiedName(Owner, Name, Sema::LookupOrdinaryName, true);
367
368 // In C++, the previous declaration we find might be a tag type
369 // (class or enum). In this case, the new declaration will hide the
370 // tag type. Note that this does does not apply if we're declaring a
371 // typedef (C++ [dcl.typedef]p4).
372 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
373 PrevDecl = 0;
374 bool Redeclaration = false;
375 bool OverloadableAttrRequired = false;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000376 SemaRef.CheckFunctionDeclaration(Constructor, PrevDecl, Redeclaration,
377 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000378
Douglas Gregor49f25ec2009-05-15 21:18:27 +0000379 Record->addedConstructor(SemaRef.Context, Constructor);
Chris Lattnereaaebc72009-04-25 08:06:05 +0000380 Owner->addDecl(SemaRef.Context, Constructor);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000381 return Constructor;
382}
383
Douglas Gregor03b2b072009-03-24 00:15:49 +0000384Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000385 Sema::LocalInstantiationScope Scope(SemaRef);
386
Douglas Gregor5545e162009-03-24 00:38:23 +0000387 llvm::SmallVector<ParmVarDecl *, 16> Params;
388 QualType T = InstantiateFunctionType(D, Params);
Douglas Gregor03b2b072009-03-24 00:15:49 +0000389 if (T.isNull())
390 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +0000391 assert(Params.size() == 0 && "Destructor with parameters?");
392
Douglas Gregor03b2b072009-03-24 00:15:49 +0000393 // Build the instantiated destructor declaration.
394 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
Douglas Gregor49f25ec2009-05-15 21:18:27 +0000395 QualType ClassTy =
396 SemaRef.Context.getCanonicalType(SemaRef.Context.getTypeDeclType(Record));
Douglas Gregor03b2b072009-03-24 00:15:49 +0000397 CXXDestructorDecl *Destructor
398 = CXXDestructorDecl::Create(SemaRef.Context, Record,
399 D->getLocation(),
400 SemaRef.Context.DeclarationNames.getCXXDestructorName(ClassTy),
401 T, D->isInline(), false);
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000402 Destructor->setInstantiationOfMemberFunction(D);
Douglas Gregor5545e162009-03-24 00:38:23 +0000403 if (InitMethodInstantiation(Destructor, D))
404 Destructor->setInvalidDecl();
Douglas Gregor03b2b072009-03-24 00:15:49 +0000405
406 bool Redeclaration = false;
407 bool OverloadableAttrRequired = false;
408 NamedDecl *PrevDecl = 0;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000409 SemaRef.CheckFunctionDeclaration(Destructor, PrevDecl, Redeclaration,
410 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000411 Owner->addDecl(SemaRef.Context, Destructor);
Douglas Gregor03b2b072009-03-24 00:15:49 +0000412 return Destructor;
413}
414
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000415Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000416 Sema::LocalInstantiationScope Scope(SemaRef);
417
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000418 llvm::SmallVector<ParmVarDecl *, 16> Params;
419 QualType T = InstantiateFunctionType(D, Params);
420 if (T.isNull())
421 return 0;
422 assert(Params.size() == 0 && "Destructor with parameters?");
423
424 // Build the instantiated conversion declaration.
425 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
426 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
427 QualType ConvTy
428 = SemaRef.Context.getCanonicalType(T->getAsFunctionType()->getResultType());
429 CXXConversionDecl *Conversion
430 = CXXConversionDecl::Create(SemaRef.Context, Record,
431 D->getLocation(),
432 SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(ConvTy),
433 T, D->isInline(), D->isExplicit());
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000434 Conversion->setInstantiationOfMemberFunction(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000435 if (InitMethodInstantiation(Conversion, D))
436 Conversion->setInvalidDecl();
437
438 bool Redeclaration = false;
439 bool OverloadableAttrRequired = false;
440 NamedDecl *PrevDecl = 0;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000441 SemaRef.CheckFunctionDeclaration(Conversion, PrevDecl, Redeclaration,
442 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000443 Owner->addDecl(SemaRef.Context, Conversion);
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000444 return Conversion;
445}
446
Douglas Gregor6477b692009-03-25 15:04:13 +0000447ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000448 QualType OrigT = SemaRef.InstantiateType(D->getOriginalType(), TemplateArgs,
Douglas Gregor7e063902009-05-11 23:53:27 +0000449 D->getLocation(), D->getDeclName());
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000450 if (OrigT.isNull())
451 return 0;
452
453 QualType T = SemaRef.adjustParameterType(OrigT);
454
455 if (D->getDefaultArg()) {
456 // FIXME: Leave a marker for "uninstantiated" default
457 // arguments. They only get instantiated on demand at the call
458 // site.
459 unsigned DiagID = SemaRef.Diags.getCustomDiagID(Diagnostic::Warning,
460 "sorry, dropping default argument during template instantiation");
461 SemaRef.Diag(D->getDefaultArg()->getSourceRange().getBegin(), DiagID)
462 << D->getDefaultArg()->getSourceRange();
463 }
464
465 // Allocate the parameter
466 ParmVarDecl *Param = 0;
467 if (T == OrigT)
468 Param = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
469 D->getIdentifier(), T, D->getStorageClass(),
470 0);
471 else
472 Param = OriginalParmVarDecl::Create(SemaRef.Context, Owner,
473 D->getLocation(), D->getIdentifier(),
474 T, OrigT, D->getStorageClass(), 0);
475
476 // Note: we don't try to instantiate function parameters until after
477 // we've instantiated the function's type. Therefore, we don't have
478 // to check for 'void' parameter types here.
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000479 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000480 return Param;
481}
482
483Decl *
484TemplateDeclInstantiator::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
485 // Since parameter types can decay either before or after
486 // instantiation, we simply treat OriginalParmVarDecls as
487 // ParmVarDecls the same way, and create one or the other depending
488 // on what happens after template instantiation.
489 return VisitParmVarDecl(D);
490}
491
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000492Decl *Sema::InstantiateDecl(Decl *D, DeclContext *Owner,
Douglas Gregor7e063902009-05-11 23:53:27 +0000493 const TemplateArgumentList &TemplateArgs) {
494 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000495 return Instantiator.Visit(D);
496}
497
Douglas Gregor5545e162009-03-24 00:38:23 +0000498/// \brief Instantiates the type of the given function, including
499/// instantiating all of the function parameters.
500///
501/// \param D The function that we will be instantiated
502///
503/// \param Params the instantiated parameter declarations
504
505/// \returns the instantiated function's type if successfull, a NULL
506/// type if there was an error.
507QualType
508TemplateDeclInstantiator::InstantiateFunctionType(FunctionDecl *D,
509 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
510 bool InvalidDecl = false;
511
512 // Instantiate the function parameters
Douglas Gregor7e063902009-05-11 23:53:27 +0000513 TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
Douglas Gregor5545e162009-03-24 00:38:23 +0000514 llvm::SmallVector<QualType, 16> ParamTys;
515 for (FunctionDecl::param_iterator P = D->param_begin(),
516 PEnd = D->param_end();
517 P != PEnd; ++P) {
Douglas Gregor6477b692009-03-25 15:04:13 +0000518 if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
Douglas Gregor5545e162009-03-24 00:38:23 +0000519 if (PInst->getType()->isVoidType()) {
520 SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
521 PInst->setInvalidDecl();
522 }
523 else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
524 PInst->getType(),
525 diag::err_abstract_type_in_decl,
Anders Carlsson8211eff2009-03-24 01:19:16 +0000526 Sema::AbstractParamType))
Douglas Gregor5545e162009-03-24 00:38:23 +0000527 PInst->setInvalidDecl();
528
529 Params.push_back(PInst);
530 ParamTys.push_back(PInst->getType());
531
532 if (PInst->isInvalidDecl())
533 InvalidDecl = true;
534 } else
535 InvalidDecl = true;
536 }
537
538 // FIXME: Deallocate dead declarations.
539 if (InvalidDecl)
540 return QualType();
541
542 const FunctionProtoType *Proto = D->getType()->getAsFunctionProtoType();
543 assert(Proto && "Missing prototype?");
544 QualType ResultType
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000545 = SemaRef.InstantiateType(Proto->getResultType(), TemplateArgs,
Douglas Gregor5545e162009-03-24 00:38:23 +0000546 D->getLocation(), D->getDeclName());
547 if (ResultType.isNull())
548 return QualType();
549
Jay Foadbeaaccd2009-05-21 09:52:38 +0000550 return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
Douglas Gregor5545e162009-03-24 00:38:23 +0000551 Proto->isVariadic(), Proto->getTypeQuals(),
552 D->getLocation(), D->getDeclName());
553}
554
555/// \brief Initializes common fields of an instantiated method
556/// declaration (New) from the corresponding fields of its template
557/// (Tmpl).
558///
559/// \returns true if there was an error
560bool
561TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
562 CXXMethodDecl *Tmpl) {
563 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
564 New->setAccess(Tmpl->getAccess());
Anders Carlsson77b7f1d2009-05-14 22:15:41 +0000565 if (Tmpl->isVirtualAsWritten()) {
566 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +0000567 Record->setAggregate(false);
568 Record->setPOD(false);
569 Record->setPolymorphic(true);
570 }
571 if (Tmpl->isDeleted())
572 New->setDeleted();
573 if (Tmpl->isPure()) {
574 New->setPure();
575 Record->setAbstract(true);
576 }
577
578 // FIXME: attributes
579 // FIXME: New needs a pointer to Tmpl
580 return false;
581}
Douglas Gregora58861f2009-05-13 20:28:22 +0000582
583/// \brief Instantiate the definition of the given function from its
584/// template.
585///
586/// \param Function the already-instantiated declaration of a
587/// function.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000588void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
589 FunctionDecl *Function) {
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000590 // FIXME: make this work for function template specializations, too.
591
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000592 if (Function->isInvalidDecl())
593 return;
594
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000595 // Find the function body that we'll be substituting.
596 const FunctionDecl *PatternDecl
597 = Function->getInstantiatedFromMemberFunction();
598 Stmt *Pattern = 0;
599 if (PatternDecl)
600 Pattern = PatternDecl->getBody(Context, PatternDecl);
601
602 if (!Pattern)
603 return;
604
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000605 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
606 if (Inst)
607 return;
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000608
Douglas Gregore2c31ff2009-05-15 17:59:04 +0000609 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
610
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000611 // Introduce a new scope where local variable instantiations will be
612 // recorded.
613 LocalInstantiationScope Scope(*this);
614
615 // Introduce the instantiated function parameters into the local
616 // instantiation scope.
617 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
618 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
619 Function->getParamDecl(I));
620
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000621 // Enter the scope of this instantiation. We don't use
622 // PushDeclContext because we don't have a scope.
623 DeclContext *PreviousContext = CurContext;
624 CurContext = Function;
625
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000626 // Instantiate the function body.
627 OwningStmtResult Body
628 = InstantiateStmt(Pattern, getTemplateInstantiationArgs(Function));
Douglas Gregore2c31ff2009-05-15 17:59:04 +0000629
630 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
631 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000632
633 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +0000634
635 DeclGroupRef DG(Function);
636 Consumer.HandleTopLevelDecl(DG);
Douglas Gregora58861f2009-05-13 20:28:22 +0000637}
638
639/// \brief Instantiate the definition of the given variable from its
640/// template.
641///
642/// \param Var the already-instantiated declaration of a variable.
643void Sema::InstantiateVariableDefinition(VarDecl *Var) {
644 // FIXME: Implement this!
645}
Douglas Gregor815215d2009-05-27 05:35:12 +0000646
647static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
648 if (D->getKind() != Other->getKind())
649 return false;
650
651 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
652 return Ctx.getCanonicalDecl(Record->getInstantiatedFromMemberClass())
653 == Ctx.getCanonicalDecl(D);
654
655 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
656 return Ctx.getCanonicalDecl(Function->getInstantiatedFromMemberFunction())
657 == Ctx.getCanonicalDecl(D);
658
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000659 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
660 return Ctx.getCanonicalDecl(Enum->getInstantiatedFromMemberEnum())
661 == Ctx.getCanonicalDecl(D);
Douglas Gregor815215d2009-05-27 05:35:12 +0000662
663 // FIXME: How can we find instantiations of anonymous unions?
664
665 return D->getDeclName() && isa<NamedDecl>(Other) &&
666 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
667}
668
669template<typename ForwardIterator>
670static NamedDecl *findInstantiationOf(ASTContext &Ctx,
671 NamedDecl *D,
672 ForwardIterator first,
673 ForwardIterator last) {
674 for (; first != last; ++first)
675 if (isInstantiationOf(Ctx, D, *first))
676 return cast<NamedDecl>(*first);
677
678 return 0;
679}
680
Douglas Gregored961e72009-05-27 17:54:46 +0000681/// \brief Find the instantiation of the given declaration within the
682/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +0000683///
684/// This routine is intended to be used when \p D is a declaration
685/// referenced from within a template, that needs to mapped into the
686/// corresponding declaration within an instantiation. For example,
687/// given:
688///
689/// \code
690/// template<typename T>
691/// struct X {
692/// enum Kind {
693/// KnownValue = sizeof(T)
694/// };
695///
696/// bool getKind() const { return KnownValue; }
697/// };
698///
699/// template struct X<int>;
700/// \endcode
701///
702/// In the instantiation of X<int>::getKind(), we need to map the
703/// EnumConstantDecl for KnownValue (which refers to
704/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +0000705/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
706/// this mapping from within the instantiation of X<int>.
707NamedDecl * Sema::InstantiateCurrentDeclRef(NamedDecl *D) {
Douglas Gregor815215d2009-05-27 05:35:12 +0000708 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor2bba76b2009-05-27 17:07:49 +0000709 if (isa<ParmVarDecl>(D) || ParentDC->isFunctionOrMethod()) {
710 // D is a local of some kind. Look into the map of local
711 // declarations to their instantiations.
712 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
713 }
Douglas Gregor815215d2009-05-27 05:35:12 +0000714
Douglas Gregor2bba76b2009-05-27 17:07:49 +0000715 if (NamedDecl *ParentDecl = dyn_cast<NamedDecl>(ParentDC)) {
Douglas Gregored961e72009-05-27 17:54:46 +0000716 ParentDecl = InstantiateCurrentDeclRef(ParentDecl);
Douglas Gregor815215d2009-05-27 05:35:12 +0000717 if (!ParentDecl)
718 return 0;
719
720 ParentDC = cast<DeclContext>(ParentDecl);
721 }
722
Douglas Gregor815215d2009-05-27 05:35:12 +0000723 if (ParentDC != D->getDeclContext()) {
724 // We performed some kind of instantiation in the parent context,
725 // so now we need to look into the instantiated parent context to
726 // find the instantiation of the declaration D.
727 NamedDecl *Result = 0;
728 if (D->getDeclName()) {
729 DeclContext::lookup_result Found
730 = ParentDC->lookup(Context, D->getDeclName());
731 Result = findInstantiationOf(Context, D, Found.first, Found.second);
732 } else {
733 // Since we don't have a name for the entity we're looking for,
734 // our only option is to walk through all of the declarations to
735 // find that name. This will occur in a few cases:
736 //
737 // - anonymous struct/union within a template
738 // - unnamed class/struct/union/enum within a template
739 //
740 // FIXME: Find a better way to find these instantiations!
741 Result = findInstantiationOf(Context, D,
742 ParentDC->decls_begin(Context),
743 ParentDC->decls_end(Context));
744 }
745 assert(Result && "Unable to find instantiation of declaration!");
746 D = Result;
747 }
748
Douglas Gregor815215d2009-05-27 05:35:12 +0000749 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
Douglas Gregored961e72009-05-27 17:54:46 +0000750 if (ClassTemplateDecl *ClassTemplate
751 = Record->getDescribedClassTemplate()) {
752 // When the declaration D was parsed, it referred to the current
753 // instantiation. Therefore, look through the current context,
754 // which contains actual instantiations, to find the
755 // instantiation of the "current instantiation" that D refers
756 // to. Alternatively, we could just instantiate the
757 // injected-class-name with the current template arguments, but
758 // such an instantiation is far more expensive.
759 for (DeclContext *DC = CurContext; !DC->isFileContext();
760 DC = DC->getParent()) {
761 if (ClassTemplateSpecializationDecl *Spec
762 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
763 if (Context.getCanonicalDecl(Spec->getSpecializedTemplate())
764 == Context.getCanonicalDecl(ClassTemplate))
765 return Spec;
766 }
767
768 assert(false &&
769 "Unable to find declaration for the current instantiation");
Douglas Gregor815215d2009-05-27 05:35:12 +0000770 }
771
772 return D;
773}