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 | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 119 | // CXXPseudoDestructorExpr |
| 120 | Stmt::child_iterator CXXPseudoDestructorExpr::child_begin() { return &Base; } |
| 121 | Stmt::child_iterator CXXPseudoDestructorExpr::child_end() { |
| 122 | return &Base + 1; |
| 123 | } |
| 124 | |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 125 | // UnresolvedFunctionNameExpr |
| 126 | Stmt::child_iterator UnresolvedFunctionNameExpr::child_begin() { |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 127 | return child_iterator(); |
| 128 | } |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 129 | Stmt::child_iterator UnresolvedFunctionNameExpr::child_end() { |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 130 | return child_iterator(); |
| 131 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 132 | // UnaryTypeTraitExpr |
| 133 | Stmt::child_iterator UnaryTypeTraitExpr::child_begin() { |
| 134 | return child_iterator(); |
| 135 | } |
| 136 | Stmt::child_iterator UnaryTypeTraitExpr::child_end() { |
| 137 | return child_iterator(); |
| 138 | } |
| 139 | |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 140 | // UnresolvedDeclRefExpr |
| 141 | StmtIterator UnresolvedDeclRefExpr::child_begin() { |
| 142 | return child_iterator(); |
| 143 | } |
| 144 | |
| 145 | StmtIterator UnresolvedDeclRefExpr::child_end() { |
| 146 | return child_iterator(); |
| 147 | } |
| 148 | |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 149 | TemplateIdRefExpr::TemplateIdRefExpr(QualType T, |
| 150 | NestedNameSpecifier *Qualifier, |
| 151 | SourceRange QualifierRange, |
| 152 | TemplateName Template, |
| 153 | SourceLocation TemplateNameLoc, |
| 154 | SourceLocation LAngleLoc, |
| 155 | const TemplateArgument *TemplateArgs, |
| 156 | unsigned NumTemplateArgs, |
| 157 | SourceLocation RAngleLoc) |
| 158 | : Expr(TemplateIdRefExprClass, T, |
| 159 | (Template.isDependent() || |
| 160 | TemplateSpecializationType::anyDependentTemplateArguments( |
| 161 | TemplateArgs, NumTemplateArgs)), |
| 162 | (Template.isDependent() || |
| 163 | TemplateSpecializationType::anyDependentTemplateArguments( |
| 164 | TemplateArgs, NumTemplateArgs))), |
| 165 | Qualifier(Qualifier), QualifierRange(QualifierRange), Template(Template), |
| 166 | TemplateNameLoc(TemplateNameLoc), LAngleLoc(LAngleLoc), |
| 167 | RAngleLoc(RAngleLoc), NumTemplateArgs(NumTemplateArgs) |
| 168 | |
| 169 | { |
| 170 | TemplateArgument *StoredTemplateArgs |
| 171 | = reinterpret_cast<TemplateArgument *> (this+1); |
| 172 | for (unsigned I = 0; I != NumTemplateArgs; ++I) |
| 173 | new (StoredTemplateArgs + I) TemplateArgument(TemplateArgs[I]); |
| 174 | } |
| 175 | |
| 176 | TemplateIdRefExpr * |
| 177 | TemplateIdRefExpr::Create(ASTContext &Context, QualType T, |
| 178 | NestedNameSpecifier *Qualifier, |
| 179 | SourceRange QualifierRange, |
| 180 | TemplateName Template, SourceLocation TemplateNameLoc, |
| 181 | SourceLocation LAngleLoc, |
| 182 | const TemplateArgument *TemplateArgs, |
| 183 | unsigned NumTemplateArgs, SourceLocation RAngleLoc) { |
| 184 | void *Mem = Context.Allocate(sizeof(TemplateIdRefExpr) + |
| 185 | sizeof(TemplateArgument) * NumTemplateArgs); |
| 186 | return new (Mem) TemplateIdRefExpr(T, Qualifier, QualifierRange, Template, |
| 187 | TemplateNameLoc, LAngleLoc, TemplateArgs, |
| 188 | NumTemplateArgs, RAngleLoc); |
| 189 | } |
| 190 | |
Douglas Gregor | 42602bb | 2009-08-07 06:08:38 +0000 | [diff] [blame] | 191 | void TemplateIdRefExpr::DoDestroy(ASTContext &Context) { |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 192 | const TemplateArgument *TemplateArgs = getTemplateArgs(); |
| 193 | for (unsigned I = 0; I != NumTemplateArgs; ++I) |
| 194 | if (Expr *E = TemplateArgs[I].getAsExpr()) |
| 195 | E->Destroy(Context); |
Douglas Gregor | 42602bb | 2009-08-07 06:08:38 +0000 | [diff] [blame] | 196 | this->~TemplateIdRefExpr(); |
| 197 | Context.Deallocate(this); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | Stmt::child_iterator TemplateIdRefExpr::child_begin() { |
| 201 | // FIXME: Walk the expressions in the template arguments (?) |
| 202 | return Stmt::child_iterator(); |
| 203 | } |
| 204 | |
| 205 | Stmt::child_iterator TemplateIdRefExpr::child_end() { |
| 206 | // FIXME: Walk the expressions in the template arguments (?) |
| 207 | return Stmt::child_iterator(); |
| 208 | } |
| 209 | |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 210 | bool UnaryTypeTraitExpr::EvaluateTrait(ASTContext& C) const { |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 211 | switch(UTT) { |
| 212 | default: assert(false && "Unknown type trait or not implemented"); |
| 213 | case UTT_IsPOD: return QueriedType->isPODType(); |
| 214 | case UTT_IsClass: // Fallthrough |
| 215 | case UTT_IsUnion: |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 216 | if (const RecordType *Record = QueriedType->getAs<RecordType>()) { |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 217 | bool Union = Record->getDecl()->isUnion(); |
| 218 | return UTT == UTT_IsUnion ? Union : !Union; |
| 219 | } |
| 220 | return false; |
| 221 | case UTT_IsEnum: return QueriedType->isEnumeralType(); |
| 222 | case UTT_IsPolymorphic: |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 223 | if (const RecordType *Record = QueriedType->getAs<RecordType>()) { |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 224 | // Type traits are only parsed in C++, so we've got CXXRecords. |
| 225 | return cast<CXXRecordDecl>(Record->getDecl())->isPolymorphic(); |
| 226 | } |
| 227 | return false; |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 228 | case UTT_IsAbstract: |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 229 | if (const RecordType *RT = QueriedType->getAs<RecordType>()) |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 230 | return cast<CXXRecordDecl>(RT->getDecl())->isAbstract(); |
| 231 | return false; |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 232 | case UTT_IsEmpty: |
| 233 | if (const RecordType *Record = QueriedType->getAs<RecordType>()) { |
| 234 | return !Record->getDecl()->isUnion() |
| 235 | && cast<CXXRecordDecl>(Record->getDecl())->isEmpty(); |
| 236 | } |
| 237 | return false; |
Anders Carlsson | 347ba89 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 238 | case UTT_HasTrivialConstructor: |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 239 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 240 | // If __is_pod (type) is true then the trait is true, else if type is |
| 241 | // a cv class or union type (or array thereof) with a trivial default |
| 242 | // constructor ([class.ctor]) then the trait is true, else it is false. |
| 243 | if (QueriedType->isPODType()) |
| 244 | return true; |
| 245 | if (const RecordType *RT = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 246 | C.getBaseElementType(QueriedType)->getAs<RecordType>()) |
Anders Carlsson | 347ba89 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 247 | return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialConstructor(); |
Anders Carlsson | 072abef | 2009-04-17 02:34:54 +0000 | [diff] [blame] | 248 | return false; |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 249 | case UTT_HasTrivialCopy: |
| 250 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 251 | // If __is_pod (type) is true or type is a reference type then |
| 252 | // the trait is true, else if type is a cv class or union type |
| 253 | // with a trivial copy constructor ([class.copy]) then the trait |
| 254 | // is true, else it is false. |
| 255 | if (QueriedType->isPODType() || QueriedType->isReferenceType()) |
| 256 | return true; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 257 | if (const RecordType *RT = QueriedType->getAs<RecordType>()) |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 258 | return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialCopyConstructor(); |
| 259 | return false; |
| 260 | case UTT_HasTrivialAssign: |
| 261 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 262 | // If type is const qualified or is a reference type then the |
| 263 | // trait is false. Otherwise if __is_pod (type) is true then the |
| 264 | // trait is true, else if type is a cv class or union type with |
| 265 | // a trivial copy assignment ([class.copy]) then the trait is |
| 266 | // true, else it is false. |
| 267 | // Note: the const and reference restrictions are interesting, |
| 268 | // given that const and reference members don't prevent a class |
| 269 | // from having a trivial copy assignment operator (but do cause |
| 270 | // errors if the copy assignment operator is actually used, q.v. |
| 271 | // [class.copy]p12). |
| 272 | |
| 273 | if (C.getBaseElementType(QueriedType).isConstQualified()) |
| 274 | return false; |
| 275 | if (QueriedType->isPODType()) |
| 276 | return true; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 277 | if (const RecordType *RT = QueriedType->getAs<RecordType>()) |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 278 | return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialCopyAssignment(); |
| 279 | return false; |
| 280 | case UTT_HasTrivialDestructor: |
| 281 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 282 | // If __is_pod (type) is true or type is a reference type |
| 283 | // then the trait is true, else if type is a cv class or union |
| 284 | // type (or array thereof) with a trivial destructor |
| 285 | // ([class.dtor]) then the trait is true, else it is |
| 286 | // false. |
| 287 | if (QueriedType->isPODType() || QueriedType->isReferenceType()) |
| 288 | return true; |
| 289 | if (const RecordType *RT = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 290 | C.getBaseElementType(QueriedType)->getAs<RecordType>()) |
Anders Carlsson | 072abef | 2009-04-17 02:34:54 +0000 | [diff] [blame] | 291 | return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialDestructor(); |
| 292 | return false; |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
Douglas Gregor | b460980 | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 296 | SourceRange CXXOperatorCallExpr::getSourceRange() const { |
| 297 | OverloadedOperatorKind Kind = getOperator(); |
| 298 | if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) { |
| 299 | if (getNumArgs() == 1) |
| 300 | // Prefix operator |
| 301 | return SourceRange(getOperatorLoc(), |
| 302 | getArg(0)->getSourceRange().getEnd()); |
| 303 | else |
| 304 | // Postfix operator |
| 305 | return SourceRange(getArg(0)->getSourceRange().getEnd(), |
| 306 | getOperatorLoc()); |
| 307 | } else if (Kind == OO_Call) { |
| 308 | return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc()); |
| 309 | } else if (Kind == OO_Subscript) { |
| 310 | return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc()); |
| 311 | } else if (getNumArgs() == 1) { |
| 312 | return SourceRange(getOperatorLoc(), getArg(0)->getSourceRange().getEnd()); |
| 313 | } else if (getNumArgs() == 2) { |
| 314 | return SourceRange(getArg(0)->getSourceRange().getBegin(), |
| 315 | getArg(1)->getSourceRange().getEnd()); |
| 316 | } else { |
| 317 | return SourceRange(); |
| 318 | } |
| 319 | } |
| 320 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 321 | Expr *CXXMemberCallExpr::getImplicitObjectArgument() { |
| 322 | if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(getCallee()->IgnoreParens())) |
| 323 | return MemExpr->getBase(); |
| 324 | |
| 325 | // FIXME: Will eventually need to cope with member pointers. |
| 326 | return 0; |
| 327 | } |
| 328 | |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 329 | //===----------------------------------------------------------------------===// |
| 330 | // Named casts |
| 331 | //===----------------------------------------------------------------------===// |
| 332 | |
| 333 | /// getCastName - Get the name of the C++ cast being used, e.g., |
| 334 | /// "static_cast", "dynamic_cast", "reinterpret_cast", or |
| 335 | /// "const_cast". The returned pointer must not be freed. |
| 336 | const char *CXXNamedCastExpr::getCastName() const { |
| 337 | switch (getStmtClass()) { |
| 338 | case CXXStaticCastExprClass: return "static_cast"; |
| 339 | case CXXDynamicCastExprClass: return "dynamic_cast"; |
| 340 | case CXXReinterpretCastExprClass: return "reinterpret_cast"; |
| 341 | case CXXConstCastExprClass: return "const_cast"; |
| 342 | default: return "<invalid cast>"; |
| 343 | } |
| 344 | } |
Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 345 | |
Anders Carlsson | c1ce477 | 2009-05-30 19:54:15 +0000 | [diff] [blame] | 346 | CXXTemporary *CXXTemporary::Create(ASTContext &C, |
Anders Carlsson | b859f35 | 2009-05-30 20:34:37 +0000 | [diff] [blame] | 347 | const CXXDestructorDecl *Destructor) { |
Anders Carlsson | 88eaf07 | 2009-05-30 22:38:53 +0000 | [diff] [blame] | 348 | return new (C) CXXTemporary(Destructor); |
| 349 | } |
| 350 | |
Douglas Gregor | 42602bb | 2009-08-07 06:08:38 +0000 | [diff] [blame] | 351 | void CXXTemporary::Destroy(ASTContext &Ctx) { |
Anders Carlsson | 88eaf07 | 2009-05-30 22:38:53 +0000 | [diff] [blame] | 352 | this->~CXXTemporary(); |
Douglas Gregor | 42602bb | 2009-08-07 06:08:38 +0000 | [diff] [blame] | 353 | Ctx.Deallocate(this); |
Anders Carlsson | c1ce477 | 2009-05-30 19:54:15 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Anders Carlsson | fceb0a8 | 2009-05-30 20:03:25 +0000 | [diff] [blame] | 356 | CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(ASTContext &C, |
| 357 | CXXTemporary *Temp, |
| 358 | Expr* SubExpr) { |
| 359 | assert(SubExpr->getType()->isRecordType() && |
| 360 | "Expression bound to a temporary must have record type!"); |
| 361 | |
Anders Carlsson | b859f35 | 2009-05-30 20:34:37 +0000 | [diff] [blame] | 362 | return new (C) CXXBindTemporaryExpr(Temp, SubExpr); |
Anders Carlsson | fceb0a8 | 2009-05-30 20:03:25 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Douglas Gregor | 42602bb | 2009-08-07 06:08:38 +0000 | [diff] [blame] | 365 | void CXXBindTemporaryExpr::DoDestroy(ASTContext &C) { |
Anders Carlsson | 88eaf07 | 2009-05-30 22:38:53 +0000 | [diff] [blame] | 366 | Temp->Destroy(C); |
| 367 | this->~CXXBindTemporaryExpr(); |
| 368 | C.Deallocate(this); |
| 369 | } |
| 370 | |
Anders Carlsson | 8e587a1 | 2009-05-30 20:56:46 +0000 | [diff] [blame] | 371 | CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(ASTContext &C, |
Anders Carlsson | 26de549 | 2009-04-24 05:23:13 +0000 | [diff] [blame] | 372 | CXXConstructorDecl *Cons, |
Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 373 | QualType writtenTy, |
| 374 | SourceLocation tyBeginLoc, |
| 375 | Expr **Args, |
| 376 | unsigned NumArgs, |
| 377 | SourceLocation rParenLoc) |
Anders Carlsson | 8e587a1 | 2009-05-30 20:56:46 +0000 | [diff] [blame] | 378 | : CXXConstructExpr(C, CXXTemporaryObjectExprClass, writtenTy, Cons, |
Anders Carlsson | 524fa13 | 2009-04-24 17:34:38 +0000 | [diff] [blame] | 379 | false, Args, NumArgs), |
| 380 | TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) { |
Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 381 | } |
Anders Carlsson | 19d28a6 | 2009-04-21 02:22:11 +0000 | [diff] [blame] | 382 | |
Anders Carlsson | 8e587a1 | 2009-05-30 20:56:46 +0000 | [diff] [blame] | 383 | CXXConstructExpr *CXXConstructExpr::Create(ASTContext &C, QualType T, |
| 384 | CXXConstructorDecl *D, bool Elidable, |
Anders Carlsson | e349bea | 2009-04-23 02:32:43 +0000 | [diff] [blame] | 385 | Expr **Args, unsigned NumArgs) { |
Anders Carlsson | 8e587a1 | 2009-05-30 20:56:46 +0000 | [diff] [blame] | 386 | return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, D, Elidable, |
Anders Carlsson | bd6734e | 2009-04-24 05:04:04 +0000 | [diff] [blame] | 387 | Args, NumArgs); |
Anders Carlsson | e349bea | 2009-04-23 02:32:43 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Anders Carlsson | 8e587a1 | 2009-05-30 20:56:46 +0000 | [diff] [blame] | 390 | CXXConstructExpr::CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T, |
| 391 | CXXConstructorDecl *D, bool elidable, |
Anders Carlsson | e349bea | 2009-04-23 02:32:43 +0000 | [diff] [blame] | 392 | Expr **args, unsigned numargs) |
Anders Carlsson | bd6734e | 2009-04-24 05:04:04 +0000 | [diff] [blame] | 393 | : Expr(SC, T, |
Anders Carlsson | e349bea | 2009-04-23 02:32:43 +0000 | [diff] [blame] | 394 | T->isDependentType(), |
| 395 | (T->isDependentType() || |
| 396 | CallExpr::hasAnyValueDependentArguments(args, numargs))), |
Anders Carlsson | 8e587a1 | 2009-05-30 20:56:46 +0000 | [diff] [blame] | 397 | Constructor(D), Elidable(elidable), Args(0), NumArgs(numargs) { |
Fariborz Jahanian | 2eeed7b | 2009-08-05 00:26:10 +0000 | [diff] [blame] | 398 | // leave room for default arguments; |
Anders Carlsson | bcb11d0 | 2009-09-08 01:23:37 +0000 | [diff] [blame] | 399 | const FunctionProtoType *FTy = |
| 400 | cast<FunctionDecl>(D)->getType()->getAsFunctionProtoType(); |
| 401 | |
| 402 | unsigned NumArgsInProto = FTy->getNumArgs(); |
| 403 | unsigned NumArgsToAllocate = FTy->isVariadic() ? NumArgs : NumArgsInProto; |
| 404 | if (NumArgsToAllocate) { |
| 405 | Args = new (C) Stmt*[NumArgsToAllocate]; |
| 406 | |
| 407 | for (unsigned i = 0; i != NumArgs; ++i) |
Anders Carlsson | e349bea | 2009-04-23 02:32:43 +0000 | [diff] [blame] | 408 | Args[i] = args[i]; |
Anders Carlsson | bcb11d0 | 2009-09-08 01:23:37 +0000 | [diff] [blame] | 409 | |
| 410 | // Set default arguments to 0. |
| 411 | for (unsigned i = NumArgs; i != NumArgsToAllocate; ++i) |
Anders Carlsson | 21e1c4e | 2009-09-06 16:54:02 +0000 | [diff] [blame] | 412 | Args[i] = 0; |
Anders Carlsson | bcb11d0 | 2009-09-08 01:23:37 +0000 | [diff] [blame] | 413 | |
| 414 | NumArgs = NumArgsToAllocate; |
Anders Carlsson | e349bea | 2009-04-23 02:32:43 +0000 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | |
Douglas Gregor | 42602bb | 2009-08-07 06:08:38 +0000 | [diff] [blame] | 418 | void CXXConstructExpr::DoDestroy(ASTContext &C) { |
Anders Carlsson | e349bea | 2009-04-23 02:32:43 +0000 | [diff] [blame] | 419 | DestroyChildren(C); |
| 420 | if (Args) |
| 421 | C.Deallocate(Args); |
| 422 | this->~CXXConstructExpr(); |
| 423 | C.Deallocate(this); |
| 424 | } |
| 425 | |
Anders Carlsson | 2d44e8a | 2009-05-01 22:18:43 +0000 | [diff] [blame] | 426 | CXXExprWithTemporaries::CXXExprWithTemporaries(Expr *subexpr, |
Anders Carlsson | ff6b3d6 | 2009-05-30 21:05:25 +0000 | [diff] [blame] | 427 | CXXTemporary **temps, |
Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 428 | unsigned numtemps, |
Anders Carlsson | f54741e | 2009-06-16 03:37:31 +0000 | [diff] [blame] | 429 | bool shoulddestroytemps) |
Anders Carlsson | 2d44e8a | 2009-05-01 22:18:43 +0000 | [diff] [blame] | 430 | : Expr(CXXExprWithTemporariesClass, subexpr->getType(), |
Anders Carlsson | 02bbfa3 | 2009-04-24 22:47:04 +0000 | [diff] [blame] | 431 | subexpr->isTypeDependent(), subexpr->isValueDependent()), |
Anders Carlsson | f54741e | 2009-06-16 03:37:31 +0000 | [diff] [blame] | 432 | SubExpr(subexpr), Temps(0), NumTemps(numtemps), |
| 433 | ShouldDestroyTemps(shoulddestroytemps) { |
Anders Carlsson | ff6b3d6 | 2009-05-30 21:05:25 +0000 | [diff] [blame] | 434 | if (NumTemps > 0) { |
| 435 | Temps = new CXXTemporary*[NumTemps]; |
| 436 | for (unsigned i = 0; i < NumTemps; ++i) |
| 437 | Temps[i] = temps[i]; |
Anders Carlsson | 02bbfa3 | 2009-04-24 22:47:04 +0000 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
Anders Carlsson | 88eaf07 | 2009-05-30 22:38:53 +0000 | [diff] [blame] | 441 | CXXExprWithTemporaries *CXXExprWithTemporaries::Create(ASTContext &C, |
| 442 | Expr *SubExpr, |
| 443 | CXXTemporary **Temps, |
Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 444 | unsigned NumTemps, |
Anders Carlsson | f54741e | 2009-06-16 03:37:31 +0000 | [diff] [blame] | 445 | bool ShouldDestroyTemps){ |
Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 446 | return new (C) CXXExprWithTemporaries(SubExpr, Temps, NumTemps, |
Anders Carlsson | f54741e | 2009-06-16 03:37:31 +0000 | [diff] [blame] | 447 | ShouldDestroyTemps); |
Anders Carlsson | 88eaf07 | 2009-05-30 22:38:53 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Douglas Gregor | 42602bb | 2009-08-07 06:08:38 +0000 | [diff] [blame] | 450 | void CXXExprWithTemporaries::DoDestroy(ASTContext &C) { |
Anders Carlsson | 88eaf07 | 2009-05-30 22:38:53 +0000 | [diff] [blame] | 451 | DestroyChildren(C); |
| 452 | this->~CXXExprWithTemporaries(); |
| 453 | C.Deallocate(this); |
| 454 | } |
| 455 | |
Anders Carlsson | 2d44e8a | 2009-05-01 22:18:43 +0000 | [diff] [blame] | 456 | CXXExprWithTemporaries::~CXXExprWithTemporaries() { |
Anders Carlsson | ff6b3d6 | 2009-05-30 21:05:25 +0000 | [diff] [blame] | 457 | delete[] Temps; |
Anders Carlsson | 02bbfa3 | 2009-04-24 22:47:04 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Anders Carlsson | fceb0a8 | 2009-05-30 20:03:25 +0000 | [diff] [blame] | 460 | // CXXBindTemporaryExpr |
| 461 | Stmt::child_iterator CXXBindTemporaryExpr::child_begin() { |
| 462 | return &SubExpr; |
| 463 | } |
| 464 | |
| 465 | Stmt::child_iterator CXXBindTemporaryExpr::child_end() { |
| 466 | return &SubExpr + 1; |
| 467 | } |
| 468 | |
Anders Carlsson | e349bea | 2009-04-23 02:32:43 +0000 | [diff] [blame] | 469 | // CXXConstructExpr |
| 470 | Stmt::child_iterator CXXConstructExpr::child_begin() { |
| 471 | return &Args[0]; |
| 472 | } |
| 473 | Stmt::child_iterator CXXConstructExpr::child_end() { |
| 474 | return &Args[0]+NumArgs; |
| 475 | } |
| 476 | |
Anders Carlsson | 55674ac | 2009-05-01 22:21:22 +0000 | [diff] [blame] | 477 | // CXXExprWithTemporaries |
| 478 | Stmt::child_iterator CXXExprWithTemporaries::child_begin() { |
| 479 | return &SubExpr; |
Anders Carlsson | 19d28a6 | 2009-04-21 02:22:11 +0000 | [diff] [blame] | 480 | } |
Anders Carlsson | 02bbfa3 | 2009-04-24 22:47:04 +0000 | [diff] [blame] | 481 | |
Anders Carlsson | 55674ac | 2009-05-01 22:21:22 +0000 | [diff] [blame] | 482 | Stmt::child_iterator CXXExprWithTemporaries::child_end() { |
| 483 | return &SubExpr + 1; |
| 484 | } |
Anders Carlsson | 02bbfa3 | 2009-04-24 22:47:04 +0000 | [diff] [blame] | 485 | |
Douglas Gregor | d81e6ca | 2009-05-20 18:46:25 +0000 | [diff] [blame] | 486 | CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr( |
| 487 | SourceLocation TyBeginLoc, |
| 488 | QualType T, |
| 489 | SourceLocation LParenLoc, |
| 490 | Expr **Args, |
| 491 | unsigned NumArgs, |
| 492 | SourceLocation RParenLoc) |
| 493 | : Expr(CXXUnresolvedConstructExprClass, T.getNonReferenceType(), |
| 494 | T->isDependentType(), true), |
| 495 | TyBeginLoc(TyBeginLoc), |
| 496 | Type(T), |
| 497 | LParenLoc(LParenLoc), |
| 498 | RParenLoc(RParenLoc), |
| 499 | NumArgs(NumArgs) { |
| 500 | Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1); |
| 501 | memcpy(StoredArgs, Args, sizeof(Expr *) * NumArgs); |
| 502 | } |
| 503 | |
| 504 | CXXUnresolvedConstructExpr * |
| 505 | CXXUnresolvedConstructExpr::Create(ASTContext &C, |
| 506 | SourceLocation TyBegin, |
| 507 | QualType T, |
| 508 | SourceLocation LParenLoc, |
| 509 | Expr **Args, |
| 510 | unsigned NumArgs, |
| 511 | SourceLocation RParenLoc) { |
| 512 | void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) + |
| 513 | sizeof(Expr *) * NumArgs); |
| 514 | return new (Mem) CXXUnresolvedConstructExpr(TyBegin, T, LParenLoc, |
| 515 | Args, NumArgs, RParenLoc); |
| 516 | } |
| 517 | |
| 518 | Stmt::child_iterator CXXUnresolvedConstructExpr::child_begin() { |
| 519 | return child_iterator(reinterpret_cast<Stmt **>(this + 1)); |
| 520 | } |
| 521 | |
| 522 | Stmt::child_iterator CXXUnresolvedConstructExpr::child_end() { |
| 523 | return child_iterator(reinterpret_cast<Stmt **>(this + 1) + NumArgs); |
| 524 | } |
Sebastian Redl | 8b0b475 | 2009-05-16 18:50:46 +0000 | [diff] [blame] | 525 | |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 526 | CXXUnresolvedMemberExpr::CXXUnresolvedMemberExpr(ASTContext &C, |
| 527 | Expr *Base, bool IsArrow, |
| 528 | SourceLocation OperatorLoc, |
| 529 | NestedNameSpecifier *Qualifier, |
| 530 | SourceRange QualifierRange, |
| 531 | NamedDecl *FirstQualifierFoundInScope, |
| 532 | DeclarationName Member, |
| 533 | SourceLocation MemberLoc, |
| 534 | bool HasExplicitTemplateArgs, |
| 535 | SourceLocation LAngleLoc, |
| 536 | const TemplateArgument *TemplateArgs, |
| 537 | unsigned NumTemplateArgs, |
| 538 | SourceLocation RAngleLoc) |
| 539 | : Expr(CXXUnresolvedMemberExprClass, C.DependentTy, true, true), |
| 540 | Base(Base), IsArrow(IsArrow), |
| 541 | HasExplicitTemplateArgumentList(HasExplicitTemplateArgs), |
| 542 | OperatorLoc(OperatorLoc), |
| 543 | Qualifier(Qualifier), QualifierRange(QualifierRange), |
| 544 | FirstQualifierFoundInScope(FirstQualifierFoundInScope), |
| 545 | Member(Member), MemberLoc(MemberLoc) |
| 546 | { |
| 547 | if (HasExplicitTemplateArgumentList) { |
| 548 | ExplicitTemplateArgumentList *ETemplateArgs |
| 549 | = getExplicitTemplateArgumentList(); |
| 550 | ETemplateArgs->LAngleLoc = LAngleLoc; |
| 551 | ETemplateArgs->RAngleLoc = RAngleLoc; |
| 552 | ETemplateArgs->NumTemplateArgs = NumTemplateArgs; |
| 553 | |
| 554 | TemplateArgument *SavedTemplateArgs = ETemplateArgs->getTemplateArgs(); |
| 555 | for (unsigned I = 0; I < NumTemplateArgs; ++I) |
| 556 | new (SavedTemplateArgs + I) TemplateArgument(TemplateArgs[I]); |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | CXXUnresolvedMemberExpr * |
| 561 | CXXUnresolvedMemberExpr::Create(ASTContext &C, |
| 562 | Expr *Base, bool IsArrow, |
| 563 | SourceLocation OperatorLoc, |
| 564 | NestedNameSpecifier *Qualifier, |
| 565 | SourceRange QualifierRange, |
| 566 | NamedDecl *FirstQualifierFoundInScope, |
| 567 | DeclarationName Member, |
| 568 | SourceLocation MemberLoc, |
| 569 | bool HasExplicitTemplateArgs, |
| 570 | SourceLocation LAngleLoc, |
| 571 | const TemplateArgument *TemplateArgs, |
| 572 | unsigned NumTemplateArgs, |
| 573 | SourceLocation RAngleLoc) |
| 574 | { |
| 575 | if (!HasExplicitTemplateArgs) |
| 576 | return new (C) CXXUnresolvedMemberExpr(C, Base, IsArrow, OperatorLoc, |
| 577 | Qualifier, QualifierRange, |
| 578 | FirstQualifierFoundInScope, |
| 579 | Member, MemberLoc); |
| 580 | |
| 581 | void *Mem = C.Allocate(sizeof(CXXUnresolvedMemberExpr) + |
| 582 | sizeof(ExplicitTemplateArgumentList) + |
| 583 | sizeof(TemplateArgument) * NumTemplateArgs, |
| 584 | llvm::alignof<CXXUnresolvedMemberExpr>()); |
| 585 | return new (Mem) CXXUnresolvedMemberExpr(C, Base, IsArrow, OperatorLoc, |
| 586 | Qualifier, QualifierRange, |
| 587 | FirstQualifierFoundInScope, |
| 588 | Member, |
| 589 | MemberLoc, |
| 590 | HasExplicitTemplateArgs, |
| 591 | LAngleLoc, |
| 592 | TemplateArgs, |
| 593 | NumTemplateArgs, |
| 594 | RAngleLoc); |
| 595 | } |
| 596 | |
Douglas Gregor | 1c0ca59 | 2009-05-22 21:13:27 +0000 | [diff] [blame] | 597 | Stmt::child_iterator CXXUnresolvedMemberExpr::child_begin() { |
| 598 | return child_iterator(&Base); |
| 599 | } |
| 600 | |
| 601 | Stmt::child_iterator CXXUnresolvedMemberExpr::child_end() { |
| 602 | return child_iterator(&Base + 1); |
| 603 | } |