blob: e21686cf4ef10d92d50fd0e48bd0866b095ae0f4 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Expr.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/StmtVisitor.h"
Chris Lattner2fd1c652007-10-07 08:58:51 +000017#include "clang/Basic/IdentifierTable.h"
Chris Lattnerd9ffbc92007-11-27 18:22:04 +000018#include "clang/Basic/TargetInfo.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019using namespace clang;
20
21//===----------------------------------------------------------------------===//
22// Primary Expressions.
23//===----------------------------------------------------------------------===//
24
25StringLiteral::StringLiteral(const char *strData, unsigned byteLength,
26 bool Wide, QualType t, SourceLocation firstLoc,
27 SourceLocation lastLoc) :
28 Expr(StringLiteralClass, t) {
29 // OPTIMIZE: could allocate this appended to the StringLiteral.
30 char *AStrData = new char[byteLength];
31 memcpy(AStrData, strData, byteLength);
32 StrData = AStrData;
33 ByteLength = byteLength;
34 IsWide = Wide;
35 firstTokLoc = firstLoc;
36 lastTokLoc = lastLoc;
37}
38
39StringLiteral::~StringLiteral() {
40 delete[] StrData;
41}
42
43bool UnaryOperator::isPostfix(Opcode Op) {
44 switch (Op) {
45 case PostInc:
46 case PostDec:
47 return true;
48 default:
49 return false;
50 }
51}
52
53/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
54/// corresponds to, e.g. "sizeof" or "[pre]++".
55const char *UnaryOperator::getOpcodeStr(Opcode Op) {
56 switch (Op) {
57 default: assert(0 && "Unknown unary operator");
58 case PostInc: return "++";
59 case PostDec: return "--";
60 case PreInc: return "++";
61 case PreDec: return "--";
62 case AddrOf: return "&";
63 case Deref: return "*";
64 case Plus: return "+";
65 case Minus: return "-";
66 case Not: return "~";
67 case LNot: return "!";
68 case Real: return "__real";
69 case Imag: return "__imag";
70 case SizeOf: return "sizeof";
71 case AlignOf: return "alignof";
72 case Extension: return "__extension__";
Chris Lattner0d9bcea2007-08-30 17:45:32 +000073 case OffsetOf: return "__builtin_offsetof";
Chris Lattner4b009652007-07-25 00:24:17 +000074 }
75}
76
77//===----------------------------------------------------------------------===//
78// Postfix Operators.
79//===----------------------------------------------------------------------===//
80
Nate Begeman9f3bfb72008-01-17 17:46:27 +000081
Chris Lattner4b009652007-07-25 00:24:17 +000082CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
83 SourceLocation rparenloc)
Ted Kremeneke4acb9c2007-08-24 18:13:47 +000084 : Expr(CallExprClass, t), NumArgs(numargs) {
85 SubExprs = new Expr*[numargs+1];
86 SubExprs[FN] = fn;
Chris Lattner4b009652007-07-25 00:24:17 +000087 for (unsigned i = 0; i != numargs; ++i)
Ted Kremeneke4acb9c2007-08-24 18:13:47 +000088 SubExprs[i+ARGS_START] = args[i];
Chris Lattner4b009652007-07-25 00:24:17 +000089 RParenLoc = rparenloc;
90}
91
Chris Lattnerc257c0d2007-12-28 05:25:02 +000092/// setNumArgs - This changes the number of arguments present in this call.
93/// Any orphaned expressions are deleted by this, and any new operands are set
94/// to null.
95void CallExpr::setNumArgs(unsigned NumArgs) {
96 // No change, just return.
97 if (NumArgs == getNumArgs()) return;
98
99 // If shrinking # arguments, just delete the extras and forgot them.
100 if (NumArgs < getNumArgs()) {
101 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
102 delete getArg(i);
103 this->NumArgs = NumArgs;
104 return;
105 }
106
107 // Otherwise, we are growing the # arguments. New an bigger argument array.
108 Expr **NewSubExprs = new Expr*[NumArgs+1];
109 // Copy over args.
110 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
111 NewSubExprs[i] = SubExprs[i];
112 // Null out new args.
113 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
114 NewSubExprs[i] = 0;
115
116 delete[] SubExprs;
117 SubExprs = NewSubExprs;
118 this->NumArgs = NumArgs;
119}
120
Steve Naroff44aec4c2008-01-31 01:07:12 +0000121bool CallExpr::isBuiltinConstantExpr() const {
122 // All simple function calls (e.g. func()) are implicitly cast to pointer to
123 // function. As a result, we try and obtain the DeclRefExpr from the
124 // ImplicitCastExpr.
125 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
126 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
127 return false;
128
129 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
130 if (!DRE)
131 return false;
132
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000133 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
134 if (!FDecl)
135 return false;
136
137 unsigned builtinID = FDecl->getIdentifier()->getBuiltinID();
138 if (!builtinID)
139 return false;
140
141 // We have a builtin that is a constant expression
Eli Friedman8845a812008-05-16 13:28:37 +0000142 return builtinID == Builtin::BI__builtin___CFStringMakeConstantString ||
143 builtinID == Builtin::BI__builtin_classify_type;
Steve Naroff44aec4c2008-01-31 01:07:12 +0000144}
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000145
Steve Naroff8d3b1702007-08-08 22:15:55 +0000146bool CallExpr::isBuiltinClassifyType(llvm::APSInt &Result) const {
147 // The following enum mimics gcc's internal "typeclass.h" file.
148 enum gcc_type_class {
149 no_type_class = -1,
150 void_type_class, integer_type_class, char_type_class,
151 enumeral_type_class, boolean_type_class,
152 pointer_type_class, reference_type_class, offset_type_class,
153 real_type_class, complex_type_class,
154 function_type_class, method_type_class,
155 record_type_class, union_type_class,
156 array_type_class, string_type_class,
157 lang_type_class
158 };
159 Result.setIsSigned(true);
160
161 // All simple function calls (e.g. func()) are implicitly cast to pointer to
162 // function. As a result, we try and obtain the DeclRefExpr from the
163 // ImplicitCastExpr.
164 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
165 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
166 return false;
167 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
168 if (!DRE)
169 return false;
170
171 // We have a DeclRefExpr.
172 if (strcmp(DRE->getDecl()->getName(), "__builtin_classify_type") == 0) {
173 // If no argument was supplied, default to "no_type_class". This isn't
174 // ideal, however it's what gcc does.
175 Result = static_cast<uint64_t>(no_type_class);
176 if (NumArgs >= 1) {
177 QualType argType = getArg(0)->getType();
178
179 if (argType->isVoidType())
180 Result = void_type_class;
181 else if (argType->isEnumeralType())
182 Result = enumeral_type_class;
183 else if (argType->isBooleanType())
184 Result = boolean_type_class;
185 else if (argType->isCharType())
186 Result = string_type_class; // gcc doesn't appear to use char_type_class
187 else if (argType->isIntegerType())
188 Result = integer_type_class;
189 else if (argType->isPointerType())
190 Result = pointer_type_class;
191 else if (argType->isReferenceType())
192 Result = reference_type_class;
193 else if (argType->isRealType())
194 Result = real_type_class;
195 else if (argType->isComplexType())
196 Result = complex_type_class;
197 else if (argType->isFunctionType())
198 Result = function_type_class;
199 else if (argType->isStructureType())
200 Result = record_type_class;
201 else if (argType->isUnionType())
202 Result = union_type_class;
203 else if (argType->isArrayType())
204 Result = array_type_class;
205 else if (argType->isUnionType())
206 Result = union_type_class;
207 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
Chris Lattner19b8f1a2007-11-08 17:56:40 +0000208 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
Steve Naroff8d3b1702007-08-08 22:15:55 +0000209 }
210 return true;
211 }
212 return false;
213}
214
Chris Lattner4b009652007-07-25 00:24:17 +0000215/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
216/// corresponds to, e.g. "<<=".
217const char *BinaryOperator::getOpcodeStr(Opcode Op) {
218 switch (Op) {
219 default: assert(0 && "Unknown binary operator");
220 case Mul: return "*";
221 case Div: return "/";
222 case Rem: return "%";
223 case Add: return "+";
224 case Sub: return "-";
225 case Shl: return "<<";
226 case Shr: return ">>";
227 case LT: return "<";
228 case GT: return ">";
229 case LE: return "<=";
230 case GE: return ">=";
231 case EQ: return "==";
232 case NE: return "!=";
233 case And: return "&";
234 case Xor: return "^";
235 case Or: return "|";
236 case LAnd: return "&&";
237 case LOr: return "||";
238 case Assign: return "=";
239 case MulAssign: return "*=";
240 case DivAssign: return "/=";
241 case RemAssign: return "%=";
242 case AddAssign: return "+=";
243 case SubAssign: return "-=";
244 case ShlAssign: return "<<=";
245 case ShrAssign: return ">>=";
246 case AndAssign: return "&=";
247 case XorAssign: return "^=";
248 case OrAssign: return "|=";
249 case Comma: return ",";
250 }
251}
252
Anders Carlsson762b7c72007-08-31 04:56:16 +0000253InitListExpr::InitListExpr(SourceLocation lbraceloc,
254 Expr **initexprs, unsigned numinits,
255 SourceLocation rbraceloc)
Steve Naroff2e335472008-05-01 02:04:18 +0000256 : Expr(InitListExprClass, QualType()),
257 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc)
Anders Carlsson762b7c72007-08-31 04:56:16 +0000258{
Anders Carlsson762b7c72007-08-31 04:56:16 +0000259 for (unsigned i = 0; i != numinits; i++)
Steve Naroff2e335472008-05-01 02:04:18 +0000260 InitExprs.push_back(initexprs[i]);
Anders Carlsson762b7c72007-08-31 04:56:16 +0000261}
Chris Lattner4b009652007-07-25 00:24:17 +0000262
263//===----------------------------------------------------------------------===//
264// Generic Expression Routines
265//===----------------------------------------------------------------------===//
266
267/// hasLocalSideEffect - Return true if this immediate expression has side
268/// effects, not counting any sub-expressions.
269bool Expr::hasLocalSideEffect() const {
270 switch (getStmtClass()) {
271 default:
272 return false;
273 case ParenExprClass:
274 return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect();
275 case UnaryOperatorClass: {
276 const UnaryOperator *UO = cast<UnaryOperator>(this);
277
278 switch (UO->getOpcode()) {
279 default: return false;
280 case UnaryOperator::PostInc:
281 case UnaryOperator::PostDec:
282 case UnaryOperator::PreInc:
283 case UnaryOperator::PreDec:
284 return true; // ++/--
285
286 case UnaryOperator::Deref:
287 // Dereferencing a volatile pointer is a side-effect.
288 return getType().isVolatileQualified();
289 case UnaryOperator::Real:
290 case UnaryOperator::Imag:
291 // accessing a piece of a volatile complex is a side-effect.
292 return UO->getSubExpr()->getType().isVolatileQualified();
293
294 case UnaryOperator::Extension:
295 return UO->getSubExpr()->hasLocalSideEffect();
296 }
297 }
Chris Lattneref95ffd2007-12-01 06:07:34 +0000298 case BinaryOperatorClass: {
299 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
300 // Consider comma to have side effects if the LHS and RHS both do.
301 if (BinOp->getOpcode() == BinaryOperator::Comma)
302 return BinOp->getLHS()->hasLocalSideEffect() &&
303 BinOp->getRHS()->hasLocalSideEffect();
304
305 return BinOp->isAssignmentOp();
306 }
Chris Lattner06078d22007-08-25 02:00:02 +0000307 case CompoundAssignOperatorClass:
Chris Lattner9c0da3b2007-08-25 01:55:00 +0000308 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000309
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000310 case ConditionalOperatorClass: {
311 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
312 return Exp->getCond()->hasLocalSideEffect()
313 || (Exp->getLHS() && Exp->getLHS()->hasLocalSideEffect())
314 || (Exp->getRHS() && Exp->getRHS()->hasLocalSideEffect());
315 }
316
Chris Lattner4b009652007-07-25 00:24:17 +0000317 case MemberExprClass:
318 case ArraySubscriptExprClass:
319 // If the base pointer or element is to a volatile pointer/field, accessing
320 // if is a side effect.
321 return getType().isVolatileQualified();
322
323 case CallExprClass:
324 // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }"
325 // should warn.
326 return true;
Chris Lattner99f5f0b2007-09-26 22:06:30 +0000327 case ObjCMessageExprClass:
328 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000329
330 case CastExprClass:
331 // If this is a cast to void, check the operand. Otherwise, the result of
332 // the cast is unused.
333 if (getType()->isVoidType())
334 return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect();
335 return false;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000336
Eli Friedmanb924c7f2008-05-19 21:24:43 +0000337 case ImplicitCastExprClass:
338 // Check the operand, since implicit casts are inserted by Sema
339 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasLocalSideEffect();
340
Chris Lattner3e254fb2008-04-08 04:40:51 +0000341 case CXXDefaultArgExprClass:
342 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasLocalSideEffect();
Chris Lattner4b009652007-07-25 00:24:17 +0000343 }
344}
345
346/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
347/// incomplete type other than void. Nonarray expressions that can be lvalues:
348/// - name, where name must be a variable
349/// - e[i]
350/// - (e), where e must be an lvalue
351/// - e.name, where e must be an lvalue
352/// - e->name
353/// - *e, the type of e cannot be a function type
354/// - string-constant
Chris Lattner5bf72022007-10-30 22:53:42 +0000355/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Chris Lattner4b009652007-07-25 00:24:17 +0000356/// - reference type [C++ [expr]]
357///
358Expr::isLvalueResult Expr::isLvalue() const {
359 // first, check the type (C99 6.3.2.1)
360 if (TR->isFunctionType()) // from isObjectType()
361 return LV_NotObjectType;
362
Steve Naroffec7736d2008-02-10 01:39:04 +0000363 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner35fef522008-02-20 20:55:12 +0000364 if (TR->isVoidType() && !TR.getCanonicalType().getCVRQualifiers())
Steve Naroffec7736d2008-02-10 01:39:04 +0000365 return LV_IncompleteVoidType;
366
Chris Lattner4b009652007-07-25 00:24:17 +0000367 if (TR->isReferenceType()) // C++ [expr]
368 return LV_Valid;
369
370 // the type looks fine, now check the expression
371 switch (getStmtClass()) {
372 case StringLiteralClass: // C99 6.5.1p4
Anders Carlsson9e933a22007-11-30 22:47:59 +0000373 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000374 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
375 // For vectors, make sure base is an lvalue (i.e. not a function call).
376 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
377 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue();
378 return LV_Valid;
379 case DeclRefExprClass: // C99 6.5.1p2
380 if (isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl()))
381 return LV_Valid;
382 break;
383 case MemberExprClass: { // C99 6.5.2.3p4
384 const MemberExpr *m = cast<MemberExpr>(this);
385 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue();
386 }
Chris Lattner5bf72022007-10-30 22:53:42 +0000387 case UnaryOperatorClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000388 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner5bf72022007-10-30 22:53:42 +0000389 return LV_Valid; // C99 6.5.3p4
390
391 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
392 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag)
393 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(); // GNU.
Chris Lattner4b009652007-07-25 00:24:17 +0000394 break;
395 case ParenExprClass: // C99 6.5.1p5
396 return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
Steve Naroffc7c66532007-12-05 04:00:10 +0000397 case CompoundLiteralExprClass: // C99 6.5.2.5p5
398 return LV_Valid;
Nate Begemanaf6ed502008-04-18 23:10:10 +0000399 case ExtVectorElementExprClass:
400 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroffba67f692007-07-30 03:29:09 +0000401 return LV_DuplicateVectorComponents;
402 return LV_Valid;
Steve Naroff46f18f22007-11-12 14:34:27 +0000403 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
404 return LV_Valid;
Chris Lattner7e637512008-01-12 08:14:25 +0000405 case PreDefinedExprClass:
406 return LV_Valid;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000407 case CXXDefaultArgExprClass:
408 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue();
Chris Lattner4b009652007-07-25 00:24:17 +0000409 default:
410 break;
411 }
412 return LV_InvalidExpression;
413}
414
415/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
416/// does not have an incomplete type, does not have a const-qualified type, and
417/// if it is a structure or union, does not have any member (including,
418/// recursively, any member or element of all contained aggregates or unions)
419/// with a const-qualified type.
420Expr::isModifiableLvalueResult Expr::isModifiableLvalue() const {
421 isLvalueResult lvalResult = isLvalue();
422
423 switch (lvalResult) {
424 case LV_Valid: break;
425 case LV_NotObjectType: return MLV_NotObjectType;
426 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroffba67f692007-07-30 03:29:09 +0000427 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner4b009652007-07-25 00:24:17 +0000428 case LV_InvalidExpression: return MLV_InvalidExpression;
429 }
430 if (TR.isConstQualified())
431 return MLV_ConstQualified;
432 if (TR->isArrayType())
433 return MLV_ArrayType;
434 if (TR->isIncompleteType())
435 return MLV_IncompleteType;
436
437 if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) {
438 if (r->hasConstFields())
439 return MLV_ConstQualified;
440 }
441 return MLV_Valid;
442}
443
Ted Kremenek5778d622008-02-27 18:39:48 +0000444/// hasGlobalStorage - Return true if this expression has static storage
Chris Lattner743ec372007-11-27 21:35:27 +0000445/// duration. This means that the address of this expression is a link-time
446/// constant.
Ted Kremenek5778d622008-02-27 18:39:48 +0000447bool Expr::hasGlobalStorage() const {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000448 switch (getStmtClass()) {
449 default:
450 return false;
Chris Lattner743ec372007-11-27 21:35:27 +0000451 case ParenExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000452 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
Chris Lattner743ec372007-11-27 21:35:27 +0000453 case ImplicitCastExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000454 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
Steve Naroffbe37fc02008-01-14 18:19:28 +0000455 case CompoundLiteralExprClass:
456 return cast<CompoundLiteralExpr>(this)->isFileScope();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000457 case DeclRefExprClass: {
458 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
459 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Ted Kremenek5778d622008-02-27 18:39:48 +0000460 return VD->hasGlobalStorage();
Seo Sanghyeone330ae72008-04-04 09:45:30 +0000461 if (isa<FunctionDecl>(D))
462 return true;
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000463 return false;
464 }
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000465 case MemberExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000466 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek5778d622008-02-27 18:39:48 +0000467 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000468 }
Chris Lattner743ec372007-11-27 21:35:27 +0000469 case ArraySubscriptExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000470 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattner7e637512008-01-12 08:14:25 +0000471 case PreDefinedExprClass:
472 return true;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000473 case CXXDefaultArgExprClass:
474 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000475 }
476}
477
Ted Kremenek87e30c52008-01-17 16:57:34 +0000478Expr* Expr::IgnoreParens() {
479 Expr* E = this;
480 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
481 E = P->getSubExpr();
482
483 return E;
484}
485
Chris Lattner7a48d9c2008-02-13 01:02:39 +0000486/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
487/// or CastExprs or ImplicitCastExprs, returning their operand.
488Expr *Expr::IgnoreParenCasts() {
489 Expr *E = this;
490 while (true) {
491 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
492 E = P->getSubExpr();
493 else if (CastExpr *P = dyn_cast<CastExpr>(E))
494 E = P->getSubExpr();
495 else if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E))
496 E = P->getSubExpr();
497 else
498 return E;
499 }
500}
501
502
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000503bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000504 switch (getStmtClass()) {
505 default:
506 if (Loc) *Loc = getLocStart();
507 return false;
508 case ParenExprClass:
509 return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(Ctx, Loc);
510 case StringLiteralClass:
Steve Naroff4fdea132007-11-09 15:00:03 +0000511 case ObjCStringLiteralClass:
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000512 case FloatingLiteralClass:
513 case IntegerLiteralClass:
514 case CharacterLiteralClass:
515 case ImaginaryLiteralClass:
Anders Carlsson855d78d2007-10-17 00:52:43 +0000516 case TypesCompatibleExprClass:
517 case CXXBoolLiteralExprClass:
Chris Lattner06db6132007-10-18 00:20:32 +0000518 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000519 case CallExprClass: {
520 const CallExpr *CE = cast<CallExpr>(this);
Steve Naroff44aec4c2008-01-31 01:07:12 +0000521 if (CE->isBuiltinConstantExpr())
522 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000523 if (Loc) *Loc = getLocStart();
524 return false;
525 }
Chris Lattner42e86b62007-11-01 02:45:17 +0000526 case DeclRefExprClass: {
527 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
528 // Accept address of function.
529 if (isa<EnumConstantDecl>(D) || isa<FunctionDecl>(D))
Chris Lattner06db6132007-10-18 00:20:32 +0000530 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000531 if (Loc) *Loc = getLocStart();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000532 if (isa<VarDecl>(D))
533 return TR->isArrayType();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000534 return false;
Chris Lattner42e86b62007-11-01 02:45:17 +0000535 }
Steve Narofff91f9722008-01-09 00:05:37 +0000536 case CompoundLiteralExprClass:
537 if (Loc) *Loc = getLocStart();
538 // Allow "(int []){2,4}", since the array will be converted to a pointer.
Nate Begemanc4e28e42008-01-25 05:34:48 +0000539 // Allow "(vector type){2,4}" since the elements are all constant.
540 return TR->isArrayType() || TR->isVectorType();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000541 case UnaryOperatorClass: {
542 const UnaryOperator *Exp = cast<UnaryOperator>(this);
543
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000544 // C99 6.6p9
Chris Lattner35b662f2007-12-11 23:11:17 +0000545 if (Exp->getOpcode() == UnaryOperator::AddrOf) {
Ted Kremenek5778d622008-02-27 18:39:48 +0000546 if (!Exp->getSubExpr()->hasGlobalStorage()) {
Chris Lattner35b662f2007-12-11 23:11:17 +0000547 if (Loc) *Loc = getLocStart();
548 return false;
549 }
550 return true;
551 }
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000552
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000553 // Get the operand value. If this is sizeof/alignof, do not evalute the
554 // operand. This affects C99 6.6p3.
Steve Narofff0b23542008-01-10 22:15:12 +0000555 if (!Exp->isSizeOfAlignOfOp() &&
556 Exp->getOpcode() != UnaryOperator::OffsetOf &&
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000557 !Exp->getSubExpr()->isConstantExpr(Ctx, Loc))
558 return false;
559
560 switch (Exp->getOpcode()) {
561 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
562 // See C99 6.6p3.
563 default:
564 if (Loc) *Loc = Exp->getOperatorLoc();
565 return false;
566 case UnaryOperator::Extension:
567 return true; // FIXME: this is wrong.
568 case UnaryOperator::SizeOf:
569 case UnaryOperator::AlignOf:
Steve Narofff0b23542008-01-10 22:15:12 +0000570 case UnaryOperator::OffsetOf:
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000571 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Eli Friedman62f67fd2008-02-15 12:20:59 +0000572 if (!Exp->getSubExpr()->getType()->isConstantSizeType()) {
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000573 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000574 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000575 }
Chris Lattner06db6132007-10-18 00:20:32 +0000576 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000577 case UnaryOperator::LNot:
578 case UnaryOperator::Plus:
579 case UnaryOperator::Minus:
580 case UnaryOperator::Not:
Chris Lattner06db6132007-10-18 00:20:32 +0000581 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000582 }
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000583 }
584 case SizeOfAlignOfTypeExprClass: {
585 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
586 // alignof always evaluates to a constant.
Chris Lattner20515462008-02-21 05:45:29 +0000587 if (Exp->isSizeOf() && !Exp->getArgumentType()->isVoidType() &&
588 !Exp->getArgumentType()->isConstantSizeType()) {
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000589 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000590 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000591 }
Chris Lattner06db6132007-10-18 00:20:32 +0000592 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000593 }
594 case BinaryOperatorClass: {
595 const BinaryOperator *Exp = cast<BinaryOperator>(this);
596
597 // The LHS of a constant expr is always evaluated and needed.
598 if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
599 return false;
600
601 if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
602 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000603 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000604 }
605 case ImplicitCastExprClass:
606 case CastExprClass: {
607 const Expr *SubExpr;
608 SourceLocation CastLoc;
609 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
610 SubExpr = C->getSubExpr();
611 CastLoc = C->getLParenLoc();
612 } else {
613 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
614 CastLoc = getLocStart();
615 }
616 if (!SubExpr->isConstantExpr(Ctx, Loc)) {
617 if (Loc) *Loc = SubExpr->getLocStart();
618 return false;
619 }
Chris Lattner06db6132007-10-18 00:20:32 +0000620 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000621 }
622 case ConditionalOperatorClass: {
623 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Chris Lattner06db6132007-10-18 00:20:32 +0000624 if (!Exp->getCond()->isConstantExpr(Ctx, Loc) ||
Anders Carlsson37365fc2007-11-30 19:04:31 +0000625 // Handle the GNU extension for missing LHS.
626 !(Exp->getLHS() && Exp->getLHS()->isConstantExpr(Ctx, Loc)) ||
Chris Lattner06db6132007-10-18 00:20:32 +0000627 !Exp->getRHS()->isConstantExpr(Ctx, Loc))
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000628 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000629 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000630 }
Steve Narofff0b23542008-01-10 22:15:12 +0000631 case InitListExprClass: {
632 const InitListExpr *Exp = cast<InitListExpr>(this);
633 unsigned numInits = Exp->getNumInits();
634 for (unsigned i = 0; i < numInits; i++) {
635 if (!Exp->getInit(i)->isConstantExpr(Ctx, Loc)) {
636 if (Loc) *Loc = Exp->getInit(i)->getLocStart();
637 return false;
638 }
639 }
640 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000641 }
Chris Lattner3e254fb2008-04-08 04:40:51 +0000642 case CXXDefaultArgExprClass:
643 return cast<CXXDefaultArgExpr>(this)->getExpr()->isConstantExpr(Ctx, Loc);
Steve Narofff0b23542008-01-10 22:15:12 +0000644 }
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000645}
646
Chris Lattner4b009652007-07-25 00:24:17 +0000647/// isIntegerConstantExpr - this recursive routine will test if an expression is
648/// an integer constant expression. Note: With the introduction of VLA's in
649/// C99 the result of the sizeof operator is no longer always a constant
650/// expression. The generalization of the wording to include any subexpression
651/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
652/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
653/// "0 || f()" can be treated as a constant expression. In C90 this expression,
654/// occurring in a context requiring a constant, would have been a constraint
655/// violation. FIXME: This routine currently implements C90 semantics.
656/// To properly implement C99 semantics this routine will need to evaluate
657/// expressions involving operators previously mentioned.
658
659/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
660/// comma, etc
661///
662/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattner9d020b32007-09-26 00:47:26 +0000663/// permit this. This includes things like (int)1e1000
Chris Lattner4b009652007-07-25 00:24:17 +0000664///
665/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
666/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
667/// cast+dereference.
668bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
669 SourceLocation *Loc, bool isEvaluated) const {
670 switch (getStmtClass()) {
671 default:
672 if (Loc) *Loc = getLocStart();
673 return false;
674 case ParenExprClass:
675 return cast<ParenExpr>(this)->getSubExpr()->
676 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
677 case IntegerLiteralClass:
678 Result = cast<IntegerLiteral>(this)->getValue();
679 break;
680 case CharacterLiteralClass: {
681 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Chris Lattner8cd0e932008-03-05 18:54:05 +0000682 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattner4b009652007-07-25 00:24:17 +0000683 Result = CL->getValue();
684 Result.setIsUnsigned(!getType()->isSignedIntegerType());
685 break;
686 }
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000687 case TypesCompatibleExprClass: {
688 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Chris Lattner8cd0e932008-03-05 18:54:05 +0000689 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Steve Naroff85f0dc52007-10-15 20:41:53 +0000690 Result = Ctx.typesAreCompatible(TCE->getArgType1(), TCE->getArgType2());
Steve Naroff1200b5a2007-08-02 00:13:27 +0000691 break;
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000692 }
Steve Naroff8d3b1702007-08-08 22:15:55 +0000693 case CallExprClass: {
694 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner8cd0e932008-03-05 18:54:05 +0000695 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Steve Naroff8d3b1702007-08-08 22:15:55 +0000696 if (CE->isBuiltinClassifyType(Result))
697 break;
698 if (Loc) *Loc = getLocStart();
699 return false;
700 }
Chris Lattner4b009652007-07-25 00:24:17 +0000701 case DeclRefExprClass:
702 if (const EnumConstantDecl *D =
703 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
704 Result = D->getInitVal();
705 break;
706 }
707 if (Loc) *Loc = getLocStart();
708 return false;
709 case UnaryOperatorClass: {
710 const UnaryOperator *Exp = cast<UnaryOperator>(this);
711
712 // Get the operand value. If this is sizeof/alignof, do not evalute the
713 // operand. This affects C99 6.6p3.
Anders Carlsson52774ad2008-01-29 15:56:48 +0000714 if (!Exp->isSizeOfAlignOfOp() && !Exp->isOffsetOfOp() &&
Chris Lattner5a9b6242007-08-23 21:42:50 +0000715 !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000716 return false;
717
718 switch (Exp->getOpcode()) {
719 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
720 // See C99 6.6p3.
721 default:
722 if (Loc) *Loc = Exp->getOperatorLoc();
723 return false;
724 case UnaryOperator::Extension:
725 return true; // FIXME: this is wrong.
726 case UnaryOperator::SizeOf:
727 case UnaryOperator::AlignOf:
Chris Lattner20515462008-02-21 05:45:29 +0000728 // Return the result in the right width.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000729 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattner20515462008-02-21 05:45:29 +0000730
731 // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
732 if (Exp->getSubExpr()->getType()->isVoidType()) {
733 Result = 1;
734 break;
735 }
736
Chris Lattner4b009652007-07-25 00:24:17 +0000737 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Eli Friedman62f67fd2008-02-15 12:20:59 +0000738 if (!Exp->getSubExpr()->getType()->isConstantSizeType()) {
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000739 if (Loc) *Loc = Exp->getOperatorLoc();
Chris Lattner4b009652007-07-25 00:24:17 +0000740 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000741 }
Chris Lattner4b009652007-07-25 00:24:17 +0000742
Chris Lattner4b009652007-07-25 00:24:17 +0000743 // Get information about the size or align.
Chris Lattnerd4dc2672008-01-02 21:54:09 +0000744 if (Exp->getSubExpr()->getType()->isFunctionType()) {
745 // GCC extension: sizeof(function) = 1.
746 Result = Exp->getOpcode() == UnaryOperator::AlignOf ? 4 : 1;
Chris Lattnerd9ffbc92007-11-27 18:22:04 +0000747 } else {
Chris Lattner8cd0e932008-03-05 18:54:05 +0000748 unsigned CharSize = Ctx.Target.getCharWidth();
Anders Carlsson1f86b032008-02-18 07:10:45 +0000749 if (Exp->getOpcode() == UnaryOperator::AlignOf)
Chris Lattner8cd0e932008-03-05 18:54:05 +0000750 Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType()) / CharSize;
Anders Carlsson1f86b032008-02-18 07:10:45 +0000751 else
Chris Lattner8cd0e932008-03-05 18:54:05 +0000752 Result = Ctx.getTypeSize(Exp->getSubExpr()->getType()) / CharSize;
Chris Lattnerd9ffbc92007-11-27 18:22:04 +0000753 }
Chris Lattner4b009652007-07-25 00:24:17 +0000754 break;
755 case UnaryOperator::LNot: {
Chris Lattnerf00fdc02008-01-25 19:16:19 +0000756 bool Val = Result == 0;
Chris Lattner8cd0e932008-03-05 18:54:05 +0000757 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattner4b009652007-07-25 00:24:17 +0000758 Result = Val;
759 break;
760 }
761 case UnaryOperator::Plus:
762 break;
763 case UnaryOperator::Minus:
764 Result = -Result;
765 break;
766 case UnaryOperator::Not:
767 Result = ~Result;
768 break;
Anders Carlsson52774ad2008-01-29 15:56:48 +0000769 case UnaryOperator::OffsetOf:
770 Result = Exp->evaluateOffsetOf(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000771 }
772 break;
773 }
774 case SizeOfAlignOfTypeExprClass: {
775 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
Chris Lattner20515462008-02-21 05:45:29 +0000776
777 // Return the result in the right width.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000778 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattner20515462008-02-21 05:45:29 +0000779
780 // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
781 if (Exp->getArgumentType()->isVoidType()) {
782 Result = 1;
783 break;
784 }
785
786 // alignof always evaluates to a constant, sizeof does if arg is not VLA.
Eli Friedman62f67fd2008-02-15 12:20:59 +0000787 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType()) {
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000788 if (Loc) *Loc = Exp->getOperatorLoc();
Chris Lattner4b009652007-07-25 00:24:17 +0000789 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000790 }
Chris Lattner4b009652007-07-25 00:24:17 +0000791
Chris Lattner4b009652007-07-25 00:24:17 +0000792 // Get information about the size or align.
Chris Lattnerd4dc2672008-01-02 21:54:09 +0000793 if (Exp->getArgumentType()->isFunctionType()) {
794 // GCC extension: sizeof(function) = 1.
795 Result = Exp->isSizeOf() ? 1 : 4;
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000796 } else {
Chris Lattner8cd0e932008-03-05 18:54:05 +0000797 unsigned CharSize = Ctx.Target.getCharWidth();
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000798 if (Exp->isSizeOf())
Chris Lattner8cd0e932008-03-05 18:54:05 +0000799 Result = Ctx.getTypeSize(Exp->getArgumentType()) / CharSize;
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000800 else
Chris Lattner8cd0e932008-03-05 18:54:05 +0000801 Result = Ctx.getTypeAlign(Exp->getArgumentType()) / CharSize;
Ted Kremeneka9d0fd92007-12-17 17:38:43 +0000802 }
Chris Lattner4b009652007-07-25 00:24:17 +0000803 break;
804 }
805 case BinaryOperatorClass: {
806 const BinaryOperator *Exp = cast<BinaryOperator>(this);
807
808 // The LHS of a constant expr is always evaluated and needed.
809 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
810 return false;
811
812 llvm::APSInt RHS(Result);
813
814 // The short-circuiting &&/|| operators don't necessarily evaluate their
815 // RHS. Make sure to pass isEvaluated down correctly.
816 if (Exp->isLogicalOp()) {
817 bool RHSEval;
818 if (Exp->getOpcode() == BinaryOperator::LAnd)
819 RHSEval = Result != 0;
820 else {
821 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
822 RHSEval = Result == 0;
823 }
824
825 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
826 isEvaluated & RHSEval))
827 return false;
828 } else {
829 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
830 return false;
831 }
832
833 switch (Exp->getOpcode()) {
834 default:
835 if (Loc) *Loc = getLocStart();
836 return false;
837 case BinaryOperator::Mul:
838 Result *= RHS;
839 break;
840 case BinaryOperator::Div:
841 if (RHS == 0) {
842 if (!isEvaluated) break;
843 if (Loc) *Loc = getLocStart();
844 return false;
845 }
846 Result /= RHS;
847 break;
848 case BinaryOperator::Rem:
849 if (RHS == 0) {
850 if (!isEvaluated) break;
851 if (Loc) *Loc = getLocStart();
852 return false;
853 }
854 Result %= RHS;
855 break;
856 case BinaryOperator::Add: Result += RHS; break;
857 case BinaryOperator::Sub: Result -= RHS; break;
858 case BinaryOperator::Shl:
Chris Lattner3496d522007-09-04 02:45:27 +0000859 Result <<=
860 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000861 break;
862 case BinaryOperator::Shr:
Chris Lattner3496d522007-09-04 02:45:27 +0000863 Result >>=
864 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000865 break;
866 case BinaryOperator::LT: Result = Result < RHS; break;
867 case BinaryOperator::GT: Result = Result > RHS; break;
868 case BinaryOperator::LE: Result = Result <= RHS; break;
869 case BinaryOperator::GE: Result = Result >= RHS; break;
870 case BinaryOperator::EQ: Result = Result == RHS; break;
871 case BinaryOperator::NE: Result = Result != RHS; break;
872 case BinaryOperator::And: Result &= RHS; break;
873 case BinaryOperator::Xor: Result ^= RHS; break;
874 case BinaryOperator::Or: Result |= RHS; break;
875 case BinaryOperator::LAnd:
876 Result = Result != 0 && RHS != 0;
877 break;
878 case BinaryOperator::LOr:
879 Result = Result != 0 || RHS != 0;
880 break;
881
882 case BinaryOperator::Comma:
883 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
884 // *except* when they are contained within a subexpression that is not
885 // evaluated". Note that Assignment can never happen due to constraints
886 // on the LHS subexpr, so we don't need to check it here.
887 if (isEvaluated) {
888 if (Loc) *Loc = getLocStart();
889 return false;
890 }
891
892 // The result of the constant expr is the RHS.
893 Result = RHS;
894 return true;
895 }
896
897 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
898 break;
899 }
900 case ImplicitCastExprClass:
901 case CastExprClass: {
902 const Expr *SubExpr;
903 SourceLocation CastLoc;
904 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
905 SubExpr = C->getSubExpr();
906 CastLoc = C->getLParenLoc();
907 } else {
908 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
909 CastLoc = getLocStart();
910 }
911
912 // C99 6.6p6: shall only convert arithmetic types to integer types.
913 if (!SubExpr->getType()->isArithmeticType() ||
914 !getType()->isIntegerType()) {
915 if (Loc) *Loc = SubExpr->getLocStart();
916 return false;
917 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000918
Chris Lattner8cd0e932008-03-05 18:54:05 +0000919 uint32_t DestWidth = static_cast<uint32_t>(Ctx.getTypeSize(getType()));
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000920
Chris Lattner4b009652007-07-25 00:24:17 +0000921 // Handle simple integer->integer casts.
922 if (SubExpr->getType()->isIntegerType()) {
923 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
924 return false;
925
926 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner4b009652007-07-25 00:24:17 +0000927 // If the input is signed, do a sign extend, noop, or truncate.
Chris Lattner000c4102008-01-09 18:59:34 +0000928 if (getType()->isBooleanType()) {
929 // Conversion to bool compares against zero.
930 Result = Result != 0;
931 Result.zextOrTrunc(DestWidth);
932 } else if (SubExpr->getType()->isSignedIntegerType())
Chris Lattner4b009652007-07-25 00:24:17 +0000933 Result.sextOrTrunc(DestWidth);
934 else // If the input is unsigned, do a zero extend, noop, or truncate.
935 Result.zextOrTrunc(DestWidth);
936 break;
937 }
938
939 // Allow floating constants that are the immediate operands of casts or that
940 // are parenthesized.
941 const Expr *Operand = SubExpr;
942 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
943 Operand = PE->getSubExpr();
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000944
945 // If this isn't a floating literal, we can't handle it.
946 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
947 if (!FL) {
948 if (Loc) *Loc = Operand->getLocStart();
949 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000950 }
Chris Lattner000c4102008-01-09 18:59:34 +0000951
952 // If the destination is boolean, compare against zero.
953 if (getType()->isBooleanType()) {
954 Result = !FL->getValue().isZero();
955 Result.zextOrTrunc(DestWidth);
956 break;
957 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000958
959 // Determine whether we are converting to unsigned or signed.
960 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattner9d020b32007-09-26 00:47:26 +0000961
962 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
963 // be called multiple times per AST.
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000964 uint64_t Space[4];
Chris Lattner9d020b32007-09-26 00:47:26 +0000965 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
966 llvm::APFloat::rmTowardZero);
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000967 Result = llvm::APInt(DestWidth, 4, Space);
968 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000969 }
970 case ConditionalOperatorClass: {
971 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
972
973 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
974 return false;
975
976 const Expr *TrueExp = Exp->getLHS();
977 const Expr *FalseExp = Exp->getRHS();
978 if (Result == 0) std::swap(TrueExp, FalseExp);
979
980 // Evaluate the false one first, discard the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +0000981 if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Chris Lattner4b009652007-07-25 00:24:17 +0000982 return false;
983 // Evalute the true one, capture the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +0000984 if (TrueExp &&
985 !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000986 return false;
987 break;
988 }
Chris Lattner3e254fb2008-04-08 04:40:51 +0000989 case CXXDefaultArgExprClass:
990 return cast<CXXDefaultArgExpr>(this)
991 ->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
Chris Lattner4b009652007-07-25 00:24:17 +0000992 }
993
994 // Cases that are valid constant exprs fall through to here.
995 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
996 return true;
997}
998
Chris Lattner4b009652007-07-25 00:24:17 +0000999/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1000/// integer constant expression with the value zero, or if this is one that is
1001/// cast to void*.
1002bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
Steve Naroffa2e53222008-01-14 16:10:57 +00001003 // Strip off a cast to void*, if it exists.
1004 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
1005 // Check that it is a cast to void*.
Eli Friedmand899dbe2008-02-13 17:29:58 +00001006 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001007 QualType Pointee = PT->getPointeeType();
Chris Lattner35fef522008-02-20 20:55:12 +00001008 if (Pointee.getCVRQualifiers() == 0 &&
1009 Pointee->isVoidType() && // to void*
Steve Naroffa2e53222008-01-14 16:10:57 +00001010 CE->getSubExpr()->getType()->isIntegerType()) // from int.
1011 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +00001012 }
Steve Naroffa2e53222008-01-14 16:10:57 +00001013 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1014 // Ignore the ImplicitCastExpr type entirely.
1015 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
1016 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1017 // Accept ((void*)0) as a null pointer constant, as many other
1018 // implementations do.
1019 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner97316c02008-04-10 02:22:51 +00001020 } else if (const CXXDefaultArgExpr *DefaultArg
1021 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner3e254fb2008-04-08 04:40:51 +00001022 // See through default argument expressions
1023 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Steve Narofff33a9852008-01-14 02:53:34 +00001024 }
Steve Naroffa2e53222008-01-14 16:10:57 +00001025
1026 // This expression must be an integer type.
1027 if (!getType()->isIntegerType())
1028 return false;
1029
Chris Lattner4b009652007-07-25 00:24:17 +00001030 // If we have an integer constant expression, we need to *evaluate* it and
1031 // test for the value 0.
1032 llvm::APSInt Val(32);
Steve Naroffa2e53222008-01-14 16:10:57 +00001033 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001034}
Steve Naroffc11705f2007-07-28 23:10:27 +00001035
Nate Begemanaf6ed502008-04-18 23:10:10 +00001036unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begemanc8e51f82008-05-09 06:41:27 +00001037 if (const VectorType *VT = getType()->getAsVectorType())
1038 return VT->getNumElements();
1039 return 1;
Chris Lattner50547852007-08-03 16:00:20 +00001040}
1041
Nate Begemanc8e51f82008-05-09 06:41:27 +00001042/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001043bool ExtVectorElementExpr::containsDuplicateElements() const {
Steve Naroffba67f692007-07-30 03:29:09 +00001044 const char *compStr = Accessor.getName();
1045 unsigned length = strlen(compStr);
1046
1047 for (unsigned i = 0; i < length-1; i++) {
1048 const char *s = compStr+i;
1049 for (const char c = *s++; *s; s++)
1050 if (c == *s)
1051 return true;
1052 }
1053 return false;
1054}
Chris Lattner42158e72007-08-02 23:36:59 +00001055
Nate Begemanc8e51f82008-05-09 06:41:27 +00001056/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemana1ae7442008-05-13 21:03:02 +00001057void ExtVectorElementExpr::getEncodedElementAccess(
1058 llvm::SmallVectorImpl<unsigned> &Elts) const {
Chris Lattner42158e72007-08-02 23:36:59 +00001059 const char *compStr = Accessor.getName();
Nate Begemanc8e51f82008-05-09 06:41:27 +00001060
1061 bool isHi = !strcmp(compStr, "hi");
1062 bool isLo = !strcmp(compStr, "lo");
1063 bool isEven = !strcmp(compStr, "e");
1064 bool isOdd = !strcmp(compStr, "o");
1065
1066 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1067 uint64_t Index;
1068
1069 if (isHi)
1070 Index = e + i;
1071 else if (isLo)
1072 Index = i;
1073 else if (isEven)
1074 Index = 2 * i;
1075 else if (isOdd)
1076 Index = 2 * i + 1;
1077 else
1078 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattner42158e72007-08-02 23:36:59 +00001079
Nate Begemana1ae7442008-05-13 21:03:02 +00001080 Elts.push_back(Index);
Chris Lattner42158e72007-08-02 23:36:59 +00001081 }
Nate Begemanc8e51f82008-05-09 06:41:27 +00001082}
1083
Steve Naroff4ed9d662007-09-27 14:38:14 +00001084// constructor for instance messages.
Steve Naroff6cb1d362007-09-28 22:22:11 +00001085ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001086 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001087 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001088 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001089 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001090 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001091 NumArgs = nargs;
1092 SubExprs = new Expr*[NumArgs+1];
Steve Naroff4ed9d662007-09-27 14:38:14 +00001093 SubExprs[RECEIVER] = receiver;
Steve Naroff9f176d12007-11-15 13:05:42 +00001094 if (NumArgs) {
1095 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001096 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1097 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001098 LBracloc = LBrac;
1099 RBracloc = RBrac;
1100}
1101
Steve Naroff4ed9d662007-09-27 14:38:14 +00001102// constructor for class messages.
1103// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff6cb1d362007-09-28 22:22:11 +00001104ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001105 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001106 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001107 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001108 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001109 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001110 NumArgs = nargs;
1111 SubExprs = new Expr*[NumArgs+1];
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001112 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | 0x1);
Steve Naroff9f176d12007-11-15 13:05:42 +00001113 if (NumArgs) {
1114 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001115 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1116 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001117 LBracloc = LBrac;
1118 RBracloc = RBrac;
1119}
1120
Chris Lattnerf624cd22007-10-25 00:29:32 +00001121bool ChooseExpr::isConditionTrue(ASTContext &C) const {
1122 llvm::APSInt CondVal(32);
1123 bool IsConst = getCond()->isIntegerConstantExpr(CondVal, C);
1124 assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst;
1125 return CondVal != 0;
1126}
1127
Anders Carlsson52774ad2008-01-29 15:56:48 +00001128static int64_t evaluateOffsetOf(ASTContext& C, const Expr *E)
1129{
1130 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
1131 QualType Ty = ME->getBase()->getType();
1132
1133 RecordDecl *RD = Ty->getAsRecordType()->getDecl();
Chris Lattner8cd0e932008-03-05 18:54:05 +00001134 const ASTRecordLayout &RL = C.getASTRecordLayout(RD);
Anders Carlsson52774ad2008-01-29 15:56:48 +00001135 FieldDecl *FD = ME->getMemberDecl();
1136
1137 // FIXME: This is linear time.
1138 unsigned i = 0, e = 0;
1139 for (i = 0, e = RD->getNumMembers(); i != e; i++) {
1140 if (RD->getMember(i) == FD)
1141 break;
1142 }
1143
1144 return RL.getFieldOffset(i) + evaluateOffsetOf(C, ME->getBase());
1145 } else if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
1146 const Expr *Base = ASE->getBase();
1147 llvm::APSInt Idx(32);
1148 bool ICE = ASE->getIdx()->isIntegerConstantExpr(Idx, C);
1149 assert(ICE && "Array index is not a constant integer!");
1150
Chris Lattner8cd0e932008-03-05 18:54:05 +00001151 int64_t size = C.getTypeSize(ASE->getType());
Anders Carlsson52774ad2008-01-29 15:56:48 +00001152 size *= Idx.getSExtValue();
1153
1154 return size + evaluateOffsetOf(C, Base);
1155 } else if (isa<CompoundLiteralExpr>(E))
1156 return 0;
1157
1158 assert(0 && "Unknown offsetof subexpression!");
1159 return 0;
1160}
1161
1162int64_t UnaryOperator::evaluateOffsetOf(ASTContext& C) const
1163{
1164 assert(Opc == OffsetOf && "Unary operator not offsetof!");
1165
Chris Lattner8cd0e932008-03-05 18:54:05 +00001166 unsigned CharSize = C.Target.getCharWidth();
Anders Carlsson52774ad2008-01-29 15:56:48 +00001167 return ::evaluateOffsetOf(C, Val) / CharSize;
1168}
1169
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001170//===----------------------------------------------------------------------===//
1171// Child Iterators for iterating over subexpressions/substatements
1172//===----------------------------------------------------------------------===//
1173
1174// DeclRefExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001175Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1176Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001177
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001178// ObjCIvarRefExpr
Ted Kremenek07af3dd2008-05-02 18:40:22 +00001179Stmt::child_iterator ObjCIvarRefExpr::child_begin() {
1180 return reinterpret_cast<Stmt**>(&Base);
1181}
1182
1183Stmt::child_iterator ObjCIvarRefExpr::child_end() {
1184 return reinterpret_cast<Stmt**>(&Base)+1;
1185}
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001186
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001187// PreDefinedExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001188Stmt::child_iterator PreDefinedExpr::child_begin() { return child_iterator(); }
1189Stmt::child_iterator PreDefinedExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001190
1191// IntegerLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001192Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1193Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001194
1195// CharacterLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001196Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); }
1197Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001198
1199// FloatingLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001200Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1201Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001202
Chris Lattner1de66eb2007-08-26 03:42:43 +00001203// ImaginaryLiteral
1204Stmt::child_iterator ImaginaryLiteral::child_begin() {
1205 return reinterpret_cast<Stmt**>(&Val);
1206}
1207Stmt::child_iterator ImaginaryLiteral::child_end() {
1208 return reinterpret_cast<Stmt**>(&Val)+1;
1209}
1210
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001211// StringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001212Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1213Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001214
1215// ParenExpr
1216Stmt::child_iterator ParenExpr::child_begin() {
1217 return reinterpret_cast<Stmt**>(&Val);
1218}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001219Stmt::child_iterator ParenExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001220 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001221}
1222
1223// UnaryOperator
1224Stmt::child_iterator UnaryOperator::child_begin() {
Ted Kremenek6d7ea522007-12-15 00:39:18 +00001225 return reinterpret_cast<Stmt**>(&Val);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001226}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001227Stmt::child_iterator UnaryOperator::child_end() {
Ted Kremenek6d7ea522007-12-15 00:39:18 +00001228 return reinterpret_cast<Stmt**>(&Val+1);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001229}
1230
1231// SizeOfAlignOfTypeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001232Stmt::child_iterator SizeOfAlignOfTypeExpr::child_begin() {
Ted Kremenekb7cf0952007-12-14 22:52:23 +00001233 // If the type is a VLA type (and not a typedef), the size expression of the
1234 // VLA needs to be treated as an executable expression.
1235 if (VariableArrayType* T = dyn_cast<VariableArrayType>(Ty.getTypePtr()))
1236 return child_iterator(T);
1237 else
1238 return child_iterator();
Ted Kremeneka6478552007-10-18 23:28:49 +00001239}
1240Stmt::child_iterator SizeOfAlignOfTypeExpr::child_end() {
Ted Kremenekb7cf0952007-12-14 22:52:23 +00001241 return child_iterator();
Ted Kremeneka6478552007-10-18 23:28:49 +00001242}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001243
1244// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001245Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001246 return reinterpret_cast<Stmt**>(&SubExprs);
1247}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001248Stmt::child_iterator ArraySubscriptExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001249 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001250}
1251
1252// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001253Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek15ede502007-08-27 21:11:44 +00001254 return reinterpret_cast<Stmt**>(&SubExprs[0]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001255}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001256Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek15ede502007-08-27 21:11:44 +00001257 return reinterpret_cast<Stmt**>(&SubExprs[NumArgs+ARGS_START]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001258}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001259
1260// MemberExpr
1261Stmt::child_iterator MemberExpr::child_begin() {
1262 return reinterpret_cast<Stmt**>(&Base);
1263}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001264Stmt::child_iterator MemberExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001265 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001266}
1267
Nate Begemanaf6ed502008-04-18 23:10:10 +00001268// ExtVectorElementExpr
1269Stmt::child_iterator ExtVectorElementExpr::child_begin() {
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001270 return reinterpret_cast<Stmt**>(&Base);
1271}
Nate Begemanaf6ed502008-04-18 23:10:10 +00001272Stmt::child_iterator ExtVectorElementExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001273 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001274}
1275
1276// CompoundLiteralExpr
1277Stmt::child_iterator CompoundLiteralExpr::child_begin() {
1278 return reinterpret_cast<Stmt**>(&Init);
1279}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001280Stmt::child_iterator CompoundLiteralExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001281 return reinterpret_cast<Stmt**>(&Init)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001282}
1283
1284// ImplicitCastExpr
1285Stmt::child_iterator ImplicitCastExpr::child_begin() {
1286 return reinterpret_cast<Stmt**>(&Op);
1287}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001288Stmt::child_iterator ImplicitCastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001289 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001290}
1291
1292// CastExpr
1293Stmt::child_iterator CastExpr::child_begin() {
1294 return reinterpret_cast<Stmt**>(&Op);
1295}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001296Stmt::child_iterator CastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001297 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001298}
1299
1300// BinaryOperator
1301Stmt::child_iterator BinaryOperator::child_begin() {
1302 return reinterpret_cast<Stmt**>(&SubExprs);
1303}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001304Stmt::child_iterator BinaryOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001305 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001306}
1307
1308// ConditionalOperator
1309Stmt::child_iterator ConditionalOperator::child_begin() {
1310 return reinterpret_cast<Stmt**>(&SubExprs);
1311}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001312Stmt::child_iterator ConditionalOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001313 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001314}
1315
1316// AddrLabelExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001317Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1318Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001319
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001320// StmtExpr
1321Stmt::child_iterator StmtExpr::child_begin() {
1322 return reinterpret_cast<Stmt**>(&SubStmt);
1323}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001324Stmt::child_iterator StmtExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001325 return reinterpret_cast<Stmt**>(&SubStmt)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001326}
1327
1328// TypesCompatibleExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001329Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1330 return child_iterator();
1331}
1332
1333Stmt::child_iterator TypesCompatibleExpr::child_end() {
1334 return child_iterator();
1335}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001336
1337// ChooseExpr
1338Stmt::child_iterator ChooseExpr::child_begin() {
1339 return reinterpret_cast<Stmt**>(&SubExprs);
1340}
1341
1342Stmt::child_iterator ChooseExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001343 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001344}
1345
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001346// OverloadExpr
1347Stmt::child_iterator OverloadExpr::child_begin() {
1348 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1349}
1350Stmt::child_iterator OverloadExpr::child_end() {
Nate Begemanbd881ef2008-01-30 20:50:20 +00001351 return reinterpret_cast<Stmt**>(&SubExprs[NumExprs]);
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001352}
1353
Eli Friedmand0e9d092008-05-14 19:38:39 +00001354// ShuffleVectorExpr
1355Stmt::child_iterator ShuffleVectorExpr::child_begin() {
1356 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1357}
1358Stmt::child_iterator ShuffleVectorExpr::child_end() {
1359 return reinterpret_cast<Stmt**>(&SubExprs[NumExprs]);
1360}
1361
Anders Carlsson36760332007-10-15 20:28:48 +00001362// VAArgExpr
1363Stmt::child_iterator VAArgExpr::child_begin() {
1364 return reinterpret_cast<Stmt**>(&Val);
1365}
1366
1367Stmt::child_iterator VAArgExpr::child_end() {
1368 return reinterpret_cast<Stmt**>(&Val)+1;
1369}
1370
Anders Carlsson762b7c72007-08-31 04:56:16 +00001371// InitListExpr
1372Stmt::child_iterator InitListExpr::child_begin() {
Steve Naroff84f75c22008-05-07 17:35:03 +00001373 return reinterpret_cast<Stmt**>(InitExprs.size() ?
1374 &InitExprs[0] : 0);
Anders Carlsson762b7c72007-08-31 04:56:16 +00001375}
1376Stmt::child_iterator InitListExpr::child_end() {
Steve Naroff84f75c22008-05-07 17:35:03 +00001377 return reinterpret_cast<Stmt**>(InitExprs.size() ?
1378 &InitExprs[0] + InitExprs.size() : 0);
Anders Carlsson762b7c72007-08-31 04:56:16 +00001379}
1380
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001381// ObjCStringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001382Stmt::child_iterator ObjCStringLiteral::child_begin() {
1383 return child_iterator();
1384}
1385Stmt::child_iterator ObjCStringLiteral::child_end() {
1386 return child_iterator();
1387}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001388
1389// ObjCEncodeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001390Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1391Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001392
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001393// ObjCSelectorExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001394Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1395 return child_iterator();
1396}
1397Stmt::child_iterator ObjCSelectorExpr::child_end() {
1398 return child_iterator();
1399}
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001400
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001401// ObjCProtocolExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001402Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1403 return child_iterator();
1404}
1405Stmt::child_iterator ObjCProtocolExpr::child_end() {
1406 return child_iterator();
1407}
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001408
Steve Naroffc39ca262007-09-18 23:55:05 +00001409// ObjCMessageExpr
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001410Stmt::child_iterator ObjCMessageExpr::child_begin() {
1411 return reinterpret_cast<Stmt**>(&SubExprs[ getReceiver() ? 0 : ARGS_START ]);
Steve Naroffc39ca262007-09-18 23:55:05 +00001412}
1413Stmt::child_iterator ObjCMessageExpr::child_end() {
Steve Naroff4ed9d662007-09-27 14:38:14 +00001414 return reinterpret_cast<Stmt**>(&SubExprs[getNumArgs()+ARGS_START]);
Steve Naroffc39ca262007-09-18 23:55:05 +00001415}
1416