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