blob: 3d0cb7d81e62d9c07670fd3023c6b6d6042d0694 [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 Naroff475cca02007-05-14 17:19:29 +0000121/// 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 Naroff47500512007-04-19 23:00:49 +0000123/// - 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 Naroff35d85152007-05-07 00:24:15 +0000128/// - *e, the type of e cannot be a function type
Steve Naroff47500512007-04-19 23:00:49 +0000129/// - string-constant
130///
Steve Naroff475cca02007-05-14 17:19:29 +0000131bool 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 Naroff5dd642e2007-05-14 18:14:51 +0000138 // the type looks fine, now check the expression
Steve Naroff47500512007-04-19 23:00:49 +0000139 switch (getStmtClass()) {
Steve Naroff475cca02007-05-14 17:19:29 +0000140 case StringLiteralClass: // C99 6.5.1p4
Steve Naroff47500512007-04-19 23:00:49 +0000141 return true;
Steve Naroff5dd642e2007-05-14 18:14:51 +0000142 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
Steve Naroff47500512007-04-19 23:00:49 +0000143 return true;
Steve Naroff475cca02007-05-14 17:19:29 +0000144 case DeclRefExprClass: // C99 6.5.1p2
Steve Naroff5dd642e2007-05-14 18:14:51 +0000145 return isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl());
Steve Naroff475cca02007-05-14 17:19:29 +0000146 case MemberExprClass: // C99 6.5.2.3p4
Steve Naroff47500512007-04-19 23:00:49 +0000147 const MemberExpr *m = cast<MemberExpr>(this);
Steve Naroff5dd642e2007-05-14 18:14:51 +0000148 return m->isArrow() ? true : m->getBase()->isLvalue();
Steve Naroff475cca02007-05-14 17:19:29 +0000149 case UnaryOperatorClass: // C99 6.5.3p4
Steve Naroff5dd642e2007-05-14 18:14:51 +0000150 return cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref;
Steve Naroff475cca02007-05-14 17:19:29 +0000151 case ParenExprClass: // C99 6.5.1p5
152 return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
Steve Naroff47500512007-04-19 23:00:49 +0000153 default:
154 return false;
155 }
156}
Steve Naroff218bc2b2007-05-04 21:54:46 +0000157
Steve Naroff475cca02007-05-14 17:19:29 +0000158/// 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.
163bool 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 Naroff8eeeb132007-05-08 21:09:37 +0000178bool 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 Naroff043d45d2007-05-15 02:32:35 +0000188 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 Naroff8eeeb132007-05-08 21:09:37 +0000197 case BinaryOperatorClass:
Steve Naroff043d45d2007-05-15 02:32:35 +0000198 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 Naroff8eeeb132007-05-08 21:09:37 +0000205 case ParenExprClass:
206 return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr();
Steve Naroff043d45d2007-05-15 02:32:35 +0000207 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 Naroff8eeeb132007-05-08 21:09:37 +0000219 case SizeOfAlignOfTypeExprClass:
Steve Naroff043d45d2007-05-15 02:32:35 +0000220 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 Naroff8eeeb132007-05-08 21:09:37 +0000233 default:
234 return false;
235 }
236}
237
238bool 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 Naroff043d45d2007-05-15 02:32:35 +0000246 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 Naroff8eeeb132007-05-08 21:09:37 +0000255 case BinaryOperatorClass:
Steve Naroff043d45d2007-05-15 02:32:35 +0000256 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 Naroff8eeeb132007-05-08 21:09:37 +0000264 case ParenExprClass:
265 return cast<ParenExpr>(this)->getSubExpr()->isIntegerConstantExpr();
Steve Naroff043d45d2007-05-15 02:32:35 +0000266 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 Naroff8eeeb132007-05-08 21:09:37 +0000278 case SizeOfAlignOfTypeExprClass:
Steve Naroff043d45d2007-05-15 02:32:35 +0000279 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 Naroff8eeeb132007-05-08 21:09:37 +0000292 default:
293 return false;
294 }
295}
296
Steve Naroff218bc2b2007-05-04 21:54:46 +0000297bool Expr::isNullPointerConstant() const {
298 const IntegerLiteral *constant = dyn_cast<IntegerLiteral>(this);
299 if (!constant || constant->getValue() != 0)
300 return false;
301 return true;
302}