blob: a519f78fc45d74ba46d4e1e0de093318f6e00476 [file] [log] [blame]
Shih-wei Liaof8fd82b2010-02-10 11:10:31 -08001//===--- DeclCXX.cpp - C++ Declaration AST Node Implementation ------------===//
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//
10// This file implements the C++ related Decl classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclCXX.h"
15#include "clang/AST/DeclTemplate.h"
16#include "clang/AST/ASTContext.h"
17#include "clang/AST/Expr.h"
18#include "clang/AST/TypeLoc.h"
19#include "clang/Basic/IdentifierTable.h"
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/SmallPtrSet.h"
22using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// Decl Allocation/Deallocation Method Implementations
26//===----------------------------------------------------------------------===//
27
28CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
29 : UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
30 UserDeclaredCopyAssignment(false), UserDeclaredDestructor(false),
31 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
32 Abstract(false), HasTrivialConstructor(true),
33 HasTrivialCopyConstructor(true), HasTrivialCopyAssignment(true),
34 HasTrivialDestructor(true), ComputedVisibleConversions(false),
35 Bases(0), NumBases(0), VBases(0), NumVBases(0),
36 Definition(D) {
37}
38
39CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
40 SourceLocation L, IdentifierInfo *Id,
41 CXXRecordDecl *PrevDecl,
42 SourceLocation TKL)
43 : RecordDecl(K, TK, DC, L, Id, PrevDecl, TKL),
44 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
45 TemplateOrInstantiation() { }
46
47CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
48 SourceLocation L, IdentifierInfo *Id,
49 SourceLocation TKL,
50 CXXRecordDecl* PrevDecl,
51 bool DelayTypeCreation) {
52 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, L, Id,
53 PrevDecl, TKL);
54
55 // FIXME: DelayTypeCreation seems like such a hack
56 if (!DelayTypeCreation)
57 C.getTypeDeclType(R, PrevDecl);
58 return R;
59}
60
61CXXRecordDecl::~CXXRecordDecl() {
62}
63
64void CXXRecordDecl::Destroy(ASTContext &C) {
65 if (data().Definition == this) {
66 C.Deallocate(data().Bases);
67 C.Deallocate(data().VBases);
68 C.Deallocate(&data());
69 }
70 this->RecordDecl::Destroy(C);
71}
72
73void
74CXXRecordDecl::setBases(ASTContext &C,
75 CXXBaseSpecifier const * const *Bases,
76 unsigned NumBases) {
77 // C++ [dcl.init.aggr]p1:
78 // An aggregate is an array or a class (clause 9) with [...]
79 // no base classes [...].
80 data().Aggregate = false;
81
82 if (data().Bases)
83 C.Deallocate(data().Bases);
84
85 int vbaseCount = 0;
86 llvm::SmallVector<const CXXBaseSpecifier*, 8> UniqueVbases;
87 bool hasDirectVirtualBase = false;
88
89 data().Bases = new(C) CXXBaseSpecifier [NumBases];
90 data().NumBases = NumBases;
91 for (unsigned i = 0; i < NumBases; ++i) {
92 data().Bases[i] = *Bases[i];
93 // Keep track of inherited vbases for this base class.
94 const CXXBaseSpecifier *Base = Bases[i];
95 QualType BaseType = Base->getType();
96 // Skip template types.
97 // FIXME. This means that this list must be rebuilt during template
98 // instantiation.
99 if (BaseType->isDependentType())
100 continue;
101 CXXRecordDecl *BaseClassDecl
102 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
103 if (Base->isVirtual())
104 hasDirectVirtualBase = true;
105 for (CXXRecordDecl::base_class_iterator VBase =
106 BaseClassDecl->vbases_begin(),
107 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
108 // Add this vbase to the array of vbases for current class if it is
109 // not already in the list.
110 // FIXME. Note that we do a linear search as number of such classes are
111 // very few.
112 int i;
113 for (i = 0; i < vbaseCount; ++i)
114 if (UniqueVbases[i]->getType() == VBase->getType())
115 break;
116 if (i == vbaseCount) {
117 UniqueVbases.push_back(VBase);
118 ++vbaseCount;
119 }
120 }
121 }
122 if (hasDirectVirtualBase) {
123 // Iterate one more time through the direct bases and add the virtual
124 // base to the list of vritual bases for current class.
125 for (unsigned i = 0; i < NumBases; ++i) {
126 const CXXBaseSpecifier *VBase = Bases[i];
127 if (!VBase->isVirtual())
128 continue;
129 int j;
130 for (j = 0; j < vbaseCount; ++j)
131 if (UniqueVbases[j]->getType() == VBase->getType())
132 break;
133 if (j == vbaseCount) {
134 UniqueVbases.push_back(VBase);
135 ++vbaseCount;
136 }
137 }
138 }
139 if (vbaseCount > 0) {
140 // build AST for inhireted, direct or indirect, virtual bases.
141 data().VBases = new (C) CXXBaseSpecifier [vbaseCount];
142 data().NumVBases = vbaseCount;
143 for (int i = 0; i < vbaseCount; i++) {
144 QualType QT = UniqueVbases[i]->getType();
145 CXXRecordDecl *VBaseClassDecl
146 = cast<CXXRecordDecl>(QT->getAs<RecordType>()->getDecl());
147 data().VBases[i] =
148 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
149 VBaseClassDecl->getTagKind() == RecordDecl::TK_class,
150 UniqueVbases[i]->getAccessSpecifier(), QT);
151 }
152 }
153}
154
155/// Callback function for CXXRecordDecl::forallBases that acknowledges
156/// that it saw a base class.
157static bool SawBase(const CXXRecordDecl *, void *) {
158 return true;
159}
160
161bool CXXRecordDecl::hasAnyDependentBases() const {
162 if (!isDependentContext())
163 return false;
164
165 return !forallBases(SawBase, 0);
166}
167
168bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
169 return getCopyConstructor(Context, Qualifiers::Const) != 0;
170}
171
172CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context,
173 unsigned TypeQuals) const{
174 QualType ClassType
175 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
176 DeclarationName ConstructorName
177 = Context.DeclarationNames.getCXXConstructorName(
178 Context.getCanonicalType(ClassType));
179 unsigned FoundTQs;
180 DeclContext::lookup_const_iterator Con, ConEnd;
181 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
182 Con != ConEnd; ++Con) {
183 // C++ [class.copy]p2:
184 // A non-template constructor for class X is a copy constructor if [...]
185 if (isa<FunctionTemplateDecl>(*Con))
186 continue;
187
188 if (cast<CXXConstructorDecl>(*Con)->isCopyConstructor(FoundTQs)) {
189 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
190 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
191 return cast<CXXConstructorDecl>(*Con);
192
193 }
194 }
195 return 0;
196}
197
198bool CXXRecordDecl::hasConstCopyAssignment(ASTContext &Context,
199 const CXXMethodDecl *& MD) const {
200 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
201 const_cast<CXXRecordDecl*>(this)));
202 DeclarationName OpName =Context.DeclarationNames.getCXXOperatorName(OO_Equal);
203
204 DeclContext::lookup_const_iterator Op, OpEnd;
205 for (llvm::tie(Op, OpEnd) = this->lookup(OpName);
206 Op != OpEnd; ++Op) {
207 // C++ [class.copy]p9:
208 // A user-declared copy assignment operator is a non-static non-template
209 // member function of class X with exactly one parameter of type X, X&,
210 // const X&, volatile X& or const volatile X&.
211 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
212 if (!Method)
213 continue;
214
215 if (Method->isStatic())
216 continue;
217 if (Method->getPrimaryTemplate())
218 continue;
219 const FunctionProtoType *FnType =
220 Method->getType()->getAs<FunctionProtoType>();
221 assert(FnType && "Overloaded operator has no prototype.");
222 // Don't assert on this; an invalid decl might have been left in the AST.
223 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
224 continue;
225 bool AcceptsConst = true;
226 QualType ArgType = FnType->getArgType(0);
227 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
228 ArgType = Ref->getPointeeType();
229 // Is it a non-const lvalue reference?
230 if (!ArgType.isConstQualified())
231 AcceptsConst = false;
232 }
233 if (!Context.hasSameUnqualifiedType(ArgType, ClassType))
234 continue;
235 MD = Method;
236 // We have a single argument of type cv X or cv X&, i.e. we've found the
237 // copy assignment operator. Return whether it accepts const arguments.
238 return AcceptsConst;
239 }
240 assert(isInvalidDecl() &&
241 "No copy assignment operator declared in valid code.");
242 return false;
243}
244
245void
246CXXRecordDecl::addedConstructor(ASTContext &Context,
247 CXXConstructorDecl *ConDecl) {
248 assert(!ConDecl->isImplicit() && "addedConstructor - not for implicit decl");
249 // Note that we have a user-declared constructor.
250 data().UserDeclaredConstructor = true;
251
252 // C++ [dcl.init.aggr]p1:
253 // An aggregate is an array or a class (clause 9) with no
254 // user-declared constructors (12.1) [...].
255 data().Aggregate = false;
256
257 // C++ [class]p4:
258 // A POD-struct is an aggregate class [...]
259 data().PlainOldData = false;
260
261 // C++ [class.ctor]p5:
262 // A constructor is trivial if it is an implicitly-declared default
263 // constructor.
264 // FIXME: C++0x: don't do this for "= default" default constructors.
265 data().HasTrivialConstructor = false;
266
267 // Note when we have a user-declared copy constructor, which will
268 // suppress the implicit declaration of a copy constructor.
269 if (ConDecl->isCopyConstructor()) {
270 data().UserDeclaredCopyConstructor = true;
271
272 // C++ [class.copy]p6:
273 // A copy constructor is trivial if it is implicitly declared.
274 // FIXME: C++0x: don't do this for "= default" copy constructors.
275 data().HasTrivialCopyConstructor = false;
276 }
277}
278
279void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context,
280 CXXMethodDecl *OpDecl) {
281 // We're interested specifically in copy assignment operators.
282 const FunctionProtoType *FnType = OpDecl->getType()->getAs<FunctionProtoType>();
283 assert(FnType && "Overloaded operator has no proto function type.");
284 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
285
286 // Copy assignment operators must be non-templates.
287 if (OpDecl->getPrimaryTemplate() || OpDecl->getDescribedFunctionTemplate())
288 return;
289
290 QualType ArgType = FnType->getArgType(0);
291 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>())
292 ArgType = Ref->getPointeeType();
293
294 ArgType = ArgType.getUnqualifiedType();
295 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
296 const_cast<CXXRecordDecl*>(this)));
297
298 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
299 return;
300
301 // This is a copy assignment operator.
302 // Note on the decl that it is a copy assignment operator.
303 OpDecl->setCopyAssignment(true);
304
305 // Suppress the implicit declaration of a copy constructor.
306 data().UserDeclaredCopyAssignment = true;
307
308 // C++ [class.copy]p11:
309 // A copy assignment operator is trivial if it is implicitly declared.
310 // FIXME: C++0x: don't do this for "= default" copy operators.
311 data().HasTrivialCopyAssignment = false;
312
313 // C++ [class]p4:
314 // A POD-struct is an aggregate class that [...] has no user-defined copy
315 // assignment operator [...].
316 data().PlainOldData = false;
317}
318
319void
320CXXRecordDecl::collectConversionFunctions(
321 llvm::SmallPtrSet<CanQualType, 8>& ConversionsTypeSet) const
322{
323 const UnresolvedSetImpl *Cs = getConversionFunctions();
324 for (UnresolvedSetImpl::iterator I = Cs->begin(), E = Cs->end();
325 I != E; ++I) {
326 NamedDecl *TopConv = *I;
327 CanQualType TConvType;
328 if (FunctionTemplateDecl *TConversionTemplate =
329 dyn_cast<FunctionTemplateDecl>(TopConv))
330 TConvType =
331 getASTContext().getCanonicalType(
332 TConversionTemplate->getTemplatedDecl()->getResultType());
333 else
334 TConvType =
335 getASTContext().getCanonicalType(
336 cast<CXXConversionDecl>(TopConv)->getConversionType());
337 ConversionsTypeSet.insert(TConvType);
338 }
339}
340
341/// getNestedVisibleConversionFunctions - imports unique conversion
342/// functions from base classes into the visible conversion function
343/// list of the class 'RD'. This is a private helper method.
344/// TopConversionsTypeSet is the set of conversion functions of the class
345/// we are interested in. HiddenConversionTypes is set of conversion functions
346/// of the immediate derived class which hides the conversion functions found
347/// in current class.
348void
349CXXRecordDecl::getNestedVisibleConversionFunctions(CXXRecordDecl *RD,
350 const llvm::SmallPtrSet<CanQualType, 8> &TopConversionsTypeSet,
351 const llvm::SmallPtrSet<CanQualType, 8> &HiddenConversionTypes)
352{
353 bool inTopClass = (RD == this);
354 QualType ClassType = getASTContext().getTypeDeclType(this);
355 if (const RecordType *Record = ClassType->getAs<RecordType>()) {
356 const UnresolvedSetImpl *Cs
357 = cast<CXXRecordDecl>(Record->getDecl())->getConversionFunctions();
358
359 for (UnresolvedSetImpl::iterator I = Cs->begin(), E = Cs->end();
360 I != E; ++I) {
361 NamedDecl *Conv = *I;
362 // Only those conversions not exact match of conversions in current
363 // class are candidateconversion routines.
364 CanQualType ConvType;
365 if (FunctionTemplateDecl *ConversionTemplate =
366 dyn_cast<FunctionTemplateDecl>(Conv))
367 ConvType =
368 getASTContext().getCanonicalType(
369 ConversionTemplate->getTemplatedDecl()->getResultType());
370 else
371 ConvType =
372 getASTContext().getCanonicalType(
373 cast<CXXConversionDecl>(Conv)->getConversionType());
374 // We only add conversion functions found in the base class if they
375 // are not hidden by those found in HiddenConversionTypes which are
376 // the conversion functions in its derived class.
377 if (inTopClass ||
378 (!TopConversionsTypeSet.count(ConvType) &&
379 !HiddenConversionTypes.count(ConvType)) ) {
380 if (FunctionTemplateDecl *ConversionTemplate =
381 dyn_cast<FunctionTemplateDecl>(Conv))
382 RD->addVisibleConversionFunction(ConversionTemplate);
383 else
384 RD->addVisibleConversionFunction(cast<CXXConversionDecl>(Conv));
385 }
386 }
387 }
388
389 if (getNumBases() == 0 && getNumVBases() == 0)
390 return;
391
392 llvm::SmallPtrSet<CanQualType, 8> ConversionFunctions;
393 if (!inTopClass)
394 collectConversionFunctions(ConversionFunctions);
395
396 for (CXXRecordDecl::base_class_iterator VBase = vbases_begin(),
397 E = vbases_end(); VBase != E; ++VBase) {
398 if (const RecordType *RT = VBase->getType()->getAs<RecordType>()) {
399 CXXRecordDecl *VBaseClassDecl
400 = cast<CXXRecordDecl>(RT->getDecl());
401 VBaseClassDecl->getNestedVisibleConversionFunctions(RD,
402 TopConversionsTypeSet,
403 (inTopClass ? TopConversionsTypeSet : ConversionFunctions));
404 }
405 }
406 for (CXXRecordDecl::base_class_iterator Base = bases_begin(),
407 E = bases_end(); Base != E; ++Base) {
408 if (Base->isVirtual())
409 continue;
410 if (const RecordType *RT = Base->getType()->getAs<RecordType>()) {
411 CXXRecordDecl *BaseClassDecl
412 = cast<CXXRecordDecl>(RT->getDecl());
413
414 BaseClassDecl->getNestedVisibleConversionFunctions(RD,
415 TopConversionsTypeSet,
416 (inTopClass ? TopConversionsTypeSet : ConversionFunctions));
417 }
418 }
419}
420
421/// getVisibleConversionFunctions - get all conversion functions visible
422/// in current class; including conversion function templates.
423const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
424 // If root class, all conversions are visible.
425 if (bases_begin() == bases_end())
426 return &data().Conversions;
427 // If visible conversion list is already evaluated, return it.
428 if (data().ComputedVisibleConversions)
429 return &data().VisibleConversions;
430 llvm::SmallPtrSet<CanQualType, 8> TopConversionsTypeSet;
431 collectConversionFunctions(TopConversionsTypeSet);
432 getNestedVisibleConversionFunctions(this, TopConversionsTypeSet,
433 TopConversionsTypeSet);
434 data().ComputedVisibleConversions = true;
435 return &data().VisibleConversions;
436}
437
438void CXXRecordDecl::addVisibleConversionFunction(
439 CXXConversionDecl *ConvDecl) {
440 assert(!ConvDecl->getDescribedFunctionTemplate() &&
441 "Conversion function templates should cast to FunctionTemplateDecl.");
442 data().VisibleConversions.addDecl(ConvDecl);
443}
444
445void CXXRecordDecl::addVisibleConversionFunction(
446 FunctionTemplateDecl *ConvDecl) {
447 assert(isa<CXXConversionDecl>(ConvDecl->getTemplatedDecl()) &&
448 "Function template is not a conversion function template");
449 data().VisibleConversions.addDecl(ConvDecl);
450}
451
452void CXXRecordDecl::addConversionFunction(CXXConversionDecl *ConvDecl) {
453 assert(!ConvDecl->getDescribedFunctionTemplate() &&
454 "Conversion function templates should cast to FunctionTemplateDecl.");
455 data().Conversions.addDecl(ConvDecl);
456}
457
458void CXXRecordDecl::addConversionFunction(FunctionTemplateDecl *ConvDecl) {
459 assert(isa<CXXConversionDecl>(ConvDecl->getTemplatedDecl()) &&
460 "Function template is not a conversion function template");
461 data().Conversions.addDecl(ConvDecl);
462}
463
464
465void CXXRecordDecl::setMethodAsVirtual(FunctionDecl *Method) {
466 Method->setVirtualAsWritten(true);
467 setAggregate(false);
468 setPOD(false);
469 setEmpty(false);
470 setPolymorphic(true);
471 setHasTrivialConstructor(false);
472 setHasTrivialCopyConstructor(false);
473 setHasTrivialCopyAssignment(false);
474}
475
476CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
477 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
478 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
479
480 return 0;
481}
482
483MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
484 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
485}
486
487void
488CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
489 TemplateSpecializationKind TSK) {
490 assert(TemplateOrInstantiation.isNull() &&
491 "Previous template or instantiation?");
492 assert(!isa<ClassTemplateSpecializationDecl>(this));
493 TemplateOrInstantiation
494 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
495}
496
497TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
498 if (const ClassTemplateSpecializationDecl *Spec
499 = dyn_cast<ClassTemplateSpecializationDecl>(this))
500 return Spec->getSpecializationKind();
501
502 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
503 return MSInfo->getTemplateSpecializationKind();
504
505 return TSK_Undeclared;
506}
507
508void
509CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
510 if (ClassTemplateSpecializationDecl *Spec
511 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
512 Spec->setSpecializationKind(TSK);
513 return;
514 }
515
516 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
517 MSInfo->setTemplateSpecializationKind(TSK);
518 return;
519 }
520
521 assert(false && "Not a class template or member class specialization");
522}
523
524CXXConstructorDecl *
525CXXRecordDecl::getDefaultConstructor(ASTContext &Context) {
526 QualType ClassType = Context.getTypeDeclType(this);
527 DeclarationName ConstructorName
528 = Context.DeclarationNames.getCXXConstructorName(
529 Context.getCanonicalType(ClassType.getUnqualifiedType()));
530
531 DeclContext::lookup_const_iterator Con, ConEnd;
532 for (llvm::tie(Con, ConEnd) = lookup(ConstructorName);
533 Con != ConEnd; ++Con) {
534 // FIXME: In C++0x, a constructor template can be a default constructor.
535 if (isa<FunctionTemplateDecl>(*Con))
536 continue;
537
538 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
539 if (Constructor->isDefaultConstructor())
540 return Constructor;
541 }
542 return 0;
543}
544
545CXXDestructorDecl *CXXRecordDecl::getDestructor(ASTContext &Context) {
546 QualType ClassType = Context.getTypeDeclType(this);
547
548 DeclarationName Name
549 = Context.DeclarationNames.getCXXDestructorName(
550 Context.getCanonicalType(ClassType));
551
552 DeclContext::lookup_iterator I, E;
553 llvm::tie(I, E) = lookup(Name);
554 assert(I != E && "Did not find a destructor!");
555
556 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
557 assert(++I == E && "Found more than one destructor!");
558
559 return Dtor;
560}
561
562CXXMethodDecl *
563CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
564 SourceLocation L, DeclarationName N,
565 QualType T, TypeSourceInfo *TInfo,
566 bool isStatic, bool isInline) {
567 return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, TInfo,
568 isStatic, isInline);
569}
570
571bool CXXMethodDecl::isUsualDeallocationFunction() const {
572 if (getOverloadedOperator() != OO_Delete &&
573 getOverloadedOperator() != OO_Array_Delete)
574 return false;
575
576 // C++ [basic.stc.dynamic.deallocation]p2:
577 // If a class T has a member deallocation function named operator delete
578 // with exactly one parameter, then that function is a usual (non-placement)
579 // deallocation function. [...]
580 if (getNumParams() == 1)
581 return true;
582
583 // C++ [basic.stc.dynamic.deallocation]p2:
584 // [...] If class T does not declare such an operator delete but does
585 // declare a member deallocation function named operator delete with
586 // exactly two parameters, the second of which has type std::size_t (18.1),
587 // then this function is a usual deallocation function.
588 ASTContext &Context = getASTContext();
589 if (getNumParams() != 2 ||
590 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
591 Context.getSizeType()))
592 return false;
593
594 // This function is a usual deallocation function if there are no
595 // single-parameter deallocation functions of the same kind.
596 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
597 R.first != R.second; ++R.first) {
598 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
599 if (FD->getNumParams() == 1)
600 return false;
601 }
602
603 return true;
604}
605
606typedef llvm::DenseMap<const CXXMethodDecl*,
607 std::vector<const CXXMethodDecl *> *>
608 OverriddenMethodsMapTy;
609
610// FIXME: We hate static data. This doesn't survive PCH saving/loading, and
611// the vtable building code uses it at CG time.
612static OverriddenMethodsMapTy *OverriddenMethods = 0;
613
614void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
615 assert(MD->isCanonicalDecl() && "Method is not canonical!");
616 assert(!MD->getParent()->isDependentContext() &&
617 "Can't add an overridden method to a class template!");
618
619 // FIXME: The CXXMethodDecl dtor needs to remove and free the entry.
620
621 if (!OverriddenMethods)
622 OverriddenMethods = new OverriddenMethodsMapTy();
623
624 std::vector<const CXXMethodDecl *> *&Methods = (*OverriddenMethods)[this];
625 if (!Methods)
626 Methods = new std::vector<const CXXMethodDecl *>;
627
628 Methods->push_back(MD);
629}
630
631CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
632 if (!OverriddenMethods)
633 return 0;
634
635 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
636 if (it == OverriddenMethods->end() || it->second->empty())
637 return 0;
638
639 return &(*it->second)[0];
640}
641
642CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
643 if (!OverriddenMethods)
644 return 0;
645
646 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
647 if (it == OverriddenMethods->end() || it->second->empty())
648 return 0;
649
650 return &(*it->second)[0] + it->second->size();
651}
652
653QualType CXXMethodDecl::getThisType(ASTContext &C) const {
654 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
655 // If the member function is declared const, the type of this is const X*,
656 // if the member function is declared volatile, the type of this is
657 // volatile X*, and if the member function is declared const volatile,
658 // the type of this is const volatile X*.
659
660 assert(isInstance() && "No 'this' for static methods!");
661
662 QualType ClassTy;
663 if (ClassTemplateDecl *TD = getParent()->getDescribedClassTemplate())
664 ClassTy = TD->getInjectedClassNameType(C);
665 else
666 ClassTy = C.getTagDeclType(getParent());
667 ClassTy = C.getQualifiedType(ClassTy,
668 Qualifiers::fromCVRMask(getTypeQualifiers()));
669 return C.getPointerType(ClassTy);
670}
671
672bool CXXMethodDecl::hasInlineBody() const {
673 // If this function is a template instantiation, look at the template from
674 // which it was instantiated.
675 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
676 if (!CheckFn)
677 CheckFn = this;
678
679 const FunctionDecl *fn;
680 return CheckFn->getBody(fn) && !fn->isOutOfLine();
681}
682
683CXXBaseOrMemberInitializer::
684CXXBaseOrMemberInitializer(ASTContext &Context,
685 TypeSourceInfo *TInfo,
686 SourceLocation L, Expr *Init, SourceLocation R)
687 : BaseOrMember(TInfo), Init(Init), AnonUnionMember(0),
688 LParenLoc(L), RParenLoc(R)
689{
690}
691
692CXXBaseOrMemberInitializer::
693CXXBaseOrMemberInitializer(ASTContext &Context,
694 FieldDecl *Member, SourceLocation MemberLoc,
695 SourceLocation L, Expr *Init, SourceLocation R)
696 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
697 AnonUnionMember(0), LParenLoc(L), RParenLoc(R)
698{
699}
700
701void CXXBaseOrMemberInitializer::Destroy(ASTContext &Context) {
702 if (Init)
703 Init->Destroy(Context);
704 this->~CXXBaseOrMemberInitializer();
705}
706
707TypeLoc CXXBaseOrMemberInitializer::getBaseClassLoc() const {
708 if (isBaseInitializer())
709 return BaseOrMember.get<TypeSourceInfo*>()->getTypeLoc();
710 else
711 return TypeLoc();
712}
713
714Type *CXXBaseOrMemberInitializer::getBaseClass() {
715 if (isBaseInitializer())
716 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
717 else
718 return 0;
719}
720
721const Type *CXXBaseOrMemberInitializer::getBaseClass() const {
722 if (isBaseInitializer())
723 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
724 else
725 return 0;
726}
727
728SourceLocation CXXBaseOrMemberInitializer::getSourceLocation() const {
729 if (isMemberInitializer())
730 return getMemberLocation();
731
732 return getBaseClassLoc().getSourceRange().getBegin();
733}
734
735SourceRange CXXBaseOrMemberInitializer::getSourceRange() const {
736 return SourceRange(getSourceLocation(), getRParenLoc());
737}
738
739CXXConstructorDecl *
740CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
741 SourceLocation L, DeclarationName N,
742 QualType T, TypeSourceInfo *TInfo,
743 bool isExplicit,
744 bool isInline, bool isImplicitlyDeclared) {
745 assert(N.getNameKind() == DeclarationName::CXXConstructorName &&
746 "Name must refer to a constructor");
747 return new (C) CXXConstructorDecl(RD, L, N, T, TInfo, isExplicit, isInline,
748 isImplicitlyDeclared);
749}
750
751bool CXXConstructorDecl::isDefaultConstructor() const {
752 // C++ [class.ctor]p5:
753 // A default constructor for a class X is a constructor of class
754 // X that can be called without an argument.
755 return (getNumParams() == 0) ||
756 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
757}
758
759bool
760CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
761 // C++ [class.copy]p2:
762 // A non-template constructor for class X is a copy constructor
763 // if its first parameter is of type X&, const X&, volatile X& or
764 // const volatile X&, and either there are no other parameters
765 // or else all other parameters have default arguments (8.3.6).
766 if ((getNumParams() < 1) ||
767 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
768 (getPrimaryTemplate() != 0) ||
769 (getDescribedFunctionTemplate() != 0))
770 return false;
771
772 const ParmVarDecl *Param = getParamDecl(0);
773
774 // Do we have a reference type? Rvalue references don't count.
775 const LValueReferenceType *ParamRefType =
776 Param->getType()->getAs<LValueReferenceType>();
777 if (!ParamRefType)
778 return false;
779
780 // Is it a reference to our class type?
781 ASTContext &Context = getASTContext();
782
783 CanQualType PointeeType
784 = Context.getCanonicalType(ParamRefType->getPointeeType());
785 CanQualType ClassTy
786 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
787 if (PointeeType.getUnqualifiedType() != ClassTy)
788 return false;
789
790 // FIXME: other qualifiers?
791
792 // We have a copy constructor.
793 TypeQuals = PointeeType.getCVRQualifiers();
794 return true;
795}
796
797bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
798 // C++ [class.conv.ctor]p1:
799 // A constructor declared without the function-specifier explicit
800 // that can be called with a single parameter specifies a
801 // conversion from the type of its first parameter to the type of
802 // its class. Such a constructor is called a converting
803 // constructor.
804 if (isExplicit() && !AllowExplicit)
805 return false;
806
807 return (getNumParams() == 0 &&
808 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
809 (getNumParams() == 1) ||
810 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
811}
812
813bool CXXConstructorDecl::isCopyConstructorLikeSpecialization() const {
814 if ((getNumParams() < 1) ||
815 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
816 (getPrimaryTemplate() == 0) ||
817 (getDescribedFunctionTemplate() != 0))
818 return false;
819
820 const ParmVarDecl *Param = getParamDecl(0);
821
822 ASTContext &Context = getASTContext();
823 CanQualType ParamType = Context.getCanonicalType(Param->getType());
824
825 // Strip off the lvalue reference, if any.
826 if (CanQual<LValueReferenceType> ParamRefType
827 = ParamType->getAs<LValueReferenceType>())
828 ParamType = ParamRefType->getPointeeType();
829
830
831 // Is it the same as our our class type?
832 CanQualType ClassTy
833 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
834 if (ParamType.getUnqualifiedType() != ClassTy)
835 return false;
836
837 return true;
838}
839
840CXXDestructorDecl *
841CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
842 SourceLocation L, DeclarationName N,
843 QualType T, bool isInline,
844 bool isImplicitlyDeclared) {
845 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
846 "Name must refer to a destructor");
847 return new (C) CXXDestructorDecl(RD, L, N, T, isInline,
848 isImplicitlyDeclared);
849}
850
851void
852CXXConstructorDecl::Destroy(ASTContext& C) {
853 C.Deallocate(BaseOrMemberInitializers);
854 CXXMethodDecl::Destroy(C);
855}
856
857CXXConversionDecl *
858CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
859 SourceLocation L, DeclarationName N,
860 QualType T, TypeSourceInfo *TInfo,
861 bool isInline, bool isExplicit) {
862 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
863 "Name must refer to a conversion function");
864 return new (C) CXXConversionDecl(RD, L, N, T, TInfo, isInline, isExplicit);
865}
866
867FriendDecl *FriendDecl::Create(ASTContext &C, DeclContext *DC,
868 SourceLocation L,
869 FriendUnion Friend,
870 SourceLocation FriendL) {
871#ifndef NDEBUG
872 if (Friend.is<NamedDecl*>()) {
873 NamedDecl *D = Friend.get<NamedDecl*>();
874 assert(isa<FunctionDecl>(D) ||
875 isa<CXXRecordDecl>(D) ||
876 isa<FunctionTemplateDecl>(D) ||
877 isa<ClassTemplateDecl>(D));
878
879 // As a temporary hack, we permit template instantiation to point
880 // to the original declaration when instantiating members.
881 assert(D->getFriendObjectKind() ||
882 (cast<CXXRecordDecl>(DC)->getTemplateSpecializationKind()));
883 }
884#endif
885
886 return new (C) FriendDecl(DC, L, Friend, FriendL);
887}
888
889LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
890 DeclContext *DC,
891 SourceLocation L,
892 LanguageIDs Lang, bool Braces) {
893 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
894}
895
896UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
897 SourceLocation L,
898 SourceLocation NamespaceLoc,
899 SourceRange QualifierRange,
900 NestedNameSpecifier *Qualifier,
901 SourceLocation IdentLoc,
902 NamedDecl *Used,
903 DeclContext *CommonAncestor) {
904 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
905 Used = NS->getOriginalNamespace();
906 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
907 Qualifier, IdentLoc, Used, CommonAncestor);
908}
909
910NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
911 if (NamespaceAliasDecl *NA =
912 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
913 return NA->getNamespace();
914 return cast_or_null<NamespaceDecl>(NominatedNamespace);
915}
916
917NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
918 SourceLocation L,
919 SourceLocation AliasLoc,
920 IdentifierInfo *Alias,
921 SourceRange QualifierRange,
922 NestedNameSpecifier *Qualifier,
923 SourceLocation IdentLoc,
924 NamedDecl *Namespace) {
925 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
926 Namespace = NS->getOriginalNamespace();
927 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange,
928 Qualifier, IdentLoc, Namespace);
929}
930
931UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
932 SourceLocation L, SourceRange NNR, SourceLocation UL,
933 NestedNameSpecifier* TargetNNS, DeclarationName Name,
934 bool IsTypeNameArg) {
935 return new (C) UsingDecl(DC, L, NNR, UL, TargetNNS, Name, IsTypeNameArg);
936}
937
938UnresolvedUsingValueDecl *
939UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
940 SourceLocation UsingLoc,
941 SourceRange TargetNNR,
942 NestedNameSpecifier *TargetNNS,
943 SourceLocation TargetNameLoc,
944 DeclarationName TargetName) {
945 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
946 TargetNNR, TargetNNS,
947 TargetNameLoc, TargetName);
948}
949
950UnresolvedUsingTypenameDecl *
951UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
952 SourceLocation UsingLoc,
953 SourceLocation TypenameLoc,
954 SourceRange TargetNNR,
955 NestedNameSpecifier *TargetNNS,
956 SourceLocation TargetNameLoc,
957 DeclarationName TargetName) {
958 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
959 TargetNNR, TargetNNS,
960 TargetNameLoc,
961 TargetName.getAsIdentifierInfo());
962}
963
964StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
965 SourceLocation L, Expr *AssertExpr,
966 StringLiteral *Message) {
967 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
968}
969
970void StaticAssertDecl::Destroy(ASTContext& C) {
971 AssertExpr->Destroy(C);
972 Message->Destroy(C);
973 this->~StaticAssertDecl();
974 C.Deallocate((void *)this);
975}
976
977StaticAssertDecl::~StaticAssertDecl() {
978}
979
980static const char *getAccessName(AccessSpecifier AS) {
981 switch (AS) {
982 default:
983 case AS_none:
984 assert("Invalid access specifier!");
985 return 0;
986 case AS_public:
987 return "public";
988 case AS_private:
989 return "private";
990 case AS_protected:
991 return "protected";
992 }
993}
994
995const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
996 AccessSpecifier AS) {
997 return DB << getAccessName(AS);
998}
999
1000