blob: 62c717b6d3df94752b97c8e7f3110f5bde1aef2e [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;
26 const TemplateArgument *TemplateArgs;
27 unsigned NumTemplateArgs;
28
29 public:
30 typedef Sema::OwningExprResult OwningExprResult;
31
32 TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner,
33 const TemplateArgument *TemplateArgs,
34 unsigned NumTemplateArgs)
35 : SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs),
36 NumTemplateArgs(NumTemplateArgs) { }
37
38 // FIXME: Once we get closer to completion, replace these
39 // manually-written declarations with automatically-generated ones
40 // from clang/AST/DeclNodes.def.
Douglas Gregor4f722be2009-03-25 15:45:12 +000041 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
42 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000043 Decl *VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +000044 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000045 Decl *VisitFieldDecl(FieldDecl *D);
46 Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
47 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor6477b692009-03-25 15:04:13 +000048 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregord475b8d2009-03-25 21:17:03 +000049 Decl *VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregor2dc0e642009-03-23 23:06:20 +000050 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
Douglas Gregor615c5d42009-03-24 16:43:20 +000051 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
Douglas Gregor03b2b072009-03-24 00:15:49 +000052 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +000053 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor6477b692009-03-25 15:04:13 +000054 ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor2dc0e642009-03-23 23:06:20 +000055 Decl *VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
Douglas Gregor5545e162009-03-24 00:38:23 +000056
Douglas Gregor8dbc2692009-03-17 21:15:40 +000057 // Base case. FIXME: Remove once we can instantiate everything.
58 Decl *VisitDecl(Decl *) {
Douglas Gregor3d7a12a2009-03-25 23:32:15 +000059 assert(false && "Template instantiation of unknown declaration kind!");
Douglas Gregor8dbc2692009-03-17 21:15:40 +000060 return 0;
61 }
Douglas Gregor5545e162009-03-24 00:38:23 +000062
63 // Helper functions for instantiating methods.
64 QualType InstantiateFunctionType(FunctionDecl *D,
65 llvm::SmallVectorImpl<ParmVarDecl *> &Params);
66 bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000067 };
68}
69
Douglas Gregor4f722be2009-03-25 15:45:12 +000070Decl *
71TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
72 assert(false && "Translation units cannot be instantiated");
73 return D;
74}
75
76Decl *
77TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
78 assert(false && "Namespaces cannot be instantiated");
79 return D;
80}
81
Douglas Gregor8dbc2692009-03-17 21:15:40 +000082Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
83 bool Invalid = false;
84 QualType T = D->getUnderlyingType();
85 if (T->isDependentType()) {
86 T = SemaRef.InstantiateType(T, TemplateArgs, NumTemplateArgs,
87 D->getLocation(),
88 D->getDeclName());
89 if (T.isNull()) {
90 Invalid = true;
91 T = SemaRef.Context.IntTy;
92 }
93 }
94
95 // Create the new typedef
96 TypedefDecl *Typedef
97 = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
98 D->getIdentifier(), T);
99 if (Invalid)
100 Typedef->setInvalidDecl();
101
Douglas Gregor6ab35242009-04-09 21:40:53 +0000102 Owner->addDecl(SemaRef.Context, Typedef);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000103 return Typedef;
104}
105
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000106Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
107 // Instantiate the type of the declaration
108 QualType T = SemaRef.InstantiateType(D->getType(), TemplateArgs,
109 NumTemplateArgs,
110 D->getTypeSpecStartLoc(),
111 D->getDeclName());
112 if (T.isNull())
113 return 0;
114
115 // Build the instantiataed declaration
116 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
117 D->getLocation(), D->getIdentifier(),
118 T, D->getStorageClass(),
119 D->getTypeSpecStartLoc());
120 Var->setThreadSpecified(D->isThreadSpecified());
121 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
122 Var->setDeclaredInCondition(D->isDeclaredInCondition());
123
124 // FIXME: In theory, we could have a previous declaration for
125 // variables that are not static data members.
126 bool Redeclaration = false;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000127 SemaRef.CheckVariableDeclaration(Var, 0, Redeclaration);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000128 Owner->addDecl(SemaRef.Context, Var);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000129
130 if (D->getInit()) {
131 OwningExprResult Init
132 = SemaRef.InstantiateExpr(D->getInit(), TemplateArgs, NumTemplateArgs);
133 if (Init.isInvalid())
134 Var->setInvalidDecl();
135 else
Chris Lattnerb28317a2009-03-28 19:18:32 +0000136 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init),
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000137 D->hasCXXDirectInitializer());
138 }
139
140 return Var;
141}
142
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000143Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
144 bool Invalid = false;
145 QualType T = D->getType();
146 if (T->isDependentType()) {
147 T = SemaRef.InstantiateType(T, TemplateArgs, NumTemplateArgs,
148 D->getLocation(),
149 D->getDeclName());
150 if (!T.isNull() && T->isFunctionType()) {
151 // C++ [temp.arg.type]p3:
152 // If a declaration acquires a function type through a type
153 // dependent on a template-parameter and this causes a
154 // declaration that does not use the syntactic form of a
155 // function declarator to have function type, the program is
156 // ill-formed.
157 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
158 << T;
159 T = QualType();
160 Invalid = true;
161 }
162 }
163
164 Expr *BitWidth = D->getBitWidth();
165 if (Invalid)
166 BitWidth = 0;
167 else if (BitWidth) {
168 OwningExprResult InstantiatedBitWidth
169 = SemaRef.InstantiateExpr(BitWidth, TemplateArgs, NumTemplateArgs);
170 if (InstantiatedBitWidth.isInvalid()) {
171 Invalid = true;
172 BitWidth = 0;
173 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000174 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000175 }
176
177 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), T,
178 cast<RecordDecl>(Owner),
179 D->getLocation(),
180 D->isMutable(),
181 BitWidth,
182 D->getAccess(),
183 0);
184 if (Field) {
185 if (Invalid)
186 Field->setInvalidDecl();
187
Douglas Gregor6ab35242009-04-09 21:40:53 +0000188 Owner->addDecl(SemaRef.Context, Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000189 }
190
191 return Field;
192}
193
194Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
195 Expr *AssertExpr = D->getAssertExpr();
196
197 OwningExprResult InstantiatedAssertExpr
198 = SemaRef.InstantiateExpr(AssertExpr, TemplateArgs, NumTemplateArgs);
199 if (InstantiatedAssertExpr.isInvalid())
200 return 0;
201
202 OwningExprResult Message = SemaRef.Clone(D->getMessage());
203 Decl *StaticAssert
Chris Lattnerb28317a2009-03-28 19:18:32 +0000204 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
205 move(InstantiatedAssertExpr),
206 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000207 return StaticAssert;
208}
209
210Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
211 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
212 D->getLocation(), D->getIdentifier(),
213 /*PrevDecl=*/0);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000214 Enum->setAccess(D->getAccess());
Douglas Gregor6ab35242009-04-09 21:40:53 +0000215 Owner->addDecl(SemaRef.Context, Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000216 Enum->startDefinition();
217
Chris Lattnerb28317a2009-03-28 19:18:32 +0000218 llvm::SmallVector<Sema::DeclPtrTy, 16> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000219
220 EnumConstantDecl *LastEnumConst = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000221 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(SemaRef.Context),
222 ECEnd = D->enumerator_end(SemaRef.Context);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000223 EC != ECEnd; ++EC) {
224 // The specified value for the enumerator.
225 OwningExprResult Value = SemaRef.Owned((Expr *)0);
226 if (Expr *UninstValue = EC->getInitExpr())
227 Value = SemaRef.InstantiateExpr(UninstValue,
228 TemplateArgs, NumTemplateArgs);
229
230 // Drop the initial value and continue.
231 bool isInvalid = false;
232 if (Value.isInvalid()) {
233 Value = SemaRef.Owned((Expr *)0);
234 isInvalid = true;
235 }
236
237 EnumConstantDecl *EnumConst
238 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
239 EC->getLocation(), EC->getIdentifier(),
240 move(Value));
241
242 if (isInvalid) {
243 if (EnumConst)
244 EnumConst->setInvalidDecl();
245 Enum->setInvalidDecl();
246 }
247
248 if (EnumConst) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000249 Enum->addDecl(SemaRef.Context, EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000250 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000251 LastEnumConst = EnumConst;
252 }
253 }
254
Chris Lattnerb28317a2009-03-28 19:18:32 +0000255 SemaRef.ActOnEnumBody(Enum->getLocation(), Sema::DeclPtrTy::make(Enum),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000256 &Enumerators[0], Enumerators.size());
257
258 return Enum;
259}
260
Douglas Gregor6477b692009-03-25 15:04:13 +0000261Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
262 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
263 return 0;
264}
265
Douglas Gregord475b8d2009-03-25 21:17:03 +0000266Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
267 CXXRecordDecl *PrevDecl = 0;
268 if (D->isInjectedClassName())
269 PrevDecl = cast<CXXRecordDecl>(Owner);
270
271 CXXRecordDecl *Record
272 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
273 D->getLocation(), D->getIdentifier(), PrevDecl);
274 Record->setImplicit(D->isImplicit());
275 Record->setAccess(D->getAccess());
276
277 if (!D->isInjectedClassName())
278 Record->setInstantiationOfMemberClass(D);
Douglas Gregorbefc20e2009-03-26 00:10:35 +0000279 else
280 Record->setDescribedClassTemplate(D->getDescribedClassTemplate());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000281
Douglas Gregor6ab35242009-04-09 21:40:53 +0000282 Owner->addDecl(SemaRef.Context, Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000283 return Record;
284}
285
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000286Decl *TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D) {
287 // Only handle actual methods; we'll deal with constructors,
288 // destructors, etc. separately.
289 if (D->getKind() != Decl::CXXMethod)
290 return 0;
291
Douglas Gregor5545e162009-03-24 00:38:23 +0000292 llvm::SmallVector<ParmVarDecl *, 16> Params;
293 QualType T = InstantiateFunctionType(D, Params);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000294 if (T.isNull())
295 return 0;
296
297 // Build the instantiated method declaration.
298 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
299 CXXMethodDecl *Method
300 = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
301 D->getDeclName(), T, D->isStatic(),
302 D->isInline());
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000303
Douglas Gregor5545e162009-03-24 00:38:23 +0000304 // Attach the parameters
305 for (unsigned P = 0; P < Params.size(); ++P)
306 Params[P]->setOwningFunction(Method);
307 Method->setParams(SemaRef.Context, &Params[0], Params.size());
308
309 if (InitMethodInstantiation(Method, D))
310 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000311
312 NamedDecl *PrevDecl
313 = SemaRef.LookupQualifiedName(Owner, Method->getDeclName(),
314 Sema::LookupOrdinaryName, true);
315 // In C++, the previous declaration we find might be a tag type
316 // (class or enum). In this case, the new declaration will hide the
317 // tag type. Note that this does does not apply if we're declaring a
318 // typedef (C++ [dcl.typedef]p4).
319 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
320 PrevDecl = 0;
321 bool Redeclaration = false;
322 bool OverloadableAttrRequired = false;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000323 SemaRef.CheckFunctionDeclaration(Method, PrevDecl, Redeclaration,
324 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000325
326 if (!Method->isInvalidDecl() || !PrevDecl)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000327 Owner->addDecl(SemaRef.Context, Method);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000328 return Method;
329}
330
Douglas Gregor615c5d42009-03-24 16:43:20 +0000331Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
332 llvm::SmallVector<ParmVarDecl *, 16> Params;
333 QualType T = InstantiateFunctionType(D, Params);
334 if (T.isNull())
335 return 0;
336
337 // Build the instantiated method declaration.
338 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
339 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
340 DeclarationName Name
341 = SemaRef.Context.DeclarationNames.getCXXConstructorName(ClassTy);
342 CXXConstructorDecl *Constructor
343 = CXXConstructorDecl::Create(SemaRef.Context, Record, D->getLocation(),
344 Name, T, D->isExplicit(), D->isInline(),
345 false);
346
347 // Attach the parameters
348 for (unsigned P = 0; P < Params.size(); ++P)
349 Params[P]->setOwningFunction(Constructor);
350 Constructor->setParams(SemaRef.Context, &Params[0], Params.size());
351
352 if (InitMethodInstantiation(Constructor, D))
353 Constructor->setInvalidDecl();
354
355 NamedDecl *PrevDecl
356 = SemaRef.LookupQualifiedName(Owner, Name, Sema::LookupOrdinaryName, true);
357
358 // In C++, the previous declaration we find might be a tag type
359 // (class or enum). In this case, the new declaration will hide the
360 // tag type. Note that this does does not apply if we're declaring a
361 // typedef (C++ [dcl.typedef]p4).
362 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
363 PrevDecl = 0;
364 bool Redeclaration = false;
365 bool OverloadableAttrRequired = false;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000366 SemaRef.CheckFunctionDeclaration(Constructor, PrevDecl, Redeclaration,
367 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000368
Chris Lattnereaaebc72009-04-25 08:06:05 +0000369 Owner->addDecl(SemaRef.Context, Constructor);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000370 return Constructor;
371}
372
Douglas Gregor03b2b072009-03-24 00:15:49 +0000373Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
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);
382 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
383 CXXDestructorDecl *Destructor
384 = CXXDestructorDecl::Create(SemaRef.Context, Record,
385 D->getLocation(),
386 SemaRef.Context.DeclarationNames.getCXXDestructorName(ClassTy),
387 T, D->isInline(), false);
Douglas Gregor5545e162009-03-24 00:38:23 +0000388 if (InitMethodInstantiation(Destructor, D))
389 Destructor->setInvalidDecl();
Douglas Gregor03b2b072009-03-24 00:15:49 +0000390
391 bool Redeclaration = false;
392 bool OverloadableAttrRequired = false;
393 NamedDecl *PrevDecl = 0;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000394 SemaRef.CheckFunctionDeclaration(Destructor, PrevDecl, Redeclaration,
395 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000396 Owner->addDecl(SemaRef.Context, Destructor);
Douglas Gregor03b2b072009-03-24 00:15:49 +0000397 return Destructor;
398}
399
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000400Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
401 llvm::SmallVector<ParmVarDecl *, 16> Params;
402 QualType T = InstantiateFunctionType(D, Params);
403 if (T.isNull())
404 return 0;
405 assert(Params.size() == 0 && "Destructor with parameters?");
406
407 // Build the instantiated conversion declaration.
408 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
409 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
410 QualType ConvTy
411 = SemaRef.Context.getCanonicalType(T->getAsFunctionType()->getResultType());
412 CXXConversionDecl *Conversion
413 = CXXConversionDecl::Create(SemaRef.Context, Record,
414 D->getLocation(),
415 SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(ConvTy),
416 T, D->isInline(), D->isExplicit());
417 if (InitMethodInstantiation(Conversion, D))
418 Conversion->setInvalidDecl();
419
420 bool Redeclaration = false;
421 bool OverloadableAttrRequired = false;
422 NamedDecl *PrevDecl = 0;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000423 SemaRef.CheckFunctionDeclaration(Conversion, PrevDecl, Redeclaration,
424 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000425 Owner->addDecl(SemaRef.Context, Conversion);
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000426 return Conversion;
427}
428
Douglas Gregor6477b692009-03-25 15:04:13 +0000429ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000430 QualType OrigT = SemaRef.InstantiateType(D->getOriginalType(), TemplateArgs,
431 NumTemplateArgs, D->getLocation(),
432 D->getDeclName());
433 if (OrigT.isNull())
434 return 0;
435
436 QualType T = SemaRef.adjustParameterType(OrigT);
437
438 if (D->getDefaultArg()) {
439 // FIXME: Leave a marker for "uninstantiated" default
440 // arguments. They only get instantiated on demand at the call
441 // site.
442 unsigned DiagID = SemaRef.Diags.getCustomDiagID(Diagnostic::Warning,
443 "sorry, dropping default argument during template instantiation");
444 SemaRef.Diag(D->getDefaultArg()->getSourceRange().getBegin(), DiagID)
445 << D->getDefaultArg()->getSourceRange();
446 }
447
448 // Allocate the parameter
449 ParmVarDecl *Param = 0;
450 if (T == OrigT)
451 Param = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
452 D->getIdentifier(), T, D->getStorageClass(),
453 0);
454 else
455 Param = OriginalParmVarDecl::Create(SemaRef.Context, Owner,
456 D->getLocation(), D->getIdentifier(),
457 T, OrigT, D->getStorageClass(), 0);
458
459 // Note: we don't try to instantiate function parameters until after
460 // we've instantiated the function's type. Therefore, we don't have
461 // to check for 'void' parameter types here.
462 return Param;
463}
464
465Decl *
466TemplateDeclInstantiator::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
467 // Since parameter types can decay either before or after
468 // instantiation, we simply treat OriginalParmVarDecls as
469 // ParmVarDecls the same way, and create one or the other depending
470 // on what happens after template instantiation.
471 return VisitParmVarDecl(D);
472}
473
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000474Decl *Sema::InstantiateDecl(Decl *D, DeclContext *Owner,
475 const TemplateArgument *TemplateArgs,
476 unsigned NumTemplateArgs) {
477 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs,
478 NumTemplateArgs);
479 return Instantiator.Visit(D);
480}
481
Douglas Gregor5545e162009-03-24 00:38:23 +0000482/// \brief Instantiates the type of the given function, including
483/// instantiating all of the function parameters.
484///
485/// \param D The function that we will be instantiated
486///
487/// \param Params the instantiated parameter declarations
488
489/// \returns the instantiated function's type if successfull, a NULL
490/// type if there was an error.
491QualType
492TemplateDeclInstantiator::InstantiateFunctionType(FunctionDecl *D,
493 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
494 bool InvalidDecl = false;
495
496 // Instantiate the function parameters
497 TemplateDeclInstantiator ParamInstantiator(SemaRef, 0,
498 TemplateArgs, NumTemplateArgs);
499 llvm::SmallVector<QualType, 16> ParamTys;
500 for (FunctionDecl::param_iterator P = D->param_begin(),
501 PEnd = D->param_end();
502 P != PEnd; ++P) {
Douglas Gregor6477b692009-03-25 15:04:13 +0000503 if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
Douglas Gregor5545e162009-03-24 00:38:23 +0000504 if (PInst->getType()->isVoidType()) {
505 SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
506 PInst->setInvalidDecl();
507 }
508 else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
509 PInst->getType(),
510 diag::err_abstract_type_in_decl,
Anders Carlsson8211eff2009-03-24 01:19:16 +0000511 Sema::AbstractParamType))
Douglas Gregor5545e162009-03-24 00:38:23 +0000512 PInst->setInvalidDecl();
513
514 Params.push_back(PInst);
515 ParamTys.push_back(PInst->getType());
516
517 if (PInst->isInvalidDecl())
518 InvalidDecl = true;
519 } else
520 InvalidDecl = true;
521 }
522
523 // FIXME: Deallocate dead declarations.
524 if (InvalidDecl)
525 return QualType();
526
527 const FunctionProtoType *Proto = D->getType()->getAsFunctionProtoType();
528 assert(Proto && "Missing prototype?");
529 QualType ResultType
530 = SemaRef.InstantiateType(Proto->getResultType(),
531 TemplateArgs, NumTemplateArgs,
532 D->getLocation(), D->getDeclName());
533 if (ResultType.isNull())
534 return QualType();
535
536 return SemaRef.BuildFunctionType(ResultType, &ParamTys[0], ParamTys.size(),
537 Proto->isVariadic(), Proto->getTypeQuals(),
538 D->getLocation(), D->getDeclName());
539}
540
541/// \brief Initializes common fields of an instantiated method
542/// declaration (New) from the corresponding fields of its template
543/// (Tmpl).
544///
545/// \returns true if there was an error
546bool
547TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
548 CXXMethodDecl *Tmpl) {
549 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
550 New->setAccess(Tmpl->getAccess());
551 if (Tmpl->isVirtual()) {
552 New->setVirtual();
553 Record->setAggregate(false);
554 Record->setPOD(false);
555 Record->setPolymorphic(true);
556 }
557 if (Tmpl->isDeleted())
558 New->setDeleted();
559 if (Tmpl->isPure()) {
560 New->setPure();
561 Record->setAbstract(true);
562 }
563
564 // FIXME: attributes
565 // FIXME: New needs a pointer to Tmpl
566 return false;
567}