blob: 7397e54ae6ac56948bc16f4b36ef5ac7c7b14d18 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +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"
15#include "clang/AST/ASTContext.h"
16#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
Steve Naroff8d3b1702007-08-08 22:15:55 +000088bool CallExpr::isBuiltinClassifyType(llvm::APSInt &Result) const {
89 // The following enum mimics gcc's internal "typeclass.h" file.
90 enum gcc_type_class {
91 no_type_class = -1,
92 void_type_class, integer_type_class, char_type_class,
93 enumeral_type_class, boolean_type_class,
94 pointer_type_class, reference_type_class, offset_type_class,
95 real_type_class, complex_type_class,
96 function_type_class, method_type_class,
97 record_type_class, union_type_class,
98 array_type_class, string_type_class,
99 lang_type_class
100 };
101 Result.setIsSigned(true);
102
103 // All simple function calls (e.g. func()) are implicitly cast to pointer to
104 // function. As a result, we try and obtain the DeclRefExpr from the
105 // ImplicitCastExpr.
106 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
107 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
108 return false;
109 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
110 if (!DRE)
111 return false;
112
113 // We have a DeclRefExpr.
114 if (strcmp(DRE->getDecl()->getName(), "__builtin_classify_type") == 0) {
115 // If no argument was supplied, default to "no_type_class". This isn't
116 // ideal, however it's what gcc does.
117 Result = static_cast<uint64_t>(no_type_class);
118 if (NumArgs >= 1) {
119 QualType argType = getArg(0)->getType();
120
121 if (argType->isVoidType())
122 Result = void_type_class;
123 else if (argType->isEnumeralType())
124 Result = enumeral_type_class;
125 else if (argType->isBooleanType())
126 Result = boolean_type_class;
127 else if (argType->isCharType())
128 Result = string_type_class; // gcc doesn't appear to use char_type_class
129 else if (argType->isIntegerType())
130 Result = integer_type_class;
131 else if (argType->isPointerType())
132 Result = pointer_type_class;
133 else if (argType->isReferenceType())
134 Result = reference_type_class;
135 else if (argType->isRealType())
136 Result = real_type_class;
137 else if (argType->isComplexType())
138 Result = complex_type_class;
139 else if (argType->isFunctionType())
140 Result = function_type_class;
141 else if (argType->isStructureType())
142 Result = record_type_class;
143 else if (argType->isUnionType())
144 Result = union_type_class;
145 else if (argType->isArrayType())
146 Result = array_type_class;
147 else if (argType->isUnionType())
148 Result = union_type_class;
149 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
150 assert(1 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
151 }
152 return true;
153 }
154 return false;
155}
156
Chris Lattner4b009652007-07-25 00:24:17 +0000157/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
158/// corresponds to, e.g. "<<=".
159const char *BinaryOperator::getOpcodeStr(Opcode Op) {
160 switch (Op) {
161 default: assert(0 && "Unknown binary operator");
162 case Mul: return "*";
163 case Div: return "/";
164 case Rem: return "%";
165 case Add: return "+";
166 case Sub: return "-";
167 case Shl: return "<<";
168 case Shr: return ">>";
169 case LT: return "<";
170 case GT: return ">";
171 case LE: return "<=";
172 case GE: return ">=";
173 case EQ: return "==";
174 case NE: return "!=";
175 case And: return "&";
176 case Xor: return "^";
177 case Or: return "|";
178 case LAnd: return "&&";
179 case LOr: return "||";
180 case Assign: return "=";
181 case MulAssign: return "*=";
182 case DivAssign: return "/=";
183 case RemAssign: return "%=";
184 case AddAssign: return "+=";
185 case SubAssign: return "-=";
186 case ShlAssign: return "<<=";
187 case ShrAssign: return ">>=";
188 case AndAssign: return "&=";
189 case XorAssign: return "^=";
190 case OrAssign: return "|=";
191 case Comma: return ",";
192 }
193}
194
195
196//===----------------------------------------------------------------------===//
197// Generic Expression Routines
198//===----------------------------------------------------------------------===//
199
200/// hasLocalSideEffect - Return true if this immediate expression has side
201/// effects, not counting any sub-expressions.
202bool Expr::hasLocalSideEffect() const {
203 switch (getStmtClass()) {
204 default:
205 return false;
206 case ParenExprClass:
207 return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect();
208 case UnaryOperatorClass: {
209 const UnaryOperator *UO = cast<UnaryOperator>(this);
210
211 switch (UO->getOpcode()) {
212 default: return false;
213 case UnaryOperator::PostInc:
214 case UnaryOperator::PostDec:
215 case UnaryOperator::PreInc:
216 case UnaryOperator::PreDec:
217 return true; // ++/--
218
219 case UnaryOperator::Deref:
220 // Dereferencing a volatile pointer is a side-effect.
221 return getType().isVolatileQualified();
222 case UnaryOperator::Real:
223 case UnaryOperator::Imag:
224 // accessing a piece of a volatile complex is a side-effect.
225 return UO->getSubExpr()->getType().isVolatileQualified();
226
227 case UnaryOperator::Extension:
228 return UO->getSubExpr()->hasLocalSideEffect();
229 }
230 }
231 case BinaryOperatorClass:
232 return cast<BinaryOperator>(this)->isAssignmentOp();
233
234 case MemberExprClass:
235 case ArraySubscriptExprClass:
236 // If the base pointer or element is to a volatile pointer/field, accessing
237 // if is a side effect.
238 return getType().isVolatileQualified();
239
240 case CallExprClass:
241 // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }"
242 // should warn.
243 return true;
244
245 case CastExprClass:
246 // If this is a cast to void, check the operand. Otherwise, the result of
247 // the cast is unused.
248 if (getType()->isVoidType())
249 return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect();
250 return false;
251 }
252}
253
254/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
255/// incomplete type other than void. Nonarray expressions that can be lvalues:
256/// - name, where name must be a variable
257/// - e[i]
258/// - (e), where e must be an lvalue
259/// - e.name, where e must be an lvalue
260/// - e->name
261/// - *e, the type of e cannot be a function type
262/// - string-constant
263/// - reference type [C++ [expr]]
264///
265Expr::isLvalueResult Expr::isLvalue() const {
266 // first, check the type (C99 6.3.2.1)
267 if (TR->isFunctionType()) // from isObjectType()
268 return LV_NotObjectType;
269
270 if (TR->isVoidType())
271 return LV_IncompleteVoidType;
272
273 if (TR->isReferenceType()) // C++ [expr]
274 return LV_Valid;
275
276 // the type looks fine, now check the expression
277 switch (getStmtClass()) {
278 case StringLiteralClass: // C99 6.5.1p4
279 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
280 // For vectors, make sure base is an lvalue (i.e. not a function call).
281 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
282 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue();
283 return LV_Valid;
284 case DeclRefExprClass: // C99 6.5.1p2
285 if (isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl()))
286 return LV_Valid;
287 break;
288 case MemberExprClass: { // C99 6.5.2.3p4
289 const MemberExpr *m = cast<MemberExpr>(this);
290 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue();
291 }
292 case UnaryOperatorClass: // C99 6.5.3p4
293 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
294 return LV_Valid;
295 break;
296 case ParenExprClass: // C99 6.5.1p5
297 return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
Chris Lattnera0d03a72007-08-03 17:31:20 +0000298 case OCUVectorElementExprClass:
299 if (cast<OCUVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroffba67f692007-07-30 03:29:09 +0000300 return LV_DuplicateVectorComponents;
301 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000302 default:
303 break;
304 }
305 return LV_InvalidExpression;
306}
307
308/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
309/// does not have an incomplete type, does not have a const-qualified type, and
310/// if it is a structure or union, does not have any member (including,
311/// recursively, any member or element of all contained aggregates or unions)
312/// with a const-qualified type.
313Expr::isModifiableLvalueResult Expr::isModifiableLvalue() const {
314 isLvalueResult lvalResult = isLvalue();
315
316 switch (lvalResult) {
317 case LV_Valid: break;
318 case LV_NotObjectType: return MLV_NotObjectType;
319 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroffba67f692007-07-30 03:29:09 +0000320 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner4b009652007-07-25 00:24:17 +0000321 case LV_InvalidExpression: return MLV_InvalidExpression;
322 }
323 if (TR.isConstQualified())
324 return MLV_ConstQualified;
325 if (TR->isArrayType())
326 return MLV_ArrayType;
327 if (TR->isIncompleteType())
328 return MLV_IncompleteType;
329
330 if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) {
331 if (r->hasConstFields())
332 return MLV_ConstQualified;
333 }
334 return MLV_Valid;
335}
336
337/// isIntegerConstantExpr - this recursive routine will test if an expression is
338/// an integer constant expression. Note: With the introduction of VLA's in
339/// C99 the result of the sizeof operator is no longer always a constant
340/// expression. The generalization of the wording to include any subexpression
341/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
342/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
343/// "0 || f()" can be treated as a constant expression. In C90 this expression,
344/// occurring in a context requiring a constant, would have been a constraint
345/// violation. FIXME: This routine currently implements C90 semantics.
346/// To properly implement C99 semantics this routine will need to evaluate
347/// expressions involving operators previously mentioned.
348
349/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
350/// comma, etc
351///
352/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
353/// permit this.
354///
355/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
356/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
357/// cast+dereference.
358bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
359 SourceLocation *Loc, bool isEvaluated) const {
360 switch (getStmtClass()) {
361 default:
362 if (Loc) *Loc = getLocStart();
363 return false;
364 case ParenExprClass:
365 return cast<ParenExpr>(this)->getSubExpr()->
366 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
367 case IntegerLiteralClass:
368 Result = cast<IntegerLiteral>(this)->getValue();
369 break;
370 case CharacterLiteralClass: {
371 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
372 Result.zextOrTrunc(Ctx.getTypeSize(getType(), CL->getLoc()));
373 Result = CL->getValue();
374 Result.setIsUnsigned(!getType()->isSignedIntegerType());
375 break;
376 }
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000377 case TypesCompatibleExprClass: {
378 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
379 Result.zextOrTrunc(Ctx.getTypeSize(getType(), TCE->getLocStart()));
380 Result = TCE->typesAreCompatible();
Steve Naroff1200b5a2007-08-02 00:13:27 +0000381 break;
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000382 }
Steve Naroff8d3b1702007-08-08 22:15:55 +0000383 case CallExprClass: {
384 const CallExpr *CE = cast<CallExpr>(this);
385 Result.zextOrTrunc(Ctx.getTypeSize(getType(), CE->getLocStart()));
386 if (CE->isBuiltinClassifyType(Result))
387 break;
388 if (Loc) *Loc = getLocStart();
389 return false;
390 }
Chris Lattner4b009652007-07-25 00:24:17 +0000391 case DeclRefExprClass:
392 if (const EnumConstantDecl *D =
393 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
394 Result = D->getInitVal();
395 break;
396 }
397 if (Loc) *Loc = getLocStart();
398 return false;
399 case UnaryOperatorClass: {
400 const UnaryOperator *Exp = cast<UnaryOperator>(this);
401
402 // Get the operand value. If this is sizeof/alignof, do not evalute the
403 // operand. This affects C99 6.6p3.
404 if (Exp->isSizeOfAlignOfOp()) isEvaluated = false;
405 if (!Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx,Loc, isEvaluated))
406 return false;
407
408 switch (Exp->getOpcode()) {
409 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
410 // See C99 6.6p3.
411 default:
412 if (Loc) *Loc = Exp->getOperatorLoc();
413 return false;
414 case UnaryOperator::Extension:
415 return true; // FIXME: this is wrong.
416 case UnaryOperator::SizeOf:
417 case UnaryOperator::AlignOf:
418 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
419 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx, Loc))
420 return false;
421
422 // Return the result in the right width.
423 Result.zextOrTrunc(Ctx.getTypeSize(getType(), Exp->getOperatorLoc()));
424
425 // Get information about the size or align.
426 if (Exp->getOpcode() == UnaryOperator::SizeOf)
427 Result = Ctx.getTypeSize(Exp->getSubExpr()->getType(),
428 Exp->getOperatorLoc());
429 else
430 Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType(),
431 Exp->getOperatorLoc());
432 break;
433 case UnaryOperator::LNot: {
434 bool Val = Result != 0;
435 Result.zextOrTrunc(Ctx.getTypeSize(getType(), Exp->getOperatorLoc()));
436 Result = Val;
437 break;
438 }
439 case UnaryOperator::Plus:
440 break;
441 case UnaryOperator::Minus:
442 Result = -Result;
443 break;
444 case UnaryOperator::Not:
445 Result = ~Result;
446 break;
447 }
448 break;
449 }
450 case SizeOfAlignOfTypeExprClass: {
451 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
452 // alignof always evaluates to a constant.
453 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx,Loc))
454 return false;
455
456 // Return the result in the right width.
457 Result.zextOrTrunc(Ctx.getTypeSize(getType(), Exp->getOperatorLoc()));
458
459 // Get information about the size or align.
460 if (Exp->isSizeOf())
461 Result = Ctx.getTypeSize(Exp->getArgumentType(), Exp->getOperatorLoc());
462 else
463 Result = Ctx.getTypeAlign(Exp->getArgumentType(), Exp->getOperatorLoc());
464 break;
465 }
466 case BinaryOperatorClass: {
467 const BinaryOperator *Exp = cast<BinaryOperator>(this);
468
469 // The LHS of a constant expr is always evaluated and needed.
470 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
471 return false;
472
473 llvm::APSInt RHS(Result);
474
475 // The short-circuiting &&/|| operators don't necessarily evaluate their
476 // RHS. Make sure to pass isEvaluated down correctly.
477 if (Exp->isLogicalOp()) {
478 bool RHSEval;
479 if (Exp->getOpcode() == BinaryOperator::LAnd)
480 RHSEval = Result != 0;
481 else {
482 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
483 RHSEval = Result == 0;
484 }
485
486 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
487 isEvaluated & RHSEval))
488 return false;
489 } else {
490 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
491 return false;
492 }
493
494 switch (Exp->getOpcode()) {
495 default:
496 if (Loc) *Loc = getLocStart();
497 return false;
498 case BinaryOperator::Mul:
499 Result *= RHS;
500 break;
501 case BinaryOperator::Div:
502 if (RHS == 0) {
503 if (!isEvaluated) break;
504 if (Loc) *Loc = getLocStart();
505 return false;
506 }
507 Result /= RHS;
508 break;
509 case BinaryOperator::Rem:
510 if (RHS == 0) {
511 if (!isEvaluated) break;
512 if (Loc) *Loc = getLocStart();
513 return false;
514 }
515 Result %= RHS;
516 break;
517 case BinaryOperator::Add: Result += RHS; break;
518 case BinaryOperator::Sub: Result -= RHS; break;
519 case BinaryOperator::Shl:
520 Result <<= RHS.getLimitedValue(Result.getBitWidth()-1);
521 break;
522 case BinaryOperator::Shr:
523 Result >>= RHS.getLimitedValue(Result.getBitWidth()-1);
524 break;
525 case BinaryOperator::LT: Result = Result < RHS; break;
526 case BinaryOperator::GT: Result = Result > RHS; break;
527 case BinaryOperator::LE: Result = Result <= RHS; break;
528 case BinaryOperator::GE: Result = Result >= RHS; break;
529 case BinaryOperator::EQ: Result = Result == RHS; break;
530 case BinaryOperator::NE: Result = Result != RHS; break;
531 case BinaryOperator::And: Result &= RHS; break;
532 case BinaryOperator::Xor: Result ^= RHS; break;
533 case BinaryOperator::Or: Result |= RHS; break;
534 case BinaryOperator::LAnd:
535 Result = Result != 0 && RHS != 0;
536 break;
537 case BinaryOperator::LOr:
538 Result = Result != 0 || RHS != 0;
539 break;
540
541 case BinaryOperator::Comma:
542 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
543 // *except* when they are contained within a subexpression that is not
544 // evaluated". Note that Assignment can never happen due to constraints
545 // on the LHS subexpr, so we don't need to check it here.
546 if (isEvaluated) {
547 if (Loc) *Loc = getLocStart();
548 return false;
549 }
550
551 // The result of the constant expr is the RHS.
552 Result = RHS;
553 return true;
554 }
555
556 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
557 break;
558 }
559 case ImplicitCastExprClass:
560 case CastExprClass: {
561 const Expr *SubExpr;
562 SourceLocation CastLoc;
563 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
564 SubExpr = C->getSubExpr();
565 CastLoc = C->getLParenLoc();
566 } else {
567 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
568 CastLoc = getLocStart();
569 }
570
571 // C99 6.6p6: shall only convert arithmetic types to integer types.
572 if (!SubExpr->getType()->isArithmeticType() ||
573 !getType()->isIntegerType()) {
574 if (Loc) *Loc = SubExpr->getLocStart();
575 return false;
576 }
577
578 // Handle simple integer->integer casts.
579 if (SubExpr->getType()->isIntegerType()) {
580 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
581 return false;
582
583 // Figure out if this is a truncate, extend or noop cast.
584 unsigned DestWidth = Ctx.getTypeSize(getType(), CastLoc);
585
586 // If the input is signed, do a sign extend, noop, or truncate.
587 if (SubExpr->getType()->isSignedIntegerType())
588 Result.sextOrTrunc(DestWidth);
589 else // If the input is unsigned, do a zero extend, noop, or truncate.
590 Result.zextOrTrunc(DestWidth);
591 break;
592 }
593
594 // Allow floating constants that are the immediate operands of casts or that
595 // are parenthesized.
596 const Expr *Operand = SubExpr;
597 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
598 Operand = PE->getSubExpr();
599
600 if (const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand)) {
601 // FIXME: Evaluate this correctly!
602 Result = (int)FL->getValue();
603 break;
604 }
605 if (Loc) *Loc = Operand->getLocStart();
606 return false;
607 }
608 case ConditionalOperatorClass: {
609 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
610
611 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
612 return false;
613
614 const Expr *TrueExp = Exp->getLHS();
615 const Expr *FalseExp = Exp->getRHS();
616 if (Result == 0) std::swap(TrueExp, FalseExp);
617
618 // Evaluate the false one first, discard the result.
619 if (!FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
620 return false;
621 // Evalute the true one, capture the result.
622 if (!TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
623 return false;
624 break;
625 }
626 }
627
628 // Cases that are valid constant exprs fall through to here.
629 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
630 return true;
631}
632
633
634/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
635/// integer constant expression with the value zero, or if this is one that is
636/// cast to void*.
637bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
638 // Strip off a cast to void*, if it exists.
639 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
640 // Check that it is a cast to void*.
641 if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) {
642 QualType Pointee = PT->getPointeeType();
643 if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void*
644 CE->getSubExpr()->getType()->isIntegerType()) // from int.
645 return CE->getSubExpr()->isNullPointerConstant(Ctx);
646 }
647 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
648 // Accept ((void*)0) as a null pointer constant, as many other
649 // implementations do.
650 return PE->getSubExpr()->isNullPointerConstant(Ctx);
651 }
652
653 // This expression must be an integer type.
654 if (!getType()->isIntegerType())
655 return false;
656
657 // If we have an integer constant expression, we need to *evaluate* it and
658 // test for the value 0.
659 llvm::APSInt Val(32);
660 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
661}
Steve Naroffc11705f2007-07-28 23:10:27 +0000662
Chris Lattnera0d03a72007-08-03 17:31:20 +0000663unsigned OCUVectorElementExpr::getNumElements() const {
Chris Lattner50547852007-08-03 16:00:20 +0000664 return strlen(Accessor.getName());
665}
666
667
Chris Lattnerf4bf5512007-08-02 21:47:28 +0000668/// getComponentType - Determine whether the components of this access are
669/// "point" "color" or "texture" elements.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000670OCUVectorElementExpr::ElementType
671OCUVectorElementExpr::getElementType() const {
Steve Naroffc11705f2007-07-28 23:10:27 +0000672 // derive the component type, no need to waste space.
673 const char *compStr = Accessor.getName();
Chris Lattnerabf25b32007-08-02 22:20:00 +0000674
Chris Lattner9096b792007-08-02 22:33:49 +0000675 if (OCUVectorType::getPointAccessorIdx(*compStr) != -1) return Point;
676 if (OCUVectorType::getColorAccessorIdx(*compStr) != -1) return Color;
Chris Lattnerabf25b32007-08-02 22:20:00 +0000677
Chris Lattner9096b792007-08-02 22:33:49 +0000678 assert(OCUVectorType::getTextureAccessorIdx(*compStr) != -1 &&
Chris Lattnerabf25b32007-08-02 22:20:00 +0000679 "getComponentType(): Illegal accessor");
680 return Texture;
Steve Naroffc11705f2007-07-28 23:10:27 +0000681}
Steve Naroffba67f692007-07-30 03:29:09 +0000682
Chris Lattnera0d03a72007-08-03 17:31:20 +0000683/// containsDuplicateElements - Return true if any element access is
Chris Lattnerf4bf5512007-08-02 21:47:28 +0000684/// repeated.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000685bool OCUVectorElementExpr::containsDuplicateElements() const {
Steve Naroffba67f692007-07-30 03:29:09 +0000686 const char *compStr = Accessor.getName();
687 unsigned length = strlen(compStr);
688
689 for (unsigned i = 0; i < length-1; i++) {
690 const char *s = compStr+i;
691 for (const char c = *s++; *s; s++)
692 if (c == *s)
693 return true;
694 }
695 return false;
696}
Chris Lattner42158e72007-08-02 23:36:59 +0000697
698/// getEncodedElementAccess - We encode fields with two bits per component.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000699unsigned OCUVectorElementExpr::getEncodedElementAccess() const {
Chris Lattner42158e72007-08-02 23:36:59 +0000700 const char *compStr = Accessor.getName();
Chris Lattnera0d03a72007-08-03 17:31:20 +0000701 unsigned length = getNumElements();
Chris Lattner42158e72007-08-02 23:36:59 +0000702
703 unsigned Result = 0;
704
705 while (length--) {
706 Result <<= 2;
707 int Idx = OCUVectorType::getAccessorIdx(compStr[length]);
708 assert(Idx != -1 && "Invalid accessor letter");
709 Result |= Idx;
710 }
711 return Result;
712}
713