Expr.h revision 7da8d94cb79f311c5b126483b8edfe5dc70d6c8f
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  static bool isArithmeticOp(Opcode Op) { return Op >= Plus && Op <= LNot; }
433
434  /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
435  /// corresponds to, e.g. "sizeof" or "[pre]++"
436  static const char *getOpcodeStr(Opcode Op);
437
438  virtual SourceRange getSourceRange() const {
439    if (isPostfix())
440      return SourceRange(Val->getLocStart(), Loc);
441    else
442      return SourceRange(Loc, Val->getLocEnd());
443  }
444  virtual SourceLocation getExprLoc() const { return Loc; }
445
446  static bool classof(const Stmt *T) {
447    return T->getStmtClass() == UnaryOperatorClass;
448  }
449  static bool classof(const UnaryOperator *) { return true; }
450
451  // Iterators
452  virtual child_iterator child_begin();
453  virtual child_iterator child_end();
454
455  virtual void EmitImpl(llvm::Serializer& S) const;
456  static UnaryOperator* CreateImpl(llvm::Deserializer& D);
457};
458
459/// SizeOfAlignOfTypeExpr - [C99 6.5.3.4] - This is only for sizeof/alignof of
460/// *types*.  sizeof(expr) is handled by UnaryOperator.
461class SizeOfAlignOfTypeExpr : public Expr {
462  bool isSizeof;  // true if sizeof, false if alignof.
463  QualType Ty;
464  SourceLocation OpLoc, RParenLoc;
465public:
466  SizeOfAlignOfTypeExpr(bool issizeof, QualType argType, QualType resultType,
467                        SourceLocation op, SourceLocation rp) :
468    Expr(SizeOfAlignOfTypeExprClass, resultType),
469    isSizeof(issizeof), Ty(argType), OpLoc(op), RParenLoc(rp) {}
470
471  bool isSizeOf() const { return isSizeof; }
472  QualType getArgumentType() const { return Ty; }
473
474  SourceLocation getOperatorLoc() const { return OpLoc; }
475  SourceRange getSourceRange() const { return SourceRange(OpLoc, RParenLoc); }
476
477  static bool classof(const Stmt *T) {
478    return T->getStmtClass() == SizeOfAlignOfTypeExprClass;
479  }
480  static bool classof(const SizeOfAlignOfTypeExpr *) { return true; }
481
482  // Iterators
483  virtual child_iterator child_begin();
484  virtual child_iterator child_end();
485
486  virtual void EmitImpl(llvm::Serializer& S) const;
487  static SizeOfAlignOfTypeExpr* CreateImpl(llvm::Deserializer& D);
488};
489
490//===----------------------------------------------------------------------===//
491// Postfix Operators.
492//===----------------------------------------------------------------------===//
493
494/// ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting.
495class ArraySubscriptExpr : public Expr {
496  enum { LHS, RHS, END_EXPR=2 };
497  Expr* SubExprs[END_EXPR];
498  SourceLocation RBracketLoc;
499public:
500  ArraySubscriptExpr(Expr *lhs, Expr *rhs, QualType t,
501                     SourceLocation rbracketloc)
502  : Expr(ArraySubscriptExprClass, t), RBracketLoc(rbracketloc) {
503    SubExprs[LHS] = lhs;
504    SubExprs[RHS] = rhs;
505  }
506
507  /// An array access can be written A[4] or 4[A] (both are equivalent).
508  /// - getBase() and getIdx() always present the normalized view: A[4].
509  ///    In this case getBase() returns "A" and getIdx() returns "4".
510  /// - getLHS() and getRHS() present the syntactic view. e.g. for
511  ///    4[A] getLHS() returns "4".
512  /// Note: Because vector element access is also written A[4] we must
513  /// predicate the format conversion in getBase and getIdx only on the
514  /// the type of the RHS, as it is possible for the LHS to be a vector of
515  /// integer type
516  Expr *getLHS() { return SubExprs[LHS]; }
517  const Expr *getLHS() const { return SubExprs[LHS]; }
518
519  Expr *getRHS() { return SubExprs[RHS]; }
520  const Expr *getRHS() const { return SubExprs[RHS]; }
521
522  Expr *getBase() {
523    return (getRHS()->getType()->isIntegerType()) ? getLHS() : getRHS();
524  }
525
526  const Expr *getBase() const {
527    return (getRHS()->getType()->isIntegerType()) ? getLHS() : getRHS();
528  }
529
530  Expr *getIdx() {
531    return (getRHS()->getType()->isIntegerType()) ? getRHS() : getLHS();
532  }
533
534  const Expr *getIdx() const {
535    return (getRHS()->getType()->isIntegerType()) ? getRHS() : getLHS();
536  }
537
538
539  SourceRange getSourceRange() const {
540    return SourceRange(getLHS()->getLocStart(), RBracketLoc);
541  }
542  virtual SourceLocation getExprLoc() const { return RBracketLoc; }
543
544  static bool classof(const Stmt *T) {
545    return T->getStmtClass() == ArraySubscriptExprClass;
546  }
547  static bool classof(const ArraySubscriptExpr *) { return true; }
548
549  // Iterators
550  virtual child_iterator child_begin();
551  virtual child_iterator child_end();
552
553  virtual void EmitImpl(llvm::Serializer& S) const;
554  static ArraySubscriptExpr* CreateImpl(llvm::Deserializer& D);
555};
556
557
558/// CallExpr - [C99 6.5.2.2] Function Calls.
559///
560class CallExpr : public Expr {
561  enum { FN=0, ARGS_START=1 };
562  Expr **SubExprs;
563  unsigned NumArgs;
564  SourceLocation RParenLoc;
565
566  // This version of the ctor is for deserialization.
567  CallExpr(Expr** subexprs, unsigned numargs, QualType t,
568           SourceLocation rparenloc)
569  : Expr(CallExprClass,t), SubExprs(subexprs),
570    NumArgs(numargs), RParenLoc(rparenloc) {}
571
572public:
573  CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
574           SourceLocation rparenloc);
575  ~CallExpr() {
576    delete [] SubExprs;
577  }
578
579  const Expr *getCallee() const { return SubExprs[FN]; }
580  Expr *getCallee() { return SubExprs[FN]; }
581  void setCallee(Expr *F) { SubExprs[FN] = F; }
582
583  /// getNumArgs - Return the number of actual arguments to this call.
584  ///
585  unsigned getNumArgs() const { return NumArgs; }
586
587  /// getArg - Return the specified argument.
588  Expr *getArg(unsigned Arg) {
589    assert(Arg < NumArgs && "Arg access out of range!");
590    return SubExprs[Arg+ARGS_START];
591  }
592  const Expr *getArg(unsigned Arg) const {
593    assert(Arg < NumArgs && "Arg access out of range!");
594    return SubExprs[Arg+ARGS_START];
595  }
596  /// setArg - Set the specified argument.
597  void setArg(unsigned Arg, Expr *ArgExpr) {
598    assert(Arg < NumArgs && "Arg access out of range!");
599    SubExprs[Arg+ARGS_START] = ArgExpr;
600  }
601
602  /// setNumArgs - This changes the number of arguments present in this call.
603  /// Any orphaned expressions are deleted by this, and any new operands are set
604  /// to null.
605  void setNumArgs(unsigned NumArgs);
606
607  typedef Expr **arg_iterator;
608  typedef Expr * const *arg_const_iterator;
609  arg_iterator arg_begin() { return SubExprs+ARGS_START; }
610  arg_iterator arg_end() { return SubExprs+ARGS_START+getNumArgs(); }
611  arg_const_iterator arg_begin() const { return SubExprs+ARGS_START; }
612  arg_const_iterator arg_end() const { return SubExprs+ARGS_START+getNumArgs(); }
613
614
615  /// getNumCommas - Return the number of commas that must have been present in
616  /// this function call.
617  unsigned getNumCommas() const { return NumArgs ? NumArgs - 1 : 0; }
618
619  bool isBuiltinClassifyType(llvm::APSInt &Result) const;
620
621  SourceLocation getRParenLoc() const { return RParenLoc; }
622  SourceRange getSourceRange() const {
623    return SourceRange(getCallee()->getLocStart(), RParenLoc);
624  }
625
626  static bool classof(const Stmt *T) {
627    return T->getStmtClass() == CallExprClass;
628  }
629  static bool classof(const CallExpr *) { return true; }
630
631  // Iterators
632  virtual child_iterator child_begin();
633  virtual child_iterator child_end();
634
635  virtual void EmitImpl(llvm::Serializer& S) const;
636  static CallExpr* CreateImpl(llvm::Deserializer& D);
637};
638
639/// MemberExpr - [C99 6.5.2.3] Structure and Union Members.
640///
641class MemberExpr : public Expr {
642  Expr *Base;
643  FieldDecl *MemberDecl;
644  SourceLocation MemberLoc;
645  bool IsArrow;      // True if this is "X->F", false if this is "X.F".
646public:
647  MemberExpr(Expr *base, bool isarrow, FieldDecl *memberdecl, SourceLocation l)
648    : Expr(MemberExprClass, memberdecl->getType()),
649      Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow) {}
650
651  Expr *getBase() const { return Base; }
652  FieldDecl *getMemberDecl() const { return MemberDecl; }
653  bool isArrow() const { return IsArrow; }
654
655  virtual SourceRange getSourceRange() const {
656    return SourceRange(getBase()->getLocStart(), MemberLoc);
657  }
658  virtual SourceLocation getExprLoc() const { return MemberLoc; }
659
660  static bool classof(const Stmt *T) {
661    return T->getStmtClass() == MemberExprClass;
662  }
663  static bool classof(const MemberExpr *) { return true; }
664
665  // Iterators
666  virtual child_iterator child_begin();
667  virtual child_iterator child_end();
668
669  virtual void EmitImpl(llvm::Serializer& S) const;
670  static MemberExpr* CreateImpl(llvm::Deserializer& D);
671};
672
673/// OCUVectorElementExpr - This represents access to specific elements of a
674/// vector, and may occur on the left hand side or right hand side.  For example
675/// the following is legal:  "V.xy = V.zw" if V is a 4 element ocu vector.
676///
677class OCUVectorElementExpr : public Expr {
678  Expr *Base;
679  IdentifierInfo &Accessor;
680  SourceLocation AccessorLoc;
681public:
682  enum ElementType {
683    Point,   // xywz
684    Color,   // rgba
685    Texture  // stpq
686  };
687  OCUVectorElementExpr(QualType ty, Expr *base, IdentifierInfo &accessor,
688                       SourceLocation loc)
689    : Expr(OCUVectorElementExprClass, ty),
690      Base(base), Accessor(accessor), AccessorLoc(loc) {}
691
692  const Expr *getBase() const { return Base; }
693  Expr *getBase() { return Base; }
694
695  IdentifierInfo &getAccessor() const { return Accessor; }
696
697  /// getNumElements - Get the number of components being selected.
698  unsigned getNumElements() const;
699
700  /// getElementType - Determine whether the components of this access are
701  /// "point" "color" or "texture" elements.
702  ElementType getElementType() const;
703
704  /// containsDuplicateElements - Return true if any element access is
705  /// repeated.
706  bool containsDuplicateElements() const;
707
708  /// getEncodedElementAccess - Encode the elements accessed into a bit vector.
709  /// The encoding currently uses 2-bit bitfields, but clients should use the
710  /// accessors below to access them.
711  ///
712  unsigned getEncodedElementAccess() const;
713
714  /// getAccessedFieldNo - Given an encoded value and a result number, return
715  /// the input field number being accessed.
716  static unsigned getAccessedFieldNo(unsigned Idx, unsigned EncodedVal) {
717    return (EncodedVal >> (Idx*2)) & 3;
718  }
719
720  virtual SourceRange getSourceRange() const {
721    return SourceRange(getBase()->getLocStart(), AccessorLoc);
722  }
723  static bool classof(const Stmt *T) {
724    return T->getStmtClass() == OCUVectorElementExprClass;
725  }
726  static bool classof(const OCUVectorElementExpr *) { return true; }
727
728  // Iterators
729  virtual child_iterator child_begin();
730  virtual child_iterator child_end();
731};
732
733/// CompoundLiteralExpr - [C99 6.5.2.5]
734///
735class CompoundLiteralExpr : public Expr {
736  /// LParenLoc - If non-null, this is the location of the left paren in a
737  /// compound literal like "(int){4}".  This can be null if this is a
738  /// synthesized compound expression.
739  SourceLocation LParenLoc;
740  Expr *Init;
741  bool FileScope;
742public:
743  CompoundLiteralExpr(SourceLocation lparenloc, QualType ty, Expr *init, bool fileScope) :
744    Expr(CompoundLiteralExprClass, ty), LParenLoc(lparenloc), Init(init), FileScope(fileScope) {}
745
746  const Expr *getInitializer() const { return Init; }
747  Expr *getInitializer() { return Init; }
748
749  bool isFileScope() const { return FileScope; }
750
751  SourceLocation getLParenLoc() const { return LParenLoc; }
752
753  virtual SourceRange getSourceRange() const {
754    // FIXME: Init should never be null.
755    if (!Init)
756      return SourceRange();
757    if (LParenLoc.isInvalid())
758      return Init->getSourceRange();
759    return SourceRange(LParenLoc, Init->getLocEnd());
760  }
761
762  static bool classof(const Stmt *T) {
763    return T->getStmtClass() == CompoundLiteralExprClass;
764  }
765  static bool classof(const CompoundLiteralExpr *) { return true; }
766
767  // Iterators
768  virtual child_iterator child_begin();
769  virtual child_iterator child_end();
770
771  virtual void EmitImpl(llvm::Serializer& S) const;
772  static CompoundLiteralExpr* CreateImpl(llvm::Deserializer& D);
773};
774
775/// ImplicitCastExpr - Allows us to explicitly represent implicit type
776/// conversions. For example: converting T[]->T*, void f()->void (*f)(),
777/// float->double, short->int, etc.
778///
779class ImplicitCastExpr : public Expr {
780  Expr *Op;
781public:
782  ImplicitCastExpr(QualType ty, Expr *op) :
783    Expr(ImplicitCastExprClass, ty), Op(op) {}
784
785  Expr *getSubExpr() { return Op; }
786  const Expr *getSubExpr() const { return Op; }
787
788  virtual SourceRange getSourceRange() const { return Op->getSourceRange(); }
789
790  static bool classof(const Stmt *T) {
791    return T->getStmtClass() == ImplicitCastExprClass;
792  }
793  static bool classof(const ImplicitCastExpr *) { return true; }
794
795  // Iterators
796  virtual child_iterator child_begin();
797  virtual child_iterator child_end();
798
799  virtual void EmitImpl(llvm::Serializer& S) const;
800  static ImplicitCastExpr* CreateImpl(llvm::Deserializer& D);
801};
802
803/// CastExpr - [C99 6.5.4] Cast Operators.
804///
805class CastExpr : public Expr {
806  Expr *Op;
807  SourceLocation Loc; // the location of the left paren
808public:
809  CastExpr(QualType ty, Expr *op, SourceLocation l) :
810    Expr(CastExprClass, ty), Op(op), Loc(l) {}
811
812  SourceLocation getLParenLoc() const { return Loc; }
813
814  Expr *getSubExpr() const { return Op; }
815
816  virtual SourceRange getSourceRange() const {
817    return SourceRange(Loc, getSubExpr()->getSourceRange().getEnd());
818  }
819  static bool classof(const Stmt *T) {
820    return T->getStmtClass() == CastExprClass;
821  }
822  static bool classof(const CastExpr *) { return true; }
823
824  // Iterators
825  virtual child_iterator child_begin();
826  virtual child_iterator child_end();
827
828  virtual void EmitImpl(llvm::Serializer& S) const;
829  static CastExpr* CreateImpl(llvm::Deserializer& D);
830};
831
832class BinaryOperator : public Expr {
833public:
834  enum Opcode {
835    // Operators listed in order of precedence.
836    // Note that additions to this should also update the StmtVisitor class.
837    Mul, Div, Rem,    // [C99 6.5.5] Multiplicative operators.
838    Add, Sub,         // [C99 6.5.6] Additive operators.
839    Shl, Shr,         // [C99 6.5.7] Bitwise shift operators.
840    LT, GT, LE, GE,   // [C99 6.5.8] Relational operators.
841    EQ, NE,           // [C99 6.5.9] Equality operators.
842    And,              // [C99 6.5.10] Bitwise AND operator.
843    Xor,              // [C99 6.5.11] Bitwise XOR operator.
844    Or,               // [C99 6.5.12] Bitwise OR operator.
845    LAnd,             // [C99 6.5.13] Logical AND operator.
846    LOr,              // [C99 6.5.14] Logical OR operator.
847    Assign, MulAssign,// [C99 6.5.16] Assignment operators.
848    DivAssign, RemAssign,
849    AddAssign, SubAssign,
850    ShlAssign, ShrAssign,
851    AndAssign, XorAssign,
852    OrAssign,
853    Comma             // [C99 6.5.17] Comma operator.
854  };
855private:
856  enum { LHS, RHS, END_EXPR };
857  Expr* SubExprs[END_EXPR];
858  Opcode Opc;
859  SourceLocation OpLoc;
860public:
861
862  BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy,
863                 SourceLocation opLoc)
864    : Expr(BinaryOperatorClass, ResTy), Opc(opc), OpLoc(opLoc) {
865    SubExprs[LHS] = lhs;
866    SubExprs[RHS] = rhs;
867    assert(!isCompoundAssignmentOp() &&
868           "Use ArithAssignBinaryOperator for compound assignments");
869  }
870
871  SourceLocation getOperatorLoc() const { return OpLoc; }
872  Opcode getOpcode() const { return Opc; }
873  Expr *getLHS() const { return SubExprs[LHS]; }
874  Expr *getRHS() const { return SubExprs[RHS]; }
875  virtual SourceRange getSourceRange() const {
876    return SourceRange(getLHS()->getLocStart(), getRHS()->getLocEnd());
877  }
878
879  /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
880  /// corresponds to, e.g. "<<=".
881  static const char *getOpcodeStr(Opcode Op);
882
883  /// predicates to categorize the respective opcodes.
884  bool isMultiplicativeOp() const { return Opc >= Mul && Opc <= Rem; }
885  bool isAdditiveOp() const { return Opc == Add || Opc == Sub; }
886  bool isShiftOp() const { return Opc == Shl || Opc == Shr; }
887  bool isBitwiseOp() const { return Opc >= And && Opc <= Or; }
888  bool isRelationalOp() const { return Opc >= LT && Opc <= GE; }
889  bool isEqualityOp() const { return Opc == EQ || Opc == NE; }
890  bool isLogicalOp() const { return Opc == LAnd || Opc == LOr; }
891  bool isAssignmentOp() const { return Opc >= Assign && Opc <= OrAssign; }
892  bool isCompoundAssignmentOp() const { return Opc > Assign && Opc <= OrAssign;}
893  bool isShiftAssignOp() const { return Opc == ShlAssign || Opc == ShrAssign; }
894
895  static bool classof(const Stmt *S) {
896    return S->getStmtClass() == BinaryOperatorClass ||
897           S->getStmtClass() == CompoundAssignOperatorClass;
898  }
899  static bool classof(const BinaryOperator *) { return true; }
900
901  // Iterators
902  virtual child_iterator child_begin();
903  virtual child_iterator child_end();
904
905  virtual void EmitImpl(llvm::Serializer& S) const;
906  static BinaryOperator* CreateImpl(llvm::Deserializer& D);
907
908protected:
909  BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy,
910                 SourceLocation oploc, bool dead)
911    : Expr(CompoundAssignOperatorClass, ResTy), Opc(opc), OpLoc(oploc) {
912    SubExprs[LHS] = lhs;
913    SubExprs[RHS] = rhs;
914  }
915};
916
917/// CompoundAssignOperator - For compound assignments (e.g. +=), we keep
918/// track of the type the operation is performed in.  Due to the semantics of
919/// these operators, the operands are promoted, the aritmetic performed, an
920/// implicit conversion back to the result type done, then the assignment takes
921/// place.  This captures the intermediate type which the computation is done
922/// in.
923class CompoundAssignOperator : public BinaryOperator {
924  QualType ComputationType;
925public:
926  CompoundAssignOperator(Expr *lhs, Expr *rhs, Opcode opc,
927                         QualType ResType, QualType CompType,
928                         SourceLocation OpLoc)
929    : BinaryOperator(lhs, rhs, opc, ResType, OpLoc, true),
930      ComputationType(CompType) {
931    assert(isCompoundAssignmentOp() &&
932           "Only should be used for compound assignments");
933  }
934
935  QualType getComputationType() const { return ComputationType; }
936
937  static bool classof(const CompoundAssignOperator *) { return true; }
938  static bool classof(const Stmt *S) {
939    return S->getStmtClass() == CompoundAssignOperatorClass;
940  }
941
942  virtual void EmitImpl(llvm::Serializer& S) const;
943  static CompoundAssignOperator* CreateImpl(llvm::Deserializer& D);
944};
945
946/// ConditionalOperator - The ?: operator.  Note that LHS may be null when the
947/// GNU "missing LHS" extension is in use.
948///
949class ConditionalOperator : public Expr {
950  enum { COND, LHS, RHS, END_EXPR };
951  Expr* SubExprs[END_EXPR]; // Left/Middle/Right hand sides.
952public:
953  ConditionalOperator(Expr *cond, Expr *lhs, Expr *rhs, QualType t)
954    : Expr(ConditionalOperatorClass, t) {
955    SubExprs[COND] = cond;
956    SubExprs[LHS] = lhs;
957    SubExprs[RHS] = rhs;
958  }
959
960  // getCond - Return the expression representing the condition for
961  //  the ?: operator.
962  Expr *getCond() const { return SubExprs[COND]; }
963
964  // getTrueExpr - Return the subexpression representing the value of the ?:
965  //  expression if the condition evaluates to true.  In most cases this value
966  //  will be the same as getLHS() except a GCC extension allows the left
967  //  subexpression to be omitted, and instead of the condition be returned.
968  //  e.g: x ?: y is shorthand for x ? x : y, except that the expression "x"
969  //  is only evaluated once.
970  Expr *getTrueExpr() const {
971    return SubExprs[LHS] ? SubExprs[COND] : SubExprs[LHS];
972  }
973
974  // getTrueExpr - Return the subexpression representing the value of the ?:
975  // expression if the condition evaluates to false. This is the same as getRHS.
976  Expr *getFalseExpr() const { return SubExprs[RHS]; }
977
978  Expr *getLHS() const { return SubExprs[LHS]; }
979  Expr *getRHS() const { return SubExprs[RHS]; }
980
981  virtual SourceRange getSourceRange() const {
982    return SourceRange(getCond()->getLocStart(), getRHS()->getLocEnd());
983  }
984  static bool classof(const Stmt *T) {
985    return T->getStmtClass() == ConditionalOperatorClass;
986  }
987  static bool classof(const ConditionalOperator *) { return true; }
988
989  // Iterators
990  virtual child_iterator child_begin();
991  virtual child_iterator child_end();
992
993  virtual void EmitImpl(llvm::Serializer& S) const;
994  static ConditionalOperator* CreateImpl(llvm::Deserializer& D);
995};
996
997/// AddrLabelExpr - The GNU address of label extension, representing &&label.
998class AddrLabelExpr : public Expr {
999  SourceLocation AmpAmpLoc, LabelLoc;
1000  LabelStmt *Label;
1001public:
1002  AddrLabelExpr(SourceLocation AALoc, SourceLocation LLoc, LabelStmt *L,
1003                QualType t)
1004    : Expr(AddrLabelExprClass, t), AmpAmpLoc(AALoc), LabelLoc(LLoc), Label(L) {}
1005
1006  virtual SourceRange getSourceRange() const {
1007    return SourceRange(AmpAmpLoc, LabelLoc);
1008  }
1009
1010  LabelStmt *getLabel() const { return Label; }
1011
1012  static bool classof(const Stmt *T) {
1013    return T->getStmtClass() == AddrLabelExprClass;
1014  }
1015  static bool classof(const AddrLabelExpr *) { return true; }
1016
1017  // Iterators
1018  virtual child_iterator child_begin();
1019  virtual child_iterator child_end();
1020
1021  virtual void EmitImpl(llvm::Serializer& S) const;
1022  static AddrLabelExpr* CreateImpl(llvm::Deserializer& D);
1023};
1024
1025/// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}).
1026/// The StmtExpr contains a single CompoundStmt node, which it evaluates and
1027/// takes the value of the last subexpression.
1028class StmtExpr : public Expr {
1029  CompoundStmt *SubStmt;
1030  SourceLocation LParenLoc, RParenLoc;
1031public:
1032  StmtExpr(CompoundStmt *substmt, QualType T,
1033           SourceLocation lp, SourceLocation rp) :
1034    Expr(StmtExprClass, T), SubStmt(substmt),  LParenLoc(lp), RParenLoc(rp) { }
1035
1036  CompoundStmt *getSubStmt() { return SubStmt; }
1037  const CompoundStmt *getSubStmt() const { return SubStmt; }
1038
1039  virtual SourceRange getSourceRange() const {
1040    return SourceRange(LParenLoc, RParenLoc);
1041  }
1042
1043  static bool classof(const Stmt *T) {
1044    return T->getStmtClass() == StmtExprClass;
1045  }
1046  static bool classof(const StmtExpr *) { return true; }
1047
1048  // Iterators
1049  virtual child_iterator child_begin();
1050  virtual child_iterator child_end();
1051
1052  virtual void EmitImpl(llvm::Serializer& S) const;
1053  static StmtExpr* CreateImpl(llvm::Deserializer& D);
1054};
1055
1056/// TypesCompatibleExpr - GNU builtin-in function __builtin_type_compatible_p.
1057/// This AST node represents a function that returns 1 if two *types* (not
1058/// expressions) are compatible. The result of this built-in function can be
1059/// used in integer constant expressions.
1060class TypesCompatibleExpr : public Expr {
1061  QualType Type1;
1062  QualType Type2;
1063  SourceLocation BuiltinLoc, RParenLoc;
1064public:
1065  TypesCompatibleExpr(QualType ReturnType, SourceLocation BLoc,
1066                      QualType t1, QualType t2, SourceLocation RP) :
1067    Expr(TypesCompatibleExprClass, ReturnType), Type1(t1), Type2(t2),
1068    BuiltinLoc(BLoc), RParenLoc(RP) {}
1069
1070  QualType getArgType1() const { return Type1; }
1071  QualType getArgType2() const { return Type2; }
1072
1073  virtual SourceRange getSourceRange() const {
1074    return SourceRange(BuiltinLoc, RParenLoc);
1075  }
1076  static bool classof(const Stmt *T) {
1077    return T->getStmtClass() == TypesCompatibleExprClass;
1078  }
1079  static bool classof(const TypesCompatibleExpr *) { return true; }
1080
1081  // Iterators
1082  virtual child_iterator child_begin();
1083  virtual child_iterator child_end();
1084};
1085
1086/// ChooseExpr - GNU builtin-in function __builtin_choose_expr.
1087/// This AST node is similar to the conditional operator (?:) in C, with
1088/// the following exceptions:
1089/// - the test expression much be a constant expression.
1090/// - the expression returned has it's type unaltered by promotion rules.
1091/// - does not evaluate the expression that was not chosen.
1092class ChooseExpr : public Expr {
1093  enum { COND, LHS, RHS, END_EXPR };
1094  Expr* SubExprs[END_EXPR]; // Left/Middle/Right hand sides.
1095  SourceLocation BuiltinLoc, RParenLoc;
1096public:
1097  ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, QualType t,
1098             SourceLocation RP)
1099    : Expr(ChooseExprClass, t),
1100      BuiltinLoc(BLoc), RParenLoc(RP) {
1101      SubExprs[COND] = cond;
1102      SubExprs[LHS] = lhs;
1103      SubExprs[RHS] = rhs;
1104    }
1105
1106  /// isConditionTrue - Return true if the condition is true.  This is always
1107  /// statically knowable for a well-formed choosexpr.
1108  bool isConditionTrue(ASTContext &C) const;
1109
1110  Expr *getCond() const { return SubExprs[COND]; }
1111  Expr *getLHS() const { return SubExprs[LHS]; }
1112  Expr *getRHS() const { return SubExprs[RHS]; }
1113
1114  virtual SourceRange getSourceRange() const {
1115    return SourceRange(BuiltinLoc, RParenLoc);
1116  }
1117  static bool classof(const Stmt *T) {
1118    return T->getStmtClass() == ChooseExprClass;
1119  }
1120  static bool classof(const ChooseExpr *) { return true; }
1121
1122  // Iterators
1123  virtual child_iterator child_begin();
1124  virtual child_iterator child_end();
1125};
1126
1127/// OverloadExpr - Clang builtin-in function __builtin_overload.
1128/// This AST node provides a way to overload functions in C
1129/// i.e. float Z = __builtin_overload(2, X, Y, modf, mod, modl);
1130/// would pick whichever of the functions modf, mod, and modl that took two
1131/// arguments of the same type as X and Y.
1132class OverloadExpr : public Expr {
1133  Expr **SubExprs;
1134  unsigned NumArgs;
1135  unsigned FnIndex;
1136  SourceLocation BuiltinLoc;
1137  SourceLocation RParenLoc;
1138public:
1139  OverloadExpr(Expr **args, unsigned narg, unsigned idx, QualType t,
1140               SourceLocation bloc, SourceLocation rploc)
1141    : Expr(OverloadExprClass, t), NumArgs(narg), FnIndex(idx), BuiltinLoc(bloc),
1142      RParenLoc(rploc) {
1143    SubExprs = new Expr*[narg];
1144    for (unsigned i = 0; i != narg; ++i)
1145      SubExprs[i] = args[i];
1146  }
1147  ~OverloadExpr() {
1148    delete [] SubExprs;
1149  }
1150
1151  typedef Expr * const *arg_const_iterator;
1152  arg_const_iterator arg_begin() const { return SubExprs+1; }
1153
1154  /// getNumArgs - Return the number of actual arguments to this call.
1155  ///
1156  unsigned getNumArgs() const { return NumArgs; }
1157
1158  /// getArg - Return the specified argument.
1159  Expr *getArg(unsigned Arg) {
1160    assert(Arg < NumArgs && "Arg access out of range!");
1161    return SubExprs[Arg];
1162  }
1163  Expr *getFn() { return SubExprs[FnIndex]; }
1164
1165  virtual SourceRange getSourceRange() const {
1166    return SourceRange(BuiltinLoc, RParenLoc);
1167  }
1168  static bool classof(const Stmt *T) {
1169    return T->getStmtClass() == OverloadExprClass;
1170  }
1171  static bool classof(const OverloadExpr *) { return true; }
1172
1173  // Iterators
1174  virtual child_iterator child_begin();
1175  virtual child_iterator child_end();
1176};
1177
1178/// VAArgExpr, used for the builtin function __builtin_va_start.
1179class VAArgExpr : public Expr {
1180  Expr *Val;
1181  SourceLocation BuiltinLoc, RParenLoc;
1182public:
1183  VAArgExpr(SourceLocation BLoc, Expr* e, QualType t, SourceLocation RPLoc)
1184    : Expr(VAArgExprClass, t),
1185      Val(e),
1186      BuiltinLoc(BLoc),
1187      RParenLoc(RPLoc) { }
1188
1189  const Expr *getSubExpr() const { return Val; }
1190  Expr *getSubExpr() { return Val; }
1191  virtual SourceRange getSourceRange() const {
1192    return SourceRange(BuiltinLoc, RParenLoc);
1193  }
1194  static bool classof(const Stmt *T) {
1195    return T->getStmtClass() == VAArgExprClass;
1196  }
1197  static bool classof(const VAArgExpr *) { return true; }
1198
1199  // Iterators
1200  virtual child_iterator child_begin();
1201  virtual child_iterator child_end();
1202};
1203
1204/// InitListExpr, used for struct and array initializers.
1205class InitListExpr : public Expr {
1206  Expr **InitExprs;
1207  unsigned NumInits;
1208  SourceLocation LBraceLoc, RBraceLoc;
1209public:
1210  InitListExpr(SourceLocation lbraceloc, Expr **initexprs, unsigned numinits,
1211               SourceLocation rbraceloc);
1212  ~InitListExpr() {
1213    delete [] InitExprs;
1214  }
1215
1216  unsigned getNumInits() const { return NumInits; }
1217
1218  const Expr* getInit(unsigned Init) const {
1219    assert(Init < NumInits && "Initializer access out of range!");
1220    return InitExprs[Init];
1221  }
1222
1223  Expr* getInit(unsigned Init) {
1224    assert(Init < NumInits && "Initializer access out of range!");
1225    return InitExprs[Init];
1226  }
1227
1228  void setInit(unsigned Init, Expr *expr) {
1229    assert(Init < NumInits && "Initializer access out of range!");
1230    InitExprs[Init] = expr;
1231  }
1232
1233  virtual SourceRange getSourceRange() const {
1234    return SourceRange(LBraceLoc, RBraceLoc);
1235  }
1236  static bool classof(const Stmt *T) {
1237    return T->getStmtClass() == InitListExprClass;
1238  }
1239  static bool classof(const InitListExpr *) { return true; }
1240
1241  // Iterators
1242  virtual child_iterator child_begin();
1243  virtual child_iterator child_end();
1244
1245  virtual void EmitImpl(llvm::Serializer& S) const;
1246  static InitListExpr* CreateImpl(llvm::Deserializer& D);
1247
1248private:
1249  // Used by serializer.
1250  InitListExpr() : Expr(InitListExprClass, QualType()),
1251                   InitExprs(NULL), NumInits(0) {}
1252};
1253
1254/// ObjCStringLiteral, used for Objective-C string literals
1255/// i.e. @"foo".
1256class ObjCStringLiteral : public Expr {
1257  StringLiteral *String;
1258  SourceLocation AtLoc;
1259public:
1260  ObjCStringLiteral(StringLiteral *SL, QualType T, SourceLocation L)
1261    : Expr(ObjCStringLiteralClass, T), String(SL), AtLoc(L) {}
1262
1263  StringLiteral* getString() { return String; }
1264
1265  const StringLiteral* getString() const { return String; }
1266
1267  SourceLocation getAtLoc() const { return AtLoc; }
1268
1269  virtual SourceRange getSourceRange() const {
1270    return SourceRange(AtLoc, String->getLocEnd());
1271  }
1272
1273  static bool classof(const Stmt *T) {
1274    return T->getStmtClass() == ObjCStringLiteralClass;
1275  }
1276  static bool classof(const ObjCStringLiteral *) { return true; }
1277
1278  // Iterators
1279  virtual child_iterator child_begin();
1280  virtual child_iterator child_end();
1281
1282  virtual void EmitImpl(llvm::Serializer& S) const;
1283  static ObjCStringLiteral* CreateImpl(llvm::Deserializer& D);
1284};
1285
1286/// ObjCEncodeExpr, used for @encode in Objective-C.
1287class ObjCEncodeExpr : public Expr {
1288  QualType EncType;
1289  SourceLocation AtLoc, RParenLoc;
1290public:
1291  ObjCEncodeExpr(QualType T, QualType ET,
1292                 SourceLocation at, SourceLocation rp)
1293    : Expr(ObjCEncodeExprClass, T), EncType(ET), AtLoc(at), RParenLoc(rp) {}
1294
1295  SourceLocation getAtLoc() const { return AtLoc; }
1296  SourceLocation getRParenLoc() const { return RParenLoc; }
1297
1298  SourceRange getSourceRange() const { return SourceRange(AtLoc, RParenLoc); }
1299
1300  QualType getEncodedType() const { return EncType; }
1301
1302  static bool classof(const Stmt *T) {
1303    return T->getStmtClass() == ObjCEncodeExprClass;
1304  }
1305  static bool classof(const ObjCEncodeExpr *) { return true; }
1306
1307  // Iterators
1308  virtual child_iterator child_begin();
1309  virtual child_iterator child_end();
1310
1311  virtual void EmitImpl(llvm::Serializer& S) const;
1312  static ObjCEncodeExpr* CreateImpl(llvm::Deserializer& D);
1313};
1314
1315/// ObjCSelectorExpr used for @selector in Objective-C.
1316class ObjCSelectorExpr : public Expr {
1317  Selector SelName;
1318  SourceLocation AtLoc, RParenLoc;
1319public:
1320  ObjCSelectorExpr(QualType T, Selector selInfo,
1321                   SourceLocation at, SourceLocation rp)
1322  : Expr(ObjCSelectorExprClass, T), SelName(selInfo),
1323  AtLoc(at), RParenLoc(rp) {}
1324
1325  const Selector &getSelector() const { return SelName; }
1326  Selector &getSelector() { return SelName; }
1327
1328  SourceLocation getAtLoc() const { return AtLoc; }
1329  SourceLocation getRParenLoc() const { return RParenLoc; }
1330  SourceRange getSourceRange() const { return SourceRange(AtLoc, RParenLoc); }
1331
1332  /// getNumArgs - Return the number of actual arguments to this call.
1333  unsigned getNumArgs() const { return SelName.getNumArgs(); }
1334
1335  static bool classof(const Stmt *T) {
1336    return T->getStmtClass() == ObjCSelectorExprClass;
1337  }
1338  static bool classof(const ObjCSelectorExpr *) { return true; }
1339
1340  // Iterators
1341  virtual child_iterator child_begin();
1342  virtual child_iterator child_end();
1343
1344  virtual void EmitImpl(llvm::Serializer& S) const;
1345  static ObjCSelectorExpr* CreateImpl(llvm::Deserializer& D);
1346};
1347
1348/// ObjCProtocolExpr used for protocol in Objective-C.
1349class ObjCProtocolExpr : public Expr {
1350  ObjCProtocolDecl *Protocol;
1351  SourceLocation AtLoc, RParenLoc;
1352public:
1353  ObjCProtocolExpr(QualType T, ObjCProtocolDecl *protocol,
1354                   SourceLocation at, SourceLocation rp)
1355  : Expr(ObjCProtocolExprClass, T), Protocol(protocol),
1356  AtLoc(at), RParenLoc(rp) {}
1357
1358  ObjCProtocolDecl *getProtocol() const { return Protocol; }
1359
1360  SourceLocation getAtLoc() const { return AtLoc; }
1361  SourceLocation getRParenLoc() const { return RParenLoc; }
1362  SourceRange getSourceRange() const { return SourceRange(AtLoc, RParenLoc); }
1363
1364  static bool classof(const Stmt *T) {
1365    return T->getStmtClass() == ObjCProtocolExprClass;
1366  }
1367  static bool classof(const ObjCProtocolExpr *) { return true; }
1368
1369  // Iterators
1370  virtual child_iterator child_begin();
1371  virtual child_iterator child_end();
1372};
1373
1374/// ObjCIvarRefExpr - A reference to an ObjC instance variable.
1375class ObjCIvarRefExpr : public Expr {
1376  class ObjCIvarDecl *D;
1377  SourceLocation Loc;
1378  Expr *Base;
1379  bool IsArrow:1;      // True if this is "X->F", false if this is "X.F".
1380  bool IsFreeIvar:1;   // True if ivar reference has no base (self assumed).
1381
1382public:
1383  ObjCIvarRefExpr(ObjCIvarDecl *d, QualType t, SourceLocation l, Expr *base=0,
1384                  bool arrow = false, bool freeIvar = false) :
1385    Expr(ObjCIvarRefExprClass, t), D(d), Loc(l), Base(base), IsArrow(arrow),
1386    IsFreeIvar(freeIvar) {}
1387
1388  ObjCIvarDecl *getDecl() { return D; }
1389  const ObjCIvarDecl *getDecl() const { return D; }
1390  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
1391  Expr *const getBase() const { return Base; }
1392  void setBase(Expr * base) { Base = base; }
1393  const bool isArrow() const { return IsArrow; }
1394  const bool isFreeIvar() const { return IsFreeIvar; }
1395
1396  SourceLocation getLocation() const { return Loc; }
1397
1398  static bool classof(const Stmt *T) {
1399    return T->getStmtClass() == ObjCIvarRefExprClass;
1400  }
1401  static bool classof(const ObjCIvarRefExpr *) { return true; }
1402
1403  // Iterators
1404  virtual child_iterator child_begin();
1405  virtual child_iterator child_end();
1406
1407  virtual void EmitImpl(llvm::Serializer& S) const;
1408  static ObjCIvarRefExpr* CreateImpl(llvm::Deserializer& D);
1409};
1410
1411class ObjCMessageExpr : public Expr {
1412  enum { RECEIVER=0, ARGS_START=1 };
1413
1414  Expr **SubExprs;
1415
1416  unsigned NumArgs;
1417
1418  // A unigue name for this message.
1419  Selector SelName;
1420
1421  // A method prototype for this message (optional).
1422  // FIXME: Since method decls contain the selector, and most messages have a
1423  // prototype, consider devising a scheme for unifying SelName/MethodProto.
1424  ObjCMethodDecl *MethodProto;
1425
1426  IdentifierInfo *ClassName; // optional - 0 for instance messages.
1427
1428  SourceLocation LBracloc, RBracloc;
1429public:
1430  // constructor for class messages.
1431  // FIXME: clsName should be typed to ObjCInterfaceType
1432  ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
1433                  QualType retType, ObjCMethodDecl *methDecl,
1434                  SourceLocation LBrac, SourceLocation RBrac,
1435                  Expr **ArgExprs, unsigned NumArgs);
1436  // constructor for instance messages.
1437  ObjCMessageExpr(Expr *receiver, Selector selInfo,
1438                  QualType retType, ObjCMethodDecl *methDecl,
1439                  SourceLocation LBrac, SourceLocation RBrac,
1440                  Expr **ArgExprs, unsigned NumArgs);
1441  ~ObjCMessageExpr() {
1442    delete [] SubExprs;
1443  }
1444
1445  const Expr *getReceiver() const { return SubExprs[RECEIVER]; }
1446  Expr *getReceiver() { return SubExprs[RECEIVER]; }
1447
1448  const Selector &getSelector() const { return SelName; }
1449  Selector &getSelector() { return SelName; }
1450
1451  const ObjCMethodDecl *getMethodDecl() const { return MethodProto; }
1452  ObjCMethodDecl *getMethodDecl() { return MethodProto; }
1453
1454  const IdentifierInfo *getClassName() const { return ClassName; }
1455  IdentifierInfo *getClassName() { return ClassName; }
1456
1457  /// getNumArgs - Return the number of actual arguments to this call.
1458  unsigned getNumArgs() const { return NumArgs; }
1459
1460/// getArg - Return the specified argument.
1461  Expr *getArg(unsigned Arg) {
1462    assert(Arg < NumArgs && "Arg access out of range!");
1463    return SubExprs[Arg+ARGS_START];
1464  }
1465  const Expr *getArg(unsigned Arg) const {
1466    assert(Arg < NumArgs && "Arg access out of range!");
1467    return SubExprs[Arg+ARGS_START];
1468  }
1469  /// setArg - Set the specified argument.
1470  void setArg(unsigned Arg, Expr *ArgExpr) {
1471    assert(Arg < NumArgs && "Arg access out of range!");
1472    SubExprs[Arg+ARGS_START] = ArgExpr;
1473  }
1474  SourceRange getSourceRange() const { return SourceRange(LBracloc, RBracloc); }
1475
1476  static bool classof(const Stmt *T) {
1477    return T->getStmtClass() == ObjCMessageExprClass;
1478  }
1479  static bool classof(const ObjCMessageExpr *) { return true; }
1480
1481  // Iterators
1482  virtual child_iterator child_begin();
1483  virtual child_iterator child_end();
1484};
1485
1486}  // end namespace clang
1487
1488#endif
1489