blob: 6d7dc2e6d5311343986de84f03d94ab980f23eb1 [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
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000101 return Typedef;
102}
103
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000104Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
105 // Instantiate the type of the declaration
106 QualType T = SemaRef.InstantiateType(D->getType(), TemplateArgs,
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000107 D->getTypeSpecStartLoc(),
108 D->getDeclName());
109 if (T.isNull())
110 return 0;
111
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000112 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000113 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
114 D->getLocation(), D->getIdentifier(),
115 T, D->getStorageClass(),
116 D->getTypeSpecStartLoc());
117 Var->setThreadSpecified(D->isThreadSpecified());
118 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
119 Var->setDeclaredInCondition(D->isDeclaredInCondition());
120
Mike Stump390b4cc2009-05-16 07:39:55 +0000121 // FIXME: In theory, we could have a previous declaration for variables that
122 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000123 bool Redeclaration = false;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000124 SemaRef.CheckVariableDeclaration(Var, 0, Redeclaration);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000125 Owner->addDecl(SemaRef.Context, Var);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000126
127 if (D->getInit()) {
128 OwningExprResult Init
Douglas Gregor7e063902009-05-11 23:53:27 +0000129 = SemaRef.InstantiateExpr(D->getInit(), TemplateArgs);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000130 if (Init.isInvalid())
131 Var->setInvalidDecl();
132 else
Chris Lattnerb28317a2009-03-28 19:18:32 +0000133 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init),
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000134 D->hasCXXDirectInitializer());
Douglas Gregord308e622009-05-18 20:51:54 +0000135 } else {
136 // FIXME: Call ActOnUninitializedDecl? (Not always)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000137 }
138
139 return Var;
140}
141
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000142Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
143 bool Invalid = false;
144 QualType T = D->getType();
145 if (T->isDependentType()) {
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000146 T = SemaRef.InstantiateType(T, TemplateArgs,
147 D->getLocation(), D->getDeclName());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000148 if (!T.isNull() && T->isFunctionType()) {
149 // C++ [temp.arg.type]p3:
150 // If a declaration acquires a function type through a type
151 // dependent on a template-parameter and this causes a
152 // declaration that does not use the syntactic form of a
153 // function declarator to have function type, the program is
154 // ill-formed.
155 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
156 << T;
157 T = QualType();
158 Invalid = true;
159 }
160 }
161
162 Expr *BitWidth = D->getBitWidth();
163 if (Invalid)
164 BitWidth = 0;
165 else if (BitWidth) {
166 OwningExprResult InstantiatedBitWidth
Douglas Gregor7e063902009-05-11 23:53:27 +0000167 = SemaRef.InstantiateExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000168 if (InstantiatedBitWidth.isInvalid()) {
169 Invalid = true;
170 BitWidth = 0;
171 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000172 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000173 }
174
175 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), T,
176 cast<RecordDecl>(Owner),
177 D->getLocation(),
178 D->isMutable(),
179 BitWidth,
180 D->getAccess(),
181 0);
182 if (Field) {
183 if (Invalid)
184 Field->setInvalidDecl();
185
Douglas Gregor6ab35242009-04-09 21:40:53 +0000186 Owner->addDecl(SemaRef.Context, Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000187 }
188
189 return Field;
190}
191
192Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
193 Expr *AssertExpr = D->getAssertExpr();
194
195 OwningExprResult InstantiatedAssertExpr
Douglas Gregor7e063902009-05-11 23:53:27 +0000196 = SemaRef.InstantiateExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000197 if (InstantiatedAssertExpr.isInvalid())
198 return 0;
199
200 OwningExprResult Message = SemaRef.Clone(D->getMessage());
201 Decl *StaticAssert
Chris Lattnerb28317a2009-03-28 19:18:32 +0000202 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
203 move(InstantiatedAssertExpr),
204 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000205 return StaticAssert;
206}
207
208Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
209 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
210 D->getLocation(), D->getIdentifier(),
211 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000212 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000213 Enum->setAccess(D->getAccess());
Douglas Gregor6ab35242009-04-09 21:40:53 +0000214 Owner->addDecl(SemaRef.Context, Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000215 Enum->startDefinition();
216
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000217 llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000218
219 EnumConstantDecl *LastEnumConst = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000220 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(SemaRef.Context),
221 ECEnd = D->enumerator_end(SemaRef.Context);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000222 EC != ECEnd; ++EC) {
223 // The specified value for the enumerator.
224 OwningExprResult Value = SemaRef.Owned((Expr *)0);
225 if (Expr *UninstValue = EC->getInitExpr())
Douglas Gregor7e063902009-05-11 23:53:27 +0000226 Value = SemaRef.InstantiateExpr(UninstValue, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000227
228 // Drop the initial value and continue.
229 bool isInvalid = false;
230 if (Value.isInvalid()) {
231 Value = SemaRef.Owned((Expr *)0);
232 isInvalid = true;
233 }
234
235 EnumConstantDecl *EnumConst
236 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
237 EC->getLocation(), EC->getIdentifier(),
238 move(Value));
239
240 if (isInvalid) {
241 if (EnumConst)
242 EnumConst->setInvalidDecl();
243 Enum->setInvalidDecl();
244 }
245
246 if (EnumConst) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000247 Enum->addDecl(SemaRef.Context, EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000248 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000249 LastEnumConst = EnumConst;
250 }
251 }
252
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000253 // FIXME: Fixup LBraceLoc and RBraceLoc
254 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
255 Sema::DeclPtrTy::make(Enum),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000256 &Enumerators[0], Enumerators.size());
257
258 return Enum;
259}
260
Douglas Gregor6477b692009-03-25 15:04:13 +0000261Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
262 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
263 return 0;
264}
265
Douglas Gregord475b8d2009-03-25 21:17:03 +0000266Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
267 CXXRecordDecl *PrevDecl = 0;
268 if (D->isInjectedClassName())
269 PrevDecl = cast<CXXRecordDecl>(Owner);
270
271 CXXRecordDecl *Record
272 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
273 D->getLocation(), D->getIdentifier(), PrevDecl);
274 Record->setImplicit(D->isImplicit());
275 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000276 if (!D->isInjectedClassName())
277 Record->setInstantiationOfMemberClass(D);
278
Douglas Gregor6ab35242009-04-09 21:40:53 +0000279 Owner->addDecl(SemaRef.Context, Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000280 return Record;
281}
282
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000283Decl *TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D) {
284 // Only handle actual methods; we'll deal with constructors,
285 // destructors, etc. separately.
286 if (D->getKind() != Decl::CXXMethod)
287 return 0;
288
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000289 Sema::LocalInstantiationScope Scope(SemaRef);
290
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000291 llvm::SmallVector<ParmVarDecl *, 4> Params;
Douglas Gregor5545e162009-03-24 00:38:23 +0000292 QualType T = InstantiateFunctionType(D, Params);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000293 if (T.isNull())
294 return 0;
295
296 // Build the instantiated method declaration.
297 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
298 CXXMethodDecl *Method
299 = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
300 D->getDeclName(), T, D->isStatic(),
301 D->isInline());
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000302 Method->setInstantiationOfMemberFunction(D);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000303
Douglas Gregor5545e162009-03-24 00:38:23 +0000304 // Attach the parameters
305 for (unsigned P = 0; P < Params.size(); ++P)
306 Params[P]->setOwningFunction(Method);
Jay Foadbeaaccd2009-05-21 09:52:38 +0000307 Method->setParams(SemaRef.Context, Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +0000308
309 if (InitMethodInstantiation(Method, D))
310 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000311
312 NamedDecl *PrevDecl
313 = SemaRef.LookupQualifiedName(Owner, Method->getDeclName(),
314 Sema::LookupOrdinaryName, true);
315 // In C++, the previous declaration we find might be a tag type
316 // (class or enum). In this case, the new declaration will hide the
317 // tag type. Note that this does does not apply if we're declaring a
318 // typedef (C++ [dcl.typedef]p4).
319 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
320 PrevDecl = 0;
321 bool Redeclaration = false;
322 bool OverloadableAttrRequired = false;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000323 SemaRef.CheckFunctionDeclaration(Method, PrevDecl, Redeclaration,
324 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000325
326 if (!Method->isInvalidDecl() || !PrevDecl)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000327 Owner->addDecl(SemaRef.Context, Method);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000328 return Method;
329}
330
Douglas Gregor615c5d42009-03-24 16:43:20 +0000331Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000332 Sema::LocalInstantiationScope Scope(SemaRef);
333
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000334 llvm::SmallVector<ParmVarDecl *, 4> Params;
Douglas Gregor615c5d42009-03-24 16:43:20 +0000335 QualType T = InstantiateFunctionType(D, Params);
336 if (T.isNull())
337 return 0;
338
339 // Build the instantiated method declaration.
340 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
341 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
342 DeclarationName Name
Douglas Gregor49f25ec2009-05-15 21:18:27 +0000343 = SemaRef.Context.DeclarationNames.getCXXConstructorName(
344 SemaRef.Context.getCanonicalType(ClassTy));
Douglas Gregor615c5d42009-03-24 16:43:20 +0000345 CXXConstructorDecl *Constructor
346 = CXXConstructorDecl::Create(SemaRef.Context, Record, D->getLocation(),
347 Name, T, D->isExplicit(), D->isInline(),
348 false);
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000349 Constructor->setInstantiationOfMemberFunction(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000350
351 // Attach the parameters
352 for (unsigned P = 0; P < Params.size(); ++P)
353 Params[P]->setOwningFunction(Constructor);
Jay Foadbeaaccd2009-05-21 09:52:38 +0000354 Constructor->setParams(SemaRef.Context, Params.data(), Params.size());
Douglas Gregor615c5d42009-03-24 16:43:20 +0000355
356 if (InitMethodInstantiation(Constructor, D))
357 Constructor->setInvalidDecl();
358
359 NamedDecl *PrevDecl
360 = SemaRef.LookupQualifiedName(Owner, Name, Sema::LookupOrdinaryName, true);
361
362 // In C++, the previous declaration we find might be a tag type
363 // (class or enum). In this case, the new declaration will hide the
364 // tag type. Note that this does does not apply if we're declaring a
365 // typedef (C++ [dcl.typedef]p4).
366 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
367 PrevDecl = 0;
368 bool Redeclaration = false;
369 bool OverloadableAttrRequired = false;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000370 SemaRef.CheckFunctionDeclaration(Constructor, PrevDecl, Redeclaration,
371 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000372
Douglas Gregor49f25ec2009-05-15 21:18:27 +0000373 Record->addedConstructor(SemaRef.Context, Constructor);
Chris Lattnereaaebc72009-04-25 08:06:05 +0000374 Owner->addDecl(SemaRef.Context, Constructor);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000375 return Constructor;
376}
377
Douglas Gregor03b2b072009-03-24 00:15:49 +0000378Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000379 Sema::LocalInstantiationScope Scope(SemaRef);
380
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000381 llvm::SmallVector<ParmVarDecl *, 4> Params;
Douglas Gregor5545e162009-03-24 00:38:23 +0000382 QualType T = InstantiateFunctionType(D, Params);
Douglas Gregor03b2b072009-03-24 00:15:49 +0000383 if (T.isNull())
384 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +0000385 assert(Params.size() == 0 && "Destructor with parameters?");
386
Douglas Gregor03b2b072009-03-24 00:15:49 +0000387 // Build the instantiated destructor declaration.
388 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
Douglas Gregor49f25ec2009-05-15 21:18:27 +0000389 QualType ClassTy =
390 SemaRef.Context.getCanonicalType(SemaRef.Context.getTypeDeclType(Record));
Douglas Gregor03b2b072009-03-24 00:15:49 +0000391 CXXDestructorDecl *Destructor
392 = CXXDestructorDecl::Create(SemaRef.Context, Record,
393 D->getLocation(),
394 SemaRef.Context.DeclarationNames.getCXXDestructorName(ClassTy),
395 T, D->isInline(), false);
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000396 Destructor->setInstantiationOfMemberFunction(D);
Douglas Gregor5545e162009-03-24 00:38:23 +0000397 if (InitMethodInstantiation(Destructor, D))
398 Destructor->setInvalidDecl();
Douglas Gregor03b2b072009-03-24 00:15:49 +0000399
400 bool Redeclaration = false;
401 bool OverloadableAttrRequired = false;
402 NamedDecl *PrevDecl = 0;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000403 SemaRef.CheckFunctionDeclaration(Destructor, PrevDecl, Redeclaration,
404 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000405 Owner->addDecl(SemaRef.Context, Destructor);
Douglas Gregor03b2b072009-03-24 00:15:49 +0000406 return Destructor;
407}
408
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000409Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000410 Sema::LocalInstantiationScope Scope(SemaRef);
411
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000412 llvm::SmallVector<ParmVarDecl *, 4> Params;
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000413 QualType T = InstantiateFunctionType(D, Params);
414 if (T.isNull())
415 return 0;
416 assert(Params.size() == 0 && "Destructor with parameters?");
417
418 // Build the instantiated conversion declaration.
419 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
420 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
421 QualType ConvTy
422 = SemaRef.Context.getCanonicalType(T->getAsFunctionType()->getResultType());
423 CXXConversionDecl *Conversion
424 = CXXConversionDecl::Create(SemaRef.Context, Record,
425 D->getLocation(),
426 SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(ConvTy),
427 T, D->isInline(), D->isExplicit());
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000428 Conversion->setInstantiationOfMemberFunction(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000429 if (InitMethodInstantiation(Conversion, D))
430 Conversion->setInvalidDecl();
431
432 bool Redeclaration = false;
433 bool OverloadableAttrRequired = false;
434 NamedDecl *PrevDecl = 0;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000435 SemaRef.CheckFunctionDeclaration(Conversion, PrevDecl, Redeclaration,
436 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000437 Owner->addDecl(SemaRef.Context, Conversion);
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000438 return Conversion;
439}
440
Douglas Gregor6477b692009-03-25 15:04:13 +0000441ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000442 QualType OrigT = SemaRef.InstantiateType(D->getOriginalType(), TemplateArgs,
Douglas Gregor7e063902009-05-11 23:53:27 +0000443 D->getLocation(), D->getDeclName());
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000444 if (OrigT.isNull())
445 return 0;
446
447 QualType T = SemaRef.adjustParameterType(OrigT);
448
449 if (D->getDefaultArg()) {
450 // FIXME: Leave a marker for "uninstantiated" default
451 // arguments. They only get instantiated on demand at the call
452 // site.
453 unsigned DiagID = SemaRef.Diags.getCustomDiagID(Diagnostic::Warning,
454 "sorry, dropping default argument during template instantiation");
455 SemaRef.Diag(D->getDefaultArg()->getSourceRange().getBegin(), DiagID)
456 << D->getDefaultArg()->getSourceRange();
457 }
458
459 // Allocate the parameter
460 ParmVarDecl *Param = 0;
461 if (T == OrigT)
462 Param = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
463 D->getIdentifier(), T, D->getStorageClass(),
464 0);
465 else
466 Param = OriginalParmVarDecl::Create(SemaRef.Context, Owner,
467 D->getLocation(), D->getIdentifier(),
468 T, OrigT, D->getStorageClass(), 0);
469
470 // Note: we don't try to instantiate function parameters until after
471 // we've instantiated the function's type. Therefore, we don't have
472 // to check for 'void' parameter types here.
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000473 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000474 return Param;
475}
476
477Decl *
478TemplateDeclInstantiator::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
479 // Since parameter types can decay either before or after
480 // instantiation, we simply treat OriginalParmVarDecls as
481 // ParmVarDecls the same way, and create one or the other depending
482 // on what happens after template instantiation.
483 return VisitParmVarDecl(D);
484}
485
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000486Decl *Sema::InstantiateDecl(Decl *D, DeclContext *Owner,
Douglas Gregor7e063902009-05-11 23:53:27 +0000487 const TemplateArgumentList &TemplateArgs) {
488 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000489 return Instantiator.Visit(D);
490}
491
Douglas Gregor5545e162009-03-24 00:38:23 +0000492/// \brief Instantiates the type of the given function, including
493/// instantiating all of the function parameters.
494///
495/// \param D The function that we will be instantiated
496///
497/// \param Params the instantiated parameter declarations
498
499/// \returns the instantiated function's type if successfull, a NULL
500/// type if there was an error.
501QualType
502TemplateDeclInstantiator::InstantiateFunctionType(FunctionDecl *D,
503 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
504 bool InvalidDecl = false;
505
506 // Instantiate the function parameters
Douglas Gregor7e063902009-05-11 23:53:27 +0000507 TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000508 llvm::SmallVector<QualType, 4> ParamTys;
Douglas Gregor5545e162009-03-24 00:38:23 +0000509 for (FunctionDecl::param_iterator P = D->param_begin(),
510 PEnd = D->param_end();
511 P != PEnd; ++P) {
Douglas Gregor6477b692009-03-25 15:04:13 +0000512 if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
Douglas Gregor5545e162009-03-24 00:38:23 +0000513 if (PInst->getType()->isVoidType()) {
514 SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
515 PInst->setInvalidDecl();
516 }
517 else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
518 PInst->getType(),
519 diag::err_abstract_type_in_decl,
Anders Carlsson8211eff2009-03-24 01:19:16 +0000520 Sema::AbstractParamType))
Douglas Gregor5545e162009-03-24 00:38:23 +0000521 PInst->setInvalidDecl();
522
523 Params.push_back(PInst);
524 ParamTys.push_back(PInst->getType());
525
526 if (PInst->isInvalidDecl())
527 InvalidDecl = true;
528 } else
529 InvalidDecl = true;
530 }
531
532 // FIXME: Deallocate dead declarations.
533 if (InvalidDecl)
534 return QualType();
535
536 const FunctionProtoType *Proto = D->getType()->getAsFunctionProtoType();
537 assert(Proto && "Missing prototype?");
538 QualType ResultType
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000539 = SemaRef.InstantiateType(Proto->getResultType(), TemplateArgs,
Douglas Gregor5545e162009-03-24 00:38:23 +0000540 D->getLocation(), D->getDeclName());
541 if (ResultType.isNull())
542 return QualType();
543
Jay Foadbeaaccd2009-05-21 09:52:38 +0000544 return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
Douglas Gregor5545e162009-03-24 00:38:23 +0000545 Proto->isVariadic(), Proto->getTypeQuals(),
546 D->getLocation(), D->getDeclName());
547}
548
549/// \brief Initializes common fields of an instantiated method
550/// declaration (New) from the corresponding fields of its template
551/// (Tmpl).
552///
553/// \returns true if there was an error
554bool
555TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
556 CXXMethodDecl *Tmpl) {
557 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
558 New->setAccess(Tmpl->getAccess());
Anders Carlsson77b7f1d2009-05-14 22:15:41 +0000559 if (Tmpl->isVirtualAsWritten()) {
560 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +0000561 Record->setAggregate(false);
562 Record->setPOD(false);
563 Record->setPolymorphic(true);
564 }
565 if (Tmpl->isDeleted())
566 New->setDeleted();
567 if (Tmpl->isPure()) {
568 New->setPure();
569 Record->setAbstract(true);
570 }
571
572 // FIXME: attributes
573 // FIXME: New needs a pointer to Tmpl
574 return false;
575}
Douglas Gregora58861f2009-05-13 20:28:22 +0000576
577/// \brief Instantiate the definition of the given function from its
578/// template.
579///
580/// \param Function the already-instantiated declaration of a
581/// function.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000582void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
583 FunctionDecl *Function) {
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000584 // FIXME: make this work for function template specializations, too.
585
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000586 if (Function->isInvalidDecl())
587 return;
588
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000589 // Find the function body that we'll be substituting.
590 const FunctionDecl *PatternDecl
591 = Function->getInstantiatedFromMemberFunction();
592 Stmt *Pattern = 0;
593 if (PatternDecl)
594 Pattern = PatternDecl->getBody(Context, PatternDecl);
595
596 if (!Pattern)
597 return;
598
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000599 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
600 if (Inst)
601 return;
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000602
Douglas Gregore2c31ff2009-05-15 17:59:04 +0000603 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
604
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000605 // Introduce a new scope where local variable instantiations will be
606 // recorded.
607 LocalInstantiationScope Scope(*this);
608
609 // Introduce the instantiated function parameters into the local
610 // instantiation scope.
611 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
612 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
613 Function->getParamDecl(I));
614
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000615 // Enter the scope of this instantiation. We don't use
616 // PushDeclContext because we don't have a scope.
617 DeclContext *PreviousContext = CurContext;
618 CurContext = Function;
619
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000620 // Instantiate the function body.
621 OwningStmtResult Body
622 = InstantiateStmt(Pattern, getTemplateInstantiationArgs(Function));
Douglas Gregore2c31ff2009-05-15 17:59:04 +0000623
624 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
625 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000626
627 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +0000628
629 DeclGroupRef DG(Function);
630 Consumer.HandleTopLevelDecl(DG);
Douglas Gregora58861f2009-05-13 20:28:22 +0000631}
632
633/// \brief Instantiate the definition of the given variable from its
634/// template.
635///
636/// \param Var the already-instantiated declaration of a variable.
637void Sema::InstantiateVariableDefinition(VarDecl *Var) {
638 // FIXME: Implement this!
639}
Douglas Gregor815215d2009-05-27 05:35:12 +0000640
641static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
642 if (D->getKind() != Other->getKind())
643 return false;
644
645 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
646 return Ctx.getCanonicalDecl(Record->getInstantiatedFromMemberClass())
647 == Ctx.getCanonicalDecl(D);
648
649 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
650 return Ctx.getCanonicalDecl(Function->getInstantiatedFromMemberFunction())
651 == Ctx.getCanonicalDecl(D);
652
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000653 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
654 return Ctx.getCanonicalDecl(Enum->getInstantiatedFromMemberEnum())
655 == Ctx.getCanonicalDecl(D);
Douglas Gregor815215d2009-05-27 05:35:12 +0000656
657 // FIXME: How can we find instantiations of anonymous unions?
658
659 return D->getDeclName() && isa<NamedDecl>(Other) &&
660 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
661}
662
663template<typename ForwardIterator>
664static NamedDecl *findInstantiationOf(ASTContext &Ctx,
665 NamedDecl *D,
666 ForwardIterator first,
667 ForwardIterator last) {
668 for (; first != last; ++first)
669 if (isInstantiationOf(Ctx, D, *first))
670 return cast<NamedDecl>(*first);
671
672 return 0;
673}
674
Douglas Gregored961e72009-05-27 17:54:46 +0000675/// \brief Find the instantiation of the given declaration within the
676/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +0000677///
678/// This routine is intended to be used when \p D is a declaration
679/// referenced from within a template, that needs to mapped into the
680/// corresponding declaration within an instantiation. For example,
681/// given:
682///
683/// \code
684/// template<typename T>
685/// struct X {
686/// enum Kind {
687/// KnownValue = sizeof(T)
688/// };
689///
690/// bool getKind() const { return KnownValue; }
691/// };
692///
693/// template struct X<int>;
694/// \endcode
695///
696/// In the instantiation of X<int>::getKind(), we need to map the
697/// EnumConstantDecl for KnownValue (which refers to
698/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +0000699/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
700/// this mapping from within the instantiation of X<int>.
701NamedDecl * Sema::InstantiateCurrentDeclRef(NamedDecl *D) {
Douglas Gregor815215d2009-05-27 05:35:12 +0000702 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor2bba76b2009-05-27 17:07:49 +0000703 if (isa<ParmVarDecl>(D) || ParentDC->isFunctionOrMethod()) {
704 // D is a local of some kind. Look into the map of local
705 // declarations to their instantiations.
706 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
707 }
Douglas Gregor815215d2009-05-27 05:35:12 +0000708
Douglas Gregor2bba76b2009-05-27 17:07:49 +0000709 if (NamedDecl *ParentDecl = dyn_cast<NamedDecl>(ParentDC)) {
Douglas Gregored961e72009-05-27 17:54:46 +0000710 ParentDecl = InstantiateCurrentDeclRef(ParentDecl);
Douglas Gregor815215d2009-05-27 05:35:12 +0000711 if (!ParentDecl)
712 return 0;
713
714 ParentDC = cast<DeclContext>(ParentDecl);
715 }
716
Douglas Gregor815215d2009-05-27 05:35:12 +0000717 if (ParentDC != D->getDeclContext()) {
718 // We performed some kind of instantiation in the parent context,
719 // so now we need to look into the instantiated parent context to
720 // find the instantiation of the declaration D.
721 NamedDecl *Result = 0;
722 if (D->getDeclName()) {
723 DeclContext::lookup_result Found
724 = ParentDC->lookup(Context, D->getDeclName());
725 Result = findInstantiationOf(Context, D, Found.first, Found.second);
726 } else {
727 // Since we don't have a name for the entity we're looking for,
728 // our only option is to walk through all of the declarations to
729 // find that name. This will occur in a few cases:
730 //
731 // - anonymous struct/union within a template
732 // - unnamed class/struct/union/enum within a template
733 //
734 // FIXME: Find a better way to find these instantiations!
735 Result = findInstantiationOf(Context, D,
736 ParentDC->decls_begin(Context),
737 ParentDC->decls_end(Context));
738 }
739 assert(Result && "Unable to find instantiation of declaration!");
740 D = Result;
741 }
742
Douglas Gregor815215d2009-05-27 05:35:12 +0000743 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
Douglas Gregored961e72009-05-27 17:54:46 +0000744 if (ClassTemplateDecl *ClassTemplate
745 = Record->getDescribedClassTemplate()) {
746 // When the declaration D was parsed, it referred to the current
747 // instantiation. Therefore, look through the current context,
748 // which contains actual instantiations, to find the
749 // instantiation of the "current instantiation" that D refers
750 // to. Alternatively, we could just instantiate the
751 // injected-class-name with the current template arguments, but
752 // such an instantiation is far more expensive.
753 for (DeclContext *DC = CurContext; !DC->isFileContext();
754 DC = DC->getParent()) {
755 if (ClassTemplateSpecializationDecl *Spec
756 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
757 if (Context.getCanonicalDecl(Spec->getSpecializedTemplate())
758 == Context.getCanonicalDecl(ClassTemplate))
759 return Spec;
760 }
761
762 assert(false &&
763 "Unable to find declaration for the current instantiation");
Douglas Gregor815215d2009-05-27 05:35:12 +0000764 }
765
766 return D;
767}