Expr.h revision d38617c8a50f9729c254ab76cd359af797c6739b
1//===--- Expr.h - Classes for representing expressions ----------*- C++ -*-===//
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 defines the Expr interface and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_AST_EXPR_H
15#define LLVM_CLANG_AST_EXPR_H
16
17#include "clang/AST/Stmt.h"
18#include "clang/AST/Type.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclObjC.h"
21#include "clang/Basic/IdentifierTable.h"
22#include "llvm/ADT/APSInt.h"
23#include "llvm/ADT/APFloat.h"
24#include "llvm/ADT/SmallVector.h"
25#include <vector>
26
27namespace clang {
28  class IdentifierInfo;
29  class Selector;
30  class Decl;
31  class ASTContext;
32
33/// Expr - This represents one expression.  Note that Expr's are subclasses of
34/// Stmt.  This allows an expression to be transparently used any place a Stmt
35/// is required.
36///
37class Expr : public Stmt {
38  QualType TR;
39protected:
40  Expr(StmtClass SC, QualType T) : Stmt(SC), TR(T) {}
41public:
42  QualType getType() const { return TR; }
43  void setType(QualType t) { TR = t; }
44
45  /// SourceLocation tokens are not useful in isolation - they are low level
46  /// value objects created/interpreted by SourceManager. We assume AST
47  /// clients will have a pointer to the respective SourceManager.
48  virtual SourceRange getSourceRange() const = 0;
49
50  /// getExprLoc - Return the preferred location for the arrow when diagnosing
51  /// a problem with a generic expression.
52  virtual SourceLocation getExprLoc() const { return getLocStart(); }
53
54  /// hasLocalSideEffect - Return true if this immediate expression has side
55  /// effects, not counting any sub-expressions.
56  bool hasLocalSideEffect() const;
57
58  /// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or
59  /// incomplete type other than void. Nonarray expressions that can be lvalues:
60  ///  - name, where name must be a variable
61  ///  - e[i]
62  ///  - (e), where e must be an lvalue
63  ///  - e.name, where e must be an lvalue
64  ///  - e->name
65  ///  - *e, the type of e cannot be a function type
66  ///  - string-constant
67  ///  - reference type [C++ [expr]]
68  ///
69  enum isLvalueResult {
70    LV_Valid,
71    LV_NotObjectType,
72    LV_IncompleteVoidType,
73    LV_DuplicateVectorComponents,
74    LV_InvalidExpression
75  };
76  isLvalueResult isLvalue() const;
77
78  /// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
79  /// does not have an incomplete type, does not have a const-qualified type,
80  /// and if it is a structure or union, does not have any member (including,
81  /// recursively, any member or element of all contained aggregates or unions)
82  /// with a const-qualified type.
83  enum isModifiableLvalueResult {
84    MLV_Valid,
85    MLV_NotObjectType,
86    MLV_IncompleteVoidType,
87    MLV_DuplicateVectorComponents,
88    MLV_InvalidExpression,
89    MLV_IncompleteType,
90    MLV_ConstQualified,
91    MLV_ArrayType
92  };
93  isModifiableLvalueResult isModifiableLvalue() const;
94
95  bool isNullPointerConstant(ASTContext &Ctx) const;
96
97  /// isIntegerConstantExpr - Return true if this expression is a valid integer
98  /// constant expression, and, if so, return its value in Result.  If not a
99  /// valid i-c-e, return false and fill in Loc (if specified) with the location
100  /// of the invalid expression.
101  bool isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
102                             SourceLocation *Loc = 0,
103                             bool isEvaluated = true) const;
104  bool isIntegerConstantExpr(ASTContext &Ctx, SourceLocation *Loc = 0) const {
105    llvm::APSInt X(32);
106    return isIntegerConstantExpr(X, Ctx, Loc);
107  }
108  /// isConstantExpr - Return true if this expression is a valid constant expr.
109  bool isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const;
110
111  /// hasGlobalStorage - Return true if this expression has static storage
112  /// duration.  This means that the address of this expression is a link-time
113  /// constant.
114  bool hasGlobalStorage() const;
115
116  /// IgnoreParens - Ignore parentheses.  If this Expr is a ParenExpr, return
117  ///  its subexpression.  If that subexpression is also a ParenExpr,
118  ///  then this method recursively returns its subexpression, and so forth.
119  ///  Otherwise, the method returns the current Expr.
120  Expr* IgnoreParens();
121
122  /// IgnoreParenCasts - Ignore parentheses and casts.  Strip off any ParenExpr
123  /// or CastExprs or ImplicitCastExprs, returning their operand.
124  Expr *IgnoreParenCasts();
125
126  const Expr* IgnoreParens() const {
127    return const_cast<Expr*>(this)->IgnoreParens();
128  }
129  const Expr *IgnoreParenCasts() const {
130    return const_cast<Expr*>(this)->IgnoreParenCasts();
131  }
132
133  static bool classof(const Stmt *T) {
134    return T->getStmtClass() >= firstExprConstant &&
135           T->getStmtClass() <= lastExprConstant;
136  }
137  static bool classof(const Expr *) { return true; }
138
139  static inline Expr* Create(llvm::Deserializer& D, ASTContext& C) {
140    return cast<Expr>(Stmt::Create(D, C));
141  }
142};
143
144//===----------------------------------------------------------------------===//
145// Primary Expressions.
146//===----------------------------------------------------------------------===//
147
148/// DeclRefExpr - [C99 6.5.1p2] - A reference to a declared variable, function,
149/// enum, etc.
150class DeclRefExpr : public Expr {
151  ValueDecl *D;
152  SourceLocation Loc;
153public:
154  DeclRefExpr(ValueDecl *d, QualType t, SourceLocation l) :
155    Expr(DeclRefExprClass, t), D(d), Loc(l) {}
156
157  ValueDecl *getDecl() { return D; }
158  const ValueDecl *getDecl() const { return D; }
159  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
160
161
162  static bool classof(const Stmt *T) {
163    return T->getStmtClass() == DeclRefExprClass;
164  }
165  static bool classof(const DeclRefExpr *) { return true; }
166
167  // Iterators
168  virtual child_iterator child_begin();
169  virtual child_iterator child_end();
170
171  virtual void EmitImpl(llvm::Serializer& S) const;
172  static DeclRefExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
173};
174
175/// PreDefinedExpr - [C99 6.4.2.2] - A pre-defined identifier such as __func__.
176class PreDefinedExpr : public Expr {
177public:
178  enum IdentType {
179    Func,
180    Function,
181    PrettyFunction
182  };
183
184private:
185  SourceLocation Loc;
186  IdentType Type;
187public:
188  PreDefinedExpr(SourceLocation l, QualType type, IdentType IT)
189    : Expr(PreDefinedExprClass, type), Loc(l), Type(IT) {}
190
191  IdentType getIdentType() const { return Type; }
192
193  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
194
195  static bool classof(const Stmt *T) {
196    return T->getStmtClass() == PreDefinedExprClass;
197  }
198  static bool classof(const PreDefinedExpr *) { return true; }
199
200  // Iterators
201  virtual child_iterator child_begin();
202  virtual child_iterator child_end();
203
204  virtual void EmitImpl(llvm::Serializer& S) const;
205  static PreDefinedExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
206};
207
208class IntegerLiteral : public Expr {
209  llvm::APInt Value;
210  SourceLocation Loc;
211public:
212  // type should be IntTy, LongTy, LongLongTy, UnsignedIntTy, UnsignedLongTy,
213  // or UnsignedLongLongTy
214  IntegerLiteral(const llvm::APInt &V, QualType type, SourceLocation l)
215    : Expr(IntegerLiteralClass, type), Value(V), Loc(l) {
216    assert(type->isIntegerType() && "Illegal type in IntegerLiteral");
217  }
218  const llvm::APInt &getValue() const { return Value; }
219  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
220
221  static bool classof(const Stmt *T) {
222    return T->getStmtClass() == IntegerLiteralClass;
223  }
224  static bool classof(const IntegerLiteral *) { return true; }
225
226  // Iterators
227  virtual child_iterator child_begin();
228  virtual child_iterator child_end();
229
230  virtual void EmitImpl(llvm::Serializer& S) const;
231  static IntegerLiteral* CreateImpl(llvm::Deserializer& D, ASTContext& C);
232};
233
234class CharacterLiteral : public Expr {
235  unsigned Value;
236  SourceLocation Loc;
237public:
238  // type should be IntTy
239  CharacterLiteral(unsigned value, QualType type, SourceLocation l)
240    : Expr(CharacterLiteralClass, type), Value(value), Loc(l) {
241  }
242  SourceLocation getLoc() const { return Loc; }
243
244  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
245
246  unsigned getValue() const { return Value; }
247
248  static bool classof(const Stmt *T) {
249    return T->getStmtClass() == CharacterLiteralClass;
250  }
251  static bool classof(const CharacterLiteral *) { return true; }
252
253  // Iterators
254  virtual child_iterator child_begin();
255  virtual child_iterator child_end();
256
257  virtual void EmitImpl(llvm::Serializer& S) const;
258  static CharacterLiteral* CreateImpl(llvm::Deserializer& D, ASTContext& C);
259};
260
261class FloatingLiteral : public Expr {
262  llvm::APFloat Value;
263  bool IsExact : 1;
264  SourceLocation Loc;
265public:
266  FloatingLiteral(const llvm::APFloat &V, bool* isexact,
267                  QualType Type, SourceLocation L)
268    : Expr(FloatingLiteralClass, Type), Value(V), IsExact(*isexact), Loc(L) {}
269
270  const llvm::APFloat &getValue() const { return Value; }
271
272  bool isExact() const { return IsExact; }
273
274  /// getValueAsDouble - This returns the value as an inaccurate double.  Note
275  /// that this may cause loss of precision, but is useful for debugging dumps
276  /// etc.
277  double getValueAsDouble() const {
278    // FIXME: We need something for long double here.
279    if (cast<BuiltinType>(getType())->getKind() == BuiltinType::Float)
280      return Value.convertToFloat();
281    else
282      return Value.convertToDouble();
283  }
284
285  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
286
287  static bool classof(const Stmt *T) {
288    return T->getStmtClass() == FloatingLiteralClass;
289  }
290  static bool classof(const FloatingLiteral *) { return true; }
291
292  // Iterators
293  virtual child_iterator child_begin();
294  virtual child_iterator child_end();
295
296  virtual void EmitImpl(llvm::Serializer& S) const;
297  static FloatingLiteral* CreateImpl(llvm::Deserializer& D, ASTContext& C);
298};
299
300/// ImaginaryLiteral - We support imaginary integer and floating point literals,
301/// like "1.0i".  We represent these as a wrapper around FloatingLiteral and
302/// IntegerLiteral classes.  Instances of this class always have a Complex type
303/// whose element type matches the subexpression.
304///
305class ImaginaryLiteral : public Expr {
306  Expr *Val;
307public:
308  ImaginaryLiteral(Expr *val, QualType Ty)
309    : Expr(ImaginaryLiteralClass, Ty), Val(val) {}
310
311  const Expr *getSubExpr() const { return Val; }
312  Expr *getSubExpr() { return Val; }
313
314  virtual SourceRange getSourceRange() const { return Val->getSourceRange(); }
315  static bool classof(const Stmt *T) {
316    return T->getStmtClass() == ImaginaryLiteralClass;
317  }
318  static bool classof(const ImaginaryLiteral *) { return true; }
319
320  // Iterators
321  virtual child_iterator child_begin();
322  virtual child_iterator child_end();
323
324  virtual void EmitImpl(llvm::Serializer& S) const;
325  static ImaginaryLiteral* CreateImpl(llvm::Deserializer& D, ASTContext& C);
326};
327
328/// StringLiteral - This represents a string literal expression, e.g. "foo"
329/// or L"bar" (wide strings).  The actual string is returned by getStrData()
330/// is NOT null-terminated, and the length of the string is determined by
331/// calling getByteLength().  The C type for a string is always a
332/// ConstantArrayType.
333class StringLiteral : public Expr {
334  const char *StrData;
335  unsigned ByteLength;
336  bool IsWide;
337  // if the StringLiteral was composed using token pasting, both locations
338  // are needed. If not (the common case), firstTokLoc == lastTokLoc.
339  // FIXME: if space becomes an issue, we should create a sub-class.
340  SourceLocation firstTokLoc, lastTokLoc;
341public:
342  StringLiteral(const char *strData, unsigned byteLength, bool Wide,
343                QualType t, SourceLocation b, SourceLocation e);
344  virtual ~StringLiteral();
345
346  const char *getStrData() const { return StrData; }
347  unsigned getByteLength() const { return ByteLength; }
348  bool isWide() const { return IsWide; }
349
350  virtual SourceRange getSourceRange() const {
351    return SourceRange(firstTokLoc,lastTokLoc);
352  }
353  static bool classof(const Stmt *T) {
354    return T->getStmtClass() == StringLiteralClass;
355  }
356  static bool classof(const StringLiteral *) { return true; }
357
358  // Iterators
359  virtual child_iterator child_begin();
360  virtual child_iterator child_end();
361
362  virtual void EmitImpl(llvm::Serializer& S) const;
363  static StringLiteral* CreateImpl(llvm::Deserializer& D, ASTContext& C);
364};
365
366/// ParenExpr - This represents a parethesized expression, e.g. "(1)".  This
367/// AST node is only formed if full location information is requested.
368class ParenExpr : public Expr {
369  SourceLocation L, R;
370  Expr *Val;
371public:
372  ParenExpr(SourceLocation l, SourceLocation r, Expr *val)
373    : Expr(ParenExprClass, val->getType()), L(l), R(r), Val(val) {}
374
375  const Expr *getSubExpr() const { return Val; }
376  Expr *getSubExpr() { return Val; }
377  virtual SourceRange getSourceRange() const { return SourceRange(L, R); }
378
379  static bool classof(const Stmt *T) {
380    return T->getStmtClass() == ParenExprClass;
381  }
382  static bool classof(const ParenExpr *) { return true; }
383
384  // Iterators
385  virtual child_iterator child_begin();
386  virtual child_iterator child_end();
387
388  virtual void EmitImpl(llvm::Serializer& S) const;
389  static ParenExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
390};
391
392
393/// UnaryOperator - This represents the unary-expression's (except sizeof of
394/// types), the postinc/postdec operators from postfix-expression, and various
395/// extensions.
396///
397/// Notes on various nodes:
398///
399/// Real/Imag - These return the real/imag part of a complex operand.  If
400///   applied to a non-complex value, the former returns its operand and the
401///   later returns zero in the type of the operand.
402///
403/// __builtin_offsetof(type, a.b[10]) is represented as a unary operator whose
404///   subexpression is a compound literal with the various MemberExpr and
405///   ArraySubscriptExpr's applied to it.
406///
407class UnaryOperator : public Expr {
408public:
409  // Note that additions to this should also update the StmtVisitor class.
410  enum Opcode {
411    PostInc, PostDec, // [C99 6.5.2.4] Postfix increment and decrement operators
412    PreInc, PreDec,   // [C99 6.5.3.1] Prefix increment and decrement operators.
413    AddrOf, Deref,    // [C99 6.5.3.2] Address and indirection operators.
414    Plus, Minus,      // [C99 6.5.3.3] Unary arithmetic operators.
415    Not, LNot,        // [C99 6.5.3.3] Unary arithmetic operators.
416    SizeOf, AlignOf,  // [C99 6.5.3.4] Sizeof (expr, not type) operator.
417    Real, Imag,       // "__real expr"/"__imag expr" Extension.
418    Extension,        // __extension__ marker.
419    OffsetOf          // __builtin_offsetof
420  };
421private:
422  Expr *Val;
423  Opcode Opc;
424  SourceLocation Loc;
425public:
426
427  UnaryOperator(Expr *input, Opcode opc, QualType type, SourceLocation l)
428    : Expr(UnaryOperatorClass, type), Val(input), Opc(opc), Loc(l) {}
429
430  Opcode getOpcode() const { return Opc; }
431  Expr *getSubExpr() const { return Val; }
432
433  /// getOperatorLoc - Return the location of the operator.
434  SourceLocation getOperatorLoc() const { return Loc; }
435
436  /// isPostfix - Return true if this is a postfix operation, like x++.
437  static bool isPostfix(Opcode Op);
438
439  bool isPostfix() const { return isPostfix(Opc); }
440  bool isIncrementOp() const {return Opc==PreInc || Opc==PostInc; }
441  bool isIncrementDecrementOp() const { return Opc>=PostInc && Opc<=PreDec; }
442  bool isSizeOfAlignOfOp() const { return Opc == SizeOf || Opc == AlignOf; }
443  bool isOffsetOfOp() const { return Opc == OffsetOf; }
444  static bool isArithmeticOp(Opcode Op) { return Op >= Plus && Op <= LNot; }
445
446  /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
447  /// corresponds to, e.g. "sizeof" or "[pre]++"
448  static const char *getOpcodeStr(Opcode Op);
449
450  virtual SourceRange getSourceRange() const {
451    if (isPostfix())
452      return SourceRange(Val->getLocStart(), Loc);
453    else
454      return SourceRange(Loc, Val->getLocEnd());
455  }
456  virtual SourceLocation getExprLoc() const { return Loc; }
457
458  static bool classof(const Stmt *T) {
459    return T->getStmtClass() == UnaryOperatorClass;
460  }
461  static bool classof(const UnaryOperator *) { return true; }
462
463  int64_t evaluateOffsetOf(ASTContext& C) const;
464
465  // Iterators
466  virtual child_iterator child_begin();
467  virtual child_iterator child_end();
468
469  virtual void EmitImpl(llvm::Serializer& S) const;
470  static UnaryOperator* CreateImpl(llvm::Deserializer& D, ASTContext& C);
471};
472
473/// SizeOfAlignOfTypeExpr - [C99 6.5.3.4] - This is only for sizeof/alignof of
474/// *types*.  sizeof(expr) is handled by UnaryOperator.
475class SizeOfAlignOfTypeExpr : public Expr {
476  bool isSizeof;  // true if sizeof, false if alignof.
477  QualType Ty;
478  SourceLocation OpLoc, RParenLoc;
479public:
480  SizeOfAlignOfTypeExpr(bool issizeof, QualType argType, QualType resultType,
481                        SourceLocation op, SourceLocation rp) :
482    Expr(SizeOfAlignOfTypeExprClass, resultType),
483    isSizeof(issizeof), Ty(argType), OpLoc(op), RParenLoc(rp) {}
484
485  bool isSizeOf() const { return isSizeof; }
486  QualType getArgumentType() const { return Ty; }
487
488  SourceLocation getOperatorLoc() const { return OpLoc; }
489
490  virtual SourceRange getSourceRange() const {
491    return SourceRange(OpLoc, RParenLoc);
492  }
493
494  static bool classof(const Stmt *T) {
495    return T->getStmtClass() == SizeOfAlignOfTypeExprClass;
496  }
497  static bool classof(const SizeOfAlignOfTypeExpr *) { return true; }
498
499  // Iterators
500  virtual child_iterator child_begin();
501  virtual child_iterator child_end();
502
503  virtual void EmitImpl(llvm::Serializer& S) const;
504  static SizeOfAlignOfTypeExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
505};
506
507//===----------------------------------------------------------------------===//
508// Postfix Operators.
509//===----------------------------------------------------------------------===//
510
511/// ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting.
512class ArraySubscriptExpr : public Expr {
513  enum { LHS, RHS, END_EXPR=2 };
514  Expr* SubExprs[END_EXPR];
515  SourceLocation RBracketLoc;
516public:
517  ArraySubscriptExpr(Expr *lhs, Expr *rhs, QualType t,
518                     SourceLocation rbracketloc)
519  : Expr(ArraySubscriptExprClass, t), RBracketLoc(rbracketloc) {
520    SubExprs[LHS] = lhs;
521    SubExprs[RHS] = rhs;
522  }
523
524  /// An array access can be written A[4] or 4[A] (both are equivalent).
525  /// - getBase() and getIdx() always present the normalized view: A[4].
526  ///    In this case getBase() returns "A" and getIdx() returns "4".
527  /// - getLHS() and getRHS() present the syntactic view. e.g. for
528  ///    4[A] getLHS() returns "4".
529  /// Note: Because vector element access is also written A[4] we must
530  /// predicate the format conversion in getBase and getIdx only on the
531  /// the type of the RHS, as it is possible for the LHS to be a vector of
532  /// integer type
533  Expr *getLHS() { return SubExprs[LHS]; }
534  const Expr *getLHS() const { return SubExprs[LHS]; }
535
536  Expr *getRHS() { return SubExprs[RHS]; }
537  const Expr *getRHS() const { return SubExprs[RHS]; }
538
539  Expr *getBase() {
540    return (getRHS()->getType()->isIntegerType()) ? getLHS() : getRHS();
541  }
542
543  const Expr *getBase() const {
544    return (getRHS()->getType()->isIntegerType()) ? getLHS() : getRHS();
545  }
546
547  Expr *getIdx() {
548    return (getRHS()->getType()->isIntegerType()) ? getRHS() : getLHS();
549  }
550
551  const Expr *getIdx() const {
552    return (getRHS()->getType()->isIntegerType()) ? getRHS() : getLHS();
553  }
554
555  virtual SourceRange getSourceRange() const {
556    return SourceRange(getLHS()->getLocStart(), RBracketLoc);
557  }
558
559  virtual SourceLocation getExprLoc() const { return RBracketLoc; }
560
561  static bool classof(const Stmt *T) {
562    return T->getStmtClass() == ArraySubscriptExprClass;
563  }
564  static bool classof(const ArraySubscriptExpr *) { return true; }
565
566  // Iterators
567  virtual child_iterator child_begin();
568  virtual child_iterator child_end();
569
570  virtual void EmitImpl(llvm::Serializer& S) const;
571  static ArraySubscriptExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
572};
573
574
575/// CallExpr - [C99 6.5.2.2] Function Calls.
576///
577class CallExpr : public Expr {
578  enum { FN=0, ARGS_START=1 };
579  Expr **SubExprs;
580  unsigned NumArgs;
581  SourceLocation RParenLoc;
582
583  // This version of the ctor is for deserialization.
584  CallExpr(Expr** subexprs, unsigned numargs, QualType t,
585           SourceLocation rparenloc)
586  : Expr(CallExprClass,t), SubExprs(subexprs),
587    NumArgs(numargs), RParenLoc(rparenloc) {}
588
589public:
590  CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
591           SourceLocation rparenloc);
592  ~CallExpr() {
593    delete [] SubExprs;
594  }
595
596  const Expr *getCallee() const { return SubExprs[FN]; }
597  Expr *getCallee() { return SubExprs[FN]; }
598  void setCallee(Expr *F) { SubExprs[FN] = F; }
599
600  /// getNumArgs - Return the number of actual arguments to this call.
601  ///
602  unsigned getNumArgs() const { return NumArgs; }
603
604  /// getArg - Return the specified argument.
605  Expr *getArg(unsigned Arg) {
606    assert(Arg < NumArgs && "Arg access out of range!");
607    return SubExprs[Arg+ARGS_START];
608  }
609  const Expr *getArg(unsigned Arg) const {
610    assert(Arg < NumArgs && "Arg access out of range!");
611    return SubExprs[Arg+ARGS_START];
612  }
613  /// setArg - Set the specified argument.
614  void setArg(unsigned Arg, Expr *ArgExpr) {
615    assert(Arg < NumArgs && "Arg access out of range!");
616    SubExprs[Arg+ARGS_START] = ArgExpr;
617  }
618
619  /// setNumArgs - This changes the number of arguments present in this call.
620  /// Any orphaned expressions are deleted by this, and any new operands are set
621  /// to null.
622  void setNumArgs(unsigned NumArgs);
623
624  typedef Expr **arg_iterator;
625  typedef Expr * const *arg_const_iterator;
626  arg_iterator arg_begin() { return SubExprs+ARGS_START; }
627  arg_iterator arg_end() { return SubExprs+ARGS_START+getNumArgs(); }
628  arg_const_iterator arg_begin() const { return SubExprs+ARGS_START; }
629  arg_const_iterator arg_end() const { return SubExprs+ARGS_START+getNumArgs(); }
630
631
632  /// getNumCommas - Return the number of commas that must have been present in
633  /// this function call.
634  unsigned getNumCommas() const { return NumArgs ? NumArgs - 1 : 0; }
635
636  bool isBuiltinClassifyType(llvm::APSInt &Result) const;
637
638  /// isBuiltinConstantExpr - Return true if this built-in call is constant.
639  bool isBuiltinConstantExpr() const;
640
641  SourceLocation getRParenLoc() const { return RParenLoc; }
642
643  virtual SourceRange getSourceRange() const {
644    return SourceRange(getCallee()->getLocStart(), RParenLoc);
645  }
646
647  static bool classof(const Stmt *T) {
648    return T->getStmtClass() == CallExprClass;
649  }
650  static bool classof(const CallExpr *) { return true; }
651
652  // Iterators
653  virtual child_iterator child_begin();
654  virtual child_iterator child_end();
655
656  virtual void EmitImpl(llvm::Serializer& S) const;
657  static CallExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
658};
659
660/// MemberExpr - [C99 6.5.2.3] Structure and Union Members.
661///
662class MemberExpr : public Expr {
663  Expr *Base;
664  FieldDecl *MemberDecl;
665  SourceLocation MemberLoc;
666  bool IsArrow;      // True if this is "X->F", false if this is "X.F".
667public:
668  MemberExpr(Expr *base, bool isarrow, FieldDecl *memberdecl, SourceLocation l,
669             QualType ty)
670    : Expr(MemberExprClass, ty),
671      Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow) {}
672
673  Expr *getBase() const { return Base; }
674  FieldDecl *getMemberDecl() const { return MemberDecl; }
675  bool isArrow() const { return IsArrow; }
676
677  virtual SourceRange getSourceRange() const {
678    return SourceRange(getBase()->getLocStart(), MemberLoc);
679  }
680
681  virtual SourceLocation getExprLoc() const { return MemberLoc; }
682
683  static bool classof(const Stmt *T) {
684    return T->getStmtClass() == MemberExprClass;
685  }
686  static bool classof(const MemberExpr *) { return true; }
687
688  // Iterators
689  virtual child_iterator child_begin();
690  virtual child_iterator child_end();
691
692  virtual void EmitImpl(llvm::Serializer& S) const;
693  static MemberExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
694};
695
696/// ExtVectorElementExpr - This represents access to specific elements of a
697/// vector, and may occur on the left hand side or right hand side.  For example
698/// the following is legal:  "V.xy = V.zw" if V is a 4 element extended vector.
699///
700class ExtVectorElementExpr : public Expr {
701  Expr *Base;
702  IdentifierInfo &Accessor;
703  SourceLocation AccessorLoc;
704public:
705  ExtVectorElementExpr(QualType ty, Expr *base, IdentifierInfo &accessor,
706                       SourceLocation loc)
707    : Expr(ExtVectorElementExprClass, ty),
708      Base(base), Accessor(accessor), AccessorLoc(loc) {}
709
710  const Expr *getBase() const { return Base; }
711  Expr *getBase() { return Base; }
712
713  IdentifierInfo &getAccessor() const { return Accessor; }
714
715  /// getNumElements - Get the number of components being selected.
716  unsigned getNumElements() const;
717
718  /// containsDuplicateElements - Return true if any element access is
719  /// repeated.
720  bool containsDuplicateElements() const;
721
722  /// getEncodedElementAccess - Encode the elements accessed into an llvm
723  /// aggregate Constant of ConstantInt(s).
724  void getEncodedElementAccess(llvm::SmallVectorImpl<unsigned> &Elts) const;
725
726  /// getAccessedFieldNo - Given an encoded value and a result number, return
727  /// the input field number being accessed.
728  static unsigned getAccessedFieldNo(unsigned Idx, const llvm::Constant *Elts);
729
730  virtual SourceRange getSourceRange() const {
731    return SourceRange(getBase()->getLocStart(), AccessorLoc);
732  }
733
734  static bool classof(const Stmt *T) {
735    return T->getStmtClass() == ExtVectorElementExprClass;
736  }
737  static bool classof(const ExtVectorElementExpr *) { return true; }
738
739  // Iterators
740  virtual child_iterator child_begin();
741  virtual child_iterator child_end();
742};
743
744/// CompoundLiteralExpr - [C99 6.5.2.5]
745///
746class CompoundLiteralExpr : public Expr {
747  /// LParenLoc - If non-null, this is the location of the left paren in a
748  /// compound literal like "(int){4}".  This can be null if this is a
749  /// synthesized compound expression.
750  SourceLocation LParenLoc;
751  Expr *Init;
752  bool FileScope;
753public:
754  CompoundLiteralExpr(SourceLocation lparenloc, QualType ty, Expr *init, bool fileScope) :
755    Expr(CompoundLiteralExprClass, ty), LParenLoc(lparenloc), Init(init), FileScope(fileScope) {}
756
757  const Expr *getInitializer() const { return Init; }
758  Expr *getInitializer() { return Init; }
759
760  bool isFileScope() const { return FileScope; }
761
762  SourceLocation getLParenLoc() const { return LParenLoc; }
763
764  virtual SourceRange getSourceRange() const {
765    // FIXME: Init should never be null.
766    if (!Init)
767      return SourceRange();
768    if (LParenLoc.isInvalid())
769      return Init->getSourceRange();
770    return SourceRange(LParenLoc, Init->getLocEnd());
771  }
772
773  static bool classof(const Stmt *T) {
774    return T->getStmtClass() == CompoundLiteralExprClass;
775  }
776  static bool classof(const CompoundLiteralExpr *) { return true; }
777
778  // Iterators
779  virtual child_iterator child_begin();
780  virtual child_iterator child_end();
781
782  virtual void EmitImpl(llvm::Serializer& S) const;
783  static CompoundLiteralExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
784};
785
786/// ImplicitCastExpr - Allows us to explicitly represent implicit type
787/// conversions. For example: converting T[]->T*, void f()->void (*f)(),
788/// float->double, short->int, etc.
789///
790class ImplicitCastExpr : public Expr {
791  Expr *Op;
792public:
793  ImplicitCastExpr(QualType ty, Expr *op) :
794    Expr(ImplicitCastExprClass, ty), Op(op) {}
795
796  Expr *getSubExpr() { return Op; }
797  const Expr *getSubExpr() const { return Op; }
798
799  virtual SourceRange getSourceRange() const { return Op->getSourceRange(); }
800
801  static bool classof(const Stmt *T) {
802    return T->getStmtClass() == ImplicitCastExprClass;
803  }
804  static bool classof(const ImplicitCastExpr *) { return true; }
805
806  // Iterators
807  virtual child_iterator child_begin();
808  virtual child_iterator child_end();
809
810  virtual void EmitImpl(llvm::Serializer& S) const;
811  static ImplicitCastExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
812};
813
814/// CastExpr - [C99 6.5.4] Cast Operators.
815///
816class CastExpr : public Expr {
817  Expr *Op;
818  SourceLocation Loc; // the location of the left paren
819public:
820  CastExpr(QualType ty, Expr *op, SourceLocation l) :
821    Expr(CastExprClass, ty), Op(op), Loc(l) {}
822
823  SourceLocation getLParenLoc() const { return Loc; }
824
825  Expr *getSubExpr() const { return Op; }
826
827  virtual SourceRange getSourceRange() const {
828    return SourceRange(Loc, getSubExpr()->getSourceRange().getEnd());
829  }
830  static bool classof(const Stmt *T) {
831    return T->getStmtClass() == CastExprClass;
832  }
833  static bool classof(const CastExpr *) { return true; }
834
835  // Iterators
836  virtual child_iterator child_begin();
837  virtual child_iterator child_end();
838
839  virtual void EmitImpl(llvm::Serializer& S) const;
840  static CastExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
841};
842
843class BinaryOperator : public Expr {
844public:
845  enum Opcode {
846    // Operators listed in order of precedence.
847    // Note that additions to this should also update the StmtVisitor class.
848    Mul, Div, Rem,    // [C99 6.5.5] Multiplicative operators.
849    Add, Sub,         // [C99 6.5.6] Additive operators.
850    Shl, Shr,         // [C99 6.5.7] Bitwise shift operators.
851    LT, GT, LE, GE,   // [C99 6.5.8] Relational operators.
852    EQ, NE,           // [C99 6.5.9] Equality operators.
853    And,              // [C99 6.5.10] Bitwise AND operator.
854    Xor,              // [C99 6.5.11] Bitwise XOR operator.
855    Or,               // [C99 6.5.12] Bitwise OR operator.
856    LAnd,             // [C99 6.5.13] Logical AND operator.
857    LOr,              // [C99 6.5.14] Logical OR operator.
858    Assign, MulAssign,// [C99 6.5.16] Assignment operators.
859    DivAssign, RemAssign,
860    AddAssign, SubAssign,
861    ShlAssign, ShrAssign,
862    AndAssign, XorAssign,
863    OrAssign,
864    Comma             // [C99 6.5.17] Comma operator.
865  };
866private:
867  enum { LHS, RHS, END_EXPR };
868  Expr* SubExprs[END_EXPR];
869  Opcode Opc;
870  SourceLocation OpLoc;
871public:
872
873  BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy,
874                 SourceLocation opLoc)
875    : Expr(BinaryOperatorClass, ResTy), Opc(opc), OpLoc(opLoc) {
876    SubExprs[LHS] = lhs;
877    SubExprs[RHS] = rhs;
878    assert(!isCompoundAssignmentOp() &&
879           "Use ArithAssignBinaryOperator for compound assignments");
880  }
881
882  SourceLocation getOperatorLoc() const { return OpLoc; }
883  Opcode getOpcode() const { return Opc; }
884  Expr *getLHS() const { return SubExprs[LHS]; }
885  Expr *getRHS() const { return SubExprs[RHS]; }
886  virtual SourceRange getSourceRange() const {
887    return SourceRange(getLHS()->getLocStart(), getRHS()->getLocEnd());
888  }
889
890  /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
891  /// corresponds to, e.g. "<<=".
892  static const char *getOpcodeStr(Opcode Op);
893
894  /// predicates to categorize the respective opcodes.
895  bool isMultiplicativeOp() const { return Opc >= Mul && Opc <= Rem; }
896  bool isAdditiveOp() const { return Opc == Add || Opc == Sub; }
897  bool isShiftOp() const { return Opc == Shl || Opc == Shr; }
898  bool isBitwiseOp() const { return Opc >= And && Opc <= Or; }
899  bool isRelationalOp() const { return Opc >= LT && Opc <= GE; }
900  bool isEqualityOp() const { return Opc == EQ || Opc == NE; }
901  bool isLogicalOp() const { return Opc == LAnd || Opc == LOr; }
902  bool isAssignmentOp() const { return Opc >= Assign && Opc <= OrAssign; }
903  bool isCompoundAssignmentOp() const { return Opc > Assign && Opc <= OrAssign;}
904  bool isShiftAssignOp() const { return Opc == ShlAssign || Opc == ShrAssign; }
905
906  static bool classof(const Stmt *S) {
907    return S->getStmtClass() == BinaryOperatorClass ||
908           S->getStmtClass() == CompoundAssignOperatorClass;
909  }
910  static bool classof(const BinaryOperator *) { return true; }
911
912  // Iterators
913  virtual child_iterator child_begin();
914  virtual child_iterator child_end();
915
916  virtual void EmitImpl(llvm::Serializer& S) const;
917  static BinaryOperator* CreateImpl(llvm::Deserializer& D, ASTContext& C);
918
919protected:
920  BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy,
921                 SourceLocation oploc, bool dead)
922    : Expr(CompoundAssignOperatorClass, ResTy), Opc(opc), OpLoc(oploc) {
923    SubExprs[LHS] = lhs;
924    SubExprs[RHS] = rhs;
925  }
926};
927
928/// CompoundAssignOperator - For compound assignments (e.g. +=), we keep
929/// track of the type the operation is performed in.  Due to the semantics of
930/// these operators, the operands are promoted, the aritmetic performed, an
931/// implicit conversion back to the result type done, then the assignment takes
932/// place.  This captures the intermediate type which the computation is done
933/// in.
934class CompoundAssignOperator : public BinaryOperator {
935  QualType ComputationType;
936public:
937  CompoundAssignOperator(Expr *lhs, Expr *rhs, Opcode opc,
938                         QualType ResType, QualType CompType,
939                         SourceLocation OpLoc)
940    : BinaryOperator(lhs, rhs, opc, ResType, OpLoc, true),
941      ComputationType(CompType) {
942    assert(isCompoundAssignmentOp() &&
943           "Only should be used for compound assignments");
944  }
945
946  QualType getComputationType() const { return ComputationType; }
947
948  static bool classof(const CompoundAssignOperator *) { return true; }
949  static bool classof(const Stmt *S) {
950    return S->getStmtClass() == CompoundAssignOperatorClass;
951  }
952
953  virtual void EmitImpl(llvm::Serializer& S) const;
954  static CompoundAssignOperator* CreateImpl(llvm::Deserializer& D,
955                                            ASTContext& C);
956};
957
958/// ConditionalOperator - The ?: operator.  Note that LHS may be null when the
959/// GNU "missing LHS" extension is in use.
960///
961class ConditionalOperator : public Expr {
962  enum { COND, LHS, RHS, END_EXPR };
963  Expr* SubExprs[END_EXPR]; // Left/Middle/Right hand sides.
964public:
965  ConditionalOperator(Expr *cond, Expr *lhs, Expr *rhs, QualType t)
966    : Expr(ConditionalOperatorClass, t) {
967    SubExprs[COND] = cond;
968    SubExprs[LHS] = lhs;
969    SubExprs[RHS] = rhs;
970  }
971
972  // getCond - Return the expression representing the condition for
973  //  the ?: operator.
974  Expr *getCond() const { return SubExprs[COND]; }
975
976  // getTrueExpr - Return the subexpression representing the value of the ?:
977  //  expression if the condition evaluates to true.  In most cases this value
978  //  will be the same as getLHS() except a GCC extension allows the left
979  //  subexpression to be omitted, and instead of the condition be returned.
980  //  e.g: x ?: y is shorthand for x ? x : y, except that the expression "x"
981  //  is only evaluated once.
982  Expr *getTrueExpr() const {
983    return SubExprs[LHS] ? SubExprs[COND] : SubExprs[LHS];
984  }
985
986  // getTrueExpr - Return the subexpression representing the value of the ?:
987  // expression if the condition evaluates to false. This is the same as getRHS.
988  Expr *getFalseExpr() const { return SubExprs[RHS]; }
989
990  Expr *getLHS() const { return SubExprs[LHS]; }
991  Expr *getRHS() const { return SubExprs[RHS]; }
992
993  virtual SourceRange getSourceRange() const {
994    return SourceRange(getCond()->getLocStart(), getRHS()->getLocEnd());
995  }
996  static bool classof(const Stmt *T) {
997    return T->getStmtClass() == ConditionalOperatorClass;
998  }
999  static bool classof(const ConditionalOperator *) { return true; }
1000
1001  // Iterators
1002  virtual child_iterator child_begin();
1003  virtual child_iterator child_end();
1004
1005  virtual void EmitImpl(llvm::Serializer& S) const;
1006  static ConditionalOperator* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1007};
1008
1009/// AddrLabelExpr - The GNU address of label extension, representing &&label.
1010class AddrLabelExpr : public Expr {
1011  SourceLocation AmpAmpLoc, LabelLoc;
1012  LabelStmt *Label;
1013public:
1014  AddrLabelExpr(SourceLocation AALoc, SourceLocation LLoc, LabelStmt *L,
1015                QualType t)
1016    : Expr(AddrLabelExprClass, t), AmpAmpLoc(AALoc), LabelLoc(LLoc), Label(L) {}
1017
1018  virtual SourceRange getSourceRange() const {
1019    return SourceRange(AmpAmpLoc, LabelLoc);
1020  }
1021
1022  LabelStmt *getLabel() const { return Label; }
1023
1024  static bool classof(const Stmt *T) {
1025    return T->getStmtClass() == AddrLabelExprClass;
1026  }
1027  static bool classof(const AddrLabelExpr *) { return true; }
1028
1029  // Iterators
1030  virtual child_iterator child_begin();
1031  virtual child_iterator child_end();
1032
1033  virtual void EmitImpl(llvm::Serializer& S) const;
1034  static AddrLabelExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1035};
1036
1037/// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}).
1038/// The StmtExpr contains a single CompoundStmt node, which it evaluates and
1039/// takes the value of the last subexpression.
1040class StmtExpr : public Expr {
1041  CompoundStmt *SubStmt;
1042  SourceLocation LParenLoc, RParenLoc;
1043public:
1044  StmtExpr(CompoundStmt *substmt, QualType T,
1045           SourceLocation lp, SourceLocation rp) :
1046    Expr(StmtExprClass, T), SubStmt(substmt),  LParenLoc(lp), RParenLoc(rp) { }
1047
1048  CompoundStmt *getSubStmt() { return SubStmt; }
1049  const CompoundStmt *getSubStmt() const { return SubStmt; }
1050
1051  virtual SourceRange getSourceRange() const {
1052    return SourceRange(LParenLoc, RParenLoc);
1053  }
1054
1055  static bool classof(const Stmt *T) {
1056    return T->getStmtClass() == StmtExprClass;
1057  }
1058  static bool classof(const StmtExpr *) { return true; }
1059
1060  // Iterators
1061  virtual child_iterator child_begin();
1062  virtual child_iterator child_end();
1063
1064  virtual void EmitImpl(llvm::Serializer& S) const;
1065  static StmtExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1066};
1067
1068/// TypesCompatibleExpr - GNU builtin-in function __builtin_type_compatible_p.
1069/// This AST node represents a function that returns 1 if two *types* (not
1070/// expressions) are compatible. The result of this built-in function can be
1071/// used in integer constant expressions.
1072class TypesCompatibleExpr : public Expr {
1073  QualType Type1;
1074  QualType Type2;
1075  SourceLocation BuiltinLoc, RParenLoc;
1076public:
1077  TypesCompatibleExpr(QualType ReturnType, SourceLocation BLoc,
1078                      QualType t1, QualType t2, SourceLocation RP) :
1079    Expr(TypesCompatibleExprClass, ReturnType), Type1(t1), Type2(t2),
1080    BuiltinLoc(BLoc), RParenLoc(RP) {}
1081
1082  QualType getArgType1() const { return Type1; }
1083  QualType getArgType2() const { return Type2; }
1084
1085  virtual SourceRange getSourceRange() const {
1086    return SourceRange(BuiltinLoc, RParenLoc);
1087  }
1088  static bool classof(const Stmt *T) {
1089    return T->getStmtClass() == TypesCompatibleExprClass;
1090  }
1091  static bool classof(const TypesCompatibleExpr *) { return true; }
1092
1093  // Iterators
1094  virtual child_iterator child_begin();
1095  virtual child_iterator child_end();
1096};
1097
1098/// ShuffleVectorExpr - clang-specific builtin-in function
1099/// __builtin_shufflevector.
1100/// This AST node represents a operator that does a constant
1101/// shuffle, similar to LLVM's shufflevector instruction. It takes
1102/// two vectors and a variable number of constant indices,
1103/// and returns the appropriately shuffled vector.
1104class ShuffleVectorExpr : public Expr {
1105  SourceLocation BuiltinLoc, RParenLoc;
1106
1107  // SubExprs - the list of values passed to the __builtin_shufflevector
1108  // function. The first two are vectors, and the rest are constant
1109  // indices.  The number of values in this list is always
1110  // 2+the number of indices in the vector type.
1111  Expr **SubExprs;
1112  unsigned NumExprs;
1113
1114public:
1115  ShuffleVectorExpr(Expr **args, unsigned nexpr,
1116                    QualType Type, SourceLocation BLoc,
1117                    SourceLocation RP) :
1118    Expr(ShuffleVectorExprClass, Type), BuiltinLoc(BLoc),
1119    RParenLoc(RP), NumExprs(nexpr)
1120  {
1121    SubExprs = new Expr*[nexpr];
1122    for (unsigned i = 0; i < nexpr; i++)
1123      SubExprs[i] = args[i];
1124  }
1125
1126  virtual SourceRange getSourceRange() const {
1127    return SourceRange(BuiltinLoc, RParenLoc);
1128  }
1129  static bool classof(const Stmt *T) {
1130    return T->getStmtClass() == ShuffleVectorExprClass;
1131  }
1132  static bool classof(const ShuffleVectorExpr *) { return true; }
1133
1134  ~ShuffleVectorExpr() {
1135    for (unsigned i = 0; i < NumExprs; i++)
1136      delete SubExprs[i];
1137    delete [] SubExprs;
1138  }
1139
1140  /// getNumSubExprs - Return the size of the SubExprs array.  This includes the
1141  /// constant expression, the actual arguments passed in, and the function
1142  /// pointers.
1143  unsigned getNumSubExprs() const { return NumExprs; }
1144
1145  /// getExpr - Return the Expr at the specified index.
1146  Expr *getExpr(unsigned Index) {
1147    assert((Index < NumExprs) && "Arg access out of range!");
1148    return SubExprs[Index];
1149  }
1150  const Expr *getExpr(unsigned Index) const {
1151    assert((Index < NumExprs) && "Arg access out of range!");
1152    return SubExprs[Index];
1153  }
1154
1155  int getShuffleMaskIdx(ASTContext &Ctx, unsigned N) {
1156    llvm::APSInt Result(32);
1157    bool result = getExpr(N+2)->isIntegerConstantExpr(Result, Ctx);
1158    assert(result && "Must be integer constant");
1159    return Result.getZExtValue();
1160  }
1161
1162  // Iterators
1163  virtual child_iterator child_begin();
1164  virtual child_iterator child_end();
1165};
1166
1167/// ChooseExpr - GNU builtin-in function __builtin_choose_expr.
1168/// This AST node is similar to the conditional operator (?:) in C, with
1169/// the following exceptions:
1170/// - the test expression much be a constant expression.
1171/// - the expression returned has it's type unaltered by promotion rules.
1172/// - does not evaluate the expression that was not chosen.
1173class ChooseExpr : public Expr {
1174  enum { COND, LHS, RHS, END_EXPR };
1175  Expr* SubExprs[END_EXPR]; // Left/Middle/Right hand sides.
1176  SourceLocation BuiltinLoc, RParenLoc;
1177public:
1178  ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, QualType t,
1179             SourceLocation RP)
1180    : Expr(ChooseExprClass, t),
1181      BuiltinLoc(BLoc), RParenLoc(RP) {
1182      SubExprs[COND] = cond;
1183      SubExprs[LHS] = lhs;
1184      SubExprs[RHS] = rhs;
1185    }
1186
1187  /// isConditionTrue - Return true if the condition is true.  This is always
1188  /// statically knowable for a well-formed choosexpr.
1189  bool isConditionTrue(ASTContext &C) const;
1190
1191  Expr *getCond() const { return SubExprs[COND]; }
1192  Expr *getLHS() const { return SubExprs[LHS]; }
1193  Expr *getRHS() const { return SubExprs[RHS]; }
1194
1195  virtual SourceRange getSourceRange() const {
1196    return SourceRange(BuiltinLoc, RParenLoc);
1197  }
1198  static bool classof(const Stmt *T) {
1199    return T->getStmtClass() == ChooseExprClass;
1200  }
1201  static bool classof(const ChooseExpr *) { return true; }
1202
1203  // Iterators
1204  virtual child_iterator child_begin();
1205  virtual child_iterator child_end();
1206};
1207
1208/// OverloadExpr - Clang builtin function __builtin_overload.
1209/// This AST node provides a way to overload functions in C.
1210///
1211/// The first argument is required to be a constant expression, for the number
1212/// of arguments passed to each candidate function.
1213///
1214/// The next N arguments, where N is the value of the constant expression,
1215/// are the values to be passed as arguments.
1216///
1217/// The rest of the arguments are values of pointer to function type, which
1218/// are the candidate functions for overloading.
1219///
1220/// The result is a equivalent to a CallExpr taking N arguments to the
1221/// candidate function whose parameter types match the types of the N arguments.
1222///
1223/// example: float Z = __builtin_overload(2, X, Y, modf, mod, modl);
1224/// If X and Y are long doubles, Z will assigned the result of modl(X, Y);
1225/// If X and Y are floats, Z will be assigned the result of modf(X, Y);
1226class OverloadExpr : public Expr {
1227  // SubExprs - the list of values passed to the __builtin_overload function.
1228  // SubExpr[0] is a constant expression
1229  // SubExpr[1-N] are the parameters to pass to the matching function call
1230  // SubExpr[N-...] are the candidate functions, of type pointer to function.
1231  Expr **SubExprs;
1232
1233  // NumExprs - the size of the SubExprs array
1234  unsigned NumExprs;
1235
1236  // The index of the matching candidate function
1237  unsigned FnIndex;
1238
1239  SourceLocation BuiltinLoc;
1240  SourceLocation RParenLoc;
1241public:
1242  OverloadExpr(Expr **args, unsigned nexprs, unsigned idx, QualType t,
1243               SourceLocation bloc, SourceLocation rploc)
1244    : Expr(OverloadExprClass, t), NumExprs(nexprs), FnIndex(idx),
1245      BuiltinLoc(bloc), RParenLoc(rploc) {
1246    SubExprs = new Expr*[nexprs];
1247    for (unsigned i = 0; i != nexprs; ++i)
1248      SubExprs[i] = args[i];
1249  }
1250  ~OverloadExpr() {
1251    delete [] SubExprs;
1252  }
1253
1254  /// arg_begin - Return a pointer to the list of arguments that will be passed
1255  /// to the matching candidate function, skipping over the initial constant
1256  /// expression.
1257  typedef Expr * const *arg_const_iterator;
1258  arg_const_iterator arg_begin() const { return SubExprs+1; }
1259
1260  /// getNumArgs - Return the number of arguments to pass to the candidate
1261  /// functions.
1262  unsigned getNumArgs(ASTContext &Ctx) const {
1263    llvm::APSInt constEval(32);
1264    (void) SubExprs[0]->isIntegerConstantExpr(constEval, Ctx);
1265    return constEval.getZExtValue();
1266  }
1267
1268  /// getNumSubExprs - Return the size of the SubExprs array.  This includes the
1269  /// constant expression, the actual arguments passed in, and the function
1270  /// pointers.
1271  unsigned getNumSubExprs() const { return NumExprs; }
1272
1273  /// getExpr - Return the Expr at the specified index.
1274  Expr *getExpr(unsigned Index) {
1275    assert((Index < NumExprs) && "Arg access out of range!");
1276    return SubExprs[Index];
1277  }
1278
1279  /// getFn - Return the matching candidate function for this OverloadExpr.
1280  Expr *getFn() const { return SubExprs[FnIndex]; }
1281
1282  virtual SourceRange getSourceRange() const {
1283    return SourceRange(BuiltinLoc, RParenLoc);
1284  }
1285  static bool classof(const Stmt *T) {
1286    return T->getStmtClass() == OverloadExprClass;
1287  }
1288  static bool classof(const OverloadExpr *) { return true; }
1289
1290  // Iterators
1291  virtual child_iterator child_begin();
1292  virtual child_iterator child_end();
1293};
1294
1295/// VAArgExpr, used for the builtin function __builtin_va_start.
1296class VAArgExpr : public Expr {
1297  Expr *Val;
1298  SourceLocation BuiltinLoc, RParenLoc;
1299public:
1300  VAArgExpr(SourceLocation BLoc, Expr* e, QualType t, SourceLocation RPLoc)
1301    : Expr(VAArgExprClass, t),
1302      Val(e),
1303      BuiltinLoc(BLoc),
1304      RParenLoc(RPLoc) { }
1305
1306  const Expr *getSubExpr() const { return Val; }
1307  Expr *getSubExpr() { return Val; }
1308  virtual SourceRange getSourceRange() const {
1309    return SourceRange(BuiltinLoc, RParenLoc);
1310  }
1311  static bool classof(const Stmt *T) {
1312    return T->getStmtClass() == VAArgExprClass;
1313  }
1314  static bool classof(const VAArgExpr *) { return true; }
1315
1316  // Iterators
1317  virtual child_iterator child_begin();
1318  virtual child_iterator child_end();
1319};
1320
1321/// InitListExpr - used for struct and array initializers, such as:
1322///    struct foo x = { 1, { 2, 3 } };
1323///
1324/// Because C is somewhat loose with braces, the AST does not necessarily
1325/// directly model the C source.  Instead, the semantic analyzer aims to make
1326/// the InitListExprs match up with the type of the decl being initialized.  We
1327/// have the following exceptions:
1328///
1329///  1. Elements at the end of the list may be dropped from the initializer.
1330///     These elements are defined to be initialized to zero.  For example:
1331///         int x[20] = { 1 };
1332///  2. Initializers may have excess initializers which are to be ignored by the
1333///     compiler.  For example:
1334///         int x[1] = { 1, 2 };
1335///  3. Redundant InitListExprs may be present around scalar elements.  These
1336///     always have a single element whose type is the same as the InitListExpr.
1337///     this can only happen for Type::isScalarType() types.
1338///         int x = { 1 };  int y[2] = { {1}, {2} };
1339///
1340class InitListExpr : public Expr {
1341  std::vector<Expr *> InitExprs;
1342  SourceLocation LBraceLoc, RBraceLoc;
1343public:
1344  InitListExpr(SourceLocation lbraceloc, Expr **initexprs, unsigned numinits,
1345               SourceLocation rbraceloc);
1346
1347  unsigned getNumInits() const { return InitExprs.size(); }
1348
1349  const Expr* getInit(unsigned Init) const {
1350    assert(Init < getNumInits() && "Initializer access out of range!");
1351    return InitExprs[Init];
1352  }
1353
1354  Expr* getInit(unsigned Init) {
1355    assert(Init < getNumInits() && "Initializer access out of range!");
1356    return InitExprs[Init];
1357  }
1358
1359  void setInit(unsigned Init, Expr *expr) {
1360    assert(Init < getNumInits() && "Initializer access out of range!");
1361    InitExprs[Init] = expr;
1362  }
1363
1364  // Dynamic removal/addition (for constructing implicit InitExpr's).
1365  void removeInit(unsigned Init) {
1366    InitExprs.erase(InitExprs.begin()+Init);
1367  }
1368  void addInit(unsigned Init, Expr *expr) {
1369    InitExprs.insert(InitExprs.begin()+Init, expr);
1370  }
1371
1372  // Explicit InitListExpr's originate from source code (and have valid source
1373  // locations). Implicit InitListExpr's are created by the semantic analyzer.
1374  bool isExplicit() {
1375    return LBraceLoc.isValid() && RBraceLoc.isValid();
1376  }
1377
1378  virtual SourceRange getSourceRange() const {
1379    return SourceRange(LBraceLoc, RBraceLoc);
1380  }
1381  static bool classof(const Stmt *T) {
1382    return T->getStmtClass() == InitListExprClass;
1383  }
1384  static bool classof(const InitListExpr *) { return true; }
1385
1386  // Iterators
1387  virtual child_iterator child_begin();
1388  virtual child_iterator child_end();
1389
1390  virtual void EmitImpl(llvm::Serializer& S) const;
1391  static InitListExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1392
1393private:
1394  // Used by serializer.
1395  InitListExpr() : Expr(InitListExprClass, QualType()) {}
1396};
1397
1398/// ObjCStringLiteral, used for Objective-C string literals
1399/// i.e. @"foo".
1400class ObjCStringLiteral : public Expr {
1401  StringLiteral *String;
1402  SourceLocation AtLoc;
1403public:
1404  ObjCStringLiteral(StringLiteral *SL, QualType T, SourceLocation L)
1405    : Expr(ObjCStringLiteralClass, T), String(SL), AtLoc(L) {}
1406
1407  StringLiteral* getString() { return String; }
1408
1409  const StringLiteral* getString() const { return String; }
1410
1411  SourceLocation getAtLoc() const { return AtLoc; }
1412
1413  virtual SourceRange getSourceRange() const {
1414    return SourceRange(AtLoc, String->getLocEnd());
1415  }
1416
1417  static bool classof(const Stmt *T) {
1418    return T->getStmtClass() == ObjCStringLiteralClass;
1419  }
1420  static bool classof(const ObjCStringLiteral *) { return true; }
1421
1422  // Iterators
1423  virtual child_iterator child_begin();
1424  virtual child_iterator child_end();
1425
1426  virtual void EmitImpl(llvm::Serializer& S) const;
1427  static ObjCStringLiteral* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1428};
1429
1430/// ObjCEncodeExpr, used for @encode in Objective-C.
1431class ObjCEncodeExpr : public Expr {
1432  QualType EncType;
1433  SourceLocation AtLoc, RParenLoc;
1434public:
1435  ObjCEncodeExpr(QualType T, QualType ET,
1436                 SourceLocation at, SourceLocation rp)
1437    : Expr(ObjCEncodeExprClass, T), EncType(ET), AtLoc(at), RParenLoc(rp) {}
1438
1439  SourceLocation getAtLoc() const { return AtLoc; }
1440  SourceLocation getRParenLoc() const { return RParenLoc; }
1441
1442  virtual SourceRange getSourceRange() const {
1443    return SourceRange(AtLoc, RParenLoc);
1444  }
1445
1446  QualType getEncodedType() const { return EncType; }
1447
1448  static bool classof(const Stmt *T) {
1449    return T->getStmtClass() == ObjCEncodeExprClass;
1450  }
1451  static bool classof(const ObjCEncodeExpr *) { return true; }
1452
1453  // Iterators
1454  virtual child_iterator child_begin();
1455  virtual child_iterator child_end();
1456
1457  virtual void EmitImpl(llvm::Serializer& S) const;
1458  static ObjCEncodeExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1459};
1460
1461/// ObjCSelectorExpr used for @selector in Objective-C.
1462class ObjCSelectorExpr : public Expr {
1463  Selector SelName;
1464  SourceLocation AtLoc, RParenLoc;
1465public:
1466  ObjCSelectorExpr(QualType T, Selector selInfo,
1467                   SourceLocation at, SourceLocation rp)
1468  : Expr(ObjCSelectorExprClass, T), SelName(selInfo),
1469  AtLoc(at), RParenLoc(rp) {}
1470
1471  Selector getSelector() const { return SelName; }
1472
1473  SourceLocation getAtLoc() const { return AtLoc; }
1474  SourceLocation getRParenLoc() const { return RParenLoc; }
1475
1476  virtual SourceRange getSourceRange() const {
1477    return SourceRange(AtLoc, RParenLoc);
1478  }
1479
1480  /// getNumArgs - Return the number of actual arguments to this call.
1481  unsigned getNumArgs() const { return SelName.getNumArgs(); }
1482
1483  static bool classof(const Stmt *T) {
1484    return T->getStmtClass() == ObjCSelectorExprClass;
1485  }
1486  static bool classof(const ObjCSelectorExpr *) { return true; }
1487
1488  // Iterators
1489  virtual child_iterator child_begin();
1490  virtual child_iterator child_end();
1491
1492  virtual void EmitImpl(llvm::Serializer& S) const;
1493  static ObjCSelectorExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1494};
1495
1496/// ObjCProtocolExpr used for protocol in Objective-C.
1497class ObjCProtocolExpr : public Expr {
1498  ObjCProtocolDecl *Protocol;
1499  SourceLocation AtLoc, RParenLoc;
1500public:
1501  ObjCProtocolExpr(QualType T, ObjCProtocolDecl *protocol,
1502                   SourceLocation at, SourceLocation rp)
1503  : Expr(ObjCProtocolExprClass, T), Protocol(protocol),
1504  AtLoc(at), RParenLoc(rp) {}
1505
1506  ObjCProtocolDecl *getProtocol() const { return Protocol; }
1507
1508  SourceLocation getAtLoc() const { return AtLoc; }
1509  SourceLocation getRParenLoc() const { return RParenLoc; }
1510
1511  virtual SourceRange getSourceRange() const {
1512    return SourceRange(AtLoc, RParenLoc);
1513  }
1514
1515  static bool classof(const Stmt *T) {
1516    return T->getStmtClass() == ObjCProtocolExprClass;
1517  }
1518  static bool classof(const ObjCProtocolExpr *) { return true; }
1519
1520  // Iterators
1521  virtual child_iterator child_begin();
1522  virtual child_iterator child_end();
1523};
1524
1525/// ObjCIvarRefExpr - A reference to an ObjC instance variable.
1526class ObjCIvarRefExpr : public Expr {
1527  class ObjCIvarDecl *D;
1528  SourceLocation Loc;
1529  Expr *Base;
1530  bool IsArrow:1;      // True if this is "X->F", false if this is "X.F".
1531  bool IsFreeIvar:1;   // True if ivar reference has no base (self assumed).
1532
1533public:
1534  ObjCIvarRefExpr(ObjCIvarDecl *d, QualType t, SourceLocation l, Expr *base=0,
1535                  bool arrow = false, bool freeIvar = false) :
1536    Expr(ObjCIvarRefExprClass, t), D(d), Loc(l), Base(base), IsArrow(arrow),
1537    IsFreeIvar(freeIvar) {}
1538
1539  ObjCIvarDecl *getDecl() { return D; }
1540  const ObjCIvarDecl *getDecl() const { return D; }
1541  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
1542  const Expr *getBase() const { return Base; }
1543  Expr *getBase() { return Base; }
1544  void setBase(Expr * base) { Base = base; }
1545  bool isArrow() const { return IsArrow; }
1546  bool isFreeIvar() const { return IsFreeIvar; }
1547
1548  SourceLocation getLocation() const { return Loc; }
1549
1550  static bool classof(const Stmt *T) {
1551    return T->getStmtClass() == ObjCIvarRefExprClass;
1552  }
1553  static bool classof(const ObjCIvarRefExpr *) { return true; }
1554
1555  // Iterators
1556  virtual child_iterator child_begin();
1557  virtual child_iterator child_end();
1558
1559  virtual void EmitImpl(llvm::Serializer& S) const;
1560  static ObjCIvarRefExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1561};
1562
1563class ObjCMessageExpr : public Expr {
1564  enum { RECEIVER=0, ARGS_START=1 };
1565
1566  Expr **SubExprs;
1567
1568  unsigned NumArgs;
1569
1570  // A unigue name for this message.
1571  Selector SelName;
1572
1573  // A method prototype for this message (optional).
1574  // FIXME: Since method decls contain the selector, and most messages have a
1575  // prototype, consider devising a scheme for unifying SelName/MethodProto.
1576  ObjCMethodDecl *MethodProto;
1577
1578  SourceLocation LBracloc, RBracloc;
1579
1580  // constructor used during deserialization
1581  ObjCMessageExpr(Selector selInfo, QualType retType,
1582                  SourceLocation LBrac, SourceLocation RBrac,
1583                  Expr **ArgExprs, unsigned nargs)
1584  : Expr(ObjCMessageExprClass, retType), NumArgs(nargs), SelName(selInfo),
1585    MethodProto(NULL), LBracloc(LBrac), RBracloc(RBrac) {}
1586
1587public:
1588  // constructor for class messages.
1589  // FIXME: clsName should be typed to ObjCInterfaceType
1590  ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
1591                  QualType retType, ObjCMethodDecl *methDecl,
1592                  SourceLocation LBrac, SourceLocation RBrac,
1593                  Expr **ArgExprs, unsigned NumArgs);
1594  // constructor for instance messages.
1595  ObjCMessageExpr(Expr *receiver, Selector selInfo,
1596                  QualType retType, ObjCMethodDecl *methDecl,
1597                  SourceLocation LBrac, SourceLocation RBrac,
1598                  Expr **ArgExprs, unsigned NumArgs);
1599
1600  ~ObjCMessageExpr() {
1601    delete [] SubExprs;
1602  }
1603
1604  /// getReceiver - Returns the receiver of the message expression.
1605  ///  This can be NULL if the message is for instance methods.  For
1606  ///  instance methods, use getClassName.
1607  Expr *getReceiver() {
1608    uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1609    return x & 0x1 ? NULL : (Expr*) x;
1610  }
1611  const Expr *getReceiver() const {
1612    return const_cast<ObjCMessageExpr*>(this)->getReceiver();
1613  }
1614
1615  Selector getSelector() const { return SelName; }
1616
1617  const ObjCMethodDecl *getMethodDecl() const { return MethodProto; }
1618  ObjCMethodDecl *getMethodDecl() { return MethodProto; }
1619
1620  /// getClassName - For instance methods, this returns the invoked class,
1621  ///  and returns NULL otherwise.  For regular methods, use getReceiver.
1622  IdentifierInfo *getClassName() {
1623    uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1624    return x & 0x1 ? (IdentifierInfo*) (x & ~0x1) : NULL;
1625  }
1626  const IdentifierInfo *getClassName() const {
1627    return const_cast<ObjCMessageExpr*>(this)->getClassName();
1628  }
1629
1630  /// getNumArgs - Return the number of actual arguments to this call.
1631  unsigned getNumArgs() const { return NumArgs; }
1632
1633  /// getArg - Return the specified argument.
1634  Expr *getArg(unsigned Arg) {
1635    assert(Arg < NumArgs && "Arg access out of range!");
1636    return SubExprs[Arg+ARGS_START];
1637  }
1638  const Expr *getArg(unsigned Arg) const {
1639    assert(Arg < NumArgs && "Arg access out of range!");
1640    return SubExprs[Arg+ARGS_START];
1641  }
1642  /// setArg - Set the specified argument.
1643  void setArg(unsigned Arg, Expr *ArgExpr) {
1644    assert(Arg < NumArgs && "Arg access out of range!");
1645    SubExprs[Arg+ARGS_START] = ArgExpr;
1646  }
1647
1648  virtual SourceRange getSourceRange() const {
1649    return SourceRange(LBracloc, RBracloc);
1650  }
1651
1652  static bool classof(const Stmt *T) {
1653    return T->getStmtClass() == ObjCMessageExprClass;
1654  }
1655  static bool classof(const ObjCMessageExpr *) { return true; }
1656
1657  // Iterators
1658  virtual child_iterator child_begin();
1659  virtual child_iterator child_end();
1660
1661  typedef Expr** arg_iterator;
1662  typedef const Expr* const* const_arg_iterator;
1663
1664  arg_iterator arg_begin() { return &SubExprs[ARGS_START]; }
1665  arg_iterator arg_end()   { return arg_begin() + NumArgs; }
1666  const_arg_iterator arg_begin() const { return &SubExprs[ARGS_START]; }
1667  const_arg_iterator arg_end() const { return arg_begin() + NumArgs; }
1668
1669  // Serialization.
1670  virtual void EmitImpl(llvm::Serializer& S) const;
1671  static ObjCMessageExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1672};
1673
1674}  // end namespace clang
1675
1676#endif
1677