blob: 726e4939cc7ec6779f707f591569d0001883a3e4 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
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"
Chris Lattner2fd1c652007-10-07 08:58:51 +000017#include "clang/Basic/IdentifierTable.h"
Chris Lattnerd9ffbc92007-11-27 18:22:04 +000018#include "clang/Basic/TargetInfo.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019using namespace clang;
20
21//===----------------------------------------------------------------------===//
22// Primary Expressions.
23//===----------------------------------------------------------------------===//
24
25StringLiteral::StringLiteral(const char *strData, unsigned byteLength,
26 bool Wide, QualType t, SourceLocation firstLoc,
27 SourceLocation lastLoc) :
28 Expr(StringLiteralClass, t) {
29 // OPTIMIZE: could allocate this appended to the StringLiteral.
30 char *AStrData = new char[byteLength];
31 memcpy(AStrData, strData, byteLength);
32 StrData = AStrData;
33 ByteLength = byteLength;
34 IsWide = Wide;
35 firstTokLoc = firstLoc;
36 lastTokLoc = lastLoc;
37}
38
39StringLiteral::~StringLiteral() {
40 delete[] StrData;
41}
42
43bool UnaryOperator::isPostfix(Opcode Op) {
44 switch (Op) {
45 case PostInc:
46 case PostDec:
47 return true;
48 default:
49 return false;
50 }
51}
52
53/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
54/// corresponds to, e.g. "sizeof" or "[pre]++".
55const char *UnaryOperator::getOpcodeStr(Opcode Op) {
56 switch (Op) {
57 default: assert(0 && "Unknown unary operator");
58 case PostInc: return "++";
59 case PostDec: return "--";
60 case PreInc: return "++";
61 case PreDec: return "--";
62 case AddrOf: return "&";
63 case Deref: return "*";
64 case Plus: return "+";
65 case Minus: return "-";
66 case Not: return "~";
67 case LNot: return "!";
68 case Real: return "__real";
69 case Imag: return "__imag";
70 case SizeOf: return "sizeof";
71 case AlignOf: return "alignof";
72 case Extension: return "__extension__";
Chris Lattner0d9bcea2007-08-30 17:45:32 +000073 case OffsetOf: return "__builtin_offsetof";
Chris Lattner4b009652007-07-25 00:24:17 +000074 }
75}
76
77//===----------------------------------------------------------------------===//
78// Postfix Operators.
79//===----------------------------------------------------------------------===//
80
81CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
82 SourceLocation rparenloc)
Ted Kremeneke4acb9c2007-08-24 18:13:47 +000083 : Expr(CallExprClass, t), NumArgs(numargs) {
84 SubExprs = new Expr*[numargs+1];
85 SubExprs[FN] = fn;
Chris Lattner4b009652007-07-25 00:24:17 +000086 for (unsigned i = 0; i != numargs; ++i)
Ted Kremeneke4acb9c2007-08-24 18:13:47 +000087 SubExprs[i+ARGS_START] = args[i];
Chris Lattner4b009652007-07-25 00:24:17 +000088 RParenLoc = rparenloc;
89}
90
Chris Lattnerc257c0d2007-12-28 05:25:02 +000091/// setNumArgs - This changes the number of arguments present in this call.
92/// Any orphaned expressions are deleted by this, and any new operands are set
93/// to null.
94void CallExpr::setNumArgs(unsigned NumArgs) {
95 // No change, just return.
96 if (NumArgs == getNumArgs()) return;
97
98 // If shrinking # arguments, just delete the extras and forgot them.
99 if (NumArgs < getNumArgs()) {
100 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
101 delete getArg(i);
102 this->NumArgs = NumArgs;
103 return;
104 }
105
106 // Otherwise, we are growing the # arguments. New an bigger argument array.
107 Expr **NewSubExprs = new Expr*[NumArgs+1];
108 // Copy over args.
109 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
110 NewSubExprs[i] = SubExprs[i];
111 // Null out new args.
112 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
113 NewSubExprs[i] = 0;
114
115 delete[] SubExprs;
116 SubExprs = NewSubExprs;
117 this->NumArgs = NumArgs;
118}
119
120
Steve Naroff8d3b1702007-08-08 22:15:55 +0000121bool CallExpr::isBuiltinClassifyType(llvm::APSInt &Result) const {
122 // The following enum mimics gcc's internal "typeclass.h" file.
123 enum gcc_type_class {
124 no_type_class = -1,
125 void_type_class, integer_type_class, char_type_class,
126 enumeral_type_class, boolean_type_class,
127 pointer_type_class, reference_type_class, offset_type_class,
128 real_type_class, complex_type_class,
129 function_type_class, method_type_class,
130 record_type_class, union_type_class,
131 array_type_class, string_type_class,
132 lang_type_class
133 };
134 Result.setIsSigned(true);
135
136 // All simple function calls (e.g. func()) are implicitly cast to pointer to
137 // function. As a result, we try and obtain the DeclRefExpr from the
138 // ImplicitCastExpr.
139 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
140 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
141 return false;
142 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
143 if (!DRE)
144 return false;
145
146 // We have a DeclRefExpr.
147 if (strcmp(DRE->getDecl()->getName(), "__builtin_classify_type") == 0) {
148 // If no argument was supplied, default to "no_type_class". This isn't
149 // ideal, however it's what gcc does.
150 Result = static_cast<uint64_t>(no_type_class);
151 if (NumArgs >= 1) {
152 QualType argType = getArg(0)->getType();
153
154 if (argType->isVoidType())
155 Result = void_type_class;
156 else if (argType->isEnumeralType())
157 Result = enumeral_type_class;
158 else if (argType->isBooleanType())
159 Result = boolean_type_class;
160 else if (argType->isCharType())
161 Result = string_type_class; // gcc doesn't appear to use char_type_class
162 else if (argType->isIntegerType())
163 Result = integer_type_class;
164 else if (argType->isPointerType())
165 Result = pointer_type_class;
166 else if (argType->isReferenceType())
167 Result = reference_type_class;
168 else if (argType->isRealType())
169 Result = real_type_class;
170 else if (argType->isComplexType())
171 Result = complex_type_class;
172 else if (argType->isFunctionType())
173 Result = function_type_class;
174 else if (argType->isStructureType())
175 Result = record_type_class;
176 else if (argType->isUnionType())
177 Result = union_type_class;
178 else if (argType->isArrayType())
179 Result = array_type_class;
180 else if (argType->isUnionType())
181 Result = union_type_class;
182 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
Chris Lattner19b8f1a2007-11-08 17:56:40 +0000183 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
Steve Naroff8d3b1702007-08-08 22:15:55 +0000184 }
185 return true;
186 }
187 return false;
188}
189
Chris Lattner4b009652007-07-25 00:24:17 +0000190/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
191/// corresponds to, e.g. "<<=".
192const char *BinaryOperator::getOpcodeStr(Opcode Op) {
193 switch (Op) {
194 default: assert(0 && "Unknown binary operator");
195 case Mul: return "*";
196 case Div: return "/";
197 case Rem: return "%";
198 case Add: return "+";
199 case Sub: return "-";
200 case Shl: return "<<";
201 case Shr: return ">>";
202 case LT: return "<";
203 case GT: return ">";
204 case LE: return "<=";
205 case GE: return ">=";
206 case EQ: return "==";
207 case NE: return "!=";
208 case And: return "&";
209 case Xor: return "^";
210 case Or: return "|";
211 case LAnd: return "&&";
212 case LOr: return "||";
213 case Assign: return "=";
214 case MulAssign: return "*=";
215 case DivAssign: return "/=";
216 case RemAssign: return "%=";
217 case AddAssign: return "+=";
218 case SubAssign: return "-=";
219 case ShlAssign: return "<<=";
220 case ShrAssign: return ">>=";
221 case AndAssign: return "&=";
222 case XorAssign: return "^=";
223 case OrAssign: return "|=";
224 case Comma: return ",";
225 }
226}
227
Anders Carlsson762b7c72007-08-31 04:56:16 +0000228InitListExpr::InitListExpr(SourceLocation lbraceloc,
229 Expr **initexprs, unsigned numinits,
230 SourceLocation rbraceloc)
231 : Expr(InitListExprClass, QualType())
232 , NumInits(numinits)
233 , LBraceLoc(lbraceloc)
234 , RBraceLoc(rbraceloc)
235{
236 InitExprs = new Expr*[numinits];
237 for (unsigned i = 0; i != numinits; i++)
238 InitExprs[i] = initexprs[i];
239}
Chris Lattner4b009652007-07-25 00:24:17 +0000240
241//===----------------------------------------------------------------------===//
242// Generic Expression Routines
243//===----------------------------------------------------------------------===//
244
245/// hasLocalSideEffect - Return true if this immediate expression has side
246/// effects, not counting any sub-expressions.
247bool Expr::hasLocalSideEffect() const {
248 switch (getStmtClass()) {
249 default:
250 return false;
251 case ParenExprClass:
252 return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect();
253 case UnaryOperatorClass: {
254 const UnaryOperator *UO = cast<UnaryOperator>(this);
255
256 switch (UO->getOpcode()) {
257 default: return false;
258 case UnaryOperator::PostInc:
259 case UnaryOperator::PostDec:
260 case UnaryOperator::PreInc:
261 case UnaryOperator::PreDec:
262 return true; // ++/--
263
264 case UnaryOperator::Deref:
265 // Dereferencing a volatile pointer is a side-effect.
266 return getType().isVolatileQualified();
267 case UnaryOperator::Real:
268 case UnaryOperator::Imag:
269 // accessing a piece of a volatile complex is a side-effect.
270 return UO->getSubExpr()->getType().isVolatileQualified();
271
272 case UnaryOperator::Extension:
273 return UO->getSubExpr()->hasLocalSideEffect();
274 }
275 }
Chris Lattneref95ffd2007-12-01 06:07:34 +0000276 case BinaryOperatorClass: {
277 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
278 // Consider comma to have side effects if the LHS and RHS both do.
279 if (BinOp->getOpcode() == BinaryOperator::Comma)
280 return BinOp->getLHS()->hasLocalSideEffect() &&
281 BinOp->getRHS()->hasLocalSideEffect();
282
283 return BinOp->isAssignmentOp();
284 }
Chris Lattner06078d22007-08-25 02:00:02 +0000285 case CompoundAssignOperatorClass:
Chris Lattner9c0da3b2007-08-25 01:55:00 +0000286 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000287
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000288 case ConditionalOperatorClass: {
289 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
290 return Exp->getCond()->hasLocalSideEffect()
291 || (Exp->getLHS() && Exp->getLHS()->hasLocalSideEffect())
292 || (Exp->getRHS() && Exp->getRHS()->hasLocalSideEffect());
293 }
294
Chris Lattner4b009652007-07-25 00:24:17 +0000295 case MemberExprClass:
296 case ArraySubscriptExprClass:
297 // If the base pointer or element is to a volatile pointer/field, accessing
298 // if is a side effect.
299 return getType().isVolatileQualified();
300
301 case CallExprClass:
302 // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }"
303 // should warn.
304 return true;
Chris Lattner99f5f0b2007-09-26 22:06:30 +0000305 case ObjCMessageExprClass:
306 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000307
308 case CastExprClass:
309 // If this is a cast to void, check the operand. Otherwise, the result of
310 // the cast is unused.
311 if (getType()->isVoidType())
312 return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect();
313 return false;
314 }
315}
316
317/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
318/// incomplete type other than void. Nonarray expressions that can be lvalues:
319/// - name, where name must be a variable
320/// - e[i]
321/// - (e), where e must be an lvalue
322/// - e.name, where e must be an lvalue
323/// - e->name
324/// - *e, the type of e cannot be a function type
325/// - string-constant
Chris Lattner5bf72022007-10-30 22:53:42 +0000326/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Chris Lattner4b009652007-07-25 00:24:17 +0000327/// - reference type [C++ [expr]]
328///
329Expr::isLvalueResult Expr::isLvalue() const {
330 // first, check the type (C99 6.3.2.1)
331 if (TR->isFunctionType()) // from isObjectType()
332 return LV_NotObjectType;
333
334 if (TR->isVoidType())
335 return LV_IncompleteVoidType;
336
337 if (TR->isReferenceType()) // C++ [expr]
338 return LV_Valid;
339
340 // the type looks fine, now check the expression
341 switch (getStmtClass()) {
342 case StringLiteralClass: // C99 6.5.1p4
Anders Carlsson9e933a22007-11-30 22:47:59 +0000343 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000344 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
345 // For vectors, make sure base is an lvalue (i.e. not a function call).
346 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
347 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue();
348 return LV_Valid;
349 case DeclRefExprClass: // C99 6.5.1p2
350 if (isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl()))
351 return LV_Valid;
352 break;
353 case MemberExprClass: { // C99 6.5.2.3p4
354 const MemberExpr *m = cast<MemberExpr>(this);
355 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue();
356 }
Chris Lattner5bf72022007-10-30 22:53:42 +0000357 case UnaryOperatorClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000358 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner5bf72022007-10-30 22:53:42 +0000359 return LV_Valid; // C99 6.5.3p4
360
361 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
362 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag)
363 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(); // GNU.
Chris Lattner4b009652007-07-25 00:24:17 +0000364 break;
365 case ParenExprClass: // C99 6.5.1p5
366 return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
Steve Naroffc7c66532007-12-05 04:00:10 +0000367 case CompoundLiteralExprClass: // C99 6.5.2.5p5
368 return LV_Valid;
Chris Lattnera0d03a72007-08-03 17:31:20 +0000369 case OCUVectorElementExprClass:
370 if (cast<OCUVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroffba67f692007-07-30 03:29:09 +0000371 return LV_DuplicateVectorComponents;
372 return LV_Valid;
Steve Naroff46f18f22007-11-12 14:34:27 +0000373 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
374 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000375 default:
376 break;
377 }
378 return LV_InvalidExpression;
379}
380
381/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
382/// does not have an incomplete type, does not have a const-qualified type, and
383/// if it is a structure or union, does not have any member (including,
384/// recursively, any member or element of all contained aggregates or unions)
385/// with a const-qualified type.
386Expr::isModifiableLvalueResult Expr::isModifiableLvalue() const {
387 isLvalueResult lvalResult = isLvalue();
388
389 switch (lvalResult) {
390 case LV_Valid: break;
391 case LV_NotObjectType: return MLV_NotObjectType;
392 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroffba67f692007-07-30 03:29:09 +0000393 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner4b009652007-07-25 00:24:17 +0000394 case LV_InvalidExpression: return MLV_InvalidExpression;
395 }
396 if (TR.isConstQualified())
397 return MLV_ConstQualified;
398 if (TR->isArrayType())
399 return MLV_ArrayType;
400 if (TR->isIncompleteType())
401 return MLV_IncompleteType;
402
403 if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) {
404 if (r->hasConstFields())
405 return MLV_ConstQualified;
406 }
407 return MLV_Valid;
408}
409
Chris Lattner743ec372007-11-27 21:35:27 +0000410/// hasStaticStorage - Return true if this expression has static storage
411/// duration. This means that the address of this expression is a link-time
412/// constant.
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000413bool Expr::hasStaticStorage() const {
414 switch (getStmtClass()) {
415 default:
416 return false;
Chris Lattner743ec372007-11-27 21:35:27 +0000417 case ParenExprClass:
418 return cast<ParenExpr>(this)->getSubExpr()->hasStaticStorage();
419 case ImplicitCastExprClass:
420 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasStaticStorage();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000421 case DeclRefExprClass: {
422 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
423 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
424 return VD->hasStaticStorage();
425 return false;
426 }
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000427 case MemberExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000428 const MemberExpr *M = cast<MemberExpr>(this);
429 return !M->isArrow() && M->getBase()->hasStaticStorage();
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000430 }
Chris Lattner743ec372007-11-27 21:35:27 +0000431 case ArraySubscriptExprClass:
432 return cast<ArraySubscriptExpr>(this)->getBase()->hasStaticStorage();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000433 }
434}
435
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000436bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000437 switch (getStmtClass()) {
438 default:
439 if (Loc) *Loc = getLocStart();
440 return false;
441 case ParenExprClass:
442 return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(Ctx, Loc);
443 case StringLiteralClass:
Steve Naroff4fdea132007-11-09 15:00:03 +0000444 case ObjCStringLiteralClass:
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000445 case FloatingLiteralClass:
446 case IntegerLiteralClass:
447 case CharacterLiteralClass:
448 case ImaginaryLiteralClass:
Anders Carlsson855d78d2007-10-17 00:52:43 +0000449 case TypesCompatibleExprClass:
450 case CXXBoolLiteralExprClass:
Chris Lattner06db6132007-10-18 00:20:32 +0000451 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000452 case CallExprClass: {
453 const CallExpr *CE = cast<CallExpr>(this);
454 llvm::APSInt Result(32);
Hartmut Kaiser38af7012007-09-16 21:35:35 +0000455 Result.zextOrTrunc(
456 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000457 if (CE->isBuiltinClassifyType(Result))
Chris Lattner06db6132007-10-18 00:20:32 +0000458 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000459 if (Loc) *Loc = getLocStart();
460 return false;
461 }
Chris Lattner42e86b62007-11-01 02:45:17 +0000462 case DeclRefExprClass: {
463 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
464 // Accept address of function.
465 if (isa<EnumConstantDecl>(D) || isa<FunctionDecl>(D))
Chris Lattner06db6132007-10-18 00:20:32 +0000466 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000467 if (Loc) *Loc = getLocStart();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000468 if (isa<VarDecl>(D))
469 return TR->isArrayType();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000470 return false;
Chris Lattner42e86b62007-11-01 02:45:17 +0000471 }
Steve Narofff91f9722008-01-09 00:05:37 +0000472 case CompoundLiteralExprClass:
473 if (Loc) *Loc = getLocStart();
474 // Allow "(int []){2,4}", since the array will be converted to a pointer.
475 return TR->isArrayType();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000476 case UnaryOperatorClass: {
477 const UnaryOperator *Exp = cast<UnaryOperator>(this);
478
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000479 // C99 6.6p9
Chris Lattner35b662f2007-12-11 23:11:17 +0000480 if (Exp->getOpcode() == UnaryOperator::AddrOf) {
481 if (!Exp->getSubExpr()->hasStaticStorage()) {
482 if (Loc) *Loc = getLocStart();
483 return false;
484 }
485 return true;
486 }
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000487
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000488 // Get the operand value. If this is sizeof/alignof, do not evalute the
489 // operand. This affects C99 6.6p3.
490 if (!Exp->isSizeOfAlignOfOp() &&
491 !Exp->getSubExpr()->isConstantExpr(Ctx, Loc))
492 return false;
493
494 switch (Exp->getOpcode()) {
495 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
496 // See C99 6.6p3.
497 default:
498 if (Loc) *Loc = Exp->getOperatorLoc();
499 return false;
500 case UnaryOperator::Extension:
501 return true; // FIXME: this is wrong.
502 case UnaryOperator::SizeOf:
503 case UnaryOperator::AlignOf:
504 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000505 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx)) {
506 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000507 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000508 }
Chris Lattner06db6132007-10-18 00:20:32 +0000509 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000510 case UnaryOperator::LNot:
511 case UnaryOperator::Plus:
512 case UnaryOperator::Minus:
513 case UnaryOperator::Not:
Chris Lattner06db6132007-10-18 00:20:32 +0000514 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000515 }
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000516 }
517 case SizeOfAlignOfTypeExprClass: {
518 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
519 // alignof always evaluates to a constant.
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000520 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx)) {
521 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000522 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000523 }
Chris Lattner06db6132007-10-18 00:20:32 +0000524 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000525 }
526 case BinaryOperatorClass: {
527 const BinaryOperator *Exp = cast<BinaryOperator>(this);
528
529 // The LHS of a constant expr is always evaluated and needed.
530 if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
531 return false;
532
533 if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
534 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000535 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000536 }
537 case ImplicitCastExprClass:
538 case CastExprClass: {
539 const Expr *SubExpr;
540 SourceLocation CastLoc;
541 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
542 SubExpr = C->getSubExpr();
543 CastLoc = C->getLParenLoc();
544 } else {
545 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
546 CastLoc = getLocStart();
547 }
548 if (!SubExpr->isConstantExpr(Ctx, Loc)) {
549 if (Loc) *Loc = SubExpr->getLocStart();
550 return false;
551 }
Chris Lattner06db6132007-10-18 00:20:32 +0000552 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000553 }
554 case ConditionalOperatorClass: {
555 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Chris Lattner06db6132007-10-18 00:20:32 +0000556 if (!Exp->getCond()->isConstantExpr(Ctx, Loc) ||
Anders Carlsson37365fc2007-11-30 19:04:31 +0000557 // Handle the GNU extension for missing LHS.
558 !(Exp->getLHS() && Exp->getLHS()->isConstantExpr(Ctx, Loc)) ||
Chris Lattner06db6132007-10-18 00:20:32 +0000559 !Exp->getRHS()->isConstantExpr(Ctx, Loc))
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000560 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000561 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000562 }
563 }
564
565 return true;
566}
567
Chris Lattner4b009652007-07-25 00:24:17 +0000568/// isIntegerConstantExpr - this recursive routine will test if an expression is
569/// an integer constant expression. Note: With the introduction of VLA's in
570/// C99 the result of the sizeof operator is no longer always a constant
571/// expression. The generalization of the wording to include any subexpression
572/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
573/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
574/// "0 || f()" can be treated as a constant expression. In C90 this expression,
575/// occurring in a context requiring a constant, would have been a constraint
576/// violation. FIXME: This routine currently implements C90 semantics.
577/// To properly implement C99 semantics this routine will need to evaluate
578/// expressions involving operators previously mentioned.
579
580/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
581/// comma, etc
582///
583/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattner9d020b32007-09-26 00:47:26 +0000584/// permit this. This includes things like (int)1e1000
Chris Lattner4b009652007-07-25 00:24:17 +0000585///
586/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
587/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
588/// cast+dereference.
589bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
590 SourceLocation *Loc, bool isEvaluated) const {
591 switch (getStmtClass()) {
592 default:
593 if (Loc) *Loc = getLocStart();
594 return false;
595 case ParenExprClass:
596 return cast<ParenExpr>(this)->getSubExpr()->
597 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
598 case IntegerLiteralClass:
599 Result = cast<IntegerLiteral>(this)->getValue();
600 break;
601 case CharacterLiteralClass: {
602 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000603 Result.zextOrTrunc(
604 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CL->getLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000605 Result = CL->getValue();
606 Result.setIsUnsigned(!getType()->isSignedIntegerType());
607 break;
608 }
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000609 case TypesCompatibleExprClass: {
610 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000611 Result.zextOrTrunc(
612 static_cast<uint32_t>(Ctx.getTypeSize(getType(), TCE->getLocStart())));
Steve Naroff85f0dc52007-10-15 20:41:53 +0000613 Result = Ctx.typesAreCompatible(TCE->getArgType1(), TCE->getArgType2());
Steve Naroff1200b5a2007-08-02 00:13:27 +0000614 break;
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000615 }
Steve Naroff8d3b1702007-08-08 22:15:55 +0000616 case CallExprClass: {
617 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000618 Result.zextOrTrunc(
619 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff8d3b1702007-08-08 22:15:55 +0000620 if (CE->isBuiltinClassifyType(Result))
621 break;
622 if (Loc) *Loc = getLocStart();
623 return false;
624 }
Chris Lattner4b009652007-07-25 00:24:17 +0000625 case DeclRefExprClass:
626 if (const EnumConstantDecl *D =
627 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
628 Result = D->getInitVal();
629 break;
630 }
631 if (Loc) *Loc = getLocStart();
632 return false;
633 case UnaryOperatorClass: {
634 const UnaryOperator *Exp = cast<UnaryOperator>(this);
635
636 // Get the operand value. If this is sizeof/alignof, do not evalute the
637 // operand. This affects C99 6.6p3.
Chris Lattner5a9b6242007-08-23 21:42:50 +0000638 if (!Exp->isSizeOfAlignOfOp() &&
639 !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000640 return false;
641
642 switch (Exp->getOpcode()) {
643 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
644 // See C99 6.6p3.
645 default:
646 if (Loc) *Loc = Exp->getOperatorLoc();
647 return false;
648 case UnaryOperator::Extension:
649 return true; // FIXME: this is wrong.
650 case UnaryOperator::SizeOf:
651 case UnaryOperator::AlignOf:
652 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000653 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx)) {
654 if (Loc) *Loc = Exp->getOperatorLoc();
Chris Lattner4b009652007-07-25 00:24:17 +0000655 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000656 }
Chris Lattner4b009652007-07-25 00:24:17 +0000657
658 // Return the result in the right width.
Chris Lattner3496d522007-09-04 02:45:27 +0000659 Result.zextOrTrunc(
Chris Lattner9d020b32007-09-26 00:47:26 +0000660 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
661 Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000662
663 // Get information about the size or align.
Chris Lattnerd4dc2672008-01-02 21:54:09 +0000664 if (Exp->getSubExpr()->getType()->isFunctionType()) {
665 // GCC extension: sizeof(function) = 1.
666 Result = Exp->getOpcode() == UnaryOperator::AlignOf ? 4 : 1;
667 } else if (Exp->getOpcode() == UnaryOperator::AlignOf) {
Chris Lattner4b009652007-07-25 00:24:17 +0000668 Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType(),
669 Exp->getOperatorLoc());
Chris Lattnerd9ffbc92007-11-27 18:22:04 +0000670 } else {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000671 unsigned CharSize =
672 Ctx.Target.getCharWidth(Ctx.getFullLoc(Exp->getOperatorLoc()));
673
Chris Lattnerd9ffbc92007-11-27 18:22:04 +0000674 Result = Ctx.getTypeSize(Exp->getSubExpr()->getType(),
675 Exp->getOperatorLoc()) / CharSize;
676 }
Chris Lattner4b009652007-07-25 00:24:17 +0000677 break;
678 case UnaryOperator::LNot: {
679 bool Val = Result != 0;
Chris Lattner3496d522007-09-04 02:45:27 +0000680 Result.zextOrTrunc(
Chris Lattner9d020b32007-09-26 00:47:26 +0000681 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
682 Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000683 Result = Val;
684 break;
685 }
686 case UnaryOperator::Plus:
687 break;
688 case UnaryOperator::Minus:
689 Result = -Result;
690 break;
691 case UnaryOperator::Not:
692 Result = ~Result;
693 break;
694 }
695 break;
696 }
697 case SizeOfAlignOfTypeExprClass: {
698 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
699 // alignof always evaluates to a constant.
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000700 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx)) {
701 if (Loc) *Loc = Exp->getOperatorLoc();
Chris Lattner4b009652007-07-25 00:24:17 +0000702 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000703 }
Chris Lattner4b009652007-07-25 00:24:17 +0000704
705 // Return the result in the right width.
Chris Lattner3496d522007-09-04 02:45:27 +0000706 Result.zextOrTrunc(
707 static_cast<uint32_t>(Ctx.getTypeSize(getType(), Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000708
709 // Get information about the size or align.
Chris Lattnerd4dc2672008-01-02 21:54:09 +0000710 if (Exp->getArgumentType()->isFunctionType()) {
711 // GCC extension: sizeof(function) = 1.
712 Result = Exp->isSizeOf() ? 1 : 4;
713 } else if (Exp->isSizeOf()) {
Ted Kremeneka9d0fd92007-12-17 17:38:43 +0000714 unsigned CharSize =
715 Ctx.Target.getCharWidth(Ctx.getFullLoc(Exp->getOperatorLoc()));
716
717 Result = Ctx.getTypeSize(Exp->getArgumentType(),
718 Exp->getOperatorLoc()) / CharSize;
719 }
Chris Lattner4b009652007-07-25 00:24:17 +0000720 else
721 Result = Ctx.getTypeAlign(Exp->getArgumentType(), Exp->getOperatorLoc());
Ted Kremeneka9d0fd92007-12-17 17:38:43 +0000722
Chris Lattner4b009652007-07-25 00:24:17 +0000723 break;
724 }
725 case BinaryOperatorClass: {
726 const BinaryOperator *Exp = cast<BinaryOperator>(this);
727
728 // The LHS of a constant expr is always evaluated and needed.
729 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
730 return false;
731
732 llvm::APSInt RHS(Result);
733
734 // The short-circuiting &&/|| operators don't necessarily evaluate their
735 // RHS. Make sure to pass isEvaluated down correctly.
736 if (Exp->isLogicalOp()) {
737 bool RHSEval;
738 if (Exp->getOpcode() == BinaryOperator::LAnd)
739 RHSEval = Result != 0;
740 else {
741 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
742 RHSEval = Result == 0;
743 }
744
745 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
746 isEvaluated & RHSEval))
747 return false;
748 } else {
749 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
750 return false;
751 }
752
753 switch (Exp->getOpcode()) {
754 default:
755 if (Loc) *Loc = getLocStart();
756 return false;
757 case BinaryOperator::Mul:
758 Result *= RHS;
759 break;
760 case BinaryOperator::Div:
761 if (RHS == 0) {
762 if (!isEvaluated) break;
763 if (Loc) *Loc = getLocStart();
764 return false;
765 }
766 Result /= RHS;
767 break;
768 case BinaryOperator::Rem:
769 if (RHS == 0) {
770 if (!isEvaluated) break;
771 if (Loc) *Loc = getLocStart();
772 return false;
773 }
774 Result %= RHS;
775 break;
776 case BinaryOperator::Add: Result += RHS; break;
777 case BinaryOperator::Sub: Result -= RHS; break;
778 case BinaryOperator::Shl:
Chris Lattner3496d522007-09-04 02:45:27 +0000779 Result <<=
780 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000781 break;
782 case BinaryOperator::Shr:
Chris Lattner3496d522007-09-04 02:45:27 +0000783 Result >>=
784 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000785 break;
786 case BinaryOperator::LT: Result = Result < RHS; break;
787 case BinaryOperator::GT: Result = Result > RHS; break;
788 case BinaryOperator::LE: Result = Result <= RHS; break;
789 case BinaryOperator::GE: Result = Result >= RHS; break;
790 case BinaryOperator::EQ: Result = Result == RHS; break;
791 case BinaryOperator::NE: Result = Result != RHS; break;
792 case BinaryOperator::And: Result &= RHS; break;
793 case BinaryOperator::Xor: Result ^= RHS; break;
794 case BinaryOperator::Or: Result |= RHS; break;
795 case BinaryOperator::LAnd:
796 Result = Result != 0 && RHS != 0;
797 break;
798 case BinaryOperator::LOr:
799 Result = Result != 0 || RHS != 0;
800 break;
801
802 case BinaryOperator::Comma:
803 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
804 // *except* when they are contained within a subexpression that is not
805 // evaluated". Note that Assignment can never happen due to constraints
806 // on the LHS subexpr, so we don't need to check it here.
807 if (isEvaluated) {
808 if (Loc) *Loc = getLocStart();
809 return false;
810 }
811
812 // The result of the constant expr is the RHS.
813 Result = RHS;
814 return true;
815 }
816
817 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
818 break;
819 }
820 case ImplicitCastExprClass:
821 case CastExprClass: {
822 const Expr *SubExpr;
823 SourceLocation CastLoc;
824 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
825 SubExpr = C->getSubExpr();
826 CastLoc = C->getLParenLoc();
827 } else {
828 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
829 CastLoc = getLocStart();
830 }
831
832 // C99 6.6p6: shall only convert arithmetic types to integer types.
833 if (!SubExpr->getType()->isArithmeticType() ||
834 !getType()->isIntegerType()) {
835 if (Loc) *Loc = SubExpr->getLocStart();
836 return false;
837 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000838
839 uint32_t DestWidth =
840 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CastLoc));
841
Chris Lattner4b009652007-07-25 00:24:17 +0000842 // Handle simple integer->integer casts.
843 if (SubExpr->getType()->isIntegerType()) {
844 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
845 return false;
846
847 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner4b009652007-07-25 00:24:17 +0000848 // If the input is signed, do a sign extend, noop, or truncate.
849 if (SubExpr->getType()->isSignedIntegerType())
850 Result.sextOrTrunc(DestWidth);
851 else // If the input is unsigned, do a zero extend, noop, or truncate.
852 Result.zextOrTrunc(DestWidth);
853 break;
854 }
855
856 // Allow floating constants that are the immediate operands of casts or that
857 // are parenthesized.
858 const Expr *Operand = SubExpr;
859 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
860 Operand = PE->getSubExpr();
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000861
862 // If this isn't a floating literal, we can't handle it.
863 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
864 if (!FL) {
865 if (Loc) *Loc = Operand->getLocStart();
866 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000867 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000868
869 // Determine whether we are converting to unsigned or signed.
870 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattner9d020b32007-09-26 00:47:26 +0000871
872 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
873 // be called multiple times per AST.
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000874 uint64_t Space[4];
Chris Lattner9d020b32007-09-26 00:47:26 +0000875 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
876 llvm::APFloat::rmTowardZero);
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000877 Result = llvm::APInt(DestWidth, 4, Space);
878 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000879 }
880 case ConditionalOperatorClass: {
881 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
882
883 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
884 return false;
885
886 const Expr *TrueExp = Exp->getLHS();
887 const Expr *FalseExp = Exp->getRHS();
888 if (Result == 0) std::swap(TrueExp, FalseExp);
889
890 // Evaluate the false one first, discard the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +0000891 if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Chris Lattner4b009652007-07-25 00:24:17 +0000892 return false;
893 // Evalute the true one, capture the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +0000894 if (TrueExp &&
895 !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000896 return false;
897 break;
898 }
899 }
900
901 // Cases that are valid constant exprs fall through to here.
902 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
903 return true;
904}
905
906
907/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
908/// integer constant expression with the value zero, or if this is one that is
909/// cast to void*.
910bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
911 // Strip off a cast to void*, if it exists.
912 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
913 // Check that it is a cast to void*.
914 if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) {
915 QualType Pointee = PT->getPointeeType();
916 if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void*
917 CE->getSubExpr()->getType()->isIntegerType()) // from int.
918 return CE->getSubExpr()->isNullPointerConstant(Ctx);
919 }
Steve Naroff1d6b2472007-08-28 21:20:34 +0000920 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
Steve Naroff3d052872007-08-29 00:00:02 +0000921 // Ignore the ImplicitCastExpr type entirely.
922 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000923 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
924 // Accept ((void*)0) as a null pointer constant, as many other
925 // implementations do.
926 return PE->getSubExpr()->isNullPointerConstant(Ctx);
927 }
928
929 // This expression must be an integer type.
930 if (!getType()->isIntegerType())
931 return false;
932
933 // If we have an integer constant expression, we need to *evaluate* it and
934 // test for the value 0.
935 llvm::APSInt Val(32);
936 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
937}
Steve Naroffc11705f2007-07-28 23:10:27 +0000938
Chris Lattnera0d03a72007-08-03 17:31:20 +0000939unsigned OCUVectorElementExpr::getNumElements() const {
Chris Lattner50547852007-08-03 16:00:20 +0000940 return strlen(Accessor.getName());
941}
942
943
Chris Lattnerf4bf5512007-08-02 21:47:28 +0000944/// getComponentType - Determine whether the components of this access are
945/// "point" "color" or "texture" elements.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000946OCUVectorElementExpr::ElementType
947OCUVectorElementExpr::getElementType() const {
Steve Naroffc11705f2007-07-28 23:10:27 +0000948 // derive the component type, no need to waste space.
949 const char *compStr = Accessor.getName();
Chris Lattnerabf25b32007-08-02 22:20:00 +0000950
Chris Lattner9096b792007-08-02 22:33:49 +0000951 if (OCUVectorType::getPointAccessorIdx(*compStr) != -1) return Point;
952 if (OCUVectorType::getColorAccessorIdx(*compStr) != -1) return Color;
Chris Lattnerabf25b32007-08-02 22:20:00 +0000953
Chris Lattner9096b792007-08-02 22:33:49 +0000954 assert(OCUVectorType::getTextureAccessorIdx(*compStr) != -1 &&
Chris Lattnerabf25b32007-08-02 22:20:00 +0000955 "getComponentType(): Illegal accessor");
956 return Texture;
Steve Naroffc11705f2007-07-28 23:10:27 +0000957}
Steve Naroffba67f692007-07-30 03:29:09 +0000958
Chris Lattnera0d03a72007-08-03 17:31:20 +0000959/// containsDuplicateElements - Return true if any element access is
Chris Lattnerf4bf5512007-08-02 21:47:28 +0000960/// repeated.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000961bool OCUVectorElementExpr::containsDuplicateElements() const {
Steve Naroffba67f692007-07-30 03:29:09 +0000962 const char *compStr = Accessor.getName();
963 unsigned length = strlen(compStr);
964
965 for (unsigned i = 0; i < length-1; i++) {
966 const char *s = compStr+i;
967 for (const char c = *s++; *s; s++)
968 if (c == *s)
969 return true;
970 }
971 return false;
972}
Chris Lattner42158e72007-08-02 23:36:59 +0000973
974/// getEncodedElementAccess - We encode fields with two bits per component.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000975unsigned OCUVectorElementExpr::getEncodedElementAccess() const {
Chris Lattner42158e72007-08-02 23:36:59 +0000976 const char *compStr = Accessor.getName();
Chris Lattnera0d03a72007-08-03 17:31:20 +0000977 unsigned length = getNumElements();
Chris Lattner42158e72007-08-02 23:36:59 +0000978
979 unsigned Result = 0;
980
981 while (length--) {
982 Result <<= 2;
983 int Idx = OCUVectorType::getAccessorIdx(compStr[length]);
984 assert(Idx != -1 && "Invalid accessor letter");
985 Result |= Idx;
986 }
987 return Result;
988}
989
Steve Naroff4ed9d662007-09-27 14:38:14 +0000990// constructor for instance messages.
Steve Naroff6cb1d362007-09-28 22:22:11 +0000991ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +0000992 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +0000993 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +0000994 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +0000995 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
996 MethodProto(mproto), ClassName(0) {
Steve Naroff9f176d12007-11-15 13:05:42 +0000997 NumArgs = nargs;
998 SubExprs = new Expr*[NumArgs+1];
Steve Naroff4ed9d662007-09-27 14:38:14 +0000999 SubExprs[RECEIVER] = receiver;
Steve Naroff9f176d12007-11-15 13:05:42 +00001000 if (NumArgs) {
1001 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001002 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1003 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001004 LBracloc = LBrac;
1005 RBracloc = RBrac;
1006}
1007
Steve Naroff4ed9d662007-09-27 14:38:14 +00001008// constructor for class messages.
1009// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff6cb1d362007-09-28 22:22:11 +00001010ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001011 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001012 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001013 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001014 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1015 MethodProto(mproto), ClassName(clsName) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001016 NumArgs = nargs;
1017 SubExprs = new Expr*[NumArgs+1];
Steve Naroffc39ca262007-09-18 23:55:05 +00001018 SubExprs[RECEIVER] = 0;
Steve Naroff9f176d12007-11-15 13:05:42 +00001019 if (NumArgs) {
1020 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001021 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1022 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001023 LBracloc = LBrac;
1024 RBracloc = RBrac;
1025}
1026
Chris Lattnerf624cd22007-10-25 00:29:32 +00001027
1028bool ChooseExpr::isConditionTrue(ASTContext &C) const {
1029 llvm::APSInt CondVal(32);
1030 bool IsConst = getCond()->isIntegerConstantExpr(CondVal, C);
1031 assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst;
1032 return CondVal != 0;
1033}
1034
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001035//===----------------------------------------------------------------------===//
1036// Child Iterators for iterating over subexpressions/substatements
1037//===----------------------------------------------------------------------===//
1038
1039// DeclRefExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001040Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1041Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001042
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001043// ObjCIvarRefExpr
1044Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return child_iterator(); }
1045Stmt::child_iterator ObjCIvarRefExpr::child_end() { return child_iterator(); }
1046
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001047// PreDefinedExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001048Stmt::child_iterator PreDefinedExpr::child_begin() { return child_iterator(); }
1049Stmt::child_iterator PreDefinedExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001050
1051// IntegerLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001052Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1053Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001054
1055// CharacterLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001056Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); }
1057Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001058
1059// FloatingLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001060Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1061Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001062
Chris Lattner1de66eb2007-08-26 03:42:43 +00001063// ImaginaryLiteral
1064Stmt::child_iterator ImaginaryLiteral::child_begin() {
1065 return reinterpret_cast<Stmt**>(&Val);
1066}
1067Stmt::child_iterator ImaginaryLiteral::child_end() {
1068 return reinterpret_cast<Stmt**>(&Val)+1;
1069}
1070
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001071// StringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001072Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1073Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001074
1075// ParenExpr
1076Stmt::child_iterator ParenExpr::child_begin() {
1077 return reinterpret_cast<Stmt**>(&Val);
1078}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001079Stmt::child_iterator ParenExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001080 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001081}
1082
1083// UnaryOperator
1084Stmt::child_iterator UnaryOperator::child_begin() {
Ted Kremenek6d7ea522007-12-15 00:39:18 +00001085 return reinterpret_cast<Stmt**>(&Val);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001086}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001087Stmt::child_iterator UnaryOperator::child_end() {
Ted Kremenek6d7ea522007-12-15 00:39:18 +00001088 return reinterpret_cast<Stmt**>(&Val+1);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001089}
1090
1091// SizeOfAlignOfTypeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001092Stmt::child_iterator SizeOfAlignOfTypeExpr::child_begin() {
Ted Kremenekb7cf0952007-12-14 22:52:23 +00001093 // If the type is a VLA type (and not a typedef), the size expression of the
1094 // VLA needs to be treated as an executable expression.
1095 if (VariableArrayType* T = dyn_cast<VariableArrayType>(Ty.getTypePtr()))
1096 return child_iterator(T);
1097 else
1098 return child_iterator();
Ted Kremeneka6478552007-10-18 23:28:49 +00001099}
1100Stmt::child_iterator SizeOfAlignOfTypeExpr::child_end() {
Ted Kremenekb7cf0952007-12-14 22:52:23 +00001101 return child_iterator();
Ted Kremeneka6478552007-10-18 23:28:49 +00001102}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001103
1104// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001105Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001106 return reinterpret_cast<Stmt**>(&SubExprs);
1107}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001108Stmt::child_iterator ArraySubscriptExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001109 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001110}
1111
1112// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001113Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek15ede502007-08-27 21:11:44 +00001114 return reinterpret_cast<Stmt**>(&SubExprs[0]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001115}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001116Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek15ede502007-08-27 21:11:44 +00001117 return reinterpret_cast<Stmt**>(&SubExprs[NumArgs+ARGS_START]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001118}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001119
1120// MemberExpr
1121Stmt::child_iterator MemberExpr::child_begin() {
1122 return reinterpret_cast<Stmt**>(&Base);
1123}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001124Stmt::child_iterator MemberExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001125 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001126}
1127
1128// OCUVectorElementExpr
1129Stmt::child_iterator OCUVectorElementExpr::child_begin() {
1130 return reinterpret_cast<Stmt**>(&Base);
1131}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001132Stmt::child_iterator OCUVectorElementExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001133 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001134}
1135
1136// CompoundLiteralExpr
1137Stmt::child_iterator CompoundLiteralExpr::child_begin() {
1138 return reinterpret_cast<Stmt**>(&Init);
1139}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001140Stmt::child_iterator CompoundLiteralExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001141 return reinterpret_cast<Stmt**>(&Init)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001142}
1143
1144// ImplicitCastExpr
1145Stmt::child_iterator ImplicitCastExpr::child_begin() {
1146 return reinterpret_cast<Stmt**>(&Op);
1147}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001148Stmt::child_iterator ImplicitCastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001149 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001150}
1151
1152// CastExpr
1153Stmt::child_iterator CastExpr::child_begin() {
1154 return reinterpret_cast<Stmt**>(&Op);
1155}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001156Stmt::child_iterator CastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001157 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001158}
1159
1160// BinaryOperator
1161Stmt::child_iterator BinaryOperator::child_begin() {
1162 return reinterpret_cast<Stmt**>(&SubExprs);
1163}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001164Stmt::child_iterator BinaryOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001165 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001166}
1167
1168// ConditionalOperator
1169Stmt::child_iterator ConditionalOperator::child_begin() {
1170 return reinterpret_cast<Stmt**>(&SubExprs);
1171}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001172Stmt::child_iterator ConditionalOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001173 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001174}
1175
1176// AddrLabelExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001177Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1178Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001179
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001180// StmtExpr
1181Stmt::child_iterator StmtExpr::child_begin() {
1182 return reinterpret_cast<Stmt**>(&SubStmt);
1183}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001184Stmt::child_iterator StmtExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001185 return reinterpret_cast<Stmt**>(&SubStmt)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001186}
1187
1188// TypesCompatibleExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001189Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1190 return child_iterator();
1191}
1192
1193Stmt::child_iterator TypesCompatibleExpr::child_end() {
1194 return child_iterator();
1195}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001196
1197// ChooseExpr
1198Stmt::child_iterator ChooseExpr::child_begin() {
1199 return reinterpret_cast<Stmt**>(&SubExprs);
1200}
1201
1202Stmt::child_iterator ChooseExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001203 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001204}
1205
Anders Carlsson36760332007-10-15 20:28:48 +00001206// VAArgExpr
1207Stmt::child_iterator VAArgExpr::child_begin() {
1208 return reinterpret_cast<Stmt**>(&Val);
1209}
1210
1211Stmt::child_iterator VAArgExpr::child_end() {
1212 return reinterpret_cast<Stmt**>(&Val)+1;
1213}
1214
Anders Carlsson762b7c72007-08-31 04:56:16 +00001215// InitListExpr
1216Stmt::child_iterator InitListExpr::child_begin() {
1217 return reinterpret_cast<Stmt**>(&InitExprs[0]);
1218}
1219Stmt::child_iterator InitListExpr::child_end() {
1220 return reinterpret_cast<Stmt**>(&InitExprs[NumInits]);
1221}
1222
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001223// ObjCStringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001224Stmt::child_iterator ObjCStringLiteral::child_begin() {
1225 return child_iterator();
1226}
1227Stmt::child_iterator ObjCStringLiteral::child_end() {
1228 return child_iterator();
1229}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001230
1231// ObjCEncodeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001232Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1233Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001234
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001235// ObjCSelectorExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001236Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1237 return child_iterator();
1238}
1239Stmt::child_iterator ObjCSelectorExpr::child_end() {
1240 return child_iterator();
1241}
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001242
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001243// ObjCProtocolExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001244Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1245 return child_iterator();
1246}
1247Stmt::child_iterator ObjCProtocolExpr::child_end() {
1248 return child_iterator();
1249}
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001250
Steve Naroffc39ca262007-09-18 23:55:05 +00001251// ObjCMessageExpr
1252Stmt::child_iterator ObjCMessageExpr::child_begin() {
1253 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1254}
1255Stmt::child_iterator ObjCMessageExpr::child_end() {
Steve Naroff4ed9d662007-09-27 14:38:14 +00001256 return reinterpret_cast<Stmt**>(&SubExprs[getNumArgs()+ARGS_START]);
Steve Naroffc39ca262007-09-18 23:55:05 +00001257}
1258