blob: 3cb04ea0170f116c68042737b17b2ab13b735a26 [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 Naroff509fe022007-05-17 01:16:00 +000025 bool Wide, QualType t, SourceLocation l) :
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;
Steve Naroff509fe022007-05-17 01:16:00 +000033 Loc = l;
Chris Lattnerd3e98952006-10-06 05:22:26 +000034}
35
Steve Naroffdf7855b2007-02-21 23:46:25 +000036StringLiteral::~StringLiteral() {
Chris Lattnerd3e98952006-10-06 05:22:26 +000037 delete[] StrData;
38}
39
Chris Lattner15768702006-11-05 23:54:51 +000040bool UnaryOperator::isPostfix(Opcode Op) {
41 switch (Op) {
42 case PostInc:
43 case PostDec:
44 return true;
45 default:
46 return false;
47 }
48}
49
Chris Lattner1b926492006-08-23 06:42:10 +000050/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
51/// corresponds to, e.g. "sizeof" or "[pre]++".
52const char *UnaryOperator::getOpcodeStr(Opcode Op) {
53 switch (Op) {
Chris Lattnerc52b1182006-10-25 05:45:55 +000054 default: assert(0 && "Unknown unary operator");
Chris Lattner15768702006-11-05 23:54:51 +000055 case PostInc: return "++";
56 case PostDec: return "--";
57 case PreInc: return "++";
58 case PreDec: return "--";
Chris Lattner1b926492006-08-23 06:42:10 +000059 case AddrOf: return "&";
60 case Deref: return "*";
61 case Plus: return "+";
62 case Minus: return "-";
63 case Not: return "~";
64 case LNot: return "!";
65 case Real: return "__real";
66 case Imag: return "__imag";
Chris Lattner26115ac2006-08-24 06:10:04 +000067 case SizeOf: return "sizeof";
68 case AlignOf: return "alignof";
Chris Lattnerc52b1182006-10-25 05:45:55 +000069 case Extension: return "__extension__";
Chris Lattner1b926492006-08-23 06:42:10 +000070 }
71}
72
Chris Lattner0eedafe2006-08-24 04:56:27 +000073//===----------------------------------------------------------------------===//
74// Postfix Operators.
75//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +000076
Steve Naroff509fe022007-05-17 01:16:00 +000077CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
78 SourceLocation l)
Steve Naroffae4143e2007-04-26 20:39:23 +000079 : Expr(CallExprClass, t), Fn(fn), NumArgs(numargs) {
Chris Lattnere165d942006-08-24 04:40:38 +000080 Args = new Expr*[numargs];
81 for (unsigned i = 0; i != numargs; ++i)
82 Args[i] = args[i];
Steve Naroff509fe022007-05-17 01:16:00 +000083 Loc = l;
Chris Lattnere165d942006-08-24 04:40:38 +000084}
85
Chris Lattner1b926492006-08-23 06:42:10 +000086/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
87/// corresponds to, e.g. "<<=".
88const char *BinaryOperator::getOpcodeStr(Opcode Op) {
89 switch (Op) {
90 default: assert(0 && "Unknown binary operator");
91 case Mul: return "*";
92 case Div: return "/";
93 case Rem: return "%";
94 case Add: return "+";
95 case Sub: return "-";
96 case Shl: return "<<";
97 case Shr: return ">>";
98 case LT: return "<";
99 case GT: return ">";
100 case LE: return "<=";
101 case GE: return ">=";
102 case EQ: return "==";
103 case NE: return "!=";
104 case And: return "&";
105 case Xor: return "^";
106 case Or: return "|";
107 case LAnd: return "&&";
108 case LOr: return "||";
109 case Assign: return "=";
110 case MulAssign: return "*=";
111 case DivAssign: return "/=";
112 case RemAssign: return "%=";
113 case AddAssign: return "+=";
114 case SubAssign: return "-=";
115 case ShlAssign: return "<<=";
116 case ShrAssign: return ">>=";
117 case AndAssign: return "&=";
118 case XorAssign: return "^=";
119 case OrAssign: return "|=";
120 case Comma: return ",";
121 }
122}
Steve Naroff47500512007-04-19 23:00:49 +0000123
Steve Naroff475cca02007-05-14 17:19:29 +0000124/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
125/// incomplete type other than void. Nonarray expressions that can be lvalues:
Steve Naroff47500512007-04-19 23:00:49 +0000126/// - name, where name must be a variable
127/// - e[i]
128/// - (e), where e must be an lvalue
129/// - e.name, where e must be an lvalue
130/// - e->name
Steve Naroff35d85152007-05-07 00:24:15 +0000131/// - *e, the type of e cannot be a function type
Steve Naroff47500512007-04-19 23:00:49 +0000132/// - string-constant
133///
Steve Naroff475cca02007-05-14 17:19:29 +0000134bool Expr::isLvalue() {
135 // first, check the type (C99 6.3.2.1)
136 if (!TR->isObjectType())
137 return false;
138 if (TR->isIncompleteType() && TR->isVoidType())
139 return false;
140
Steve Naroff5dd642e2007-05-14 18:14:51 +0000141 // the type looks fine, now check the expression
Steve Naroff47500512007-04-19 23:00:49 +0000142 switch (getStmtClass()) {
Steve Naroff475cca02007-05-14 17:19:29 +0000143 case StringLiteralClass: // C99 6.5.1p4
Steve Naroff47500512007-04-19 23:00:49 +0000144 return true;
Steve Naroff5dd642e2007-05-14 18:14:51 +0000145 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
Steve Naroff47500512007-04-19 23:00:49 +0000146 return true;
Steve Naroff475cca02007-05-14 17:19:29 +0000147 case DeclRefExprClass: // C99 6.5.1p2
Steve Naroff5dd642e2007-05-14 18:14:51 +0000148 return isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl());
Steve Naroff475cca02007-05-14 17:19:29 +0000149 case MemberExprClass: // C99 6.5.2.3p4
Steve Naroff47500512007-04-19 23:00:49 +0000150 const MemberExpr *m = cast<MemberExpr>(this);
Steve Naroff5dd642e2007-05-14 18:14:51 +0000151 return m->isArrow() ? true : m->getBase()->isLvalue();
Steve Naroff475cca02007-05-14 17:19:29 +0000152 case UnaryOperatorClass: // C99 6.5.3p4
Steve Naroff5dd642e2007-05-14 18:14:51 +0000153 return cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref;
Steve Naroff475cca02007-05-14 17:19:29 +0000154 case ParenExprClass: // C99 6.5.1p5
155 return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
Steve Naroff47500512007-04-19 23:00:49 +0000156 default:
157 return false;
158 }
159}
Steve Naroff218bc2b2007-05-04 21:54:46 +0000160
Steve Naroff475cca02007-05-14 17:19:29 +0000161/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
162/// does not have an incomplete type, does not have a const-qualified type, and
163/// if it is a structure or union, does not have any member (including,
164/// recursively, any member or element of all contained aggregates or unions)
165/// with a const-qualified type.
166bool Expr::isModifiableLvalue() {
167 if (!isLvalue())
168 return false;
169
170 if (TR.isConstQualified())
171 return false;
172 if (TR->isArrayType())
173 return false;
174 if (TR->isIncompleteType())
175 return false;
176 if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType()))
177 return r->isModifiableLvalue();
178 return true;
179}
180
Steve Narofff8a28c52007-05-15 20:29:32 +0000181/// isConstantExpr - this recursive routine will test if an expression is
182/// either a constant expression (isIntConst == false) or an integer constant
183/// expression (isIntConst == true). Note: With the introduction of VLA's in
184/// C99 the result of the sizeof operator is no longer always a constant
185/// expression. The generalization of the wording to include any subexpression
186/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
187/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
188/// "0 || f()" can be treated as a constant expression. In C90 this expression,
189/// occurring in a context requiring a constant, would have been a constraint
190/// violation. FIXME: This routine currently implements C90 semantics.
191/// To properly implement C99 semantics this routine will need to evaluate
192/// expressions involving operators previously mentioned.
193bool Expr::isConstantExpr(bool isIntConst) const {
Steve Naroff8eeeb132007-05-08 21:09:37 +0000194 switch (getStmtClass()) {
195 case IntegerLiteralClass:
Steve Naroff8eeeb132007-05-08 21:09:37 +0000196 case CharacterLiteralClass:
Steve Naroff8eeeb132007-05-08 21:09:37 +0000197 return true;
Steve Narofff8a28c52007-05-15 20:29:32 +0000198 case FloatingLiteralClass:
199 case StringLiteralClass:
200 return isIntConst ? false : true;
Steve Naroff8eeeb132007-05-08 21:09:37 +0000201 case DeclRefExprClass:
202 return isa<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl());
203 case UnaryOperatorClass:
Steve Naroff043d45d2007-05-15 02:32:35 +0000204 const UnaryOperator *uop = cast<UnaryOperator>(this);
205 if (uop->isIncrementDecrementOp()) // C99 6.6p3
206 return false;
207 // C99 6.5.3.4p2: otherwise, the operand is *not* evaluated and the result
208 // is an integer constant. This effective ignores any subexpression that
209 // isn't actually a constant expression (what an odd language:-)
210 if (uop->isSizeOfAlignOfOp())
Steve Narofff8a28c52007-05-15 20:29:32 +0000211 return uop->getSubExpr()->getType()->isConstantSizeType();
212 return uop->getSubExpr()->isConstantExpr(isIntConst);
Steve Naroff8eeeb132007-05-08 21:09:37 +0000213 case BinaryOperatorClass:
Steve Naroff043d45d2007-05-15 02:32:35 +0000214 const BinaryOperator *bop = cast<BinaryOperator>(this);
215 // C99 6.6p3: shall not contain assignment, increment/decrement,
216 // function call, or comma operators, *except* when they are contained
217 // within a subexpression that is not evaluated.
218 if (bop->isAssignmentOp() || bop->getOpcode() == BinaryOperator::Comma)
219 return false;
Steve Narofff8a28c52007-05-15 20:29:32 +0000220 return bop->getLHS()->isConstantExpr(isIntConst) &&
221 bop->getRHS()->isConstantExpr(isIntConst);
Steve Naroff8eeeb132007-05-08 21:09:37 +0000222 case ParenExprClass:
223 return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr();
Steve Naroff043d45d2007-05-15 02:32:35 +0000224 case CastExprClass:
225 const CastExpr *castExpr = cast<CastExpr>(this);
226 // C99 6.6p6: shall only convert arithmetic types to integer types.
227 if (!castExpr->getSubExpr()->getType()->isArithmeticType())
228 return false;
229 if (!castExpr->getDestType()->isIntegerType())
230 return false;
231 // allow floating constants that are the immediate operands of casts.
232 if (castExpr->getSubExpr()->isConstantExpr() ||
233 isa<FloatingLiteral>(castExpr->getSubExpr()))
234 return true;
235 return false;
Steve Naroff8eeeb132007-05-08 21:09:37 +0000236 case SizeOfAlignOfTypeExprClass:
Steve Naroff043d45d2007-05-15 02:32:35 +0000237 const SizeOfAlignOfTypeExpr *sizeExpr = cast<SizeOfAlignOfTypeExpr>(this);
238 if (sizeExpr->isSizeOf())
239 return sizeExpr->getArgumentType()->isConstantSizeType();
240 return true; // alignof will always evaluate to a constant
241 case ConditionalOperatorClass:
Steve Naroff043d45d2007-05-15 02:32:35 +0000242 const ConditionalOperator *condExpr = cast<ConditionalOperator>(this);
Steve Narofff8a28c52007-05-15 20:29:32 +0000243 return condExpr->getCond()->isConstantExpr(isIntConst) &&
244 condExpr->getLHS()->isConstantExpr(isIntConst) &&
245 condExpr->getRHS()->isConstantExpr(isIntConst);
Steve Naroff8eeeb132007-05-08 21:09:37 +0000246 default:
247 return false;
248 }
249}
250
Steve Naroff218bc2b2007-05-04 21:54:46 +0000251bool Expr::isNullPointerConstant() const {
252 const IntegerLiteral *constant = dyn_cast<IntegerLiteral>(this);
253 if (!constant || constant->getValue() != 0)
254 return false;
255 return true;
256}