Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1 | //===--- 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" |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclTemplate.h" |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Argyrios Kyrtzidis | 7b90340 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTMutationListener.h" |
Douglas Gregor | 7a39dd0 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 18 | #include "clang/AST/CXXInheritance.h" |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 20 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | 7d7e672 | 2008-11-12 23:21:09 +0000 | [diff] [blame] | 21 | #include "clang/Basic/IdentifierTable.h" |
Douglas Gregor | fdfab6b | 2008-12-23 21:31:30 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/STLExtras.h" |
Fariborz Jahanian | faebcbb | 2009-09-12 19:52:10 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallPtrSet.h" |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | // Decl Allocation/Deallocation Method Implementations |
| 28 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 29 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 30 | CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D) |
| 31 | : UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false), |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 32 | UserDeclaredCopyAssignment(false), UserDeclaredDestructor(false), |
Eli Friedman | 97c134e | 2009-08-15 22:23:00 +0000 | [diff] [blame] | 33 | Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false), |
Benjamin Kramer | 6e35b65 | 2011-04-30 08:55:35 +0000 | [diff] [blame^] | 34 | Abstract(false), HasStandardLayout(true), HasTrivialConstructor(true), |
Chandler Carruth | 9b6347c | 2011-04-24 02:49:34 +0000 | [diff] [blame] | 35 | HasConstExprNonCopyMoveConstructor(false), |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 36 | HasTrivialCopyConstructor(true), HasTrivialMoveConstructor(true), |
| 37 | HasTrivialCopyAssignment(true), HasTrivialMoveAssignment(true), |
Chandler Carruth | 9b6347c | 2011-04-24 02:49:34 +0000 | [diff] [blame] | 38 | HasTrivialDestructor(true), HasNonLiteralTypeFieldsOrBases(false), |
| 39 | ComputedVisibleConversions(false), |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 40 | DeclaredDefaultConstructor(false), DeclaredCopyConstructor(false), |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 41 | DeclaredCopyAssignment(false), DeclaredDestructor(false), |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 42 | NumBases(0), NumVBases(0), Bases(), VBases(), |
| 43 | Definition(D), FirstFriend(0) { |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 47 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 48 | IdentifierInfo *Id, CXXRecordDecl *PrevDecl) |
| 49 | : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl), |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 50 | DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0), |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 51 | TemplateOrInstantiation() { } |
Douglas Gregor | 7d7e672 | 2008-11-12 23:21:09 +0000 | [diff] [blame] | 52 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 53 | CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 54 | DeclContext *DC, SourceLocation StartLoc, |
| 55 | SourceLocation IdLoc, IdentifierInfo *Id, |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 56 | CXXRecordDecl* PrevDecl, |
| 57 | bool DelayTypeCreation) { |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 58 | CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc, |
| 59 | Id, PrevDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 60 | |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 61 | // FIXME: DelayTypeCreation seems like such a hack |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 62 | if (!DelayTypeCreation) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 63 | C.getTypeDeclType(R, PrevDecl); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 64 | return R; |
| 65 | } |
| 66 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 67 | CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, EmptyShell Empty) { |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 68 | return new (C) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(), |
| 69 | SourceLocation(), 0, 0); |
Argyrios Kyrtzidis | b8b03e6 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 72 | void |
Douglas Gregor | 2d5b703 | 2010-02-11 01:30:34 +0000 | [diff] [blame] | 73 | CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases, |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 74 | unsigned NumBases) { |
Douglas Gregor | 2d5b703 | 2010-02-11 01:30:34 +0000 | [diff] [blame] | 75 | ASTContext &C = getASTContext(); |
| 76 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 77 | // C++ [dcl.init.aggr]p1: |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 78 | // An aggregate is an array or a class (clause 9) with [...] |
| 79 | // no base classes [...]. |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 80 | data().Aggregate = false; |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 81 | |
Douglas Gregor | 7c789c1 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 82 | if (!data().Bases.isOffset() && data().NumBases > 0) |
| 83 | C.Deallocate(data().getBases()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 84 | |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 85 | // The set of seen virtual base types. |
Anders Carlsson | 1c36393 | 2010-03-29 19:49:09 +0000 | [diff] [blame] | 86 | llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes; |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 87 | |
| 88 | // The virtual bases of this class. |
| 89 | llvm::SmallVector<const CXXBaseSpecifier *, 8> VBases; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 90 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 91 | data().Bases = new(C) CXXBaseSpecifier [NumBases]; |
| 92 | data().NumBases = NumBases; |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 93 | for (unsigned i = 0; i < NumBases; ++i) { |
Douglas Gregor | 7c789c1 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 94 | data().getBases()[i] = *Bases[i]; |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 95 | // Keep track of inherited vbases for this base class. |
| 96 | const CXXBaseSpecifier *Base = Bases[i]; |
| 97 | QualType BaseType = Base->getType(); |
Douglas Gregor | 5fe8c04 | 2010-02-27 00:25:28 +0000 | [diff] [blame] | 98 | // Skip dependent types; we can't do any checking on them now. |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 99 | if (BaseType->isDependentType()) |
| 100 | continue; |
| 101 | CXXRecordDecl *BaseClassDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 102 | = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()); |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 103 | |
Douglas Gregor | 9fe183a | 2010-09-28 19:45:33 +0000 | [diff] [blame] | 104 | // C++ [dcl.init.aggr]p1: |
| 105 | // An aggregate is [...] a class with [...] no base classes [...]. |
| 106 | data().Aggregate = false; |
| 107 | |
| 108 | // C++ [class]p4: |
| 109 | // A POD-struct is an aggregate class... |
| 110 | data().PlainOldData = false; |
| 111 | |
Douglas Gregor | 2cf9d65 | 2010-09-28 20:38:10 +0000 | [diff] [blame] | 112 | // A class with a non-empty base class is not empty. |
| 113 | // FIXME: Standard ref? |
| 114 | if (!BaseClassDecl->isEmpty()) |
| 115 | data().Empty = false; |
| 116 | |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 117 | // C++ [class.virtual]p1: |
| 118 | // A class that declares or inherits a virtual function is called a |
| 119 | // polymorphic class. |
| 120 | if (BaseClassDecl->isPolymorphic()) |
| 121 | data().Polymorphic = true; |
Chandler Carruth | 9b6347c | 2011-04-24 02:49:34 +0000 | [diff] [blame] | 122 | |
| 123 | // Record if this base is the first non-literal field or base. |
| 124 | if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType()) |
| 125 | data().HasNonLiteralTypeFieldsOrBases = true; |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 126 | |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 127 | // Now go through all virtual bases of this base and add them. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 128 | for (CXXRecordDecl::base_class_iterator VBase = |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 129 | BaseClassDecl->vbases_begin(), |
| 130 | E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) { |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 131 | // Add this base if it's not already in the list. |
Anders Carlsson | 1c36393 | 2010-03-29 19:49:09 +0000 | [diff] [blame] | 132 | if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType()))) |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 133 | VBases.push_back(VBase); |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 134 | } |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 135 | |
| 136 | if (Base->isVirtual()) { |
| 137 | // Add this base if it's not already in the list. |
Anders Carlsson | 1c36393 | 2010-03-29 19:49:09 +0000 | [diff] [blame] | 138 | if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType))) |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 139 | VBases.push_back(Base); |
Douglas Gregor | 2cf9d65 | 2010-09-28 20:38:10 +0000 | [diff] [blame] | 140 | |
| 141 | // C++0x [meta.unary.prop] is_empty: |
| 142 | // T is a class type, but not a union type, with ... no virtual base |
| 143 | // classes |
| 144 | data().Empty = false; |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 145 | |
| 146 | // C++ [class.ctor]p5: |
| 147 | // A constructor is trivial if its class has no virtual base classes. |
| 148 | data().HasTrivialConstructor = false; |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 149 | |
| 150 | // C++0x [class.copy]p13: |
| 151 | // A copy/move constructor for class X is trivial if it is neither |
| 152 | // user-provided nor deleted and if |
| 153 | // -- class X has no virtual functions and no virtual base classes, and |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 154 | data().HasTrivialCopyConstructor = false; |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 155 | data().HasTrivialMoveConstructor = false; |
| 156 | |
| 157 | // C++0x [class.copy]p27: |
| 158 | // A copy/move assignment operator for class X is trivial if it is |
| 159 | // neither user-provided nor deleted and if |
| 160 | // -- class X has no virtual functions and no virtual base classes, and |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 161 | data().HasTrivialCopyAssignment = false; |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 162 | data().HasTrivialMoveAssignment = false; |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 163 | } else { |
| 164 | // C++ [class.ctor]p5: |
| 165 | // A constructor is trivial if all the direct base classes of its |
| 166 | // class have trivial constructors. |
| 167 | if (!BaseClassDecl->hasTrivialConstructor()) |
| 168 | data().HasTrivialConstructor = false; |
| 169 | |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 170 | // C++0x [class.copy]p13: |
| 171 | // A copy/move constructor for class X is trivial if [...] |
| 172 | // [...] |
| 173 | // -- the constructor selected to copy/move each direct base class |
| 174 | // subobject is trivial, and |
| 175 | // FIXME: C++0x: We need to only consider the selected constructor |
| 176 | // instead of all of them. |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 177 | if (!BaseClassDecl->hasTrivialCopyConstructor()) |
| 178 | data().HasTrivialCopyConstructor = false; |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 179 | if (!BaseClassDecl->hasTrivialMoveConstructor()) |
| 180 | data().HasTrivialMoveConstructor = false; |
| 181 | |
| 182 | // C++0x [class.copy]p27: |
| 183 | // A copy/move assignment operator for class X is trivial if [...] |
| 184 | // [...] |
| 185 | // -- the assignment operator selected to copy/move each direct base |
| 186 | // class subobject is trivial, and |
| 187 | // FIXME: C++0x: We need to only consider the selected operator instead |
| 188 | // of all of them. |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 189 | if (!BaseClassDecl->hasTrivialCopyAssignment()) |
| 190 | data().HasTrivialCopyAssignment = false; |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 191 | if (!BaseClassDecl->hasTrivialMoveAssignment()) |
| 192 | data().HasTrivialMoveAssignment = false; |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 193 | } |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 194 | |
| 195 | // C++ [class.ctor]p3: |
| 196 | // A destructor is trivial if all the direct base classes of its class |
| 197 | // have trivial destructors. |
| 198 | if (!BaseClassDecl->hasTrivialDestructor()) |
| 199 | data().HasTrivialDestructor = false; |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 200 | } |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 201 | |
| 202 | if (VBases.empty()) |
| 203 | return; |
| 204 | |
| 205 | // Create base specifier for any direct or indirect virtual bases. |
| 206 | data().VBases = new (C) CXXBaseSpecifier[VBases.size()]; |
| 207 | data().NumVBases = VBases.size(); |
| 208 | for (int I = 0, E = VBases.size(); I != E; ++I) { |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 209 | TypeSourceInfo *VBaseTypeInfo = VBases[I]->getTypeSourceInfo(); |
| 210 | |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 211 | // Skip dependent types; we can't do any checking on them now. |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 212 | if (VBaseTypeInfo->getType()->isDependentType()) |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 213 | continue; |
| 214 | |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 215 | CXXRecordDecl *VBaseClassDecl = cast<CXXRecordDecl>( |
| 216 | VBaseTypeInfo->getType()->getAs<RecordType>()->getDecl()); |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 217 | |
Douglas Gregor | 7c789c1 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 218 | data().getVBases()[I] = |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 219 | CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true, |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 220 | VBaseClassDecl->getTagKind() == TTK_Class, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 221 | VBases[I]->getAccessSpecifier(), VBaseTypeInfo, |
| 222 | SourceLocation()); |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 223 | } |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 226 | /// Callback function for CXXRecordDecl::forallBases that acknowledges |
| 227 | /// that it saw a base class. |
| 228 | static bool SawBase(const CXXRecordDecl *, void *) { |
| 229 | return true; |
| 230 | } |
| 231 | |
| 232 | bool CXXRecordDecl::hasAnyDependentBases() const { |
| 233 | if (!isDependentContext()) |
| 234 | return false; |
| 235 | |
| 236 | return !forallBases(SawBase, 0); |
| 237 | } |
| 238 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 239 | bool CXXRecordDecl::hasConstCopyConstructor(const ASTContext &Context) const { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 240 | return getCopyConstructor(Context, Qualifiers::Const) != 0; |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Chandler Carruth | b7e9589 | 2011-04-23 10:47:28 +0000 | [diff] [blame] | 243 | bool CXXRecordDecl::isTriviallyCopyable() const { |
| 244 | // C++0x [class]p5: |
| 245 | // A trivially copyable class is a class that: |
| 246 | // -- has no non-trivial copy constructors, |
| 247 | if (!hasTrivialCopyConstructor()) return false; |
| 248 | // -- has no non-trivial move constructors, |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 249 | if (!hasTrivialMoveConstructor()) return false; |
Chandler Carruth | b7e9589 | 2011-04-23 10:47:28 +0000 | [diff] [blame] | 250 | // -- has no non-trivial copy assignment operators, |
| 251 | if (!hasTrivialCopyAssignment()) return false; |
| 252 | // -- has no non-trivial move assignment operators, and |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 253 | if (!hasTrivialMoveAssignment()) return false; |
Chandler Carruth | b7e9589 | 2011-04-23 10:47:28 +0000 | [diff] [blame] | 254 | // -- has a trivial destructor. |
| 255 | if (!hasTrivialDestructor()) return false; |
| 256 | |
| 257 | return true; |
| 258 | } |
| 259 | |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 260 | /// \brief Perform a simplistic form of overload resolution that only considers |
| 261 | /// cv-qualifiers on a single parameter, and return the best overload candidate |
| 262 | /// (if there is one). |
| 263 | static CXXMethodDecl * |
| 264 | GetBestOverloadCandidateSimple( |
| 265 | const llvm::SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) { |
| 266 | if (Cands.empty()) |
| 267 | return 0; |
| 268 | if (Cands.size() == 1) |
| 269 | return Cands[0].first; |
| 270 | |
| 271 | unsigned Best = 0, N = Cands.size(); |
| 272 | for (unsigned I = 1; I != N; ++I) |
Douglas Gregor | 61d0b6b | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 273 | if (Cands[Best].second.compatiblyIncludes(Cands[I].second)) |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 274 | Best = I; |
| 275 | |
| 276 | for (unsigned I = 1; I != N; ++I) |
Douglas Gregor | 61d0b6b | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 277 | if (Cands[Best].second.compatiblyIncludes(Cands[I].second)) |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 278 | return 0; |
| 279 | |
| 280 | return Cands[Best].first; |
| 281 | } |
| 282 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 283 | CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(const ASTContext &Context, |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 284 | unsigned TypeQuals) const{ |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 285 | QualType ClassType |
| 286 | = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 287 | DeclarationName ConstructorName |
Douglas Gregor | 9e7d9de | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 288 | = Context.DeclarationNames.getCXXConstructorName( |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 289 | Context.getCanonicalType(ClassType)); |
| 290 | unsigned FoundTQs; |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 291 | llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found; |
Douglas Gregor | fdfab6b | 2008-12-23 21:31:30 +0000 | [diff] [blame] | 292 | DeclContext::lookup_const_iterator Con, ConEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 293 | for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName); |
Douglas Gregor | fdfab6b | 2008-12-23 21:31:30 +0000 | [diff] [blame] | 294 | Con != ConEnd; ++Con) { |
Douglas Gregor | d93bacf | 2009-09-04 14:46:39 +0000 | [diff] [blame] | 295 | // C++ [class.copy]p2: |
| 296 | // A non-template constructor for class X is a copy constructor if [...] |
| 297 | if (isa<FunctionTemplateDecl>(*Con)) |
| 298 | continue; |
| 299 | |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 300 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); |
| 301 | if (Constructor->isCopyConstructor(FoundTQs)) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 302 | if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) || |
| 303 | (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const))) |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 304 | Found.push_back(std::make_pair( |
| 305 | const_cast<CXXConstructorDecl *>(Constructor), |
| 306 | Qualifiers::fromCVRMask(FoundTQs))); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 307 | } |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 308 | } |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 309 | |
| 310 | return cast_or_null<CXXConstructorDecl>( |
| 311 | GetBestOverloadCandidateSimple(Found)); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 314 | CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const { |
| 315 | ASTContext &Context = getASTContext(); |
| 316 | QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this)); |
| 317 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 318 | |
| 319 | llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found; |
| 320 | DeclContext::lookup_const_iterator Op, OpEnd; |
| 321 | for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) { |
| 322 | // C++ [class.copy]p9: |
| 323 | // A user-declared copy assignment operator is a non-static non-template |
| 324 | // member function of class X with exactly one parameter of type X, X&, |
| 325 | // const X&, volatile X& or const volatile X&. |
| 326 | const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op); |
| 327 | if (!Method || Method->isStatic() || Method->getPrimaryTemplate()) |
| 328 | continue; |
| 329 | |
| 330 | const FunctionProtoType *FnType |
| 331 | = Method->getType()->getAs<FunctionProtoType>(); |
| 332 | assert(FnType && "Overloaded operator has no prototype."); |
| 333 | // Don't assert on this; an invalid decl might have been left in the AST. |
| 334 | if (FnType->getNumArgs() != 1 || FnType->isVariadic()) |
| 335 | continue; |
| 336 | |
| 337 | QualType ArgType = FnType->getArgType(0); |
| 338 | Qualifiers Quals; |
| 339 | if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) { |
| 340 | ArgType = Ref->getPointeeType(); |
| 341 | // If we have a const argument and we have a reference to a non-const, |
| 342 | // this function does not match. |
| 343 | if (ArgIsConst && !ArgType.isConstQualified()) |
| 344 | continue; |
| 345 | |
| 346 | Quals = ArgType.getQualifiers(); |
| 347 | } else { |
| 348 | // By-value copy-assignment operators are treated like const X& |
| 349 | // copy-assignment operators. |
| 350 | Quals = Qualifiers::fromCVRMask(Qualifiers::Const); |
| 351 | } |
| 352 | |
| 353 | if (!Context.hasSameUnqualifiedType(ArgType, Class)) |
| 354 | continue; |
| 355 | |
| 356 | // Save this copy-assignment operator. It might be "the one". |
| 357 | Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals)); |
| 358 | } |
| 359 | |
| 360 | // Use a simplistic form of overload resolution to find the candidate. |
| 361 | return GetBestOverloadCandidateSimple(Found); |
| 362 | } |
| 363 | |
Douglas Gregor | 2138664 | 2010-09-28 21:55:22 +0000 | [diff] [blame] | 364 | void CXXRecordDecl::markedVirtualFunctionPure() { |
| 365 | // C++ [class.abstract]p2: |
| 366 | // A class is abstract if it has at least one pure virtual function. |
| 367 | data().Abstract = true; |
| 368 | } |
| 369 | |
| 370 | void CXXRecordDecl::addedMember(Decl *D) { |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 371 | // Ignore friends and invalid declarations. |
| 372 | if (D->getFriendObjectKind() || D->isInvalidDecl()) |
Douglas Gregor | 5c0646b | 2010-09-27 21:17:54 +0000 | [diff] [blame] | 373 | return; |
| 374 | |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 375 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D); |
| 376 | if (FunTmpl) |
| 377 | D = FunTmpl->getTemplatedDecl(); |
| 378 | |
Douglas Gregor | 9fe183a | 2010-09-28 19:45:33 +0000 | [diff] [blame] | 379 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 380 | if (Method->isVirtual()) { |
| 381 | // C++ [dcl.init.aggr]p1: |
| 382 | // An aggregate is an array or a class with [...] no virtual functions. |
| 383 | data().Aggregate = false; |
| 384 | |
| 385 | // C++ [class]p4: |
| 386 | // A POD-struct is an aggregate class... |
| 387 | data().PlainOldData = false; |
Douglas Gregor | 2cf9d65 | 2010-09-28 20:38:10 +0000 | [diff] [blame] | 388 | |
| 389 | // Virtual functions make the class non-empty. |
| 390 | // FIXME: Standard ref? |
| 391 | data().Empty = false; |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 392 | |
| 393 | // C++ [class.virtual]p1: |
| 394 | // A class that declares or inherits a virtual function is called a |
| 395 | // polymorphic class. |
| 396 | data().Polymorphic = true; |
| 397 | |
| 398 | // None of the special member functions are trivial. |
| 399 | data().HasTrivialConstructor = false; |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 400 | |
| 401 | // C++0x [class.copy]p13: |
| 402 | // A copy/move constructor for class X is trivial if [...] |
| 403 | // -- class X has no virtual functions [...] |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 404 | data().HasTrivialCopyConstructor = false; |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 405 | data().HasTrivialMoveConstructor = false; |
| 406 | |
| 407 | // C++0x [class.copy]p27: |
| 408 | // A copy/move assignment operator for class X is trivial if [...] |
| 409 | // -- class X has no virtual functions [...] |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 410 | data().HasTrivialCopyAssignment = false; |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 411 | data().HasTrivialMoveAssignment = false; |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 412 | // FIXME: Destructor? |
Douglas Gregor | 9fe183a | 2010-09-28 19:45:33 +0000 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 416 | if (D->isImplicit()) { |
Argyrios Kyrtzidis | b6cc0e1 | 2010-10-24 17:26:54 +0000 | [diff] [blame] | 417 | // Notify that an implicit member was added after the definition |
| 418 | // was completed. |
| 419 | if (!isBeingDefined()) |
| 420 | if (ASTMutationListener *L = getASTMutationListener()) |
| 421 | L->AddedCXXImplicitMember(data().Definition, D); |
Argyrios Kyrtzidis | 046c03b | 2010-10-20 23:48:42 +0000 | [diff] [blame] | 422 | |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 423 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
| 424 | // If this is the implicit default constructor, note that we have now |
| 425 | // declared it. |
| 426 | if (Constructor->isDefaultConstructor()) |
| 427 | data().DeclaredDefaultConstructor = true; |
| 428 | // If this is the implicit copy constructor, note that we have now |
| 429 | // declared it. |
| 430 | else if (Constructor->isCopyConstructor()) |
| 431 | data().DeclaredCopyConstructor = true; |
Douglas Gregor | 2cf9d65 | 2010-09-28 20:38:10 +0000 | [diff] [blame] | 432 | return; |
| 433 | } |
| 434 | |
| 435 | if (isa<CXXDestructorDecl>(D)) { |
Douglas Gregor | 0ed2e08 | 2010-09-27 22:48:58 +0000 | [diff] [blame] | 436 | data().DeclaredDestructor = true; |
Douglas Gregor | 2cf9d65 | 2010-09-28 20:38:10 +0000 | [diff] [blame] | 437 | return; |
| 438 | } |
| 439 | |
| 440 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 441 | // If this is the implicit copy constructor, note that we have now |
| 442 | // declared it. |
| 443 | // FIXME: Move constructors |
Douglas Gregor | 3e9438b | 2010-09-27 22:37:28 +0000 | [diff] [blame] | 444 | if (Method->getOverloadedOperator() == OO_Equal) |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 445 | data().DeclaredCopyAssignment = true; |
Douglas Gregor | 2cf9d65 | 2010-09-28 20:38:10 +0000 | [diff] [blame] | 446 | return; |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 447 | } |
Douglas Gregor | 2cf9d65 | 2010-09-28 20:38:10 +0000 | [diff] [blame] | 448 | |
| 449 | // Any other implicit declarations are handled like normal declarations. |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | // Handle (user-declared) constructors. |
| 453 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
| 454 | // Note that we have a user-declared constructor. |
| 455 | data().UserDeclaredConstructor = true; |
| 456 | |
| 457 | // Note that we have no need of an implicitly-declared default constructor. |
| 458 | data().DeclaredDefaultConstructor = true; |
| 459 | |
| 460 | // C++ [dcl.init.aggr]p1: |
| 461 | // An aggregate is an array or a class (clause 9) with no |
| 462 | // user-declared constructors (12.1) [...]. |
| 463 | data().Aggregate = false; |
| 464 | |
| 465 | // C++ [class]p4: |
| 466 | // A POD-struct is an aggregate class [...] |
| 467 | data().PlainOldData = false; |
| 468 | |
| 469 | // C++ [class.ctor]p5: |
| 470 | // A constructor is trivial if it is an implicitly-declared default |
| 471 | // constructor. |
| 472 | // FIXME: C++0x: don't do this for "= default" default constructors. |
| 473 | data().HasTrivialConstructor = false; |
| 474 | |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 475 | // Note when we have a user-declared copy or move constructor, which will |
| 476 | // suppress the implicit declaration of those constructors. |
| 477 | if (!FunTmpl) { |
| 478 | if (Constructor->isCopyConstructor()) { |
| 479 | data().UserDeclaredCopyConstructor = true; |
| 480 | data().DeclaredCopyConstructor = true; |
| 481 | |
| 482 | // C++0x [class.copy]p13: |
| 483 | // A copy/move constructor for class X is trivial if it is neither |
| 484 | // user-provided nor deleted |
| 485 | // FIXME: C++0x: don't do this for "= default" copy constructors. |
| 486 | data().HasTrivialCopyConstructor = false; |
| 487 | } else if (Constructor->isMoveConstructor()) { |
| 488 | // C++0x [class.copy]p13: |
| 489 | // A copy/move constructor for class X is trivial if it is neither |
| 490 | // user-provided nor deleted |
| 491 | // FIXME: C++0x: don't do this for "= default" move constructors. |
| 492 | data().HasTrivialMoveConstructor = false; |
| 493 | } |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 494 | } |
Chandler Carruth | 9b6347c | 2011-04-24 02:49:34 +0000 | [diff] [blame] | 495 | if (Constructor->isConstExpr() && |
| 496 | !Constructor->isCopyOrMoveConstructor()) { |
| 497 | // Record if we see any constexpr constructors which are niether copy |
| 498 | // nor move constructors. |
| 499 | data().HasConstExprNonCopyMoveConstructor = true; |
| 500 | } |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 501 | |
Douglas Gregor | 5c0646b | 2010-09-27 21:17:54 +0000 | [diff] [blame] | 502 | return; |
| 503 | } |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 504 | |
Douglas Gregor | 0ed2e08 | 2010-09-27 22:48:58 +0000 | [diff] [blame] | 505 | // Handle (user-declared) destructors. |
| 506 | if (isa<CXXDestructorDecl>(D)) { |
| 507 | data().DeclaredDestructor = true; |
| 508 | data().UserDeclaredDestructor = true; |
Douglas Gregor | 9fe183a | 2010-09-28 19:45:33 +0000 | [diff] [blame] | 509 | |
| 510 | // C++ [class]p4: |
| 511 | // A POD-struct is an aggregate class that has [...] no user-defined |
| 512 | // destructor. |
| 513 | data().PlainOldData = false; |
| 514 | |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 515 | // C++ [class.dtor]p3: |
| 516 | // A destructor is trivial if it is an implicitly-declared destructor and |
| 517 | // [...]. |
| 518 | // |
| 519 | // FIXME: C++0x: don't do this for "= default" destructors |
| 520 | data().HasTrivialDestructor = false; |
| 521 | |
Douglas Gregor | 0ed2e08 | 2010-09-27 22:48:58 +0000 | [diff] [blame] | 522 | return; |
| 523 | } |
Douglas Gregor | 5c0646b | 2010-09-27 21:17:54 +0000 | [diff] [blame] | 524 | |
Douglas Gregor | 0ed2e08 | 2010-09-27 22:48:58 +0000 | [diff] [blame] | 525 | // Handle (user-declared) member functions. |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 526 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 527 | if (Method->getOverloadedOperator() == OO_Equal) { |
| 528 | // We're interested specifically in copy assignment operators. |
| 529 | const FunctionProtoType *FnType |
| 530 | = Method->getType()->getAs<FunctionProtoType>(); |
| 531 | assert(FnType && "Overloaded operator has no proto function type."); |
| 532 | assert(FnType->getNumArgs() == 1 && !FnType->isVariadic()); |
| 533 | |
| 534 | // Copy assignment operators must be non-templates. |
| 535 | if (Method->getPrimaryTemplate() || FunTmpl) |
| 536 | return; |
| 537 | |
| 538 | ASTContext &Context = getASTContext(); |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 539 | QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType( |
| 540 | const_cast<CXXRecordDecl*>(this))); |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 541 | |
| 542 | bool isRValueRefArg = false; |
| 543 | QualType ArgType = FnType->getArgType(0); |
| 544 | if (const LValueReferenceType *Ref = |
| 545 | ArgType->getAs<LValueReferenceType>()) { |
| 546 | ArgType = Ref->getPointeeType(); |
| 547 | } else if (const RValueReferenceType *Ref = |
| 548 | ArgType->getAs<RValueReferenceType>()) { |
| 549 | ArgType = Ref->getPointeeType(); |
| 550 | isRValueRefArg = true; |
| 551 | } |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 552 | if (!Context.hasSameUnqualifiedType(ClassType, ArgType)) |
| 553 | return; |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 554 | |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 555 | // C++ [class]p4: |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 556 | // A POD-struct is an aggregate class that [...] has no user-defined |
| 557 | // copy assignment operator [...]. |
| 558 | // FIXME: This should be probably determined dynamically in terms of |
| 559 | // other more precise attributes to correctly model how it is specified |
| 560 | // in C++0x. Setting it here happens to do the right thing. |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 561 | data().PlainOldData = false; |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 562 | |
| 563 | if (!isRValueRefArg) { |
| 564 | // This is a copy assignment operator. |
| 565 | |
| 566 | // Suppress the implicit declaration of a copy constructor. |
| 567 | data().UserDeclaredCopyAssignment = true; |
| 568 | data().DeclaredCopyAssignment = true; |
| 569 | |
| 570 | // C++0x [class.copy]p27: |
| 571 | // A copy/move assignment operator for class X is trivial if it is |
| 572 | // neither user-provided nor deleted [...] |
| 573 | // FIXME: C++0x: don't do this for "= default" copy operators. |
| 574 | data().HasTrivialCopyAssignment = false; |
| 575 | } else { |
| 576 | // This is a move assignment operator. |
| 577 | |
| 578 | // C++0x [class.copy]p27: |
| 579 | // A copy/move assignment operator for class X is trivial if it is |
| 580 | // neither user-provided nor deleted [...] |
| 581 | // FIXME: C++0x: don't do this for "= default" copy operators. |
| 582 | data().HasTrivialMoveAssignment = false; |
| 583 | } |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 584 | } |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 585 | |
Douglas Gregor | e80622f | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 586 | // Keep the list of conversion functions up-to-date. |
| 587 | if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) { |
| 588 | // We don't record specializations. |
| 589 | if (Conversion->getPrimaryTemplate()) |
| 590 | return; |
| 591 | |
| 592 | // FIXME: We intentionally don't use the decl's access here because it |
| 593 | // hasn't been set yet. That's really just a misdesign in Sema. |
| 594 | |
| 595 | if (FunTmpl) { |
| 596 | if (FunTmpl->getPreviousDeclaration()) |
| 597 | data().Conversions.replace(FunTmpl->getPreviousDeclaration(), |
| 598 | FunTmpl); |
| 599 | else |
| 600 | data().Conversions.addDecl(FunTmpl); |
| 601 | } else { |
| 602 | if (Conversion->getPreviousDeclaration()) |
| 603 | data().Conversions.replace(Conversion->getPreviousDeclaration(), |
| 604 | Conversion); |
| 605 | else |
| 606 | data().Conversions.addDecl(Conversion); |
| 607 | } |
| 608 | } |
| 609 | |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 610 | return; |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 611 | } |
Douglas Gregor | 9fe183a | 2010-09-28 19:45:33 +0000 | [diff] [blame] | 612 | |
| 613 | // Handle non-static data members. |
| 614 | if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) { |
| 615 | // C++ [dcl.init.aggr]p1: |
| 616 | // An aggregate is an array or a class (clause 9) with [...] no |
| 617 | // private or protected non-static data members (clause 11). |
| 618 | // |
| 619 | // A POD must be an aggregate. |
| 620 | if (D->getAccess() == AS_private || D->getAccess() == AS_protected) { |
| 621 | data().Aggregate = false; |
| 622 | data().PlainOldData = false; |
| 623 | } |
| 624 | |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 625 | // C++0x [class]p9: |
Douglas Gregor | 9fe183a | 2010-09-28 19:45:33 +0000 | [diff] [blame] | 626 | // A POD struct is a class that is both a trivial class and a |
| 627 | // standard-layout class, and has no non-static data members of type |
| 628 | // non-POD struct, non-POD union (or array of such types). |
| 629 | ASTContext &Context = getASTContext(); |
| 630 | QualType T = Context.getBaseElementType(Field->getType()); |
| 631 | if (!T->isPODType()) |
| 632 | data().PlainOldData = false; |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 633 | if (T->isReferenceType()) |
| 634 | data().HasTrivialConstructor = false; |
Chandler Carruth | 9b6347c | 2011-04-24 02:49:34 +0000 | [diff] [blame] | 635 | |
| 636 | // Record if this field is the first non-literal field or base. |
| 637 | if (!hasNonLiteralTypeFieldsOrBases() && !T->isLiteralType()) |
| 638 | data().HasNonLiteralTypeFieldsOrBases = true; |
| 639 | |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 640 | if (const RecordType *RecordTy = T->getAs<RecordType>()) { |
| 641 | CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 642 | if (FieldRec->getDefinition()) { |
| 643 | if (!FieldRec->hasTrivialConstructor()) |
| 644 | data().HasTrivialConstructor = false; |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 645 | |
| 646 | // C++0x [class.copy]p13: |
| 647 | // A copy/move constructor for class X is trivial if [...] |
| 648 | // [...] |
| 649 | // -- for each non-static data member of X that is of class type (or |
| 650 | // an array thereof), the constructor selected to copy/move that |
| 651 | // member is trivial; |
| 652 | // FIXME: C++0x: We don't correctly model 'selected' constructors. |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 653 | if (!FieldRec->hasTrivialCopyConstructor()) |
| 654 | data().HasTrivialCopyConstructor = false; |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 655 | if (!FieldRec->hasTrivialMoveConstructor()) |
| 656 | data().HasTrivialMoveConstructor = false; |
| 657 | |
| 658 | // C++0x [class.copy]p27: |
| 659 | // A copy/move assignment operator for class X is trivial if [...] |
| 660 | // [...] |
| 661 | // -- for each non-static data member of X that is of class type (or |
| 662 | // an array thereof), the assignment operator selected to |
| 663 | // copy/move that member is trivial; |
| 664 | // FIXME: C++0x: We don't correctly model 'selected' operators. |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 665 | if (!FieldRec->hasTrivialCopyAssignment()) |
| 666 | data().HasTrivialCopyAssignment = false; |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 667 | if (!FieldRec->hasTrivialMoveAssignment()) |
| 668 | data().HasTrivialMoveAssignment = false; |
| 669 | |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 670 | if (!FieldRec->hasTrivialDestructor()) |
| 671 | data().HasTrivialDestructor = false; |
| 672 | } |
| 673 | } |
Douglas Gregor | 2cf9d65 | 2010-09-28 20:38:10 +0000 | [diff] [blame] | 674 | |
| 675 | // If this is not a zero-length bit-field, then the class is not empty. |
| 676 | if (data().Empty) { |
| 677 | if (!Field->getBitWidth()) |
| 678 | data().Empty = false; |
| 679 | else if (!Field->getBitWidth()->isTypeDependent() && |
| 680 | !Field->getBitWidth()->isValueDependent()) { |
| 681 | llvm::APSInt Bits; |
| 682 | if (Field->getBitWidth()->isIntegerConstantExpr(Bits, Context)) |
| 683 | if (!!Bits) |
| 684 | data().Empty = false; |
| 685 | } |
| 686 | } |
Douglas Gregor | 9fe183a | 2010-09-28 19:45:33 +0000 | [diff] [blame] | 687 | } |
Douglas Gregor | e80622f | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 688 | |
| 689 | // Handle using declarations of conversion functions. |
| 690 | if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D)) |
| 691 | if (Shadow->getDeclName().getNameKind() |
| 692 | == DeclarationName::CXXConversionFunctionName) |
| 693 | data().Conversions.addDecl(Shadow, Shadow->getAccess()); |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 694 | } |
| 695 | |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 696 | static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) { |
| 697 | QualType T; |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 698 | if (isa<UsingShadowDecl>(Conv)) |
| 699 | Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl(); |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 700 | if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv)) |
| 701 | T = ConvTemp->getTemplatedDecl()->getResultType(); |
| 702 | else |
| 703 | T = cast<CXXConversionDecl>(Conv)->getConversionType(); |
| 704 | return Context.getCanonicalType(T); |
Fariborz Jahanian | 0351a1e | 2009-10-07 20:43:36 +0000 | [diff] [blame] | 705 | } |
| 706 | |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 707 | /// Collect the visible conversions of a base class. |
| 708 | /// |
| 709 | /// \param Base a base class of the class we're considering |
| 710 | /// \param InVirtual whether this base class is a virtual base (or a base |
| 711 | /// of a virtual base) |
| 712 | /// \param Access the access along the inheritance path to this base |
| 713 | /// \param ParentHiddenTypes the conversions provided by the inheritors |
| 714 | /// of this base |
| 715 | /// \param Output the set to which to add conversions from non-virtual bases |
| 716 | /// \param VOutput the set to which to add conversions from virtual bases |
| 717 | /// \param HiddenVBaseCs the set of conversions which were hidden in a |
| 718 | /// virtual base along some inheritance path |
| 719 | static void CollectVisibleConversions(ASTContext &Context, |
| 720 | CXXRecordDecl *Record, |
| 721 | bool InVirtual, |
| 722 | AccessSpecifier Access, |
| 723 | const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes, |
| 724 | UnresolvedSetImpl &Output, |
| 725 | UnresolvedSetImpl &VOutput, |
| 726 | llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) { |
| 727 | // The set of types which have conversions in this class or its |
| 728 | // subclasses. As an optimization, we don't copy the derived set |
| 729 | // unless it might change. |
| 730 | const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes; |
| 731 | llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer; |
| 732 | |
| 733 | // Collect the direct conversions and figure out which conversions |
| 734 | // will be hidden in the subclasses. |
| 735 | UnresolvedSetImpl &Cs = *Record->getConversionFunctions(); |
| 736 | if (!Cs.empty()) { |
| 737 | HiddenTypesBuffer = ParentHiddenTypes; |
| 738 | HiddenTypes = &HiddenTypesBuffer; |
| 739 | |
| 740 | for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) { |
| 741 | bool Hidden = |
| 742 | !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl())); |
| 743 | |
| 744 | // If this conversion is hidden and we're in a virtual base, |
| 745 | // remember that it's hidden along some inheritance path. |
| 746 | if (Hidden && InVirtual) |
| 747 | HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())); |
| 748 | |
| 749 | // If this conversion isn't hidden, add it to the appropriate output. |
| 750 | else if (!Hidden) { |
| 751 | AccessSpecifier IAccess |
| 752 | = CXXRecordDecl::MergeAccess(Access, I.getAccess()); |
| 753 | |
| 754 | if (InVirtual) |
| 755 | VOutput.addDecl(I.getDecl(), IAccess); |
Fariborz Jahanian | 6250921 | 2009-09-12 18:26:03 +0000 | [diff] [blame] | 756 | else |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 757 | Output.addDecl(I.getDecl(), IAccess); |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 758 | } |
| 759 | } |
| 760 | } |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 761 | |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 762 | // Collect information recursively from any base classes. |
| 763 | for (CXXRecordDecl::base_class_iterator |
| 764 | I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) { |
| 765 | const RecordType *RT = I->getType()->getAs<RecordType>(); |
| 766 | if (!RT) continue; |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 767 | |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 768 | AccessSpecifier BaseAccess |
| 769 | = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier()); |
| 770 | bool BaseInVirtual = InVirtual || I->isVirtual(); |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 771 | |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 772 | CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl()); |
| 773 | CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess, |
| 774 | *HiddenTypes, Output, VOutput, HiddenVBaseCs); |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 775 | } |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 776 | } |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 777 | |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 778 | /// Collect the visible conversions of a class. |
| 779 | /// |
| 780 | /// This would be extremely straightforward if it weren't for virtual |
| 781 | /// bases. It might be worth special-casing that, really. |
| 782 | static void CollectVisibleConversions(ASTContext &Context, |
| 783 | CXXRecordDecl *Record, |
| 784 | UnresolvedSetImpl &Output) { |
| 785 | // The collection of all conversions in virtual bases that we've |
| 786 | // found. These will be added to the output as long as they don't |
| 787 | // appear in the hidden-conversions set. |
| 788 | UnresolvedSet<8> VBaseCs; |
| 789 | |
| 790 | // The set of conversions in virtual bases that we've determined to |
| 791 | // be hidden. |
| 792 | llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs; |
| 793 | |
| 794 | // The set of types hidden by classes derived from this one. |
| 795 | llvm::SmallPtrSet<CanQualType, 8> HiddenTypes; |
| 796 | |
| 797 | // Go ahead and collect the direct conversions and add them to the |
| 798 | // hidden-types set. |
| 799 | UnresolvedSetImpl &Cs = *Record->getConversionFunctions(); |
| 800 | Output.append(Cs.begin(), Cs.end()); |
| 801 | for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) |
| 802 | HiddenTypes.insert(GetConversionType(Context, I.getDecl())); |
| 803 | |
| 804 | // Recursively collect conversions from base classes. |
| 805 | for (CXXRecordDecl::base_class_iterator |
| 806 | I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) { |
| 807 | const RecordType *RT = I->getType()->getAs<RecordType>(); |
| 808 | if (!RT) continue; |
| 809 | |
| 810 | CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()), |
| 811 | I->isVirtual(), I->getAccessSpecifier(), |
| 812 | HiddenTypes, Output, VBaseCs, HiddenVBaseCs); |
| 813 | } |
| 814 | |
| 815 | // Add any unhidden conversions provided by virtual bases. |
| 816 | for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end(); |
| 817 | I != E; ++I) { |
| 818 | if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()))) |
| 819 | Output.addDecl(I.getDecl(), I.getAccess()); |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 820 | } |
Fariborz Jahanian | 6250921 | 2009-09-12 18:26:03 +0000 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | /// getVisibleConversionFunctions - get all conversion functions visible |
| 824 | /// in current class; including conversion function templates. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 825 | const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() { |
Fariborz Jahanian | 6250921 | 2009-09-12 18:26:03 +0000 | [diff] [blame] | 826 | // If root class, all conversions are visible. |
| 827 | if (bases_begin() == bases_end()) |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 828 | return &data().Conversions; |
Fariborz Jahanian | 6250921 | 2009-09-12 18:26:03 +0000 | [diff] [blame] | 829 | // If visible conversion list is already evaluated, return it. |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 830 | if (data().ComputedVisibleConversions) |
| 831 | return &data().VisibleConversions; |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 832 | CollectVisibleConversions(getASTContext(), this, data().VisibleConversions); |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 833 | data().ComputedVisibleConversions = true; |
| 834 | return &data().VisibleConversions; |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 835 | } |
| 836 | |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 837 | void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) { |
| 838 | // This operation is O(N) but extremely rare. Sema only uses it to |
| 839 | // remove UsingShadowDecls in a class that were followed by a direct |
| 840 | // declaration, e.g.: |
| 841 | // class A : B { |
| 842 | // using B::operator int; |
| 843 | // operator int(); |
| 844 | // }; |
| 845 | // This is uncommon by itself and even more uncommon in conjunction |
| 846 | // with sufficiently large numbers of directly-declared conversions |
| 847 | // that asymptotic behavior matters. |
| 848 | |
| 849 | UnresolvedSetImpl &Convs = *getConversionFunctions(); |
| 850 | for (unsigned I = 0, E = Convs.size(); I != E; ++I) { |
| 851 | if (Convs[I].getDecl() == ConvDecl) { |
| 852 | Convs.erase(I); |
| 853 | assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end() |
| 854 | && "conversion was found multiple times in unresolved set"); |
| 855 | return; |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | llvm_unreachable("conversion not found in set!"); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 860 | } |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 861 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 862 | CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 863 | if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 864 | return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom()); |
| 865 | |
| 866 | return 0; |
| 867 | } |
| 868 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 869 | MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const { |
| 870 | return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>(); |
| 871 | } |
| 872 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 873 | void |
| 874 | CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD, |
| 875 | TemplateSpecializationKind TSK) { |
| 876 | assert(TemplateOrInstantiation.isNull() && |
| 877 | "Previous template or instantiation?"); |
| 878 | assert(!isa<ClassTemplateSpecializationDecl>(this)); |
| 879 | TemplateOrInstantiation |
| 880 | = new (getASTContext()) MemberSpecializationInfo(RD, TSK); |
| 881 | } |
| 882 | |
Anders Carlsson | b13e357 | 2009-12-07 06:33:48 +0000 | [diff] [blame] | 883 | TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{ |
| 884 | if (const ClassTemplateSpecializationDecl *Spec |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 885 | = dyn_cast<ClassTemplateSpecializationDecl>(this)) |
| 886 | return Spec->getSpecializationKind(); |
| 887 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 888 | if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 889 | return MSInfo->getTemplateSpecializationKind(); |
| 890 | |
| 891 | return TSK_Undeclared; |
| 892 | } |
| 893 | |
| 894 | void |
| 895 | CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) { |
| 896 | if (ClassTemplateSpecializationDecl *Spec |
| 897 | = dyn_cast<ClassTemplateSpecializationDecl>(this)) { |
| 898 | Spec->setSpecializationKind(TSK); |
| 899 | return; |
| 900 | } |
| 901 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 902 | if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 903 | MSInfo->setTemplateSpecializationKind(TSK); |
| 904 | return; |
| 905 | } |
| 906 | |
| 907 | assert(false && "Not a class template or member class specialization"); |
| 908 | } |
| 909 | |
Douglas Gregor | 1d110e0 | 2010-07-01 14:13:13 +0000 | [diff] [blame] | 910 | CXXDestructorDecl *CXXRecordDecl::getDestructor() const { |
| 911 | ASTContext &Context = getASTContext(); |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 912 | QualType ClassType = Context.getTypeDeclType(this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 913 | |
| 914 | DeclarationName Name |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 915 | = Context.DeclarationNames.getCXXDestructorName( |
| 916 | Context.getCanonicalType(ClassType)); |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 917 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 918 | DeclContext::lookup_const_iterator I, E; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 919 | llvm::tie(I, E) = lookup(Name); |
Sebastian Redl | d4b25cb | 2010-09-02 23:19:42 +0000 | [diff] [blame] | 920 | if (I == E) |
| 921 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 922 | |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 923 | CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I); |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 924 | return Dtor; |
| 925 | } |
| 926 | |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 927 | void CXXRecordDecl::completeDefinition() { |
| 928 | completeDefinition(0); |
John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 929 | |
| 930 | ASTContext &Context = getASTContext(); |
| 931 | if (const RecordType *RT = getTypeForDecl()->getAs<RecordType>()) |
| 932 | data().HasStandardLayout = RT->hasStandardLayout(Context); |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) { |
| 936 | RecordDecl::completeDefinition(); |
| 937 | |
Douglas Gregor | 7a39dd0 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 938 | // If the class may be abstract (but hasn't been marked as such), check for |
| 939 | // any pure final overriders. |
| 940 | if (mayBeAbstract()) { |
| 941 | CXXFinalOverriderMap MyFinalOverriders; |
| 942 | if (!FinalOverriders) { |
| 943 | getFinalOverriders(MyFinalOverriders); |
| 944 | FinalOverriders = &MyFinalOverriders; |
| 945 | } |
| 946 | |
| 947 | bool Done = false; |
| 948 | for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(), |
| 949 | MEnd = FinalOverriders->end(); |
| 950 | M != MEnd && !Done; ++M) { |
| 951 | for (OverridingMethods::iterator SO = M->second.begin(), |
| 952 | SOEnd = M->second.end(); |
| 953 | SO != SOEnd && !Done; ++SO) { |
| 954 | assert(SO->second.size() > 0 && |
| 955 | "All virtual functions have overridding virtual functions"); |
| 956 | |
| 957 | // C++ [class.abstract]p4: |
| 958 | // A class is abstract if it contains or inherits at least one |
| 959 | // pure virtual function for which the final overrider is pure |
| 960 | // virtual. |
| 961 | if (SO->second.front().Method->isPure()) { |
| 962 | data().Abstract = true; |
| 963 | Done = true; |
| 964 | break; |
| 965 | } |
| 966 | } |
| 967 | } |
| 968 | } |
Douglas Gregor | e80622f | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 969 | |
| 970 | // Set access bits correctly on the directly-declared conversions. |
| 971 | for (UnresolvedSetIterator I = data().Conversions.begin(), |
| 972 | E = data().Conversions.end(); |
| 973 | I != E; ++I) |
| 974 | data().Conversions.setAccess(I, (*I)->getAccess()); |
Douglas Gregor | 7a39dd0 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | bool CXXRecordDecl::mayBeAbstract() const { |
| 978 | if (data().Abstract || isInvalidDecl() || !data().Polymorphic || |
| 979 | isDependentContext()) |
| 980 | return false; |
| 981 | |
| 982 | for (CXXRecordDecl::base_class_const_iterator B = bases_begin(), |
| 983 | BEnd = bases_end(); |
| 984 | B != BEnd; ++B) { |
| 985 | CXXRecordDecl *BaseDecl |
| 986 | = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl()); |
| 987 | if (BaseDecl->isAbstract()) |
| 988 | return true; |
| 989 | } |
| 990 | |
| 991 | return false; |
| 992 | } |
| 993 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 994 | CXXMethodDecl * |
| 995 | CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 996 | SourceLocation StartLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 997 | const DeclarationNameInfo &NameInfo, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 998 | QualType T, TypeSourceInfo *TInfo, |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 999 | bool isStatic, StorageClass SCAsWritten, bool isInline, |
| 1000 | SourceLocation EndLocation) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1001 | return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo, |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 1002 | isStatic, SCAsWritten, isInline, EndLocation); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 1005 | bool CXXMethodDecl::isUsualDeallocationFunction() const { |
| 1006 | if (getOverloadedOperator() != OO_Delete && |
| 1007 | getOverloadedOperator() != OO_Array_Delete) |
| 1008 | return false; |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1009 | |
| 1010 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 1011 | // A template instance is never a usual deallocation function, |
| 1012 | // regardless of its signature. |
| 1013 | if (getPrimaryTemplate()) |
| 1014 | return false; |
| 1015 | |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 1016 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 1017 | // If a class T has a member deallocation function named operator delete |
| 1018 | // with exactly one parameter, then that function is a usual (non-placement) |
| 1019 | // deallocation function. [...] |
| 1020 | if (getNumParams() == 1) |
| 1021 | return true; |
| 1022 | |
| 1023 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 1024 | // [...] If class T does not declare such an operator delete but does |
| 1025 | // declare a member deallocation function named operator delete with |
| 1026 | // exactly two parameters, the second of which has type std::size_t (18.1), |
| 1027 | // then this function is a usual deallocation function. |
| 1028 | ASTContext &Context = getASTContext(); |
| 1029 | if (getNumParams() != 2 || |
Chandler Carruth | e228ba9 | 2010-02-08 18:54:05 +0000 | [diff] [blame] | 1030 | !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(), |
| 1031 | Context.getSizeType())) |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 1032 | return false; |
| 1033 | |
| 1034 | // This function is a usual deallocation function if there are no |
| 1035 | // single-parameter deallocation functions of the same kind. |
| 1036 | for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName()); |
| 1037 | R.first != R.second; ++R.first) { |
| 1038 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first)) |
| 1039 | if (FD->getNumParams() == 1) |
| 1040 | return false; |
| 1041 | } |
| 1042 | |
| 1043 | return true; |
| 1044 | } |
| 1045 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 1046 | bool CXXMethodDecl::isCopyAssignmentOperator() const { |
| 1047 | // C++0x [class.copy]p19: |
| 1048 | // A user-declared copy assignment operator X::operator= is a non-static |
| 1049 | // non-template member function of class X with exactly one parameter of |
| 1050 | // type X, X&, const X&, volatile X& or const volatile X&. |
| 1051 | if (/*operator=*/getOverloadedOperator() != OO_Equal || |
| 1052 | /*non-static*/ isStatic() || |
| 1053 | /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() || |
| 1054 | /*exactly one parameter*/getNumParams() != 1) |
| 1055 | return false; |
| 1056 | |
| 1057 | QualType ParamType = getParamDecl(0)->getType(); |
| 1058 | if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>()) |
| 1059 | ParamType = Ref->getPointeeType(); |
| 1060 | |
| 1061 | ASTContext &Context = getASTContext(); |
| 1062 | QualType ClassType |
| 1063 | = Context.getCanonicalType(Context.getTypeDeclType(getParent())); |
| 1064 | return Context.hasSameUnqualifiedType(ClassType, ParamType); |
| 1065 | } |
| 1066 | |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 1067 | void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) { |
Anders Carlsson | 3aaf486 | 2009-12-04 05:51:56 +0000 | [diff] [blame] | 1068 | assert(MD->isCanonicalDecl() && "Method is not canonical!"); |
Anders Carlsson | c076c45 | 2010-01-30 17:42:34 +0000 | [diff] [blame] | 1069 | assert(!MD->getParent()->isDependentContext() && |
| 1070 | "Can't add an overridden method to a class template!"); |
| 1071 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1072 | getASTContext().addOverriddenMethod(this, MD); |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const { |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1076 | return getASTContext().overridden_methods_begin(this); |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const { |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1080 | return getASTContext().overridden_methods_end(this); |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
Argyrios Kyrtzidis | c91e9f4 | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 1083 | unsigned CXXMethodDecl::size_overridden_methods() const { |
| 1084 | return getASTContext().overridden_methods_size(this); |
| 1085 | } |
| 1086 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1087 | QualType CXXMethodDecl::getThisType(ASTContext &C) const { |
Argyrios Kyrtzidis | b0d178d | 2008-10-24 22:28:18 +0000 | [diff] [blame] | 1088 | // C++ 9.3.2p1: The type of this in a member function of a class X is X*. |
| 1089 | // If the member function is declared const, the type of this is const X*, |
| 1090 | // if the member function is declared volatile, the type of this is |
| 1091 | // volatile X*, and if the member function is declared const volatile, |
| 1092 | // the type of this is const volatile X*. |
| 1093 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1094 | assert(isInstance() && "No 'this' for static methods!"); |
Anders Carlsson | 31a0875 | 2009-06-13 02:59:33 +0000 | [diff] [blame] | 1095 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1096 | QualType ClassTy = C.getTypeDeclType(getParent()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1097 | ClassTy = C.getQualifiedType(ClassTy, |
| 1098 | Qualifiers::fromCVRMask(getTypeQualifiers())); |
Anders Carlsson | 4e57992 | 2009-07-10 21:35:09 +0000 | [diff] [blame] | 1099 | return C.getPointerType(ClassTy); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
Eli Friedman | d7d7f67 | 2009-12-06 20:50:05 +0000 | [diff] [blame] | 1102 | bool CXXMethodDecl::hasInlineBody() const { |
Douglas Gregor | bd6d619 | 2010-01-05 19:06:31 +0000 | [diff] [blame] | 1103 | // If this function is a template instantiation, look at the template from |
| 1104 | // which it was instantiated. |
| 1105 | const FunctionDecl *CheckFn = getTemplateInstantiationPattern(); |
| 1106 | if (!CheckFn) |
| 1107 | CheckFn = this; |
| 1108 | |
Eli Friedman | d7d7f67 | 2009-12-06 20:50:05 +0000 | [diff] [blame] | 1109 | const FunctionDecl *fn; |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1110 | return CheckFn->hasBody(fn) && !fn->isOutOfLine(); |
Eli Friedman | d7d7f67 | 2009-12-06 20:50:05 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1113 | CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, |
| 1114 | TypeSourceInfo *TInfo, bool IsVirtual, |
| 1115 | SourceLocation L, Expr *Init, |
| 1116 | SourceLocation R, |
| 1117 | SourceLocation EllipsisLoc) |
Sean Hunt | f51d0b6 | 2011-01-08 23:01:16 +0000 | [diff] [blame] | 1118 | : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init), |
Abramo Bagnara | a0af3b4 | 2010-05-26 18:09:23 +0000 | [diff] [blame] | 1119 | LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false), |
| 1120 | SourceOrderOrNumArrayIndices(0) |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1121 | { |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1124 | CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, |
| 1125 | FieldDecl *Member, |
| 1126 | SourceLocation MemberLoc, |
| 1127 | SourceLocation L, Expr *Init, |
| 1128 | SourceLocation R) |
Sean Hunt | f51d0b6 | 2011-01-08 23:01:16 +0000 | [diff] [blame] | 1129 | : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init), |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1130 | LParenLoc(L), RParenLoc(R), IsVirtual(false), |
| 1131 | IsWritten(false), SourceOrderOrNumArrayIndices(0) |
| 1132 | { |
| 1133 | } |
| 1134 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1135 | CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, |
| 1136 | IndirectFieldDecl *Member, |
| 1137 | SourceLocation MemberLoc, |
| 1138 | SourceLocation L, Expr *Init, |
| 1139 | SourceLocation R) |
Sean Hunt | f51d0b6 | 2011-01-08 23:01:16 +0000 | [diff] [blame] | 1140 | : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init), |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1141 | LParenLoc(L), RParenLoc(R), IsVirtual(false), |
Abramo Bagnara | a0af3b4 | 2010-05-26 18:09:23 +0000 | [diff] [blame] | 1142 | IsWritten(false), SourceOrderOrNumArrayIndices(0) |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1143 | { |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1146 | CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 1147 | SourceLocation D, SourceLocation L, |
| 1148 | CXXConstructorDecl *Target, Expr *Init, |
| 1149 | SourceLocation R) |
| 1150 | : Initializee(Target), MemberOrEllipsisLocation(D), Init(Init), |
| 1151 | LParenLoc(L), RParenLoc(R), IsVirtual(false), |
| 1152 | IsWritten(false), SourceOrderOrNumArrayIndices(0) |
| 1153 | { |
| 1154 | } |
| 1155 | |
| 1156 | CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1157 | FieldDecl *Member, |
| 1158 | SourceLocation MemberLoc, |
| 1159 | SourceLocation L, Expr *Init, |
| 1160 | SourceLocation R, |
| 1161 | VarDecl **Indices, |
| 1162 | unsigned NumIndices) |
Sean Hunt | f51d0b6 | 2011-01-08 23:01:16 +0000 | [diff] [blame] | 1163 | : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init), |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1164 | LParenLoc(L), RParenLoc(R), IsVirtual(false), |
Abramo Bagnara | a0af3b4 | 2010-05-26 18:09:23 +0000 | [diff] [blame] | 1165 | IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices) |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1166 | { |
| 1167 | VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1); |
| 1168 | memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *)); |
| 1169 | } |
| 1170 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1171 | CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context, |
| 1172 | FieldDecl *Member, |
| 1173 | SourceLocation MemberLoc, |
| 1174 | SourceLocation L, Expr *Init, |
| 1175 | SourceLocation R, |
| 1176 | VarDecl **Indices, |
| 1177 | unsigned NumIndices) { |
| 1178 | void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) + |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1179 | sizeof(VarDecl *) * NumIndices, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1180 | llvm::alignOf<CXXCtorInitializer>()); |
Sean Hunt | f51d0b6 | 2011-01-08 23:01:16 +0000 | [diff] [blame] | 1181 | return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R, |
| 1182 | Indices, NumIndices); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1185 | TypeLoc CXXCtorInitializer::getBaseClassLoc() const { |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1186 | if (isBaseInitializer()) |
Sean Hunt | f51d0b6 | 2011-01-08 23:01:16 +0000 | [diff] [blame] | 1187 | return Initializee.get<TypeSourceInfo*>()->getTypeLoc(); |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1188 | else |
| 1189 | return TypeLoc(); |
| 1190 | } |
| 1191 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1192 | const Type *CXXCtorInitializer::getBaseClass() const { |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1193 | if (isBaseInitializer()) |
Sean Hunt | f51d0b6 | 2011-01-08 23:01:16 +0000 | [diff] [blame] | 1194 | return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr(); |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1195 | else |
| 1196 | return 0; |
| 1197 | } |
| 1198 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1199 | SourceLocation CXXCtorInitializer::getSourceLocation() const { |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 1200 | if (isAnyMemberInitializer() || isDelegatingInitializer()) |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1201 | return getMemberLocation(); |
| 1202 | |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1203 | return getBaseClassLoc().getLocalSourceRange().getBegin(); |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1206 | SourceRange CXXCtorInitializer::getSourceRange() const { |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1207 | return SourceRange(getSourceLocation(), getRParenLoc()); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1210 | CXXConstructorDecl * |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 1211 | CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1212 | return new (C) CXXConstructorDecl(0, SourceLocation(), DeclarationNameInfo(), |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 1213 | QualType(), 0, false, false, false); |
| 1214 | } |
| 1215 | |
| 1216 | CXXConstructorDecl * |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1217 | CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1218 | SourceLocation StartLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1219 | const DeclarationNameInfo &NameInfo, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1220 | QualType T, TypeSourceInfo *TInfo, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 1221 | bool isExplicit, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1222 | bool isInline, |
| 1223 | bool isImplicitlyDeclared) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1224 | assert(NameInfo.getName().getNameKind() |
| 1225 | == DeclarationName::CXXConstructorName && |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1226 | "Name must refer to a constructor"); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1227 | return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo, |
| 1228 | isExplicit, isInline, isImplicitlyDeclared); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 1231 | bool CXXConstructorDecl::isDefaultConstructor() const { |
| 1232 | // C++ [class.ctor]p5: |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 1233 | // A default constructor for a class X is a constructor of class |
| 1234 | // X that can be called without an argument. |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 1235 | return (getNumParams() == 0) || |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 1236 | (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg()); |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 1237 | } |
| 1238 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1239 | bool |
Douglas Gregor | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 1240 | CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const { |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 1241 | return isCopyOrMoveConstructor(TypeQuals) && |
| 1242 | getParamDecl(0)->getType()->isLValueReferenceType(); |
| 1243 | } |
| 1244 | |
| 1245 | bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const { |
| 1246 | return isCopyOrMoveConstructor(TypeQuals) && |
| 1247 | getParamDecl(0)->getType()->isRValueReferenceType(); |
| 1248 | } |
| 1249 | |
| 1250 | /// \brief Determine whether this is a copy or move constructor. |
| 1251 | bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const { |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 1252 | // C++ [class.copy]p2: |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 1253 | // A non-template constructor for class X is a copy constructor |
| 1254 | // if its first parameter is of type X&, const X&, volatile X& or |
| 1255 | // const volatile X&, and either there are no other parameters |
| 1256 | // or else all other parameters have default arguments (8.3.6). |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 1257 | // C++0x [class.copy]p3: |
| 1258 | // A non-template constructor for class X is a move constructor if its |
| 1259 | // first parameter is of type X&&, const X&&, volatile X&&, or |
| 1260 | // const volatile X&&, and either there are no other parameters or else |
| 1261 | // all other parameters have default arguments. |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 1262 | if ((getNumParams() < 1) || |
Douglas Gregor | 77da3f4 | 2009-10-13 23:45:19 +0000 | [diff] [blame] | 1263 | (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) || |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 1264 | (getPrimaryTemplate() != 0) || |
Douglas Gregor | 77da3f4 | 2009-10-13 23:45:19 +0000 | [diff] [blame] | 1265 | (getDescribedFunctionTemplate() != 0)) |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 1266 | return false; |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 1267 | |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 1268 | const ParmVarDecl *Param = getParamDecl(0); |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 1269 | |
| 1270 | // Do we have a reference type? |
| 1271 | const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>(); |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 1272 | if (!ParamRefType) |
| 1273 | return false; |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 1274 | |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 1275 | // Is it a reference to our class type? |
Douglas Gregor | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 1276 | ASTContext &Context = getASTContext(); |
| 1277 | |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 1278 | CanQualType PointeeType |
| 1279 | = Context.getCanonicalType(ParamRefType->getPointeeType()); |
Douglas Gregor | 14e0b3d | 2009-09-15 20:50:23 +0000 | [diff] [blame] | 1280 | CanQualType ClassTy |
| 1281 | = Context.getCanonicalType(Context.getTagDeclType(getParent())); |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 1282 | if (PointeeType.getUnqualifiedType() != ClassTy) |
| 1283 | return false; |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 1284 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1285 | // FIXME: other qualifiers? |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 1286 | |
| 1287 | // We have a copy or move constructor. |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 1288 | TypeQuals = PointeeType.getCVRQualifiers(); |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 1289 | return true; |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 1290 | } |
| 1291 | |
Anders Carlsson | faccd72 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 1292 | bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1293 | // C++ [class.conv.ctor]p1: |
| 1294 | // A constructor declared without the function-specifier explicit |
| 1295 | // that can be called with a single parameter specifies a |
| 1296 | // conversion from the type of its first parameter to the type of |
| 1297 | // its class. Such a constructor is called a converting |
| 1298 | // constructor. |
Anders Carlsson | faccd72 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 1299 | if (isExplicit() && !AllowExplicit) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1300 | return false; |
| 1301 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1302 | return (getNumParams() == 0 && |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1303 | getType()->getAs<FunctionProtoType>()->isVariadic()) || |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1304 | (getNumParams() == 1) || |
Anders Carlsson | ae0b4e7 | 2009-06-06 04:14:07 +0000 | [diff] [blame] | 1305 | (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg()); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1306 | } |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1307 | |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 1308 | bool CXXConstructorDecl::isSpecializationCopyingObject() const { |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1309 | if ((getNumParams() < 1) || |
| 1310 | (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) || |
| 1311 | (getPrimaryTemplate() == 0) || |
| 1312 | (getDescribedFunctionTemplate() != 0)) |
| 1313 | return false; |
| 1314 | |
| 1315 | const ParmVarDecl *Param = getParamDecl(0); |
| 1316 | |
| 1317 | ASTContext &Context = getASTContext(); |
| 1318 | CanQualType ParamType = Context.getCanonicalType(Param->getType()); |
| 1319 | |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1320 | // Is it the same as our our class type? |
| 1321 | CanQualType ClassTy |
| 1322 | = Context.getCanonicalType(Context.getTagDeclType(getParent())); |
| 1323 | if (ParamType.getUnqualifiedType() != ClassTy) |
| 1324 | return false; |
| 1325 | |
| 1326 | return true; |
| 1327 | } |
| 1328 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 1329 | const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const { |
| 1330 | // Hack: we store the inherited constructor in the overridden method table |
| 1331 | method_iterator It = begin_overridden_methods(); |
| 1332 | if (It == end_overridden_methods()) |
| 1333 | return 0; |
| 1334 | |
| 1335 | return cast<CXXConstructorDecl>(*It); |
| 1336 | } |
| 1337 | |
| 1338 | void |
| 1339 | CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){ |
| 1340 | // Hack: we store the inherited constructor in the overridden method table |
| 1341 | assert(size_overridden_methods() == 0 && "Base ctor already set."); |
| 1342 | addOverriddenMethod(BaseCtor); |
| 1343 | } |
| 1344 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1345 | CXXDestructorDecl * |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 1346 | CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1347 | return new (C) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(), |
Craig Silverstein | b41d899 | 2010-10-21 00:44:50 +0000 | [diff] [blame] | 1348 | QualType(), 0, false, false); |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | CXXDestructorDecl * |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1352 | CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1353 | SourceLocation StartLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1354 | const DeclarationNameInfo &NameInfo, |
Craig Silverstein | b41d899 | 2010-10-21 00:44:50 +0000 | [diff] [blame] | 1355 | QualType T, TypeSourceInfo *TInfo, |
| 1356 | bool isInline, |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1357 | bool isImplicitlyDeclared) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1358 | assert(NameInfo.getName().getNameKind() |
| 1359 | == DeclarationName::CXXDestructorName && |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1360 | "Name must refer to a destructor"); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1361 | return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1362 | isImplicitlyDeclared); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1365 | CXXConversionDecl * |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 1366 | CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1367 | return new (C) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(), |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 1368 | QualType(), 0, false, false, |
| 1369 | SourceLocation()); |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
| 1372 | CXXConversionDecl * |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1373 | CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1374 | SourceLocation StartLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1375 | const DeclarationNameInfo &NameInfo, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1376 | QualType T, TypeSourceInfo *TInfo, |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 1377 | bool isInline, bool isExplicit, |
| 1378 | SourceLocation EndLocation) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1379 | assert(NameInfo.getName().getNameKind() |
| 1380 | == DeclarationName::CXXConversionFunctionName && |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1381 | "Name must refer to a conversion function"); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1382 | return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo, |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 1383 | isInline, isExplicit, EndLocation); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
Chris Lattner | 21ef7ae | 2008-11-04 16:51:42 +0000 | [diff] [blame] | 1386 | LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1387 | DeclContext *DC, |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 1388 | SourceLocation ExternLoc, |
| 1389 | SourceLocation LangLoc, |
Abramo Bagnara | 5f6bcbe | 2011-03-03 14:52:38 +0000 | [diff] [blame] | 1390 | LanguageIDs Lang, |
Abramo Bagnara | 5f6bcbe | 2011-03-03 14:52:38 +0000 | [diff] [blame] | 1391 | SourceLocation RBraceLoc) { |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 1392 | return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc); |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 1393 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1394 | |
| 1395 | UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC, |
| 1396 | SourceLocation L, |
| 1397 | SourceLocation NamespaceLoc, |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 1398 | NestedNameSpecifierLoc QualifierLoc, |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1399 | SourceLocation IdentLoc, |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 1400 | NamedDecl *Used, |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1401 | DeclContext *CommonAncestor) { |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 1402 | if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used)) |
| 1403 | Used = NS->getOriginalNamespace(); |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 1404 | return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc, |
| 1405 | IdentLoc, Used, CommonAncestor); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 1408 | NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() { |
| 1409 | if (NamespaceAliasDecl *NA = |
| 1410 | dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace)) |
| 1411 | return NA->getNamespace(); |
| 1412 | return cast_or_null<NamespaceDecl>(NominatedNamespace); |
| 1413 | } |
| 1414 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1415 | NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC, |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 1416 | SourceLocation UsingLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1417 | SourceLocation AliasLoc, |
| 1418 | IdentifierInfo *Alias, |
Douglas Gregor | 0cfaf6a | 2011-02-25 17:08:07 +0000 | [diff] [blame] | 1419 | NestedNameSpecifierLoc QualifierLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1420 | SourceLocation IdentLoc, |
Anders Carlsson | 68771c7 | 2009-03-28 22:58:02 +0000 | [diff] [blame] | 1421 | NamedDecl *Namespace) { |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 1422 | if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace)) |
| 1423 | Namespace = NS->getOriginalNamespace(); |
Douglas Gregor | 0cfaf6a | 2011-02-25 17:08:07 +0000 | [diff] [blame] | 1424 | return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias, |
| 1425 | QualifierLoc, IdentLoc, Namespace); |
Anders Carlsson | 68771c7 | 2009-03-28 22:58:02 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
Argyrios Kyrtzidis | 826faa2 | 2010-11-10 05:40:41 +0000 | [diff] [blame] | 1428 | UsingDecl *UsingShadowDecl::getUsingDecl() const { |
| 1429 | const UsingShadowDecl *Shadow = this; |
| 1430 | while (const UsingShadowDecl *NextShadow = |
| 1431 | dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow)) |
| 1432 | Shadow = NextShadow; |
| 1433 | return cast<UsingDecl>(Shadow->UsingOrNextShadow); |
| 1434 | } |
| 1435 | |
| 1436 | void UsingDecl::addShadowDecl(UsingShadowDecl *S) { |
| 1437 | assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() && |
| 1438 | "declaration already in set"); |
| 1439 | assert(S->getUsingDecl() == this); |
| 1440 | |
| 1441 | if (FirstUsingShadow) |
| 1442 | S->UsingOrNextShadow = FirstUsingShadow; |
| 1443 | FirstUsingShadow = S; |
| 1444 | } |
| 1445 | |
| 1446 | void UsingDecl::removeShadowDecl(UsingShadowDecl *S) { |
| 1447 | assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() && |
| 1448 | "declaration not in set"); |
| 1449 | assert(S->getUsingDecl() == this); |
| 1450 | |
| 1451 | // Remove S from the shadow decl chain. This is O(n) but hopefully rare. |
| 1452 | |
| 1453 | if (FirstUsingShadow == S) { |
| 1454 | FirstUsingShadow = dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow); |
| 1455 | S->UsingOrNextShadow = this; |
| 1456 | return; |
| 1457 | } |
| 1458 | |
| 1459 | UsingShadowDecl *Prev = FirstUsingShadow; |
| 1460 | while (Prev->UsingOrNextShadow != S) |
| 1461 | Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow); |
| 1462 | Prev->UsingOrNextShadow = S->UsingOrNextShadow; |
| 1463 | S->UsingOrNextShadow = this; |
| 1464 | } |
| 1465 | |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1466 | UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL, |
| 1467 | NestedNameSpecifierLoc QualifierLoc, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 1468 | const DeclarationNameInfo &NameInfo, |
| 1469 | bool IsTypeNameArg) { |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1470 | return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg); |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 1471 | } |
| 1472 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1473 | UnresolvedUsingValueDecl * |
| 1474 | UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC, |
| 1475 | SourceLocation UsingLoc, |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1476 | NestedNameSpecifierLoc QualifierLoc, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 1477 | const DeclarationNameInfo &NameInfo) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1478 | return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc, |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1479 | QualifierLoc, NameInfo); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
| 1482 | UnresolvedUsingTypenameDecl * |
| 1483 | UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC, |
| 1484 | SourceLocation UsingLoc, |
| 1485 | SourceLocation TypenameLoc, |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1486 | NestedNameSpecifierLoc QualifierLoc, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1487 | SourceLocation TargetNameLoc, |
| 1488 | DeclarationName TargetName) { |
| 1489 | return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc, |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1490 | QualifierLoc, TargetNameLoc, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1491 | TargetName.getAsIdentifierInfo()); |
Anders Carlsson | 665b49c | 2009-08-28 05:30:28 +0000 | [diff] [blame] | 1492 | } |
| 1493 | |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 1494 | StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 1495 | SourceLocation StaticAssertLoc, |
| 1496 | Expr *AssertExpr, |
| 1497 | StringLiteral *Message, |
| 1498 | SourceLocation RParenLoc) { |
| 1499 | return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message, |
| 1500 | RParenLoc); |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
Anders Carlsson | 05bf2c7 | 2009-03-26 23:46:50 +0000 | [diff] [blame] | 1503 | static const char *getAccessName(AccessSpecifier AS) { |
| 1504 | switch (AS) { |
| 1505 | default: |
| 1506 | case AS_none: |
| 1507 | assert("Invalid access specifier!"); |
| 1508 | return 0; |
| 1509 | case AS_public: |
| 1510 | return "public"; |
| 1511 | case AS_private: |
| 1512 | return "private"; |
| 1513 | case AS_protected: |
| 1514 | return "protected"; |
| 1515 | } |
| 1516 | } |
| 1517 | |
| 1518 | const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB, |
| 1519 | AccessSpecifier AS) { |
| 1520 | return DB << getAccessName(AS); |
| 1521 | } |