Expr.h revision 8bf3b1d0e75da8b15bc331ebeb44f34b689f03ad
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  virtual SourceRange getSourceRange() const {
727    return SourceRange(getBase()->getLocStart(), AccessorLoc);
728  }
729
730  static bool classof(const Stmt *T) {
731    return T->getStmtClass() == ExtVectorElementExprClass;
732  }
733  static bool classof(const ExtVectorElementExpr *) { return true; }
734
735  // Iterators
736  virtual child_iterator child_begin();
737  virtual child_iterator child_end();
738};
739
740/// CompoundLiteralExpr - [C99 6.5.2.5]
741///
742class CompoundLiteralExpr : public Expr {
743  /// LParenLoc - If non-null, this is the location of the left paren in a
744  /// compound literal like "(int){4}".  This can be null if this is a
745  /// synthesized compound expression.
746  SourceLocation LParenLoc;
747  Expr *Init;
748  bool FileScope;
749public:
750  CompoundLiteralExpr(SourceLocation lparenloc, QualType ty, Expr *init, bool fileScope) :
751    Expr(CompoundLiteralExprClass, ty), LParenLoc(lparenloc), Init(init), FileScope(fileScope) {}
752
753  const Expr *getInitializer() const { return Init; }
754  Expr *getInitializer() { return Init; }
755
756  bool isFileScope() const { return FileScope; }
757
758  SourceLocation getLParenLoc() const { return LParenLoc; }
759
760  virtual SourceRange getSourceRange() const {
761    // FIXME: Init should never be null.
762    if (!Init)
763      return SourceRange();
764    if (LParenLoc.isInvalid())
765      return Init->getSourceRange();
766    return SourceRange(LParenLoc, Init->getLocEnd());
767  }
768
769  static bool classof(const Stmt *T) {
770    return T->getStmtClass() == CompoundLiteralExprClass;
771  }
772  static bool classof(const CompoundLiteralExpr *) { return true; }
773
774  // Iterators
775  virtual child_iterator child_begin();
776  virtual child_iterator child_end();
777
778  virtual void EmitImpl(llvm::Serializer& S) const;
779  static CompoundLiteralExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
780};
781
782/// ImplicitCastExpr - Allows us to explicitly represent implicit type
783/// conversions. For example: converting T[]->T*, void f()->void (*f)(),
784/// float->double, short->int, etc.
785///
786class ImplicitCastExpr : public Expr {
787  Expr *Op;
788public:
789  ImplicitCastExpr(QualType ty, Expr *op) :
790    Expr(ImplicitCastExprClass, ty), Op(op) {}
791
792  Expr *getSubExpr() { return Op; }
793  const Expr *getSubExpr() const { return Op; }
794
795  virtual SourceRange getSourceRange() const { return Op->getSourceRange(); }
796
797  static bool classof(const Stmt *T) {
798    return T->getStmtClass() == ImplicitCastExprClass;
799  }
800  static bool classof(const ImplicitCastExpr *) { return true; }
801
802  // Iterators
803  virtual child_iterator child_begin();
804  virtual child_iterator child_end();
805
806  virtual void EmitImpl(llvm::Serializer& S) const;
807  static ImplicitCastExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
808};
809
810/// CastExpr - [C99 6.5.4] Cast Operators.
811///
812class CastExpr : public Expr {
813  Expr *Op;
814  SourceLocation Loc; // the location of the left paren
815public:
816  CastExpr(QualType ty, Expr *op, SourceLocation l) :
817    Expr(CastExprClass, ty), Op(op), Loc(l) {}
818
819  SourceLocation getLParenLoc() const { return Loc; }
820
821  Expr *getSubExpr() const { return Op; }
822
823  virtual SourceRange getSourceRange() const {
824    return SourceRange(Loc, getSubExpr()->getSourceRange().getEnd());
825  }
826  static bool classof(const Stmt *T) {
827    return T->getStmtClass() == CastExprClass;
828  }
829  static bool classof(const CastExpr *) { return true; }
830
831  // Iterators
832  virtual child_iterator child_begin();
833  virtual child_iterator child_end();
834
835  virtual void EmitImpl(llvm::Serializer& S) const;
836  static CastExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
837};
838
839class BinaryOperator : public Expr {
840public:
841  enum Opcode {
842    // Operators listed in order of precedence.
843    // Note that additions to this should also update the StmtVisitor class.
844    Mul, Div, Rem,    // [C99 6.5.5] Multiplicative operators.
845    Add, Sub,         // [C99 6.5.6] Additive operators.
846    Shl, Shr,         // [C99 6.5.7] Bitwise shift operators.
847    LT, GT, LE, GE,   // [C99 6.5.8] Relational operators.
848    EQ, NE,           // [C99 6.5.9] Equality operators.
849    And,              // [C99 6.5.10] Bitwise AND operator.
850    Xor,              // [C99 6.5.11] Bitwise XOR operator.
851    Or,               // [C99 6.5.12] Bitwise OR operator.
852    LAnd,             // [C99 6.5.13] Logical AND operator.
853    LOr,              // [C99 6.5.14] Logical OR operator.
854    Assign, MulAssign,// [C99 6.5.16] Assignment operators.
855    DivAssign, RemAssign,
856    AddAssign, SubAssign,
857    ShlAssign, ShrAssign,
858    AndAssign, XorAssign,
859    OrAssign,
860    Comma             // [C99 6.5.17] Comma operator.
861  };
862private:
863  enum { LHS, RHS, END_EXPR };
864  Expr* SubExprs[END_EXPR];
865  Opcode Opc;
866  SourceLocation OpLoc;
867public:
868
869  BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy,
870                 SourceLocation opLoc)
871    : Expr(BinaryOperatorClass, ResTy), Opc(opc), OpLoc(opLoc) {
872    SubExprs[LHS] = lhs;
873    SubExprs[RHS] = rhs;
874    assert(!isCompoundAssignmentOp() &&
875           "Use ArithAssignBinaryOperator for compound assignments");
876  }
877
878  SourceLocation getOperatorLoc() const { return OpLoc; }
879  Opcode getOpcode() const { return Opc; }
880  Expr *getLHS() const { return SubExprs[LHS]; }
881  Expr *getRHS() const { return SubExprs[RHS]; }
882  virtual SourceRange getSourceRange() const {
883    return SourceRange(getLHS()->getLocStart(), getRHS()->getLocEnd());
884  }
885
886  /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
887  /// corresponds to, e.g. "<<=".
888  static const char *getOpcodeStr(Opcode Op);
889
890  /// predicates to categorize the respective opcodes.
891  bool isMultiplicativeOp() const { return Opc >= Mul && Opc <= Rem; }
892  bool isAdditiveOp() const { return Opc == Add || Opc == Sub; }
893  bool isShiftOp() const { return Opc == Shl || Opc == Shr; }
894  bool isBitwiseOp() const { return Opc >= And && Opc <= Or; }
895  bool isRelationalOp() const { return Opc >= LT && Opc <= GE; }
896  bool isEqualityOp() const { return Opc == EQ || Opc == NE; }
897  bool isLogicalOp() const { return Opc == LAnd || Opc == LOr; }
898  bool isAssignmentOp() const { return Opc >= Assign && Opc <= OrAssign; }
899  bool isCompoundAssignmentOp() const { return Opc > Assign && Opc <= OrAssign;}
900  bool isShiftAssignOp() const { return Opc == ShlAssign || Opc == ShrAssign; }
901
902  static bool classof(const Stmt *S) {
903    return S->getStmtClass() == BinaryOperatorClass ||
904           S->getStmtClass() == CompoundAssignOperatorClass;
905  }
906  static bool classof(const BinaryOperator *) { return true; }
907
908  // Iterators
909  virtual child_iterator child_begin();
910  virtual child_iterator child_end();
911
912  virtual void EmitImpl(llvm::Serializer& S) const;
913  static BinaryOperator* CreateImpl(llvm::Deserializer& D, ASTContext& C);
914
915protected:
916  BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy,
917                 SourceLocation oploc, bool dead)
918    : Expr(CompoundAssignOperatorClass, ResTy), Opc(opc), OpLoc(oploc) {
919    SubExprs[LHS] = lhs;
920    SubExprs[RHS] = rhs;
921  }
922};
923
924/// CompoundAssignOperator - For compound assignments (e.g. +=), we keep
925/// track of the type the operation is performed in.  Due to the semantics of
926/// these operators, the operands are promoted, the aritmetic performed, an
927/// implicit conversion back to the result type done, then the assignment takes
928/// place.  This captures the intermediate type which the computation is done
929/// in.
930class CompoundAssignOperator : public BinaryOperator {
931  QualType ComputationType;
932public:
933  CompoundAssignOperator(Expr *lhs, Expr *rhs, Opcode opc,
934                         QualType ResType, QualType CompType,
935                         SourceLocation OpLoc)
936    : BinaryOperator(lhs, rhs, opc, ResType, OpLoc, true),
937      ComputationType(CompType) {
938    assert(isCompoundAssignmentOp() &&
939           "Only should be used for compound assignments");
940  }
941
942  QualType getComputationType() const { return ComputationType; }
943
944  static bool classof(const CompoundAssignOperator *) { return true; }
945  static bool classof(const Stmt *S) {
946    return S->getStmtClass() == CompoundAssignOperatorClass;
947  }
948
949  virtual void EmitImpl(llvm::Serializer& S) const;
950  static CompoundAssignOperator* CreateImpl(llvm::Deserializer& D,
951                                            ASTContext& C);
952};
953
954/// ConditionalOperator - The ?: operator.  Note that LHS may be null when the
955/// GNU "missing LHS" extension is in use.
956///
957class ConditionalOperator : public Expr {
958  enum { COND, LHS, RHS, END_EXPR };
959  Expr* SubExprs[END_EXPR]; // Left/Middle/Right hand sides.
960public:
961  ConditionalOperator(Expr *cond, Expr *lhs, Expr *rhs, QualType t)
962    : Expr(ConditionalOperatorClass, t) {
963    SubExprs[COND] = cond;
964    SubExprs[LHS] = lhs;
965    SubExprs[RHS] = rhs;
966  }
967
968  // getCond - Return the expression representing the condition for
969  //  the ?: operator.
970  Expr *getCond() const { return SubExprs[COND]; }
971
972  // getTrueExpr - Return the subexpression representing the value of the ?:
973  //  expression if the condition evaluates to true.  In most cases this value
974  //  will be the same as getLHS() except a GCC extension allows the left
975  //  subexpression to be omitted, and instead of the condition be returned.
976  //  e.g: x ?: y is shorthand for x ? x : y, except that the expression "x"
977  //  is only evaluated once.
978  Expr *getTrueExpr() const {
979    return SubExprs[LHS] ? SubExprs[COND] : SubExprs[LHS];
980  }
981
982  // getTrueExpr - Return the subexpression representing the value of the ?:
983  // expression if the condition evaluates to false. This is the same as getRHS.
984  Expr *getFalseExpr() const { return SubExprs[RHS]; }
985
986  Expr *getLHS() const { return SubExprs[LHS]; }
987  Expr *getRHS() const { return SubExprs[RHS]; }
988
989  virtual SourceRange getSourceRange() const {
990    return SourceRange(getCond()->getLocStart(), getRHS()->getLocEnd());
991  }
992  static bool classof(const Stmt *T) {
993    return T->getStmtClass() == ConditionalOperatorClass;
994  }
995  static bool classof(const ConditionalOperator *) { return true; }
996
997  // Iterators
998  virtual child_iterator child_begin();
999  virtual child_iterator child_end();
1000
1001  virtual void EmitImpl(llvm::Serializer& S) const;
1002  static ConditionalOperator* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1003};
1004
1005/// AddrLabelExpr - The GNU address of label extension, representing &&label.
1006class AddrLabelExpr : public Expr {
1007  SourceLocation AmpAmpLoc, LabelLoc;
1008  LabelStmt *Label;
1009public:
1010  AddrLabelExpr(SourceLocation AALoc, SourceLocation LLoc, LabelStmt *L,
1011                QualType t)
1012    : Expr(AddrLabelExprClass, t), AmpAmpLoc(AALoc), LabelLoc(LLoc), Label(L) {}
1013
1014  virtual SourceRange getSourceRange() const {
1015    return SourceRange(AmpAmpLoc, LabelLoc);
1016  }
1017
1018  LabelStmt *getLabel() const { return Label; }
1019
1020  static bool classof(const Stmt *T) {
1021    return T->getStmtClass() == AddrLabelExprClass;
1022  }
1023  static bool classof(const AddrLabelExpr *) { return true; }
1024
1025  // Iterators
1026  virtual child_iterator child_begin();
1027  virtual child_iterator child_end();
1028
1029  virtual void EmitImpl(llvm::Serializer& S) const;
1030  static AddrLabelExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1031};
1032
1033/// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}).
1034/// The StmtExpr contains a single CompoundStmt node, which it evaluates and
1035/// takes the value of the last subexpression.
1036class StmtExpr : public Expr {
1037  CompoundStmt *SubStmt;
1038  SourceLocation LParenLoc, RParenLoc;
1039public:
1040  StmtExpr(CompoundStmt *substmt, QualType T,
1041           SourceLocation lp, SourceLocation rp) :
1042    Expr(StmtExprClass, T), SubStmt(substmt),  LParenLoc(lp), RParenLoc(rp) { }
1043
1044  CompoundStmt *getSubStmt() { return SubStmt; }
1045  const CompoundStmt *getSubStmt() const { return SubStmt; }
1046
1047  virtual SourceRange getSourceRange() const {
1048    return SourceRange(LParenLoc, RParenLoc);
1049  }
1050
1051  static bool classof(const Stmt *T) {
1052    return T->getStmtClass() == StmtExprClass;
1053  }
1054  static bool classof(const StmtExpr *) { return true; }
1055
1056  // Iterators
1057  virtual child_iterator child_begin();
1058  virtual child_iterator child_end();
1059
1060  virtual void EmitImpl(llvm::Serializer& S) const;
1061  static StmtExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1062};
1063
1064/// TypesCompatibleExpr - GNU builtin-in function __builtin_type_compatible_p.
1065/// This AST node represents a function that returns 1 if two *types* (not
1066/// expressions) are compatible. The result of this built-in function can be
1067/// used in integer constant expressions.
1068class TypesCompatibleExpr : public Expr {
1069  QualType Type1;
1070  QualType Type2;
1071  SourceLocation BuiltinLoc, RParenLoc;
1072public:
1073  TypesCompatibleExpr(QualType ReturnType, SourceLocation BLoc,
1074                      QualType t1, QualType t2, SourceLocation RP) :
1075    Expr(TypesCompatibleExprClass, ReturnType), Type1(t1), Type2(t2),
1076    BuiltinLoc(BLoc), RParenLoc(RP) {}
1077
1078  QualType getArgType1() const { return Type1; }
1079  QualType getArgType2() const { return Type2; }
1080
1081  virtual SourceRange getSourceRange() const {
1082    return SourceRange(BuiltinLoc, RParenLoc);
1083  }
1084  static bool classof(const Stmt *T) {
1085    return T->getStmtClass() == TypesCompatibleExprClass;
1086  }
1087  static bool classof(const TypesCompatibleExpr *) { return true; }
1088
1089  // Iterators
1090  virtual child_iterator child_begin();
1091  virtual child_iterator child_end();
1092};
1093
1094/// ShuffleVectorExpr - clang-specific builtin-in function
1095/// __builtin_shufflevector.
1096/// This AST node represents a operator that does a constant
1097/// shuffle, similar to LLVM's shufflevector instruction. It takes
1098/// two vectors and a variable number of constant indices,
1099/// and returns the appropriately shuffled vector.
1100class ShuffleVectorExpr : public Expr {
1101  SourceLocation BuiltinLoc, RParenLoc;
1102
1103  // SubExprs - the list of values passed to the __builtin_shufflevector
1104  // function. The first two are vectors, and the rest are constant
1105  // indices.  The number of values in this list is always
1106  // 2+the number of indices in the vector type.
1107  Expr **SubExprs;
1108  unsigned NumExprs;
1109
1110public:
1111  ShuffleVectorExpr(Expr **args, unsigned nexpr,
1112                    QualType Type, SourceLocation BLoc,
1113                    SourceLocation RP) :
1114    Expr(ShuffleVectorExprClass, Type), BuiltinLoc(BLoc),
1115    RParenLoc(RP), NumExprs(nexpr) {
1116
1117    SubExprs = new Expr*[nexpr];
1118    for (unsigned i = 0; i < nexpr; i++)
1119      SubExprs[i] = args[i];
1120  }
1121
1122  virtual SourceRange getSourceRange() const {
1123    return SourceRange(BuiltinLoc, RParenLoc);
1124  }
1125  static bool classof(const Stmt *T) {
1126    return T->getStmtClass() == ShuffleVectorExprClass;
1127  }
1128  static bool classof(const ShuffleVectorExpr *) { return true; }
1129
1130  ~ShuffleVectorExpr() {
1131    delete [] SubExprs;
1132  }
1133
1134  /// getNumSubExprs - Return the size of the SubExprs array.  This includes the
1135  /// constant expression, the actual arguments passed in, and the function
1136  /// pointers.
1137  unsigned getNumSubExprs() const { return NumExprs; }
1138
1139  /// getExpr - Return the Expr at the specified index.
1140  Expr *getExpr(unsigned Index) {
1141    assert((Index < NumExprs) && "Arg access out of range!");
1142    return SubExprs[Index];
1143  }
1144  const Expr *getExpr(unsigned Index) const {
1145    assert((Index < NumExprs) && "Arg access out of range!");
1146    return SubExprs[Index];
1147  }
1148
1149  unsigned getShuffleMaskIdx(ASTContext &Ctx, unsigned N) {
1150    assert((N < NumExprs - 2) && "Shuffle idx out of range!");
1151    llvm::APSInt Result(32);
1152    bool result = getExpr(N+2)->isIntegerConstantExpr(Result, Ctx);
1153    assert(result && "Must be integer constant");
1154    return Result.getZExtValue();
1155  }
1156
1157  // Iterators
1158  virtual child_iterator child_begin();
1159  virtual child_iterator child_end();
1160};
1161
1162/// ChooseExpr - GNU builtin-in function __builtin_choose_expr.
1163/// This AST node is similar to the conditional operator (?:) in C, with
1164/// the following exceptions:
1165/// - the test expression much be a constant expression.
1166/// - the expression returned has it's type unaltered by promotion rules.
1167/// - does not evaluate the expression that was not chosen.
1168class ChooseExpr : public Expr {
1169  enum { COND, LHS, RHS, END_EXPR };
1170  Expr* SubExprs[END_EXPR]; // Left/Middle/Right hand sides.
1171  SourceLocation BuiltinLoc, RParenLoc;
1172public:
1173  ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, QualType t,
1174             SourceLocation RP)
1175    : Expr(ChooseExprClass, t),
1176      BuiltinLoc(BLoc), RParenLoc(RP) {
1177      SubExprs[COND] = cond;
1178      SubExprs[LHS] = lhs;
1179      SubExprs[RHS] = rhs;
1180    }
1181
1182  /// isConditionTrue - Return true if the condition is true.  This is always
1183  /// statically knowable for a well-formed choosexpr.
1184  bool isConditionTrue(ASTContext &C) const;
1185
1186  Expr *getCond() const { return SubExprs[COND]; }
1187  Expr *getLHS() const { return SubExprs[LHS]; }
1188  Expr *getRHS() const { return SubExprs[RHS]; }
1189
1190  virtual SourceRange getSourceRange() const {
1191    return SourceRange(BuiltinLoc, RParenLoc);
1192  }
1193  static bool classof(const Stmt *T) {
1194    return T->getStmtClass() == ChooseExprClass;
1195  }
1196  static bool classof(const ChooseExpr *) { return true; }
1197
1198  // Iterators
1199  virtual child_iterator child_begin();
1200  virtual child_iterator child_end();
1201};
1202
1203/// OverloadExpr - Clang builtin function __builtin_overload.
1204/// This AST node provides a way to overload functions in C.
1205///
1206/// The first argument is required to be a constant expression, for the number
1207/// of arguments passed to each candidate function.
1208///
1209/// The next N arguments, where N is the value of the constant expression,
1210/// are the values to be passed as arguments.
1211///
1212/// The rest of the arguments are values of pointer to function type, which
1213/// are the candidate functions for overloading.
1214///
1215/// The result is a equivalent to a CallExpr taking N arguments to the
1216/// candidate function whose parameter types match the types of the N arguments.
1217///
1218/// example: float Z = __builtin_overload(2, X, Y, modf, mod, modl);
1219/// If X and Y are long doubles, Z will assigned the result of modl(X, Y);
1220/// If X and Y are floats, Z will be assigned the result of modf(X, Y);
1221class OverloadExpr : public Expr {
1222  // SubExprs - the list of values passed to the __builtin_overload function.
1223  // SubExpr[0] is a constant expression
1224  // SubExpr[1-N] are the parameters to pass to the matching function call
1225  // SubExpr[N-...] are the candidate functions, of type pointer to function.
1226  Expr **SubExprs;
1227
1228  // NumExprs - the size of the SubExprs array
1229  unsigned NumExprs;
1230
1231  // The index of the matching candidate function
1232  unsigned FnIndex;
1233
1234  SourceLocation BuiltinLoc;
1235  SourceLocation RParenLoc;
1236public:
1237  OverloadExpr(Expr **args, unsigned nexprs, unsigned idx, QualType t,
1238               SourceLocation bloc, SourceLocation rploc)
1239    : Expr(OverloadExprClass, t), NumExprs(nexprs), FnIndex(idx),
1240      BuiltinLoc(bloc), RParenLoc(rploc) {
1241    SubExprs = new Expr*[nexprs];
1242    for (unsigned i = 0; i != nexprs; ++i)
1243      SubExprs[i] = args[i];
1244  }
1245  ~OverloadExpr() {
1246    delete [] SubExprs;
1247  }
1248
1249  /// arg_begin - Return a pointer to the list of arguments that will be passed
1250  /// to the matching candidate function, skipping over the initial constant
1251  /// expression.
1252  typedef Expr * const *arg_const_iterator;
1253  arg_const_iterator arg_begin() const { return SubExprs+1; }
1254
1255  /// getNumArgs - Return the number of arguments to pass to the candidate
1256  /// functions.
1257  unsigned getNumArgs(ASTContext &Ctx) const {
1258    llvm::APSInt constEval(32);
1259    (void) SubExprs[0]->isIntegerConstantExpr(constEval, Ctx);
1260    return constEval.getZExtValue();
1261  }
1262
1263  /// getNumSubExprs - Return the size of the SubExprs array.  This includes the
1264  /// constant expression, the actual arguments passed in, and the function
1265  /// pointers.
1266  unsigned getNumSubExprs() const { return NumExprs; }
1267
1268  /// getExpr - Return the Expr at the specified index.
1269  Expr *getExpr(unsigned Index) {
1270    assert((Index < NumExprs) && "Arg access out of range!");
1271    return SubExprs[Index];
1272  }
1273
1274  /// getFn - Return the matching candidate function for this OverloadExpr.
1275  Expr *getFn() const { return SubExprs[FnIndex]; }
1276
1277  virtual SourceRange getSourceRange() const {
1278    return SourceRange(BuiltinLoc, RParenLoc);
1279  }
1280  static bool classof(const Stmt *T) {
1281    return T->getStmtClass() == OverloadExprClass;
1282  }
1283  static bool classof(const OverloadExpr *) { return true; }
1284
1285  // Iterators
1286  virtual child_iterator child_begin();
1287  virtual child_iterator child_end();
1288};
1289
1290/// VAArgExpr, used for the builtin function __builtin_va_start.
1291class VAArgExpr : public Expr {
1292  Expr *Val;
1293  SourceLocation BuiltinLoc, RParenLoc;
1294public:
1295  VAArgExpr(SourceLocation BLoc, Expr* e, QualType t, SourceLocation RPLoc)
1296    : Expr(VAArgExprClass, t),
1297      Val(e),
1298      BuiltinLoc(BLoc),
1299      RParenLoc(RPLoc) { }
1300
1301  const Expr *getSubExpr() const { return Val; }
1302  Expr *getSubExpr() { return Val; }
1303  virtual SourceRange getSourceRange() const {
1304    return SourceRange(BuiltinLoc, RParenLoc);
1305  }
1306  static bool classof(const Stmt *T) {
1307    return T->getStmtClass() == VAArgExprClass;
1308  }
1309  static bool classof(const VAArgExpr *) { return true; }
1310
1311  // Iterators
1312  virtual child_iterator child_begin();
1313  virtual child_iterator child_end();
1314};
1315
1316/// InitListExpr - used for struct and array initializers, such as:
1317///    struct foo x = { 1, { 2, 3 } };
1318///
1319/// Because C is somewhat loose with braces, the AST does not necessarily
1320/// directly model the C source.  Instead, the semantic analyzer aims to make
1321/// the InitListExprs match up with the type of the decl being initialized.  We
1322/// have the following exceptions:
1323///
1324///  1. Elements at the end of the list may be dropped from the initializer.
1325///     These elements are defined to be initialized to zero.  For example:
1326///         int x[20] = { 1 };
1327///  2. Initializers may have excess initializers which are to be ignored by the
1328///     compiler.  For example:
1329///         int x[1] = { 1, 2 };
1330///  3. Redundant InitListExprs may be present around scalar elements.  These
1331///     always have a single element whose type is the same as the InitListExpr.
1332///     this can only happen for Type::isScalarType() types.
1333///         int x = { 1 };  int y[2] = { {1}, {2} };
1334///
1335class InitListExpr : public Expr {
1336  std::vector<Expr *> InitExprs;
1337  SourceLocation LBraceLoc, RBraceLoc;
1338public:
1339  InitListExpr(SourceLocation lbraceloc, Expr **initexprs, unsigned numinits,
1340               SourceLocation rbraceloc);
1341
1342  unsigned getNumInits() const { return InitExprs.size(); }
1343
1344  const Expr* getInit(unsigned Init) const {
1345    assert(Init < getNumInits() && "Initializer access out of range!");
1346    return InitExprs[Init];
1347  }
1348
1349  Expr* getInit(unsigned Init) {
1350    assert(Init < getNumInits() && "Initializer access out of range!");
1351    return InitExprs[Init];
1352  }
1353
1354  void setInit(unsigned Init, Expr *expr) {
1355    assert(Init < getNumInits() && "Initializer access out of range!");
1356    InitExprs[Init] = expr;
1357  }
1358
1359  // Dynamic removal/addition (for constructing implicit InitExpr's).
1360  void removeInit(unsigned Init) {
1361    InitExprs.erase(InitExprs.begin()+Init);
1362  }
1363  void addInit(unsigned Init, Expr *expr) {
1364    InitExprs.insert(InitExprs.begin()+Init, expr);
1365  }
1366
1367  // Explicit InitListExpr's originate from source code (and have valid source
1368  // locations). Implicit InitListExpr's are created by the semantic analyzer.
1369  bool isExplicit() {
1370    return LBraceLoc.isValid() && RBraceLoc.isValid();
1371  }
1372
1373  virtual SourceRange getSourceRange() const {
1374    return SourceRange(LBraceLoc, RBraceLoc);
1375  }
1376  static bool classof(const Stmt *T) {
1377    return T->getStmtClass() == InitListExprClass;
1378  }
1379  static bool classof(const InitListExpr *) { return true; }
1380
1381  // Iterators
1382  virtual child_iterator child_begin();
1383  virtual child_iterator child_end();
1384
1385  virtual void EmitImpl(llvm::Serializer& S) const;
1386  static InitListExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1387
1388private:
1389  // Used by serializer.
1390  InitListExpr() : Expr(InitListExprClass, QualType()) {}
1391};
1392
1393/// ObjCStringLiteral, used for Objective-C string literals
1394/// i.e. @"foo".
1395class ObjCStringLiteral : public Expr {
1396  StringLiteral *String;
1397  SourceLocation AtLoc;
1398public:
1399  ObjCStringLiteral(StringLiteral *SL, QualType T, SourceLocation L)
1400    : Expr(ObjCStringLiteralClass, T), String(SL), AtLoc(L) {}
1401
1402  StringLiteral* getString() { return String; }
1403
1404  const StringLiteral* getString() const { return String; }
1405
1406  SourceLocation getAtLoc() const { return AtLoc; }
1407
1408  virtual SourceRange getSourceRange() const {
1409    return SourceRange(AtLoc, String->getLocEnd());
1410  }
1411
1412  static bool classof(const Stmt *T) {
1413    return T->getStmtClass() == ObjCStringLiteralClass;
1414  }
1415  static bool classof(const ObjCStringLiteral *) { return true; }
1416
1417  // Iterators
1418  virtual child_iterator child_begin();
1419  virtual child_iterator child_end();
1420
1421  virtual void EmitImpl(llvm::Serializer& S) const;
1422  static ObjCStringLiteral* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1423};
1424
1425/// ObjCEncodeExpr, used for @encode in Objective-C.
1426class ObjCEncodeExpr : public Expr {
1427  QualType EncType;
1428  SourceLocation AtLoc, RParenLoc;
1429public:
1430  ObjCEncodeExpr(QualType T, QualType ET,
1431                 SourceLocation at, SourceLocation rp)
1432    : Expr(ObjCEncodeExprClass, T), EncType(ET), AtLoc(at), RParenLoc(rp) {}
1433
1434  SourceLocation getAtLoc() const { return AtLoc; }
1435  SourceLocation getRParenLoc() const { return RParenLoc; }
1436
1437  virtual SourceRange getSourceRange() const {
1438    return SourceRange(AtLoc, RParenLoc);
1439  }
1440
1441  QualType getEncodedType() const { return EncType; }
1442
1443  static bool classof(const Stmt *T) {
1444    return T->getStmtClass() == ObjCEncodeExprClass;
1445  }
1446  static bool classof(const ObjCEncodeExpr *) { return true; }
1447
1448  // Iterators
1449  virtual child_iterator child_begin();
1450  virtual child_iterator child_end();
1451
1452  virtual void EmitImpl(llvm::Serializer& S) const;
1453  static ObjCEncodeExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1454};
1455
1456/// ObjCSelectorExpr used for @selector in Objective-C.
1457class ObjCSelectorExpr : public Expr {
1458  Selector SelName;
1459  SourceLocation AtLoc, RParenLoc;
1460public:
1461  ObjCSelectorExpr(QualType T, Selector selInfo,
1462                   SourceLocation at, SourceLocation rp)
1463  : Expr(ObjCSelectorExprClass, T), SelName(selInfo),
1464  AtLoc(at), RParenLoc(rp) {}
1465
1466  Selector getSelector() const { return SelName; }
1467
1468  SourceLocation getAtLoc() const { return AtLoc; }
1469  SourceLocation getRParenLoc() const { return RParenLoc; }
1470
1471  virtual SourceRange getSourceRange() const {
1472    return SourceRange(AtLoc, RParenLoc);
1473  }
1474
1475  /// getNumArgs - Return the number of actual arguments to this call.
1476  unsigned getNumArgs() const { return SelName.getNumArgs(); }
1477
1478  static bool classof(const Stmt *T) {
1479    return T->getStmtClass() == ObjCSelectorExprClass;
1480  }
1481  static bool classof(const ObjCSelectorExpr *) { return true; }
1482
1483  // Iterators
1484  virtual child_iterator child_begin();
1485  virtual child_iterator child_end();
1486
1487  virtual void EmitImpl(llvm::Serializer& S) const;
1488  static ObjCSelectorExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1489};
1490
1491/// ObjCProtocolExpr used for protocol in Objective-C.
1492class ObjCProtocolExpr : public Expr {
1493  ObjCProtocolDecl *Protocol;
1494  SourceLocation AtLoc, RParenLoc;
1495public:
1496  ObjCProtocolExpr(QualType T, ObjCProtocolDecl *protocol,
1497                   SourceLocation at, SourceLocation rp)
1498  : Expr(ObjCProtocolExprClass, T), Protocol(protocol),
1499  AtLoc(at), RParenLoc(rp) {}
1500
1501  ObjCProtocolDecl *getProtocol() const { return Protocol; }
1502
1503  SourceLocation getAtLoc() const { return AtLoc; }
1504  SourceLocation getRParenLoc() const { return RParenLoc; }
1505
1506  virtual SourceRange getSourceRange() const {
1507    return SourceRange(AtLoc, RParenLoc);
1508  }
1509
1510  static bool classof(const Stmt *T) {
1511    return T->getStmtClass() == ObjCProtocolExprClass;
1512  }
1513  static bool classof(const ObjCProtocolExpr *) { return true; }
1514
1515  // Iterators
1516  virtual child_iterator child_begin();
1517  virtual child_iterator child_end();
1518};
1519
1520/// ObjCIvarRefExpr - A reference to an ObjC instance variable.
1521class ObjCIvarRefExpr : public Expr {
1522  class ObjCIvarDecl *D;
1523  SourceLocation Loc;
1524  Expr *Base;
1525  bool IsArrow:1;      // True if this is "X->F", false if this is "X.F".
1526  bool IsFreeIvar:1;   // True if ivar reference has no base (self assumed).
1527
1528public:
1529  ObjCIvarRefExpr(ObjCIvarDecl *d, QualType t, SourceLocation l, Expr *base=0,
1530                  bool arrow = false, bool freeIvar = false) :
1531    Expr(ObjCIvarRefExprClass, t), D(d), Loc(l), Base(base), IsArrow(arrow),
1532    IsFreeIvar(freeIvar) {}
1533
1534  ObjCIvarDecl *getDecl() { return D; }
1535  const ObjCIvarDecl *getDecl() const { return D; }
1536  virtual SourceRange getSourceRange() const {
1537    return isFreeIvar() ? SourceRange(Loc)
1538                        : SourceRange(getBase()->getLocStart(), Loc);
1539  }
1540  const Expr *getBase() const { return Base; }
1541  Expr *getBase() { return Base; }
1542  void setBase(Expr * base) { Base = base; }
1543  bool isArrow() const { return IsArrow; }
1544  bool isFreeIvar() const { return IsFreeIvar; }
1545
1546  SourceLocation getLocation() const { return Loc; }
1547
1548  static bool classof(const Stmt *T) {
1549    return T->getStmtClass() == ObjCIvarRefExprClass;
1550  }
1551  static bool classof(const ObjCIvarRefExpr *) { return true; }
1552
1553  // Iterators
1554  virtual child_iterator child_begin();
1555  virtual child_iterator child_end();
1556
1557  virtual void EmitImpl(llvm::Serializer& S) const;
1558  static ObjCIvarRefExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1559};
1560
1561class ObjCMessageExpr : public Expr {
1562  enum { RECEIVER=0, ARGS_START=1 };
1563
1564  Expr **SubExprs;
1565
1566  unsigned NumArgs;
1567
1568  // A unigue name for this message.
1569  Selector SelName;
1570
1571  // A method prototype for this message (optional).
1572  // FIXME: Since method decls contain the selector, and most messages have a
1573  // prototype, consider devising a scheme for unifying SelName/MethodProto.
1574  ObjCMethodDecl *MethodProto;
1575
1576  SourceLocation LBracloc, RBracloc;
1577
1578  // constructor used during deserialization
1579  ObjCMessageExpr(Selector selInfo, QualType retType,
1580                  SourceLocation LBrac, SourceLocation RBrac,
1581                  Expr **ArgExprs, unsigned nargs)
1582  : Expr(ObjCMessageExprClass, retType), NumArgs(nargs), SelName(selInfo),
1583    MethodProto(NULL), LBracloc(LBrac), RBracloc(RBrac) {}
1584
1585public:
1586  // constructor for class messages.
1587  // FIXME: clsName should be typed to ObjCInterfaceType
1588  ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
1589                  QualType retType, ObjCMethodDecl *methDecl,
1590                  SourceLocation LBrac, SourceLocation RBrac,
1591                  Expr **ArgExprs, unsigned NumArgs);
1592  // constructor for instance messages.
1593  ObjCMessageExpr(Expr *receiver, Selector selInfo,
1594                  QualType retType, ObjCMethodDecl *methDecl,
1595                  SourceLocation LBrac, SourceLocation RBrac,
1596                  Expr **ArgExprs, unsigned NumArgs);
1597
1598  ~ObjCMessageExpr() {
1599    delete [] SubExprs;
1600  }
1601
1602  /// getReceiver - Returns the receiver of the message expression.
1603  ///  This can be NULL if the message is for instance methods.  For
1604  ///  instance methods, use getClassName.
1605  Expr *getReceiver() {
1606    uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1607    return x & 0x1 ? NULL : (Expr*) x;
1608  }
1609  const Expr *getReceiver() const {
1610    return const_cast<ObjCMessageExpr*>(this)->getReceiver();
1611  }
1612
1613  Selector getSelector() const { return SelName; }
1614
1615  const ObjCMethodDecl *getMethodDecl() const { return MethodProto; }
1616  ObjCMethodDecl *getMethodDecl() { return MethodProto; }
1617
1618  /// getClassName - For instance methods, this returns the invoked class,
1619  ///  and returns NULL otherwise.  For regular methods, use getReceiver.
1620  IdentifierInfo *getClassName() {
1621    uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1622    return x & 0x1 ? (IdentifierInfo*) (x & ~0x1) : NULL;
1623  }
1624  const IdentifierInfo *getClassName() const {
1625    return const_cast<ObjCMessageExpr*>(this)->getClassName();
1626  }
1627
1628  /// getNumArgs - Return the number of actual arguments to this call.
1629  unsigned getNumArgs() const { return NumArgs; }
1630
1631  /// getArg - Return the specified argument.
1632  Expr *getArg(unsigned Arg) {
1633    assert(Arg < NumArgs && "Arg access out of range!");
1634    return SubExprs[Arg+ARGS_START];
1635  }
1636  const Expr *getArg(unsigned Arg) const {
1637    assert(Arg < NumArgs && "Arg access out of range!");
1638    return SubExprs[Arg+ARGS_START];
1639  }
1640  /// setArg - Set the specified argument.
1641  void setArg(unsigned Arg, Expr *ArgExpr) {
1642    assert(Arg < NumArgs && "Arg access out of range!");
1643    SubExprs[Arg+ARGS_START] = ArgExpr;
1644  }
1645
1646  virtual SourceRange getSourceRange() const {
1647    return SourceRange(LBracloc, RBracloc);
1648  }
1649
1650  static bool classof(const Stmt *T) {
1651    return T->getStmtClass() == ObjCMessageExprClass;
1652  }
1653  static bool classof(const ObjCMessageExpr *) { return true; }
1654
1655  // Iterators
1656  virtual child_iterator child_begin();
1657  virtual child_iterator child_end();
1658
1659  typedef Expr** arg_iterator;
1660  typedef const Expr* const* const_arg_iterator;
1661
1662  arg_iterator arg_begin() { return &SubExprs[ARGS_START]; }
1663  arg_iterator arg_end()   { return arg_begin() + NumArgs; }
1664  const_arg_iterator arg_begin() const { return &SubExprs[ARGS_START]; }
1665  const_arg_iterator arg_end() const { return arg_begin() + NumArgs; }
1666
1667  // Serialization.
1668  virtual void EmitImpl(llvm::Serializer& S) const;
1669  static ObjCMessageExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1670};
1671
1672}  // end namespace clang
1673
1674#endif
1675