Expr.h revision 1c0cfd4599e816cfd7a8f348286bf0ad79652ffc
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/APValue.h"
18#include "clang/AST/Stmt.h"
19#include "clang/AST/Type.h"
20#include "llvm/ADT/APSInt.h"
21#include "llvm/ADT/APFloat.h"
22#include "llvm/ADT/SmallVector.h"
23#include <vector>
24
25namespace clang {
26  class ASTContext;
27  class APValue;
28  class Decl;
29  class IdentifierInfo;
30  class ParmVarDecl;
31  class NamedDecl;
32  class ValueDecl;
33  class BlockDecl;
34
35/// Expr - This represents one expression.  Note that Expr's are subclasses of
36/// Stmt.  This allows an expression to be transparently used any place a Stmt
37/// is required.
38///
39class Expr : public Stmt {
40  QualType TR;
41
42  /// TypeDependent - Whether this expression is type-dependent
43  /// (C++ [temp.dep.expr]).
44  bool TypeDependent : 1;
45
46  /// ValueDependent - Whether this expression is value-dependent
47  /// (C++ [temp.dep.constexpr]).
48  bool ValueDependent : 1;
49
50protected:
51  // FIXME: Eventually, this constructor should go away and we should
52  // require every subclass to provide type/value-dependence
53  // information.
54  Expr(StmtClass SC, QualType T)
55    : Stmt(SC), TypeDependent(false), ValueDependent(false) { setType(T); }
56
57  Expr(StmtClass SC, QualType T, bool TD, bool VD)
58    : Stmt(SC), TypeDependent(TD), ValueDependent(VD) {
59    setType(T);
60  }
61
62public:
63  QualType getType() const { return TR; }
64  void setType(QualType t) {
65    // In C++, the type of an expression is always adjusted so that it
66    // will not have reference type an expression will never have
67    // reference type (C++ [expr]p6). Use
68    // QualType::getNonReferenceType() to retrieve the non-reference
69    // type. Additionally, inspect Expr::isLvalue to determine whether
70    // an expression that is adjusted in this manner should be
71    // considered an lvalue.
72    assert((TR.isNull() || !TR->isReferenceType()) &&
73           "Expressions can't have reference type");
74
75    TR = t;
76  }
77
78  /// isValueDependent - Determines whether this expression is
79  /// value-dependent (C++ [temp.dep.constexpr]). For example, the
80  /// array bound of "Chars" in the following example is
81  /// value-dependent.
82  /// @code
83  /// template<int Size, char (&Chars)[Size]> struct meta_string;
84  /// @endcode
85  bool isValueDependent() const { return ValueDependent; }
86
87  /// isTypeDependent - Determines whether this expression is
88  /// type-dependent (C++ [temp.dep.expr]), which means that its type
89  /// could change from one template instantiation to the next. For
90  /// example, the expressions "x" and "x + y" are type-dependent in
91  /// the following code, but "y" is not type-dependent:
92  /// @code
93  /// template<typename T>
94  /// void add(T x, int y) {
95  ///   x + y;
96  /// }
97  /// @endcode
98  bool isTypeDependent() const { return TypeDependent; }
99
100  /// SourceLocation tokens are not useful in isolation - they are low level
101  /// value objects created/interpreted by SourceManager. We assume AST
102  /// clients will have a pointer to the respective SourceManager.
103  virtual SourceRange getSourceRange() const = 0;
104
105  /// getExprLoc - Return the preferred location for the arrow when diagnosing
106  /// a problem with a generic expression.
107  virtual SourceLocation getExprLoc() const { return getLocStart(); }
108
109  /// hasLocalSideEffect - Return true if this immediate expression has side
110  /// effects, not counting any sub-expressions.
111  bool hasLocalSideEffect() const;
112
113  /// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or
114  /// incomplete type other than void. Nonarray expressions that can be lvalues:
115  ///  - name, where name must be a variable
116  ///  - e[i]
117  ///  - (e), where e must be an lvalue
118  ///  - e.name, where e must be an lvalue
119  ///  - e->name
120  ///  - *e, the type of e cannot be a function type
121  ///  - string-constant
122  ///  - reference type [C++ [expr]]
123  ///
124  enum isLvalueResult {
125    LV_Valid,
126    LV_NotObjectType,
127    LV_IncompleteVoidType,
128    LV_DuplicateVectorComponents,
129    LV_InvalidExpression
130  };
131  isLvalueResult isLvalue(ASTContext &Ctx) const;
132
133  /// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
134  /// does not have an incomplete type, does not have a const-qualified type,
135  /// and if it is a structure or union, does not have any member (including,
136  /// recursively, any member or element of all contained aggregates or unions)
137  /// with a const-qualified type.
138  enum isModifiableLvalueResult {
139    MLV_Valid,
140    MLV_NotObjectType,
141    MLV_IncompleteVoidType,
142    MLV_DuplicateVectorComponents,
143    MLV_InvalidExpression,
144    MLV_LValueCast,           // Specialized form of MLV_InvalidExpression.
145    MLV_IncompleteType,
146    MLV_ConstQualified,
147    MLV_ArrayType,
148    MLV_NotBlockQualified,
149    MLV_ReadonlyProperty,
150    MLV_NoSetterProperty
151  };
152  isModifiableLvalueResult isModifiableLvalue(ASTContext &Ctx) const;
153
154  bool isBitField();
155
156  /// getIntegerConstantExprValue() - Return the value of an integer
157  /// constant expression. The expression must be a valid integer
158  /// constant expression as determined by isIntegerConstantExpr.
159  llvm::APSInt getIntegerConstantExprValue(ASTContext &Ctx) const {
160    llvm::APSInt X;
161    bool success = isIntegerConstantExpr(X, Ctx);
162    success = success;
163    assert(success && "Illegal argument to getIntegerConstantExpr");
164    return X;
165  }
166
167  /// isIntegerConstantExpr - Return true if this expression is a valid integer
168  /// constant expression, and, if so, return its value in Result.  If not a
169  /// valid i-c-e, return false and fill in Loc (if specified) with the location
170  /// of the invalid expression.
171  bool isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
172                             SourceLocation *Loc = 0,
173                             bool isEvaluated = true) const;
174  bool isIntegerConstantExpr(ASTContext &Ctx, SourceLocation *Loc = 0) const {
175    llvm::APSInt X;
176    return isIntegerConstantExpr(X, Ctx, Loc);
177  }
178  /// isConstantExpr - Return true if this expression is a valid constant expr.
179  bool isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const;
180
181  /// EvalResult is a struct with detailed info about an evaluated expression.
182  struct EvalResult {
183    /// Val - This is the scalar value the expression can be folded to.
184    APValue Val;
185
186    /// HasSideEffects - Whether the evaluated expression has side effects.
187    /// For example, (f() && 0) can be folded, but it still has side effects.
188    bool HasSideEffects;
189
190    /// Diag - If the expression is unfoldable, then Diag contains a note
191    /// diagnostic indicating why it's not foldable. DiagLoc indicates a caret
192    /// position for the error, and DiagExpr is the expression that caused
193    /// the error.
194    /// If the expression is foldable, but not an integer constant expression,
195    /// Diag contains a note diagnostic that describes why it isn't an integer
196    /// constant expression. If the expression *is* an integer constant
197    /// expression, then Diag will be zero.
198    unsigned Diag;
199    const Expr *DiagExpr;
200    SourceLocation DiagLoc;
201
202    EvalResult() : HasSideEffects(false), Diag(0), DiagExpr(0) {}
203  };
204
205  /// Evaluate - Return true if this is a constant which we can fold using
206  /// any crazy technique (that has nothing to do with language standards) that
207  /// we want to.  If this function returns true, it returns the folded constant
208  /// in Result.
209  bool Evaluate(EvalResult &Result, ASTContext &Ctx) const;
210
211  /// isEvaluatable - Call Evaluate to see if this expression can be constant
212  /// folded, but discard the result.
213  bool isEvaluatable(ASTContext &Ctx) const;
214
215  /// EvaluateAsInt - Call Evaluate and return the folded integer. This
216  /// must be called on an expression that constant folds to an integer.
217  llvm::APSInt EvaluateAsInt(ASTContext &Ctx) const;
218
219  /// isNullPointerConstant - C99 6.3.2.3p3 -  Return true if this is either an
220  /// integer constant expression with the value zero, or if this is one that is
221  /// cast to void*.
222  bool isNullPointerConstant(ASTContext &Ctx) const;
223
224  /// hasGlobalStorage - Return true if this expression has static storage
225  /// duration.  This means that the address of this expression is a link-time
226  /// constant.
227  bool hasGlobalStorage() const;
228
229  /// IgnoreParens - Ignore parentheses.  If this Expr is a ParenExpr, return
230  ///  its subexpression.  If that subexpression is also a ParenExpr,
231  ///  then this method recursively returns its subexpression, and so forth.
232  ///  Otherwise, the method returns the current Expr.
233  Expr* IgnoreParens();
234
235  /// IgnoreParenCasts - Ignore parentheses and casts.  Strip off any ParenExpr
236  /// or CastExprs, returning their operand.
237  Expr *IgnoreParenCasts();
238
239  const Expr* IgnoreParens() const {
240    return const_cast<Expr*>(this)->IgnoreParens();
241  }
242  const Expr *IgnoreParenCasts() const {
243    return const_cast<Expr*>(this)->IgnoreParenCasts();
244  }
245
246  static bool hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs);
247  static bool hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs);
248
249  static bool classof(const Stmt *T) {
250    return T->getStmtClass() >= firstExprConstant &&
251           T->getStmtClass() <= lastExprConstant;
252  }
253  static bool classof(const Expr *) { return true; }
254
255  static inline Expr* Create(llvm::Deserializer& D, ASTContext& C) {
256    return cast<Expr>(Stmt::Create(D, C));
257  }
258};
259
260
261//===----------------------------------------------------------------------===//
262// Primary Expressions.
263//===----------------------------------------------------------------------===//
264
265/// DeclRefExpr - [C99 6.5.1p2] - A reference to a declared variable, function,
266/// enum, etc.
267class DeclRefExpr : public Expr {
268  NamedDecl *D;
269  SourceLocation Loc;
270
271protected:
272  DeclRefExpr(StmtClass SC, NamedDecl *d, QualType t, SourceLocation l) :
273    Expr(SC, t), D(d), Loc(l) {}
274
275public:
276  // FIXME: Eventually, this constructor will go away and all clients
277  // will have to provide the type- and value-dependent flags.
278  DeclRefExpr(NamedDecl *d, QualType t, SourceLocation l) :
279    Expr(DeclRefExprClass, t), D(d), Loc(l) {}
280
281  DeclRefExpr(NamedDecl *d, QualType t, SourceLocation l, bool TD, bool VD) :
282    Expr(DeclRefExprClass, t, TD, VD), D(d), Loc(l) {}
283
284  NamedDecl *getDecl() { return D; }
285  const NamedDecl *getDecl() const { return D; }
286  void setDecl(NamedDecl *NewD) { D = NewD; }
287
288  SourceLocation getLocation() const { return Loc; }
289  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
290
291  static bool classof(const Stmt *T) {
292    return T->getStmtClass() == DeclRefExprClass ||
293           T->getStmtClass() == CXXConditionDeclExprClass;
294  }
295  static bool classof(const DeclRefExpr *) { return true; }
296
297  // Iterators
298  virtual child_iterator child_begin();
299  virtual child_iterator child_end();
300
301  virtual void EmitImpl(llvm::Serializer& S) const;
302  static DeclRefExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
303};
304
305/// PredefinedExpr - [C99 6.4.2.2] - A predefined identifier such as __func__.
306class PredefinedExpr : public Expr {
307public:
308  enum IdentType {
309    Func,
310    Function,
311    PrettyFunction
312  };
313
314private:
315  SourceLocation Loc;
316  IdentType Type;
317public:
318  PredefinedExpr(SourceLocation l, QualType type, IdentType IT)
319    : Expr(PredefinedExprClass, type), Loc(l), Type(IT) {}
320
321  IdentType getIdentType() const { return Type; }
322
323  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
324
325  static bool classof(const Stmt *T) {
326    return T->getStmtClass() == PredefinedExprClass;
327  }
328  static bool classof(const PredefinedExpr *) { return true; }
329
330  // Iterators
331  virtual child_iterator child_begin();
332  virtual child_iterator child_end();
333
334  virtual void EmitImpl(llvm::Serializer& S) const;
335  static PredefinedExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
336};
337
338class IntegerLiteral : public Expr {
339  llvm::APInt Value;
340  SourceLocation Loc;
341public:
342  // type should be IntTy, LongTy, LongLongTy, UnsignedIntTy, UnsignedLongTy,
343  // or UnsignedLongLongTy
344  IntegerLiteral(const llvm::APInt &V, QualType type, SourceLocation l)
345    : Expr(IntegerLiteralClass, type), Value(V), Loc(l) {
346    assert(type->isIntegerType() && "Illegal type in IntegerLiteral");
347  }
348  const llvm::APInt &getValue() const { return Value; }
349  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
350
351  static bool classof(const Stmt *T) {
352    return T->getStmtClass() == IntegerLiteralClass;
353  }
354  static bool classof(const IntegerLiteral *) { 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 IntegerLiteral* CreateImpl(llvm::Deserializer& D, ASTContext& C);
362};
363
364class CharacterLiteral : public Expr {
365  unsigned Value;
366  SourceLocation Loc;
367  bool IsWide;
368public:
369  // type should be IntTy
370  CharacterLiteral(unsigned value, bool iswide, QualType type, SourceLocation l)
371    : Expr(CharacterLiteralClass, type), Value(value), Loc(l), IsWide(iswide) {
372  }
373  SourceLocation getLoc() const { return Loc; }
374  bool isWide() const { return IsWide; }
375
376  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
377
378  unsigned getValue() const { return Value; }
379
380  static bool classof(const Stmt *T) {
381    return T->getStmtClass() == CharacterLiteralClass;
382  }
383  static bool classof(const CharacterLiteral *) { return true; }
384
385  // Iterators
386  virtual child_iterator child_begin();
387  virtual child_iterator child_end();
388
389  virtual void EmitImpl(llvm::Serializer& S) const;
390  static CharacterLiteral* CreateImpl(llvm::Deserializer& D, ASTContext& C);
391};
392
393class FloatingLiteral : public Expr {
394  llvm::APFloat Value;
395  bool IsExact : 1;
396  SourceLocation Loc;
397public:
398  FloatingLiteral(const llvm::APFloat &V, bool* isexact,
399                  QualType Type, SourceLocation L)
400    : Expr(FloatingLiteralClass, Type), Value(V), IsExact(*isexact), Loc(L) {}
401
402  const llvm::APFloat &getValue() const { return Value; }
403
404  bool isExact() const { return IsExact; }
405
406  /// getValueAsApproximateDouble - This returns the value as an inaccurate
407  /// double.  Note that this may cause loss of precision, but is useful for
408  /// debugging dumps, etc.
409  double getValueAsApproximateDouble() const;
410
411  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
412
413  static bool classof(const Stmt *T) {
414    return T->getStmtClass() == FloatingLiteralClass;
415  }
416  static bool classof(const FloatingLiteral *) { return true; }
417
418  // Iterators
419  virtual child_iterator child_begin();
420  virtual child_iterator child_end();
421
422  virtual void EmitImpl(llvm::Serializer& S) const;
423  static FloatingLiteral* CreateImpl(llvm::Deserializer& D, ASTContext& C);
424};
425
426/// ImaginaryLiteral - We support imaginary integer and floating point literals,
427/// like "1.0i".  We represent these as a wrapper around FloatingLiteral and
428/// IntegerLiteral classes.  Instances of this class always have a Complex type
429/// whose element type matches the subexpression.
430///
431class ImaginaryLiteral : public Expr {
432  Stmt *Val;
433public:
434  ImaginaryLiteral(Expr *val, QualType Ty)
435    : Expr(ImaginaryLiteralClass, Ty), Val(val) {}
436
437  const Expr *getSubExpr() const { return cast<Expr>(Val); }
438  Expr *getSubExpr() { return cast<Expr>(Val); }
439
440  virtual SourceRange getSourceRange() const { return Val->getSourceRange(); }
441  static bool classof(const Stmt *T) {
442    return T->getStmtClass() == ImaginaryLiteralClass;
443  }
444  static bool classof(const ImaginaryLiteral *) { return true; }
445
446  // Iterators
447  virtual child_iterator child_begin();
448  virtual child_iterator child_end();
449
450  virtual void EmitImpl(llvm::Serializer& S) const;
451  static ImaginaryLiteral* CreateImpl(llvm::Deserializer& D, ASTContext& C);
452};
453
454/// StringLiteral - This represents a string literal expression, e.g. "foo"
455/// or L"bar" (wide strings).  The actual string is returned by getStrData()
456/// is NOT null-terminated, and the length of the string is determined by
457/// calling getByteLength().  The C type for a string is always a
458/// ConstantArrayType.
459class StringLiteral : public Expr {
460  const char *StrData;
461  unsigned ByteLength;
462  bool IsWide;
463  // if the StringLiteral was composed using token pasting, both locations
464  // are needed. If not (the common case), firstTokLoc == lastTokLoc.
465  // FIXME: if space becomes an issue, we should create a sub-class.
466  SourceLocation firstTokLoc, lastTokLoc;
467public:
468  StringLiteral(const char *strData, unsigned byteLength, bool Wide,
469                QualType t, SourceLocation b, SourceLocation e);
470  virtual ~StringLiteral();
471
472  const char *getStrData() const { return StrData; }
473  unsigned getByteLength() const { return ByteLength; }
474  bool isWide() const { return IsWide; }
475
476  virtual SourceRange getSourceRange() const {
477    return SourceRange(firstTokLoc,lastTokLoc);
478  }
479  static bool classof(const Stmt *T) {
480    return T->getStmtClass() == StringLiteralClass;
481  }
482  static bool classof(const StringLiteral *) { return true; }
483
484  // Iterators
485  virtual child_iterator child_begin();
486  virtual child_iterator child_end();
487
488  virtual void EmitImpl(llvm::Serializer& S) const;
489  static StringLiteral* CreateImpl(llvm::Deserializer& D, ASTContext& C);
490};
491
492/// ParenExpr - This represents a parethesized expression, e.g. "(1)".  This
493/// AST node is only formed if full location information is requested.
494class ParenExpr : public Expr {
495  SourceLocation L, R;
496  Stmt *Val;
497public:
498  ParenExpr(SourceLocation l, SourceLocation r, Expr *val)
499    : Expr(ParenExprClass, val->getType(),
500           val->isTypeDependent(), val->isValueDependent()),
501      L(l), R(r), Val(val) {}
502
503  const Expr *getSubExpr() const { return cast<Expr>(Val); }
504  Expr *getSubExpr() { return cast<Expr>(Val); }
505  virtual SourceRange getSourceRange() const { return SourceRange(L, R); }
506
507  static bool classof(const Stmt *T) {
508    return T->getStmtClass() == ParenExprClass;
509  }
510  static bool classof(const ParenExpr *) { return true; }
511
512  // Iterators
513  virtual child_iterator child_begin();
514  virtual child_iterator child_end();
515
516  virtual void EmitImpl(llvm::Serializer& S) const;
517  static ParenExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
518};
519
520
521/// UnaryOperator - This represents the unary-expression's (except sizeof and
522/// alignof), the postinc/postdec operators from postfix-expression, and various
523/// extensions.
524///
525/// Notes on various nodes:
526///
527/// Real/Imag - These return the real/imag part of a complex operand.  If
528///   applied to a non-complex value, the former returns its operand and the
529///   later returns zero in the type of the operand.
530///
531/// __builtin_offsetof(type, a.b[10]) is represented as a unary operator whose
532///   subexpression is a compound literal with the various MemberExpr and
533///   ArraySubscriptExpr's applied to it.
534///
535class UnaryOperator : public Expr {
536public:
537  // Note that additions to this should also update the StmtVisitor class.
538  enum Opcode {
539    PostInc, PostDec, // [C99 6.5.2.4] Postfix increment and decrement operators
540    PreInc, PreDec,   // [C99 6.5.3.1] Prefix increment and decrement operators.
541    AddrOf, Deref,    // [C99 6.5.3.2] Address and indirection operators.
542    Plus, Minus,      // [C99 6.5.3.3] Unary arithmetic operators.
543    Not, LNot,        // [C99 6.5.3.3] Unary arithmetic operators.
544    Real, Imag,       // "__real expr"/"__imag expr" Extension.
545    Extension,        // __extension__ marker.
546    OffsetOf          // __builtin_offsetof
547  };
548private:
549  Stmt *Val;
550  Opcode Opc;
551  SourceLocation Loc;
552public:
553
554  UnaryOperator(Expr *input, Opcode opc, QualType type, SourceLocation l)
555    : Expr(UnaryOperatorClass, type,
556           input->isTypeDependent() && opc != OffsetOf,
557           input->isValueDependent()),
558      Val(input), Opc(opc), Loc(l) {}
559
560  Opcode getOpcode() const { return Opc; }
561  Expr *getSubExpr() const { return cast<Expr>(Val); }
562
563  /// getOperatorLoc - Return the location of the operator.
564  SourceLocation getOperatorLoc() const { return Loc; }
565
566  /// isPostfix - Return true if this is a postfix operation, like x++.
567  static bool isPostfix(Opcode Op);
568
569  /// isPostfix - Return true if this is a prefix operation, like --x.
570  static bool isPrefix(Opcode Op);
571
572  bool isPrefix() const { return isPrefix(Opc); }
573  bool isPostfix() const { return isPostfix(Opc); }
574  bool isIncrementOp() const {return Opc==PreInc || Opc==PostInc; }
575  bool isIncrementDecrementOp() const { return Opc>=PostInc && Opc<=PreDec; }
576  bool isOffsetOfOp() const { return Opc == OffsetOf; }
577  static bool isArithmeticOp(Opcode Op) { return Op >= Plus && Op <= LNot; }
578
579  /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
580  /// corresponds to, e.g. "sizeof" or "[pre]++"
581  static const char *getOpcodeStr(Opcode Op);
582
583  virtual SourceRange getSourceRange() const {
584    if (isPostfix())
585      return SourceRange(Val->getLocStart(), Loc);
586    else
587      return SourceRange(Loc, Val->getLocEnd());
588  }
589  virtual SourceLocation getExprLoc() const { return Loc; }
590
591  static bool classof(const Stmt *T) {
592    return T->getStmtClass() == UnaryOperatorClass;
593  }
594  static bool classof(const UnaryOperator *) { return true; }
595
596  int64_t evaluateOffsetOf(ASTContext& C) const;
597
598  // Iterators
599  virtual child_iterator child_begin();
600  virtual child_iterator child_end();
601
602  virtual void EmitImpl(llvm::Serializer& S) const;
603  static UnaryOperator* CreateImpl(llvm::Deserializer& D, ASTContext& C);
604};
605
606/// SizeOfAlignOfExpr - [C99 6.5.3.4] - This is for sizeof/alignof, both of
607/// types and expressions.
608class SizeOfAlignOfExpr : public Expr {
609  bool isSizeof : 1;  // true if sizeof, false if alignof.
610  bool isType : 1;    // true if operand is a type, false if an expression
611  union {
612    void *Ty;
613    Stmt *Ex;
614  } Argument;
615  SourceLocation OpLoc, RParenLoc;
616public:
617  SizeOfAlignOfExpr(bool issizeof, bool istype, void *argument,
618                    QualType resultType, SourceLocation op,
619                    SourceLocation rp) :
620      Expr(SizeOfAlignOfExprClass, resultType), isSizeof(issizeof),
621      isType(istype), OpLoc(op), RParenLoc(rp) {
622    if (isType)
623      Argument.Ty = argument;
624    else
625      // argument was an Expr*, so cast it back to that to be safe
626      Argument.Ex = static_cast<Expr*>(argument);
627  }
628
629  virtual void Destroy(ASTContext& C);
630
631  bool isSizeOf() const { return isSizeof; }
632  bool isArgumentType() const { return isType; }
633  QualType getArgumentType() const {
634    assert(isArgumentType() && "calling getArgumentType() when arg is expr");
635    return QualType::getFromOpaquePtr(Argument.Ty);
636  }
637  Expr* getArgumentExpr() const {
638    assert(!isArgumentType() && "calling getArgumentExpr() when arg is type");
639    return static_cast<Expr*>(Argument.Ex);
640  }
641  /// Gets the argument type, or the type of the argument expression, whichever
642  /// is appropriate.
643  QualType getTypeOfArgument() const {
644    return isArgumentType() ? getArgumentType() : getArgumentExpr()->getType();
645  }
646
647  SourceLocation getOperatorLoc() const { return OpLoc; }
648
649  virtual SourceRange getSourceRange() const {
650    return SourceRange(OpLoc, RParenLoc);
651  }
652
653  static bool classof(const Stmt *T) {
654    return T->getStmtClass() == SizeOfAlignOfExprClass;
655  }
656  static bool classof(const SizeOfAlignOfExpr *) { return true; }
657
658  // Iterators
659  virtual child_iterator child_begin();
660  virtual child_iterator child_end();
661
662  virtual void EmitImpl(llvm::Serializer& S) const;
663  static SizeOfAlignOfExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
664};
665
666//===----------------------------------------------------------------------===//
667// Postfix Operators.
668//===----------------------------------------------------------------------===//
669
670/// ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting.
671class ArraySubscriptExpr : public Expr {
672  enum { LHS, RHS, END_EXPR=2 };
673  Stmt* SubExprs[END_EXPR];
674  SourceLocation RBracketLoc;
675public:
676  ArraySubscriptExpr(Expr *lhs, Expr *rhs, QualType t,
677                     SourceLocation rbracketloc)
678  : Expr(ArraySubscriptExprClass, t), RBracketLoc(rbracketloc) {
679    SubExprs[LHS] = lhs;
680    SubExprs[RHS] = rhs;
681  }
682
683  /// An array access can be written A[4] or 4[A] (both are equivalent).
684  /// - getBase() and getIdx() always present the normalized view: A[4].
685  ///    In this case getBase() returns "A" and getIdx() returns "4".
686  /// - getLHS() and getRHS() present the syntactic view. e.g. for
687  ///    4[A] getLHS() returns "4".
688  /// Note: Because vector element access is also written A[4] we must
689  /// predicate the format conversion in getBase and getIdx only on the
690  /// the type of the RHS, as it is possible for the LHS to be a vector of
691  /// integer type
692  Expr *getLHS() { return cast<Expr>(SubExprs[LHS]); }
693  const Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); }
694
695  Expr *getRHS() { return cast<Expr>(SubExprs[RHS]); }
696  const Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); }
697
698  Expr *getBase() {
699    return cast<Expr>(getRHS()->getType()->isIntegerType() ? getLHS():getRHS());
700  }
701
702  const Expr *getBase() const {
703    return cast<Expr>(getRHS()->getType()->isIntegerType() ? getLHS():getRHS());
704  }
705
706  Expr *getIdx() {
707    return cast<Expr>(getRHS()->getType()->isIntegerType() ? getRHS():getLHS());
708  }
709
710  const Expr *getIdx() const {
711    return cast<Expr>(getRHS()->getType()->isIntegerType() ? getRHS():getLHS());
712  }
713
714  virtual SourceRange getSourceRange() const {
715    return SourceRange(getLHS()->getLocStart(), RBracketLoc);
716  }
717
718  virtual SourceLocation getExprLoc() const { return RBracketLoc; }
719
720  static bool classof(const Stmt *T) {
721    return T->getStmtClass() == ArraySubscriptExprClass;
722  }
723  static bool classof(const ArraySubscriptExpr *) { return true; }
724
725  // Iterators
726  virtual child_iterator child_begin();
727  virtual child_iterator child_end();
728
729  virtual void EmitImpl(llvm::Serializer& S) const;
730  static ArraySubscriptExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
731};
732
733
734/// CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
735/// CallExpr itself represents a normal function call, e.g., "f(x, 2)",
736/// while its subclasses may represent alternative syntax that (semantically)
737/// results in a function call. For example, CXXOperatorCallExpr is
738/// a subclass for overloaded operator calls that use operator syntax, e.g.,
739/// "str1 + str2" to resolve to a function call.
740class CallExpr : public Expr {
741  enum { FN=0, ARGS_START=1 };
742  Stmt **SubExprs;
743  unsigned NumArgs;
744  SourceLocation RParenLoc;
745
746  // This version of the ctor is for deserialization.
747  CallExpr(StmtClass SC, Stmt** subexprs, unsigned numargs, QualType t,
748           SourceLocation rparenloc)
749  : Expr(SC,t), SubExprs(subexprs),
750    NumArgs(numargs), RParenLoc(rparenloc) {}
751
752protected:
753  // This version of the constructor is for derived classes.
754  CallExpr(StmtClass SC, Expr *fn, Expr **args, unsigned numargs, QualType t,
755           SourceLocation rparenloc);
756
757public:
758  CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
759           SourceLocation rparenloc);
760  ~CallExpr() {
761    delete [] SubExprs;
762  }
763
764  const Expr *getCallee() const { return cast<Expr>(SubExprs[FN]); }
765  Expr *getCallee() { return cast<Expr>(SubExprs[FN]); }
766  void setCallee(Expr *F) { SubExprs[FN] = F; }
767
768  /// getNumArgs - Return the number of actual arguments to this call.
769  ///
770  unsigned getNumArgs() const { return NumArgs; }
771
772  /// getArg - Return the specified argument.
773  Expr *getArg(unsigned Arg) {
774    assert(Arg < NumArgs && "Arg access out of range!");
775    return cast<Expr>(SubExprs[Arg+ARGS_START]);
776  }
777  const Expr *getArg(unsigned Arg) const {
778    assert(Arg < NumArgs && "Arg access out of range!");
779    return cast<Expr>(SubExprs[Arg+ARGS_START]);
780  }
781  /// setArg - Set the specified argument.
782  void setArg(unsigned Arg, Expr *ArgExpr) {
783    assert(Arg < NumArgs && "Arg access out of range!");
784    SubExprs[Arg+ARGS_START] = ArgExpr;
785  }
786
787  /// setNumArgs - This changes the number of arguments present in this call.
788  /// Any orphaned expressions are deleted by this, and any new operands are set
789  /// to null.
790  void setNumArgs(unsigned NumArgs);
791
792  typedef ExprIterator arg_iterator;
793  typedef ConstExprIterator const_arg_iterator;
794
795  arg_iterator arg_begin() { return SubExprs+ARGS_START; }
796  arg_iterator arg_end() { return SubExprs+ARGS_START+getNumArgs(); }
797  const_arg_iterator arg_begin() const { return SubExprs+ARGS_START; }
798  const_arg_iterator arg_end() const { return SubExprs+ARGS_START+getNumArgs();}
799
800  /// getNumCommas - Return the number of commas that must have been present in
801  /// this function call.
802  unsigned getNumCommas() const { return NumArgs ? NumArgs - 1 : 0; }
803
804  /// isBuiltinCall - If this is a call to a builtin, return the builtin ID.  If
805  /// not, return 0.
806  unsigned isBuiltinCall() const;
807
808  SourceLocation getRParenLoc() const { return RParenLoc; }
809
810  virtual SourceRange getSourceRange() const {
811    return SourceRange(getCallee()->getLocStart(), RParenLoc);
812  }
813
814  static bool classof(const Stmt *T) {
815    return T->getStmtClass() == CallExprClass ||
816           T->getStmtClass() == CXXOperatorCallExprClass;
817  }
818  static bool classof(const CallExpr *) { return true; }
819
820  // Iterators
821  virtual child_iterator child_begin();
822  virtual child_iterator child_end();
823
824  virtual void EmitImpl(llvm::Serializer& S) const;
825  static CallExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C,
826                              StmtClass SC);
827};
828
829/// MemberExpr - [C99 6.5.2.3] Structure and Union Members.
830///
831class MemberExpr : public Expr {
832  Stmt *Base;
833  FieldDecl *MemberDecl;
834  SourceLocation MemberLoc;
835  bool IsArrow;      // True if this is "X->F", false if this is "X.F".
836public:
837  MemberExpr(Expr *base, bool isarrow, FieldDecl *memberdecl, SourceLocation l,
838             QualType ty)
839    : Expr(MemberExprClass, ty),
840      Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow) {}
841
842  Expr *getBase() const { return cast<Expr>(Base); }
843  FieldDecl *getMemberDecl() const { return MemberDecl; }
844  bool isArrow() const { return IsArrow; }
845
846  virtual SourceRange getSourceRange() const {
847    return SourceRange(getBase()->getLocStart(), MemberLoc);
848  }
849
850  virtual SourceLocation getExprLoc() const { return MemberLoc; }
851
852  static bool classof(const Stmt *T) {
853    return T->getStmtClass() == MemberExprClass;
854  }
855  static bool classof(const MemberExpr *) { return true; }
856
857  // Iterators
858  virtual child_iterator child_begin();
859  virtual child_iterator child_end();
860
861  virtual void EmitImpl(llvm::Serializer& S) const;
862  static MemberExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
863};
864
865/// CompoundLiteralExpr - [C99 6.5.2.5]
866///
867class CompoundLiteralExpr : public Expr {
868  /// LParenLoc - If non-null, this is the location of the left paren in a
869  /// compound literal like "(int){4}".  This can be null if this is a
870  /// synthesized compound expression.
871  SourceLocation LParenLoc;
872  Stmt *Init;
873  bool FileScope;
874public:
875  CompoundLiteralExpr(SourceLocation lparenloc, QualType ty, Expr *init,
876                      bool fileScope)
877    : Expr(CompoundLiteralExprClass, ty), LParenLoc(lparenloc), Init(init),
878      FileScope(fileScope) {}
879
880  const Expr *getInitializer() const { return cast<Expr>(Init); }
881  Expr *getInitializer() { return cast<Expr>(Init); }
882
883  bool isFileScope() const { return FileScope; }
884
885  SourceLocation getLParenLoc() const { return LParenLoc; }
886
887  virtual SourceRange getSourceRange() const {
888    // FIXME: Init should never be null.
889    if (!Init)
890      return SourceRange();
891    if (LParenLoc.isInvalid())
892      return Init->getSourceRange();
893    return SourceRange(LParenLoc, Init->getLocEnd());
894  }
895
896  static bool classof(const Stmt *T) {
897    return T->getStmtClass() == CompoundLiteralExprClass;
898  }
899  static bool classof(const CompoundLiteralExpr *) { 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 CompoundLiteralExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
907};
908
909/// CastExpr - Base class for type casts, including both implicit
910/// casts (ImplicitCastExpr) and explicit casts that have some
911/// representation in the source code (ExplicitCastExpr's derived
912/// classes).
913class CastExpr : public Expr {
914  Stmt *Op;
915protected:
916  CastExpr(StmtClass SC, QualType ty, Expr *op) :
917    Expr(SC, ty,
918         // Cast expressions are type-dependent if the type is
919         // dependent (C++ [temp.dep.expr]p3).
920         ty->isDependentType(),
921         // Cast expressions are value-dependent if the type is
922         // dependent or if the subexpression is value-dependent.
923         ty->isDependentType() || (op && op->isValueDependent())),
924    Op(op) {}
925
926public:
927  Expr *getSubExpr() { return cast<Expr>(Op); }
928  const Expr *getSubExpr() const { return cast<Expr>(Op); }
929
930  static bool classof(const Stmt *T) {
931    StmtClass SC = T->getStmtClass();
932    if (SC >= CXXNamedCastExprClass && SC <= CXXFunctionalCastExprClass)
933      return true;
934
935    if (SC >= ImplicitCastExprClass && SC <= CStyleCastExprClass)
936      return true;
937
938    return false;
939  }
940  static bool classof(const CastExpr *) { return true; }
941
942  // Iterators
943  virtual child_iterator child_begin();
944  virtual child_iterator child_end();
945};
946
947/// ImplicitCastExpr - Allows us to explicitly represent implicit type
948/// conversions, which have no direct representation in the original
949/// source code. For example: converting T[]->T*, void f()->void
950/// (*f)(), float->double, short->int, etc.
951///
952/// In C, implicit casts always produce rvalues. However, in C++, an
953/// implicit cast whose result is being bound to a reference will be
954/// an lvalue. For example:
955///
956/// @code
957/// class Base { };
958/// class Derived : public Base { };
959/// void f(Derived d) {
960///   Base& b = d; // initializer is an ImplicitCastExpr to an lvalue of type Base
961/// }
962/// @endcode
963class ImplicitCastExpr : public CastExpr {
964  /// LvalueCast - Whether this cast produces an lvalue.
965  bool LvalueCast;
966
967public:
968  ImplicitCastExpr(QualType ty, Expr *op, bool Lvalue) :
969    CastExpr(ImplicitCastExprClass, ty, op), LvalueCast(Lvalue) {}
970
971  virtual SourceRange getSourceRange() const {
972    return getSubExpr()->getSourceRange();
973  }
974
975  /// isLvalueCast - Whether this cast produces an lvalue.
976  bool isLvalueCast() const { return LvalueCast; }
977
978  /// setLvalueCast - Set whether this cast produces an lvalue.
979  void setLvalueCast(bool Lvalue) { LvalueCast = Lvalue; }
980
981  static bool classof(const Stmt *T) {
982    return T->getStmtClass() == ImplicitCastExprClass;
983  }
984  static bool classof(const ImplicitCastExpr *) { return true; }
985
986  virtual void EmitImpl(llvm::Serializer& S) const;
987  static ImplicitCastExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
988};
989
990/// ExplicitCastExpr - An explicit cast written in the source
991/// code.
992///
993/// This class is effectively an abstract class, because it provides
994/// the basic representation of an explicitly-written cast without
995/// specifying which kind of cast (C cast, functional cast, static
996/// cast, etc.) was written; specific derived classes represent the
997/// particular style of cast and its location information.
998///
999/// Unlike implicit casts, explicit cast nodes have two different
1000/// types: the type that was written into the source code, and the
1001/// actual type of the expression as determined by semantic
1002/// analysis. These types may differ slightly. For example, in C++ one
1003/// can cast to a reference type, which indicates that the resulting
1004/// expression will be an lvalue. The reference type, however, will
1005/// not be used as the type of the expression.
1006class ExplicitCastExpr : public CastExpr {
1007  /// TypeAsWritten - The type that this expression is casting to, as
1008  /// written in the source code.
1009  QualType TypeAsWritten;
1010
1011protected:
1012  ExplicitCastExpr(StmtClass SC, QualType exprTy, Expr *op, QualType writtenTy)
1013    : CastExpr(SC, exprTy, op), TypeAsWritten(writtenTy) {}
1014
1015public:
1016  /// getTypeAsWritten - Returns the type that this expression is
1017  /// casting to, as written in the source code.
1018  QualType getTypeAsWritten() const { return TypeAsWritten; }
1019
1020  static bool classof(const Stmt *T) {
1021    StmtClass SC = T->getStmtClass();
1022    if (SC >= ExplicitCastExprClass && SC <= CStyleCastExprClass)
1023      return true;
1024    if (SC >= CXXNamedCastExprClass && SC <= CXXFunctionalCastExprClass)
1025      return true;
1026
1027    return false;
1028  }
1029  static bool classof(const ExplicitCastExpr *) { return true; }
1030};
1031
1032/// CStyleCastExpr - An explicit cast in C (C99 6.5.4) or a C-style
1033/// cast in C++ (C++ [expr.cast]), which uses the syntax
1034/// (Type)expr. For example: @c (int)f.
1035class CStyleCastExpr : public ExplicitCastExpr {
1036  SourceLocation LPLoc; // the location of the left paren
1037  SourceLocation RPLoc; // the location of the right paren
1038public:
1039  CStyleCastExpr(QualType exprTy, Expr *op, QualType writtenTy,
1040                    SourceLocation l, SourceLocation r) :
1041    ExplicitCastExpr(CStyleCastExprClass, exprTy, op, writtenTy),
1042    LPLoc(l), RPLoc(r) {}
1043
1044  SourceLocation getLParenLoc() const { return LPLoc; }
1045  SourceLocation getRParenLoc() const { return RPLoc; }
1046
1047  virtual SourceRange getSourceRange() const {
1048    return SourceRange(LPLoc, getSubExpr()->getSourceRange().getEnd());
1049  }
1050  static bool classof(const Stmt *T) {
1051    return T->getStmtClass() == CStyleCastExprClass;
1052  }
1053  static bool classof(const CStyleCastExpr *) { return true; }
1054
1055  virtual void EmitImpl(llvm::Serializer& S) const;
1056  static CStyleCastExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1057};
1058
1059class BinaryOperator : public Expr {
1060public:
1061  enum Opcode {
1062    // Operators listed in order of precedence.
1063    // Note that additions to this should also update the StmtVisitor class.
1064    Mul, Div, Rem,    // [C99 6.5.5] Multiplicative operators.
1065    Add, Sub,         // [C99 6.5.6] Additive operators.
1066    Shl, Shr,         // [C99 6.5.7] Bitwise shift operators.
1067    LT, GT, LE, GE,   // [C99 6.5.8] Relational operators.
1068    EQ, NE,           // [C99 6.5.9] Equality operators.
1069    And,              // [C99 6.5.10] Bitwise AND operator.
1070    Xor,              // [C99 6.5.11] Bitwise XOR operator.
1071    Or,               // [C99 6.5.12] Bitwise OR operator.
1072    LAnd,             // [C99 6.5.13] Logical AND operator.
1073    LOr,              // [C99 6.5.14] Logical OR operator.
1074    Assign, MulAssign,// [C99 6.5.16] Assignment operators.
1075    DivAssign, RemAssign,
1076    AddAssign, SubAssign,
1077    ShlAssign, ShrAssign,
1078    AndAssign, XorAssign,
1079    OrAssign,
1080    Comma             // [C99 6.5.17] Comma operator.
1081  };
1082private:
1083  enum { LHS, RHS, END_EXPR };
1084  Stmt* SubExprs[END_EXPR];
1085  Opcode Opc;
1086  SourceLocation OpLoc;
1087public:
1088
1089  BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy,
1090                 SourceLocation opLoc)
1091    : Expr(BinaryOperatorClass, ResTy,
1092           lhs->isTypeDependent() || rhs->isTypeDependent(),
1093           lhs->isValueDependent() || rhs->isValueDependent()),
1094      Opc(opc), OpLoc(opLoc) {
1095    SubExprs[LHS] = lhs;
1096    SubExprs[RHS] = rhs;
1097    assert(!isCompoundAssignmentOp() &&
1098           "Use ArithAssignBinaryOperator for compound assignments");
1099  }
1100
1101  SourceLocation getOperatorLoc() const { return OpLoc; }
1102  Opcode getOpcode() const { return Opc; }
1103  Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); }
1104  Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); }
1105  virtual SourceRange getSourceRange() const {
1106    return SourceRange(getLHS()->getLocStart(), getRHS()->getLocEnd());
1107  }
1108
1109  /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1110  /// corresponds to, e.g. "<<=".
1111  static const char *getOpcodeStr(Opcode Op);
1112
1113  /// predicates to categorize the respective opcodes.
1114  bool isMultiplicativeOp() const { return Opc >= Mul && Opc <= Rem; }
1115  bool isAdditiveOp() const { return Opc == Add || Opc == Sub; }
1116  bool isShiftOp() const { return Opc == Shl || Opc == Shr; }
1117  bool isBitwiseOp() const { return Opc >= And && Opc <= Or; }
1118
1119  static bool isRelationalOp(Opcode Opc) { return Opc >= LT && Opc <= GE; }
1120  bool isRelationalOp() const { return isRelationalOp(Opc); }
1121
1122  static bool isEqualityOp(Opcode Opc) { return Opc == EQ || Opc == NE; }
1123  bool isEqualityOp() const { return isEqualityOp(Opc); }
1124
1125  static bool isLogicalOp(Opcode Opc) { return Opc == LAnd || Opc == LOr; }
1126  bool isLogicalOp() const { return isLogicalOp(Opc); }
1127
1128  bool isAssignmentOp() const { return Opc >= Assign && Opc <= OrAssign; }
1129  bool isCompoundAssignmentOp() const { return Opc > Assign && Opc <= OrAssign;}
1130  bool isShiftAssignOp() const { return Opc == ShlAssign || Opc == ShrAssign; }
1131
1132  static bool classof(const Stmt *S) {
1133    return S->getStmtClass() == BinaryOperatorClass ||
1134           S->getStmtClass() == CompoundAssignOperatorClass;
1135  }
1136  static bool classof(const BinaryOperator *) { return true; }
1137
1138  // Iterators
1139  virtual child_iterator child_begin();
1140  virtual child_iterator child_end();
1141
1142  virtual void EmitImpl(llvm::Serializer& S) const;
1143  static BinaryOperator* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1144
1145protected:
1146  BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy,
1147                 SourceLocation oploc, bool dead)
1148    : Expr(CompoundAssignOperatorClass, ResTy), Opc(opc), OpLoc(oploc) {
1149    SubExprs[LHS] = lhs;
1150    SubExprs[RHS] = rhs;
1151  }
1152};
1153
1154/// CompoundAssignOperator - For compound assignments (e.g. +=), we keep
1155/// track of the type the operation is performed in.  Due to the semantics of
1156/// these operators, the operands are promoted, the aritmetic performed, an
1157/// implicit conversion back to the result type done, then the assignment takes
1158/// place.  This captures the intermediate type which the computation is done
1159/// in.
1160class CompoundAssignOperator : public BinaryOperator {
1161  QualType ComputationType;
1162public:
1163  CompoundAssignOperator(Expr *lhs, Expr *rhs, Opcode opc,
1164                         QualType ResType, QualType CompType,
1165                         SourceLocation OpLoc)
1166    : BinaryOperator(lhs, rhs, opc, ResType, OpLoc, true),
1167      ComputationType(CompType) {
1168    assert(isCompoundAssignmentOp() &&
1169           "Only should be used for compound assignments");
1170  }
1171
1172  QualType getComputationType() const { return ComputationType; }
1173
1174  static bool classof(const CompoundAssignOperator *) { return true; }
1175  static bool classof(const Stmt *S) {
1176    return S->getStmtClass() == CompoundAssignOperatorClass;
1177  }
1178
1179  virtual void EmitImpl(llvm::Serializer& S) const;
1180  static CompoundAssignOperator* CreateImpl(llvm::Deserializer& D,
1181                                            ASTContext& C);
1182};
1183
1184/// ConditionalOperator - The ?: operator.  Note that LHS may be null when the
1185/// GNU "missing LHS" extension is in use.
1186///
1187class ConditionalOperator : public Expr {
1188  enum { COND, LHS, RHS, END_EXPR };
1189  Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides.
1190public:
1191  ConditionalOperator(Expr *cond, Expr *lhs, Expr *rhs, QualType t)
1192    : Expr(ConditionalOperatorClass, t,
1193           // FIXME: the type of the conditional operator doesn't
1194           // depend on the type of the conditional, but the standard
1195           // seems to imply that it could. File a bug!
1196           ((lhs && lhs->isTypeDependent()) || (rhs && rhs->isTypeDependent())),
1197           (cond->isValueDependent() ||
1198            (lhs && lhs->isValueDependent()) ||
1199            (rhs && rhs->isValueDependent()))) {
1200    SubExprs[COND] = cond;
1201    SubExprs[LHS] = lhs;
1202    SubExprs[RHS] = rhs;
1203  }
1204
1205  // getCond - Return the expression representing the condition for
1206  //  the ?: operator.
1207  Expr *getCond() const { return cast<Expr>(SubExprs[COND]); }
1208
1209  // getTrueExpr - Return the subexpression representing the value of the ?:
1210  //  expression if the condition evaluates to true.  In most cases this value
1211  //  will be the same as getLHS() except a GCC extension allows the left
1212  //  subexpression to be omitted, and instead of the condition be returned.
1213  //  e.g: x ?: y is shorthand for x ? x : y, except that the expression "x"
1214  //  is only evaluated once.
1215  Expr *getTrueExpr() const {
1216    return cast<Expr>(SubExprs[LHS] ? SubExprs[LHS] : SubExprs[COND]);
1217  }
1218
1219  // getTrueExpr - Return the subexpression representing the value of the ?:
1220  // expression if the condition evaluates to false. This is the same as getRHS.
1221  Expr *getFalseExpr() const { return cast<Expr>(SubExprs[RHS]); }
1222
1223  Expr *getLHS() const { return cast_or_null<Expr>(SubExprs[LHS]); }
1224  Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); }
1225
1226  virtual SourceRange getSourceRange() const {
1227    return SourceRange(getCond()->getLocStart(), getRHS()->getLocEnd());
1228  }
1229  static bool classof(const Stmt *T) {
1230    return T->getStmtClass() == ConditionalOperatorClass;
1231  }
1232  static bool classof(const ConditionalOperator *) { return true; }
1233
1234  // Iterators
1235  virtual child_iterator child_begin();
1236  virtual child_iterator child_end();
1237
1238  virtual void EmitImpl(llvm::Serializer& S) const;
1239  static ConditionalOperator* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1240};
1241
1242/// AddrLabelExpr - The GNU address of label extension, representing &&label.
1243class AddrLabelExpr : public Expr {
1244  SourceLocation AmpAmpLoc, LabelLoc;
1245  LabelStmt *Label;
1246public:
1247  AddrLabelExpr(SourceLocation AALoc, SourceLocation LLoc, LabelStmt *L,
1248                QualType t)
1249    : Expr(AddrLabelExprClass, t), AmpAmpLoc(AALoc), LabelLoc(LLoc), Label(L) {}
1250
1251  virtual SourceRange getSourceRange() const {
1252    return SourceRange(AmpAmpLoc, LabelLoc);
1253  }
1254
1255  LabelStmt *getLabel() const { return Label; }
1256
1257  static bool classof(const Stmt *T) {
1258    return T->getStmtClass() == AddrLabelExprClass;
1259  }
1260  static bool classof(const AddrLabelExpr *) { return true; }
1261
1262  // Iterators
1263  virtual child_iterator child_begin();
1264  virtual child_iterator child_end();
1265
1266  virtual void EmitImpl(llvm::Serializer& S) const;
1267  static AddrLabelExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1268};
1269
1270/// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}).
1271/// The StmtExpr contains a single CompoundStmt node, which it evaluates and
1272/// takes the value of the last subexpression.
1273class StmtExpr : public Expr {
1274  Stmt *SubStmt;
1275  SourceLocation LParenLoc, RParenLoc;
1276public:
1277  StmtExpr(CompoundStmt *substmt, QualType T,
1278           SourceLocation lp, SourceLocation rp) :
1279    Expr(StmtExprClass, T), SubStmt(substmt),  LParenLoc(lp), RParenLoc(rp) { }
1280
1281  CompoundStmt *getSubStmt() { return cast<CompoundStmt>(SubStmt); }
1282  const CompoundStmt *getSubStmt() const { return cast<CompoundStmt>(SubStmt); }
1283
1284  virtual SourceRange getSourceRange() const {
1285    return SourceRange(LParenLoc, RParenLoc);
1286  }
1287
1288  static bool classof(const Stmt *T) {
1289    return T->getStmtClass() == StmtExprClass;
1290  }
1291  static bool classof(const StmtExpr *) { return true; }
1292
1293  // Iterators
1294  virtual child_iterator child_begin();
1295  virtual child_iterator child_end();
1296
1297  virtual void EmitImpl(llvm::Serializer& S) const;
1298  static StmtExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1299};
1300
1301/// TypesCompatibleExpr - GNU builtin-in function __builtin_type_compatible_p.
1302/// This AST node represents a function that returns 1 if two *types* (not
1303/// expressions) are compatible. The result of this built-in function can be
1304/// used in integer constant expressions.
1305class TypesCompatibleExpr : public Expr {
1306  QualType Type1;
1307  QualType Type2;
1308  SourceLocation BuiltinLoc, RParenLoc;
1309public:
1310  TypesCompatibleExpr(QualType ReturnType, SourceLocation BLoc,
1311                      QualType t1, QualType t2, SourceLocation RP) :
1312    Expr(TypesCompatibleExprClass, ReturnType), Type1(t1), Type2(t2),
1313    BuiltinLoc(BLoc), RParenLoc(RP) {}
1314
1315  QualType getArgType1() const { return Type1; }
1316  QualType getArgType2() const { return Type2; }
1317
1318  virtual SourceRange getSourceRange() const {
1319    return SourceRange(BuiltinLoc, RParenLoc);
1320  }
1321  static bool classof(const Stmt *T) {
1322    return T->getStmtClass() == TypesCompatibleExprClass;
1323  }
1324  static bool classof(const TypesCompatibleExpr *) { return true; }
1325
1326  // Iterators
1327  virtual child_iterator child_begin();
1328  virtual child_iterator child_end();
1329
1330  virtual void EmitImpl(llvm::Serializer& S) const;
1331  static TypesCompatibleExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1332};
1333
1334/// ShuffleVectorExpr - clang-specific builtin-in function
1335/// __builtin_shufflevector.
1336/// This AST node represents a operator that does a constant
1337/// shuffle, similar to LLVM's shufflevector instruction. It takes
1338/// two vectors and a variable number of constant indices,
1339/// and returns the appropriately shuffled vector.
1340class ShuffleVectorExpr : public Expr {
1341  SourceLocation BuiltinLoc, RParenLoc;
1342
1343  // SubExprs - the list of values passed to the __builtin_shufflevector
1344  // function. The first two are vectors, and the rest are constant
1345  // indices.  The number of values in this list is always
1346  // 2+the number of indices in the vector type.
1347  Stmt **SubExprs;
1348  unsigned NumExprs;
1349
1350public:
1351  ShuffleVectorExpr(Expr **args, unsigned nexpr,
1352                    QualType Type, SourceLocation BLoc,
1353                    SourceLocation RP) :
1354    Expr(ShuffleVectorExprClass, Type), BuiltinLoc(BLoc),
1355    RParenLoc(RP), NumExprs(nexpr) {
1356
1357    SubExprs = new Stmt*[nexpr];
1358    for (unsigned i = 0; i < nexpr; i++)
1359      SubExprs[i] = args[i];
1360  }
1361
1362  virtual SourceRange getSourceRange() const {
1363    return SourceRange(BuiltinLoc, RParenLoc);
1364  }
1365  static bool classof(const Stmt *T) {
1366    return T->getStmtClass() == ShuffleVectorExprClass;
1367  }
1368  static bool classof(const ShuffleVectorExpr *) { return true; }
1369
1370  ~ShuffleVectorExpr() {
1371    delete [] SubExprs;
1372  }
1373
1374  /// getNumSubExprs - Return the size of the SubExprs array.  This includes the
1375  /// constant expression, the actual arguments passed in, and the function
1376  /// pointers.
1377  unsigned getNumSubExprs() const { return NumExprs; }
1378
1379  /// getExpr - Return the Expr at the specified index.
1380  Expr *getExpr(unsigned Index) {
1381    assert((Index < NumExprs) && "Arg access out of range!");
1382    return cast<Expr>(SubExprs[Index]);
1383  }
1384  const Expr *getExpr(unsigned Index) const {
1385    assert((Index < NumExprs) && "Arg access out of range!");
1386    return cast<Expr>(SubExprs[Index]);
1387  }
1388
1389  unsigned getShuffleMaskIdx(ASTContext &Ctx, unsigned N) {
1390    assert((N < NumExprs - 2) && "Shuffle idx out of range!");
1391    return getExpr(N+2)->getIntegerConstantExprValue(Ctx).getZExtValue();
1392  }
1393
1394  // Iterators
1395  virtual child_iterator child_begin();
1396  virtual child_iterator child_end();
1397
1398  virtual void EmitImpl(llvm::Serializer& S) const;
1399  static ShuffleVectorExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1400};
1401
1402/// ChooseExpr - GNU builtin-in function __builtin_choose_expr.
1403/// This AST node is similar to the conditional operator (?:) in C, with
1404/// the following exceptions:
1405/// - the test expression must be a constant expression.
1406/// - the expression returned has it's type unaltered by promotion rules.
1407/// - does not evaluate the expression that was not chosen.
1408class ChooseExpr : public Expr {
1409  enum { COND, LHS, RHS, END_EXPR };
1410  Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides.
1411  SourceLocation BuiltinLoc, RParenLoc;
1412public:
1413  ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, QualType t,
1414             SourceLocation RP)
1415    : Expr(ChooseExprClass, t),
1416      BuiltinLoc(BLoc), RParenLoc(RP) {
1417      SubExprs[COND] = cond;
1418      SubExprs[LHS] = lhs;
1419      SubExprs[RHS] = rhs;
1420    }
1421
1422  /// isConditionTrue - Return true if the condition is true.  This is always
1423  /// statically knowable for a well-formed choosexpr.
1424  bool isConditionTrue(ASTContext &C) const;
1425
1426  Expr *getCond() const { return cast<Expr>(SubExprs[COND]); }
1427  Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); }
1428  Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); }
1429
1430  virtual SourceRange getSourceRange() const {
1431    return SourceRange(BuiltinLoc, RParenLoc);
1432  }
1433  static bool classof(const Stmt *T) {
1434    return T->getStmtClass() == ChooseExprClass;
1435  }
1436  static bool classof(const ChooseExpr *) { return true; }
1437
1438  // Iterators
1439  virtual child_iterator child_begin();
1440  virtual child_iterator child_end();
1441
1442  virtual void EmitImpl(llvm::Serializer& S) const;
1443  static ChooseExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1444};
1445
1446/// GNUNullExpr - Implements the GNU __null extension, which is a name
1447/// for a null pointer constant that has integral type (e.g., int or
1448/// long) and is the same size and alignment as a pointer. The __null
1449/// extension is typically only used by system headers, which define
1450/// NULL as __null in C++ rather than using 0 (which is an integer
1451/// that may not match the size of a pointer).
1452class GNUNullExpr : public Expr {
1453  /// TokenLoc - The location of the __null keyword.
1454  SourceLocation TokenLoc;
1455
1456public:
1457  GNUNullExpr(QualType Ty, SourceLocation Loc)
1458    : Expr(GNUNullExprClass, Ty), TokenLoc(Loc) { }
1459
1460  /// getTokenLocation - The location of the __null token.
1461  SourceLocation getTokenLocation() const { return TokenLoc; }
1462
1463  virtual SourceRange getSourceRange() const {
1464    return SourceRange(TokenLoc);
1465  }
1466  static bool classof(const Stmt *T) {
1467    return T->getStmtClass() == GNUNullExprClass;
1468  }
1469  static bool classof(const GNUNullExpr *) { return true; }
1470
1471  // Iterators
1472  virtual child_iterator child_begin();
1473  virtual child_iterator child_end();
1474
1475  virtual void EmitImpl(llvm::Serializer& S) const;
1476  static GNUNullExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1477};
1478
1479/// OverloadExpr - Clang builtin function __builtin_overload.
1480/// This AST node provides a way to overload functions in C.
1481///
1482/// The first argument is required to be a constant expression, for the number
1483/// of arguments passed to each candidate function.
1484///
1485/// The next N arguments, where N is the value of the constant expression,
1486/// are the values to be passed as arguments.
1487///
1488/// The rest of the arguments are values of pointer to function type, which
1489/// are the candidate functions for overloading.
1490///
1491/// The result is a equivalent to a CallExpr taking N arguments to the
1492/// candidate function whose parameter types match the types of the N arguments.
1493///
1494/// example: float Z = __builtin_overload(2, X, Y, modf, mod, modl);
1495/// If X and Y are long doubles, Z will assigned the result of modl(X, Y);
1496/// If X and Y are floats, Z will be assigned the result of modf(X, Y);
1497class OverloadExpr : public Expr {
1498  // SubExprs - the list of values passed to the __builtin_overload function.
1499  // SubExpr[0] is a constant expression
1500  // SubExpr[1-N] are the parameters to pass to the matching function call
1501  // SubExpr[N-...] are the candidate functions, of type pointer to function.
1502  Stmt **SubExprs;
1503
1504  // NumExprs - the size of the SubExprs array
1505  unsigned NumExprs;
1506
1507  // The index of the matching candidate function
1508  unsigned FnIndex;
1509
1510  SourceLocation BuiltinLoc;
1511  SourceLocation RParenLoc;
1512public:
1513  OverloadExpr(Expr **args, unsigned nexprs, unsigned idx, QualType t,
1514               SourceLocation bloc, SourceLocation rploc)
1515    : Expr(OverloadExprClass, t), NumExprs(nexprs), FnIndex(idx),
1516      BuiltinLoc(bloc), RParenLoc(rploc) {
1517    SubExprs = new Stmt*[nexprs];
1518    for (unsigned i = 0; i != nexprs; ++i)
1519      SubExprs[i] = args[i];
1520  }
1521  ~OverloadExpr() {
1522    delete [] SubExprs;
1523  }
1524
1525  /// arg_begin - Return a pointer to the list of arguments that will be passed
1526  /// to the matching candidate function, skipping over the initial constant
1527  /// expression.
1528  typedef ConstExprIterator const_arg_iterator;
1529  const_arg_iterator arg_begin() const { return &SubExprs[0]+1; }
1530  const_arg_iterator arg_end(ASTContext& Ctx) const {
1531    return &SubExprs[0]+1+getNumArgs(Ctx);
1532  }
1533
1534  /// getNumArgs - Return the number of arguments to pass to the candidate
1535  /// functions.
1536  unsigned getNumArgs(ASTContext &Ctx) const {
1537    return getExpr(0)->getIntegerConstantExprValue(Ctx).getZExtValue();
1538  }
1539
1540  /// getNumSubExprs - Return the size of the SubExprs array.  This includes the
1541  /// constant expression, the actual arguments passed in, and the function
1542  /// pointers.
1543  unsigned getNumSubExprs() const { return NumExprs; }
1544
1545  /// getExpr - Return the Expr at the specified index.
1546  Expr *getExpr(unsigned Index) const {
1547    assert((Index < NumExprs) && "Arg access out of range!");
1548    return cast<Expr>(SubExprs[Index]);
1549  }
1550
1551  /// getFn - Return the matching candidate function for this OverloadExpr.
1552  Expr *getFn() const { return cast<Expr>(SubExprs[FnIndex]); }
1553
1554  virtual SourceRange getSourceRange() const {
1555    return SourceRange(BuiltinLoc, RParenLoc);
1556  }
1557  static bool classof(const Stmt *T) {
1558    return T->getStmtClass() == OverloadExprClass;
1559  }
1560  static bool classof(const OverloadExpr *) { return true; }
1561
1562  // Iterators
1563  virtual child_iterator child_begin();
1564  virtual child_iterator child_end();
1565
1566  virtual void EmitImpl(llvm::Serializer& S) const;
1567  static OverloadExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1568};
1569
1570/// VAArgExpr, used for the builtin function __builtin_va_start.
1571class VAArgExpr : public Expr {
1572  Stmt *Val;
1573  SourceLocation BuiltinLoc, RParenLoc;
1574public:
1575  VAArgExpr(SourceLocation BLoc, Expr* e, QualType t, SourceLocation RPLoc)
1576    : Expr(VAArgExprClass, t),
1577      Val(e),
1578      BuiltinLoc(BLoc),
1579      RParenLoc(RPLoc) { }
1580
1581  const Expr *getSubExpr() const { return cast<Expr>(Val); }
1582  Expr *getSubExpr() { return cast<Expr>(Val); }
1583  virtual SourceRange getSourceRange() const {
1584    return SourceRange(BuiltinLoc, RParenLoc);
1585  }
1586  static bool classof(const Stmt *T) {
1587    return T->getStmtClass() == VAArgExprClass;
1588  }
1589  static bool classof(const VAArgExpr *) { return true; }
1590
1591  // Iterators
1592  virtual child_iterator child_begin();
1593  virtual child_iterator child_end();
1594
1595  virtual void EmitImpl(llvm::Serializer& S) const;
1596  static VAArgExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1597};
1598
1599/// InitListExpr - used for struct and array initializers, such as:
1600///    struct foo x = { 1, { 2, 3 } };
1601///
1602/// Because C is somewhat loose with braces, the AST does not necessarily
1603/// directly model the C source.  Instead, the semantic analyzer aims to make
1604/// the InitListExprs match up with the type of the decl being initialized.  We
1605/// have the following exceptions:
1606///
1607///  1. Elements at the end of the list may be dropped from the initializer.
1608///     These elements are defined to be initialized to zero.  For example:
1609///         int x[20] = { 1 };
1610///  2. Initializers may have excess initializers which are to be ignored by the
1611///     compiler.  For example:
1612///         int x[1] = { 1, 2 };
1613///  3. Redundant InitListExprs may be present around scalar elements.  These
1614///     always have a single element whose type is the same as the InitListExpr.
1615///     this can only happen for Type::isScalarType() types.
1616///         int x = { 1 };  int y[2] = { {1}, {2} };
1617///
1618class InitListExpr : public Expr {
1619  std::vector<Stmt *> InitExprs;
1620  SourceLocation LBraceLoc, RBraceLoc;
1621
1622  /// HadDesignators - Return true if there were any designators in this
1623  /// init list expr.  FIXME: this should be replaced by storing the designators
1624  /// somehow and updating codegen.
1625  bool HadDesignators;
1626public:
1627  InitListExpr(SourceLocation lbraceloc, Expr **initexprs, unsigned numinits,
1628               SourceLocation rbraceloc, bool HadDesignators);
1629
1630  unsigned getNumInits() const { return InitExprs.size(); }
1631  bool hadDesignators() const { return HadDesignators; }
1632
1633  const Expr* getInit(unsigned Init) const {
1634    assert(Init < getNumInits() && "Initializer access out of range!");
1635    return cast<Expr>(InitExprs[Init]);
1636  }
1637
1638  Expr* getInit(unsigned Init) {
1639    assert(Init < getNumInits() && "Initializer access out of range!");
1640    return cast<Expr>(InitExprs[Init]);
1641  }
1642
1643  void setInit(unsigned Init, Expr *expr) {
1644    assert(Init < getNumInits() && "Initializer access out of range!");
1645    InitExprs[Init] = expr;
1646  }
1647
1648  // Dynamic removal/addition (for constructing implicit InitExpr's).
1649  void removeInit(unsigned Init) {
1650    InitExprs.erase(InitExprs.begin()+Init);
1651  }
1652  void addInit(unsigned Init, Expr *expr) {
1653    InitExprs.insert(InitExprs.begin()+Init, expr);
1654  }
1655
1656  // Explicit InitListExpr's originate from source code (and have valid source
1657  // locations). Implicit InitListExpr's are created by the semantic analyzer.
1658  bool isExplicit() {
1659    return LBraceLoc.isValid() && RBraceLoc.isValid();
1660  }
1661
1662  virtual SourceRange getSourceRange() const {
1663    return SourceRange(LBraceLoc, RBraceLoc);
1664  }
1665  static bool classof(const Stmt *T) {
1666    return T->getStmtClass() == InitListExprClass;
1667  }
1668  static bool classof(const InitListExpr *) { return true; }
1669
1670  // Iterators
1671  virtual child_iterator child_begin();
1672  virtual child_iterator child_end();
1673
1674  typedef std::vector<Stmt *>::iterator iterator;
1675  typedef std::vector<Stmt *>::reverse_iterator reverse_iterator;
1676
1677  iterator begin() { return InitExprs.begin(); }
1678  iterator end() { return InitExprs.end(); }
1679  reverse_iterator rbegin() { return InitExprs.rbegin(); }
1680  reverse_iterator rend() { return InitExprs.rend(); }
1681
1682  // Serailization.
1683  virtual void EmitImpl(llvm::Serializer& S) const;
1684  static InitListExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1685
1686private:
1687  // Used by serializer.
1688  InitListExpr() : Expr(InitListExprClass, QualType()) {}
1689};
1690
1691//===----------------------------------------------------------------------===//
1692// Clang Extensions
1693//===----------------------------------------------------------------------===//
1694
1695
1696/// ExtVectorElementExpr - This represents access to specific elements of a
1697/// vector, and may occur on the left hand side or right hand side.  For example
1698/// the following is legal:  "V.xy = V.zw" if V is a 4 element extended vector.
1699///
1700class ExtVectorElementExpr : public Expr {
1701  Stmt *Base;
1702  IdentifierInfo &Accessor;
1703  SourceLocation AccessorLoc;
1704public:
1705  ExtVectorElementExpr(QualType ty, Expr *base, IdentifierInfo &accessor,
1706                       SourceLocation loc)
1707    : Expr(ExtVectorElementExprClass, ty),
1708      Base(base), Accessor(accessor), AccessorLoc(loc) {}
1709
1710  const Expr *getBase() const { return cast<Expr>(Base); }
1711  Expr *getBase() { return cast<Expr>(Base); }
1712
1713  IdentifierInfo &getAccessor() const { return Accessor; }
1714
1715  /// getNumElements - Get the number of components being selected.
1716  unsigned getNumElements() const;
1717
1718  /// containsDuplicateElements - Return true if any element access is
1719  /// repeated.
1720  bool containsDuplicateElements() const;
1721
1722  /// getEncodedElementAccess - Encode the elements accessed into an llvm
1723  /// aggregate Constant of ConstantInt(s).
1724  void getEncodedElementAccess(llvm::SmallVectorImpl<unsigned> &Elts) const;
1725
1726  virtual SourceRange getSourceRange() const {
1727    return SourceRange(getBase()->getLocStart(), AccessorLoc);
1728  }
1729
1730  static bool classof(const Stmt *T) {
1731    return T->getStmtClass() == ExtVectorElementExprClass;
1732  }
1733  static bool classof(const ExtVectorElementExpr *) { return true; }
1734
1735  // Iterators
1736  virtual child_iterator child_begin();
1737  virtual child_iterator child_end();
1738
1739  virtual void EmitImpl(llvm::Serializer& S) const;
1740  static ExtVectorElementExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1741};
1742
1743
1744/// BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
1745/// ^{ statement-body }   or   ^(int arg1, float arg2){ statement-body }
1746class BlockExpr : public Expr {
1747protected:
1748  BlockDecl *TheBlock;
1749public:
1750  BlockExpr(BlockDecl *BD, QualType ty) : Expr(BlockExprClass, ty),
1751            TheBlock(BD) {}
1752
1753  BlockDecl *getBlockDecl() { return TheBlock; }
1754
1755  // Convenience functions for probing the underlying BlockDecl.
1756  SourceLocation getCaretLocation() const;
1757  const Stmt *getBody() const;
1758  Stmt *getBody();
1759
1760  virtual SourceRange getSourceRange() const {
1761    return SourceRange(getCaretLocation(), getBody()->getLocEnd());
1762  }
1763
1764  /// getFunctionType - Return the underlying function type for this block.
1765  const FunctionType *getFunctionType() const;
1766
1767  static bool classof(const Stmt *T) {
1768    return T->getStmtClass() == BlockExprClass;
1769  }
1770  static bool classof(const BlockExpr *) { return true; }
1771
1772  // Iterators
1773  virtual child_iterator child_begin();
1774  virtual child_iterator child_end();
1775
1776  virtual void EmitImpl(llvm::Serializer& S) const;
1777  static BlockExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1778};
1779
1780/// BlockDeclRefExpr - A reference to a declared variable, function,
1781/// enum, etc.
1782class BlockDeclRefExpr : public Expr {
1783  ValueDecl *D;
1784  SourceLocation Loc;
1785  bool IsByRef;
1786public:
1787  BlockDeclRefExpr(ValueDecl *d, QualType t, SourceLocation l, bool ByRef) :
1788       Expr(BlockDeclRefExprClass, t), D(d), Loc(l), IsByRef(ByRef) {}
1789
1790  ValueDecl *getDecl() { return D; }
1791  const ValueDecl *getDecl() const { return D; }
1792  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
1793
1794  bool isByRef() const { return IsByRef; }
1795
1796  static bool classof(const Stmt *T) {
1797    return T->getStmtClass() == BlockDeclRefExprClass;
1798  }
1799  static bool classof(const BlockDeclRefExpr *) { return true; }
1800
1801  // Iterators
1802  virtual child_iterator child_begin();
1803  virtual child_iterator child_end();
1804
1805  virtual void EmitImpl(llvm::Serializer& S) const;
1806  static BlockDeclRefExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1807};
1808
1809}  // end namespace clang
1810
1811#endif
1812