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