Expr.h revision 0979c805475d1ba49b5d6ef93c4d2ce6d2eab6ed
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  class CXXOperatorCallExpr;
35  class CXXMemberCallExpr;
36  class CXXAdornedMemberExpr;
37
38/// Expr - This represents one expression.  Note that Expr's are subclasses of
39/// Stmt.  This allows an expression to be transparently used any place a Stmt
40/// is required.
41///
42class Expr : public Stmt {
43  QualType TR;
44
45protected:
46  /// TypeDependent - Whether this expression is type-dependent
47  /// (C++ [temp.dep.expr]).
48  bool TypeDependent : 1;
49
50  /// ValueDependent - Whether this expression is value-dependent
51  /// (C++ [temp.dep.constexpr]).
52  bool ValueDependent : 1;
53
54  // FIXME: Eventually, this constructor should go away and we should
55  // require every subclass to provide type/value-dependence
56  // information.
57  Expr(StmtClass SC, QualType T)
58    : Stmt(SC), TypeDependent(false), ValueDependent(false) {
59    setType(T);
60  }
61
62  Expr(StmtClass SC, QualType T, bool TD, bool VD)
63    : Stmt(SC), TypeDependent(TD), ValueDependent(VD) {
64    setType(T);
65  }
66
67  /// \brief Construct an empty expression.
68  explicit Expr(StmtClass SC, EmptyShell) : Stmt(SC) { }
69
70public:
71  /// \brief Increases the reference count for this expression.
72  ///
73  /// Invoke the Retain() operation when this expression
74  /// is being shared by another owner.
75  Expr *Retain() {
76    Stmt::Retain();
77    return this;
78  }
79
80  QualType getType() const { return TR; }
81  void setType(QualType t) {
82    // In C++, the type of an expression is always adjusted so that it
83    // will not have reference type an expression will never have
84    // reference type (C++ [expr]p6). Use
85    // QualType::getNonReferenceType() to retrieve the non-reference
86    // type. Additionally, inspect Expr::isLvalue to determine whether
87    // an expression that is adjusted in this manner should be
88    // considered an lvalue.
89    assert((TR.isNull() || !TR->isReferenceType()) &&
90           "Expressions can't have reference type");
91
92    TR = t;
93  }
94
95  /// isValueDependent - Determines whether this expression is
96  /// value-dependent (C++ [temp.dep.constexpr]). For example, the
97  /// array bound of "Chars" in the following example is
98  /// value-dependent.
99  /// @code
100  /// template<int Size, char (&Chars)[Size]> struct meta_string;
101  /// @endcode
102  bool isValueDependent() const { return ValueDependent; }
103
104  /// \brief Set whether this expression is value-dependent or not.
105  void setValueDependent(bool VD) { ValueDependent = VD; }
106
107  /// isTypeDependent - Determines whether this expression is
108  /// type-dependent (C++ [temp.dep.expr]), which means that its type
109  /// could change from one template instantiation to the next. For
110  /// example, the expressions "x" and "x + y" are type-dependent in
111  /// the following code, but "y" is not type-dependent:
112  /// @code
113  /// template<typename T>
114  /// void add(T x, int y) {
115  ///   x + y;
116  /// }
117  /// @endcode
118  bool isTypeDependent() const { return TypeDependent; }
119
120  /// \brief Set whether this expression is type-dependent or not.
121  void setTypeDependent(bool TD) { TypeDependent = TD; }
122
123  /// SourceLocation tokens are not useful in isolation - they are low level
124  /// value objects created/interpreted by SourceManager. We assume AST
125  /// clients will have a pointer to the respective SourceManager.
126  virtual SourceRange getSourceRange() const = 0;
127
128  /// getExprLoc - Return the preferred location for the arrow when diagnosing
129  /// a problem with a generic expression.
130  virtual SourceLocation getExprLoc() const { return getLocStart(); }
131
132  /// isUnusedResultAWarning - Return true if this immediate expression should
133  /// be warned about if the result is unused.  If so, fill in Loc and Ranges
134  /// with location to warn on and the source range[s] to report with the
135  /// warning.
136  bool isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
137                              SourceRange &R2) const;
138
139  /// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or
140  /// incomplete type other than void. Nonarray expressions that can be lvalues:
141  ///  - name, where name must be a variable
142  ///  - e[i]
143  ///  - (e), where e must be an lvalue
144  ///  - e.name, where e must be an lvalue
145  ///  - e->name
146  ///  - *e, the type of e cannot be a function type
147  ///  - string-constant
148  ///  - reference type [C++ [expr]]
149  ///  - b ? x : y, where x and y are lvalues of suitable types [C++]
150  ///
151  enum isLvalueResult {
152    LV_Valid,
153    LV_NotObjectType,
154    LV_IncompleteVoidType,
155    LV_DuplicateVectorComponents,
156    LV_InvalidExpression,
157    LV_MemberFunction
158  };
159  isLvalueResult isLvalue(ASTContext &Ctx) const;
160
161  // Same as above, but excluding checks for non-object and void types in C
162  isLvalueResult isLvalueInternal(ASTContext &Ctx) const;
163
164  /// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
165  /// does not have an incomplete type, does not have a const-qualified type,
166  /// and if it is a structure or union, does not have any member (including,
167  /// recursively, any member or element of all contained aggregates or unions)
168  /// with a const-qualified type.
169  ///
170  /// \param Loc [in] [out] - A source location which *may* be filled
171  /// in with the location of the expression making this a
172  /// non-modifiable lvalue, if specified.
173  enum isModifiableLvalueResult {
174    MLV_Valid,
175    MLV_NotObjectType,
176    MLV_IncompleteVoidType,
177    MLV_DuplicateVectorComponents,
178    MLV_InvalidExpression,
179    MLV_LValueCast,           // Specialized form of MLV_InvalidExpression.
180    MLV_IncompleteType,
181    MLV_ConstQualified,
182    MLV_ArrayType,
183    MLV_NotBlockQualified,
184    MLV_ReadonlyProperty,
185    MLV_NoSetterProperty,
186    MLV_MemberFunction
187  };
188  isModifiableLvalueResult isModifiableLvalue(ASTContext &Ctx,
189                                              SourceLocation *Loc = 0) const;
190
191  /// \brief If this expression refers to a bit-field, retrieve the
192  /// declaration of that bit-field.
193  FieldDecl *getBitField();
194
195  const FieldDecl *getBitField() const {
196    return const_cast<Expr*>(this)->getBitField();
197  }
198
199  /// isIntegerConstantExpr - Return true if this expression is a valid integer
200  /// constant expression, and, if so, return its value in Result.  If not a
201  /// valid i-c-e, return false and fill in Loc (if specified) with the location
202  /// of the invalid expression.
203  bool isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
204                             SourceLocation *Loc = 0,
205                             bool isEvaluated = true) const;
206  bool isIntegerConstantExpr(ASTContext &Ctx, SourceLocation *Loc = 0) const {
207    llvm::APSInt X;
208    return isIntegerConstantExpr(X, Ctx, Loc);
209  }
210  /// isConstantInitializer - Returns true if this expression is a constant
211  /// initializer, which can be emitted at compile-time.
212  bool isConstantInitializer(ASTContext &Ctx) const;
213
214  /// EvalResult is a struct with detailed info about an evaluated expression.
215  struct EvalResult {
216    /// Val - This is the value the expression can be folded to.
217    APValue Val;
218
219    /// HasSideEffects - Whether the evaluated expression has side effects.
220    /// For example, (f() && 0) can be folded, but it still has side effects.
221    bool HasSideEffects;
222
223    /// Diag - If the expression is unfoldable, then Diag contains a note
224    /// diagnostic indicating why it's not foldable. DiagLoc indicates a caret
225    /// position for the error, and DiagExpr is the expression that caused
226    /// the error.
227    /// If the expression is foldable, but not an integer constant expression,
228    /// Diag contains a note diagnostic that describes why it isn't an integer
229    /// constant expression. If the expression *is* an integer constant
230    /// expression, then Diag will be zero.
231    unsigned Diag;
232    const Expr *DiagExpr;
233    SourceLocation DiagLoc;
234
235    EvalResult() : HasSideEffects(false), Diag(0), DiagExpr(0) {}
236  };
237
238  /// Evaluate - Return true if this is a constant which we can fold using
239  /// any crazy technique (that has nothing to do with language standards) that
240  /// we want to.  If this function returns true, it returns the folded constant
241  /// in Result.
242  bool Evaluate(EvalResult &Result, ASTContext &Ctx) const;
243
244  /// isEvaluatable - Call Evaluate to see if this expression can be constant
245  /// folded, but discard the result.
246  bool isEvaluatable(ASTContext &Ctx) const;
247
248  /// EvaluateAsInt - Call Evaluate and return the folded integer. This
249  /// must be called on an expression that constant folds to an integer.
250  llvm::APSInt EvaluateAsInt(ASTContext &Ctx) const;
251
252  /// EvaluateAsLValue - Evaluate an expression to see if it's a valid LValue.
253  bool EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const;
254
255  /// isNullPointerConstant - C99 6.3.2.3p3 -  Return true if this is either an
256  /// integer constant expression with the value zero, or if this is one that is
257  /// cast to void*.
258  bool isNullPointerConstant(ASTContext &Ctx) const;
259
260  /// isOBJCGCCandidate - Return true if this expression may be used in a read/
261  /// write barrier.
262  bool isOBJCGCCandidate(ASTContext &Ctx) const;
263
264  /// IgnoreParens - Ignore parentheses.  If this Expr is a ParenExpr, return
265  ///  its subexpression.  If that subexpression is also a ParenExpr,
266  ///  then this method recursively returns its subexpression, and so forth.
267  ///  Otherwise, the method returns the current Expr.
268  Expr* IgnoreParens();
269
270  /// IgnoreParenCasts - Ignore parentheses and casts.  Strip off any ParenExpr
271  /// or CastExprs, returning their operand.
272  Expr *IgnoreParenCasts();
273
274  /// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
275  /// value (including ptr->int casts of the same size).  Strip off any
276  /// ParenExpr or CastExprs, returning their operand.
277  Expr *IgnoreParenNoopCasts(ASTContext &Ctx);
278
279  const Expr* IgnoreParens() const {
280    return const_cast<Expr*>(this)->IgnoreParens();
281  }
282  const Expr *IgnoreParenCasts() const {
283    return const_cast<Expr*>(this)->IgnoreParenCasts();
284  }
285  const Expr *IgnoreParenNoopCasts(ASTContext &Ctx) const {
286    return const_cast<Expr*>(this)->IgnoreParenNoopCasts(Ctx);
287  }
288
289  static bool hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs);
290  static bool hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs);
291
292  static bool classof(const Stmt *T) {
293    return T->getStmtClass() >= firstExprConstant &&
294           T->getStmtClass() <= lastExprConstant;
295  }
296  static bool classof(const Expr *) { return true; }
297};
298
299
300//===----------------------------------------------------------------------===//
301// Primary Expressions.
302//===----------------------------------------------------------------------===//
303
304/// DeclRefExpr - [C99 6.5.1p2] - A reference to a declared variable, function,
305/// enum, etc.
306class DeclRefExpr : public Expr {
307  NamedDecl *D;
308  SourceLocation Loc;
309
310protected:
311  // FIXME: Eventually, this constructor will go away and all subclasses
312  // will have to provide the type- and value-dependent flags.
313  DeclRefExpr(StmtClass SC, NamedDecl *d, QualType t, SourceLocation l) :
314    Expr(SC, t), D(d), Loc(l) {}
315
316  DeclRefExpr(StmtClass SC, NamedDecl *d, QualType t, SourceLocation l, bool TD,
317              bool VD) :
318    Expr(SC, t, TD, VD), D(d), Loc(l) {}
319
320public:
321  // FIXME: Eventually, this constructor will go away and all clients
322  // will have to provide the type- and value-dependent flags.
323  DeclRefExpr(NamedDecl *d, QualType t, SourceLocation l) :
324    Expr(DeclRefExprClass, t), D(d), Loc(l) {}
325
326  DeclRefExpr(NamedDecl *d, QualType t, SourceLocation l, bool TD, bool VD) :
327    Expr(DeclRefExprClass, t, TD, VD), D(d), Loc(l) {}
328
329  /// \brief Construct an empty declaration reference expression.
330  explicit DeclRefExpr(EmptyShell Empty)
331    : Expr(DeclRefExprClass, Empty) { }
332
333  NamedDecl *getDecl() { return D; }
334  const NamedDecl *getDecl() const { return D; }
335  void setDecl(NamedDecl *NewD) { D = NewD; }
336
337  SourceLocation getLocation() const { return Loc; }
338  void setLocation(SourceLocation L) { Loc = L; }
339  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
340
341  static bool classof(const Stmt *T) {
342    return T->getStmtClass() == DeclRefExprClass ||
343           T->getStmtClass() == CXXConditionDeclExprClass ||
344           T->getStmtClass() == QualifiedDeclRefExprClass;
345  }
346  static bool classof(const DeclRefExpr *) { return true; }
347
348  // Iterators
349  virtual child_iterator child_begin();
350  virtual child_iterator child_end();
351};
352
353/// PredefinedExpr - [C99 6.4.2.2] - A predefined identifier such as __func__.
354class PredefinedExpr : public Expr {
355public:
356  enum IdentType {
357    Func,
358    Function,
359    PrettyFunction
360  };
361
362private:
363  SourceLocation Loc;
364  IdentType Type;
365public:
366  PredefinedExpr(SourceLocation l, QualType type, IdentType IT)
367    : Expr(PredefinedExprClass, type), Loc(l), Type(IT) {}
368
369  /// \brief Construct an empty predefined expression.
370  explicit PredefinedExpr(EmptyShell Empty)
371    : Expr(PredefinedExprClass, Empty) { }
372
373  IdentType getIdentType() const { return Type; }
374  void setIdentType(IdentType IT) { Type = IT; }
375
376  SourceLocation getLocation() const { return Loc; }
377  void setLocation(SourceLocation L) { Loc = L; }
378
379  // FIXME: The logic for computing the value of a predefined expr should go
380  // into a method here that takes the inner-most code decl (a block, function
381  // or objc method) that the expr lives in.  This would allow sema and codegen
382  // to be consistent for things like sizeof(__func__) etc.
383
384  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
385
386  static bool classof(const Stmt *T) {
387    return T->getStmtClass() == PredefinedExprClass;
388  }
389  static bool classof(const PredefinedExpr *) { return true; }
390
391  // Iterators
392  virtual child_iterator child_begin();
393  virtual child_iterator child_end();
394};
395
396class IntegerLiteral : public Expr {
397  llvm::APInt Value;
398  SourceLocation Loc;
399public:
400  // type should be IntTy, LongTy, LongLongTy, UnsignedIntTy, UnsignedLongTy,
401  // or UnsignedLongLongTy
402  IntegerLiteral(const llvm::APInt &V, QualType type, SourceLocation l)
403    : Expr(IntegerLiteralClass, type), Value(V), Loc(l) {
404    assert(type->isIntegerType() && "Illegal type in IntegerLiteral");
405  }
406
407  /// \brief Construct an empty integer literal.
408  explicit IntegerLiteral(EmptyShell Empty)
409    : Expr(IntegerLiteralClass, Empty) { }
410
411  const llvm::APInt &getValue() const { return Value; }
412  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
413
414  /// \brief Retrieve the location of the literal.
415  SourceLocation getLocation() const { return Loc; }
416
417  void setValue(const llvm::APInt &Val) { Value = Val; }
418  void setLocation(SourceLocation Location) { Loc = Location; }
419
420  static bool classof(const Stmt *T) {
421    return T->getStmtClass() == IntegerLiteralClass;
422  }
423  static bool classof(const IntegerLiteral *) { return true; }
424
425  // Iterators
426  virtual child_iterator child_begin();
427  virtual child_iterator child_end();
428};
429
430class CharacterLiteral : public Expr {
431  unsigned Value;
432  SourceLocation Loc;
433  bool IsWide;
434public:
435  // type should be IntTy
436  CharacterLiteral(unsigned value, bool iswide, QualType type, SourceLocation l)
437    : Expr(CharacterLiteralClass, type), Value(value), Loc(l), IsWide(iswide) {
438  }
439
440  /// \brief Construct an empty character literal.
441  CharacterLiteral(EmptyShell Empty) : Expr(CharacterLiteralClass, Empty) { }
442
443  SourceLocation getLocation() const { return Loc; }
444  bool isWide() const { return IsWide; }
445
446  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
447
448  unsigned getValue() const { return Value; }
449
450  void setLocation(SourceLocation Location) { Loc = Location; }
451  void setWide(bool W) { IsWide = W; }
452  void setValue(unsigned Val) { Value = Val; }
453
454  static bool classof(const Stmt *T) {
455    return T->getStmtClass() == CharacterLiteralClass;
456  }
457  static bool classof(const CharacterLiteral *) { return true; }
458
459  // Iterators
460  virtual child_iterator child_begin();
461  virtual child_iterator child_end();
462};
463
464class FloatingLiteral : public Expr {
465  llvm::APFloat Value;
466  bool IsExact : 1;
467  SourceLocation Loc;
468public:
469  FloatingLiteral(const llvm::APFloat &V, bool isexact,
470                  QualType Type, SourceLocation L)
471    : Expr(FloatingLiteralClass, Type), Value(V), IsExact(isexact), Loc(L) {}
472
473  /// \brief Construct an empty floating-point literal.
474  explicit FloatingLiteral(EmptyShell Empty)
475    : Expr(FloatingLiteralClass, Empty), Value(0.0) { }
476
477  const llvm::APFloat &getValue() const { return Value; }
478  void setValue(const llvm::APFloat &Val) { Value = Val; }
479
480  bool isExact() const { return IsExact; }
481  void setExact(bool E) { IsExact = E; }
482
483  /// getValueAsApproximateDouble - This returns the value as an inaccurate
484  /// double.  Note that this may cause loss of precision, but is useful for
485  /// debugging dumps, etc.
486  double getValueAsApproximateDouble() const;
487
488  SourceLocation getLocation() const { return Loc; }
489  void setLocation(SourceLocation L) { Loc = L; }
490
491  // FIXME: The logic for computing the value of a predefined expr should go
492  // into a method here that takes the inner-most code decl (a block, function
493  // or objc method) that the expr lives in.  This would allow sema and codegen
494  // to be consistent for things like sizeof(__func__) etc.
495
496  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
497
498  static bool classof(const Stmt *T) {
499    return T->getStmtClass() == FloatingLiteralClass;
500  }
501  static bool classof(const FloatingLiteral *) { return true; }
502
503  // Iterators
504  virtual child_iterator child_begin();
505  virtual child_iterator child_end();
506};
507
508/// ImaginaryLiteral - We support imaginary integer and floating point literals,
509/// like "1.0i".  We represent these as a wrapper around FloatingLiteral and
510/// IntegerLiteral classes.  Instances of this class always have a Complex type
511/// whose element type matches the subexpression.
512///
513class ImaginaryLiteral : public Expr {
514  Stmt *Val;
515public:
516  ImaginaryLiteral(Expr *val, QualType Ty)
517    : Expr(ImaginaryLiteralClass, Ty), Val(val) {}
518
519  /// \brief Build an empty imaginary literal.
520  explicit ImaginaryLiteral(EmptyShell Empty)
521    : Expr(ImaginaryLiteralClass, Empty) { }
522
523  const Expr *getSubExpr() const { return cast<Expr>(Val); }
524  Expr *getSubExpr() { return cast<Expr>(Val); }
525  void setSubExpr(Expr *E) { Val = E; }
526
527  virtual SourceRange getSourceRange() const { return Val->getSourceRange(); }
528  static bool classof(const Stmt *T) {
529    return T->getStmtClass() == ImaginaryLiteralClass;
530  }
531  static bool classof(const ImaginaryLiteral *) { return true; }
532
533  // Iterators
534  virtual child_iterator child_begin();
535  virtual child_iterator child_end();
536};
537
538/// StringLiteral - This represents a string literal expression, e.g. "foo"
539/// or L"bar" (wide strings).  The actual string is returned by getStrData()
540/// is NOT null-terminated, and the length of the string is determined by
541/// calling getByteLength().  The C type for a string is always a
542/// ConstantArrayType.  In C++, the char type is const qualified, in C it is
543/// not.
544///
545/// Note that strings in C can be formed by concatenation of multiple string
546/// literal pptokens in translation phase #6.  This keeps track of the locations
547/// of each of these pieces.
548///
549/// Strings in C can also be truncated and extended by assigning into arrays,
550/// e.g. with constructs like:
551///   char X[2] = "foobar";
552/// In this case, getByteLength() will return 6, but the string literal will
553/// have type "char[2]".
554class StringLiteral : public Expr {
555  const char *StrData;
556  unsigned ByteLength;
557  bool IsWide;
558  unsigned NumConcatenated;
559  SourceLocation TokLocs[1];
560
561  StringLiteral(QualType Ty) : Expr(StringLiteralClass, Ty) {}
562
563protected:
564  virtual void DoDestroy(ASTContext &C);
565
566public:
567  /// This is the "fully general" constructor that allows representation of
568  /// strings formed from multiple concatenated tokens.
569  static StringLiteral *Create(ASTContext &C, const char *StrData,
570                               unsigned ByteLength, bool Wide, QualType Ty,
571                               const SourceLocation *Loc, unsigned NumStrs);
572
573  /// Simple constructor for string literals made from one token.
574  static StringLiteral *Create(ASTContext &C, const char *StrData,
575                               unsigned ByteLength,
576                               bool Wide, QualType Ty, SourceLocation Loc) {
577    return Create(C, StrData, ByteLength, Wide, Ty, &Loc, 1);
578  }
579
580  /// \brief Construct an empty string literal.
581  static StringLiteral *CreateEmpty(ASTContext &C, unsigned NumStrs);
582
583  const char *getStrData() const { return StrData; }
584  unsigned getByteLength() const { return ByteLength; }
585
586  /// \brief Sets the string data to the given string data.
587  void setStrData(ASTContext &C, const char *Str, unsigned Len);
588
589  bool isWide() const { return IsWide; }
590  void setWide(bool W) { IsWide = W; }
591
592  bool containsNonAsciiOrNull() const {
593    for (unsigned i = 0; i < getByteLength(); ++i)
594      if (!isascii(getStrData()[i]) || !getStrData()[i])
595        return true;
596    return false;
597  }
598  /// getNumConcatenated - Get the number of string literal tokens that were
599  /// concatenated in translation phase #6 to form this string literal.
600  unsigned getNumConcatenated() const { return NumConcatenated; }
601
602  SourceLocation getStrTokenLoc(unsigned TokNum) const {
603    assert(TokNum < NumConcatenated && "Invalid tok number");
604    return TokLocs[TokNum];
605  }
606  void setStrTokenLoc(unsigned TokNum, SourceLocation L) {
607    assert(TokNum < NumConcatenated && "Invalid tok number");
608    TokLocs[TokNum] = L;
609  }
610
611  typedef const SourceLocation *tokloc_iterator;
612  tokloc_iterator tokloc_begin() const { return TokLocs; }
613  tokloc_iterator tokloc_end() const { return TokLocs+NumConcatenated; }
614
615  virtual SourceRange getSourceRange() const {
616    return SourceRange(TokLocs[0], TokLocs[NumConcatenated-1]);
617  }
618  static bool classof(const Stmt *T) {
619    return T->getStmtClass() == StringLiteralClass;
620  }
621  static bool classof(const StringLiteral *) { return true; }
622
623  // Iterators
624  virtual child_iterator child_begin();
625  virtual child_iterator child_end();
626};
627
628/// ParenExpr - This represents a parethesized expression, e.g. "(1)".  This
629/// AST node is only formed if full location information is requested.
630class ParenExpr : public Expr {
631  SourceLocation L, R;
632  Stmt *Val;
633public:
634  ParenExpr(SourceLocation l, SourceLocation r, Expr *val)
635    : Expr(ParenExprClass, val->getType(),
636           val->isTypeDependent(), val->isValueDependent()),
637      L(l), R(r), Val(val) {}
638
639  /// \brief Construct an empty parenthesized expression.
640  explicit ParenExpr(EmptyShell Empty)
641    : Expr(ParenExprClass, Empty) { }
642
643  const Expr *getSubExpr() const { return cast<Expr>(Val); }
644  Expr *getSubExpr() { return cast<Expr>(Val); }
645  void setSubExpr(Expr *E) { Val = E; }
646
647  virtual SourceRange getSourceRange() const { return SourceRange(L, R); }
648
649  /// \brief Get the location of the left parentheses '('.
650  SourceLocation getLParen() const { return L; }
651  void setLParen(SourceLocation Loc) { L = Loc; }
652
653  /// \brief Get the location of the right parentheses ')'.
654  SourceLocation getRParen() const { return R; }
655  void setRParen(SourceLocation Loc) { R = Loc; }
656
657  static bool classof(const Stmt *T) {
658    return T->getStmtClass() == ParenExprClass;
659  }
660  static bool classof(const ParenExpr *) { return true; }
661
662  // Iterators
663  virtual child_iterator child_begin();
664  virtual child_iterator child_end();
665};
666
667
668/// UnaryOperator - This represents the unary-expression's (except sizeof and
669/// alignof), the postinc/postdec operators from postfix-expression, and various
670/// extensions.
671///
672/// Notes on various nodes:
673///
674/// Real/Imag - These return the real/imag part of a complex operand.  If
675///   applied to a non-complex value, the former returns its operand and the
676///   later returns zero in the type of the operand.
677///
678/// __builtin_offsetof(type, a.b[10]) is represented as a unary operator whose
679///   subexpression is a compound literal with the various MemberExpr and
680///   ArraySubscriptExpr's applied to it.
681///
682class UnaryOperator : public Expr {
683public:
684  // Note that additions to this should also update the StmtVisitor class.
685  enum Opcode {
686    PostInc, PostDec, // [C99 6.5.2.4] Postfix increment and decrement operators
687    PreInc, PreDec,   // [C99 6.5.3.1] Prefix increment and decrement operators.
688    AddrOf, Deref,    // [C99 6.5.3.2] Address and indirection operators.
689    Plus, Minus,      // [C99 6.5.3.3] Unary arithmetic operators.
690    Not, LNot,        // [C99 6.5.3.3] Unary arithmetic operators.
691    Real, Imag,       // "__real expr"/"__imag expr" Extension.
692    Extension,        // __extension__ marker.
693    OffsetOf          // __builtin_offsetof
694  };
695private:
696  Stmt *Val;
697  Opcode Opc;
698  SourceLocation Loc;
699public:
700
701  UnaryOperator(Expr *input, Opcode opc, QualType type, SourceLocation l)
702    : Expr(UnaryOperatorClass, type,
703           input->isTypeDependent() && opc != OffsetOf,
704           input->isValueDependent()),
705      Val(input), Opc(opc), Loc(l) {}
706
707  /// \brief Build an empty unary operator.
708  explicit UnaryOperator(EmptyShell Empty)
709    : Expr(UnaryOperatorClass, Empty), Opc(AddrOf) { }
710
711  Opcode getOpcode() const { return Opc; }
712  void setOpcode(Opcode O) { Opc = O; }
713
714  Expr *getSubExpr() const { return cast<Expr>(Val); }
715  void setSubExpr(Expr *E) { Val = E; }
716
717  /// getOperatorLoc - Return the location of the operator.
718  SourceLocation getOperatorLoc() const { return Loc; }
719  void setOperatorLoc(SourceLocation L) { Loc = L; }
720
721  /// isPostfix - Return true if this is a postfix operation, like x++.
722  static bool isPostfix(Opcode Op) {
723    return Op == PostInc || Op == PostDec;
724  }
725
726  /// isPostfix - Return true if this is a prefix operation, like --x.
727  static bool isPrefix(Opcode Op) {
728    return Op == PreInc || Op == PreDec;
729  }
730
731  bool isPrefix() const { return isPrefix(Opc); }
732  bool isPostfix() const { return isPostfix(Opc); }
733  bool isIncrementOp() const {return Opc==PreInc || Opc==PostInc; }
734  bool isIncrementDecrementOp() const { return Opc>=PostInc && Opc<=PreDec; }
735  bool isOffsetOfOp() const { return Opc == OffsetOf; }
736  static bool isArithmeticOp(Opcode Op) { return Op >= Plus && Op <= LNot; }
737  bool isArithmeticOp() const { return isArithmeticOp(Opc); }
738
739  /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
740  /// corresponds to, e.g. "sizeof" or "[pre]++"
741  static const char *getOpcodeStr(Opcode Op);
742
743  /// \brief Retrieve the unary opcode that corresponds to the given
744  /// overloaded operator.
745  static Opcode getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix);
746
747  /// \brief Retrieve the overloaded operator kind that corresponds to
748  /// the given unary opcode.
749  static OverloadedOperatorKind getOverloadedOperator(Opcode Opc);
750
751  virtual SourceRange getSourceRange() const {
752    if (isPostfix())
753      return SourceRange(Val->getLocStart(), Loc);
754    else
755      return SourceRange(Loc, Val->getLocEnd());
756  }
757  virtual SourceLocation getExprLoc() const { return Loc; }
758
759  static bool classof(const Stmt *T) {
760    return T->getStmtClass() == UnaryOperatorClass;
761  }
762  static bool classof(const UnaryOperator *) { return true; }
763
764  // Iterators
765  virtual child_iterator child_begin();
766  virtual child_iterator child_end();
767};
768
769/// SizeOfAlignOfExpr - [C99 6.5.3.4] - This is for sizeof/alignof, both of
770/// types and expressions.
771class SizeOfAlignOfExpr : public Expr {
772  bool isSizeof : 1;  // true if sizeof, false if alignof.
773  bool isType : 1;    // true if operand is a type, false if an expression
774  union {
775    void *Ty;
776    Stmt *Ex;
777  } Argument;
778  SourceLocation OpLoc, RParenLoc;
779
780protected:
781  virtual void DoDestroy(ASTContext& C);
782
783public:
784  SizeOfAlignOfExpr(bool issizeof, QualType T,
785                    QualType resultType, SourceLocation op,
786                    SourceLocation rp) :
787      Expr(SizeOfAlignOfExprClass, resultType,
788           false, // Never type-dependent (C++ [temp.dep.expr]p3).
789           // Value-dependent if the argument is type-dependent.
790           T->isDependentType()),
791      isSizeof(issizeof), isType(true), OpLoc(op), RParenLoc(rp) {
792    Argument.Ty = T.getAsOpaquePtr();
793  }
794
795  SizeOfAlignOfExpr(bool issizeof, Expr *E,
796                    QualType resultType, SourceLocation op,
797                    SourceLocation rp) :
798      Expr(SizeOfAlignOfExprClass, resultType,
799           false, // Never type-dependent (C++ [temp.dep.expr]p3).
800           // Value-dependent if the argument is type-dependent.
801           E->isTypeDependent()),
802      isSizeof(issizeof), isType(false), OpLoc(op), RParenLoc(rp) {
803    Argument.Ex = E;
804  }
805
806  /// \brief Construct an empty sizeof/alignof expression.
807  explicit SizeOfAlignOfExpr(EmptyShell Empty)
808    : Expr(SizeOfAlignOfExprClass, Empty) { }
809
810  bool isSizeOf() const { return isSizeof; }
811  void setSizeof(bool S) { isSizeof = S; }
812
813  bool isArgumentType() const { return isType; }
814  QualType getArgumentType() const {
815    assert(isArgumentType() && "calling getArgumentType() when arg is expr");
816    return QualType::getFromOpaquePtr(Argument.Ty);
817  }
818  Expr *getArgumentExpr() {
819    assert(!isArgumentType() && "calling getArgumentExpr() when arg is type");
820    return static_cast<Expr*>(Argument.Ex);
821  }
822  const Expr *getArgumentExpr() const {
823    return const_cast<SizeOfAlignOfExpr*>(this)->getArgumentExpr();
824  }
825
826  void setArgument(Expr *E) { Argument.Ex = E; isType = false; }
827  void setArgument(QualType T) {
828    Argument.Ty = T.getAsOpaquePtr();
829    isType = true;
830  }
831
832  /// Gets the argument type, or the type of the argument expression, whichever
833  /// is appropriate.
834  QualType getTypeOfArgument() const {
835    return isArgumentType() ? getArgumentType() : getArgumentExpr()->getType();
836  }
837
838  SourceLocation getOperatorLoc() const { return OpLoc; }
839  void setOperatorLoc(SourceLocation L) { OpLoc = L; }
840
841  SourceLocation getRParenLoc() const { return RParenLoc; }
842  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
843
844  virtual SourceRange getSourceRange() const {
845    return SourceRange(OpLoc, RParenLoc);
846  }
847
848  static bool classof(const Stmt *T) {
849    return T->getStmtClass() == SizeOfAlignOfExprClass;
850  }
851  static bool classof(const SizeOfAlignOfExpr *) { return true; }
852
853  // Iterators
854  virtual child_iterator child_begin();
855  virtual child_iterator child_end();
856};
857
858//===----------------------------------------------------------------------===//
859// Postfix Operators.
860//===----------------------------------------------------------------------===//
861
862/// ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting.
863class ArraySubscriptExpr : public Expr {
864  enum { LHS, RHS, END_EXPR=2 };
865  Stmt* SubExprs[END_EXPR];
866  SourceLocation RBracketLoc;
867public:
868  ArraySubscriptExpr(Expr *lhs, Expr *rhs, QualType t,
869                     SourceLocation rbracketloc)
870  : Expr(ArraySubscriptExprClass, t,
871         lhs->isTypeDependent() || rhs->isTypeDependent(),
872         lhs->isValueDependent() || rhs->isValueDependent()),
873    RBracketLoc(rbracketloc) {
874    SubExprs[LHS] = lhs;
875    SubExprs[RHS] = rhs;
876  }
877
878  /// \brief Create an empty array subscript expression.
879  explicit ArraySubscriptExpr(EmptyShell Shell)
880    : Expr(ArraySubscriptExprClass, Shell) { }
881
882  /// An array access can be written A[4] or 4[A] (both are equivalent).
883  /// - getBase() and getIdx() always present the normalized view: A[4].
884  ///    In this case getBase() returns "A" and getIdx() returns "4".
885  /// - getLHS() and getRHS() present the syntactic view. e.g. for
886  ///    4[A] getLHS() returns "4".
887  /// Note: Because vector element access is also written A[4] we must
888  /// predicate the format conversion in getBase and getIdx only on the
889  /// the type of the RHS, as it is possible for the LHS to be a vector of
890  /// integer type
891  Expr *getLHS() { return cast<Expr>(SubExprs[LHS]); }
892  const Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); }
893  void setLHS(Expr *E) { SubExprs[LHS] = E; }
894
895  Expr *getRHS() { return cast<Expr>(SubExprs[RHS]); }
896  const Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); }
897  void setRHS(Expr *E) { SubExprs[RHS] = E; }
898
899  Expr *getBase() {
900    return cast<Expr>(getRHS()->getType()->isIntegerType() ? getLHS():getRHS());
901  }
902
903  const Expr *getBase() const {
904    return cast<Expr>(getRHS()->getType()->isIntegerType() ? getLHS():getRHS());
905  }
906
907  Expr *getIdx() {
908    return cast<Expr>(getRHS()->getType()->isIntegerType() ? getRHS():getLHS());
909  }
910
911  const Expr *getIdx() const {
912    return cast<Expr>(getRHS()->getType()->isIntegerType() ? getRHS():getLHS());
913  }
914
915  virtual SourceRange getSourceRange() const {
916    return SourceRange(getLHS()->getLocStart(), RBracketLoc);
917  }
918
919  SourceLocation getRBracketLoc() const { return RBracketLoc; }
920  void setRBracketLoc(SourceLocation L) { RBracketLoc = L; }
921
922  virtual SourceLocation getExprLoc() const { return getBase()->getExprLoc(); }
923
924  static bool classof(const Stmt *T) {
925    return T->getStmtClass() == ArraySubscriptExprClass;
926  }
927  static bool classof(const ArraySubscriptExpr *) { return true; }
928
929  // Iterators
930  virtual child_iterator child_begin();
931  virtual child_iterator child_end();
932};
933
934
935/// CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
936/// CallExpr itself represents a normal function call, e.g., "f(x, 2)",
937/// while its subclasses may represent alternative syntax that (semantically)
938/// results in a function call. For example, CXXOperatorCallExpr is
939/// a subclass for overloaded operator calls that use operator syntax, e.g.,
940/// "str1 + str2" to resolve to a function call.
941class CallExpr : public Expr {
942  enum { FN=0, ARGS_START=1 };
943  Stmt **SubExprs;
944  unsigned NumArgs;
945  SourceLocation RParenLoc;
946
947protected:
948  // This version of the constructor is for derived classes.
949  CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args, unsigned numargs,
950           QualType t, SourceLocation rparenloc);
951
952  virtual void DoDestroy(ASTContext& C);
953
954public:
955  CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs, QualType t,
956           SourceLocation rparenloc);
957
958  /// \brief Build an empty call expression.
959  CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty);
960
961  ~CallExpr() {}
962
963  const Expr *getCallee() const { return cast<Expr>(SubExprs[FN]); }
964  Expr *getCallee() { return cast<Expr>(SubExprs[FN]); }
965  void setCallee(Expr *F) { SubExprs[FN] = F; }
966
967  /// \brief If the callee is a FunctionDecl, return it. Otherwise return 0.
968  FunctionDecl *getDirectCallee();
969
970  /// getNumArgs - Return the number of actual arguments to this call.
971  ///
972  unsigned getNumArgs() const { return NumArgs; }
973
974  /// getArg - Return the specified argument.
975  Expr *getArg(unsigned Arg) {
976    assert(Arg < NumArgs && "Arg access out of range!");
977    return cast<Expr>(SubExprs[Arg+ARGS_START]);
978  }
979  const Expr *getArg(unsigned Arg) const {
980    assert(Arg < NumArgs && "Arg access out of range!");
981    return cast<Expr>(SubExprs[Arg+ARGS_START]);
982  }
983
984  /// setArg - Set the specified argument.
985  void setArg(unsigned Arg, Expr *ArgExpr) {
986    assert(Arg < NumArgs && "Arg access out of range!");
987    SubExprs[Arg+ARGS_START] = ArgExpr;
988  }
989
990  /// setNumArgs - This changes the number of arguments present in this call.
991  /// Any orphaned expressions are deleted by this, and any new operands are set
992  /// to null.
993  void setNumArgs(ASTContext& C, unsigned NumArgs);
994
995  typedef ExprIterator arg_iterator;
996  typedef ConstExprIterator const_arg_iterator;
997
998  arg_iterator arg_begin() { return SubExprs+ARGS_START; }
999  arg_iterator arg_end() { return SubExprs+ARGS_START+getNumArgs(); }
1000  const_arg_iterator arg_begin() const { return SubExprs+ARGS_START; }
1001  const_arg_iterator arg_end() const { return SubExprs+ARGS_START+getNumArgs();}
1002
1003  /// getNumCommas - Return the number of commas that must have been present in
1004  /// this function call.
1005  unsigned getNumCommas() const { return NumArgs ? NumArgs - 1 : 0; }
1006
1007  /// isBuiltinCall - If this is a call to a builtin, return the builtin ID.  If
1008  /// not, return 0.
1009  unsigned isBuiltinCall(ASTContext &Context) const;
1010
1011  /// getCallReturnType - Get the return type of the call expr. This is not
1012  /// always the type of the expr itself, if the return type is a reference
1013  /// type.
1014  QualType getCallReturnType() const;
1015
1016  SourceLocation getRParenLoc() const { return RParenLoc; }
1017  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1018
1019  virtual SourceRange getSourceRange() const {
1020    return SourceRange(getCallee()->getLocStart(), RParenLoc);
1021  }
1022
1023  static bool classof(const Stmt *T) {
1024    return T->getStmtClass() == CallExprClass ||
1025           T->getStmtClass() == CXXOperatorCallExprClass ||
1026           T->getStmtClass() == CXXMemberCallExprClass;
1027  }
1028  static bool classof(const CallExpr *) { return true; }
1029  static bool classof(const CXXOperatorCallExpr *) { return true; }
1030  static bool classof(const CXXMemberCallExpr *) { return true; }
1031
1032  // Iterators
1033  virtual child_iterator child_begin();
1034  virtual child_iterator child_end();
1035};
1036
1037/// MemberExpr - [C99 6.5.2.3] Structure and Union Members.  X->F and X.F.
1038///
1039class MemberExpr : public Expr {
1040  /// Base - the expression for the base pointer or structure references.  In
1041  /// X.F, this is "X".
1042  Stmt *Base;
1043
1044  /// MemberDecl - This is the decl being referenced by the field/member name.
1045  /// In X.F, this is the decl referenced by F.
1046  NamedDecl *MemberDecl;
1047
1048  /// MemberLoc - This is the location of the member name.
1049  SourceLocation MemberLoc;
1050
1051  /// IsArrow - True if this is "X->F", false if this is "X.F".
1052  bool IsArrow;
1053
1054protected:
1055  MemberExpr(StmtClass SC, Expr *base, bool isarrow, NamedDecl *memberdecl,
1056             SourceLocation l, QualType ty)
1057    : Expr(SC, ty,
1058           base->isTypeDependent(), base->isValueDependent()),
1059  Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow) {}
1060
1061public:
1062  MemberExpr(Expr *base, bool isarrow, NamedDecl *memberdecl, SourceLocation l,
1063             QualType ty)
1064    : Expr(MemberExprClass, ty,
1065           base->isTypeDependent(), base->isValueDependent()),
1066      Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow) {}
1067
1068  /// \brief Build an empty member reference expression.
1069  explicit MemberExpr(EmptyShell Empty) : Expr(MemberExprClass, Empty) { }
1070
1071  void setBase(Expr *E) { Base = E; }
1072  Expr *getBase() const { return cast<Expr>(Base); }
1073
1074  /// \brief Retrieve the member declaration to which this expression refers.
1075  ///
1076  /// The returned declaration will either be a FieldDecl or (in C++)
1077  /// a CXXMethodDecl.
1078  NamedDecl *getMemberDecl() const { return MemberDecl; }
1079  void setMemberDecl(NamedDecl *D) { MemberDecl = D; }
1080
1081  /// \brief Determines whether this adorned member expression actually had
1082  /// a C++ nested-name-specifier prior to the name of the member, e.g.,
1083  /// x->Base::foo.
1084  bool hasQualifier() const;
1085
1086  bool isArrow() const { return IsArrow; }
1087  void setArrow(bool A) { IsArrow = A; }
1088
1089  /// getMemberLoc - Return the location of the "member", in X->F, it is the
1090  /// location of 'F'.
1091  SourceLocation getMemberLoc() const { return MemberLoc; }
1092  void setMemberLoc(SourceLocation L) { MemberLoc = L; }
1093
1094  virtual SourceRange getSourceRange() const {
1095    // If we have an implicit base (like a C++ implicit this),
1096    // make sure not to return its location
1097    SourceLocation BaseLoc = getBase()->getLocStart();
1098    if (BaseLoc.isInvalid())
1099      return SourceRange(MemberLoc, MemberLoc);
1100    return SourceRange(BaseLoc, MemberLoc);
1101  }
1102
1103  virtual SourceLocation getExprLoc() const { return MemberLoc; }
1104
1105  static bool classof(const Stmt *T) {
1106    return T->getStmtClass() == MemberExprClass ||
1107      T->getStmtClass() == CXXAdornedMemberExprClass;
1108  }
1109  static bool classof(const MemberExpr *) { return true; }
1110  static bool classof(const CXXAdornedMemberExpr *) { return true; }
1111
1112  // Iterators
1113  virtual child_iterator child_begin();
1114  virtual child_iterator child_end();
1115};
1116
1117/// CompoundLiteralExpr - [C99 6.5.2.5]
1118///
1119class CompoundLiteralExpr : public Expr {
1120  /// LParenLoc - If non-null, this is the location of the left paren in a
1121  /// compound literal like "(int){4}".  This can be null if this is a
1122  /// synthesized compound expression.
1123  SourceLocation LParenLoc;
1124  Stmt *Init;
1125  bool FileScope;
1126public:
1127  CompoundLiteralExpr(SourceLocation lparenloc, QualType ty, Expr *init,
1128                      bool fileScope)
1129    : Expr(CompoundLiteralExprClass, ty), LParenLoc(lparenloc), Init(init),
1130      FileScope(fileScope) {}
1131
1132  /// \brief Construct an empty compound literal.
1133  explicit CompoundLiteralExpr(EmptyShell Empty)
1134    : Expr(CompoundLiteralExprClass, Empty) { }
1135
1136  const Expr *getInitializer() const { return cast<Expr>(Init); }
1137  Expr *getInitializer() { return cast<Expr>(Init); }
1138  void setInitializer(Expr *E) { Init = E; }
1139
1140  bool isFileScope() const { return FileScope; }
1141  void setFileScope(bool FS) { FileScope = FS; }
1142
1143  SourceLocation getLParenLoc() const { return LParenLoc; }
1144  void setLParenLoc(SourceLocation L) { LParenLoc = L; }
1145
1146  virtual SourceRange getSourceRange() const {
1147    // FIXME: Init should never be null.
1148    if (!Init)
1149      return SourceRange();
1150    if (LParenLoc.isInvalid())
1151      return Init->getSourceRange();
1152    return SourceRange(LParenLoc, Init->getLocEnd());
1153  }
1154
1155  static bool classof(const Stmt *T) {
1156    return T->getStmtClass() == CompoundLiteralExprClass;
1157  }
1158  static bool classof(const CompoundLiteralExpr *) { return true; }
1159
1160  // Iterators
1161  virtual child_iterator child_begin();
1162  virtual child_iterator child_end();
1163};
1164
1165/// CastExpr - Base class for type casts, including both implicit
1166/// casts (ImplicitCastExpr) and explicit casts that have some
1167/// representation in the source code (ExplicitCastExpr's derived
1168/// classes).
1169class CastExpr : public Expr {
1170public:
1171  /// CastKind - the kind of cast this represents.
1172  enum CastKind {
1173    /// CK_Unknown - Unknown cast kind.
1174    /// FIXME: The goal is to get rid of this and make all casts have a
1175    /// kind so that the AST client doesn't have to try to figure out what's
1176    /// going on.
1177    CK_Unknown,
1178
1179    /// CK_BitCast - Used for reinterpret_cast.
1180    CK_BitCast,
1181
1182    /// CK_NoOp - Used for const_cast.
1183    CK_NoOp,
1184
1185    /// CK_DerivedToBase - Derived to base class casts.
1186    CK_DerivedToBase,
1187
1188    /// CK_Dynamic - Dynamic cast.
1189    CK_Dynamic,
1190
1191    /// CK_ToUnion - Cast to union (GCC extension).
1192    CK_ToUnion,
1193
1194    /// CK_ArrayToPointerDecay - Array to pointer decay.
1195    CK_ArrayToPointerDecay,
1196
1197    /// CK_NullToMemberPointer - Null pointer to member pointer.
1198    CK_NullToMemberPointer,
1199
1200    /// CK_BaseToDerivedMemberPointer - Member pointer in base class to
1201    /// member pointer in derived class.
1202    CK_BaseToDerivedMemberPointer,
1203
1204    /// CK_UserDefinedConversion - Conversion using a user defined type
1205    /// conversion function.
1206    CK_UserDefinedConversion,
1207
1208    /// CK_ConstructorConversion - Conversion by constructor
1209    CK_ConstructorConversion
1210  };
1211
1212  struct CastInfo {
1213    const CastKind Kind;
1214
1215    // FIXME: This should assert that the CastKind does not require extra
1216    // information.
1217    CastInfo(CastKind Kind)
1218      : Kind(Kind) { }
1219  };
1220
1221private:
1222  CastKind Kind;
1223  Stmt *Op;
1224protected:
1225  CastExpr(StmtClass SC, QualType ty, const CastInfo &info, Expr *op) :
1226    Expr(SC, ty,
1227         // Cast expressions are type-dependent if the type is
1228         // dependent (C++ [temp.dep.expr]p3).
1229         ty->isDependentType(),
1230         // Cast expressions are value-dependent if the type is
1231         // dependent or if the subexpression is value-dependent.
1232         ty->isDependentType() || (op && op->isValueDependent())),
1233    Kind(info.Kind), Op(op) {}
1234
1235  /// \brief Construct an empty cast.
1236  CastExpr(StmtClass SC, EmptyShell Empty)
1237    : Expr(SC, Empty) { }
1238
1239public:
1240  CastKind getCastKind() const { return Kind; }
1241  void setCastKind(CastKind K) { Kind = K; }
1242
1243  Expr *getSubExpr() { return cast<Expr>(Op); }
1244  const Expr *getSubExpr() const { return cast<Expr>(Op); }
1245  void setSubExpr(Expr *E) { Op = E; }
1246
1247  static bool classof(const Stmt *T) {
1248    StmtClass SC = T->getStmtClass();
1249    if (SC >= CXXNamedCastExprClass && SC <= CXXFunctionalCastExprClass)
1250      return true;
1251
1252    if (SC >= ImplicitCastExprClass && SC <= CStyleCastExprClass)
1253      return true;
1254
1255    return false;
1256  }
1257  static bool classof(const CastExpr *) { return true; }
1258
1259  // Iterators
1260  virtual child_iterator child_begin();
1261  virtual child_iterator child_end();
1262};
1263
1264/// ImplicitCastExpr - Allows us to explicitly represent implicit type
1265/// conversions, which have no direct representation in the original
1266/// source code. For example: converting T[]->T*, void f()->void
1267/// (*f)(), float->double, short->int, etc.
1268///
1269/// In C, implicit casts always produce rvalues. However, in C++, an
1270/// implicit cast whose result is being bound to a reference will be
1271/// an lvalue. For example:
1272///
1273/// @code
1274/// class Base { };
1275/// class Derived : public Base { };
1276/// void f(Derived d) {
1277///   Base& b = d; // initializer is an ImplicitCastExpr to an lvalue of type Base
1278/// }
1279/// @endcode
1280class ImplicitCastExpr : public CastExpr {
1281  /// LvalueCast - Whether this cast produces an lvalue.
1282  bool LvalueCast;
1283
1284public:
1285  ImplicitCastExpr(QualType ty, const CastInfo &info, Expr *op, bool Lvalue) :
1286    CastExpr(ImplicitCastExprClass, ty, info, op), LvalueCast(Lvalue) { }
1287
1288  /// \brief Construct an empty implicit cast.
1289  explicit ImplicitCastExpr(EmptyShell Shell)
1290    : CastExpr(ImplicitCastExprClass, Shell) { }
1291
1292
1293  virtual SourceRange getSourceRange() const {
1294    return getSubExpr()->getSourceRange();
1295  }
1296
1297  /// isLvalueCast - Whether this cast produces an lvalue.
1298  bool isLvalueCast() const { return LvalueCast; }
1299
1300  /// setLvalueCast - Set whether this cast produces an lvalue.
1301  void setLvalueCast(bool Lvalue) { LvalueCast = Lvalue; }
1302
1303  static bool classof(const Stmt *T) {
1304    return T->getStmtClass() == ImplicitCastExprClass;
1305  }
1306  static bool classof(const ImplicitCastExpr *) { return true; }
1307};
1308
1309/// ExplicitCastExpr - An explicit cast written in the source
1310/// code.
1311///
1312/// This class is effectively an abstract class, because it provides
1313/// the basic representation of an explicitly-written cast without
1314/// specifying which kind of cast (C cast, functional cast, static
1315/// cast, etc.) was written; specific derived classes represent the
1316/// particular style of cast and its location information.
1317///
1318/// Unlike implicit casts, explicit cast nodes have two different
1319/// types: the type that was written into the source code, and the
1320/// actual type of the expression as determined by semantic
1321/// analysis. These types may differ slightly. For example, in C++ one
1322/// can cast to a reference type, which indicates that the resulting
1323/// expression will be an lvalue. The reference type, however, will
1324/// not be used as the type of the expression.
1325class ExplicitCastExpr : public CastExpr {
1326  /// TypeAsWritten - The type that this expression is casting to, as
1327  /// written in the source code.
1328  QualType TypeAsWritten;
1329
1330protected:
1331  ExplicitCastExpr(StmtClass SC, QualType exprTy, const CastInfo &info,
1332                   Expr *op, QualType writtenTy)
1333    : CastExpr(SC, exprTy, info, op), TypeAsWritten(writtenTy) {}
1334
1335  /// \brief Construct an empty explicit cast.
1336  ExplicitCastExpr(StmtClass SC, EmptyShell Shell)
1337    : CastExpr(SC, Shell) { }
1338
1339public:
1340  /// getTypeAsWritten - Returns the type that this expression is
1341  /// casting to, as written in the source code.
1342  QualType getTypeAsWritten() const { return TypeAsWritten; }
1343  void setTypeAsWritten(QualType T) { TypeAsWritten = T; }
1344
1345  static bool classof(const Stmt *T) {
1346    StmtClass SC = T->getStmtClass();
1347    if (SC >= ExplicitCastExprClass && SC <= CStyleCastExprClass)
1348      return true;
1349    if (SC >= CXXNamedCastExprClass && SC <= CXXFunctionalCastExprClass)
1350      return true;
1351
1352    return false;
1353  }
1354  static bool classof(const ExplicitCastExpr *) { return true; }
1355};
1356
1357/// CStyleCastExpr - An explicit cast in C (C99 6.5.4) or a C-style
1358/// cast in C++ (C++ [expr.cast]), which uses the syntax
1359/// (Type)expr. For example: @c (int)f.
1360class CStyleCastExpr : public ExplicitCastExpr {
1361  SourceLocation LPLoc; // the location of the left paren
1362  SourceLocation RPLoc; // the location of the right paren
1363public:
1364  CStyleCastExpr(QualType exprTy, CastKind kind, Expr *op, QualType writtenTy,
1365                    SourceLocation l, SourceLocation r) :
1366    ExplicitCastExpr(CStyleCastExprClass, exprTy, kind, op, writtenTy),
1367    LPLoc(l), RPLoc(r) {}
1368
1369  /// \brief Construct an empty C-style explicit cast.
1370  explicit CStyleCastExpr(EmptyShell Shell)
1371    : ExplicitCastExpr(CStyleCastExprClass, Shell) { }
1372
1373  SourceLocation getLParenLoc() const { return LPLoc; }
1374  void setLParenLoc(SourceLocation L) { LPLoc = L; }
1375
1376  SourceLocation getRParenLoc() const { return RPLoc; }
1377  void setRParenLoc(SourceLocation L) { RPLoc = L; }
1378
1379  virtual SourceRange getSourceRange() const {
1380    return SourceRange(LPLoc, getSubExpr()->getSourceRange().getEnd());
1381  }
1382  static bool classof(const Stmt *T) {
1383    return T->getStmtClass() == CStyleCastExprClass;
1384  }
1385  static bool classof(const CStyleCastExpr *) { return true; }
1386};
1387
1388/// \brief A builtin binary operation expression such as "x + y" or "x <= y".
1389///
1390/// This expression node kind describes a builtin binary operation,
1391/// such as "x + y" for integer values "x" and "y". The operands will
1392/// already have been converted to appropriate types (e.g., by
1393/// performing promotions or conversions).
1394///
1395/// In C++, where operators may be overloaded, a different kind of
1396/// expression node (CXXOperatorCallExpr) is used to express the
1397/// invocation of an overloaded operator with operator syntax. Within
1398/// a C++ template, whether BinaryOperator or CXXOperatorCallExpr is
1399/// used to store an expression "x + y" depends on the subexpressions
1400/// for x and y. If neither x or y is type-dependent, and the "+"
1401/// operator resolves to a built-in operation, BinaryOperator will be
1402/// used to express the computation (x and y may still be
1403/// value-dependent). If either x or y is type-dependent, or if the
1404/// "+" resolves to an overloaded operator, CXXOperatorCallExpr will
1405/// be used to express the computation.
1406class BinaryOperator : public Expr {
1407public:
1408  enum Opcode {
1409    // Operators listed in order of precedence.
1410    // Note that additions to this should also update the StmtVisitor class.
1411    PtrMemD, PtrMemI, // [C++ 5.5] Pointer-to-member operators.
1412    Mul, Div, Rem,    // [C99 6.5.5] Multiplicative operators.
1413    Add, Sub,         // [C99 6.5.6] Additive operators.
1414    Shl, Shr,         // [C99 6.5.7] Bitwise shift operators.
1415    LT, GT, LE, GE,   // [C99 6.5.8] Relational operators.
1416    EQ, NE,           // [C99 6.5.9] Equality operators.
1417    And,              // [C99 6.5.10] Bitwise AND operator.
1418    Xor,              // [C99 6.5.11] Bitwise XOR operator.
1419    Or,               // [C99 6.5.12] Bitwise OR operator.
1420    LAnd,             // [C99 6.5.13] Logical AND operator.
1421    LOr,              // [C99 6.5.14] Logical OR operator.
1422    Assign, MulAssign,// [C99 6.5.16] Assignment operators.
1423    DivAssign, RemAssign,
1424    AddAssign, SubAssign,
1425    ShlAssign, ShrAssign,
1426    AndAssign, XorAssign,
1427    OrAssign,
1428    Comma             // [C99 6.5.17] Comma operator.
1429  };
1430private:
1431  enum { LHS, RHS, END_EXPR };
1432  Stmt* SubExprs[END_EXPR];
1433  Opcode Opc;
1434  SourceLocation OpLoc;
1435public:
1436
1437  BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy,
1438                 SourceLocation opLoc)
1439    : Expr(BinaryOperatorClass, ResTy,
1440           lhs->isTypeDependent() || rhs->isTypeDependent(),
1441           lhs->isValueDependent() || rhs->isValueDependent()),
1442      Opc(opc), OpLoc(opLoc) {
1443    SubExprs[LHS] = lhs;
1444    SubExprs[RHS] = rhs;
1445    assert(!isCompoundAssignmentOp() &&
1446           "Use ArithAssignBinaryOperator for compound assignments");
1447  }
1448
1449  /// \brief Construct an empty binary operator.
1450  explicit BinaryOperator(EmptyShell Empty)
1451    : Expr(BinaryOperatorClass, Empty), Opc(Comma) { }
1452
1453  SourceLocation getOperatorLoc() const { return OpLoc; }
1454  void setOperatorLoc(SourceLocation L) { OpLoc = L; }
1455
1456  Opcode getOpcode() const { return Opc; }
1457  void setOpcode(Opcode O) { Opc = O; }
1458
1459  Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); }
1460  void setLHS(Expr *E) { SubExprs[LHS] = E; }
1461  Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); }
1462  void setRHS(Expr *E) { SubExprs[RHS] = E; }
1463
1464  virtual SourceRange getSourceRange() const {
1465    return SourceRange(getLHS()->getLocStart(), getRHS()->getLocEnd());
1466  }
1467
1468  /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1469  /// corresponds to, e.g. "<<=".
1470  static const char *getOpcodeStr(Opcode Op);
1471
1472  /// \brief Retrieve the binary opcode that corresponds to the given
1473  /// overloaded operator.
1474  static Opcode getOverloadedOpcode(OverloadedOperatorKind OO);
1475
1476  /// \brief Retrieve the overloaded operator kind that corresponds to
1477  /// the given binary opcode.
1478  static OverloadedOperatorKind getOverloadedOperator(Opcode Opc);
1479
1480  /// predicates to categorize the respective opcodes.
1481  bool isMultiplicativeOp() const { return Opc >= Mul && Opc <= Rem; }
1482  bool isAdditiveOp() const { return Opc == Add || Opc == Sub; }
1483  bool isShiftOp() const { return Opc == Shl || Opc == Shr; }
1484  bool isBitwiseOp() const { return Opc >= And && Opc <= Or; }
1485
1486  static bool isRelationalOp(Opcode Opc) { return Opc >= LT && Opc <= GE; }
1487  bool isRelationalOp() const { return isRelationalOp(Opc); }
1488
1489  static bool isEqualityOp(Opcode Opc) { return Opc == EQ || Opc == NE; }
1490  bool isEqualityOp() const { return isEqualityOp(Opc); }
1491
1492  static bool isLogicalOp(Opcode Opc) { return Opc == LAnd || Opc == LOr; }
1493  bool isLogicalOp() const { return isLogicalOp(Opc); }
1494
1495  bool isAssignmentOp() const { return Opc >= Assign && Opc <= OrAssign; }
1496  bool isCompoundAssignmentOp() const { return Opc > Assign && Opc <= OrAssign;}
1497  bool isShiftAssignOp() const { return Opc == ShlAssign || Opc == ShrAssign; }
1498
1499  static bool classof(const Stmt *S) {
1500    return S->getStmtClass() == BinaryOperatorClass ||
1501           S->getStmtClass() == CompoundAssignOperatorClass;
1502  }
1503  static bool classof(const BinaryOperator *) { return true; }
1504
1505  // Iterators
1506  virtual child_iterator child_begin();
1507  virtual child_iterator child_end();
1508
1509protected:
1510  BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy,
1511                 SourceLocation oploc, bool dead)
1512    : Expr(CompoundAssignOperatorClass, ResTy), Opc(opc), OpLoc(oploc) {
1513    SubExprs[LHS] = lhs;
1514    SubExprs[RHS] = rhs;
1515  }
1516
1517  BinaryOperator(StmtClass SC, EmptyShell Empty)
1518    : Expr(SC, Empty), Opc(MulAssign) { }
1519};
1520
1521/// CompoundAssignOperator - For compound assignments (e.g. +=), we keep
1522/// track of the type the operation is performed in.  Due to the semantics of
1523/// these operators, the operands are promoted, the aritmetic performed, an
1524/// implicit conversion back to the result type done, then the assignment takes
1525/// place.  This captures the intermediate type which the computation is done
1526/// in.
1527class CompoundAssignOperator : public BinaryOperator {
1528  QualType ComputationLHSType;
1529  QualType ComputationResultType;
1530public:
1531  CompoundAssignOperator(Expr *lhs, Expr *rhs, Opcode opc,
1532                         QualType ResType, QualType CompLHSType,
1533                         QualType CompResultType,
1534                         SourceLocation OpLoc)
1535    : BinaryOperator(lhs, rhs, opc, ResType, OpLoc, true),
1536      ComputationLHSType(CompLHSType),
1537      ComputationResultType(CompResultType) {
1538    assert(isCompoundAssignmentOp() &&
1539           "Only should be used for compound assignments");
1540  }
1541
1542  /// \brief Build an empty compound assignment operator expression.
1543  explicit CompoundAssignOperator(EmptyShell Empty)
1544    : BinaryOperator(CompoundAssignOperatorClass, Empty) { }
1545
1546  // The two computation types are the type the LHS is converted
1547  // to for the computation and the type of the result; the two are
1548  // distinct in a few cases (specifically, int+=ptr and ptr-=ptr).
1549  QualType getComputationLHSType() const { return ComputationLHSType; }
1550  void setComputationLHSType(QualType T) { ComputationLHSType = T; }
1551
1552  QualType getComputationResultType() const { return ComputationResultType; }
1553  void setComputationResultType(QualType T) { ComputationResultType = T; }
1554
1555  static bool classof(const CompoundAssignOperator *) { return true; }
1556  static bool classof(const Stmt *S) {
1557    return S->getStmtClass() == CompoundAssignOperatorClass;
1558  }
1559};
1560
1561/// ConditionalOperator - The ?: operator.  Note that LHS may be null when the
1562/// GNU "missing LHS" extension is in use.
1563///
1564class ConditionalOperator : public Expr {
1565  enum { COND, LHS, RHS, END_EXPR };
1566  Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides.
1567  SourceLocation QuestionLoc, ColonLoc;
1568public:
1569  ConditionalOperator(Expr *cond, SourceLocation QLoc, Expr *lhs,
1570                      SourceLocation CLoc, Expr *rhs, QualType t)
1571    : Expr(ConditionalOperatorClass, t,
1572           // FIXME: the type of the conditional operator doesn't
1573           // depend on the type of the conditional, but the standard
1574           // seems to imply that it could. File a bug!
1575           ((lhs && lhs->isTypeDependent()) || (rhs && rhs->isTypeDependent())),
1576           (cond->isValueDependent() ||
1577            (lhs && lhs->isValueDependent()) ||
1578            (rhs && rhs->isValueDependent()))),
1579      QuestionLoc(QLoc),
1580      ColonLoc(CLoc) {
1581    SubExprs[COND] = cond;
1582    SubExprs[LHS] = lhs;
1583    SubExprs[RHS] = rhs;
1584  }
1585
1586  /// \brief Build an empty conditional operator.
1587  explicit ConditionalOperator(EmptyShell Empty)
1588    : Expr(ConditionalOperatorClass, Empty) { }
1589
1590  // getCond - Return the expression representing the condition for
1591  //  the ?: operator.
1592  Expr *getCond() const { return cast<Expr>(SubExprs[COND]); }
1593  void setCond(Expr *E) { SubExprs[COND] = E; }
1594
1595  // getTrueExpr - Return the subexpression representing the value of the ?:
1596  //  expression if the condition evaluates to true.  In most cases this value
1597  //  will be the same as getLHS() except a GCC extension allows the left
1598  //  subexpression to be omitted, and instead of the condition be returned.
1599  //  e.g: x ?: y is shorthand for x ? x : y, except that the expression "x"
1600  //  is only evaluated once.
1601  Expr *getTrueExpr() const {
1602    return cast<Expr>(SubExprs[LHS] ? SubExprs[LHS] : SubExprs[COND]);
1603  }
1604
1605  // getTrueExpr - Return the subexpression representing the value of the ?:
1606  // expression if the condition evaluates to false. This is the same as getRHS.
1607  Expr *getFalseExpr() const { return cast<Expr>(SubExprs[RHS]); }
1608
1609  Expr *getLHS() const { return cast_or_null<Expr>(SubExprs[LHS]); }
1610  void setLHS(Expr *E) { SubExprs[LHS] = E; }
1611
1612  Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); }
1613  void setRHS(Expr *E) { SubExprs[RHS] = E; }
1614
1615  SourceLocation getQuestionLoc() const { return QuestionLoc; }
1616  void setQuestionLoc(SourceLocation L) { QuestionLoc = L; }
1617
1618  SourceLocation getColonLoc() const { return ColonLoc; }
1619  void setColonLoc(SourceLocation L) { ColonLoc = L; }
1620
1621  virtual SourceRange getSourceRange() const {
1622    return SourceRange(getCond()->getLocStart(), getRHS()->getLocEnd());
1623  }
1624  static bool classof(const Stmt *T) {
1625    return T->getStmtClass() == ConditionalOperatorClass;
1626  }
1627  static bool classof(const ConditionalOperator *) { return true; }
1628
1629  // Iterators
1630  virtual child_iterator child_begin();
1631  virtual child_iterator child_end();
1632};
1633
1634/// AddrLabelExpr - The GNU address of label extension, representing &&label.
1635class AddrLabelExpr : public Expr {
1636  SourceLocation AmpAmpLoc, LabelLoc;
1637  LabelStmt *Label;
1638public:
1639  AddrLabelExpr(SourceLocation AALoc, SourceLocation LLoc, LabelStmt *L,
1640                QualType t)
1641    : Expr(AddrLabelExprClass, t), AmpAmpLoc(AALoc), LabelLoc(LLoc), Label(L) {}
1642
1643  /// \brief Build an empty address of a label expression.
1644  explicit AddrLabelExpr(EmptyShell Empty)
1645    : Expr(AddrLabelExprClass, Empty) { }
1646
1647  SourceLocation getAmpAmpLoc() const { return AmpAmpLoc; }
1648  void setAmpAmpLoc(SourceLocation L) { AmpAmpLoc = L; }
1649  SourceLocation getLabelLoc() const { return LabelLoc; }
1650  void setLabelLoc(SourceLocation L) { LabelLoc = L; }
1651
1652  virtual SourceRange getSourceRange() const {
1653    return SourceRange(AmpAmpLoc, LabelLoc);
1654  }
1655
1656  LabelStmt *getLabel() const { return Label; }
1657  void setLabel(LabelStmt *S) { Label = S; }
1658
1659  static bool classof(const Stmt *T) {
1660    return T->getStmtClass() == AddrLabelExprClass;
1661  }
1662  static bool classof(const AddrLabelExpr *) { return true; }
1663
1664  // Iterators
1665  virtual child_iterator child_begin();
1666  virtual child_iterator child_end();
1667};
1668
1669/// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}).
1670/// The StmtExpr contains a single CompoundStmt node, which it evaluates and
1671/// takes the value of the last subexpression.
1672class StmtExpr : public Expr {
1673  Stmt *SubStmt;
1674  SourceLocation LParenLoc, RParenLoc;
1675public:
1676  StmtExpr(CompoundStmt *substmt, QualType T,
1677           SourceLocation lp, SourceLocation rp) :
1678    Expr(StmtExprClass, T), SubStmt(substmt),  LParenLoc(lp), RParenLoc(rp) { }
1679
1680  /// \brief Build an empty statement expression.
1681  explicit StmtExpr(EmptyShell Empty) : Expr(StmtExprClass, Empty) { }
1682
1683  CompoundStmt *getSubStmt() { return cast<CompoundStmt>(SubStmt); }
1684  const CompoundStmt *getSubStmt() const { return cast<CompoundStmt>(SubStmt); }
1685  void setSubStmt(CompoundStmt *S) { SubStmt = S; }
1686
1687  virtual SourceRange getSourceRange() const {
1688    return SourceRange(LParenLoc, RParenLoc);
1689  }
1690
1691  SourceLocation getLParenLoc() const { return LParenLoc; }
1692  void setLParenLoc(SourceLocation L) { LParenLoc = L; }
1693  SourceLocation getRParenLoc() const { return RParenLoc; }
1694  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1695
1696  static bool classof(const Stmt *T) {
1697    return T->getStmtClass() == StmtExprClass;
1698  }
1699  static bool classof(const StmtExpr *) { return true; }
1700
1701  // Iterators
1702  virtual child_iterator child_begin();
1703  virtual child_iterator child_end();
1704};
1705
1706/// TypesCompatibleExpr - GNU builtin-in function __builtin_types_compatible_p.
1707/// This AST node represents a function that returns 1 if two *types* (not
1708/// expressions) are compatible. The result of this built-in function can be
1709/// used in integer constant expressions.
1710class TypesCompatibleExpr : public Expr {
1711  QualType Type1;
1712  QualType Type2;
1713  SourceLocation BuiltinLoc, RParenLoc;
1714public:
1715  TypesCompatibleExpr(QualType ReturnType, SourceLocation BLoc,
1716                      QualType t1, QualType t2, SourceLocation RP) :
1717    Expr(TypesCompatibleExprClass, ReturnType), Type1(t1), Type2(t2),
1718    BuiltinLoc(BLoc), RParenLoc(RP) {}
1719
1720  /// \brief Build an empty __builtin_type_compatible_p expression.
1721  explicit TypesCompatibleExpr(EmptyShell Empty)
1722    : Expr(TypesCompatibleExprClass, Empty) { }
1723
1724  QualType getArgType1() const { return Type1; }
1725  void setArgType1(QualType T) { Type1 = T; }
1726  QualType getArgType2() const { return Type2; }
1727  void setArgType2(QualType T) { Type2 = T; }
1728
1729  SourceLocation getBuiltinLoc() const { return BuiltinLoc; }
1730  void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; }
1731
1732  SourceLocation getRParenLoc() const { return RParenLoc; }
1733  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1734
1735  virtual SourceRange getSourceRange() const {
1736    return SourceRange(BuiltinLoc, RParenLoc);
1737  }
1738  static bool classof(const Stmt *T) {
1739    return T->getStmtClass() == TypesCompatibleExprClass;
1740  }
1741  static bool classof(const TypesCompatibleExpr *) { return true; }
1742
1743  // Iterators
1744  virtual child_iterator child_begin();
1745  virtual child_iterator child_end();
1746};
1747
1748/// ShuffleVectorExpr - clang-specific builtin-in function
1749/// __builtin_shufflevector.
1750/// This AST node represents a operator that does a constant
1751/// shuffle, similar to LLVM's shufflevector instruction. It takes
1752/// two vectors and a variable number of constant indices,
1753/// and returns the appropriately shuffled vector.
1754class ShuffleVectorExpr : public Expr {
1755  SourceLocation BuiltinLoc, RParenLoc;
1756
1757  // SubExprs - the list of values passed to the __builtin_shufflevector
1758  // function. The first two are vectors, and the rest are constant
1759  // indices.  The number of values in this list is always
1760  // 2+the number of indices in the vector type.
1761  Stmt **SubExprs;
1762  unsigned NumExprs;
1763
1764protected:
1765  virtual void DoDestroy(ASTContext &C);
1766
1767public:
1768  ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
1769                    QualType Type, SourceLocation BLoc,
1770                    SourceLocation RP) :
1771    Expr(ShuffleVectorExprClass, Type), BuiltinLoc(BLoc),
1772    RParenLoc(RP), NumExprs(nexpr) {
1773
1774    SubExprs = new (C) Stmt*[nexpr];
1775    for (unsigned i = 0; i < nexpr; i++)
1776      SubExprs[i] = args[i];
1777  }
1778
1779  /// \brief Build an empty vector-shuffle expression.
1780  explicit ShuffleVectorExpr(EmptyShell Empty)
1781    : Expr(ShuffleVectorExprClass, Empty), SubExprs(0) { }
1782
1783  SourceLocation getBuiltinLoc() const { return BuiltinLoc; }
1784  void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; }
1785
1786  SourceLocation getRParenLoc() const { return RParenLoc; }
1787  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1788
1789  virtual SourceRange getSourceRange() const {
1790    return SourceRange(BuiltinLoc, RParenLoc);
1791  }
1792  static bool classof(const Stmt *T) {
1793    return T->getStmtClass() == ShuffleVectorExprClass;
1794  }
1795  static bool classof(const ShuffleVectorExpr *) { return true; }
1796
1797  ~ShuffleVectorExpr() {}
1798
1799  /// getNumSubExprs - Return the size of the SubExprs array.  This includes the
1800  /// constant expression, the actual arguments passed in, and the function
1801  /// pointers.
1802  unsigned getNumSubExprs() const { return NumExprs; }
1803
1804  /// getExpr - Return the Expr at the specified index.
1805  Expr *getExpr(unsigned Index) {
1806    assert((Index < NumExprs) && "Arg access out of range!");
1807    return cast<Expr>(SubExprs[Index]);
1808  }
1809  const Expr *getExpr(unsigned Index) const {
1810    assert((Index < NumExprs) && "Arg access out of range!");
1811    return cast<Expr>(SubExprs[Index]);
1812  }
1813
1814  void setExprs(ASTContext &C, Expr ** Exprs, unsigned NumExprs);
1815
1816  unsigned getShuffleMaskIdx(ASTContext &Ctx, unsigned N) {
1817    assert((N < NumExprs - 2) && "Shuffle idx out of range!");
1818    return getExpr(N+2)->EvaluateAsInt(Ctx).getZExtValue();
1819  }
1820
1821  // Iterators
1822  virtual child_iterator child_begin();
1823  virtual child_iterator child_end();
1824};
1825
1826/// ChooseExpr - GNU builtin-in function __builtin_choose_expr.
1827/// This AST node is similar to the conditional operator (?:) in C, with
1828/// the following exceptions:
1829/// - the test expression must be a integer constant expression.
1830/// - the expression returned acts like the chosen subexpression in every
1831///   visible way: the type is the same as that of the chosen subexpression,
1832///   and all predicates (whether it's an l-value, whether it's an integer
1833///   constant expression, etc.) return the same result as for the chosen
1834///   sub-expression.
1835class ChooseExpr : public Expr {
1836  enum { COND, LHS, RHS, END_EXPR };
1837  Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides.
1838  SourceLocation BuiltinLoc, RParenLoc;
1839public:
1840  ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, QualType t,
1841             SourceLocation RP)
1842    : Expr(ChooseExprClass, t),
1843      BuiltinLoc(BLoc), RParenLoc(RP) {
1844      SubExprs[COND] = cond;
1845      SubExprs[LHS] = lhs;
1846      SubExprs[RHS] = rhs;
1847    }
1848
1849  /// \brief Build an empty __builtin_choose_expr.
1850  explicit ChooseExpr(EmptyShell Empty) : Expr(ChooseExprClass, Empty) { }
1851
1852  /// isConditionTrue - Return whether the condition is true (i.e. not
1853  /// equal to zero).
1854  bool isConditionTrue(ASTContext &C) const;
1855
1856  /// getChosenSubExpr - Return the subexpression chosen according to the
1857  /// condition.
1858  Expr *getChosenSubExpr(ASTContext &C) const {
1859    return isConditionTrue(C) ? getLHS() : getRHS();
1860  }
1861
1862  Expr *getCond() const { return cast<Expr>(SubExprs[COND]); }
1863  void setCond(Expr *E) { SubExprs[COND] = E; }
1864  Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); }
1865  void setLHS(Expr *E) { SubExprs[LHS] = E; }
1866  Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); }
1867  void setRHS(Expr *E) { SubExprs[RHS] = E; }
1868
1869  SourceLocation getBuiltinLoc() const { return BuiltinLoc; }
1870  void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; }
1871
1872  SourceLocation getRParenLoc() const { return RParenLoc; }
1873  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1874
1875  virtual SourceRange getSourceRange() const {
1876    return SourceRange(BuiltinLoc, RParenLoc);
1877  }
1878  static bool classof(const Stmt *T) {
1879    return T->getStmtClass() == ChooseExprClass;
1880  }
1881  static bool classof(const ChooseExpr *) { return true; }
1882
1883  // Iterators
1884  virtual child_iterator child_begin();
1885  virtual child_iterator child_end();
1886};
1887
1888/// GNUNullExpr - Implements the GNU __null extension, which is a name
1889/// for a null pointer constant that has integral type (e.g., int or
1890/// long) and is the same size and alignment as a pointer. The __null
1891/// extension is typically only used by system headers, which define
1892/// NULL as __null in C++ rather than using 0 (which is an integer
1893/// that may not match the size of a pointer).
1894class GNUNullExpr : public Expr {
1895  /// TokenLoc - The location of the __null keyword.
1896  SourceLocation TokenLoc;
1897
1898public:
1899  GNUNullExpr(QualType Ty, SourceLocation Loc)
1900    : Expr(GNUNullExprClass, Ty), TokenLoc(Loc) { }
1901
1902  /// \brief Build an empty GNU __null expression.
1903  explicit GNUNullExpr(EmptyShell Empty) : Expr(GNUNullExprClass, Empty) { }
1904
1905  /// getTokenLocation - The location of the __null token.
1906  SourceLocation getTokenLocation() const { return TokenLoc; }
1907  void setTokenLocation(SourceLocation L) { TokenLoc = L; }
1908
1909  virtual SourceRange getSourceRange() const {
1910    return SourceRange(TokenLoc);
1911  }
1912  static bool classof(const Stmt *T) {
1913    return T->getStmtClass() == GNUNullExprClass;
1914  }
1915  static bool classof(const GNUNullExpr *) { return true; }
1916
1917  // Iterators
1918  virtual child_iterator child_begin();
1919  virtual child_iterator child_end();
1920};
1921
1922/// VAArgExpr, used for the builtin function __builtin_va_start.
1923class VAArgExpr : public Expr {
1924  Stmt *Val;
1925  SourceLocation BuiltinLoc, RParenLoc;
1926public:
1927  VAArgExpr(SourceLocation BLoc, Expr* e, QualType t, SourceLocation RPLoc)
1928    : Expr(VAArgExprClass, t),
1929      Val(e),
1930      BuiltinLoc(BLoc),
1931      RParenLoc(RPLoc) { }
1932
1933  /// \brief Create an empty __builtin_va_start expression.
1934  explicit VAArgExpr(EmptyShell Empty) : Expr(VAArgExprClass, Empty) { }
1935
1936  const Expr *getSubExpr() const { return cast<Expr>(Val); }
1937  Expr *getSubExpr() { return cast<Expr>(Val); }
1938  void setSubExpr(Expr *E) { Val = E; }
1939
1940  SourceLocation getBuiltinLoc() const { return BuiltinLoc; }
1941  void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; }
1942
1943  SourceLocation getRParenLoc() const { return RParenLoc; }
1944  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1945
1946  virtual SourceRange getSourceRange() const {
1947    return SourceRange(BuiltinLoc, RParenLoc);
1948  }
1949  static bool classof(const Stmt *T) {
1950    return T->getStmtClass() == VAArgExprClass;
1951  }
1952  static bool classof(const VAArgExpr *) { return true; }
1953
1954  // Iterators
1955  virtual child_iterator child_begin();
1956  virtual child_iterator child_end();
1957};
1958
1959/// @brief Describes an C or C++ initializer list.
1960///
1961/// InitListExpr describes an initializer list, which can be used to
1962/// initialize objects of different types, including
1963/// struct/class/union types, arrays, and vectors. For example:
1964///
1965/// @code
1966/// struct foo x = { 1, { 2, 3 } };
1967/// @endcode
1968///
1969/// Prior to semantic analysis, an initializer list will represent the
1970/// initializer list as written by the user, but will have the
1971/// placeholder type "void". This initializer list is called the
1972/// syntactic form of the initializer, and may contain C99 designated
1973/// initializers (represented as DesignatedInitExprs), initializations
1974/// of subobject members without explicit braces, and so on. Clients
1975/// interested in the original syntax of the initializer list should
1976/// use the syntactic form of the initializer list.
1977///
1978/// After semantic analysis, the initializer list will represent the
1979/// semantic form of the initializer, where the initializations of all
1980/// subobjects are made explicit with nested InitListExpr nodes and
1981/// C99 designators have been eliminated by placing the designated
1982/// initializations into the subobject they initialize. Additionally,
1983/// any "holes" in the initialization, where no initializer has been
1984/// specified for a particular subobject, will be replaced with
1985/// implicitly-generated ImplicitValueInitExpr expressions that
1986/// value-initialize the subobjects. Note, however, that the
1987/// initializer lists may still have fewer initializers than there are
1988/// elements to initialize within the object.
1989///
1990/// Given the semantic form of the initializer list, one can retrieve
1991/// the original syntactic form of that initializer list (if it
1992/// exists) using getSyntacticForm(). Since many initializer lists
1993/// have the same syntactic and semantic forms, getSyntacticForm() may
1994/// return NULL, indicating that the current initializer list also
1995/// serves as its syntactic form.
1996class InitListExpr : public Expr {
1997  // FIXME: Eliminate this vector in favor of ASTContext allocation
1998  std::vector<Stmt *> InitExprs;
1999  SourceLocation LBraceLoc, RBraceLoc;
2000
2001  /// Contains the initializer list that describes the syntactic form
2002  /// written in the source code.
2003  InitListExpr *SyntacticForm;
2004
2005  /// If this initializer list initializes a union, specifies which
2006  /// field within the union will be initialized.
2007  FieldDecl *UnionFieldInit;
2008
2009  /// Whether this initializer list originally had a GNU array-range
2010  /// designator in it. This is a temporary marker used by CodeGen.
2011  bool HadArrayRangeDesignator;
2012
2013public:
2014  InitListExpr(SourceLocation lbraceloc, Expr **initexprs, unsigned numinits,
2015               SourceLocation rbraceloc);
2016
2017  /// \brief Build an empty initializer list.
2018  explicit InitListExpr(EmptyShell Empty) : Expr(InitListExprClass, Empty) { }
2019
2020  unsigned getNumInits() const { return InitExprs.size(); }
2021
2022  const Expr* getInit(unsigned Init) const {
2023    assert(Init < getNumInits() && "Initializer access out of range!");
2024    return cast_or_null<Expr>(InitExprs[Init]);
2025  }
2026
2027  Expr* getInit(unsigned Init) {
2028    assert(Init < getNumInits() && "Initializer access out of range!");
2029    return cast_or_null<Expr>(InitExprs[Init]);
2030  }
2031
2032  void setInit(unsigned Init, Expr *expr) {
2033    assert(Init < getNumInits() && "Initializer access out of range!");
2034    InitExprs[Init] = expr;
2035  }
2036
2037  /// \brief Reserve space for some number of initializers.
2038  void reserveInits(unsigned NumInits);
2039
2040  /// @brief Specify the number of initializers
2041  ///
2042  /// If there are more than @p NumInits initializers, the remaining
2043  /// initializers will be destroyed. If there are fewer than @p
2044  /// NumInits initializers, NULL expressions will be added for the
2045  /// unknown initializers.
2046  void resizeInits(ASTContext &Context, unsigned NumInits);
2047
2048  /// @brief Updates the initializer at index @p Init with the new
2049  /// expression @p expr, and returns the old expression at that
2050  /// location.
2051  ///
2052  /// When @p Init is out of range for this initializer list, the
2053  /// initializer list will be extended with NULL expressions to
2054  /// accomodate the new entry.
2055  Expr *updateInit(unsigned Init, Expr *expr);
2056
2057  /// \brief If this initializes a union, specifies which field in the
2058  /// union to initialize.
2059  ///
2060  /// Typically, this field is the first named field within the
2061  /// union. However, a designated initializer can specify the
2062  /// initialization of a different field within the union.
2063  FieldDecl *getInitializedFieldInUnion() { return UnionFieldInit; }
2064  void setInitializedFieldInUnion(FieldDecl *FD) { UnionFieldInit = FD; }
2065
2066  // Explicit InitListExpr's originate from source code (and have valid source
2067  // locations). Implicit InitListExpr's are created by the semantic analyzer.
2068  bool isExplicit() {
2069    return LBraceLoc.isValid() && RBraceLoc.isValid();
2070  }
2071
2072  SourceLocation getLBraceLoc() const { return LBraceLoc; }
2073  void setLBraceLoc(SourceLocation Loc) { LBraceLoc = Loc; }
2074  SourceLocation getRBraceLoc() const { return RBraceLoc; }
2075  void setRBraceLoc(SourceLocation Loc) { RBraceLoc = Loc; }
2076
2077  /// @brief Retrieve the initializer list that describes the
2078  /// syntactic form of the initializer.
2079  ///
2080  ///
2081  InitListExpr *getSyntacticForm() const { return SyntacticForm; }
2082  void setSyntacticForm(InitListExpr *Init) { SyntacticForm = Init; }
2083
2084  bool hadArrayRangeDesignator() const { return HadArrayRangeDesignator; }
2085  void sawArrayRangeDesignator(bool ARD = true) {
2086    HadArrayRangeDesignator = ARD;
2087  }
2088
2089  virtual SourceRange getSourceRange() const {
2090    return SourceRange(LBraceLoc, RBraceLoc);
2091  }
2092  static bool classof(const Stmt *T) {
2093    return T->getStmtClass() == InitListExprClass;
2094  }
2095  static bool classof(const InitListExpr *) { return true; }
2096
2097  // Iterators
2098  virtual child_iterator child_begin();
2099  virtual child_iterator child_end();
2100
2101  typedef std::vector<Stmt *>::iterator iterator;
2102  typedef std::vector<Stmt *>::reverse_iterator reverse_iterator;
2103
2104  iterator begin() { return InitExprs.begin(); }
2105  iterator end() { return InitExprs.end(); }
2106  reverse_iterator rbegin() { return InitExprs.rbegin(); }
2107  reverse_iterator rend() { return InitExprs.rend(); }
2108};
2109
2110/// @brief Represents a C99 designated initializer expression.
2111///
2112/// A designated initializer expression (C99 6.7.8) contains one or
2113/// more designators (which can be field designators, array
2114/// designators, or GNU array-range designators) followed by an
2115/// expression that initializes the field or element(s) that the
2116/// designators refer to. For example, given:
2117///
2118/// @code
2119/// struct point {
2120///   double x;
2121///   double y;
2122/// };
2123/// struct point ptarray[10] = { [2].y = 1.0, [2].x = 2.0, [0].x = 1.0 };
2124/// @endcode
2125///
2126/// The InitListExpr contains three DesignatedInitExprs, the first of
2127/// which covers @c [2].y=1.0. This DesignatedInitExpr will have two
2128/// designators, one array designator for @c [2] followed by one field
2129/// designator for @c .y. The initalization expression will be 1.0.
2130class DesignatedInitExpr : public Expr {
2131public:
2132  /// \brief Forward declaration of the Designator class.
2133  class Designator;
2134
2135private:
2136  /// The location of the '=' or ':' prior to the actual initializer
2137  /// expression.
2138  SourceLocation EqualOrColonLoc;
2139
2140  /// Whether this designated initializer used the GNU deprecated
2141  /// syntax rather than the C99 '=' syntax.
2142  bool GNUSyntax : 1;
2143
2144  /// The number of designators in this initializer expression.
2145  unsigned NumDesignators : 15;
2146
2147  /// \brief The designators in this designated initialization
2148  /// expression.
2149  Designator *Designators;
2150
2151  /// The number of subexpressions of this initializer expression,
2152  /// which contains both the initializer and any additional
2153  /// expressions used by array and array-range designators.
2154  unsigned NumSubExprs : 16;
2155
2156
2157  DesignatedInitExpr(QualType Ty, unsigned NumDesignators,
2158                     const Designator *Designators,
2159                     SourceLocation EqualOrColonLoc, bool GNUSyntax,
2160                     Expr **IndexExprs, unsigned NumIndexExprs,
2161                     Expr *Init);
2162
2163  explicit DesignatedInitExpr(unsigned NumSubExprs)
2164    : Expr(DesignatedInitExprClass, EmptyShell()),
2165      NumDesignators(0), Designators(0), NumSubExprs(NumSubExprs) { }
2166
2167protected:
2168  virtual void DoDestroy(ASTContext &C);
2169
2170public:
2171  /// A field designator, e.g., ".x".
2172  struct FieldDesignator {
2173    /// Refers to the field that is being initialized. The low bit
2174    /// of this field determines whether this is actually a pointer
2175    /// to an IdentifierInfo (if 1) or a FieldDecl (if 0). When
2176    /// initially constructed, a field designator will store an
2177    /// IdentifierInfo*. After semantic analysis has resolved that
2178    /// name, the field designator will instead store a FieldDecl*.
2179    uintptr_t NameOrField;
2180
2181    /// The location of the '.' in the designated initializer.
2182    unsigned DotLoc;
2183
2184    /// The location of the field name in the designated initializer.
2185    unsigned FieldLoc;
2186  };
2187
2188  /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]".
2189  struct ArrayOrRangeDesignator {
2190    /// Location of the first index expression within the designated
2191    /// initializer expression's list of subexpressions.
2192    unsigned Index;
2193    /// The location of the '[' starting the array range designator.
2194    unsigned LBracketLoc;
2195    /// The location of the ellipsis separating the start and end
2196    /// indices. Only valid for GNU array-range designators.
2197    unsigned EllipsisLoc;
2198    /// The location of the ']' terminating the array range designator.
2199    unsigned RBracketLoc;
2200  };
2201
2202  /// @brief Represents a single C99 designator.
2203  ///
2204  /// @todo This class is infuriatingly similar to clang::Designator,
2205  /// but minor differences (storing indices vs. storing pointers)
2206  /// keep us from reusing it. Try harder, later, to rectify these
2207  /// differences.
2208  class Designator {
2209    /// @brief The kind of designator this describes.
2210    enum {
2211      FieldDesignator,
2212      ArrayDesignator,
2213      ArrayRangeDesignator
2214    } Kind;
2215
2216    union {
2217      /// A field designator, e.g., ".x".
2218      struct FieldDesignator Field;
2219      /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]".
2220      struct ArrayOrRangeDesignator ArrayOrRange;
2221    };
2222    friend class DesignatedInitExpr;
2223
2224  public:
2225    Designator() {}
2226
2227    /// @brief Initializes a field designator.
2228    Designator(const IdentifierInfo *FieldName, SourceLocation DotLoc,
2229               SourceLocation FieldLoc)
2230      : Kind(FieldDesignator) {
2231      Field.NameOrField = reinterpret_cast<uintptr_t>(FieldName) | 0x01;
2232      Field.DotLoc = DotLoc.getRawEncoding();
2233      Field.FieldLoc = FieldLoc.getRawEncoding();
2234    }
2235
2236    /// @brief Initializes an array designator.
2237    Designator(unsigned Index, SourceLocation LBracketLoc,
2238               SourceLocation RBracketLoc)
2239      : Kind(ArrayDesignator) {
2240      ArrayOrRange.Index = Index;
2241      ArrayOrRange.LBracketLoc = LBracketLoc.getRawEncoding();
2242      ArrayOrRange.EllipsisLoc = SourceLocation().getRawEncoding();
2243      ArrayOrRange.RBracketLoc = RBracketLoc.getRawEncoding();
2244    }
2245
2246    /// @brief Initializes a GNU array-range designator.
2247    Designator(unsigned Index, SourceLocation LBracketLoc,
2248               SourceLocation EllipsisLoc, SourceLocation RBracketLoc)
2249      : Kind(ArrayRangeDesignator) {
2250      ArrayOrRange.Index = Index;
2251      ArrayOrRange.LBracketLoc = LBracketLoc.getRawEncoding();
2252      ArrayOrRange.EllipsisLoc = EllipsisLoc.getRawEncoding();
2253      ArrayOrRange.RBracketLoc = RBracketLoc.getRawEncoding();
2254    }
2255
2256    bool isFieldDesignator() const { return Kind == FieldDesignator; }
2257    bool isArrayDesignator() const { return Kind == ArrayDesignator; }
2258    bool isArrayRangeDesignator() const { return Kind == ArrayRangeDesignator; }
2259
2260    IdentifierInfo * getFieldName();
2261
2262    FieldDecl *getField() {
2263      assert(Kind == FieldDesignator && "Only valid on a field designator");
2264      if (Field.NameOrField & 0x01)
2265        return 0;
2266      else
2267        return reinterpret_cast<FieldDecl *>(Field.NameOrField);
2268    }
2269
2270    void setField(FieldDecl *FD) {
2271      assert(Kind == FieldDesignator && "Only valid on a field designator");
2272      Field.NameOrField = reinterpret_cast<uintptr_t>(FD);
2273    }
2274
2275    SourceLocation getDotLoc() const {
2276      assert(Kind == FieldDesignator && "Only valid on a field designator");
2277      return SourceLocation::getFromRawEncoding(Field.DotLoc);
2278    }
2279
2280    SourceLocation getFieldLoc() const {
2281      assert(Kind == FieldDesignator && "Only valid on a field designator");
2282      return SourceLocation::getFromRawEncoding(Field.FieldLoc);
2283    }
2284
2285    SourceLocation getLBracketLoc() const {
2286      assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) &&
2287             "Only valid on an array or array-range designator");
2288      return SourceLocation::getFromRawEncoding(ArrayOrRange.LBracketLoc);
2289    }
2290
2291    SourceLocation getRBracketLoc() const {
2292      assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) &&
2293             "Only valid on an array or array-range designator");
2294      return SourceLocation::getFromRawEncoding(ArrayOrRange.RBracketLoc);
2295    }
2296
2297    SourceLocation getEllipsisLoc() const {
2298      assert(Kind == ArrayRangeDesignator &&
2299             "Only valid on an array-range designator");
2300      return SourceLocation::getFromRawEncoding(ArrayOrRange.EllipsisLoc);
2301    }
2302
2303    unsigned getFirstExprIndex() const {
2304      assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) &&
2305             "Only valid on an array or array-range designator");
2306      return ArrayOrRange.Index;
2307    }
2308
2309    SourceLocation getStartLocation() const {
2310      if (Kind == FieldDesignator)
2311        return getDotLoc().isInvalid()? getFieldLoc() : getDotLoc();
2312      else
2313        return getLBracketLoc();
2314    }
2315  };
2316
2317  static DesignatedInitExpr *Create(ASTContext &C, Designator *Designators,
2318                                    unsigned NumDesignators,
2319                                    Expr **IndexExprs, unsigned NumIndexExprs,
2320                                    SourceLocation EqualOrColonLoc,
2321                                    bool GNUSyntax, Expr *Init);
2322
2323  static DesignatedInitExpr *CreateEmpty(ASTContext &C, unsigned NumIndexExprs);
2324
2325  /// @brief Returns the number of designators in this initializer.
2326  unsigned size() const { return NumDesignators; }
2327
2328  // Iterator access to the designators.
2329  typedef Designator* designators_iterator;
2330  designators_iterator designators_begin() { return Designators; }
2331  designators_iterator designators_end() {
2332    return Designators + NumDesignators;
2333  }
2334
2335  Designator *getDesignator(unsigned Idx) { return &designators_begin()[Idx]; }
2336
2337  void setDesignators(const Designator *Desigs, unsigned NumDesigs);
2338
2339  Expr *getArrayIndex(const Designator& D);
2340  Expr *getArrayRangeStart(const Designator& D);
2341  Expr *getArrayRangeEnd(const Designator& D);
2342
2343  /// @brief Retrieve the location of the '=' that precedes the
2344  /// initializer value itself, if present.
2345  SourceLocation getEqualOrColonLoc() const { return EqualOrColonLoc; }
2346  void setEqualOrColonLoc(SourceLocation L) { EqualOrColonLoc = L; }
2347
2348  /// @brief Determines whether this designated initializer used the
2349  /// deprecated GNU syntax for designated initializers.
2350  bool usesGNUSyntax() const { return GNUSyntax; }
2351  void setGNUSyntax(bool GNU) { GNUSyntax = GNU; }
2352
2353  /// @brief Retrieve the initializer value.
2354  Expr *getInit() const {
2355    return cast<Expr>(*const_cast<DesignatedInitExpr*>(this)->child_begin());
2356  }
2357
2358  void setInit(Expr *init) {
2359    *child_begin() = init;
2360  }
2361
2362  /// \brief Retrieve the total number of subexpressions in this
2363  /// designated initializer expression, including the actual
2364  /// initialized value and any expressions that occur within array
2365  /// and array-range designators.
2366  unsigned getNumSubExprs() const { return NumSubExprs; }
2367
2368  Expr *getSubExpr(unsigned Idx) {
2369    assert(Idx < NumSubExprs && "Subscript out of range");
2370    char* Ptr = static_cast<char*>(static_cast<void *>(this));
2371    Ptr += sizeof(DesignatedInitExpr);
2372    return reinterpret_cast<Expr**>(reinterpret_cast<void**>(Ptr))[Idx];
2373  }
2374
2375  void setSubExpr(unsigned Idx, Expr *E) {
2376    assert(Idx < NumSubExprs && "Subscript out of range");
2377    char* Ptr = static_cast<char*>(static_cast<void *>(this));
2378    Ptr += sizeof(DesignatedInitExpr);
2379    reinterpret_cast<Expr**>(reinterpret_cast<void**>(Ptr))[Idx] = E;
2380  }
2381
2382  /// \brief Replaces the designator at index @p Idx with the series
2383  /// of designators in [First, Last).
2384  void ExpandDesignator(unsigned Idx, const Designator *First,
2385                        const Designator *Last);
2386
2387  virtual SourceRange getSourceRange() const;
2388
2389  static bool classof(const Stmt *T) {
2390    return T->getStmtClass() == DesignatedInitExprClass;
2391  }
2392  static bool classof(const DesignatedInitExpr *) { return true; }
2393
2394  // Iterators
2395  virtual child_iterator child_begin();
2396  virtual child_iterator child_end();
2397};
2398
2399/// \brief Represents an implicitly-generated value initialization of
2400/// an object of a given type.
2401///
2402/// Implicit value initializations occur within semantic initializer
2403/// list expressions (InitListExpr) as placeholders for subobject
2404/// initializations not explicitly specified by the user.
2405///
2406/// \see InitListExpr
2407class ImplicitValueInitExpr : public Expr {
2408public:
2409  explicit ImplicitValueInitExpr(QualType ty)
2410    : Expr(ImplicitValueInitExprClass, ty) { }
2411
2412  /// \brief Construct an empty implicit value initialization.
2413  explicit ImplicitValueInitExpr(EmptyShell Empty)
2414    : Expr(ImplicitValueInitExprClass, Empty) { }
2415
2416  static bool classof(const Stmt *T) {
2417    return T->getStmtClass() == ImplicitValueInitExprClass;
2418  }
2419  static bool classof(const ImplicitValueInitExpr *) { return true; }
2420
2421  virtual SourceRange getSourceRange() const {
2422    return SourceRange();
2423  }
2424
2425  // Iterators
2426  virtual child_iterator child_begin();
2427  virtual child_iterator child_end();
2428};
2429
2430
2431class ParenListExpr : public Expr {
2432  Stmt **Exprs;
2433  unsigned NumExprs;
2434  SourceLocation LParenLoc, RParenLoc;
2435
2436protected:
2437  virtual void DoDestroy(ASTContext& C);
2438
2439public:
2440  ParenListExpr(ASTContext& C, SourceLocation lparenloc, Expr **exprs,
2441                unsigned numexprs, SourceLocation rparenloc);
2442
2443  ~ParenListExpr() {}
2444
2445  /// \brief Build an empty paren list.
2446  //explicit ParenListExpr(EmptyShell Empty) : Expr(ParenListExprClass, Empty) { }
2447
2448  unsigned getNumExprs() const { return NumExprs; }
2449
2450  const Expr* getExpr(unsigned Init) const {
2451    assert(Init < getNumExprs() && "Initializer access out of range!");
2452    return cast_or_null<Expr>(Exprs[Init]);
2453  }
2454
2455  Expr* getExpr(unsigned Init) {
2456    assert(Init < getNumExprs() && "Initializer access out of range!");
2457    return cast_or_null<Expr>(Exprs[Init]);
2458  }
2459
2460  Expr **getExprs() { return reinterpret_cast<Expr **>(Exprs); }
2461
2462  SourceLocation getLParenLoc() const { return LParenLoc; }
2463  SourceLocation getRParenLoc() const { return RParenLoc; }
2464
2465  virtual SourceRange getSourceRange() const {
2466    return SourceRange(LParenLoc, RParenLoc);
2467  }
2468  static bool classof(const Stmt *T) {
2469    return T->getStmtClass() == ParenListExprClass;
2470  }
2471  static bool classof(const ParenListExpr *) { return true; }
2472
2473  // Iterators
2474  virtual child_iterator child_begin();
2475  virtual child_iterator child_end();
2476};
2477
2478
2479//===----------------------------------------------------------------------===//
2480// Clang Extensions
2481//===----------------------------------------------------------------------===//
2482
2483
2484/// ExtVectorElementExpr - This represents access to specific elements of a
2485/// vector, and may occur on the left hand side or right hand side.  For example
2486/// the following is legal:  "V.xy = V.zw" if V is a 4 element extended vector.
2487///
2488/// Note that the base may have either vector or pointer to vector type, just
2489/// like a struct field reference.
2490///
2491class ExtVectorElementExpr : public Expr {
2492  Stmt *Base;
2493  IdentifierInfo *Accessor;
2494  SourceLocation AccessorLoc;
2495public:
2496  ExtVectorElementExpr(QualType ty, Expr *base, IdentifierInfo &accessor,
2497                       SourceLocation loc)
2498    : Expr(ExtVectorElementExprClass, ty),
2499      Base(base), Accessor(&accessor), AccessorLoc(loc) {}
2500
2501  /// \brief Build an empty vector element expression.
2502  explicit ExtVectorElementExpr(EmptyShell Empty)
2503    : Expr(ExtVectorElementExprClass, Empty) { }
2504
2505  const Expr *getBase() const { return cast<Expr>(Base); }
2506  Expr *getBase() { return cast<Expr>(Base); }
2507  void setBase(Expr *E) { Base = E; }
2508
2509  IdentifierInfo &getAccessor() const { return *Accessor; }
2510  void setAccessor(IdentifierInfo *II) { Accessor = II; }
2511
2512  SourceLocation getAccessorLoc() const { return AccessorLoc; }
2513  void setAccessorLoc(SourceLocation L) { AccessorLoc = L; }
2514
2515  /// getNumElements - Get the number of components being selected.
2516  unsigned getNumElements() const;
2517
2518  /// containsDuplicateElements - Return true if any element access is
2519  /// repeated.
2520  bool containsDuplicateElements() const;
2521
2522  /// getEncodedElementAccess - Encode the elements accessed into an llvm
2523  /// aggregate Constant of ConstantInt(s).
2524  void getEncodedElementAccess(llvm::SmallVectorImpl<unsigned> &Elts) const;
2525
2526  virtual SourceRange getSourceRange() const {
2527    return SourceRange(getBase()->getLocStart(), AccessorLoc);
2528  }
2529
2530  /// isArrow - Return true if the base expression is a pointer to vector,
2531  /// return false if the base expression is a vector.
2532  bool isArrow() const;
2533
2534  static bool classof(const Stmt *T) {
2535    return T->getStmtClass() == ExtVectorElementExprClass;
2536  }
2537  static bool classof(const ExtVectorElementExpr *) { return true; }
2538
2539  // Iterators
2540  virtual child_iterator child_begin();
2541  virtual child_iterator child_end();
2542};
2543
2544
2545/// BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
2546/// ^{ statement-body }   or   ^(int arg1, float arg2){ statement-body }
2547class BlockExpr : public Expr {
2548protected:
2549  BlockDecl *TheBlock;
2550  bool HasBlockDeclRefExprs;
2551public:
2552  BlockExpr(BlockDecl *BD, QualType ty, bool hasBlockDeclRefExprs)
2553    : Expr(BlockExprClass, ty),
2554      TheBlock(BD), HasBlockDeclRefExprs(hasBlockDeclRefExprs) {}
2555
2556  /// \brief Build an empty block expression.
2557  explicit BlockExpr(EmptyShell Empty) : Expr(BlockExprClass, Empty) { }
2558
2559  const BlockDecl *getBlockDecl() const { return TheBlock; }
2560  BlockDecl *getBlockDecl() { return TheBlock; }
2561  void setBlockDecl(BlockDecl *BD) { TheBlock = BD; }
2562
2563  // Convenience functions for probing the underlying BlockDecl.
2564  SourceLocation getCaretLocation() const;
2565  const Stmt *getBody() const;
2566  Stmt *getBody();
2567
2568  virtual SourceRange getSourceRange() const {
2569    return SourceRange(getCaretLocation(), getBody()->getLocEnd());
2570  }
2571
2572  /// getFunctionType - Return the underlying function type for this block.
2573  const FunctionType *getFunctionType() const;
2574
2575  /// hasBlockDeclRefExprs - Return true iff the block has BlockDeclRefExpr
2576  /// inside of the block that reference values outside the block.
2577  bool hasBlockDeclRefExprs() const { return HasBlockDeclRefExprs; }
2578  void setHasBlockDeclRefExprs(bool BDRE) { HasBlockDeclRefExprs = BDRE; }
2579
2580  static bool classof(const Stmt *T) {
2581    return T->getStmtClass() == BlockExprClass;
2582  }
2583  static bool classof(const BlockExpr *) { return true; }
2584
2585  // Iterators
2586  virtual child_iterator child_begin();
2587  virtual child_iterator child_end();
2588};
2589
2590/// BlockDeclRefExpr - A reference to a declared variable, function,
2591/// enum, etc.
2592class BlockDeclRefExpr : public Expr {
2593  ValueDecl *D;
2594  SourceLocation Loc;
2595  bool IsByRef : 1;
2596  bool ConstQualAdded : 1;
2597public:
2598  BlockDeclRefExpr(ValueDecl *d, QualType t, SourceLocation l, bool ByRef,
2599                   bool constAdded = false) :
2600       Expr(BlockDeclRefExprClass, t), D(d), Loc(l), IsByRef(ByRef),
2601                                       ConstQualAdded(constAdded) {}
2602
2603  // \brief Build an empty reference to a declared variable in a
2604  // block.
2605  explicit BlockDeclRefExpr(EmptyShell Empty)
2606    : Expr(BlockDeclRefExprClass, Empty) { }
2607
2608  ValueDecl *getDecl() { return D; }
2609  const ValueDecl *getDecl() const { return D; }
2610  void setDecl(ValueDecl *VD) { D = VD; }
2611
2612  SourceLocation getLocation() const { return Loc; }
2613  void setLocation(SourceLocation L) { Loc = L; }
2614
2615  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
2616
2617  bool isByRef() const { return IsByRef; }
2618  void setByRef(bool BR) { IsByRef = BR; }
2619
2620  bool isConstQualAdded() const { return ConstQualAdded; }
2621  void setConstQualAdded(bool C) { ConstQualAdded = C; }
2622
2623  static bool classof(const Stmt *T) {
2624    return T->getStmtClass() == BlockDeclRefExprClass;
2625  }
2626  static bool classof(const BlockDeclRefExpr *) { return true; }
2627
2628  // Iterators
2629  virtual child_iterator child_begin();
2630  virtual child_iterator child_end();
2631};
2632
2633}  // end namespace clang
2634
2635#endif
2636