]> granicus.if.org Git - clang/blob - AST/Expr.cpp
implement codegen support for sizeof(void), fixing PR2080.
[clang] / AST / Expr.cpp
1 //===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the Expr class and subclasses.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "clang/AST/Expr.h"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/StmtVisitor.h"
17 #include "clang/Basic/IdentifierTable.h"
18 #include "clang/Basic/TargetInfo.h"
19 using namespace clang;
20
21 //===----------------------------------------------------------------------===//
22 // Primary Expressions.
23 //===----------------------------------------------------------------------===//
24
25 StringLiteral::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
39 StringLiteral::~StringLiteral() {
40   delete[] StrData;
41 }
42
43 bool 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]++".
55 const 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__";
73   case OffsetOf: return "__builtin_offsetof";
74   }
75 }
76
77 //===----------------------------------------------------------------------===//
78 // Postfix Operators.
79 //===----------------------------------------------------------------------===//
80
81
82 CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
83                    SourceLocation rparenloc)
84   : Expr(CallExprClass, t), NumArgs(numargs) {
85   SubExprs = new Expr*[numargs+1];
86   SubExprs[FN] = fn;
87   for (unsigned i = 0; i != numargs; ++i)
88     SubExprs[i+ARGS_START] = args[i];
89   RParenLoc = rparenloc;
90 }
91
92 /// 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.
95 void 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 bool 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
133   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
142   if (builtinID == Builtin::BI__builtin___CFStringMakeConstantString)
143     return true;
144   return false;
145 }
146
147 bool CallExpr::isBuiltinClassifyType(llvm::APSInt &Result) const {
148   // The following enum mimics gcc's internal "typeclass.h" file.
149   enum gcc_type_class {
150     no_type_class = -1,
151     void_type_class, integer_type_class, char_type_class,
152     enumeral_type_class, boolean_type_class,
153     pointer_type_class, reference_type_class, offset_type_class,
154     real_type_class, complex_type_class,
155     function_type_class, method_type_class,
156     record_type_class, union_type_class,
157     array_type_class, string_type_class,
158     lang_type_class
159   };
160   Result.setIsSigned(true);
161   
162   // All simple function calls (e.g. func()) are implicitly cast to pointer to
163   // function. As a result, we try and obtain the DeclRefExpr from the 
164   // ImplicitCastExpr.
165   const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
166   if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
167     return false;
168   const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
169   if (!DRE)
170     return false;
171
172   // We have a DeclRefExpr.
173   if (strcmp(DRE->getDecl()->getName(), "__builtin_classify_type") == 0) {
174     // If no argument was supplied, default to "no_type_class". This isn't 
175     // ideal, however it's what gcc does.
176     Result = static_cast<uint64_t>(no_type_class);
177     if (NumArgs >= 1) {
178       QualType argType = getArg(0)->getType();
179       
180       if (argType->isVoidType())
181         Result = void_type_class;
182       else if (argType->isEnumeralType())
183         Result = enumeral_type_class;
184       else if (argType->isBooleanType())
185         Result = boolean_type_class;
186       else if (argType->isCharType())
187         Result = string_type_class; // gcc doesn't appear to use char_type_class
188       else if (argType->isIntegerType())
189         Result = integer_type_class;
190       else if (argType->isPointerType())
191         Result = pointer_type_class;
192       else if (argType->isReferenceType())
193         Result = reference_type_class;
194       else if (argType->isRealType())
195         Result = real_type_class;
196       else if (argType->isComplexType())
197         Result = complex_type_class;
198       else if (argType->isFunctionType())
199         Result = function_type_class;
200       else if (argType->isStructureType())
201         Result = record_type_class;
202       else if (argType->isUnionType())
203         Result = union_type_class;
204       else if (argType->isArrayType())
205         Result = array_type_class;
206       else if (argType->isUnionType())
207         Result = union_type_class;
208       else  // FIXME: offset_type_class, method_type_class, & lang_type_class?
209         assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
210     }
211     return true;
212   }
213   return false;
214 }
215
216 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
217 /// corresponds to, e.g. "<<=".
218 const char *BinaryOperator::getOpcodeStr(Opcode Op) {
219   switch (Op) {
220   default: assert(0 && "Unknown binary operator");
221   case Mul:       return "*";
222   case Div:       return "/";
223   case Rem:       return "%";
224   case Add:       return "+";
225   case Sub:       return "-";
226   case Shl:       return "<<";
227   case Shr:       return ">>";
228   case LT:        return "<";
229   case GT:        return ">";
230   case LE:        return "<=";
231   case GE:        return ">=";
232   case EQ:        return "==";
233   case NE:        return "!=";
234   case And:       return "&";
235   case Xor:       return "^";
236   case Or:        return "|";
237   case LAnd:      return "&&";
238   case LOr:       return "||";
239   case Assign:    return "=";
240   case MulAssign: return "*=";
241   case DivAssign: return "/=";
242   case RemAssign: return "%=";
243   case AddAssign: return "+=";
244   case SubAssign: return "-=";
245   case ShlAssign: return "<<=";
246   case ShrAssign: return ">>=";
247   case AndAssign: return "&=";
248   case XorAssign: return "^=";
249   case OrAssign:  return "|=";
250   case Comma:     return ",";
251   }
252 }
253
254 InitListExpr::InitListExpr(SourceLocation lbraceloc, 
255                            Expr **initexprs, unsigned numinits,
256                            SourceLocation rbraceloc)
257   : Expr(InitListExprClass, QualType())
258   , NumInits(numinits)
259   , LBraceLoc(lbraceloc)
260   , RBraceLoc(rbraceloc)
261 {
262   InitExprs = new Expr*[numinits];
263   for (unsigned i = 0; i != numinits; i++)
264     InitExprs[i] = initexprs[i];
265 }
266
267 //===----------------------------------------------------------------------===//
268 // Generic Expression Routines
269 //===----------------------------------------------------------------------===//
270
271 /// hasLocalSideEffect - Return true if this immediate expression has side
272 /// effects, not counting any sub-expressions.
273 bool Expr::hasLocalSideEffect() const {
274   switch (getStmtClass()) {
275   default:
276     return false;
277   case ParenExprClass:
278     return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect();
279   case UnaryOperatorClass: {
280     const UnaryOperator *UO = cast<UnaryOperator>(this);
281     
282     switch (UO->getOpcode()) {
283     default: return false;
284     case UnaryOperator::PostInc:
285     case UnaryOperator::PostDec:
286     case UnaryOperator::PreInc:
287     case UnaryOperator::PreDec:
288       return true;                     // ++/--
289
290     case UnaryOperator::Deref:
291       // Dereferencing a volatile pointer is a side-effect.
292       return getType().isVolatileQualified();
293     case UnaryOperator::Real:
294     case UnaryOperator::Imag:
295       // accessing a piece of a volatile complex is a side-effect.
296       return UO->getSubExpr()->getType().isVolatileQualified();
297
298     case UnaryOperator::Extension:
299       return UO->getSubExpr()->hasLocalSideEffect();
300     }
301   }
302   case BinaryOperatorClass: {
303     const BinaryOperator *BinOp = cast<BinaryOperator>(this);
304     // Consider comma to have side effects if the LHS and RHS both do.
305     if (BinOp->getOpcode() == BinaryOperator::Comma)
306       return BinOp->getLHS()->hasLocalSideEffect() &&
307              BinOp->getRHS()->hasLocalSideEffect();
308       
309     return BinOp->isAssignmentOp();
310   }
311   case CompoundAssignOperatorClass:
312     return true;
313
314   case ConditionalOperatorClass: {
315     const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
316     return Exp->getCond()->hasLocalSideEffect()
317            || (Exp->getLHS() && Exp->getLHS()->hasLocalSideEffect())
318            || (Exp->getRHS() && Exp->getRHS()->hasLocalSideEffect());
319   }
320
321   case MemberExprClass:
322   case ArraySubscriptExprClass:
323     // If the base pointer or element is to a volatile pointer/field, accessing
324     // if is a side effect.
325     return getType().isVolatileQualified();
326     
327   case CallExprClass:
328     // TODO: check attributes for pure/const.   "void foo() { strlen("bar"); }"
329     // should warn.
330     return true;
331   case ObjCMessageExprClass:
332     return true;
333     
334   case CastExprClass:
335     // If this is a cast to void, check the operand.  Otherwise, the result of
336     // the cast is unused.
337     if (getType()->isVoidType())
338       return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect();
339     return false;
340   }     
341 }
342
343 /// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
344 /// incomplete type other than void. Nonarray expressions that can be lvalues:
345 ///  - name, where name must be a variable
346 ///  - e[i]
347 ///  - (e), where e must be an lvalue
348 ///  - e.name, where e must be an lvalue
349 ///  - e->name
350 ///  - *e, the type of e cannot be a function type
351 ///  - string-constant
352 ///  - (__real__ e) and (__imag__ e) where e is an lvalue  [GNU extension]
353 ///  - reference type [C++ [expr]]
354 ///
355 Expr::isLvalueResult Expr::isLvalue() const {
356   // first, check the type (C99 6.3.2.1)
357   if (TR->isFunctionType()) // from isObjectType()
358     return LV_NotObjectType;
359
360   // Allow qualified void which is an incomplete type other than void (yuck).
361   if (TR->isVoidType() && !TR.getCanonicalType().getCVRQualifiers())
362     return LV_IncompleteVoidType;
363
364   if (TR->isReferenceType()) // C++ [expr]
365     return LV_Valid;
366
367   // the type looks fine, now check the expression
368   switch (getStmtClass()) {
369   case StringLiteralClass: // C99 6.5.1p4
370     return LV_Valid;
371   case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
372     // For vectors, make sure base is an lvalue (i.e. not a function call).
373     if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
374       return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue();
375     return LV_Valid;
376   case DeclRefExprClass: // C99 6.5.1p2
377     if (isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl()))
378       return LV_Valid;
379     break;
380   case MemberExprClass: { // C99 6.5.2.3p4
381     const MemberExpr *m = cast<MemberExpr>(this);
382     return m->isArrow() ? LV_Valid : m->getBase()->isLvalue();
383   }
384   case UnaryOperatorClass:
385     if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
386       return LV_Valid; // C99 6.5.3p4
387
388     if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
389         cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag)
390       return cast<UnaryOperator>(this)->getSubExpr()->isLvalue();  // GNU.
391     break;
392   case ParenExprClass: // C99 6.5.1p5
393     return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
394   case CompoundLiteralExprClass: // C99 6.5.2.5p5
395     return LV_Valid;
396   case OCUVectorElementExprClass:
397     if (cast<OCUVectorElementExpr>(this)->containsDuplicateElements())
398       return LV_DuplicateVectorComponents;
399     return LV_Valid;
400   case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
401     return LV_Valid;
402   case PreDefinedExprClass:
403     return LV_Valid;
404   default:
405     break;
406   }
407   return LV_InvalidExpression;
408 }
409
410 /// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
411 /// does not have an incomplete type, does not have a const-qualified type, and
412 /// if it is a structure or union, does not have any member (including, 
413 /// recursively, any member or element of all contained aggregates or unions)
414 /// with a const-qualified type.
415 Expr::isModifiableLvalueResult Expr::isModifiableLvalue() const {
416   isLvalueResult lvalResult = isLvalue();
417     
418   switch (lvalResult) {
419   case LV_Valid: break;
420   case LV_NotObjectType: return MLV_NotObjectType;
421   case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
422   case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
423   case LV_InvalidExpression: return MLV_InvalidExpression;
424   }
425   if (TR.isConstQualified())
426     return MLV_ConstQualified;
427   if (TR->isArrayType())
428     return MLV_ArrayType;
429   if (TR->isIncompleteType())
430     return MLV_IncompleteType;
431     
432   if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) {
433     if (r->hasConstFields()) 
434       return MLV_ConstQualified;
435   }
436   return MLV_Valid;    
437 }
438
439 /// hasStaticStorage - Return true if this expression has static storage
440 /// duration.  This means that the address of this expression is a link-time
441 /// constant.
442 bool Expr::hasStaticStorage() const {
443   switch (getStmtClass()) {
444   default:
445     return false;
446   case ParenExprClass:
447     return cast<ParenExpr>(this)->getSubExpr()->hasStaticStorage();
448   case ImplicitCastExprClass:
449     return cast<ImplicitCastExpr>(this)->getSubExpr()->hasStaticStorage();
450   case CompoundLiteralExprClass:
451     return cast<CompoundLiteralExpr>(this)->isFileScope();
452   case DeclRefExprClass: {
453     const Decl *D = cast<DeclRefExpr>(this)->getDecl();
454     if (const VarDecl *VD = dyn_cast<VarDecl>(D))
455       return VD->hasStaticStorage();
456     return false;
457   }
458   case MemberExprClass: {
459     const MemberExpr *M = cast<MemberExpr>(this);
460     return !M->isArrow() && M->getBase()->hasStaticStorage();
461   }
462   case ArraySubscriptExprClass:
463     return cast<ArraySubscriptExpr>(this)->getBase()->hasStaticStorage();
464   case PreDefinedExprClass:
465     return true;
466   }
467 }
468
469 Expr* Expr::IgnoreParens() {
470   Expr* E = this;
471   while (ParenExpr* P = dyn_cast<ParenExpr>(E))
472     E = P->getSubExpr();
473   
474   return E;
475 }
476
477 /// IgnoreParenCasts - Ignore parentheses and casts.  Strip off any ParenExpr
478 /// or CastExprs or ImplicitCastExprs, returning their operand.
479 Expr *Expr::IgnoreParenCasts() {
480   Expr *E = this;
481   while (true) {
482     if (ParenExpr *P = dyn_cast<ParenExpr>(E))
483       E = P->getSubExpr();
484     else if (CastExpr *P = dyn_cast<CastExpr>(E))
485       E = P->getSubExpr();
486     else if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E))
487       E = P->getSubExpr();
488     else
489       return E;
490   }
491 }
492
493
494 bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
495   switch (getStmtClass()) {
496   default:
497     if (Loc) *Loc = getLocStart();
498     return false;
499   case ParenExprClass:
500     return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(Ctx, Loc);
501   case StringLiteralClass:
502   case ObjCStringLiteralClass:
503   case FloatingLiteralClass:
504   case IntegerLiteralClass:
505   case CharacterLiteralClass:
506   case ImaginaryLiteralClass:
507   case TypesCompatibleExprClass:
508   case CXXBoolLiteralExprClass:
509     return true;
510   case CallExprClass: {
511     const CallExpr *CE = cast<CallExpr>(this);
512     llvm::APSInt Result(32);
513     Result.zextOrTrunc(
514       static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
515     if (CE->isBuiltinClassifyType(Result))
516       return true;
517     if (CE->isBuiltinConstantExpr())
518       return true;
519     if (Loc) *Loc = getLocStart();
520     return false;
521   }
522   case DeclRefExprClass: {
523     const Decl *D = cast<DeclRefExpr>(this)->getDecl();
524     // Accept address of function.
525     if (isa<EnumConstantDecl>(D) || isa<FunctionDecl>(D))
526       return true;
527     if (Loc) *Loc = getLocStart();
528     if (isa<VarDecl>(D))
529       return TR->isArrayType();
530     return false;
531   }
532   case CompoundLiteralExprClass:
533     if (Loc) *Loc = getLocStart();
534     // Allow "(int []){2,4}", since the array will be converted to a pointer.
535     // Allow "(vector type){2,4}" since the elements are all constant.
536     return TR->isArrayType() || TR->isVectorType();
537   case UnaryOperatorClass: {
538     const UnaryOperator *Exp = cast<UnaryOperator>(this);
539     
540     // C99 6.6p9
541     if (Exp->getOpcode() == UnaryOperator::AddrOf) {
542       if (!Exp->getSubExpr()->hasStaticStorage()) {
543         if (Loc) *Loc = getLocStart();
544         return false;
545       }
546       return true;
547     }
548
549     // Get the operand value.  If this is sizeof/alignof, do not evalute the
550     // operand.  This affects C99 6.6p3.
551     if (!Exp->isSizeOfAlignOfOp() && 
552         Exp->getOpcode() != UnaryOperator::OffsetOf &&
553         !Exp->getSubExpr()->isConstantExpr(Ctx, Loc))
554       return false;
555   
556     switch (Exp->getOpcode()) {
557     // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
558     // See C99 6.6p3.
559     default:
560       if (Loc) *Loc = Exp->getOperatorLoc();
561       return false;
562     case UnaryOperator::Extension:
563       return true;  // FIXME: this is wrong.
564     case UnaryOperator::SizeOf:
565     case UnaryOperator::AlignOf:
566     case UnaryOperator::OffsetOf:
567       // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
568       if (!Exp->getSubExpr()->getType()->isConstantSizeType()) {
569         if (Loc) *Loc = Exp->getOperatorLoc();
570         return false;
571       }
572       return true;
573     case UnaryOperator::LNot:
574     case UnaryOperator::Plus:
575     case UnaryOperator::Minus:
576     case UnaryOperator::Not:
577       return true;
578     }
579   }
580   case SizeOfAlignOfTypeExprClass: {
581     const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
582     // alignof always evaluates to a constant.
583     if (Exp->isSizeOf() && !Exp->getArgumentType()->isVoidType() &&
584         !Exp->getArgumentType()->isConstantSizeType()) {
585       if (Loc) *Loc = Exp->getOperatorLoc();
586       return false;
587     }
588     return true;
589   }
590   case BinaryOperatorClass: {
591     const BinaryOperator *Exp = cast<BinaryOperator>(this);
592     
593     // The LHS of a constant expr is always evaluated and needed.
594     if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
595       return false;
596
597     if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
598       return false;
599     return true;
600   }
601   case ImplicitCastExprClass:
602   case CastExprClass: {
603     const Expr *SubExpr;
604     SourceLocation CastLoc;
605     if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
606       SubExpr = C->getSubExpr();
607       CastLoc = C->getLParenLoc();
608     } else {
609       SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
610       CastLoc = getLocStart();
611     }
612     if (!SubExpr->isConstantExpr(Ctx, Loc)) {
613       if (Loc) *Loc = SubExpr->getLocStart();
614       return false;
615     }
616     return true;
617   }
618   case ConditionalOperatorClass: {
619     const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
620     if (!Exp->getCond()->isConstantExpr(Ctx, Loc) ||
621         // Handle the GNU extension for missing LHS.
622         !(Exp->getLHS() && Exp->getLHS()->isConstantExpr(Ctx, Loc)) ||
623         !Exp->getRHS()->isConstantExpr(Ctx, Loc))
624       return false;
625     return true;
626   }
627   case InitListExprClass: {
628     const InitListExpr *Exp = cast<InitListExpr>(this);
629     unsigned numInits = Exp->getNumInits();
630     for (unsigned i = 0; i < numInits; i++) {
631       if (!Exp->getInit(i)->isConstantExpr(Ctx, Loc)) {
632         if (Loc) *Loc = Exp->getInit(i)->getLocStart();
633         return false;
634       }
635     }
636     return true;
637   }
638   }
639 }
640
641 /// isIntegerConstantExpr - this recursive routine will test if an expression is
642 /// an integer constant expression. Note: With the introduction of VLA's in
643 /// C99 the result of the sizeof operator is no longer always a constant
644 /// expression. The generalization of the wording to include any subexpression
645 /// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
646 /// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
647 /// "0 || f()" can be treated as a constant expression. In C90 this expression,
648 /// occurring in a context requiring a constant, would have been a constraint
649 /// violation. FIXME: This routine currently implements C90 semantics.
650 /// To properly implement C99 semantics this routine will need to evaluate
651 /// expressions involving operators previously mentioned.
652
653 /// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
654 /// comma, etc
655 ///
656 /// FIXME: This should ext-warn on overflow during evaluation!  ISO C does not
657 /// permit this.  This includes things like (int)1e1000
658 ///
659 /// FIXME: Handle offsetof.  Two things to do:  Handle GCC's __builtin_offsetof
660 /// to support gcc 4.0+  and handle the idiom GCC recognizes with a null pointer
661 /// cast+dereference.
662 bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
663                                  SourceLocation *Loc, bool isEvaluated) const {
664   switch (getStmtClass()) {
665   default:
666     if (Loc) *Loc = getLocStart();
667     return false;
668   case ParenExprClass:
669     return cast<ParenExpr>(this)->getSubExpr()->
670                      isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
671   case IntegerLiteralClass:
672     Result = cast<IntegerLiteral>(this)->getValue();
673     break;
674   case CharacterLiteralClass: {
675     const CharacterLiteral *CL = cast<CharacterLiteral>(this);
676     Result.zextOrTrunc(
677       static_cast<uint32_t>(Ctx.getTypeSize(getType(), CL->getLoc())));
678     Result = CL->getValue();
679     Result.setIsUnsigned(!getType()->isSignedIntegerType());
680     break;
681   }
682   case TypesCompatibleExprClass: {
683     const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
684     Result.zextOrTrunc(
685       static_cast<uint32_t>(Ctx.getTypeSize(getType(), TCE->getLocStart())));
686     Result = Ctx.typesAreCompatible(TCE->getArgType1(), TCE->getArgType2());
687     break;
688   }
689   case CallExprClass: {
690     const CallExpr *CE = cast<CallExpr>(this);
691     Result.zextOrTrunc(
692       static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
693     if (CE->isBuiltinClassifyType(Result))
694       break;
695     if (Loc) *Loc = getLocStart();
696     return false;
697   }
698   case DeclRefExprClass:
699     if (const EnumConstantDecl *D = 
700           dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
701       Result = D->getInitVal();
702       break;
703     }
704     if (Loc) *Loc = getLocStart();
705     return false;
706   case UnaryOperatorClass: {
707     const UnaryOperator *Exp = cast<UnaryOperator>(this);
708     
709     // Get the operand value.  If this is sizeof/alignof, do not evalute the
710     // operand.  This affects C99 6.6p3.
711     if (!Exp->isSizeOfAlignOfOp() && !Exp->isOffsetOfOp() &&
712         !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated))
713       return false;
714
715     switch (Exp->getOpcode()) {
716     // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
717     // See C99 6.6p3.
718     default:
719       if (Loc) *Loc = Exp->getOperatorLoc();
720       return false;
721     case UnaryOperator::Extension:
722       return true;  // FIXME: this is wrong.
723     case UnaryOperator::SizeOf:
724     case UnaryOperator::AlignOf:
725       // Return the result in the right width.
726       Result.zextOrTrunc(
727                          static_cast<uint32_t>(Ctx.getTypeSize(getType(),
728                                                        Exp->getOperatorLoc())));
729         
730       // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
731       if (Exp->getSubExpr()->getType()->isVoidType()) {
732         Result = 1;
733         break;
734       }
735         
736       // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
737       if (!Exp->getSubExpr()->getType()->isConstantSizeType()) {
738         if (Loc) *Loc = Exp->getOperatorLoc();
739         return false;
740       }
741       
742       // Get information about the size or align.
743       if (Exp->getSubExpr()->getType()->isFunctionType()) {
744         // GCC extension: sizeof(function) = 1.
745         Result = Exp->getOpcode() == UnaryOperator::AlignOf ? 4 : 1;
746       } else {
747         unsigned CharSize = 
748           Ctx.Target.getCharWidth(Ctx.getFullLoc(Exp->getOperatorLoc()));
749         
750         if (Exp->getOpcode() == UnaryOperator::AlignOf)
751           Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType(),
752                                     Exp->getOperatorLoc()) / CharSize;
753         else
754           Result = Ctx.getTypeSize(Exp->getSubExpr()->getType(),
755                                    Exp->getOperatorLoc()) / CharSize;
756       }
757       break;
758     case UnaryOperator::LNot: {
759       bool Val = Result == 0;
760       Result.zextOrTrunc(
761         static_cast<uint32_t>(Ctx.getTypeSize(getType(),
762                                               Exp->getOperatorLoc())));
763       Result = Val;
764       break;
765     }
766     case UnaryOperator::Plus:
767       break;
768     case UnaryOperator::Minus:
769       Result = -Result;
770       break;
771     case UnaryOperator::Not:
772       Result = ~Result;
773       break;
774     case UnaryOperator::OffsetOf:
775       Result = Exp->evaluateOffsetOf(Ctx);
776     }
777     break;
778   }
779   case SizeOfAlignOfTypeExprClass: {
780     const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
781     
782     // Return the result in the right width.
783     Result.zextOrTrunc(
784       static_cast<uint32_t>(Ctx.getTypeSize(getType(), Exp->getOperatorLoc())));
785     
786     // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
787     if (Exp->getArgumentType()->isVoidType()) {
788       Result = 1;
789       break;
790     }
791     
792     // alignof always evaluates to a constant, sizeof does if arg is not VLA.
793     if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType()) {
794       if (Loc) *Loc = Exp->getOperatorLoc();
795       return false;
796     }
797
798     // Get information about the size or align.
799     if (Exp->getArgumentType()->isFunctionType()) {
800       // GCC extension: sizeof(function) = 1.
801       Result = Exp->isSizeOf() ? 1 : 4;
802     } else { 
803       unsigned CharSize =
804         Ctx.Target.getCharWidth(Ctx.getFullLoc(Exp->getOperatorLoc()));
805       
806       if (Exp->isSizeOf())
807         Result = Ctx.getTypeSize(Exp->getArgumentType(),
808                                  Exp->getOperatorLoc()) / CharSize;
809       else
810         Result = Ctx.getTypeAlign(Exp->getArgumentType(), 
811                                   Exp->getOperatorLoc()) / CharSize;
812     }
813
814     break;
815   }
816   case BinaryOperatorClass: {
817     const BinaryOperator *Exp = cast<BinaryOperator>(this);
818     
819     // The LHS of a constant expr is always evaluated and needed.
820     if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
821       return false;
822     
823     llvm::APSInt RHS(Result);
824     
825     // The short-circuiting &&/|| operators don't necessarily evaluate their
826     // RHS.  Make sure to pass isEvaluated down correctly.
827     if (Exp->isLogicalOp()) {
828       bool RHSEval;
829       if (Exp->getOpcode() == BinaryOperator::LAnd)
830         RHSEval = Result != 0;
831       else {
832         assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
833         RHSEval = Result == 0;
834       }
835       
836       if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
837                                                 isEvaluated & RHSEval))
838         return false;
839     } else {
840       if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
841         return false;
842     }
843     
844     switch (Exp->getOpcode()) {
845     default:
846       if (Loc) *Loc = getLocStart();
847       return false;
848     case BinaryOperator::Mul:
849       Result *= RHS;
850       break;
851     case BinaryOperator::Div:
852       if (RHS == 0) {
853         if (!isEvaluated) break;
854         if (Loc) *Loc = getLocStart();
855         return false;
856       }
857       Result /= RHS;
858       break;
859     case BinaryOperator::Rem:
860       if (RHS == 0) {
861         if (!isEvaluated) break;
862         if (Loc) *Loc = getLocStart();
863         return false;
864       }
865       Result %= RHS;
866       break;
867     case BinaryOperator::Add: Result += RHS; break;
868     case BinaryOperator::Sub: Result -= RHS; break;
869     case BinaryOperator::Shl:
870       Result <<= 
871         static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
872       break;
873     case BinaryOperator::Shr:
874       Result >>= 
875         static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
876       break;
877     case BinaryOperator::LT:  Result = Result < RHS; break;
878     case BinaryOperator::GT:  Result = Result > RHS; break;
879     case BinaryOperator::LE:  Result = Result <= RHS; break;
880     case BinaryOperator::GE:  Result = Result >= RHS; break;
881     case BinaryOperator::EQ:  Result = Result == RHS; break;
882     case BinaryOperator::NE:  Result = Result != RHS; break;
883     case BinaryOperator::And: Result &= RHS; break;
884     case BinaryOperator::Xor: Result ^= RHS; break;
885     case BinaryOperator::Or:  Result |= RHS; break;
886     case BinaryOperator::LAnd:
887       Result = Result != 0 && RHS != 0;
888       break;
889     case BinaryOperator::LOr:
890       Result = Result != 0 || RHS != 0;
891       break;
892       
893     case BinaryOperator::Comma:
894       // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
895       // *except* when they are contained within a subexpression that is not
896       // evaluated".  Note that Assignment can never happen due to constraints
897       // on the LHS subexpr, so we don't need to check it here.
898       if (isEvaluated) {
899         if (Loc) *Loc = getLocStart();
900         return false;
901       }
902       
903       // The result of the constant expr is the RHS.
904       Result = RHS;
905       return true;
906     }
907     
908     assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
909     break;
910   }
911   case ImplicitCastExprClass:
912   case CastExprClass: {
913     const Expr *SubExpr;
914     SourceLocation CastLoc;
915     if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
916       SubExpr = C->getSubExpr();
917       CastLoc = C->getLParenLoc();
918     } else {
919       SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
920       CastLoc = getLocStart();
921     }
922     
923     // C99 6.6p6: shall only convert arithmetic types to integer types.
924     if (!SubExpr->getType()->isArithmeticType() ||
925         !getType()->isIntegerType()) {
926       if (Loc) *Loc = SubExpr->getLocStart();
927       return false;
928     }
929
930     uint32_t DestWidth = 
931       static_cast<uint32_t>(Ctx.getTypeSize(getType(), CastLoc));
932     
933     // Handle simple integer->integer casts.
934     if (SubExpr->getType()->isIntegerType()) {
935       if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
936         return false;
937       
938       // Figure out if this is a truncate, extend or noop cast.
939       // If the input is signed, do a sign extend, noop, or truncate.
940       if (getType()->isBooleanType()) {
941         // Conversion to bool compares against zero.
942         Result = Result != 0;
943         Result.zextOrTrunc(DestWidth);
944       } else if (SubExpr->getType()->isSignedIntegerType())
945         Result.sextOrTrunc(DestWidth);
946       else  // If the input is unsigned, do a zero extend, noop, or truncate.
947         Result.zextOrTrunc(DestWidth);
948       break;
949     }
950     
951     // Allow floating constants that are the immediate operands of casts or that
952     // are parenthesized.
953     const Expr *Operand = SubExpr;
954     while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
955       Operand = PE->getSubExpr();
956
957     // If this isn't a floating literal, we can't handle it.
958     const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
959     if (!FL) {
960       if (Loc) *Loc = Operand->getLocStart();
961       return false;
962     }
963
964     // If the destination is boolean, compare against zero.
965     if (getType()->isBooleanType()) {
966       Result = !FL->getValue().isZero();
967       Result.zextOrTrunc(DestWidth);
968       break;
969     }     
970     
971     // Determine whether we are converting to unsigned or signed.
972     bool DestSigned = getType()->isSignedIntegerType();
973
974     // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
975     // be called multiple times per AST.
976     uint64_t Space[4]; 
977     (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
978                                           llvm::APFloat::rmTowardZero);
979     Result = llvm::APInt(DestWidth, 4, Space);
980     break;
981   }
982   case ConditionalOperatorClass: {
983     const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
984     
985     if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
986       return false;
987     
988     const Expr *TrueExp  = Exp->getLHS();
989     const Expr *FalseExp = Exp->getRHS();
990     if (Result == 0) std::swap(TrueExp, FalseExp);
991     
992     // Evaluate the false one first, discard the result.
993     if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
994       return false;
995     // Evalute the true one, capture the result.
996     if (TrueExp && 
997         !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
998       return false;
999     break;
1000   }
1001   }
1002
1003   // Cases that are valid constant exprs fall through to here.
1004   Result.setIsUnsigned(getType()->isUnsignedIntegerType());
1005   return true;
1006 }
1007
1008 /// isNullPointerConstant - C99 6.3.2.3p3 -  Return true if this is either an
1009 /// integer constant expression with the value zero, or if this is one that is
1010 /// cast to void*.
1011 bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
1012   // Strip off a cast to void*, if it exists.
1013   if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
1014     // Check that it is a cast to void*.
1015     if (const PointerType *PT = CE->getType()->getAsPointerType()) {
1016       QualType Pointee = PT->getPointeeType();
1017       if (Pointee.getCVRQualifiers() == 0 && 
1018           Pointee->isVoidType() &&                                 // to void*
1019           CE->getSubExpr()->getType()->isIntegerType())            // from int.
1020         return CE->getSubExpr()->isNullPointerConstant(Ctx);
1021     }
1022   } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1023     // Ignore the ImplicitCastExpr type entirely.
1024     return ICE->getSubExpr()->isNullPointerConstant(Ctx);
1025   } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1026     // Accept ((void*)0) as a null pointer constant, as many other
1027     // implementations do.
1028     return PE->getSubExpr()->isNullPointerConstant(Ctx);
1029   }
1030   
1031   // This expression must be an integer type.
1032   if (!getType()->isIntegerType())
1033     return false;
1034   
1035   // If we have an integer constant expression, we need to *evaluate* it and
1036   // test for the value 0.
1037   llvm::APSInt Val(32);
1038   return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
1039 }
1040
1041 unsigned OCUVectorElementExpr::getNumElements() const {
1042   return strlen(Accessor.getName());
1043 }
1044
1045
1046 /// getComponentType - Determine whether the components of this access are
1047 /// "point" "color" or "texture" elements.
1048 OCUVectorElementExpr::ElementType 
1049 OCUVectorElementExpr::getElementType() const {
1050   // derive the component type, no need to waste space.
1051   const char *compStr = Accessor.getName();
1052   
1053   if (OCUVectorType::getPointAccessorIdx(*compStr) != -1) return Point;
1054   if (OCUVectorType::getColorAccessorIdx(*compStr) != -1) return Color;
1055   
1056   assert(OCUVectorType::getTextureAccessorIdx(*compStr) != -1 &&
1057          "getComponentType(): Illegal accessor");
1058   return Texture;
1059 }
1060
1061 /// containsDuplicateElements - Return true if any element access is
1062 /// repeated.
1063 bool OCUVectorElementExpr::containsDuplicateElements() const {
1064   const char *compStr = Accessor.getName();
1065   unsigned length = strlen(compStr);
1066   
1067   for (unsigned i = 0; i < length-1; i++) {
1068     const char *s = compStr+i;
1069     for (const char c = *s++; *s; s++)
1070       if (c == *s) 
1071         return true;
1072   }
1073   return false;
1074 }
1075
1076 /// getEncodedElementAccess - We encode fields with two bits per component.
1077 unsigned OCUVectorElementExpr::getEncodedElementAccess() const {
1078   const char *compStr = Accessor.getName();
1079   unsigned length = getNumElements();
1080
1081   unsigned Result = 0;
1082   
1083   while (length--) {
1084     Result <<= 2;
1085     int Idx = OCUVectorType::getAccessorIdx(compStr[length]);
1086     assert(Idx != -1 && "Invalid accessor letter");
1087     Result |= Idx;
1088   }
1089   return Result;
1090 }
1091
1092 // constructor for instance messages.
1093 ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
1094                 QualType retType, ObjCMethodDecl *mproto,
1095                 SourceLocation LBrac, SourceLocation RBrac,
1096                 Expr **ArgExprs, unsigned nargs)
1097   : Expr(ObjCMessageExprClass, retType), SelName(selInfo), 
1098     MethodProto(mproto), ClassName(0) {
1099   NumArgs = nargs;
1100   SubExprs = new Expr*[NumArgs+1];
1101   SubExprs[RECEIVER] = receiver;
1102   if (NumArgs) {
1103     for (unsigned i = 0; i != NumArgs; ++i)
1104       SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1105   }
1106   LBracloc = LBrac;
1107   RBracloc = RBrac;
1108 }
1109
1110 // constructor for class messages. 
1111 // FIXME: clsName should be typed to ObjCInterfaceType
1112 ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
1113                 QualType retType, ObjCMethodDecl *mproto,
1114                 SourceLocation LBrac, SourceLocation RBrac,
1115                 Expr **ArgExprs, unsigned nargs)
1116   : Expr(ObjCMessageExprClass, retType), SelName(selInfo), 
1117     MethodProto(mproto), ClassName(clsName) {
1118   NumArgs = nargs;
1119   SubExprs = new Expr*[NumArgs+1];
1120   SubExprs[RECEIVER] = 0;
1121   if (NumArgs) {
1122     for (unsigned i = 0; i != NumArgs; ++i)
1123       SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1124   }
1125   LBracloc = LBrac;
1126   RBracloc = RBrac;
1127 }
1128
1129
1130 bool ChooseExpr::isConditionTrue(ASTContext &C) const {
1131   llvm::APSInt CondVal(32);
1132   bool IsConst = getCond()->isIntegerConstantExpr(CondVal, C);
1133   assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst;
1134   return CondVal != 0;
1135 }
1136
1137 static int64_t evaluateOffsetOf(ASTContext& C, const Expr *E)
1138 {
1139   if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
1140     QualType Ty = ME->getBase()->getType();
1141     
1142     RecordDecl *RD = Ty->getAsRecordType()->getDecl();
1143     const ASTRecordLayout &RL = C.getASTRecordLayout(RD, SourceLocation());
1144     FieldDecl *FD = ME->getMemberDecl();
1145     
1146     // FIXME: This is linear time.
1147     unsigned i = 0, e = 0;
1148     for (i = 0, e = RD->getNumMembers(); i != e; i++) {
1149       if (RD->getMember(i) == FD)
1150         break;
1151     }
1152     
1153     return RL.getFieldOffset(i) + evaluateOffsetOf(C, ME->getBase());
1154   } else if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
1155     const Expr *Base = ASE->getBase();
1156     llvm::APSInt Idx(32);
1157     bool ICE = ASE->getIdx()->isIntegerConstantExpr(Idx, C);
1158     assert(ICE && "Array index is not a constant integer!");
1159     
1160     int64_t size = C.getTypeSize(ASE->getType(), SourceLocation());
1161     size *= Idx.getSExtValue();
1162     
1163     return size + evaluateOffsetOf(C, Base);
1164   } else if (isa<CompoundLiteralExpr>(E))
1165     return 0;  
1166
1167   assert(0 && "Unknown offsetof subexpression!");
1168   return 0;
1169 }
1170
1171 int64_t UnaryOperator::evaluateOffsetOf(ASTContext& C) const
1172 {
1173   assert(Opc == OffsetOf && "Unary operator not offsetof!");
1174   
1175   unsigned CharSize = 
1176     C.Target.getCharWidth(C.getFullLoc(getOperatorLoc()));
1177   
1178   return ::evaluateOffsetOf(C, Val) / CharSize;
1179 }
1180
1181 //===----------------------------------------------------------------------===//
1182 //  Child Iterators for iterating over subexpressions/substatements
1183 //===----------------------------------------------------------------------===//
1184
1185 // DeclRefExpr
1186 Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1187 Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
1188
1189 // ObjCIvarRefExpr
1190 Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return child_iterator(); }
1191 Stmt::child_iterator ObjCIvarRefExpr::child_end() { return child_iterator(); }
1192
1193 // PreDefinedExpr
1194 Stmt::child_iterator PreDefinedExpr::child_begin() { return child_iterator(); }
1195 Stmt::child_iterator PreDefinedExpr::child_end() { return child_iterator(); }
1196
1197 // IntegerLiteral
1198 Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1199 Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
1200
1201 // CharacterLiteral
1202 Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); }
1203 Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
1204
1205 // FloatingLiteral
1206 Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1207 Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
1208
1209 // ImaginaryLiteral
1210 Stmt::child_iterator ImaginaryLiteral::child_begin() {
1211   return reinterpret_cast<Stmt**>(&Val);
1212 }
1213 Stmt::child_iterator ImaginaryLiteral::child_end() {
1214   return reinterpret_cast<Stmt**>(&Val)+1;
1215 }
1216
1217 // StringLiteral
1218 Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1219 Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
1220
1221 // ParenExpr
1222 Stmt::child_iterator ParenExpr::child_begin() {
1223   return reinterpret_cast<Stmt**>(&Val);
1224 }
1225 Stmt::child_iterator ParenExpr::child_end() {
1226   return reinterpret_cast<Stmt**>(&Val)+1;
1227 }
1228
1229 // UnaryOperator
1230 Stmt::child_iterator UnaryOperator::child_begin() {
1231   return reinterpret_cast<Stmt**>(&Val);
1232 }
1233 Stmt::child_iterator UnaryOperator::child_end() {
1234   return reinterpret_cast<Stmt**>(&Val+1);
1235 }
1236
1237 // SizeOfAlignOfTypeExpr
1238 Stmt::child_iterator SizeOfAlignOfTypeExpr::child_begin() { 
1239   // If the type is a VLA type (and not a typedef), the size expression of the
1240   // VLA needs to be treated as an executable expression.
1241   if (VariableArrayType* T = dyn_cast<VariableArrayType>(Ty.getTypePtr()))
1242     return child_iterator(T);
1243   else
1244     return child_iterator(); 
1245 }
1246 Stmt::child_iterator SizeOfAlignOfTypeExpr::child_end() {
1247   return child_iterator(); 
1248 }
1249
1250 // ArraySubscriptExpr
1251 Stmt::child_iterator ArraySubscriptExpr::child_begin() {
1252   return reinterpret_cast<Stmt**>(&SubExprs);
1253 }
1254 Stmt::child_iterator ArraySubscriptExpr::child_end() {
1255   return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
1256 }
1257
1258 // CallExpr
1259 Stmt::child_iterator CallExpr::child_begin() {
1260   return reinterpret_cast<Stmt**>(&SubExprs[0]);
1261 }
1262 Stmt::child_iterator CallExpr::child_end() {
1263   return reinterpret_cast<Stmt**>(&SubExprs[NumArgs+ARGS_START]);
1264 }
1265
1266 // MemberExpr
1267 Stmt::child_iterator MemberExpr::child_begin() {
1268   return reinterpret_cast<Stmt**>(&Base);
1269 }
1270 Stmt::child_iterator MemberExpr::child_end() {
1271   return reinterpret_cast<Stmt**>(&Base)+1;
1272 }
1273
1274 // OCUVectorElementExpr
1275 Stmt::child_iterator OCUVectorElementExpr::child_begin() {
1276   return reinterpret_cast<Stmt**>(&Base);
1277 }
1278 Stmt::child_iterator OCUVectorElementExpr::child_end() {
1279   return reinterpret_cast<Stmt**>(&Base)+1;
1280 }
1281
1282 // CompoundLiteralExpr
1283 Stmt::child_iterator CompoundLiteralExpr::child_begin() {
1284   return reinterpret_cast<Stmt**>(&Init);
1285 }
1286 Stmt::child_iterator CompoundLiteralExpr::child_end() {
1287   return reinterpret_cast<Stmt**>(&Init)+1;
1288 }
1289
1290 // ImplicitCastExpr
1291 Stmt::child_iterator ImplicitCastExpr::child_begin() {
1292   return reinterpret_cast<Stmt**>(&Op);
1293 }
1294 Stmt::child_iterator ImplicitCastExpr::child_end() {
1295   return reinterpret_cast<Stmt**>(&Op)+1;
1296 }
1297
1298 // CastExpr
1299 Stmt::child_iterator CastExpr::child_begin() {
1300   return reinterpret_cast<Stmt**>(&Op);
1301 }
1302 Stmt::child_iterator CastExpr::child_end() {
1303   return reinterpret_cast<Stmt**>(&Op)+1;
1304 }
1305
1306 // BinaryOperator
1307 Stmt::child_iterator BinaryOperator::child_begin() {
1308   return reinterpret_cast<Stmt**>(&SubExprs);
1309 }
1310 Stmt::child_iterator BinaryOperator::child_end() {
1311   return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
1312 }
1313
1314 // ConditionalOperator
1315 Stmt::child_iterator ConditionalOperator::child_begin() {
1316   return reinterpret_cast<Stmt**>(&SubExprs);
1317 }
1318 Stmt::child_iterator ConditionalOperator::child_end() {
1319   return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
1320 }
1321
1322 // AddrLabelExpr
1323 Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1324 Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
1325
1326 // StmtExpr
1327 Stmt::child_iterator StmtExpr::child_begin() {
1328   return reinterpret_cast<Stmt**>(&SubStmt);
1329 }
1330 Stmt::child_iterator StmtExpr::child_end() {
1331   return reinterpret_cast<Stmt**>(&SubStmt)+1;
1332 }
1333
1334 // TypesCompatibleExpr
1335 Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1336   return child_iterator();
1337 }
1338
1339 Stmt::child_iterator TypesCompatibleExpr::child_end() {
1340   return child_iterator();
1341 }
1342
1343 // ChooseExpr
1344 Stmt::child_iterator ChooseExpr::child_begin() {
1345   return reinterpret_cast<Stmt**>(&SubExprs);
1346 }
1347
1348 Stmt::child_iterator ChooseExpr::child_end() {
1349   return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
1350 }
1351
1352 // OverloadExpr
1353 Stmt::child_iterator OverloadExpr::child_begin() {
1354   return reinterpret_cast<Stmt**>(&SubExprs[0]);
1355 }
1356 Stmt::child_iterator OverloadExpr::child_end() {
1357   return reinterpret_cast<Stmt**>(&SubExprs[NumExprs]);
1358 }
1359
1360 // VAArgExpr
1361 Stmt::child_iterator VAArgExpr::child_begin() {
1362   return reinterpret_cast<Stmt**>(&Val);
1363 }
1364
1365 Stmt::child_iterator VAArgExpr::child_end() {
1366   return reinterpret_cast<Stmt**>(&Val)+1;
1367 }
1368
1369 // InitListExpr
1370 Stmt::child_iterator InitListExpr::child_begin() {
1371   return reinterpret_cast<Stmt**>(&InitExprs[0]);
1372 }
1373 Stmt::child_iterator InitListExpr::child_end() {
1374   return reinterpret_cast<Stmt**>(&InitExprs[NumInits]);
1375 }
1376
1377 // ObjCStringLiteral
1378 Stmt::child_iterator ObjCStringLiteral::child_begin() { 
1379   return child_iterator();
1380 }
1381 Stmt::child_iterator ObjCStringLiteral::child_end() {
1382   return child_iterator();
1383 }
1384
1385 // ObjCEncodeExpr
1386 Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1387 Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
1388
1389 // ObjCSelectorExpr
1390 Stmt::child_iterator ObjCSelectorExpr::child_begin() { 
1391   return child_iterator();
1392 }
1393 Stmt::child_iterator ObjCSelectorExpr::child_end() {
1394   return child_iterator();
1395 }
1396
1397 // ObjCProtocolExpr
1398 Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1399   return child_iterator();
1400 }
1401 Stmt::child_iterator ObjCProtocolExpr::child_end() {
1402   return child_iterator();
1403 }
1404
1405 // ObjCMessageExpr
1406 Stmt::child_iterator ObjCMessageExpr::child_begin() {
1407   return reinterpret_cast<Stmt**>(&SubExprs[0]);
1408 }
1409 Stmt::child_iterator ObjCMessageExpr::child_end() {
1410   return reinterpret_cast<Stmt**>(&SubExprs[getNumArgs()+ARGS_START]);
1411 }
1412