blob: 3bfeb06d1485b60647860474eebad1a882c2188c [file] [log] [blame]
Chris Lattner1b926492006-08-23 06:42:10 +00001//===--- 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 Lattner5e9a8782006-11-04 06:21:51 +000015#include "clang/AST/StmtVisitor.h"
Chris Lattnere165d942006-08-24 04:40:38 +000016#include "clang/Lex/IdentifierTable.h"
Chris Lattner1b926492006-08-23 06:42:10 +000017using namespace llvm;
18using namespace clang;
19
Chris Lattner0eedafe2006-08-24 04:56:27 +000020//===----------------------------------------------------------------------===//
21// Primary Expressions.
22//===----------------------------------------------------------------------===//
23
Steve Naroff408451b2007-02-26 22:17:12 +000024StringLiteral::StringLiteral(const char *strData, unsigned byteLength,
Steve Naroffe5aa9be2007-04-05 22:36:20 +000025 bool Wide, QualType t) :
Steve Narofff1e53692007-03-23 22:27:02 +000026 Expr(StringLiteralClass, t) {
Steve Naroffdf7855b2007-02-21 23:46:25 +000027 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattnerd3e98952006-10-06 05:22:26 +000028 char *AStrData = new char[byteLength];
29 memcpy(AStrData, strData, byteLength);
30 StrData = AStrData;
31 ByteLength = byteLength;
Chris Lattner882f7882006-11-04 18:52:07 +000032 IsWide = Wide;
Chris Lattnerd3e98952006-10-06 05:22:26 +000033}
34
Steve Naroffdf7855b2007-02-21 23:46:25 +000035StringLiteral::~StringLiteral() {
Chris Lattnerd3e98952006-10-06 05:22:26 +000036 delete[] StrData;
37}
38
Chris Lattner15768702006-11-05 23:54:51 +000039bool 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 Lattner1b926492006-08-23 06:42:10 +000049/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
50/// corresponds to, e.g. "sizeof" or "[pre]++".
51const char *UnaryOperator::getOpcodeStr(Opcode Op) {
52 switch (Op) {
Chris Lattnerc52b1182006-10-25 05:45:55 +000053 default: assert(0 && "Unknown unary operator");
Chris Lattner15768702006-11-05 23:54:51 +000054 case PostInc: return "++";
55 case PostDec: return "--";
56 case PreInc: return "++";
57 case PreDec: return "--";
Chris Lattner1b926492006-08-23 06:42:10 +000058 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 Lattner26115ac2006-08-24 06:10:04 +000066 case SizeOf: return "sizeof";
67 case AlignOf: return "alignof";
Chris Lattnerc52b1182006-10-25 05:45:55 +000068 case Extension: return "__extension__";
Chris Lattner1b926492006-08-23 06:42:10 +000069 }
70}
71
Chris Lattner0eedafe2006-08-24 04:56:27 +000072//===----------------------------------------------------------------------===//
73// Postfix Operators.
74//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +000075
Steve Naroffae4143e2007-04-26 20:39:23 +000076CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t)
77 : Expr(CallExprClass, t), Fn(fn), NumArgs(numargs) {
Chris Lattnere165d942006-08-24 04:40:38 +000078 Args = new Expr*[numargs];
79 for (unsigned i = 0; i != numargs; ++i)
80 Args[i] = args[i];
81}
82
Chris Lattner1b926492006-08-23 06:42:10 +000083/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
84/// corresponds to, e.g. "<<=".
85const 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 Naroff47500512007-04-19 23:00:49 +0000120
Steve Naroff17f76e02007-05-03 21:03:48 +0000121/// Nonarray expressions that can be lvalues:
Steve Naroff47500512007-04-19 23:00:49 +0000122/// - 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 Naroff35d85152007-05-07 00:24:15 +0000127/// - *e, the type of e cannot be a function type
Steve Naroff47500512007-04-19 23:00:49 +0000128/// - string-constant
129///
Steve Naroff17f76e02007-05-03 21:03:48 +0000130bool Expr::isModifiableLvalue() {
Steve Naroff47500512007-04-19 23:00:49 +0000131 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 Naroff17f76e02007-05-03 21:03:48 +0000138 if (const VarDecl *vd = dyn_cast<VarDecl>(d->getDecl()))
139 if (vd->getType().isModifiableLvalue())
140 return true;
Steve Naroff47500512007-04-19 23:00:49 +0000141 return false;
142 case MemberExprClass:
143 const MemberExpr *m = cast<MemberExpr>(this);
144 if (m->isArrow())
145 return true;
Steve Naroff17f76e02007-05-03 21:03:48 +0000146 return m->getBase()->isModifiableLvalue(); // make sure "." is an lvalue
Steve Naroff47500512007-04-19 23:00:49 +0000147 case UnaryOperatorClass:
148 const UnaryOperator *u = cast<UnaryOperator>(this);
Steve Naroff35d85152007-05-07 00:24:15 +0000149 return u->getOpcode() == UnaryOperator::Deref &&
150 u->getType().isModifiableLvalue(); // C99 6.5.3.2p4
Steve Naroff47500512007-04-19 23:00:49 +0000151 case ParenExprClass:
Steve Naroff17f76e02007-05-03 21:03:48 +0000152 return cast<ParenExpr>(this)->getSubExpr()->isModifiableLvalue();
Steve Naroff47500512007-04-19 23:00:49 +0000153 default:
154 return false;
155 }
156}
Steve Naroff218bc2b2007-05-04 21:54:46 +0000157
158bool Expr::isNullPointerConstant() const {
159 const IntegerLiteral *constant = dyn_cast<IntegerLiteral>(this);
160 if (!constant || constant->getValue() != 0)
161 return false;
162 return true;
163}