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