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