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