Ted Kremenek | a758d09 | 2007-08-24 20:21:10 +0000 | [diff] [blame] | 1 | //===--- ExprCXX.cpp - (C++) Expression AST Node Implementation -----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Ted Kremenek | a758d09 | 2007-08-24 20:21:10 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the subclesses of Expr class declared in ExprCXX.h |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Douglas Gregor | b460980 | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 14 | #include "clang/Basic/IdentifierTable.h" |
| 15 | #include "clang/AST/DeclCXX.h" |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclTemplate.h" |
Ted Kremenek | a758d09 | 2007-08-24 20:21:10 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExprCXX.h" |
| 18 | using namespace clang; |
| 19 | |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | // Child Iterators for iterating over subexpressions/substatements |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 24 | // CXXTypeidExpr - has child iterators if the operand is an expression |
| 25 | Stmt::child_iterator CXXTypeidExpr::child_begin() { |
Sebastian Redl | d457589 | 2008-12-03 23:17:54 +0000 | [diff] [blame] | 26 | return isTypeOperand() ? child_iterator() : &Operand.Ex; |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 27 | } |
| 28 | Stmt::child_iterator CXXTypeidExpr::child_end() { |
Sebastian Redl | d457589 | 2008-12-03 23:17:54 +0000 | [diff] [blame] | 29 | return isTypeOperand() ? child_iterator() : &Operand.Ex+1; |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 30 | } |
Ted Kremenek | a758d09 | 2007-08-24 20:21:10 +0000 | [diff] [blame] | 31 | |
Ted Kremenek | a758d09 | 2007-08-24 20:21:10 +0000 | [diff] [blame] | 32 | // CXXBoolLiteralExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 33 | Stmt::child_iterator CXXBoolLiteralExpr::child_begin() { |
| 34 | return child_iterator(); |
| 35 | } |
| 36 | Stmt::child_iterator CXXBoolLiteralExpr::child_end() { |
| 37 | return child_iterator(); |
| 38 | } |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 39 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 40 | // CXXNullPtrLiteralExpr |
| 41 | Stmt::child_iterator CXXNullPtrLiteralExpr::child_begin() { |
| 42 | return child_iterator(); |
| 43 | } |
| 44 | Stmt::child_iterator CXXNullPtrLiteralExpr::child_end() { |
| 45 | return child_iterator(); |
| 46 | } |
| 47 | |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 48 | // CXXThisExpr |
| 49 | Stmt::child_iterator CXXThisExpr::child_begin() { return child_iterator(); } |
| 50 | Stmt::child_iterator CXXThisExpr::child_end() { return child_iterator(); } |
| 51 | |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 52 | // CXXThrowExpr |
Ted Kremenek | 1060aff | 2008-06-17 03:11:08 +0000 | [diff] [blame] | 53 | Stmt::child_iterator CXXThrowExpr::child_begin() { return &Op; } |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 54 | Stmt::child_iterator CXXThrowExpr::child_end() { |
| 55 | // If Op is 0, we are processing throw; which has no children. |
Ted Kremenek | 1060aff | 2008-06-17 03:11:08 +0000 | [diff] [blame] | 56 | return Op ? &Op+1 : &Op; |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 57 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 58 | |
| 59 | // CXXDefaultArgExpr |
| 60 | Stmt::child_iterator CXXDefaultArgExpr::child_begin() { |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 61 | return child_iterator(); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 62 | } |
| 63 | Stmt::child_iterator CXXDefaultArgExpr::child_end() { |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 64 | return child_iterator(); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 65 | } |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 66 | |
| 67 | // CXXZeroInitValueExpr |
| 68 | Stmt::child_iterator CXXZeroInitValueExpr::child_begin() { |
| 69 | return child_iterator(); |
| 70 | } |
| 71 | Stmt::child_iterator CXXZeroInitValueExpr::child_end() { |
| 72 | return child_iterator(); |
| 73 | } |
Argyrios Kyrtzidis | 9e922b1 | 2008-09-09 23:47:53 +0000 | [diff] [blame] | 74 | |
| 75 | // CXXConditionDeclExpr |
| 76 | Stmt::child_iterator CXXConditionDeclExpr::child_begin() { |
| 77 | return getVarDecl(); |
| 78 | } |
| 79 | Stmt::child_iterator CXXConditionDeclExpr::child_end() { |
| 80 | return child_iterator(); |
| 81 | } |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 82 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 83 | // CXXNewExpr |
| 84 | CXXNewExpr::CXXNewExpr(bool globalNew, FunctionDecl *operatorNew, |
| 85 | Expr **placementArgs, unsigned numPlaceArgs, |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 86 | bool parenTypeId, Expr *arraySize, |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 87 | CXXConstructorDecl *constructor, bool initializer, |
| 88 | Expr **constructorArgs, unsigned numConsArgs, |
| 89 | FunctionDecl *operatorDelete, QualType ty, |
| 90 | SourceLocation startLoc, SourceLocation endLoc) |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 91 | : Expr(CXXNewExprClass, ty, ty->isDependentType(), ty->isDependentType()), |
| 92 | GlobalNew(globalNew), ParenTypeId(parenTypeId), |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 93 | Initializer(initializer), Array(arraySize), NumPlacementArgs(numPlaceArgs), |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 94 | NumConstructorArgs(numConsArgs), OperatorNew(operatorNew), |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 95 | OperatorDelete(operatorDelete), Constructor(constructor), |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 96 | StartLoc(startLoc), EndLoc(endLoc) |
| 97 | { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 98 | unsigned TotalSize = Array + NumPlacementArgs + NumConstructorArgs; |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 99 | SubExprs = new Stmt*[TotalSize]; |
| 100 | unsigned i = 0; |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 101 | if (Array) |
| 102 | SubExprs[i++] = arraySize; |
| 103 | for (unsigned j = 0; j < NumPlacementArgs; ++j) |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 104 | SubExprs[i++] = placementArgs[j]; |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 105 | for (unsigned j = 0; j < NumConstructorArgs; ++j) |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 106 | SubExprs[i++] = constructorArgs[j]; |
| 107 | assert(i == TotalSize); |
| 108 | } |
| 109 | |
| 110 | Stmt::child_iterator CXXNewExpr::child_begin() { return &SubExprs[0]; } |
| 111 | Stmt::child_iterator CXXNewExpr::child_end() { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 112 | return &SubExprs[0] + Array + getNumPlacementArgs() + getNumConstructorArgs(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | // CXXDeleteExpr |
| 116 | Stmt::child_iterator CXXDeleteExpr::child_begin() { return &Argument; } |
| 117 | Stmt::child_iterator CXXDeleteExpr::child_end() { return &Argument+1; } |
| 118 | |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 119 | // UnresolvedFunctionNameExpr |
| 120 | Stmt::child_iterator UnresolvedFunctionNameExpr::child_begin() { |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 121 | return child_iterator(); |
| 122 | } |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 123 | Stmt::child_iterator UnresolvedFunctionNameExpr::child_end() { |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 124 | return child_iterator(); |
| 125 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 126 | // UnaryTypeTraitExpr |
| 127 | Stmt::child_iterator UnaryTypeTraitExpr::child_begin() { |
| 128 | return child_iterator(); |
| 129 | } |
| 130 | Stmt::child_iterator UnaryTypeTraitExpr::child_end() { |
| 131 | return child_iterator(); |
| 132 | } |
| 133 | |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 134 | // UnresolvedDeclRefExpr |
| 135 | StmtIterator UnresolvedDeclRefExpr::child_begin() { |
| 136 | return child_iterator(); |
| 137 | } |
| 138 | |
| 139 | StmtIterator UnresolvedDeclRefExpr::child_end() { |
| 140 | return child_iterator(); |
| 141 | } |
| 142 | |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 143 | TemplateIdRefExpr::TemplateIdRefExpr(QualType T, |
| 144 | NestedNameSpecifier *Qualifier, |
| 145 | SourceRange QualifierRange, |
| 146 | TemplateName Template, |
| 147 | SourceLocation TemplateNameLoc, |
| 148 | SourceLocation LAngleLoc, |
| 149 | const TemplateArgument *TemplateArgs, |
| 150 | unsigned NumTemplateArgs, |
| 151 | SourceLocation RAngleLoc) |
| 152 | : Expr(TemplateIdRefExprClass, T, |
| 153 | (Template.isDependent() || |
| 154 | TemplateSpecializationType::anyDependentTemplateArguments( |
| 155 | TemplateArgs, NumTemplateArgs)), |
| 156 | (Template.isDependent() || |
| 157 | TemplateSpecializationType::anyDependentTemplateArguments( |
| 158 | TemplateArgs, NumTemplateArgs))), |
| 159 | Qualifier(Qualifier), QualifierRange(QualifierRange), Template(Template), |
| 160 | TemplateNameLoc(TemplateNameLoc), LAngleLoc(LAngleLoc), |
| 161 | RAngleLoc(RAngleLoc), NumTemplateArgs(NumTemplateArgs) |
| 162 | |
| 163 | { |
| 164 | TemplateArgument *StoredTemplateArgs |
| 165 | = reinterpret_cast<TemplateArgument *> (this+1); |
| 166 | for (unsigned I = 0; I != NumTemplateArgs; ++I) |
| 167 | new (StoredTemplateArgs + I) TemplateArgument(TemplateArgs[I]); |
| 168 | } |
| 169 | |
| 170 | TemplateIdRefExpr * |
| 171 | TemplateIdRefExpr::Create(ASTContext &Context, QualType T, |
| 172 | NestedNameSpecifier *Qualifier, |
| 173 | SourceRange QualifierRange, |
| 174 | TemplateName Template, SourceLocation TemplateNameLoc, |
| 175 | SourceLocation LAngleLoc, |
| 176 | const TemplateArgument *TemplateArgs, |
| 177 | unsigned NumTemplateArgs, SourceLocation RAngleLoc) { |
| 178 | void *Mem = Context.Allocate(sizeof(TemplateIdRefExpr) + |
| 179 | sizeof(TemplateArgument) * NumTemplateArgs); |
| 180 | return new (Mem) TemplateIdRefExpr(T, Qualifier, QualifierRange, Template, |
| 181 | TemplateNameLoc, LAngleLoc, TemplateArgs, |
| 182 | NumTemplateArgs, RAngleLoc); |
| 183 | } |
| 184 | |
Douglas Gregor | 42602bb | 2009-08-07 06:08:38 +0000 | [diff] [blame] | 185 | void TemplateIdRefExpr::DoDestroy(ASTContext &Context) { |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 186 | const TemplateArgument *TemplateArgs = getTemplateArgs(); |
| 187 | for (unsigned I = 0; I != NumTemplateArgs; ++I) |
| 188 | if (Expr *E = TemplateArgs[I].getAsExpr()) |
| 189 | E->Destroy(Context); |
Douglas Gregor | 42602bb | 2009-08-07 06:08:38 +0000 | [diff] [blame] | 190 | this->~TemplateIdRefExpr(); |
| 191 | Context.Deallocate(this); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | Stmt::child_iterator TemplateIdRefExpr::child_begin() { |
| 195 | // FIXME: Walk the expressions in the template arguments (?) |
| 196 | return Stmt::child_iterator(); |
| 197 | } |
| 198 | |
| 199 | Stmt::child_iterator TemplateIdRefExpr::child_end() { |
| 200 | // FIXME: Walk the expressions in the template arguments (?) |
| 201 | return Stmt::child_iterator(); |
| 202 | } |
| 203 | |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 204 | bool UnaryTypeTraitExpr::EvaluateTrait(ASTContext& C) const { |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 205 | switch(UTT) { |
| 206 | default: assert(false && "Unknown type trait or not implemented"); |
| 207 | case UTT_IsPOD: return QueriedType->isPODType(); |
| 208 | case UTT_IsClass: // Fallthrough |
| 209 | case UTT_IsUnion: |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 210 | if (const RecordType *Record = QueriedType->getAs<RecordType>()) { |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 211 | bool Union = Record->getDecl()->isUnion(); |
| 212 | return UTT == UTT_IsUnion ? Union : !Union; |
| 213 | } |
| 214 | return false; |
| 215 | case UTT_IsEnum: return QueriedType->isEnumeralType(); |
| 216 | case UTT_IsPolymorphic: |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 217 | if (const RecordType *Record = QueriedType->getAs<RecordType>()) { |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 218 | // Type traits are only parsed in C++, so we've got CXXRecords. |
| 219 | return cast<CXXRecordDecl>(Record->getDecl())->isPolymorphic(); |
| 220 | } |
| 221 | return false; |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 222 | case UTT_IsAbstract: |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 223 | if (const RecordType *RT = QueriedType->getAs<RecordType>()) |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 224 | return cast<CXXRecordDecl>(RT->getDecl())->isAbstract(); |
| 225 | return false; |
Anders Carlsson | 347ba89 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 226 | case UTT_HasTrivialConstructor: |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 227 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 228 | // If __is_pod (type) is true then the trait is true, else if type is |
| 229 | // a cv class or union type (or array thereof) with a trivial default |
| 230 | // constructor ([class.ctor]) then the trait is true, else it is false. |
| 231 | if (QueriedType->isPODType()) |
| 232 | return true; |
| 233 | if (const RecordType *RT = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 234 | C.getBaseElementType(QueriedType)->getAs<RecordType>()) |
Anders Carlsson | 347ba89 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 235 | return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialConstructor(); |
Anders Carlsson | 072abef | 2009-04-17 02:34:54 +0000 | [diff] [blame] | 236 | return false; |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 237 | case UTT_HasTrivialCopy: |
| 238 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 239 | // If __is_pod (type) is true or type is a reference type then |
| 240 | // the trait is true, else if type is a cv class or union type |
| 241 | // with a trivial copy constructor ([class.copy]) then the trait |
| 242 | // is true, else it is false. |
| 243 | if (QueriedType->isPODType() || QueriedType->isReferenceType()) |
| 244 | return true; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 245 | if (const RecordType *RT = QueriedType->getAs<RecordType>()) |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 246 | return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialCopyConstructor(); |
| 247 | return false; |
| 248 | case UTT_HasTrivialAssign: |
| 249 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 250 | // If type is const qualified or is a reference type then the |
| 251 | // trait is false. Otherwise if __is_pod (type) is true then the |
| 252 | // trait is true, else if type is a cv class or union type with |
| 253 | // a trivial copy assignment ([class.copy]) then the trait is |
| 254 | // true, else it is false. |
| 255 | // Note: the const and reference restrictions are interesting, |
| 256 | // given that const and reference members don't prevent a class |
| 257 | // from having a trivial copy assignment operator (but do cause |
| 258 | // errors if the copy assignment operator is actually used, q.v. |
| 259 | // [class.copy]p12). |
| 260 | |
| 261 | if (C.getBaseElementType(QueriedType).isConstQualified()) |
| 262 | return false; |
| 263 | if (QueriedType->isPODType()) |
| 264 | return true; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 265 | if (const RecordType *RT = QueriedType->getAs<RecordType>()) |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 266 | return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialCopyAssignment(); |
| 267 | return false; |
| 268 | case UTT_HasTrivialDestructor: |
| 269 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 270 | // If __is_pod (type) is true or type is a reference type |
| 271 | // then the trait is true, else if type is a cv class or union |
| 272 | // type (or array thereof) with a trivial destructor |
| 273 | // ([class.dtor]) then the trait is true, else it is |
| 274 | // false. |
| 275 | if (QueriedType->isPODType() || QueriedType->isReferenceType()) |
| 276 | return true; |
| 277 | if (const RecordType *RT = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 278 | C.getBaseElementType(QueriedType)->getAs<RecordType>()) |
Anders Carlsson | 072abef | 2009-04-17 02:34:54 +0000 | [diff] [blame] | 279 | return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialDestructor(); |
| 280 | return false; |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
Douglas Gregor | b460980 | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 284 | SourceRange CXXOperatorCallExpr::getSourceRange() const { |
| 285 | OverloadedOperatorKind Kind = getOperator(); |
| 286 | if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) { |
| 287 | if (getNumArgs() == 1) |
| 288 | // Prefix operator |
| 289 | return SourceRange(getOperatorLoc(), |
| 290 | getArg(0)->getSourceRange().getEnd()); |
| 291 | else |
| 292 | // Postfix operator |
| 293 | return SourceRange(getArg(0)->getSourceRange().getEnd(), |
| 294 | getOperatorLoc()); |
| 295 | } else if (Kind == OO_Call) { |
| 296 | return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc()); |
| 297 | } else if (Kind == OO_Subscript) { |
| 298 | return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc()); |
| 299 | } else if (getNumArgs() == 1) { |
| 300 | return SourceRange(getOperatorLoc(), getArg(0)->getSourceRange().getEnd()); |
| 301 | } else if (getNumArgs() == 2) { |
| 302 | return SourceRange(getArg(0)->getSourceRange().getBegin(), |
| 303 | getArg(1)->getSourceRange().getEnd()); |
| 304 | } else { |
| 305 | return SourceRange(); |
| 306 | } |
| 307 | } |
| 308 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 309 | Expr *CXXMemberCallExpr::getImplicitObjectArgument() { |
| 310 | if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(getCallee()->IgnoreParens())) |
| 311 | return MemExpr->getBase(); |
| 312 | |
| 313 | // FIXME: Will eventually need to cope with member pointers. |
| 314 | return 0; |
| 315 | } |
| 316 | |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 317 | //===----------------------------------------------------------------------===// |
| 318 | // Named casts |
| 319 | //===----------------------------------------------------------------------===// |
| 320 | |
| 321 | /// getCastName - Get the name of the C++ cast being used, e.g., |
| 322 | /// "static_cast", "dynamic_cast", "reinterpret_cast", or |
| 323 | /// "const_cast". The returned pointer must not be freed. |
| 324 | const char *CXXNamedCastExpr::getCastName() const { |
| 325 | switch (getStmtClass()) { |
| 326 | case CXXStaticCastExprClass: return "static_cast"; |
| 327 | case CXXDynamicCastExprClass: return "dynamic_cast"; |
| 328 | case CXXReinterpretCastExprClass: return "reinterpret_cast"; |
| 329 | case CXXConstCastExprClass: return "const_cast"; |
| 330 | default: return "<invalid cast>"; |
| 331 | } |
| 332 | } |
Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 333 | |
Anders Carlsson | c1ce477 | 2009-05-30 19:54:15 +0000 | [diff] [blame] | 334 | CXXTemporary *CXXTemporary::Create(ASTContext &C, |
Anders Carlsson | b859f35 | 2009-05-30 20:34:37 +0000 | [diff] [blame] | 335 | const CXXDestructorDecl *Destructor) { |
Anders Carlsson | 88eaf07 | 2009-05-30 22:38:53 +0000 | [diff] [blame] | 336 | return new (C) CXXTemporary(Destructor); |
| 337 | } |
| 338 | |
Douglas Gregor | 42602bb | 2009-08-07 06:08:38 +0000 | [diff] [blame] | 339 | void CXXTemporary::Destroy(ASTContext &Ctx) { |
Anders Carlsson | 88eaf07 | 2009-05-30 22:38:53 +0000 | [diff] [blame] | 340 | this->~CXXTemporary(); |
Douglas Gregor | 42602bb | 2009-08-07 06:08:38 +0000 | [diff] [blame] | 341 | Ctx.Deallocate(this); |
Anders Carlsson | c1ce477 | 2009-05-30 19:54:15 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Anders Carlsson | fceb0a8 | 2009-05-30 20:03:25 +0000 | [diff] [blame] | 344 | CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(ASTContext &C, |
| 345 | CXXTemporary *Temp, |
| 346 | Expr* SubExpr) { |
| 347 | assert(SubExpr->getType()->isRecordType() && |
| 348 | "Expression bound to a temporary must have record type!"); |
| 349 | |
Anders Carlsson | b859f35 | 2009-05-30 20:34:37 +0000 | [diff] [blame] | 350 | return new (C) CXXBindTemporaryExpr(Temp, SubExpr); |
Anders Carlsson | fceb0a8 | 2009-05-30 20:03:25 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Douglas Gregor | 42602bb | 2009-08-07 06:08:38 +0000 | [diff] [blame] | 353 | void CXXBindTemporaryExpr::DoDestroy(ASTContext &C) { |
Anders Carlsson | 88eaf07 | 2009-05-30 22:38:53 +0000 | [diff] [blame] | 354 | Temp->Destroy(C); |
| 355 | this->~CXXBindTemporaryExpr(); |
| 356 | C.Deallocate(this); |
| 357 | } |
| 358 | |
Anders Carlsson | 8e587a1 | 2009-05-30 20:56:46 +0000 | [diff] [blame] | 359 | CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(ASTContext &C, |
Anders Carlsson | 26de549 | 2009-04-24 05:23:13 +0000 | [diff] [blame] | 360 | CXXConstructorDecl *Cons, |
Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 361 | QualType writtenTy, |
| 362 | SourceLocation tyBeginLoc, |
| 363 | Expr **Args, |
| 364 | unsigned NumArgs, |
| 365 | SourceLocation rParenLoc) |
Anders Carlsson | 8e587a1 | 2009-05-30 20:56:46 +0000 | [diff] [blame] | 366 | : CXXConstructExpr(C, CXXTemporaryObjectExprClass, writtenTy, Cons, |
Anders Carlsson | 524fa13 | 2009-04-24 17:34:38 +0000 | [diff] [blame] | 367 | false, Args, NumArgs), |
| 368 | TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) { |
Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 369 | } |
Anders Carlsson | 19d28a6 | 2009-04-21 02:22:11 +0000 | [diff] [blame] | 370 | |
Anders Carlsson | 8e587a1 | 2009-05-30 20:56:46 +0000 | [diff] [blame] | 371 | CXXConstructExpr *CXXConstructExpr::Create(ASTContext &C, QualType T, |
| 372 | CXXConstructorDecl *D, bool Elidable, |
Anders Carlsson | e349bea | 2009-04-23 02:32:43 +0000 | [diff] [blame] | 373 | Expr **Args, unsigned NumArgs) { |
Anders Carlsson | 8e587a1 | 2009-05-30 20:56:46 +0000 | [diff] [blame] | 374 | return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, D, Elidable, |
Anders Carlsson | bd6734e | 2009-04-24 05:04:04 +0000 | [diff] [blame] | 375 | Args, NumArgs); |
Anders Carlsson | e349bea | 2009-04-23 02:32:43 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Anders Carlsson | 8e587a1 | 2009-05-30 20:56:46 +0000 | [diff] [blame] | 378 | CXXConstructExpr::CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T, |
| 379 | CXXConstructorDecl *D, bool elidable, |
Anders Carlsson | e349bea | 2009-04-23 02:32:43 +0000 | [diff] [blame] | 380 | Expr **args, unsigned numargs) |
Anders Carlsson | bd6734e | 2009-04-24 05:04:04 +0000 | [diff] [blame] | 381 | : Expr(SC, T, |
Anders Carlsson | e349bea | 2009-04-23 02:32:43 +0000 | [diff] [blame] | 382 | T->isDependentType(), |
| 383 | (T->isDependentType() || |
| 384 | CallExpr::hasAnyValueDependentArguments(args, numargs))), |
Anders Carlsson | 8e587a1 | 2009-05-30 20:56:46 +0000 | [diff] [blame] | 385 | Constructor(D), Elidable(elidable), Args(0), NumArgs(numargs) { |
Fariborz Jahanian | 2eeed7b | 2009-08-05 00:26:10 +0000 | [diff] [blame] | 386 | // leave room for default arguments; |
| 387 | FunctionDecl *FDecl = cast<FunctionDecl>(D); |
| 388 | unsigned NumArgsInProto = FDecl->param_size(); |
| 389 | NumArgs += (NumArgsInProto - numargs); |
Anders Carlsson | e349bea | 2009-04-23 02:32:43 +0000 | [diff] [blame] | 390 | if (NumArgs > 0) { |
| 391 | Args = new (C) Stmt*[NumArgs]; |
Fariborz Jahanian | 2eeed7b | 2009-08-05 00:26:10 +0000 | [diff] [blame] | 392 | for (unsigned i = 0; i < numargs; ++i) |
Anders Carlsson | e349bea | 2009-04-23 02:32:43 +0000 | [diff] [blame] | 393 | Args[i] = args[i]; |
| 394 | } |
| 395 | } |
| 396 | |
Douglas Gregor | 42602bb | 2009-08-07 06:08:38 +0000 | [diff] [blame] | 397 | void CXXConstructExpr::DoDestroy(ASTContext &C) { |
Anders Carlsson | e349bea | 2009-04-23 02:32:43 +0000 | [diff] [blame] | 398 | DestroyChildren(C); |
| 399 | if (Args) |
| 400 | C.Deallocate(Args); |
| 401 | this->~CXXConstructExpr(); |
| 402 | C.Deallocate(this); |
| 403 | } |
| 404 | |
Anders Carlsson | 2d44e8a | 2009-05-01 22:18:43 +0000 | [diff] [blame] | 405 | CXXExprWithTemporaries::CXXExprWithTemporaries(Expr *subexpr, |
Anders Carlsson | ff6b3d6 | 2009-05-30 21:05:25 +0000 | [diff] [blame] | 406 | CXXTemporary **temps, |
Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 407 | unsigned numtemps, |
Anders Carlsson | f54741e | 2009-06-16 03:37:31 +0000 | [diff] [blame] | 408 | bool shoulddestroytemps) |
Anders Carlsson | 2d44e8a | 2009-05-01 22:18:43 +0000 | [diff] [blame] | 409 | : Expr(CXXExprWithTemporariesClass, subexpr->getType(), |
Anders Carlsson | 02bbfa3 | 2009-04-24 22:47:04 +0000 | [diff] [blame] | 410 | subexpr->isTypeDependent(), subexpr->isValueDependent()), |
Anders Carlsson | f54741e | 2009-06-16 03:37:31 +0000 | [diff] [blame] | 411 | SubExpr(subexpr), Temps(0), NumTemps(numtemps), |
| 412 | ShouldDestroyTemps(shoulddestroytemps) { |
Anders Carlsson | ff6b3d6 | 2009-05-30 21:05:25 +0000 | [diff] [blame] | 413 | if (NumTemps > 0) { |
| 414 | Temps = new CXXTemporary*[NumTemps]; |
| 415 | for (unsigned i = 0; i < NumTemps; ++i) |
| 416 | Temps[i] = temps[i]; |
Anders Carlsson | 02bbfa3 | 2009-04-24 22:47:04 +0000 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | |
Anders Carlsson | 88eaf07 | 2009-05-30 22:38:53 +0000 | [diff] [blame] | 420 | CXXExprWithTemporaries *CXXExprWithTemporaries::Create(ASTContext &C, |
| 421 | Expr *SubExpr, |
| 422 | CXXTemporary **Temps, |
Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 423 | unsigned NumTemps, |
Anders Carlsson | f54741e | 2009-06-16 03:37:31 +0000 | [diff] [blame] | 424 | bool ShouldDestroyTemps){ |
Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 425 | return new (C) CXXExprWithTemporaries(SubExpr, Temps, NumTemps, |
Anders Carlsson | f54741e | 2009-06-16 03:37:31 +0000 | [diff] [blame] | 426 | ShouldDestroyTemps); |
Anders Carlsson | 88eaf07 | 2009-05-30 22:38:53 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Douglas Gregor | 42602bb | 2009-08-07 06:08:38 +0000 | [diff] [blame] | 429 | void CXXExprWithTemporaries::DoDestroy(ASTContext &C) { |
Anders Carlsson | 88eaf07 | 2009-05-30 22:38:53 +0000 | [diff] [blame] | 430 | DestroyChildren(C); |
| 431 | this->~CXXExprWithTemporaries(); |
| 432 | C.Deallocate(this); |
| 433 | } |
| 434 | |
Anders Carlsson | 2d44e8a | 2009-05-01 22:18:43 +0000 | [diff] [blame] | 435 | CXXExprWithTemporaries::~CXXExprWithTemporaries() { |
Anders Carlsson | ff6b3d6 | 2009-05-30 21:05:25 +0000 | [diff] [blame] | 436 | delete[] Temps; |
Anders Carlsson | 02bbfa3 | 2009-04-24 22:47:04 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Anders Carlsson | fceb0a8 | 2009-05-30 20:03:25 +0000 | [diff] [blame] | 439 | // CXXBindTemporaryExpr |
| 440 | Stmt::child_iterator CXXBindTemporaryExpr::child_begin() { |
| 441 | return &SubExpr; |
| 442 | } |
| 443 | |
| 444 | Stmt::child_iterator CXXBindTemporaryExpr::child_end() { |
| 445 | return &SubExpr + 1; |
| 446 | } |
| 447 | |
Anders Carlsson | e349bea | 2009-04-23 02:32:43 +0000 | [diff] [blame] | 448 | // CXXConstructExpr |
| 449 | Stmt::child_iterator CXXConstructExpr::child_begin() { |
| 450 | return &Args[0]; |
| 451 | } |
| 452 | Stmt::child_iterator CXXConstructExpr::child_end() { |
| 453 | return &Args[0]+NumArgs; |
| 454 | } |
| 455 | |
Anders Carlsson | 55674ac | 2009-05-01 22:21:22 +0000 | [diff] [blame] | 456 | // CXXExprWithTemporaries |
| 457 | Stmt::child_iterator CXXExprWithTemporaries::child_begin() { |
| 458 | return &SubExpr; |
Anders Carlsson | 19d28a6 | 2009-04-21 02:22:11 +0000 | [diff] [blame] | 459 | } |
Anders Carlsson | 02bbfa3 | 2009-04-24 22:47:04 +0000 | [diff] [blame] | 460 | |
Anders Carlsson | 55674ac | 2009-05-01 22:21:22 +0000 | [diff] [blame] | 461 | Stmt::child_iterator CXXExprWithTemporaries::child_end() { |
| 462 | return &SubExpr + 1; |
| 463 | } |
Anders Carlsson | 02bbfa3 | 2009-04-24 22:47:04 +0000 | [diff] [blame] | 464 | |
Douglas Gregor | d81e6ca | 2009-05-20 18:46:25 +0000 | [diff] [blame] | 465 | CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr( |
| 466 | SourceLocation TyBeginLoc, |
| 467 | QualType T, |
| 468 | SourceLocation LParenLoc, |
| 469 | Expr **Args, |
| 470 | unsigned NumArgs, |
| 471 | SourceLocation RParenLoc) |
| 472 | : Expr(CXXUnresolvedConstructExprClass, T.getNonReferenceType(), |
| 473 | T->isDependentType(), true), |
| 474 | TyBeginLoc(TyBeginLoc), |
| 475 | Type(T), |
| 476 | LParenLoc(LParenLoc), |
| 477 | RParenLoc(RParenLoc), |
| 478 | NumArgs(NumArgs) { |
| 479 | Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1); |
| 480 | memcpy(StoredArgs, Args, sizeof(Expr *) * NumArgs); |
| 481 | } |
| 482 | |
| 483 | CXXUnresolvedConstructExpr * |
| 484 | CXXUnresolvedConstructExpr::Create(ASTContext &C, |
| 485 | SourceLocation TyBegin, |
| 486 | QualType T, |
| 487 | SourceLocation LParenLoc, |
| 488 | Expr **Args, |
| 489 | unsigned NumArgs, |
| 490 | SourceLocation RParenLoc) { |
| 491 | void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) + |
| 492 | sizeof(Expr *) * NumArgs); |
| 493 | return new (Mem) CXXUnresolvedConstructExpr(TyBegin, T, LParenLoc, |
| 494 | Args, NumArgs, RParenLoc); |
| 495 | } |
| 496 | |
| 497 | Stmt::child_iterator CXXUnresolvedConstructExpr::child_begin() { |
| 498 | return child_iterator(reinterpret_cast<Stmt **>(this + 1)); |
| 499 | } |
| 500 | |
| 501 | Stmt::child_iterator CXXUnresolvedConstructExpr::child_end() { |
| 502 | return child_iterator(reinterpret_cast<Stmt **>(this + 1) + NumArgs); |
| 503 | } |
Sebastian Redl | 8b0b475 | 2009-05-16 18:50:46 +0000 | [diff] [blame] | 504 | |
Douglas Gregor | 1c0ca59 | 2009-05-22 21:13:27 +0000 | [diff] [blame] | 505 | Stmt::child_iterator CXXUnresolvedMemberExpr::child_begin() { |
| 506 | return child_iterator(&Base); |
| 507 | } |
| 508 | |
| 509 | Stmt::child_iterator CXXUnresolvedMemberExpr::child_end() { |
| 510 | return child_iterator(&Base + 1); |
| 511 | } |