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" |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 17 | #include "clang/AST/Expr.h" |
Douglas Gregor | 7d7e672 | 2008-11-12 23:21:09 +0000 | [diff] [blame] | 18 | #include "clang/Basic/IdentifierTable.h" |
Douglas Gregor | fdfab6b | 2008-12-23 21:31:30 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/STLExtras.h" |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | // Decl Allocation/Deallocation Method Implementations |
| 24 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 25 | |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 26 | CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC, |
Douglas Gregor | 7d7e672 | 2008-11-12 23:21:09 +0000 | [diff] [blame] | 27 | SourceLocation L, IdentifierInfo *Id) |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 28 | : RecordDecl(K, TK, DC, L, Id), |
Douglas Gregor | 7d7e672 | 2008-11-12 23:21:09 +0000 | [diff] [blame] | 29 | UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false), |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 30 | UserDeclaredCopyAssignment(false), UserDeclaredDestructor(false), |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 31 | Aggregate(true), PlainOldData(true), Polymorphic(false), Abstract(false), |
Anders Carlsson | 072abef | 2009-04-17 02:34:54 +0000 | [diff] [blame] | 32 | HasTrivialConstructor(true), HasTrivialDestructor(true), |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 33 | Bases(0), NumBases(0), Conversions(DC, DeclarationName()), |
| 34 | TemplateOrInstantiation() { } |
Douglas Gregor | 7d7e672 | 2008-11-12 23:21:09 +0000 | [diff] [blame] | 35 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 36 | CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC, |
| 37 | SourceLocation L, IdentifierInfo *Id, |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 38 | CXXRecordDecl* PrevDecl, |
| 39 | bool DelayTypeCreation) { |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 40 | CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, L, Id); |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 41 | if (!DelayTypeCreation) |
| 42 | C.getTypeDeclType(R, PrevDecl); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 43 | return R; |
| 44 | } |
| 45 | |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 46 | CXXRecordDecl::~CXXRecordDecl() { |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 47 | delete [] Bases; |
| 48 | } |
| 49 | |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 50 | void |
| 51 | CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases, |
| 52 | unsigned NumBases) { |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 53 | // C++ [dcl.init.aggr]p1: |
| 54 | // An aggregate is an array or a class (clause 9) with [...] |
| 55 | // no base classes [...]. |
| 56 | Aggregate = false; |
| 57 | |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 58 | if (this->Bases) |
| 59 | delete [] this->Bases; |
| 60 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 61 | // FIXME: allocate using the ASTContext |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 62 | this->Bases = new CXXBaseSpecifier[NumBases]; |
| 63 | this->NumBases = NumBases; |
| 64 | for (unsigned i = 0; i < NumBases; ++i) |
| 65 | this->Bases[i] = *Bases[i]; |
| 66 | } |
| 67 | |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 68 | bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const { |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 69 | QualType ClassType |
| 70 | = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this)); |
Douglas Gregor | 9e7d9de | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 71 | DeclarationName ConstructorName |
| 72 | = Context.DeclarationNames.getCXXConstructorName( |
| 73 | Context.getCanonicalType(ClassType)); |
| 74 | unsigned TypeQuals; |
Douglas Gregor | fdfab6b | 2008-12-23 21:31:30 +0000 | [diff] [blame] | 75 | DeclContext::lookup_const_iterator Con, ConEnd; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 76 | for (llvm::tie(Con, ConEnd) = this->lookup(Context, ConstructorName); |
Douglas Gregor | fdfab6b | 2008-12-23 21:31:30 +0000 | [diff] [blame] | 77 | Con != ConEnd; ++Con) { |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 78 | if (cast<CXXConstructorDecl>(*Con)->isCopyConstructor(Context, TypeQuals) && |
Eli Friedman | e8e3205 | 2008-12-16 20:06:41 +0000 | [diff] [blame] | 79 | (TypeQuals & QualType::Const) != 0) |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 80 | return true; |
| 81 | } |
Douglas Gregor | fdfab6b | 2008-12-23 21:31:30 +0000 | [diff] [blame] | 82 | |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 83 | return false; |
| 84 | } |
| 85 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 86 | bool CXXRecordDecl::hasConstCopyAssignment(ASTContext &Context) const { |
| 87 | QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType( |
| 88 | const_cast<CXXRecordDecl*>(this))); |
| 89 | DeclarationName OpName =Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 90 | |
| 91 | DeclContext::lookup_const_iterator Op, OpEnd; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 92 | for (llvm::tie(Op, OpEnd) = this->lookup(Context, OpName); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 93 | Op != OpEnd; ++Op) { |
| 94 | // C++ [class.copy]p9: |
| 95 | // A user-declared copy assignment operator is a non-static non-template |
| 96 | // member function of class X with exactly one parameter of type X, X&, |
| 97 | // const X&, volatile X& or const volatile X&. |
| 98 | const CXXMethodDecl* Method = cast<CXXMethodDecl>(*Op); |
| 99 | if (Method->isStatic()) |
| 100 | continue; |
| 101 | // TODO: Skip templates? Or is this implicitly done due to parameter types? |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 102 | const FunctionProtoType *FnType = |
| 103 | Method->getType()->getAsFunctionProtoType(); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 104 | assert(FnType && "Overloaded operator has no prototype."); |
| 105 | // Don't assert on this; an invalid decl might have been left in the AST. |
| 106 | if (FnType->getNumArgs() != 1 || FnType->isVariadic()) |
| 107 | continue; |
| 108 | bool AcceptsConst = true; |
| 109 | QualType ArgType = FnType->getArgType(0); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 110 | if (const LValueReferenceType *Ref = ArgType->getAsLValueReferenceType()) { |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 111 | ArgType = Ref->getPointeeType(); |
Douglas Gregor | 2ff4478 | 2009-03-20 20:21:37 +0000 | [diff] [blame] | 112 | // Is it a non-const lvalue reference? |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 113 | if (!ArgType.isConstQualified()) |
| 114 | AcceptsConst = false; |
| 115 | } |
| 116 | if (Context.getCanonicalType(ArgType).getUnqualifiedType() != ClassType) |
| 117 | continue; |
| 118 | |
| 119 | // We have a single argument of type cv X or cv X&, i.e. we've found the |
| 120 | // copy assignment operator. Return whether it accepts const arguments. |
| 121 | return AcceptsConst; |
| 122 | } |
| 123 | assert(isInvalidDecl() && |
| 124 | "No copy assignment operator declared in valid code."); |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | void |
Douglas Gregor | 9e7d9de | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 129 | CXXRecordDecl::addedConstructor(ASTContext &Context, |
| 130 | CXXConstructorDecl *ConDecl) { |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 131 | if (!ConDecl->isImplicit()) { |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 132 | // Note that we have a user-declared constructor. |
| 133 | UserDeclaredConstructor = true; |
| 134 | |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 135 | // C++ [dcl.init.aggr]p1: |
| 136 | // An aggregate is an array or a class (clause 9) with no |
| 137 | // user-declared constructors (12.1) [...]. |
| 138 | Aggregate = false; |
| 139 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 140 | // C++ [class]p4: |
| 141 | // A POD-struct is an aggregate class [...] |
| 142 | PlainOldData = false; |
| 143 | |
Anders Carlsson | 347ba89 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 144 | // C++ [class.ctor]p5: |
| 145 | // A constructor is trivial if it is an implicitly-declared default |
| 146 | // constructor. |
| 147 | HasTrivialConstructor = false; |
| 148 | |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 149 | // Note when we have a user-declared copy constructor, which will |
| 150 | // suppress the implicit declaration of a copy constructor. |
| 151 | if (ConDecl->isCopyConstructor(Context)) |
| 152 | UserDeclaredCopyConstructor = true; |
| 153 | } |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 156 | void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context, |
| 157 | CXXMethodDecl *OpDecl) { |
| 158 | // We're interested specifically in copy assignment operators. |
| 159 | // Unlike addedConstructor, this method is not called for implicit |
| 160 | // declarations. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 161 | const FunctionProtoType *FnType = OpDecl->getType()->getAsFunctionProtoType(); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 162 | assert(FnType && "Overloaded operator has no proto function type."); |
| 163 | assert(FnType->getNumArgs() == 1 && !FnType->isVariadic()); |
| 164 | QualType ArgType = FnType->getArgType(0); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 165 | if (const LValueReferenceType *Ref = ArgType->getAsLValueReferenceType()) |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 166 | ArgType = Ref->getPointeeType(); |
| 167 | |
| 168 | ArgType = ArgType.getUnqualifiedType(); |
| 169 | QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType( |
| 170 | const_cast<CXXRecordDecl*>(this))); |
| 171 | |
| 172 | if (ClassType != Context.getCanonicalType(ArgType)) |
| 173 | return; |
| 174 | |
| 175 | // This is a copy assignment operator. |
| 176 | // Suppress the implicit declaration of a copy constructor. |
| 177 | UserDeclaredCopyAssignment = true; |
| 178 | |
| 179 | // C++ [class]p4: |
| 180 | // A POD-struct is an aggregate class that [...] has no user-defined copy |
| 181 | // assignment operator [...]. |
| 182 | PlainOldData = false; |
| 183 | } |
| 184 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 185 | void CXXRecordDecl::addConversionFunction(ASTContext &Context, |
| 186 | CXXConversionDecl *ConvDecl) { |
| 187 | Conversions.addOverload(ConvDecl); |
| 188 | } |
| 189 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 190 | CXXMethodDecl * |
| 191 | CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 192 | SourceLocation L, DeclarationName N, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 193 | QualType T, bool isStatic, bool isInline) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 194 | return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, isStatic, isInline); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame^] | 197 | |
| 198 | typedef llvm::DenseMap<const CXXMethodDecl*, |
| 199 | std::vector<const CXXMethodDecl *> *> |
| 200 | OverriddenMethodsMapTy; |
| 201 | |
| 202 | static OverriddenMethodsMapTy *OverriddenMethods = 0; |
| 203 | |
| 204 | void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) { |
| 205 | // FIXME: The CXXMethodDecl dtor needs to remove and free the entry. |
| 206 | |
| 207 | if (!OverriddenMethods) |
| 208 | OverriddenMethods = new OverriddenMethodsMapTy(); |
| 209 | |
| 210 | std::vector<const CXXMethodDecl *> *&Methods = (*OverriddenMethods)[this]; |
| 211 | if (!Methods) |
| 212 | Methods = new std::vector<const CXXMethodDecl *>; |
| 213 | |
| 214 | Methods->push_back(MD); |
| 215 | } |
| 216 | |
| 217 | CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const { |
| 218 | if (!OverriddenMethods) |
| 219 | return 0; |
| 220 | |
| 221 | OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this); |
| 222 | if (it == OverriddenMethods->end()) |
| 223 | return 0; |
| 224 | return &(*it->second)[0]; |
| 225 | } |
| 226 | |
| 227 | CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const { |
| 228 | if (!OverriddenMethods) |
| 229 | return 0; |
| 230 | |
| 231 | OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this); |
| 232 | if (it == OverriddenMethods->end()) |
| 233 | return 0; |
| 234 | |
| 235 | return &(*it->second)[it->second->size()]; |
| 236 | } |
| 237 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 238 | QualType CXXMethodDecl::getThisType(ASTContext &C) const { |
Argyrios Kyrtzidis | b0d178d | 2008-10-24 22:28:18 +0000 | [diff] [blame] | 239 | // C++ 9.3.2p1: The type of this in a member function of a class X is X*. |
| 240 | // If the member function is declared const, the type of this is const X*, |
| 241 | // if the member function is declared volatile, the type of this is |
| 242 | // volatile X*, and if the member function is declared const volatile, |
| 243 | // the type of this is const volatile X*. |
| 244 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 245 | assert(isInstance() && "No 'this' for static methods!"); |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 246 | QualType ClassTy = C.getTagDeclType(const_cast<CXXRecordDecl*>(getParent())); |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 247 | ClassTy = ClassTy.getWithAdditionalQualifiers(getTypeQualifiers()); |
| 248 | return C.getPointerType(ClassTy).withConst(); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 251 | CXXBaseOrMemberInitializer:: |
| 252 | CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs) |
| 253 | : Args(0), NumArgs(0) { |
| 254 | BaseOrMember = reinterpret_cast<uintptr_t>(BaseType.getTypePtr()); |
| 255 | assert((BaseOrMember & 0x01) == 0 && "Invalid base class type pointer"); |
| 256 | BaseOrMember |= 0x01; |
| 257 | |
| 258 | if (NumArgs > 0) { |
| 259 | this->NumArgs = NumArgs; |
| 260 | this->Args = new Expr*[NumArgs]; |
| 261 | for (unsigned Idx = 0; Idx < NumArgs; ++Idx) |
| 262 | this->Args[Idx] = Args[Idx]; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | CXXBaseOrMemberInitializer:: |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 267 | CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs) |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 268 | : Args(0), NumArgs(0) { |
| 269 | BaseOrMember = reinterpret_cast<uintptr_t>(Member); |
| 270 | assert((BaseOrMember & 0x01) == 0 && "Invalid member pointer"); |
| 271 | |
| 272 | if (NumArgs > 0) { |
| 273 | this->NumArgs = NumArgs; |
| 274 | this->Args = new Expr*[NumArgs]; |
| 275 | for (unsigned Idx = 0; Idx < NumArgs; ++Idx) |
| 276 | this->Args[Idx] = Args[Idx]; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | CXXBaseOrMemberInitializer::~CXXBaseOrMemberInitializer() { |
| 281 | delete [] Args; |
| 282 | } |
| 283 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 284 | CXXConstructorDecl * |
| 285 | CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 286 | SourceLocation L, DeclarationName N, |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 287 | QualType T, bool isExplicit, |
| 288 | bool isInline, bool isImplicitlyDeclared) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 289 | assert(N.getNameKind() == DeclarationName::CXXConstructorName && |
| 290 | "Name must refer to a constructor"); |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 291 | return new (C) CXXConstructorDecl(RD, L, N, T, isExplicit, isInline, |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 292 | isImplicitlyDeclared); |
| 293 | } |
| 294 | |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 295 | bool CXXConstructorDecl::isDefaultConstructor() const { |
| 296 | // C++ [class.ctor]p5: |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 297 | // A default constructor for a class X is a constructor of class |
| 298 | // X that can be called without an argument. |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 299 | return (getNumParams() == 0) || |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 300 | (getNumParams() > 0 && getParamDecl(0)->getDefaultArg() != 0); |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | bool |
| 304 | CXXConstructorDecl::isCopyConstructor(ASTContext &Context, |
| 305 | unsigned &TypeQuals) const { |
| 306 | // C++ [class.copy]p2: |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 307 | // A non-template constructor for class X is a copy constructor |
| 308 | // if its first parameter is of type X&, const X&, volatile X& or |
| 309 | // const volatile X&, and either there are no other parameters |
| 310 | // or else all other parameters have default arguments (8.3.6). |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 311 | if ((getNumParams() < 1) || |
| 312 | (getNumParams() > 1 && getParamDecl(1)->getDefaultArg() == 0)) |
| 313 | return false; |
| 314 | |
| 315 | const ParmVarDecl *Param = getParamDecl(0); |
| 316 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 317 | // Do we have a reference type? Rvalue references don't count. |
| 318 | const LValueReferenceType *ParamRefType = |
| 319 | Param->getType()->getAsLValueReferenceType(); |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 320 | if (!ParamRefType) |
| 321 | return false; |
| 322 | |
| 323 | // Is it a reference to our class type? |
| 324 | QualType PointeeType |
| 325 | = Context.getCanonicalType(ParamRefType->getPointeeType()); |
| 326 | QualType ClassTy |
| 327 | = Context.getTagDeclType(const_cast<CXXRecordDecl*>(getParent())); |
| 328 | if (PointeeType.getUnqualifiedType() != ClassTy) |
| 329 | return false; |
| 330 | |
| 331 | // We have a copy constructor. |
| 332 | TypeQuals = PointeeType.getCVRQualifiers(); |
| 333 | return true; |
| 334 | } |
| 335 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 336 | bool CXXConstructorDecl::isConvertingConstructor() const { |
| 337 | // C++ [class.conv.ctor]p1: |
| 338 | // A constructor declared without the function-specifier explicit |
| 339 | // that can be called with a single parameter specifies a |
| 340 | // conversion from the type of its first parameter to the type of |
| 341 | // its class. Such a constructor is called a converting |
| 342 | // constructor. |
| 343 | if (isExplicit()) |
| 344 | return false; |
| 345 | |
| 346 | return (getNumParams() == 0 && |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 347 | getType()->getAsFunctionProtoType()->isVariadic()) || |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 348 | (getNumParams() == 1) || |
| 349 | (getNumParams() > 1 && getParamDecl(1)->getDefaultArg() != 0); |
| 350 | } |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 351 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 352 | CXXDestructorDecl * |
| 353 | CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 354 | SourceLocation L, DeclarationName N, |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 355 | QualType T, bool isInline, |
| 356 | bool isImplicitlyDeclared) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 357 | assert(N.getNameKind() == DeclarationName::CXXDestructorName && |
| 358 | "Name must refer to a destructor"); |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 359 | return new (C) CXXDestructorDecl(RD, L, N, T, isInline, |
| 360 | isImplicitlyDeclared); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 363 | CXXConversionDecl * |
| 364 | CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 365 | SourceLocation L, DeclarationName N, |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 366 | QualType T, bool isInline, bool isExplicit) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 367 | assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 368 | "Name must refer to a conversion function"); |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 369 | return new (C) CXXConversionDecl(RD, L, N, T, isInline, isExplicit); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 372 | OverloadedFunctionDecl * |
| 373 | OverloadedFunctionDecl::Create(ASTContext &C, DeclContext *DC, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 374 | DeclarationName N) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 375 | return new (C) OverloadedFunctionDecl(DC, N); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 376 | } |
Chris Lattner | 21ef7ae | 2008-11-04 16:51:42 +0000 | [diff] [blame] | 377 | |
| 378 | LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C, |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 379 | DeclContext *DC, |
Chris Lattner | 21ef7ae | 2008-11-04 16:51:42 +0000 | [diff] [blame] | 380 | SourceLocation L, |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 381 | LanguageIDs Lang, bool Braces) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 382 | return new (C) LinkageSpecDecl(DC, L, Lang, Braces); |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 383 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 384 | |
| 385 | UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC, |
| 386 | SourceLocation L, |
| 387 | SourceLocation NamespaceLoc, |
| 388 | SourceLocation IdentLoc, |
| 389 | NamespaceDecl *Used, |
| 390 | DeclContext *CommonAncestor) { |
| 391 | return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, IdentLoc, |
| 392 | Used, CommonAncestor); |
| 393 | } |
| 394 | |
Anders Carlsson | 68771c7 | 2009-03-28 22:58:02 +0000 | [diff] [blame] | 395 | NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC, |
| 396 | SourceLocation L, |
| 397 | SourceLocation AliasLoc, |
| 398 | IdentifierInfo *Alias, |
| 399 | SourceLocation IdentLoc, |
| 400 | NamedDecl *Namespace) { |
| 401 | return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, IdentLoc, |
| 402 | Namespace); |
| 403 | } |
| 404 | |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 405 | StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC, |
| 406 | SourceLocation L, Expr *AssertExpr, |
| 407 | StringLiteral *Message) { |
| 408 | return new (C) StaticAssertDecl(DC, L, AssertExpr, Message); |
| 409 | } |
| 410 | |
| 411 | void StaticAssertDecl::Destroy(ASTContext& C) { |
| 412 | AssertExpr->Destroy(C); |
| 413 | Message->Destroy(C); |
| 414 | this->~StaticAssertDecl(); |
| 415 | C.Deallocate((void *)this); |
| 416 | } |
| 417 | |
| 418 | StaticAssertDecl::~StaticAssertDecl() { |
| 419 | } |
| 420 | |
Anders Carlsson | 86fa750 | 2009-04-21 01:57:48 +0000 | [diff] [blame] | 421 | CXXTempVarDecl *CXXTempVarDecl::Create(ASTContext &C, DeclContext *DC, |
| 422 | QualType T) { |
Eli Friedman | 617bd45 | 2009-04-25 22:20:56 +0000 | [diff] [blame] | 423 | assert((T->isDependentType() || |
| 424 | isa<CXXRecordDecl>(T->getAsRecordType()->getDecl())) && |
Anders Carlsson | f627406 | 2009-04-24 06:06:07 +0000 | [diff] [blame] | 425 | "CXXTempVarDecl must either have a dependent type " |
| 426 | "or a C++ record type!"); |
Anders Carlsson | 86fa750 | 2009-04-21 01:57:48 +0000 | [diff] [blame] | 427 | return new (C) CXXTempVarDecl(DC, T); |
| 428 | } |
| 429 | |
Anders Carlsson | 05bf2c7 | 2009-03-26 23:46:50 +0000 | [diff] [blame] | 430 | static const char *getAccessName(AccessSpecifier AS) { |
| 431 | switch (AS) { |
| 432 | default: |
| 433 | case AS_none: |
| 434 | assert("Invalid access specifier!"); |
| 435 | return 0; |
| 436 | case AS_public: |
| 437 | return "public"; |
| 438 | case AS_private: |
| 439 | return "private"; |
| 440 | case AS_protected: |
| 441 | return "protected"; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB, |
| 446 | AccessSpecifier AS) { |
| 447 | return DB << getAccessName(AS); |
| 448 | } |
| 449 | |
| 450 | |