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