blob: 925bf768002cc2830967ac2249f0f13ff1f2b5bc [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 Naroff53f07dc2007-05-17 21:49:33 +000025 bool Wide, QualType t, SourceLocation firstLoc,
26 SourceLocation lastLoc) :
Steve Narofff1e53692007-03-23 22:27:02 +000027 Expr(StringLiteralClass, t) {
Steve Naroffdf7855b2007-02-21 23:46:25 +000028 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattnerd3e98952006-10-06 05:22:26 +000029 char *AStrData = new char[byteLength];
30 memcpy(AStrData, strData, byteLength);
31 StrData = AStrData;
32 ByteLength = byteLength;
Chris Lattner882f7882006-11-04 18:52:07 +000033 IsWide = Wide;
Steve Naroff53f07dc2007-05-17 21:49:33 +000034 firstTokLoc = firstLoc;
35 lastTokLoc = lastLoc;
Chris Lattnerd3e98952006-10-06 05:22:26 +000036}
37
Steve Naroffdf7855b2007-02-21 23:46:25 +000038StringLiteral::~StringLiteral() {
Chris Lattnerd3e98952006-10-06 05:22:26 +000039 delete[] StrData;
40}
41
Chris Lattner15768702006-11-05 23:54:51 +000042bool UnaryOperator::isPostfix(Opcode Op) {
43 switch (Op) {
44 case PostInc:
45 case PostDec:
46 return true;
47 default:
48 return false;
49 }
50}
51
Chris Lattner1b926492006-08-23 06:42:10 +000052/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
53/// corresponds to, e.g. "sizeof" or "[pre]++".
54const char *UnaryOperator::getOpcodeStr(Opcode Op) {
55 switch (Op) {
Chris Lattnerc52b1182006-10-25 05:45:55 +000056 default: assert(0 && "Unknown unary operator");
Chris Lattner15768702006-11-05 23:54:51 +000057 case PostInc: return "++";
58 case PostDec: return "--";
59 case PreInc: return "++";
60 case PreDec: return "--";
Chris Lattner1b926492006-08-23 06:42:10 +000061 case AddrOf: return "&";
62 case Deref: return "*";
63 case Plus: return "+";
64 case Minus: return "-";
65 case Not: return "~";
66 case LNot: return "!";
67 case Real: return "__real";
68 case Imag: return "__imag";
Chris Lattner26115ac2006-08-24 06:10:04 +000069 case SizeOf: return "sizeof";
70 case AlignOf: return "alignof";
Chris Lattnerc52b1182006-10-25 05:45:55 +000071 case Extension: return "__extension__";
Chris Lattner1b926492006-08-23 06:42:10 +000072 }
73}
74
Chris Lattner0eedafe2006-08-24 04:56:27 +000075//===----------------------------------------------------------------------===//
76// Postfix Operators.
77//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +000078
Steve Naroff509fe022007-05-17 01:16:00 +000079CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
80 SourceLocation l)
Steve Naroffae4143e2007-04-26 20:39:23 +000081 : Expr(CallExprClass, t), Fn(fn), NumArgs(numargs) {
Chris Lattnere165d942006-08-24 04:40:38 +000082 Args = new Expr*[numargs];
83 for (unsigned i = 0; i != numargs; ++i)
84 Args[i] = args[i];
Steve Naroff509fe022007-05-17 01:16:00 +000085 Loc = l;
Chris Lattnere165d942006-08-24 04:40:38 +000086}
87
Chris Lattner1b926492006-08-23 06:42:10 +000088/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
89/// corresponds to, e.g. "<<=".
90const char *BinaryOperator::getOpcodeStr(Opcode Op) {
91 switch (Op) {
92 default: assert(0 && "Unknown binary operator");
93 case Mul: return "*";
94 case Div: return "/";
95 case Rem: return "%";
96 case Add: return "+";
97 case Sub: return "-";
98 case Shl: return "<<";
99 case Shr: return ">>";
100 case LT: return "<";
101 case GT: return ">";
102 case LE: return "<=";
103 case GE: return ">=";
104 case EQ: return "==";
105 case NE: return "!=";
106 case And: return "&";
107 case Xor: return "^";
108 case Or: return "|";
109 case LAnd: return "&&";
110 case LOr: return "||";
111 case Assign: return "=";
112 case MulAssign: return "*=";
113 case DivAssign: return "/=";
114 case RemAssign: return "%=";
115 case AddAssign: return "+=";
116 case SubAssign: return "-=";
117 case ShlAssign: return "<<=";
118 case ShrAssign: return ">>=";
119 case AndAssign: return "&=";
120 case XorAssign: return "^=";
121 case OrAssign: return "|=";
122 case Comma: return ",";
123 }
124}
Steve Naroff47500512007-04-19 23:00:49 +0000125
Steve Naroff475cca02007-05-14 17:19:29 +0000126/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
127/// incomplete type other than void. Nonarray expressions that can be lvalues:
Steve Naroff47500512007-04-19 23:00:49 +0000128/// - name, where name must be a variable
129/// - e[i]
130/// - (e), where e must be an lvalue
131/// - e.name, where e must be an lvalue
132/// - e->name
Steve Naroff35d85152007-05-07 00:24:15 +0000133/// - *e, the type of e cannot be a function type
Steve Naroff47500512007-04-19 23:00:49 +0000134/// - string-constant
135///
Steve Naroff475cca02007-05-14 17:19:29 +0000136bool Expr::isLvalue() {
137 // first, check the type (C99 6.3.2.1)
138 if (!TR->isObjectType())
139 return false;
140 if (TR->isIncompleteType() && TR->isVoidType())
141 return false;
142
Steve Naroff5dd642e2007-05-14 18:14:51 +0000143 // the type looks fine, now check the expression
Steve Naroff47500512007-04-19 23:00:49 +0000144 switch (getStmtClass()) {
Steve Naroff475cca02007-05-14 17:19:29 +0000145 case StringLiteralClass: // C99 6.5.1p4
Steve Naroff47500512007-04-19 23:00:49 +0000146 return true;
Steve Naroff5dd642e2007-05-14 18:14:51 +0000147 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
Steve Naroff47500512007-04-19 23:00:49 +0000148 return true;
Steve Naroff475cca02007-05-14 17:19:29 +0000149 case DeclRefExprClass: // C99 6.5.1p2
Steve Naroff5dd642e2007-05-14 18:14:51 +0000150 return isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl());
Steve Naroff475cca02007-05-14 17:19:29 +0000151 case MemberExprClass: // C99 6.5.2.3p4
Steve Naroff47500512007-04-19 23:00:49 +0000152 const MemberExpr *m = cast<MemberExpr>(this);
Steve Naroff5dd642e2007-05-14 18:14:51 +0000153 return m->isArrow() ? true : m->getBase()->isLvalue();
Steve Naroff475cca02007-05-14 17:19:29 +0000154 case UnaryOperatorClass: // C99 6.5.3p4
Steve Naroff5dd642e2007-05-14 18:14:51 +0000155 return cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref;
Steve Naroff475cca02007-05-14 17:19:29 +0000156 case ParenExprClass: // C99 6.5.1p5
157 return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
Steve Naroff47500512007-04-19 23:00:49 +0000158 default:
159 return false;
160 }
161}
Steve Naroff218bc2b2007-05-04 21:54:46 +0000162
Steve Naroff475cca02007-05-14 17:19:29 +0000163/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
164/// does not have an incomplete type, does not have a const-qualified type, and
165/// if it is a structure or union, does not have any member (including,
166/// recursively, any member or element of all contained aggregates or unions)
167/// with a const-qualified type.
168bool Expr::isModifiableLvalue() {
169 if (!isLvalue())
170 return false;
171
172 if (TR.isConstQualified())
173 return false;
174 if (TR->isArrayType())
175 return false;
176 if (TR->isIncompleteType())
177 return false;
178 if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType()))
179 return r->isModifiableLvalue();
180 return true;
181}
182
Steve Narofff8a28c52007-05-15 20:29:32 +0000183/// isConstantExpr - this recursive routine will test if an expression is
184/// either a constant expression (isIntConst == false) or an integer constant
185/// expression (isIntConst == true). Note: With the introduction of VLA's in
186/// C99 the result of the sizeof operator is no longer always a constant
187/// expression. The generalization of the wording to include any subexpression
188/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
189/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
190/// "0 || f()" can be treated as a constant expression. In C90 this expression,
191/// occurring in a context requiring a constant, would have been a constraint
192/// violation. FIXME: This routine currently implements C90 semantics.
193/// To properly implement C99 semantics this routine will need to evaluate
194/// expressions involving operators previously mentioned.
Steve Naroff72cada02007-05-18 00:18:52 +0000195bool Expr::isConstantExpr(bool isIntConst, SourceLocation &loc) const {
Steve Naroff8eeeb132007-05-08 21:09:37 +0000196 switch (getStmtClass()) {
197 case IntegerLiteralClass:
Steve Naroff8eeeb132007-05-08 21:09:37 +0000198 case CharacterLiteralClass:
Steve Naroff8eeeb132007-05-08 21:09:37 +0000199 return true;
Steve Narofff8a28c52007-05-15 20:29:32 +0000200 case FloatingLiteralClass:
201 case StringLiteralClass:
Steve Naroff72cada02007-05-18 00:18:52 +0000202 if (!isIntConst)
203 return true;
204 loc = getLocStart();
205 return false;
Steve Naroff8eeeb132007-05-08 21:09:37 +0000206 case DeclRefExprClass:
Steve Naroff72cada02007-05-18 00:18:52 +0000207 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl()))
208 return true;
209 loc = getLocStart();
210 return false;
Steve Naroff8eeeb132007-05-08 21:09:37 +0000211 case UnaryOperatorClass:
Steve Naroff043d45d2007-05-15 02:32:35 +0000212 const UnaryOperator *uop = cast<UnaryOperator>(this);
Steve Naroff72cada02007-05-18 00:18:52 +0000213 if (uop->isIncrementDecrementOp()) { // C99 6.6p3
214 loc = getLocStart();
Steve Naroff043d45d2007-05-15 02:32:35 +0000215 return false;
Steve Naroff72cada02007-05-18 00:18:52 +0000216 }
Steve Naroff043d45d2007-05-15 02:32:35 +0000217 // C99 6.5.3.4p2: otherwise, the operand is *not* evaluated and the result
218 // is an integer constant. This effective ignores any subexpression that
219 // isn't actually a constant expression (what an odd language:-)
220 if (uop->isSizeOfAlignOfOp())
Steve Naroff72cada02007-05-18 00:18:52 +0000221 return uop->getSubExpr()->getType()->isConstantSizeType(loc);
222 return uop->getSubExpr()->isConstantExpr(isIntConst, loc);
Steve Naroff8eeeb132007-05-08 21:09:37 +0000223 case BinaryOperatorClass:
Steve Naroff043d45d2007-05-15 02:32:35 +0000224 const BinaryOperator *bop = cast<BinaryOperator>(this);
225 // C99 6.6p3: shall not contain assignment, increment/decrement,
226 // function call, or comma operators, *except* when they are contained
227 // within a subexpression that is not evaluated.
Steve Naroff72cada02007-05-18 00:18:52 +0000228 if (bop->isAssignmentOp() || bop->getOpcode() == BinaryOperator::Comma) {
229 loc = getLocStart();
Steve Naroff043d45d2007-05-15 02:32:35 +0000230 return false;
Steve Naroff72cada02007-05-18 00:18:52 +0000231 }
232 return bop->getLHS()->isConstantExpr(isIntConst, loc) &&
233 bop->getRHS()->isConstantExpr(isIntConst, loc);
Steve Naroff8eeeb132007-05-08 21:09:37 +0000234 case ParenExprClass:
Steve Naroff72cada02007-05-18 00:18:52 +0000235 return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(isIntConst, loc);
Steve Naroff043d45d2007-05-15 02:32:35 +0000236 case CastExprClass:
237 const CastExpr *castExpr = cast<CastExpr>(this);
238 // C99 6.6p6: shall only convert arithmetic types to integer types.
Steve Naroff72cada02007-05-18 00:18:52 +0000239 if (!castExpr->getSubExpr()->getType()->isArithmeticType()) {
240 loc = castExpr->getSubExpr()->getLocStart();
Steve Naroff043d45d2007-05-15 02:32:35 +0000241 return false;
Steve Naroff72cada02007-05-18 00:18:52 +0000242 }
243 if (!castExpr->getDestType()->isIntegerType()) {
244 loc = getLocStart();
245 return false;
246 }
Steve Naroff043d45d2007-05-15 02:32:35 +0000247 // allow floating constants that are the immediate operands of casts.
Steve Naroff72cada02007-05-18 00:18:52 +0000248 if (castExpr->getSubExpr()->isConstantExpr(isIntConst, loc) ||
Steve Naroff043d45d2007-05-15 02:32:35 +0000249 isa<FloatingLiteral>(castExpr->getSubExpr()))
250 return true;
Steve Naroff72cada02007-05-18 00:18:52 +0000251 loc = getLocStart();
Steve Naroff043d45d2007-05-15 02:32:35 +0000252 return false;
Steve Naroff8eeeb132007-05-08 21:09:37 +0000253 case SizeOfAlignOfTypeExprClass:
Steve Naroff043d45d2007-05-15 02:32:35 +0000254 const SizeOfAlignOfTypeExpr *sizeExpr = cast<SizeOfAlignOfTypeExpr>(this);
255 if (sizeExpr->isSizeOf())
Steve Naroff72cada02007-05-18 00:18:52 +0000256 return sizeExpr->getArgumentType()->isConstantSizeType(loc);
Steve Naroff043d45d2007-05-15 02:32:35 +0000257 return true; // alignof will always evaluate to a constant
258 case ConditionalOperatorClass:
Steve Naroff043d45d2007-05-15 02:32:35 +0000259 const ConditionalOperator *condExpr = cast<ConditionalOperator>(this);
Steve Naroff72cada02007-05-18 00:18:52 +0000260 return condExpr->getCond()->isConstantExpr(isIntConst, loc) &&
261 condExpr->getLHS()->isConstantExpr(isIntConst, loc) &&
262 condExpr->getRHS()->isConstantExpr(isIntConst, loc);
263 default:
264 loc = getLocStart();
Steve Naroff8eeeb132007-05-08 21:09:37 +0000265 return false;
266 }
267}
268
Steve Naroffada7d422007-05-20 17:54:12 +0000269// C99 6.3.2.3p3: FIXME: If we have an integer constant expression, we need
270// to *evaluate* it and test for the value 0. The current code is too
271// simplistic...it only allows for the integer literal "0".
272// For example, the following is valid code:
273//
274// void test1() { *(n ? p : (void *)(7-7)) = 1; }
275//
Steve Naroff218bc2b2007-05-04 21:54:46 +0000276bool Expr::isNullPointerConstant() const {
Steve Naroffada7d422007-05-20 17:54:12 +0000277 const IntegerLiteral *constant = 0;
278
279 switch (getStmtClass()) {
280 case IntegerLiteralClass:
281 constant = cast<IntegerLiteral>(this);
282 break;
283 case CastExprClass:
284 const CastExpr *cExpr = cast<CastExpr>(this);
285 if (const PointerType *p = dyn_cast<PointerType>(cExpr->getDestType())) {
286 QualType t = p->getPointeeType();
287 // the type needs to be "void *" (no qualifiers are permitted)
288 if (!t.getQualifiers() && t->isVoidType())
289 constant = dyn_cast<IntegerLiteral>(cExpr->getSubExpr());
290 }
291 break;
292 default:
293 break;
294 }
Steve Naroff218bc2b2007-05-04 21:54:46 +0000295 if (!constant || constant->getValue() != 0)
296 return false;
297 return true;
298}