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