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 | } |
| 868 | if (Loc) *Loc = getLocStart(); |
| 869 | return false; |
| 870 | case UnaryOperatorClass: { |
| 871 | const UnaryOperator *Exp = cast<UnaryOperator>(this); |
| 872 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 873 | // Get the operand value. If this is offsetof, do not evalute the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 874 | // operand. This affects C99 6.6p3. |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 875 | if (!Exp->isOffsetOfOp() && !Exp->getSubExpr()-> |
| 876 | isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 877 | return false; |
| 878 | |
| 879 | switch (Exp->getOpcode()) { |
| 880 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 881 | // See C99 6.6p3. |
| 882 | default: |
| 883 | if (Loc) *Loc = Exp->getOperatorLoc(); |
| 884 | return false; |
| 885 | case UnaryOperator::Extension: |
Chris Lattner | 76e773a | 2007-07-18 18:38:36 +0000 | [diff] [blame] | 886 | return true; // FIXME: this is wrong. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 887 | case UnaryOperator::LNot: { |
Chris Lattner | bf75538 | 2008-01-25 19:16:19 +0000 | [diff] [blame] | 888 | bool Val = Result == 0; |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 889 | Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 890 | Result = Val; |
| 891 | break; |
| 892 | } |
| 893 | case UnaryOperator::Plus: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 894 | break; |
| 895 | case UnaryOperator::Minus: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 896 | Result = -Result; |
| 897 | break; |
| 898 | case UnaryOperator::Not: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 899 | Result = ~Result; |
| 900 | break; |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 901 | case UnaryOperator::OffsetOf: |
Daniel Dunbar | aa1f9f1 | 2008-08-28 18:42:20 +0000 | [diff] [blame] | 902 | Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 903 | Result = Exp->evaluateOffsetOf(Ctx); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 904 | } |
| 905 | break; |
| 906 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 907 | case SizeOfAlignOfExprClass: { |
| 908 | const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(this); |
Chris Lattner | a269ebf | 2008-02-21 05:45:29 +0000 | [diff] [blame] | 909 | |
| 910 | // Return the result in the right width. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 911 | Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType()))); |
Chris Lattner | a269ebf | 2008-02-21 05:45:29 +0000 | [diff] [blame] | 912 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 913 | QualType ArgTy = Exp->getTypeOfArgument(); |
Chris Lattner | a269ebf | 2008-02-21 05:45:29 +0000 | [diff] [blame] | 914 | // sizeof(void) and __alignof__(void) = 1 as a gcc extension. |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 915 | if (ArgTy->isVoidType()) { |
Chris Lattner | a269ebf | 2008-02-21 05:45:29 +0000 | [diff] [blame] | 916 | Result = 1; |
| 917 | break; |
| 918 | } |
| 919 | |
| 920 | // 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] | 921 | if (Exp->isSizeOf() && !ArgTy->isConstantSizeType()) { |
Chris Lattner | 6538347 | 2007-12-18 07:15:40 +0000 | [diff] [blame] | 922 | if (Loc) *Loc = Exp->getOperatorLoc(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 923 | return false; |
Chris Lattner | 6538347 | 2007-12-18 07:15:40 +0000 | [diff] [blame] | 924 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 925 | |
Chris Lattner | 76e773a | 2007-07-18 18:38:36 +0000 | [diff] [blame] | 926 | // Get information about the size or align. |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 927 | if (ArgTy->isFunctionType()) { |
Chris Lattner | efdd157 | 2008-01-02 21:54:09 +0000 | [diff] [blame] | 928 | // GCC extension: sizeof(function) = 1. |
| 929 | Result = Exp->isSizeOf() ? 1 : 4; |
Anders Carlsson | 6a24acb | 2008-02-16 01:20:23 +0000 | [diff] [blame] | 930 | } else { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 931 | unsigned CharSize = Ctx.Target.getCharWidth(); |
Anders Carlsson | 6a24acb | 2008-02-16 01:20:23 +0000 | [diff] [blame] | 932 | if (Exp->isSizeOf()) |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 933 | Result = Ctx.getTypeSize(ArgTy) / CharSize; |
Anders Carlsson | 6a24acb | 2008-02-16 01:20:23 +0000 | [diff] [blame] | 934 | else |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 935 | Result = Ctx.getTypeAlign(ArgTy) / CharSize; |
Ted Kremenek | 060e470 | 2007-12-17 17:38:43 +0000 | [diff] [blame] | 936 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 937 | break; |
| 938 | } |
| 939 | case BinaryOperatorClass: { |
| 940 | const BinaryOperator *Exp = cast<BinaryOperator>(this); |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 941 | llvm::APSInt LHS, RHS; |
| 942 | |
| 943 | // Initialize result to have correct signedness and width. |
| 944 | Result = llvm::APSInt(static_cast<uint32_t>(Ctx.getTypeSize(getType())), |
Eli Friedman | b11e778 | 2008-11-13 02:13:11 +0000 | [diff] [blame] | 945 | !getType()->isSignedIntegerType()); |
| 946 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 947 | // The LHS of a constant expr is always evaluated and needed. |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 948 | if (!Exp->getLHS()->isIntegerConstantExpr(LHS, Ctx, Loc, isEvaluated)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 949 | return false; |
| 950 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 951 | // The short-circuiting &&/|| operators don't necessarily evaluate their |
| 952 | // RHS. Make sure to pass isEvaluated down correctly. |
| 953 | if (Exp->isLogicalOp()) { |
| 954 | bool RHSEval; |
| 955 | if (Exp->getOpcode() == BinaryOperator::LAnd) |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 956 | RHSEval = LHS != 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 957 | else { |
| 958 | assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical"); |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 959 | RHSEval = LHS == 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 960 | } |
| 961 | |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 962 | if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 963 | isEvaluated & RHSEval)) |
| 964 | return false; |
| 965 | } else { |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 966 | if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 967 | return false; |
| 968 | } |
| 969 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 970 | switch (Exp->getOpcode()) { |
| 971 | default: |
| 972 | if (Loc) *Loc = getLocStart(); |
| 973 | return false; |
| 974 | case BinaryOperator::Mul: |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 975 | Result = LHS * RHS; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 976 | break; |
| 977 | case BinaryOperator::Div: |
| 978 | if (RHS == 0) { |
| 979 | if (!isEvaluated) break; |
| 980 | if (Loc) *Loc = getLocStart(); |
| 981 | return false; |
| 982 | } |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 983 | Result = LHS / RHS; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 984 | break; |
| 985 | case BinaryOperator::Rem: |
| 986 | if (RHS == 0) { |
| 987 | if (!isEvaluated) break; |
| 988 | if (Loc) *Loc = getLocStart(); |
| 989 | return false; |
| 990 | } |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 991 | Result = LHS % RHS; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 992 | break; |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 993 | case BinaryOperator::Add: Result = LHS + RHS; break; |
| 994 | case BinaryOperator::Sub: Result = LHS - RHS; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 995 | case BinaryOperator::Shl: |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 996 | Result = LHS << |
| 997 | static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1)); |
| 998 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 999 | case BinaryOperator::Shr: |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 1000 | Result = LHS >> |
| 1001 | static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1002 | break; |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 1003 | case BinaryOperator::LT: Result = LHS < RHS; break; |
| 1004 | case BinaryOperator::GT: Result = LHS > RHS; break; |
| 1005 | case BinaryOperator::LE: Result = LHS <= RHS; break; |
| 1006 | case BinaryOperator::GE: Result = LHS >= RHS; break; |
| 1007 | case BinaryOperator::EQ: Result = LHS == RHS; break; |
| 1008 | case BinaryOperator::NE: Result = LHS != RHS; break; |
| 1009 | case BinaryOperator::And: Result = LHS & RHS; break; |
| 1010 | case BinaryOperator::Xor: Result = LHS ^ RHS; break; |
| 1011 | case BinaryOperator::Or: Result = LHS | RHS; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1012 | case BinaryOperator::LAnd: |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 1013 | Result = LHS != 0 && RHS != 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1014 | break; |
| 1015 | case BinaryOperator::LOr: |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 1016 | Result = LHS != 0 || RHS != 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1017 | break; |
Eli Friedman | b11e778 | 2008-11-13 02:13:11 +0000 | [diff] [blame] | 1018 | |
| 1019 | case BinaryOperator::Comma: |
| 1020 | // C99 6.6p3: "shall not contain assignment, ..., or comma operators, |
| 1021 | // *except* when they are contained within a subexpression that is not |
| 1022 | // evaluated". Note that Assignment can never happen due to constraints |
| 1023 | // on the LHS subexpr, so we don't need to check it here. |
| 1024 | if (isEvaluated) { |
| 1025 | if (Loc) *Loc = getLocStart(); |
| 1026 | return false; |
| 1027 | } |
| 1028 | |
| 1029 | // The result of the constant expr is the RHS. |
| 1030 | Result = RHS; |
| 1031 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!"); |
| 1035 | break; |
| 1036 | } |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 1037 | case ImplicitCastExprClass: |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 1038 | case CStyleCastExprClass: |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1039 | case CXXFunctionalCastExprClass: { |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 1040 | const Expr *SubExpr = cast<CastExpr>(this)->getSubExpr(); |
| 1041 | SourceLocation CastLoc = getLocStart(); |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 1042 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1043 | // C99 6.6p6: shall only convert arithmetic types to integer types. |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 1044 | if (!SubExpr->getType()->isArithmeticType() || |
| 1045 | !getType()->isIntegerType()) { |
| 1046 | if (Loc) *Loc = SubExpr->getLocStart(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1047 | return false; |
| 1048 | } |
Chris Lattner | 987b15d | 2007-09-22 19:04:13 +0000 | [diff] [blame] | 1049 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1050 | uint32_t DestWidth = static_cast<uint32_t>(Ctx.getTypeSize(getType())); |
Chris Lattner | 987b15d | 2007-09-22 19:04:13 +0000 | [diff] [blame] | 1051 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1052 | // Handle simple integer->integer casts. |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 1053 | if (SubExpr->getType()->isIntegerType()) { |
| 1054 | if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1055 | return false; |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 1056 | |
| 1057 | // Figure out if this is a truncate, extend or noop cast. |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 1058 | // If the input is signed, do a sign extend, noop, or truncate. |
Chris Lattner | c0a356b | 2008-01-09 18:59:34 +0000 | [diff] [blame] | 1059 | if (getType()->isBooleanType()) { |
| 1060 | // Conversion to bool compares against zero. |
| 1061 | Result = Result != 0; |
| 1062 | Result.zextOrTrunc(DestWidth); |
| 1063 | } else if (SubExpr->getType()->isSignedIntegerType()) |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 1064 | Result.sextOrTrunc(DestWidth); |
| 1065 | else // If the input is unsigned, do a zero extend, noop, or truncate. |
| 1066 | Result.zextOrTrunc(DestWidth); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1067 | break; |
| 1068 | } |
| 1069 | |
| 1070 | // Allow floating constants that are the immediate operands of casts or that |
| 1071 | // are parenthesized. |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 1072 | const Expr *Operand = SubExpr; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1073 | while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand)) |
| 1074 | Operand = PE->getSubExpr(); |
Chris Lattner | 987b15d | 2007-09-22 19:04:13 +0000 | [diff] [blame] | 1075 | |
| 1076 | // If this isn't a floating literal, we can't handle it. |
| 1077 | const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand); |
| 1078 | if (!FL) { |
| 1079 | if (Loc) *Loc = Operand->getLocStart(); |
| 1080 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1081 | } |
Chris Lattner | c0a356b | 2008-01-09 18:59:34 +0000 | [diff] [blame] | 1082 | |
| 1083 | // If the destination is boolean, compare against zero. |
| 1084 | if (getType()->isBooleanType()) { |
| 1085 | Result = !FL->getValue().isZero(); |
| 1086 | Result.zextOrTrunc(DestWidth); |
| 1087 | break; |
| 1088 | } |
Chris Lattner | 987b15d | 2007-09-22 19:04:13 +0000 | [diff] [blame] | 1089 | |
| 1090 | // Determine whether we are converting to unsigned or signed. |
| 1091 | bool DestSigned = getType()->isSignedIntegerType(); |
Chris Lattner | ccc213f | 2007-09-26 00:47:26 +0000 | [diff] [blame] | 1092 | |
| 1093 | // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can |
| 1094 | // be called multiple times per AST. |
Dale Johannesen | ee5a700 | 2008-10-09 23:02:32 +0000 | [diff] [blame] | 1095 | uint64_t Space[4]; |
| 1096 | bool ignored; |
Chris Lattner | ccc213f | 2007-09-26 00:47:26 +0000 | [diff] [blame] | 1097 | (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned, |
Dale Johannesen | ee5a700 | 2008-10-09 23:02:32 +0000 | [diff] [blame] | 1098 | llvm::APFloat::rmTowardZero, |
| 1099 | &ignored); |
Chris Lattner | 987b15d | 2007-09-22 19:04:13 +0000 | [diff] [blame] | 1100 | Result = llvm::APInt(DestWidth, 4, Space); |
| 1101 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1102 | } |
| 1103 | case ConditionalOperatorClass: { |
| 1104 | const ConditionalOperator *Exp = cast<ConditionalOperator>(this); |
| 1105 | |
Chris Lattner | 28daa53 | 2008-12-12 06:55:44 +0000 | [diff] [blame] | 1106 | const Expr *Cond = Exp->getCond(); |
| 1107 | |
| 1108 | if (!Cond->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1109 | return false; |
| 1110 | |
| 1111 | const Expr *TrueExp = Exp->getLHS(); |
| 1112 | const Expr *FalseExp = Exp->getRHS(); |
| 1113 | if (Result == 0) std::swap(TrueExp, FalseExp); |
| 1114 | |
Chris Lattner | 28daa53 | 2008-12-12 06:55:44 +0000 | [diff] [blame] | 1115 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
| 1116 | // then only the true side is actually considered in an integer constant |
Chris Lattner | 42b83dd | 2008-12-12 18:00:51 +0000 | [diff] [blame] | 1117 | // expression, and it is fully evaluated. This is an important GNU |
| 1118 | // extension. See GCC PR38377 for discussion. |
Chris Lattner | 28daa53 | 2008-12-12 06:55:44 +0000 | [diff] [blame] | 1119 | if (const CallExpr *CallCE = dyn_cast<CallExpr>(Cond->IgnoreParenCasts())) |
Chris Lattner | 42b83dd | 2008-12-12 18:00:51 +0000 | [diff] [blame] | 1120 | if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p) { |
| 1121 | EvalResult EVResult; |
| 1122 | if (!Evaluate(EVResult, Ctx) || EVResult.HasSideEffects) |
| 1123 | return false; |
| 1124 | assert(EVResult.Val.isInt() && "FP conditional expr not expected"); |
| 1125 | Result = EVResult.Val.getInt(); |
| 1126 | if (Loc) *Loc = EVResult.DiagLoc; |
| 1127 | return true; |
| 1128 | } |
Chris Lattner | 28daa53 | 2008-12-12 06:55:44 +0000 | [diff] [blame] | 1129 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1130 | // Evaluate the false one first, discard the result. |
Anders Carlsson | 3907323 | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 1131 | if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1132 | return false; |
| 1133 | // Evalute the true one, capture the result. |
Anders Carlsson | 3907323 | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 1134 | if (TrueExp && |
| 1135 | !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1136 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1137 | break; |
| 1138 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1139 | case CXXDefaultArgExprClass: |
| 1140 | return cast<CXXDefaultArgExpr>(this) |
| 1141 | ->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1142 | |
| 1143 | case UnaryTypeTraitExprClass: |
| 1144 | Result = cast<UnaryTypeTraitExpr>(this)->Evaluate(); |
| 1145 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | // Cases that are valid constant exprs fall through to here. |
| 1149 | Result.setIsUnsigned(getType()->isUnsignedIntegerType()); |
| 1150 | return true; |
| 1151 | } |
| 1152 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1153 | /// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an |
| 1154 | /// integer constant expression with the value zero, or if this is one that is |
| 1155 | /// cast to void*. |
Anders Carlsson | efa9b38 | 2008-12-01 02:13:57 +0000 | [diff] [blame] | 1156 | bool Expr::isNullPointerConstant(ASTContext &Ctx) const |
| 1157 | { |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1158 | // Strip off a cast to void*, if it exists. Except in C++. |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 1159 | if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) { |
Sebastian Redl | 6215dee | 2008-11-04 11:45:54 +0000 | [diff] [blame] | 1160 | if (!Ctx.getLangOptions().CPlusPlus) { |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1161 | // Check that it is a cast to void*. |
| 1162 | if (const PointerType *PT = CE->getType()->getAsPointerType()) { |
| 1163 | QualType Pointee = PT->getPointeeType(); |
| 1164 | if (Pointee.getCVRQualifiers() == 0 && |
| 1165 | Pointee->isVoidType() && // to void* |
| 1166 | CE->getSubExpr()->getType()->isIntegerType()) // from int. |
Anders Carlsson | d265277 | 2008-12-01 06:28:23 +0000 | [diff] [blame] | 1167 | return CE->getSubExpr()->isNullPointerConstant(Ctx); |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1168 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1169 | } |
Steve Naroff | aa58f00 | 2008-01-14 16:10:57 +0000 | [diff] [blame] | 1170 | } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) { |
| 1171 | // Ignore the ImplicitCastExpr type entirely. |
Anders Carlsson | d265277 | 2008-12-01 06:28:23 +0000 | [diff] [blame] | 1172 | return ICE->getSubExpr()->isNullPointerConstant(Ctx); |
Steve Naroff | aa58f00 | 2008-01-14 16:10:57 +0000 | [diff] [blame] | 1173 | } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) { |
| 1174 | // Accept ((void*)0) as a null pointer constant, as many other |
| 1175 | // implementations do. |
Anders Carlsson | d265277 | 2008-12-01 06:28:23 +0000 | [diff] [blame] | 1176 | return PE->getSubExpr()->isNullPointerConstant(Ctx); |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1177 | } else if (const CXXDefaultArgExpr *DefaultArg |
| 1178 | = dyn_cast<CXXDefaultArgExpr>(this)) { |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1179 | // See through default argument expressions |
Anders Carlsson | d265277 | 2008-12-01 06:28:23 +0000 | [diff] [blame] | 1180 | return DefaultArg->getExpr()->isNullPointerConstant(Ctx); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 1181 | } else if (isa<GNUNullExpr>(this)) { |
| 1182 | // The GNU __null extension is always a null pointer constant. |
| 1183 | return true; |
Steve Naroff | aaffbf7 | 2008-01-14 02:53:34 +0000 | [diff] [blame] | 1184 | } |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 1185 | |
Steve Naroff | aa58f00 | 2008-01-14 16:10:57 +0000 | [diff] [blame] | 1186 | // This expression must be an integer type. |
| 1187 | if (!getType()->isIntegerType()) |
| 1188 | return false; |
| 1189 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1190 | // If we have an integer constant expression, we need to *evaluate* it and |
| 1191 | // test for the value 0. |
Anders Carlsson | d265277 | 2008-12-01 06:28:23 +0000 | [diff] [blame] | 1192 | // FIXME: We should probably return false if we're compiling in strict mode |
| 1193 | // and Diag is not null (this indicates that the value was foldable but not |
| 1194 | // an ICE. |
| 1195 | EvalResult Result; |
Anders Carlsson | efa9b38 | 2008-12-01 02:13:57 +0000 | [diff] [blame] | 1196 | return Evaluate(Result, Ctx) && !Result.HasSideEffects && |
Anders Carlsson | d265277 | 2008-12-01 06:28:23 +0000 | [diff] [blame] | 1197 | Result.Val.isInt() && Result.Val.getInt() == 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1198 | } |
Steve Naroff | 31a4584 | 2007-07-28 23:10:27 +0000 | [diff] [blame] | 1199 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1200 | /// isBitField - Return true if this expression is a bit-field. |
| 1201 | bool Expr::isBitField() { |
| 1202 | Expr *E = this->IgnoreParenCasts(); |
| 1203 | if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E)) |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1204 | if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl())) |
| 1205 | return Field->isBitField(); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1206 | return false; |
| 1207 | } |
| 1208 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1209 | unsigned ExtVectorElementExpr::getNumElements() const { |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1210 | if (const VectorType *VT = getType()->getAsVectorType()) |
| 1211 | return VT->getNumElements(); |
| 1212 | return 1; |
Chris Lattner | 4d0ac88 | 2007-08-03 16:00:20 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1215 | /// containsDuplicateElements - Return true if any element access is repeated. |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1216 | bool ExtVectorElementExpr::containsDuplicateElements() const { |
Steve Naroff | fec0b49 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 1217 | const char *compStr = Accessor.getName(); |
Chris Lattner | 7e3e9b1 | 2008-11-19 07:55:04 +0000 | [diff] [blame] | 1218 | unsigned length = Accessor.getLength(); |
Nate Begeman | 190d6a2 | 2009-01-18 02:01:21 +0000 | [diff] [blame] | 1219 | |
| 1220 | // Halving swizzles do not contain duplicate elements. |
| 1221 | if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || |
| 1222 | !strcmp(compStr, "even") || !strcmp(compStr, "odd")) |
| 1223 | return false; |
| 1224 | |
| 1225 | // Advance past s-char prefix on hex swizzles. |
| 1226 | if (*compStr == 's') { |
| 1227 | compStr++; |
| 1228 | length--; |
| 1229 | } |
Steve Naroff | fec0b49 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 1230 | |
Chris Lattner | 7e3e9b1 | 2008-11-19 07:55:04 +0000 | [diff] [blame] | 1231 | for (unsigned i = 0; i != length-1; i++) { |
Steve Naroff | fec0b49 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 1232 | const char *s = compStr+i; |
| 1233 | for (const char c = *s++; *s; s++) |
| 1234 | if (c == *s) |
| 1235 | return true; |
| 1236 | } |
| 1237 | return false; |
| 1238 | } |
Chris Lattner | b8f849d | 2007-08-02 23:36:59 +0000 | [diff] [blame] | 1239 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1240 | /// getEncodedElementAccess - We encode the fields as a llvm ConstantArray. |
Nate Begeman | 3b8d116 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 1241 | void ExtVectorElementExpr::getEncodedElementAccess( |
| 1242 | llvm::SmallVectorImpl<unsigned> &Elts) const { |
Chris Lattner | 7e3e9b1 | 2008-11-19 07:55:04 +0000 | [diff] [blame] | 1243 | const char *compStr = Accessor.getName(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1244 | if (*compStr == 's') |
| 1245 | compStr++; |
| 1246 | |
| 1247 | bool isHi = !strcmp(compStr, "hi"); |
| 1248 | bool isLo = !strcmp(compStr, "lo"); |
| 1249 | bool isEven = !strcmp(compStr, "even"); |
| 1250 | bool isOdd = !strcmp(compStr, "odd"); |
| 1251 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1252 | for (unsigned i = 0, e = getNumElements(); i != e; ++i) { |
| 1253 | uint64_t Index; |
| 1254 | |
| 1255 | if (isHi) |
| 1256 | Index = e + i; |
| 1257 | else if (isLo) |
| 1258 | Index = i; |
| 1259 | else if (isEven) |
| 1260 | Index = 2 * i; |
| 1261 | else if (isOdd) |
| 1262 | Index = 2 * i + 1; |
| 1263 | else |
| 1264 | Index = ExtVectorType::getAccessorIdx(compStr[i]); |
Chris Lattner | b8f849d | 2007-08-02 23:36:59 +0000 | [diff] [blame] | 1265 | |
Nate Begeman | 3b8d116 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 1266 | Elts.push_back(Index); |
Chris Lattner | b8f849d | 2007-08-02 23:36:59 +0000 | [diff] [blame] | 1267 | } |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1270 | // constructor for instance messages. |
Steve Naroff | bcfb06a | 2007-09-28 22:22:11 +0000 | [diff] [blame] | 1271 | ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo, |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1272 | QualType retType, ObjCMethodDecl *mproto, |
Steve Naroff | db611d5 | 2007-11-03 16:37:59 +0000 | [diff] [blame] | 1273 | SourceLocation LBrac, SourceLocation RBrac, |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1274 | Expr **ArgExprs, unsigned nargs) |
Steve Naroff | db611d5 | 2007-11-03 16:37:59 +0000 | [diff] [blame] | 1275 | : Expr(ObjCMessageExprClass, retType), SelName(selInfo), |
Ted Kremenek | ea958e57 | 2008-05-01 17:26:20 +0000 | [diff] [blame] | 1276 | MethodProto(mproto) { |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1277 | NumArgs = nargs; |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1278 | SubExprs = new Stmt*[NumArgs+1]; |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1279 | SubExprs[RECEIVER] = receiver; |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1280 | if (NumArgs) { |
| 1281 | for (unsigned i = 0; i != NumArgs; ++i) |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1282 | SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]); |
| 1283 | } |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 1284 | LBracloc = LBrac; |
| 1285 | RBracloc = RBrac; |
| 1286 | } |
| 1287 | |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1288 | // constructor for class messages. |
| 1289 | // FIXME: clsName should be typed to ObjCInterfaceType |
Steve Naroff | bcfb06a | 2007-09-28 22:22:11 +0000 | [diff] [blame] | 1290 | ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo, |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1291 | QualType retType, ObjCMethodDecl *mproto, |
Steve Naroff | db611d5 | 2007-11-03 16:37:59 +0000 | [diff] [blame] | 1292 | SourceLocation LBrac, SourceLocation RBrac, |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1293 | Expr **ArgExprs, unsigned nargs) |
Steve Naroff | db611d5 | 2007-11-03 16:37:59 +0000 | [diff] [blame] | 1294 | : Expr(ObjCMessageExprClass, retType), SelName(selInfo), |
Ted Kremenek | ea958e57 | 2008-05-01 17:26:20 +0000 | [diff] [blame] | 1295 | MethodProto(mproto) { |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1296 | NumArgs = nargs; |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1297 | SubExprs = new Stmt*[NumArgs+1]; |
Ted Kremenek | 4df728e | 2008-06-24 15:50:53 +0000 | [diff] [blame] | 1298 | SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown); |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1299 | if (NumArgs) { |
| 1300 | for (unsigned i = 0; i != NumArgs; ++i) |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1301 | SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]); |
| 1302 | } |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 1303 | LBracloc = LBrac; |
| 1304 | RBracloc = RBrac; |
| 1305 | } |
| 1306 | |
Ted Kremenek | 4df728e | 2008-06-24 15:50:53 +0000 | [diff] [blame] | 1307 | // constructor for class messages. |
| 1308 | ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo, |
| 1309 | QualType retType, ObjCMethodDecl *mproto, |
| 1310 | SourceLocation LBrac, SourceLocation RBrac, |
| 1311 | Expr **ArgExprs, unsigned nargs) |
| 1312 | : Expr(ObjCMessageExprClass, retType), SelName(selInfo), |
| 1313 | MethodProto(mproto) { |
| 1314 | NumArgs = nargs; |
| 1315 | SubExprs = new Stmt*[NumArgs+1]; |
| 1316 | SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown); |
| 1317 | if (NumArgs) { |
| 1318 | for (unsigned i = 0; i != NumArgs; ++i) |
| 1319 | SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]); |
| 1320 | } |
| 1321 | LBracloc = LBrac; |
| 1322 | RBracloc = RBrac; |
| 1323 | } |
| 1324 | |
| 1325 | ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const { |
| 1326 | uintptr_t x = (uintptr_t) SubExprs[RECEIVER]; |
| 1327 | switch (x & Flags) { |
| 1328 | default: |
| 1329 | assert(false && "Invalid ObjCMessageExpr."); |
| 1330 | case IsInstMeth: |
| 1331 | return ClassInfo(0, 0); |
| 1332 | case IsClsMethDeclUnknown: |
| 1333 | return ClassInfo(0, (IdentifierInfo*) (x & ~Flags)); |
| 1334 | case IsClsMethDeclKnown: { |
| 1335 | ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags); |
| 1336 | return ClassInfo(D, D->getIdentifier()); |
| 1337 | } |
| 1338 | } |
| 1339 | } |
| 1340 | |
Chris Lattner | 27437ca | 2007-10-25 00:29:32 +0000 | [diff] [blame] | 1341 | bool ChooseExpr::isConditionTrue(ASTContext &C) const { |
Daniel Dunbar | 32442bb | 2008-08-13 23:47:13 +0000 | [diff] [blame] | 1342 | return getCond()->getIntegerConstantExprValue(C) != 0; |
Chris Lattner | 27437ca | 2007-10-25 00:29:32 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
Chris Lattner | 670a62c | 2008-12-12 05:35:08 +0000 | [diff] [blame] | 1345 | static int64_t evaluateOffsetOf(ASTContext& C, const Expr *E) { |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 1346 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) { |
| 1347 | QualType Ty = ME->getBase()->getType(); |
| 1348 | |
| 1349 | RecordDecl *RD = Ty->getAsRecordType()->getDecl(); |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1350 | const ASTRecordLayout &RL = C.getASTRecordLayout(RD); |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1351 | if (FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) { |
| 1352 | // FIXME: This is linear time. And the fact that we're indexing |
| 1353 | // into the layout by position in the record means that we're |
| 1354 | // either stuck numbering the fields in the AST or we have to keep |
| 1355 | // the linear search (yuck and yuck). |
| 1356 | unsigned i = 0; |
| 1357 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 1358 | FieldEnd = RD->field_end(); |
| 1359 | Field != FieldEnd; (void)++Field, ++i) { |
| 1360 | if (*Field == FD) |
| 1361 | break; |
| 1362 | } |
| 1363 | |
| 1364 | return RL.getFieldOffset(i) + evaluateOffsetOf(C, ME->getBase()); |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 1365 | } |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 1366 | } else if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) { |
| 1367 | const Expr *Base = ASE->getBase(); |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 1368 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1369 | int64_t size = C.getTypeSize(ASE->getType()); |
Daniel Dunbar | 32442bb | 2008-08-13 23:47:13 +0000 | [diff] [blame] | 1370 | size *= ASE->getIdx()->getIntegerConstantExprValue(C).getSExtValue(); |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 1371 | |
| 1372 | return size + evaluateOffsetOf(C, Base); |
| 1373 | } else if (isa<CompoundLiteralExpr>(E)) |
| 1374 | return 0; |
| 1375 | |
| 1376 | assert(0 && "Unknown offsetof subexpression!"); |
| 1377 | return 0; |
| 1378 | } |
| 1379 | |
| 1380 | int64_t UnaryOperator::evaluateOffsetOf(ASTContext& C) const |
| 1381 | { |
| 1382 | assert(Opc == OffsetOf && "Unary operator not offsetof!"); |
| 1383 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1384 | unsigned CharSize = C.Target.getCharWidth(); |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1385 | return ::evaluateOffsetOf(C, cast<Expr>(Val)) / CharSize; |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1388 | void SizeOfAlignOfExpr::Destroy(ASTContext& C) { |
| 1389 | // Override default behavior of traversing children. If this has a type |
| 1390 | // operand and the type is a variable-length array, the child iteration |
| 1391 | // will iterate over the size expression. However, this expression belongs |
| 1392 | // to the type, not to this, so we don't want to delete it. |
| 1393 | // We still want to delete this expression. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame^] | 1394 | if (isArgumentType()) { |
| 1395 | this->~SizeOfAlignOfExpr(); |
| 1396 | C.Deallocate(this); |
| 1397 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1398 | else |
| 1399 | Expr::Destroy(C); |
Daniel Dunbar | 9048891 | 2008-08-28 18:02:04 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1402 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1403 | // DesignatedInitExpr |
| 1404 | //===----------------------------------------------------------------------===// |
| 1405 | |
| 1406 | IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() { |
| 1407 | assert(Kind == FieldDesignator && "Only valid on a field designator"); |
| 1408 | if (Field.NameOrField & 0x01) |
| 1409 | return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01); |
| 1410 | else |
| 1411 | return getField()->getIdentifier(); |
| 1412 | } |
| 1413 | |
| 1414 | DesignatedInitExpr * |
| 1415 | DesignatedInitExpr::Create(ASTContext &C, Designator *Designators, |
| 1416 | unsigned NumDesignators, |
| 1417 | Expr **IndexExprs, unsigned NumIndexExprs, |
| 1418 | SourceLocation ColonOrEqualLoc, |
| 1419 | bool UsesColonSyntax, Expr *Init) { |
Steve Naroff | c0ac492 | 2009-01-27 23:20:32 +0000 | [diff] [blame] | 1420 | void *Mem = C.Allocate(sizeof(DesignatedInitExpr) + |
| 1421 | sizeof(Designator) * NumDesignators + |
| 1422 | sizeof(Stmt *) * (NumIndexExprs + 1), 8); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1423 | DesignatedInitExpr *DIE |
| 1424 | = new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators, |
| 1425 | ColonOrEqualLoc, UsesColonSyntax, |
| 1426 | NumIndexExprs + 1); |
| 1427 | |
| 1428 | // Fill in the designators |
| 1429 | unsigned ExpectedNumSubExprs = 0; |
| 1430 | designators_iterator Desig = DIE->designators_begin(); |
| 1431 | for (unsigned Idx = 0; Idx < NumDesignators; ++Idx, ++Desig) { |
| 1432 | new (static_cast<void*>(Desig)) Designator(Designators[Idx]); |
| 1433 | if (Designators[Idx].isArrayDesignator()) |
| 1434 | ++ExpectedNumSubExprs; |
| 1435 | else if (Designators[Idx].isArrayRangeDesignator()) |
| 1436 | ExpectedNumSubExprs += 2; |
| 1437 | } |
| 1438 | assert(ExpectedNumSubExprs == NumIndexExprs && "Wrong number of indices!"); |
| 1439 | |
| 1440 | // Fill in the subexpressions, including the initializer expression. |
| 1441 | child_iterator Child = DIE->child_begin(); |
| 1442 | *Child++ = Init; |
| 1443 | for (unsigned Idx = 0; Idx < NumIndexExprs; ++Idx, ++Child) |
| 1444 | *Child = IndexExprs[Idx]; |
| 1445 | |
| 1446 | return DIE; |
| 1447 | } |
| 1448 | |
| 1449 | SourceRange DesignatedInitExpr::getSourceRange() const { |
| 1450 | SourceLocation StartLoc; |
| 1451 | Designator &First = *const_cast<DesignatedInitExpr*>(this)->designators_begin(); |
| 1452 | if (First.isFieldDesignator()) { |
| 1453 | if (UsesColonSyntax) |
| 1454 | StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc); |
| 1455 | else |
| 1456 | StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc); |
| 1457 | } else |
| 1458 | StartLoc = SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc); |
| 1459 | return SourceRange(StartLoc, getInit()->getSourceRange().getEnd()); |
| 1460 | } |
| 1461 | |
| 1462 | DesignatedInitExpr::designators_iterator DesignatedInitExpr::designators_begin() { |
| 1463 | char* Ptr = static_cast<char*>(static_cast<void *>(this)); |
| 1464 | Ptr += sizeof(DesignatedInitExpr); |
| 1465 | return static_cast<Designator*>(static_cast<void*>(Ptr)); |
| 1466 | } |
| 1467 | |
| 1468 | DesignatedInitExpr::designators_iterator DesignatedInitExpr::designators_end() { |
| 1469 | return designators_begin() + NumDesignators; |
| 1470 | } |
| 1471 | |
| 1472 | Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) { |
| 1473 | assert(D.Kind == Designator::ArrayDesignator && "Requires array designator"); |
| 1474 | char* Ptr = static_cast<char*>(static_cast<void *>(this)); |
| 1475 | Ptr += sizeof(DesignatedInitExpr); |
| 1476 | Ptr += sizeof(Designator) * NumDesignators; |
| 1477 | Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); |
| 1478 | return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1)); |
| 1479 | } |
| 1480 | |
| 1481 | Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) { |
| 1482 | assert(D.Kind == Designator::ArrayRangeDesignator && |
| 1483 | "Requires array range designator"); |
| 1484 | char* Ptr = static_cast<char*>(static_cast<void *>(this)); |
| 1485 | Ptr += sizeof(DesignatedInitExpr); |
| 1486 | Ptr += sizeof(Designator) * NumDesignators; |
| 1487 | Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); |
| 1488 | return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1)); |
| 1489 | } |
| 1490 | |
| 1491 | Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) { |
| 1492 | assert(D.Kind == Designator::ArrayRangeDesignator && |
| 1493 | "Requires array range designator"); |
| 1494 | char* Ptr = static_cast<char*>(static_cast<void *>(this)); |
| 1495 | Ptr += sizeof(DesignatedInitExpr); |
| 1496 | Ptr += sizeof(Designator) * NumDesignators; |
| 1497 | Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); |
| 1498 | return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2)); |
| 1499 | } |
| 1500 | |
| 1501 | //===----------------------------------------------------------------------===// |
Ted Kremenek | ce2fc3a | 2008-10-27 18:40:21 +0000 | [diff] [blame] | 1502 | // ExprIterator. |
| 1503 | //===----------------------------------------------------------------------===// |
| 1504 | |
| 1505 | Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); } |
| 1506 | Expr* ExprIterator::operator*() const { return cast<Expr>(*I); } |
| 1507 | Expr* ExprIterator::operator->() const { return cast<Expr>(*I); } |
| 1508 | const Expr* ConstExprIterator::operator[](size_t idx) const { |
| 1509 | return cast<Expr>(I[idx]); |
| 1510 | } |
| 1511 | const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); } |
| 1512 | const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); } |
| 1513 | |
| 1514 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1515 | // Child Iterators for iterating over subexpressions/substatements |
| 1516 | //===----------------------------------------------------------------------===// |
| 1517 | |
| 1518 | // DeclRefExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1519 | Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); } |
| 1520 | Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1521 | |
Steve Naroff | 7779db4 | 2007-11-12 14:29:37 +0000 | [diff] [blame] | 1522 | // ObjCIvarRefExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1523 | Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; } |
| 1524 | Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; } |
Steve Naroff | 7779db4 | 2007-11-12 14:29:37 +0000 | [diff] [blame] | 1525 | |
Steve Naroff | e3e9add | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 1526 | // ObjCPropertyRefExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1527 | Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; } |
| 1528 | Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; } |
Steve Naroff | ae78407 | 2008-05-30 00:40:33 +0000 | [diff] [blame] | 1529 | |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 1530 | // ObjCKVCRefExpr |
| 1531 | Stmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; } |
| 1532 | Stmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; } |
| 1533 | |
Douglas Gregor | cd9b46e | 2008-11-04 14:56:14 +0000 | [diff] [blame] | 1534 | // ObjCSuperExpr |
| 1535 | Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); } |
| 1536 | Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); } |
| 1537 | |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 1538 | // PredefinedExpr |
| 1539 | Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); } |
| 1540 | Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1541 | |
| 1542 | // IntegerLiteral |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1543 | Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); } |
| 1544 | Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1545 | |
| 1546 | // CharacterLiteral |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1547 | Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); } |
| 1548 | Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1549 | |
| 1550 | // FloatingLiteral |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1551 | Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); } |
| 1552 | Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1553 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1554 | // ImaginaryLiteral |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1555 | Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; } |
| 1556 | Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; } |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1557 | |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1558 | // StringLiteral |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1559 | Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); } |
| 1560 | Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1561 | |
| 1562 | // ParenExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1563 | Stmt::child_iterator ParenExpr::child_begin() { return &Val; } |
| 1564 | Stmt::child_iterator ParenExpr::child_end() { return &Val+1; } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1565 | |
| 1566 | // UnaryOperator |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1567 | Stmt::child_iterator UnaryOperator::child_begin() { return &Val; } |
| 1568 | Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1569 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1570 | // SizeOfAlignOfExpr |
| 1571 | Stmt::child_iterator SizeOfAlignOfExpr::child_begin() { |
| 1572 | // If this is of a type and the type is a VLA type (and not a typedef), the |
| 1573 | // size expression of the VLA needs to be treated as an executable expression. |
| 1574 | // Why isn't this weirdness documented better in StmtIterator? |
| 1575 | if (isArgumentType()) { |
| 1576 | if (VariableArrayType* T = dyn_cast<VariableArrayType>( |
| 1577 | getArgumentType().getTypePtr())) |
| 1578 | return child_iterator(T); |
| 1579 | return child_iterator(); |
| 1580 | } |
Sebastian Redl | d457589 | 2008-12-03 23:17:54 +0000 | [diff] [blame] | 1581 | return child_iterator(&Argument.Ex); |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1582 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1583 | Stmt::child_iterator SizeOfAlignOfExpr::child_end() { |
| 1584 | if (isArgumentType()) |
| 1585 | return child_iterator(); |
Sebastian Redl | d457589 | 2008-12-03 23:17:54 +0000 | [diff] [blame] | 1586 | return child_iterator(&Argument.Ex + 1); |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1587 | } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1588 | |
| 1589 | // ArraySubscriptExpr |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1590 | Stmt::child_iterator ArraySubscriptExpr::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1591 | return &SubExprs[0]; |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1592 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1593 | Stmt::child_iterator ArraySubscriptExpr::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1594 | return &SubExprs[0]+END_EXPR; |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
| 1597 | // CallExpr |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1598 | Stmt::child_iterator CallExpr::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1599 | return &SubExprs[0]; |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1600 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1601 | Stmt::child_iterator CallExpr::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1602 | return &SubExprs[0]+NumArgs+ARGS_START; |
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 | |
| 1605 | // MemberExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1606 | Stmt::child_iterator MemberExpr::child_begin() { return &Base; } |
| 1607 | Stmt::child_iterator MemberExpr::child_end() { return &Base+1; } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1608 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1609 | // ExtVectorElementExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1610 | Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; } |
| 1611 | Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1612 | |
| 1613 | // CompoundLiteralExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1614 | Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; } |
| 1615 | Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1616 | |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1617 | // CastExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1618 | Stmt::child_iterator CastExpr::child_begin() { return &Op; } |
| 1619 | Stmt::child_iterator CastExpr::child_end() { return &Op+1; } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1620 | |
| 1621 | // BinaryOperator |
| 1622 | Stmt::child_iterator BinaryOperator::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1623 | return &SubExprs[0]; |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1624 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1625 | Stmt::child_iterator BinaryOperator::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1626 | return &SubExprs[0]+END_EXPR; |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1627 | } |
| 1628 | |
| 1629 | // ConditionalOperator |
| 1630 | Stmt::child_iterator ConditionalOperator::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1631 | return &SubExprs[0]; |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1632 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1633 | Stmt::child_iterator ConditionalOperator::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1634 | return &SubExprs[0]+END_EXPR; |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
| 1637 | // AddrLabelExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1638 | Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); } |
| 1639 | Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1640 | |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1641 | // StmtExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1642 | Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; } |
| 1643 | Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1644 | |
| 1645 | // TypesCompatibleExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1646 | Stmt::child_iterator TypesCompatibleExpr::child_begin() { |
| 1647 | return child_iterator(); |
| 1648 | } |
| 1649 | |
| 1650 | Stmt::child_iterator TypesCompatibleExpr::child_end() { |
| 1651 | return child_iterator(); |
| 1652 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1653 | |
| 1654 | // ChooseExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1655 | Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; } |
| 1656 | Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1657 | |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 1658 | // GNUNullExpr |
| 1659 | Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); } |
| 1660 | Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); } |
| 1661 | |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 1662 | // OverloadExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1663 | Stmt::child_iterator OverloadExpr::child_begin() { return &SubExprs[0]; } |
| 1664 | Stmt::child_iterator OverloadExpr::child_end() { return &SubExprs[0]+NumExprs; } |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 1665 | |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1666 | // ShuffleVectorExpr |
| 1667 | Stmt::child_iterator ShuffleVectorExpr::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1668 | return &SubExprs[0]; |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1669 | } |
| 1670 | Stmt::child_iterator ShuffleVectorExpr::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1671 | return &SubExprs[0]+NumExprs; |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 1674 | // VAArgExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1675 | Stmt::child_iterator VAArgExpr::child_begin() { return &Val; } |
| 1676 | Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; } |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 1677 | |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 1678 | // InitListExpr |
| 1679 | Stmt::child_iterator InitListExpr::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1680 | return InitExprs.size() ? &InitExprs[0] : 0; |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 1681 | } |
| 1682 | Stmt::child_iterator InitListExpr::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1683 | return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0; |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 1684 | } |
| 1685 | |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1686 | // DesignatedInitExpr |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1687 | Stmt::child_iterator DesignatedInitExpr::child_begin() { |
| 1688 | char* Ptr = static_cast<char*>(static_cast<void *>(this)); |
| 1689 | Ptr += sizeof(DesignatedInitExpr); |
| 1690 | Ptr += sizeof(Designator) * NumDesignators; |
| 1691 | return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); |
| 1692 | } |
| 1693 | Stmt::child_iterator DesignatedInitExpr::child_end() { |
| 1694 | return child_iterator(&*child_begin() + NumSubExprs); |
| 1695 | } |
| 1696 | |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1697 | // ImplicitValueInitExpr |
| 1698 | Stmt::child_iterator ImplicitValueInitExpr::child_begin() { |
| 1699 | return child_iterator(); |
| 1700 | } |
| 1701 | |
| 1702 | Stmt::child_iterator ImplicitValueInitExpr::child_end() { |
| 1703 | return child_iterator(); |
| 1704 | } |
| 1705 | |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1706 | // ObjCStringLiteral |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1707 | Stmt::child_iterator ObjCStringLiteral::child_begin() { |
| 1708 | return child_iterator(); |
| 1709 | } |
| 1710 | Stmt::child_iterator ObjCStringLiteral::child_end() { |
| 1711 | return child_iterator(); |
| 1712 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1713 | |
| 1714 | // ObjCEncodeExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1715 | Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); } |
| 1716 | Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1717 | |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 1718 | // ObjCSelectorExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1719 | Stmt::child_iterator ObjCSelectorExpr::child_begin() { |
| 1720 | return child_iterator(); |
| 1721 | } |
| 1722 | Stmt::child_iterator ObjCSelectorExpr::child_end() { |
| 1723 | return child_iterator(); |
| 1724 | } |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 1725 | |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 1726 | // ObjCProtocolExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1727 | Stmt::child_iterator ObjCProtocolExpr::child_begin() { |
| 1728 | return child_iterator(); |
| 1729 | } |
| 1730 | Stmt::child_iterator ObjCProtocolExpr::child_end() { |
| 1731 | return child_iterator(); |
| 1732 | } |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 1733 | |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 1734 | // ObjCMessageExpr |
Ted Kremenek | ea958e57 | 2008-05-01 17:26:20 +0000 | [diff] [blame] | 1735 | Stmt::child_iterator ObjCMessageExpr::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1736 | return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START; |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 1737 | } |
| 1738 | Stmt::child_iterator ObjCMessageExpr::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1739 | return &SubExprs[0]+ARGS_START+getNumArgs(); |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 1742 | // Blocks |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 1743 | Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); } |
| 1744 | Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); } |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 1745 | |
Ted Kremenek | 9da13f9 | 2008-09-26 23:24:14 +0000 | [diff] [blame] | 1746 | Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();} |
| 1747 | Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); } |