blob: 0f6ac7492b7fadd6a8a21703d88c81e85835b684 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner2eadfb62007-07-15 23:32:58 +000015#include "clang/AST/ASTContext.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/AST/StmtVisitor.h"
17#include "clang/Lex/IdentifierTable.h"
18using namespace clang;
19
20//===----------------------------------------------------------------------===//
21// Primary Expressions.
22//===----------------------------------------------------------------------===//
23
24StringLiteral::StringLiteral(const char *strData, unsigned byteLength,
25 bool Wide, QualType t, SourceLocation firstLoc,
26 SourceLocation lastLoc) :
27 Expr(StringLiteralClass, t) {
28 // OPTIMIZE: could allocate this appended to the StringLiteral.
29 char *AStrData = new char[byteLength];
30 memcpy(AStrData, strData, byteLength);
31 StrData = AStrData;
32 ByteLength = byteLength;
33 IsWide = Wide;
34 firstTokLoc = firstLoc;
35 lastTokLoc = lastLoc;
36}
37
38StringLiteral::~StringLiteral() {
39 delete[] StrData;
40}
41
42bool UnaryOperator::isPostfix(Opcode Op) {
43 switch (Op) {
44 case PostInc:
45 case PostDec:
46 return true;
47 default:
48 return false;
49 }
50}
51
52/// 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) {
56 default: assert(0 && "Unknown unary operator");
57 case PostInc: return "++";
58 case PostDec: return "--";
59 case PreInc: return "++";
60 case PreDec: return "--";
61 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";
69 case SizeOf: return "sizeof";
70 case AlignOf: return "alignof";
71 case Extension: return "__extension__";
72 }
73}
74
75//===----------------------------------------------------------------------===//
76// Postfix Operators.
77//===----------------------------------------------------------------------===//
78
79CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
80 SourceLocation rparenloc)
Ted Kremenek77ed8e42007-08-24 18:13:47 +000081 : Expr(CallExprClass, t), NumArgs(numargs) {
82 SubExprs = new Expr*[numargs+1];
83 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +000084 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +000085 SubExprs[i+ARGS_START] = args[i];
Reid Spencer5f016e22007-07-11 17:01:13 +000086 RParenLoc = rparenloc;
87}
88
Steve Naroff13b7c5f2007-08-08 22:15:55 +000089bool CallExpr::isBuiltinClassifyType(llvm::APSInt &Result) const {
90 // The following enum mimics gcc's internal "typeclass.h" file.
91 enum gcc_type_class {
92 no_type_class = -1,
93 void_type_class, integer_type_class, char_type_class,
94 enumeral_type_class, boolean_type_class,
95 pointer_type_class, reference_type_class, offset_type_class,
96 real_type_class, complex_type_class,
97 function_type_class, method_type_class,
98 record_type_class, union_type_class,
99 array_type_class, string_type_class,
100 lang_type_class
101 };
102 Result.setIsSigned(true);
103
104 // All simple function calls (e.g. func()) are implicitly cast to pointer to
105 // function. As a result, we try and obtain the DeclRefExpr from the
106 // ImplicitCastExpr.
107 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
108 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
109 return false;
110 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
111 if (!DRE)
112 return false;
113
114 // We have a DeclRefExpr.
115 if (strcmp(DRE->getDecl()->getName(), "__builtin_classify_type") == 0) {
116 // If no argument was supplied, default to "no_type_class". This isn't
117 // ideal, however it's what gcc does.
118 Result = static_cast<uint64_t>(no_type_class);
119 if (NumArgs >= 1) {
120 QualType argType = getArg(0)->getType();
121
122 if (argType->isVoidType())
123 Result = void_type_class;
124 else if (argType->isEnumeralType())
125 Result = enumeral_type_class;
126 else if (argType->isBooleanType())
127 Result = boolean_type_class;
128 else if (argType->isCharType())
129 Result = string_type_class; // gcc doesn't appear to use char_type_class
130 else if (argType->isIntegerType())
131 Result = integer_type_class;
132 else if (argType->isPointerType())
133 Result = pointer_type_class;
134 else if (argType->isReferenceType())
135 Result = reference_type_class;
136 else if (argType->isRealType())
137 Result = real_type_class;
138 else if (argType->isComplexType())
139 Result = complex_type_class;
140 else if (argType->isFunctionType())
141 Result = function_type_class;
142 else if (argType->isStructureType())
143 Result = record_type_class;
144 else if (argType->isUnionType())
145 Result = union_type_class;
146 else if (argType->isArrayType())
147 Result = array_type_class;
148 else if (argType->isUnionType())
149 Result = union_type_class;
150 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
151 assert(1 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
152 }
153 return true;
154 }
155 return false;
156}
157
Reid Spencer5f016e22007-07-11 17:01:13 +0000158/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
159/// corresponds to, e.g. "<<=".
160const char *BinaryOperator::getOpcodeStr(Opcode Op) {
161 switch (Op) {
162 default: assert(0 && "Unknown binary operator");
163 case Mul: return "*";
164 case Div: return "/";
165 case Rem: return "%";
166 case Add: return "+";
167 case Sub: return "-";
168 case Shl: return "<<";
169 case Shr: return ">>";
170 case LT: return "<";
171 case GT: return ">";
172 case LE: return "<=";
173 case GE: return ">=";
174 case EQ: return "==";
175 case NE: return "!=";
176 case And: return "&";
177 case Xor: return "^";
178 case Or: return "|";
179 case LAnd: return "&&";
180 case LOr: return "||";
181 case Assign: return "=";
182 case MulAssign: return "*=";
183 case DivAssign: return "/=";
184 case RemAssign: return "%=";
185 case AddAssign: return "+=";
186 case SubAssign: return "-=";
187 case ShlAssign: return "<<=";
188 case ShrAssign: return ">>=";
189 case AndAssign: return "&=";
190 case XorAssign: return "^=";
191 case OrAssign: return "|=";
192 case Comma: return ",";
193 }
194}
195
196
197//===----------------------------------------------------------------------===//
198// Generic Expression Routines
199//===----------------------------------------------------------------------===//
200
201/// hasLocalSideEffect - Return true if this immediate expression has side
202/// effects, not counting any sub-expressions.
203bool Expr::hasLocalSideEffect() const {
204 switch (getStmtClass()) {
205 default:
206 return false;
207 case ParenExprClass:
208 return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect();
209 case UnaryOperatorClass: {
210 const UnaryOperator *UO = cast<UnaryOperator>(this);
211
212 switch (UO->getOpcode()) {
213 default: return false;
214 case UnaryOperator::PostInc:
215 case UnaryOperator::PostDec:
216 case UnaryOperator::PreInc:
217 case UnaryOperator::PreDec:
218 return true; // ++/--
219
220 case UnaryOperator::Deref:
221 // Dereferencing a volatile pointer is a side-effect.
222 return getType().isVolatileQualified();
223 case UnaryOperator::Real:
224 case UnaryOperator::Imag:
225 // accessing a piece of a volatile complex is a side-effect.
226 return UO->getSubExpr()->getType().isVolatileQualified();
227
228 case UnaryOperator::Extension:
229 return UO->getSubExpr()->hasLocalSideEffect();
230 }
231 }
232 case BinaryOperatorClass:
233 return cast<BinaryOperator>(this)->isAssignmentOp();
Chris Lattnereb14fe82007-08-25 02:00:02 +0000234 case CompoundAssignOperatorClass:
Chris Lattner1f683e92007-08-25 01:55:00 +0000235 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000236
237 case MemberExprClass:
238 case ArraySubscriptExprClass:
239 // If the base pointer or element is to a volatile pointer/field, accessing
240 // if is a side effect.
241 return getType().isVolatileQualified();
242
243 case CallExprClass:
244 // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }"
245 // should warn.
246 return true;
247
248 case CastExprClass:
249 // If this is a cast to void, check the operand. Otherwise, the result of
250 // the cast is unused.
251 if (getType()->isVoidType())
252 return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect();
253 return false;
254 }
255}
256
257/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
258/// incomplete type other than void. Nonarray expressions that can be lvalues:
259/// - name, where name must be a variable
260/// - e[i]
261/// - (e), where e must be an lvalue
262/// - e.name, where e must be an lvalue
263/// - e->name
264/// - *e, the type of e cannot be a function type
265/// - string-constant
Bill Wendling08ad47c2007-07-17 03:52:31 +0000266/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +0000267///
Bill Wendlingca51c972007-07-16 07:07:56 +0000268Expr::isLvalueResult Expr::isLvalue() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000269 // first, check the type (C99 6.3.2.1)
Chris Lattnercb4f9a62007-07-21 05:33:26 +0000270 if (TR->isFunctionType()) // from isObjectType()
Reid Spencer5f016e22007-07-11 17:01:13 +0000271 return LV_NotObjectType;
272
Steve Naroff731ec572007-07-21 13:32:03 +0000273 if (TR->isVoidType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000274 return LV_IncompleteVoidType;
Bill Wendling08ad47c2007-07-17 03:52:31 +0000275
Chris Lattnercb4f9a62007-07-21 05:33:26 +0000276 if (TR->isReferenceType()) // C++ [expr]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000277 return LV_Valid;
278
Reid Spencer5f016e22007-07-11 17:01:13 +0000279 // the type looks fine, now check the expression
280 switch (getStmtClass()) {
281 case StringLiteralClass: // C99 6.5.1p4
282 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
283 // For vectors, make sure base is an lvalue (i.e. not a function call).
284 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
285 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue();
286 return LV_Valid;
287 case DeclRefExprClass: // C99 6.5.1p2
288 if (isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl()))
289 return LV_Valid;
290 break;
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000291 case MemberExprClass: { // C99 6.5.2.3p4
Reid Spencer5f016e22007-07-11 17:01:13 +0000292 const MemberExpr *m = cast<MemberExpr>(this);
293 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue();
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000294 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 case UnaryOperatorClass: // C99 6.5.3p4
296 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
297 return LV_Valid;
298 break;
299 case ParenExprClass: // C99 6.5.1p5
300 return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
Chris Lattner6481a572007-08-03 17:31:20 +0000301 case OCUVectorElementExprClass:
302 if (cast<OCUVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +0000303 return LV_DuplicateVectorComponents;
304 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 default:
306 break;
307 }
308 return LV_InvalidExpression;
309}
310
311/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
312/// does not have an incomplete type, does not have a const-qualified type, and
313/// if it is a structure or union, does not have any member (including,
314/// recursively, any member or element of all contained aggregates or unions)
315/// with a const-qualified type.
Bill Wendlingca51c972007-07-16 07:07:56 +0000316Expr::isModifiableLvalueResult Expr::isModifiableLvalue() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000317 isLvalueResult lvalResult = isLvalue();
318
319 switch (lvalResult) {
320 case LV_Valid: break;
321 case LV_NotObjectType: return MLV_NotObjectType;
322 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +0000323 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Reid Spencer5f016e22007-07-11 17:01:13 +0000324 case LV_InvalidExpression: return MLV_InvalidExpression;
325 }
326 if (TR.isConstQualified())
327 return MLV_ConstQualified;
328 if (TR->isArrayType())
329 return MLV_ArrayType;
330 if (TR->isIncompleteType())
331 return MLV_IncompleteType;
332
333 if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) {
334 if (r->hasConstFields())
335 return MLV_ConstQualified;
336 }
337 return MLV_Valid;
338}
339
340/// isIntegerConstantExpr - this recursive routine will test if an expression is
341/// an integer constant expression. Note: With the introduction of VLA's in
342/// C99 the result of the sizeof operator is no longer always a constant
343/// expression. The generalization of the wording to include any subexpression
344/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
345/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
346/// "0 || f()" can be treated as a constant expression. In C90 this expression,
347/// occurring in a context requiring a constant, would have been a constraint
348/// violation. FIXME: This routine currently implements C90 semantics.
349/// To properly implement C99 semantics this routine will need to evaluate
350/// expressions involving operators previously mentioned.
351
352/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
353/// comma, etc
354///
355/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
356/// permit this.
Chris Lattnerce0afc02007-07-18 05:21:20 +0000357///
358/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
359/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
360/// cast+dereference.
Chris Lattner590b6642007-07-15 23:26:56 +0000361bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
362 SourceLocation *Loc, bool isEvaluated) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000363 switch (getStmtClass()) {
364 default:
365 if (Loc) *Loc = getLocStart();
366 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000367 case ParenExprClass:
368 return cast<ParenExpr>(this)->getSubExpr()->
Chris Lattner590b6642007-07-15 23:26:56 +0000369 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
Reid Spencer5f016e22007-07-11 17:01:13 +0000370 case IntegerLiteralClass:
371 Result = cast<IntegerLiteral>(this)->getValue();
372 break;
Chris Lattner2eadfb62007-07-15 23:32:58 +0000373 case CharacterLiteralClass: {
374 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
375 Result.zextOrTrunc(Ctx.getTypeSize(getType(), CL->getLoc()));
376 Result = CL->getValue();
Chris Lattnerf0fbcb32007-07-16 21:04:56 +0000377 Result.setIsUnsigned(!getType()->isSignedIntegerType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000378 break;
Chris Lattner2eadfb62007-07-15 23:32:58 +0000379 }
Steve Naroff7b658aa2007-08-02 04:09:23 +0000380 case TypesCompatibleExprClass: {
381 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
382 Result.zextOrTrunc(Ctx.getTypeSize(getType(), TCE->getLocStart()));
383 Result = TCE->typesAreCompatible();
Steve Naroff389cecc2007-08-02 00:13:27 +0000384 break;
Steve Naroff7b658aa2007-08-02 04:09:23 +0000385 }
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000386 case CallExprClass: {
387 const CallExpr *CE = cast<CallExpr>(this);
388 Result.zextOrTrunc(Ctx.getTypeSize(getType(), CE->getLocStart()));
389 if (CE->isBuiltinClassifyType(Result))
390 break;
391 if (Loc) *Loc = getLocStart();
392 return false;
393 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000394 case DeclRefExprClass:
395 if (const EnumConstantDecl *D =
396 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
397 Result = D->getInitVal();
398 break;
399 }
400 if (Loc) *Loc = getLocStart();
401 return false;
402 case UnaryOperatorClass: {
403 const UnaryOperator *Exp = cast<UnaryOperator>(this);
404
405 // Get the operand value. If this is sizeof/alignof, do not evalute the
406 // operand. This affects C99 6.6p3.
Chris Lattner602dafd2007-08-23 21:42:50 +0000407 if (!Exp->isSizeOfAlignOfOp() &&
408 !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000409 return false;
410
411 switch (Exp->getOpcode()) {
412 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
413 // See C99 6.6p3.
414 default:
415 if (Loc) *Loc = Exp->getOperatorLoc();
416 return false;
417 case UnaryOperator::Extension:
Chris Lattner76e773a2007-07-18 18:38:36 +0000418 return true; // FIXME: this is wrong.
Reid Spencer5f016e22007-07-11 17:01:13 +0000419 case UnaryOperator::SizeOf:
420 case UnaryOperator::AlignOf:
421 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattner590b6642007-07-15 23:26:56 +0000422 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx, Loc))
Reid Spencer5f016e22007-07-11 17:01:13 +0000423 return false;
424
Chris Lattner76e773a2007-07-18 18:38:36 +0000425 // Return the result in the right width.
426 Result.zextOrTrunc(Ctx.getTypeSize(getType(), Exp->getOperatorLoc()));
427
428 // Get information about the size or align.
429 if (Exp->getOpcode() == UnaryOperator::SizeOf)
430 Result = Ctx.getTypeSize(Exp->getSubExpr()->getType(),
431 Exp->getOperatorLoc());
432 else
433 Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType(),
434 Exp->getOperatorLoc());
Reid Spencer5f016e22007-07-11 17:01:13 +0000435 break;
436 case UnaryOperator::LNot: {
437 bool Val = Result != 0;
Chris Lattner76e773a2007-07-18 18:38:36 +0000438 Result.zextOrTrunc(Ctx.getTypeSize(getType(), Exp->getOperatorLoc()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000439 Result = Val;
440 break;
441 }
442 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000443 break;
444 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000445 Result = -Result;
446 break;
447 case UnaryOperator::Not:
Reid Spencer5f016e22007-07-11 17:01:13 +0000448 Result = ~Result;
449 break;
450 }
451 break;
452 }
453 case SizeOfAlignOfTypeExprClass: {
454 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
455 // alignof always evaluates to a constant.
Chris Lattner590b6642007-07-15 23:26:56 +0000456 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx,Loc))
Reid Spencer5f016e22007-07-11 17:01:13 +0000457 return false;
458
Chris Lattner76e773a2007-07-18 18:38:36 +0000459 // Return the result in the right width.
460 Result.zextOrTrunc(Ctx.getTypeSize(getType(), Exp->getOperatorLoc()));
461
462 // Get information about the size or align.
463 if (Exp->isSizeOf())
464 Result = Ctx.getTypeSize(Exp->getArgumentType(), Exp->getOperatorLoc());
465 else
466 Result = Ctx.getTypeAlign(Exp->getArgumentType(), Exp->getOperatorLoc());
Reid Spencer5f016e22007-07-11 17:01:13 +0000467 break;
468 }
469 case BinaryOperatorClass: {
470 const BinaryOperator *Exp = cast<BinaryOperator>(this);
471
472 // The LHS of a constant expr is always evaluated and needed.
Chris Lattner590b6642007-07-15 23:26:56 +0000473 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000474 return false;
475
476 llvm::APSInt RHS(Result);
477
478 // The short-circuiting &&/|| operators don't necessarily evaluate their
479 // RHS. Make sure to pass isEvaluated down correctly.
480 if (Exp->isLogicalOp()) {
481 bool RHSEval;
482 if (Exp->getOpcode() == BinaryOperator::LAnd)
483 RHSEval = Result != 0;
484 else {
485 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
486 RHSEval = Result == 0;
487 }
488
Chris Lattner590b6642007-07-15 23:26:56 +0000489 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000490 isEvaluated & RHSEval))
491 return false;
492 } else {
Chris Lattner590b6642007-07-15 23:26:56 +0000493 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000494 return false;
495 }
496
Reid Spencer5f016e22007-07-11 17:01:13 +0000497 switch (Exp->getOpcode()) {
498 default:
499 if (Loc) *Loc = getLocStart();
500 return false;
501 case BinaryOperator::Mul:
502 Result *= RHS;
503 break;
504 case BinaryOperator::Div:
505 if (RHS == 0) {
506 if (!isEvaluated) break;
507 if (Loc) *Loc = getLocStart();
508 return false;
509 }
510 Result /= RHS;
511 break;
512 case BinaryOperator::Rem:
513 if (RHS == 0) {
514 if (!isEvaluated) break;
515 if (Loc) *Loc = getLocStart();
516 return false;
517 }
518 Result %= RHS;
519 break;
520 case BinaryOperator::Add: Result += RHS; break;
521 case BinaryOperator::Sub: Result -= RHS; break;
522 case BinaryOperator::Shl:
523 Result <<= RHS.getLimitedValue(Result.getBitWidth()-1);
524 break;
525 case BinaryOperator::Shr:
526 Result >>= RHS.getLimitedValue(Result.getBitWidth()-1);
527 break;
528 case BinaryOperator::LT: Result = Result < RHS; break;
529 case BinaryOperator::GT: Result = Result > RHS; break;
530 case BinaryOperator::LE: Result = Result <= RHS; break;
531 case BinaryOperator::GE: Result = Result >= RHS; break;
532 case BinaryOperator::EQ: Result = Result == RHS; break;
533 case BinaryOperator::NE: Result = Result != RHS; break;
534 case BinaryOperator::And: Result &= RHS; break;
535 case BinaryOperator::Xor: Result ^= RHS; break;
536 case BinaryOperator::Or: Result |= RHS; break;
537 case BinaryOperator::LAnd:
538 Result = Result != 0 && RHS != 0;
539 break;
540 case BinaryOperator::LOr:
541 Result = Result != 0 || RHS != 0;
542 break;
543
544 case BinaryOperator::Comma:
545 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
546 // *except* when they are contained within a subexpression that is not
547 // evaluated". Note that Assignment can never happen due to constraints
548 // on the LHS subexpr, so we don't need to check it here.
549 if (isEvaluated) {
550 if (Loc) *Loc = getLocStart();
551 return false;
552 }
553
554 // The result of the constant expr is the RHS.
555 Result = RHS;
556 return true;
557 }
558
559 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
560 break;
561 }
Chris Lattner26dc7b32007-07-15 23:54:50 +0000562 case ImplicitCastExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000563 case CastExprClass: {
Chris Lattner26dc7b32007-07-15 23:54:50 +0000564 const Expr *SubExpr;
565 SourceLocation CastLoc;
566 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
567 SubExpr = C->getSubExpr();
568 CastLoc = C->getLParenLoc();
569 } else {
570 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
571 CastLoc = getLocStart();
572 }
573
Reid Spencer5f016e22007-07-11 17:01:13 +0000574 // C99 6.6p6: shall only convert arithmetic types to integer types.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000575 if (!SubExpr->getType()->isArithmeticType() ||
576 !getType()->isIntegerType()) {
577 if (Loc) *Loc = SubExpr->getLocStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000578 return false;
579 }
580
581 // Handle simple integer->integer casts.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000582 if (SubExpr->getType()->isIntegerType()) {
583 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000584 return false;
Chris Lattner26dc7b32007-07-15 23:54:50 +0000585
586 // Figure out if this is a truncate, extend or noop cast.
587 unsigned DestWidth = Ctx.getTypeSize(getType(), CastLoc);
588
589 // If the input is signed, do a sign extend, noop, or truncate.
590 if (SubExpr->getType()->isSignedIntegerType())
591 Result.sextOrTrunc(DestWidth);
592 else // If the input is unsigned, do a zero extend, noop, or truncate.
593 Result.zextOrTrunc(DestWidth);
Reid Spencer5f016e22007-07-11 17:01:13 +0000594 break;
595 }
596
597 // Allow floating constants that are the immediate operands of casts or that
598 // are parenthesized.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000599 const Expr *Operand = SubExpr;
Reid Spencer5f016e22007-07-11 17:01:13 +0000600 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
601 Operand = PE->getSubExpr();
602
603 if (const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand)) {
604 // FIXME: Evaluate this correctly!
605 Result = (int)FL->getValue();
606 break;
607 }
608 if (Loc) *Loc = Operand->getLocStart();
609 return false;
610 }
611 case ConditionalOperatorClass: {
612 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
613
Chris Lattner590b6642007-07-15 23:26:56 +0000614 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000615 return false;
616
617 const Expr *TrueExp = Exp->getLHS();
618 const Expr *FalseExp = Exp->getRHS();
619 if (Result == 0) std::swap(TrueExp, FalseExp);
620
621 // Evaluate the false one first, discard the result.
Chris Lattner590b6642007-07-15 23:26:56 +0000622 if (!FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Reid Spencer5f016e22007-07-11 17:01:13 +0000623 return false;
624 // Evalute the true one, capture the result.
Chris Lattner590b6642007-07-15 23:26:56 +0000625 if (!TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000626 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000627 break;
628 }
629 }
630
631 // Cases that are valid constant exprs fall through to here.
632 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
633 return true;
634}
635
636
637/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
638/// integer constant expression with the value zero, or if this is one that is
639/// cast to void*.
Chris Lattner590b6642007-07-15 23:26:56 +0000640bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000641 // Strip off a cast to void*, if it exists.
642 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
643 // Check that it is a cast to void*.
644 if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) {
645 QualType Pointee = PT->getPointeeType();
646 if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void*
647 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Chris Lattner590b6642007-07-15 23:26:56 +0000648 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000649 }
650 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
651 // Accept ((void*)0) as a null pointer constant, as many other
652 // implementations do.
Chris Lattner590b6642007-07-15 23:26:56 +0000653 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000654 }
655
656 // This expression must be an integer type.
657 if (!getType()->isIntegerType())
658 return false;
659
660 // If we have an integer constant expression, we need to *evaluate* it and
661 // test for the value 0.
662 llvm::APSInt Val(32);
Chris Lattner590b6642007-07-15 23:26:56 +0000663 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000664}
Steve Naroff31a45842007-07-28 23:10:27 +0000665
Chris Lattner6481a572007-08-03 17:31:20 +0000666unsigned OCUVectorElementExpr::getNumElements() const {
Chris Lattner4d0ac882007-08-03 16:00:20 +0000667 return strlen(Accessor.getName());
668}
669
670
Chris Lattnercb92a112007-08-02 21:47:28 +0000671/// getComponentType - Determine whether the components of this access are
672/// "point" "color" or "texture" elements.
Chris Lattner6481a572007-08-03 17:31:20 +0000673OCUVectorElementExpr::ElementType
674OCUVectorElementExpr::getElementType() const {
Steve Naroff31a45842007-07-28 23:10:27 +0000675 // derive the component type, no need to waste space.
676 const char *compStr = Accessor.getName();
Chris Lattnerb4878f42007-08-02 22:20:00 +0000677
Chris Lattner88dca042007-08-02 22:33:49 +0000678 if (OCUVectorType::getPointAccessorIdx(*compStr) != -1) return Point;
679 if (OCUVectorType::getColorAccessorIdx(*compStr) != -1) return Color;
Chris Lattnerb4878f42007-08-02 22:20:00 +0000680
Chris Lattner88dca042007-08-02 22:33:49 +0000681 assert(OCUVectorType::getTextureAccessorIdx(*compStr) != -1 &&
Chris Lattnerb4878f42007-08-02 22:20:00 +0000682 "getComponentType(): Illegal accessor");
683 return Texture;
Steve Naroff31a45842007-07-28 23:10:27 +0000684}
Steve Narofffec0b492007-07-30 03:29:09 +0000685
Chris Lattner6481a572007-08-03 17:31:20 +0000686/// containsDuplicateElements - Return true if any element access is
Chris Lattnercb92a112007-08-02 21:47:28 +0000687/// repeated.
Chris Lattner6481a572007-08-03 17:31:20 +0000688bool OCUVectorElementExpr::containsDuplicateElements() const {
Steve Narofffec0b492007-07-30 03:29:09 +0000689 const char *compStr = Accessor.getName();
690 unsigned length = strlen(compStr);
691
692 for (unsigned i = 0; i < length-1; i++) {
693 const char *s = compStr+i;
694 for (const char c = *s++; *s; s++)
695 if (c == *s)
696 return true;
697 }
698 return false;
699}
Chris Lattnerb8f849d2007-08-02 23:36:59 +0000700
701/// getEncodedElementAccess - We encode fields with two bits per component.
Chris Lattner6481a572007-08-03 17:31:20 +0000702unsigned OCUVectorElementExpr::getEncodedElementAccess() const {
Chris Lattnerb8f849d2007-08-02 23:36:59 +0000703 const char *compStr = Accessor.getName();
Chris Lattner6481a572007-08-03 17:31:20 +0000704 unsigned length = getNumElements();
Chris Lattnerb8f849d2007-08-02 23:36:59 +0000705
706 unsigned Result = 0;
707
708 while (length--) {
709 Result <<= 2;
710 int Idx = OCUVectorType::getAccessorIdx(compStr[length]);
711 assert(Idx != -1 && "Invalid accessor letter");
712 Result |= Idx;
713 }
714 return Result;
715}
716
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000717//===----------------------------------------------------------------------===//
718// Child Iterators for iterating over subexpressions/substatements
719//===----------------------------------------------------------------------===//
720
721// DeclRefExpr
Ted Kremenek1237c672007-08-24 20:06:47 +0000722Stmt::child_iterator DeclRefExpr::child_begin() { return NULL; }
723Stmt::child_iterator DeclRefExpr::child_end() { return NULL; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000724
725// PreDefinedExpr
Ted Kremenek1237c672007-08-24 20:06:47 +0000726Stmt::child_iterator PreDefinedExpr::child_begin() { return NULL; }
727Stmt::child_iterator PreDefinedExpr::child_end() { return NULL; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000728
729// IntegerLiteral
Ted Kremenek1237c672007-08-24 20:06:47 +0000730Stmt::child_iterator IntegerLiteral::child_begin() { return NULL; }
731Stmt::child_iterator IntegerLiteral::child_end() { return NULL; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000732
733// CharacterLiteral
Ted Kremenek1237c672007-08-24 20:06:47 +0000734Stmt::child_iterator CharacterLiteral::child_begin() { return NULL; }
735Stmt::child_iterator CharacterLiteral::child_end() { return NULL; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000736
737// FloatingLiteral
Ted Kremenek1237c672007-08-24 20:06:47 +0000738Stmt::child_iterator FloatingLiteral::child_begin() { return NULL; }
739Stmt::child_iterator FloatingLiteral::child_end() { return NULL; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000740
Chris Lattner5d661452007-08-26 03:42:43 +0000741// ImaginaryLiteral
742Stmt::child_iterator ImaginaryLiteral::child_begin() {
743 return reinterpret_cast<Stmt**>(&Val);
744}
745Stmt::child_iterator ImaginaryLiteral::child_end() {
746 return reinterpret_cast<Stmt**>(&Val)+1;
747}
748
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000749// StringLiteral
Ted Kremenek1237c672007-08-24 20:06:47 +0000750Stmt::child_iterator StringLiteral::child_begin() { return NULL; }
751Stmt::child_iterator StringLiteral::child_end() { return NULL; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000752
753// ParenExpr
754Stmt::child_iterator ParenExpr::child_begin() {
755 return reinterpret_cast<Stmt**>(&Val);
756}
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000757Stmt::child_iterator ParenExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +0000758 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000759}
760
761// UnaryOperator
762Stmt::child_iterator UnaryOperator::child_begin() {
763 return reinterpret_cast<Stmt**>(&Val);
764}
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000765Stmt::child_iterator UnaryOperator::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +0000766 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000767}
768
769// SizeOfAlignOfTypeExpr
Chris Lattner5d661452007-08-26 03:42:43 +0000770Stmt::child_iterator SizeOfAlignOfTypeExpr::child_begin() { return NULL; }
771Stmt::child_iterator SizeOfAlignOfTypeExpr::child_end() { return NULL; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000772
773// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +0000774Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000775 return reinterpret_cast<Stmt**>(&SubExprs);
776}
Ted Kremenek1237c672007-08-24 20:06:47 +0000777Stmt::child_iterator ArraySubscriptExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +0000778 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000779}
780
781// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +0000782Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000783 return reinterpret_cast<Stmt**>(&SubExprs);
784}
Ted Kremenek1237c672007-08-24 20:06:47 +0000785Stmt::child_iterator CallExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +0000786 return reinterpret_cast<Stmt**>(&SubExprs)+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000787}
Ted Kremenek1237c672007-08-24 20:06:47 +0000788
789// MemberExpr
790Stmt::child_iterator MemberExpr::child_begin() {
791 return reinterpret_cast<Stmt**>(&Base);
792}
Ted Kremenek1237c672007-08-24 20:06:47 +0000793Stmt::child_iterator MemberExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +0000794 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +0000795}
796
797// OCUVectorElementExpr
798Stmt::child_iterator OCUVectorElementExpr::child_begin() {
799 return reinterpret_cast<Stmt**>(&Base);
800}
Ted Kremenek1237c672007-08-24 20:06:47 +0000801Stmt::child_iterator OCUVectorElementExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +0000802 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +0000803}
804
805// CompoundLiteralExpr
806Stmt::child_iterator CompoundLiteralExpr::child_begin() {
807 return reinterpret_cast<Stmt**>(&Init);
808}
Ted Kremenek1237c672007-08-24 20:06:47 +0000809Stmt::child_iterator CompoundLiteralExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +0000810 return reinterpret_cast<Stmt**>(&Init)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +0000811}
812
813// ImplicitCastExpr
814Stmt::child_iterator ImplicitCastExpr::child_begin() {
815 return reinterpret_cast<Stmt**>(&Op);
816}
Ted Kremenek1237c672007-08-24 20:06:47 +0000817Stmt::child_iterator ImplicitCastExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +0000818 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +0000819}
820
821// CastExpr
822Stmt::child_iterator CastExpr::child_begin() {
823 return reinterpret_cast<Stmt**>(&Op);
824}
Ted Kremenek1237c672007-08-24 20:06:47 +0000825Stmt::child_iterator CastExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +0000826 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +0000827}
828
829// BinaryOperator
830Stmt::child_iterator BinaryOperator::child_begin() {
831 return reinterpret_cast<Stmt**>(&SubExprs);
832}
Ted Kremenek1237c672007-08-24 20:06:47 +0000833Stmt::child_iterator BinaryOperator::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +0000834 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +0000835}
836
837// ConditionalOperator
838Stmt::child_iterator ConditionalOperator::child_begin() {
839 return reinterpret_cast<Stmt**>(&SubExprs);
840}
Ted Kremenek1237c672007-08-24 20:06:47 +0000841Stmt::child_iterator ConditionalOperator::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +0000842 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +0000843}
844
845// AddrLabelExpr
846Stmt::child_iterator AddrLabelExpr::child_begin() { return NULL; }
847Stmt::child_iterator AddrLabelExpr::child_end() { return NULL; }
848
Ted Kremenek1237c672007-08-24 20:06:47 +0000849// StmtExpr
850Stmt::child_iterator StmtExpr::child_begin() {
851 return reinterpret_cast<Stmt**>(&SubStmt);
852}
Ted Kremenek1237c672007-08-24 20:06:47 +0000853Stmt::child_iterator StmtExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +0000854 return reinterpret_cast<Stmt**>(&SubStmt)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +0000855}
856
857// TypesCompatibleExpr
858Stmt::child_iterator TypesCompatibleExpr::child_begin() { return NULL; }
859Stmt::child_iterator TypesCompatibleExpr::child_end() { return NULL; }
860
861// ChooseExpr
862Stmt::child_iterator ChooseExpr::child_begin() {
863 return reinterpret_cast<Stmt**>(&SubExprs);
864}
865
866Stmt::child_iterator ChooseExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +0000867 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +0000868}
869
870// ObjCStringLiteral
871Stmt::child_iterator ObjCStringLiteral::child_begin() { return NULL; }
872Stmt::child_iterator ObjCStringLiteral::child_end() { return NULL; }
873
874// ObjCEncodeExpr
875Stmt::child_iterator ObjCEncodeExpr::child_begin() { return NULL; }
876Stmt::child_iterator ObjCEncodeExpr::child_end() { return NULL; }
877