Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- Expr.cpp - 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. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Expr class and subclasses. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 14 | #include "clang/AST/Expr.h" |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 15 | #include "clang/AST/APValue.h" |
Chris Lattner | 2eadfb6 | 2007-07-15 23:32:58 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclCXX.h" |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclTemplate.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 20 | #include "clang/AST/RecordLayout.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | #include "clang/AST/StmtVisitor.h" |
Chris Lattner | da5a6b6 | 2007-11-27 18:22:04 +0000 | [diff] [blame] | 22 | #include "clang/Basic/TargetInfo.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | // Primary Expressions. |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | |
Chris Lattner | da8249e | 2008-06-07 22:13:43 +0000 | [diff] [blame] | 29 | /// getValueAsApproximateDouble - This returns the value as an inaccurate |
| 30 | /// double. Note that this may cause loss of precision, but is useful for |
| 31 | /// debugging dumps, etc. |
| 32 | double FloatingLiteral::getValueAsApproximateDouble() const { |
| 33 | llvm::APFloat V = getValue(); |
Dale Johannesen | ee5a700 | 2008-10-09 23:02:32 +0000 | [diff] [blame] | 34 | bool ignored; |
| 35 | V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven, |
| 36 | &ignored); |
Chris Lattner | da8249e | 2008-06-07 22:13:43 +0000 | [diff] [blame] | 37 | return V.convertToDouble(); |
| 38 | } |
| 39 | |
| 40 | |
Ted Kremenek | 6e94ef5 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 41 | StringLiteral::StringLiteral(ASTContext& C, const char *strData, |
| 42 | unsigned byteLength, bool Wide, QualType t, |
| 43 | SourceLocation firstLoc, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 44 | SourceLocation lastLoc) : |
| 45 | Expr(StringLiteralClass, t) { |
| 46 | // OPTIMIZE: could allocate this appended to the StringLiteral. |
Ted Kremenek | 6e94ef5 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 47 | char *AStrData = new (C, 1) char[byteLength]; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 48 | memcpy(AStrData, strData, byteLength); |
| 49 | StrData = AStrData; |
| 50 | ByteLength = byteLength; |
| 51 | IsWide = Wide; |
| 52 | firstTokLoc = firstLoc; |
| 53 | lastTokLoc = lastLoc; |
| 54 | } |
| 55 | |
Ted Kremenek | 6e94ef5 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 56 | void StringLiteral::Destroy(ASTContext &C) { |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 57 | C.Deallocate(const_cast<char*>(StrData)); |
Ted Kremenek | 353ffce | 2009-02-09 17:10:09 +0000 | [diff] [blame] | 58 | this->~StringLiteral(); |
| 59 | C.Deallocate(this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | bool UnaryOperator::isPostfix(Opcode Op) { |
| 63 | switch (Op) { |
| 64 | case PostInc: |
| 65 | case PostDec: |
| 66 | return true; |
| 67 | default: |
| 68 | return false; |
| 69 | } |
| 70 | } |
| 71 | |
Ted Kremenek | 5a56ac3 | 2008-07-23 22:18:43 +0000 | [diff] [blame] | 72 | bool UnaryOperator::isPrefix(Opcode Op) { |
| 73 | switch (Op) { |
| 74 | case PreInc: |
| 75 | case PreDec: |
| 76 | return true; |
| 77 | default: |
| 78 | return false; |
| 79 | } |
| 80 | } |
| 81 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 82 | /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it |
| 83 | /// corresponds to, e.g. "sizeof" or "[pre]++". |
| 84 | const char *UnaryOperator::getOpcodeStr(Opcode Op) { |
| 85 | switch (Op) { |
| 86 | default: assert(0 && "Unknown unary operator"); |
| 87 | case PostInc: return "++"; |
| 88 | case PostDec: return "--"; |
| 89 | case PreInc: return "++"; |
| 90 | case PreDec: return "--"; |
| 91 | case AddrOf: return "&"; |
| 92 | case Deref: return "*"; |
| 93 | case Plus: return "+"; |
| 94 | case Minus: return "-"; |
| 95 | case Not: return "~"; |
| 96 | case LNot: return "!"; |
| 97 | case Real: return "__real"; |
| 98 | case Imag: return "__imag"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 99 | case Extension: return "__extension__"; |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 100 | case OffsetOf: return "__builtin_offsetof"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
| 104 | //===----------------------------------------------------------------------===// |
| 105 | // Postfix Operators. |
| 106 | //===----------------------------------------------------------------------===// |
| 107 | |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 108 | CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 109 | unsigned numargs, QualType t, SourceLocation rparenloc) |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 110 | : Expr(SC, t, |
| 111 | fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs), |
| 112 | fn->isValueDependent() || hasAnyValueDependentArguments(args, numargs)), |
| 113 | NumArgs(numargs) { |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 114 | |
| 115 | SubExprs = new (C) Stmt*[numargs+1]; |
Douglas Gregor | b460980 | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 116 | SubExprs[FN] = fn; |
| 117 | for (unsigned i = 0; i != numargs; ++i) |
| 118 | SubExprs[i+ARGS_START] = args[i]; |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 119 | |
Douglas Gregor | b460980 | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 120 | RParenLoc = rparenloc; |
| 121 | } |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 122 | |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 123 | CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs, |
| 124 | QualType t, SourceLocation rparenloc) |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 125 | : Expr(CallExprClass, t, |
| 126 | fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs), |
| 127 | fn->isValueDependent() || hasAnyValueDependentArguments(args, numargs)), |
| 128 | NumArgs(numargs) { |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 129 | |
| 130 | SubExprs = new (C) Stmt*[numargs+1]; |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 131 | SubExprs[FN] = fn; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 132 | for (unsigned i = 0; i != numargs; ++i) |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 133 | SubExprs[i+ARGS_START] = args[i]; |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 134 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 135 | RParenLoc = rparenloc; |
| 136 | } |
| 137 | |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 138 | void CallExpr::Destroy(ASTContext& C) { |
| 139 | DestroyChildren(C); |
| 140 | if (SubExprs) C.Deallocate(SubExprs); |
| 141 | this->~CallExpr(); |
| 142 | C.Deallocate(this); |
| 143 | } |
| 144 | |
Chris Lattner | d18b329 | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 145 | /// setNumArgs - This changes the number of arguments present in this call. |
| 146 | /// Any orphaned expressions are deleted by this, and any new operands are set |
| 147 | /// to null. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 148 | void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) { |
Chris Lattner | d18b329 | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 149 | // No change, just return. |
| 150 | if (NumArgs == getNumArgs()) return; |
| 151 | |
| 152 | // If shrinking # arguments, just delete the extras and forgot them. |
| 153 | if (NumArgs < getNumArgs()) { |
| 154 | for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 155 | getArg(i)->Destroy(C); |
Chris Lattner | d18b329 | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 156 | this->NumArgs = NumArgs; |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | // Otherwise, we are growing the # arguments. New an bigger argument array. |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 161 | Stmt **NewSubExprs = new Stmt*[NumArgs+1]; |
Chris Lattner | d18b329 | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 162 | // Copy over args. |
| 163 | for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i) |
| 164 | NewSubExprs[i] = SubExprs[i]; |
| 165 | // Null out new args. |
| 166 | for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i) |
| 167 | NewSubExprs[i] = 0; |
| 168 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 169 | delete [] SubExprs; |
Chris Lattner | d18b329 | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 170 | SubExprs = NewSubExprs; |
| 171 | this->NumArgs = NumArgs; |
| 172 | } |
| 173 | |
Chris Lattner | cb88896 | 2008-10-06 05:00:53 +0000 | [diff] [blame] | 174 | /// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If |
| 175 | /// not, return 0. |
| 176 | unsigned CallExpr::isBuiltinCall() const { |
Steve Naroff | c4f8e8b | 2008-01-31 01:07:12 +0000 | [diff] [blame] | 177 | // All simple function calls (e.g. func()) are implicitly cast to pointer to |
| 178 | // function. As a result, we try and obtain the DeclRefExpr from the |
| 179 | // ImplicitCastExpr. |
| 180 | const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee()); |
| 181 | if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()). |
Chris Lattner | cb88896 | 2008-10-06 05:00:53 +0000 | [diff] [blame] | 182 | return 0; |
| 183 | |
Steve Naroff | c4f8e8b | 2008-01-31 01:07:12 +0000 | [diff] [blame] | 184 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr()); |
| 185 | if (!DRE) |
Chris Lattner | cb88896 | 2008-10-06 05:00:53 +0000 | [diff] [blame] | 186 | return 0; |
| 187 | |
Anders Carlsson | bcba201 | 2008-01-31 02:13:57 +0000 | [diff] [blame] | 188 | const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 189 | if (!FDecl) |
Chris Lattner | cb88896 | 2008-10-06 05:00:53 +0000 | [diff] [blame] | 190 | return 0; |
| 191 | |
Douglas Gregor | 4fcd399 | 2008-11-21 15:30:19 +0000 | [diff] [blame] | 192 | if (!FDecl->getIdentifier()) |
| 193 | return 0; |
| 194 | |
Chris Lattner | cb88896 | 2008-10-06 05:00:53 +0000 | [diff] [blame] | 195 | return FDecl->getIdentifier()->getBuiltinID(); |
| 196 | } |
Anders Carlsson | bcba201 | 2008-01-31 02:13:57 +0000 | [diff] [blame] | 197 | |
Chris Lattner | cb88896 | 2008-10-06 05:00:53 +0000 | [diff] [blame] | 198 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 199 | /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it |
| 200 | /// corresponds to, e.g. "<<=". |
| 201 | const char *BinaryOperator::getOpcodeStr(Opcode Op) { |
| 202 | switch (Op) { |
| 203 | default: assert(0 && "Unknown binary operator"); |
| 204 | case Mul: return "*"; |
| 205 | case Div: return "/"; |
| 206 | case Rem: return "%"; |
| 207 | case Add: return "+"; |
| 208 | case Sub: return "-"; |
| 209 | case Shl: return "<<"; |
| 210 | case Shr: return ">>"; |
| 211 | case LT: return "<"; |
| 212 | case GT: return ">"; |
| 213 | case LE: return "<="; |
| 214 | case GE: return ">="; |
| 215 | case EQ: return "=="; |
| 216 | case NE: return "!="; |
| 217 | case And: return "&"; |
| 218 | case Xor: return "^"; |
| 219 | case Or: return "|"; |
| 220 | case LAnd: return "&&"; |
| 221 | case LOr: return "||"; |
| 222 | case Assign: return "="; |
| 223 | case MulAssign: return "*="; |
| 224 | case DivAssign: return "/="; |
| 225 | case RemAssign: return "%="; |
| 226 | case AddAssign: return "+="; |
| 227 | case SubAssign: return "-="; |
| 228 | case ShlAssign: return "<<="; |
| 229 | case ShrAssign: return ">>="; |
| 230 | case AndAssign: return "&="; |
| 231 | case XorAssign: return "^="; |
| 232 | case OrAssign: return "|="; |
| 233 | case Comma: return ","; |
| 234 | } |
| 235 | } |
| 236 | |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 237 | InitListExpr::InitListExpr(SourceLocation lbraceloc, |
Chris Lattner | 418f6c7 | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 238 | Expr **initExprs, unsigned numInits, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 239 | SourceLocation rbraceloc) |
Steve Naroff | c5ae899 | 2008-05-01 02:04:18 +0000 | [diff] [blame] | 240 | : Expr(InitListExprClass, QualType()), |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 241 | LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0), |
Douglas Gregor | a9c8780 | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 242 | UnionFieldInit(0), HadArrayRangeDesignator(false) { |
Chris Lattner | 418f6c7 | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 243 | |
| 244 | InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits); |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 245 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 246 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 247 | void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) { |
| 248 | for (unsigned Idx = NumInits, LastIdx = InitExprs.size(); Idx < LastIdx; ++Idx) |
| 249 | delete InitExprs[Idx]; |
| 250 | InitExprs.resize(NumInits, 0); |
| 251 | } |
| 252 | |
| 253 | Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) { |
| 254 | if (Init >= InitExprs.size()) { |
| 255 | InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0); |
| 256 | InitExprs.back() = expr; |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | Expr *Result = cast_or_null<Expr>(InitExprs[Init]); |
| 261 | InitExprs[Init] = expr; |
| 262 | return Result; |
| 263 | } |
| 264 | |
Steve Naroff | bfdcae6 | 2008-09-04 15:31:07 +0000 | [diff] [blame] | 265 | /// getFunctionType - Return the underlying function type for this block. |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 266 | /// |
| 267 | const FunctionType *BlockExpr::getFunctionType() const { |
| 268 | return getType()->getAsBlockPointerType()-> |
| 269 | getPointeeType()->getAsFunctionType(); |
| 270 | } |
| 271 | |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 272 | SourceLocation BlockExpr::getCaretLocation() const { |
| 273 | return TheBlock->getCaretLocation(); |
| 274 | } |
| 275 | const Stmt *BlockExpr::getBody() const { return TheBlock->getBody(); } |
| 276 | Stmt *BlockExpr::getBody() { return TheBlock->getBody(); } |
| 277 | |
| 278 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 279 | //===----------------------------------------------------------------------===// |
| 280 | // Generic Expression Routines |
| 281 | //===----------------------------------------------------------------------===// |
| 282 | |
| 283 | /// hasLocalSideEffect - Return true if this immediate expression has side |
| 284 | /// effects, not counting any sub-expressions. |
| 285 | bool Expr::hasLocalSideEffect() const { |
| 286 | switch (getStmtClass()) { |
| 287 | default: |
| 288 | return false; |
| 289 | case ParenExprClass: |
| 290 | return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect(); |
| 291 | case UnaryOperatorClass: { |
| 292 | const UnaryOperator *UO = cast<UnaryOperator>(this); |
| 293 | |
| 294 | switch (UO->getOpcode()) { |
| 295 | default: return false; |
| 296 | case UnaryOperator::PostInc: |
| 297 | case UnaryOperator::PostDec: |
| 298 | case UnaryOperator::PreInc: |
| 299 | case UnaryOperator::PreDec: |
| 300 | return true; // ++/-- |
| 301 | |
| 302 | case UnaryOperator::Deref: |
| 303 | // Dereferencing a volatile pointer is a side-effect. |
| 304 | return getType().isVolatileQualified(); |
| 305 | case UnaryOperator::Real: |
| 306 | case UnaryOperator::Imag: |
| 307 | // accessing a piece of a volatile complex is a side-effect. |
| 308 | return UO->getSubExpr()->getType().isVolatileQualified(); |
| 309 | |
| 310 | case UnaryOperator::Extension: |
| 311 | return UO->getSubExpr()->hasLocalSideEffect(); |
| 312 | } |
| 313 | } |
Chris Lattner | e7716e6 | 2007-12-01 06:07:34 +0000 | [diff] [blame] | 314 | case BinaryOperatorClass: { |
| 315 | const BinaryOperator *BinOp = cast<BinaryOperator>(this); |
| 316 | // Consider comma to have side effects if the LHS and RHS both do. |
| 317 | if (BinOp->getOpcode() == BinaryOperator::Comma) |
| 318 | return BinOp->getLHS()->hasLocalSideEffect() && |
| 319 | BinOp->getRHS()->hasLocalSideEffect(); |
| 320 | |
| 321 | return BinOp->isAssignmentOp(); |
| 322 | } |
Chris Lattner | eb14fe8 | 2007-08-25 02:00:02 +0000 | [diff] [blame] | 323 | case CompoundAssignOperatorClass: |
Chris Lattner | 1f683e9 | 2007-08-25 01:55:00 +0000 | [diff] [blame] | 324 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 325 | |
Fariborz Jahanian | ab38e4b | 2007-12-01 19:58:28 +0000 | [diff] [blame] | 326 | case ConditionalOperatorClass: { |
| 327 | const ConditionalOperator *Exp = cast<ConditionalOperator>(this); |
| 328 | return Exp->getCond()->hasLocalSideEffect() |
| 329 | || (Exp->getLHS() && Exp->getLHS()->hasLocalSideEffect()) |
| 330 | || (Exp->getRHS() && Exp->getRHS()->hasLocalSideEffect()); |
| 331 | } |
| 332 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 333 | case MemberExprClass: |
| 334 | case ArraySubscriptExprClass: |
| 335 | // If the base pointer or element is to a volatile pointer/field, accessing |
| 336 | // if is a side effect. |
| 337 | return getType().isVolatileQualified(); |
Eli Friedman | 211f6ad | 2008-05-27 15:24:04 +0000 | [diff] [blame] | 338 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 339 | case CallExprClass: |
Douglas Gregor | b460980 | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 340 | case CXXOperatorCallExprClass: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 341 | // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }" |
| 342 | // should warn. |
| 343 | return true; |
Chris Lattner | a9c0102 | 2007-09-26 22:06:30 +0000 | [diff] [blame] | 344 | case ObjCMessageExprClass: |
| 345 | return true; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 346 | case StmtExprClass: { |
| 347 | // Statement exprs don't logically have side effects themselves, but are |
| 348 | // sometimes used in macros in ways that give them a type that is unused. |
| 349 | // For example ({ blah; foo(); }) will end up with a type if foo has a type. |
| 350 | // however, if the result of the stmt expr is dead, we don't want to emit a |
| 351 | // warning. |
| 352 | const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt(); |
| 353 | if (!CS->body_empty()) |
| 354 | if (const Expr *E = dyn_cast<Expr>(CS->body_back())) |
| 355 | return E->hasLocalSideEffect(); |
| 356 | return false; |
| 357 | } |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 358 | case CStyleCastExprClass: |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 359 | case CXXFunctionalCastExprClass: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 360 | // If this is a cast to void, check the operand. Otherwise, the result of |
| 361 | // the cast is unused. |
| 362 | if (getType()->isVoidType()) |
| 363 | return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect(); |
| 364 | return false; |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 365 | |
Eli Friedman | 4be1f47 | 2008-05-19 21:24:43 +0000 | [diff] [blame] | 366 | case ImplicitCastExprClass: |
| 367 | // Check the operand, since implicit casts are inserted by Sema |
| 368 | return cast<ImplicitCastExpr>(this)->getSubExpr()->hasLocalSideEffect(); |
| 369 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 370 | case CXXDefaultArgExprClass: |
| 371 | return cast<CXXDefaultArgExpr>(this)->getExpr()->hasLocalSideEffect(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 372 | |
| 373 | case CXXNewExprClass: |
| 374 | // FIXME: In theory, there might be new expressions that don't have side |
| 375 | // effects (e.g. a placement new with an uninitialized POD). |
| 376 | case CXXDeleteExprClass: |
| 377 | return true; |
| 378 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 381 | /// DeclCanBeLvalue - Determine whether the given declaration can be |
| 382 | /// an lvalue. This is a helper routine for isLvalue. |
| 383 | static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) { |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 384 | // C++ [temp.param]p6: |
| 385 | // A non-type non-reference template-parameter is not an lvalue. |
| 386 | if (const NonTypeTemplateParmDecl *NTTParm |
| 387 | = dyn_cast<NonTypeTemplateParmDecl>(Decl)) |
| 388 | return NTTParm->getType()->isReferenceType(); |
| 389 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 390 | return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) || |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 391 | // C++ 3.10p2: An lvalue refers to an object or function. |
| 392 | (Ctx.getLangOptions().CPlusPlus && |
| 393 | (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl))); |
| 394 | } |
| 395 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 396 | /// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an |
| 397 | /// incomplete type other than void. Nonarray expressions that can be lvalues: |
| 398 | /// - name, where name must be a variable |
| 399 | /// - e[i] |
| 400 | /// - (e), where e must be an lvalue |
| 401 | /// - e.name, where e must be an lvalue |
| 402 | /// - e->name |
| 403 | /// - *e, the type of e cannot be a function type |
| 404 | /// - string-constant |
Chris Lattner | 7da36f6 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 405 | /// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension] |
Bill Wendling | 08ad47c | 2007-07-17 03:52:31 +0000 | [diff] [blame] | 406 | /// - reference type [C++ [expr]] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 407 | /// |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 408 | Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 409 | // first, check the type (C99 6.3.2.1). Expressions with function |
| 410 | // type in C are not lvalues, but they can be lvalues in C++. |
| 411 | if (!Ctx.getLangOptions().CPlusPlus && TR->isFunctionType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 412 | return LV_NotObjectType; |
| 413 | |
Steve Naroff | acb818a | 2008-02-10 01:39:04 +0000 | [diff] [blame] | 414 | // Allow qualified void which is an incomplete type other than void (yuck). |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 415 | if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers()) |
Steve Naroff | acb818a | 2008-02-10 01:39:04 +0000 | [diff] [blame] | 416 | return LV_IncompleteVoidType; |
| 417 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 418 | /// FIXME: Expressions can't have reference type, so the following |
| 419 | /// isn't needed. |
Chris Lattner | cb4f9a6 | 2007-07-21 05:33:26 +0000 | [diff] [blame] | 420 | if (TR->isReferenceType()) // C++ [expr] |
Bill Wendling | 08ad47c | 2007-07-17 03:52:31 +0000 | [diff] [blame] | 421 | return LV_Valid; |
| 422 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 423 | // the type looks fine, now check the expression |
| 424 | switch (getStmtClass()) { |
| 425 | case StringLiteralClass: // C99 6.5.1p4 |
Anders Carlsson | 7323a62 | 2007-11-30 22:47:59 +0000 | [diff] [blame] | 426 | return LV_Valid; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 427 | case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2)))) |
| 428 | // For vectors, make sure base is an lvalue (i.e. not a function call). |
| 429 | if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType()) |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 430 | return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 431 | return LV_Valid; |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 432 | case DeclRefExprClass: |
| 433 | case QualifiedDeclRefExprClass: { // C99 6.5.1p2 |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 434 | const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl(); |
| 435 | if (DeclCanBeLvalue(RefdDecl, Ctx)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 436 | return LV_Valid; |
| 437 | break; |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 438 | } |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 439 | case BlockDeclRefExprClass: { |
| 440 | const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this); |
Steve Naroff | 4f6a7d7 | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 441 | if (isa<VarDecl>(BDR->getDecl())) |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 442 | return LV_Valid; |
| 443 | break; |
| 444 | } |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 445 | case MemberExprClass: { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 446 | const MemberExpr *m = cast<MemberExpr>(this); |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 447 | if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4: |
| 448 | NamedDecl *Member = m->getMemberDecl(); |
| 449 | // C++ [expr.ref]p4: |
| 450 | // If E2 is declared to have type "reference to T", then E1.E2 |
| 451 | // is an lvalue. |
| 452 | if (ValueDecl *Value = dyn_cast<ValueDecl>(Member)) |
| 453 | if (Value->getType()->isReferenceType()) |
| 454 | return LV_Valid; |
| 455 | |
| 456 | // -- If E2 is a static data member [...] then E1.E2 is an lvalue. |
| 457 | if (isa<CXXClassVarDecl>(Member)) |
| 458 | return LV_Valid; |
| 459 | |
| 460 | // -- If E2 is a non-static data member [...]. If E1 is an |
| 461 | // lvalue, then E1.E2 is an lvalue. |
| 462 | if (isa<FieldDecl>(Member)) |
| 463 | return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx); |
| 464 | |
| 465 | // -- If it refers to a static member function [...], then |
| 466 | // E1.E2 is an lvalue. |
| 467 | // -- Otherwise, if E1.E2 refers to a non-static member |
| 468 | // function [...], then E1.E2 is not an lvalue. |
| 469 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member)) |
| 470 | return Method->isStatic()? LV_Valid : LV_MemberFunction; |
| 471 | |
| 472 | // -- If E2 is a member enumerator [...], the expression E1.E2 |
| 473 | // is not an lvalue. |
| 474 | if (isa<EnumConstantDecl>(Member)) |
| 475 | return LV_InvalidExpression; |
| 476 | |
| 477 | // Not an lvalue. |
| 478 | return LV_InvalidExpression; |
| 479 | } |
| 480 | |
| 481 | // C99 6.5.2.3p4 |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 482 | return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx); |
Anton Korobeynikov | fdd7566 | 2007-07-12 15:26:50 +0000 | [diff] [blame] | 483 | } |
Chris Lattner | 7da36f6 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 484 | case UnaryOperatorClass: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 485 | if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref) |
Chris Lattner | 7da36f6 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 486 | return LV_Valid; // C99 6.5.3p4 |
| 487 | |
| 488 | if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real || |
Chris Lattner | baf0d66 | 2008-07-25 18:07:19 +0000 | [diff] [blame] | 489 | cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag || |
| 490 | cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension) |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 491 | return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 492 | |
| 493 | if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1 |
| 494 | (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc || |
| 495 | cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec)) |
| 496 | return LV_Valid; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 497 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 498 | case ImplicitCastExprClass: |
| 499 | return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid |
| 500 | : LV_InvalidExpression; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 501 | case ParenExprClass: // C99 6.5.1p5 |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 502 | return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 503 | case BinaryOperatorClass: |
| 504 | case CompoundAssignOperatorClass: { |
| 505 | const BinaryOperator *BinOp = cast<BinaryOperator>(this); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 506 | |
| 507 | if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1 |
| 508 | BinOp->getOpcode() == BinaryOperator::Comma) |
| 509 | return BinOp->getRHS()->isLvalue(Ctx); |
| 510 | |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 511 | // C++ [expr.mptr.oper]p6 |
| 512 | if ((BinOp->getOpcode() == BinaryOperator::PtrMemD || |
| 513 | BinOp->getOpcode() == BinaryOperator::PtrMemI) && |
| 514 | !BinOp->getType()->isFunctionType()) |
| 515 | return BinOp->getLHS()->isLvalue(Ctx); |
| 516 | |
Douglas Gregor | bf3af05 | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 517 | if (!BinOp->isAssignmentOp()) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 518 | return LV_InvalidExpression; |
| 519 | |
Douglas Gregor | bf3af05 | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 520 | if (Ctx.getLangOptions().CPlusPlus) |
| 521 | // C++ [expr.ass]p1: |
| 522 | // The result of an assignment operation [...] is an lvalue. |
| 523 | return LV_Valid; |
| 524 | |
| 525 | |
| 526 | // C99 6.5.16: |
| 527 | // An assignment expression [...] is not an lvalue. |
| 528 | return LV_InvalidExpression; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 529 | } |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 530 | // FIXME: OverloadExprClass |
Douglas Gregor | b460980 | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 531 | case CallExprClass: |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 532 | case CXXOperatorCallExprClass: |
| 533 | case CXXMemberCallExprClass: { |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 534 | // C++ [expr.call]p10: |
| 535 | // A function call is an lvalue if and only if the result type |
| 536 | // is a reference. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 537 | QualType CalleeType = cast<CallExpr>(this)->getCallee()->getType(); |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 538 | if (const PointerType *FnTypePtr = CalleeType->getAsPointerType()) |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 539 | CalleeType = FnTypePtr->getPointeeType(); |
| 540 | if (const FunctionType *FnType = CalleeType->getAsFunctionType()) |
| 541 | if (FnType->getResultType()->isReferenceType()) |
| 542 | return LV_Valid; |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 543 | |
| 544 | break; |
| 545 | } |
Steve Naroff | e638639 | 2007-12-05 04:00:10 +0000 | [diff] [blame] | 546 | case CompoundLiteralExprClass: // C99 6.5.2.5p5 |
| 547 | return LV_Valid; |
Chris Lattner | 670a62c | 2008-12-12 05:35:08 +0000 | [diff] [blame] | 548 | case ChooseExprClass: |
| 549 | // __builtin_choose_expr is an lvalue if the selected operand is. |
| 550 | if (cast<ChooseExpr>(this)->isConditionTrue(Ctx)) |
| 551 | return cast<ChooseExpr>(this)->getLHS()->isLvalue(Ctx); |
| 552 | else |
| 553 | return cast<ChooseExpr>(this)->getRHS()->isLvalue(Ctx); |
| 554 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 555 | case ExtVectorElementExprClass: |
| 556 | if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements()) |
Steve Naroff | fec0b49 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 557 | return LV_DuplicateVectorComponents; |
| 558 | return LV_Valid; |
Steve Naroff | 027282d | 2007-11-12 14:34:27 +0000 | [diff] [blame] | 559 | case ObjCIvarRefExprClass: // ObjC instance variables are lvalues. |
| 560 | return LV_Valid; |
Steve Naroff | 799a6a6 | 2008-05-30 23:23:16 +0000 | [diff] [blame] | 561 | case ObjCPropertyRefExprClass: // FIXME: check if read-only property. |
| 562 | return LV_Valid; |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 563 | case ObjCKVCRefExprClass: // FIXME: check if read-only property. |
Chris Lattner | 670a62c | 2008-12-12 05:35:08 +0000 | [diff] [blame] | 564 | return LV_Valid; |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 565 | case PredefinedExprClass: |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 566 | return LV_Valid; |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 567 | case VAArgExprClass: |
Daniel Dunbar | adadd8d | 2009-02-12 09:21:08 +0000 | [diff] [blame^] | 568 | return LV_NotObjectType; |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 569 | case CXXDefaultArgExprClass: |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 570 | return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx); |
Argyrios Kyrtzidis | 24b41fa | 2008-09-11 04:22:26 +0000 | [diff] [blame] | 571 | case CXXConditionDeclExprClass: |
| 572 | return LV_Valid; |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 573 | case CStyleCastExprClass: |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 574 | case CXXFunctionalCastExprClass: |
| 575 | case CXXStaticCastExprClass: |
| 576 | case CXXDynamicCastExprClass: |
| 577 | case CXXReinterpretCastExprClass: |
| 578 | case CXXConstCastExprClass: |
| 579 | // The result of an explicit cast is an lvalue if the type we are |
| 580 | // casting to is a reference type. See C++ [expr.cast]p1, |
| 581 | // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2, |
| 582 | // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1. |
| 583 | if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isReferenceType()) |
| 584 | return LV_Valid; |
| 585 | break; |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 586 | case CXXTypeidExprClass: |
| 587 | // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ... |
| 588 | return LV_Valid; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 589 | default: |
| 590 | break; |
| 591 | } |
| 592 | return LV_InvalidExpression; |
| 593 | } |
| 594 | |
| 595 | /// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type, |
| 596 | /// does not have an incomplete type, does not have a const-qualified type, and |
| 597 | /// if it is a structure or union, does not have any member (including, |
| 598 | /// recursively, any member or element of all contained aggregates or unions) |
| 599 | /// with a const-qualified type. |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 600 | Expr::isModifiableLvalueResult Expr::isModifiableLvalue(ASTContext &Ctx) const { |
| 601 | isLvalueResult lvalResult = isLvalue(Ctx); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 602 | |
| 603 | switch (lvalResult) { |
Douglas Gregor | ae8d467 | 2008-10-22 00:03:08 +0000 | [diff] [blame] | 604 | case LV_Valid: |
| 605 | // C++ 3.10p11: Functions cannot be modified, but pointers to |
| 606 | // functions can be modifiable. |
| 607 | if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType()) |
| 608 | return MLV_NotObjectType; |
| 609 | break; |
| 610 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 611 | case LV_NotObjectType: return MLV_NotObjectType; |
| 612 | case LV_IncompleteVoidType: return MLV_IncompleteVoidType; |
Steve Naroff | fec0b49 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 613 | case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents; |
Chris Lattner | ca354fa | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 614 | case LV_InvalidExpression: |
| 615 | // If the top level is a C-style cast, and the subexpression is a valid |
| 616 | // lvalue, then this is probably a use of the old-school "cast as lvalue" |
| 617 | // GCC extension. We don't support it, but we want to produce good |
| 618 | // diagnostics when it happens so that the user knows why. |
| 619 | if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(this)) |
| 620 | if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) |
| 621 | return MLV_LValueCast; |
| 622 | return MLV_InvalidExpression; |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 623 | case LV_MemberFunction: return MLV_MemberFunction; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 624 | } |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 625 | |
| 626 | QualType CT = Ctx.getCanonicalType(getType()); |
| 627 | |
| 628 | if (CT.isConstQualified()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 629 | return MLV_ConstQualified; |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 630 | if (CT->isArrayType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 631 | return MLV_ArrayType; |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 632 | if (CT->isIncompleteType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 633 | return MLV_IncompleteType; |
| 634 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 635 | if (const RecordType *r = CT->getAsRecordType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 636 | if (r->hasConstFields()) |
| 637 | return MLV_ConstQualified; |
| 638 | } |
Steve Naroff | 4f6a7d7 | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 639 | // The following is illegal: |
| 640 | // void takeclosure(void (^C)(void)); |
| 641 | // void func() { int x = 1; takeclosure(^{ x = 7 }); } |
| 642 | // |
| 643 | if (getStmtClass() == BlockDeclRefExprClass) { |
| 644 | const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this); |
| 645 | if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl())) |
| 646 | return MLV_NotBlockQualified; |
| 647 | } |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 648 | |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 649 | // Assigning to an 'implicit' property? |
Fariborz Jahanian | 6669db9 | 2008-11-25 17:56:43 +0000 | [diff] [blame] | 650 | else if (getStmtClass() == ObjCKVCRefExprClass) { |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 651 | const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this); |
| 652 | if (KVCExpr->getSetterMethod() == 0) |
| 653 | return MLV_NoSetterProperty; |
| 654 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 655 | return MLV_Valid; |
| 656 | } |
| 657 | |
Ted Kremenek | 2e5f54a | 2008-02-27 18:39:48 +0000 | [diff] [blame] | 658 | /// hasGlobalStorage - Return true if this expression has static storage |
Chris Lattner | 4cc6271 | 2007-11-27 21:35:27 +0000 | [diff] [blame] | 659 | /// duration. This means that the address of this expression is a link-time |
| 660 | /// constant. |
Ted Kremenek | 2e5f54a | 2008-02-27 18:39:48 +0000 | [diff] [blame] | 661 | bool Expr::hasGlobalStorage() const { |
Chris Lattner | 1d09ecc | 2007-11-13 18:05:45 +0000 | [diff] [blame] | 662 | switch (getStmtClass()) { |
| 663 | default: |
| 664 | return false; |
Chris Lattner | 4cc6271 | 2007-11-27 21:35:27 +0000 | [diff] [blame] | 665 | case ParenExprClass: |
Ted Kremenek | 2e5f54a | 2008-02-27 18:39:48 +0000 | [diff] [blame] | 666 | return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage(); |
Chris Lattner | 4cc6271 | 2007-11-27 21:35:27 +0000 | [diff] [blame] | 667 | case ImplicitCastExprClass: |
Ted Kremenek | 2e5f54a | 2008-02-27 18:39:48 +0000 | [diff] [blame] | 668 | return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage(); |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 669 | case CompoundLiteralExprClass: |
| 670 | return cast<CompoundLiteralExpr>(this)->isFileScope(); |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 671 | case DeclRefExprClass: |
| 672 | case QualifiedDeclRefExprClass: { |
Chris Lattner | 1d09ecc | 2007-11-13 18:05:45 +0000 | [diff] [blame] | 673 | const Decl *D = cast<DeclRefExpr>(this)->getDecl(); |
| 674 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
Ted Kremenek | 2e5f54a | 2008-02-27 18:39:48 +0000 | [diff] [blame] | 675 | return VD->hasGlobalStorage(); |
Seo Sanghyeon | 63f067f | 2008-04-04 09:45:30 +0000 | [diff] [blame] | 676 | if (isa<FunctionDecl>(D)) |
| 677 | return true; |
Chris Lattner | 1d09ecc | 2007-11-13 18:05:45 +0000 | [diff] [blame] | 678 | return false; |
| 679 | } |
Chris Lattner | fb70806 | 2007-11-28 04:30:09 +0000 | [diff] [blame] | 680 | case MemberExprClass: { |
Chris Lattner | 1d09ecc | 2007-11-13 18:05:45 +0000 | [diff] [blame] | 681 | const MemberExpr *M = cast<MemberExpr>(this); |
Ted Kremenek | 2e5f54a | 2008-02-27 18:39:48 +0000 | [diff] [blame] | 682 | return !M->isArrow() && M->getBase()->hasGlobalStorage(); |
Chris Lattner | fb70806 | 2007-11-28 04:30:09 +0000 | [diff] [blame] | 683 | } |
Chris Lattner | 4cc6271 | 2007-11-27 21:35:27 +0000 | [diff] [blame] | 684 | case ArraySubscriptExprClass: |
Ted Kremenek | 2e5f54a | 2008-02-27 18:39:48 +0000 | [diff] [blame] | 685 | return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage(); |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 686 | case PredefinedExprClass: |
Chris Lattner | fa28b30 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 687 | return true; |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 688 | case CXXDefaultArgExprClass: |
| 689 | return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage(); |
Chris Lattner | 1d09ecc | 2007-11-13 18:05:45 +0000 | [diff] [blame] | 690 | } |
| 691 | } |
| 692 | |
Ted Kremenek | 4e99a5f | 2008-01-17 16:57:34 +0000 | [diff] [blame] | 693 | Expr* Expr::IgnoreParens() { |
| 694 | Expr* E = this; |
| 695 | while (ParenExpr* P = dyn_cast<ParenExpr>(E)) |
| 696 | E = P->getSubExpr(); |
| 697 | |
| 698 | return E; |
| 699 | } |
| 700 | |
Chris Lattner | 56f3494 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 701 | /// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr |
| 702 | /// or CastExprs or ImplicitCastExprs, returning their operand. |
| 703 | Expr *Expr::IgnoreParenCasts() { |
| 704 | Expr *E = this; |
| 705 | while (true) { |
| 706 | if (ParenExpr *P = dyn_cast<ParenExpr>(E)) |
| 707 | E = P->getSubExpr(); |
| 708 | else if (CastExpr *P = dyn_cast<CastExpr>(E)) |
| 709 | E = P->getSubExpr(); |
Chris Lattner | 56f3494 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 710 | else |
| 711 | return E; |
| 712 | } |
| 713 | } |
| 714 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 715 | /// hasAnyTypeDependentArguments - Determines if any of the expressions |
| 716 | /// in Exprs is type-dependent. |
| 717 | bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) { |
| 718 | for (unsigned I = 0; I < NumExprs; ++I) |
| 719 | if (Exprs[I]->isTypeDependent()) |
| 720 | return true; |
| 721 | |
| 722 | return false; |
| 723 | } |
| 724 | |
| 725 | /// hasAnyValueDependentArguments - Determines if any of the expressions |
| 726 | /// in Exprs is value-dependent. |
| 727 | bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) { |
| 728 | for (unsigned I = 0; I < NumExprs; ++I) |
| 729 | if (Exprs[I]->isValueDependent()) |
| 730 | return true; |
| 731 | |
| 732 | return false; |
| 733 | } |
| 734 | |
Eli Friedman | c9e8f60 | 2009-01-25 02:32:41 +0000 | [diff] [blame] | 735 | bool Expr::isConstantInitializer(ASTContext &Ctx) const { |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 736 | // This function is attempting whether an expression is an initializer |
| 737 | // which can be evaluated at compile-time. isEvaluatable handles most |
| 738 | // of the cases, but it can't deal with some initializer-specific |
| 739 | // expressions, and it can't deal with aggregates; we deal with those here, |
| 740 | // and fall back to isEvaluatable for the other cases. |
| 741 | |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 742 | switch (getStmtClass()) { |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 743 | default: break; |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 744 | case StringLiteralClass: |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 745 | return true; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 746 | case CompoundLiteralExprClass: { |
| 747 | const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer(); |
Eli Friedman | c9e8f60 | 2009-01-25 02:32:41 +0000 | [diff] [blame] | 748 | return Exp->isConstantInitializer(Ctx); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 749 | } |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 750 | case InitListExprClass: { |
| 751 | const InitListExpr *Exp = cast<InitListExpr>(this); |
| 752 | unsigned numInits = Exp->getNumInits(); |
| 753 | for (unsigned i = 0; i < numInits; i++) { |
Eli Friedman | c9e8f60 | 2009-01-25 02:32:41 +0000 | [diff] [blame] | 754 | if (!Exp->getInit(i)->isConstantInitializer(Ctx)) |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 755 | return false; |
| 756 | } |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 757 | return true; |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 758 | } |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 759 | case ImplicitValueInitExprClass: |
| 760 | return true; |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 761 | case ParenExprClass: { |
| 762 | return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx); |
| 763 | } |
| 764 | case UnaryOperatorClass: { |
| 765 | const UnaryOperator* Exp = cast<UnaryOperator>(this); |
| 766 | if (Exp->getOpcode() == UnaryOperator::Extension) |
| 767 | return Exp->getSubExpr()->isConstantInitializer(Ctx); |
| 768 | break; |
| 769 | } |
| 770 | case CStyleCastExprClass: |
| 771 | // Handle casts with a destination that's a struct or union; this |
| 772 | // deals with both the gcc no-op struct cast extension and the |
| 773 | // cast-to-union extension. |
| 774 | if (getType()->isRecordType()) |
| 775 | return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx); |
| 776 | break; |
Eli Friedman | 32a311e | 2009-01-25 03:27:40 +0000 | [diff] [blame] | 777 | case DesignatedInitExprClass: |
Sebastian Redl | 4e716e0 | 2009-01-25 13:34:47 +0000 | [diff] [blame] | 778 | return cast<DesignatedInitExpr>(this)-> |
| 779 | getInit()->isConstantInitializer(Ctx); |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 782 | return isEvaluatable(Ctx); |
Steve Naroff | 38374b0 | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 783 | } |
| 784 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 785 | /// isIntegerConstantExpr - this recursive routine will test if an expression is |
| 786 | /// an integer constant expression. Note: With the introduction of VLA's in |
| 787 | /// C99 the result of the sizeof operator is no longer always a constant |
| 788 | /// expression. The generalization of the wording to include any subexpression |
| 789 | /// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions |
| 790 | /// can appear as operands to other operators (e.g. &&, ||, ?:). For instance, |
Nuno Lopes | 5f6b632 | 2008-07-08 21:13:06 +0000 | [diff] [blame] | 791 | /// "0 || f()" can be treated as a constant expression. In C90 this expression, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 792 | /// occurring in a context requiring a constant, would have been a constraint |
| 793 | /// violation. FIXME: This routine currently implements C90 semantics. |
| 794 | /// To properly implement C99 semantics this routine will need to evaluate |
| 795 | /// expressions involving operators previously mentioned. |
| 796 | |
| 797 | /// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, |
| 798 | /// comma, etc |
| 799 | /// |
| 800 | /// FIXME: This should ext-warn on overflow during evaluation! ISO C does not |
Chris Lattner | ccc213f | 2007-09-26 00:47:26 +0000 | [diff] [blame] | 801 | /// permit this. This includes things like (int)1e1000 |
Chris Lattner | ce0afc0 | 2007-07-18 05:21:20 +0000 | [diff] [blame] | 802 | /// |
| 803 | /// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof |
| 804 | /// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer |
| 805 | /// cast+dereference. |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 806 | bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx, |
| 807 | SourceLocation *Loc, bool isEvaluated) const { |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 808 | // Pretest for integral type; some parts of the code crash for types that |
| 809 | // can't be sized. |
| 810 | if (!getType()->isIntegralType()) { |
| 811 | if (Loc) *Loc = getLocStart(); |
| 812 | return false; |
| 813 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 814 | switch (getStmtClass()) { |
| 815 | default: |
| 816 | if (Loc) *Loc = getLocStart(); |
| 817 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 818 | case ParenExprClass: |
| 819 | return cast<ParenExpr>(this)->getSubExpr()-> |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 820 | isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 821 | case IntegerLiteralClass: |
| 822 | Result = cast<IntegerLiteral>(this)->getValue(); |
| 823 | break; |
Chris Lattner | 2eadfb6 | 2007-07-15 23:32:58 +0000 | [diff] [blame] | 824 | case CharacterLiteralClass: { |
| 825 | const CharacterLiteral *CL = cast<CharacterLiteral>(this); |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 826 | Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); |
Chris Lattner | 2eadfb6 | 2007-07-15 23:32:58 +0000 | [diff] [blame] | 827 | Result = CL->getValue(); |
Chris Lattner | f0fbcb3 | 2007-07-16 21:04:56 +0000 | [diff] [blame] | 828 | Result.setIsUnsigned(!getType()->isSignedIntegerType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 829 | break; |
Chris Lattner | 2eadfb6 | 2007-07-15 23:32:58 +0000 | [diff] [blame] | 830 | } |
Anders Carlsson | b88d45e | 2008-08-23 21:12:35 +0000 | [diff] [blame] | 831 | case CXXBoolLiteralExprClass: { |
| 832 | const CXXBoolLiteralExpr *BL = cast<CXXBoolLiteralExpr>(this); |
| 833 | Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); |
| 834 | Result = BL->getValue(); |
| 835 | Result.setIsUnsigned(!getType()->isSignedIntegerType()); |
| 836 | break; |
| 837 | } |
Argyrios Kyrtzidis | 7267f78 | 2008-08-23 19:35:47 +0000 | [diff] [blame] | 838 | case CXXZeroInitValueExprClass: |
| 839 | Result.clear(); |
| 840 | break; |
Steve Naroff | 7b658aa | 2007-08-02 04:09:23 +0000 | [diff] [blame] | 841 | case TypesCompatibleExprClass: { |
| 842 | const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this); |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 843 | Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); |
Daniel Dunbar | ac620de | 2008-10-24 08:07:57 +0000 | [diff] [blame] | 844 | // Per gcc docs "this built-in function ignores top level |
| 845 | // qualifiers". We need to use the canonical version to properly |
| 846 | // be able to strip CRV qualifiers from the type. |
| 847 | QualType T0 = Ctx.getCanonicalType(TCE->getArgType1()); |
| 848 | QualType T1 = Ctx.getCanonicalType(TCE->getArgType2()); |
| 849 | Result = Ctx.typesAreCompatible(T0.getUnqualifiedType(), |
| 850 | T1.getUnqualifiedType()); |
Steve Naroff | 389cecc | 2007-08-02 00:13:27 +0000 | [diff] [blame] | 851 | break; |
Steve Naroff | 7b658aa | 2007-08-02 04:09:23 +0000 | [diff] [blame] | 852 | } |
Douglas Gregor | b460980 | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 853 | case CallExprClass: |
| 854 | case CXXOperatorCallExprClass: { |
Steve Naroff | 13b7c5f | 2007-08-08 22:15:55 +0000 | [diff] [blame] | 855 | const CallExpr *CE = cast<CallExpr>(this); |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 856 | Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 857 | |
| 858 | // If this is a call to a builtin function, constant fold it otherwise |
| 859 | // reject it. |
| 860 | if (CE->isBuiltinCall()) { |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 861 | EvalResult EvalResult; |
| 862 | if (CE->Evaluate(EvalResult, Ctx)) { |
| 863 | assert(!EvalResult.HasSideEffects && |
| 864 | "Foldable builtin call should not have side effects!"); |
| 865 | Result = EvalResult.Val.getInt(); |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 866 | break; // It is a constant, expand it. |
| 867 | } |
| 868 | } |
| 869 | |
Steve Naroff | 13b7c5f | 2007-08-08 22:15:55 +0000 | [diff] [blame] | 870 | if (Loc) *Loc = getLocStart(); |
| 871 | return false; |
| 872 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 873 | case DeclRefExprClass: |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 874 | case QualifiedDeclRefExprClass: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 875 | if (const EnumConstantDecl *D = |
| 876 | dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) { |
| 877 | Result = D->getInitVal(); |
| 878 | break; |
| 879 | } |
Sebastian Redl | 4a4251b | 2009-02-07 13:06:23 +0000 | [diff] [blame] | 880 | if (Ctx.getLangOptions().CPlusPlus && |
| 881 | getType().getCVRQualifiers() == QualType::Const) { |
| 882 | // C++ 7.1.5.1p2 |
| 883 | // A variable of non-volatile const-qualified integral or enumeration |
| 884 | // type initialized by an ICE can be used in ICEs. |
| 885 | if (const VarDecl *Dcl = |
| 886 | dyn_cast<VarDecl>(cast<DeclRefExpr>(this)->getDecl())) { |
| 887 | if (const Expr *Init = Dcl->getInit()) |
| 888 | return Init->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated); |
| 889 | } |
| 890 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 891 | if (Loc) *Loc = getLocStart(); |
| 892 | return false; |
| 893 | case UnaryOperatorClass: { |
| 894 | const UnaryOperator *Exp = cast<UnaryOperator>(this); |
| 895 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 896 | // Get the operand value. If this is offsetof, do not evalute the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 897 | // operand. This affects C99 6.6p3. |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 898 | if (!Exp->isOffsetOfOp() && !Exp->getSubExpr()-> |
| 899 | isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 900 | return false; |
| 901 | |
| 902 | switch (Exp->getOpcode()) { |
| 903 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 904 | // See C99 6.6p3. |
| 905 | default: |
| 906 | if (Loc) *Loc = Exp->getOperatorLoc(); |
| 907 | return false; |
| 908 | case UnaryOperator::Extension: |
Chris Lattner | 76e773a | 2007-07-18 18:38:36 +0000 | [diff] [blame] | 909 | return true; // FIXME: this is wrong. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 910 | case UnaryOperator::LNot: { |
Chris Lattner | bf75538 | 2008-01-25 19:16:19 +0000 | [diff] [blame] | 911 | bool Val = Result == 0; |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 912 | Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 913 | Result = Val; |
| 914 | break; |
| 915 | } |
| 916 | case UnaryOperator::Plus: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 917 | break; |
| 918 | case UnaryOperator::Minus: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 919 | Result = -Result; |
| 920 | break; |
| 921 | case UnaryOperator::Not: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 922 | Result = ~Result; |
| 923 | break; |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 924 | case UnaryOperator::OffsetOf: |
Daniel Dunbar | aa1f9f1 | 2008-08-28 18:42:20 +0000 | [diff] [blame] | 925 | Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 926 | Result = Exp->evaluateOffsetOf(Ctx); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 927 | } |
| 928 | break; |
| 929 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 930 | case SizeOfAlignOfExprClass: { |
| 931 | const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(this); |
Chris Lattner | a269ebf | 2008-02-21 05:45:29 +0000 | [diff] [blame] | 932 | |
| 933 | // Return the result in the right width. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 934 | Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); |
Chris Lattner | a269ebf | 2008-02-21 05:45:29 +0000 | [diff] [blame] | 935 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 936 | QualType ArgTy = Exp->getTypeOfArgument(); |
Chris Lattner | a269ebf | 2008-02-21 05:45:29 +0000 | [diff] [blame] | 937 | // sizeof(void) and __alignof__(void) = 1 as a gcc extension. |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 938 | if (ArgTy->isVoidType()) { |
Chris Lattner | a269ebf | 2008-02-21 05:45:29 +0000 | [diff] [blame] | 939 | Result = 1; |
| 940 | break; |
| 941 | } |
| 942 | |
| 943 | // alignof always evaluates to a constant, sizeof does if arg is not VLA. |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 944 | if (Exp->isSizeOf() && !ArgTy->isConstantSizeType()) { |
Chris Lattner | 6538347 | 2007-12-18 07:15:40 +0000 | [diff] [blame] | 945 | if (Loc) *Loc = Exp->getOperatorLoc(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 946 | return false; |
Chris Lattner | 6538347 | 2007-12-18 07:15:40 +0000 | [diff] [blame] | 947 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 948 | |
Chris Lattner | 76e773a | 2007-07-18 18:38:36 +0000 | [diff] [blame] | 949 | // Get information about the size or align. |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 950 | if (ArgTy->isFunctionType()) { |
Chris Lattner | efdd157 | 2008-01-02 21:54:09 +0000 | [diff] [blame] | 951 | // GCC extension: sizeof(function) = 1. |
| 952 | Result = Exp->isSizeOf() ? 1 : 4; |
Anders Carlsson | 6a24acb | 2008-02-16 01:20:23 +0000 | [diff] [blame] | 953 | } else { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 954 | unsigned CharSize = Ctx.Target.getCharWidth(); |
Anders Carlsson | 6a24acb | 2008-02-16 01:20:23 +0000 | [diff] [blame] | 955 | if (Exp->isSizeOf()) |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 956 | Result = Ctx.getTypeSize(ArgTy) / CharSize; |
Anders Carlsson | 6a24acb | 2008-02-16 01:20:23 +0000 | [diff] [blame] | 957 | else |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 958 | Result = Ctx.getTypeAlign(ArgTy) / CharSize; |
Ted Kremenek | 060e470 | 2007-12-17 17:38:43 +0000 | [diff] [blame] | 959 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 960 | break; |
| 961 | } |
| 962 | case BinaryOperatorClass: { |
| 963 | const BinaryOperator *Exp = cast<BinaryOperator>(this); |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 964 | llvm::APSInt LHS, RHS; |
| 965 | |
| 966 | // Initialize result to have correct signedness and width. |
| 967 | Result = llvm::APSInt(static_cast<uint32_t>(Ctx.getTypeSize(getType())), |
Eli Friedman | b11e778 | 2008-11-13 02:13:11 +0000 | [diff] [blame] | 968 | !getType()->isSignedIntegerType()); |
| 969 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 970 | // The LHS of a constant expr is always evaluated and needed. |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 971 | if (!Exp->getLHS()->isIntegerConstantExpr(LHS, Ctx, Loc, isEvaluated)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 972 | return false; |
| 973 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 974 | // The short-circuiting &&/|| operators don't necessarily evaluate their |
| 975 | // RHS. Make sure to pass isEvaluated down correctly. |
| 976 | if (Exp->isLogicalOp()) { |
| 977 | bool RHSEval; |
| 978 | if (Exp->getOpcode() == BinaryOperator::LAnd) |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 979 | RHSEval = LHS != 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 980 | else { |
| 981 | assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical"); |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 982 | RHSEval = LHS == 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 983 | } |
| 984 | |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 985 | if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 986 | isEvaluated & RHSEval)) |
| 987 | return false; |
| 988 | } else { |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 989 | if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 990 | return false; |
| 991 | } |
| 992 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 993 | switch (Exp->getOpcode()) { |
| 994 | default: |
| 995 | if (Loc) *Loc = getLocStart(); |
| 996 | return false; |
| 997 | case BinaryOperator::Mul: |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 998 | Result = LHS * RHS; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 999 | break; |
| 1000 | case BinaryOperator::Div: |
| 1001 | if (RHS == 0) { |
| 1002 | if (!isEvaluated) break; |
| 1003 | if (Loc) *Loc = getLocStart(); |
| 1004 | return false; |
| 1005 | } |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 1006 | Result = LHS / RHS; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1007 | break; |
| 1008 | case BinaryOperator::Rem: |
| 1009 | if (RHS == 0) { |
| 1010 | if (!isEvaluated) break; |
| 1011 | if (Loc) *Loc = getLocStart(); |
| 1012 | return false; |
| 1013 | } |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 1014 | Result = LHS % RHS; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1015 | break; |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 1016 | case BinaryOperator::Add: Result = LHS + RHS; break; |
| 1017 | case BinaryOperator::Sub: Result = LHS - RHS; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1018 | case BinaryOperator::Shl: |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 1019 | Result = LHS << |
| 1020 | static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1)); |
| 1021 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1022 | case BinaryOperator::Shr: |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 1023 | Result = LHS >> |
| 1024 | static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1025 | break; |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 1026 | case BinaryOperator::LT: Result = LHS < RHS; break; |
| 1027 | case BinaryOperator::GT: Result = LHS > RHS; break; |
| 1028 | case BinaryOperator::LE: Result = LHS <= RHS; break; |
| 1029 | case BinaryOperator::GE: Result = LHS >= RHS; break; |
| 1030 | case BinaryOperator::EQ: Result = LHS == RHS; break; |
| 1031 | case BinaryOperator::NE: Result = LHS != RHS; break; |
| 1032 | case BinaryOperator::And: Result = LHS & RHS; break; |
| 1033 | case BinaryOperator::Xor: Result = LHS ^ RHS; break; |
| 1034 | case BinaryOperator::Or: Result = LHS | RHS; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1035 | case BinaryOperator::LAnd: |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 1036 | Result = LHS != 0 && RHS != 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1037 | break; |
| 1038 | case BinaryOperator::LOr: |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 1039 | Result = LHS != 0 || RHS != 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1040 | break; |
Eli Friedman | b11e778 | 2008-11-13 02:13:11 +0000 | [diff] [blame] | 1041 | |
| 1042 | case BinaryOperator::Comma: |
| 1043 | // C99 6.6p3: "shall not contain assignment, ..., or comma operators, |
| 1044 | // *except* when they are contained within a subexpression that is not |
| 1045 | // evaluated". Note that Assignment can never happen due to constraints |
| 1046 | // on the LHS subexpr, so we don't need to check it here. |
| 1047 | if (isEvaluated) { |
| 1048 | if (Loc) *Loc = getLocStart(); |
| 1049 | return false; |
| 1050 | } |
| 1051 | |
| 1052 | // The result of the constant expr is the RHS. |
| 1053 | Result = RHS; |
| 1054 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!"); |
| 1058 | break; |
| 1059 | } |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 1060 | case ImplicitCastExprClass: |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 1061 | case CStyleCastExprClass: |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1062 | case CXXFunctionalCastExprClass: { |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 1063 | const Expr *SubExpr = cast<CastExpr>(this)->getSubExpr(); |
| 1064 | SourceLocation CastLoc = getLocStart(); |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 1065 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1066 | // C99 6.6p6: shall only convert arithmetic types to integer types. |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 1067 | if (!SubExpr->getType()->isArithmeticType() || |
| 1068 | !getType()->isIntegerType()) { |
| 1069 | if (Loc) *Loc = SubExpr->getLocStart(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1070 | return false; |
| 1071 | } |
Chris Lattner | 987b15d | 2007-09-22 19:04:13 +0000 | [diff] [blame] | 1072 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1073 | uint32_t DestWidth = static_cast<uint32_t>(Ctx.getTypeSize(getType())); |
Chris Lattner | 987b15d | 2007-09-22 19:04:13 +0000 | [diff] [blame] | 1074 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1075 | // Handle simple integer->integer casts. |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 1076 | if (SubExpr->getType()->isIntegerType()) { |
| 1077 | if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1078 | return false; |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 1079 | |
| 1080 | // Figure out if this is a truncate, extend or noop cast. |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 1081 | // If the input is signed, do a sign extend, noop, or truncate. |
Chris Lattner | c0a356b | 2008-01-09 18:59:34 +0000 | [diff] [blame] | 1082 | if (getType()->isBooleanType()) { |
| 1083 | // Conversion to bool compares against zero. |
| 1084 | Result = Result != 0; |
| 1085 | Result.zextOrTrunc(DestWidth); |
| 1086 | } else if (SubExpr->getType()->isSignedIntegerType()) |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 1087 | Result.sextOrTrunc(DestWidth); |
| 1088 | else // If the input is unsigned, do a zero extend, noop, or truncate. |
| 1089 | Result.zextOrTrunc(DestWidth); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1090 | break; |
| 1091 | } |
| 1092 | |
| 1093 | // Allow floating constants that are the immediate operands of casts or that |
| 1094 | // are parenthesized. |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 1095 | const Expr *Operand = SubExpr; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1096 | while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand)) |
| 1097 | Operand = PE->getSubExpr(); |
Chris Lattner | 987b15d | 2007-09-22 19:04:13 +0000 | [diff] [blame] | 1098 | |
| 1099 | // If this isn't a floating literal, we can't handle it. |
| 1100 | const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand); |
| 1101 | if (!FL) { |
| 1102 | if (Loc) *Loc = Operand->getLocStart(); |
| 1103 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1104 | } |
Chris Lattner | c0a356b | 2008-01-09 18:59:34 +0000 | [diff] [blame] | 1105 | |
| 1106 | // If the destination is boolean, compare against zero. |
| 1107 | if (getType()->isBooleanType()) { |
| 1108 | Result = !FL->getValue().isZero(); |
| 1109 | Result.zextOrTrunc(DestWidth); |
| 1110 | break; |
| 1111 | } |
Chris Lattner | 987b15d | 2007-09-22 19:04:13 +0000 | [diff] [blame] | 1112 | |
| 1113 | // Determine whether we are converting to unsigned or signed. |
| 1114 | bool DestSigned = getType()->isSignedIntegerType(); |
Chris Lattner | ccc213f | 2007-09-26 00:47:26 +0000 | [diff] [blame] | 1115 | |
| 1116 | // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can |
| 1117 | // be called multiple times per AST. |
Dale Johannesen | ee5a700 | 2008-10-09 23:02:32 +0000 | [diff] [blame] | 1118 | uint64_t Space[4]; |
| 1119 | bool ignored; |
Chris Lattner | ccc213f | 2007-09-26 00:47:26 +0000 | [diff] [blame] | 1120 | (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned, |
Dale Johannesen | ee5a700 | 2008-10-09 23:02:32 +0000 | [diff] [blame] | 1121 | llvm::APFloat::rmTowardZero, |
| 1122 | &ignored); |
Chris Lattner | 987b15d | 2007-09-22 19:04:13 +0000 | [diff] [blame] | 1123 | Result = llvm::APInt(DestWidth, 4, Space); |
| 1124 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1125 | } |
| 1126 | case ConditionalOperatorClass: { |
| 1127 | const ConditionalOperator *Exp = cast<ConditionalOperator>(this); |
| 1128 | |
Chris Lattner | 28daa53 | 2008-12-12 06:55:44 +0000 | [diff] [blame] | 1129 | const Expr *Cond = Exp->getCond(); |
| 1130 | |
| 1131 | if (!Cond->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1132 | return false; |
| 1133 | |
| 1134 | const Expr *TrueExp = Exp->getLHS(); |
| 1135 | const Expr *FalseExp = Exp->getRHS(); |
| 1136 | if (Result == 0) std::swap(TrueExp, FalseExp); |
| 1137 | |
Chris Lattner | 28daa53 | 2008-12-12 06:55:44 +0000 | [diff] [blame] | 1138 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
| 1139 | // then only the true side is actually considered in an integer constant |
Chris Lattner | 42b83dd | 2008-12-12 18:00:51 +0000 | [diff] [blame] | 1140 | // expression, and it is fully evaluated. This is an important GNU |
| 1141 | // extension. See GCC PR38377 for discussion. |
Chris Lattner | 28daa53 | 2008-12-12 06:55:44 +0000 | [diff] [blame] | 1142 | if (const CallExpr *CallCE = dyn_cast<CallExpr>(Cond->IgnoreParenCasts())) |
Chris Lattner | 42b83dd | 2008-12-12 18:00:51 +0000 | [diff] [blame] | 1143 | if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p) { |
| 1144 | EvalResult EVResult; |
| 1145 | if (!Evaluate(EVResult, Ctx) || EVResult.HasSideEffects) |
| 1146 | return false; |
| 1147 | assert(EVResult.Val.isInt() && "FP conditional expr not expected"); |
| 1148 | Result = EVResult.Val.getInt(); |
| 1149 | if (Loc) *Loc = EVResult.DiagLoc; |
| 1150 | return true; |
| 1151 | } |
Chris Lattner | 28daa53 | 2008-12-12 06:55:44 +0000 | [diff] [blame] | 1152 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1153 | // Evaluate the false one first, discard the result. |
Anders Carlsson | 3907323 | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 1154 | if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1155 | return false; |
| 1156 | // Evalute the true one, capture the result. |
Anders Carlsson | 3907323 | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 1157 | if (TrueExp && |
| 1158 | !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1159 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1160 | break; |
| 1161 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1162 | case CXXDefaultArgExprClass: |
| 1163 | return cast<CXXDefaultArgExpr>(this) |
| 1164 | ->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1165 | |
| 1166 | case UnaryTypeTraitExprClass: |
| 1167 | Result = cast<UnaryTypeTraitExpr>(this)->Evaluate(); |
| 1168 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | // Cases that are valid constant exprs fall through to here. |
| 1172 | Result.setIsUnsigned(getType()->isUnsignedIntegerType()); |
| 1173 | return true; |
| 1174 | } |
| 1175 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1176 | /// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an |
| 1177 | /// integer constant expression with the value zero, or if this is one that is |
| 1178 | /// cast to void*. |
Anders Carlsson | efa9b38 | 2008-12-01 02:13:57 +0000 | [diff] [blame] | 1179 | bool Expr::isNullPointerConstant(ASTContext &Ctx) const |
| 1180 | { |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1181 | // Strip off a cast to void*, if it exists. Except in C++. |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 1182 | if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) { |
Sebastian Redl | 6215dee | 2008-11-04 11:45:54 +0000 | [diff] [blame] | 1183 | if (!Ctx.getLangOptions().CPlusPlus) { |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1184 | // Check that it is a cast to void*. |
| 1185 | if (const PointerType *PT = CE->getType()->getAsPointerType()) { |
| 1186 | QualType Pointee = PT->getPointeeType(); |
| 1187 | if (Pointee.getCVRQualifiers() == 0 && |
| 1188 | Pointee->isVoidType() && // to void* |
| 1189 | CE->getSubExpr()->getType()->isIntegerType()) // from int. |
Anders Carlsson | d265277 | 2008-12-01 06:28:23 +0000 | [diff] [blame] | 1190 | return CE->getSubExpr()->isNullPointerConstant(Ctx); |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1191 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1192 | } |
Steve Naroff | aa58f00 | 2008-01-14 16:10:57 +0000 | [diff] [blame] | 1193 | } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) { |
| 1194 | // Ignore the ImplicitCastExpr type entirely. |
Anders Carlsson | d265277 | 2008-12-01 06:28:23 +0000 | [diff] [blame] | 1195 | return ICE->getSubExpr()->isNullPointerConstant(Ctx); |
Steve Naroff | aa58f00 | 2008-01-14 16:10:57 +0000 | [diff] [blame] | 1196 | } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) { |
| 1197 | // Accept ((void*)0) as a null pointer constant, as many other |
| 1198 | // implementations do. |
Anders Carlsson | d265277 | 2008-12-01 06:28:23 +0000 | [diff] [blame] | 1199 | return PE->getSubExpr()->isNullPointerConstant(Ctx); |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1200 | } else if (const CXXDefaultArgExpr *DefaultArg |
| 1201 | = dyn_cast<CXXDefaultArgExpr>(this)) { |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1202 | // See through default argument expressions |
Anders Carlsson | d265277 | 2008-12-01 06:28:23 +0000 | [diff] [blame] | 1203 | return DefaultArg->getExpr()->isNullPointerConstant(Ctx); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 1204 | } else if (isa<GNUNullExpr>(this)) { |
| 1205 | // The GNU __null extension is always a null pointer constant. |
| 1206 | return true; |
Steve Naroff | aaffbf7 | 2008-01-14 02:53:34 +0000 | [diff] [blame] | 1207 | } |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 1208 | |
Steve Naroff | aa58f00 | 2008-01-14 16:10:57 +0000 | [diff] [blame] | 1209 | // This expression must be an integer type. |
| 1210 | if (!getType()->isIntegerType()) |
| 1211 | return false; |
| 1212 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1213 | // If we have an integer constant expression, we need to *evaluate* it and |
| 1214 | // test for the value 0. |
Anders Carlsson | d265277 | 2008-12-01 06:28:23 +0000 | [diff] [blame] | 1215 | // FIXME: We should probably return false if we're compiling in strict mode |
| 1216 | // and Diag is not null (this indicates that the value was foldable but not |
| 1217 | // an ICE. |
| 1218 | EvalResult Result; |
Anders Carlsson | efa9b38 | 2008-12-01 02:13:57 +0000 | [diff] [blame] | 1219 | return Evaluate(Result, Ctx) && !Result.HasSideEffects && |
Anders Carlsson | d265277 | 2008-12-01 06:28:23 +0000 | [diff] [blame] | 1220 | Result.Val.isInt() && Result.Val.getInt() == 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1221 | } |
Steve Naroff | 31a4584 | 2007-07-28 23:10:27 +0000 | [diff] [blame] | 1222 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1223 | /// isBitField - Return true if this expression is a bit-field. |
| 1224 | bool Expr::isBitField() { |
| 1225 | Expr *E = this->IgnoreParenCasts(); |
| 1226 | if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E)) |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1227 | if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl())) |
| 1228 | return Field->isBitField(); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1229 | return false; |
| 1230 | } |
| 1231 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1232 | unsigned ExtVectorElementExpr::getNumElements() const { |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1233 | if (const VectorType *VT = getType()->getAsVectorType()) |
| 1234 | return VT->getNumElements(); |
| 1235 | return 1; |
Chris Lattner | 4d0ac88 | 2007-08-03 16:00:20 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1238 | /// containsDuplicateElements - Return true if any element access is repeated. |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1239 | bool ExtVectorElementExpr::containsDuplicateElements() const { |
Steve Naroff | fec0b49 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 1240 | const char *compStr = Accessor.getName(); |
Chris Lattner | 7e3e9b1 | 2008-11-19 07:55:04 +0000 | [diff] [blame] | 1241 | unsigned length = Accessor.getLength(); |
Nate Begeman | 190d6a2 | 2009-01-18 02:01:21 +0000 | [diff] [blame] | 1242 | |
| 1243 | // Halving swizzles do not contain duplicate elements. |
| 1244 | if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || |
| 1245 | !strcmp(compStr, "even") || !strcmp(compStr, "odd")) |
| 1246 | return false; |
| 1247 | |
| 1248 | // Advance past s-char prefix on hex swizzles. |
| 1249 | if (*compStr == 's') { |
| 1250 | compStr++; |
| 1251 | length--; |
| 1252 | } |
Steve Naroff | fec0b49 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 1253 | |
Chris Lattner | 7e3e9b1 | 2008-11-19 07:55:04 +0000 | [diff] [blame] | 1254 | for (unsigned i = 0; i != length-1; i++) { |
Steve Naroff | fec0b49 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 1255 | const char *s = compStr+i; |
| 1256 | for (const char c = *s++; *s; s++) |
| 1257 | if (c == *s) |
| 1258 | return true; |
| 1259 | } |
| 1260 | return false; |
| 1261 | } |
Chris Lattner | b8f849d | 2007-08-02 23:36:59 +0000 | [diff] [blame] | 1262 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1263 | /// getEncodedElementAccess - We encode the fields as a llvm ConstantArray. |
Nate Begeman | 3b8d116 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 1264 | void ExtVectorElementExpr::getEncodedElementAccess( |
| 1265 | llvm::SmallVectorImpl<unsigned> &Elts) const { |
Chris Lattner | 7e3e9b1 | 2008-11-19 07:55:04 +0000 | [diff] [blame] | 1266 | const char *compStr = Accessor.getName(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1267 | if (*compStr == 's') |
| 1268 | compStr++; |
| 1269 | |
| 1270 | bool isHi = !strcmp(compStr, "hi"); |
| 1271 | bool isLo = !strcmp(compStr, "lo"); |
| 1272 | bool isEven = !strcmp(compStr, "even"); |
| 1273 | bool isOdd = !strcmp(compStr, "odd"); |
| 1274 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1275 | for (unsigned i = 0, e = getNumElements(); i != e; ++i) { |
| 1276 | uint64_t Index; |
| 1277 | |
| 1278 | if (isHi) |
| 1279 | Index = e + i; |
| 1280 | else if (isLo) |
| 1281 | Index = i; |
| 1282 | else if (isEven) |
| 1283 | Index = 2 * i; |
| 1284 | else if (isOdd) |
| 1285 | Index = 2 * i + 1; |
| 1286 | else |
| 1287 | Index = ExtVectorType::getAccessorIdx(compStr[i]); |
Chris Lattner | b8f849d | 2007-08-02 23:36:59 +0000 | [diff] [blame] | 1288 | |
Nate Begeman | 3b8d116 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 1289 | Elts.push_back(Index); |
Chris Lattner | b8f849d | 2007-08-02 23:36:59 +0000 | [diff] [blame] | 1290 | } |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1293 | // constructor for instance messages. |
Steve Naroff | bcfb06a | 2007-09-28 22:22:11 +0000 | [diff] [blame] | 1294 | ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo, |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1295 | QualType retType, ObjCMethodDecl *mproto, |
Steve Naroff | db611d5 | 2007-11-03 16:37:59 +0000 | [diff] [blame] | 1296 | SourceLocation LBrac, SourceLocation RBrac, |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1297 | Expr **ArgExprs, unsigned nargs) |
Steve Naroff | db611d5 | 2007-11-03 16:37:59 +0000 | [diff] [blame] | 1298 | : Expr(ObjCMessageExprClass, retType), SelName(selInfo), |
Ted Kremenek | ea958e57 | 2008-05-01 17:26:20 +0000 | [diff] [blame] | 1299 | MethodProto(mproto) { |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1300 | NumArgs = nargs; |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1301 | SubExprs = new Stmt*[NumArgs+1]; |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1302 | SubExprs[RECEIVER] = receiver; |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1303 | if (NumArgs) { |
| 1304 | for (unsigned i = 0; i != NumArgs; ++i) |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1305 | SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]); |
| 1306 | } |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 1307 | LBracloc = LBrac; |
| 1308 | RBracloc = RBrac; |
| 1309 | } |
| 1310 | |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1311 | // constructor for class messages. |
| 1312 | // FIXME: clsName should be typed to ObjCInterfaceType |
Steve Naroff | bcfb06a | 2007-09-28 22:22:11 +0000 | [diff] [blame] | 1313 | ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo, |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1314 | QualType retType, ObjCMethodDecl *mproto, |
Steve Naroff | db611d5 | 2007-11-03 16:37:59 +0000 | [diff] [blame] | 1315 | SourceLocation LBrac, SourceLocation RBrac, |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1316 | Expr **ArgExprs, unsigned nargs) |
Steve Naroff | db611d5 | 2007-11-03 16:37:59 +0000 | [diff] [blame] | 1317 | : Expr(ObjCMessageExprClass, retType), SelName(selInfo), |
Ted Kremenek | ea958e57 | 2008-05-01 17:26:20 +0000 | [diff] [blame] | 1318 | MethodProto(mproto) { |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1319 | NumArgs = nargs; |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1320 | SubExprs = new Stmt*[NumArgs+1]; |
Ted Kremenek | 4df728e | 2008-06-24 15:50:53 +0000 | [diff] [blame] | 1321 | SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown); |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1322 | if (NumArgs) { |
| 1323 | for (unsigned i = 0; i != NumArgs; ++i) |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1324 | SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]); |
| 1325 | } |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 1326 | LBracloc = LBrac; |
| 1327 | RBracloc = RBrac; |
| 1328 | } |
| 1329 | |
Ted Kremenek | 4df728e | 2008-06-24 15:50:53 +0000 | [diff] [blame] | 1330 | // constructor for class messages. |
| 1331 | ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo, |
| 1332 | QualType retType, ObjCMethodDecl *mproto, |
| 1333 | SourceLocation LBrac, SourceLocation RBrac, |
| 1334 | Expr **ArgExprs, unsigned nargs) |
| 1335 | : Expr(ObjCMessageExprClass, retType), SelName(selInfo), |
| 1336 | MethodProto(mproto) { |
| 1337 | NumArgs = nargs; |
| 1338 | SubExprs = new Stmt*[NumArgs+1]; |
| 1339 | SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown); |
| 1340 | if (NumArgs) { |
| 1341 | for (unsigned i = 0; i != NumArgs; ++i) |
| 1342 | SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]); |
| 1343 | } |
| 1344 | LBracloc = LBrac; |
| 1345 | RBracloc = RBrac; |
| 1346 | } |
| 1347 | |
| 1348 | ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const { |
| 1349 | uintptr_t x = (uintptr_t) SubExprs[RECEIVER]; |
| 1350 | switch (x & Flags) { |
| 1351 | default: |
| 1352 | assert(false && "Invalid ObjCMessageExpr."); |
| 1353 | case IsInstMeth: |
| 1354 | return ClassInfo(0, 0); |
| 1355 | case IsClsMethDeclUnknown: |
| 1356 | return ClassInfo(0, (IdentifierInfo*) (x & ~Flags)); |
| 1357 | case IsClsMethDeclKnown: { |
| 1358 | ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags); |
| 1359 | return ClassInfo(D, D->getIdentifier()); |
| 1360 | } |
| 1361 | } |
| 1362 | } |
| 1363 | |
Chris Lattner | 27437ca | 2007-10-25 00:29:32 +0000 | [diff] [blame] | 1364 | bool ChooseExpr::isConditionTrue(ASTContext &C) const { |
Daniel Dunbar | 32442bb | 2008-08-13 23:47:13 +0000 | [diff] [blame] | 1365 | return getCond()->getIntegerConstantExprValue(C) != 0; |
Chris Lattner | 27437ca | 2007-10-25 00:29:32 +0000 | [diff] [blame] | 1366 | } |
| 1367 | |
Chris Lattner | 670a62c | 2008-12-12 05:35:08 +0000 | [diff] [blame] | 1368 | static int64_t evaluateOffsetOf(ASTContext& C, const Expr *E) { |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 1369 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) { |
| 1370 | QualType Ty = ME->getBase()->getType(); |
| 1371 | |
| 1372 | RecordDecl *RD = Ty->getAsRecordType()->getDecl(); |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1373 | const ASTRecordLayout &RL = C.getASTRecordLayout(RD); |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1374 | if (FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) { |
| 1375 | // FIXME: This is linear time. And the fact that we're indexing |
| 1376 | // into the layout by position in the record means that we're |
| 1377 | // either stuck numbering the fields in the AST or we have to keep |
| 1378 | // the linear search (yuck and yuck). |
| 1379 | unsigned i = 0; |
| 1380 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 1381 | FieldEnd = RD->field_end(); |
| 1382 | Field != FieldEnd; (void)++Field, ++i) { |
| 1383 | if (*Field == FD) |
| 1384 | break; |
| 1385 | } |
| 1386 | |
| 1387 | return RL.getFieldOffset(i) + evaluateOffsetOf(C, ME->getBase()); |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 1388 | } |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 1389 | } else if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) { |
| 1390 | const Expr *Base = ASE->getBase(); |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 1391 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1392 | int64_t size = C.getTypeSize(ASE->getType()); |
Daniel Dunbar | 32442bb | 2008-08-13 23:47:13 +0000 | [diff] [blame] | 1393 | size *= ASE->getIdx()->getIntegerConstantExprValue(C).getSExtValue(); |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 1394 | |
| 1395 | return size + evaluateOffsetOf(C, Base); |
| 1396 | } else if (isa<CompoundLiteralExpr>(E)) |
| 1397 | return 0; |
| 1398 | |
| 1399 | assert(0 && "Unknown offsetof subexpression!"); |
| 1400 | return 0; |
| 1401 | } |
| 1402 | |
| 1403 | int64_t UnaryOperator::evaluateOffsetOf(ASTContext& C) const |
| 1404 | { |
| 1405 | assert(Opc == OffsetOf && "Unary operator not offsetof!"); |
| 1406 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1407 | unsigned CharSize = C.Target.getCharWidth(); |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1408 | return ::evaluateOffsetOf(C, cast<Expr>(Val)) / CharSize; |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 1409 | } |
| 1410 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1411 | void SizeOfAlignOfExpr::Destroy(ASTContext& C) { |
| 1412 | // Override default behavior of traversing children. If this has a type |
| 1413 | // operand and the type is a variable-length array, the child iteration |
| 1414 | // will iterate over the size expression. However, this expression belongs |
| 1415 | // to the type, not to this, so we don't want to delete it. |
| 1416 | // We still want to delete this expression. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1417 | if (isArgumentType()) { |
| 1418 | this->~SizeOfAlignOfExpr(); |
| 1419 | C.Deallocate(this); |
| 1420 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1421 | else |
| 1422 | Expr::Destroy(C); |
Daniel Dunbar | 9048891 | 2008-08-28 18:02:04 +0000 | [diff] [blame] | 1423 | } |
| 1424 | |
Ted Kremenek | fb7413f | 2009-02-09 17:08:14 +0000 | [diff] [blame] | 1425 | void OverloadExpr::Destroy(ASTContext& C) { |
| 1426 | DestroyChildren(C); |
| 1427 | C.Deallocate(SubExprs); |
| 1428 | this->~OverloadExpr(); |
| 1429 | C.Deallocate(this); |
| 1430 | } |
| 1431 | |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1432 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1433 | // DesignatedInitExpr |
| 1434 | //===----------------------------------------------------------------------===// |
| 1435 | |
| 1436 | IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() { |
| 1437 | assert(Kind == FieldDesignator && "Only valid on a field designator"); |
| 1438 | if (Field.NameOrField & 0x01) |
| 1439 | return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01); |
| 1440 | else |
| 1441 | return getField()->getIdentifier(); |
| 1442 | } |
| 1443 | |
| 1444 | DesignatedInitExpr * |
| 1445 | DesignatedInitExpr::Create(ASTContext &C, Designator *Designators, |
| 1446 | unsigned NumDesignators, |
| 1447 | Expr **IndexExprs, unsigned NumIndexExprs, |
| 1448 | SourceLocation ColonOrEqualLoc, |
| 1449 | bool UsesColonSyntax, Expr *Init) { |
Steve Naroff | c0ac492 | 2009-01-27 23:20:32 +0000 | [diff] [blame] | 1450 | void *Mem = C.Allocate(sizeof(DesignatedInitExpr) + |
| 1451 | sizeof(Designator) * NumDesignators + |
| 1452 | sizeof(Stmt *) * (NumIndexExprs + 1), 8); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1453 | DesignatedInitExpr *DIE |
| 1454 | = new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators, |
| 1455 | ColonOrEqualLoc, UsesColonSyntax, |
| 1456 | NumIndexExprs + 1); |
| 1457 | |
| 1458 | // Fill in the designators |
| 1459 | unsigned ExpectedNumSubExprs = 0; |
| 1460 | designators_iterator Desig = DIE->designators_begin(); |
| 1461 | for (unsigned Idx = 0; Idx < NumDesignators; ++Idx, ++Desig) { |
| 1462 | new (static_cast<void*>(Desig)) Designator(Designators[Idx]); |
| 1463 | if (Designators[Idx].isArrayDesignator()) |
| 1464 | ++ExpectedNumSubExprs; |
| 1465 | else if (Designators[Idx].isArrayRangeDesignator()) |
| 1466 | ExpectedNumSubExprs += 2; |
| 1467 | } |
| 1468 | assert(ExpectedNumSubExprs == NumIndexExprs && "Wrong number of indices!"); |
| 1469 | |
| 1470 | // Fill in the subexpressions, including the initializer expression. |
| 1471 | child_iterator Child = DIE->child_begin(); |
| 1472 | *Child++ = Init; |
| 1473 | for (unsigned Idx = 0; Idx < NumIndexExprs; ++Idx, ++Child) |
| 1474 | *Child = IndexExprs[Idx]; |
| 1475 | |
| 1476 | return DIE; |
| 1477 | } |
| 1478 | |
| 1479 | SourceRange DesignatedInitExpr::getSourceRange() const { |
| 1480 | SourceLocation StartLoc; |
| 1481 | Designator &First = *const_cast<DesignatedInitExpr*>(this)->designators_begin(); |
| 1482 | if (First.isFieldDesignator()) { |
| 1483 | if (UsesColonSyntax) |
| 1484 | StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc); |
| 1485 | else |
| 1486 | StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc); |
| 1487 | } else |
| 1488 | StartLoc = SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc); |
| 1489 | return SourceRange(StartLoc, getInit()->getSourceRange().getEnd()); |
| 1490 | } |
| 1491 | |
| 1492 | DesignatedInitExpr::designators_iterator DesignatedInitExpr::designators_begin() { |
| 1493 | char* Ptr = static_cast<char*>(static_cast<void *>(this)); |
| 1494 | Ptr += sizeof(DesignatedInitExpr); |
| 1495 | return static_cast<Designator*>(static_cast<void*>(Ptr)); |
| 1496 | } |
| 1497 | |
| 1498 | DesignatedInitExpr::designators_iterator DesignatedInitExpr::designators_end() { |
| 1499 | return designators_begin() + NumDesignators; |
| 1500 | } |
| 1501 | |
| 1502 | Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) { |
| 1503 | assert(D.Kind == Designator::ArrayDesignator && "Requires array designator"); |
| 1504 | char* Ptr = static_cast<char*>(static_cast<void *>(this)); |
| 1505 | Ptr += sizeof(DesignatedInitExpr); |
| 1506 | Ptr += sizeof(Designator) * NumDesignators; |
| 1507 | Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); |
| 1508 | return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1)); |
| 1509 | } |
| 1510 | |
| 1511 | Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) { |
| 1512 | assert(D.Kind == Designator::ArrayRangeDesignator && |
| 1513 | "Requires array range designator"); |
| 1514 | char* Ptr = static_cast<char*>(static_cast<void *>(this)); |
| 1515 | Ptr += sizeof(DesignatedInitExpr); |
| 1516 | Ptr += sizeof(Designator) * NumDesignators; |
| 1517 | Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); |
| 1518 | return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1)); |
| 1519 | } |
| 1520 | |
| 1521 | Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) { |
| 1522 | assert(D.Kind == Designator::ArrayRangeDesignator && |
| 1523 | "Requires array range designator"); |
| 1524 | char* Ptr = static_cast<char*>(static_cast<void *>(this)); |
| 1525 | Ptr += sizeof(DesignatedInitExpr); |
| 1526 | Ptr += sizeof(Designator) * NumDesignators; |
| 1527 | Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); |
| 1528 | return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2)); |
| 1529 | } |
| 1530 | |
| 1531 | //===----------------------------------------------------------------------===// |
Ted Kremenek | ce2fc3a | 2008-10-27 18:40:21 +0000 | [diff] [blame] | 1532 | // ExprIterator. |
| 1533 | //===----------------------------------------------------------------------===// |
| 1534 | |
| 1535 | Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); } |
| 1536 | Expr* ExprIterator::operator*() const { return cast<Expr>(*I); } |
| 1537 | Expr* ExprIterator::operator->() const { return cast<Expr>(*I); } |
| 1538 | const Expr* ConstExprIterator::operator[](size_t idx) const { |
| 1539 | return cast<Expr>(I[idx]); |
| 1540 | } |
| 1541 | const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); } |
| 1542 | const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); } |
| 1543 | |
| 1544 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1545 | // Child Iterators for iterating over subexpressions/substatements |
| 1546 | //===----------------------------------------------------------------------===// |
| 1547 | |
| 1548 | // DeclRefExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1549 | Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); } |
| 1550 | Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1551 | |
Steve Naroff | 7779db4 | 2007-11-12 14:29:37 +0000 | [diff] [blame] | 1552 | // ObjCIvarRefExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1553 | Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; } |
| 1554 | Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; } |
Steve Naroff | 7779db4 | 2007-11-12 14:29:37 +0000 | [diff] [blame] | 1555 | |
Steve Naroff | e3e9add | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 1556 | // ObjCPropertyRefExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1557 | Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; } |
| 1558 | Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; } |
Steve Naroff | ae78407 | 2008-05-30 00:40:33 +0000 | [diff] [blame] | 1559 | |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 1560 | // ObjCKVCRefExpr |
| 1561 | Stmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; } |
| 1562 | Stmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; } |
| 1563 | |
Douglas Gregor | cd9b46e | 2008-11-04 14:56:14 +0000 | [diff] [blame] | 1564 | // ObjCSuperExpr |
| 1565 | Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); } |
| 1566 | Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); } |
| 1567 | |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 1568 | // PredefinedExpr |
| 1569 | Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); } |
| 1570 | Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1571 | |
| 1572 | // IntegerLiteral |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1573 | Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); } |
| 1574 | Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1575 | |
| 1576 | // CharacterLiteral |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1577 | Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); } |
| 1578 | Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1579 | |
| 1580 | // FloatingLiteral |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1581 | Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); } |
| 1582 | Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1583 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1584 | // ImaginaryLiteral |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1585 | Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; } |
| 1586 | Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; } |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1587 | |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1588 | // StringLiteral |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1589 | Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); } |
| 1590 | Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1591 | |
| 1592 | // ParenExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1593 | Stmt::child_iterator ParenExpr::child_begin() { return &Val; } |
| 1594 | Stmt::child_iterator ParenExpr::child_end() { return &Val+1; } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1595 | |
| 1596 | // UnaryOperator |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1597 | Stmt::child_iterator UnaryOperator::child_begin() { return &Val; } |
| 1598 | Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1599 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1600 | // SizeOfAlignOfExpr |
| 1601 | Stmt::child_iterator SizeOfAlignOfExpr::child_begin() { |
| 1602 | // If this is of a type and the type is a VLA type (and not a typedef), the |
| 1603 | // size expression of the VLA needs to be treated as an executable expression. |
| 1604 | // Why isn't this weirdness documented better in StmtIterator? |
| 1605 | if (isArgumentType()) { |
| 1606 | if (VariableArrayType* T = dyn_cast<VariableArrayType>( |
| 1607 | getArgumentType().getTypePtr())) |
| 1608 | return child_iterator(T); |
| 1609 | return child_iterator(); |
| 1610 | } |
Sebastian Redl | d457589 | 2008-12-03 23:17:54 +0000 | [diff] [blame] | 1611 | return child_iterator(&Argument.Ex); |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1612 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1613 | Stmt::child_iterator SizeOfAlignOfExpr::child_end() { |
| 1614 | if (isArgumentType()) |
| 1615 | return child_iterator(); |
Sebastian Redl | d457589 | 2008-12-03 23:17:54 +0000 | [diff] [blame] | 1616 | return child_iterator(&Argument.Ex + 1); |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1617 | } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1618 | |
| 1619 | // ArraySubscriptExpr |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1620 | Stmt::child_iterator ArraySubscriptExpr::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1621 | return &SubExprs[0]; |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1622 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1623 | Stmt::child_iterator ArraySubscriptExpr::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1624 | return &SubExprs[0]+END_EXPR; |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
| 1627 | // CallExpr |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1628 | Stmt::child_iterator CallExpr::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1629 | return &SubExprs[0]; |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1630 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1631 | Stmt::child_iterator CallExpr::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1632 | return &SubExprs[0]+NumArgs+ARGS_START; |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1633 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1634 | |
| 1635 | // MemberExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1636 | Stmt::child_iterator MemberExpr::child_begin() { return &Base; } |
| 1637 | Stmt::child_iterator MemberExpr::child_end() { return &Base+1; } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1638 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1639 | // ExtVectorElementExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1640 | Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; } |
| 1641 | Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1642 | |
| 1643 | // CompoundLiteralExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1644 | Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; } |
| 1645 | Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1646 | |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1647 | // CastExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1648 | Stmt::child_iterator CastExpr::child_begin() { return &Op; } |
| 1649 | Stmt::child_iterator CastExpr::child_end() { return &Op+1; } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1650 | |
| 1651 | // BinaryOperator |
| 1652 | Stmt::child_iterator BinaryOperator::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1653 | return &SubExprs[0]; |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1654 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1655 | Stmt::child_iterator BinaryOperator::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1656 | return &SubExprs[0]+END_EXPR; |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
| 1659 | // ConditionalOperator |
| 1660 | Stmt::child_iterator ConditionalOperator::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1661 | return &SubExprs[0]; |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1662 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1663 | Stmt::child_iterator ConditionalOperator::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1664 | return &SubExprs[0]+END_EXPR; |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1665 | } |
| 1666 | |
| 1667 | // AddrLabelExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1668 | Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); } |
| 1669 | Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1670 | |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1671 | // StmtExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1672 | Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; } |
| 1673 | Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1674 | |
| 1675 | // TypesCompatibleExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1676 | Stmt::child_iterator TypesCompatibleExpr::child_begin() { |
| 1677 | return child_iterator(); |
| 1678 | } |
| 1679 | |
| 1680 | Stmt::child_iterator TypesCompatibleExpr::child_end() { |
| 1681 | return child_iterator(); |
| 1682 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1683 | |
| 1684 | // ChooseExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1685 | Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; } |
| 1686 | Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1687 | |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 1688 | // GNUNullExpr |
| 1689 | Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); } |
| 1690 | Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); } |
| 1691 | |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 1692 | // OverloadExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1693 | Stmt::child_iterator OverloadExpr::child_begin() { return &SubExprs[0]; } |
| 1694 | Stmt::child_iterator OverloadExpr::child_end() { return &SubExprs[0]+NumExprs; } |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 1695 | |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1696 | // ShuffleVectorExpr |
| 1697 | Stmt::child_iterator ShuffleVectorExpr::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1698 | return &SubExprs[0]; |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1699 | } |
| 1700 | Stmt::child_iterator ShuffleVectorExpr::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1701 | return &SubExprs[0]+NumExprs; |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 1704 | // VAArgExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1705 | Stmt::child_iterator VAArgExpr::child_begin() { return &Val; } |
| 1706 | Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; } |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 1707 | |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 1708 | // InitListExpr |
| 1709 | Stmt::child_iterator InitListExpr::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1710 | return InitExprs.size() ? &InitExprs[0] : 0; |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 1711 | } |
| 1712 | Stmt::child_iterator InitListExpr::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1713 | return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0; |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1716 | // DesignatedInitExpr |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1717 | Stmt::child_iterator DesignatedInitExpr::child_begin() { |
| 1718 | char* Ptr = static_cast<char*>(static_cast<void *>(this)); |
| 1719 | Ptr += sizeof(DesignatedInitExpr); |
| 1720 | Ptr += sizeof(Designator) * NumDesignators; |
| 1721 | return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); |
| 1722 | } |
| 1723 | Stmt::child_iterator DesignatedInitExpr::child_end() { |
| 1724 | return child_iterator(&*child_begin() + NumSubExprs); |
| 1725 | } |
| 1726 | |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1727 | // ImplicitValueInitExpr |
| 1728 | Stmt::child_iterator ImplicitValueInitExpr::child_begin() { |
| 1729 | return child_iterator(); |
| 1730 | } |
| 1731 | |
| 1732 | Stmt::child_iterator ImplicitValueInitExpr::child_end() { |
| 1733 | return child_iterator(); |
| 1734 | } |
| 1735 | |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1736 | // ObjCStringLiteral |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1737 | Stmt::child_iterator ObjCStringLiteral::child_begin() { |
| 1738 | return child_iterator(); |
| 1739 | } |
| 1740 | Stmt::child_iterator ObjCStringLiteral::child_end() { |
| 1741 | return child_iterator(); |
| 1742 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1743 | |
| 1744 | // ObjCEncodeExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1745 | Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); } |
| 1746 | Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1747 | |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 1748 | // ObjCSelectorExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1749 | Stmt::child_iterator ObjCSelectorExpr::child_begin() { |
| 1750 | return child_iterator(); |
| 1751 | } |
| 1752 | Stmt::child_iterator ObjCSelectorExpr::child_end() { |
| 1753 | return child_iterator(); |
| 1754 | } |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 1755 | |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 1756 | // ObjCProtocolExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1757 | Stmt::child_iterator ObjCProtocolExpr::child_begin() { |
| 1758 | return child_iterator(); |
| 1759 | } |
| 1760 | Stmt::child_iterator ObjCProtocolExpr::child_end() { |
| 1761 | return child_iterator(); |
| 1762 | } |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 1763 | |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 1764 | // ObjCMessageExpr |
Ted Kremenek | ea958e57 | 2008-05-01 17:26:20 +0000 | [diff] [blame] | 1765 | Stmt::child_iterator ObjCMessageExpr::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1766 | return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START; |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 1767 | } |
| 1768 | Stmt::child_iterator ObjCMessageExpr::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1769 | return &SubExprs[0]+ARGS_START+getNumArgs(); |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 1770 | } |
| 1771 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 1772 | // Blocks |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 1773 | Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); } |
| 1774 | Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); } |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 1775 | |
Ted Kremenek | 9da13f9 | 2008-09-26 23:24:14 +0000 | [diff] [blame] | 1776 | Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();} |
| 1777 | Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); } |