blob: 382c2fa98ac13de40947b721402783598018ac83 [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
35 // FIXME: Once we get closer to completion, replace these
36 // manually-written declarations with automatically-generated ones
37 // from 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
119 // FIXME: In theory, we could have a previous declaration for
120 // variables that are not static data members.
121 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());
133 }
134
135 return Var;
136}
137
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000138Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
139 bool Invalid = false;
140 QualType T = D->getType();
141 if (T->isDependentType()) {
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000142 T = SemaRef.InstantiateType(T, TemplateArgs,
143 D->getLocation(), D->getDeclName());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000144 if (!T.isNull() && T->isFunctionType()) {
145 // C++ [temp.arg.type]p3:
146 // If a declaration acquires a function type through a type
147 // dependent on a template-parameter and this causes a
148 // declaration that does not use the syntactic form of a
149 // function declarator to have function type, the program is
150 // ill-formed.
151 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
152 << T;
153 T = QualType();
154 Invalid = true;
155 }
156 }
157
158 Expr *BitWidth = D->getBitWidth();
159 if (Invalid)
160 BitWidth = 0;
161 else if (BitWidth) {
162 OwningExprResult InstantiatedBitWidth
Douglas Gregor7e063902009-05-11 23:53:27 +0000163 = SemaRef.InstantiateExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000164 if (InstantiatedBitWidth.isInvalid()) {
165 Invalid = true;
166 BitWidth = 0;
167 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000168 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000169 }
170
171 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), T,
172 cast<RecordDecl>(Owner),
173 D->getLocation(),
174 D->isMutable(),
175 BitWidth,
176 D->getAccess(),
177 0);
178 if (Field) {
179 if (Invalid)
180 Field->setInvalidDecl();
181
Douglas Gregor6ab35242009-04-09 21:40:53 +0000182 Owner->addDecl(SemaRef.Context, Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000183 }
184
185 return Field;
186}
187
188Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
189 Expr *AssertExpr = D->getAssertExpr();
190
191 OwningExprResult InstantiatedAssertExpr
Douglas Gregor7e063902009-05-11 23:53:27 +0000192 = SemaRef.InstantiateExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000193 if (InstantiatedAssertExpr.isInvalid())
194 return 0;
195
196 OwningExprResult Message = SemaRef.Clone(D->getMessage());
197 Decl *StaticAssert
Chris Lattnerb28317a2009-03-28 19:18:32 +0000198 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
199 move(InstantiatedAssertExpr),
200 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000201 return StaticAssert;
202}
203
204Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
205 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
206 D->getLocation(), D->getIdentifier(),
207 /*PrevDecl=*/0);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000208 Enum->setAccess(D->getAccess());
Douglas Gregor6ab35242009-04-09 21:40:53 +0000209 Owner->addDecl(SemaRef.Context, Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000210 Enum->startDefinition();
211
Chris Lattnerb28317a2009-03-28 19:18:32 +0000212 llvm::SmallVector<Sema::DeclPtrTy, 16> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000213
214 EnumConstantDecl *LastEnumConst = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000215 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(SemaRef.Context),
216 ECEnd = D->enumerator_end(SemaRef.Context);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000217 EC != ECEnd; ++EC) {
218 // The specified value for the enumerator.
219 OwningExprResult Value = SemaRef.Owned((Expr *)0);
220 if (Expr *UninstValue = EC->getInitExpr())
Douglas Gregor7e063902009-05-11 23:53:27 +0000221 Value = SemaRef.InstantiateExpr(UninstValue, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000222
223 // Drop the initial value and continue.
224 bool isInvalid = false;
225 if (Value.isInvalid()) {
226 Value = SemaRef.Owned((Expr *)0);
227 isInvalid = true;
228 }
229
230 EnumConstantDecl *EnumConst
231 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
232 EC->getLocation(), EC->getIdentifier(),
233 move(Value));
234
235 if (isInvalid) {
236 if (EnumConst)
237 EnumConst->setInvalidDecl();
238 Enum->setInvalidDecl();
239 }
240
241 if (EnumConst) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000242 Enum->addDecl(SemaRef.Context, EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000243 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000244 LastEnumConst = EnumConst;
245 }
246 }
247
Chris Lattnerb28317a2009-03-28 19:18:32 +0000248 SemaRef.ActOnEnumBody(Enum->getLocation(), Sema::DeclPtrTy::make(Enum),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000249 &Enumerators[0], Enumerators.size());
250
251 return Enum;
252}
253
Douglas Gregor6477b692009-03-25 15:04:13 +0000254Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
255 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
256 return 0;
257}
258
Douglas Gregord475b8d2009-03-25 21:17:03 +0000259Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
260 CXXRecordDecl *PrevDecl = 0;
261 if (D->isInjectedClassName())
262 PrevDecl = cast<CXXRecordDecl>(Owner);
263
264 CXXRecordDecl *Record
265 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
266 D->getLocation(), D->getIdentifier(), PrevDecl);
267 Record->setImplicit(D->isImplicit());
268 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000269 if (!D->isInjectedClassName())
270 Record->setInstantiationOfMemberClass(D);
271
Douglas Gregor6ab35242009-04-09 21:40:53 +0000272 Owner->addDecl(SemaRef.Context, Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000273 return Record;
274}
275
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000276Decl *TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D) {
277 // Only handle actual methods; we'll deal with constructors,
278 // destructors, etc. separately.
279 if (D->getKind() != Decl::CXXMethod)
280 return 0;
281
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000282 Sema::LocalInstantiationScope Scope(SemaRef);
283
Douglas Gregor5545e162009-03-24 00:38:23 +0000284 llvm::SmallVector<ParmVarDecl *, 16> Params;
285 QualType T = InstantiateFunctionType(D, Params);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000286 if (T.isNull())
287 return 0;
288
289 // Build the instantiated method declaration.
290 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
291 CXXMethodDecl *Method
292 = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
293 D->getDeclName(), T, D->isStatic(),
294 D->isInline());
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000295 Method->setInstantiationOfMemberFunction(D);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000296
Douglas Gregor5545e162009-03-24 00:38:23 +0000297 // Attach the parameters
298 for (unsigned P = 0; P < Params.size(); ++P)
299 Params[P]->setOwningFunction(Method);
300 Method->setParams(SemaRef.Context, &Params[0], Params.size());
301
302 if (InitMethodInstantiation(Method, D))
303 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000304
305 NamedDecl *PrevDecl
306 = SemaRef.LookupQualifiedName(Owner, Method->getDeclName(),
307 Sema::LookupOrdinaryName, true);
308 // In C++, the previous declaration we find might be a tag type
309 // (class or enum). In this case, the new declaration will hide the
310 // tag type. Note that this does does not apply if we're declaring a
311 // typedef (C++ [dcl.typedef]p4).
312 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
313 PrevDecl = 0;
314 bool Redeclaration = false;
315 bool OverloadableAttrRequired = false;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000316 SemaRef.CheckFunctionDeclaration(Method, PrevDecl, Redeclaration,
317 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000318
319 if (!Method->isInvalidDecl() || !PrevDecl)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000320 Owner->addDecl(SemaRef.Context, Method);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000321 return Method;
322}
323
Douglas Gregor615c5d42009-03-24 16:43:20 +0000324Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000325 Sema::LocalInstantiationScope Scope(SemaRef);
326
Douglas Gregor615c5d42009-03-24 16:43:20 +0000327 llvm::SmallVector<ParmVarDecl *, 16> Params;
328 QualType T = InstantiateFunctionType(D, Params);
329 if (T.isNull())
330 return 0;
331
332 // Build the instantiated method declaration.
333 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
334 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
335 DeclarationName Name
Douglas Gregor49f25ec2009-05-15 21:18:27 +0000336 = SemaRef.Context.DeclarationNames.getCXXConstructorName(
337 SemaRef.Context.getCanonicalType(ClassTy));
Douglas Gregor615c5d42009-03-24 16:43:20 +0000338 CXXConstructorDecl *Constructor
339 = CXXConstructorDecl::Create(SemaRef.Context, Record, D->getLocation(),
340 Name, T, D->isExplicit(), D->isInline(),
341 false);
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000342 Constructor->setInstantiationOfMemberFunction(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000343
344 // Attach the parameters
345 for (unsigned P = 0; P < Params.size(); ++P)
346 Params[P]->setOwningFunction(Constructor);
347 Constructor->setParams(SemaRef.Context, &Params[0], Params.size());
348
349 if (InitMethodInstantiation(Constructor, D))
350 Constructor->setInvalidDecl();
351
352 NamedDecl *PrevDecl
353 = SemaRef.LookupQualifiedName(Owner, Name, Sema::LookupOrdinaryName, true);
354
355 // In C++, the previous declaration we find might be a tag type
356 // (class or enum). In this case, the new declaration will hide the
357 // tag type. Note that this does does not apply if we're declaring a
358 // typedef (C++ [dcl.typedef]p4).
359 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
360 PrevDecl = 0;
361 bool Redeclaration = false;
362 bool OverloadableAttrRequired = false;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000363 SemaRef.CheckFunctionDeclaration(Constructor, PrevDecl, Redeclaration,
364 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000365
Douglas Gregor49f25ec2009-05-15 21:18:27 +0000366 Record->addedConstructor(SemaRef.Context, Constructor);
Chris Lattnereaaebc72009-04-25 08:06:05 +0000367 Owner->addDecl(SemaRef.Context, Constructor);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000368 return Constructor;
369}
370
Douglas Gregor03b2b072009-03-24 00:15:49 +0000371Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000372 Sema::LocalInstantiationScope Scope(SemaRef);
373
Douglas Gregor5545e162009-03-24 00:38:23 +0000374 llvm::SmallVector<ParmVarDecl *, 16> Params;
375 QualType T = InstantiateFunctionType(D, Params);
Douglas Gregor03b2b072009-03-24 00:15:49 +0000376 if (T.isNull())
377 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +0000378 assert(Params.size() == 0 && "Destructor with parameters?");
379
Douglas Gregor03b2b072009-03-24 00:15:49 +0000380 // Build the instantiated destructor declaration.
381 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
Douglas Gregor49f25ec2009-05-15 21:18:27 +0000382 QualType ClassTy =
383 SemaRef.Context.getCanonicalType(SemaRef.Context.getTypeDeclType(Record));
Douglas Gregor03b2b072009-03-24 00:15:49 +0000384 CXXDestructorDecl *Destructor
385 = CXXDestructorDecl::Create(SemaRef.Context, Record,
386 D->getLocation(),
387 SemaRef.Context.DeclarationNames.getCXXDestructorName(ClassTy),
388 T, D->isInline(), false);
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000389 Destructor->setInstantiationOfMemberFunction(D);
Douglas Gregor5545e162009-03-24 00:38:23 +0000390 if (InitMethodInstantiation(Destructor, D))
391 Destructor->setInvalidDecl();
Douglas Gregor03b2b072009-03-24 00:15:49 +0000392
393 bool Redeclaration = false;
394 bool OverloadableAttrRequired = false;
395 NamedDecl *PrevDecl = 0;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000396 SemaRef.CheckFunctionDeclaration(Destructor, PrevDecl, Redeclaration,
397 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000398 Owner->addDecl(SemaRef.Context, Destructor);
Douglas Gregor03b2b072009-03-24 00:15:49 +0000399 return Destructor;
400}
401
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000402Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000403 Sema::LocalInstantiationScope Scope(SemaRef);
404
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000405 llvm::SmallVector<ParmVarDecl *, 16> Params;
406 QualType T = InstantiateFunctionType(D, Params);
407 if (T.isNull())
408 return 0;
409 assert(Params.size() == 0 && "Destructor with parameters?");
410
411 // Build the instantiated conversion declaration.
412 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
413 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
414 QualType ConvTy
415 = SemaRef.Context.getCanonicalType(T->getAsFunctionType()->getResultType());
416 CXXConversionDecl *Conversion
417 = CXXConversionDecl::Create(SemaRef.Context, Record,
418 D->getLocation(),
419 SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(ConvTy),
420 T, D->isInline(), D->isExplicit());
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000421 Conversion->setInstantiationOfMemberFunction(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000422 if (InitMethodInstantiation(Conversion, D))
423 Conversion->setInvalidDecl();
424
425 bool Redeclaration = false;
426 bool OverloadableAttrRequired = false;
427 NamedDecl *PrevDecl = 0;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000428 SemaRef.CheckFunctionDeclaration(Conversion, PrevDecl, Redeclaration,
429 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000430 Owner->addDecl(SemaRef.Context, Conversion);
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000431 return Conversion;
432}
433
Douglas Gregor6477b692009-03-25 15:04:13 +0000434ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000435 QualType OrigT = SemaRef.InstantiateType(D->getOriginalType(), TemplateArgs,
Douglas Gregor7e063902009-05-11 23:53:27 +0000436 D->getLocation(), D->getDeclName());
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000437 if (OrigT.isNull())
438 return 0;
439
440 QualType T = SemaRef.adjustParameterType(OrigT);
441
442 if (D->getDefaultArg()) {
443 // FIXME: Leave a marker for "uninstantiated" default
444 // arguments. They only get instantiated on demand at the call
445 // site.
446 unsigned DiagID = SemaRef.Diags.getCustomDiagID(Diagnostic::Warning,
447 "sorry, dropping default argument during template instantiation");
448 SemaRef.Diag(D->getDefaultArg()->getSourceRange().getBegin(), DiagID)
449 << D->getDefaultArg()->getSourceRange();
450 }
451
452 // Allocate the parameter
453 ParmVarDecl *Param = 0;
454 if (T == OrigT)
455 Param = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
456 D->getIdentifier(), T, D->getStorageClass(),
457 0);
458 else
459 Param = OriginalParmVarDecl::Create(SemaRef.Context, Owner,
460 D->getLocation(), D->getIdentifier(),
461 T, OrigT, D->getStorageClass(), 0);
462
463 // Note: we don't try to instantiate function parameters until after
464 // we've instantiated the function's type. Therefore, we don't have
465 // to check for 'void' parameter types here.
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000466 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000467 return Param;
468}
469
470Decl *
471TemplateDeclInstantiator::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
472 // Since parameter types can decay either before or after
473 // instantiation, we simply treat OriginalParmVarDecls as
474 // ParmVarDecls the same way, and create one or the other depending
475 // on what happens after template instantiation.
476 return VisitParmVarDecl(D);
477}
478
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000479Decl *Sema::InstantiateDecl(Decl *D, DeclContext *Owner,
Douglas Gregor7e063902009-05-11 23:53:27 +0000480 const TemplateArgumentList &TemplateArgs) {
481 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000482 return Instantiator.Visit(D);
483}
484
Douglas Gregor5545e162009-03-24 00:38:23 +0000485/// \brief Instantiates the type of the given function, including
486/// instantiating all of the function parameters.
487///
488/// \param D The function that we will be instantiated
489///
490/// \param Params the instantiated parameter declarations
491
492/// \returns the instantiated function's type if successfull, a NULL
493/// type if there was an error.
494QualType
495TemplateDeclInstantiator::InstantiateFunctionType(FunctionDecl *D,
496 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
497 bool InvalidDecl = false;
498
499 // Instantiate the function parameters
Douglas Gregor7e063902009-05-11 23:53:27 +0000500 TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
Douglas Gregor5545e162009-03-24 00:38:23 +0000501 llvm::SmallVector<QualType, 16> ParamTys;
502 for (FunctionDecl::param_iterator P = D->param_begin(),
503 PEnd = D->param_end();
504 P != PEnd; ++P) {
Douglas Gregor6477b692009-03-25 15:04:13 +0000505 if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
Douglas Gregor5545e162009-03-24 00:38:23 +0000506 if (PInst->getType()->isVoidType()) {
507 SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
508 PInst->setInvalidDecl();
509 }
510 else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
511 PInst->getType(),
512 diag::err_abstract_type_in_decl,
Anders Carlsson8211eff2009-03-24 01:19:16 +0000513 Sema::AbstractParamType))
Douglas Gregor5545e162009-03-24 00:38:23 +0000514 PInst->setInvalidDecl();
515
516 Params.push_back(PInst);
517 ParamTys.push_back(PInst->getType());
518
519 if (PInst->isInvalidDecl())
520 InvalidDecl = true;
521 } else
522 InvalidDecl = true;
523 }
524
525 // FIXME: Deallocate dead declarations.
526 if (InvalidDecl)
527 return QualType();
528
529 const FunctionProtoType *Proto = D->getType()->getAsFunctionProtoType();
530 assert(Proto && "Missing prototype?");
531 QualType ResultType
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000532 = SemaRef.InstantiateType(Proto->getResultType(), TemplateArgs,
Douglas Gregor5545e162009-03-24 00:38:23 +0000533 D->getLocation(), D->getDeclName());
534 if (ResultType.isNull())
535 return QualType();
536
537 return SemaRef.BuildFunctionType(ResultType, &ParamTys[0], ParamTys.size(),
538 Proto->isVariadic(), Proto->getTypeQuals(),
539 D->getLocation(), D->getDeclName());
540}
541
542/// \brief Initializes common fields of an instantiated method
543/// declaration (New) from the corresponding fields of its template
544/// (Tmpl).
545///
546/// \returns true if there was an error
547bool
548TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
549 CXXMethodDecl *Tmpl) {
550 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
551 New->setAccess(Tmpl->getAccess());
Anders Carlsson77b7f1d2009-05-14 22:15:41 +0000552 if (Tmpl->isVirtualAsWritten()) {
553 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +0000554 Record->setAggregate(false);
555 Record->setPOD(false);
556 Record->setPolymorphic(true);
557 }
558 if (Tmpl->isDeleted())
559 New->setDeleted();
560 if (Tmpl->isPure()) {
561 New->setPure();
562 Record->setAbstract(true);
563 }
564
565 // FIXME: attributes
566 // FIXME: New needs a pointer to Tmpl
567 return false;
568}
Douglas Gregora58861f2009-05-13 20:28:22 +0000569
570/// \brief Instantiate the definition of the given function from its
571/// template.
572///
573/// \param Function the already-instantiated declaration of a
574/// function.
575void Sema::InstantiateFunctionDefinition(FunctionDecl *Function) {
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000576 // FIXME: make this work for function template specializations, too.
577
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000578 if (Function->isInvalidDecl())
579 return;
580
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000581 // Find the function body that we'll be substituting.
582 const FunctionDecl *PatternDecl
583 = Function->getInstantiatedFromMemberFunction();
584 Stmt *Pattern = 0;
585 if (PatternDecl)
586 Pattern = PatternDecl->getBody(Context, PatternDecl);
587
588 if (!Pattern)
589 return;
590
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000591 // FIXME: add to the instantiation stack.
592
Douglas Gregore2c31ff2009-05-15 17:59:04 +0000593 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
594
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000595 // Introduce a new scope where local variable instantiations will be
596 // recorded.
597 LocalInstantiationScope Scope(*this);
598
599 // Introduce the instantiated function parameters into the local
600 // instantiation scope.
601 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
602 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
603 Function->getParamDecl(I));
604
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000605 // Enter the scope of this instantiation. We don't use
606 // PushDeclContext because we don't have a scope.
607 DeclContext *PreviousContext = CurContext;
608 CurContext = Function;
609
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000610 // Instantiate the function body.
611 OwningStmtResult Body
612 = InstantiateStmt(Pattern, getTemplateInstantiationArgs(Function));
Douglas Gregore2c31ff2009-05-15 17:59:04 +0000613
614 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
615 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000616
617 CurContext = PreviousContext;
Douglas Gregora58861f2009-05-13 20:28:22 +0000618}
619
620/// \brief Instantiate the definition of the given variable from its
621/// template.
622///
623/// \param Var the already-instantiated declaration of a variable.
624void Sema::InstantiateVariableDefinition(VarDecl *Var) {
625 // FIXME: Implement this!
626}