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 | |
| 121 | /// Expressions that can be lvalues: |
| 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 |
| 127 | /// - *e |
| 128 | /// - string-constant |
| 129 | /// |
| 130 | bool Expr::isLvalue() { |
| 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 | f633d09 | 2007-04-25 19:01:39 +0000 | [diff] [blame] | 138 | if (isa<VarDecl>(d->getDecl())) |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 139 | return true; |
| 140 | return false; |
| 141 | case MemberExprClass: |
| 142 | const MemberExpr *m = cast<MemberExpr>(this); |
| 143 | if (m->isArrow()) |
| 144 | return true; |
| 145 | return m->getBase()->isLvalue(); // make sure "." is an lvalue |
| 146 | case UnaryOperatorClass: |
| 147 | const UnaryOperator *u = cast<UnaryOperator>(this); |
Steve Naroff | f633d09 | 2007-04-25 19:01:39 +0000 | [diff] [blame] | 148 | return u->getOpcode() == UnaryOperator::Deref; |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 149 | case ParenExprClass: |
Steve Naroff | f633d09 | 2007-04-25 19:01:39 +0000 | [diff] [blame] | 150 | return cast<ParenExpr>(this)->getSubExpr()->isLvalue(); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 151 | default: |
| 152 | return false; |
| 153 | } |
| 154 | } |