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" |
| 15 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 7d7e672 | 2008-11-12 23:21:09 +0000 | [diff] [blame] | 16 | #include "clang/Basic/IdentifierTable.h" |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 17 | using namespace clang; |
| 18 | |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | // Decl Allocation/Deallocation Method Implementations |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | |
| 23 | CXXFieldDecl *CXXFieldDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
| 24 | SourceLocation L, IdentifierInfo *Id, |
Sebastian Redl | a11f42f | 2008-11-17 23:24:37 +0000 | [diff] [blame] | 25 | QualType T, bool Mut, Expr *BW) { |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 26 | void *Mem = C.getAllocator().Allocate<CXXFieldDecl>(); |
Sebastian Redl | a11f42f | 2008-11-17 23:24:37 +0000 | [diff] [blame] | 27 | return new (Mem) CXXFieldDecl(RD, L, Id, T, Mut, BW); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 30 | CXXRecordDecl::CXXRecordDecl(TagKind TK, DeclContext *DC, |
Douglas Gregor | 7d7e672 | 2008-11-12 23:21:09 +0000 | [diff] [blame] | 31 | SourceLocation L, IdentifierInfo *Id) |
| 32 | : RecordDecl(CXXRecord, TK, DC, L, Id), DeclContext(CXXRecord), |
| 33 | UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false), |
| 34 | Aggregate(true), Polymorphic(false), Bases(0), NumBases(0), |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 35 | Constructors(DC, DeclarationName()), |
Douglas Gregor | 7d7e672 | 2008-11-12 23:21:09 +0000 | [diff] [blame] | 36 | Destructor(0), |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 37 | Conversions(DC, DeclarationName()) { } |
Douglas Gregor | 7d7e672 | 2008-11-12 23:21:09 +0000 | [diff] [blame] | 38 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 39 | CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC, |
| 40 | SourceLocation L, IdentifierInfo *Id, |
| 41 | CXXRecordDecl* PrevDecl) { |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 42 | void *Mem = C.getAllocator().Allocate<CXXRecordDecl>(); |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 43 | CXXRecordDecl* R = new (Mem) CXXRecordDecl(TK, DC, L, Id); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 44 | C.getTypeDeclType(R, PrevDecl); |
| 45 | return R; |
| 46 | } |
| 47 | |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 48 | CXXRecordDecl::~CXXRecordDecl() { |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 49 | delete [] Bases; |
| 50 | } |
| 51 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 52 | void CXXRecordDecl::Destroy(ASTContext &C) { |
| 53 | for (OverloadedFunctionDecl::function_iterator func |
| 54 | = Constructors.function_begin(); |
| 55 | func != Constructors.function_end(); ++func) |
| 56 | (*func)->Destroy(C); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 57 | |
| 58 | if (isDefinition()) |
| 59 | Destructor->Destroy(C); |
| 60 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 61 | for (OverloadedFunctionDecl::function_iterator func |
| 62 | = Conversions.function_begin(); |
| 63 | func != Conversions.function_end(); ++func) |
| 64 | (*func)->Destroy(C); |
| 65 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 66 | RecordDecl::Destroy(C); |
| 67 | } |
| 68 | |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 69 | void |
| 70 | CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases, |
| 71 | unsigned NumBases) { |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 72 | // C++ [dcl.init.aggr]p1: |
| 73 | // An aggregate is an array or a class (clause 9) with [...] |
| 74 | // no base classes [...]. |
| 75 | Aggregate = false; |
| 76 | |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 77 | if (this->Bases) |
| 78 | delete [] this->Bases; |
| 79 | |
| 80 | this->Bases = new CXXBaseSpecifier[NumBases]; |
| 81 | this->NumBases = NumBases; |
| 82 | for (unsigned i = 0; i < NumBases; ++i) |
| 83 | this->Bases[i] = *Bases[i]; |
| 84 | } |
| 85 | |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 86 | bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const { |
| 87 | for (OverloadedFunctionDecl::function_const_iterator Con |
| 88 | = Constructors.function_begin(); |
| 89 | Con != Constructors.function_end(); ++Con) { |
| 90 | unsigned TypeQuals; |
| 91 | if (cast<CXXConstructorDecl>(*Con)->isCopyConstructor(Context, TypeQuals) && |
| 92 | (TypeQuals & QualType::Const != 0)) |
| 93 | return true; |
| 94 | } |
| 95 | return false; |
| 96 | } |
| 97 | |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 98 | void |
| 99 | CXXRecordDecl::addConstructor(ASTContext &Context, |
| 100 | CXXConstructorDecl *ConDecl) { |
| 101 | if (!ConDecl->isImplicitlyDeclared()) { |
| 102 | // Note that we have a user-declared constructor. |
| 103 | UserDeclaredConstructor = true; |
| 104 | |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 105 | // C++ [dcl.init.aggr]p1: |
| 106 | // An aggregate is an array or a class (clause 9) with no |
| 107 | // user-declared constructors (12.1) [...]. |
| 108 | Aggregate = false; |
| 109 | |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 110 | // Note when we have a user-declared copy constructor, which will |
| 111 | // suppress the implicit declaration of a copy constructor. |
| 112 | if (ConDecl->isCopyConstructor(Context)) |
| 113 | UserDeclaredCopyConstructor = true; |
| 114 | } |
| 115 | |
| 116 | Constructors.addOverload(ConDecl); |
| 117 | } |
| 118 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 119 | void CXXRecordDecl::addConversionFunction(ASTContext &Context, |
| 120 | CXXConversionDecl *ConvDecl) { |
| 121 | Conversions.addOverload(ConvDecl); |
| 122 | } |
| 123 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 124 | CXXMethodDecl * |
| 125 | CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 126 | SourceLocation L, DeclarationName N, |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 127 | QualType T, bool isStatic, bool isInline, |
| 128 | ScopedDecl *PrevDecl) { |
| 129 | void *Mem = C.getAllocator().Allocate<CXXMethodDecl>(); |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 130 | return new (Mem) CXXMethodDecl(CXXMethod, RD, L, N, T, isStatic, isInline, |
| 131 | PrevDecl); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | QualType CXXMethodDecl::getThisType(ASTContext &C) const { |
Argyrios Kyrtzidis | b0d178d | 2008-10-24 22:28:18 +0000 | [diff] [blame] | 135 | // C++ 9.3.2p1: The type of this in a member function of a class X is X*. |
| 136 | // If the member function is declared const, the type of this is const X*, |
| 137 | // if the member function is declared volatile, the type of this is |
| 138 | // volatile X*, and if the member function is declared const volatile, |
| 139 | // the type of this is const volatile X*. |
| 140 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 141 | assert(isInstance() && "No 'this' for static methods!"); |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 142 | QualType ClassTy = C.getTagDeclType(const_cast<CXXRecordDecl*>(getParent())); |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 143 | ClassTy = ClassTy.getWithAdditionalQualifiers(getTypeQualifiers()); |
| 144 | return C.getPointerType(ClassTy).withConst(); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 147 | CXXBaseOrMemberInitializer:: |
| 148 | CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs) |
| 149 | : Args(0), NumArgs(0) { |
| 150 | BaseOrMember = reinterpret_cast<uintptr_t>(BaseType.getTypePtr()); |
| 151 | assert((BaseOrMember & 0x01) == 0 && "Invalid base class type pointer"); |
| 152 | BaseOrMember |= 0x01; |
| 153 | |
| 154 | if (NumArgs > 0) { |
| 155 | this->NumArgs = NumArgs; |
| 156 | this->Args = new Expr*[NumArgs]; |
| 157 | for (unsigned Idx = 0; Idx < NumArgs; ++Idx) |
| 158 | this->Args[Idx] = Args[Idx]; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | CXXBaseOrMemberInitializer:: |
| 163 | CXXBaseOrMemberInitializer(CXXFieldDecl *Member, Expr **Args, unsigned NumArgs) |
| 164 | : Args(0), NumArgs(0) { |
| 165 | BaseOrMember = reinterpret_cast<uintptr_t>(Member); |
| 166 | assert((BaseOrMember & 0x01) == 0 && "Invalid member pointer"); |
| 167 | |
| 168 | if (NumArgs > 0) { |
| 169 | this->NumArgs = NumArgs; |
| 170 | this->Args = new Expr*[NumArgs]; |
| 171 | for (unsigned Idx = 0; Idx < NumArgs; ++Idx) |
| 172 | this->Args[Idx] = Args[Idx]; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | CXXBaseOrMemberInitializer::~CXXBaseOrMemberInitializer() { |
| 177 | delete [] Args; |
| 178 | } |
| 179 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 180 | CXXConstructorDecl * |
| 181 | CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 182 | SourceLocation L, DeclarationName N, |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 183 | QualType T, bool isExplicit, |
| 184 | bool isInline, bool isImplicitlyDeclared) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 185 | assert(N.getNameKind() == DeclarationName::CXXConstructorName && |
| 186 | "Name must refer to a constructor"); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 187 | void *Mem = C.getAllocator().Allocate<CXXConstructorDecl>(); |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 188 | return new (Mem) CXXConstructorDecl(RD, L, N, T, isExplicit, isInline, |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 189 | isImplicitlyDeclared); |
| 190 | } |
| 191 | |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 192 | bool CXXConstructorDecl::isDefaultConstructor() const { |
| 193 | // C++ [class.ctor]p5: |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 194 | // A default constructor for a class X is a constructor of class |
| 195 | // X that can be called without an argument. |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 196 | return (getNumParams() == 0) || |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 197 | (getNumParams() > 0 && getParamDecl(0)->getDefaultArg() != 0); |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | bool |
| 201 | CXXConstructorDecl::isCopyConstructor(ASTContext &Context, |
| 202 | unsigned &TypeQuals) const { |
| 203 | // C++ [class.copy]p2: |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 204 | // A non-template constructor for class X is a copy constructor |
| 205 | // if its first parameter is of type X&, const X&, volatile X& or |
| 206 | // const volatile X&, and either there are no other parameters |
| 207 | // or else all other parameters have default arguments (8.3.6). |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 208 | if ((getNumParams() < 1) || |
| 209 | (getNumParams() > 1 && getParamDecl(1)->getDefaultArg() == 0)) |
| 210 | return false; |
| 211 | |
| 212 | const ParmVarDecl *Param = getParamDecl(0); |
| 213 | |
| 214 | // Do we have a reference type? |
| 215 | const ReferenceType *ParamRefType = Param->getType()->getAsReferenceType(); |
| 216 | if (!ParamRefType) |
| 217 | return false; |
| 218 | |
| 219 | // Is it a reference to our class type? |
| 220 | QualType PointeeType |
| 221 | = Context.getCanonicalType(ParamRefType->getPointeeType()); |
| 222 | QualType ClassTy |
| 223 | = Context.getTagDeclType(const_cast<CXXRecordDecl*>(getParent())); |
| 224 | if (PointeeType.getUnqualifiedType() != ClassTy) |
| 225 | return false; |
| 226 | |
| 227 | // We have a copy constructor. |
| 228 | TypeQuals = PointeeType.getCVRQualifiers(); |
| 229 | return true; |
| 230 | } |
| 231 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 232 | bool CXXConstructorDecl::isConvertingConstructor() const { |
| 233 | // C++ [class.conv.ctor]p1: |
| 234 | // A constructor declared without the function-specifier explicit |
| 235 | // that can be called with a single parameter specifies a |
| 236 | // conversion from the type of its first parameter to the type of |
| 237 | // its class. Such a constructor is called a converting |
| 238 | // constructor. |
| 239 | if (isExplicit()) |
| 240 | return false; |
| 241 | |
| 242 | return (getNumParams() == 0 && |
| 243 | getType()->getAsFunctionTypeProto()->isVariadic()) || |
| 244 | (getNumParams() == 1) || |
| 245 | (getNumParams() > 1 && getParamDecl(1)->getDefaultArg() != 0); |
| 246 | } |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 247 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 248 | CXXDestructorDecl * |
| 249 | CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 250 | SourceLocation L, DeclarationName N, |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 251 | QualType T, bool isInline, |
| 252 | bool isImplicitlyDeclared) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 253 | assert(N.getNameKind() == DeclarationName::CXXDestructorName && |
| 254 | "Name must refer to a destructor"); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 255 | void *Mem = C.getAllocator().Allocate<CXXDestructorDecl>(); |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 256 | return new (Mem) CXXDestructorDecl(RD, L, N, T, isInline, |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 257 | isImplicitlyDeclared); |
| 258 | } |
| 259 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 260 | CXXConversionDecl * |
| 261 | CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 262 | SourceLocation L, DeclarationName N, |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 263 | QualType T, bool isInline, bool isExplicit) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 264 | assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 265 | "Name must refer to a conversion function"); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 266 | void *Mem = C.getAllocator().Allocate<CXXConversionDecl>(); |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 267 | return new (Mem) CXXConversionDecl(RD, L, N, T, isInline, isExplicit); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 270 | CXXClassVarDecl *CXXClassVarDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
| 271 | SourceLocation L, IdentifierInfo *Id, |
| 272 | QualType T, ScopedDecl *PrevDecl) { |
| 273 | void *Mem = C.getAllocator().Allocate<CXXClassVarDecl>(); |
| 274 | return new (Mem) CXXClassVarDecl(RD, L, Id, T, PrevDecl); |
| 275 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 276 | |
| 277 | OverloadedFunctionDecl * |
| 278 | OverloadedFunctionDecl::Create(ASTContext &C, DeclContext *DC, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 279 | DeclarationName N) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 280 | void *Mem = C.getAllocator().Allocate<OverloadedFunctionDecl>(); |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 281 | return new (Mem) OverloadedFunctionDecl(DC, N); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 282 | } |
Chris Lattner | 21ef7ae | 2008-11-04 16:51:42 +0000 | [diff] [blame] | 283 | |
| 284 | LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C, |
| 285 | SourceLocation L, |
| 286 | LanguageIDs Lang, Decl *D) { |
| 287 | void *Mem = C.getAllocator().Allocate<LinkageSpecDecl>(); |
| 288 | return new (Mem) LinkageSpecDecl(L, Lang, D); |
| 289 | } |
| 290 | |