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