blob: 385112b7d77f940599c68c4d8d3dfd25d0cb1d23 [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"
13#include "clang/AST/ASTContext.h"
14#include "clang/AST/DeclTemplate.h"
15#include "clang/AST/DeclVisitor.h"
16#include "clang/AST/Expr.h"
17#include "llvm/Support/Compiler.h"
18
19using namespace clang;
20
21namespace {
22 class VISIBILITY_HIDDEN TemplateDeclInstantiator
Chris Lattnerb28317a2009-03-28 19:18:32 +000023 : public DeclVisitor<TemplateDeclInstantiator, Decl *> {
Douglas Gregor8dbc2692009-03-17 21:15:40 +000024 Sema &SemaRef;
25 DeclContext *Owner;
Douglas Gregor7e063902009-05-11 23:53:27 +000026 const TemplateArgumentList &TemplateArgs;
Douglas Gregor8dbc2692009-03-17 21:15:40 +000027
28 public:
29 typedef Sema::OwningExprResult OwningExprResult;
30
31 TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner,
Douglas Gregor7e063902009-05-11 23:53:27 +000032 const TemplateArgumentList &TemplateArgs)
33 : SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs) { }
Douglas Gregor8dbc2692009-03-17 21:15:40 +000034
Mike Stump390b4cc2009-05-16 07:39:55 +000035 // FIXME: Once we get closer to completion, replace these manually-written
36 // declarations with automatically-generated ones from
37 // clang/AST/DeclNodes.def.
Douglas Gregor4f722be2009-03-25 15:45:12 +000038 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
39 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000040 Decl *VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +000041 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000042 Decl *VisitFieldDecl(FieldDecl *D);
43 Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
44 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor6477b692009-03-25 15:04:13 +000045 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregord475b8d2009-03-25 21:17:03 +000046 Decl *VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregor2dc0e642009-03-23 23:06:20 +000047 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
Douglas Gregor615c5d42009-03-24 16:43:20 +000048 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
Douglas Gregor03b2b072009-03-24 00:15:49 +000049 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +000050 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor6477b692009-03-25 15:04:13 +000051 ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor2dc0e642009-03-23 23:06:20 +000052 Decl *VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
Douglas Gregor5545e162009-03-24 00:38:23 +000053
Douglas Gregor8dbc2692009-03-17 21:15:40 +000054 // Base case. FIXME: Remove once we can instantiate everything.
55 Decl *VisitDecl(Decl *) {
Douglas Gregor3d7a12a2009-03-25 23:32:15 +000056 assert(false && "Template instantiation of unknown declaration kind!");
Douglas Gregor8dbc2692009-03-17 21:15:40 +000057 return 0;
58 }
Douglas Gregor5545e162009-03-24 00:38:23 +000059
60 // Helper functions for instantiating methods.
61 QualType InstantiateFunctionType(FunctionDecl *D,
62 llvm::SmallVectorImpl<ParmVarDecl *> &Params);
63 bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000064 };
65}
66
Douglas Gregor4f722be2009-03-25 15:45:12 +000067Decl *
68TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
69 assert(false && "Translation units cannot be instantiated");
70 return D;
71}
72
73Decl *
74TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
75 assert(false && "Namespaces cannot be instantiated");
76 return D;
77}
78
Douglas Gregor8dbc2692009-03-17 21:15:40 +000079Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
80 bool Invalid = false;
81 QualType T = D->getUnderlyingType();
82 if (T->isDependentType()) {
Douglas Gregor1eee0e72009-05-14 21:06:31 +000083 T = SemaRef.InstantiateType(T, TemplateArgs,
84 D->getLocation(), D->getDeclName());
Douglas Gregor8dbc2692009-03-17 21:15:40 +000085 if (T.isNull()) {
86 Invalid = true;
87 T = SemaRef.Context.IntTy;
88 }
89 }
90
91 // Create the new typedef
92 TypedefDecl *Typedef
93 = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
94 D->getIdentifier(), T);
95 if (Invalid)
96 Typedef->setInvalidDecl();
97
Douglas Gregor6ab35242009-04-09 21:40:53 +000098 Owner->addDecl(SemaRef.Context, Typedef);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000099 return Typedef;
100}
101
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000102Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
103 // Instantiate the type of the declaration
104 QualType T = SemaRef.InstantiateType(D->getType(), TemplateArgs,
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000105 D->getTypeSpecStartLoc(),
106 D->getDeclName());
107 if (T.isNull())
108 return 0;
109
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000110 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000111 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
112 D->getLocation(), D->getIdentifier(),
113 T, D->getStorageClass(),
114 D->getTypeSpecStartLoc());
115 Var->setThreadSpecified(D->isThreadSpecified());
116 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
117 Var->setDeclaredInCondition(D->isDeclaredInCondition());
118
Mike Stump390b4cc2009-05-16 07:39:55 +0000119 // FIXME: In theory, we could have a previous declaration for variables that
120 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000121 bool Redeclaration = false;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000122 SemaRef.CheckVariableDeclaration(Var, 0, Redeclaration);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000123 Owner->addDecl(SemaRef.Context, Var);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000124
125 if (D->getInit()) {
126 OwningExprResult Init
Douglas Gregor7e063902009-05-11 23:53:27 +0000127 = SemaRef.InstantiateExpr(D->getInit(), TemplateArgs);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000128 if (Init.isInvalid())
129 Var->setInvalidDecl();
130 else
Chris Lattnerb28317a2009-03-28 19:18:32 +0000131 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init),
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000132 D->hasCXXDirectInitializer());
Douglas Gregord308e622009-05-18 20:51:54 +0000133 } else {
134 // FIXME: Call ActOnUninitializedDecl? (Not always)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000135 }
136
137 return Var;
138}
139
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000140Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
141 bool Invalid = false;
142 QualType T = D->getType();
143 if (T->isDependentType()) {
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000144 T = SemaRef.InstantiateType(T, TemplateArgs,
145 D->getLocation(), D->getDeclName());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000146 if (!T.isNull() && T->isFunctionType()) {
147 // C++ [temp.arg.type]p3:
148 // If a declaration acquires a function type through a type
149 // dependent on a template-parameter and this causes a
150 // declaration that does not use the syntactic form of a
151 // function declarator to have function type, the program is
152 // ill-formed.
153 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
154 << T;
155 T = QualType();
156 Invalid = true;
157 }
158 }
159
160 Expr *BitWidth = D->getBitWidth();
161 if (Invalid)
162 BitWidth = 0;
163 else if (BitWidth) {
164 OwningExprResult InstantiatedBitWidth
Douglas Gregor7e063902009-05-11 23:53:27 +0000165 = SemaRef.InstantiateExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000166 if (InstantiatedBitWidth.isInvalid()) {
167 Invalid = true;
168 BitWidth = 0;
169 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000170 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000171 }
172
173 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), T,
174 cast<RecordDecl>(Owner),
175 D->getLocation(),
176 D->isMutable(),
177 BitWidth,
178 D->getAccess(),
179 0);
180 if (Field) {
181 if (Invalid)
182 Field->setInvalidDecl();
183
Douglas Gregor6ab35242009-04-09 21:40:53 +0000184 Owner->addDecl(SemaRef.Context, Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000185 }
186
187 return Field;
188}
189
190Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
191 Expr *AssertExpr = D->getAssertExpr();
192
193 OwningExprResult InstantiatedAssertExpr
Douglas Gregor7e063902009-05-11 23:53:27 +0000194 = SemaRef.InstantiateExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000195 if (InstantiatedAssertExpr.isInvalid())
196 return 0;
197
198 OwningExprResult Message = SemaRef.Clone(D->getMessage());
199 Decl *StaticAssert
Chris Lattnerb28317a2009-03-28 19:18:32 +0000200 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
201 move(InstantiatedAssertExpr),
202 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000203 return StaticAssert;
204}
205
206Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
207 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
208 D->getLocation(), D->getIdentifier(),
209 /*PrevDecl=*/0);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000210 Enum->setAccess(D->getAccess());
Douglas Gregor6ab35242009-04-09 21:40:53 +0000211 Owner->addDecl(SemaRef.Context, Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000212 Enum->startDefinition();
213
Chris Lattnerb28317a2009-03-28 19:18:32 +0000214 llvm::SmallVector<Sema::DeclPtrTy, 16> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000215
216 EnumConstantDecl *LastEnumConst = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000217 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(SemaRef.Context),
218 ECEnd = D->enumerator_end(SemaRef.Context);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000219 EC != ECEnd; ++EC) {
220 // The specified value for the enumerator.
221 OwningExprResult Value = SemaRef.Owned((Expr *)0);
222 if (Expr *UninstValue = EC->getInitExpr())
Douglas Gregor7e063902009-05-11 23:53:27 +0000223 Value = SemaRef.InstantiateExpr(UninstValue, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000224
225 // Drop the initial value and continue.
226 bool isInvalid = false;
227 if (Value.isInvalid()) {
228 Value = SemaRef.Owned((Expr *)0);
229 isInvalid = true;
230 }
231
232 EnumConstantDecl *EnumConst
233 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
234 EC->getLocation(), EC->getIdentifier(),
235 move(Value));
236
237 if (isInvalid) {
238 if (EnumConst)
239 EnumConst->setInvalidDecl();
240 Enum->setInvalidDecl();
241 }
242
243 if (EnumConst) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000244 Enum->addDecl(SemaRef.Context, EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000245 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000246 LastEnumConst = EnumConst;
247 }
248 }
249
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000250 // FIXME: Fixup LBraceLoc and RBraceLoc
251 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
252 Sema::DeclPtrTy::make(Enum),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000253 &Enumerators[0], Enumerators.size());
254
255 return Enum;
256}
257
Douglas Gregor6477b692009-03-25 15:04:13 +0000258Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
259 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
260 return 0;
261}
262
Douglas Gregord475b8d2009-03-25 21:17:03 +0000263Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
264 CXXRecordDecl *PrevDecl = 0;
265 if (D->isInjectedClassName())
266 PrevDecl = cast<CXXRecordDecl>(Owner);
267
268 CXXRecordDecl *Record
269 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
270 D->getLocation(), D->getIdentifier(), PrevDecl);
271 Record->setImplicit(D->isImplicit());
272 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000273 if (!D->isInjectedClassName())
274 Record->setInstantiationOfMemberClass(D);
275
Douglas Gregor6ab35242009-04-09 21:40:53 +0000276 Owner->addDecl(SemaRef.Context, Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000277 return Record;
278}
279
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000280Decl *TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D) {
281 // Only handle actual methods; we'll deal with constructors,
282 // destructors, etc. separately.
283 if (D->getKind() != Decl::CXXMethod)
284 return 0;
285
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000286 Sema::LocalInstantiationScope Scope(SemaRef);
287
Douglas Gregor5545e162009-03-24 00:38:23 +0000288 llvm::SmallVector<ParmVarDecl *, 16> Params;
289 QualType T = InstantiateFunctionType(D, Params);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000290 if (T.isNull())
291 return 0;
292
293 // Build the instantiated method declaration.
294 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
295 CXXMethodDecl *Method
296 = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
297 D->getDeclName(), T, D->isStatic(),
298 D->isInline());
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000299 Method->setInstantiationOfMemberFunction(D);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000300
Douglas Gregor5545e162009-03-24 00:38:23 +0000301 // Attach the parameters
302 for (unsigned P = 0; P < Params.size(); ++P)
303 Params[P]->setOwningFunction(Method);
304 Method->setParams(SemaRef.Context, &Params[0], Params.size());
305
306 if (InitMethodInstantiation(Method, D))
307 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000308
309 NamedDecl *PrevDecl
310 = SemaRef.LookupQualifiedName(Owner, Method->getDeclName(),
311 Sema::LookupOrdinaryName, true);
312 // In C++, the previous declaration we find might be a tag type
313 // (class or enum). In this case, the new declaration will hide the
314 // tag type. Note that this does does not apply if we're declaring a
315 // typedef (C++ [dcl.typedef]p4).
316 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
317 PrevDecl = 0;
318 bool Redeclaration = false;
319 bool OverloadableAttrRequired = false;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000320 SemaRef.CheckFunctionDeclaration(Method, PrevDecl, Redeclaration,
321 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000322
323 if (!Method->isInvalidDecl() || !PrevDecl)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000324 Owner->addDecl(SemaRef.Context, Method);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000325 return Method;
326}
327
Douglas Gregor615c5d42009-03-24 16:43:20 +0000328Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000329 Sema::LocalInstantiationScope Scope(SemaRef);
330
Douglas Gregor615c5d42009-03-24 16:43:20 +0000331 llvm::SmallVector<ParmVarDecl *, 16> Params;
332 QualType T = InstantiateFunctionType(D, Params);
333 if (T.isNull())
334 return 0;
335
336 // Build the instantiated method declaration.
337 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
338 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
339 DeclarationName Name
Douglas Gregor49f25ec2009-05-15 21:18:27 +0000340 = SemaRef.Context.DeclarationNames.getCXXConstructorName(
341 SemaRef.Context.getCanonicalType(ClassTy));
Douglas Gregor615c5d42009-03-24 16:43:20 +0000342 CXXConstructorDecl *Constructor
343 = CXXConstructorDecl::Create(SemaRef.Context, Record, D->getLocation(),
344 Name, T, D->isExplicit(), D->isInline(),
345 false);
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000346 Constructor->setInstantiationOfMemberFunction(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000347
348 // Attach the parameters
349 for (unsigned P = 0; P < Params.size(); ++P)
350 Params[P]->setOwningFunction(Constructor);
351 Constructor->setParams(SemaRef.Context, &Params[0], Params.size());
352
353 if (InitMethodInstantiation(Constructor, D))
354 Constructor->setInvalidDecl();
355
356 NamedDecl *PrevDecl
357 = SemaRef.LookupQualifiedName(Owner, Name, Sema::LookupOrdinaryName, true);
358
359 // In C++, the previous declaration we find might be a tag type
360 // (class or enum). In this case, the new declaration will hide the
361 // tag type. Note that this does does not apply if we're declaring a
362 // typedef (C++ [dcl.typedef]p4).
363 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
364 PrevDecl = 0;
365 bool Redeclaration = false;
366 bool OverloadableAttrRequired = false;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000367 SemaRef.CheckFunctionDeclaration(Constructor, PrevDecl, Redeclaration,
368 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000369
Douglas Gregor49f25ec2009-05-15 21:18:27 +0000370 Record->addedConstructor(SemaRef.Context, Constructor);
Chris Lattnereaaebc72009-04-25 08:06:05 +0000371 Owner->addDecl(SemaRef.Context, Constructor);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000372 return Constructor;
373}
374
Douglas Gregor03b2b072009-03-24 00:15:49 +0000375Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000376 Sema::LocalInstantiationScope Scope(SemaRef);
377
Douglas Gregor5545e162009-03-24 00:38:23 +0000378 llvm::SmallVector<ParmVarDecl *, 16> Params;
379 QualType T = InstantiateFunctionType(D, Params);
Douglas Gregor03b2b072009-03-24 00:15:49 +0000380 if (T.isNull())
381 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +0000382 assert(Params.size() == 0 && "Destructor with parameters?");
383
Douglas Gregor03b2b072009-03-24 00:15:49 +0000384 // Build the instantiated destructor declaration.
385 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
Douglas Gregor49f25ec2009-05-15 21:18:27 +0000386 QualType ClassTy =
387 SemaRef.Context.getCanonicalType(SemaRef.Context.getTypeDeclType(Record));
Douglas Gregor03b2b072009-03-24 00:15:49 +0000388 CXXDestructorDecl *Destructor
389 = CXXDestructorDecl::Create(SemaRef.Context, Record,
390 D->getLocation(),
391 SemaRef.Context.DeclarationNames.getCXXDestructorName(ClassTy),
392 T, D->isInline(), false);
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000393 Destructor->setInstantiationOfMemberFunction(D);
Douglas Gregor5545e162009-03-24 00:38:23 +0000394 if (InitMethodInstantiation(Destructor, D))
395 Destructor->setInvalidDecl();
Douglas Gregor03b2b072009-03-24 00:15:49 +0000396
397 bool Redeclaration = false;
398 bool OverloadableAttrRequired = false;
399 NamedDecl *PrevDecl = 0;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000400 SemaRef.CheckFunctionDeclaration(Destructor, PrevDecl, Redeclaration,
401 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000402 Owner->addDecl(SemaRef.Context, Destructor);
Douglas Gregor03b2b072009-03-24 00:15:49 +0000403 return Destructor;
404}
405
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000406Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000407 Sema::LocalInstantiationScope Scope(SemaRef);
408
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000409 llvm::SmallVector<ParmVarDecl *, 16> Params;
410 QualType T = InstantiateFunctionType(D, Params);
411 if (T.isNull())
412 return 0;
413 assert(Params.size() == 0 && "Destructor with parameters?");
414
415 // Build the instantiated conversion declaration.
416 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
417 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
418 QualType ConvTy
419 = SemaRef.Context.getCanonicalType(T->getAsFunctionType()->getResultType());
420 CXXConversionDecl *Conversion
421 = CXXConversionDecl::Create(SemaRef.Context, Record,
422 D->getLocation(),
423 SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(ConvTy),
424 T, D->isInline(), D->isExplicit());
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000425 Conversion->setInstantiationOfMemberFunction(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000426 if (InitMethodInstantiation(Conversion, D))
427 Conversion->setInvalidDecl();
428
429 bool Redeclaration = false;
430 bool OverloadableAttrRequired = false;
431 NamedDecl *PrevDecl = 0;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000432 SemaRef.CheckFunctionDeclaration(Conversion, PrevDecl, Redeclaration,
433 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000434 Owner->addDecl(SemaRef.Context, Conversion);
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000435 return Conversion;
436}
437
Douglas Gregor6477b692009-03-25 15:04:13 +0000438ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000439 QualType OrigT = SemaRef.InstantiateType(D->getOriginalType(), TemplateArgs,
Douglas Gregor7e063902009-05-11 23:53:27 +0000440 D->getLocation(), D->getDeclName());
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000441 if (OrigT.isNull())
442 return 0;
443
444 QualType T = SemaRef.adjustParameterType(OrigT);
445
446 if (D->getDefaultArg()) {
447 // FIXME: Leave a marker for "uninstantiated" default
448 // arguments. They only get instantiated on demand at the call
449 // site.
450 unsigned DiagID = SemaRef.Diags.getCustomDiagID(Diagnostic::Warning,
451 "sorry, dropping default argument during template instantiation");
452 SemaRef.Diag(D->getDefaultArg()->getSourceRange().getBegin(), DiagID)
453 << D->getDefaultArg()->getSourceRange();
454 }
455
456 // Allocate the parameter
457 ParmVarDecl *Param = 0;
458 if (T == OrigT)
459 Param = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
460 D->getIdentifier(), T, D->getStorageClass(),
461 0);
462 else
463 Param = OriginalParmVarDecl::Create(SemaRef.Context, Owner,
464 D->getLocation(), D->getIdentifier(),
465 T, OrigT, D->getStorageClass(), 0);
466
467 // Note: we don't try to instantiate function parameters until after
468 // we've instantiated the function's type. Therefore, we don't have
469 // to check for 'void' parameter types here.
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000470 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000471 return Param;
472}
473
474Decl *
475TemplateDeclInstantiator::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
476 // Since parameter types can decay either before or after
477 // instantiation, we simply treat OriginalParmVarDecls as
478 // ParmVarDecls the same way, and create one or the other depending
479 // on what happens after template instantiation.
480 return VisitParmVarDecl(D);
481}
482
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000483Decl *Sema::InstantiateDecl(Decl *D, DeclContext *Owner,
Douglas Gregor7e063902009-05-11 23:53:27 +0000484 const TemplateArgumentList &TemplateArgs) {
485 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000486 return Instantiator.Visit(D);
487}
488
Douglas Gregor5545e162009-03-24 00:38:23 +0000489/// \brief Instantiates the type of the given function, including
490/// instantiating all of the function parameters.
491///
492/// \param D The function that we will be instantiated
493///
494/// \param Params the instantiated parameter declarations
495
496/// \returns the instantiated function's type if successfull, a NULL
497/// type if there was an error.
498QualType
499TemplateDeclInstantiator::InstantiateFunctionType(FunctionDecl *D,
500 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
501 bool InvalidDecl = false;
502
503 // Instantiate the function parameters
Douglas Gregor7e063902009-05-11 23:53:27 +0000504 TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
Douglas Gregor5545e162009-03-24 00:38:23 +0000505 llvm::SmallVector<QualType, 16> ParamTys;
506 for (FunctionDecl::param_iterator P = D->param_begin(),
507 PEnd = D->param_end();
508 P != PEnd; ++P) {
Douglas Gregor6477b692009-03-25 15:04:13 +0000509 if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
Douglas Gregor5545e162009-03-24 00:38:23 +0000510 if (PInst->getType()->isVoidType()) {
511 SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
512 PInst->setInvalidDecl();
513 }
514 else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
515 PInst->getType(),
516 diag::err_abstract_type_in_decl,
Anders Carlsson8211eff2009-03-24 01:19:16 +0000517 Sema::AbstractParamType))
Douglas Gregor5545e162009-03-24 00:38:23 +0000518 PInst->setInvalidDecl();
519
520 Params.push_back(PInst);
521 ParamTys.push_back(PInst->getType());
522
523 if (PInst->isInvalidDecl())
524 InvalidDecl = true;
525 } else
526 InvalidDecl = true;
527 }
528
529 // FIXME: Deallocate dead declarations.
530 if (InvalidDecl)
531 return QualType();
532
533 const FunctionProtoType *Proto = D->getType()->getAsFunctionProtoType();
534 assert(Proto && "Missing prototype?");
535 QualType ResultType
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000536 = SemaRef.InstantiateType(Proto->getResultType(), TemplateArgs,
Douglas Gregor5545e162009-03-24 00:38:23 +0000537 D->getLocation(), D->getDeclName());
538 if (ResultType.isNull())
539 return QualType();
540
541 return SemaRef.BuildFunctionType(ResultType, &ParamTys[0], ParamTys.size(),
542 Proto->isVariadic(), Proto->getTypeQuals(),
543 D->getLocation(), D->getDeclName());
544}
545
546/// \brief Initializes common fields of an instantiated method
547/// declaration (New) from the corresponding fields of its template
548/// (Tmpl).
549///
550/// \returns true if there was an error
551bool
552TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
553 CXXMethodDecl *Tmpl) {
554 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
555 New->setAccess(Tmpl->getAccess());
Anders Carlsson77b7f1d2009-05-14 22:15:41 +0000556 if (Tmpl->isVirtualAsWritten()) {
557 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +0000558 Record->setAggregate(false);
559 Record->setPOD(false);
560 Record->setPolymorphic(true);
561 }
562 if (Tmpl->isDeleted())
563 New->setDeleted();
564 if (Tmpl->isPure()) {
565 New->setPure();
566 Record->setAbstract(true);
567 }
568
569 // FIXME: attributes
570 // FIXME: New needs a pointer to Tmpl
571 return false;
572}
Douglas Gregora58861f2009-05-13 20:28:22 +0000573
574/// \brief Instantiate the definition of the given function from its
575/// template.
576///
577/// \param Function the already-instantiated declaration of a
578/// function.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000579void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
580 FunctionDecl *Function) {
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000581 // FIXME: make this work for function template specializations, too.
582
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000583 if (Function->isInvalidDecl())
584 return;
585
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000586 // Find the function body that we'll be substituting.
587 const FunctionDecl *PatternDecl
588 = Function->getInstantiatedFromMemberFunction();
589 Stmt *Pattern = 0;
590 if (PatternDecl)
591 Pattern = PatternDecl->getBody(Context, PatternDecl);
592
593 if (!Pattern)
594 return;
595
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000596 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
597 if (Inst)
598 return;
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000599
Douglas Gregore2c31ff2009-05-15 17:59:04 +0000600 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
601
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000602 // Introduce a new scope where local variable instantiations will be
603 // recorded.
604 LocalInstantiationScope Scope(*this);
605
606 // Introduce the instantiated function parameters into the local
607 // instantiation scope.
608 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
609 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
610 Function->getParamDecl(I));
611
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000612 // Enter the scope of this instantiation. We don't use
613 // PushDeclContext because we don't have a scope.
614 DeclContext *PreviousContext = CurContext;
615 CurContext = Function;
616
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000617 // Instantiate the function body.
618 OwningStmtResult Body
619 = InstantiateStmt(Pattern, getTemplateInstantiationArgs(Function));
Douglas Gregore2c31ff2009-05-15 17:59:04 +0000620
621 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
622 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000623
624 CurContext = PreviousContext;
Douglas Gregora58861f2009-05-13 20:28:22 +0000625}
626
627/// \brief Instantiate the definition of the given variable from its
628/// template.
629///
630/// \param Var the already-instantiated declaration of a variable.
631void Sema::InstantiateVariableDefinition(VarDecl *Var) {
632 // FIXME: Implement this!
633}