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