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