blob: e209db35ffb9a71c671ea40314736c5272260b9f [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Expr.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000015#include "clang/AST/ASTContext.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/AST/StmtVisitor.h"
Chris Lattnerc7229c32007-10-07 08:58:51 +000017#include "clang/Basic/IdentifierTable.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000018#include "clang/Basic/TargetInfo.h"
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner73d0d4f2007-08-30 17:45:32 +000073 case OffsetOf: return "__builtin_offsetof";
Reid Spencer5f016e22007-07-11 17:01:13 +000074 }
75}
76
77//===----------------------------------------------------------------------===//
78// Postfix Operators.
79//===----------------------------------------------------------------------===//
80
Nate Begemane2ce1d92008-01-17 17:46:27 +000081
Reid Spencer5f016e22007-07-11 17:01:13 +000082CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
83 SourceLocation rparenloc)
Ted Kremenek77ed8e42007-08-24 18:13:47 +000084 : Expr(CallExprClass, t), NumArgs(numargs) {
85 SubExprs = new Expr*[numargs+1];
86 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +000087 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +000088 SubExprs[i+ARGS_START] = args[i];
Reid Spencer5f016e22007-07-11 17:01:13 +000089 RParenLoc = rparenloc;
90}
91
Chris Lattnerd18b3292007-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
121
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000122bool CallExpr::isBuiltinClassifyType(llvm::APSInt &Result) const {
123 // The following enum mimics gcc's internal "typeclass.h" file.
124 enum gcc_type_class {
125 no_type_class = -1,
126 void_type_class, integer_type_class, char_type_class,
127 enumeral_type_class, boolean_type_class,
128 pointer_type_class, reference_type_class, offset_type_class,
129 real_type_class, complex_type_class,
130 function_type_class, method_type_class,
131 record_type_class, union_type_class,
132 array_type_class, string_type_class,
133 lang_type_class
134 };
135 Result.setIsSigned(true);
136
137 // All simple function calls (e.g. func()) are implicitly cast to pointer to
138 // function. As a result, we try and obtain the DeclRefExpr from the
139 // ImplicitCastExpr.
140 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
141 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
142 return false;
143 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
144 if (!DRE)
145 return false;
146
147 // We have a DeclRefExpr.
148 if (strcmp(DRE->getDecl()->getName(), "__builtin_classify_type") == 0) {
149 // If no argument was supplied, default to "no_type_class". This isn't
150 // ideal, however it's what gcc does.
151 Result = static_cast<uint64_t>(no_type_class);
152 if (NumArgs >= 1) {
153 QualType argType = getArg(0)->getType();
154
155 if (argType->isVoidType())
156 Result = void_type_class;
157 else if (argType->isEnumeralType())
158 Result = enumeral_type_class;
159 else if (argType->isBooleanType())
160 Result = boolean_type_class;
161 else if (argType->isCharType())
162 Result = string_type_class; // gcc doesn't appear to use char_type_class
163 else if (argType->isIntegerType())
164 Result = integer_type_class;
165 else if (argType->isPointerType())
166 Result = pointer_type_class;
167 else if (argType->isReferenceType())
168 Result = reference_type_class;
169 else if (argType->isRealType())
170 Result = real_type_class;
171 else if (argType->isComplexType())
172 Result = complex_type_class;
173 else if (argType->isFunctionType())
174 Result = function_type_class;
175 else if (argType->isStructureType())
176 Result = record_type_class;
177 else if (argType->isUnionType())
178 Result = union_type_class;
179 else if (argType->isArrayType())
180 Result = array_type_class;
181 else if (argType->isUnionType())
182 Result = union_type_class;
183 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
Chris Lattner3ef5bc02007-11-08 17:56:40 +0000184 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000185 }
186 return true;
187 }
188 return false;
189}
190
Reid Spencer5f016e22007-07-11 17:01:13 +0000191/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
192/// corresponds to, e.g. "<<=".
193const char *BinaryOperator::getOpcodeStr(Opcode Op) {
194 switch (Op) {
195 default: assert(0 && "Unknown binary operator");
196 case Mul: return "*";
197 case Div: return "/";
198 case Rem: return "%";
199 case Add: return "+";
200 case Sub: return "-";
201 case Shl: return "<<";
202 case Shr: return ">>";
203 case LT: return "<";
204 case GT: return ">";
205 case LE: return "<=";
206 case GE: return ">=";
207 case EQ: return "==";
208 case NE: return "!=";
209 case And: return "&";
210 case Xor: return "^";
211 case Or: return "|";
212 case LAnd: return "&&";
213 case LOr: return "||";
214 case Assign: return "=";
215 case MulAssign: return "*=";
216 case DivAssign: return "/=";
217 case RemAssign: return "%=";
218 case AddAssign: return "+=";
219 case SubAssign: return "-=";
220 case ShlAssign: return "<<=";
221 case ShrAssign: return ">>=";
222 case AndAssign: return "&=";
223 case XorAssign: return "^=";
224 case OrAssign: return "|=";
225 case Comma: return ",";
226 }
227}
228
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000229InitListExpr::InitListExpr(SourceLocation lbraceloc,
230 Expr **initexprs, unsigned numinits,
231 SourceLocation rbraceloc)
232 : Expr(InitListExprClass, QualType())
233 , NumInits(numinits)
234 , LBraceLoc(lbraceloc)
235 , RBraceLoc(rbraceloc)
236{
237 InitExprs = new Expr*[numinits];
238 for (unsigned i = 0; i != numinits; i++)
239 InitExprs[i] = initexprs[i];
240}
Reid Spencer5f016e22007-07-11 17:01:13 +0000241
242//===----------------------------------------------------------------------===//
243// Generic Expression Routines
244//===----------------------------------------------------------------------===//
245
246/// hasLocalSideEffect - Return true if this immediate expression has side
247/// effects, not counting any sub-expressions.
248bool Expr::hasLocalSideEffect() const {
249 switch (getStmtClass()) {
250 default:
251 return false;
252 case ParenExprClass:
253 return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect();
254 case UnaryOperatorClass: {
255 const UnaryOperator *UO = cast<UnaryOperator>(this);
256
257 switch (UO->getOpcode()) {
258 default: return false;
259 case UnaryOperator::PostInc:
260 case UnaryOperator::PostDec:
261 case UnaryOperator::PreInc:
262 case UnaryOperator::PreDec:
263 return true; // ++/--
264
265 case UnaryOperator::Deref:
266 // Dereferencing a volatile pointer is a side-effect.
267 return getType().isVolatileQualified();
268 case UnaryOperator::Real:
269 case UnaryOperator::Imag:
270 // accessing a piece of a volatile complex is a side-effect.
271 return UO->getSubExpr()->getType().isVolatileQualified();
272
273 case UnaryOperator::Extension:
274 return UO->getSubExpr()->hasLocalSideEffect();
275 }
276 }
Chris Lattnere7716e62007-12-01 06:07:34 +0000277 case BinaryOperatorClass: {
278 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
279 // Consider comma to have side effects if the LHS and RHS both do.
280 if (BinOp->getOpcode() == BinaryOperator::Comma)
281 return BinOp->getLHS()->hasLocalSideEffect() &&
282 BinOp->getRHS()->hasLocalSideEffect();
283
284 return BinOp->isAssignmentOp();
285 }
Chris Lattnereb14fe82007-08-25 02:00:02 +0000286 case CompoundAssignOperatorClass:
Chris Lattner1f683e92007-08-25 01:55:00 +0000287 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000288
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000289 case ConditionalOperatorClass: {
290 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
291 return Exp->getCond()->hasLocalSideEffect()
292 || (Exp->getLHS() && Exp->getLHS()->hasLocalSideEffect())
293 || (Exp->getRHS() && Exp->getRHS()->hasLocalSideEffect());
294 }
295
Reid Spencer5f016e22007-07-11 17:01:13 +0000296 case MemberExprClass:
297 case ArraySubscriptExprClass:
298 // If the base pointer or element is to a volatile pointer/field, accessing
299 // if is a side effect.
300 return getType().isVolatileQualified();
301
302 case CallExprClass:
303 // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }"
304 // should warn.
305 return true;
Chris Lattnera9c01022007-09-26 22:06:30 +0000306 case ObjCMessageExprClass:
307 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000308
309 case CastExprClass:
310 // If this is a cast to void, check the operand. Otherwise, the result of
311 // the cast is unused.
312 if (getType()->isVoidType())
313 return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect();
314 return false;
315 }
316}
317
318/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
319/// incomplete type other than void. Nonarray expressions that can be lvalues:
320/// - name, where name must be a variable
321/// - e[i]
322/// - (e), where e must be an lvalue
323/// - e.name, where e must be an lvalue
324/// - e->name
325/// - *e, the type of e cannot be a function type
326/// - string-constant
Chris Lattner7da36f62007-10-30 22:53:42 +0000327/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000328/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +0000329///
Bill Wendlingca51c972007-07-16 07:07:56 +0000330Expr::isLvalueResult Expr::isLvalue() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000331 // first, check the type (C99 6.3.2.1)
Chris Lattnercb4f9a62007-07-21 05:33:26 +0000332 if (TR->isFunctionType()) // from isObjectType()
Reid Spencer5f016e22007-07-11 17:01:13 +0000333 return LV_NotObjectType;
334
Steve Naroff731ec572007-07-21 13:32:03 +0000335 if (TR->isVoidType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000336 return LV_IncompleteVoidType;
Bill Wendling08ad47c2007-07-17 03:52:31 +0000337
Chris Lattnercb4f9a62007-07-21 05:33:26 +0000338 if (TR->isReferenceType()) // C++ [expr]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000339 return LV_Valid;
340
Reid Spencer5f016e22007-07-11 17:01:13 +0000341 // the type looks fine, now check the expression
342 switch (getStmtClass()) {
343 case StringLiteralClass: // C99 6.5.1p4
Anders Carlsson7323a622007-11-30 22:47:59 +0000344 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000345 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
346 // For vectors, make sure base is an lvalue (i.e. not a function call).
347 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
348 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue();
349 return LV_Valid;
350 case DeclRefExprClass: // C99 6.5.1p2
351 if (isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl()))
352 return LV_Valid;
353 break;
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000354 case MemberExprClass: { // C99 6.5.2.3p4
Reid Spencer5f016e22007-07-11 17:01:13 +0000355 const MemberExpr *m = cast<MemberExpr>(this);
356 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue();
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000357 }
Chris Lattner7da36f62007-10-30 22:53:42 +0000358 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000359 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +0000360 return LV_Valid; // C99 6.5.3p4
361
362 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
363 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag)
364 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(); // GNU.
Reid Spencer5f016e22007-07-11 17:01:13 +0000365 break;
366 case ParenExprClass: // C99 6.5.1p5
367 return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
Steve Naroffe6386392007-12-05 04:00:10 +0000368 case CompoundLiteralExprClass: // C99 6.5.2.5p5
369 return LV_Valid;
Chris Lattner6481a572007-08-03 17:31:20 +0000370 case OCUVectorElementExprClass:
371 if (cast<OCUVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +0000372 return LV_DuplicateVectorComponents;
373 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +0000374 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
375 return LV_Valid;
Chris Lattnerfa28b302008-01-12 08:14:25 +0000376 case PreDefinedExprClass:
377 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000378 default:
379 break;
380 }
381 return LV_InvalidExpression;
382}
383
384/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
385/// does not have an incomplete type, does not have a const-qualified type, and
386/// if it is a structure or union, does not have any member (including,
387/// recursively, any member or element of all contained aggregates or unions)
388/// with a const-qualified type.
Bill Wendlingca51c972007-07-16 07:07:56 +0000389Expr::isModifiableLvalueResult Expr::isModifiableLvalue() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000390 isLvalueResult lvalResult = isLvalue();
391
392 switch (lvalResult) {
393 case LV_Valid: break;
394 case LV_NotObjectType: return MLV_NotObjectType;
395 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +0000396 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Reid Spencer5f016e22007-07-11 17:01:13 +0000397 case LV_InvalidExpression: return MLV_InvalidExpression;
398 }
399 if (TR.isConstQualified())
400 return MLV_ConstQualified;
401 if (TR->isArrayType())
402 return MLV_ArrayType;
403 if (TR->isIncompleteType())
404 return MLV_IncompleteType;
405
406 if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) {
407 if (r->hasConstFields())
408 return MLV_ConstQualified;
409 }
410 return MLV_Valid;
411}
412
Chris Lattner4cc62712007-11-27 21:35:27 +0000413/// hasStaticStorage - Return true if this expression has static storage
414/// duration. This means that the address of this expression is a link-time
415/// constant.
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000416bool Expr::hasStaticStorage() const {
417 switch (getStmtClass()) {
418 default:
419 return false;
Chris Lattner4cc62712007-11-27 21:35:27 +0000420 case ParenExprClass:
421 return cast<ParenExpr>(this)->getSubExpr()->hasStaticStorage();
422 case ImplicitCastExprClass:
423 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasStaticStorage();
Steve Naroffe9b12192008-01-14 18:19:28 +0000424 case CompoundLiteralExprClass:
425 return cast<CompoundLiteralExpr>(this)->isFileScope();
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000426 case DeclRefExprClass: {
427 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
428 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
429 return VD->hasStaticStorage();
430 return false;
431 }
Chris Lattnerfb708062007-11-28 04:30:09 +0000432 case MemberExprClass: {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000433 const MemberExpr *M = cast<MemberExpr>(this);
434 return !M->isArrow() && M->getBase()->hasStaticStorage();
Chris Lattnerfb708062007-11-28 04:30:09 +0000435 }
Chris Lattner4cc62712007-11-27 21:35:27 +0000436 case ArraySubscriptExprClass:
437 return cast<ArraySubscriptExpr>(this)->getBase()->hasStaticStorage();
Chris Lattnerfa28b302008-01-12 08:14:25 +0000438 case PreDefinedExprClass:
439 return true;
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000440 }
441}
442
Ted Kremenek4e99a5f2008-01-17 16:57:34 +0000443Expr* Expr::IgnoreParens() {
444 Expr* E = this;
445 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
446 E = P->getSubExpr();
447
448 return E;
449}
450
Steve Naroff38374b02007-09-02 20:30:18 +0000451bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
Steve Naroff38374b02007-09-02 20:30:18 +0000452 switch (getStmtClass()) {
453 default:
454 if (Loc) *Loc = getLocStart();
455 return false;
456 case ParenExprClass:
457 return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(Ctx, Loc);
458 case StringLiteralClass:
Steve Naroff5d37e322007-11-09 15:00:03 +0000459 case ObjCStringLiteralClass:
Steve Naroff38374b02007-09-02 20:30:18 +0000460 case FloatingLiteralClass:
461 case IntegerLiteralClass:
462 case CharacterLiteralClass:
463 case ImaginaryLiteralClass:
Anders Carlsson1a86b332007-10-17 00:52:43 +0000464 case TypesCompatibleExprClass:
465 case CXXBoolLiteralExprClass:
Chris Lattner2777e492007-10-18 00:20:32 +0000466 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000467 case CallExprClass: {
468 const CallExpr *CE = cast<CallExpr>(this);
469 llvm::APSInt Result(32);
Hartmut Kaiser86fd3552007-09-16 21:35:35 +0000470 Result.zextOrTrunc(
471 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff38374b02007-09-02 20:30:18 +0000472 if (CE->isBuiltinClassifyType(Result))
Chris Lattner2777e492007-10-18 00:20:32 +0000473 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000474 if (Loc) *Loc = getLocStart();
475 return false;
476 }
Chris Lattner4ef8dd62007-11-01 02:45:17 +0000477 case DeclRefExprClass: {
478 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
479 // Accept address of function.
480 if (isa<EnumConstantDecl>(D) || isa<FunctionDecl>(D))
Chris Lattner2777e492007-10-18 00:20:32 +0000481 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000482 if (Loc) *Loc = getLocStart();
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000483 if (isa<VarDecl>(D))
484 return TR->isArrayType();
Steve Naroff38374b02007-09-02 20:30:18 +0000485 return false;
Chris Lattner4ef8dd62007-11-01 02:45:17 +0000486 }
Steve Naroffb8f13a82008-01-09 00:05:37 +0000487 case CompoundLiteralExprClass:
488 if (Loc) *Loc = getLocStart();
489 // Allow "(int []){2,4}", since the array will be converted to a pointer.
Nate Begemand47d4f52008-01-25 05:34:48 +0000490 // Allow "(vector type){2,4}" since the elements are all constant.
491 return TR->isArrayType() || TR->isVectorType();
Steve Naroff38374b02007-09-02 20:30:18 +0000492 case UnaryOperatorClass: {
493 const UnaryOperator *Exp = cast<UnaryOperator>(this);
494
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000495 // C99 6.6p9
Chris Lattner239c15e2007-12-11 23:11:17 +0000496 if (Exp->getOpcode() == UnaryOperator::AddrOf) {
497 if (!Exp->getSubExpr()->hasStaticStorage()) {
498 if (Loc) *Loc = getLocStart();
499 return false;
500 }
501 return true;
502 }
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000503
Steve Naroff38374b02007-09-02 20:30:18 +0000504 // Get the operand value. If this is sizeof/alignof, do not evalute the
505 // operand. This affects C99 6.6p3.
Steve Naroffd0091aa2008-01-10 22:15:12 +0000506 if (!Exp->isSizeOfAlignOfOp() &&
507 Exp->getOpcode() != UnaryOperator::OffsetOf &&
Steve Naroff38374b02007-09-02 20:30:18 +0000508 !Exp->getSubExpr()->isConstantExpr(Ctx, Loc))
509 return false;
510
511 switch (Exp->getOpcode()) {
512 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
513 // See C99 6.6p3.
514 default:
515 if (Loc) *Loc = Exp->getOperatorLoc();
516 return false;
517 case UnaryOperator::Extension:
518 return true; // FIXME: this is wrong.
519 case UnaryOperator::SizeOf:
520 case UnaryOperator::AlignOf:
Steve Naroffd0091aa2008-01-10 22:15:12 +0000521 case UnaryOperator::OffsetOf:
Steve Naroff38374b02007-09-02 20:30:18 +0000522 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattner65383472007-12-18 07:15:40 +0000523 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx)) {
524 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff38374b02007-09-02 20:30:18 +0000525 return false;
Chris Lattner65383472007-12-18 07:15:40 +0000526 }
Chris Lattner2777e492007-10-18 00:20:32 +0000527 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000528 case UnaryOperator::LNot:
529 case UnaryOperator::Plus:
530 case UnaryOperator::Minus:
531 case UnaryOperator::Not:
Chris Lattner2777e492007-10-18 00:20:32 +0000532 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000533 }
Steve Naroff38374b02007-09-02 20:30:18 +0000534 }
535 case SizeOfAlignOfTypeExprClass: {
536 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
537 // alignof always evaluates to a constant.
Chris Lattner65383472007-12-18 07:15:40 +0000538 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx)) {
539 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff38374b02007-09-02 20:30:18 +0000540 return false;
Chris Lattner65383472007-12-18 07:15:40 +0000541 }
Chris Lattner2777e492007-10-18 00:20:32 +0000542 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000543 }
544 case BinaryOperatorClass: {
545 const BinaryOperator *Exp = cast<BinaryOperator>(this);
546
547 // The LHS of a constant expr is always evaluated and needed.
548 if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
549 return false;
550
551 if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
552 return false;
Chris Lattner2777e492007-10-18 00:20:32 +0000553 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000554 }
555 case ImplicitCastExprClass:
556 case CastExprClass: {
557 const Expr *SubExpr;
558 SourceLocation CastLoc;
559 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
560 SubExpr = C->getSubExpr();
561 CastLoc = C->getLParenLoc();
562 } else {
563 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
564 CastLoc = getLocStart();
565 }
566 if (!SubExpr->isConstantExpr(Ctx, Loc)) {
567 if (Loc) *Loc = SubExpr->getLocStart();
568 return false;
569 }
Chris Lattner2777e492007-10-18 00:20:32 +0000570 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000571 }
572 case ConditionalOperatorClass: {
573 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Chris Lattner2777e492007-10-18 00:20:32 +0000574 if (!Exp->getCond()->isConstantExpr(Ctx, Loc) ||
Anders Carlsson39073232007-11-30 19:04:31 +0000575 // Handle the GNU extension for missing LHS.
576 !(Exp->getLHS() && Exp->getLHS()->isConstantExpr(Ctx, Loc)) ||
Chris Lattner2777e492007-10-18 00:20:32 +0000577 !Exp->getRHS()->isConstantExpr(Ctx, Loc))
Steve Naroff38374b02007-09-02 20:30:18 +0000578 return false;
Chris Lattner2777e492007-10-18 00:20:32 +0000579 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000580 }
Steve Naroffd0091aa2008-01-10 22:15:12 +0000581 case InitListExprClass: {
582 const InitListExpr *Exp = cast<InitListExpr>(this);
583 unsigned numInits = Exp->getNumInits();
584 for (unsigned i = 0; i < numInits; i++) {
585 if (!Exp->getInit(i)->isConstantExpr(Ctx, Loc)) {
586 if (Loc) *Loc = Exp->getInit(i)->getLocStart();
587 return false;
588 }
589 }
590 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000591 }
Steve Naroffd0091aa2008-01-10 22:15:12 +0000592 }
Steve Naroff38374b02007-09-02 20:30:18 +0000593}
594
Reid Spencer5f016e22007-07-11 17:01:13 +0000595/// isIntegerConstantExpr - this recursive routine will test if an expression is
596/// an integer constant expression. Note: With the introduction of VLA's in
597/// C99 the result of the sizeof operator is no longer always a constant
598/// expression. The generalization of the wording to include any subexpression
599/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
600/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
601/// "0 || f()" can be treated as a constant expression. In C90 this expression,
602/// occurring in a context requiring a constant, would have been a constraint
603/// violation. FIXME: This routine currently implements C90 semantics.
604/// To properly implement C99 semantics this routine will need to evaluate
605/// expressions involving operators previously mentioned.
606
607/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
608/// comma, etc
609///
610/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattnerccc213f2007-09-26 00:47:26 +0000611/// permit this. This includes things like (int)1e1000
Chris Lattnerce0afc02007-07-18 05:21:20 +0000612///
613/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
614/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
615/// cast+dereference.
Chris Lattner590b6642007-07-15 23:26:56 +0000616bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
617 SourceLocation *Loc, bool isEvaluated) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000618 switch (getStmtClass()) {
619 default:
620 if (Loc) *Loc = getLocStart();
621 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000622 case ParenExprClass:
623 return cast<ParenExpr>(this)->getSubExpr()->
Chris Lattner590b6642007-07-15 23:26:56 +0000624 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
Reid Spencer5f016e22007-07-11 17:01:13 +0000625 case IntegerLiteralClass:
626 Result = cast<IntegerLiteral>(this)->getValue();
627 break;
Chris Lattner2eadfb62007-07-15 23:32:58 +0000628 case CharacterLiteralClass: {
629 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Chris Lattner701e5eb2007-09-04 02:45:27 +0000630 Result.zextOrTrunc(
631 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CL->getLoc())));
Chris Lattner2eadfb62007-07-15 23:32:58 +0000632 Result = CL->getValue();
Chris Lattnerf0fbcb32007-07-16 21:04:56 +0000633 Result.setIsUnsigned(!getType()->isSignedIntegerType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000634 break;
Chris Lattner2eadfb62007-07-15 23:32:58 +0000635 }
Steve Naroff7b658aa2007-08-02 04:09:23 +0000636 case TypesCompatibleExprClass: {
637 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Chris Lattner701e5eb2007-09-04 02:45:27 +0000638 Result.zextOrTrunc(
639 static_cast<uint32_t>(Ctx.getTypeSize(getType(), TCE->getLocStart())));
Steve Naroffec0550f2007-10-15 20:41:53 +0000640 Result = Ctx.typesAreCompatible(TCE->getArgType1(), TCE->getArgType2());
Steve Naroff389cecc2007-08-02 00:13:27 +0000641 break;
Steve Naroff7b658aa2007-08-02 04:09:23 +0000642 }
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000643 case CallExprClass: {
644 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner701e5eb2007-09-04 02:45:27 +0000645 Result.zextOrTrunc(
646 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000647 if (CE->isBuiltinClassifyType(Result))
648 break;
649 if (Loc) *Loc = getLocStart();
650 return false;
651 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000652 case DeclRefExprClass:
653 if (const EnumConstantDecl *D =
654 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
655 Result = D->getInitVal();
656 break;
657 }
658 if (Loc) *Loc = getLocStart();
659 return false;
660 case UnaryOperatorClass: {
661 const UnaryOperator *Exp = cast<UnaryOperator>(this);
662
663 // Get the operand value. If this is sizeof/alignof, do not evalute the
664 // operand. This affects C99 6.6p3.
Anders Carlsson5a1deb82008-01-29 15:56:48 +0000665 if (!Exp->isSizeOfAlignOfOp() && !Exp->isOffsetOfOp() &&
Chris Lattner602dafd2007-08-23 21:42:50 +0000666 !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000667 return false;
668
669 switch (Exp->getOpcode()) {
670 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
671 // See C99 6.6p3.
672 default:
673 if (Loc) *Loc = Exp->getOperatorLoc();
674 return false;
675 case UnaryOperator::Extension:
Chris Lattner76e773a2007-07-18 18:38:36 +0000676 return true; // FIXME: this is wrong.
Reid Spencer5f016e22007-07-11 17:01:13 +0000677 case UnaryOperator::SizeOf:
678 case UnaryOperator::AlignOf:
679 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattner65383472007-12-18 07:15:40 +0000680 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx)) {
681 if (Loc) *Loc = Exp->getOperatorLoc();
Reid Spencer5f016e22007-07-11 17:01:13 +0000682 return false;
Chris Lattner65383472007-12-18 07:15:40 +0000683 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000684
Chris Lattner76e773a2007-07-18 18:38:36 +0000685 // Return the result in the right width.
Chris Lattner701e5eb2007-09-04 02:45:27 +0000686 Result.zextOrTrunc(
Chris Lattnerccc213f2007-09-26 00:47:26 +0000687 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
688 Exp->getOperatorLoc())));
Chris Lattner76e773a2007-07-18 18:38:36 +0000689
690 // Get information about the size or align.
Chris Lattnerefdd1572008-01-02 21:54:09 +0000691 if (Exp->getSubExpr()->getType()->isFunctionType()) {
692 // GCC extension: sizeof(function) = 1.
693 Result = Exp->getOpcode() == UnaryOperator::AlignOf ? 4 : 1;
694 } else if (Exp->getOpcode() == UnaryOperator::AlignOf) {
Chris Lattner76e773a2007-07-18 18:38:36 +0000695 Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType(),
696 Exp->getOperatorLoc());
Chris Lattnerda5a6b62007-11-27 18:22:04 +0000697 } else {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000698 unsigned CharSize =
699 Ctx.Target.getCharWidth(Ctx.getFullLoc(Exp->getOperatorLoc()));
700
Chris Lattnerda5a6b62007-11-27 18:22:04 +0000701 Result = Ctx.getTypeSize(Exp->getSubExpr()->getType(),
702 Exp->getOperatorLoc()) / CharSize;
703 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000704 break;
705 case UnaryOperator::LNot: {
Chris Lattnerbf755382008-01-25 19:16:19 +0000706 bool Val = Result == 0;
Chris Lattner701e5eb2007-09-04 02:45:27 +0000707 Result.zextOrTrunc(
Chris Lattnerccc213f2007-09-26 00:47:26 +0000708 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
709 Exp->getOperatorLoc())));
Reid Spencer5f016e22007-07-11 17:01:13 +0000710 Result = Val;
711 break;
712 }
713 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000714 break;
715 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000716 Result = -Result;
717 break;
718 case UnaryOperator::Not:
Reid Spencer5f016e22007-07-11 17:01:13 +0000719 Result = ~Result;
720 break;
Anders Carlsson5a1deb82008-01-29 15:56:48 +0000721 case UnaryOperator::OffsetOf:
722 Result = Exp->evaluateOffsetOf(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000723 }
724 break;
725 }
726 case SizeOfAlignOfTypeExprClass: {
727 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
728 // alignof always evaluates to a constant.
Chris Lattner65383472007-12-18 07:15:40 +0000729 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx)) {
730 if (Loc) *Loc = Exp->getOperatorLoc();
Reid Spencer5f016e22007-07-11 17:01:13 +0000731 return false;
Chris Lattner65383472007-12-18 07:15:40 +0000732 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000733
Chris Lattner76e773a2007-07-18 18:38:36 +0000734 // Return the result in the right width.
Chris Lattner701e5eb2007-09-04 02:45:27 +0000735 Result.zextOrTrunc(
736 static_cast<uint32_t>(Ctx.getTypeSize(getType(), Exp->getOperatorLoc())));
Chris Lattner76e773a2007-07-18 18:38:36 +0000737
738 // Get information about the size or align.
Chris Lattnerefdd1572008-01-02 21:54:09 +0000739 if (Exp->getArgumentType()->isFunctionType()) {
740 // GCC extension: sizeof(function) = 1.
741 Result = Exp->isSizeOf() ? 1 : 4;
742 } else if (Exp->isSizeOf()) {
Ted Kremenek060e4702007-12-17 17:38:43 +0000743 unsigned CharSize =
744 Ctx.Target.getCharWidth(Ctx.getFullLoc(Exp->getOperatorLoc()));
745
746 Result = Ctx.getTypeSize(Exp->getArgumentType(),
747 Exp->getOperatorLoc()) / CharSize;
748 }
Chris Lattner76e773a2007-07-18 18:38:36 +0000749 else
750 Result = Ctx.getTypeAlign(Exp->getArgumentType(), Exp->getOperatorLoc());
Ted Kremenek060e4702007-12-17 17:38:43 +0000751
Reid Spencer5f016e22007-07-11 17:01:13 +0000752 break;
753 }
754 case BinaryOperatorClass: {
755 const BinaryOperator *Exp = cast<BinaryOperator>(this);
756
757 // The LHS of a constant expr is always evaluated and needed.
Chris Lattner590b6642007-07-15 23:26:56 +0000758 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000759 return false;
760
761 llvm::APSInt RHS(Result);
762
763 // The short-circuiting &&/|| operators don't necessarily evaluate their
764 // RHS. Make sure to pass isEvaluated down correctly.
765 if (Exp->isLogicalOp()) {
766 bool RHSEval;
767 if (Exp->getOpcode() == BinaryOperator::LAnd)
768 RHSEval = Result != 0;
769 else {
770 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
771 RHSEval = Result == 0;
772 }
773
Chris Lattner590b6642007-07-15 23:26:56 +0000774 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000775 isEvaluated & RHSEval))
776 return false;
777 } else {
Chris Lattner590b6642007-07-15 23:26:56 +0000778 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000779 return false;
780 }
781
Reid Spencer5f016e22007-07-11 17:01:13 +0000782 switch (Exp->getOpcode()) {
783 default:
784 if (Loc) *Loc = getLocStart();
785 return false;
786 case BinaryOperator::Mul:
787 Result *= RHS;
788 break;
789 case BinaryOperator::Div:
790 if (RHS == 0) {
791 if (!isEvaluated) break;
792 if (Loc) *Loc = getLocStart();
793 return false;
794 }
795 Result /= RHS;
796 break;
797 case BinaryOperator::Rem:
798 if (RHS == 0) {
799 if (!isEvaluated) break;
800 if (Loc) *Loc = getLocStart();
801 return false;
802 }
803 Result %= RHS;
804 break;
805 case BinaryOperator::Add: Result += RHS; break;
806 case BinaryOperator::Sub: Result -= RHS; break;
807 case BinaryOperator::Shl:
Chris Lattner701e5eb2007-09-04 02:45:27 +0000808 Result <<=
809 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Reid Spencer5f016e22007-07-11 17:01:13 +0000810 break;
811 case BinaryOperator::Shr:
Chris Lattner701e5eb2007-09-04 02:45:27 +0000812 Result >>=
813 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Reid Spencer5f016e22007-07-11 17:01:13 +0000814 break;
815 case BinaryOperator::LT: Result = Result < RHS; break;
816 case BinaryOperator::GT: Result = Result > RHS; break;
817 case BinaryOperator::LE: Result = Result <= RHS; break;
818 case BinaryOperator::GE: Result = Result >= RHS; break;
819 case BinaryOperator::EQ: Result = Result == RHS; break;
820 case BinaryOperator::NE: Result = Result != RHS; break;
821 case BinaryOperator::And: Result &= RHS; break;
822 case BinaryOperator::Xor: Result ^= RHS; break;
823 case BinaryOperator::Or: Result |= RHS; break;
824 case BinaryOperator::LAnd:
825 Result = Result != 0 && RHS != 0;
826 break;
827 case BinaryOperator::LOr:
828 Result = Result != 0 || RHS != 0;
829 break;
830
831 case BinaryOperator::Comma:
832 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
833 // *except* when they are contained within a subexpression that is not
834 // evaluated". Note that Assignment can never happen due to constraints
835 // on the LHS subexpr, so we don't need to check it here.
836 if (isEvaluated) {
837 if (Loc) *Loc = getLocStart();
838 return false;
839 }
840
841 // The result of the constant expr is the RHS.
842 Result = RHS;
843 return true;
844 }
845
846 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
847 break;
848 }
Chris Lattner26dc7b32007-07-15 23:54:50 +0000849 case ImplicitCastExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000850 case CastExprClass: {
Chris Lattner26dc7b32007-07-15 23:54:50 +0000851 const Expr *SubExpr;
852 SourceLocation CastLoc;
853 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
854 SubExpr = C->getSubExpr();
855 CastLoc = C->getLParenLoc();
856 } else {
857 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
858 CastLoc = getLocStart();
859 }
860
Reid Spencer5f016e22007-07-11 17:01:13 +0000861 // C99 6.6p6: shall only convert arithmetic types to integer types.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000862 if (!SubExpr->getType()->isArithmeticType() ||
863 !getType()->isIntegerType()) {
864 if (Loc) *Loc = SubExpr->getLocStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000865 return false;
866 }
Chris Lattner987b15d2007-09-22 19:04:13 +0000867
868 uint32_t DestWidth =
869 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CastLoc));
870
Reid Spencer5f016e22007-07-11 17:01:13 +0000871 // Handle simple integer->integer casts.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000872 if (SubExpr->getType()->isIntegerType()) {
873 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000874 return false;
Chris Lattner26dc7b32007-07-15 23:54:50 +0000875
876 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000877 // If the input is signed, do a sign extend, noop, or truncate.
Chris Lattnerc0a356b2008-01-09 18:59:34 +0000878 if (getType()->isBooleanType()) {
879 // Conversion to bool compares against zero.
880 Result = Result != 0;
881 Result.zextOrTrunc(DestWidth);
882 } else if (SubExpr->getType()->isSignedIntegerType())
Chris Lattner26dc7b32007-07-15 23:54:50 +0000883 Result.sextOrTrunc(DestWidth);
884 else // If the input is unsigned, do a zero extend, noop, or truncate.
885 Result.zextOrTrunc(DestWidth);
Reid Spencer5f016e22007-07-11 17:01:13 +0000886 break;
887 }
888
889 // Allow floating constants that are the immediate operands of casts or that
890 // are parenthesized.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000891 const Expr *Operand = SubExpr;
Reid Spencer5f016e22007-07-11 17:01:13 +0000892 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
893 Operand = PE->getSubExpr();
Chris Lattner987b15d2007-09-22 19:04:13 +0000894
895 // If this isn't a floating literal, we can't handle it.
896 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
897 if (!FL) {
898 if (Loc) *Loc = Operand->getLocStart();
899 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000900 }
Chris Lattnerc0a356b2008-01-09 18:59:34 +0000901
902 // If the destination is boolean, compare against zero.
903 if (getType()->isBooleanType()) {
904 Result = !FL->getValue().isZero();
905 Result.zextOrTrunc(DestWidth);
906 break;
907 }
Chris Lattner987b15d2007-09-22 19:04:13 +0000908
909 // Determine whether we are converting to unsigned or signed.
910 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattnerccc213f2007-09-26 00:47:26 +0000911
912 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
913 // be called multiple times per AST.
Chris Lattner987b15d2007-09-22 19:04:13 +0000914 uint64_t Space[4];
Chris Lattnerccc213f2007-09-26 00:47:26 +0000915 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
916 llvm::APFloat::rmTowardZero);
Chris Lattner987b15d2007-09-22 19:04:13 +0000917 Result = llvm::APInt(DestWidth, 4, Space);
918 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000919 }
920 case ConditionalOperatorClass: {
921 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
922
Chris Lattner590b6642007-07-15 23:26:56 +0000923 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000924 return false;
925
926 const Expr *TrueExp = Exp->getLHS();
927 const Expr *FalseExp = Exp->getRHS();
928 if (Result == 0) std::swap(TrueExp, FalseExp);
929
930 // Evaluate the false one first, discard the result.
Anders Carlsson39073232007-11-30 19:04:31 +0000931 if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Reid Spencer5f016e22007-07-11 17:01:13 +0000932 return false;
933 // Evalute the true one, capture the result.
Anders Carlsson39073232007-11-30 19:04:31 +0000934 if (TrueExp &&
935 !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000936 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000937 break;
938 }
939 }
940
941 // Cases that are valid constant exprs fall through to here.
942 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
943 return true;
944}
945
Reid Spencer5f016e22007-07-11 17:01:13 +0000946/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
947/// integer constant expression with the value zero, or if this is one that is
948/// cast to void*.
Chris Lattner590b6642007-07-15 23:26:56 +0000949bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
Steve Naroffaa58f002008-01-14 16:10:57 +0000950 // Strip off a cast to void*, if it exists.
951 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
952 // Check that it is a cast to void*.
Reid Spencer5f016e22007-07-11 17:01:13 +0000953 if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) {
954 QualType Pointee = PT->getPointeeType();
Steve Naroffaa58f002008-01-14 16:10:57 +0000955 if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void*
956 CE->getSubExpr()->getType()->isIntegerType()) // from int.
957 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000958 }
Steve Naroffaa58f002008-01-14 16:10:57 +0000959 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
960 // Ignore the ImplicitCastExpr type entirely.
961 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
962 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
963 // Accept ((void*)0) as a null pointer constant, as many other
964 // implementations do.
965 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffaaffbf72008-01-14 02:53:34 +0000966 }
Steve Naroffaa58f002008-01-14 16:10:57 +0000967
968 // This expression must be an integer type.
969 if (!getType()->isIntegerType())
970 return false;
971
Reid Spencer5f016e22007-07-11 17:01:13 +0000972 // If we have an integer constant expression, we need to *evaluate* it and
973 // test for the value 0.
974 llvm::APSInt Val(32);
Steve Naroffaa58f002008-01-14 16:10:57 +0000975 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000976}
Steve Naroff31a45842007-07-28 23:10:27 +0000977
Chris Lattner6481a572007-08-03 17:31:20 +0000978unsigned OCUVectorElementExpr::getNumElements() const {
Chris Lattner4d0ac882007-08-03 16:00:20 +0000979 return strlen(Accessor.getName());
980}
981
982
Chris Lattnercb92a112007-08-02 21:47:28 +0000983/// getComponentType - Determine whether the components of this access are
984/// "point" "color" or "texture" elements.
Chris Lattner6481a572007-08-03 17:31:20 +0000985OCUVectorElementExpr::ElementType
986OCUVectorElementExpr::getElementType() const {
Steve Naroff31a45842007-07-28 23:10:27 +0000987 // derive the component type, no need to waste space.
988 const char *compStr = Accessor.getName();
Chris Lattnerb4878f42007-08-02 22:20:00 +0000989
Chris Lattner88dca042007-08-02 22:33:49 +0000990 if (OCUVectorType::getPointAccessorIdx(*compStr) != -1) return Point;
991 if (OCUVectorType::getColorAccessorIdx(*compStr) != -1) return Color;
Chris Lattnerb4878f42007-08-02 22:20:00 +0000992
Chris Lattner88dca042007-08-02 22:33:49 +0000993 assert(OCUVectorType::getTextureAccessorIdx(*compStr) != -1 &&
Chris Lattnerb4878f42007-08-02 22:20:00 +0000994 "getComponentType(): Illegal accessor");
995 return Texture;
Steve Naroff31a45842007-07-28 23:10:27 +0000996}
Steve Narofffec0b492007-07-30 03:29:09 +0000997
Chris Lattner6481a572007-08-03 17:31:20 +0000998/// containsDuplicateElements - Return true if any element access is
Chris Lattnercb92a112007-08-02 21:47:28 +0000999/// repeated.
Chris Lattner6481a572007-08-03 17:31:20 +00001000bool OCUVectorElementExpr::containsDuplicateElements() const {
Steve Narofffec0b492007-07-30 03:29:09 +00001001 const char *compStr = Accessor.getName();
1002 unsigned length = strlen(compStr);
1003
1004 for (unsigned i = 0; i < length-1; i++) {
1005 const char *s = compStr+i;
1006 for (const char c = *s++; *s; s++)
1007 if (c == *s)
1008 return true;
1009 }
1010 return false;
1011}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001012
1013/// getEncodedElementAccess - We encode fields with two bits per component.
Chris Lattner6481a572007-08-03 17:31:20 +00001014unsigned OCUVectorElementExpr::getEncodedElementAccess() const {
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001015 const char *compStr = Accessor.getName();
Chris Lattner6481a572007-08-03 17:31:20 +00001016 unsigned length = getNumElements();
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001017
1018 unsigned Result = 0;
1019
1020 while (length--) {
1021 Result <<= 2;
1022 int Idx = OCUVectorType::getAccessorIdx(compStr[length]);
1023 assert(Idx != -1 && "Invalid accessor letter");
1024 Result |= Idx;
1025 }
1026 return Result;
1027}
1028
Steve Naroff68d331a2007-09-27 14:38:14 +00001029// constructor for instance messages.
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001030ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001031 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001032 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001033 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001034 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1035 MethodProto(mproto), ClassName(0) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001036 NumArgs = nargs;
1037 SubExprs = new Expr*[NumArgs+1];
Steve Naroff68d331a2007-09-27 14:38:14 +00001038 SubExprs[RECEIVER] = receiver;
Steve Naroff49f109c2007-11-15 13:05:42 +00001039 if (NumArgs) {
1040 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001041 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1042 }
Steve Naroff563477d2007-09-18 23:55:05 +00001043 LBracloc = LBrac;
1044 RBracloc = RBrac;
1045}
1046
Steve Naroff68d331a2007-09-27 14:38:14 +00001047// constructor for class messages.
1048// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001049ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001050 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001051 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001052 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001053 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1054 MethodProto(mproto), ClassName(clsName) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001055 NumArgs = nargs;
1056 SubExprs = new Expr*[NumArgs+1];
Steve Naroff563477d2007-09-18 23:55:05 +00001057 SubExprs[RECEIVER] = 0;
Steve Naroff49f109c2007-11-15 13:05:42 +00001058 if (NumArgs) {
1059 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001060 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1061 }
Steve Naroff563477d2007-09-18 23:55:05 +00001062 LBracloc = LBrac;
1063 RBracloc = RBrac;
1064}
1065
Chris Lattner27437ca2007-10-25 00:29:32 +00001066
1067bool ChooseExpr::isConditionTrue(ASTContext &C) const {
1068 llvm::APSInt CondVal(32);
1069 bool IsConst = getCond()->isIntegerConstantExpr(CondVal, C);
1070 assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst;
1071 return CondVal != 0;
1072}
1073
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001074static int64_t evaluateOffsetOf(ASTContext& C, const Expr *E)
1075{
1076 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
1077 QualType Ty = ME->getBase()->getType();
1078
1079 RecordDecl *RD = Ty->getAsRecordType()->getDecl();
1080 const ASTRecordLayout &RL = C.getASTRecordLayout(RD, SourceLocation());
1081 FieldDecl *FD = ME->getMemberDecl();
1082
1083 // FIXME: This is linear time.
1084 unsigned i = 0, e = 0;
1085 for (i = 0, e = RD->getNumMembers(); i != e; i++) {
1086 if (RD->getMember(i) == FD)
1087 break;
1088 }
1089
1090 return RL.getFieldOffset(i) + evaluateOffsetOf(C, ME->getBase());
1091 } else if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
1092 const Expr *Base = ASE->getBase();
1093 llvm::APSInt Idx(32);
1094 bool ICE = ASE->getIdx()->isIntegerConstantExpr(Idx, C);
1095 assert(ICE && "Array index is not a constant integer!");
1096
1097 int64_t size = C.getTypeSize(ASE->getType(), SourceLocation());
1098 size *= Idx.getSExtValue();
1099
1100 return size + evaluateOffsetOf(C, Base);
1101 } else if (isa<CompoundLiteralExpr>(E))
1102 return 0;
1103
1104 assert(0 && "Unknown offsetof subexpression!");
1105 return 0;
1106}
1107
1108int64_t UnaryOperator::evaluateOffsetOf(ASTContext& C) const
1109{
1110 assert(Opc == OffsetOf && "Unary operator not offsetof!");
1111
1112 unsigned CharSize =
1113 C.Target.getCharWidth(C.getFullLoc(getOperatorLoc()));
1114
1115 return ::evaluateOffsetOf(C, Val) / CharSize;
1116}
1117
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001118//===----------------------------------------------------------------------===//
1119// Child Iterators for iterating over subexpressions/substatements
1120//===----------------------------------------------------------------------===//
1121
1122// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001123Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1124Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001125
Steve Naroff7779db42007-11-12 14:29:37 +00001126// ObjCIvarRefExpr
1127Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return child_iterator(); }
1128Stmt::child_iterator ObjCIvarRefExpr::child_end() { return child_iterator(); }
1129
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001130// PreDefinedExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001131Stmt::child_iterator PreDefinedExpr::child_begin() { return child_iterator(); }
1132Stmt::child_iterator PreDefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001133
1134// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001135Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1136Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001137
1138// CharacterLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001139Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); }
1140Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001141
1142// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001143Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1144Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001145
Chris Lattner5d661452007-08-26 03:42:43 +00001146// ImaginaryLiteral
1147Stmt::child_iterator ImaginaryLiteral::child_begin() {
1148 return reinterpret_cast<Stmt**>(&Val);
1149}
1150Stmt::child_iterator ImaginaryLiteral::child_end() {
1151 return reinterpret_cast<Stmt**>(&Val)+1;
1152}
1153
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001154// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001155Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1156Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001157
1158// ParenExpr
1159Stmt::child_iterator ParenExpr::child_begin() {
1160 return reinterpret_cast<Stmt**>(&Val);
1161}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001162Stmt::child_iterator ParenExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001163 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001164}
1165
1166// UnaryOperator
1167Stmt::child_iterator UnaryOperator::child_begin() {
Ted Kremenekf816f772007-12-15 00:39:18 +00001168 return reinterpret_cast<Stmt**>(&Val);
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001169}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001170Stmt::child_iterator UnaryOperator::child_end() {
Ted Kremenekf816f772007-12-15 00:39:18 +00001171 return reinterpret_cast<Stmt**>(&Val+1);
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001172}
1173
1174// SizeOfAlignOfTypeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001175Stmt::child_iterator SizeOfAlignOfTypeExpr::child_begin() {
Ted Kremenek699e9fb2007-12-14 22:52:23 +00001176 // If the type is a VLA type (and not a typedef), the size expression of the
1177 // VLA needs to be treated as an executable expression.
1178 if (VariableArrayType* T = dyn_cast<VariableArrayType>(Ty.getTypePtr()))
1179 return child_iterator(T);
1180 else
1181 return child_iterator();
Ted Kremenek9ac59282007-10-18 23:28:49 +00001182}
1183Stmt::child_iterator SizeOfAlignOfTypeExpr::child_end() {
Ted Kremenek699e9fb2007-12-14 22:52:23 +00001184 return child_iterator();
Ted Kremenek9ac59282007-10-18 23:28:49 +00001185}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001186
1187// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001188Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001189 return reinterpret_cast<Stmt**>(&SubExprs);
1190}
Ted Kremenek1237c672007-08-24 20:06:47 +00001191Stmt::child_iterator ArraySubscriptExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001192 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001193}
1194
1195// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001196Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek42a29772007-08-27 21:11:44 +00001197 return reinterpret_cast<Stmt**>(&SubExprs[0]);
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001198}
Ted Kremenek1237c672007-08-24 20:06:47 +00001199Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek42a29772007-08-27 21:11:44 +00001200 return reinterpret_cast<Stmt**>(&SubExprs[NumArgs+ARGS_START]);
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001201}
Ted Kremenek1237c672007-08-24 20:06:47 +00001202
1203// MemberExpr
1204Stmt::child_iterator MemberExpr::child_begin() {
1205 return reinterpret_cast<Stmt**>(&Base);
1206}
Ted Kremenek1237c672007-08-24 20:06:47 +00001207Stmt::child_iterator MemberExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001208 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001209}
1210
1211// OCUVectorElementExpr
1212Stmt::child_iterator OCUVectorElementExpr::child_begin() {
1213 return reinterpret_cast<Stmt**>(&Base);
1214}
Ted Kremenek1237c672007-08-24 20:06:47 +00001215Stmt::child_iterator OCUVectorElementExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001216 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001217}
1218
1219// CompoundLiteralExpr
1220Stmt::child_iterator CompoundLiteralExpr::child_begin() {
1221 return reinterpret_cast<Stmt**>(&Init);
1222}
Ted Kremenek1237c672007-08-24 20:06:47 +00001223Stmt::child_iterator CompoundLiteralExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001224 return reinterpret_cast<Stmt**>(&Init)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001225}
1226
1227// ImplicitCastExpr
1228Stmt::child_iterator ImplicitCastExpr::child_begin() {
1229 return reinterpret_cast<Stmt**>(&Op);
1230}
Ted Kremenek1237c672007-08-24 20:06:47 +00001231Stmt::child_iterator ImplicitCastExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001232 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001233}
1234
1235// CastExpr
1236Stmt::child_iterator CastExpr::child_begin() {
1237 return reinterpret_cast<Stmt**>(&Op);
1238}
Ted Kremenek1237c672007-08-24 20:06:47 +00001239Stmt::child_iterator CastExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001240 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001241}
1242
1243// BinaryOperator
1244Stmt::child_iterator BinaryOperator::child_begin() {
1245 return reinterpret_cast<Stmt**>(&SubExprs);
1246}
Ted Kremenek1237c672007-08-24 20:06:47 +00001247Stmt::child_iterator BinaryOperator::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001248 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001249}
1250
1251// ConditionalOperator
1252Stmt::child_iterator ConditionalOperator::child_begin() {
1253 return reinterpret_cast<Stmt**>(&SubExprs);
1254}
Ted Kremenek1237c672007-08-24 20:06:47 +00001255Stmt::child_iterator ConditionalOperator::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001256 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001257}
1258
1259// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001260Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1261Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001262
Ted Kremenek1237c672007-08-24 20:06:47 +00001263// StmtExpr
1264Stmt::child_iterator StmtExpr::child_begin() {
1265 return reinterpret_cast<Stmt**>(&SubStmt);
1266}
Ted Kremenek1237c672007-08-24 20:06:47 +00001267Stmt::child_iterator StmtExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001268 return reinterpret_cast<Stmt**>(&SubStmt)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001269}
1270
1271// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001272Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1273 return child_iterator();
1274}
1275
1276Stmt::child_iterator TypesCompatibleExpr::child_end() {
1277 return child_iterator();
1278}
Ted Kremenek1237c672007-08-24 20:06:47 +00001279
1280// ChooseExpr
1281Stmt::child_iterator ChooseExpr::child_begin() {
1282 return reinterpret_cast<Stmt**>(&SubExprs);
1283}
1284
1285Stmt::child_iterator ChooseExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001286 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001287}
1288
Nate Begemane2ce1d92008-01-17 17:46:27 +00001289// OverloadExpr
1290Stmt::child_iterator OverloadExpr::child_begin() {
1291 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1292}
1293Stmt::child_iterator OverloadExpr::child_end() {
Nate Begeman67295d02008-01-30 20:50:20 +00001294 return reinterpret_cast<Stmt**>(&SubExprs[NumExprs]);
Nate Begemane2ce1d92008-01-17 17:46:27 +00001295}
1296
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001297// VAArgExpr
1298Stmt::child_iterator VAArgExpr::child_begin() {
1299 return reinterpret_cast<Stmt**>(&Val);
1300}
1301
1302Stmt::child_iterator VAArgExpr::child_end() {
1303 return reinterpret_cast<Stmt**>(&Val)+1;
1304}
1305
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001306// InitListExpr
1307Stmt::child_iterator InitListExpr::child_begin() {
1308 return reinterpret_cast<Stmt**>(&InitExprs[0]);
1309}
1310Stmt::child_iterator InitListExpr::child_end() {
1311 return reinterpret_cast<Stmt**>(&InitExprs[NumInits]);
1312}
1313
Ted Kremenek1237c672007-08-24 20:06:47 +00001314// ObjCStringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001315Stmt::child_iterator ObjCStringLiteral::child_begin() {
1316 return child_iterator();
1317}
1318Stmt::child_iterator ObjCStringLiteral::child_end() {
1319 return child_iterator();
1320}
Ted Kremenek1237c672007-08-24 20:06:47 +00001321
1322// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001323Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1324Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001325
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001326// ObjCSelectorExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001327Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1328 return child_iterator();
1329}
1330Stmt::child_iterator ObjCSelectorExpr::child_end() {
1331 return child_iterator();
1332}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001333
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001334// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001335Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1336 return child_iterator();
1337}
1338Stmt::child_iterator ObjCProtocolExpr::child_end() {
1339 return child_iterator();
1340}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001341
Steve Naroff563477d2007-09-18 23:55:05 +00001342// ObjCMessageExpr
1343Stmt::child_iterator ObjCMessageExpr::child_begin() {
1344 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1345}
1346Stmt::child_iterator ObjCMessageExpr::child_end() {
Steve Naroff68d331a2007-09-27 14:38:14 +00001347 return reinterpret_cast<Stmt**>(&SubExprs[getNumArgs()+ARGS_START]);
Steve Naroff563477d2007-09-18 23:55:05 +00001348}
1349