blob: e7210b3431ba235f92ed2549470bf89b6cb63dcd [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
102 Owner->addDecl(Typedef);
103 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;
127 if (SemaRef.CheckVariableDeclaration(Var, 0, Redeclaration))
128 Var->setInvalidDecl();
129
130 Owner->addDecl(Var);
131
132 if (D->getInit()) {
133 OwningExprResult Init
134 = SemaRef.InstantiateExpr(D->getInit(), TemplateArgs, NumTemplateArgs);
135 if (Init.isInvalid())
136 Var->setInvalidDecl();
137 else
Chris Lattnerb28317a2009-03-28 19:18:32 +0000138 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init),
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000139 D->hasCXXDirectInitializer());
140 }
141
142 return Var;
143}
144
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000145Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
146 bool Invalid = false;
147 QualType T = D->getType();
148 if (T->isDependentType()) {
149 T = SemaRef.InstantiateType(T, TemplateArgs, NumTemplateArgs,
150 D->getLocation(),
151 D->getDeclName());
152 if (!T.isNull() && T->isFunctionType()) {
153 // C++ [temp.arg.type]p3:
154 // If a declaration acquires a function type through a type
155 // dependent on a template-parameter and this causes a
156 // declaration that does not use the syntactic form of a
157 // function declarator to have function type, the program is
158 // ill-formed.
159 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
160 << T;
161 T = QualType();
162 Invalid = true;
163 }
164 }
165
166 Expr *BitWidth = D->getBitWidth();
167 if (Invalid)
168 BitWidth = 0;
169 else if (BitWidth) {
170 OwningExprResult InstantiatedBitWidth
171 = SemaRef.InstantiateExpr(BitWidth, TemplateArgs, NumTemplateArgs);
172 if (InstantiatedBitWidth.isInvalid()) {
173 Invalid = true;
174 BitWidth = 0;
175 } else
176 BitWidth = (Expr *)InstantiatedBitWidth.release();
177 }
178
179 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), T,
180 cast<RecordDecl>(Owner),
181 D->getLocation(),
182 D->isMutable(),
183 BitWidth,
184 D->getAccess(),
185 0);
186 if (Field) {
187 if (Invalid)
188 Field->setInvalidDecl();
189
190 Owner->addDecl(Field);
191 }
192
193 return Field;
194}
195
196Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
197 Expr *AssertExpr = D->getAssertExpr();
198
199 OwningExprResult InstantiatedAssertExpr
200 = SemaRef.InstantiateExpr(AssertExpr, TemplateArgs, NumTemplateArgs);
201 if (InstantiatedAssertExpr.isInvalid())
202 return 0;
203
204 OwningExprResult Message = SemaRef.Clone(D->getMessage());
205 Decl *StaticAssert
Chris Lattnerb28317a2009-03-28 19:18:32 +0000206 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
207 move(InstantiatedAssertExpr),
208 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000209 return StaticAssert;
210}
211
212Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
213 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
214 D->getLocation(), D->getIdentifier(),
215 /*PrevDecl=*/0);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000216 Enum->setAccess(D->getAccess());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000217 Owner->addDecl(Enum);
218 Enum->startDefinition();
219
Chris Lattnerb28317a2009-03-28 19:18:32 +0000220 llvm::SmallVector<Sema::DeclPtrTy, 16> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000221
222 EnumConstantDecl *LastEnumConst = 0;
223 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
224 ECEnd = D->enumerator_end();
225 EC != ECEnd; ++EC) {
226 // The specified value for the enumerator.
227 OwningExprResult Value = SemaRef.Owned((Expr *)0);
228 if (Expr *UninstValue = EC->getInitExpr())
229 Value = SemaRef.InstantiateExpr(UninstValue,
230 TemplateArgs, NumTemplateArgs);
231
232 // Drop the initial value and continue.
233 bool isInvalid = false;
234 if (Value.isInvalid()) {
235 Value = SemaRef.Owned((Expr *)0);
236 isInvalid = true;
237 }
238
239 EnumConstantDecl *EnumConst
240 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
241 EC->getLocation(), EC->getIdentifier(),
242 move(Value));
243
244 if (isInvalid) {
245 if (EnumConst)
246 EnumConst->setInvalidDecl();
247 Enum->setInvalidDecl();
248 }
249
250 if (EnumConst) {
251 Enum->addDecl(EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000252 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000253 LastEnumConst = EnumConst;
254 }
255 }
256
Chris Lattnerb28317a2009-03-28 19:18:32 +0000257 SemaRef.ActOnEnumBody(Enum->getLocation(), Sema::DeclPtrTy::make(Enum),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000258 &Enumerators[0], Enumerators.size());
259
260 return Enum;
261}
262
Douglas Gregor6477b692009-03-25 15:04:13 +0000263Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
264 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
265 return 0;
266}
267
Douglas Gregord475b8d2009-03-25 21:17:03 +0000268Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
269 CXXRecordDecl *PrevDecl = 0;
270 if (D->isInjectedClassName())
271 PrevDecl = cast<CXXRecordDecl>(Owner);
272
273 CXXRecordDecl *Record
274 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
275 D->getLocation(), D->getIdentifier(), PrevDecl);
276 Record->setImplicit(D->isImplicit());
277 Record->setAccess(D->getAccess());
278
279 if (!D->isInjectedClassName())
280 Record->setInstantiationOfMemberClass(D);
Douglas Gregorbefc20e2009-03-26 00:10:35 +0000281 else
282 Record->setDescribedClassTemplate(D->getDescribedClassTemplate());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000283
284 Owner->addDecl(Record);
285 return Record;
286}
287
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000288Decl *TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D) {
289 // Only handle actual methods; we'll deal with constructors,
290 // destructors, etc. separately.
291 if (D->getKind() != Decl::CXXMethod)
292 return 0;
293
Douglas Gregor5545e162009-03-24 00:38:23 +0000294 llvm::SmallVector<ParmVarDecl *, 16> Params;
295 QualType T = InstantiateFunctionType(D, Params);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000296 if (T.isNull())
297 return 0;
298
299 // Build the instantiated method declaration.
300 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
301 CXXMethodDecl *Method
302 = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
303 D->getDeclName(), T, D->isStatic(),
304 D->isInline());
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000305
Douglas Gregor5545e162009-03-24 00:38:23 +0000306 // Attach the parameters
307 for (unsigned P = 0; P < Params.size(); ++P)
308 Params[P]->setOwningFunction(Method);
309 Method->setParams(SemaRef.Context, &Params[0], Params.size());
310
311 if (InitMethodInstantiation(Method, D))
312 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000313
314 NamedDecl *PrevDecl
315 = SemaRef.LookupQualifiedName(Owner, Method->getDeclName(),
316 Sema::LookupOrdinaryName, true);
317 // In C++, the previous declaration we find might be a tag type
318 // (class or enum). In this case, the new declaration will hide the
319 // tag type. Note that this does does not apply if we're declaring a
320 // typedef (C++ [dcl.typedef]p4).
321 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
322 PrevDecl = 0;
323 bool Redeclaration = false;
324 bool OverloadableAttrRequired = false;
325 if (SemaRef.CheckFunctionDeclaration(Method, PrevDecl, Redeclaration,
326 /*FIXME:*/OverloadableAttrRequired))
327 Method->setInvalidDecl();
328
329 if (!Method->isInvalidDecl() || !PrevDecl)
330 Owner->addDecl(Method);
331 return Method;
332}
333
Douglas Gregor615c5d42009-03-24 16:43:20 +0000334Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
335 llvm::SmallVector<ParmVarDecl *, 16> Params;
336 QualType T = InstantiateFunctionType(D, Params);
337 if (T.isNull())
338 return 0;
339
340 // Build the instantiated method declaration.
341 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
342 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
343 DeclarationName Name
344 = SemaRef.Context.DeclarationNames.getCXXConstructorName(ClassTy);
345 CXXConstructorDecl *Constructor
346 = CXXConstructorDecl::Create(SemaRef.Context, Record, D->getLocation(),
347 Name, T, D->isExplicit(), D->isInline(),
348 false);
349
350 // Attach the parameters
351 for (unsigned P = 0; P < Params.size(); ++P)
352 Params[P]->setOwningFunction(Constructor);
353 Constructor->setParams(SemaRef.Context, &Params[0], Params.size());
354
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;
369 if (SemaRef.CheckFunctionDeclaration(Constructor, PrevDecl, Redeclaration,
370 /*FIXME:*/OverloadableAttrRequired))
371 Constructor->setInvalidDecl();
372
373 if (!Constructor->isInvalidDecl())
374 Owner->addDecl(Constructor);
375 return Constructor;
376}
377
Douglas Gregor03b2b072009-03-24 00:15:49 +0000378Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor5545e162009-03-24 00:38:23 +0000379 llvm::SmallVector<ParmVarDecl *, 16> Params;
380 QualType T = InstantiateFunctionType(D, Params);
Douglas Gregor03b2b072009-03-24 00:15:49 +0000381 if (T.isNull())
382 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +0000383 assert(Params.size() == 0 && "Destructor with parameters?");
384
Douglas Gregor03b2b072009-03-24 00:15:49 +0000385 // Build the instantiated destructor declaration.
386 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
387 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
388 CXXDestructorDecl *Destructor
389 = CXXDestructorDecl::Create(SemaRef.Context, Record,
390 D->getLocation(),
391 SemaRef.Context.DeclarationNames.getCXXDestructorName(ClassTy),
392 T, D->isInline(), false);
Douglas Gregor5545e162009-03-24 00:38:23 +0000393 if (InitMethodInstantiation(Destructor, D))
394 Destructor->setInvalidDecl();
Douglas Gregor03b2b072009-03-24 00:15:49 +0000395
396 bool Redeclaration = false;
397 bool OverloadableAttrRequired = false;
398 NamedDecl *PrevDecl = 0;
399 if (SemaRef.CheckFunctionDeclaration(Destructor, PrevDecl, Redeclaration,
400 /*FIXME:*/OverloadableAttrRequired))
401 Destructor->setInvalidDecl();
402 Owner->addDecl(Destructor);
403 return Destructor;
404}
405
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000406Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
407 llvm::SmallVector<ParmVarDecl *, 16> Params;
408 QualType T = InstantiateFunctionType(D, Params);
409 if (T.isNull())
410 return 0;
411 assert(Params.size() == 0 && "Destructor with parameters?");
412
413 // Build the instantiated conversion declaration.
414 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
415 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
416 QualType ConvTy
417 = SemaRef.Context.getCanonicalType(T->getAsFunctionType()->getResultType());
418 CXXConversionDecl *Conversion
419 = CXXConversionDecl::Create(SemaRef.Context, Record,
420 D->getLocation(),
421 SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(ConvTy),
422 T, D->isInline(), D->isExplicit());
423 if (InitMethodInstantiation(Conversion, D))
424 Conversion->setInvalidDecl();
425
426 bool Redeclaration = false;
427 bool OverloadableAttrRequired = false;
428 NamedDecl *PrevDecl = 0;
429 if (SemaRef.CheckFunctionDeclaration(Conversion, PrevDecl, Redeclaration,
430 /*FIXME:*/OverloadableAttrRequired))
431 Conversion->setInvalidDecl();
432 Owner->addDecl(Conversion);
433 return Conversion;
434}
435
Douglas Gregor6477b692009-03-25 15:04:13 +0000436ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000437 QualType OrigT = SemaRef.InstantiateType(D->getOriginalType(), TemplateArgs,
438 NumTemplateArgs, D->getLocation(),
439 D->getDeclName());
440 if (OrigT.isNull())
441 return 0;
442
443 QualType T = SemaRef.adjustParameterType(OrigT);
444
445 if (D->getDefaultArg()) {
446 // FIXME: Leave a marker for "uninstantiated" default
447 // arguments. They only get instantiated on demand at the call
448 // site.
449 unsigned DiagID = SemaRef.Diags.getCustomDiagID(Diagnostic::Warning,
450 "sorry, dropping default argument during template instantiation");
451 SemaRef.Diag(D->getDefaultArg()->getSourceRange().getBegin(), DiagID)
452 << D->getDefaultArg()->getSourceRange();
453 }
454
455 // Allocate the parameter
456 ParmVarDecl *Param = 0;
457 if (T == OrigT)
458 Param = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
459 D->getIdentifier(), T, D->getStorageClass(),
460 0);
461 else
462 Param = OriginalParmVarDecl::Create(SemaRef.Context, Owner,
463 D->getLocation(), D->getIdentifier(),
464 T, OrigT, D->getStorageClass(), 0);
465
466 // Note: we don't try to instantiate function parameters until after
467 // we've instantiated the function's type. Therefore, we don't have
468 // to check for 'void' parameter types here.
469 return Param;
470}
471
472Decl *
473TemplateDeclInstantiator::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
474 // Since parameter types can decay either before or after
475 // instantiation, we simply treat OriginalParmVarDecls as
476 // ParmVarDecls the same way, and create one or the other depending
477 // on what happens after template instantiation.
478 return VisitParmVarDecl(D);
479}
480
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000481Decl *Sema::InstantiateDecl(Decl *D, DeclContext *Owner,
482 const TemplateArgument *TemplateArgs,
483 unsigned NumTemplateArgs) {
484 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs,
485 NumTemplateArgs);
486 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
504 TemplateDeclInstantiator ParamInstantiator(SemaRef, 0,
505 TemplateArgs, NumTemplateArgs);
506 llvm::SmallVector<QualType, 16> ParamTys;
507 for (FunctionDecl::param_iterator P = D->param_begin(),
508 PEnd = D->param_end();
509 P != PEnd; ++P) {
Douglas Gregor6477b692009-03-25 15:04:13 +0000510 if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
Douglas Gregor5545e162009-03-24 00:38:23 +0000511 if (PInst->getType()->isVoidType()) {
512 SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
513 PInst->setInvalidDecl();
514 }
515 else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
516 PInst->getType(),
517 diag::err_abstract_type_in_decl,
Anders Carlsson8211eff2009-03-24 01:19:16 +0000518 Sema::AbstractParamType))
Douglas Gregor5545e162009-03-24 00:38:23 +0000519 PInst->setInvalidDecl();
520
521 Params.push_back(PInst);
522 ParamTys.push_back(PInst->getType());
523
524 if (PInst->isInvalidDecl())
525 InvalidDecl = true;
526 } else
527 InvalidDecl = true;
528 }
529
530 // FIXME: Deallocate dead declarations.
531 if (InvalidDecl)
532 return QualType();
533
534 const FunctionProtoType *Proto = D->getType()->getAsFunctionProtoType();
535 assert(Proto && "Missing prototype?");
536 QualType ResultType
537 = SemaRef.InstantiateType(Proto->getResultType(),
538 TemplateArgs, NumTemplateArgs,
539 D->getLocation(), D->getDeclName());
540 if (ResultType.isNull())
541 return QualType();
542
543 return SemaRef.BuildFunctionType(ResultType, &ParamTys[0], ParamTys.size(),
544 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());
558 if (Tmpl->isVirtual()) {
559 New->setVirtual();
560 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}