1 //===--- DeclCXX.cpp - C++ Declaration AST Node Implementation ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the C++ related Decl classes.
12 //===----------------------------------------------------------------------===//
14 #include "clang/AST/DeclCXX.h"
15 #include "clang/AST/DeclTemplate.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/ASTMutationListener.h"
18 #include "clang/AST/CXXInheritance.h"
19 #include "clang/AST/Expr.h"
20 #include "clang/AST/TypeLoc.h"
21 #include "clang/Basic/IdentifierTable.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/ADT/SmallPtrSet.h"
24 using namespace clang;
26 //===----------------------------------------------------------------------===//
27 // Decl Allocation/Deallocation Method Implementations
28 //===----------------------------------------------------------------------===//
30 CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
31 : UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
32 UserDeclaredMoveConstructor(false), UserDeclaredCopyAssignment(false),
33 UserDeclaredMoveAssignment(false), UserDeclaredDestructor(false),
34 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
35 Abstract(false), IsStandardLayout(true), HasNoNonEmptyBases(true),
36 HasPrivateFields(false), HasProtectedFields(false), HasPublicFields(false),
37 HasMutableFields(false), HasTrivialDefaultConstructor(true),
38 HasConstExprNonCopyMoveConstructor(false), HasTrivialCopyConstructor(true),
39 HasTrivialMoveConstructor(true), HasTrivialCopyAssignment(true),
40 HasTrivialMoveAssignment(true), HasTrivialDestructor(true),
41 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
42 UserProvidedDefaultConstructor(false), DeclaredDefaultConstructor(false),
43 DeclaredCopyConstructor(false), DeclaredMoveConstructor(false),
44 DeclaredCopyAssignment(false), DeclaredMoveAssignment(false),
45 DeclaredDestructor(false), NumBases(0), NumVBases(0), Bases(), VBases(),
46 Definition(D), FirstFriend(0) {
49 CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
50 SourceLocation StartLoc, SourceLocation IdLoc,
51 IdentifierInfo *Id, CXXRecordDecl *PrevDecl)
52 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl),
53 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
54 TemplateOrInstantiation() { }
56 CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
57 DeclContext *DC, SourceLocation StartLoc,
58 SourceLocation IdLoc, IdentifierInfo *Id,
59 CXXRecordDecl* PrevDecl,
60 bool DelayTypeCreation) {
61 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc,
64 // FIXME: DelayTypeCreation seems like such a hack
65 if (!DelayTypeCreation)
66 C.getTypeDeclType(R, PrevDecl);
70 CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, EmptyShell Empty) {
71 return new (C) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(),
72 SourceLocation(), 0, 0);
76 CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
78 ASTContext &C = getASTContext();
80 // C++ [dcl.init.aggr]p1:
81 // An aggregate is an array or a class (clause 9) with [...]
82 // no base classes [...].
83 data().Aggregate = false;
85 if (!data().Bases.isOffset() && data().NumBases > 0)
86 C.Deallocate(data().getBases());
88 // The set of seen virtual base types.
89 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
91 // The virtual bases of this class.
92 llvm::SmallVector<const CXXBaseSpecifier *, 8> VBases;
94 data().Bases = new(C) CXXBaseSpecifier [NumBases];
95 data().NumBases = NumBases;
96 for (unsigned i = 0; i < NumBases; ++i) {
97 data().getBases()[i] = *Bases[i];
98 // Keep track of inherited vbases for this base class.
99 const CXXBaseSpecifier *Base = Bases[i];
100 QualType BaseType = Base->getType();
101 // Skip dependent types; we can't do any checking on them now.
102 if (BaseType->isDependentType())
104 CXXRecordDecl *BaseClassDecl
105 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
107 // C++ [dcl.init.aggr]p1:
108 // An aggregate is [...] a class with [...] no base classes [...].
109 data().Aggregate = false;
112 // A POD-struct is an aggregate class...
113 data().PlainOldData = false;
115 // A class with a non-empty base class is not empty.
116 // FIXME: Standard ref?
117 if (!BaseClassDecl->isEmpty()) {
120 // A standard-layout class is a class that:
122 // -- either has no non-static data members in the most derived
123 // class and at most one base class with non-static data members,
124 // or has no base classes with non-static data members, and
125 // If this is the second non-empty base, then neither of these two
126 // clauses can be true.
127 data().IsStandardLayout = false;
130 data().Empty = false;
131 data().HasNoNonEmptyBases = false;
134 // C++ [class.virtual]p1:
135 // A class that declares or inherits a virtual function is called a
136 // polymorphic class.
137 if (BaseClassDecl->isPolymorphic())
138 data().Polymorphic = true;
141 // A standard-layout class is a class that: [...]
142 // -- has no non-standard-layout base classes
143 if (!BaseClassDecl->isStandardLayout())
144 data().IsStandardLayout = false;
146 // Record if this base is the first non-literal field or base.
147 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType())
148 data().HasNonLiteralTypeFieldsOrBases = true;
150 // Now go through all virtual bases of this base and add them.
151 for (CXXRecordDecl::base_class_iterator VBase =
152 BaseClassDecl->vbases_begin(),
153 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
154 // Add this base if it's not already in the list.
155 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
156 VBases.push_back(VBase);
159 if (Base->isVirtual()) {
160 // Add this base if it's not already in the list.
161 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
162 VBases.push_back(Base);
164 // C++0x [meta.unary.prop] is_empty:
165 // T is a class type, but not a union type, with ... no virtual base
167 data().Empty = false;
169 // C++ [class.ctor]p5:
170 // A default constructor is trivial [...] if:
171 // -- its class has [...] no virtual bases
172 data().HasTrivialDefaultConstructor = false;
174 // C++0x [class.copy]p13:
175 // A copy/move constructor for class X is trivial if it is neither
176 // user-provided nor deleted and if
177 // -- class X has no virtual functions and no virtual base classes, and
178 data().HasTrivialCopyConstructor = false;
179 data().HasTrivialMoveConstructor = false;
181 // C++0x [class.copy]p27:
182 // A copy/move assignment operator for class X is trivial if it is
183 // neither user-provided nor deleted and if
184 // -- class X has no virtual functions and no virtual base classes, and
185 data().HasTrivialCopyAssignment = false;
186 data().HasTrivialMoveAssignment = false;
189 // A standard-layout class is a class that: [...]
190 // -- has [...] no virtual base classes
191 data().IsStandardLayout = false;
193 // C++ [class.ctor]p5:
194 // A default constructor is trivial [...] if:
195 // -- all the direct base classes of its class have trivial default
197 if (!BaseClassDecl->hasTrivialDefaultConstructor())
198 data().HasTrivialDefaultConstructor = false;
200 // C++0x [class.copy]p13:
201 // A copy/move constructor for class X is trivial if [...]
203 // -- the constructor selected to copy/move each direct base class
204 // subobject is trivial, and
205 // FIXME: C++0x: We need to only consider the selected constructor
206 // instead of all of them.
207 if (!BaseClassDecl->hasTrivialCopyConstructor())
208 data().HasTrivialCopyConstructor = false;
209 if (!BaseClassDecl->hasTrivialMoveConstructor())
210 data().HasTrivialMoveConstructor = false;
212 // C++0x [class.copy]p27:
213 // A copy/move assignment operator for class X is trivial if [...]
215 // -- the assignment operator selected to copy/move each direct base
216 // class subobject is trivial, and
217 // FIXME: C++0x: We need to only consider the selected operator instead
219 if (!BaseClassDecl->hasTrivialCopyAssignment())
220 data().HasTrivialCopyAssignment = false;
221 if (!BaseClassDecl->hasTrivialMoveAssignment())
222 data().HasTrivialMoveAssignment = false;
225 // C++ [class.ctor]p3:
226 // A destructor is trivial if all the direct base classes of its class
227 // have trivial destructors.
228 if (!BaseClassDecl->hasTrivialDestructor())
229 data().HasTrivialDestructor = false;
231 // Keep track of the presence of mutable fields.
232 if (BaseClassDecl->hasMutableFields())
233 data().HasMutableFields = true;
239 // Create base specifier for any direct or indirect virtual bases.
240 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
241 data().NumVBases = VBases.size();
242 for (int I = 0, E = VBases.size(); I != E; ++I) {
243 TypeSourceInfo *VBaseTypeInfo = VBases[I]->getTypeSourceInfo();
245 // Skip dependent types; we can't do any checking on them now.
246 if (VBaseTypeInfo->getType()->isDependentType())
249 CXXRecordDecl *VBaseClassDecl = cast<CXXRecordDecl>(
250 VBaseTypeInfo->getType()->getAs<RecordType>()->getDecl());
252 data().getVBases()[I] =
253 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
254 VBaseClassDecl->getTagKind() == TTK_Class,
255 VBases[I]->getAccessSpecifier(), VBaseTypeInfo,
260 /// Callback function for CXXRecordDecl::forallBases that acknowledges
261 /// that it saw a base class.
262 static bool SawBase(const CXXRecordDecl *, void *) {
266 bool CXXRecordDecl::hasAnyDependentBases() const {
267 if (!isDependentContext())
270 return !forallBases(SawBase, 0);
273 bool CXXRecordDecl::hasConstCopyConstructor() const {
274 return getCopyConstructor(Qualifiers::Const) != 0;
277 bool CXXRecordDecl::isTriviallyCopyable() const {
279 // A trivially copyable class is a class that:
280 // -- has no non-trivial copy constructors,
281 if (!hasTrivialCopyConstructor()) return false;
282 // -- has no non-trivial move constructors,
283 if (!hasTrivialMoveConstructor()) return false;
284 // -- has no non-trivial copy assignment operators,
285 if (!hasTrivialCopyAssignment()) return false;
286 // -- has no non-trivial move assignment operators, and
287 if (!hasTrivialMoveAssignment()) return false;
288 // -- has a trivial destructor.
289 if (!hasTrivialDestructor()) return false;
294 /// \brief Perform a simplistic form of overload resolution that only considers
295 /// cv-qualifiers on a single parameter, and return the best overload candidate
296 /// (if there is one).
297 static CXXMethodDecl *
298 GetBestOverloadCandidateSimple(
299 const llvm::SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
302 if (Cands.size() == 1)
303 return Cands[0].first;
305 unsigned Best = 0, N = Cands.size();
306 for (unsigned I = 1; I != N; ++I)
307 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
310 for (unsigned I = 1; I != N; ++I)
311 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
314 return Cands[Best].first;
317 CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(unsigned TypeQuals) const{
318 ASTContext &Context = getASTContext();
320 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
321 DeclarationName ConstructorName
322 = Context.DeclarationNames.getCXXConstructorName(
323 Context.getCanonicalType(ClassType));
325 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
326 DeclContext::lookup_const_iterator Con, ConEnd;
327 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
328 Con != ConEnd; ++Con) {
329 // C++ [class.copy]p2:
330 // A non-template constructor for class X is a copy constructor if [...]
331 if (isa<FunctionTemplateDecl>(*Con))
334 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
335 if (Constructor->isCopyConstructor(FoundTQs)) {
336 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
337 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
338 Found.push_back(std::make_pair(
339 const_cast<CXXConstructorDecl *>(Constructor),
340 Qualifiers::fromCVRMask(FoundTQs)));
344 return cast_or_null<CXXConstructorDecl>(
345 GetBestOverloadCandidateSimple(Found));
348 CXXConstructorDecl *CXXRecordDecl::getMoveConstructor() const {
349 for (ctor_iterator I = ctor_begin(), E = ctor_end(); I != E; ++I)
350 if (I->isMoveConstructor())
356 CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
357 ASTContext &Context = getASTContext();
358 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
359 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
361 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
362 DeclContext::lookup_const_iterator Op, OpEnd;
363 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
364 // C++ [class.copy]p9:
365 // A user-declared copy assignment operator is a non-static non-template
366 // member function of class X with exactly one parameter of type X, X&,
367 // const X&, volatile X& or const volatile X&.
368 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
369 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
372 const FunctionProtoType *FnType
373 = Method->getType()->getAs<FunctionProtoType>();
374 assert(FnType && "Overloaded operator has no prototype.");
375 // Don't assert on this; an invalid decl might have been left in the AST.
376 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
379 QualType ArgType = FnType->getArgType(0);
381 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
382 ArgType = Ref->getPointeeType();
383 // If we have a const argument and we have a reference to a non-const,
384 // this function does not match.
385 if (ArgIsConst && !ArgType.isConstQualified())
388 Quals = ArgType.getQualifiers();
390 // By-value copy-assignment operators are treated like const X&
391 // copy-assignment operators.
392 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
395 if (!Context.hasSameUnqualifiedType(ArgType, Class))
398 // Save this copy-assignment operator. It might be "the one".
399 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
402 // Use a simplistic form of overload resolution to find the candidate.
403 return GetBestOverloadCandidateSimple(Found);
406 CXXMethodDecl *CXXRecordDecl::getMoveAssignmentOperator() const {
407 for (method_iterator I = method_begin(), E = method_end(); I != E; ++I)
408 if (I->isMoveAssignmentOperator())
414 void CXXRecordDecl::markedVirtualFunctionPure() {
415 // C++ [class.abstract]p2:
416 // A class is abstract if it has at least one pure virtual function.
417 data().Abstract = true;
420 void CXXRecordDecl::addedMember(Decl *D) {
421 // Ignore friends and invalid declarations.
422 if (D->getFriendObjectKind() || D->isInvalidDecl())
425 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
427 D = FunTmpl->getTemplatedDecl();
429 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
430 if (Method->isVirtual()) {
431 // C++ [dcl.init.aggr]p1:
432 // An aggregate is an array or a class with [...] no virtual functions.
433 data().Aggregate = false;
436 // A POD-struct is an aggregate class...
437 data().PlainOldData = false;
439 // Virtual functions make the class non-empty.
440 // FIXME: Standard ref?
441 data().Empty = false;
443 // C++ [class.virtual]p1:
444 // A class that declares or inherits a virtual function is called a
445 // polymorphic class.
446 data().Polymorphic = true;
448 // C++0x [class.ctor]p5
449 // A default constructor is trivial [...] if:
450 // -- its class has no virtual functions [...]
451 data().HasTrivialDefaultConstructor = false;
453 // C++0x [class.copy]p13:
454 // A copy/move constructor for class X is trivial if [...]
455 // -- class X has no virtual functions [...]
456 data().HasTrivialCopyConstructor = false;
457 data().HasTrivialMoveConstructor = false;
459 // C++0x [class.copy]p27:
460 // A copy/move assignment operator for class X is trivial if [...]
461 // -- class X has no virtual functions [...]
462 data().HasTrivialCopyAssignment = false;
463 data().HasTrivialMoveAssignment = false;
464 // FIXME: Destructor?
467 // A standard-layout class is a class that: [...]
468 // -- has no virtual functions
469 data().IsStandardLayout = false;
473 if (D->isImplicit()) {
474 // Notify that an implicit member was added after the definition
476 if (!isBeingDefined())
477 if (ASTMutationListener *L = getASTMutationListener())
478 L->AddedCXXImplicitMember(data().Definition, D);
480 // If this is a special member function, note that it was added and then
482 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
483 if (Constructor->isDefaultConstructor())
484 data().DeclaredDefaultConstructor = true;
485 else if (Constructor->isCopyConstructor())
486 data().DeclaredCopyConstructor = true;
487 else if (Constructor->isMoveConstructor())
488 data().DeclaredMoveConstructor = true;
490 goto NotASpecialMember;
492 } else if (isa<CXXDestructorDecl>(D)) {
493 data().DeclaredDestructor = true;
495 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
496 if (Method->isCopyAssignmentOperator())
497 data().DeclaredCopyAssignment = true;
498 else if (Method->isMoveAssignmentOperator())
499 data().DeclaredMoveAssignment = true;
501 goto NotASpecialMember;
506 // Any other implicit declarations are handled like normal declarations.
509 // Handle (user-declared) constructors.
510 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
511 // Note that we have a user-declared constructor.
512 data().UserDeclaredConstructor = true;
514 // FIXME: Under C++0x, /only/ special member functions may be user-provided.
515 // This is probably a defect.
516 bool UserProvided = false;
518 // C++0x [class.ctor]p5:
519 // A default constructor is trivial if it is not user-provided [...]
520 if (Constructor->isDefaultConstructor()) {
521 data().DeclaredDefaultConstructor = true;
522 if (Constructor->isUserProvided()) {
523 data().HasTrivialDefaultConstructor = false;
524 data().UserProvidedDefaultConstructor = true;
529 // Note when we have a user-declared copy or move constructor, which will
530 // suppress the implicit declaration of those constructors.
532 if (Constructor->isCopyConstructor()) {
533 data().UserDeclaredCopyConstructor = true;
534 data().DeclaredCopyConstructor = true;
536 // C++0x [class.copy]p13:
537 // A copy/move constructor for class X is trivial if it is not
538 // user-provided [...]
539 if (Constructor->isUserProvided()) {
540 data().HasTrivialCopyConstructor = false;
543 } else if (Constructor->isMoveConstructor()) {
544 data().UserDeclaredMoveConstructor = true;
545 data().DeclaredMoveConstructor = true;
547 // C++0x [class.copy]p13:
548 // A copy/move constructor for class X is trivial if it is not
549 // user-provided [...]
550 if (Constructor->isUserProvided()) {
551 data().HasTrivialMoveConstructor = false;
556 if (Constructor->isConstExpr() &&
557 !Constructor->isCopyOrMoveConstructor()) {
558 // Record if we see any constexpr constructors which are niether copy
559 // nor move constructors.
560 data().HasConstExprNonCopyMoveConstructor = true;
563 // C++ [dcl.init.aggr]p1:
564 // An aggregate is an array or a class with no user-declared
565 // constructors [...].
566 // C++0x [dcl.init.aggr]p1:
567 // An aggregate is an array or a class with no user-provided
568 // constructors [...].
569 if (!getASTContext().getLangOptions().CPlusPlus0x || UserProvided)
570 data().Aggregate = false;
573 // A POD-struct is an aggregate class [...]
574 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
575 // type is technically an aggregate in C++0x since it wouldn't be in 03.
576 data().PlainOldData = false;
581 // Handle (user-declared) destructors.
582 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
583 data().DeclaredDestructor = true;
584 data().UserDeclaredDestructor = true;
587 // A POD-struct is an aggregate class that has [...] no user-defined
589 // This bit is the C++03 POD bit, not the 0x one.
590 data().PlainOldData = false;
592 // C++0x [class.dtor]p5:
593 // A destructor is trivial if it is not user-provided and [...]
594 if (DD->isUserProvided())
595 data().HasTrivialDestructor = false;
600 // Handle (user-declared) member functions.
601 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
602 if (Method->isCopyAssignmentOperator()) {
604 // A POD-struct is an aggregate class that [...] has no user-defined
605 // copy assignment operator [...].
606 // This is the C++03 bit only.
607 data().PlainOldData = false;
609 // This is a copy assignment operator.
611 // Suppress the implicit declaration of a copy constructor.
612 data().UserDeclaredCopyAssignment = true;
613 data().DeclaredCopyAssignment = true;
615 // C++0x [class.copy]p27:
616 // A copy/move assignment operator for class X is trivial if it is
617 // neither user-provided nor deleted [...]
618 if (Method->isUserProvided())
619 data().HasTrivialCopyAssignment = false;
624 if (Method->isMoveAssignmentOperator()) {
625 // This is an extension in C++03 mode, but we'll keep consistency by
626 // taking a move assignment operator to induce non-POD-ness
627 data().PlainOldData = false;
629 // This is a move assignment operator.
630 data().UserDeclaredMoveAssignment = true;
631 data().DeclaredMoveAssignment = true;
633 // C++0x [class.copy]p27:
634 // A copy/move assignment operator for class X is trivial if it is
635 // neither user-provided nor deleted [...]
636 if (Method->isUserProvided())
637 data().HasTrivialMoveAssignment = false;
640 // Keep the list of conversion functions up-to-date.
641 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
642 // We don't record specializations.
643 if (Conversion->getPrimaryTemplate())
646 // FIXME: We intentionally don't use the decl's access here because it
647 // hasn't been set yet. That's really just a misdesign in Sema.
650 if (FunTmpl->getPreviousDeclaration())
651 data().Conversions.replace(FunTmpl->getPreviousDeclaration(),
654 data().Conversions.addDecl(FunTmpl);
656 if (Conversion->getPreviousDeclaration())
657 data().Conversions.replace(Conversion->getPreviousDeclaration(),
660 data().Conversions.addDecl(Conversion);
667 // Handle non-static data members.
668 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
669 // C++ [dcl.init.aggr]p1:
670 // An aggregate is an array or a class (clause 9) with [...] no
671 // private or protected non-static data members (clause 11).
673 // A POD must be an aggregate.
674 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
675 data().Aggregate = false;
676 data().PlainOldData = false;
680 // A standard-layout class is a class that:
682 // -- has the same access control for all non-static data members,
683 switch (D->getAccess()) {
684 case AS_private: data().HasPrivateFields = true; break;
685 case AS_protected: data().HasProtectedFields = true; break;
686 case AS_public: data().HasPublicFields = true; break;
687 case AS_none: assert(0 && "Invalid access specifier");
689 if ((data().HasPrivateFields + data().HasProtectedFields +
690 data().HasPublicFields) > 1)
691 data().IsStandardLayout = false;
693 // Keep track of the presence of mutable fields.
694 if (Field->isMutable())
695 data().HasMutableFields = true;
698 // A POD struct is a class that is both a trivial class and a
699 // standard-layout class, and has no non-static data members of type
700 // non-POD struct, non-POD union (or array of such types).
701 ASTContext &Context = getASTContext();
702 QualType T = Context.getBaseElementType(Field->getType());
704 data().PlainOldData = false;
705 if (T->isReferenceType()) {
706 data().HasTrivialDefaultConstructor = false;
709 // A standard-layout class is a class that:
710 // -- has no non-static data members of type [...] reference,
711 data().IsStandardLayout = false;
714 // Record if this field is the first non-literal field or base.
715 if (!hasNonLiteralTypeFieldsOrBases() && !T->isLiteralType())
716 data().HasNonLiteralTypeFieldsOrBases = true;
718 if (Field->hasInClassInitializer()) {
720 // A default constructor is trivial if [...] no non-static data member
721 // of its class has a brace-or-equal-initializer.
722 data().HasTrivialDefaultConstructor = false;
724 // C++0x [dcl.init.aggr]p1:
725 // An aggregate is a [...] class with [...] no
726 // brace-or-equal-initializers for non-static data members.
727 data().Aggregate = false;
730 // A POD struct is [...] a trivial class.
731 data().PlainOldData = false;
734 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
735 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
736 if (FieldRec->getDefinition()) {
737 // C++0x [class.ctor]p5:
738 // A defulat constructor is trivial [...] if:
739 // -- for all the non-static data members of its class that are of
740 // class type (or array thereof), each such class has a trivial
741 // default constructor.
742 if (!FieldRec->hasTrivialDefaultConstructor())
743 data().HasTrivialDefaultConstructor = false;
745 // C++0x [class.copy]p13:
746 // A copy/move constructor for class X is trivial if [...]
748 // -- for each non-static data member of X that is of class type (or
749 // an array thereof), the constructor selected to copy/move that
750 // member is trivial;
751 // FIXME: C++0x: We don't correctly model 'selected' constructors.
752 if (!FieldRec->hasTrivialCopyConstructor())
753 data().HasTrivialCopyConstructor = false;
754 if (!FieldRec->hasTrivialMoveConstructor())
755 data().HasTrivialMoveConstructor = false;
757 // C++0x [class.copy]p27:
758 // A copy/move assignment operator for class X is trivial if [...]
760 // -- for each non-static data member of X that is of class type (or
761 // an array thereof), the assignment operator selected to
762 // copy/move that member is trivial;
763 // FIXME: C++0x: We don't correctly model 'selected' operators.
764 if (!FieldRec->hasTrivialCopyAssignment())
765 data().HasTrivialCopyAssignment = false;
766 if (!FieldRec->hasTrivialMoveAssignment())
767 data().HasTrivialMoveAssignment = false;
769 if (!FieldRec->hasTrivialDestructor())
770 data().HasTrivialDestructor = false;
773 // A standard-layout class is a class that:
774 // -- has no non-static data members of type non-standard-layout
775 // class (or array of such types) [...]
776 if (!FieldRec->isStandardLayout())
777 data().IsStandardLayout = false;
780 // A standard-layout class is a class that:
782 // -- has no base classes of the same type as the first non-static
784 // We don't want to expend bits in the state of the record decl
785 // tracking whether this is the first non-static data member so we
786 // cheat a bit and use some of the existing state: the empty bit.
787 // Virtual bases and virtual methods make a class non-empty, but they
788 // also make it non-standard-layout so we needn't check here.
789 // A non-empty base class may leave the class standard-layout, but not
790 // if we have arrived here, and have at least on non-static data
791 // member. If IsStandardLayout remains true, then the first non-static
792 // data member must come through here with Empty still true, and Empty
793 // will subsequently be set to false below.
794 if (data().IsStandardLayout && data().Empty) {
795 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
798 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
799 data().IsStandardLayout = false;
805 // Keep track of the presence of mutable fields.
806 if (FieldRec->hasMutableFields())
807 data().HasMutableFields = true;
812 // A standard-layout class is a class that:
814 // -- either has no non-static data members in the most derived
815 // class and at most one base class with non-static data members,
816 // or has no base classes with non-static data members, and
817 // At this point we know that we have a non-static data member, so the last
819 if (!data().HasNoNonEmptyBases)
820 data().IsStandardLayout = false;
822 // If this is not a zero-length bit-field, then the class is not empty.
824 if (!Field->getBitWidth())
825 data().Empty = false;
826 else if (!Field->getBitWidth()->isTypeDependent() &&
827 !Field->getBitWidth()->isValueDependent()) {
829 if (Field->getBitWidth()->isIntegerConstantExpr(Bits, Context))
831 data().Empty = false;
836 // Handle using declarations of conversion functions.
837 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
838 if (Shadow->getDeclName().getNameKind()
839 == DeclarationName::CXXConversionFunctionName)
840 data().Conversions.addDecl(Shadow, Shadow->getAccess());
843 static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
845 if (isa<UsingShadowDecl>(Conv))
846 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
847 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
848 T = ConvTemp->getTemplatedDecl()->getResultType();
850 T = cast<CXXConversionDecl>(Conv)->getConversionType();
851 return Context.getCanonicalType(T);
854 /// Collect the visible conversions of a base class.
856 /// \param Base a base class of the class we're considering
857 /// \param InVirtual whether this base class is a virtual base (or a base
858 /// of a virtual base)
859 /// \param Access the access along the inheritance path to this base
860 /// \param ParentHiddenTypes the conversions provided by the inheritors
862 /// \param Output the set to which to add conversions from non-virtual bases
863 /// \param VOutput the set to which to add conversions from virtual bases
864 /// \param HiddenVBaseCs the set of conversions which were hidden in a
865 /// virtual base along some inheritance path
866 static void CollectVisibleConversions(ASTContext &Context,
867 CXXRecordDecl *Record,
869 AccessSpecifier Access,
870 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
871 UnresolvedSetImpl &Output,
872 UnresolvedSetImpl &VOutput,
873 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
874 // The set of types which have conversions in this class or its
875 // subclasses. As an optimization, we don't copy the derived set
876 // unless it might change.
877 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
878 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
880 // Collect the direct conversions and figure out which conversions
881 // will be hidden in the subclasses.
882 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
884 HiddenTypesBuffer = ParentHiddenTypes;
885 HiddenTypes = &HiddenTypesBuffer;
887 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
889 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
891 // If this conversion is hidden and we're in a virtual base,
892 // remember that it's hidden along some inheritance path.
893 if (Hidden && InVirtual)
894 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
896 // If this conversion isn't hidden, add it to the appropriate output.
898 AccessSpecifier IAccess
899 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
902 VOutput.addDecl(I.getDecl(), IAccess);
904 Output.addDecl(I.getDecl(), IAccess);
909 // Collect information recursively from any base classes.
910 for (CXXRecordDecl::base_class_iterator
911 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
912 const RecordType *RT = I->getType()->getAs<RecordType>();
915 AccessSpecifier BaseAccess
916 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
917 bool BaseInVirtual = InVirtual || I->isVirtual();
919 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
920 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
921 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
925 /// Collect the visible conversions of a class.
927 /// This would be extremely straightforward if it weren't for virtual
928 /// bases. It might be worth special-casing that, really.
929 static void CollectVisibleConversions(ASTContext &Context,
930 CXXRecordDecl *Record,
931 UnresolvedSetImpl &Output) {
932 // The collection of all conversions in virtual bases that we've
933 // found. These will be added to the output as long as they don't
934 // appear in the hidden-conversions set.
935 UnresolvedSet<8> VBaseCs;
937 // The set of conversions in virtual bases that we've determined to
939 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
941 // The set of types hidden by classes derived from this one.
942 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
944 // Go ahead and collect the direct conversions and add them to the
946 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
947 Output.append(Cs.begin(), Cs.end());
948 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
949 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
951 // Recursively collect conversions from base classes.
952 for (CXXRecordDecl::base_class_iterator
953 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
954 const RecordType *RT = I->getType()->getAs<RecordType>();
957 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
958 I->isVirtual(), I->getAccessSpecifier(),
959 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
962 // Add any unhidden conversions provided by virtual bases.
963 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
965 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
966 Output.addDecl(I.getDecl(), I.getAccess());
970 /// getVisibleConversionFunctions - get all conversion functions visible
971 /// in current class; including conversion function templates.
972 const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
973 // If root class, all conversions are visible.
974 if (bases_begin() == bases_end())
975 return &data().Conversions;
976 // If visible conversion list is already evaluated, return it.
977 if (data().ComputedVisibleConversions)
978 return &data().VisibleConversions;
979 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
980 data().ComputedVisibleConversions = true;
981 return &data().VisibleConversions;
984 void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
985 // This operation is O(N) but extremely rare. Sema only uses it to
986 // remove UsingShadowDecls in a class that were followed by a direct
987 // declaration, e.g.:
989 // using B::operator int;
992 // This is uncommon by itself and even more uncommon in conjunction
993 // with sufficiently large numbers of directly-declared conversions
994 // that asymptotic behavior matters.
996 UnresolvedSetImpl &Convs = *getConversionFunctions();
997 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
998 if (Convs[I].getDecl() == ConvDecl) {
1000 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1001 && "conversion was found multiple times in unresolved set");
1006 llvm_unreachable("conversion not found in set!");
1009 CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
1010 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
1011 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1016 MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1017 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1021 CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1022 TemplateSpecializationKind TSK) {
1023 assert(TemplateOrInstantiation.isNull() &&
1024 "Previous template or instantiation?");
1025 assert(!isa<ClassTemplateSpecializationDecl>(this));
1026 TemplateOrInstantiation
1027 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1030 TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1031 if (const ClassTemplateSpecializationDecl *Spec
1032 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1033 return Spec->getSpecializationKind();
1035 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
1036 return MSInfo->getTemplateSpecializationKind();
1038 return TSK_Undeclared;
1042 CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1043 if (ClassTemplateSpecializationDecl *Spec
1044 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1045 Spec->setSpecializationKind(TSK);
1049 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
1050 MSInfo->setTemplateSpecializationKind(TSK);
1054 assert(false && "Not a class template or member class specialization");
1057 CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1058 ASTContext &Context = getASTContext();
1059 QualType ClassType = Context.getTypeDeclType(this);
1061 DeclarationName Name
1062 = Context.DeclarationNames.getCXXDestructorName(
1063 Context.getCanonicalType(ClassType));
1065 DeclContext::lookup_const_iterator I, E;
1066 llvm::tie(I, E) = lookup(Name);
1070 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
1074 void CXXRecordDecl::completeDefinition() {
1075 completeDefinition(0);
1078 void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1079 RecordDecl::completeDefinition();
1081 // If the class may be abstract (but hasn't been marked as such), check for
1082 // any pure final overriders.
1083 if (mayBeAbstract()) {
1084 CXXFinalOverriderMap MyFinalOverriders;
1085 if (!FinalOverriders) {
1086 getFinalOverriders(MyFinalOverriders);
1087 FinalOverriders = &MyFinalOverriders;
1091 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1092 MEnd = FinalOverriders->end();
1093 M != MEnd && !Done; ++M) {
1094 for (OverridingMethods::iterator SO = M->second.begin(),
1095 SOEnd = M->second.end();
1096 SO != SOEnd && !Done; ++SO) {
1097 assert(SO->second.size() > 0 &&
1098 "All virtual functions have overridding virtual functions");
1100 // C++ [class.abstract]p4:
1101 // A class is abstract if it contains or inherits at least one
1102 // pure virtual function for which the final overrider is pure
1104 if (SO->second.front().Method->isPure()) {
1105 data().Abstract = true;
1113 // Set access bits correctly on the directly-declared conversions.
1114 for (UnresolvedSetIterator I = data().Conversions.begin(),
1115 E = data().Conversions.end();
1117 data().Conversions.setAccess(I, (*I)->getAccess());
1120 bool CXXRecordDecl::mayBeAbstract() const {
1121 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1122 isDependentContext())
1125 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1128 CXXRecordDecl *BaseDecl
1129 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1130 if (BaseDecl->isAbstract())
1138 CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
1139 SourceLocation StartLoc,
1140 const DeclarationNameInfo &NameInfo,
1141 QualType T, TypeSourceInfo *TInfo,
1142 bool isStatic, StorageClass SCAsWritten, bool isInline,
1143 SourceLocation EndLocation) {
1144 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
1145 isStatic, SCAsWritten, isInline, EndLocation);
1148 bool CXXMethodDecl::isUsualDeallocationFunction() const {
1149 if (getOverloadedOperator() != OO_Delete &&
1150 getOverloadedOperator() != OO_Array_Delete)
1153 // C++ [basic.stc.dynamic.deallocation]p2:
1154 // A template instance is never a usual deallocation function,
1155 // regardless of its signature.
1156 if (getPrimaryTemplate())
1159 // C++ [basic.stc.dynamic.deallocation]p2:
1160 // If a class T has a member deallocation function named operator delete
1161 // with exactly one parameter, then that function is a usual (non-placement)
1162 // deallocation function. [...]
1163 if (getNumParams() == 1)
1166 // C++ [basic.stc.dynamic.deallocation]p2:
1167 // [...] If class T does not declare such an operator delete but does
1168 // declare a member deallocation function named operator delete with
1169 // exactly two parameters, the second of which has type std::size_t (18.1),
1170 // then this function is a usual deallocation function.
1171 ASTContext &Context = getASTContext();
1172 if (getNumParams() != 2 ||
1173 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1174 Context.getSizeType()))
1177 // This function is a usual deallocation function if there are no
1178 // single-parameter deallocation functions of the same kind.
1179 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1180 R.first != R.second; ++R.first) {
1181 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
1182 if (FD->getNumParams() == 1)
1189 bool CXXMethodDecl::isCopyAssignmentOperator() const {
1190 // C++0x [class.copy]p17:
1191 // A user-declared copy assignment operator X::operator= is a non-static
1192 // non-template member function of class X with exactly one parameter of
1193 // type X, X&, const X&, volatile X& or const volatile X&.
1194 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1195 /*non-static*/ isStatic() ||
1196 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate())
1199 QualType ParamType = getParamDecl(0)->getType();
1200 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1201 ParamType = Ref->getPointeeType();
1203 ASTContext &Context = getASTContext();
1205 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1206 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1209 bool CXXMethodDecl::isMoveAssignmentOperator() const {
1210 // C++0x [class.copy]p19:
1211 // A user-declared move assignment operator X::operator= is a non-static
1212 // non-template member function of class X with exactly one parameter of type
1213 // X&&, const X&&, volatile X&&, or const volatile X&&.
1214 if (getOverloadedOperator() != OO_Equal || isStatic() ||
1215 getPrimaryTemplate() || getDescribedFunctionTemplate())
1218 QualType ParamType = getParamDecl(0)->getType();
1219 if (!isa<RValueReferenceType>(ParamType))
1221 ParamType = ParamType->getPointeeType();
1223 ASTContext &Context = getASTContext();
1225 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1226 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1229 void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
1230 assert(MD->isCanonicalDecl() && "Method is not canonical!");
1231 assert(!MD->getParent()->isDependentContext() &&
1232 "Can't add an overridden method to a class template!");
1234 getASTContext().addOverriddenMethod(this, MD);
1237 CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
1238 return getASTContext().overridden_methods_begin(this);
1241 CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
1242 return getASTContext().overridden_methods_end(this);
1245 unsigned CXXMethodDecl::size_overridden_methods() const {
1246 return getASTContext().overridden_methods_size(this);
1249 QualType CXXMethodDecl::getThisType(ASTContext &C) const {
1250 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1251 // If the member function is declared const, the type of this is const X*,
1252 // if the member function is declared volatile, the type of this is
1253 // volatile X*, and if the member function is declared const volatile,
1254 // the type of this is const volatile X*.
1256 assert(isInstance() && "No 'this' for static methods!");
1258 QualType ClassTy = C.getTypeDeclType(getParent());
1259 ClassTy = C.getQualifiedType(ClassTy,
1260 Qualifiers::fromCVRMask(getTypeQualifiers()));
1261 return C.getPointerType(ClassTy);
1264 bool CXXMethodDecl::hasInlineBody() const {
1265 // If this function is a template instantiation, look at the template from
1266 // which it was instantiated.
1267 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1271 const FunctionDecl *fn;
1272 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
1275 CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1276 TypeSourceInfo *TInfo, bool IsVirtual,
1277 SourceLocation L, Expr *Init,
1279 SourceLocation EllipsisLoc)
1280 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
1281 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false),
1282 SourceOrderOrNumArrayIndices(0)
1286 CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1288 SourceLocation MemberLoc,
1289 SourceLocation L, Expr *Init,
1291 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
1292 LParenLoc(L), RParenLoc(R), IsVirtual(false),
1293 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1297 CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1298 IndirectFieldDecl *Member,
1299 SourceLocation MemberLoc,
1300 SourceLocation L, Expr *Init,
1302 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
1303 LParenLoc(L), RParenLoc(R), IsVirtual(false),
1304 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1308 CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1309 SourceLocation D, SourceLocation L,
1310 CXXConstructorDecl *Target, Expr *Init,
1312 : Initializee(Target), MemberOrEllipsisLocation(D), Init(Init),
1313 LParenLoc(L), RParenLoc(R), IsVirtual(false),
1314 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1318 CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1320 SourceLocation MemberLoc,
1321 SourceLocation L, Expr *Init,
1324 unsigned NumIndices)
1325 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
1326 LParenLoc(L), RParenLoc(R), IsVirtual(false),
1327 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
1329 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1330 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1333 CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1335 SourceLocation MemberLoc,
1336 SourceLocation L, Expr *Init,
1339 unsigned NumIndices) {
1340 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
1341 sizeof(VarDecl *) * NumIndices,
1342 llvm::alignOf<CXXCtorInitializer>());
1343 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1344 Indices, NumIndices);
1347 TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
1348 if (isBaseInitializer())
1349 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
1354 const Type *CXXCtorInitializer::getBaseClass() const {
1355 if (isBaseInitializer())
1356 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
1361 SourceLocation CXXCtorInitializer::getSourceLocation() const {
1362 if (isAnyMemberInitializer() || isDelegatingInitializer())
1363 return getMemberLocation();
1365 if (isInClassMemberInitializer())
1366 return getAnyMember()->getLocation();
1368 return getBaseClassLoc().getLocalSourceRange().getBegin();
1371 SourceRange CXXCtorInitializer::getSourceRange() const {
1372 if (isInClassMemberInitializer()) {
1373 FieldDecl *D = getAnyMember();
1374 if (Expr *I = D->getInClassInitializer())
1375 return I->getSourceRange();
1376 return SourceRange();
1379 return SourceRange(getSourceLocation(), getRParenLoc());
1382 CXXConstructorDecl *
1383 CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
1384 return new (C) CXXConstructorDecl(0, SourceLocation(), DeclarationNameInfo(),
1385 QualType(), 0, false, false, false);
1388 CXXConstructorDecl *
1389 CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
1390 SourceLocation StartLoc,
1391 const DeclarationNameInfo &NameInfo,
1392 QualType T, TypeSourceInfo *TInfo,
1395 bool isImplicitlyDeclared) {
1396 assert(NameInfo.getName().getNameKind()
1397 == DeclarationName::CXXConstructorName &&
1398 "Name must refer to a constructor");
1399 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
1400 isExplicit, isInline, isImplicitlyDeclared);
1403 bool CXXConstructorDecl::isDefaultConstructor() const {
1404 // C++ [class.ctor]p5:
1405 // A default constructor for a class X is a constructor of class
1406 // X that can be called without an argument.
1407 return (getNumParams() == 0) ||
1408 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
1412 CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
1413 return isCopyOrMoveConstructor(TypeQuals) &&
1414 getParamDecl(0)->getType()->isLValueReferenceType();
1417 bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1418 return isCopyOrMoveConstructor(TypeQuals) &&
1419 getParamDecl(0)->getType()->isRValueReferenceType();
1422 /// \brief Determine whether this is a copy or move constructor.
1423 bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
1424 // C++ [class.copy]p2:
1425 // A non-template constructor for class X is a copy constructor
1426 // if its first parameter is of type X&, const X&, volatile X& or
1427 // const volatile X&, and either there are no other parameters
1428 // or else all other parameters have default arguments (8.3.6).
1429 // C++0x [class.copy]p3:
1430 // A non-template constructor for class X is a move constructor if its
1431 // first parameter is of type X&&, const X&&, volatile X&&, or
1432 // const volatile X&&, and either there are no other parameters or else
1433 // all other parameters have default arguments.
1434 if ((getNumParams() < 1) ||
1435 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1436 (getPrimaryTemplate() != 0) ||
1437 (getDescribedFunctionTemplate() != 0))
1440 const ParmVarDecl *Param = getParamDecl(0);
1442 // Do we have a reference type?
1443 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
1447 // Is it a reference to our class type?
1448 ASTContext &Context = getASTContext();
1450 CanQualType PointeeType
1451 = Context.getCanonicalType(ParamRefType->getPointeeType());
1453 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1454 if (PointeeType.getUnqualifiedType() != ClassTy)
1457 // FIXME: other qualifiers?
1459 // We have a copy or move constructor.
1460 TypeQuals = PointeeType.getCVRQualifiers();
1464 bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
1465 // C++ [class.conv.ctor]p1:
1466 // A constructor declared without the function-specifier explicit
1467 // that can be called with a single parameter specifies a
1468 // conversion from the type of its first parameter to the type of
1469 // its class. Such a constructor is called a converting
1471 if (isExplicit() && !AllowExplicit)
1474 return (getNumParams() == 0 &&
1475 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
1476 (getNumParams() == 1) ||
1477 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
1480 bool CXXConstructorDecl::isSpecializationCopyingObject() const {
1481 if ((getNumParams() < 1) ||
1482 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1483 (getPrimaryTemplate() == 0) ||
1484 (getDescribedFunctionTemplate() != 0))
1487 const ParmVarDecl *Param = getParamDecl(0);
1489 ASTContext &Context = getASTContext();
1490 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1492 // Is it the same as our our class type?
1494 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1495 if (ParamType.getUnqualifiedType() != ClassTy)
1501 const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1502 // Hack: we store the inherited constructor in the overridden method table
1503 method_iterator It = begin_overridden_methods();
1504 if (It == end_overridden_methods())
1507 return cast<CXXConstructorDecl>(*It);
1511 CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1512 // Hack: we store the inherited constructor in the overridden method table
1513 assert(size_overridden_methods() == 0 && "Base ctor already set.");
1514 addOverriddenMethod(BaseCtor);
1518 CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
1519 return new (C) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
1520 QualType(), 0, false, false);
1524 CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
1525 SourceLocation StartLoc,
1526 const DeclarationNameInfo &NameInfo,
1527 QualType T, TypeSourceInfo *TInfo,
1529 bool isImplicitlyDeclared) {
1530 assert(NameInfo.getName().getNameKind()
1531 == DeclarationName::CXXDestructorName &&
1532 "Name must refer to a destructor");
1533 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
1534 isImplicitlyDeclared);
1538 CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
1539 return new (C) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
1540 QualType(), 0, false, false,
1545 CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
1546 SourceLocation StartLoc,
1547 const DeclarationNameInfo &NameInfo,
1548 QualType T, TypeSourceInfo *TInfo,
1549 bool isInline, bool isExplicit,
1550 SourceLocation EndLocation) {
1551 assert(NameInfo.getName().getNameKind()
1552 == DeclarationName::CXXConversionFunctionName &&
1553 "Name must refer to a conversion function");
1554 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
1555 isInline, isExplicit, EndLocation);
1558 LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
1560 SourceLocation ExternLoc,
1561 SourceLocation LangLoc,
1563 SourceLocation RBraceLoc) {
1564 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
1567 UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1569 SourceLocation NamespaceLoc,
1570 NestedNameSpecifierLoc QualifierLoc,
1571 SourceLocation IdentLoc,
1573 DeclContext *CommonAncestor) {
1574 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1575 Used = NS->getOriginalNamespace();
1576 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1577 IdentLoc, Used, CommonAncestor);
1580 NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1581 if (NamespaceAliasDecl *NA =
1582 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1583 return NA->getNamespace();
1584 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1587 NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
1588 SourceLocation UsingLoc,
1589 SourceLocation AliasLoc,
1590 IdentifierInfo *Alias,
1591 NestedNameSpecifierLoc QualifierLoc,
1592 SourceLocation IdentLoc,
1593 NamedDecl *Namespace) {
1594 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1595 Namespace = NS->getOriginalNamespace();
1596 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1597 QualifierLoc, IdentLoc, Namespace);
1600 UsingDecl *UsingShadowDecl::getUsingDecl() const {
1601 const UsingShadowDecl *Shadow = this;
1602 while (const UsingShadowDecl *NextShadow =
1603 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1604 Shadow = NextShadow;
1605 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1608 void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1609 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1610 "declaration already in set");
1611 assert(S->getUsingDecl() == this);
1613 if (FirstUsingShadow)
1614 S->UsingOrNextShadow = FirstUsingShadow;
1615 FirstUsingShadow = S;
1618 void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1619 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1620 "declaration not in set");
1621 assert(S->getUsingDecl() == this);
1623 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1625 if (FirstUsingShadow == S) {
1626 FirstUsingShadow = dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow);
1627 S->UsingOrNextShadow = this;
1631 UsingShadowDecl *Prev = FirstUsingShadow;
1632 while (Prev->UsingOrNextShadow != S)
1633 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1634 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1635 S->UsingOrNextShadow = this;
1638 UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1639 NestedNameSpecifierLoc QualifierLoc,
1640 const DeclarationNameInfo &NameInfo,
1641 bool IsTypeNameArg) {
1642 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
1645 UnresolvedUsingValueDecl *
1646 UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1647 SourceLocation UsingLoc,
1648 NestedNameSpecifierLoc QualifierLoc,
1649 const DeclarationNameInfo &NameInfo) {
1650 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
1651 QualifierLoc, NameInfo);
1654 UnresolvedUsingTypenameDecl *
1655 UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1656 SourceLocation UsingLoc,
1657 SourceLocation TypenameLoc,
1658 NestedNameSpecifierLoc QualifierLoc,
1659 SourceLocation TargetNameLoc,
1660 DeclarationName TargetName) {
1661 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
1662 QualifierLoc, TargetNameLoc,
1663 TargetName.getAsIdentifierInfo());
1666 StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
1667 SourceLocation StaticAssertLoc,
1669 StringLiteral *Message,
1670 SourceLocation RParenLoc) {
1671 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
1675 static const char *getAccessName(AccessSpecifier AS) {
1679 assert("Invalid access specifier!");
1690 const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1691 AccessSpecifier AS) {
1692 return DB << getAccessName(AS);