Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 1 | //===--- Expr.cpp - Expression AST Node Implementation --------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Expr class and subclasses. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/Expr.h" |
Chris Lattner | 5e9a878 | 2006-11-04 06:21:51 +0000 | [diff] [blame] | 15 | #include "clang/AST/StmtVisitor.h" |
Chris Lattner | e165d94 | 2006-08-24 04:40:38 +0000 | [diff] [blame] | 16 | #include "clang/Lex/IdentifierTable.h" |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 17 | using namespace clang; |
| 18 | |
Chris Lattner | 0eedafe | 2006-08-24 04:56:27 +0000 | [diff] [blame] | 19 | //===----------------------------------------------------------------------===// |
| 20 | // Primary Expressions. |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | |
Steve Naroff | 408451b | 2007-02-26 22:17:12 +0000 | [diff] [blame] | 23 | StringLiteral::StringLiteral(const char *strData, unsigned byteLength, |
Steve Naroff | 53f07dc | 2007-05-17 21:49:33 +0000 | [diff] [blame] | 24 | bool Wide, QualType t, SourceLocation firstLoc, |
| 25 | SourceLocation lastLoc) : |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 26 | Expr(StringLiteralClass, t) { |
Steve Naroff | df7855b | 2007-02-21 23:46:25 +0000 | [diff] [blame] | 27 | // OPTIMIZE: could allocate this appended to the StringLiteral. |
Chris Lattner | d3e9895 | 2006-10-06 05:22:26 +0000 | [diff] [blame] | 28 | char *AStrData = new char[byteLength]; |
| 29 | memcpy(AStrData, strData, byteLength); |
| 30 | StrData = AStrData; |
| 31 | ByteLength = byteLength; |
Chris Lattner | 882f788 | 2006-11-04 18:52:07 +0000 | [diff] [blame] | 32 | IsWide = Wide; |
Steve Naroff | 53f07dc | 2007-05-17 21:49:33 +0000 | [diff] [blame] | 33 | firstTokLoc = firstLoc; |
| 34 | lastTokLoc = lastLoc; |
Chris Lattner | d3e9895 | 2006-10-06 05:22:26 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Steve Naroff | df7855b | 2007-02-21 23:46:25 +0000 | [diff] [blame] | 37 | StringLiteral::~StringLiteral() { |
Chris Lattner | d3e9895 | 2006-10-06 05:22:26 +0000 | [diff] [blame] | 38 | delete[] StrData; |
| 39 | } |
| 40 | |
Chris Lattner | 1576870 | 2006-11-05 23:54:51 +0000 | [diff] [blame] | 41 | bool UnaryOperator::isPostfix(Opcode Op) { |
| 42 | switch (Op) { |
| 43 | case PostInc: |
| 44 | case PostDec: |
| 45 | return true; |
| 46 | default: |
| 47 | return false; |
| 48 | } |
| 49 | } |
| 50 | |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 51 | /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it |
| 52 | /// corresponds to, e.g. "sizeof" or "[pre]++". |
| 53 | const char *UnaryOperator::getOpcodeStr(Opcode Op) { |
| 54 | switch (Op) { |
Chris Lattner | c52b118 | 2006-10-25 05:45:55 +0000 | [diff] [blame] | 55 | default: assert(0 && "Unknown unary operator"); |
Chris Lattner | 1576870 | 2006-11-05 23:54:51 +0000 | [diff] [blame] | 56 | case PostInc: return "++"; |
| 57 | case PostDec: return "--"; |
| 58 | case PreInc: return "++"; |
| 59 | case PreDec: return "--"; |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 60 | case AddrOf: return "&"; |
| 61 | case Deref: return "*"; |
| 62 | case Plus: return "+"; |
| 63 | case Minus: return "-"; |
| 64 | case Not: return "~"; |
| 65 | case LNot: return "!"; |
| 66 | case Real: return "__real"; |
| 67 | case Imag: return "__imag"; |
Chris Lattner | 26115ac | 2006-08-24 06:10:04 +0000 | [diff] [blame] | 68 | case SizeOf: return "sizeof"; |
| 69 | case AlignOf: return "alignof"; |
Chris Lattner | c52b118 | 2006-10-25 05:45:55 +0000 | [diff] [blame] | 70 | case Extension: return "__extension__"; |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
Chris Lattner | 0eedafe | 2006-08-24 04:56:27 +0000 | [diff] [blame] | 74 | //===----------------------------------------------------------------------===// |
| 75 | // Postfix Operators. |
| 76 | //===----------------------------------------------------------------------===// |
Chris Lattner | e165d94 | 2006-08-24 04:40:38 +0000 | [diff] [blame] | 77 | |
Steve Naroff | 509fe02 | 2007-05-17 01:16:00 +0000 | [diff] [blame] | 78 | CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t, |
Chris Lattner | 9b3b9a1 | 2007-06-27 06:08:24 +0000 | [diff] [blame] | 79 | SourceLocation rparenloc) |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 80 | : Expr(CallExprClass, t), Fn(fn), NumArgs(numargs) { |
Chris Lattner | e165d94 | 2006-08-24 04:40:38 +0000 | [diff] [blame] | 81 | Args = new Expr*[numargs]; |
| 82 | for (unsigned i = 0; i != numargs; ++i) |
| 83 | Args[i] = args[i]; |
Chris Lattner | 9b3b9a1 | 2007-06-27 06:08:24 +0000 | [diff] [blame] | 84 | RParenLoc = rparenloc; |
Chris Lattner | e165d94 | 2006-08-24 04:40:38 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 87 | /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it |
| 88 | /// corresponds to, e.g. "<<=". |
| 89 | const char *BinaryOperator::getOpcodeStr(Opcode Op) { |
| 90 | switch (Op) { |
| 91 | default: assert(0 && "Unknown binary operator"); |
| 92 | case Mul: return "*"; |
| 93 | case Div: return "/"; |
| 94 | case Rem: return "%"; |
| 95 | case Add: return "+"; |
| 96 | case Sub: return "-"; |
| 97 | case Shl: return "<<"; |
| 98 | case Shr: return ">>"; |
| 99 | case LT: return "<"; |
| 100 | case GT: return ">"; |
| 101 | case LE: return "<="; |
| 102 | case GE: return ">="; |
| 103 | case EQ: return "=="; |
| 104 | case NE: return "!="; |
| 105 | case And: return "&"; |
| 106 | case Xor: return "^"; |
| 107 | case Or: return "|"; |
| 108 | case LAnd: return "&&"; |
| 109 | case LOr: return "||"; |
| 110 | case Assign: return "="; |
| 111 | case MulAssign: return "*="; |
| 112 | case DivAssign: return "/="; |
| 113 | case RemAssign: return "%="; |
| 114 | case AddAssign: return "+="; |
| 115 | case SubAssign: return "-="; |
| 116 | case ShlAssign: return "<<="; |
| 117 | case ShrAssign: return ">>="; |
| 118 | case AndAssign: return "&="; |
| 119 | case XorAssign: return "^="; |
| 120 | case OrAssign: return "|="; |
| 121 | case Comma: return ","; |
| 122 | } |
| 123 | } |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 124 | |
Chris Lattner | 1ec5f56 | 2007-06-27 05:38:08 +0000 | [diff] [blame] | 125 | |
| 126 | //===----------------------------------------------------------------------===// |
| 127 | // Generic Expression Routines |
| 128 | //===----------------------------------------------------------------------===// |
| 129 | |
| 130 | /// hasLocalSideEffect - Return true if this immediate expression has side |
| 131 | /// effects, not counting any sub-expressions. |
| 132 | bool Expr::hasLocalSideEffect() const { |
| 133 | switch (getStmtClass()) { |
| 134 | default: |
| 135 | return false; |
| 136 | case ParenExprClass: |
| 137 | return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect(); |
| 138 | case UnaryOperatorClass: { |
| 139 | const UnaryOperator *UO = cast<UnaryOperator>(this); |
| 140 | |
| 141 | switch (UO->getOpcode()) { |
| 142 | default: return false; |
| 143 | case UnaryOperator::PostInc: |
| 144 | case UnaryOperator::PostDec: |
| 145 | case UnaryOperator::PreInc: |
| 146 | case UnaryOperator::PreDec: |
| 147 | return true; // ++/-- |
| 148 | |
Chris Lattner | a44d116 | 2007-06-27 05:58:59 +0000 | [diff] [blame] | 149 | case UnaryOperator::Deref: |
| 150 | // Dereferencing a volatile pointer is a side-effect. |
| 151 | return getType().isVolatileQualified(); |
| 152 | case UnaryOperator::Real: |
| 153 | case UnaryOperator::Imag: |
| 154 | // accessing a piece of a volatile complex is a side-effect. |
| 155 | return UO->getSubExpr()->getType().isVolatileQualified(); |
| 156 | |
Chris Lattner | 1ec5f56 | 2007-06-27 05:38:08 +0000 | [diff] [blame] | 157 | case UnaryOperator::Extension: |
| 158 | return UO->getSubExpr()->hasLocalSideEffect(); |
| 159 | } |
| 160 | } |
| 161 | case BinaryOperatorClass: |
| 162 | return cast<BinaryOperator>(this)->isAssignmentOp(); |
| 163 | |
Chris Lattner | a44d116 | 2007-06-27 05:58:59 +0000 | [diff] [blame] | 164 | case MemberExprClass: |
Chris Lattner | 1ec5f56 | 2007-06-27 05:38:08 +0000 | [diff] [blame] | 165 | case ArraySubscriptExprClass: |
Chris Lattner | a44d116 | 2007-06-27 05:58:59 +0000 | [diff] [blame] | 166 | // If the base pointer or element is to a volatile pointer/field, accessing |
| 167 | // if is a side effect. |
| 168 | return getType().isVolatileQualified(); |
Chris Lattner | 1ec5f56 | 2007-06-27 05:38:08 +0000 | [diff] [blame] | 169 | |
| 170 | case CallExprClass: |
Chris Lattner | a44d116 | 2007-06-27 05:58:59 +0000 | [diff] [blame] | 171 | // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }" |
| 172 | // should warn. |
Chris Lattner | 1ec5f56 | 2007-06-27 05:38:08 +0000 | [diff] [blame] | 173 | return true; |
Chris Lattner | 1ec5f56 | 2007-06-27 05:38:08 +0000 | [diff] [blame] | 174 | |
| 175 | case CastExprClass: |
| 176 | // If this is a cast to void, check the operand. Otherwise, the result of |
| 177 | // the cast is unused. |
| 178 | if (getType()->isVoidType()) |
| 179 | return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect(); |
| 180 | return false; |
| 181 | } |
| 182 | } |
| 183 | |
Steve Naroff | 475cca0 | 2007-05-14 17:19:29 +0000 | [diff] [blame] | 184 | /// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an |
| 185 | /// incomplete type other than void. Nonarray expressions that can be lvalues: |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 186 | /// - name, where name must be a variable |
| 187 | /// - e[i] |
| 188 | /// - (e), where e must be an lvalue |
| 189 | /// - e.name, where e must be an lvalue |
| 190 | /// - e->name |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 191 | /// - *e, the type of e cannot be a function type |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 192 | /// - string-constant |
| 193 | /// |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 194 | Expr::isLvalueResult Expr::isLvalue() { |
Steve Naroff | 475cca0 | 2007-05-14 17:19:29 +0000 | [diff] [blame] | 195 | // first, check the type (C99 6.3.2.1) |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 196 | if (isa<FunctionType>(TR.getCanonicalType())) // from isObjectType() |
| 197 | return LV_NotObjectType; |
Steve Naroff | e728ba3 | 2007-07-10 22:20:04 +0000 | [diff] [blame^] | 198 | |
Steve Naroff | 475cca0 | 2007-05-14 17:19:29 +0000 | [diff] [blame] | 199 | if (TR->isIncompleteType() && TR->isVoidType()) |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 200 | return LV_IncompleteVoidType; |
Steve Naroff | e728ba3 | 2007-07-10 22:20:04 +0000 | [diff] [blame^] | 201 | |
Steve Naroff | 5dd642e | 2007-05-14 18:14:51 +0000 | [diff] [blame] | 202 | // the type looks fine, now check the expression |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 203 | switch (getStmtClass()) { |
Steve Naroff | 475cca0 | 2007-05-14 17:19:29 +0000 | [diff] [blame] | 204 | case StringLiteralClass: // C99 6.5.1p4 |
Steve Naroff | 5dd642e | 2007-05-14 18:14:51 +0000 | [diff] [blame] | 205 | case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2)))) |
Steve Naroff | e728ba3 | 2007-07-10 22:20:04 +0000 | [diff] [blame^] | 206 | // For vectors, make sure base is an lvalue (i.e. not a function call). |
| 207 | if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType()) |
| 208 | return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(); |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 209 | return LV_Valid; |
Steve Naroff | 475cca0 | 2007-05-14 17:19:29 +0000 | [diff] [blame] | 210 | case DeclRefExprClass: // C99 6.5.1p2 |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 211 | if (isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl())) |
| 212 | return LV_Valid; |
| 213 | break; |
Steve Naroff | 475cca0 | 2007-05-14 17:19:29 +0000 | [diff] [blame] | 214 | case MemberExprClass: // C99 6.5.2.3p4 |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 215 | const MemberExpr *m = cast<MemberExpr>(this); |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 216 | return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(); |
Steve Naroff | 475cca0 | 2007-05-14 17:19:29 +0000 | [diff] [blame] | 217 | case UnaryOperatorClass: // C99 6.5.3p4 |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 218 | if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref) |
| 219 | return LV_Valid; |
| 220 | break; |
Steve Naroff | 475cca0 | 2007-05-14 17:19:29 +0000 | [diff] [blame] | 221 | case ParenExprClass: // C99 6.5.1p5 |
| 222 | return cast<ParenExpr>(this)->getSubExpr()->isLvalue(); |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 223 | default: |
| 224 | break; |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 225 | } |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 226 | return LV_InvalidExpression; |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 227 | } |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 228 | |
Steve Naroff | 475cca0 | 2007-05-14 17:19:29 +0000 | [diff] [blame] | 229 | /// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type, |
| 230 | /// does not have an incomplete type, does not have a const-qualified type, and |
| 231 | /// if it is a structure or union, does not have any member (including, |
| 232 | /// recursively, any member or element of all contained aggregates or unions) |
| 233 | /// with a const-qualified type. |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 234 | Expr::isModifiableLvalueResult Expr::isModifiableLvalue() { |
| 235 | isLvalueResult lvalResult = isLvalue(); |
| 236 | |
| 237 | switch (lvalResult) { |
Chris Lattner | 1ec5f56 | 2007-06-27 05:38:08 +0000 | [diff] [blame] | 238 | case LV_Valid: break; |
| 239 | case LV_NotObjectType: return MLV_NotObjectType; |
| 240 | case LV_IncompleteVoidType: return MLV_IncompleteVoidType; |
| 241 | case LV_InvalidExpression: return MLV_InvalidExpression; |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 242 | } |
Steve Naroff | 475cca0 | 2007-05-14 17:19:29 +0000 | [diff] [blame] | 243 | if (TR.isConstQualified()) |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 244 | return MLV_ConstQualified; |
Steve Naroff | 475cca0 | 2007-05-14 17:19:29 +0000 | [diff] [blame] | 245 | if (TR->isArrayType()) |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 246 | return MLV_ArrayType; |
Steve Naroff | 475cca0 | 2007-05-14 17:19:29 +0000 | [diff] [blame] | 247 | if (TR->isIncompleteType()) |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 248 | return MLV_IncompleteType; |
| 249 | |
| 250 | if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) { |
| 251 | if (r->hasConstFields()) |
| 252 | return MLV_ConstQualified; |
| 253 | } |
| 254 | return MLV_Valid; |
Steve Naroff | 475cca0 | 2007-05-14 17:19:29 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Chris Lattner | 1f4479e | 2007-06-05 04:15:44 +0000 | [diff] [blame] | 257 | /// isIntegerConstantExpr - this recursive routine will test if an expression is |
| 258 | /// an integer constant expression. Note: With the introduction of VLA's in |
Steve Naroff | f8a28c5 | 2007-05-15 20:29:32 +0000 | [diff] [blame] | 259 | /// C99 the result of the sizeof operator is no longer always a constant |
| 260 | /// expression. The generalization of the wording to include any subexpression |
| 261 | /// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions |
| 262 | /// can appear as operands to other operators (e.g. &&, ||, ?:). For instance, |
| 263 | /// "0 || f()" can be treated as a constant expression. In C90 this expression, |
| 264 | /// occurring in a context requiring a constant, would have been a constraint |
| 265 | /// violation. FIXME: This routine currently implements C90 semantics. |
| 266 | /// To properly implement C99 semantics this routine will need to evaluate |
| 267 | /// expressions involving operators previously mentioned. |
Chris Lattner | e0da5dc | 2007-06-05 05:58:31 +0000 | [diff] [blame] | 268 | |
| 269 | /// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, |
| 270 | /// comma, etc |
Chris Lattner | 4ef4001 | 2007-06-11 01:28:17 +0000 | [diff] [blame] | 271 | /// |
| 272 | /// FIXME: This should ext-warn on overflow during evaluation! ISO C does not |
| 273 | /// permit this. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 274 | bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, SourceLocation *Loc, |
Chris Lattner | e0da5dc | 2007-06-05 05:58:31 +0000 | [diff] [blame] | 275 | bool isEvaluated) const { |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 276 | switch (getStmtClass()) { |
Steve Naroff | 72cada0 | 2007-05-18 00:18:52 +0000 | [diff] [blame] | 277 | default: |
Chris Lattner | 238cbc5 | 2007-06-02 04:48:48 +0000 | [diff] [blame] | 278 | if (Loc) *Loc = getLocStart(); |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 279 | return false; |
Chris Lattner | e0da5dc | 2007-06-05 05:58:31 +0000 | [diff] [blame] | 280 | case ParenExprClass: |
| 281 | return cast<ParenExpr>(this)->getSubExpr()-> |
| 282 | isIntegerConstantExpr(Result, Loc, isEvaluated); |
| 283 | case IntegerLiteralClass: |
| 284 | Result = cast<IntegerLiteral>(this)->getValue(); |
| 285 | break; |
| 286 | case CharacterLiteralClass: |
| 287 | // FIXME: This doesn't set the right width etc. |
| 288 | Result.zextOrTrunc(32); // FIXME: NOT RIGHT IN GENERAL. |
Chris Lattner | 4cd73fd | 2007-06-08 21:51:02 +0000 | [diff] [blame] | 289 | Result = cast<CharacterLiteral>(this)->getValue(); |
Chris Lattner | e0da5dc | 2007-06-05 05:58:31 +0000 | [diff] [blame] | 290 | break; |
| 291 | case DeclRefExprClass: |
| 292 | if (const EnumConstantDecl *D = |
| 293 | dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) { |
Chris Lattner | 9fba028 | 2007-06-11 03:47:05 +0000 | [diff] [blame] | 294 | Result = D->getInitVal(); |
Chris Lattner | e0da5dc | 2007-06-05 05:58:31 +0000 | [diff] [blame] | 295 | break; |
| 296 | } |
| 297 | if (Loc) *Loc = getLocStart(); |
| 298 | return false; |
| 299 | case UnaryOperatorClass: { |
| 300 | const UnaryOperator *Exp = cast<UnaryOperator>(this); |
| 301 | |
| 302 | // Get the operand value. If this is sizeof/alignof, do not evalute the |
| 303 | // operand. This affects C99 6.6p3. |
| 304 | if (Exp->isSizeOfAlignOfOp()) isEvaluated = false; |
| 305 | if (!Exp->getSubExpr()->isIntegerConstantExpr(Result, Loc, isEvaluated)) |
| 306 | return false; |
| 307 | |
| 308 | switch (Exp->getOpcode()) { |
| 309 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 310 | // See C99 6.6p3. |
| 311 | default: |
| 312 | if (Loc) *Loc = Exp->getOperatorLoc(); |
| 313 | return false; |
| 314 | case UnaryOperator::Extension: |
| 315 | return true; |
| 316 | case UnaryOperator::SizeOf: |
| 317 | case UnaryOperator::AlignOf: |
| 318 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
| 319 | if (!Exp->getSubExpr()->getType()->isConstantSizeType(Loc)) |
| 320 | return false; |
| 321 | |
| 322 | // FIXME: Evaluate sizeof/alignof. |
| 323 | Result.zextOrTrunc(32); // FIXME: NOT RIGHT IN GENERAL. |
| 324 | Result = 1; // FIXME: Obviously bogus |
| 325 | break; |
| 326 | case UnaryOperator::LNot: { |
| 327 | bool Val = Result != 0; |
| 328 | Result.zextOrTrunc(32); // FIXME: NOT RIGHT IN GENERAL. |
| 329 | Result = Val; |
| 330 | break; |
| 331 | } |
| 332 | case UnaryOperator::Plus: |
| 333 | // FIXME: Do usual unary promotions here! |
| 334 | break; |
| 335 | case UnaryOperator::Minus: |
| 336 | // FIXME: Do usual unary promotions here! |
| 337 | Result = -Result; |
| 338 | break; |
| 339 | case UnaryOperator::Not: |
| 340 | // FIXME: Do usual unary promotions here! |
| 341 | Result = ~Result; |
| 342 | break; |
| 343 | } |
| 344 | break; |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 345 | } |
Chris Lattner | e0da5dc | 2007-06-05 05:58:31 +0000 | [diff] [blame] | 346 | case SizeOfAlignOfTypeExprClass: { |
| 347 | const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this); |
| 348 | // alignof always evaluates to a constant. |
| 349 | if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Loc)) |
| 350 | return false; |
| 351 | |
| 352 | // FIXME: Evaluate sizeof/alignof. |
| 353 | Result.zextOrTrunc(32); // FIXME: NOT RIGHT IN GENERAL. |
| 354 | Result = 1; // FIXME: Obviously bogus |
| 355 | break; |
| 356 | } |
| 357 | case BinaryOperatorClass: { |
| 358 | const BinaryOperator *Exp = cast<BinaryOperator>(this); |
| 359 | |
| 360 | // The LHS of a constant expr is always evaluated and needed. |
| 361 | if (!Exp->getLHS()->isIntegerConstantExpr(Result, Loc, isEvaluated)) |
| 362 | return false; |
| 363 | |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 364 | llvm::APSInt RHS(Result); |
Chris Lattner | e0da5dc | 2007-06-05 05:58:31 +0000 | [diff] [blame] | 365 | |
| 366 | // The short-circuiting &&/|| operators don't necessarily evaluate their |
| 367 | // RHS. Make sure to pass isEvaluated down correctly. |
| 368 | if (Exp->isLogicalOp()) { |
| 369 | bool RHSEval; |
| 370 | if (Exp->getOpcode() == BinaryOperator::LAnd) |
| 371 | RHSEval = Result != 0; |
| 372 | else { |
| 373 | assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical"); |
| 374 | RHSEval = Result == 0; |
| 375 | } |
| 376 | |
| 377 | if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Loc, |
| 378 | isEvaluated & RHSEval)) |
| 379 | return false; |
| 380 | } else { |
| 381 | if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Loc, isEvaluated)) |
| 382 | return false; |
| 383 | } |
| 384 | |
| 385 | // FIXME: These should all do the standard promotions, etc. |
| 386 | switch (Exp->getOpcode()) { |
| 387 | default: |
| 388 | if (Loc) *Loc = getLocStart(); |
| 389 | return false; |
| 390 | case BinaryOperator::Mul: |
| 391 | Result *= RHS; |
| 392 | break; |
| 393 | case BinaryOperator::Div: |
| 394 | if (RHS == 0) { |
| 395 | if (!isEvaluated) break; |
| 396 | if (Loc) *Loc = getLocStart(); |
| 397 | return false; |
| 398 | } |
| 399 | Result /= RHS; |
| 400 | break; |
| 401 | case BinaryOperator::Rem: |
| 402 | if (RHS == 0) { |
| 403 | if (!isEvaluated) break; |
| 404 | if (Loc) *Loc = getLocStart(); |
| 405 | return false; |
| 406 | } |
| 407 | Result %= RHS; |
| 408 | break; |
| 409 | case BinaryOperator::Add: Result += RHS; break; |
| 410 | case BinaryOperator::Sub: Result -= RHS; break; |
Chris Lattner | 901ae1f | 2007-06-08 21:54:26 +0000 | [diff] [blame] | 411 | case BinaryOperator::Shl: |
| 412 | Result <<= RHS.getLimitedValue(Result.getBitWidth()-1); |
| 413 | break; |
| 414 | case BinaryOperator::Shr: |
| 415 | Result >>= RHS.getLimitedValue(Result.getBitWidth()-1); |
| 416 | break; |
Chris Lattner | e0da5dc | 2007-06-05 05:58:31 +0000 | [diff] [blame] | 417 | case BinaryOperator::LT: Result = Result < RHS; break; |
| 418 | case BinaryOperator::GT: Result = Result > RHS; break; |
| 419 | case BinaryOperator::LE: Result = Result <= RHS; break; |
| 420 | case BinaryOperator::GE: Result = Result >= RHS; break; |
| 421 | case BinaryOperator::EQ: Result = Result == RHS; break; |
| 422 | case BinaryOperator::NE: Result = Result != RHS; break; |
| 423 | case BinaryOperator::And: Result &= RHS; break; |
| 424 | case BinaryOperator::Xor: Result ^= RHS; break; |
| 425 | case BinaryOperator::Or: Result |= RHS; break; |
| 426 | case BinaryOperator::LAnd: |
| 427 | Result = Result != 0 && RHS != 0; |
| 428 | break; |
| 429 | case BinaryOperator::LOr: |
| 430 | Result = Result != 0 || RHS != 0; |
| 431 | break; |
| 432 | |
| 433 | case BinaryOperator::Comma: |
| 434 | // C99 6.6p3: "shall not contain assignment, ..., or comma operators, |
| 435 | // *except* when they are contained within a subexpression that is not |
| 436 | // evaluated". Note that Assignment can never happen due to constraints |
| 437 | // on the LHS subexpr, so we don't need to check it here. |
| 438 | if (isEvaluated) { |
| 439 | if (Loc) *Loc = getLocStart(); |
| 440 | return false; |
| 441 | } |
| 442 | |
| 443 | // The result of the constant expr is the RHS. |
| 444 | Result = RHS; |
| 445 | return true; |
| 446 | } |
| 447 | |
| 448 | assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!"); |
| 449 | break; |
| 450 | } |
| 451 | case CastExprClass: { |
| 452 | const CastExpr *Exp = cast<CastExpr>(this); |
| 453 | // C99 6.6p6: shall only convert arithmetic types to integer types. |
| 454 | if (!Exp->getSubExpr()->getType()->isArithmeticType() || |
| 455 | !Exp->getDestType()->isIntegerType()) { |
| 456 | if (Loc) *Loc = Exp->getSubExpr()->getLocStart(); |
| 457 | return false; |
| 458 | } |
| 459 | |
| 460 | // Handle simple integer->integer casts. |
| 461 | if (Exp->getSubExpr()->getType()->isIntegerType()) { |
| 462 | if (!Exp->getSubExpr()->isIntegerConstantExpr(Result, Loc, isEvaluated)) |
| 463 | return false; |
| 464 | // FIXME: do the conversion on Result. |
| 465 | break; |
| 466 | } |
| 467 | |
| 468 | // Allow floating constants that are the immediate operands of casts or that |
| 469 | // are parenthesized. |
| 470 | const Expr *Operand = Exp->getSubExpr(); |
| 471 | while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand)) |
| 472 | Operand = PE->getSubExpr(); |
| 473 | |
| 474 | if (const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand)) { |
| 475 | // FIXME: Evaluate this correctly! |
| 476 | Result = (int)FL->getValue(); |
| 477 | break; |
| 478 | } |
Chris Lattner | 6046e00 | 2007-06-05 06:39:23 +0000 | [diff] [blame] | 479 | if (Loc) *Loc = Operand->getLocStart(); |
Chris Lattner | e0da5dc | 2007-06-05 05:58:31 +0000 | [diff] [blame] | 480 | return false; |
| 481 | } |
| 482 | case ConditionalOperatorClass: { |
| 483 | const ConditionalOperator *Exp = cast<ConditionalOperator>(this); |
| 484 | |
| 485 | if (!Exp->getCond()->isIntegerConstantExpr(Result, Loc, isEvaluated)) |
| 486 | return false; |
| 487 | |
| 488 | const Expr *TrueExp = Exp->getLHS(); |
| 489 | const Expr *FalseExp = Exp->getRHS(); |
| 490 | if (Result == 0) std::swap(TrueExp, FalseExp); |
| 491 | |
| 492 | // Evaluate the false one first, discard the result. |
| 493 | if (!FalseExp->isIntegerConstantExpr(Result, Loc, false)) |
| 494 | return false; |
| 495 | // Evalute the true one, capture the result. |
| 496 | if (!TrueExp->isIntegerConstantExpr(Result, Loc, isEvaluated)) |
| 497 | return false; |
| 498 | // FIXME: promotions on result. |
| 499 | break; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | // Cases that are valid constant exprs fall through to here. |
| 504 | Result.setIsUnsigned(getType()->isUnsignedIntegerType()); |
| 505 | return true; |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Chris Lattner | 1f4479e | 2007-06-05 04:15:44 +0000 | [diff] [blame] | 508 | |
Chris Lattner | 7eef919 | 2007-05-24 01:23:49 +0000 | [diff] [blame] | 509 | /// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an |
| 510 | /// integer constant expression with the value zero, or if this is one that is |
| 511 | /// cast to void*. |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 512 | bool Expr::isNullPointerConstant() const { |
Chris Lattner | 7eef919 | 2007-05-24 01:23:49 +0000 | [diff] [blame] | 513 | // Strip off a cast to void*, if it exists. |
| 514 | if (const CastExpr *CE = dyn_cast<CastExpr>(this)) { |
| 515 | // Check that it is a cast to void*. |
| 516 | if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) { |
| 517 | QualType Pointee = PT->getPointeeType(); |
| 518 | if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void* |
| 519 | CE->getSubExpr()->getType()->isIntegerType()) // from int. |
| 520 | return CE->getSubExpr()->isNullPointerConstant(); |
Steve Naroff | ada7d42 | 2007-05-20 17:54:12 +0000 | [diff] [blame] | 521 | } |
Chris Lattner | 7eef919 | 2007-05-24 01:23:49 +0000 | [diff] [blame] | 522 | } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) { |
| 523 | // Accept ((void*)0) as a null pointer constant, as many other |
| 524 | // implementations do. |
| 525 | return PE->getSubExpr()->isNullPointerConstant(); |
Chris Lattner | e6b0f62 | 2007-05-21 05:58:35 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Chris Lattner | 7eef919 | 2007-05-24 01:23:49 +0000 | [diff] [blame] | 528 | // This expression must be an integer type. |
| 529 | if (!getType()->isIntegerType()) |
| 530 | return false; |
| 531 | |
Chris Lattner | 1abbd41 | 2007-06-08 17:58:43 +0000 | [diff] [blame] | 532 | // If we have an integer constant expression, we need to *evaluate* it and |
| 533 | // test for the value 0. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 534 | llvm::APSInt Val(32); |
Chris Lattner | 1abbd41 | 2007-06-08 17:58:43 +0000 | [diff] [blame] | 535 | return isIntegerConstantExpr(Val, 0, true) && Val == 0; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 536 | } |