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 llvm; |
| 18 | using namespace clang; |
| 19 | |
Chris Lattner | 0eedafe | 2006-08-24 04:56:27 +0000 | [diff] [blame] | 20 | //===----------------------------------------------------------------------===// |
| 21 | // Primary Expressions. |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | |
Steve Naroff | 408451b | 2007-02-26 22:17:12 +0000 | [diff] [blame] | 24 | StringLiteral::StringLiteral(const char *strData, unsigned byteLength, |
Steve Naroff | 53f07dc | 2007-05-17 21:49:33 +0000 | [diff] [blame] | 25 | bool Wide, QualType t, SourceLocation firstLoc, |
| 26 | SourceLocation lastLoc) : |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 27 | Expr(StringLiteralClass, t) { |
Steve Naroff | df7855b | 2007-02-21 23:46:25 +0000 | [diff] [blame] | 28 | // OPTIMIZE: could allocate this appended to the StringLiteral. |
Chris Lattner | d3e9895 | 2006-10-06 05:22:26 +0000 | [diff] [blame] | 29 | char *AStrData = new char[byteLength]; |
| 30 | memcpy(AStrData, strData, byteLength); |
| 31 | StrData = AStrData; |
| 32 | ByteLength = byteLength; |
Chris Lattner | 882f788 | 2006-11-04 18:52:07 +0000 | [diff] [blame] | 33 | IsWide = Wide; |
Steve Naroff | 53f07dc | 2007-05-17 21:49:33 +0000 | [diff] [blame] | 34 | firstTokLoc = firstLoc; |
| 35 | lastTokLoc = lastLoc; |
Chris Lattner | d3e9895 | 2006-10-06 05:22:26 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Steve Naroff | df7855b | 2007-02-21 23:46:25 +0000 | [diff] [blame] | 38 | StringLiteral::~StringLiteral() { |
Chris Lattner | d3e9895 | 2006-10-06 05:22:26 +0000 | [diff] [blame] | 39 | delete[] StrData; |
| 40 | } |
| 41 | |
Chris Lattner | 1576870 | 2006-11-05 23:54:51 +0000 | [diff] [blame] | 42 | bool UnaryOperator::isPostfix(Opcode Op) { |
| 43 | switch (Op) { |
| 44 | case PostInc: |
| 45 | case PostDec: |
| 46 | return true; |
| 47 | default: |
| 48 | return false; |
| 49 | } |
| 50 | } |
| 51 | |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 52 | /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it |
| 53 | /// corresponds to, e.g. "sizeof" or "[pre]++". |
| 54 | const char *UnaryOperator::getOpcodeStr(Opcode Op) { |
| 55 | switch (Op) { |
Chris Lattner | c52b118 | 2006-10-25 05:45:55 +0000 | [diff] [blame] | 56 | default: assert(0 && "Unknown unary operator"); |
Chris Lattner | 1576870 | 2006-11-05 23:54:51 +0000 | [diff] [blame] | 57 | case PostInc: return "++"; |
| 58 | case PostDec: return "--"; |
| 59 | case PreInc: return "++"; |
| 60 | case PreDec: return "--"; |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 61 | case AddrOf: return "&"; |
| 62 | case Deref: return "*"; |
| 63 | case Plus: return "+"; |
| 64 | case Minus: return "-"; |
| 65 | case Not: return "~"; |
| 66 | case LNot: return "!"; |
| 67 | case Real: return "__real"; |
| 68 | case Imag: return "__imag"; |
Chris Lattner | 26115ac | 2006-08-24 06:10:04 +0000 | [diff] [blame] | 69 | case SizeOf: return "sizeof"; |
| 70 | case AlignOf: return "alignof"; |
Chris Lattner | c52b118 | 2006-10-25 05:45:55 +0000 | [diff] [blame] | 71 | case Extension: return "__extension__"; |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
Chris Lattner | 0eedafe | 2006-08-24 04:56:27 +0000 | [diff] [blame] | 75 | //===----------------------------------------------------------------------===// |
| 76 | // Postfix Operators. |
| 77 | //===----------------------------------------------------------------------===// |
Chris Lattner | e165d94 | 2006-08-24 04:40:38 +0000 | [diff] [blame] | 78 | |
Steve Naroff | 509fe02 | 2007-05-17 01:16:00 +0000 | [diff] [blame] | 79 | CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t, |
| 80 | SourceLocation l) |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 81 | : Expr(CallExprClass, t), Fn(fn), NumArgs(numargs) { |
Chris Lattner | e165d94 | 2006-08-24 04:40:38 +0000 | [diff] [blame] | 82 | Args = new Expr*[numargs]; |
| 83 | for (unsigned i = 0; i != numargs; ++i) |
| 84 | Args[i] = args[i]; |
Steve Naroff | 509fe02 | 2007-05-17 01:16:00 +0000 | [diff] [blame] | 85 | Loc = l; |
Chris Lattner | e165d94 | 2006-08-24 04:40:38 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 88 | /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it |
| 89 | /// corresponds to, e.g. "<<=". |
| 90 | const char *BinaryOperator::getOpcodeStr(Opcode Op) { |
| 91 | switch (Op) { |
| 92 | default: assert(0 && "Unknown binary operator"); |
| 93 | case Mul: return "*"; |
| 94 | case Div: return "/"; |
| 95 | case Rem: return "%"; |
| 96 | case Add: return "+"; |
| 97 | case Sub: return "-"; |
| 98 | case Shl: return "<<"; |
| 99 | case Shr: return ">>"; |
| 100 | case LT: return "<"; |
| 101 | case GT: return ">"; |
| 102 | case LE: return "<="; |
| 103 | case GE: return ">="; |
| 104 | case EQ: return "=="; |
| 105 | case NE: return "!="; |
| 106 | case And: return "&"; |
| 107 | case Xor: return "^"; |
| 108 | case Or: return "|"; |
| 109 | case LAnd: return "&&"; |
| 110 | case LOr: return "||"; |
| 111 | case Assign: return "="; |
| 112 | case MulAssign: return "*="; |
| 113 | case DivAssign: return "/="; |
| 114 | case RemAssign: return "%="; |
| 115 | case AddAssign: return "+="; |
| 116 | case SubAssign: return "-="; |
| 117 | case ShlAssign: return "<<="; |
| 118 | case ShrAssign: return ">>="; |
| 119 | case AndAssign: return "&="; |
| 120 | case XorAssign: return "^="; |
| 121 | case OrAssign: return "|="; |
| 122 | case Comma: return ","; |
| 123 | } |
| 124 | } |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 125 | |
Steve Naroff | 475cca0 | 2007-05-14 17:19:29 +0000 | [diff] [blame] | 126 | /// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an |
| 127 | /// incomplete type other than void. Nonarray expressions that can be lvalues: |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 128 | /// - name, where name must be a variable |
| 129 | /// - e[i] |
| 130 | /// - (e), where e must be an lvalue |
| 131 | /// - e.name, where e must be an lvalue |
| 132 | /// - e->name |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 133 | /// - *e, the type of e cannot be a function type |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 134 | /// - string-constant |
| 135 | /// |
Steve Naroff | 475cca0 | 2007-05-14 17:19:29 +0000 | [diff] [blame] | 136 | bool Expr::isLvalue() { |
| 137 | // first, check the type (C99 6.3.2.1) |
| 138 | if (!TR->isObjectType()) |
| 139 | return false; |
| 140 | if (TR->isIncompleteType() && TR->isVoidType()) |
| 141 | return false; |
| 142 | |
Steve Naroff | 5dd642e | 2007-05-14 18:14:51 +0000 | [diff] [blame] | 143 | // the type looks fine, now check the expression |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 144 | switch (getStmtClass()) { |
Steve Naroff | 475cca0 | 2007-05-14 17:19:29 +0000 | [diff] [blame] | 145 | case StringLiteralClass: // C99 6.5.1p4 |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 146 | return true; |
Steve Naroff | 5dd642e | 2007-05-14 18:14:51 +0000 | [diff] [blame] | 147 | case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2)))) |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 148 | return true; |
Steve Naroff | 475cca0 | 2007-05-14 17:19:29 +0000 | [diff] [blame] | 149 | case DeclRefExprClass: // C99 6.5.1p2 |
Steve Naroff | 5dd642e | 2007-05-14 18:14:51 +0000 | [diff] [blame] | 150 | return isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl()); |
Steve Naroff | 475cca0 | 2007-05-14 17:19:29 +0000 | [diff] [blame] | 151 | case MemberExprClass: // C99 6.5.2.3p4 |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 152 | const MemberExpr *m = cast<MemberExpr>(this); |
Steve Naroff | 5dd642e | 2007-05-14 18:14:51 +0000 | [diff] [blame] | 153 | return m->isArrow() ? true : m->getBase()->isLvalue(); |
Steve Naroff | 475cca0 | 2007-05-14 17:19:29 +0000 | [diff] [blame] | 154 | case UnaryOperatorClass: // C99 6.5.3p4 |
Steve Naroff | 5dd642e | 2007-05-14 18:14:51 +0000 | [diff] [blame] | 155 | return cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref; |
Steve Naroff | 475cca0 | 2007-05-14 17:19:29 +0000 | [diff] [blame] | 156 | case ParenExprClass: // C99 6.5.1p5 |
| 157 | return cast<ParenExpr>(this)->getSubExpr()->isLvalue(); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 158 | default: |
| 159 | return false; |
| 160 | } |
| 161 | } |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 162 | |
Steve Naroff | 475cca0 | 2007-05-14 17:19:29 +0000 | [diff] [blame] | 163 | /// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type, |
| 164 | /// does not have an incomplete type, does not have a const-qualified type, and |
| 165 | /// if it is a structure or union, does not have any member (including, |
| 166 | /// recursively, any member or element of all contained aggregates or unions) |
| 167 | /// with a const-qualified type. |
| 168 | bool Expr::isModifiableLvalue() { |
| 169 | if (!isLvalue()) |
| 170 | return false; |
| 171 | |
| 172 | if (TR.isConstQualified()) |
| 173 | return false; |
| 174 | if (TR->isArrayType()) |
| 175 | return false; |
| 176 | if (TR->isIncompleteType()) |
| 177 | return false; |
| 178 | if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) |
| 179 | return r->isModifiableLvalue(); |
| 180 | return true; |
| 181 | } |
| 182 | |
Steve Naroff | f8a28c5 | 2007-05-15 20:29:32 +0000 | [diff] [blame] | 183 | /// isConstantExpr - this recursive routine will test if an expression is |
| 184 | /// either a constant expression (isIntConst == false) or an integer constant |
| 185 | /// expression (isIntConst == true). Note: With the introduction of VLA's in |
| 186 | /// C99 the result of the sizeof operator is no longer always a constant |
| 187 | /// expression. The generalization of the wording to include any subexpression |
| 188 | /// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions |
| 189 | /// can appear as operands to other operators (e.g. &&, ||, ?:). For instance, |
| 190 | /// "0 || f()" can be treated as a constant expression. In C90 this expression, |
| 191 | /// occurring in a context requiring a constant, would have been a constraint |
| 192 | /// violation. FIXME: This routine currently implements C90 semantics. |
| 193 | /// To properly implement C99 semantics this routine will need to evaluate |
| 194 | /// expressions involving operators previously mentioned. |
Steve Naroff | 72cada0 | 2007-05-18 00:18:52 +0000 | [diff] [blame] | 195 | bool Expr::isConstantExpr(bool isIntConst, SourceLocation &loc) const { |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 196 | switch (getStmtClass()) { |
| 197 | case IntegerLiteralClass: |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 198 | case CharacterLiteralClass: |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 199 | return true; |
Steve Naroff | f8a28c5 | 2007-05-15 20:29:32 +0000 | [diff] [blame] | 200 | case FloatingLiteralClass: |
| 201 | case StringLiteralClass: |
Steve Naroff | 72cada0 | 2007-05-18 00:18:52 +0000 | [diff] [blame] | 202 | if (!isIntConst) |
| 203 | return true; |
| 204 | loc = getLocStart(); |
| 205 | return false; |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 206 | case DeclRefExprClass: |
Steve Naroff | 72cada0 | 2007-05-18 00:18:52 +0000 | [diff] [blame] | 207 | if (isa<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) |
| 208 | return true; |
| 209 | loc = getLocStart(); |
| 210 | return false; |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 211 | case UnaryOperatorClass: |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 212 | const UnaryOperator *uop = cast<UnaryOperator>(this); |
Steve Naroff | 72cada0 | 2007-05-18 00:18:52 +0000 | [diff] [blame] | 213 | if (uop->isIncrementDecrementOp()) { // C99 6.6p3 |
| 214 | loc = getLocStart(); |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 215 | return false; |
Steve Naroff | 72cada0 | 2007-05-18 00:18:52 +0000 | [diff] [blame] | 216 | } |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 217 | // C99 6.5.3.4p2: otherwise, the operand is *not* evaluated and the result |
| 218 | // is an integer constant. This effective ignores any subexpression that |
| 219 | // isn't actually a constant expression (what an odd language:-) |
| 220 | if (uop->isSizeOfAlignOfOp()) |
Steve Naroff | 72cada0 | 2007-05-18 00:18:52 +0000 | [diff] [blame] | 221 | return uop->getSubExpr()->getType()->isConstantSizeType(loc); |
| 222 | return uop->getSubExpr()->isConstantExpr(isIntConst, loc); |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 223 | case BinaryOperatorClass: |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 224 | const BinaryOperator *bop = cast<BinaryOperator>(this); |
| 225 | // C99 6.6p3: shall not contain assignment, increment/decrement, |
| 226 | // function call, or comma operators, *except* when they are contained |
| 227 | // within a subexpression that is not evaluated. |
Steve Naroff | 72cada0 | 2007-05-18 00:18:52 +0000 | [diff] [blame] | 228 | if (bop->isAssignmentOp() || bop->getOpcode() == BinaryOperator::Comma) { |
| 229 | loc = getLocStart(); |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 230 | return false; |
Steve Naroff | 72cada0 | 2007-05-18 00:18:52 +0000 | [diff] [blame] | 231 | } |
| 232 | return bop->getLHS()->isConstantExpr(isIntConst, loc) && |
| 233 | bop->getRHS()->isConstantExpr(isIntConst, loc); |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 234 | case ParenExprClass: |
Steve Naroff | 72cada0 | 2007-05-18 00:18:52 +0000 | [diff] [blame] | 235 | return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(isIntConst, loc); |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 236 | case CastExprClass: |
| 237 | const CastExpr *castExpr = cast<CastExpr>(this); |
| 238 | // C99 6.6p6: shall only convert arithmetic types to integer types. |
Steve Naroff | 72cada0 | 2007-05-18 00:18:52 +0000 | [diff] [blame] | 239 | if (!castExpr->getSubExpr()->getType()->isArithmeticType()) { |
| 240 | loc = castExpr->getSubExpr()->getLocStart(); |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 241 | return false; |
Steve Naroff | 72cada0 | 2007-05-18 00:18:52 +0000 | [diff] [blame] | 242 | } |
| 243 | if (!castExpr->getDestType()->isIntegerType()) { |
| 244 | loc = getLocStart(); |
| 245 | return false; |
| 246 | } |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 247 | // allow floating constants that are the immediate operands of casts. |
Steve Naroff | 72cada0 | 2007-05-18 00:18:52 +0000 | [diff] [blame] | 248 | if (castExpr->getSubExpr()->isConstantExpr(isIntConst, loc) || |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 249 | isa<FloatingLiteral>(castExpr->getSubExpr())) |
| 250 | return true; |
Steve Naroff | 72cada0 | 2007-05-18 00:18:52 +0000 | [diff] [blame] | 251 | loc = getLocStart(); |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 252 | return false; |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 253 | case SizeOfAlignOfTypeExprClass: |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 254 | const SizeOfAlignOfTypeExpr *sizeExpr = cast<SizeOfAlignOfTypeExpr>(this); |
| 255 | if (sizeExpr->isSizeOf()) |
Steve Naroff | 72cada0 | 2007-05-18 00:18:52 +0000 | [diff] [blame] | 256 | return sizeExpr->getArgumentType()->isConstantSizeType(loc); |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 257 | return true; // alignof will always evaluate to a constant |
| 258 | case ConditionalOperatorClass: |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 259 | const ConditionalOperator *condExpr = cast<ConditionalOperator>(this); |
Steve Naroff | 72cada0 | 2007-05-18 00:18:52 +0000 | [diff] [blame] | 260 | return condExpr->getCond()->isConstantExpr(isIntConst, loc) && |
| 261 | condExpr->getLHS()->isConstantExpr(isIntConst, loc) && |
| 262 | condExpr->getRHS()->isConstantExpr(isIntConst, loc); |
| 263 | default: |
| 264 | loc = getLocStart(); |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 265 | return false; |
| 266 | } |
| 267 | } |
| 268 | |
Steve Naroff | ada7d42 | 2007-05-20 17:54:12 +0000 | [diff] [blame^] | 269 | // C99 6.3.2.3p3: FIXME: If we have an integer constant expression, we need |
| 270 | // to *evaluate* it and test for the value 0. The current code is too |
| 271 | // simplistic...it only allows for the integer literal "0". |
| 272 | // For example, the following is valid code: |
| 273 | // |
| 274 | // void test1() { *(n ? p : (void *)(7-7)) = 1; } |
| 275 | // |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 276 | bool Expr::isNullPointerConstant() const { |
Steve Naroff | ada7d42 | 2007-05-20 17:54:12 +0000 | [diff] [blame^] | 277 | const IntegerLiteral *constant = 0; |
| 278 | |
| 279 | switch (getStmtClass()) { |
| 280 | case IntegerLiteralClass: |
| 281 | constant = cast<IntegerLiteral>(this); |
| 282 | break; |
| 283 | case CastExprClass: |
| 284 | const CastExpr *cExpr = cast<CastExpr>(this); |
| 285 | if (const PointerType *p = dyn_cast<PointerType>(cExpr->getDestType())) { |
| 286 | QualType t = p->getPointeeType(); |
| 287 | // the type needs to be "void *" (no qualifiers are permitted) |
| 288 | if (!t.getQualifiers() && t->isVoidType()) |
| 289 | constant = dyn_cast<IntegerLiteral>(cExpr->getSubExpr()); |
| 290 | } |
| 291 | break; |
| 292 | default: |
| 293 | break; |
| 294 | } |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 295 | if (!constant || constant->getValue() != 0) |
| 296 | return false; |
| 297 | return true; |
| 298 | } |