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