blob: 545ebd8e182538cf25158be60c9d237ac638e8e7 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Expr.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/StmtVisitor.h"
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
Steve Naroff8d3b1702007-08-08 22:15:55 +000091bool CallExpr::isBuiltinClassifyType(llvm::APSInt &Result) const {
92 // The following enum mimics gcc's internal "typeclass.h" file.
93 enum gcc_type_class {
94 no_type_class = -1,
95 void_type_class, integer_type_class, char_type_class,
96 enumeral_type_class, boolean_type_class,
97 pointer_type_class, reference_type_class, offset_type_class,
98 real_type_class, complex_type_class,
99 function_type_class, method_type_class,
100 record_type_class, union_type_class,
101 array_type_class, string_type_class,
102 lang_type_class
103 };
104 Result.setIsSigned(true);
105
106 // All simple function calls (e.g. func()) are implicitly cast to pointer to
107 // function. As a result, we try and obtain the DeclRefExpr from the
108 // ImplicitCastExpr.
109 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
110 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
111 return false;
112 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
113 if (!DRE)
114 return false;
115
116 // We have a DeclRefExpr.
117 if (strcmp(DRE->getDecl()->getName(), "__builtin_classify_type") == 0) {
118 // If no argument was supplied, default to "no_type_class". This isn't
119 // ideal, however it's what gcc does.
120 Result = static_cast<uint64_t>(no_type_class);
121 if (NumArgs >= 1) {
122 QualType argType = getArg(0)->getType();
123
124 if (argType->isVoidType())
125 Result = void_type_class;
126 else if (argType->isEnumeralType())
127 Result = enumeral_type_class;
128 else if (argType->isBooleanType())
129 Result = boolean_type_class;
130 else if (argType->isCharType())
131 Result = string_type_class; // gcc doesn't appear to use char_type_class
132 else if (argType->isIntegerType())
133 Result = integer_type_class;
134 else if (argType->isPointerType())
135 Result = pointer_type_class;
136 else if (argType->isReferenceType())
137 Result = reference_type_class;
138 else if (argType->isRealType())
139 Result = real_type_class;
140 else if (argType->isComplexType())
141 Result = complex_type_class;
142 else if (argType->isFunctionType())
143 Result = function_type_class;
144 else if (argType->isStructureType())
145 Result = record_type_class;
146 else if (argType->isUnionType())
147 Result = union_type_class;
148 else if (argType->isArrayType())
149 Result = array_type_class;
150 else if (argType->isUnionType())
151 Result = union_type_class;
152 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
Chris Lattner19b8f1a2007-11-08 17:56:40 +0000153 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
Steve Naroff8d3b1702007-08-08 22:15:55 +0000154 }
155 return true;
156 }
157 return false;
158}
159
Chris Lattner4b009652007-07-25 00:24:17 +0000160/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
161/// corresponds to, e.g. "<<=".
162const char *BinaryOperator::getOpcodeStr(Opcode Op) {
163 switch (Op) {
164 default: assert(0 && "Unknown binary operator");
165 case Mul: return "*";
166 case Div: return "/";
167 case Rem: return "%";
168 case Add: return "+";
169 case Sub: return "-";
170 case Shl: return "<<";
171 case Shr: return ">>";
172 case LT: return "<";
173 case GT: return ">";
174 case LE: return "<=";
175 case GE: return ">=";
176 case EQ: return "==";
177 case NE: return "!=";
178 case And: return "&";
179 case Xor: return "^";
180 case Or: return "|";
181 case LAnd: return "&&";
182 case LOr: return "||";
183 case Assign: return "=";
184 case MulAssign: return "*=";
185 case DivAssign: return "/=";
186 case RemAssign: return "%=";
187 case AddAssign: return "+=";
188 case SubAssign: return "-=";
189 case ShlAssign: return "<<=";
190 case ShrAssign: return ">>=";
191 case AndAssign: return "&=";
192 case XorAssign: return "^=";
193 case OrAssign: return "|=";
194 case Comma: return ",";
195 }
196}
197
Anders Carlsson762b7c72007-08-31 04:56:16 +0000198InitListExpr::InitListExpr(SourceLocation lbraceloc,
199 Expr **initexprs, unsigned numinits,
200 SourceLocation rbraceloc)
201 : Expr(InitListExprClass, QualType())
202 , NumInits(numinits)
203 , LBraceLoc(lbraceloc)
204 , RBraceLoc(rbraceloc)
205{
206 InitExprs = new Expr*[numinits];
207 for (unsigned i = 0; i != numinits; i++)
208 InitExprs[i] = initexprs[i];
209}
Chris Lattner4b009652007-07-25 00:24:17 +0000210
211//===----------------------------------------------------------------------===//
212// Generic Expression Routines
213//===----------------------------------------------------------------------===//
214
215/// hasLocalSideEffect - Return true if this immediate expression has side
216/// effects, not counting any sub-expressions.
217bool Expr::hasLocalSideEffect() const {
218 switch (getStmtClass()) {
219 default:
220 return false;
221 case ParenExprClass:
222 return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect();
223 case UnaryOperatorClass: {
224 const UnaryOperator *UO = cast<UnaryOperator>(this);
225
226 switch (UO->getOpcode()) {
227 default: return false;
228 case UnaryOperator::PostInc:
229 case UnaryOperator::PostDec:
230 case UnaryOperator::PreInc:
231 case UnaryOperator::PreDec:
232 return true; // ++/--
233
234 case UnaryOperator::Deref:
235 // Dereferencing a volatile pointer is a side-effect.
236 return getType().isVolatileQualified();
237 case UnaryOperator::Real:
238 case UnaryOperator::Imag:
239 // accessing a piece of a volatile complex is a side-effect.
240 return UO->getSubExpr()->getType().isVolatileQualified();
241
242 case UnaryOperator::Extension:
243 return UO->getSubExpr()->hasLocalSideEffect();
244 }
245 }
Chris Lattneref95ffd2007-12-01 06:07:34 +0000246 case BinaryOperatorClass: {
247 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
248 // Consider comma to have side effects if the LHS and RHS both do.
249 if (BinOp->getOpcode() == BinaryOperator::Comma)
250 return BinOp->getLHS()->hasLocalSideEffect() &&
251 BinOp->getRHS()->hasLocalSideEffect();
252
253 return BinOp->isAssignmentOp();
254 }
Chris Lattner06078d22007-08-25 02:00:02 +0000255 case CompoundAssignOperatorClass:
Chris Lattner9c0da3b2007-08-25 01:55:00 +0000256 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000257
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000258 case ConditionalOperatorClass: {
259 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
260 return Exp->getCond()->hasLocalSideEffect()
261 || (Exp->getLHS() && Exp->getLHS()->hasLocalSideEffect())
262 || (Exp->getRHS() && Exp->getRHS()->hasLocalSideEffect());
263 }
264
Chris Lattner4b009652007-07-25 00:24:17 +0000265 case MemberExprClass:
266 case ArraySubscriptExprClass:
267 // If the base pointer or element is to a volatile pointer/field, accessing
268 // if is a side effect.
269 return getType().isVolatileQualified();
270
271 case CallExprClass:
272 // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }"
273 // should warn.
274 return true;
Chris Lattner99f5f0b2007-09-26 22:06:30 +0000275 case ObjCMessageExprClass:
276 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000277
278 case CastExprClass:
279 // If this is a cast to void, check the operand. Otherwise, the result of
280 // the cast is unused.
281 if (getType()->isVoidType())
282 return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect();
283 return false;
284 }
285}
286
287/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
288/// incomplete type other than void. Nonarray expressions that can be lvalues:
289/// - name, where name must be a variable
290/// - e[i]
291/// - (e), where e must be an lvalue
292/// - e.name, where e must be an lvalue
293/// - e->name
294/// - *e, the type of e cannot be a function type
295/// - string-constant
Chris Lattner5bf72022007-10-30 22:53:42 +0000296/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Chris Lattner4b009652007-07-25 00:24:17 +0000297/// - reference type [C++ [expr]]
298///
299Expr::isLvalueResult Expr::isLvalue() const {
300 // first, check the type (C99 6.3.2.1)
301 if (TR->isFunctionType()) // from isObjectType()
302 return LV_NotObjectType;
303
304 if (TR->isVoidType())
305 return LV_IncompleteVoidType;
306
307 if (TR->isReferenceType()) // C++ [expr]
308 return LV_Valid;
309
310 // the type looks fine, now check the expression
311 switch (getStmtClass()) {
312 case StringLiteralClass: // C99 6.5.1p4
Anders Carlsson9e933a22007-11-30 22:47:59 +0000313 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000314 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
315 // For vectors, make sure base is an lvalue (i.e. not a function call).
316 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
317 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue();
318 return LV_Valid;
319 case DeclRefExprClass: // C99 6.5.1p2
320 if (isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl()))
321 return LV_Valid;
322 break;
323 case MemberExprClass: { // C99 6.5.2.3p4
324 const MemberExpr *m = cast<MemberExpr>(this);
325 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue();
326 }
Chris Lattner5bf72022007-10-30 22:53:42 +0000327 case UnaryOperatorClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000328 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner5bf72022007-10-30 22:53:42 +0000329 return LV_Valid; // C99 6.5.3p4
330
331 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
332 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag)
333 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(); // GNU.
Chris Lattner4b009652007-07-25 00:24:17 +0000334 break;
335 case ParenExprClass: // C99 6.5.1p5
336 return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
Steve Naroffc7c66532007-12-05 04:00:10 +0000337 case CompoundLiteralExprClass: // C99 6.5.2.5p5
338 return LV_Valid;
Chris Lattnera0d03a72007-08-03 17:31:20 +0000339 case OCUVectorElementExprClass:
340 if (cast<OCUVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroffba67f692007-07-30 03:29:09 +0000341 return LV_DuplicateVectorComponents;
342 return LV_Valid;
Steve Naroff46f18f22007-11-12 14:34:27 +0000343 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
344 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000345 default:
346 break;
347 }
348 return LV_InvalidExpression;
349}
350
351/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
352/// does not have an incomplete type, does not have a const-qualified type, and
353/// if it is a structure or union, does not have any member (including,
354/// recursively, any member or element of all contained aggregates or unions)
355/// with a const-qualified type.
356Expr::isModifiableLvalueResult Expr::isModifiableLvalue() const {
357 isLvalueResult lvalResult = isLvalue();
358
359 switch (lvalResult) {
360 case LV_Valid: break;
361 case LV_NotObjectType: return MLV_NotObjectType;
362 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroffba67f692007-07-30 03:29:09 +0000363 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner4b009652007-07-25 00:24:17 +0000364 case LV_InvalidExpression: return MLV_InvalidExpression;
365 }
366 if (TR.isConstQualified())
367 return MLV_ConstQualified;
368 if (TR->isArrayType())
369 return MLV_ArrayType;
370 if (TR->isIncompleteType())
371 return MLV_IncompleteType;
372
373 if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) {
374 if (r->hasConstFields())
375 return MLV_ConstQualified;
376 }
377 return MLV_Valid;
378}
379
Chris Lattner743ec372007-11-27 21:35:27 +0000380/// hasStaticStorage - Return true if this expression has static storage
381/// duration. This means that the address of this expression is a link-time
382/// constant.
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000383bool Expr::hasStaticStorage() const {
384 switch (getStmtClass()) {
385 default:
386 return false;
Chris Lattner743ec372007-11-27 21:35:27 +0000387 case ParenExprClass:
388 return cast<ParenExpr>(this)->getSubExpr()->hasStaticStorage();
389 case ImplicitCastExprClass:
390 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasStaticStorage();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000391 case DeclRefExprClass: {
392 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
393 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
394 return VD->hasStaticStorage();
395 return false;
396 }
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000397 case MemberExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000398 const MemberExpr *M = cast<MemberExpr>(this);
399 return !M->isArrow() && M->getBase()->hasStaticStorage();
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000400 }
Chris Lattner743ec372007-11-27 21:35:27 +0000401 case ArraySubscriptExprClass:
402 return cast<ArraySubscriptExpr>(this)->getBase()->hasStaticStorage();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000403 }
404}
405
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000406bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000407 switch (getStmtClass()) {
408 default:
409 if (Loc) *Loc = getLocStart();
410 return false;
411 case ParenExprClass:
412 return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(Ctx, Loc);
413 case StringLiteralClass:
Steve Naroff4fdea132007-11-09 15:00:03 +0000414 case ObjCStringLiteralClass:
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000415 case FloatingLiteralClass:
416 case IntegerLiteralClass:
417 case CharacterLiteralClass:
418 case ImaginaryLiteralClass:
Anders Carlsson855d78d2007-10-17 00:52:43 +0000419 case TypesCompatibleExprClass:
420 case CXXBoolLiteralExprClass:
Chris Lattner06db6132007-10-18 00:20:32 +0000421 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000422 case CallExprClass: {
423 const CallExpr *CE = cast<CallExpr>(this);
424 llvm::APSInt Result(32);
Hartmut Kaiser38af7012007-09-16 21:35:35 +0000425 Result.zextOrTrunc(
426 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000427 if (CE->isBuiltinClassifyType(Result))
Chris Lattner06db6132007-10-18 00:20:32 +0000428 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000429 if (Loc) *Loc = getLocStart();
430 return false;
431 }
Chris Lattner42e86b62007-11-01 02:45:17 +0000432 case DeclRefExprClass: {
433 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
434 // Accept address of function.
435 if (isa<EnumConstantDecl>(D) || isa<FunctionDecl>(D))
Chris Lattner06db6132007-10-18 00:20:32 +0000436 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000437 if (Loc) *Loc = getLocStart();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000438 if (isa<VarDecl>(D))
439 return TR->isArrayType();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000440 return false;
Chris Lattner42e86b62007-11-01 02:45:17 +0000441 }
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000442 case UnaryOperatorClass: {
443 const UnaryOperator *Exp = cast<UnaryOperator>(this);
444
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000445 // C99 6.6p9
446 if (Exp->getOpcode() == UnaryOperator::AddrOf)
447 return Exp->getSubExpr()->hasStaticStorage();
448
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000449 // Get the operand value. If this is sizeof/alignof, do not evalute the
450 // operand. This affects C99 6.6p3.
451 if (!Exp->isSizeOfAlignOfOp() &&
452 !Exp->getSubExpr()->isConstantExpr(Ctx, Loc))
453 return false;
454
455 switch (Exp->getOpcode()) {
456 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
457 // See C99 6.6p3.
458 default:
459 if (Loc) *Loc = Exp->getOperatorLoc();
460 return false;
461 case UnaryOperator::Extension:
462 return true; // FIXME: this is wrong.
463 case UnaryOperator::SizeOf:
464 case UnaryOperator::AlignOf:
465 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
466 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx, Loc))
467 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000468 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000469 case UnaryOperator::LNot:
470 case UnaryOperator::Plus:
471 case UnaryOperator::Minus:
472 case UnaryOperator::Not:
Chris Lattner06db6132007-10-18 00:20:32 +0000473 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000474 }
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000475 }
476 case SizeOfAlignOfTypeExprClass: {
477 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
478 // alignof always evaluates to a constant.
479 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx,Loc))
480 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000481 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000482 }
483 case BinaryOperatorClass: {
484 const BinaryOperator *Exp = cast<BinaryOperator>(this);
485
486 // The LHS of a constant expr is always evaluated and needed.
487 if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
488 return false;
489
490 if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
491 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000492 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000493 }
494 case ImplicitCastExprClass:
495 case CastExprClass: {
496 const Expr *SubExpr;
497 SourceLocation CastLoc;
498 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
499 SubExpr = C->getSubExpr();
500 CastLoc = C->getLParenLoc();
501 } else {
502 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
503 CastLoc = getLocStart();
504 }
505 if (!SubExpr->isConstantExpr(Ctx, Loc)) {
506 if (Loc) *Loc = SubExpr->getLocStart();
507 return false;
508 }
Chris Lattner06db6132007-10-18 00:20:32 +0000509 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000510 }
511 case ConditionalOperatorClass: {
512 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Chris Lattner06db6132007-10-18 00:20:32 +0000513 if (!Exp->getCond()->isConstantExpr(Ctx, Loc) ||
Anders Carlsson37365fc2007-11-30 19:04:31 +0000514 // Handle the GNU extension for missing LHS.
515 !(Exp->getLHS() && Exp->getLHS()->isConstantExpr(Ctx, Loc)) ||
Chris Lattner06db6132007-10-18 00:20:32 +0000516 !Exp->getRHS()->isConstantExpr(Ctx, Loc))
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000517 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000518 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000519 }
520 }
521
522 return true;
523}
524
Chris Lattner4b009652007-07-25 00:24:17 +0000525/// isIntegerConstantExpr - this recursive routine will test if an expression is
526/// an integer constant expression. Note: With the introduction of VLA's in
527/// C99 the result of the sizeof operator is no longer always a constant
528/// expression. The generalization of the wording to include any subexpression
529/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
530/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
531/// "0 || f()" can be treated as a constant expression. In C90 this expression,
532/// occurring in a context requiring a constant, would have been a constraint
533/// violation. FIXME: This routine currently implements C90 semantics.
534/// To properly implement C99 semantics this routine will need to evaluate
535/// expressions involving operators previously mentioned.
536
537/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
538/// comma, etc
539///
540/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattner9d020b32007-09-26 00:47:26 +0000541/// permit this. This includes things like (int)1e1000
Chris Lattner4b009652007-07-25 00:24:17 +0000542///
543/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
544/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
545/// cast+dereference.
546bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
547 SourceLocation *Loc, bool isEvaluated) const {
548 switch (getStmtClass()) {
549 default:
550 if (Loc) *Loc = getLocStart();
551 return false;
552 case ParenExprClass:
553 return cast<ParenExpr>(this)->getSubExpr()->
554 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
555 case IntegerLiteralClass:
556 Result = cast<IntegerLiteral>(this)->getValue();
557 break;
558 case CharacterLiteralClass: {
559 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000560 Result.zextOrTrunc(
561 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CL->getLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000562 Result = CL->getValue();
563 Result.setIsUnsigned(!getType()->isSignedIntegerType());
564 break;
565 }
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000566 case TypesCompatibleExprClass: {
567 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000568 Result.zextOrTrunc(
569 static_cast<uint32_t>(Ctx.getTypeSize(getType(), TCE->getLocStart())));
Steve Naroff85f0dc52007-10-15 20:41:53 +0000570 Result = Ctx.typesAreCompatible(TCE->getArgType1(), TCE->getArgType2());
Steve Naroff1200b5a2007-08-02 00:13:27 +0000571 break;
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000572 }
Steve Naroff8d3b1702007-08-08 22:15:55 +0000573 case CallExprClass: {
574 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000575 Result.zextOrTrunc(
576 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff8d3b1702007-08-08 22:15:55 +0000577 if (CE->isBuiltinClassifyType(Result))
578 break;
579 if (Loc) *Loc = getLocStart();
580 return false;
581 }
Chris Lattner4b009652007-07-25 00:24:17 +0000582 case DeclRefExprClass:
583 if (const EnumConstantDecl *D =
584 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
585 Result = D->getInitVal();
586 break;
587 }
588 if (Loc) *Loc = getLocStart();
589 return false;
590 case UnaryOperatorClass: {
591 const UnaryOperator *Exp = cast<UnaryOperator>(this);
592
593 // Get the operand value. If this is sizeof/alignof, do not evalute the
594 // operand. This affects C99 6.6p3.
Chris Lattner5a9b6242007-08-23 21:42:50 +0000595 if (!Exp->isSizeOfAlignOfOp() &&
596 !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000597 return false;
598
599 switch (Exp->getOpcode()) {
600 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
601 // See C99 6.6p3.
602 default:
603 if (Loc) *Loc = Exp->getOperatorLoc();
604 return false;
605 case UnaryOperator::Extension:
606 return true; // FIXME: this is wrong.
607 case UnaryOperator::SizeOf:
608 case UnaryOperator::AlignOf:
609 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
610 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx, Loc))
611 return false;
612
613 // Return the result in the right width.
Chris Lattner3496d522007-09-04 02:45:27 +0000614 Result.zextOrTrunc(
Chris Lattner9d020b32007-09-26 00:47:26 +0000615 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
616 Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000617
618 // Get information about the size or align.
Chris Lattnerd9ffbc92007-11-27 18:22:04 +0000619 if (Exp->getOpcode() == UnaryOperator::AlignOf) {
Chris Lattner4b009652007-07-25 00:24:17 +0000620 Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType(),
621 Exp->getOperatorLoc());
Chris Lattnerd9ffbc92007-11-27 18:22:04 +0000622 } else {
623 unsigned CharSize = Ctx.Target.getCharWidth(Exp->getOperatorLoc());
624 Result = Ctx.getTypeSize(Exp->getSubExpr()->getType(),
625 Exp->getOperatorLoc()) / CharSize;
626 }
Chris Lattner4b009652007-07-25 00:24:17 +0000627 break;
628 case UnaryOperator::LNot: {
629 bool Val = Result != 0;
Chris Lattner3496d522007-09-04 02:45:27 +0000630 Result.zextOrTrunc(
Chris Lattner9d020b32007-09-26 00:47:26 +0000631 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
632 Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000633 Result = Val;
634 break;
635 }
636 case UnaryOperator::Plus:
637 break;
638 case UnaryOperator::Minus:
639 Result = -Result;
640 break;
641 case UnaryOperator::Not:
642 Result = ~Result;
643 break;
644 }
645 break;
646 }
647 case SizeOfAlignOfTypeExprClass: {
648 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
649 // alignof always evaluates to a constant.
650 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx,Loc))
651 return false;
652
653 // Return the result in the right width.
Chris Lattner3496d522007-09-04 02:45:27 +0000654 Result.zextOrTrunc(
655 static_cast<uint32_t>(Ctx.getTypeSize(getType(), Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000656
657 // Get information about the size or align.
658 if (Exp->isSizeOf())
659 Result = Ctx.getTypeSize(Exp->getArgumentType(), Exp->getOperatorLoc());
660 else
661 Result = Ctx.getTypeAlign(Exp->getArgumentType(), Exp->getOperatorLoc());
662 break;
663 }
664 case BinaryOperatorClass: {
665 const BinaryOperator *Exp = cast<BinaryOperator>(this);
666
667 // The LHS of a constant expr is always evaluated and needed.
668 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
669 return false;
670
671 llvm::APSInt RHS(Result);
672
673 // The short-circuiting &&/|| operators don't necessarily evaluate their
674 // RHS. Make sure to pass isEvaluated down correctly.
675 if (Exp->isLogicalOp()) {
676 bool RHSEval;
677 if (Exp->getOpcode() == BinaryOperator::LAnd)
678 RHSEval = Result != 0;
679 else {
680 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
681 RHSEval = Result == 0;
682 }
683
684 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
685 isEvaluated & RHSEval))
686 return false;
687 } else {
688 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
689 return false;
690 }
691
692 switch (Exp->getOpcode()) {
693 default:
694 if (Loc) *Loc = getLocStart();
695 return false;
696 case BinaryOperator::Mul:
697 Result *= RHS;
698 break;
699 case BinaryOperator::Div:
700 if (RHS == 0) {
701 if (!isEvaluated) break;
702 if (Loc) *Loc = getLocStart();
703 return false;
704 }
705 Result /= RHS;
706 break;
707 case BinaryOperator::Rem:
708 if (RHS == 0) {
709 if (!isEvaluated) break;
710 if (Loc) *Loc = getLocStart();
711 return false;
712 }
713 Result %= RHS;
714 break;
715 case BinaryOperator::Add: Result += RHS; break;
716 case BinaryOperator::Sub: Result -= RHS; break;
717 case BinaryOperator::Shl:
Chris Lattner3496d522007-09-04 02:45:27 +0000718 Result <<=
719 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000720 break;
721 case BinaryOperator::Shr:
Chris Lattner3496d522007-09-04 02:45:27 +0000722 Result >>=
723 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000724 break;
725 case BinaryOperator::LT: Result = Result < RHS; break;
726 case BinaryOperator::GT: Result = Result > RHS; break;
727 case BinaryOperator::LE: Result = Result <= RHS; break;
728 case BinaryOperator::GE: Result = Result >= RHS; break;
729 case BinaryOperator::EQ: Result = Result == RHS; break;
730 case BinaryOperator::NE: Result = Result != RHS; break;
731 case BinaryOperator::And: Result &= RHS; break;
732 case BinaryOperator::Xor: Result ^= RHS; break;
733 case BinaryOperator::Or: Result |= RHS; break;
734 case BinaryOperator::LAnd:
735 Result = Result != 0 && RHS != 0;
736 break;
737 case BinaryOperator::LOr:
738 Result = Result != 0 || RHS != 0;
739 break;
740
741 case BinaryOperator::Comma:
742 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
743 // *except* when they are contained within a subexpression that is not
744 // evaluated". Note that Assignment can never happen due to constraints
745 // on the LHS subexpr, so we don't need to check it here.
746 if (isEvaluated) {
747 if (Loc) *Loc = getLocStart();
748 return false;
749 }
750
751 // The result of the constant expr is the RHS.
752 Result = RHS;
753 return true;
754 }
755
756 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
757 break;
758 }
759 case ImplicitCastExprClass:
760 case CastExprClass: {
761 const Expr *SubExpr;
762 SourceLocation CastLoc;
763 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
764 SubExpr = C->getSubExpr();
765 CastLoc = C->getLParenLoc();
766 } else {
767 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
768 CastLoc = getLocStart();
769 }
770
771 // C99 6.6p6: shall only convert arithmetic types to integer types.
772 if (!SubExpr->getType()->isArithmeticType() ||
773 !getType()->isIntegerType()) {
774 if (Loc) *Loc = SubExpr->getLocStart();
775 return false;
776 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000777
778 uint32_t DestWidth =
779 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CastLoc));
780
Chris Lattner4b009652007-07-25 00:24:17 +0000781 // Handle simple integer->integer casts.
782 if (SubExpr->getType()->isIntegerType()) {
783 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
784 return false;
785
786 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner4b009652007-07-25 00:24:17 +0000787 // If the input is signed, do a sign extend, noop, or truncate.
788 if (SubExpr->getType()->isSignedIntegerType())
789 Result.sextOrTrunc(DestWidth);
790 else // If the input is unsigned, do a zero extend, noop, or truncate.
791 Result.zextOrTrunc(DestWidth);
792 break;
793 }
794
795 // Allow floating constants that are the immediate operands of casts or that
796 // are parenthesized.
797 const Expr *Operand = SubExpr;
798 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
799 Operand = PE->getSubExpr();
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000800
801 // If this isn't a floating literal, we can't handle it.
802 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
803 if (!FL) {
804 if (Loc) *Loc = Operand->getLocStart();
805 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000806 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000807
808 // Determine whether we are converting to unsigned or signed.
809 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattner9d020b32007-09-26 00:47:26 +0000810
811 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
812 // be called multiple times per AST.
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000813 uint64_t Space[4];
Chris Lattner9d020b32007-09-26 00:47:26 +0000814 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
815 llvm::APFloat::rmTowardZero);
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000816 Result = llvm::APInt(DestWidth, 4, Space);
817 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000818 }
819 case ConditionalOperatorClass: {
820 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
821
822 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
823 return false;
824
825 const Expr *TrueExp = Exp->getLHS();
826 const Expr *FalseExp = Exp->getRHS();
827 if (Result == 0) std::swap(TrueExp, FalseExp);
828
829 // Evaluate the false one first, discard the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +0000830 if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Chris Lattner4b009652007-07-25 00:24:17 +0000831 return false;
832 // Evalute the true one, capture the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +0000833 if (TrueExp &&
834 !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000835 return false;
836 break;
837 }
838 }
839
840 // Cases that are valid constant exprs fall through to here.
841 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
842 return true;
843}
844
845
846/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
847/// integer constant expression with the value zero, or if this is one that is
848/// cast to void*.
849bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
850 // Strip off a cast to void*, if it exists.
851 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
852 // Check that it is a cast to void*.
853 if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) {
854 QualType Pointee = PT->getPointeeType();
855 if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void*
856 CE->getSubExpr()->getType()->isIntegerType()) // from int.
857 return CE->getSubExpr()->isNullPointerConstant(Ctx);
858 }
Steve Naroff1d6b2472007-08-28 21:20:34 +0000859 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
Steve Naroff3d052872007-08-29 00:00:02 +0000860 // Ignore the ImplicitCastExpr type entirely.
861 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000862 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
863 // Accept ((void*)0) as a null pointer constant, as many other
864 // implementations do.
865 return PE->getSubExpr()->isNullPointerConstant(Ctx);
866 }
867
868 // This expression must be an integer type.
869 if (!getType()->isIntegerType())
870 return false;
871
872 // If we have an integer constant expression, we need to *evaluate* it and
873 // test for the value 0.
874 llvm::APSInt Val(32);
875 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
876}
Steve Naroffc11705f2007-07-28 23:10:27 +0000877
Chris Lattnera0d03a72007-08-03 17:31:20 +0000878unsigned OCUVectorElementExpr::getNumElements() const {
Chris Lattner50547852007-08-03 16:00:20 +0000879 return strlen(Accessor.getName());
880}
881
882
Chris Lattnerf4bf5512007-08-02 21:47:28 +0000883/// getComponentType - Determine whether the components of this access are
884/// "point" "color" or "texture" elements.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000885OCUVectorElementExpr::ElementType
886OCUVectorElementExpr::getElementType() const {
Steve Naroffc11705f2007-07-28 23:10:27 +0000887 // derive the component type, no need to waste space.
888 const char *compStr = Accessor.getName();
Chris Lattnerabf25b32007-08-02 22:20:00 +0000889
Chris Lattner9096b792007-08-02 22:33:49 +0000890 if (OCUVectorType::getPointAccessorIdx(*compStr) != -1) return Point;
891 if (OCUVectorType::getColorAccessorIdx(*compStr) != -1) return Color;
Chris Lattnerabf25b32007-08-02 22:20:00 +0000892
Chris Lattner9096b792007-08-02 22:33:49 +0000893 assert(OCUVectorType::getTextureAccessorIdx(*compStr) != -1 &&
Chris Lattnerabf25b32007-08-02 22:20:00 +0000894 "getComponentType(): Illegal accessor");
895 return Texture;
Steve Naroffc11705f2007-07-28 23:10:27 +0000896}
Steve Naroffba67f692007-07-30 03:29:09 +0000897
Chris Lattnera0d03a72007-08-03 17:31:20 +0000898/// containsDuplicateElements - Return true if any element access is
Chris Lattnerf4bf5512007-08-02 21:47:28 +0000899/// repeated.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000900bool OCUVectorElementExpr::containsDuplicateElements() const {
Steve Naroffba67f692007-07-30 03:29:09 +0000901 const char *compStr = Accessor.getName();
902 unsigned length = strlen(compStr);
903
904 for (unsigned i = 0; i < length-1; i++) {
905 const char *s = compStr+i;
906 for (const char c = *s++; *s; s++)
907 if (c == *s)
908 return true;
909 }
910 return false;
911}
Chris Lattner42158e72007-08-02 23:36:59 +0000912
913/// getEncodedElementAccess - We encode fields with two bits per component.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000914unsigned OCUVectorElementExpr::getEncodedElementAccess() const {
Chris Lattner42158e72007-08-02 23:36:59 +0000915 const char *compStr = Accessor.getName();
Chris Lattnera0d03a72007-08-03 17:31:20 +0000916 unsigned length = getNumElements();
Chris Lattner42158e72007-08-02 23:36:59 +0000917
918 unsigned Result = 0;
919
920 while (length--) {
921 Result <<= 2;
922 int Idx = OCUVectorType::getAccessorIdx(compStr[length]);
923 assert(Idx != -1 && "Invalid accessor letter");
924 Result |= Idx;
925 }
926 return Result;
927}
928
Steve Naroff4ed9d662007-09-27 14:38:14 +0000929// constructor for instance messages.
Steve Naroff6cb1d362007-09-28 22:22:11 +0000930ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Steve Naroff1e1c3912007-11-03 16:37:59 +0000931 QualType retType, ObjcMethodDecl *mproto,
932 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +0000933 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +0000934 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
935 MethodProto(mproto), ClassName(0) {
Steve Naroff9f176d12007-11-15 13:05:42 +0000936 NumArgs = nargs;
937 SubExprs = new Expr*[NumArgs+1];
Steve Naroff4ed9d662007-09-27 14:38:14 +0000938 SubExprs[RECEIVER] = receiver;
Steve Naroff9f176d12007-11-15 13:05:42 +0000939 if (NumArgs) {
940 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +0000941 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
942 }
Steve Naroffc39ca262007-09-18 23:55:05 +0000943 LBracloc = LBrac;
944 RBracloc = RBrac;
945}
946
Steve Naroff4ed9d662007-09-27 14:38:14 +0000947// constructor for class messages.
948// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff6cb1d362007-09-28 22:22:11 +0000949ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Steve Naroff1e1c3912007-11-03 16:37:59 +0000950 QualType retType, ObjcMethodDecl *mproto,
951 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +0000952 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +0000953 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
954 MethodProto(mproto), ClassName(clsName) {
Steve Naroff9f176d12007-11-15 13:05:42 +0000955 NumArgs = nargs;
956 SubExprs = new Expr*[NumArgs+1];
Steve Naroffc39ca262007-09-18 23:55:05 +0000957 SubExprs[RECEIVER] = 0;
Steve Naroff9f176d12007-11-15 13:05:42 +0000958 if (NumArgs) {
959 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +0000960 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
961 }
Steve Naroffc39ca262007-09-18 23:55:05 +0000962 LBracloc = LBrac;
963 RBracloc = RBrac;
964}
965
Chris Lattnerf624cd22007-10-25 00:29:32 +0000966
967bool ChooseExpr::isConditionTrue(ASTContext &C) const {
968 llvm::APSInt CondVal(32);
969 bool IsConst = getCond()->isIntegerConstantExpr(CondVal, C);
970 assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst;
971 return CondVal != 0;
972}
973
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000974//===----------------------------------------------------------------------===//
975// Child Iterators for iterating over subexpressions/substatements
976//===----------------------------------------------------------------------===//
977
978// DeclRefExpr
Ted Kremeneka6478552007-10-18 23:28:49 +0000979Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
980Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000981
Steve Naroff5eb2a4a2007-11-12 14:29:37 +0000982// ObjCIvarRefExpr
983Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return child_iterator(); }
984Stmt::child_iterator ObjCIvarRefExpr::child_end() { return child_iterator(); }
985
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000986// PreDefinedExpr
Ted Kremeneka6478552007-10-18 23:28:49 +0000987Stmt::child_iterator PreDefinedExpr::child_begin() { return child_iterator(); }
988Stmt::child_iterator PreDefinedExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000989
990// IntegerLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +0000991Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
992Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000993
994// CharacterLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +0000995Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); }
996Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000997
998// FloatingLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +0000999Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1000Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001001
Chris Lattner1de66eb2007-08-26 03:42:43 +00001002// ImaginaryLiteral
1003Stmt::child_iterator ImaginaryLiteral::child_begin() {
1004 return reinterpret_cast<Stmt**>(&Val);
1005}
1006Stmt::child_iterator ImaginaryLiteral::child_end() {
1007 return reinterpret_cast<Stmt**>(&Val)+1;
1008}
1009
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001010// StringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001011Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1012Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001013
1014// ParenExpr
1015Stmt::child_iterator ParenExpr::child_begin() {
1016 return reinterpret_cast<Stmt**>(&Val);
1017}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001018Stmt::child_iterator ParenExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001019 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001020}
1021
1022// UnaryOperator
1023Stmt::child_iterator UnaryOperator::child_begin() {
1024 return reinterpret_cast<Stmt**>(&Val);
1025}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001026Stmt::child_iterator UnaryOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001027 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001028}
1029
1030// SizeOfAlignOfTypeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001031Stmt::child_iterator SizeOfAlignOfTypeExpr::child_begin() {
1032 return child_iterator();
1033}
1034Stmt::child_iterator SizeOfAlignOfTypeExpr::child_end() {
1035 return child_iterator();
1036}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001037
1038// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001039Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001040 return reinterpret_cast<Stmt**>(&SubExprs);
1041}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001042Stmt::child_iterator ArraySubscriptExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001043 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001044}
1045
1046// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001047Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek15ede502007-08-27 21:11:44 +00001048 return reinterpret_cast<Stmt**>(&SubExprs[0]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001049}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001050Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek15ede502007-08-27 21:11:44 +00001051 return reinterpret_cast<Stmt**>(&SubExprs[NumArgs+ARGS_START]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001052}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001053
1054// MemberExpr
1055Stmt::child_iterator MemberExpr::child_begin() {
1056 return reinterpret_cast<Stmt**>(&Base);
1057}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001058Stmt::child_iterator MemberExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001059 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001060}
1061
1062// OCUVectorElementExpr
1063Stmt::child_iterator OCUVectorElementExpr::child_begin() {
1064 return reinterpret_cast<Stmt**>(&Base);
1065}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001066Stmt::child_iterator OCUVectorElementExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001067 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001068}
1069
1070// CompoundLiteralExpr
1071Stmt::child_iterator CompoundLiteralExpr::child_begin() {
1072 return reinterpret_cast<Stmt**>(&Init);
1073}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001074Stmt::child_iterator CompoundLiteralExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001075 return reinterpret_cast<Stmt**>(&Init)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001076}
1077
1078// ImplicitCastExpr
1079Stmt::child_iterator ImplicitCastExpr::child_begin() {
1080 return reinterpret_cast<Stmt**>(&Op);
1081}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001082Stmt::child_iterator ImplicitCastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001083 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001084}
1085
1086// CastExpr
1087Stmt::child_iterator CastExpr::child_begin() {
1088 return reinterpret_cast<Stmt**>(&Op);
1089}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001090Stmt::child_iterator CastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001091 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001092}
1093
1094// BinaryOperator
1095Stmt::child_iterator BinaryOperator::child_begin() {
1096 return reinterpret_cast<Stmt**>(&SubExprs);
1097}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001098Stmt::child_iterator BinaryOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001099 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001100}
1101
1102// ConditionalOperator
1103Stmt::child_iterator ConditionalOperator::child_begin() {
1104 return reinterpret_cast<Stmt**>(&SubExprs);
1105}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001106Stmt::child_iterator ConditionalOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001107 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001108}
1109
1110// AddrLabelExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001111Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1112Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001113
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001114// StmtExpr
1115Stmt::child_iterator StmtExpr::child_begin() {
1116 return reinterpret_cast<Stmt**>(&SubStmt);
1117}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001118Stmt::child_iterator StmtExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001119 return reinterpret_cast<Stmt**>(&SubStmt)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001120}
1121
1122// TypesCompatibleExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001123Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1124 return child_iterator();
1125}
1126
1127Stmt::child_iterator TypesCompatibleExpr::child_end() {
1128 return child_iterator();
1129}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001130
1131// ChooseExpr
1132Stmt::child_iterator ChooseExpr::child_begin() {
1133 return reinterpret_cast<Stmt**>(&SubExprs);
1134}
1135
1136Stmt::child_iterator ChooseExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001137 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001138}
1139
Anders Carlsson36760332007-10-15 20:28:48 +00001140// VAArgExpr
1141Stmt::child_iterator VAArgExpr::child_begin() {
1142 return reinterpret_cast<Stmt**>(&Val);
1143}
1144
1145Stmt::child_iterator VAArgExpr::child_end() {
1146 return reinterpret_cast<Stmt**>(&Val)+1;
1147}
1148
Anders Carlsson762b7c72007-08-31 04:56:16 +00001149// InitListExpr
1150Stmt::child_iterator InitListExpr::child_begin() {
1151 return reinterpret_cast<Stmt**>(&InitExprs[0]);
1152}
1153Stmt::child_iterator InitListExpr::child_end() {
1154 return reinterpret_cast<Stmt**>(&InitExprs[NumInits]);
1155}
1156
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001157// ObjCStringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001158Stmt::child_iterator ObjCStringLiteral::child_begin() {
1159 return child_iterator();
1160}
1161Stmt::child_iterator ObjCStringLiteral::child_end() {
1162 return child_iterator();
1163}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001164
1165// ObjCEncodeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001166Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1167Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001168
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001169// ObjCSelectorExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001170Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1171 return child_iterator();
1172}
1173Stmt::child_iterator ObjCSelectorExpr::child_end() {
1174 return child_iterator();
1175}
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001176
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001177// ObjCProtocolExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001178Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1179 return child_iterator();
1180}
1181Stmt::child_iterator ObjCProtocolExpr::child_end() {
1182 return child_iterator();
1183}
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001184
Steve Naroffc39ca262007-09-18 23:55:05 +00001185// ObjCMessageExpr
1186Stmt::child_iterator ObjCMessageExpr::child_begin() {
1187 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1188}
1189Stmt::child_iterator ObjCMessageExpr::child_end() {
Steve Naroff4ed9d662007-09-27 14:38:14 +00001190 return reinterpret_cast<Stmt**>(&SubExprs[getNumArgs()+ARGS_START]);
Steve Naroffc39ca262007-09-18 23:55:05 +00001191}
1192