blob: 33eedcec38a486a99dd8ce0a1498e7138ff30f73 [file] [log] [blame]
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001//===--- SemaTemplateInstantiateDecl.cpp - C++ Template Decl Instantiation ===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//
9// This file implements C++ template instantiation for declarations.
10//
11//===----------------------------------------------------------------------===/
12#include "Sema.h"
Douglas Gregoraba43bb2009-05-26 20:50:29 +000013#include "clang/AST/ASTConsumer.h"
Douglas Gregor8dbc2692009-03-17 21:15:40 +000014#include "clang/AST/ASTContext.h"
15#include "clang/AST/DeclTemplate.h"
16#include "clang/AST/DeclVisitor.h"
17#include "clang/AST/Expr.h"
18#include "llvm/Support/Compiler.h"
19
20using namespace clang;
21
22namespace {
23 class VISIBILITY_HIDDEN TemplateDeclInstantiator
Chris Lattnerb28317a2009-03-28 19:18:32 +000024 : public DeclVisitor<TemplateDeclInstantiator, Decl *> {
Douglas Gregor8dbc2692009-03-17 21:15:40 +000025 Sema &SemaRef;
26 DeclContext *Owner;
Douglas Gregor7e063902009-05-11 23:53:27 +000027 const TemplateArgumentList &TemplateArgs;
Douglas Gregor8dbc2692009-03-17 21:15:40 +000028
29 public:
30 typedef Sema::OwningExprResult OwningExprResult;
31
32 TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner,
Douglas Gregor7e063902009-05-11 23:53:27 +000033 const TemplateArgumentList &TemplateArgs)
34 : SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs) { }
Douglas Gregor8dbc2692009-03-17 21:15:40 +000035
Mike Stump390b4cc2009-05-16 07:39:55 +000036 // FIXME: Once we get closer to completion, replace these manually-written
37 // declarations with automatically-generated ones from
38 // clang/AST/DeclNodes.def.
Douglas Gregor4f722be2009-03-25 15:45:12 +000039 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
40 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000041 Decl *VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +000042 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000043 Decl *VisitFieldDecl(FieldDecl *D);
44 Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
45 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor6477b692009-03-25 15:04:13 +000046 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregord475b8d2009-03-25 21:17:03 +000047 Decl *VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregor2dc0e642009-03-23 23:06:20 +000048 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
Douglas Gregor615c5d42009-03-24 16:43:20 +000049 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
Douglas Gregor03b2b072009-03-24 00:15:49 +000050 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +000051 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor6477b692009-03-25 15:04:13 +000052 ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor2dc0e642009-03-23 23:06:20 +000053 Decl *VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
Douglas Gregor5545e162009-03-24 00:38:23 +000054
Douglas Gregor8dbc2692009-03-17 21:15:40 +000055 // Base case. FIXME: Remove once we can instantiate everything.
56 Decl *VisitDecl(Decl *) {
Douglas Gregor3d7a12a2009-03-25 23:32:15 +000057 assert(false && "Template instantiation of unknown declaration kind!");
Douglas Gregor8dbc2692009-03-17 21:15:40 +000058 return 0;
59 }
Douglas Gregor5545e162009-03-24 00:38:23 +000060
61 // Helper functions for instantiating methods.
62 QualType InstantiateFunctionType(FunctionDecl *D,
63 llvm::SmallVectorImpl<ParmVarDecl *> &Params);
64 bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000065 };
66}
67
Douglas Gregor4f722be2009-03-25 15:45:12 +000068Decl *
69TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
70 assert(false && "Translation units cannot be instantiated");
71 return D;
72}
73
74Decl *
75TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
76 assert(false && "Namespaces cannot be instantiated");
77 return D;
78}
79
Douglas Gregor8dbc2692009-03-17 21:15:40 +000080Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
81 bool Invalid = false;
82 QualType T = D->getUnderlyingType();
83 if (T->isDependentType()) {
Douglas Gregor1eee0e72009-05-14 21:06:31 +000084 T = SemaRef.InstantiateType(T, TemplateArgs,
85 D->getLocation(), D->getDeclName());
Douglas Gregor8dbc2692009-03-17 21:15:40 +000086 if (T.isNull()) {
87 Invalid = true;
88 T = SemaRef.Context.IntTy;
89 }
90 }
91
92 // Create the new typedef
93 TypedefDecl *Typedef
94 = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
95 D->getIdentifier(), T);
96 if (Invalid)
97 Typedef->setInvalidDecl();
98
Douglas Gregor6ab35242009-04-09 21:40:53 +000099 Owner->addDecl(SemaRef.Context, Typedef);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000100 return Typedef;
101}
102
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000103Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
104 // Instantiate the type of the declaration
105 QualType T = SemaRef.InstantiateType(D->getType(), TemplateArgs,
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000106 D->getTypeSpecStartLoc(),
107 D->getDeclName());
108 if (T.isNull())
109 return 0;
110
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000111 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000112 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
113 D->getLocation(), D->getIdentifier(),
114 T, D->getStorageClass(),
115 D->getTypeSpecStartLoc());
116 Var->setThreadSpecified(D->isThreadSpecified());
117 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
118 Var->setDeclaredInCondition(D->isDeclaredInCondition());
119
Mike Stump390b4cc2009-05-16 07:39:55 +0000120 // FIXME: In theory, we could have a previous declaration for variables that
121 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000122 bool Redeclaration = false;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000123 SemaRef.CheckVariableDeclaration(Var, 0, Redeclaration);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000124 Owner->addDecl(SemaRef.Context, Var);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000125
126 if (D->getInit()) {
127 OwningExprResult Init
Douglas Gregor7e063902009-05-11 23:53:27 +0000128 = SemaRef.InstantiateExpr(D->getInit(), TemplateArgs);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000129 if (Init.isInvalid())
130 Var->setInvalidDecl();
131 else
Chris Lattnerb28317a2009-03-28 19:18:32 +0000132 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init),
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000133 D->hasCXXDirectInitializer());
Douglas Gregord308e622009-05-18 20:51:54 +0000134 } else {
135 // FIXME: Call ActOnUninitializedDecl? (Not always)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000136 }
137
138 return Var;
139}
140
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000141Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
142 bool Invalid = false;
143 QualType T = D->getType();
144 if (T->isDependentType()) {
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000145 T = SemaRef.InstantiateType(T, TemplateArgs,
146 D->getLocation(), D->getDeclName());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000147 if (!T.isNull() && T->isFunctionType()) {
148 // C++ [temp.arg.type]p3:
149 // If a declaration acquires a function type through a type
150 // dependent on a template-parameter and this causes a
151 // declaration that does not use the syntactic form of a
152 // function declarator to have function type, the program is
153 // ill-formed.
154 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
155 << T;
156 T = QualType();
157 Invalid = true;
158 }
159 }
160
161 Expr *BitWidth = D->getBitWidth();
162 if (Invalid)
163 BitWidth = 0;
164 else if (BitWidth) {
165 OwningExprResult InstantiatedBitWidth
Douglas Gregor7e063902009-05-11 23:53:27 +0000166 = SemaRef.InstantiateExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000167 if (InstantiatedBitWidth.isInvalid()) {
168 Invalid = true;
169 BitWidth = 0;
170 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000171 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000172 }
173
174 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), T,
175 cast<RecordDecl>(Owner),
176 D->getLocation(),
177 D->isMutable(),
178 BitWidth,
179 D->getAccess(),
180 0);
181 if (Field) {
182 if (Invalid)
183 Field->setInvalidDecl();
184
Douglas Gregor6ab35242009-04-09 21:40:53 +0000185 Owner->addDecl(SemaRef.Context, Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000186 }
187
188 return Field;
189}
190
191Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
192 Expr *AssertExpr = D->getAssertExpr();
193
194 OwningExprResult InstantiatedAssertExpr
Douglas Gregor7e063902009-05-11 23:53:27 +0000195 = SemaRef.InstantiateExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000196 if (InstantiatedAssertExpr.isInvalid())
197 return 0;
198
199 OwningExprResult Message = SemaRef.Clone(D->getMessage());
200 Decl *StaticAssert
Chris Lattnerb28317a2009-03-28 19:18:32 +0000201 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
202 move(InstantiatedAssertExpr),
203 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000204 return StaticAssert;
205}
206
207Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
208 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
209 D->getLocation(), D->getIdentifier(),
210 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000211 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000212 Enum->setAccess(D->getAccess());
Douglas Gregor6ab35242009-04-09 21:40:53 +0000213 Owner->addDecl(SemaRef.Context, Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000214 Enum->startDefinition();
215
Chris Lattnerb28317a2009-03-28 19:18:32 +0000216 llvm::SmallVector<Sema::DeclPtrTy, 16> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000217
218 EnumConstantDecl *LastEnumConst = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000219 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(SemaRef.Context),
220 ECEnd = D->enumerator_end(SemaRef.Context);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000221 EC != ECEnd; ++EC) {
222 // The specified value for the enumerator.
223 OwningExprResult Value = SemaRef.Owned((Expr *)0);
224 if (Expr *UninstValue = EC->getInitExpr())
Douglas Gregor7e063902009-05-11 23:53:27 +0000225 Value = SemaRef.InstantiateExpr(UninstValue, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000226
227 // Drop the initial value and continue.
228 bool isInvalid = false;
229 if (Value.isInvalid()) {
230 Value = SemaRef.Owned((Expr *)0);
231 isInvalid = true;
232 }
233
234 EnumConstantDecl *EnumConst
235 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
236 EC->getLocation(), EC->getIdentifier(),
237 move(Value));
238
239 if (isInvalid) {
240 if (EnumConst)
241 EnumConst->setInvalidDecl();
242 Enum->setInvalidDecl();
243 }
244
245 if (EnumConst) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000246 Enum->addDecl(SemaRef.Context, EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000247 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000248 LastEnumConst = EnumConst;
249 }
250 }
251
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000252 // FIXME: Fixup LBraceLoc and RBraceLoc
253 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
254 Sema::DeclPtrTy::make(Enum),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000255 &Enumerators[0], Enumerators.size());
256
257 return Enum;
258}
259
Douglas Gregor6477b692009-03-25 15:04:13 +0000260Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
261 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
262 return 0;
263}
264
Douglas Gregord475b8d2009-03-25 21:17:03 +0000265Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
266 CXXRecordDecl *PrevDecl = 0;
267 if (D->isInjectedClassName())
268 PrevDecl = cast<CXXRecordDecl>(Owner);
269
270 CXXRecordDecl *Record
271 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
272 D->getLocation(), D->getIdentifier(), PrevDecl);
273 Record->setImplicit(D->isImplicit());
274 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000275 if (!D->isInjectedClassName())
276 Record->setInstantiationOfMemberClass(D);
277
Douglas Gregor6ab35242009-04-09 21:40:53 +0000278 Owner->addDecl(SemaRef.Context, Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000279 return Record;
280}
281
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000282Decl *TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D) {
283 // Only handle actual methods; we'll deal with constructors,
284 // destructors, etc. separately.
285 if (D->getKind() != Decl::CXXMethod)
286 return 0;
287
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000288 Sema::LocalInstantiationScope Scope(SemaRef);
289
Douglas Gregor5545e162009-03-24 00:38:23 +0000290 llvm::SmallVector<ParmVarDecl *, 16> Params;
291 QualType T = InstantiateFunctionType(D, Params);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000292 if (T.isNull())
293 return 0;
294
295 // Build the instantiated method declaration.
296 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
297 CXXMethodDecl *Method
298 = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
299 D->getDeclName(), T, D->isStatic(),
300 D->isInline());
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000301 Method->setInstantiationOfMemberFunction(D);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000302
Douglas Gregor5545e162009-03-24 00:38:23 +0000303 // Attach the parameters
304 for (unsigned P = 0; P < Params.size(); ++P)
305 Params[P]->setOwningFunction(Method);
Jay Foadbeaaccd2009-05-21 09:52:38 +0000306 Method->setParams(SemaRef.Context, Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +0000307
308 if (InitMethodInstantiation(Method, D))
309 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000310
311 NamedDecl *PrevDecl
312 = SemaRef.LookupQualifiedName(Owner, Method->getDeclName(),
313 Sema::LookupOrdinaryName, true);
314 // In C++, the previous declaration we find might be a tag type
315 // (class or enum). In this case, the new declaration will hide the
316 // tag type. Note that this does does not apply if we're declaring a
317 // typedef (C++ [dcl.typedef]p4).
318 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
319 PrevDecl = 0;
320 bool Redeclaration = false;
321 bool OverloadableAttrRequired = false;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000322 SemaRef.CheckFunctionDeclaration(Method, PrevDecl, Redeclaration,
323 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000324
325 if (!Method->isInvalidDecl() || !PrevDecl)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000326 Owner->addDecl(SemaRef.Context, Method);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000327 return Method;
328}
329
Douglas Gregor615c5d42009-03-24 16:43:20 +0000330Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000331 Sema::LocalInstantiationScope Scope(SemaRef);
332
Douglas Gregor615c5d42009-03-24 16:43:20 +0000333 llvm::SmallVector<ParmVarDecl *, 16> Params;
334 QualType T = InstantiateFunctionType(D, Params);
335 if (T.isNull())
336 return 0;
337
338 // Build the instantiated method declaration.
339 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
340 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
341 DeclarationName Name
Douglas Gregor49f25ec2009-05-15 21:18:27 +0000342 = SemaRef.Context.DeclarationNames.getCXXConstructorName(
343 SemaRef.Context.getCanonicalType(ClassTy));
Douglas Gregor615c5d42009-03-24 16:43:20 +0000344 CXXConstructorDecl *Constructor
345 = CXXConstructorDecl::Create(SemaRef.Context, Record, D->getLocation(),
346 Name, T, D->isExplicit(), D->isInline(),
347 false);
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000348 Constructor->setInstantiationOfMemberFunction(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000349
350 // Attach the parameters
351 for (unsigned P = 0; P < Params.size(); ++P)
352 Params[P]->setOwningFunction(Constructor);
Jay Foadbeaaccd2009-05-21 09:52:38 +0000353 Constructor->setParams(SemaRef.Context, Params.data(), Params.size());
Douglas Gregor615c5d42009-03-24 16:43:20 +0000354
355 if (InitMethodInstantiation(Constructor, D))
356 Constructor->setInvalidDecl();
357
358 NamedDecl *PrevDecl
359 = SemaRef.LookupQualifiedName(Owner, Name, Sema::LookupOrdinaryName, true);
360
361 // In C++, the previous declaration we find might be a tag type
362 // (class or enum). In this case, the new declaration will hide the
363 // tag type. Note that this does does not apply if we're declaring a
364 // typedef (C++ [dcl.typedef]p4).
365 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
366 PrevDecl = 0;
367 bool Redeclaration = false;
368 bool OverloadableAttrRequired = false;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000369 SemaRef.CheckFunctionDeclaration(Constructor, PrevDecl, Redeclaration,
370 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000371
Douglas Gregor49f25ec2009-05-15 21:18:27 +0000372 Record->addedConstructor(SemaRef.Context, Constructor);
Chris Lattnereaaebc72009-04-25 08:06:05 +0000373 Owner->addDecl(SemaRef.Context, Constructor);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000374 return Constructor;
375}
376
Douglas Gregor03b2b072009-03-24 00:15:49 +0000377Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000378 Sema::LocalInstantiationScope Scope(SemaRef);
379
Douglas Gregor5545e162009-03-24 00:38:23 +0000380 llvm::SmallVector<ParmVarDecl *, 16> Params;
381 QualType T = InstantiateFunctionType(D, Params);
Douglas Gregor03b2b072009-03-24 00:15:49 +0000382 if (T.isNull())
383 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +0000384 assert(Params.size() == 0 && "Destructor with parameters?");
385
Douglas Gregor03b2b072009-03-24 00:15:49 +0000386 // Build the instantiated destructor declaration.
387 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
Douglas Gregor49f25ec2009-05-15 21:18:27 +0000388 QualType ClassTy =
389 SemaRef.Context.getCanonicalType(SemaRef.Context.getTypeDeclType(Record));
Douglas Gregor03b2b072009-03-24 00:15:49 +0000390 CXXDestructorDecl *Destructor
391 = CXXDestructorDecl::Create(SemaRef.Context, Record,
392 D->getLocation(),
393 SemaRef.Context.DeclarationNames.getCXXDestructorName(ClassTy),
394 T, D->isInline(), false);
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000395 Destructor->setInstantiationOfMemberFunction(D);
Douglas Gregor5545e162009-03-24 00:38:23 +0000396 if (InitMethodInstantiation(Destructor, D))
397 Destructor->setInvalidDecl();
Douglas Gregor03b2b072009-03-24 00:15:49 +0000398
399 bool Redeclaration = false;
400 bool OverloadableAttrRequired = false;
401 NamedDecl *PrevDecl = 0;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000402 SemaRef.CheckFunctionDeclaration(Destructor, PrevDecl, Redeclaration,
403 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000404 Owner->addDecl(SemaRef.Context, Destructor);
Douglas Gregor03b2b072009-03-24 00:15:49 +0000405 return Destructor;
406}
407
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000408Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000409 Sema::LocalInstantiationScope Scope(SemaRef);
410
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000411 llvm::SmallVector<ParmVarDecl *, 16> Params;
412 QualType T = InstantiateFunctionType(D, Params);
413 if (T.isNull())
414 return 0;
415 assert(Params.size() == 0 && "Destructor with parameters?");
416
417 // Build the instantiated conversion declaration.
418 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
419 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
420 QualType ConvTy
421 = SemaRef.Context.getCanonicalType(T->getAsFunctionType()->getResultType());
422 CXXConversionDecl *Conversion
423 = CXXConversionDecl::Create(SemaRef.Context, Record,
424 D->getLocation(),
425 SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(ConvTy),
426 T, D->isInline(), D->isExplicit());
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000427 Conversion->setInstantiationOfMemberFunction(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000428 if (InitMethodInstantiation(Conversion, D))
429 Conversion->setInvalidDecl();
430
431 bool Redeclaration = false;
432 bool OverloadableAttrRequired = false;
433 NamedDecl *PrevDecl = 0;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000434 SemaRef.CheckFunctionDeclaration(Conversion, PrevDecl, Redeclaration,
435 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000436 Owner->addDecl(SemaRef.Context, Conversion);
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000437 return Conversion;
438}
439
Douglas Gregor6477b692009-03-25 15:04:13 +0000440ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000441 QualType OrigT = SemaRef.InstantiateType(D->getOriginalType(), TemplateArgs,
Douglas Gregor7e063902009-05-11 23:53:27 +0000442 D->getLocation(), D->getDeclName());
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000443 if (OrigT.isNull())
444 return 0;
445
446 QualType T = SemaRef.adjustParameterType(OrigT);
447
448 if (D->getDefaultArg()) {
449 // FIXME: Leave a marker for "uninstantiated" default
450 // arguments. They only get instantiated on demand at the call
451 // site.
452 unsigned DiagID = SemaRef.Diags.getCustomDiagID(Diagnostic::Warning,
453 "sorry, dropping default argument during template instantiation");
454 SemaRef.Diag(D->getDefaultArg()->getSourceRange().getBegin(), DiagID)
455 << D->getDefaultArg()->getSourceRange();
456 }
457
458 // Allocate the parameter
459 ParmVarDecl *Param = 0;
460 if (T == OrigT)
461 Param = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
462 D->getIdentifier(), T, D->getStorageClass(),
463 0);
464 else
465 Param = OriginalParmVarDecl::Create(SemaRef.Context, Owner,
466 D->getLocation(), D->getIdentifier(),
467 T, OrigT, D->getStorageClass(), 0);
468
469 // Note: we don't try to instantiate function parameters until after
470 // we've instantiated the function's type. Therefore, we don't have
471 // to check for 'void' parameter types here.
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000472 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000473 return Param;
474}
475
476Decl *
477TemplateDeclInstantiator::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
478 // Since parameter types can decay either before or after
479 // instantiation, we simply treat OriginalParmVarDecls as
480 // ParmVarDecls the same way, and create one or the other depending
481 // on what happens after template instantiation.
482 return VisitParmVarDecl(D);
483}
484
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000485Decl *Sema::InstantiateDecl(Decl *D, DeclContext *Owner,
Douglas Gregor7e063902009-05-11 23:53:27 +0000486 const TemplateArgumentList &TemplateArgs) {
487 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000488 return Instantiator.Visit(D);
489}
490
Douglas Gregor5545e162009-03-24 00:38:23 +0000491/// \brief Instantiates the type of the given function, including
492/// instantiating all of the function parameters.
493///
494/// \param D The function that we will be instantiated
495///
496/// \param Params the instantiated parameter declarations
497
498/// \returns the instantiated function's type if successfull, a NULL
499/// type if there was an error.
500QualType
501TemplateDeclInstantiator::InstantiateFunctionType(FunctionDecl *D,
502 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
503 bool InvalidDecl = false;
504
505 // Instantiate the function parameters
Douglas Gregor7e063902009-05-11 23:53:27 +0000506 TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
Douglas Gregor5545e162009-03-24 00:38:23 +0000507 llvm::SmallVector<QualType, 16> ParamTys;
508 for (FunctionDecl::param_iterator P = D->param_begin(),
509 PEnd = D->param_end();
510 P != PEnd; ++P) {
Douglas Gregor6477b692009-03-25 15:04:13 +0000511 if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
Douglas Gregor5545e162009-03-24 00:38:23 +0000512 if (PInst->getType()->isVoidType()) {
513 SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
514 PInst->setInvalidDecl();
515 }
516 else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
517 PInst->getType(),
518 diag::err_abstract_type_in_decl,
Anders Carlsson8211eff2009-03-24 01:19:16 +0000519 Sema::AbstractParamType))
Douglas Gregor5545e162009-03-24 00:38:23 +0000520 PInst->setInvalidDecl();
521
522 Params.push_back(PInst);
523 ParamTys.push_back(PInst->getType());
524
525 if (PInst->isInvalidDecl())
526 InvalidDecl = true;
527 } else
528 InvalidDecl = true;
529 }
530
531 // FIXME: Deallocate dead declarations.
532 if (InvalidDecl)
533 return QualType();
534
535 const FunctionProtoType *Proto = D->getType()->getAsFunctionProtoType();
536 assert(Proto && "Missing prototype?");
537 QualType ResultType
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000538 = SemaRef.InstantiateType(Proto->getResultType(), TemplateArgs,
Douglas Gregor5545e162009-03-24 00:38:23 +0000539 D->getLocation(), D->getDeclName());
540 if (ResultType.isNull())
541 return QualType();
542
Jay Foadbeaaccd2009-05-21 09:52:38 +0000543 return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
Douglas Gregor5545e162009-03-24 00:38:23 +0000544 Proto->isVariadic(), Proto->getTypeQuals(),
545 D->getLocation(), D->getDeclName());
546}
547
548/// \brief Initializes common fields of an instantiated method
549/// declaration (New) from the corresponding fields of its template
550/// (Tmpl).
551///
552/// \returns true if there was an error
553bool
554TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
555 CXXMethodDecl *Tmpl) {
556 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
557 New->setAccess(Tmpl->getAccess());
Anders Carlsson77b7f1d2009-05-14 22:15:41 +0000558 if (Tmpl->isVirtualAsWritten()) {
559 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +0000560 Record->setAggregate(false);
561 Record->setPOD(false);
562 Record->setPolymorphic(true);
563 }
564 if (Tmpl->isDeleted())
565 New->setDeleted();
566 if (Tmpl->isPure()) {
567 New->setPure();
568 Record->setAbstract(true);
569 }
570
571 // FIXME: attributes
572 // FIXME: New needs a pointer to Tmpl
573 return false;
574}
Douglas Gregora58861f2009-05-13 20:28:22 +0000575
576/// \brief Instantiate the definition of the given function from its
577/// template.
578///
579/// \param Function the already-instantiated declaration of a
580/// function.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000581void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
582 FunctionDecl *Function) {
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000583 // FIXME: make this work for function template specializations, too.
584
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000585 if (Function->isInvalidDecl())
586 return;
587
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000588 // Find the function body that we'll be substituting.
589 const FunctionDecl *PatternDecl
590 = Function->getInstantiatedFromMemberFunction();
591 Stmt *Pattern = 0;
592 if (PatternDecl)
593 Pattern = PatternDecl->getBody(Context, PatternDecl);
594
595 if (!Pattern)
596 return;
597
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000598 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
599 if (Inst)
600 return;
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000601
Douglas Gregore2c31ff2009-05-15 17:59:04 +0000602 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
603
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000604 // Introduce a new scope where local variable instantiations will be
605 // recorded.
606 LocalInstantiationScope Scope(*this);
607
608 // Introduce the instantiated function parameters into the local
609 // instantiation scope.
610 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
611 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
612 Function->getParamDecl(I));
613
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000614 // Enter the scope of this instantiation. We don't use
615 // PushDeclContext because we don't have a scope.
616 DeclContext *PreviousContext = CurContext;
617 CurContext = Function;
618
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000619 // Instantiate the function body.
620 OwningStmtResult Body
621 = InstantiateStmt(Pattern, getTemplateInstantiationArgs(Function));
Douglas Gregore2c31ff2009-05-15 17:59:04 +0000622
623 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
624 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000625
626 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +0000627
628 DeclGroupRef DG(Function);
629 Consumer.HandleTopLevelDecl(DG);
Douglas Gregora58861f2009-05-13 20:28:22 +0000630}
631
632/// \brief Instantiate the definition of the given variable from its
633/// template.
634///
635/// \param Var the already-instantiated declaration of a variable.
636void Sema::InstantiateVariableDefinition(VarDecl *Var) {
637 // FIXME: Implement this!
638}
Douglas Gregor815215d2009-05-27 05:35:12 +0000639
640static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
641 if (D->getKind() != Other->getKind())
642 return false;
643
644 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
645 return Ctx.getCanonicalDecl(Record->getInstantiatedFromMemberClass())
646 == Ctx.getCanonicalDecl(D);
647
648 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
649 return Ctx.getCanonicalDecl(Function->getInstantiatedFromMemberFunction())
650 == Ctx.getCanonicalDecl(D);
651
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000652 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
653 return Ctx.getCanonicalDecl(Enum->getInstantiatedFromMemberEnum())
654 == Ctx.getCanonicalDecl(D);
Douglas Gregor815215d2009-05-27 05:35:12 +0000655
656 // FIXME: How can we find instantiations of anonymous unions?
657
658 return D->getDeclName() && isa<NamedDecl>(Other) &&
659 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
660}
661
662template<typename ForwardIterator>
663static NamedDecl *findInstantiationOf(ASTContext &Ctx,
664 NamedDecl *D,
665 ForwardIterator first,
666 ForwardIterator last) {
667 for (; first != last; ++first)
668 if (isInstantiationOf(Ctx, D, *first))
669 return cast<NamedDecl>(*first);
670
671 return 0;
672}
673
Douglas Gregored961e72009-05-27 17:54:46 +0000674/// \brief Find the instantiation of the given declaration within the
675/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +0000676///
677/// This routine is intended to be used when \p D is a declaration
678/// referenced from within a template, that needs to mapped into the
679/// corresponding declaration within an instantiation. For example,
680/// given:
681///
682/// \code
683/// template<typename T>
684/// struct X {
685/// enum Kind {
686/// KnownValue = sizeof(T)
687/// };
688///
689/// bool getKind() const { return KnownValue; }
690/// };
691///
692/// template struct X<int>;
693/// \endcode
694///
695/// In the instantiation of X<int>::getKind(), we need to map the
696/// EnumConstantDecl for KnownValue (which refers to
697/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +0000698/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
699/// this mapping from within the instantiation of X<int>.
700NamedDecl * Sema::InstantiateCurrentDeclRef(NamedDecl *D) {
Douglas Gregor815215d2009-05-27 05:35:12 +0000701 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor2bba76b2009-05-27 17:07:49 +0000702 if (isa<ParmVarDecl>(D) || ParentDC->isFunctionOrMethod()) {
703 // D is a local of some kind. Look into the map of local
704 // declarations to their instantiations.
705 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
706 }
Douglas Gregor815215d2009-05-27 05:35:12 +0000707
Douglas Gregor2bba76b2009-05-27 17:07:49 +0000708 if (NamedDecl *ParentDecl = dyn_cast<NamedDecl>(ParentDC)) {
Douglas Gregored961e72009-05-27 17:54:46 +0000709 ParentDecl = InstantiateCurrentDeclRef(ParentDecl);
Douglas Gregor815215d2009-05-27 05:35:12 +0000710 if (!ParentDecl)
711 return 0;
712
713 ParentDC = cast<DeclContext>(ParentDecl);
714 }
715
Douglas Gregor815215d2009-05-27 05:35:12 +0000716 if (ParentDC != D->getDeclContext()) {
717 // We performed some kind of instantiation in the parent context,
718 // so now we need to look into the instantiated parent context to
719 // find the instantiation of the declaration D.
720 NamedDecl *Result = 0;
721 if (D->getDeclName()) {
722 DeclContext::lookup_result Found
723 = ParentDC->lookup(Context, D->getDeclName());
724 Result = findInstantiationOf(Context, D, Found.first, Found.second);
725 } else {
726 // Since we don't have a name for the entity we're looking for,
727 // our only option is to walk through all of the declarations to
728 // find that name. This will occur in a few cases:
729 //
730 // - anonymous struct/union within a template
731 // - unnamed class/struct/union/enum within a template
732 //
733 // FIXME: Find a better way to find these instantiations!
734 Result = findInstantiationOf(Context, D,
735 ParentDC->decls_begin(Context),
736 ParentDC->decls_end(Context));
737 }
738 assert(Result && "Unable to find instantiation of declaration!");
739 D = Result;
740 }
741
Douglas Gregor815215d2009-05-27 05:35:12 +0000742 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
Douglas Gregored961e72009-05-27 17:54:46 +0000743 if (ClassTemplateDecl *ClassTemplate
744 = Record->getDescribedClassTemplate()) {
745 // When the declaration D was parsed, it referred to the current
746 // instantiation. Therefore, look through the current context,
747 // which contains actual instantiations, to find the
748 // instantiation of the "current instantiation" that D refers
749 // to. Alternatively, we could just instantiate the
750 // injected-class-name with the current template arguments, but
751 // such an instantiation is far more expensive.
752 for (DeclContext *DC = CurContext; !DC->isFileContext();
753 DC = DC->getParent()) {
754 if (ClassTemplateSpecializationDecl *Spec
755 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
756 if (Context.getCanonicalDecl(Spec->getSpecializedTemplate())
757 == Context.getCanonicalDecl(ClassTemplate))
758 return Spec;
759 }
760
761 assert(false &&
762 "Unable to find declaration for the current instantiation");
Douglas Gregor815215d2009-05-27 05:35:12 +0000763 }
764
765 return D;
766}