blob: 625634d7877c2d25407fc95f729ef1d2a27296d0 [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)
81 : Expr(CallExprClass, t), Fn(fn), NumArgs(numargs) {
82 Args = new Expr*[numargs];
83 for (unsigned i = 0; i != numargs; ++i)
84 Args[i] = args[i];
85 RParenLoc = rparenloc;
86}
87
88/// 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}
125
126
127//===----------------------------------------------------------------------===//
128// Generic Expression Routines
129//===----------------------------------------------------------------------===//
130
131/// hasLocalSideEffect - Return true if this immediate expression has side
132/// effects, not counting any sub-expressions.
133bool Expr::hasLocalSideEffect() const {
134 switch (getStmtClass()) {
135 default:
136 return false;
137 case ParenExprClass:
138 return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect();
139 case UnaryOperatorClass: {
140 const UnaryOperator *UO = cast<UnaryOperator>(this);
141
142 switch (UO->getOpcode()) {
143 default: return false;
144 case UnaryOperator::PostInc:
145 case UnaryOperator::PostDec:
146 case UnaryOperator::PreInc:
147 case UnaryOperator::PreDec:
148 return true; // ++/--
149
150 case UnaryOperator::Deref:
151 // Dereferencing a volatile pointer is a side-effect.
152 return getType().isVolatileQualified();
153 case UnaryOperator::Real:
154 case UnaryOperator::Imag:
155 // accessing a piece of a volatile complex is a side-effect.
156 return UO->getSubExpr()->getType().isVolatileQualified();
157
158 case UnaryOperator::Extension:
159 return UO->getSubExpr()->hasLocalSideEffect();
160 }
161 }
162 case BinaryOperatorClass:
163 return cast<BinaryOperator>(this)->isAssignmentOp();
164
165 case MemberExprClass:
166 case ArraySubscriptExprClass:
167 // If the base pointer or element is to a volatile pointer/field, accessing
168 // if is a side effect.
169 return getType().isVolatileQualified();
170
171 case CallExprClass:
172 // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }"
173 // should warn.
174 return true;
175
176 case CastExprClass:
177 // If this is a cast to void, check the operand. Otherwise, the result of
178 // the cast is unused.
179 if (getType()->isVoidType())
180 return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect();
181 return false;
182 }
183}
184
185/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
186/// incomplete type other than void. Nonarray expressions that can be lvalues:
187/// - name, where name must be a variable
188/// - e[i]
189/// - (e), where e must be an lvalue
190/// - e.name, where e must be an lvalue
191/// - e->name
192/// - *e, the type of e cannot be a function type
193/// - string-constant
Bill Wendling08ad47c2007-07-17 03:52:31 +0000194/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +0000195///
Bill Wendlingca51c972007-07-16 07:07:56 +0000196Expr::isLvalueResult Expr::isLvalue() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 // first, check the type (C99 6.3.2.1)
Chris Lattnercb4f9a62007-07-21 05:33:26 +0000198 if (TR->isFunctionType()) // from isObjectType()
Reid Spencer5f016e22007-07-11 17:01:13 +0000199 return LV_NotObjectType;
200
Steve Naroff731ec572007-07-21 13:32:03 +0000201 if (TR->isVoidType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000202 return LV_IncompleteVoidType;
Bill Wendling08ad47c2007-07-17 03:52:31 +0000203
Chris Lattnercb4f9a62007-07-21 05:33:26 +0000204 if (TR->isReferenceType()) // C++ [expr]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000205 return LV_Valid;
206
Reid Spencer5f016e22007-07-11 17:01:13 +0000207 // the type looks fine, now check the expression
208 switch (getStmtClass()) {
209 case StringLiteralClass: // C99 6.5.1p4
210 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
211 // For vectors, make sure base is an lvalue (i.e. not a function call).
212 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
213 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue();
214 return LV_Valid;
215 case DeclRefExprClass: // C99 6.5.1p2
216 if (isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl()))
217 return LV_Valid;
218 break;
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000219 case MemberExprClass: { // C99 6.5.2.3p4
Reid Spencer5f016e22007-07-11 17:01:13 +0000220 const MemberExpr *m = cast<MemberExpr>(this);
221 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue();
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000222 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000223 case UnaryOperatorClass: // C99 6.5.3p4
224 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
225 return LV_Valid;
226 break;
227 case ParenExprClass: // C99 6.5.1p5
228 return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
Steve Narofffec0b492007-07-30 03:29:09 +0000229 case OCUVectorComponentClass:
230 if (cast<OCUVectorComponent>(this)->containsDuplicateComponents())
231 return LV_DuplicateVectorComponents;
232 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000233 default:
234 break;
235 }
236 return LV_InvalidExpression;
237}
238
239/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
240/// does not have an incomplete type, does not have a const-qualified type, and
241/// if it is a structure or union, does not have any member (including,
242/// recursively, any member or element of all contained aggregates or unions)
243/// with a const-qualified type.
Bill Wendlingca51c972007-07-16 07:07:56 +0000244Expr::isModifiableLvalueResult Expr::isModifiableLvalue() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000245 isLvalueResult lvalResult = isLvalue();
246
247 switch (lvalResult) {
248 case LV_Valid: break;
249 case LV_NotObjectType: return MLV_NotObjectType;
250 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +0000251 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Reid Spencer5f016e22007-07-11 17:01:13 +0000252 case LV_InvalidExpression: return MLV_InvalidExpression;
253 }
254 if (TR.isConstQualified())
255 return MLV_ConstQualified;
256 if (TR->isArrayType())
257 return MLV_ArrayType;
258 if (TR->isIncompleteType())
259 return MLV_IncompleteType;
260
261 if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) {
262 if (r->hasConstFields())
263 return MLV_ConstQualified;
264 }
265 return MLV_Valid;
266}
267
268/// isIntegerConstantExpr - this recursive routine will test if an expression is
269/// an integer constant expression. Note: With the introduction of VLA's in
270/// C99 the result of the sizeof operator is no longer always a constant
271/// expression. The generalization of the wording to include any subexpression
272/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
273/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
274/// "0 || f()" can be treated as a constant expression. In C90 this expression,
275/// occurring in a context requiring a constant, would have been a constraint
276/// violation. FIXME: This routine currently implements C90 semantics.
277/// To properly implement C99 semantics this routine will need to evaluate
278/// expressions involving operators previously mentioned.
279
280/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
281/// comma, etc
282///
283/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
284/// permit this.
Chris Lattnerce0afc02007-07-18 05:21:20 +0000285///
286/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
287/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
288/// cast+dereference.
Chris Lattner590b6642007-07-15 23:26:56 +0000289bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
290 SourceLocation *Loc, bool isEvaluated) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000291 switch (getStmtClass()) {
292 default:
293 if (Loc) *Loc = getLocStart();
294 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 case ParenExprClass:
296 return cast<ParenExpr>(this)->getSubExpr()->
Chris Lattner590b6642007-07-15 23:26:56 +0000297 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
Reid Spencer5f016e22007-07-11 17:01:13 +0000298 case IntegerLiteralClass:
299 Result = cast<IntegerLiteral>(this)->getValue();
300 break;
Chris Lattner2eadfb62007-07-15 23:32:58 +0000301 case CharacterLiteralClass: {
302 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
303 Result.zextOrTrunc(Ctx.getTypeSize(getType(), CL->getLoc()));
304 Result = CL->getValue();
Chris Lattnerf0fbcb32007-07-16 21:04:56 +0000305 Result.setIsUnsigned(!getType()->isSignedIntegerType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000306 break;
Chris Lattner2eadfb62007-07-15 23:32:58 +0000307 }
Steve Naroff7b658aa2007-08-02 04:09:23 +0000308 case TypesCompatibleExprClass: {
309 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
310 Result.zextOrTrunc(Ctx.getTypeSize(getType(), TCE->getLocStart()));
311 Result = TCE->typesAreCompatible();
Steve Naroff389cecc2007-08-02 00:13:27 +0000312 break;
Steve Naroff7b658aa2007-08-02 04:09:23 +0000313 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000314 case DeclRefExprClass:
315 if (const EnumConstantDecl *D =
316 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
317 Result = D->getInitVal();
318 break;
319 }
320 if (Loc) *Loc = getLocStart();
321 return false;
322 case UnaryOperatorClass: {
323 const UnaryOperator *Exp = cast<UnaryOperator>(this);
324
325 // Get the operand value. If this is sizeof/alignof, do not evalute the
326 // operand. This affects C99 6.6p3.
327 if (Exp->isSizeOfAlignOfOp()) isEvaluated = false;
Chris Lattner590b6642007-07-15 23:26:56 +0000328 if (!Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx,Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000329 return false;
330
331 switch (Exp->getOpcode()) {
332 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
333 // See C99 6.6p3.
334 default:
335 if (Loc) *Loc = Exp->getOperatorLoc();
336 return false;
337 case UnaryOperator::Extension:
Chris Lattner76e773a2007-07-18 18:38:36 +0000338 return true; // FIXME: this is wrong.
Reid Spencer5f016e22007-07-11 17:01:13 +0000339 case UnaryOperator::SizeOf:
340 case UnaryOperator::AlignOf:
341 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattner590b6642007-07-15 23:26:56 +0000342 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx, Loc))
Reid Spencer5f016e22007-07-11 17:01:13 +0000343 return false;
344
Chris Lattner76e773a2007-07-18 18:38:36 +0000345 // Return the result in the right width.
346 Result.zextOrTrunc(Ctx.getTypeSize(getType(), Exp->getOperatorLoc()));
347
348 // Get information about the size or align.
349 if (Exp->getOpcode() == UnaryOperator::SizeOf)
350 Result = Ctx.getTypeSize(Exp->getSubExpr()->getType(),
351 Exp->getOperatorLoc());
352 else
353 Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType(),
354 Exp->getOperatorLoc());
Reid Spencer5f016e22007-07-11 17:01:13 +0000355 break;
356 case UnaryOperator::LNot: {
357 bool Val = Result != 0;
Chris Lattner76e773a2007-07-18 18:38:36 +0000358 Result.zextOrTrunc(Ctx.getTypeSize(getType(), Exp->getOperatorLoc()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000359 Result = Val;
360 break;
361 }
362 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000363 break;
364 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000365 Result = -Result;
366 break;
367 case UnaryOperator::Not:
Reid Spencer5f016e22007-07-11 17:01:13 +0000368 Result = ~Result;
369 break;
370 }
371 break;
372 }
373 case SizeOfAlignOfTypeExprClass: {
374 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
375 // alignof always evaluates to a constant.
Chris Lattner590b6642007-07-15 23:26:56 +0000376 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx,Loc))
Reid Spencer5f016e22007-07-11 17:01:13 +0000377 return false;
378
Chris Lattner76e773a2007-07-18 18:38:36 +0000379 // Return the result in the right width.
380 Result.zextOrTrunc(Ctx.getTypeSize(getType(), Exp->getOperatorLoc()));
381
382 // Get information about the size or align.
383 if (Exp->isSizeOf())
384 Result = Ctx.getTypeSize(Exp->getArgumentType(), Exp->getOperatorLoc());
385 else
386 Result = Ctx.getTypeAlign(Exp->getArgumentType(), Exp->getOperatorLoc());
Reid Spencer5f016e22007-07-11 17:01:13 +0000387 break;
388 }
389 case BinaryOperatorClass: {
390 const BinaryOperator *Exp = cast<BinaryOperator>(this);
391
392 // The LHS of a constant expr is always evaluated and needed.
Chris Lattner590b6642007-07-15 23:26:56 +0000393 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000394 return false;
395
396 llvm::APSInt RHS(Result);
397
398 // The short-circuiting &&/|| operators don't necessarily evaluate their
399 // RHS. Make sure to pass isEvaluated down correctly.
400 if (Exp->isLogicalOp()) {
401 bool RHSEval;
402 if (Exp->getOpcode() == BinaryOperator::LAnd)
403 RHSEval = Result != 0;
404 else {
405 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
406 RHSEval = Result == 0;
407 }
408
Chris Lattner590b6642007-07-15 23:26:56 +0000409 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000410 isEvaluated & RHSEval))
411 return false;
412 } else {
Chris Lattner590b6642007-07-15 23:26:56 +0000413 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000414 return false;
415 }
416
Reid Spencer5f016e22007-07-11 17:01:13 +0000417 switch (Exp->getOpcode()) {
418 default:
419 if (Loc) *Loc = getLocStart();
420 return false;
421 case BinaryOperator::Mul:
422 Result *= RHS;
423 break;
424 case BinaryOperator::Div:
425 if (RHS == 0) {
426 if (!isEvaluated) break;
427 if (Loc) *Loc = getLocStart();
428 return false;
429 }
430 Result /= RHS;
431 break;
432 case BinaryOperator::Rem:
433 if (RHS == 0) {
434 if (!isEvaluated) break;
435 if (Loc) *Loc = getLocStart();
436 return false;
437 }
438 Result %= RHS;
439 break;
440 case BinaryOperator::Add: Result += RHS; break;
441 case BinaryOperator::Sub: Result -= RHS; break;
442 case BinaryOperator::Shl:
443 Result <<= RHS.getLimitedValue(Result.getBitWidth()-1);
444 break;
445 case BinaryOperator::Shr:
446 Result >>= RHS.getLimitedValue(Result.getBitWidth()-1);
447 break;
448 case BinaryOperator::LT: Result = Result < RHS; break;
449 case BinaryOperator::GT: Result = Result > RHS; break;
450 case BinaryOperator::LE: Result = Result <= RHS; break;
451 case BinaryOperator::GE: Result = Result >= RHS; break;
452 case BinaryOperator::EQ: Result = Result == RHS; break;
453 case BinaryOperator::NE: Result = Result != RHS; break;
454 case BinaryOperator::And: Result &= RHS; break;
455 case BinaryOperator::Xor: Result ^= RHS; break;
456 case BinaryOperator::Or: Result |= RHS; break;
457 case BinaryOperator::LAnd:
458 Result = Result != 0 && RHS != 0;
459 break;
460 case BinaryOperator::LOr:
461 Result = Result != 0 || RHS != 0;
462 break;
463
464 case BinaryOperator::Comma:
465 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
466 // *except* when they are contained within a subexpression that is not
467 // evaluated". Note that Assignment can never happen due to constraints
468 // on the LHS subexpr, so we don't need to check it here.
469 if (isEvaluated) {
470 if (Loc) *Loc = getLocStart();
471 return false;
472 }
473
474 // The result of the constant expr is the RHS.
475 Result = RHS;
476 return true;
477 }
478
479 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
480 break;
481 }
Chris Lattner26dc7b32007-07-15 23:54:50 +0000482 case ImplicitCastExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000483 case CastExprClass: {
Chris Lattner26dc7b32007-07-15 23:54:50 +0000484 const Expr *SubExpr;
485 SourceLocation CastLoc;
486 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
487 SubExpr = C->getSubExpr();
488 CastLoc = C->getLParenLoc();
489 } else {
490 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
491 CastLoc = getLocStart();
492 }
493
Reid Spencer5f016e22007-07-11 17:01:13 +0000494 // C99 6.6p6: shall only convert arithmetic types to integer types.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000495 if (!SubExpr->getType()->isArithmeticType() ||
496 !getType()->isIntegerType()) {
497 if (Loc) *Loc = SubExpr->getLocStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000498 return false;
499 }
500
501 // Handle simple integer->integer casts.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000502 if (SubExpr->getType()->isIntegerType()) {
503 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000504 return false;
Chris Lattner26dc7b32007-07-15 23:54:50 +0000505
506 // Figure out if this is a truncate, extend or noop cast.
507 unsigned DestWidth = Ctx.getTypeSize(getType(), CastLoc);
508
509 // If the input is signed, do a sign extend, noop, or truncate.
510 if (SubExpr->getType()->isSignedIntegerType())
511 Result.sextOrTrunc(DestWidth);
512 else // If the input is unsigned, do a zero extend, noop, or truncate.
513 Result.zextOrTrunc(DestWidth);
Reid Spencer5f016e22007-07-11 17:01:13 +0000514 break;
515 }
516
517 // Allow floating constants that are the immediate operands of casts or that
518 // are parenthesized.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000519 const Expr *Operand = SubExpr;
Reid Spencer5f016e22007-07-11 17:01:13 +0000520 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
521 Operand = PE->getSubExpr();
522
523 if (const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand)) {
524 // FIXME: Evaluate this correctly!
525 Result = (int)FL->getValue();
526 break;
527 }
528 if (Loc) *Loc = Operand->getLocStart();
529 return false;
530 }
531 case ConditionalOperatorClass: {
532 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
533
Chris Lattner590b6642007-07-15 23:26:56 +0000534 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000535 return false;
536
537 const Expr *TrueExp = Exp->getLHS();
538 const Expr *FalseExp = Exp->getRHS();
539 if (Result == 0) std::swap(TrueExp, FalseExp);
540
541 // Evaluate the false one first, discard the result.
Chris Lattner590b6642007-07-15 23:26:56 +0000542 if (!FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Reid Spencer5f016e22007-07-11 17:01:13 +0000543 return false;
544 // Evalute the true one, capture the result.
Chris Lattner590b6642007-07-15 23:26:56 +0000545 if (!TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000546 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000547 break;
548 }
549 }
550
551 // Cases that are valid constant exprs fall through to here.
552 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
553 return true;
554}
555
556
557/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
558/// integer constant expression with the value zero, or if this is one that is
559/// cast to void*.
Chris Lattner590b6642007-07-15 23:26:56 +0000560bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000561 // Strip off a cast to void*, if it exists.
562 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
563 // Check that it is a cast to void*.
564 if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) {
565 QualType Pointee = PT->getPointeeType();
566 if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void*
567 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Chris Lattner590b6642007-07-15 23:26:56 +0000568 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000569 }
570 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
571 // Accept ((void*)0) as a null pointer constant, as many other
572 // implementations do.
Chris Lattner590b6642007-07-15 23:26:56 +0000573 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000574 }
575
576 // This expression must be an integer type.
577 if (!getType()->isIntegerType())
578 return false;
579
580 // If we have an integer constant expression, we need to *evaluate* it and
581 // test for the value 0.
582 llvm::APSInt Val(32);
Chris Lattner590b6642007-07-15 23:26:56 +0000583 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000584}
Steve Naroff31a45842007-07-28 23:10:27 +0000585
Chris Lattner4d0ac882007-08-03 16:00:20 +0000586unsigned OCUVectorComponent::getNumComponents() const {
587 return strlen(Accessor.getName());
588}
589
590
Chris Lattnercb92a112007-08-02 21:47:28 +0000591/// getComponentType - Determine whether the components of this access are
592/// "point" "color" or "texture" elements.
Steve Naroff31a45842007-07-28 23:10:27 +0000593OCUVectorComponent::ComponentType OCUVectorComponent::getComponentType() const {
594 // derive the component type, no need to waste space.
595 const char *compStr = Accessor.getName();
Chris Lattnerb4878f42007-08-02 22:20:00 +0000596
Chris Lattner88dca042007-08-02 22:33:49 +0000597 if (OCUVectorType::getPointAccessorIdx(*compStr) != -1) return Point;
598 if (OCUVectorType::getColorAccessorIdx(*compStr) != -1) return Color;
Chris Lattnerb4878f42007-08-02 22:20:00 +0000599
Chris Lattner88dca042007-08-02 22:33:49 +0000600 assert(OCUVectorType::getTextureAccessorIdx(*compStr) != -1 &&
Chris Lattnerb4878f42007-08-02 22:20:00 +0000601 "getComponentType(): Illegal accessor");
602 return Texture;
Steve Naroff31a45842007-07-28 23:10:27 +0000603}
Steve Narofffec0b492007-07-30 03:29:09 +0000604
Chris Lattnercb92a112007-08-02 21:47:28 +0000605/// containsDuplicateComponents - Return true if any element access is
606/// repeated.
Steve Narofffec0b492007-07-30 03:29:09 +0000607bool OCUVectorComponent::containsDuplicateComponents() const {
608 const char *compStr = Accessor.getName();
609 unsigned length = strlen(compStr);
610
611 for (unsigned i = 0; i < length-1; i++) {
612 const char *s = compStr+i;
613 for (const char c = *s++; *s; s++)
614 if (c == *s)
615 return true;
616 }
617 return false;
618}
Chris Lattnerb8f849d2007-08-02 23:36:59 +0000619
620/// getEncodedElementAccess - We encode fields with two bits per component.
621unsigned OCUVectorComponent::getEncodedElementAccess() const {
622 const char *compStr = Accessor.getName();
Chris Lattner4d0ac882007-08-03 16:00:20 +0000623 unsigned length = getNumComponents();
Chris Lattnerb8f849d2007-08-02 23:36:59 +0000624
625 unsigned Result = 0;
626
627 while (length--) {
628 Result <<= 2;
629 int Idx = OCUVectorType::getAccessorIdx(compStr[length]);
630 assert(Idx != -1 && "Invalid accessor letter");
631 Result |= Idx;
632 }
633 return Result;
634}
635