Expr.h revision cb6accb4659af86627ce43f7a90bc92f763b075e
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  bool isSizeOf() const { return isSizeof; }
544  QualType getArgumentType() const { return Ty; }
545
546  SourceLocation getOperatorLoc() const { return OpLoc; }
547
548  virtual SourceRange getSourceRange() const {
549    return SourceRange(OpLoc, RParenLoc);
550  }
551
552  static bool classof(const Stmt *T) {
553    return T->getStmtClass() == SizeOfAlignOfTypeExprClass;
554  }
555  static bool classof(const SizeOfAlignOfTypeExpr *) { return true; }
556
557  // Iterators
558  virtual child_iterator child_begin();
559  virtual child_iterator child_end();
560
561  virtual void EmitImpl(llvm::Serializer& S) const;
562  static SizeOfAlignOfTypeExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
563};
564
565//===----------------------------------------------------------------------===//
566// Postfix Operators.
567//===----------------------------------------------------------------------===//
568
569/// ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting.
570class ArraySubscriptExpr : public Expr {
571  enum { LHS, RHS, END_EXPR=2 };
572  Stmt* SubExprs[END_EXPR];
573  SourceLocation RBracketLoc;
574public:
575  ArraySubscriptExpr(Expr *lhs, Expr *rhs, QualType t,
576                     SourceLocation rbracketloc)
577  : Expr(ArraySubscriptExprClass, t), RBracketLoc(rbracketloc) {
578    SubExprs[LHS] = lhs;
579    SubExprs[RHS] = rhs;
580  }
581
582  /// An array access can be written A[4] or 4[A] (both are equivalent).
583  /// - getBase() and getIdx() always present the normalized view: A[4].
584  ///    In this case getBase() returns "A" and getIdx() returns "4".
585  /// - getLHS() and getRHS() present the syntactic view. e.g. for
586  ///    4[A] getLHS() returns "4".
587  /// Note: Because vector element access is also written A[4] we must
588  /// predicate the format conversion in getBase and getIdx only on the
589  /// the type of the RHS, as it is possible for the LHS to be a vector of
590  /// integer type
591  Expr *getLHS() { return cast<Expr>(SubExprs[LHS]); }
592  const Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); }
593
594  Expr *getRHS() { return cast<Expr>(SubExprs[RHS]); }
595  const Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); }
596
597  Expr *getBase() {
598    return cast<Expr>(getRHS()->getType()->isIntegerType() ? getLHS():getRHS());
599  }
600
601  const Expr *getBase() const {
602    return cast<Expr>(getRHS()->getType()->isIntegerType() ? getLHS():getRHS());
603  }
604
605  Expr *getIdx() {
606    return cast<Expr>(getRHS()->getType()->isIntegerType() ? getRHS():getLHS());
607  }
608
609  const Expr *getIdx() const {
610    return cast<Expr>(getRHS()->getType()->isIntegerType() ? getRHS():getLHS());
611  }
612
613  virtual SourceRange getSourceRange() const {
614    return SourceRange(getLHS()->getLocStart(), RBracketLoc);
615  }
616
617  virtual SourceLocation getExprLoc() const { return RBracketLoc; }
618
619  static bool classof(const Stmt *T) {
620    return T->getStmtClass() == ArraySubscriptExprClass;
621  }
622  static bool classof(const ArraySubscriptExpr *) { return true; }
623
624  // Iterators
625  virtual child_iterator child_begin();
626  virtual child_iterator child_end();
627
628  virtual void EmitImpl(llvm::Serializer& S) const;
629  static ArraySubscriptExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
630};
631
632
633/// CallExpr - [C99 6.5.2.2] Function Calls.
634///
635class CallExpr : public Expr {
636  enum { FN=0, ARGS_START=1 };
637  Stmt **SubExprs;
638  unsigned NumArgs;
639  SourceLocation RParenLoc;
640
641  // This version of the ctor is for deserialization.
642  CallExpr(Stmt** subexprs, unsigned numargs, QualType t,
643           SourceLocation rparenloc)
644  : Expr(CallExprClass,t), SubExprs(subexprs),
645    NumArgs(numargs), RParenLoc(rparenloc) {}
646
647public:
648  CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
649           SourceLocation rparenloc);
650  ~CallExpr() {
651    delete [] SubExprs;
652  }
653
654  const Expr *getCallee() const { return cast<Expr>(SubExprs[FN]); }
655  Expr *getCallee() { return cast<Expr>(SubExprs[FN]); }
656  void setCallee(Expr *F) { SubExprs[FN] = F; }
657
658  /// getNumArgs - Return the number of actual arguments to this call.
659  ///
660  unsigned getNumArgs() const { return NumArgs; }
661
662  /// getArg - Return the specified argument.
663  Expr *getArg(unsigned Arg) {
664    assert(Arg < NumArgs && "Arg access out of range!");
665    return cast<Expr>(SubExprs[Arg+ARGS_START]);
666  }
667  const Expr *getArg(unsigned Arg) const {
668    assert(Arg < NumArgs && "Arg access out of range!");
669    return cast<Expr>(SubExprs[Arg+ARGS_START]);
670  }
671  /// setArg - Set the specified argument.
672  void setArg(unsigned Arg, Expr *ArgExpr) {
673    assert(Arg < NumArgs && "Arg access out of range!");
674    SubExprs[Arg+ARGS_START] = ArgExpr;
675  }
676
677  /// setNumArgs - This changes the number of arguments present in this call.
678  /// Any orphaned expressions are deleted by this, and any new operands are set
679  /// to null.
680  void setNumArgs(unsigned NumArgs);
681
682  typedef ExprIterator arg_iterator;
683  typedef ConstExprIterator const_arg_iterator;
684
685  arg_iterator arg_begin() { return SubExprs+ARGS_START; }
686  arg_iterator arg_end() { return SubExprs+ARGS_START+getNumArgs(); }
687  const_arg_iterator arg_begin() const { return SubExprs+ARGS_START; }
688  const_arg_iterator arg_end() const { return SubExprs+ARGS_START+getNumArgs();}
689
690  /// getNumCommas - Return the number of commas that must have been present in
691  /// this function call.
692  unsigned getNumCommas() const { return NumArgs ? NumArgs - 1 : 0; }
693
694  bool isBuiltinClassifyType(llvm::APSInt &Result) const;
695
696  /// isBuiltinConstantExpr - Return true if this built-in call is constant.
697  bool isBuiltinConstantExpr() const;
698
699  SourceLocation getRParenLoc() const { return RParenLoc; }
700
701  virtual SourceRange getSourceRange() const {
702    return SourceRange(getCallee()->getLocStart(), RParenLoc);
703  }
704
705  static bool classof(const Stmt *T) {
706    return T->getStmtClass() == CallExprClass;
707  }
708  static bool classof(const CallExpr *) { return true; }
709
710  // Iterators
711  virtual child_iterator child_begin();
712  virtual child_iterator child_end();
713
714  virtual void EmitImpl(llvm::Serializer& S) const;
715  static CallExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
716};
717
718/// MemberExpr - [C99 6.5.2.3] Structure and Union Members.
719///
720class MemberExpr : public Expr {
721  Stmt *Base;
722  FieldDecl *MemberDecl;
723  SourceLocation MemberLoc;
724  bool IsArrow;      // True if this is "X->F", false if this is "X.F".
725public:
726  MemberExpr(Expr *base, bool isarrow, FieldDecl *memberdecl, SourceLocation l,
727             QualType ty)
728    : Expr(MemberExprClass, ty),
729      Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow) {}
730
731  Expr *getBase() const { return cast<Expr>(Base); }
732  FieldDecl *getMemberDecl() const { return MemberDecl; }
733  bool isArrow() const { return IsArrow; }
734
735  virtual SourceRange getSourceRange() const {
736    return SourceRange(getBase()->getLocStart(), MemberLoc);
737  }
738
739  virtual SourceLocation getExprLoc() const { return MemberLoc; }
740
741  static bool classof(const Stmt *T) {
742    return T->getStmtClass() == MemberExprClass;
743  }
744  static bool classof(const MemberExpr *) { return true; }
745
746  // Iterators
747  virtual child_iterator child_begin();
748  virtual child_iterator child_end();
749
750  virtual void EmitImpl(llvm::Serializer& S) const;
751  static MemberExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
752};
753
754/// ExtVectorElementExpr - This represents access to specific elements of a
755/// vector, and may occur on the left hand side or right hand side.  For example
756/// the following is legal:  "V.xy = V.zw" if V is a 4 element extended vector.
757///
758class ExtVectorElementExpr : public Expr {
759  Stmt *Base;
760  IdentifierInfo &Accessor;
761  SourceLocation AccessorLoc;
762public:
763  ExtVectorElementExpr(QualType ty, Expr *base, IdentifierInfo &accessor,
764                       SourceLocation loc)
765    : Expr(ExtVectorElementExprClass, ty),
766      Base(base), Accessor(accessor), AccessorLoc(loc) {}
767
768  const Expr *getBase() const { return cast<Expr>(Base); }
769  Expr *getBase() { return cast<Expr>(Base); }
770
771  IdentifierInfo &getAccessor() const { return Accessor; }
772
773  /// getNumElements - Get the number of components being selected.
774  unsigned getNumElements() const;
775
776  /// containsDuplicateElements - Return true if any element access is
777  /// repeated.
778  bool containsDuplicateElements() const;
779
780  /// getEncodedElementAccess - Encode the elements accessed into an llvm
781  /// aggregate Constant of ConstantInt(s).
782  void getEncodedElementAccess(llvm::SmallVectorImpl<unsigned> &Elts) const;
783
784  virtual SourceRange getSourceRange() const {
785    return SourceRange(getBase()->getLocStart(), AccessorLoc);
786  }
787
788  static bool classof(const Stmt *T) {
789    return T->getStmtClass() == ExtVectorElementExprClass;
790  }
791  static bool classof(const ExtVectorElementExpr *) { return true; }
792
793  // Iterators
794  virtual child_iterator child_begin();
795  virtual child_iterator child_end();
796};
797
798/// CompoundLiteralExpr - [C99 6.5.2.5]
799///
800class CompoundLiteralExpr : public Expr {
801  /// LParenLoc - If non-null, this is the location of the left paren in a
802  /// compound literal like "(int){4}".  This can be null if this is a
803  /// synthesized compound expression.
804  SourceLocation LParenLoc;
805  Stmt *Init;
806  bool FileScope;
807public:
808  CompoundLiteralExpr(SourceLocation lparenloc, QualType ty, Expr *init, bool fileScope) :
809    Expr(CompoundLiteralExprClass, ty), LParenLoc(lparenloc), Init(init), FileScope(fileScope) {}
810
811  const Expr *getInitializer() const { return cast<Expr>(Init); }
812  Expr *getInitializer() { return cast<Expr>(Init); }
813
814  bool isFileScope() const { return FileScope; }
815
816  SourceLocation getLParenLoc() const { return LParenLoc; }
817
818  virtual SourceRange getSourceRange() const {
819    // FIXME: Init should never be null.
820    if (!Init)
821      return SourceRange();
822    if (LParenLoc.isInvalid())
823      return Init->getSourceRange();
824    return SourceRange(LParenLoc, Init->getLocEnd());
825  }
826
827  static bool classof(const Stmt *T) {
828    return T->getStmtClass() == CompoundLiteralExprClass;
829  }
830  static bool classof(const CompoundLiteralExpr *) { return true; }
831
832  // Iterators
833  virtual child_iterator child_begin();
834  virtual child_iterator child_end();
835
836  virtual void EmitImpl(llvm::Serializer& S) const;
837  static CompoundLiteralExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
838};
839
840/// CastExpr - Base class for Cast Operators (explicit, implicit, etc.).
841/// Classes that derive from CastExpr are:
842///
843///   ImplicitCastExpr
844///   ExplicitCastExpr
845///
846class CastExpr : public Expr {
847  Stmt *Op;
848protected:
849  CastExpr(StmtClass SC, QualType ty, Expr *op) :
850    Expr(SC, ty), Op(op) {}
851
852public:
853  Expr *getSubExpr() { return cast<Expr>(Op); }
854  const Expr *getSubExpr() const { return cast<Expr>(Op); }
855
856  static bool classof(const Stmt *T) {
857    switch (T->getStmtClass()) {
858    case ImplicitCastExprClass:
859    case ExplicitCastExprClass:
860    case CXXFunctionalCastExprClass:
861      return true;
862    default:
863      return false;
864    }
865  }
866  static bool classof(const CastExpr *) { return true; }
867
868  // Iterators
869  virtual child_iterator child_begin();
870  virtual child_iterator child_end();
871};
872
873/// ImplicitCastExpr - Allows us to explicitly represent implicit type
874/// conversions. For example: converting T[]->T*, void f()->void (*f)(),
875/// float->double, short->int, etc.
876///
877class ImplicitCastExpr : public CastExpr {
878public:
879  ImplicitCastExpr(QualType ty, Expr *op) :
880    CastExpr(ImplicitCastExprClass, ty, op) {}
881
882  virtual SourceRange getSourceRange() const {
883    return getSubExpr()->getSourceRange();
884  }
885
886  static bool classof(const Stmt *T) {
887    return T->getStmtClass() == ImplicitCastExprClass;
888  }
889  static bool classof(const ImplicitCastExpr *) { return true; }
890
891  virtual void EmitImpl(llvm::Serializer& S) const;
892  static ImplicitCastExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
893};
894
895/// ExplicitCastExpr - [C99 6.5.4] Cast Operators.
896///
897class ExplicitCastExpr : public CastExpr {
898  SourceLocation Loc; // the location of the left paren
899public:
900  ExplicitCastExpr(QualType ty, Expr *op, SourceLocation l) :
901    CastExpr(ExplicitCastExprClass, ty, op), Loc(l) {}
902
903  SourceLocation getLParenLoc() const { return Loc; }
904
905  virtual SourceRange getSourceRange() const {
906    return SourceRange(Loc, getSubExpr()->getSourceRange().getEnd());
907  }
908  static bool classof(const Stmt *T) {
909    return T->getStmtClass() == ExplicitCastExprClass;
910  }
911  static bool classof(const ExplicitCastExpr *) { return true; }
912
913  virtual void EmitImpl(llvm::Serializer& S) const;
914  static ExplicitCastExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
915};
916
917class BinaryOperator : public Expr {
918public:
919  enum Opcode {
920    // Operators listed in order of precedence.
921    // Note that additions to this should also update the StmtVisitor class.
922    Mul, Div, Rem,    // [C99 6.5.5] Multiplicative operators.
923    Add, Sub,         // [C99 6.5.6] Additive operators.
924    Shl, Shr,         // [C99 6.5.7] Bitwise shift operators.
925    LT, GT, LE, GE,   // [C99 6.5.8] Relational operators.
926    EQ, NE,           // [C99 6.5.9] Equality operators.
927    And,              // [C99 6.5.10] Bitwise AND operator.
928    Xor,              // [C99 6.5.11] Bitwise XOR operator.
929    Or,               // [C99 6.5.12] Bitwise OR operator.
930    LAnd,             // [C99 6.5.13] Logical AND operator.
931    LOr,              // [C99 6.5.14] Logical OR operator.
932    Assign, MulAssign,// [C99 6.5.16] Assignment operators.
933    DivAssign, RemAssign,
934    AddAssign, SubAssign,
935    ShlAssign, ShrAssign,
936    AndAssign, XorAssign,
937    OrAssign,
938    Comma             // [C99 6.5.17] Comma operator.
939  };
940private:
941  enum { LHS, RHS, END_EXPR };
942  Stmt* SubExprs[END_EXPR];
943  Opcode Opc;
944  SourceLocation OpLoc;
945public:
946
947  BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy,
948                 SourceLocation opLoc)
949    : Expr(BinaryOperatorClass, ResTy), Opc(opc), OpLoc(opLoc) {
950    SubExprs[LHS] = lhs;
951    SubExprs[RHS] = rhs;
952    assert(!isCompoundAssignmentOp() &&
953           "Use ArithAssignBinaryOperator for compound assignments");
954  }
955
956  SourceLocation getOperatorLoc() const { return OpLoc; }
957  Opcode getOpcode() const { return Opc; }
958  Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); }
959  Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); }
960  virtual SourceRange getSourceRange() const {
961    return SourceRange(getLHS()->getLocStart(), getRHS()->getLocEnd());
962  }
963
964  /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
965  /// corresponds to, e.g. "<<=".
966  static const char *getOpcodeStr(Opcode Op);
967
968  /// predicates to categorize the respective opcodes.
969  bool isMultiplicativeOp() const { return Opc >= Mul && Opc <= Rem; }
970  bool isAdditiveOp() const { return Opc == Add || Opc == Sub; }
971  bool isShiftOp() const { return Opc == Shl || Opc == Shr; }
972  bool isBitwiseOp() const { return Opc >= And && Opc <= Or; }
973
974  static bool isRelationalOp(Opcode Opc) { return Opc >= LT && Opc <= GE; }
975  bool isRelationalOp() const { return isRelationalOp(Opc); }
976
977  static bool isEqualityOp(Opcode Opc) { return Opc == EQ || Opc == NE; }
978  bool isEqualityOp() const { return isEqualityOp(Opc); }
979
980  static bool isLogicalOp(Opcode Opc) { return Opc == LAnd || Opc == LOr; }
981  bool isLogicalOp() const { return isLogicalOp(Opc); }
982
983  bool isAssignmentOp() const { return Opc >= Assign && Opc <= OrAssign; }
984  bool isCompoundAssignmentOp() const { return Opc > Assign && Opc <= OrAssign;}
985  bool isShiftAssignOp() const { return Opc == ShlAssign || Opc == ShrAssign; }
986
987  static bool classof(const Stmt *S) {
988    return S->getStmtClass() == BinaryOperatorClass ||
989           S->getStmtClass() == CompoundAssignOperatorClass;
990  }
991  static bool classof(const BinaryOperator *) { return true; }
992
993  // Iterators
994  virtual child_iterator child_begin();
995  virtual child_iterator child_end();
996
997  virtual void EmitImpl(llvm::Serializer& S) const;
998  static BinaryOperator* CreateImpl(llvm::Deserializer& D, ASTContext& C);
999
1000protected:
1001  BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy,
1002                 SourceLocation oploc, bool dead)
1003    : Expr(CompoundAssignOperatorClass, ResTy), Opc(opc), OpLoc(oploc) {
1004    SubExprs[LHS] = lhs;
1005    SubExprs[RHS] = rhs;
1006  }
1007};
1008
1009/// CompoundAssignOperator - For compound assignments (e.g. +=), we keep
1010/// track of the type the operation is performed in.  Due to the semantics of
1011/// these operators, the operands are promoted, the aritmetic performed, an
1012/// implicit conversion back to the result type done, then the assignment takes
1013/// place.  This captures the intermediate type which the computation is done
1014/// in.
1015class CompoundAssignOperator : public BinaryOperator {
1016  QualType ComputationType;
1017public:
1018  CompoundAssignOperator(Expr *lhs, Expr *rhs, Opcode opc,
1019                         QualType ResType, QualType CompType,
1020                         SourceLocation OpLoc)
1021    : BinaryOperator(lhs, rhs, opc, ResType, OpLoc, true),
1022      ComputationType(CompType) {
1023    assert(isCompoundAssignmentOp() &&
1024           "Only should be used for compound assignments");
1025  }
1026
1027  QualType getComputationType() const { return ComputationType; }
1028
1029  static bool classof(const CompoundAssignOperator *) { return true; }
1030  static bool classof(const Stmt *S) {
1031    return S->getStmtClass() == CompoundAssignOperatorClass;
1032  }
1033
1034  virtual void EmitImpl(llvm::Serializer& S) const;
1035  static CompoundAssignOperator* CreateImpl(llvm::Deserializer& D,
1036                                            ASTContext& C);
1037};
1038
1039/// ConditionalOperator - The ?: operator.  Note that LHS may be null when the
1040/// GNU "missing LHS" extension is in use.
1041///
1042class ConditionalOperator : public Expr {
1043  enum { COND, LHS, RHS, END_EXPR };
1044  Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides.
1045public:
1046  ConditionalOperator(Expr *cond, Expr *lhs, Expr *rhs, QualType t)
1047    : Expr(ConditionalOperatorClass, t) {
1048    SubExprs[COND] = cond;
1049    SubExprs[LHS] = lhs;
1050    SubExprs[RHS] = rhs;
1051  }
1052
1053  // getCond - Return the expression representing the condition for
1054  //  the ?: operator.
1055  Expr *getCond() const { return cast<Expr>(SubExprs[COND]); }
1056
1057  // getTrueExpr - Return the subexpression representing the value of the ?:
1058  //  expression if the condition evaluates to true.  In most cases this value
1059  //  will be the same as getLHS() except a GCC extension allows the left
1060  //  subexpression to be omitted, and instead of the condition be returned.
1061  //  e.g: x ?: y is shorthand for x ? x : y, except that the expression "x"
1062  //  is only evaluated once.
1063  Expr *getTrueExpr() const {
1064    return cast<Expr>(SubExprs[LHS] ? SubExprs[LHS] : SubExprs[COND]);
1065  }
1066
1067  // getTrueExpr - Return the subexpression representing the value of the ?:
1068  // expression if the condition evaluates to false. This is the same as getRHS.
1069  Expr *getFalseExpr() const { return cast<Expr>(SubExprs[RHS]); }
1070
1071  Expr *getLHS() const { return cast_or_null<Expr>(SubExprs[LHS]); }
1072  Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); }
1073
1074  virtual SourceRange getSourceRange() const {
1075    return SourceRange(getCond()->getLocStart(), getRHS()->getLocEnd());
1076  }
1077  static bool classof(const Stmt *T) {
1078    return T->getStmtClass() == ConditionalOperatorClass;
1079  }
1080  static bool classof(const ConditionalOperator *) { return true; }
1081
1082  // Iterators
1083  virtual child_iterator child_begin();
1084  virtual child_iterator child_end();
1085
1086  virtual void EmitImpl(llvm::Serializer& S) const;
1087  static ConditionalOperator* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1088};
1089
1090/// AddrLabelExpr - The GNU address of label extension, representing &&label.
1091class AddrLabelExpr : public Expr {
1092  SourceLocation AmpAmpLoc, LabelLoc;
1093  LabelStmt *Label;
1094public:
1095  AddrLabelExpr(SourceLocation AALoc, SourceLocation LLoc, LabelStmt *L,
1096                QualType t)
1097    : Expr(AddrLabelExprClass, t), AmpAmpLoc(AALoc), LabelLoc(LLoc), Label(L) {}
1098
1099  virtual SourceRange getSourceRange() const {
1100    return SourceRange(AmpAmpLoc, LabelLoc);
1101  }
1102
1103  LabelStmt *getLabel() const { return Label; }
1104
1105  static bool classof(const Stmt *T) {
1106    return T->getStmtClass() == AddrLabelExprClass;
1107  }
1108  static bool classof(const AddrLabelExpr *) { return true; }
1109
1110  // Iterators
1111  virtual child_iterator child_begin();
1112  virtual child_iterator child_end();
1113
1114  virtual void EmitImpl(llvm::Serializer& S) const;
1115  static AddrLabelExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1116};
1117
1118/// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}).
1119/// The StmtExpr contains a single CompoundStmt node, which it evaluates and
1120/// takes the value of the last subexpression.
1121class StmtExpr : public Expr {
1122  Stmt *SubStmt;
1123  SourceLocation LParenLoc, RParenLoc;
1124public:
1125  StmtExpr(CompoundStmt *substmt, QualType T,
1126           SourceLocation lp, SourceLocation rp) :
1127    Expr(StmtExprClass, T), SubStmt(substmt),  LParenLoc(lp), RParenLoc(rp) { }
1128
1129  CompoundStmt *getSubStmt() { return cast<CompoundStmt>(SubStmt); }
1130  const CompoundStmt *getSubStmt() const { return cast<CompoundStmt>(SubStmt); }
1131
1132  virtual SourceRange getSourceRange() const {
1133    return SourceRange(LParenLoc, RParenLoc);
1134  }
1135
1136  static bool classof(const Stmt *T) {
1137    return T->getStmtClass() == StmtExprClass;
1138  }
1139  static bool classof(const StmtExpr *) { return true; }
1140
1141  // Iterators
1142  virtual child_iterator child_begin();
1143  virtual child_iterator child_end();
1144
1145  virtual void EmitImpl(llvm::Serializer& S) const;
1146  static StmtExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1147};
1148
1149/// TypesCompatibleExpr - GNU builtin-in function __builtin_type_compatible_p.
1150/// This AST node represents a function that returns 1 if two *types* (not
1151/// expressions) are compatible. The result of this built-in function can be
1152/// used in integer constant expressions.
1153class TypesCompatibleExpr : public Expr {
1154  QualType Type1;
1155  QualType Type2;
1156  SourceLocation BuiltinLoc, RParenLoc;
1157public:
1158  TypesCompatibleExpr(QualType ReturnType, SourceLocation BLoc,
1159                      QualType t1, QualType t2, SourceLocation RP) :
1160    Expr(TypesCompatibleExprClass, ReturnType), Type1(t1), Type2(t2),
1161    BuiltinLoc(BLoc), RParenLoc(RP) {}
1162
1163  QualType getArgType1() const { return Type1; }
1164  QualType getArgType2() const { return Type2; }
1165
1166  virtual SourceRange getSourceRange() const {
1167    return SourceRange(BuiltinLoc, RParenLoc);
1168  }
1169  static bool classof(const Stmt *T) {
1170    return T->getStmtClass() == TypesCompatibleExprClass;
1171  }
1172  static bool classof(const TypesCompatibleExpr *) { return true; }
1173
1174  // Iterators
1175  virtual child_iterator child_begin();
1176  virtual child_iterator child_end();
1177};
1178
1179/// ShuffleVectorExpr - clang-specific builtin-in function
1180/// __builtin_shufflevector.
1181/// This AST node represents a operator that does a constant
1182/// shuffle, similar to LLVM's shufflevector instruction. It takes
1183/// two vectors and a variable number of constant indices,
1184/// and returns the appropriately shuffled vector.
1185class ShuffleVectorExpr : public Expr {
1186  SourceLocation BuiltinLoc, RParenLoc;
1187
1188  // SubExprs - the list of values passed to the __builtin_shufflevector
1189  // function. The first two are vectors, and the rest are constant
1190  // indices.  The number of values in this list is always
1191  // 2+the number of indices in the vector type.
1192  Stmt **SubExprs;
1193  unsigned NumExprs;
1194
1195public:
1196  ShuffleVectorExpr(Expr **args, unsigned nexpr,
1197                    QualType Type, SourceLocation BLoc,
1198                    SourceLocation RP) :
1199    Expr(ShuffleVectorExprClass, Type), BuiltinLoc(BLoc),
1200    RParenLoc(RP), NumExprs(nexpr) {
1201
1202    SubExprs = new Stmt*[nexpr];
1203    for (unsigned i = 0; i < nexpr; i++)
1204      SubExprs[i] = args[i];
1205  }
1206
1207  virtual SourceRange getSourceRange() const {
1208    return SourceRange(BuiltinLoc, RParenLoc);
1209  }
1210  static bool classof(const Stmt *T) {
1211    return T->getStmtClass() == ShuffleVectorExprClass;
1212  }
1213  static bool classof(const ShuffleVectorExpr *) { return true; }
1214
1215  ~ShuffleVectorExpr() {
1216    delete [] SubExprs;
1217  }
1218
1219  /// getNumSubExprs - Return the size of the SubExprs array.  This includes the
1220  /// constant expression, the actual arguments passed in, and the function
1221  /// pointers.
1222  unsigned getNumSubExprs() const { return NumExprs; }
1223
1224  /// getExpr - Return the Expr at the specified index.
1225  Expr *getExpr(unsigned Index) {
1226    assert((Index < NumExprs) && "Arg access out of range!");
1227    return cast<Expr>(SubExprs[Index]);
1228  }
1229  const Expr *getExpr(unsigned Index) const {
1230    assert((Index < NumExprs) && "Arg access out of range!");
1231    return cast<Expr>(SubExprs[Index]);
1232  }
1233
1234  unsigned getShuffleMaskIdx(ASTContext &Ctx, unsigned N) {
1235    assert((N < NumExprs - 2) && "Shuffle idx out of range!");
1236    return getExpr(N+2)->getIntegerConstantExprValue(Ctx).getZExtValue();
1237  }
1238
1239  // Iterators
1240  virtual child_iterator child_begin();
1241  virtual child_iterator child_end();
1242};
1243
1244/// ChooseExpr - GNU builtin-in function __builtin_choose_expr.
1245/// This AST node is similar to the conditional operator (?:) in C, with
1246/// the following exceptions:
1247/// - the test expression much be a constant expression.
1248/// - the expression returned has it's type unaltered by promotion rules.
1249/// - does not evaluate the expression that was not chosen.
1250class ChooseExpr : public Expr {
1251  enum { COND, LHS, RHS, END_EXPR };
1252  Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides.
1253  SourceLocation BuiltinLoc, RParenLoc;
1254public:
1255  ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, QualType t,
1256             SourceLocation RP)
1257    : Expr(ChooseExprClass, t),
1258      BuiltinLoc(BLoc), RParenLoc(RP) {
1259      SubExprs[COND] = cond;
1260      SubExprs[LHS] = lhs;
1261      SubExprs[RHS] = rhs;
1262    }
1263
1264  /// isConditionTrue - Return true if the condition is true.  This is always
1265  /// statically knowable for a well-formed choosexpr.
1266  bool isConditionTrue(ASTContext &C) const;
1267
1268  Expr *getCond() const { return cast<Expr>(SubExprs[COND]); }
1269  Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); }
1270  Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); }
1271
1272  virtual SourceRange getSourceRange() const {
1273    return SourceRange(BuiltinLoc, RParenLoc);
1274  }
1275  static bool classof(const Stmt *T) {
1276    return T->getStmtClass() == ChooseExprClass;
1277  }
1278  static bool classof(const ChooseExpr *) { return true; }
1279
1280  // Iterators
1281  virtual child_iterator child_begin();
1282  virtual child_iterator child_end();
1283};
1284
1285/// OverloadExpr - Clang builtin function __builtin_overload.
1286/// This AST node provides a way to overload functions in C.
1287///
1288/// The first argument is required to be a constant expression, for the number
1289/// of arguments passed to each candidate function.
1290///
1291/// The next N arguments, where N is the value of the constant expression,
1292/// are the values to be passed as arguments.
1293///
1294/// The rest of the arguments are values of pointer to function type, which
1295/// are the candidate functions for overloading.
1296///
1297/// The result is a equivalent to a CallExpr taking N arguments to the
1298/// candidate function whose parameter types match the types of the N arguments.
1299///
1300/// example: float Z = __builtin_overload(2, X, Y, modf, mod, modl);
1301/// If X and Y are long doubles, Z will assigned the result of modl(X, Y);
1302/// If X and Y are floats, Z will be assigned the result of modf(X, Y);
1303class OverloadExpr : public Expr {
1304  // SubExprs - the list of values passed to the __builtin_overload function.
1305  // SubExpr[0] is a constant expression
1306  // SubExpr[1-N] are the parameters to pass to the matching function call
1307  // SubExpr[N-...] are the candidate functions, of type pointer to function.
1308  Stmt **SubExprs;
1309
1310  // NumExprs - the size of the SubExprs array
1311  unsigned NumExprs;
1312
1313  // The index of the matching candidate function
1314  unsigned FnIndex;
1315
1316  SourceLocation BuiltinLoc;
1317  SourceLocation RParenLoc;
1318public:
1319  OverloadExpr(Expr **args, unsigned nexprs, unsigned idx, QualType t,
1320               SourceLocation bloc, SourceLocation rploc)
1321    : Expr(OverloadExprClass, t), NumExprs(nexprs), FnIndex(idx),
1322      BuiltinLoc(bloc), RParenLoc(rploc) {
1323    SubExprs = new Stmt*[nexprs];
1324    for (unsigned i = 0; i != nexprs; ++i)
1325      SubExprs[i] = args[i];
1326  }
1327  ~OverloadExpr() {
1328    delete [] SubExprs;
1329  }
1330
1331  /// arg_begin - Return a pointer to the list of arguments that will be passed
1332  /// to the matching candidate function, skipping over the initial constant
1333  /// expression.
1334  typedef ConstExprIterator const_arg_iterator;
1335  const_arg_iterator arg_begin() const { return &SubExprs[0]+1; }
1336  const_arg_iterator arg_end(ASTContext& Ctx) const {
1337    return &SubExprs[0]+1+getNumArgs(Ctx);
1338  }
1339
1340  /// getNumArgs - Return the number of arguments to pass to the candidate
1341  /// functions.
1342  unsigned getNumArgs(ASTContext &Ctx) const {
1343    return getExpr(0)->getIntegerConstantExprValue(Ctx).getZExtValue();
1344  }
1345
1346  /// getNumSubExprs - Return the size of the SubExprs array.  This includes the
1347  /// constant expression, the actual arguments passed in, and the function
1348  /// pointers.
1349  unsigned getNumSubExprs() const { return NumExprs; }
1350
1351  /// getExpr - Return the Expr at the specified index.
1352  Expr *getExpr(unsigned Index) const {
1353    assert((Index < NumExprs) && "Arg access out of range!");
1354    return cast<Expr>(SubExprs[Index]);
1355  }
1356
1357  /// getFn - Return the matching candidate function for this OverloadExpr.
1358  Expr *getFn() const { return cast<Expr>(SubExprs[FnIndex]); }
1359
1360  virtual SourceRange getSourceRange() const {
1361    return SourceRange(BuiltinLoc, RParenLoc);
1362  }
1363  static bool classof(const Stmt *T) {
1364    return T->getStmtClass() == OverloadExprClass;
1365  }
1366  static bool classof(const OverloadExpr *) { return true; }
1367
1368  // Iterators
1369  virtual child_iterator child_begin();
1370  virtual child_iterator child_end();
1371};
1372
1373/// VAArgExpr, used for the builtin function __builtin_va_start.
1374class VAArgExpr : public Expr {
1375  Stmt *Val;
1376  SourceLocation BuiltinLoc, RParenLoc;
1377public:
1378  VAArgExpr(SourceLocation BLoc, Expr* e, QualType t, SourceLocation RPLoc)
1379    : Expr(VAArgExprClass, t),
1380      Val(e),
1381      BuiltinLoc(BLoc),
1382      RParenLoc(RPLoc) { }
1383
1384  const Expr *getSubExpr() const { return cast<Expr>(Val); }
1385  Expr *getSubExpr() { return cast<Expr>(Val); }
1386  virtual SourceRange getSourceRange() const {
1387    return SourceRange(BuiltinLoc, RParenLoc);
1388  }
1389  static bool classof(const Stmt *T) {
1390    return T->getStmtClass() == VAArgExprClass;
1391  }
1392  static bool classof(const VAArgExpr *) { return true; }
1393
1394  // Iterators
1395  virtual child_iterator child_begin();
1396  virtual child_iterator child_end();
1397};
1398
1399/// InitListExpr - used for struct and array initializers, such as:
1400///    struct foo x = { 1, { 2, 3 } };
1401///
1402/// Because C is somewhat loose with braces, the AST does not necessarily
1403/// directly model the C source.  Instead, the semantic analyzer aims to make
1404/// the InitListExprs match up with the type of the decl being initialized.  We
1405/// have the following exceptions:
1406///
1407///  1. Elements at the end of the list may be dropped from the initializer.
1408///     These elements are defined to be initialized to zero.  For example:
1409///         int x[20] = { 1 };
1410///  2. Initializers may have excess initializers which are to be ignored by the
1411///     compiler.  For example:
1412///         int x[1] = { 1, 2 };
1413///  3. Redundant InitListExprs may be present around scalar elements.  These
1414///     always have a single element whose type is the same as the InitListExpr.
1415///     this can only happen for Type::isScalarType() types.
1416///         int x = { 1 };  int y[2] = { {1}, {2} };
1417///
1418class InitListExpr : public Expr {
1419  std::vector<Stmt *> InitExprs;
1420  SourceLocation LBraceLoc, RBraceLoc;
1421public:
1422  InitListExpr(SourceLocation lbraceloc, Expr **initexprs, unsigned numinits,
1423               SourceLocation rbraceloc);
1424
1425  unsigned getNumInits() const { return InitExprs.size(); }
1426
1427  const Expr* getInit(unsigned Init) const {
1428    assert(Init < getNumInits() && "Initializer access out of range!");
1429    return cast<Expr>(InitExprs[Init]);
1430  }
1431
1432  Expr* getInit(unsigned Init) {
1433    assert(Init < getNumInits() && "Initializer access out of range!");
1434    return cast<Expr>(InitExprs[Init]);
1435  }
1436
1437  void setInit(unsigned Init, Expr *expr) {
1438    assert(Init < getNumInits() && "Initializer access out of range!");
1439    InitExprs[Init] = expr;
1440  }
1441
1442  // Dynamic removal/addition (for constructing implicit InitExpr's).
1443  void removeInit(unsigned Init) {
1444    InitExprs.erase(InitExprs.begin()+Init);
1445  }
1446  void addInit(unsigned Init, Expr *expr) {
1447    InitExprs.insert(InitExprs.begin()+Init, expr);
1448  }
1449
1450  // Explicit InitListExpr's originate from source code (and have valid source
1451  // locations). Implicit InitListExpr's are created by the semantic analyzer.
1452  bool isExplicit() {
1453    return LBraceLoc.isValid() && RBraceLoc.isValid();
1454  }
1455
1456  virtual SourceRange getSourceRange() const {
1457    return SourceRange(LBraceLoc, RBraceLoc);
1458  }
1459  static bool classof(const Stmt *T) {
1460    return T->getStmtClass() == InitListExprClass;
1461  }
1462  static bool classof(const InitListExpr *) { return true; }
1463
1464  // Iterators
1465  virtual child_iterator child_begin();
1466  virtual child_iterator child_end();
1467
1468  virtual void EmitImpl(llvm::Serializer& S) const;
1469  static InitListExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1470
1471private:
1472  // Used by serializer.
1473  InitListExpr() : Expr(InitListExprClass, QualType()) {}
1474};
1475
1476}  // end namespace clang
1477
1478#endif
1479