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