blob: 236c7839220abae672b2c4bf1c8741970816e13f [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 Naroff7c9d72d2007-09-02 20:30:18 +0000472 case UnaryOperatorClass: {
473 const UnaryOperator *Exp = cast<UnaryOperator>(this);
474
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000475 // C99 6.6p9
Chris Lattner35b662f2007-12-11 23:11:17 +0000476 if (Exp->getOpcode() == UnaryOperator::AddrOf) {
477 if (!Exp->getSubExpr()->hasStaticStorage()) {
478 if (Loc) *Loc = getLocStart();
479 return false;
480 }
481 return true;
482 }
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000483
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000484 // Get the operand value. If this is sizeof/alignof, do not evalute the
485 // operand. This affects C99 6.6p3.
486 if (!Exp->isSizeOfAlignOfOp() &&
487 !Exp->getSubExpr()->isConstantExpr(Ctx, Loc))
488 return false;
489
490 switch (Exp->getOpcode()) {
491 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
492 // See C99 6.6p3.
493 default:
494 if (Loc) *Loc = Exp->getOperatorLoc();
495 return false;
496 case UnaryOperator::Extension:
497 return true; // FIXME: this is wrong.
498 case UnaryOperator::SizeOf:
499 case UnaryOperator::AlignOf:
500 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000501 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx)) {
502 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000503 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000504 }
Chris Lattner06db6132007-10-18 00:20:32 +0000505 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000506 case UnaryOperator::LNot:
507 case UnaryOperator::Plus:
508 case UnaryOperator::Minus:
509 case UnaryOperator::Not:
Chris Lattner06db6132007-10-18 00:20:32 +0000510 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000511 }
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000512 }
513 case SizeOfAlignOfTypeExprClass: {
514 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
515 // alignof always evaluates to a constant.
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000516 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx)) {
517 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000518 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000519 }
Chris Lattner06db6132007-10-18 00:20:32 +0000520 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000521 }
522 case BinaryOperatorClass: {
523 const BinaryOperator *Exp = cast<BinaryOperator>(this);
524
525 // The LHS of a constant expr is always evaluated and needed.
526 if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
527 return false;
528
529 if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
530 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000531 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000532 }
533 case ImplicitCastExprClass:
534 case CastExprClass: {
535 const Expr *SubExpr;
536 SourceLocation CastLoc;
537 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
538 SubExpr = C->getSubExpr();
539 CastLoc = C->getLParenLoc();
540 } else {
541 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
542 CastLoc = getLocStart();
543 }
544 if (!SubExpr->isConstantExpr(Ctx, Loc)) {
545 if (Loc) *Loc = SubExpr->getLocStart();
546 return false;
547 }
Chris Lattner06db6132007-10-18 00:20:32 +0000548 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000549 }
550 case ConditionalOperatorClass: {
551 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Chris Lattner06db6132007-10-18 00:20:32 +0000552 if (!Exp->getCond()->isConstantExpr(Ctx, Loc) ||
Anders Carlsson37365fc2007-11-30 19:04:31 +0000553 // Handle the GNU extension for missing LHS.
554 !(Exp->getLHS() && Exp->getLHS()->isConstantExpr(Ctx, Loc)) ||
Chris Lattner06db6132007-10-18 00:20:32 +0000555 !Exp->getRHS()->isConstantExpr(Ctx, Loc))
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000556 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000557 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000558 }
559 }
560
561 return true;
562}
563
Chris Lattner4b009652007-07-25 00:24:17 +0000564/// isIntegerConstantExpr - this recursive routine will test if an expression is
565/// an integer constant expression. Note: With the introduction of VLA's in
566/// C99 the result of the sizeof operator is no longer always a constant
567/// expression. The generalization of the wording to include any subexpression
568/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
569/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
570/// "0 || f()" can be treated as a constant expression. In C90 this expression,
571/// occurring in a context requiring a constant, would have been a constraint
572/// violation. FIXME: This routine currently implements C90 semantics.
573/// To properly implement C99 semantics this routine will need to evaluate
574/// expressions involving operators previously mentioned.
575
576/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
577/// comma, etc
578///
579/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattner9d020b32007-09-26 00:47:26 +0000580/// permit this. This includes things like (int)1e1000
Chris Lattner4b009652007-07-25 00:24:17 +0000581///
582/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
583/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
584/// cast+dereference.
585bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
586 SourceLocation *Loc, bool isEvaluated) const {
587 switch (getStmtClass()) {
588 default:
589 if (Loc) *Loc = getLocStart();
590 return false;
591 case ParenExprClass:
592 return cast<ParenExpr>(this)->getSubExpr()->
593 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
594 case IntegerLiteralClass:
595 Result = cast<IntegerLiteral>(this)->getValue();
596 break;
597 case CharacterLiteralClass: {
598 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000599 Result.zextOrTrunc(
600 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CL->getLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000601 Result = CL->getValue();
602 Result.setIsUnsigned(!getType()->isSignedIntegerType());
603 break;
604 }
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000605 case TypesCompatibleExprClass: {
606 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000607 Result.zextOrTrunc(
608 static_cast<uint32_t>(Ctx.getTypeSize(getType(), TCE->getLocStart())));
Steve Naroff85f0dc52007-10-15 20:41:53 +0000609 Result = Ctx.typesAreCompatible(TCE->getArgType1(), TCE->getArgType2());
Steve Naroff1200b5a2007-08-02 00:13:27 +0000610 break;
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000611 }
Steve Naroff8d3b1702007-08-08 22:15:55 +0000612 case CallExprClass: {
613 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000614 Result.zextOrTrunc(
615 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff8d3b1702007-08-08 22:15:55 +0000616 if (CE->isBuiltinClassifyType(Result))
617 break;
618 if (Loc) *Loc = getLocStart();
619 return false;
620 }
Chris Lattner4b009652007-07-25 00:24:17 +0000621 case DeclRefExprClass:
622 if (const EnumConstantDecl *D =
623 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
624 Result = D->getInitVal();
625 break;
626 }
627 if (Loc) *Loc = getLocStart();
628 return false;
629 case UnaryOperatorClass: {
630 const UnaryOperator *Exp = cast<UnaryOperator>(this);
631
632 // Get the operand value. If this is sizeof/alignof, do not evalute the
633 // operand. This affects C99 6.6p3.
Chris Lattner5a9b6242007-08-23 21:42:50 +0000634 if (!Exp->isSizeOfAlignOfOp() &&
635 !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000636 return false;
637
638 switch (Exp->getOpcode()) {
639 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
640 // See C99 6.6p3.
641 default:
642 if (Loc) *Loc = Exp->getOperatorLoc();
643 return false;
644 case UnaryOperator::Extension:
645 return true; // FIXME: this is wrong.
646 case UnaryOperator::SizeOf:
647 case UnaryOperator::AlignOf:
648 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000649 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx)) {
650 if (Loc) *Loc = Exp->getOperatorLoc();
Chris Lattner4b009652007-07-25 00:24:17 +0000651 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000652 }
Chris Lattner4b009652007-07-25 00:24:17 +0000653
654 // Return the result in the right width.
Chris Lattner3496d522007-09-04 02:45:27 +0000655 Result.zextOrTrunc(
Chris Lattner9d020b32007-09-26 00:47:26 +0000656 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
657 Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000658
659 // Get information about the size or align.
Chris Lattnerd4dc2672008-01-02 21:54:09 +0000660 if (Exp->getSubExpr()->getType()->isFunctionType()) {
661 // GCC extension: sizeof(function) = 1.
662 Result = Exp->getOpcode() == UnaryOperator::AlignOf ? 4 : 1;
663 } else if (Exp->getOpcode() == UnaryOperator::AlignOf) {
Chris Lattner4b009652007-07-25 00:24:17 +0000664 Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType(),
665 Exp->getOperatorLoc());
Chris Lattnerd9ffbc92007-11-27 18:22:04 +0000666 } else {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000667 unsigned CharSize =
668 Ctx.Target.getCharWidth(Ctx.getFullLoc(Exp->getOperatorLoc()));
669
Chris Lattnerd9ffbc92007-11-27 18:22:04 +0000670 Result = Ctx.getTypeSize(Exp->getSubExpr()->getType(),
671 Exp->getOperatorLoc()) / CharSize;
672 }
Chris Lattner4b009652007-07-25 00:24:17 +0000673 break;
674 case UnaryOperator::LNot: {
675 bool Val = Result != 0;
Chris Lattner3496d522007-09-04 02:45:27 +0000676 Result.zextOrTrunc(
Chris Lattner9d020b32007-09-26 00:47:26 +0000677 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
678 Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000679 Result = Val;
680 break;
681 }
682 case UnaryOperator::Plus:
683 break;
684 case UnaryOperator::Minus:
685 Result = -Result;
686 break;
687 case UnaryOperator::Not:
688 Result = ~Result;
689 break;
690 }
691 break;
692 }
693 case SizeOfAlignOfTypeExprClass: {
694 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
695 // alignof always evaluates to a constant.
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000696 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx)) {
697 if (Loc) *Loc = Exp->getOperatorLoc();
Chris Lattner4b009652007-07-25 00:24:17 +0000698 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000699 }
Chris Lattner4b009652007-07-25 00:24:17 +0000700
701 // Return the result in the right width.
Chris Lattner3496d522007-09-04 02:45:27 +0000702 Result.zextOrTrunc(
703 static_cast<uint32_t>(Ctx.getTypeSize(getType(), Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000704
705 // Get information about the size or align.
Chris Lattnerd4dc2672008-01-02 21:54:09 +0000706 if (Exp->getArgumentType()->isFunctionType()) {
707 // GCC extension: sizeof(function) = 1.
708 Result = Exp->isSizeOf() ? 1 : 4;
709 } else if (Exp->isSizeOf()) {
Ted Kremeneka9d0fd92007-12-17 17:38:43 +0000710 unsigned CharSize =
711 Ctx.Target.getCharWidth(Ctx.getFullLoc(Exp->getOperatorLoc()));
712
713 Result = Ctx.getTypeSize(Exp->getArgumentType(),
714 Exp->getOperatorLoc()) / CharSize;
715 }
Chris Lattner4b009652007-07-25 00:24:17 +0000716 else
717 Result = Ctx.getTypeAlign(Exp->getArgumentType(), Exp->getOperatorLoc());
Ted Kremeneka9d0fd92007-12-17 17:38:43 +0000718
Chris Lattner4b009652007-07-25 00:24:17 +0000719 break;
720 }
721 case BinaryOperatorClass: {
722 const BinaryOperator *Exp = cast<BinaryOperator>(this);
723
724 // The LHS of a constant expr is always evaluated and needed.
725 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
726 return false;
727
728 llvm::APSInt RHS(Result);
729
730 // The short-circuiting &&/|| operators don't necessarily evaluate their
731 // RHS. Make sure to pass isEvaluated down correctly.
732 if (Exp->isLogicalOp()) {
733 bool RHSEval;
734 if (Exp->getOpcode() == BinaryOperator::LAnd)
735 RHSEval = Result != 0;
736 else {
737 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
738 RHSEval = Result == 0;
739 }
740
741 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
742 isEvaluated & RHSEval))
743 return false;
744 } else {
745 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
746 return false;
747 }
748
749 switch (Exp->getOpcode()) {
750 default:
751 if (Loc) *Loc = getLocStart();
752 return false;
753 case BinaryOperator::Mul:
754 Result *= RHS;
755 break;
756 case BinaryOperator::Div:
757 if (RHS == 0) {
758 if (!isEvaluated) break;
759 if (Loc) *Loc = getLocStart();
760 return false;
761 }
762 Result /= RHS;
763 break;
764 case BinaryOperator::Rem:
765 if (RHS == 0) {
766 if (!isEvaluated) break;
767 if (Loc) *Loc = getLocStart();
768 return false;
769 }
770 Result %= RHS;
771 break;
772 case BinaryOperator::Add: Result += RHS; break;
773 case BinaryOperator::Sub: Result -= RHS; break;
774 case BinaryOperator::Shl:
Chris Lattner3496d522007-09-04 02:45:27 +0000775 Result <<=
776 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000777 break;
778 case BinaryOperator::Shr:
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::LT: Result = Result < RHS; break;
783 case BinaryOperator::GT: Result = Result > RHS; break;
784 case BinaryOperator::LE: Result = Result <= RHS; break;
785 case BinaryOperator::GE: Result = Result >= RHS; break;
786 case BinaryOperator::EQ: Result = Result == RHS; break;
787 case BinaryOperator::NE: Result = Result != RHS; break;
788 case BinaryOperator::And: Result &= RHS; break;
789 case BinaryOperator::Xor: Result ^= RHS; break;
790 case BinaryOperator::Or: Result |= RHS; break;
791 case BinaryOperator::LAnd:
792 Result = Result != 0 && RHS != 0;
793 break;
794 case BinaryOperator::LOr:
795 Result = Result != 0 || RHS != 0;
796 break;
797
798 case BinaryOperator::Comma:
799 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
800 // *except* when they are contained within a subexpression that is not
801 // evaluated". Note that Assignment can never happen due to constraints
802 // on the LHS subexpr, so we don't need to check it here.
803 if (isEvaluated) {
804 if (Loc) *Loc = getLocStart();
805 return false;
806 }
807
808 // The result of the constant expr is the RHS.
809 Result = RHS;
810 return true;
811 }
812
813 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
814 break;
815 }
816 case ImplicitCastExprClass:
817 case CastExprClass: {
818 const Expr *SubExpr;
819 SourceLocation CastLoc;
820 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
821 SubExpr = C->getSubExpr();
822 CastLoc = C->getLParenLoc();
823 } else {
824 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
825 CastLoc = getLocStart();
826 }
827
828 // C99 6.6p6: shall only convert arithmetic types to integer types.
829 if (!SubExpr->getType()->isArithmeticType() ||
830 !getType()->isIntegerType()) {
831 if (Loc) *Loc = SubExpr->getLocStart();
832 return false;
833 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000834
835 uint32_t DestWidth =
836 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CastLoc));
837
Chris Lattner4b009652007-07-25 00:24:17 +0000838 // Handle simple integer->integer casts.
839 if (SubExpr->getType()->isIntegerType()) {
840 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
841 return false;
842
843 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner4b009652007-07-25 00:24:17 +0000844 // If the input is signed, do a sign extend, noop, or truncate.
845 if (SubExpr->getType()->isSignedIntegerType())
846 Result.sextOrTrunc(DestWidth);
847 else // If the input is unsigned, do a zero extend, noop, or truncate.
848 Result.zextOrTrunc(DestWidth);
849 break;
850 }
851
852 // Allow floating constants that are the immediate operands of casts or that
853 // are parenthesized.
854 const Expr *Operand = SubExpr;
855 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
856 Operand = PE->getSubExpr();
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000857
858 // If this isn't a floating literal, we can't handle it.
859 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
860 if (!FL) {
861 if (Loc) *Loc = Operand->getLocStart();
862 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000863 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000864
865 // Determine whether we are converting to unsigned or signed.
866 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattner9d020b32007-09-26 00:47:26 +0000867
868 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
869 // be called multiple times per AST.
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000870 uint64_t Space[4];
Chris Lattner9d020b32007-09-26 00:47:26 +0000871 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
872 llvm::APFloat::rmTowardZero);
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000873 Result = llvm::APInt(DestWidth, 4, Space);
874 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000875 }
876 case ConditionalOperatorClass: {
877 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
878
879 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
880 return false;
881
882 const Expr *TrueExp = Exp->getLHS();
883 const Expr *FalseExp = Exp->getRHS();
884 if (Result == 0) std::swap(TrueExp, FalseExp);
885
886 // Evaluate the false one first, discard the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +0000887 if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Chris Lattner4b009652007-07-25 00:24:17 +0000888 return false;
889 // Evalute the true one, capture the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +0000890 if (TrueExp &&
891 !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000892 return false;
893 break;
894 }
895 }
896
897 // Cases that are valid constant exprs fall through to here.
898 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
899 return true;
900}
901
902
903/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
904/// integer constant expression with the value zero, or if this is one that is
905/// cast to void*.
906bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
907 // Strip off a cast to void*, if it exists.
908 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
909 // Check that it is a cast to void*.
910 if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) {
911 QualType Pointee = PT->getPointeeType();
912 if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void*
913 CE->getSubExpr()->getType()->isIntegerType()) // from int.
914 return CE->getSubExpr()->isNullPointerConstant(Ctx);
915 }
Steve Naroff1d6b2472007-08-28 21:20:34 +0000916 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
Steve Naroff3d052872007-08-29 00:00:02 +0000917 // Ignore the ImplicitCastExpr type entirely.
918 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000919 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
920 // Accept ((void*)0) as a null pointer constant, as many other
921 // implementations do.
922 return PE->getSubExpr()->isNullPointerConstant(Ctx);
923 }
924
925 // This expression must be an integer type.
926 if (!getType()->isIntegerType())
927 return false;
928
929 // If we have an integer constant expression, we need to *evaluate* it and
930 // test for the value 0.
931 llvm::APSInt Val(32);
932 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
933}
Steve Naroffc11705f2007-07-28 23:10:27 +0000934
Chris Lattnera0d03a72007-08-03 17:31:20 +0000935unsigned OCUVectorElementExpr::getNumElements() const {
Chris Lattner50547852007-08-03 16:00:20 +0000936 return strlen(Accessor.getName());
937}
938
939
Chris Lattnerf4bf5512007-08-02 21:47:28 +0000940/// getComponentType - Determine whether the components of this access are
941/// "point" "color" or "texture" elements.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000942OCUVectorElementExpr::ElementType
943OCUVectorElementExpr::getElementType() const {
Steve Naroffc11705f2007-07-28 23:10:27 +0000944 // derive the component type, no need to waste space.
945 const char *compStr = Accessor.getName();
Chris Lattnerabf25b32007-08-02 22:20:00 +0000946
Chris Lattner9096b792007-08-02 22:33:49 +0000947 if (OCUVectorType::getPointAccessorIdx(*compStr) != -1) return Point;
948 if (OCUVectorType::getColorAccessorIdx(*compStr) != -1) return Color;
Chris Lattnerabf25b32007-08-02 22:20:00 +0000949
Chris Lattner9096b792007-08-02 22:33:49 +0000950 assert(OCUVectorType::getTextureAccessorIdx(*compStr) != -1 &&
Chris Lattnerabf25b32007-08-02 22:20:00 +0000951 "getComponentType(): Illegal accessor");
952 return Texture;
Steve Naroffc11705f2007-07-28 23:10:27 +0000953}
Steve Naroffba67f692007-07-30 03:29:09 +0000954
Chris Lattnera0d03a72007-08-03 17:31:20 +0000955/// containsDuplicateElements - Return true if any element access is
Chris Lattnerf4bf5512007-08-02 21:47:28 +0000956/// repeated.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000957bool OCUVectorElementExpr::containsDuplicateElements() const {
Steve Naroffba67f692007-07-30 03:29:09 +0000958 const char *compStr = Accessor.getName();
959 unsigned length = strlen(compStr);
960
961 for (unsigned i = 0; i < length-1; i++) {
962 const char *s = compStr+i;
963 for (const char c = *s++; *s; s++)
964 if (c == *s)
965 return true;
966 }
967 return false;
968}
Chris Lattner42158e72007-08-02 23:36:59 +0000969
970/// getEncodedElementAccess - We encode fields with two bits per component.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000971unsigned OCUVectorElementExpr::getEncodedElementAccess() const {
Chris Lattner42158e72007-08-02 23:36:59 +0000972 const char *compStr = Accessor.getName();
Chris Lattnera0d03a72007-08-03 17:31:20 +0000973 unsigned length = getNumElements();
Chris Lattner42158e72007-08-02 23:36:59 +0000974
975 unsigned Result = 0;
976
977 while (length--) {
978 Result <<= 2;
979 int Idx = OCUVectorType::getAccessorIdx(compStr[length]);
980 assert(Idx != -1 && "Invalid accessor letter");
981 Result |= Idx;
982 }
983 return Result;
984}
985
Steve Naroff4ed9d662007-09-27 14:38:14 +0000986// constructor for instance messages.
Steve Naroff6cb1d362007-09-28 22:22:11 +0000987ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +0000988 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +0000989 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +0000990 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +0000991 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
992 MethodProto(mproto), ClassName(0) {
Steve Naroff9f176d12007-11-15 13:05:42 +0000993 NumArgs = nargs;
994 SubExprs = new Expr*[NumArgs+1];
Steve Naroff4ed9d662007-09-27 14:38:14 +0000995 SubExprs[RECEIVER] = receiver;
Steve Naroff9f176d12007-11-15 13:05:42 +0000996 if (NumArgs) {
997 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +0000998 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
999 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001000 LBracloc = LBrac;
1001 RBracloc = RBrac;
1002}
1003
Steve Naroff4ed9d662007-09-27 14:38:14 +00001004// constructor for class messages.
1005// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff6cb1d362007-09-28 22:22:11 +00001006ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001007 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001008 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001009 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001010 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1011 MethodProto(mproto), ClassName(clsName) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001012 NumArgs = nargs;
1013 SubExprs = new Expr*[NumArgs+1];
Steve Naroffc39ca262007-09-18 23:55:05 +00001014 SubExprs[RECEIVER] = 0;
Steve Naroff9f176d12007-11-15 13:05:42 +00001015 if (NumArgs) {
1016 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001017 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1018 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001019 LBracloc = LBrac;
1020 RBracloc = RBrac;
1021}
1022
Chris Lattnerf624cd22007-10-25 00:29:32 +00001023
1024bool ChooseExpr::isConditionTrue(ASTContext &C) const {
1025 llvm::APSInt CondVal(32);
1026 bool IsConst = getCond()->isIntegerConstantExpr(CondVal, C);
1027 assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst;
1028 return CondVal != 0;
1029}
1030
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001031//===----------------------------------------------------------------------===//
1032// Child Iterators for iterating over subexpressions/substatements
1033//===----------------------------------------------------------------------===//
1034
1035// DeclRefExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001036Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1037Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001038
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001039// ObjCIvarRefExpr
1040Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return child_iterator(); }
1041Stmt::child_iterator ObjCIvarRefExpr::child_end() { return child_iterator(); }
1042
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001043// PreDefinedExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001044Stmt::child_iterator PreDefinedExpr::child_begin() { return child_iterator(); }
1045Stmt::child_iterator PreDefinedExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001046
1047// IntegerLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001048Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1049Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001050
1051// CharacterLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001052Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); }
1053Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001054
1055// FloatingLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001056Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1057Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001058
Chris Lattner1de66eb2007-08-26 03:42:43 +00001059// ImaginaryLiteral
1060Stmt::child_iterator ImaginaryLiteral::child_begin() {
1061 return reinterpret_cast<Stmt**>(&Val);
1062}
1063Stmt::child_iterator ImaginaryLiteral::child_end() {
1064 return reinterpret_cast<Stmt**>(&Val)+1;
1065}
1066
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001067// StringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001068Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1069Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001070
1071// ParenExpr
1072Stmt::child_iterator ParenExpr::child_begin() {
1073 return reinterpret_cast<Stmt**>(&Val);
1074}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001075Stmt::child_iterator ParenExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001076 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001077}
1078
1079// UnaryOperator
1080Stmt::child_iterator UnaryOperator::child_begin() {
Ted Kremenek6d7ea522007-12-15 00:39:18 +00001081 return reinterpret_cast<Stmt**>(&Val);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001082}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001083Stmt::child_iterator UnaryOperator::child_end() {
Ted Kremenek6d7ea522007-12-15 00:39:18 +00001084 return reinterpret_cast<Stmt**>(&Val+1);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001085}
1086
1087// SizeOfAlignOfTypeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001088Stmt::child_iterator SizeOfAlignOfTypeExpr::child_begin() {
Ted Kremenekb7cf0952007-12-14 22:52:23 +00001089 // If the type is a VLA type (and not a typedef), the size expression of the
1090 // VLA needs to be treated as an executable expression.
1091 if (VariableArrayType* T = dyn_cast<VariableArrayType>(Ty.getTypePtr()))
1092 return child_iterator(T);
1093 else
1094 return child_iterator();
Ted Kremeneka6478552007-10-18 23:28:49 +00001095}
1096Stmt::child_iterator SizeOfAlignOfTypeExpr::child_end() {
Ted Kremenekb7cf0952007-12-14 22:52:23 +00001097 return child_iterator();
Ted Kremeneka6478552007-10-18 23:28:49 +00001098}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001099
1100// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001101Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001102 return reinterpret_cast<Stmt**>(&SubExprs);
1103}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001104Stmt::child_iterator ArraySubscriptExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001105 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001106}
1107
1108// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001109Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek15ede502007-08-27 21:11:44 +00001110 return reinterpret_cast<Stmt**>(&SubExprs[0]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001111}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001112Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek15ede502007-08-27 21:11:44 +00001113 return reinterpret_cast<Stmt**>(&SubExprs[NumArgs+ARGS_START]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001114}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001115
1116// MemberExpr
1117Stmt::child_iterator MemberExpr::child_begin() {
1118 return reinterpret_cast<Stmt**>(&Base);
1119}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001120Stmt::child_iterator MemberExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001121 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001122}
1123
1124// OCUVectorElementExpr
1125Stmt::child_iterator OCUVectorElementExpr::child_begin() {
1126 return reinterpret_cast<Stmt**>(&Base);
1127}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001128Stmt::child_iterator OCUVectorElementExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001129 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001130}
1131
1132// CompoundLiteralExpr
1133Stmt::child_iterator CompoundLiteralExpr::child_begin() {
1134 return reinterpret_cast<Stmt**>(&Init);
1135}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001136Stmt::child_iterator CompoundLiteralExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001137 return reinterpret_cast<Stmt**>(&Init)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001138}
1139
1140// ImplicitCastExpr
1141Stmt::child_iterator ImplicitCastExpr::child_begin() {
1142 return reinterpret_cast<Stmt**>(&Op);
1143}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001144Stmt::child_iterator ImplicitCastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001145 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001146}
1147
1148// CastExpr
1149Stmt::child_iterator CastExpr::child_begin() {
1150 return reinterpret_cast<Stmt**>(&Op);
1151}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001152Stmt::child_iterator CastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001153 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001154}
1155
1156// BinaryOperator
1157Stmt::child_iterator BinaryOperator::child_begin() {
1158 return reinterpret_cast<Stmt**>(&SubExprs);
1159}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001160Stmt::child_iterator BinaryOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001161 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001162}
1163
1164// ConditionalOperator
1165Stmt::child_iterator ConditionalOperator::child_begin() {
1166 return reinterpret_cast<Stmt**>(&SubExprs);
1167}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001168Stmt::child_iterator ConditionalOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001169 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001170}
1171
1172// AddrLabelExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001173Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1174Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001175
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001176// StmtExpr
1177Stmt::child_iterator StmtExpr::child_begin() {
1178 return reinterpret_cast<Stmt**>(&SubStmt);
1179}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001180Stmt::child_iterator StmtExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001181 return reinterpret_cast<Stmt**>(&SubStmt)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001182}
1183
1184// TypesCompatibleExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001185Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1186 return child_iterator();
1187}
1188
1189Stmt::child_iterator TypesCompatibleExpr::child_end() {
1190 return child_iterator();
1191}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001192
1193// ChooseExpr
1194Stmt::child_iterator ChooseExpr::child_begin() {
1195 return reinterpret_cast<Stmt**>(&SubExprs);
1196}
1197
1198Stmt::child_iterator ChooseExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001199 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001200}
1201
Anders Carlsson36760332007-10-15 20:28:48 +00001202// VAArgExpr
1203Stmt::child_iterator VAArgExpr::child_begin() {
1204 return reinterpret_cast<Stmt**>(&Val);
1205}
1206
1207Stmt::child_iterator VAArgExpr::child_end() {
1208 return reinterpret_cast<Stmt**>(&Val)+1;
1209}
1210
Anders Carlsson762b7c72007-08-31 04:56:16 +00001211// InitListExpr
1212Stmt::child_iterator InitListExpr::child_begin() {
1213 return reinterpret_cast<Stmt**>(&InitExprs[0]);
1214}
1215Stmt::child_iterator InitListExpr::child_end() {
1216 return reinterpret_cast<Stmt**>(&InitExprs[NumInits]);
1217}
1218
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001219// ObjCStringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001220Stmt::child_iterator ObjCStringLiteral::child_begin() {
1221 return child_iterator();
1222}
1223Stmt::child_iterator ObjCStringLiteral::child_end() {
1224 return child_iterator();
1225}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001226
1227// ObjCEncodeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001228Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1229Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001230
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001231// ObjCSelectorExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001232Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1233 return child_iterator();
1234}
1235Stmt::child_iterator ObjCSelectorExpr::child_end() {
1236 return child_iterator();
1237}
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001238
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001239// ObjCProtocolExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001240Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1241 return child_iterator();
1242}
1243Stmt::child_iterator ObjCProtocolExpr::child_end() {
1244 return child_iterator();
1245}
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001246
Steve Naroffc39ca262007-09-18 23:55:05 +00001247// ObjCMessageExpr
1248Stmt::child_iterator ObjCMessageExpr::child_begin() {
1249 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1250}
1251Stmt::child_iterator ObjCMessageExpr::child_end() {
Steve Naroff4ed9d662007-09-27 14:38:14 +00001252 return reinterpret_cast<Stmt**>(&SubExprs[getNumArgs()+ARGS_START]);
Steve Naroffc39ca262007-09-18 23:55:05 +00001253}
1254