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