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 | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 121 | /// Nonarray expressions that can be lvalues: |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 122 | /// - name, where name must be a variable |
| 123 | /// - e[i] |
| 124 | /// - (e), where e must be an lvalue |
| 125 | /// - e.name, where e must be an lvalue |
| 126 | /// - e->name |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame^] | 127 | /// - *e, the type of e cannot be a function type |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 128 | /// - string-constant |
| 129 | /// |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 130 | bool Expr::isModifiableLvalue() { |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 131 | switch (getStmtClass()) { |
| 132 | case StringLiteralClass: |
| 133 | return true; |
| 134 | case ArraySubscriptExprClass: |
| 135 | return true; |
| 136 | case DeclRefExprClass: |
| 137 | const DeclRefExpr *d = cast<DeclRefExpr>(this); |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 138 | if (const VarDecl *vd = dyn_cast<VarDecl>(d->getDecl())) |
| 139 | if (vd->getType().isModifiableLvalue()) |
| 140 | return true; |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 141 | return false; |
| 142 | case MemberExprClass: |
| 143 | const MemberExpr *m = cast<MemberExpr>(this); |
| 144 | if (m->isArrow()) |
| 145 | return true; |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 146 | return m->getBase()->isModifiableLvalue(); // make sure "." is an lvalue |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 147 | case UnaryOperatorClass: |
| 148 | const UnaryOperator *u = cast<UnaryOperator>(this); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame^] | 149 | return u->getOpcode() == UnaryOperator::Deref && |
| 150 | u->getType().isModifiableLvalue(); // C99 6.5.3.2p4 |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 151 | case ParenExprClass: |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 152 | return cast<ParenExpr>(this)->getSubExpr()->isModifiableLvalue(); |
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 | |
| 158 | bool Expr::isNullPointerConstant() const { |
| 159 | const IntegerLiteral *constant = dyn_cast<IntegerLiteral>(this); |
| 160 | if (!constant || constant->getValue() != 0) |
| 161 | return false; |
| 162 | return true; |
| 163 | } |