blob: 86498a568b1dafae27c62f43e5a78b910dcff98a [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.
195bool Expr::isConstantExpr(bool isIntConst) 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:
202 return isIntConst ? false : true;
Steve Naroff8eeeb132007-05-08 21:09:37 +0000203 case DeclRefExprClass:
204 return isa<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl());
205 case UnaryOperatorClass:
Steve Naroff043d45d2007-05-15 02:32:35 +0000206 const UnaryOperator *uop = cast<UnaryOperator>(this);
207 if (uop->isIncrementDecrementOp()) // C99 6.6p3
208 return false;
209 // C99 6.5.3.4p2: otherwise, the operand is *not* evaluated and the result
210 // is an integer constant. This effective ignores any subexpression that
211 // isn't actually a constant expression (what an odd language:-)
212 if (uop->isSizeOfAlignOfOp())
Steve Narofff8a28c52007-05-15 20:29:32 +0000213 return uop->getSubExpr()->getType()->isConstantSizeType();
214 return uop->getSubExpr()->isConstantExpr(isIntConst);
Steve Naroff8eeeb132007-05-08 21:09:37 +0000215 case BinaryOperatorClass:
Steve Naroff043d45d2007-05-15 02:32:35 +0000216 const BinaryOperator *bop = cast<BinaryOperator>(this);
217 // C99 6.6p3: shall not contain assignment, increment/decrement,
218 // function call, or comma operators, *except* when they are contained
219 // within a subexpression that is not evaluated.
220 if (bop->isAssignmentOp() || bop->getOpcode() == BinaryOperator::Comma)
221 return false;
Steve Narofff8a28c52007-05-15 20:29:32 +0000222 return bop->getLHS()->isConstantExpr(isIntConst) &&
223 bop->getRHS()->isConstantExpr(isIntConst);
Steve Naroff8eeeb132007-05-08 21:09:37 +0000224 case ParenExprClass:
225 return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr();
Steve Naroff043d45d2007-05-15 02:32:35 +0000226 case CastExprClass:
227 const CastExpr *castExpr = cast<CastExpr>(this);
228 // C99 6.6p6: shall only convert arithmetic types to integer types.
229 if (!castExpr->getSubExpr()->getType()->isArithmeticType())
230 return false;
231 if (!castExpr->getDestType()->isIntegerType())
232 return false;
233 // allow floating constants that are the immediate operands of casts.
234 if (castExpr->getSubExpr()->isConstantExpr() ||
235 isa<FloatingLiteral>(castExpr->getSubExpr()))
236 return true;
237 return false;
Steve Naroff8eeeb132007-05-08 21:09:37 +0000238 case SizeOfAlignOfTypeExprClass:
Steve Naroff043d45d2007-05-15 02:32:35 +0000239 const SizeOfAlignOfTypeExpr *sizeExpr = cast<SizeOfAlignOfTypeExpr>(this);
240 if (sizeExpr->isSizeOf())
241 return sizeExpr->getArgumentType()->isConstantSizeType();
242 return true; // alignof will always evaluate to a constant
243 case ConditionalOperatorClass:
Steve Naroff043d45d2007-05-15 02:32:35 +0000244 const ConditionalOperator *condExpr = cast<ConditionalOperator>(this);
Steve Narofff8a28c52007-05-15 20:29:32 +0000245 return condExpr->getCond()->isConstantExpr(isIntConst) &&
246 condExpr->getLHS()->isConstantExpr(isIntConst) &&
247 condExpr->getRHS()->isConstantExpr(isIntConst);
Steve Naroff8eeeb132007-05-08 21:09:37 +0000248 default:
249 return false;
250 }
251}
252
Steve Naroff218bc2b2007-05-04 21:54:46 +0000253bool Expr::isNullPointerConstant() const {
254 const IntegerLiteral *constant = dyn_cast<IntegerLiteral>(this);
255 if (!constant || constant->getValue() != 0)
256 return false;
257 return true;
258}