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