blob: e327ffb91177e300981d2955b8d568c322c902fe [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"
Nate Begemanc8e51f82008-05-09 06:41:27 +000019#include "llvm/Constants.h"
20#include "llvm/DerivedTypes.h"
Chris Lattner4b009652007-07-25 00:24:17 +000021using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// Primary Expressions.
25//===----------------------------------------------------------------------===//
26
27StringLiteral::StringLiteral(const char *strData, unsigned byteLength,
28 bool Wide, QualType t, SourceLocation firstLoc,
29 SourceLocation lastLoc) :
30 Expr(StringLiteralClass, t) {
31 // OPTIMIZE: could allocate this appended to the StringLiteral.
32 char *AStrData = new char[byteLength];
33 memcpy(AStrData, strData, byteLength);
34 StrData = AStrData;
35 ByteLength = byteLength;
36 IsWide = Wide;
37 firstTokLoc = firstLoc;
38 lastTokLoc = lastLoc;
39}
40
41StringLiteral::~StringLiteral() {
42 delete[] StrData;
43}
44
45bool UnaryOperator::isPostfix(Opcode Op) {
46 switch (Op) {
47 case PostInc:
48 case PostDec:
49 return true;
50 default:
51 return false;
52 }
53}
54
55/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
56/// corresponds to, e.g. "sizeof" or "[pre]++".
57const char *UnaryOperator::getOpcodeStr(Opcode Op) {
58 switch (Op) {
59 default: assert(0 && "Unknown unary operator");
60 case PostInc: return "++";
61 case PostDec: return "--";
62 case PreInc: return "++";
63 case PreDec: return "--";
64 case AddrOf: return "&";
65 case Deref: return "*";
66 case Plus: return "+";
67 case Minus: return "-";
68 case Not: return "~";
69 case LNot: return "!";
70 case Real: return "__real";
71 case Imag: return "__imag";
72 case SizeOf: return "sizeof";
73 case AlignOf: return "alignof";
74 case Extension: return "__extension__";
Chris Lattner0d9bcea2007-08-30 17:45:32 +000075 case OffsetOf: return "__builtin_offsetof";
Chris Lattner4b009652007-07-25 00:24:17 +000076 }
77}
78
79//===----------------------------------------------------------------------===//
80// Postfix Operators.
81//===----------------------------------------------------------------------===//
82
Nate Begeman9f3bfb72008-01-17 17:46:27 +000083
Chris Lattner4b009652007-07-25 00:24:17 +000084CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
85 SourceLocation rparenloc)
Ted Kremeneke4acb9c2007-08-24 18:13:47 +000086 : Expr(CallExprClass, t), NumArgs(numargs) {
87 SubExprs = new Expr*[numargs+1];
88 SubExprs[FN] = fn;
Chris Lattner4b009652007-07-25 00:24:17 +000089 for (unsigned i = 0; i != numargs; ++i)
Ted Kremeneke4acb9c2007-08-24 18:13:47 +000090 SubExprs[i+ARGS_START] = args[i];
Chris Lattner4b009652007-07-25 00:24:17 +000091 RParenLoc = rparenloc;
92}
93
Chris Lattnerc257c0d2007-12-28 05:25:02 +000094/// setNumArgs - This changes the number of arguments present in this call.
95/// Any orphaned expressions are deleted by this, and any new operands are set
96/// to null.
97void CallExpr::setNumArgs(unsigned NumArgs) {
98 // No change, just return.
99 if (NumArgs == getNumArgs()) return;
100
101 // If shrinking # arguments, just delete the extras and forgot them.
102 if (NumArgs < getNumArgs()) {
103 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
104 delete getArg(i);
105 this->NumArgs = NumArgs;
106 return;
107 }
108
109 // Otherwise, we are growing the # arguments. New an bigger argument array.
110 Expr **NewSubExprs = new Expr*[NumArgs+1];
111 // Copy over args.
112 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
113 NewSubExprs[i] = SubExprs[i];
114 // Null out new args.
115 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
116 NewSubExprs[i] = 0;
117
118 delete[] SubExprs;
119 SubExprs = NewSubExprs;
120 this->NumArgs = NumArgs;
121}
122
Steve Naroff44aec4c2008-01-31 01:07:12 +0000123bool CallExpr::isBuiltinConstantExpr() const {
124 // All simple function calls (e.g. func()) are implicitly cast to pointer to
125 // function. As a result, we try and obtain the DeclRefExpr from the
126 // ImplicitCastExpr.
127 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
128 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
129 return false;
130
131 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
132 if (!DRE)
133 return false;
134
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000135 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
136 if (!FDecl)
137 return false;
138
139 unsigned builtinID = FDecl->getIdentifier()->getBuiltinID();
140 if (!builtinID)
141 return false;
142
143 // We have a builtin that is a constant expression
Eli Friedman8845a812008-05-16 13:28:37 +0000144 return builtinID == Builtin::BI__builtin___CFStringMakeConstantString ||
145 builtinID == Builtin::BI__builtin_classify_type;
Steve Naroff44aec4c2008-01-31 01:07:12 +0000146}
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000147
Steve Naroff8d3b1702007-08-08 22:15:55 +0000148bool CallExpr::isBuiltinClassifyType(llvm::APSInt &Result) const {
149 // The following enum mimics gcc's internal "typeclass.h" file.
150 enum gcc_type_class {
151 no_type_class = -1,
152 void_type_class, integer_type_class, char_type_class,
153 enumeral_type_class, boolean_type_class,
154 pointer_type_class, reference_type_class, offset_type_class,
155 real_type_class, complex_type_class,
156 function_type_class, method_type_class,
157 record_type_class, union_type_class,
158 array_type_class, string_type_class,
159 lang_type_class
160 };
161 Result.setIsSigned(true);
162
163 // All simple function calls (e.g. func()) are implicitly cast to pointer to
164 // function. As a result, we try and obtain the DeclRefExpr from the
165 // ImplicitCastExpr.
166 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
167 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
168 return false;
169 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
170 if (!DRE)
171 return false;
172
173 // We have a DeclRefExpr.
174 if (strcmp(DRE->getDecl()->getName(), "__builtin_classify_type") == 0) {
175 // If no argument was supplied, default to "no_type_class". This isn't
176 // ideal, however it's what gcc does.
177 Result = static_cast<uint64_t>(no_type_class);
178 if (NumArgs >= 1) {
179 QualType argType = getArg(0)->getType();
180
181 if (argType->isVoidType())
182 Result = void_type_class;
183 else if (argType->isEnumeralType())
184 Result = enumeral_type_class;
185 else if (argType->isBooleanType())
186 Result = boolean_type_class;
187 else if (argType->isCharType())
188 Result = string_type_class; // gcc doesn't appear to use char_type_class
189 else if (argType->isIntegerType())
190 Result = integer_type_class;
191 else if (argType->isPointerType())
192 Result = pointer_type_class;
193 else if (argType->isReferenceType())
194 Result = reference_type_class;
195 else if (argType->isRealType())
196 Result = real_type_class;
197 else if (argType->isComplexType())
198 Result = complex_type_class;
199 else if (argType->isFunctionType())
200 Result = function_type_class;
201 else if (argType->isStructureType())
202 Result = record_type_class;
203 else if (argType->isUnionType())
204 Result = union_type_class;
205 else if (argType->isArrayType())
206 Result = array_type_class;
207 else if (argType->isUnionType())
208 Result = union_type_class;
209 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
Chris Lattner19b8f1a2007-11-08 17:56:40 +0000210 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
Steve Naroff8d3b1702007-08-08 22:15:55 +0000211 }
212 return true;
213 }
214 return false;
215}
216
Chris Lattner4b009652007-07-25 00:24:17 +0000217/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
218/// corresponds to, e.g. "<<=".
219const char *BinaryOperator::getOpcodeStr(Opcode Op) {
220 switch (Op) {
221 default: assert(0 && "Unknown binary operator");
222 case Mul: return "*";
223 case Div: return "/";
224 case Rem: return "%";
225 case Add: return "+";
226 case Sub: return "-";
227 case Shl: return "<<";
228 case Shr: return ">>";
229 case LT: return "<";
230 case GT: return ">";
231 case LE: return "<=";
232 case GE: return ">=";
233 case EQ: return "==";
234 case NE: return "!=";
235 case And: return "&";
236 case Xor: return "^";
237 case Or: return "|";
238 case LAnd: return "&&";
239 case LOr: return "||";
240 case Assign: return "=";
241 case MulAssign: return "*=";
242 case DivAssign: return "/=";
243 case RemAssign: return "%=";
244 case AddAssign: return "+=";
245 case SubAssign: return "-=";
246 case ShlAssign: return "<<=";
247 case ShrAssign: return ">>=";
248 case AndAssign: return "&=";
249 case XorAssign: return "^=";
250 case OrAssign: return "|=";
251 case Comma: return ",";
252 }
253}
254
Anders Carlsson762b7c72007-08-31 04:56:16 +0000255InitListExpr::InitListExpr(SourceLocation lbraceloc,
256 Expr **initexprs, unsigned numinits,
257 SourceLocation rbraceloc)
Steve Naroff2e335472008-05-01 02:04:18 +0000258 : Expr(InitListExprClass, QualType()),
259 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc)
Anders Carlsson762b7c72007-08-31 04:56:16 +0000260{
Anders Carlsson762b7c72007-08-31 04:56:16 +0000261 for (unsigned i = 0; i != numinits; i++)
Steve Naroff2e335472008-05-01 02:04:18 +0000262 InitExprs.push_back(initexprs[i]);
Anders Carlsson762b7c72007-08-31 04:56:16 +0000263}
Chris Lattner4b009652007-07-25 00:24:17 +0000264
265//===----------------------------------------------------------------------===//
266// Generic Expression Routines
267//===----------------------------------------------------------------------===//
268
269/// hasLocalSideEffect - Return true if this immediate expression has side
270/// effects, not counting any sub-expressions.
271bool Expr::hasLocalSideEffect() const {
272 switch (getStmtClass()) {
273 default:
274 return false;
275 case ParenExprClass:
276 return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect();
277 case UnaryOperatorClass: {
278 const UnaryOperator *UO = cast<UnaryOperator>(this);
279
280 switch (UO->getOpcode()) {
281 default: return false;
282 case UnaryOperator::PostInc:
283 case UnaryOperator::PostDec:
284 case UnaryOperator::PreInc:
285 case UnaryOperator::PreDec:
286 return true; // ++/--
287
288 case UnaryOperator::Deref:
289 // Dereferencing a volatile pointer is a side-effect.
290 return getType().isVolatileQualified();
291 case UnaryOperator::Real:
292 case UnaryOperator::Imag:
293 // accessing a piece of a volatile complex is a side-effect.
294 return UO->getSubExpr()->getType().isVolatileQualified();
295
296 case UnaryOperator::Extension:
297 return UO->getSubExpr()->hasLocalSideEffect();
298 }
299 }
Chris Lattneref95ffd2007-12-01 06:07:34 +0000300 case BinaryOperatorClass: {
301 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
302 // Consider comma to have side effects if the LHS and RHS both do.
303 if (BinOp->getOpcode() == BinaryOperator::Comma)
304 return BinOp->getLHS()->hasLocalSideEffect() &&
305 BinOp->getRHS()->hasLocalSideEffect();
306
307 return BinOp->isAssignmentOp();
308 }
Chris Lattner06078d22007-08-25 02:00:02 +0000309 case CompoundAssignOperatorClass:
Chris Lattner9c0da3b2007-08-25 01:55:00 +0000310 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000311
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000312 case ConditionalOperatorClass: {
313 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
314 return Exp->getCond()->hasLocalSideEffect()
315 || (Exp->getLHS() && Exp->getLHS()->hasLocalSideEffect())
316 || (Exp->getRHS() && Exp->getRHS()->hasLocalSideEffect());
317 }
318
Chris Lattner4b009652007-07-25 00:24:17 +0000319 case MemberExprClass:
320 case ArraySubscriptExprClass:
321 // If the base pointer or element is to a volatile pointer/field, accessing
322 // if is a side effect.
323 return getType().isVolatileQualified();
324
325 case CallExprClass:
326 // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }"
327 // should warn.
328 return true;
Chris Lattner99f5f0b2007-09-26 22:06:30 +0000329 case ObjCMessageExprClass:
330 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000331
332 case CastExprClass:
333 // If this is a cast to void, check the operand. Otherwise, the result of
334 // the cast is unused.
335 if (getType()->isVoidType())
336 return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect();
337 return false;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000338
Eli Friedmanb924c7f2008-05-19 21:24:43 +0000339 case ImplicitCastExprClass:
340 // Check the operand, since implicit casts are inserted by Sema
341 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasLocalSideEffect();
342
Chris Lattner3e254fb2008-04-08 04:40:51 +0000343 case CXXDefaultArgExprClass:
344 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasLocalSideEffect();
Chris Lattner4b009652007-07-25 00:24:17 +0000345 }
346}
347
348/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
349/// incomplete type other than void. Nonarray expressions that can be lvalues:
350/// - name, where name must be a variable
351/// - e[i]
352/// - (e), where e must be an lvalue
353/// - e.name, where e must be an lvalue
354/// - e->name
355/// - *e, the type of e cannot be a function type
356/// - string-constant
Chris Lattner5bf72022007-10-30 22:53:42 +0000357/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Chris Lattner4b009652007-07-25 00:24:17 +0000358/// - reference type [C++ [expr]]
359///
360Expr::isLvalueResult Expr::isLvalue() const {
361 // first, check the type (C99 6.3.2.1)
362 if (TR->isFunctionType()) // from isObjectType()
363 return LV_NotObjectType;
364
Steve Naroffec7736d2008-02-10 01:39:04 +0000365 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner35fef522008-02-20 20:55:12 +0000366 if (TR->isVoidType() && !TR.getCanonicalType().getCVRQualifiers())
Steve Naroffec7736d2008-02-10 01:39:04 +0000367 return LV_IncompleteVoidType;
368
Chris Lattner4b009652007-07-25 00:24:17 +0000369 if (TR->isReferenceType()) // C++ [expr]
370 return LV_Valid;
371
372 // the type looks fine, now check the expression
373 switch (getStmtClass()) {
374 case StringLiteralClass: // C99 6.5.1p4
Anders Carlsson9e933a22007-11-30 22:47:59 +0000375 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000376 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
377 // For vectors, make sure base is an lvalue (i.e. not a function call).
378 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
379 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue();
380 return LV_Valid;
381 case DeclRefExprClass: // C99 6.5.1p2
382 if (isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl()))
383 return LV_Valid;
384 break;
385 case MemberExprClass: { // C99 6.5.2.3p4
386 const MemberExpr *m = cast<MemberExpr>(this);
387 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue();
388 }
Chris Lattner5bf72022007-10-30 22:53:42 +0000389 case UnaryOperatorClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000390 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner5bf72022007-10-30 22:53:42 +0000391 return LV_Valid; // C99 6.5.3p4
392
393 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
394 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag)
395 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(); // GNU.
Chris Lattner4b009652007-07-25 00:24:17 +0000396 break;
397 case ParenExprClass: // C99 6.5.1p5
398 return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
Steve Naroffc7c66532007-12-05 04:00:10 +0000399 case CompoundLiteralExprClass: // C99 6.5.2.5p5
400 return LV_Valid;
Nate Begemanaf6ed502008-04-18 23:10:10 +0000401 case ExtVectorElementExprClass:
402 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroffba67f692007-07-30 03:29:09 +0000403 return LV_DuplicateVectorComponents;
404 return LV_Valid;
Steve Naroff46f18f22007-11-12 14:34:27 +0000405 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
406 return LV_Valid;
Chris Lattner7e637512008-01-12 08:14:25 +0000407 case PreDefinedExprClass:
408 return LV_Valid;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000409 case CXXDefaultArgExprClass:
410 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue();
Chris Lattner4b009652007-07-25 00:24:17 +0000411 default:
412 break;
413 }
414 return LV_InvalidExpression;
415}
416
417/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
418/// does not have an incomplete type, does not have a const-qualified type, and
419/// if it is a structure or union, does not have any member (including,
420/// recursively, any member or element of all contained aggregates or unions)
421/// with a const-qualified type.
422Expr::isModifiableLvalueResult Expr::isModifiableLvalue() const {
423 isLvalueResult lvalResult = isLvalue();
424
425 switch (lvalResult) {
426 case LV_Valid: break;
427 case LV_NotObjectType: return MLV_NotObjectType;
428 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroffba67f692007-07-30 03:29:09 +0000429 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner4b009652007-07-25 00:24:17 +0000430 case LV_InvalidExpression: return MLV_InvalidExpression;
431 }
432 if (TR.isConstQualified())
433 return MLV_ConstQualified;
434 if (TR->isArrayType())
435 return MLV_ArrayType;
436 if (TR->isIncompleteType())
437 return MLV_IncompleteType;
438
439 if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) {
440 if (r->hasConstFields())
441 return MLV_ConstQualified;
442 }
443 return MLV_Valid;
444}
445
Ted Kremenek5778d622008-02-27 18:39:48 +0000446/// hasGlobalStorage - Return true if this expression has static storage
Chris Lattner743ec372007-11-27 21:35:27 +0000447/// duration. This means that the address of this expression is a link-time
448/// constant.
Ted Kremenek5778d622008-02-27 18:39:48 +0000449bool Expr::hasGlobalStorage() const {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000450 switch (getStmtClass()) {
451 default:
452 return false;
Chris Lattner743ec372007-11-27 21:35:27 +0000453 case ParenExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000454 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
Chris Lattner743ec372007-11-27 21:35:27 +0000455 case ImplicitCastExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000456 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
Steve Naroffbe37fc02008-01-14 18:19:28 +0000457 case CompoundLiteralExprClass:
458 return cast<CompoundLiteralExpr>(this)->isFileScope();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000459 case DeclRefExprClass: {
460 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
461 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Ted Kremenek5778d622008-02-27 18:39:48 +0000462 return VD->hasGlobalStorage();
Seo Sanghyeone330ae72008-04-04 09:45:30 +0000463 if (isa<FunctionDecl>(D))
464 return true;
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000465 return false;
466 }
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000467 case MemberExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000468 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek5778d622008-02-27 18:39:48 +0000469 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000470 }
Chris Lattner743ec372007-11-27 21:35:27 +0000471 case ArraySubscriptExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000472 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattner7e637512008-01-12 08:14:25 +0000473 case PreDefinedExprClass:
474 return true;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000475 case CXXDefaultArgExprClass:
476 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000477 }
478}
479
Ted Kremenek87e30c52008-01-17 16:57:34 +0000480Expr* Expr::IgnoreParens() {
481 Expr* E = this;
482 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
483 E = P->getSubExpr();
484
485 return E;
486}
487
Chris Lattner7a48d9c2008-02-13 01:02:39 +0000488/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
489/// or CastExprs or ImplicitCastExprs, returning their operand.
490Expr *Expr::IgnoreParenCasts() {
491 Expr *E = this;
492 while (true) {
493 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
494 E = P->getSubExpr();
495 else if (CastExpr *P = dyn_cast<CastExpr>(E))
496 E = P->getSubExpr();
497 else if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E))
498 E = P->getSubExpr();
499 else
500 return E;
501 }
502}
503
504
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000505bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000506 switch (getStmtClass()) {
507 default:
508 if (Loc) *Loc = getLocStart();
509 return false;
510 case ParenExprClass:
511 return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(Ctx, Loc);
512 case StringLiteralClass:
Steve Naroff4fdea132007-11-09 15:00:03 +0000513 case ObjCStringLiteralClass:
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000514 case FloatingLiteralClass:
515 case IntegerLiteralClass:
516 case CharacterLiteralClass:
517 case ImaginaryLiteralClass:
Anders Carlsson855d78d2007-10-17 00:52:43 +0000518 case TypesCompatibleExprClass:
519 case CXXBoolLiteralExprClass:
Chris Lattner06db6132007-10-18 00:20:32 +0000520 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000521 case CallExprClass: {
522 const CallExpr *CE = cast<CallExpr>(this);
Steve Naroff44aec4c2008-01-31 01:07:12 +0000523 if (CE->isBuiltinConstantExpr())
524 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000525 if (Loc) *Loc = getLocStart();
526 return false;
527 }
Chris Lattner42e86b62007-11-01 02:45:17 +0000528 case DeclRefExprClass: {
529 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
530 // Accept address of function.
531 if (isa<EnumConstantDecl>(D) || isa<FunctionDecl>(D))
Chris Lattner06db6132007-10-18 00:20:32 +0000532 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000533 if (Loc) *Loc = getLocStart();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000534 if (isa<VarDecl>(D))
535 return TR->isArrayType();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000536 return false;
Chris Lattner42e86b62007-11-01 02:45:17 +0000537 }
Steve Narofff91f9722008-01-09 00:05:37 +0000538 case CompoundLiteralExprClass:
539 if (Loc) *Loc = getLocStart();
540 // Allow "(int []){2,4}", since the array will be converted to a pointer.
Nate Begemanc4e28e42008-01-25 05:34:48 +0000541 // Allow "(vector type){2,4}" since the elements are all constant.
542 return TR->isArrayType() || TR->isVectorType();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000543 case UnaryOperatorClass: {
544 const UnaryOperator *Exp = cast<UnaryOperator>(this);
545
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000546 // C99 6.6p9
Chris Lattner35b662f2007-12-11 23:11:17 +0000547 if (Exp->getOpcode() == UnaryOperator::AddrOf) {
Ted Kremenek5778d622008-02-27 18:39:48 +0000548 if (!Exp->getSubExpr()->hasGlobalStorage()) {
Chris Lattner35b662f2007-12-11 23:11:17 +0000549 if (Loc) *Loc = getLocStart();
550 return false;
551 }
552 return true;
553 }
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000554
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000555 // Get the operand value. If this is sizeof/alignof, do not evalute the
556 // operand. This affects C99 6.6p3.
Steve Narofff0b23542008-01-10 22:15:12 +0000557 if (!Exp->isSizeOfAlignOfOp() &&
558 Exp->getOpcode() != UnaryOperator::OffsetOf &&
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000559 !Exp->getSubExpr()->isConstantExpr(Ctx, Loc))
560 return false;
561
562 switch (Exp->getOpcode()) {
563 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
564 // See C99 6.6p3.
565 default:
566 if (Loc) *Loc = Exp->getOperatorLoc();
567 return false;
568 case UnaryOperator::Extension:
569 return true; // FIXME: this is wrong.
570 case UnaryOperator::SizeOf:
571 case UnaryOperator::AlignOf:
Steve Narofff0b23542008-01-10 22:15:12 +0000572 case UnaryOperator::OffsetOf:
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000573 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Eli Friedman62f67fd2008-02-15 12:20:59 +0000574 if (!Exp->getSubExpr()->getType()->isConstantSizeType()) {
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000575 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000576 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000577 }
Chris Lattner06db6132007-10-18 00:20:32 +0000578 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000579 case UnaryOperator::LNot:
580 case UnaryOperator::Plus:
581 case UnaryOperator::Minus:
582 case UnaryOperator::Not:
Chris Lattner06db6132007-10-18 00:20:32 +0000583 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000584 }
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000585 }
586 case SizeOfAlignOfTypeExprClass: {
587 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
588 // alignof always evaluates to a constant.
Chris Lattner20515462008-02-21 05:45:29 +0000589 if (Exp->isSizeOf() && !Exp->getArgumentType()->isVoidType() &&
590 !Exp->getArgumentType()->isConstantSizeType()) {
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000591 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000592 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000593 }
Chris Lattner06db6132007-10-18 00:20:32 +0000594 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000595 }
596 case BinaryOperatorClass: {
597 const BinaryOperator *Exp = cast<BinaryOperator>(this);
598
599 // The LHS of a constant expr is always evaluated and needed.
600 if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
601 return false;
602
603 if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
604 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000605 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000606 }
607 case ImplicitCastExprClass:
608 case CastExprClass: {
609 const Expr *SubExpr;
610 SourceLocation CastLoc;
611 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
612 SubExpr = C->getSubExpr();
613 CastLoc = C->getLParenLoc();
614 } else {
615 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
616 CastLoc = getLocStart();
617 }
618 if (!SubExpr->isConstantExpr(Ctx, Loc)) {
619 if (Loc) *Loc = SubExpr->getLocStart();
620 return false;
621 }
Chris Lattner06db6132007-10-18 00:20:32 +0000622 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000623 }
624 case ConditionalOperatorClass: {
625 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Chris Lattner06db6132007-10-18 00:20:32 +0000626 if (!Exp->getCond()->isConstantExpr(Ctx, Loc) ||
Anders Carlsson37365fc2007-11-30 19:04:31 +0000627 // Handle the GNU extension for missing LHS.
628 !(Exp->getLHS() && Exp->getLHS()->isConstantExpr(Ctx, Loc)) ||
Chris Lattner06db6132007-10-18 00:20:32 +0000629 !Exp->getRHS()->isConstantExpr(Ctx, Loc))
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000630 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000631 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000632 }
Steve Narofff0b23542008-01-10 22:15:12 +0000633 case InitListExprClass: {
634 const InitListExpr *Exp = cast<InitListExpr>(this);
635 unsigned numInits = Exp->getNumInits();
636 for (unsigned i = 0; i < numInits; i++) {
637 if (!Exp->getInit(i)->isConstantExpr(Ctx, Loc)) {
638 if (Loc) *Loc = Exp->getInit(i)->getLocStart();
639 return false;
640 }
641 }
642 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000643 }
Chris Lattner3e254fb2008-04-08 04:40:51 +0000644 case CXXDefaultArgExprClass:
645 return cast<CXXDefaultArgExpr>(this)->getExpr()->isConstantExpr(Ctx, Loc);
Steve Narofff0b23542008-01-10 22:15:12 +0000646 }
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000647}
648
Chris Lattner4b009652007-07-25 00:24:17 +0000649/// isIntegerConstantExpr - this recursive routine will test if an expression is
650/// an integer constant expression. Note: With the introduction of VLA's in
651/// C99 the result of the sizeof operator is no longer always a constant
652/// expression. The generalization of the wording to include any subexpression
653/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
654/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
655/// "0 || f()" can be treated as a constant expression. In C90 this expression,
656/// occurring in a context requiring a constant, would have been a constraint
657/// violation. FIXME: This routine currently implements C90 semantics.
658/// To properly implement C99 semantics this routine will need to evaluate
659/// expressions involving operators previously mentioned.
660
661/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
662/// comma, etc
663///
664/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattner9d020b32007-09-26 00:47:26 +0000665/// permit this. This includes things like (int)1e1000
Chris Lattner4b009652007-07-25 00:24:17 +0000666///
667/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
668/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
669/// cast+dereference.
670bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
671 SourceLocation *Loc, bool isEvaluated) const {
672 switch (getStmtClass()) {
673 default:
674 if (Loc) *Loc = getLocStart();
675 return false;
676 case ParenExprClass:
677 return cast<ParenExpr>(this)->getSubExpr()->
678 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
679 case IntegerLiteralClass:
680 Result = cast<IntegerLiteral>(this)->getValue();
681 break;
682 case CharacterLiteralClass: {
683 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Chris Lattner8cd0e932008-03-05 18:54:05 +0000684 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattner4b009652007-07-25 00:24:17 +0000685 Result = CL->getValue();
686 Result.setIsUnsigned(!getType()->isSignedIntegerType());
687 break;
688 }
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000689 case TypesCompatibleExprClass: {
690 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Chris Lattner8cd0e932008-03-05 18:54:05 +0000691 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Steve Naroff85f0dc52007-10-15 20:41:53 +0000692 Result = Ctx.typesAreCompatible(TCE->getArgType1(), TCE->getArgType2());
Steve Naroff1200b5a2007-08-02 00:13:27 +0000693 break;
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000694 }
Steve Naroff8d3b1702007-08-08 22:15:55 +0000695 case CallExprClass: {
696 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner8cd0e932008-03-05 18:54:05 +0000697 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Steve Naroff8d3b1702007-08-08 22:15:55 +0000698 if (CE->isBuiltinClassifyType(Result))
699 break;
700 if (Loc) *Loc = getLocStart();
701 return false;
702 }
Chris Lattner4b009652007-07-25 00:24:17 +0000703 case DeclRefExprClass:
704 if (const EnumConstantDecl *D =
705 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
706 Result = D->getInitVal();
707 break;
708 }
709 if (Loc) *Loc = getLocStart();
710 return false;
711 case UnaryOperatorClass: {
712 const UnaryOperator *Exp = cast<UnaryOperator>(this);
713
714 // Get the operand value. If this is sizeof/alignof, do not evalute the
715 // operand. This affects C99 6.6p3.
Anders Carlsson52774ad2008-01-29 15:56:48 +0000716 if (!Exp->isSizeOfAlignOfOp() && !Exp->isOffsetOfOp() &&
Chris Lattner5a9b6242007-08-23 21:42:50 +0000717 !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000718 return false;
719
720 switch (Exp->getOpcode()) {
721 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
722 // See C99 6.6p3.
723 default:
724 if (Loc) *Loc = Exp->getOperatorLoc();
725 return false;
726 case UnaryOperator::Extension:
727 return true; // FIXME: this is wrong.
728 case UnaryOperator::SizeOf:
729 case UnaryOperator::AlignOf:
Chris Lattner20515462008-02-21 05:45:29 +0000730 // Return the result in the right width.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000731 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattner20515462008-02-21 05:45:29 +0000732
733 // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
734 if (Exp->getSubExpr()->getType()->isVoidType()) {
735 Result = 1;
736 break;
737 }
738
Chris Lattner4b009652007-07-25 00:24:17 +0000739 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Eli Friedman62f67fd2008-02-15 12:20:59 +0000740 if (!Exp->getSubExpr()->getType()->isConstantSizeType()) {
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000741 if (Loc) *Loc = Exp->getOperatorLoc();
Chris Lattner4b009652007-07-25 00:24:17 +0000742 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000743 }
Chris Lattner4b009652007-07-25 00:24:17 +0000744
Chris Lattner4b009652007-07-25 00:24:17 +0000745 // Get information about the size or align.
Chris Lattnerd4dc2672008-01-02 21:54:09 +0000746 if (Exp->getSubExpr()->getType()->isFunctionType()) {
747 // GCC extension: sizeof(function) = 1.
748 Result = Exp->getOpcode() == UnaryOperator::AlignOf ? 4 : 1;
Chris Lattnerd9ffbc92007-11-27 18:22:04 +0000749 } else {
Chris Lattner8cd0e932008-03-05 18:54:05 +0000750 unsigned CharSize = Ctx.Target.getCharWidth();
Anders Carlsson1f86b032008-02-18 07:10:45 +0000751 if (Exp->getOpcode() == UnaryOperator::AlignOf)
Chris Lattner8cd0e932008-03-05 18:54:05 +0000752 Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType()) / CharSize;
Anders Carlsson1f86b032008-02-18 07:10:45 +0000753 else
Chris Lattner8cd0e932008-03-05 18:54:05 +0000754 Result = Ctx.getTypeSize(Exp->getSubExpr()->getType()) / CharSize;
Chris Lattnerd9ffbc92007-11-27 18:22:04 +0000755 }
Chris Lattner4b009652007-07-25 00:24:17 +0000756 break;
757 case UnaryOperator::LNot: {
Chris Lattnerf00fdc02008-01-25 19:16:19 +0000758 bool Val = Result == 0;
Chris Lattner8cd0e932008-03-05 18:54:05 +0000759 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattner4b009652007-07-25 00:24:17 +0000760 Result = Val;
761 break;
762 }
763 case UnaryOperator::Plus:
764 break;
765 case UnaryOperator::Minus:
766 Result = -Result;
767 break;
768 case UnaryOperator::Not:
769 Result = ~Result;
770 break;
Anders Carlsson52774ad2008-01-29 15:56:48 +0000771 case UnaryOperator::OffsetOf:
772 Result = Exp->evaluateOffsetOf(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000773 }
774 break;
775 }
776 case SizeOfAlignOfTypeExprClass: {
777 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
Chris Lattner20515462008-02-21 05:45:29 +0000778
779 // Return the result in the right width.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000780 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattner20515462008-02-21 05:45:29 +0000781
782 // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
783 if (Exp->getArgumentType()->isVoidType()) {
784 Result = 1;
785 break;
786 }
787
788 // alignof always evaluates to a constant, sizeof does if arg is not VLA.
Eli Friedman62f67fd2008-02-15 12:20:59 +0000789 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType()) {
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000790 if (Loc) *Loc = Exp->getOperatorLoc();
Chris Lattner4b009652007-07-25 00:24:17 +0000791 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000792 }
Chris Lattner4b009652007-07-25 00:24:17 +0000793
Chris Lattner4b009652007-07-25 00:24:17 +0000794 // Get information about the size or align.
Chris Lattnerd4dc2672008-01-02 21:54:09 +0000795 if (Exp->getArgumentType()->isFunctionType()) {
796 // GCC extension: sizeof(function) = 1.
797 Result = Exp->isSizeOf() ? 1 : 4;
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000798 } else {
Chris Lattner8cd0e932008-03-05 18:54:05 +0000799 unsigned CharSize = Ctx.Target.getCharWidth();
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000800 if (Exp->isSizeOf())
Chris Lattner8cd0e932008-03-05 18:54:05 +0000801 Result = Ctx.getTypeSize(Exp->getArgumentType()) / CharSize;
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000802 else
Chris Lattner8cd0e932008-03-05 18:54:05 +0000803 Result = Ctx.getTypeAlign(Exp->getArgumentType()) / CharSize;
Ted Kremeneka9d0fd92007-12-17 17:38:43 +0000804 }
Chris Lattner4b009652007-07-25 00:24:17 +0000805 break;
806 }
807 case BinaryOperatorClass: {
808 const BinaryOperator *Exp = cast<BinaryOperator>(this);
809
810 // The LHS of a constant expr is always evaluated and needed.
811 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
812 return false;
813
814 llvm::APSInt RHS(Result);
815
816 // The short-circuiting &&/|| operators don't necessarily evaluate their
817 // RHS. Make sure to pass isEvaluated down correctly.
818 if (Exp->isLogicalOp()) {
819 bool RHSEval;
820 if (Exp->getOpcode() == BinaryOperator::LAnd)
821 RHSEval = Result != 0;
822 else {
823 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
824 RHSEval = Result == 0;
825 }
826
827 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
828 isEvaluated & RHSEval))
829 return false;
830 } else {
831 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
832 return false;
833 }
834
835 switch (Exp->getOpcode()) {
836 default:
837 if (Loc) *Loc = getLocStart();
838 return false;
839 case BinaryOperator::Mul:
840 Result *= RHS;
841 break;
842 case BinaryOperator::Div:
843 if (RHS == 0) {
844 if (!isEvaluated) break;
845 if (Loc) *Loc = getLocStart();
846 return false;
847 }
848 Result /= RHS;
849 break;
850 case BinaryOperator::Rem:
851 if (RHS == 0) {
852 if (!isEvaluated) break;
853 if (Loc) *Loc = getLocStart();
854 return false;
855 }
856 Result %= RHS;
857 break;
858 case BinaryOperator::Add: Result += RHS; break;
859 case BinaryOperator::Sub: Result -= RHS; break;
860 case BinaryOperator::Shl:
Chris Lattner3496d522007-09-04 02:45:27 +0000861 Result <<=
862 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000863 break;
864 case BinaryOperator::Shr:
Chris Lattner3496d522007-09-04 02:45:27 +0000865 Result >>=
866 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000867 break;
868 case BinaryOperator::LT: Result = Result < RHS; break;
869 case BinaryOperator::GT: Result = Result > RHS; break;
870 case BinaryOperator::LE: Result = Result <= RHS; break;
871 case BinaryOperator::GE: Result = Result >= RHS; break;
872 case BinaryOperator::EQ: Result = Result == RHS; break;
873 case BinaryOperator::NE: Result = Result != RHS; break;
874 case BinaryOperator::And: Result &= RHS; break;
875 case BinaryOperator::Xor: Result ^= RHS; break;
876 case BinaryOperator::Or: Result |= RHS; break;
877 case BinaryOperator::LAnd:
878 Result = Result != 0 && RHS != 0;
879 break;
880 case BinaryOperator::LOr:
881 Result = Result != 0 || RHS != 0;
882 break;
883
884 case BinaryOperator::Comma:
885 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
886 // *except* when they are contained within a subexpression that is not
887 // evaluated". Note that Assignment can never happen due to constraints
888 // on the LHS subexpr, so we don't need to check it here.
889 if (isEvaluated) {
890 if (Loc) *Loc = getLocStart();
891 return false;
892 }
893
894 // The result of the constant expr is the RHS.
895 Result = RHS;
896 return true;
897 }
898
899 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
900 break;
901 }
902 case ImplicitCastExprClass:
903 case CastExprClass: {
904 const Expr *SubExpr;
905 SourceLocation CastLoc;
906 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
907 SubExpr = C->getSubExpr();
908 CastLoc = C->getLParenLoc();
909 } else {
910 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
911 CastLoc = getLocStart();
912 }
913
914 // C99 6.6p6: shall only convert arithmetic types to integer types.
915 if (!SubExpr->getType()->isArithmeticType() ||
916 !getType()->isIntegerType()) {
917 if (Loc) *Loc = SubExpr->getLocStart();
918 return false;
919 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000920
Chris Lattner8cd0e932008-03-05 18:54:05 +0000921 uint32_t DestWidth = static_cast<uint32_t>(Ctx.getTypeSize(getType()));
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000922
Chris Lattner4b009652007-07-25 00:24:17 +0000923 // Handle simple integer->integer casts.
924 if (SubExpr->getType()->isIntegerType()) {
925 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
926 return false;
927
928 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner4b009652007-07-25 00:24:17 +0000929 // If the input is signed, do a sign extend, noop, or truncate.
Chris Lattner000c4102008-01-09 18:59:34 +0000930 if (getType()->isBooleanType()) {
931 // Conversion to bool compares against zero.
932 Result = Result != 0;
933 Result.zextOrTrunc(DestWidth);
934 } else if (SubExpr->getType()->isSignedIntegerType())
Chris Lattner4b009652007-07-25 00:24:17 +0000935 Result.sextOrTrunc(DestWidth);
936 else // If the input is unsigned, do a zero extend, noop, or truncate.
937 Result.zextOrTrunc(DestWidth);
938 break;
939 }
940
941 // Allow floating constants that are the immediate operands of casts or that
942 // are parenthesized.
943 const Expr *Operand = SubExpr;
944 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
945 Operand = PE->getSubExpr();
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000946
947 // If this isn't a floating literal, we can't handle it.
948 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
949 if (!FL) {
950 if (Loc) *Loc = Operand->getLocStart();
951 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000952 }
Chris Lattner000c4102008-01-09 18:59:34 +0000953
954 // If the destination is boolean, compare against zero.
955 if (getType()->isBooleanType()) {
956 Result = !FL->getValue().isZero();
957 Result.zextOrTrunc(DestWidth);
958 break;
959 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000960
961 // Determine whether we are converting to unsigned or signed.
962 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattner9d020b32007-09-26 00:47:26 +0000963
964 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
965 // be called multiple times per AST.
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000966 uint64_t Space[4];
Chris Lattner9d020b32007-09-26 00:47:26 +0000967 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
968 llvm::APFloat::rmTowardZero);
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000969 Result = llvm::APInt(DestWidth, 4, Space);
970 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000971 }
972 case ConditionalOperatorClass: {
973 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
974
975 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
976 return false;
977
978 const Expr *TrueExp = Exp->getLHS();
979 const Expr *FalseExp = Exp->getRHS();
980 if (Result == 0) std::swap(TrueExp, FalseExp);
981
982 // Evaluate the false one first, discard the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +0000983 if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Chris Lattner4b009652007-07-25 00:24:17 +0000984 return false;
985 // Evalute the true one, capture the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +0000986 if (TrueExp &&
987 !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000988 return false;
989 break;
990 }
Chris Lattner3e254fb2008-04-08 04:40:51 +0000991 case CXXDefaultArgExprClass:
992 return cast<CXXDefaultArgExpr>(this)
993 ->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
Chris Lattner4b009652007-07-25 00:24:17 +0000994 }
995
996 // Cases that are valid constant exprs fall through to here.
997 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
998 return true;
999}
1000
Chris Lattner4b009652007-07-25 00:24:17 +00001001/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1002/// integer constant expression with the value zero, or if this is one that is
1003/// cast to void*.
1004bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
Steve Naroffa2e53222008-01-14 16:10:57 +00001005 // Strip off a cast to void*, if it exists.
1006 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
1007 // Check that it is a cast to void*.
Eli Friedmand899dbe2008-02-13 17:29:58 +00001008 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001009 QualType Pointee = PT->getPointeeType();
Chris Lattner35fef522008-02-20 20:55:12 +00001010 if (Pointee.getCVRQualifiers() == 0 &&
1011 Pointee->isVoidType() && // to void*
Steve Naroffa2e53222008-01-14 16:10:57 +00001012 CE->getSubExpr()->getType()->isIntegerType()) // from int.
1013 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +00001014 }
Steve Naroffa2e53222008-01-14 16:10:57 +00001015 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1016 // Ignore the ImplicitCastExpr type entirely.
1017 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
1018 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1019 // Accept ((void*)0) as a null pointer constant, as many other
1020 // implementations do.
1021 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner97316c02008-04-10 02:22:51 +00001022 } else if (const CXXDefaultArgExpr *DefaultArg
1023 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner3e254fb2008-04-08 04:40:51 +00001024 // See through default argument expressions
1025 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Steve Narofff33a9852008-01-14 02:53:34 +00001026 }
Steve Naroffa2e53222008-01-14 16:10:57 +00001027
1028 // This expression must be an integer type.
1029 if (!getType()->isIntegerType())
1030 return false;
1031
Chris Lattner4b009652007-07-25 00:24:17 +00001032 // If we have an integer constant expression, we need to *evaluate* it and
1033 // test for the value 0.
1034 llvm::APSInt Val(32);
Steve Naroffa2e53222008-01-14 16:10:57 +00001035 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001036}
Steve Naroffc11705f2007-07-28 23:10:27 +00001037
Nate Begemanaf6ed502008-04-18 23:10:10 +00001038unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begemanc8e51f82008-05-09 06:41:27 +00001039 if (const VectorType *VT = getType()->getAsVectorType())
1040 return VT->getNumElements();
1041 return 1;
Chris Lattner50547852007-08-03 16:00:20 +00001042}
1043
Nate Begemanc8e51f82008-05-09 06:41:27 +00001044/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001045bool ExtVectorElementExpr::containsDuplicateElements() const {
Steve Naroffba67f692007-07-30 03:29:09 +00001046 const char *compStr = Accessor.getName();
1047 unsigned length = strlen(compStr);
1048
1049 for (unsigned i = 0; i < length-1; i++) {
1050 const char *s = compStr+i;
1051 for (const char c = *s++; *s; s++)
1052 if (c == *s)
1053 return true;
1054 }
1055 return false;
1056}
Chris Lattner42158e72007-08-02 23:36:59 +00001057
Nate Begemanc8e51f82008-05-09 06:41:27 +00001058/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemana1ae7442008-05-13 21:03:02 +00001059void ExtVectorElementExpr::getEncodedElementAccess(
1060 llvm::SmallVectorImpl<unsigned> &Elts) const {
Chris Lattner42158e72007-08-02 23:36:59 +00001061 const char *compStr = Accessor.getName();
Nate Begemanc8e51f82008-05-09 06:41:27 +00001062
1063 bool isHi = !strcmp(compStr, "hi");
1064 bool isLo = !strcmp(compStr, "lo");
1065 bool isEven = !strcmp(compStr, "e");
1066 bool isOdd = !strcmp(compStr, "o");
1067
1068 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1069 uint64_t Index;
1070
1071 if (isHi)
1072 Index = e + i;
1073 else if (isLo)
1074 Index = i;
1075 else if (isEven)
1076 Index = 2 * i;
1077 else if (isOdd)
1078 Index = 2 * i + 1;
1079 else
1080 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattner42158e72007-08-02 23:36:59 +00001081
Nate Begemana1ae7442008-05-13 21:03:02 +00001082 Elts.push_back(Index);
Chris Lattner42158e72007-08-02 23:36:59 +00001083 }
Nate Begemanc8e51f82008-05-09 06:41:27 +00001084}
1085
1086unsigned
1087ExtVectorElementExpr::getAccessedFieldNo(unsigned Idx,
1088 const llvm::Constant *Elts) {
1089 if (isa<llvm::ConstantAggregateZero>(Elts))
1090 return 0;
1091
1092 return cast<llvm::ConstantInt>(Elts->getOperand(Idx))->getZExtValue();
Chris Lattner42158e72007-08-02 23:36:59 +00001093}
1094
Steve Naroff4ed9d662007-09-27 14:38:14 +00001095// constructor for instance messages.
Steve Naroff6cb1d362007-09-28 22:22:11 +00001096ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001097 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001098 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001099 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001100 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001101 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001102 NumArgs = nargs;
1103 SubExprs = new Expr*[NumArgs+1];
Steve Naroff4ed9d662007-09-27 14:38:14 +00001104 SubExprs[RECEIVER] = receiver;
Steve Naroff9f176d12007-11-15 13:05:42 +00001105 if (NumArgs) {
1106 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001107 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1108 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001109 LBracloc = LBrac;
1110 RBracloc = RBrac;
1111}
1112
Steve Naroff4ed9d662007-09-27 14:38:14 +00001113// constructor for class messages.
1114// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff6cb1d362007-09-28 22:22:11 +00001115ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001116 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001117 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001118 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001119 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001120 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001121 NumArgs = nargs;
1122 SubExprs = new Expr*[NumArgs+1];
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001123 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | 0x1);
Steve Naroff9f176d12007-11-15 13:05:42 +00001124 if (NumArgs) {
1125 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001126 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1127 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001128 LBracloc = LBrac;
1129 RBracloc = RBrac;
1130}
1131
Chris Lattnerf624cd22007-10-25 00:29:32 +00001132bool ChooseExpr::isConditionTrue(ASTContext &C) const {
1133 llvm::APSInt CondVal(32);
1134 bool IsConst = getCond()->isIntegerConstantExpr(CondVal, C);
1135 assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst;
1136 return CondVal != 0;
1137}
1138
Anders Carlsson52774ad2008-01-29 15:56:48 +00001139static int64_t evaluateOffsetOf(ASTContext& C, const Expr *E)
1140{
1141 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
1142 QualType Ty = ME->getBase()->getType();
1143
1144 RecordDecl *RD = Ty->getAsRecordType()->getDecl();
Chris Lattner8cd0e932008-03-05 18:54:05 +00001145 const ASTRecordLayout &RL = C.getASTRecordLayout(RD);
Anders Carlsson52774ad2008-01-29 15:56:48 +00001146 FieldDecl *FD = ME->getMemberDecl();
1147
1148 // FIXME: This is linear time.
1149 unsigned i = 0, e = 0;
1150 for (i = 0, e = RD->getNumMembers(); i != e; i++) {
1151 if (RD->getMember(i) == FD)
1152 break;
1153 }
1154
1155 return RL.getFieldOffset(i) + evaluateOffsetOf(C, ME->getBase());
1156 } else if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
1157 const Expr *Base = ASE->getBase();
1158 llvm::APSInt Idx(32);
1159 bool ICE = ASE->getIdx()->isIntegerConstantExpr(Idx, C);
1160 assert(ICE && "Array index is not a constant integer!");
1161
Chris Lattner8cd0e932008-03-05 18:54:05 +00001162 int64_t size = C.getTypeSize(ASE->getType());
Anders Carlsson52774ad2008-01-29 15:56:48 +00001163 size *= Idx.getSExtValue();
1164
1165 return size + evaluateOffsetOf(C, Base);
1166 } else if (isa<CompoundLiteralExpr>(E))
1167 return 0;
1168
1169 assert(0 && "Unknown offsetof subexpression!");
1170 return 0;
1171}
1172
1173int64_t UnaryOperator::evaluateOffsetOf(ASTContext& C) const
1174{
1175 assert(Opc == OffsetOf && "Unary operator not offsetof!");
1176
Chris Lattner8cd0e932008-03-05 18:54:05 +00001177 unsigned CharSize = C.Target.getCharWidth();
Anders Carlsson52774ad2008-01-29 15:56:48 +00001178 return ::evaluateOffsetOf(C, Val) / CharSize;
1179}
1180
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001181//===----------------------------------------------------------------------===//
1182// Child Iterators for iterating over subexpressions/substatements
1183//===----------------------------------------------------------------------===//
1184
1185// DeclRefExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001186Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1187Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001188
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001189// ObjCIvarRefExpr
Ted Kremenek07af3dd2008-05-02 18:40:22 +00001190Stmt::child_iterator ObjCIvarRefExpr::child_begin() {
1191 return reinterpret_cast<Stmt**>(&Base);
1192}
1193
1194Stmt::child_iterator ObjCIvarRefExpr::child_end() {
1195 return reinterpret_cast<Stmt**>(&Base)+1;
1196}
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001197
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001198// PreDefinedExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001199Stmt::child_iterator PreDefinedExpr::child_begin() { return child_iterator(); }
1200Stmt::child_iterator PreDefinedExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001201
1202// IntegerLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001203Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1204Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001205
1206// CharacterLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001207Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); }
1208Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001209
1210// FloatingLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001211Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1212Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001213
Chris Lattner1de66eb2007-08-26 03:42:43 +00001214// ImaginaryLiteral
1215Stmt::child_iterator ImaginaryLiteral::child_begin() {
1216 return reinterpret_cast<Stmt**>(&Val);
1217}
1218Stmt::child_iterator ImaginaryLiteral::child_end() {
1219 return reinterpret_cast<Stmt**>(&Val)+1;
1220}
1221
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001222// StringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001223Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1224Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001225
1226// ParenExpr
1227Stmt::child_iterator ParenExpr::child_begin() {
1228 return reinterpret_cast<Stmt**>(&Val);
1229}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001230Stmt::child_iterator ParenExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001231 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001232}
1233
1234// UnaryOperator
1235Stmt::child_iterator UnaryOperator::child_begin() {
Ted Kremenek6d7ea522007-12-15 00:39:18 +00001236 return reinterpret_cast<Stmt**>(&Val);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001237}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001238Stmt::child_iterator UnaryOperator::child_end() {
Ted Kremenek6d7ea522007-12-15 00:39:18 +00001239 return reinterpret_cast<Stmt**>(&Val+1);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001240}
1241
1242// SizeOfAlignOfTypeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001243Stmt::child_iterator SizeOfAlignOfTypeExpr::child_begin() {
Ted Kremenekb7cf0952007-12-14 22:52:23 +00001244 // If the type is a VLA type (and not a typedef), the size expression of the
1245 // VLA needs to be treated as an executable expression.
1246 if (VariableArrayType* T = dyn_cast<VariableArrayType>(Ty.getTypePtr()))
1247 return child_iterator(T);
1248 else
1249 return child_iterator();
Ted Kremeneka6478552007-10-18 23:28:49 +00001250}
1251Stmt::child_iterator SizeOfAlignOfTypeExpr::child_end() {
Ted Kremenekb7cf0952007-12-14 22:52:23 +00001252 return child_iterator();
Ted Kremeneka6478552007-10-18 23:28:49 +00001253}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001254
1255// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001256Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001257 return reinterpret_cast<Stmt**>(&SubExprs);
1258}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001259Stmt::child_iterator ArraySubscriptExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001260 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001261}
1262
1263// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001264Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek15ede502007-08-27 21:11:44 +00001265 return reinterpret_cast<Stmt**>(&SubExprs[0]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001266}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001267Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek15ede502007-08-27 21:11:44 +00001268 return reinterpret_cast<Stmt**>(&SubExprs[NumArgs+ARGS_START]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001269}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001270
1271// MemberExpr
1272Stmt::child_iterator MemberExpr::child_begin() {
1273 return reinterpret_cast<Stmt**>(&Base);
1274}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001275Stmt::child_iterator MemberExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001276 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001277}
1278
Nate Begemanaf6ed502008-04-18 23:10:10 +00001279// ExtVectorElementExpr
1280Stmt::child_iterator ExtVectorElementExpr::child_begin() {
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001281 return reinterpret_cast<Stmt**>(&Base);
1282}
Nate Begemanaf6ed502008-04-18 23:10:10 +00001283Stmt::child_iterator ExtVectorElementExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001284 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001285}
1286
1287// CompoundLiteralExpr
1288Stmt::child_iterator CompoundLiteralExpr::child_begin() {
1289 return reinterpret_cast<Stmt**>(&Init);
1290}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001291Stmt::child_iterator CompoundLiteralExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001292 return reinterpret_cast<Stmt**>(&Init)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001293}
1294
1295// ImplicitCastExpr
1296Stmt::child_iterator ImplicitCastExpr::child_begin() {
1297 return reinterpret_cast<Stmt**>(&Op);
1298}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001299Stmt::child_iterator ImplicitCastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001300 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001301}
1302
1303// CastExpr
1304Stmt::child_iterator CastExpr::child_begin() {
1305 return reinterpret_cast<Stmt**>(&Op);
1306}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001307Stmt::child_iterator CastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001308 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001309}
1310
1311// BinaryOperator
1312Stmt::child_iterator BinaryOperator::child_begin() {
1313 return reinterpret_cast<Stmt**>(&SubExprs);
1314}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001315Stmt::child_iterator BinaryOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001316 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001317}
1318
1319// ConditionalOperator
1320Stmt::child_iterator ConditionalOperator::child_begin() {
1321 return reinterpret_cast<Stmt**>(&SubExprs);
1322}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001323Stmt::child_iterator ConditionalOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001324 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001325}
1326
1327// AddrLabelExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001328Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1329Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001330
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001331// StmtExpr
1332Stmt::child_iterator StmtExpr::child_begin() {
1333 return reinterpret_cast<Stmt**>(&SubStmt);
1334}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001335Stmt::child_iterator StmtExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001336 return reinterpret_cast<Stmt**>(&SubStmt)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001337}
1338
1339// TypesCompatibleExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001340Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1341 return child_iterator();
1342}
1343
1344Stmt::child_iterator TypesCompatibleExpr::child_end() {
1345 return child_iterator();
1346}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001347
1348// ChooseExpr
1349Stmt::child_iterator ChooseExpr::child_begin() {
1350 return reinterpret_cast<Stmt**>(&SubExprs);
1351}
1352
1353Stmt::child_iterator ChooseExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001354 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001355}
1356
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001357// OverloadExpr
1358Stmt::child_iterator OverloadExpr::child_begin() {
1359 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1360}
1361Stmt::child_iterator OverloadExpr::child_end() {
Nate Begemanbd881ef2008-01-30 20:50:20 +00001362 return reinterpret_cast<Stmt**>(&SubExprs[NumExprs]);
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001363}
1364
Eli Friedmand0e9d092008-05-14 19:38:39 +00001365// ShuffleVectorExpr
1366Stmt::child_iterator ShuffleVectorExpr::child_begin() {
1367 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1368}
1369Stmt::child_iterator ShuffleVectorExpr::child_end() {
1370 return reinterpret_cast<Stmt**>(&SubExprs[NumExprs]);
1371}
1372
Anders Carlsson36760332007-10-15 20:28:48 +00001373// VAArgExpr
1374Stmt::child_iterator VAArgExpr::child_begin() {
1375 return reinterpret_cast<Stmt**>(&Val);
1376}
1377
1378Stmt::child_iterator VAArgExpr::child_end() {
1379 return reinterpret_cast<Stmt**>(&Val)+1;
1380}
1381
Anders Carlsson762b7c72007-08-31 04:56:16 +00001382// InitListExpr
1383Stmt::child_iterator InitListExpr::child_begin() {
Steve Naroff84f75c22008-05-07 17:35:03 +00001384 return reinterpret_cast<Stmt**>(InitExprs.size() ?
1385 &InitExprs[0] : 0);
Anders Carlsson762b7c72007-08-31 04:56:16 +00001386}
1387Stmt::child_iterator InitListExpr::child_end() {
Steve Naroff84f75c22008-05-07 17:35:03 +00001388 return reinterpret_cast<Stmt**>(InitExprs.size() ?
1389 &InitExprs[0] + InitExprs.size() : 0);
Anders Carlsson762b7c72007-08-31 04:56:16 +00001390}
1391
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001392// ObjCStringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001393Stmt::child_iterator ObjCStringLiteral::child_begin() {
1394 return child_iterator();
1395}
1396Stmt::child_iterator ObjCStringLiteral::child_end() {
1397 return child_iterator();
1398}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001399
1400// ObjCEncodeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001401Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1402Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001403
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001404// ObjCSelectorExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001405Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1406 return child_iterator();
1407}
1408Stmt::child_iterator ObjCSelectorExpr::child_end() {
1409 return child_iterator();
1410}
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001411
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001412// ObjCProtocolExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001413Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1414 return child_iterator();
1415}
1416Stmt::child_iterator ObjCProtocolExpr::child_end() {
1417 return child_iterator();
1418}
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001419
Steve Naroffc39ca262007-09-18 23:55:05 +00001420// ObjCMessageExpr
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001421Stmt::child_iterator ObjCMessageExpr::child_begin() {
1422 return reinterpret_cast<Stmt**>(&SubExprs[ getReceiver() ? 0 : ARGS_START ]);
Steve Naroffc39ca262007-09-18 23:55:05 +00001423}
1424Stmt::child_iterator ObjCMessageExpr::child_end() {
Steve Naroff4ed9d662007-09-27 14:38:14 +00001425 return reinterpret_cast<Stmt**>(&SubExprs[getNumArgs()+ARGS_START]);
Steve Naroffc39ca262007-09-18 23:55:05 +00001426}
1427