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