Expr.h revision 16c5dea6c2d3e4cf529de9d9b37f6a40441acb2c
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===--- Expr.h - Classes for representing expressions ----------*- C++ -*-===//
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//                     The LLVM Compiler Infrastructure
4c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file is distributed under the University of Illinois Open Source
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// License. See LICENSE.TXT for details.
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//===----------------------------------------------------------------------===//
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//  This file defines the Expr interface and subclasses.
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//===----------------------------------------------------------------------===//
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
147dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#ifndef LLVM_CLANG_AST_EXPR_H
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define LLVM_CLANG_AST_EXPR_H
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/AST/APValue.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/AST/Stmt.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/AST/Type.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/AST/DeclAccessPair.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/AST/OperationKinds.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/AST/ASTVector.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/AST/UsuallyTinyPtrVector.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/ADT/APSInt.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/ADT/APFloat.h"
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/ADT/SmallVector.h"
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/ADT/StringRef.h"
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace clang {
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class ASTContext;
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class APValue;
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class Decl;
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class IdentifierInfo;
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class ParmVarDecl;
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class NamedDecl;
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class ValueDecl;
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class BlockDecl;
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class CXXBaseSpecifier;
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class CXXOperatorCallExpr;
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class CXXMemberCallExpr;
427dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  class TemplateArgumentLoc;
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class TemplateArgumentListInfo;
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// \brief A simple array of base specifiers.
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef llvm::SmallVector<CXXBaseSpecifier*, 4> CXXCastPath;
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// Expr - This represents one expression.  Note that Expr's are subclasses of
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// Stmt.  This allows an expression to be transparently used any place a Stmt
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// is required.
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Expr : public Stmt {
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  QualType TR;
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void ANCHOR(); // key function.
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)protected:
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// TypeDependent - Whether this expression is type-dependent
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// (C++ [temp.dep.expr]).
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool TypeDependent : 1;
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// ValueDependent - Whether this expression is value-dependent
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// (C++ [temp.dep.constexpr]).
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool ValueDependent : 1;
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// ValueKind - The value classification of this expression.
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// Only actually used by certain subclasses.
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  unsigned ValueKind : 2;
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum { BitsRemaining = 28 };
70ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
71ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  Expr(StmtClass SC, QualType T, bool TD, bool VD)
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : Stmt(SC), TypeDependent(TD), ValueDependent(VD), ValueKind(0) {
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    setType(T);
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
76ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  /// \brief Construct an empty expression.
77ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  explicit Expr(StmtClass SC, EmptyShell) : Stmt(SC) { }
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)public:
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Increases the reference count for this expression.
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// Invoke the Retain() operation when this expression
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// is being shared by another owner.
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Expr *Retain() {
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Stmt::Retain();
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return this;
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  QualType getType() const { return TR; }
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void setType(QualType t) {
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // In C++, the type of an expression is always adjusted so that it
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // will not have reference type an expression will never have
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // reference type (C++ [expr]p6). Use
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // QualType::getNonReferenceType() to retrieve the non-reference
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // type. Additionally, inspect Expr::isLvalue to determine whether
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // an expression that is adjusted in this manner should be
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // considered an lvalue.
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    assert((t.isNull() || !t->isReferenceType()) &&
997dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch           "Expressions can't have reference type");
100c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
101c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    TR = t;
102c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
103c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// isValueDependent - Determines whether this expression is
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// value-dependent (C++ [temp.dep.constexpr]). For example, the
1067dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  /// array bound of "Chars" in the following example is
1077dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  /// value-dependent.
1087dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  /// @code
1097dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  /// template<int Size, char (&Chars)[Size]> struct meta_string;
1107dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  /// @endcode
1117dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  bool isValueDependent() const { return ValueDependent; }
1127dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1137dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  /// \brief Set whether this expression is value-dependent or not.
1147dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  void setValueDependent(bool VD) { ValueDependent = VD; }
1157dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1167dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  /// isTypeDependent - Determines whether this expression is
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// type-dependent (C++ [temp.dep.expr]), which means that its type
118ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  /// could change from one template instantiation to the next. For
119ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  /// example, the expressions "x" and "x + y" are type-dependent in
120ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  /// the following code, but "y" is not type-dependent:
121ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  /// @code
122ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  /// template<typename T>
123ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  /// void add(T x, int y) {
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///   x + y;
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// }
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// @endcode
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool isTypeDependent() const { return TypeDependent; }
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Set whether this expression is type-dependent or not.
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void setTypeDependent(bool TD) { TypeDependent = TD; }
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// SourceLocation tokens are not useful in isolation - they are low level
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// value objects created/interpreted by SourceManager. We assume AST
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// clients will have a pointer to the respective SourceManager.
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual SourceRange getSourceRange() const = 0;
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// getExprLoc - Return the preferred location for the arrow when diagnosing
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// a problem with a generic expression.
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual SourceLocation getExprLoc() const { return getLocStart(); }
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// isUnusedResultAWarning - Return true if this immediate expression should
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// be warned about if the result is unused.  If so, fill in Loc and Ranges
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// with location to warn on and the source range[s] to report with the
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// warning.
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              SourceRange &R2, ASTContext &Ctx) const;
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// incomplete type other than void. Nonarray expressions that can be lvalues:
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///  - name, where name must be a variable
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///  - e[i]
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///  - (e), where e must be an lvalue
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///  - e.name, where e must be an lvalue
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///  - e->name
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///  - *e, the type of e cannot be a function type
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///  - string-constant
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///  - reference type [C++ [expr]]
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///  - b ? x : y, where x and y are lvalues of suitable types [C++]
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum isLvalueResult {
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LV_Valid,
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LV_NotObjectType,
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LV_IncompleteVoidType,
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LV_DuplicateVectorComponents,
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LV_InvalidExpression,
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LV_MemberFunction,
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LV_SubObjCPropertySetting,
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LV_ClassTemporary
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  isLvalueResult isLvalue(ASTContext &Ctx) const;
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// does not have an incomplete type, does not have a const-qualified type,
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// and if it is a structure or union, does not have any member (including,
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// recursively, any member or element of all contained aggregates or unions)
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// with a const-qualified type.
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \param Loc [in] [out] - A source location which *may* be filled
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// in with the location of the expression making this a
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// non-modifiable lvalue, if specified.
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum isModifiableLvalueResult {
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MLV_Valid,
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MLV_NotObjectType,
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MLV_IncompleteVoidType,
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MLV_DuplicateVectorComponents,
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MLV_InvalidExpression,
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MLV_LValueCast,           // Specialized form of MLV_InvalidExpression.
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MLV_IncompleteType,
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MLV_ConstQualified,
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MLV_ArrayType,
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MLV_NotBlockQualified,
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MLV_ReadonlyProperty,
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MLV_NoSetterProperty,
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MLV_MemberFunction,
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MLV_SubObjCPropertySetting,
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MLV_ClassTemporary
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  isModifiableLvalueResult isModifiableLvalue(ASTContext &Ctx,
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                              SourceLocation *Loc = 0) const;
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief The return type of classify(). Represents the C++0x expression
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///        taxonomy.
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class Classification {
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  public:
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// \brief The various classification results. Most of these mean prvalue.
206c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    enum Kinds {
207c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      CL_LValue,
208c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      CL_XValue,
209c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      CL_Function, // Functions cannot be lvalues in C.
210c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      CL_Void, // Void cannot be an lvalue in C.
211c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      CL_DuplicateVectorComponents, // A vector shuffle with dupes.
212c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      CL_MemberFunction, // An expression referring to a member function
213c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      CL_SubObjCPropertySetting,
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CL_ClassTemporary, // A prvalue of class type
215c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      CL_PRValue // A prvalue for any other reason, of any other type
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    };
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// \brief The results of modification testing.
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    enum ModifiableType {
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CM_Untested, // testModifiable was false.
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CM_Modifiable,
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CM_RValue, // Not modifiable because it's an rvalue
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CM_Function, // Not modifiable because it's a function; C++ only
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CM_LValueCast, // Same as CM_RValue, but indicates GCC cast-as-lvalue ext
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CM_NotBlockQualified, // Not captured in the closure
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CM_NoSetterProperty,// Implicit assignment to ObjC property without setter
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CM_ConstQualified,
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CM_ArrayType,
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CM_IncompleteType
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    };
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  private:
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    friend class Expr;
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned short Kind;
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned short Modifiable;
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    explicit Classification(Kinds k, ModifiableType m)
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      : Kind(k), Modifiable(m)
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    {}
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  public:
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Classification() {}
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Kinds getKind() const { return static_cast<Kinds>(Kind); }
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ModifiableType getModifiable() const {
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      assert(Modifiable != CM_Untested && "Did not test for modifiability.");
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return static_cast<ModifiableType>(Modifiable);
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool isLValue() const { return Kind == CL_LValue; }
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool isXValue() const { return Kind == CL_XValue; }
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool isGLValue() const { return Kind <= CL_XValue; }
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool isPRValue() const { return Kind >= CL_Function; }
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool isRValue() const { return Kind >= CL_XValue; }
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool isModifiable() const { return getModifiable() == CM_Modifiable; }
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Classify - Classify this expression according to the C++0x
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///        expression taxonomy.
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// C++0x defines ([basic.lval]) a new taxonomy of expressions to replace the
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// old lvalue vs rvalue. This function determines the type of expression this
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// is. There are three expression types:
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// - lvalues are classical lvalues as in C++03.
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// - prvalues are equivalent to rvalues in C++03.
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// - xvalues are expressions yielding unnamed rvalue references, e.g. a
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///   function returning an rvalue reference.
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// lvalues and xvalues are collectively referred to as glvalues, while
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// prvalues and xvalues together form rvalues.
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Classification Classify(ASTContext &Ctx) const {
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return ClassifyImpl(Ctx, 0);
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief ClassifyModifiable - Classify this expression according to the
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///        C++0x expression taxonomy, and see if it is valid on the left side
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///        of an assignment.
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// This function extends classify in that it also tests whether the
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// expression is modifiable (C99 6.3.2.1p1).
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \param Loc A source location that might be filled with a relevant location
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///            if the expression is not modifiable.
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Classification ClassifyModifiable(ASTContext &Ctx, SourceLocation &Loc) const{
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return ClassifyImpl(Ctx, &Loc);
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2837dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
2847dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochprivate:
2857dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  Classification ClassifyImpl(ASTContext &Ctx, SourceLocation *Loc) const;
2867dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
2877dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochpublic:
2887dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
2897dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  /// \brief If this expression refers to a bit-field, retrieve the
2907dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  /// declaration of that bit-field.
2917dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  FieldDecl *getBitField();
2927dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
2937dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  const FieldDecl *getBitField() const {
2947dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    return const_cast<Expr*>(this)->getBitField();
2957dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
2967dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
2977dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  /// \brief Returns whether this expression refers to a vector element.
2987dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  bool refersToVectorElement() const;
2997dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
3007dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  /// isKnownToHaveBooleanValue - Return true if this is an integer expression
3017dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  /// that is known to return 0 or 1.  This happens for _Bool/bool expressions
3027dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  /// but also int expressions which are produced by things like comparisons in
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// C.
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool isKnownToHaveBooleanValue() const;
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// isIntegerConstantExpr - Return true if this expression is a valid integer
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// constant expression, and, if so, return its value in Result.  If not a
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// valid i-c-e, return false and fill in Loc (if specified) with the location
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// of the invalid expression.
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             SourceLocation *Loc = 0,
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             bool isEvaluated = true) const;
3132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool isIntegerConstantExpr(ASTContext &Ctx, SourceLocation *Loc = 0) const {
3142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    llvm::APSInt X;
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return isIntegerConstantExpr(X, Ctx, Loc);
3162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// isConstantInitializer - Returns true if this expression is a constant
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// initializer, which can be emitted at compile-time.
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool isConstantInitializer(ASTContext &Ctx, bool ForRef) const;
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// EvalResult is a struct with detailed info about an evaluated expression.
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  struct EvalResult {
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// Val - This is the value the expression can be folded to.
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    APValue Val;
3252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// HasSideEffects - Whether the evaluated expression has side effects.
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// For example, (f() && 0) can be folded, but it still has side effects.
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool HasSideEffects;
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// Diag - If the expression is unfoldable, then Diag contains a note
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// diagnostic indicating why it's not foldable. DiagLoc indicates a caret
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// position for the error, and DiagExpr is the expression that caused
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// the error.
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// If the expression is foldable, but not an integer constant expression,
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// Diag contains a note diagnostic that describes why it isn't an integer
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// constant expression. If the expression *is* an integer constant
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// expression, then Diag will be zero.
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned Diag;
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const Expr *DiagExpr;
3407dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    SourceLocation DiagLoc;
3417dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
3427dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    EvalResult() : HasSideEffects(false), Diag(0), DiagExpr(0) {}
3437dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
3447dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // isGlobalLValue - Return true if the evaluated lvalue expression
3457dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // is global.
3467dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    bool isGlobalLValue() const;
3477dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // hasSideEffects - Return true if the evaluated expression has
3487dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // side effects.
3497dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    bool hasSideEffects() const {
3507dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      return HasSideEffects;
3517dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    }
3527dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  };
3537dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
3547dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  /// Evaluate - Return true if this is a constant which we can fold using
3557dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  /// any crazy technique (that has nothing to do with language standards) that
3567dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  /// we want to.  If this function returns true, it returns the folded constant
3577dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  /// in Result.
3587dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  bool Evaluate(EvalResult &Result, ASTContext &Ctx) const;
3597dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// EvaluateAsBooleanCondition - Return true if this is a constant
3617dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  /// which we we can fold and convert to a boolean condition using
3627dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  /// any crazy technique that we want to.
3637dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  bool EvaluateAsBooleanCondition(bool &Result, ASTContext &Ctx) const;
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3657dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  /// isEvaluatable - Call Evaluate to see if this expression can be constant
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// folded, but discard the result.
3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool isEvaluatable(ASTContext &Ctx) const;
3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// HasSideEffects - This routine returns true for all those expressions
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// which must be evaluated each time and must not be optimized away
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// or evaluated at compile time. Example is a function call, volatile
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// variable read.
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool HasSideEffects(ASTContext &Ctx) const;
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// EvaluateAsInt - Call Evaluate and return the folded integer. This
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// must be called on an expression that constant folds to an integer.
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  llvm::APSInt EvaluateAsInt(ASTContext &Ctx) const;
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// EvaluateAsLValue - Evaluate an expression to see if it's a lvalue
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// with link time known address.
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const;
3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// EvaluateAsLValue - Evaluate an expression to see if it's a lvalue.
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const;
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Enumeration used to describe how \c isNullPointerConstant()
3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// should cope with value-dependent expressions.
3885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum NullPointerConstantValueDependence {
3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// \brief Specifies that the expression should never be value-dependent.
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NPC_NeverValueDependent = 0,
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// \brief Specifies that a value-dependent expression of integral or
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// dependent type should be considered a null pointer constant.
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NPC_ValueDependentIsNull,
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// \brief Specifies that a value-dependent expression should be considered
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// to never be a null pointer constant.
3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NPC_ValueDependentIsNotNull
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4014e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// isNullPointerConstant - C99 6.3.2.3p3 -  Return true if this is either an
4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// integer constant expression with the value zero, or if this is one that is
4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// cast to void*.
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool isNullPointerConstant(ASTContext &Ctx,
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             NullPointerConstantValueDependence NPC) const;
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// isOBJCGCCandidate - Return true if this expression may be used in a read/
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// write barrier.
4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool isOBJCGCCandidate(ASTContext &Ctx) const;
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Result type of CanThrow().
4125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum CanThrowResult {
4135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CT_Cannot,
4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CT_Dependent,
4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CT_Can
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Test if this expression, if evaluated, might throw, according to
4185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///        the rules of C++ [expr.unary.noexcept].
4195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CanThrowResult CanThrow(ASTContext &C) const;
4205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// IgnoreParens - Ignore parentheses.  If this Expr is a ParenExpr, return
4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///  its subexpression.  If that subexpression is also a ParenExpr,
4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///  then this method recursively returns its subexpression, and so forth.
4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///  Otherwise, the method returns the current Expr.
4255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Expr *IgnoreParens();
4265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// IgnoreParenCasts - Ignore parentheses and casts.  Strip off any ParenExpr
4285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// or CastExprs, returning their operand.
4295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Expr *IgnoreParenCasts();
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// IgnoreParenImpCasts - Ignore parentheses and implicit casts.  Strip off any
4325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// ParenExpr or ImplicitCastExprs, returning their operand.
4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Expr *IgnoreParenImpCasts();
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const Expr *IgnoreParenImpCasts() const {
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return const_cast<Expr*>(this)->IgnoreParenImpCasts();
4375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// value (including ptr->int casts of the same size).  Strip off any
4415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// ParenExpr or CastExprs, returning their operand.
4422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Expr *IgnoreParenNoopCasts(ASTContext &Ctx);
4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Determine whether this expression is a default function argument.
4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// Default arguments are implicitly generated in the abstract syntax tree
4475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// by semantic analysis for function calls, object constructions, etc. in
4482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// C++. Default arguments are represented by \c CXXDefaultArgExpr nodes;
4492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// this routine also looks through any implicit casts to determine whether
4505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// the expression is a default argument.
4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool isDefaultArgument() const;
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// \brief Determine whether the result of this expression is a
4542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// temporary object of the given class type.
4555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool isTemporaryObject(ASTContext &Ctx, const CXXRecordDecl *TempTy) const;
4565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const Expr *IgnoreParens() const {
4585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return const_cast<Expr*>(this)->IgnoreParens();
4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const Expr *IgnoreParenCasts() const {
4615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return const_cast<Expr*>(this)->IgnoreParenCasts();
4625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const Expr *IgnoreParenNoopCasts(ASTContext &Ctx) const {
4645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return const_cast<Expr*>(this)->IgnoreParenNoopCasts(Ctx);
4655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static bool hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs);
4685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static bool hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs);
4695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static bool classof(const Stmt *T) {
4715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return T->getStmtClass() >= firstExprConstant &&
4725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           T->getStmtClass() <= lastExprConstant;
4735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static bool classof(const Expr *) { return true; }
4755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
4765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===//
4795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Primary Expressions.
4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===//
4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// \brief Represents the qualifier that may precede a C++ name, e.g., the
4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// "std::" in "std::sort".
4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct NameQualifier {
4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief The nested name specifier.
4865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NestedNameSpecifier *NNS;
4875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief The source range covered by the nested name specifier.
4895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SourceRange Range;
4905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
4915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// \brief Represents an explicit template argument list in C++, e.g.,
4935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// the "<int>" in "sort<int>".
4945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct ExplicitTemplateArgumentList {
4955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief The source location of the left angle bracket ('<');
4965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SourceLocation LAngleLoc;
4975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief The source location of the right angle bracket ('>');
4995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SourceLocation RAngleLoc;
5005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief The number of template arguments in TemplateArgs.
5025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// The actual template arguments (if any) are stored after the
5035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// ExplicitTemplateArgumentList structure.
5045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  unsigned NumTemplateArgs;
5055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Retrieve the template arguments
5075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateArgumentLoc *getTemplateArgs() {
5085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return reinterpret_cast<TemplateArgumentLoc *> (this + 1);
5095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Retrieve the template arguments
5125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const TemplateArgumentLoc *getTemplateArgs() const {
5135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return reinterpret_cast<const TemplateArgumentLoc *> (this + 1);
5145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void initializeFrom(const TemplateArgumentListInfo &List);
5175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void copyInto(TemplateArgumentListInfo &List) const;
5185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static std::size_t sizeFor(unsigned NumTemplateArgs);
5195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static std::size_t sizeFor(const TemplateArgumentListInfo &List);
5205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
5215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// DeclRefExpr - [C99 6.5.1p2] - A reference to a declared variable, function,
5235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// enum, etc.
5245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class DeclRefExpr : public Expr {
5255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum {
5265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Flag on DecoratedD that specifies when this declaration reference
5275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // expression has a C++ nested-name-specifier.
5285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    HasQualifierFlag = 0x01,
5295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Flag on DecoratedD that specifies when this declaration reference
5305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // expression has an explicit C++ template argument list.
5315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    HasExplicitTemplateArgumentListFlag = 0x02
5325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
5335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // DecoratedD - The declaration that we are referencing, plus two bits to
5355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // indicate whether (1) the declaration's name was explicitly qualified and
5365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // (2) the declaration's name was followed by an explicit template
5375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // argument list.
5385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  llvm::PointerIntPair<ValueDecl *, 2> DecoratedD;
5395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Loc - The location of the declaration name itself.
5415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SourceLocation Loc;
5425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// DNLoc - Provides source/type location info for the
5445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// declaration name embedded in DecoratedD.
5455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DeclarationNameLoc DNLoc;
5465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Retrieve the qualifier that preceded the declaration name, if any.
5485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NameQualifier *getNameQualifier() {
5495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if ((DecoratedD.getInt() & HasQualifierFlag) == 0)
5505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return 0;
5515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return reinterpret_cast<NameQualifier *> (this + 1);
5535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Retrieve the qualifier that preceded the member name, if any.
5565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const NameQualifier *getNameQualifier() const {
5575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return const_cast<DeclRefExpr *>(this)->getNameQualifier();
5585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DeclRefExpr(NestedNameSpecifier *Qualifier, SourceRange QualifierRange,
5615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              ValueDecl *D, SourceLocation NameLoc,
5625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              const TemplateArgumentListInfo *TemplateArgs,
5635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              QualType T);
5645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DeclRefExpr(NestedNameSpecifier *Qualifier, SourceRange QualifierRange,
5665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              ValueDecl *D, const DeclarationNameInfo &NameInfo,
5675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              const TemplateArgumentListInfo *TemplateArgs,
5685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              QualType T);
5695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Construct an empty declaration reference expression.
5715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit DeclRefExpr(EmptyShell Empty)
5725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : Expr(DeclRefExprClass, Empty) { }
5735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Computes the type- and value-dependence flags for this
5755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// declaration reference expression.
5765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void computeDependence();
5775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)public:
5795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DeclRefExpr(ValueDecl *d, QualType t, SourceLocation l) :
5805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Expr(DeclRefExprClass, t, false, false), DecoratedD(d, 0), Loc(l) {
5815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    computeDependence();
5825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static DeclRefExpr *Create(ASTContext &Context,
5855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             NestedNameSpecifier *Qualifier,
5865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             SourceRange QualifierRange,
5875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             ValueDecl *D,
5885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             SourceLocation NameLoc,
5895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             QualType T,
5905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             const TemplateArgumentListInfo *TemplateArgs = 0);
5915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static DeclRefExpr *Create(ASTContext &Context,
5935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             NestedNameSpecifier *Qualifier,
5945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             SourceRange QualifierRange,
5955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             ValueDecl *D,
5965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             const DeclarationNameInfo &NameInfo,
5974e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                             QualType T,
5985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             const TemplateArgumentListInfo *TemplateArgs = 0);
5995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Construct an empty declaration reference expression.
6015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static DeclRefExpr *CreateEmpty(ASTContext &Context,
6025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  bool HasQualifier, unsigned NumTemplateArgs);
6035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ValueDecl *getDecl() { return DecoratedD.getPointer(); }
6055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const ValueDecl *getDecl() const { return DecoratedD.getPointer(); }
6065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void setDecl(ValueDecl *NewD) { DecoratedD.setPointer(NewD); }
6075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DeclarationNameInfo getNameInfo() const {
6095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return DeclarationNameInfo(getDecl()->getDeclName(), Loc, DNLoc);
6105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SourceLocation getLocation() const { return Loc; }
6135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void setLocation(SourceLocation L) { Loc = L; }
6145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual SourceRange getSourceRange() const;
6155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Determine whether this declaration reference was preceded by a
6175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// C++ nested-name-specifier, e.g., \c N::foo.
6185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool hasQualifier() const { return DecoratedD.getInt() & HasQualifierFlag; }
6195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief If the name was qualified, retrieves the source range of
6215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// the nested-name-specifier that precedes the name. Otherwise,
6225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// returns an empty source range.
6235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SourceRange getQualifierRange() const {
6245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!hasQualifier())
6255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return SourceRange();
6265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return getNameQualifier()->Range;
6285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief If the name was qualified, retrieves the nested-name-specifier
6315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// that precedes the name. Otherwise, returns NULL.
6325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NestedNameSpecifier *getQualifier() const {
6335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!hasQualifier())
6345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return 0;
6355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return getNameQualifier()->NNS;
6375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool hasExplicitTemplateArgs() const {
6405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return (DecoratedD.getInt() & HasExplicitTemplateArgumentListFlag);
6415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Retrieve the explicit template argument list that followed the
6445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// member template name.
6455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ExplicitTemplateArgumentList &getExplicitTemplateArgs() {
6465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    assert(hasExplicitTemplateArgs());
6475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if ((DecoratedD.getInt() & HasQualifierFlag) == 0)
6495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return *reinterpret_cast<ExplicitTemplateArgumentList *>(this + 1);
6505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return *reinterpret_cast<ExplicitTemplateArgumentList *>(
6525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                      getNameQualifier() + 1);
6535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Retrieve the explicit template argument list that followed the
6565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// member template name.
6575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const ExplicitTemplateArgumentList &getExplicitTemplateArgs() const {
6585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return const_cast<DeclRefExpr *>(this)->getExplicitTemplateArgs();
6595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Retrieves the optional explicit template arguments.
6625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// This points to the same data as getExplicitTemplateArgs(), but
6635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// returns null if there are no explicit template arguments.
6645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const ExplicitTemplateArgumentList *getExplicitTemplateArgsOpt() const {
6655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!hasExplicitTemplateArgs()) return 0;
6665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return &getExplicitTemplateArgs();
6675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Copies the template arguments (if present) into the given
6705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// structure.
6715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const {
6725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (hasExplicitTemplateArgs())
6735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      getExplicitTemplateArgs().copyInto(List);
6745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Retrieve the location of the left angle bracket following the
6775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// member name ('<'), if any.
6785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SourceLocation getLAngleLoc() const {
6797dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    if (!hasExplicitTemplateArgs())
6805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return SourceLocation();
6815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return getExplicitTemplateArgs().LAngleLoc;
6835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Retrieve the template arguments provided as part of this
6865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// template-id.
6875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const TemplateArgumentLoc *getTemplateArgs() const {
6885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!hasExplicitTemplateArgs())
6895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return 0;
6905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return getExplicitTemplateArgs().getTemplateArgs();
6925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Retrieve the number of template arguments provided as part of this
6952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// template-id.
6962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  unsigned getNumTemplateArgs() const {
6975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!hasExplicitTemplateArgs())
6985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return 0;
6995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
700    return getExplicitTemplateArgs().NumTemplateArgs;
701  }
702
703  /// \brief Retrieve the location of the right angle bracket following the
704  /// template arguments ('>').
705  SourceLocation getRAngleLoc() const {
706    if (!hasExplicitTemplateArgs())
707      return SourceLocation();
708
709    return getExplicitTemplateArgs().RAngleLoc;
710  }
711
712  static bool classof(const Stmt *T) {
713    return T->getStmtClass() == DeclRefExprClass;
714  }
715  static bool classof(const DeclRefExpr *) { return true; }
716
717  // Iterators
718  virtual child_iterator child_begin();
719  virtual child_iterator child_end();
720
721  friend class ASTStmtReader;
722  friend class ASTStmtWriter;
723};
724
725/// PredefinedExpr - [C99 6.4.2.2] - A predefined identifier such as __func__.
726class PredefinedExpr : public Expr {
727public:
728  enum IdentType {
729    Func,
730    Function,
731    PrettyFunction,
732    /// PrettyFunctionNoVirtual - The same as PrettyFunction, except that the
733    /// 'virtual' keyword is omitted for virtual member functions.
734    PrettyFunctionNoVirtual
735  };
736
737private:
738  SourceLocation Loc;
739  IdentType Type;
740public:
741  PredefinedExpr(SourceLocation l, QualType type, IdentType IT)
742    : Expr(PredefinedExprClass, type, type->isDependentType(),
743           type->isDependentType()), Loc(l), Type(IT) {}
744
745  /// \brief Construct an empty predefined expression.
746  explicit PredefinedExpr(EmptyShell Empty)
747    : Expr(PredefinedExprClass, Empty) { }
748
749  IdentType getIdentType() const { return Type; }
750  void setIdentType(IdentType IT) { Type = IT; }
751
752  SourceLocation getLocation() const { return Loc; }
753  void setLocation(SourceLocation L) { Loc = L; }
754
755  static std::string ComputeName(IdentType IT, const Decl *CurrentDecl);
756
757  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
758
759  static bool classof(const Stmt *T) {
760    return T->getStmtClass() == PredefinedExprClass;
761  }
762  static bool classof(const PredefinedExpr *) { return true; }
763
764  // Iterators
765  virtual child_iterator child_begin();
766  virtual child_iterator child_end();
767};
768
769/// \brief Used by IntegerLiteral/FloatingLiteral to store the numeric without
770/// leaking memory.
771///
772/// For large floats/integers, APFloat/APInt will allocate memory from the heap
773/// to represent these numbers.  Unfortunately, when we use a BumpPtrAllocator
774/// to allocate IntegerLiteral/FloatingLiteral nodes the memory associated with
775/// the APFloat/APInt values will never get freed. APNumericStorage uses
776/// ASTContext's allocator for memory allocation.
777class APNumericStorage {
778  unsigned BitWidth;
779  union {
780    uint64_t VAL;    ///< Used to store the <= 64 bits integer value.
781    uint64_t *pVal;  ///< Used to store the >64 bits integer value.
782  };
783
784  bool hasAllocation() const { return llvm::APInt::getNumWords(BitWidth) > 1; }
785
786  APNumericStorage(const APNumericStorage&); // do not implement
787  APNumericStorage& operator=(const APNumericStorage&); // do not implement
788
789protected:
790  APNumericStorage() : BitWidth(0), VAL(0) { }
791
792  llvm::APInt getIntValue() const {
793    unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
794    if (NumWords > 1)
795      return llvm::APInt(BitWidth, NumWords, pVal);
796    else
797      return llvm::APInt(BitWidth, VAL);
798  }
799  void setIntValue(ASTContext &C, const llvm::APInt &Val);
800};
801
802class APIntStorage : public APNumericStorage {
803public:
804  llvm::APInt getValue() const { return getIntValue(); }
805  void setValue(ASTContext &C, const llvm::APInt &Val) { setIntValue(C, Val); }
806};
807
808class APFloatStorage : public APNumericStorage {
809public:
810  llvm::APFloat getValue() const { return llvm::APFloat(getIntValue()); }
811  void setValue(ASTContext &C, const llvm::APFloat &Val) {
812    setIntValue(C, Val.bitcastToAPInt());
813  }
814};
815
816class IntegerLiteral : public Expr {
817  APIntStorage Num;
818  SourceLocation Loc;
819
820  /// \brief Construct an empty integer literal.
821  explicit IntegerLiteral(EmptyShell Empty)
822    : Expr(IntegerLiteralClass, Empty) { }
823
824public:
825  // type should be IntTy, LongTy, LongLongTy, UnsignedIntTy, UnsignedLongTy,
826  // or UnsignedLongLongTy
827  IntegerLiteral(ASTContext &C, const llvm::APInt &V,
828                 QualType type, SourceLocation l)
829    : Expr(IntegerLiteralClass, type, false, false), Loc(l) {
830    assert(type->isIntegerType() && "Illegal type in IntegerLiteral");
831    setValue(C, V);
832  }
833
834  // type should be IntTy, LongTy, LongLongTy, UnsignedIntTy, UnsignedLongTy,
835  // or UnsignedLongLongTy
836  static IntegerLiteral *Create(ASTContext &C, const llvm::APInt &V,
837                                QualType type, SourceLocation l);
838  static IntegerLiteral *Create(ASTContext &C, EmptyShell Empty);
839
840  llvm::APInt getValue() const { return Num.getValue(); }
841  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
842
843  /// \brief Retrieve the location of the literal.
844  SourceLocation getLocation() const { return Loc; }
845
846  void setValue(ASTContext &C, const llvm::APInt &Val) { Num.setValue(C, Val); }
847  void setLocation(SourceLocation Location) { Loc = Location; }
848
849  static bool classof(const Stmt *T) {
850    return T->getStmtClass() == IntegerLiteralClass;
851  }
852  static bool classof(const IntegerLiteral *) { return true; }
853
854  // Iterators
855  virtual child_iterator child_begin();
856  virtual child_iterator child_end();
857};
858
859class CharacterLiteral : public Expr {
860  unsigned Value;
861  SourceLocation Loc;
862  bool IsWide;
863public:
864  // type should be IntTy
865  CharacterLiteral(unsigned value, bool iswide, QualType type, SourceLocation l)
866    : Expr(CharacterLiteralClass, type, false, false), Value(value), Loc(l),
867      IsWide(iswide) {
868  }
869
870  /// \brief Construct an empty character literal.
871  CharacterLiteral(EmptyShell Empty) : Expr(CharacterLiteralClass, Empty) { }
872
873  SourceLocation getLocation() const { return Loc; }
874  bool isWide() const { return IsWide; }
875
876  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
877
878  unsigned getValue() const { return Value; }
879
880  void setLocation(SourceLocation Location) { Loc = Location; }
881  void setWide(bool W) { IsWide = W; }
882  void setValue(unsigned Val) { Value = Val; }
883
884  static bool classof(const Stmt *T) {
885    return T->getStmtClass() == CharacterLiteralClass;
886  }
887  static bool classof(const CharacterLiteral *) { return true; }
888
889  // Iterators
890  virtual child_iterator child_begin();
891  virtual child_iterator child_end();
892};
893
894class FloatingLiteral : public Expr {
895  APFloatStorage Num;
896  bool IsExact : 1;
897  SourceLocation Loc;
898
899  FloatingLiteral(ASTContext &C, const llvm::APFloat &V, bool isexact,
900                  QualType Type, SourceLocation L)
901    : Expr(FloatingLiteralClass, Type, false, false),
902      IsExact(isexact), Loc(L) {
903    setValue(C, V);
904  }
905
906  /// \brief Construct an empty floating-point literal.
907  explicit FloatingLiteral(EmptyShell Empty)
908    : Expr(FloatingLiteralClass, Empty), IsExact(false) { }
909
910public:
911  static FloatingLiteral *Create(ASTContext &C, const llvm::APFloat &V,
912                                 bool isexact, QualType Type, SourceLocation L);
913  static FloatingLiteral *Create(ASTContext &C, EmptyShell Empty);
914
915  llvm::APFloat getValue() const { return Num.getValue(); }
916  void setValue(ASTContext &C, const llvm::APFloat &Val) {
917    Num.setValue(C, Val);
918  }
919
920  bool isExact() const { return IsExact; }
921  void setExact(bool E) { IsExact = E; }
922
923  /// getValueAsApproximateDouble - This returns the value as an inaccurate
924  /// double.  Note that this may cause loss of precision, but is useful for
925  /// debugging dumps, etc.
926  double getValueAsApproximateDouble() const;
927
928  SourceLocation getLocation() const { return Loc; }
929  void setLocation(SourceLocation L) { Loc = L; }
930
931  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
932
933  static bool classof(const Stmt *T) {
934    return T->getStmtClass() == FloatingLiteralClass;
935  }
936  static bool classof(const FloatingLiteral *) { return true; }
937
938  // Iterators
939  virtual child_iterator child_begin();
940  virtual child_iterator child_end();
941};
942
943/// ImaginaryLiteral - We support imaginary integer and floating point literals,
944/// like "1.0i".  We represent these as a wrapper around FloatingLiteral and
945/// IntegerLiteral classes.  Instances of this class always have a Complex type
946/// whose element type matches the subexpression.
947///
948class ImaginaryLiteral : public Expr {
949  Stmt *Val;
950public:
951  ImaginaryLiteral(Expr *val, QualType Ty)
952    : Expr(ImaginaryLiteralClass, Ty, false, false), Val(val) {}
953
954  /// \brief Build an empty imaginary literal.
955  explicit ImaginaryLiteral(EmptyShell Empty)
956    : Expr(ImaginaryLiteralClass, Empty) { }
957
958  const Expr *getSubExpr() const { return cast<Expr>(Val); }
959  Expr *getSubExpr() { return cast<Expr>(Val); }
960  void setSubExpr(Expr *E) { Val = E; }
961
962  virtual SourceRange getSourceRange() const { return Val->getSourceRange(); }
963  static bool classof(const Stmt *T) {
964    return T->getStmtClass() == ImaginaryLiteralClass;
965  }
966  static bool classof(const ImaginaryLiteral *) { return true; }
967
968  // Iterators
969  virtual child_iterator child_begin();
970  virtual child_iterator child_end();
971};
972
973/// StringLiteral - This represents a string literal expression, e.g. "foo"
974/// or L"bar" (wide strings).  The actual string is returned by getStrData()
975/// is NOT null-terminated, and the length of the string is determined by
976/// calling getByteLength().  The C type for a string is always a
977/// ConstantArrayType.  In C++, the char type is const qualified, in C it is
978/// not.
979///
980/// Note that strings in C can be formed by concatenation of multiple string
981/// literal pptokens in translation phase #6.  This keeps track of the locations
982/// of each of these pieces.
983///
984/// Strings in C can also be truncated and extended by assigning into arrays,
985/// e.g. with constructs like:
986///   char X[2] = "foobar";
987/// In this case, getByteLength() will return 6, but the string literal will
988/// have type "char[2]".
989class StringLiteral : public Expr {
990  const char *StrData;
991  unsigned ByteLength;
992  bool IsWide;
993  unsigned NumConcatenated;
994  SourceLocation TokLocs[1];
995
996  StringLiteral(QualType Ty) : Expr(StringLiteralClass, Ty, false, false) {}
997
998public:
999  /// This is the "fully general" constructor that allows representation of
1000  /// strings formed from multiple concatenated tokens.
1001  static StringLiteral *Create(ASTContext &C, const char *StrData,
1002                               unsigned ByteLength, bool Wide, QualType Ty,
1003                               const SourceLocation *Loc, unsigned NumStrs);
1004
1005  /// Simple constructor for string literals made from one token.
1006  static StringLiteral *Create(ASTContext &C, const char *StrData,
1007                               unsigned ByteLength,
1008                               bool Wide, QualType Ty, SourceLocation Loc) {
1009    return Create(C, StrData, ByteLength, Wide, Ty, &Loc, 1);
1010  }
1011
1012  /// \brief Construct an empty string literal.
1013  static StringLiteral *CreateEmpty(ASTContext &C, unsigned NumStrs);
1014
1015  llvm::StringRef getString() const {
1016    return llvm::StringRef(StrData, ByteLength);
1017  }
1018
1019  unsigned getByteLength() const { return ByteLength; }
1020
1021  /// \brief Sets the string data to the given string data.
1022  void setString(ASTContext &C, llvm::StringRef Str);
1023
1024  bool isWide() const { return IsWide; }
1025  void setWide(bool W) { IsWide = W; }
1026
1027  bool containsNonAsciiOrNull() const {
1028    llvm::StringRef Str = getString();
1029    for (unsigned i = 0, e = Str.size(); i != e; ++i)
1030      if (!isascii(Str[i]) || !Str[i])
1031        return true;
1032    return false;
1033  }
1034  /// getNumConcatenated - Get the number of string literal tokens that were
1035  /// concatenated in translation phase #6 to form this string literal.
1036  unsigned getNumConcatenated() const { return NumConcatenated; }
1037
1038  SourceLocation getStrTokenLoc(unsigned TokNum) const {
1039    assert(TokNum < NumConcatenated && "Invalid tok number");
1040    return TokLocs[TokNum];
1041  }
1042  void setStrTokenLoc(unsigned TokNum, SourceLocation L) {
1043    assert(TokNum < NumConcatenated && "Invalid tok number");
1044    TokLocs[TokNum] = L;
1045  }
1046
1047  typedef const SourceLocation *tokloc_iterator;
1048  tokloc_iterator tokloc_begin() const { return TokLocs; }
1049  tokloc_iterator tokloc_end() const { return TokLocs+NumConcatenated; }
1050
1051  virtual SourceRange getSourceRange() const {
1052    return SourceRange(TokLocs[0], TokLocs[NumConcatenated-1]);
1053  }
1054  static bool classof(const Stmt *T) {
1055    return T->getStmtClass() == StringLiteralClass;
1056  }
1057  static bool classof(const StringLiteral *) { return true; }
1058
1059  // Iterators
1060  virtual child_iterator child_begin();
1061  virtual child_iterator child_end();
1062};
1063
1064/// ParenExpr - This represents a parethesized expression, e.g. "(1)".  This
1065/// AST node is only formed if full location information is requested.
1066class ParenExpr : public Expr {
1067  SourceLocation L, R;
1068  Stmt *Val;
1069public:
1070  ParenExpr(SourceLocation l, SourceLocation r, Expr *val)
1071    : Expr(ParenExprClass, val->getType(),
1072           val->isTypeDependent(), val->isValueDependent()),
1073      L(l), R(r), Val(val) {}
1074
1075  /// \brief Construct an empty parenthesized expression.
1076  explicit ParenExpr(EmptyShell Empty)
1077    : Expr(ParenExprClass, Empty) { }
1078
1079  const Expr *getSubExpr() const { return cast<Expr>(Val); }
1080  Expr *getSubExpr() { return cast<Expr>(Val); }
1081  void setSubExpr(Expr *E) { Val = E; }
1082
1083  virtual SourceRange getSourceRange() const { return SourceRange(L, R); }
1084
1085  /// \brief Get the location of the left parentheses '('.
1086  SourceLocation getLParen() const { return L; }
1087  void setLParen(SourceLocation Loc) { L = Loc; }
1088
1089  /// \brief Get the location of the right parentheses ')'.
1090  SourceLocation getRParen() const { return R; }
1091  void setRParen(SourceLocation Loc) { R = Loc; }
1092
1093  static bool classof(const Stmt *T) {
1094    return T->getStmtClass() == ParenExprClass;
1095  }
1096  static bool classof(const ParenExpr *) { return true; }
1097
1098  // Iterators
1099  virtual child_iterator child_begin();
1100  virtual child_iterator child_end();
1101};
1102
1103
1104/// UnaryOperator - This represents the unary-expression's (except sizeof and
1105/// alignof), the postinc/postdec operators from postfix-expression, and various
1106/// extensions.
1107///
1108/// Notes on various nodes:
1109///
1110/// Real/Imag - These return the real/imag part of a complex operand.  If
1111///   applied to a non-complex value, the former returns its operand and the
1112///   later returns zero in the type of the operand.
1113///
1114class UnaryOperator : public Expr {
1115public:
1116  typedef UnaryOperatorKind Opcode;
1117
1118private:
1119  unsigned Opc : 5;
1120  SourceLocation Loc;
1121  Stmt *Val;
1122public:
1123
1124  UnaryOperator(Expr *input, Opcode opc, QualType type, SourceLocation l)
1125    : Expr(UnaryOperatorClass, type,
1126           input->isTypeDependent() || type->isDependentType(),
1127           input->isValueDependent()),
1128      Opc(opc), Loc(l), Val(input) {}
1129
1130  /// \brief Build an empty unary operator.
1131  explicit UnaryOperator(EmptyShell Empty)
1132    : Expr(UnaryOperatorClass, Empty), Opc(UO_AddrOf) { }
1133
1134  Opcode getOpcode() const { return static_cast<Opcode>(Opc); }
1135  void setOpcode(Opcode O) { Opc = O; }
1136
1137  Expr *getSubExpr() const { return cast<Expr>(Val); }
1138  void setSubExpr(Expr *E) { Val = E; }
1139
1140  /// getOperatorLoc - Return the location of the operator.
1141  SourceLocation getOperatorLoc() const { return Loc; }
1142  void setOperatorLoc(SourceLocation L) { Loc = L; }
1143
1144  /// isPostfix - Return true if this is a postfix operation, like x++.
1145  static bool isPostfix(Opcode Op) {
1146    return Op == UO_PostInc || Op == UO_PostDec;
1147  }
1148
1149  /// isPostfix - Return true if this is a prefix operation, like --x.
1150  static bool isPrefix(Opcode Op) {
1151    return Op == UO_PreInc || Op == UO_PreDec;
1152  }
1153
1154  bool isPrefix() const { return isPrefix(getOpcode()); }
1155  bool isPostfix() const { return isPostfix(getOpcode()); }
1156  bool isIncrementOp() const {
1157    return Opc == UO_PreInc || Opc == UO_PostInc;
1158  }
1159  bool isIncrementDecrementOp() const {
1160    return Opc <= UO_PreDec;
1161  }
1162  static bool isArithmeticOp(Opcode Op) {
1163    return Op >= UO_Plus && Op <= UO_LNot;
1164  }
1165  bool isArithmeticOp() const { return isArithmeticOp(getOpcode()); }
1166
1167  /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1168  /// corresponds to, e.g. "sizeof" or "[pre]++"
1169  static const char *getOpcodeStr(Opcode Op);
1170
1171  /// \brief Retrieve the unary opcode that corresponds to the given
1172  /// overloaded operator.
1173  static Opcode getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix);
1174
1175  /// \brief Retrieve the overloaded operator kind that corresponds to
1176  /// the given unary opcode.
1177  static OverloadedOperatorKind getOverloadedOperator(Opcode Opc);
1178
1179  virtual SourceRange getSourceRange() const {
1180    if (isPostfix())
1181      return SourceRange(Val->getLocStart(), Loc);
1182    else
1183      return SourceRange(Loc, Val->getLocEnd());
1184  }
1185  virtual SourceLocation getExprLoc() const { return Loc; }
1186
1187  static bool classof(const Stmt *T) {
1188    return T->getStmtClass() == UnaryOperatorClass;
1189  }
1190  static bool classof(const UnaryOperator *) { return true; }
1191
1192  // Iterators
1193  virtual child_iterator child_begin();
1194  virtual child_iterator child_end();
1195};
1196
1197/// OffsetOfExpr - [C99 7.17] - This represents an expression of the form
1198/// offsetof(record-type, member-designator). For example, given:
1199/// @code
1200/// struct S {
1201///   float f;
1202///   double d;
1203/// };
1204/// struct T {
1205///   int i;
1206///   struct S s[10];
1207/// };
1208/// @endcode
1209/// we can represent and evaluate the expression @c offsetof(struct T, s[2].d).
1210
1211class OffsetOfExpr : public Expr {
1212public:
1213  // __builtin_offsetof(type, identifier(.identifier|[expr])*)
1214  class OffsetOfNode {
1215  public:
1216    /// \brief The kind of offsetof node we have.
1217    enum Kind {
1218      /// \brief An index into an array.
1219      Array = 0x00,
1220      /// \brief A field.
1221      Field = 0x01,
1222      /// \brief A field in a dependent type, known only by its name.
1223      Identifier = 0x02,
1224      /// \brief An implicit indirection through a C++ base class, when the
1225      /// field found is in a base class.
1226      Base = 0x03
1227    };
1228
1229  private:
1230    enum { MaskBits = 2, Mask = 0x03 };
1231
1232    /// \brief The source range that covers this part of the designator.
1233    SourceRange Range;
1234
1235    /// \brief The data describing the designator, which comes in three
1236    /// different forms, depending on the lower two bits.
1237    ///   - An unsigned index into the array of Expr*'s stored after this node
1238    ///     in memory, for [constant-expression] designators.
1239    ///   - A FieldDecl*, for references to a known field.
1240    ///   - An IdentifierInfo*, for references to a field with a given name
1241    ///     when the class type is dependent.
1242    ///   - A CXXBaseSpecifier*, for references that look at a field in a
1243    ///     base class.
1244    uintptr_t Data;
1245
1246  public:
1247    /// \brief Create an offsetof node that refers to an array element.
1248    OffsetOfNode(SourceLocation LBracketLoc, unsigned Index,
1249                 SourceLocation RBracketLoc)
1250      : Range(LBracketLoc, RBracketLoc), Data((Index << 2) | Array) { }
1251
1252    /// \brief Create an offsetof node that refers to a field.
1253    OffsetOfNode(SourceLocation DotLoc, FieldDecl *Field,
1254                 SourceLocation NameLoc)
1255      : Range(DotLoc.isValid()? DotLoc : NameLoc, NameLoc),
1256        Data(reinterpret_cast<uintptr_t>(Field) | OffsetOfNode::Field) { }
1257
1258    /// \brief Create an offsetof node that refers to an identifier.
1259    OffsetOfNode(SourceLocation DotLoc, IdentifierInfo *Name,
1260                 SourceLocation NameLoc)
1261      : Range(DotLoc.isValid()? DotLoc : NameLoc, NameLoc),
1262        Data(reinterpret_cast<uintptr_t>(Name) | Identifier) { }
1263
1264    /// \brief Create an offsetof node that refers into a C++ base class.
1265    explicit OffsetOfNode(const CXXBaseSpecifier *Base)
1266      : Range(), Data(reinterpret_cast<uintptr_t>(Base) | OffsetOfNode::Base) {}
1267
1268    /// \brief Determine what kind of offsetof node this is.
1269    Kind getKind() const {
1270      return static_cast<Kind>(Data & Mask);
1271    }
1272
1273    /// \brief For an array element node, returns the index into the array
1274    /// of expressions.
1275    unsigned getArrayExprIndex() const {
1276      assert(getKind() == Array);
1277      return Data >> 2;
1278    }
1279
1280    /// \brief For a field offsetof node, returns the field.
1281    FieldDecl *getField() const {
1282      assert(getKind() == Field);
1283      return reinterpret_cast<FieldDecl *>(Data & ~(uintptr_t)Mask);
1284    }
1285
1286    /// \brief For a field or identifier offsetof node, returns the name of
1287    /// the field.
1288    IdentifierInfo *getFieldName() const;
1289
1290    /// \brief For a base class node, returns the base specifier.
1291    CXXBaseSpecifier *getBase() const {
1292      assert(getKind() == Base);
1293      return reinterpret_cast<CXXBaseSpecifier *>(Data & ~(uintptr_t)Mask);
1294    }
1295
1296    /// \brief Retrieve the source range that covers this offsetof node.
1297    ///
1298    /// For an array element node, the source range contains the locations of
1299    /// the square brackets. For a field or identifier node, the source range
1300    /// contains the location of the period (if there is one) and the
1301    /// identifier.
1302    SourceRange getRange() const { return Range; }
1303  };
1304
1305private:
1306
1307  SourceLocation OperatorLoc, RParenLoc;
1308  // Base type;
1309  TypeSourceInfo *TSInfo;
1310  // Number of sub-components (i.e. instances of OffsetOfNode).
1311  unsigned NumComps;
1312  // Number of sub-expressions (i.e. array subscript expressions).
1313  unsigned NumExprs;
1314
1315  OffsetOfExpr(ASTContext &C, QualType type,
1316               SourceLocation OperatorLoc, TypeSourceInfo *tsi,
1317               OffsetOfNode* compsPtr, unsigned numComps,
1318               Expr** exprsPtr, unsigned numExprs,
1319               SourceLocation RParenLoc);
1320
1321  explicit OffsetOfExpr(unsigned numComps, unsigned numExprs)
1322    : Expr(OffsetOfExprClass, EmptyShell()),
1323      TSInfo(0), NumComps(numComps), NumExprs(numExprs) {}
1324
1325public:
1326
1327  static OffsetOfExpr *Create(ASTContext &C, QualType type,
1328                              SourceLocation OperatorLoc, TypeSourceInfo *tsi,
1329                              OffsetOfNode* compsPtr, unsigned numComps,
1330                              Expr** exprsPtr, unsigned numExprs,
1331                              SourceLocation RParenLoc);
1332
1333  static OffsetOfExpr *CreateEmpty(ASTContext &C,
1334                                   unsigned NumComps, unsigned NumExprs);
1335
1336  /// getOperatorLoc - Return the location of the operator.
1337  SourceLocation getOperatorLoc() const { return OperatorLoc; }
1338  void setOperatorLoc(SourceLocation L) { OperatorLoc = L; }
1339
1340  /// \brief Return the location of the right parentheses.
1341  SourceLocation getRParenLoc() const { return RParenLoc; }
1342  void setRParenLoc(SourceLocation R) { RParenLoc = R; }
1343
1344  TypeSourceInfo *getTypeSourceInfo() const {
1345    return TSInfo;
1346  }
1347  void setTypeSourceInfo(TypeSourceInfo *tsi) {
1348    TSInfo = tsi;
1349  }
1350
1351  const OffsetOfNode &getComponent(unsigned Idx) {
1352    assert(Idx < NumComps && "Subscript out of range");
1353    return reinterpret_cast<OffsetOfNode *> (this + 1)[Idx];
1354  }
1355
1356  void setComponent(unsigned Idx, OffsetOfNode ON) {
1357    assert(Idx < NumComps && "Subscript out of range");
1358    reinterpret_cast<OffsetOfNode *> (this + 1)[Idx] = ON;
1359  }
1360
1361  unsigned getNumComponents() const {
1362    return NumComps;
1363  }
1364
1365  Expr* getIndexExpr(unsigned Idx) {
1366    assert(Idx < NumExprs && "Subscript out of range");
1367    return reinterpret_cast<Expr **>(
1368                    reinterpret_cast<OffsetOfNode *>(this+1) + NumComps)[Idx];
1369  }
1370
1371  void setIndexExpr(unsigned Idx, Expr* E) {
1372    assert(Idx < NumComps && "Subscript out of range");
1373    reinterpret_cast<Expr **>(
1374                reinterpret_cast<OffsetOfNode *>(this+1) + NumComps)[Idx] = E;
1375  }
1376
1377  unsigned getNumExpressions() const {
1378    return NumExprs;
1379  }
1380
1381  virtual SourceRange getSourceRange() const {
1382    return SourceRange(OperatorLoc, RParenLoc);
1383  }
1384
1385  static bool classof(const Stmt *T) {
1386    return T->getStmtClass() == OffsetOfExprClass;
1387  }
1388
1389  static bool classof(const OffsetOfExpr *) { return true; }
1390
1391  // Iterators
1392  virtual child_iterator child_begin();
1393  virtual child_iterator child_end();
1394};
1395
1396/// SizeOfAlignOfExpr - [C99 6.5.3.4] - This is for sizeof/alignof, both of
1397/// types and expressions.
1398class SizeOfAlignOfExpr : public Expr {
1399  bool isSizeof : 1;  // true if sizeof, false if alignof.
1400  bool isType : 1;    // true if operand is a type, false if an expression
1401  union {
1402    TypeSourceInfo *Ty;
1403    Stmt *Ex;
1404  } Argument;
1405  SourceLocation OpLoc, RParenLoc;
1406
1407public:
1408  SizeOfAlignOfExpr(bool issizeof, TypeSourceInfo *TInfo,
1409                    QualType resultType, SourceLocation op,
1410                    SourceLocation rp) :
1411      Expr(SizeOfAlignOfExprClass, resultType,
1412           false, // Never type-dependent (C++ [temp.dep.expr]p3).
1413           // Value-dependent if the argument is type-dependent.
1414           TInfo->getType()->isDependentType()),
1415      isSizeof(issizeof), isType(true), OpLoc(op), RParenLoc(rp) {
1416    Argument.Ty = TInfo;
1417  }
1418
1419  SizeOfAlignOfExpr(bool issizeof, Expr *E,
1420                    QualType resultType, SourceLocation op,
1421                    SourceLocation rp) :
1422      Expr(SizeOfAlignOfExprClass, resultType,
1423           false, // Never type-dependent (C++ [temp.dep.expr]p3).
1424           // Value-dependent if the argument is type-dependent.
1425           E->isTypeDependent()),
1426      isSizeof(issizeof), isType(false), OpLoc(op), RParenLoc(rp) {
1427    Argument.Ex = E;
1428  }
1429
1430  /// \brief Construct an empty sizeof/alignof expression.
1431  explicit SizeOfAlignOfExpr(EmptyShell Empty)
1432    : Expr(SizeOfAlignOfExprClass, Empty) { }
1433
1434  bool isSizeOf() const { return isSizeof; }
1435  void setSizeof(bool S) { isSizeof = S; }
1436
1437  bool isArgumentType() const { return isType; }
1438  QualType getArgumentType() const {
1439    return getArgumentTypeInfo()->getType();
1440  }
1441  TypeSourceInfo *getArgumentTypeInfo() const {
1442    assert(isArgumentType() && "calling getArgumentType() when arg is expr");
1443    return Argument.Ty;
1444  }
1445  Expr *getArgumentExpr() {
1446    assert(!isArgumentType() && "calling getArgumentExpr() when arg is type");
1447    return static_cast<Expr*>(Argument.Ex);
1448  }
1449  const Expr *getArgumentExpr() const {
1450    return const_cast<SizeOfAlignOfExpr*>(this)->getArgumentExpr();
1451  }
1452
1453  void setArgument(Expr *E) { Argument.Ex = E; isType = false; }
1454  void setArgument(TypeSourceInfo *TInfo) {
1455    Argument.Ty = TInfo;
1456    isType = true;
1457  }
1458
1459  /// Gets the argument type, or the type of the argument expression, whichever
1460  /// is appropriate.
1461  QualType getTypeOfArgument() const {
1462    return isArgumentType() ? getArgumentType() : getArgumentExpr()->getType();
1463  }
1464
1465  SourceLocation getOperatorLoc() const { return OpLoc; }
1466  void setOperatorLoc(SourceLocation L) { OpLoc = L; }
1467
1468  SourceLocation getRParenLoc() const { return RParenLoc; }
1469  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1470
1471  virtual SourceRange getSourceRange() const {
1472    return SourceRange(OpLoc, RParenLoc);
1473  }
1474
1475  static bool classof(const Stmt *T) {
1476    return T->getStmtClass() == SizeOfAlignOfExprClass;
1477  }
1478  static bool classof(const SizeOfAlignOfExpr *) { return true; }
1479
1480  // Iterators
1481  virtual child_iterator child_begin();
1482  virtual child_iterator child_end();
1483};
1484
1485//===----------------------------------------------------------------------===//
1486// Postfix Operators.
1487//===----------------------------------------------------------------------===//
1488
1489/// ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting.
1490class ArraySubscriptExpr : public Expr {
1491  enum { LHS, RHS, END_EXPR=2 };
1492  Stmt* SubExprs[END_EXPR];
1493  SourceLocation RBracketLoc;
1494public:
1495  ArraySubscriptExpr(Expr *lhs, Expr *rhs, QualType t,
1496                     SourceLocation rbracketloc)
1497  : Expr(ArraySubscriptExprClass, t,
1498         lhs->isTypeDependent() || rhs->isTypeDependent(),
1499         lhs->isValueDependent() || rhs->isValueDependent()),
1500    RBracketLoc(rbracketloc) {
1501    SubExprs[LHS] = lhs;
1502    SubExprs[RHS] = rhs;
1503  }
1504
1505  /// \brief Create an empty array subscript expression.
1506  explicit ArraySubscriptExpr(EmptyShell Shell)
1507    : Expr(ArraySubscriptExprClass, Shell) { }
1508
1509  /// An array access can be written A[4] or 4[A] (both are equivalent).
1510  /// - getBase() and getIdx() always present the normalized view: A[4].
1511  ///    In this case getBase() returns "A" and getIdx() returns "4".
1512  /// - getLHS() and getRHS() present the syntactic view. e.g. for
1513  ///    4[A] getLHS() returns "4".
1514  /// Note: Because vector element access is also written A[4] we must
1515  /// predicate the format conversion in getBase and getIdx only on the
1516  /// the type of the RHS, as it is possible for the LHS to be a vector of
1517  /// integer type
1518  Expr *getLHS() { return cast<Expr>(SubExprs[LHS]); }
1519  const Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); }
1520  void setLHS(Expr *E) { SubExprs[LHS] = E; }
1521
1522  Expr *getRHS() { return cast<Expr>(SubExprs[RHS]); }
1523  const Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); }
1524  void setRHS(Expr *E) { SubExprs[RHS] = E; }
1525
1526  Expr *getBase() {
1527    return cast<Expr>(getRHS()->getType()->isIntegerType() ? getLHS():getRHS());
1528  }
1529
1530  const Expr *getBase() const {
1531    return cast<Expr>(getRHS()->getType()->isIntegerType() ? getLHS():getRHS());
1532  }
1533
1534  Expr *getIdx() {
1535    return cast<Expr>(getRHS()->getType()->isIntegerType() ? getRHS():getLHS());
1536  }
1537
1538  const Expr *getIdx() const {
1539    return cast<Expr>(getRHS()->getType()->isIntegerType() ? getRHS():getLHS());
1540  }
1541
1542  virtual SourceRange getSourceRange() const {
1543    return SourceRange(getLHS()->getLocStart(), RBracketLoc);
1544  }
1545
1546  SourceLocation getRBracketLoc() const { return RBracketLoc; }
1547  void setRBracketLoc(SourceLocation L) { RBracketLoc = L; }
1548
1549  virtual SourceLocation getExprLoc() const { return getBase()->getExprLoc(); }
1550
1551  static bool classof(const Stmt *T) {
1552    return T->getStmtClass() == ArraySubscriptExprClass;
1553  }
1554  static bool classof(const ArraySubscriptExpr *) { return true; }
1555
1556  // Iterators
1557  virtual child_iterator child_begin();
1558  virtual child_iterator child_end();
1559};
1560
1561
1562/// CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
1563/// CallExpr itself represents a normal function call, e.g., "f(x, 2)",
1564/// while its subclasses may represent alternative syntax that (semantically)
1565/// results in a function call. For example, CXXOperatorCallExpr is
1566/// a subclass for overloaded operator calls that use operator syntax, e.g.,
1567/// "str1 + str2" to resolve to a function call.
1568class CallExpr : public Expr {
1569  enum { FN=0, ARGS_START=1 };
1570  Stmt **SubExprs;
1571  unsigned NumArgs;
1572  SourceLocation RParenLoc;
1573
1574protected:
1575  // This version of the constructor is for derived classes.
1576  CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args, unsigned numargs,
1577           QualType t, SourceLocation rparenloc);
1578
1579public:
1580  CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs, QualType t,
1581           SourceLocation rparenloc);
1582
1583  /// \brief Build an empty call expression.
1584  CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty);
1585
1586  const Expr *getCallee() const { return cast<Expr>(SubExprs[FN]); }
1587  Expr *getCallee() { return cast<Expr>(SubExprs[FN]); }
1588  void setCallee(Expr *F) { SubExprs[FN] = F; }
1589
1590  Decl *getCalleeDecl();
1591  const Decl *getCalleeDecl() const {
1592    return const_cast<CallExpr*>(this)->getCalleeDecl();
1593  }
1594
1595  /// \brief If the callee is a FunctionDecl, return it. Otherwise return 0.
1596  FunctionDecl *getDirectCallee();
1597  const FunctionDecl *getDirectCallee() const {
1598    return const_cast<CallExpr*>(this)->getDirectCallee();
1599  }
1600
1601  /// getNumArgs - Return the number of actual arguments to this call.
1602  ///
1603  unsigned getNumArgs() const { return NumArgs; }
1604
1605  /// getArg - Return the specified argument.
1606  Expr *getArg(unsigned Arg) {
1607    assert(Arg < NumArgs && "Arg access out of range!");
1608    return cast<Expr>(SubExprs[Arg+ARGS_START]);
1609  }
1610  const Expr *getArg(unsigned Arg) const {
1611    assert(Arg < NumArgs && "Arg access out of range!");
1612    return cast<Expr>(SubExprs[Arg+ARGS_START]);
1613  }
1614
1615  /// setArg - Set the specified argument.
1616  void setArg(unsigned Arg, Expr *ArgExpr) {
1617    assert(Arg < NumArgs && "Arg access out of range!");
1618    SubExprs[Arg+ARGS_START] = ArgExpr;
1619  }
1620
1621  /// setNumArgs - This changes the number of arguments present in this call.
1622  /// Any orphaned expressions are deleted by this, and any new operands are set
1623  /// to null.
1624  void setNumArgs(ASTContext& C, unsigned NumArgs);
1625
1626  typedef ExprIterator arg_iterator;
1627  typedef ConstExprIterator const_arg_iterator;
1628
1629  arg_iterator arg_begin() { return SubExprs+ARGS_START; }
1630  arg_iterator arg_end() { return SubExprs+ARGS_START+getNumArgs(); }
1631  const_arg_iterator arg_begin() const { return SubExprs+ARGS_START; }
1632  const_arg_iterator arg_end() const { return SubExprs+ARGS_START+getNumArgs();}
1633
1634  /// getNumCommas - Return the number of commas that must have been present in
1635  /// this function call.
1636  unsigned getNumCommas() const { return NumArgs ? NumArgs - 1 : 0; }
1637
1638  /// isBuiltinCall - If this is a call to a builtin, return the builtin ID.  If
1639  /// not, return 0.
1640  unsigned isBuiltinCall(ASTContext &Context) const;
1641
1642  /// getCallReturnType - Get the return type of the call expr. This is not
1643  /// always the type of the expr itself, if the return type is a reference
1644  /// type.
1645  QualType getCallReturnType() const;
1646
1647  SourceLocation getRParenLoc() const { return RParenLoc; }
1648  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1649
1650  virtual SourceRange getSourceRange() const {
1651    return SourceRange(getCallee()->getLocStart(), RParenLoc);
1652  }
1653
1654  static bool classof(const Stmt *T) {
1655    return T->getStmtClass() >= firstCallExprConstant &&
1656           T->getStmtClass() <= lastCallExprConstant;
1657  }
1658  static bool classof(const CallExpr *) { return true; }
1659
1660  // Iterators
1661  virtual child_iterator child_begin();
1662  virtual child_iterator child_end();
1663};
1664
1665/// MemberExpr - [C99 6.5.2.3] Structure and Union Members.  X->F and X.F.
1666///
1667class MemberExpr : public Expr {
1668  /// Extra data stored in some member expressions.
1669  struct MemberNameQualifier : public NameQualifier {
1670    DeclAccessPair FoundDecl;
1671  };
1672
1673  /// Base - the expression for the base pointer or structure references.  In
1674  /// X.F, this is "X".
1675  Stmt *Base;
1676
1677  /// MemberDecl - This is the decl being referenced by the field/member name.
1678  /// In X.F, this is the decl referenced by F.
1679  ValueDecl *MemberDecl;
1680
1681  /// MemberLoc - This is the location of the member name.
1682  SourceLocation MemberLoc;
1683
1684  /// MemberDNLoc - Provides source/type location info for the
1685  /// declaration name embedded in MemberDecl.
1686  DeclarationNameLoc MemberDNLoc;
1687
1688  /// IsArrow - True if this is "X->F", false if this is "X.F".
1689  bool IsArrow : 1;
1690
1691  /// \brief True if this member expression used a nested-name-specifier to
1692  /// refer to the member, e.g., "x->Base::f", or found its member via a using
1693  /// declaration.  When true, a MemberNameQualifier
1694  /// structure is allocated immediately after the MemberExpr.
1695  bool HasQualifierOrFoundDecl : 1;
1696
1697  /// \brief True if this member expression specified a template argument list
1698  /// explicitly, e.g., x->f<int>. When true, an ExplicitTemplateArgumentList
1699  /// structure (and its TemplateArguments) are allocated immediately after
1700  /// the MemberExpr or, if the member expression also has a qualifier, after
1701  /// the MemberNameQualifier structure.
1702  bool HasExplicitTemplateArgumentList : 1;
1703
1704  /// \brief Retrieve the qualifier that preceded the member name, if any.
1705  MemberNameQualifier *getMemberQualifier() {
1706    assert(HasQualifierOrFoundDecl);
1707    return reinterpret_cast<MemberNameQualifier *> (this + 1);
1708  }
1709
1710  /// \brief Retrieve the qualifier that preceded the member name, if any.
1711  const MemberNameQualifier *getMemberQualifier() const {
1712    return const_cast<MemberExpr *>(this)->getMemberQualifier();
1713  }
1714
1715public:
1716  MemberExpr(Expr *base, bool isarrow, ValueDecl *memberdecl,
1717             const DeclarationNameInfo &NameInfo, QualType ty)
1718    : Expr(MemberExprClass, ty,
1719           base->isTypeDependent(), base->isValueDependent()),
1720      Base(base), MemberDecl(memberdecl), MemberLoc(NameInfo.getLoc()),
1721      MemberDNLoc(NameInfo.getInfo()), IsArrow(isarrow),
1722      HasQualifierOrFoundDecl(false), HasExplicitTemplateArgumentList(false) {
1723    assert(memberdecl->getDeclName() == NameInfo.getName());
1724  }
1725
1726  // NOTE: this constructor should be used only when it is known that
1727  // the member name can not provide additional syntactic info
1728  // (i.e., source locations for C++ operator names or type source info
1729  // for constructors, destructors and conversion oeprators).
1730  MemberExpr(Expr *base, bool isarrow, ValueDecl *memberdecl,
1731             SourceLocation l, QualType ty)
1732    : Expr(MemberExprClass, ty,
1733           base->isTypeDependent(), base->isValueDependent()),
1734      Base(base), MemberDecl(memberdecl), MemberLoc(l), MemberDNLoc(),
1735      IsArrow(isarrow),
1736      HasQualifierOrFoundDecl(false), HasExplicitTemplateArgumentList(false) {}
1737
1738  static MemberExpr *Create(ASTContext &C, Expr *base, bool isarrow,
1739                            NestedNameSpecifier *qual, SourceRange qualrange,
1740                            ValueDecl *memberdecl, DeclAccessPair founddecl,
1741                            DeclarationNameInfo MemberNameInfo,
1742                            const TemplateArgumentListInfo *targs,
1743                            QualType ty);
1744
1745  void setBase(Expr *E) { Base = E; }
1746  Expr *getBase() const { return cast<Expr>(Base); }
1747
1748  /// \brief Retrieve the member declaration to which this expression refers.
1749  ///
1750  /// The returned declaration will either be a FieldDecl or (in C++)
1751  /// a CXXMethodDecl.
1752  ValueDecl *getMemberDecl() const { return MemberDecl; }
1753  void setMemberDecl(ValueDecl *D) { MemberDecl = D; }
1754
1755  /// \brief Retrieves the declaration found by lookup.
1756  DeclAccessPair getFoundDecl() const {
1757    if (!HasQualifierOrFoundDecl)
1758      return DeclAccessPair::make(getMemberDecl(),
1759                                  getMemberDecl()->getAccess());
1760    return getMemberQualifier()->FoundDecl;
1761  }
1762
1763  /// \brief Determines whether this member expression actually had
1764  /// a C++ nested-name-specifier prior to the name of the member, e.g.,
1765  /// x->Base::foo.
1766  bool hasQualifier() const { return getQualifier() != 0; }
1767
1768  /// \brief If the member name was qualified, retrieves the source range of
1769  /// the nested-name-specifier that precedes the member name. Otherwise,
1770  /// returns an empty source range.
1771  SourceRange getQualifierRange() const {
1772    if (!HasQualifierOrFoundDecl)
1773      return SourceRange();
1774
1775    return getMemberQualifier()->Range;
1776  }
1777
1778  /// \brief If the member name was qualified, retrieves the
1779  /// nested-name-specifier that precedes the member name. Otherwise, returns
1780  /// NULL.
1781  NestedNameSpecifier *getQualifier() const {
1782    if (!HasQualifierOrFoundDecl)
1783      return 0;
1784
1785    return getMemberQualifier()->NNS;
1786  }
1787
1788  /// \brief Determines whether this member expression actually had a C++
1789  /// template argument list explicitly specified, e.g., x.f<int>.
1790  bool hasExplicitTemplateArgs() const {
1791    return HasExplicitTemplateArgumentList;
1792  }
1793
1794  /// \brief Copies the template arguments (if present) into the given
1795  /// structure.
1796  void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const {
1797    if (hasExplicitTemplateArgs())
1798      getExplicitTemplateArgs().copyInto(List);
1799  }
1800
1801  /// \brief Retrieve the explicit template argument list that
1802  /// follow the member template name.  This must only be called on an
1803  /// expression with explicit template arguments.
1804  ExplicitTemplateArgumentList &getExplicitTemplateArgs() {
1805    assert(HasExplicitTemplateArgumentList);
1806    if (!HasQualifierOrFoundDecl)
1807      return *reinterpret_cast<ExplicitTemplateArgumentList *>(this + 1);
1808
1809    return *reinterpret_cast<ExplicitTemplateArgumentList *>(
1810                                                      getMemberQualifier() + 1);
1811  }
1812
1813  /// \brief Retrieve the explicit template argument list that
1814  /// followed the member template name.  This must only be called on
1815  /// an expression with explicit template arguments.
1816  const ExplicitTemplateArgumentList &getExplicitTemplateArgs() const {
1817    return const_cast<MemberExpr *>(this)->getExplicitTemplateArgs();
1818  }
1819
1820  /// \brief Retrieves the optional explicit template arguments.
1821  /// This points to the same data as getExplicitTemplateArgs(), but
1822  /// returns null if there are no explicit template arguments.
1823  const ExplicitTemplateArgumentList *getOptionalExplicitTemplateArgs() const {
1824    if (!hasExplicitTemplateArgs()) return 0;
1825    return &getExplicitTemplateArgs();
1826  }
1827
1828  /// \brief Retrieve the location of the left angle bracket following the
1829  /// member name ('<'), if any.
1830  SourceLocation getLAngleLoc() const {
1831    if (!HasExplicitTemplateArgumentList)
1832      return SourceLocation();
1833
1834    return getExplicitTemplateArgs().LAngleLoc;
1835  }
1836
1837  /// \brief Retrieve the template arguments provided as part of this
1838  /// template-id.
1839  const TemplateArgumentLoc *getTemplateArgs() const {
1840    if (!HasExplicitTemplateArgumentList)
1841      return 0;
1842
1843    return getExplicitTemplateArgs().getTemplateArgs();
1844  }
1845
1846  /// \brief Retrieve the number of template arguments provided as part of this
1847  /// template-id.
1848  unsigned getNumTemplateArgs() const {
1849    if (!HasExplicitTemplateArgumentList)
1850      return 0;
1851
1852    return getExplicitTemplateArgs().NumTemplateArgs;
1853  }
1854
1855  /// \brief Retrieve the location of the right angle bracket following the
1856  /// template arguments ('>').
1857  SourceLocation getRAngleLoc() const {
1858    if (!HasExplicitTemplateArgumentList)
1859      return SourceLocation();
1860
1861    return getExplicitTemplateArgs().RAngleLoc;
1862  }
1863
1864  /// \brief Retrieve the member declaration name info.
1865  DeclarationNameInfo getMemberNameInfo() const {
1866    return DeclarationNameInfo(MemberDecl->getDeclName(),
1867                               MemberLoc, MemberDNLoc);
1868  }
1869
1870  bool isArrow() const { return IsArrow; }
1871  void setArrow(bool A) { IsArrow = A; }
1872
1873  /// getMemberLoc - Return the location of the "member", in X->F, it is the
1874  /// location of 'F'.
1875  SourceLocation getMemberLoc() const { return MemberLoc; }
1876  void setMemberLoc(SourceLocation L) { MemberLoc = L; }
1877
1878  virtual SourceRange getSourceRange() const {
1879    // If we have an implicit base (like a C++ implicit this),
1880    // make sure not to return its location
1881    SourceLocation EndLoc = (HasExplicitTemplateArgumentList)
1882      ? getRAngleLoc() : getMemberNameInfo().getEndLoc();
1883
1884    SourceLocation BaseLoc = getBase()->getLocStart();
1885    if (BaseLoc.isInvalid())
1886      return SourceRange(MemberLoc, EndLoc);
1887    return SourceRange(BaseLoc, EndLoc);
1888  }
1889
1890  virtual SourceLocation getExprLoc() const { return MemberLoc; }
1891
1892  static bool classof(const Stmt *T) {
1893    return T->getStmtClass() == MemberExprClass;
1894  }
1895  static bool classof(const MemberExpr *) { return true; }
1896
1897  // Iterators
1898  virtual child_iterator child_begin();
1899  virtual child_iterator child_end();
1900};
1901
1902/// CompoundLiteralExpr - [C99 6.5.2.5]
1903///
1904class CompoundLiteralExpr : public Expr {
1905  /// LParenLoc - If non-null, this is the location of the left paren in a
1906  /// compound literal like "(int){4}".  This can be null if this is a
1907  /// synthesized compound expression.
1908  SourceLocation LParenLoc;
1909
1910  /// The type as written.  This can be an incomplete array type, in
1911  /// which case the actual expression type will be different.
1912  TypeSourceInfo *TInfo;
1913  Stmt *Init;
1914  bool FileScope;
1915public:
1916  // FIXME: Can compound literals be value-dependent?
1917  CompoundLiteralExpr(SourceLocation lparenloc, TypeSourceInfo *tinfo,
1918                      QualType T, Expr *init, bool fileScope)
1919    : Expr(CompoundLiteralExprClass, T,
1920           tinfo->getType()->isDependentType(), false),
1921      LParenLoc(lparenloc), TInfo(tinfo), Init(init), FileScope(fileScope) {}
1922
1923  /// \brief Construct an empty compound literal.
1924  explicit CompoundLiteralExpr(EmptyShell Empty)
1925    : Expr(CompoundLiteralExprClass, Empty) { }
1926
1927  const Expr *getInitializer() const { return cast<Expr>(Init); }
1928  Expr *getInitializer() { return cast<Expr>(Init); }
1929  void setInitializer(Expr *E) { Init = E; }
1930
1931  bool isFileScope() const { return FileScope; }
1932  void setFileScope(bool FS) { FileScope = FS; }
1933
1934  SourceLocation getLParenLoc() const { return LParenLoc; }
1935  void setLParenLoc(SourceLocation L) { LParenLoc = L; }
1936
1937  TypeSourceInfo *getTypeSourceInfo() const { return TInfo; }
1938  void setTypeSourceInfo(TypeSourceInfo* tinfo) { TInfo = tinfo; }
1939
1940  virtual SourceRange getSourceRange() const {
1941    // FIXME: Init should never be null.
1942    if (!Init)
1943      return SourceRange();
1944    if (LParenLoc.isInvalid())
1945      return Init->getSourceRange();
1946    return SourceRange(LParenLoc, Init->getLocEnd());
1947  }
1948
1949  static bool classof(const Stmt *T) {
1950    return T->getStmtClass() == CompoundLiteralExprClass;
1951  }
1952  static bool classof(const CompoundLiteralExpr *) { return true; }
1953
1954  // Iterators
1955  virtual child_iterator child_begin();
1956  virtual child_iterator child_end();
1957};
1958
1959/// CastExpr - Base class for type casts, including both implicit
1960/// casts (ImplicitCastExpr) and explicit casts that have some
1961/// representation in the source code (ExplicitCastExpr's derived
1962/// classes).
1963class CastExpr : public Expr {
1964public:
1965  typedef clang::CastKind CastKind;
1966
1967private:
1968  unsigned Kind : 5;
1969  unsigned BasePathSize : BitsRemaining - 5;
1970  Stmt *Op;
1971
1972  void CheckBasePath() const {
1973#ifndef NDEBUG
1974    switch (getCastKind()) {
1975    case CK_DerivedToBase:
1976    case CK_UncheckedDerivedToBase:
1977    case CK_DerivedToBaseMemberPointer:
1978    case CK_BaseToDerived:
1979    case CK_BaseToDerivedMemberPointer:
1980      assert(!path_empty() && "Cast kind should have a base path!");
1981      break;
1982
1983    // These should not have an inheritance path.
1984    case CK_Unknown:
1985    case CK_BitCast:
1986    case CK_LValueBitCast:
1987    case CK_NoOp:
1988    case CK_Dynamic:
1989    case CK_ToUnion:
1990    case CK_ArrayToPointerDecay:
1991    case CK_FunctionToPointerDecay:
1992    case CK_NullToMemberPointer:
1993    case CK_UserDefinedConversion:
1994    case CK_ConstructorConversion:
1995    case CK_IntegralToPointer:
1996    case CK_PointerToIntegral:
1997    case CK_ToVoid:
1998    case CK_VectorSplat:
1999    case CK_IntegralCast:
2000    case CK_IntegralToFloating:
2001    case CK_FloatingToIntegral:
2002    case CK_FloatingCast:
2003    case CK_MemberPointerToBoolean:
2004    case CK_AnyPointerToObjCPointerCast:
2005    case CK_AnyPointerToBlockPointerCast:
2006    case CK_ObjCObjectLValueCast:
2007      assert(path_empty() && "Cast kind should not have a base path!");
2008      break;
2009    }
2010#endif
2011  }
2012
2013  const CXXBaseSpecifier * const *path_buffer() const {
2014    return const_cast<CastExpr*>(this)->path_buffer();
2015  }
2016  CXXBaseSpecifier **path_buffer();
2017
2018protected:
2019  CastExpr(StmtClass SC, QualType ty, const CastKind kind, Expr *op,
2020           unsigned BasePathSize) :
2021    Expr(SC, ty,
2022         // Cast expressions are type-dependent if the type is
2023         // dependent (C++ [temp.dep.expr]p3).
2024         ty->isDependentType(),
2025         // Cast expressions are value-dependent if the type is
2026         // dependent or if the subexpression is value-dependent.
2027         ty->isDependentType() || (op && op->isValueDependent())),
2028    Kind(kind), BasePathSize(BasePathSize), Op(op) {
2029    CheckBasePath();
2030  }
2031
2032  /// \brief Construct an empty cast.
2033  CastExpr(StmtClass SC, EmptyShell Empty, unsigned BasePathSize)
2034    : Expr(SC, Empty), BasePathSize(BasePathSize) { }
2035
2036public:
2037  CastKind getCastKind() const { return static_cast<CastKind>(Kind); }
2038  void setCastKind(CastKind K) { Kind = K; }
2039  const char *getCastKindName() const;
2040
2041  Expr *getSubExpr() { return cast<Expr>(Op); }
2042  const Expr *getSubExpr() const { return cast<Expr>(Op); }
2043  void setSubExpr(Expr *E) { Op = E; }
2044
2045  /// \brief Retrieve the cast subexpression as it was written in the source
2046  /// code, looking through any implicit casts or other intermediate nodes
2047  /// introduced by semantic analysis.
2048  Expr *getSubExprAsWritten();
2049  const Expr *getSubExprAsWritten() const {
2050    return const_cast<CastExpr *>(this)->getSubExprAsWritten();
2051  }
2052
2053  typedef CXXBaseSpecifier **path_iterator;
2054  typedef const CXXBaseSpecifier * const *path_const_iterator;
2055  bool path_empty() const { return BasePathSize == 0; }
2056  unsigned path_size() const { return BasePathSize; }
2057  path_iterator path_begin() { return path_buffer(); }
2058  path_iterator path_end() { return path_buffer() + path_size(); }
2059  path_const_iterator path_begin() const { return path_buffer(); }
2060  path_const_iterator path_end() const { return path_buffer() + path_size(); }
2061
2062  void setCastPath(const CXXCastPath &Path);
2063
2064  static bool classof(const Stmt *T) {
2065    return T->getStmtClass() >= firstCastExprConstant &&
2066           T->getStmtClass() <= lastCastExprConstant;
2067  }
2068  static bool classof(const CastExpr *) { return true; }
2069
2070  // Iterators
2071  virtual child_iterator child_begin();
2072  virtual child_iterator child_end();
2073};
2074
2075/// ImplicitCastExpr - Allows us to explicitly represent implicit type
2076/// conversions, which have no direct representation in the original
2077/// source code. For example: converting T[]->T*, void f()->void
2078/// (*f)(), float->double, short->int, etc.
2079///
2080/// In C, implicit casts always produce rvalues. However, in C++, an
2081/// implicit cast whose result is being bound to a reference will be
2082/// an lvalue or xvalue. For example:
2083///
2084/// @code
2085/// class Base { };
2086/// class Derived : public Base { };
2087/// Derived &&ref();
2088/// void f(Derived d) {
2089///   Base& b = d; // initializer is an ImplicitCastExpr
2090///                // to an lvalue of type Base
2091///   Base&& r = ref(); // initializer is an ImplicitCastExpr
2092///                     // to an xvalue of type Base
2093/// }
2094/// @endcode
2095class ImplicitCastExpr : public CastExpr {
2096private:
2097  ImplicitCastExpr(QualType ty, CastKind kind, Expr *op,
2098                   unsigned BasePathLength, ExprValueKind VK)
2099    : CastExpr(ImplicitCastExprClass, ty, kind, op, BasePathLength) {
2100    ValueKind = VK;
2101  }
2102
2103  /// \brief Construct an empty implicit cast.
2104  explicit ImplicitCastExpr(EmptyShell Shell, unsigned PathSize)
2105    : CastExpr(ImplicitCastExprClass, Shell, PathSize) { }
2106
2107public:
2108  enum OnStack_t { OnStack };
2109  ImplicitCastExpr(OnStack_t _, QualType ty, CastKind kind, Expr *op,
2110                   ExprValueKind VK)
2111    : CastExpr(ImplicitCastExprClass, ty, kind, op, 0) {
2112    ValueKind = VK;
2113  }
2114
2115  static ImplicitCastExpr *Create(ASTContext &Context, QualType T,
2116                                  CastKind Kind, Expr *Operand,
2117                                  const CXXCastPath *BasePath,
2118                                  ExprValueKind Cat);
2119
2120  static ImplicitCastExpr *CreateEmpty(ASTContext &Context, unsigned PathSize);
2121
2122  virtual SourceRange getSourceRange() const {
2123    return getSubExpr()->getSourceRange();
2124  }
2125
2126  /// getValueKind - The value kind that this cast produces.
2127  ExprValueKind getValueKind() const {
2128    return static_cast<ExprValueKind>(ValueKind);
2129  }
2130
2131  /// setValueKind - Set the value kind this cast produces.
2132  void setValueKind(ExprValueKind Cat) { ValueKind = Cat; }
2133
2134  static bool classof(const Stmt *T) {
2135    return T->getStmtClass() == ImplicitCastExprClass;
2136  }
2137  static bool classof(const ImplicitCastExpr *) { return true; }
2138};
2139
2140/// ExplicitCastExpr - An explicit cast written in the source
2141/// code.
2142///
2143/// This class is effectively an abstract class, because it provides
2144/// the basic representation of an explicitly-written cast without
2145/// specifying which kind of cast (C cast, functional cast, static
2146/// cast, etc.) was written; specific derived classes represent the
2147/// particular style of cast and its location information.
2148///
2149/// Unlike implicit casts, explicit cast nodes have two different
2150/// types: the type that was written into the source code, and the
2151/// actual type of the expression as determined by semantic
2152/// analysis. These types may differ slightly. For example, in C++ one
2153/// can cast to a reference type, which indicates that the resulting
2154/// expression will be an lvalue or xvalue. The reference type, however,
2155/// will not be used as the type of the expression.
2156class ExplicitCastExpr : public CastExpr {
2157  /// TInfo - Source type info for the (written) type
2158  /// this expression is casting to.
2159  TypeSourceInfo *TInfo;
2160
2161protected:
2162  ExplicitCastExpr(StmtClass SC, QualType exprTy, CastKind kind,
2163                   Expr *op, unsigned PathSize, TypeSourceInfo *writtenTy)
2164    : CastExpr(SC, exprTy, kind, op, PathSize), TInfo(writtenTy) {}
2165
2166  /// \brief Construct an empty explicit cast.
2167  ExplicitCastExpr(StmtClass SC, EmptyShell Shell, unsigned PathSize)
2168    : CastExpr(SC, Shell, PathSize) { }
2169
2170public:
2171  /// getTypeInfoAsWritten - Returns the type source info for the type
2172  /// that this expression is casting to.
2173  TypeSourceInfo *getTypeInfoAsWritten() const { return TInfo; }
2174  void setTypeInfoAsWritten(TypeSourceInfo *writtenTy) { TInfo = writtenTy; }
2175
2176  /// getTypeAsWritten - Returns the type that this expression is
2177  /// casting to, as written in the source code.
2178  QualType getTypeAsWritten() const { return TInfo->getType(); }
2179
2180  static bool classof(const Stmt *T) {
2181     return T->getStmtClass() >= firstExplicitCastExprConstant &&
2182            T->getStmtClass() <= lastExplicitCastExprConstant;
2183  }
2184  static bool classof(const ExplicitCastExpr *) { return true; }
2185};
2186
2187/// CStyleCastExpr - An explicit cast in C (C99 6.5.4) or a C-style
2188/// cast in C++ (C++ [expr.cast]), which uses the syntax
2189/// (Type)expr. For example: @c (int)f.
2190class CStyleCastExpr : public ExplicitCastExpr {
2191  SourceLocation LPLoc; // the location of the left paren
2192  SourceLocation RPLoc; // the location of the right paren
2193
2194  CStyleCastExpr(QualType exprTy, CastKind kind, Expr *op,
2195                 unsigned PathSize, TypeSourceInfo *writtenTy,
2196                 SourceLocation l, SourceLocation r)
2197    : ExplicitCastExpr(CStyleCastExprClass, exprTy, kind, op, PathSize,
2198                       writtenTy), LPLoc(l), RPLoc(r) {}
2199
2200  /// \brief Construct an empty C-style explicit cast.
2201  explicit CStyleCastExpr(EmptyShell Shell, unsigned PathSize)
2202    : ExplicitCastExpr(CStyleCastExprClass, Shell, PathSize) { }
2203
2204public:
2205  static CStyleCastExpr *Create(ASTContext &Context, QualType T, CastKind K,
2206                                Expr *Op, const CXXCastPath *BasePath,
2207                                TypeSourceInfo *WrittenTy, SourceLocation L,
2208                                SourceLocation R);
2209
2210  static CStyleCastExpr *CreateEmpty(ASTContext &Context, unsigned PathSize);
2211
2212  SourceLocation getLParenLoc() const { return LPLoc; }
2213  void setLParenLoc(SourceLocation L) { LPLoc = L; }
2214
2215  SourceLocation getRParenLoc() const { return RPLoc; }
2216  void setRParenLoc(SourceLocation L) { RPLoc = L; }
2217
2218  virtual SourceRange getSourceRange() const {
2219    return SourceRange(LPLoc, getSubExpr()->getSourceRange().getEnd());
2220  }
2221  static bool classof(const Stmt *T) {
2222    return T->getStmtClass() == CStyleCastExprClass;
2223  }
2224  static bool classof(const CStyleCastExpr *) { return true; }
2225};
2226
2227/// \brief A builtin binary operation expression such as "x + y" or "x <= y".
2228///
2229/// This expression node kind describes a builtin binary operation,
2230/// such as "x + y" for integer values "x" and "y". The operands will
2231/// already have been converted to appropriate types (e.g., by
2232/// performing promotions or conversions).
2233///
2234/// In C++, where operators may be overloaded, a different kind of
2235/// expression node (CXXOperatorCallExpr) is used to express the
2236/// invocation of an overloaded operator with operator syntax. Within
2237/// a C++ template, whether BinaryOperator or CXXOperatorCallExpr is
2238/// used to store an expression "x + y" depends on the subexpressions
2239/// for x and y. If neither x or y is type-dependent, and the "+"
2240/// operator resolves to a built-in operation, BinaryOperator will be
2241/// used to express the computation (x and y may still be
2242/// value-dependent). If either x or y is type-dependent, or if the
2243/// "+" resolves to an overloaded operator, CXXOperatorCallExpr will
2244/// be used to express the computation.
2245class BinaryOperator : public Expr {
2246public:
2247  typedef BinaryOperatorKind Opcode;
2248
2249private:
2250  unsigned Opc : 6;
2251  SourceLocation OpLoc;
2252
2253  enum { LHS, RHS, END_EXPR };
2254  Stmt* SubExprs[END_EXPR];
2255public:
2256
2257  BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy,
2258                 SourceLocation opLoc)
2259    : Expr(BinaryOperatorClass, ResTy,
2260           lhs->isTypeDependent() || rhs->isTypeDependent(),
2261           lhs->isValueDependent() || rhs->isValueDependent()),
2262      Opc(opc), OpLoc(opLoc) {
2263    SubExprs[LHS] = lhs;
2264    SubExprs[RHS] = rhs;
2265    assert(!isCompoundAssignmentOp() &&
2266           "Use ArithAssignBinaryOperator for compound assignments");
2267  }
2268
2269  /// \brief Construct an empty binary operator.
2270  explicit BinaryOperator(EmptyShell Empty)
2271    : Expr(BinaryOperatorClass, Empty), Opc(BO_Comma) { }
2272
2273  SourceLocation getOperatorLoc() const { return OpLoc; }
2274  void setOperatorLoc(SourceLocation L) { OpLoc = L; }
2275
2276  Opcode getOpcode() const { return static_cast<Opcode>(Opc); }
2277  void setOpcode(Opcode O) { Opc = O; }
2278
2279  Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); }
2280  void setLHS(Expr *E) { SubExprs[LHS] = E; }
2281  Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); }
2282  void setRHS(Expr *E) { SubExprs[RHS] = E; }
2283
2284  virtual SourceRange getSourceRange() const {
2285    return SourceRange(getLHS()->getLocStart(), getRHS()->getLocEnd());
2286  }
2287
2288  /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
2289  /// corresponds to, e.g. "<<=".
2290  static const char *getOpcodeStr(Opcode Op);
2291
2292  const char *getOpcodeStr() const { return getOpcodeStr(getOpcode()); }
2293
2294  /// \brief Retrieve the binary opcode that corresponds to the given
2295  /// overloaded operator.
2296  static Opcode getOverloadedOpcode(OverloadedOperatorKind OO);
2297
2298  /// \brief Retrieve the overloaded operator kind that corresponds to
2299  /// the given binary opcode.
2300  static OverloadedOperatorKind getOverloadedOperator(Opcode Opc);
2301
2302  /// predicates to categorize the respective opcodes.
2303  bool isPtrMemOp() const { return Opc == BO_PtrMemD || Opc == BO_PtrMemI; }
2304  bool isMultiplicativeOp() const { return Opc >= BO_Mul && Opc <= BO_Rem; }
2305  static bool isAdditiveOp(Opcode Opc) { return Opc == BO_Add || Opc==BO_Sub; }
2306  bool isAdditiveOp() const { return isAdditiveOp(getOpcode()); }
2307  static bool isShiftOp(Opcode Opc) { return Opc == BO_Shl || Opc == BO_Shr; }
2308  bool isShiftOp() const { return isShiftOp(getOpcode()); }
2309
2310  static bool isBitwiseOp(Opcode Opc) { return Opc >= BO_And && Opc <= BO_Or; }
2311  bool isBitwiseOp() const { return isBitwiseOp(getOpcode()); }
2312
2313  static bool isRelationalOp(Opcode Opc) { return Opc >= BO_LT && Opc<=BO_GE; }
2314  bool isRelationalOp() const { return isRelationalOp(getOpcode()); }
2315
2316  static bool isEqualityOp(Opcode Opc) { return Opc == BO_EQ || Opc == BO_NE; }
2317  bool isEqualityOp() const { return isEqualityOp(getOpcode()); }
2318
2319  static bool isComparisonOp(Opcode Opc) { return Opc >= BO_LT && Opc<=BO_NE; }
2320  bool isComparisonOp() const { return isComparisonOp(getOpcode()); }
2321
2322  static bool isLogicalOp(Opcode Opc) { return Opc == BO_LAnd || Opc==BO_LOr; }
2323  bool isLogicalOp() const { return isLogicalOp(getOpcode()); }
2324
2325  bool isAssignmentOp() const { return Opc >= BO_Assign && Opc <= BO_OrAssign; }
2326  bool isCompoundAssignmentOp() const {
2327    return Opc > BO_Assign && Opc <= BO_OrAssign;
2328  }
2329  bool isShiftAssignOp() const {
2330    return Opc == BO_ShlAssign || Opc == BO_ShrAssign;
2331  }
2332
2333  static bool classof(const Stmt *S) {
2334    return S->getStmtClass() >= firstBinaryOperatorConstant &&
2335           S->getStmtClass() <= lastBinaryOperatorConstant;
2336  }
2337  static bool classof(const BinaryOperator *) { return true; }
2338
2339  // Iterators
2340  virtual child_iterator child_begin();
2341  virtual child_iterator child_end();
2342
2343protected:
2344  BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy,
2345                 SourceLocation opLoc, bool dead)
2346    : Expr(CompoundAssignOperatorClass, ResTy,
2347           lhs->isTypeDependent() || rhs->isTypeDependent(),
2348           lhs->isValueDependent() || rhs->isValueDependent()),
2349      Opc(opc), OpLoc(opLoc) {
2350    SubExprs[LHS] = lhs;
2351    SubExprs[RHS] = rhs;
2352  }
2353
2354  BinaryOperator(StmtClass SC, EmptyShell Empty)
2355    : Expr(SC, Empty), Opc(BO_MulAssign) { }
2356};
2357
2358/// CompoundAssignOperator - For compound assignments (e.g. +=), we keep
2359/// track of the type the operation is performed in.  Due to the semantics of
2360/// these operators, the operands are promoted, the aritmetic performed, an
2361/// implicit conversion back to the result type done, then the assignment takes
2362/// place.  This captures the intermediate type which the computation is done
2363/// in.
2364class CompoundAssignOperator : public BinaryOperator {
2365  QualType ComputationLHSType;
2366  QualType ComputationResultType;
2367public:
2368  CompoundAssignOperator(Expr *lhs, Expr *rhs, Opcode opc,
2369                         QualType ResType, QualType CompLHSType,
2370                         QualType CompResultType,
2371                         SourceLocation OpLoc)
2372    : BinaryOperator(lhs, rhs, opc, ResType, OpLoc, true),
2373      ComputationLHSType(CompLHSType),
2374      ComputationResultType(CompResultType) {
2375    assert(isCompoundAssignmentOp() &&
2376           "Only should be used for compound assignments");
2377  }
2378
2379  /// \brief Build an empty compound assignment operator expression.
2380  explicit CompoundAssignOperator(EmptyShell Empty)
2381    : BinaryOperator(CompoundAssignOperatorClass, Empty) { }
2382
2383  // The two computation types are the type the LHS is converted
2384  // to for the computation and the type of the result; the two are
2385  // distinct in a few cases (specifically, int+=ptr and ptr-=ptr).
2386  QualType getComputationLHSType() const { return ComputationLHSType; }
2387  void setComputationLHSType(QualType T) { ComputationLHSType = T; }
2388
2389  QualType getComputationResultType() const { return ComputationResultType; }
2390  void setComputationResultType(QualType T) { ComputationResultType = T; }
2391
2392  static bool classof(const CompoundAssignOperator *) { return true; }
2393  static bool classof(const Stmt *S) {
2394    return S->getStmtClass() == CompoundAssignOperatorClass;
2395  }
2396};
2397
2398/// ConditionalOperator - The ?: operator.  Note that LHS may be null when the
2399/// GNU "missing LHS" extension is in use.
2400///
2401class ConditionalOperator : public Expr {
2402  enum { COND, LHS, RHS, END_EXPR };
2403  Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides.
2404  Stmt* Save;
2405  SourceLocation QuestionLoc, ColonLoc;
2406public:
2407  ConditionalOperator(Expr *cond, SourceLocation QLoc, Expr *lhs,
2408                      SourceLocation CLoc, Expr *rhs, Expr *save, QualType t)
2409    : Expr(ConditionalOperatorClass, t,
2410           // FIXME: the type of the conditional operator doesn't
2411           // depend on the type of the conditional, but the standard
2412           // seems to imply that it could. File a bug!
2413           ((lhs && lhs->isTypeDependent()) || (rhs && rhs->isTypeDependent())),
2414           (cond->isValueDependent() ||
2415            (lhs && lhs->isValueDependent()) ||
2416            (rhs && rhs->isValueDependent()))),
2417      QuestionLoc(QLoc),
2418      ColonLoc(CLoc) {
2419    SubExprs[COND] = cond;
2420    SubExprs[LHS] = lhs;
2421    SubExprs[RHS] = rhs;
2422    Save = save;
2423  }
2424
2425  /// \brief Build an empty conditional operator.
2426  explicit ConditionalOperator(EmptyShell Empty)
2427    : Expr(ConditionalOperatorClass, Empty) { }
2428
2429  // getCond - Return the expression representing the condition for
2430  //  the ?: operator.
2431  Expr *getCond() const { return cast<Expr>(SubExprs[COND]); }
2432  void setCond(Expr *E) { SubExprs[COND] = E; }
2433
2434  // getTrueExpr - Return the subexpression representing the value of the ?:
2435  //  expression if the condition evaluates to true.
2436  Expr *getTrueExpr() const {
2437    return cast<Expr>(SubExprs[LHS]);
2438  }
2439
2440  // getFalseExpr - Return the subexpression representing the value of the ?:
2441  // expression if the condition evaluates to false. This is the same as getRHS.
2442  Expr *getFalseExpr() const { return cast<Expr>(SubExprs[RHS]); }
2443
2444  // getSaveExpr - In most cases this value will be null. Except a GCC extension
2445  // allows the left subexpression to be omitted, and instead of that condition
2446  // be returned. e.g: x ?: y is shorthand for x ? x : y, except that the
2447  // expression "x" is only evaluated once. Under this senario, this function
2448  // returns the original, non-converted condition expression for the ?:operator
2449  Expr *getSaveExpr() const { return Save? cast<Expr>(Save) : (Expr*)0; }
2450
2451  Expr *getLHS() const { return Save ? 0 : cast<Expr>(SubExprs[LHS]); }
2452  void setLHS(Expr *E) { SubExprs[LHS] = E; }
2453
2454  Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); }
2455  void setRHS(Expr *E) { SubExprs[RHS] = E; }
2456
2457  Expr *getSAVE() const { return Save? cast<Expr>(Save) : (Expr*)0; }
2458  void setSAVE(Expr *E) { Save = E; }
2459
2460  SourceLocation getQuestionLoc() const { return QuestionLoc; }
2461  void setQuestionLoc(SourceLocation L) { QuestionLoc = L; }
2462
2463  SourceLocation getColonLoc() const { return ColonLoc; }
2464  void setColonLoc(SourceLocation L) { ColonLoc = L; }
2465
2466  virtual SourceRange getSourceRange() const {
2467    return SourceRange(getCond()->getLocStart(), getRHS()->getLocEnd());
2468  }
2469  static bool classof(const Stmt *T) {
2470    return T->getStmtClass() == ConditionalOperatorClass;
2471  }
2472  static bool classof(const ConditionalOperator *) { return true; }
2473
2474  // Iterators
2475  virtual child_iterator child_begin();
2476  virtual child_iterator child_end();
2477};
2478
2479/// AddrLabelExpr - The GNU address of label extension, representing &&label.
2480class AddrLabelExpr : public Expr {
2481  SourceLocation AmpAmpLoc, LabelLoc;
2482  LabelStmt *Label;
2483public:
2484  AddrLabelExpr(SourceLocation AALoc, SourceLocation LLoc, LabelStmt *L,
2485                QualType t)
2486    : Expr(AddrLabelExprClass, t, false, false),
2487      AmpAmpLoc(AALoc), LabelLoc(LLoc), Label(L) {}
2488
2489  /// \brief Build an empty address of a label expression.
2490  explicit AddrLabelExpr(EmptyShell Empty)
2491    : Expr(AddrLabelExprClass, Empty) { }
2492
2493  SourceLocation getAmpAmpLoc() const { return AmpAmpLoc; }
2494  void setAmpAmpLoc(SourceLocation L) { AmpAmpLoc = L; }
2495  SourceLocation getLabelLoc() const { return LabelLoc; }
2496  void setLabelLoc(SourceLocation L) { LabelLoc = L; }
2497
2498  virtual SourceRange getSourceRange() const {
2499    return SourceRange(AmpAmpLoc, LabelLoc);
2500  }
2501
2502  LabelStmt *getLabel() const { return Label; }
2503  void setLabel(LabelStmt *S) { Label = S; }
2504
2505  static bool classof(const Stmt *T) {
2506    return T->getStmtClass() == AddrLabelExprClass;
2507  }
2508  static bool classof(const AddrLabelExpr *) { return true; }
2509
2510  // Iterators
2511  virtual child_iterator child_begin();
2512  virtual child_iterator child_end();
2513};
2514
2515/// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}).
2516/// The StmtExpr contains a single CompoundStmt node, which it evaluates and
2517/// takes the value of the last subexpression.
2518class StmtExpr : public Expr {
2519  Stmt *SubStmt;
2520  SourceLocation LParenLoc, RParenLoc;
2521public:
2522  // FIXME: Does type-dependence need to be computed differently?
2523  StmtExpr(CompoundStmt *substmt, QualType T,
2524           SourceLocation lp, SourceLocation rp) :
2525    Expr(StmtExprClass, T, T->isDependentType(), false),
2526    SubStmt(substmt), LParenLoc(lp), RParenLoc(rp) { }
2527
2528  /// \brief Build an empty statement expression.
2529  explicit StmtExpr(EmptyShell Empty) : Expr(StmtExprClass, Empty) { }
2530
2531  CompoundStmt *getSubStmt() { return cast<CompoundStmt>(SubStmt); }
2532  const CompoundStmt *getSubStmt() const { return cast<CompoundStmt>(SubStmt); }
2533  void setSubStmt(CompoundStmt *S) { SubStmt = S; }
2534
2535  virtual SourceRange getSourceRange() const {
2536    return SourceRange(LParenLoc, RParenLoc);
2537  }
2538
2539  SourceLocation getLParenLoc() const { return LParenLoc; }
2540  void setLParenLoc(SourceLocation L) { LParenLoc = L; }
2541  SourceLocation getRParenLoc() const { return RParenLoc; }
2542  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
2543
2544  static bool classof(const Stmt *T) {
2545    return T->getStmtClass() == StmtExprClass;
2546  }
2547  static bool classof(const StmtExpr *) { return true; }
2548
2549  // Iterators
2550  virtual child_iterator child_begin();
2551  virtual child_iterator child_end();
2552};
2553
2554/// TypesCompatibleExpr - GNU builtin-in function __builtin_types_compatible_p.
2555/// This AST node represents a function that returns 1 if two *types* (not
2556/// expressions) are compatible. The result of this built-in function can be
2557/// used in integer constant expressions.
2558class TypesCompatibleExpr : public Expr {
2559  TypeSourceInfo *TInfo1;
2560  TypeSourceInfo *TInfo2;
2561  SourceLocation BuiltinLoc, RParenLoc;
2562public:
2563  TypesCompatibleExpr(QualType ReturnType, SourceLocation BLoc,
2564                      TypeSourceInfo *tinfo1, TypeSourceInfo *tinfo2,
2565                      SourceLocation RP) :
2566    Expr(TypesCompatibleExprClass, ReturnType, false, false),
2567    TInfo1(tinfo1), TInfo2(tinfo2), BuiltinLoc(BLoc), RParenLoc(RP) {}
2568
2569  /// \brief Build an empty __builtin_type_compatible_p expression.
2570  explicit TypesCompatibleExpr(EmptyShell Empty)
2571    : Expr(TypesCompatibleExprClass, Empty) { }
2572
2573  TypeSourceInfo *getArgTInfo1() const { return TInfo1; }
2574  void setArgTInfo1(TypeSourceInfo *TInfo) { TInfo1 = TInfo; }
2575  TypeSourceInfo *getArgTInfo2() const { return TInfo2; }
2576  void setArgTInfo2(TypeSourceInfo *TInfo) { TInfo2 = TInfo; }
2577
2578  QualType getArgType1() const { return TInfo1->getType(); }
2579  QualType getArgType2() const { return TInfo2->getType(); }
2580
2581  SourceLocation getBuiltinLoc() const { return BuiltinLoc; }
2582  void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; }
2583
2584  SourceLocation getRParenLoc() const { return RParenLoc; }
2585  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
2586
2587  virtual SourceRange getSourceRange() const {
2588    return SourceRange(BuiltinLoc, RParenLoc);
2589  }
2590  static bool classof(const Stmt *T) {
2591    return T->getStmtClass() == TypesCompatibleExprClass;
2592  }
2593  static bool classof(const TypesCompatibleExpr *) { return true; }
2594
2595  // Iterators
2596  virtual child_iterator child_begin();
2597  virtual child_iterator child_end();
2598};
2599
2600/// ShuffleVectorExpr - clang-specific builtin-in function
2601/// __builtin_shufflevector.
2602/// This AST node represents a operator that does a constant
2603/// shuffle, similar to LLVM's shufflevector instruction. It takes
2604/// two vectors and a variable number of constant indices,
2605/// and returns the appropriately shuffled vector.
2606class ShuffleVectorExpr : public Expr {
2607  SourceLocation BuiltinLoc, RParenLoc;
2608
2609  // SubExprs - the list of values passed to the __builtin_shufflevector
2610  // function. The first two are vectors, and the rest are constant
2611  // indices.  The number of values in this list is always
2612  // 2+the number of indices in the vector type.
2613  Stmt **SubExprs;
2614  unsigned NumExprs;
2615
2616public:
2617  // FIXME: Can a shufflevector be value-dependent?  Does type-dependence need
2618  // to be computed differently?
2619  ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
2620                    QualType Type, SourceLocation BLoc,
2621                    SourceLocation RP) :
2622    Expr(ShuffleVectorExprClass, Type, Type->isDependentType(), false),
2623    BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr) {
2624
2625    SubExprs = new (C) Stmt*[nexpr];
2626    for (unsigned i = 0; i < nexpr; i++)
2627      SubExprs[i] = args[i];
2628  }
2629
2630  /// \brief Build an empty vector-shuffle expression.
2631  explicit ShuffleVectorExpr(EmptyShell Empty)
2632    : Expr(ShuffleVectorExprClass, Empty), SubExprs(0) { }
2633
2634  SourceLocation getBuiltinLoc() const { return BuiltinLoc; }
2635  void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; }
2636
2637  SourceLocation getRParenLoc() const { return RParenLoc; }
2638  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
2639
2640  virtual SourceRange getSourceRange() const {
2641    return SourceRange(BuiltinLoc, RParenLoc);
2642  }
2643  static bool classof(const Stmt *T) {
2644    return T->getStmtClass() == ShuffleVectorExprClass;
2645  }
2646  static bool classof(const ShuffleVectorExpr *) { return true; }
2647
2648  /// getNumSubExprs - Return the size of the SubExprs array.  This includes the
2649  /// constant expression, the actual arguments passed in, and the function
2650  /// pointers.
2651  unsigned getNumSubExprs() const { return NumExprs; }
2652
2653  /// getExpr - Return the Expr at the specified index.
2654  Expr *getExpr(unsigned Index) {
2655    assert((Index < NumExprs) && "Arg access out of range!");
2656    return cast<Expr>(SubExprs[Index]);
2657  }
2658  const Expr *getExpr(unsigned Index) const {
2659    assert((Index < NumExprs) && "Arg access out of range!");
2660    return cast<Expr>(SubExprs[Index]);
2661  }
2662
2663  void setExprs(ASTContext &C, Expr ** Exprs, unsigned NumExprs);
2664
2665  unsigned getShuffleMaskIdx(ASTContext &Ctx, unsigned N) {
2666    assert((N < NumExprs - 2) && "Shuffle idx out of range!");
2667    return getExpr(N+2)->EvaluateAsInt(Ctx).getZExtValue();
2668  }
2669
2670  // Iterators
2671  virtual child_iterator child_begin();
2672  virtual child_iterator child_end();
2673};
2674
2675/// ChooseExpr - GNU builtin-in function __builtin_choose_expr.
2676/// This AST node is similar to the conditional operator (?:) in C, with
2677/// the following exceptions:
2678/// - the test expression must be a integer constant expression.
2679/// - the expression returned acts like the chosen subexpression in every
2680///   visible way: the type is the same as that of the chosen subexpression,
2681///   and all predicates (whether it's an l-value, whether it's an integer
2682///   constant expression, etc.) return the same result as for the chosen
2683///   sub-expression.
2684class ChooseExpr : public Expr {
2685  enum { COND, LHS, RHS, END_EXPR };
2686  Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides.
2687  SourceLocation BuiltinLoc, RParenLoc;
2688public:
2689  ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, QualType t,
2690             SourceLocation RP, bool TypeDependent, bool ValueDependent)
2691    : Expr(ChooseExprClass, t, TypeDependent, ValueDependent),
2692      BuiltinLoc(BLoc), RParenLoc(RP) {
2693      SubExprs[COND] = cond;
2694      SubExprs[LHS] = lhs;
2695      SubExprs[RHS] = rhs;
2696    }
2697
2698  /// \brief Build an empty __builtin_choose_expr.
2699  explicit ChooseExpr(EmptyShell Empty) : Expr(ChooseExprClass, Empty) { }
2700
2701  /// isConditionTrue - Return whether the condition is true (i.e. not
2702  /// equal to zero).
2703  bool isConditionTrue(ASTContext &C) const;
2704
2705  /// getChosenSubExpr - Return the subexpression chosen according to the
2706  /// condition.
2707  Expr *getChosenSubExpr(ASTContext &C) const {
2708    return isConditionTrue(C) ? getLHS() : getRHS();
2709  }
2710
2711  Expr *getCond() const { return cast<Expr>(SubExprs[COND]); }
2712  void setCond(Expr *E) { SubExprs[COND] = E; }
2713  Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); }
2714  void setLHS(Expr *E) { SubExprs[LHS] = E; }
2715  Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); }
2716  void setRHS(Expr *E) { SubExprs[RHS] = E; }
2717
2718  SourceLocation getBuiltinLoc() const { return BuiltinLoc; }
2719  void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; }
2720
2721  SourceLocation getRParenLoc() const { return RParenLoc; }
2722  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
2723
2724  virtual SourceRange getSourceRange() const {
2725    return SourceRange(BuiltinLoc, RParenLoc);
2726  }
2727  static bool classof(const Stmt *T) {
2728    return T->getStmtClass() == ChooseExprClass;
2729  }
2730  static bool classof(const ChooseExpr *) { return true; }
2731
2732  // Iterators
2733  virtual child_iterator child_begin();
2734  virtual child_iterator child_end();
2735};
2736
2737/// GNUNullExpr - Implements the GNU __null extension, which is a name
2738/// for a null pointer constant that has integral type (e.g., int or
2739/// long) and is the same size and alignment as a pointer. The __null
2740/// extension is typically only used by system headers, which define
2741/// NULL as __null in C++ rather than using 0 (which is an integer
2742/// that may not match the size of a pointer).
2743class GNUNullExpr : public Expr {
2744  /// TokenLoc - The location of the __null keyword.
2745  SourceLocation TokenLoc;
2746
2747public:
2748  GNUNullExpr(QualType Ty, SourceLocation Loc)
2749    : Expr(GNUNullExprClass, Ty, false, false), TokenLoc(Loc) { }
2750
2751  /// \brief Build an empty GNU __null expression.
2752  explicit GNUNullExpr(EmptyShell Empty) : Expr(GNUNullExprClass, Empty) { }
2753
2754  /// getTokenLocation - The location of the __null token.
2755  SourceLocation getTokenLocation() const { return TokenLoc; }
2756  void setTokenLocation(SourceLocation L) { TokenLoc = L; }
2757
2758  virtual SourceRange getSourceRange() const {
2759    return SourceRange(TokenLoc);
2760  }
2761  static bool classof(const Stmt *T) {
2762    return T->getStmtClass() == GNUNullExprClass;
2763  }
2764  static bool classof(const GNUNullExpr *) { return true; }
2765
2766  // Iterators
2767  virtual child_iterator child_begin();
2768  virtual child_iterator child_end();
2769};
2770
2771/// VAArgExpr, used for the builtin function __builtin_va_arg.
2772class VAArgExpr : public Expr {
2773  Stmt *Val;
2774  TypeSourceInfo *TInfo;
2775  SourceLocation BuiltinLoc, RParenLoc;
2776public:
2777  VAArgExpr(SourceLocation BLoc, Expr* e, TypeSourceInfo *TInfo,
2778            SourceLocation RPLoc, QualType t)
2779    : Expr(VAArgExprClass, t, t->isDependentType(), false),
2780      Val(e), TInfo(TInfo),
2781      BuiltinLoc(BLoc),
2782      RParenLoc(RPLoc) { }
2783
2784  /// \brief Create an empty __builtin_va_arg expression.
2785  explicit VAArgExpr(EmptyShell Empty) : Expr(VAArgExprClass, Empty) { }
2786
2787  const Expr *getSubExpr() const { return cast<Expr>(Val); }
2788  Expr *getSubExpr() { return cast<Expr>(Val); }
2789  void setSubExpr(Expr *E) { Val = E; }
2790
2791  TypeSourceInfo *getWrittenTypeInfo() const { return TInfo; }
2792  void setWrittenTypeInfo(TypeSourceInfo *TI) { TInfo = TI; }
2793
2794  SourceLocation getBuiltinLoc() const { return BuiltinLoc; }
2795  void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; }
2796
2797  SourceLocation getRParenLoc() const { return RParenLoc; }
2798  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
2799
2800  virtual SourceRange getSourceRange() const {
2801    return SourceRange(BuiltinLoc, RParenLoc);
2802  }
2803  static bool classof(const Stmt *T) {
2804    return T->getStmtClass() == VAArgExprClass;
2805  }
2806  static bool classof(const VAArgExpr *) { return true; }
2807
2808  // Iterators
2809  virtual child_iterator child_begin();
2810  virtual child_iterator child_end();
2811};
2812
2813/// @brief Describes an C or C++ initializer list.
2814///
2815/// InitListExpr describes an initializer list, which can be used to
2816/// initialize objects of different types, including
2817/// struct/class/union types, arrays, and vectors. For example:
2818///
2819/// @code
2820/// struct foo x = { 1, { 2, 3 } };
2821/// @endcode
2822///
2823/// Prior to semantic analysis, an initializer list will represent the
2824/// initializer list as written by the user, but will have the
2825/// placeholder type "void". This initializer list is called the
2826/// syntactic form of the initializer, and may contain C99 designated
2827/// initializers (represented as DesignatedInitExprs), initializations
2828/// of subobject members without explicit braces, and so on. Clients
2829/// interested in the original syntax of the initializer list should
2830/// use the syntactic form of the initializer list.
2831///
2832/// After semantic analysis, the initializer list will represent the
2833/// semantic form of the initializer, where the initializations of all
2834/// subobjects are made explicit with nested InitListExpr nodes and
2835/// C99 designators have been eliminated by placing the designated
2836/// initializations into the subobject they initialize. Additionally,
2837/// any "holes" in the initialization, where no initializer has been
2838/// specified for a particular subobject, will be replaced with
2839/// implicitly-generated ImplicitValueInitExpr expressions that
2840/// value-initialize the subobjects. Note, however, that the
2841/// initializer lists may still have fewer initializers than there are
2842/// elements to initialize within the object.
2843///
2844/// Given the semantic form of the initializer list, one can retrieve
2845/// the original syntactic form of that initializer list (if it
2846/// exists) using getSyntacticForm(). Since many initializer lists
2847/// have the same syntactic and semantic forms, getSyntacticForm() may
2848/// return NULL, indicating that the current initializer list also
2849/// serves as its syntactic form.
2850class InitListExpr : public Expr {
2851  // FIXME: Eliminate this vector in favor of ASTContext allocation
2852  typedef ASTVector<Stmt *> InitExprsTy;
2853  InitExprsTy InitExprs;
2854  SourceLocation LBraceLoc, RBraceLoc;
2855
2856  /// Contains the initializer list that describes the syntactic form
2857  /// written in the source code.
2858  InitListExpr *SyntacticForm;
2859
2860  /// If this initializer list initializes a union, specifies which
2861  /// field within the union will be initialized.
2862  FieldDecl *UnionFieldInit;
2863
2864  /// Whether this initializer list originally had a GNU array-range
2865  /// designator in it. This is a temporary marker used by CodeGen.
2866  bool HadArrayRangeDesignator;
2867
2868public:
2869  InitListExpr(ASTContext &C, SourceLocation lbraceloc,
2870               Expr **initexprs, unsigned numinits,
2871               SourceLocation rbraceloc);
2872
2873  /// \brief Build an empty initializer list.
2874  explicit InitListExpr(ASTContext &C, EmptyShell Empty)
2875    : Expr(InitListExprClass, Empty), InitExprs(C) { }
2876
2877  unsigned getNumInits() const { return InitExprs.size(); }
2878
2879  const Expr *getInit(unsigned Init) const {
2880    assert(Init < getNumInits() && "Initializer access out of range!");
2881    return cast_or_null<Expr>(InitExprs[Init]);
2882  }
2883
2884  Expr *getInit(unsigned Init) {
2885    assert(Init < getNumInits() && "Initializer access out of range!");
2886    return cast_or_null<Expr>(InitExprs[Init]);
2887  }
2888
2889  void setInit(unsigned Init, Expr *expr) {
2890    assert(Init < getNumInits() && "Initializer access out of range!");
2891    InitExprs[Init] = expr;
2892  }
2893
2894  /// \brief Reserve space for some number of initializers.
2895  void reserveInits(ASTContext &C, unsigned NumInits);
2896
2897  /// @brief Specify the number of initializers
2898  ///
2899  /// If there are more than @p NumInits initializers, the remaining
2900  /// initializers will be destroyed. If there are fewer than @p
2901  /// NumInits initializers, NULL expressions will be added for the
2902  /// unknown initializers.
2903  void resizeInits(ASTContext &Context, unsigned NumInits);
2904
2905  /// @brief Updates the initializer at index @p Init with the new
2906  /// expression @p expr, and returns the old expression at that
2907  /// location.
2908  ///
2909  /// When @p Init is out of range for this initializer list, the
2910  /// initializer list will be extended with NULL expressions to
2911  /// accomodate the new entry.
2912  Expr *updateInit(ASTContext &C, unsigned Init, Expr *expr);
2913
2914  /// \brief If this initializes a union, specifies which field in the
2915  /// union to initialize.
2916  ///
2917  /// Typically, this field is the first named field within the
2918  /// union. However, a designated initializer can specify the
2919  /// initialization of a different field within the union.
2920  FieldDecl *getInitializedFieldInUnion() { return UnionFieldInit; }
2921  void setInitializedFieldInUnion(FieldDecl *FD) { UnionFieldInit = FD; }
2922
2923  // Explicit InitListExpr's originate from source code (and have valid source
2924  // locations). Implicit InitListExpr's are created by the semantic analyzer.
2925  bool isExplicit() {
2926    return LBraceLoc.isValid() && RBraceLoc.isValid();
2927  }
2928
2929  SourceLocation getLBraceLoc() const { return LBraceLoc; }
2930  void setLBraceLoc(SourceLocation Loc) { LBraceLoc = Loc; }
2931  SourceLocation getRBraceLoc() const { return RBraceLoc; }
2932  void setRBraceLoc(SourceLocation Loc) { RBraceLoc = Loc; }
2933
2934  /// @brief Retrieve the initializer list that describes the
2935  /// syntactic form of the initializer.
2936  ///
2937  ///
2938  InitListExpr *getSyntacticForm() const { return SyntacticForm; }
2939  void setSyntacticForm(InitListExpr *Init) { SyntacticForm = Init; }
2940
2941  bool hadArrayRangeDesignator() const { return HadArrayRangeDesignator; }
2942  void sawArrayRangeDesignator(bool ARD = true) {
2943    HadArrayRangeDesignator = ARD;
2944  }
2945
2946  virtual SourceRange getSourceRange() const {
2947    return SourceRange(LBraceLoc, RBraceLoc);
2948  }
2949  static bool classof(const Stmt *T) {
2950    return T->getStmtClass() == InitListExprClass;
2951  }
2952  static bool classof(const InitListExpr *) { return true; }
2953
2954  // Iterators
2955  virtual child_iterator child_begin();
2956  virtual child_iterator child_end();
2957
2958  typedef InitExprsTy::iterator iterator;
2959  typedef InitExprsTy::const_iterator const_iterator;
2960  typedef InitExprsTy::reverse_iterator reverse_iterator;
2961  typedef InitExprsTy::const_reverse_iterator const_reverse_iterator;
2962
2963  iterator begin() { return InitExprs.begin(); }
2964  const_iterator begin() const { return InitExprs.begin(); }
2965  iterator end() { return InitExprs.end(); }
2966  const_iterator end() const { return InitExprs.end(); }
2967  reverse_iterator rbegin() { return InitExprs.rbegin(); }
2968  const_reverse_iterator rbegin() const { return InitExprs.rbegin(); }
2969  reverse_iterator rend() { return InitExprs.rend(); }
2970  const_reverse_iterator rend() const { return InitExprs.rend(); }
2971};
2972
2973/// @brief Represents a C99 designated initializer expression.
2974///
2975/// A designated initializer expression (C99 6.7.8) contains one or
2976/// more designators (which can be field designators, array
2977/// designators, or GNU array-range designators) followed by an
2978/// expression that initializes the field or element(s) that the
2979/// designators refer to. For example, given:
2980///
2981/// @code
2982/// struct point {
2983///   double x;
2984///   double y;
2985/// };
2986/// struct point ptarray[10] = { [2].y = 1.0, [2].x = 2.0, [0].x = 1.0 };
2987/// @endcode
2988///
2989/// The InitListExpr contains three DesignatedInitExprs, the first of
2990/// which covers @c [2].y=1.0. This DesignatedInitExpr will have two
2991/// designators, one array designator for @c [2] followed by one field
2992/// designator for @c .y. The initalization expression will be 1.0.
2993class DesignatedInitExpr : public Expr {
2994public:
2995  /// \brief Forward declaration of the Designator class.
2996  class Designator;
2997
2998private:
2999  /// The location of the '=' or ':' prior to the actual initializer
3000  /// expression.
3001  SourceLocation EqualOrColonLoc;
3002
3003  /// Whether this designated initializer used the GNU deprecated
3004  /// syntax rather than the C99 '=' syntax.
3005  bool GNUSyntax : 1;
3006
3007  /// The number of designators in this initializer expression.
3008  unsigned NumDesignators : 15;
3009
3010  /// \brief The designators in this designated initialization
3011  /// expression.
3012  Designator *Designators;
3013
3014  /// The number of subexpressions of this initializer expression,
3015  /// which contains both the initializer and any additional
3016  /// expressions used by array and array-range designators.
3017  unsigned NumSubExprs : 16;
3018
3019
3020  DesignatedInitExpr(ASTContext &C, QualType Ty, unsigned NumDesignators,
3021                     const Designator *Designators,
3022                     SourceLocation EqualOrColonLoc, bool GNUSyntax,
3023                     Expr **IndexExprs, unsigned NumIndexExprs,
3024                     Expr *Init);
3025
3026  explicit DesignatedInitExpr(unsigned NumSubExprs)
3027    : Expr(DesignatedInitExprClass, EmptyShell()),
3028      NumDesignators(0), Designators(0), NumSubExprs(NumSubExprs) { }
3029
3030public:
3031  /// A field designator, e.g., ".x".
3032  struct FieldDesignator {
3033    /// Refers to the field that is being initialized. The low bit
3034    /// of this field determines whether this is actually a pointer
3035    /// to an IdentifierInfo (if 1) or a FieldDecl (if 0). When
3036    /// initially constructed, a field designator will store an
3037    /// IdentifierInfo*. After semantic analysis has resolved that
3038    /// name, the field designator will instead store a FieldDecl*.
3039    uintptr_t NameOrField;
3040
3041    /// The location of the '.' in the designated initializer.
3042    unsigned DotLoc;
3043
3044    /// The location of the field name in the designated initializer.
3045    unsigned FieldLoc;
3046  };
3047
3048  /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]".
3049  struct ArrayOrRangeDesignator {
3050    /// Location of the first index expression within the designated
3051    /// initializer expression's list of subexpressions.
3052    unsigned Index;
3053    /// The location of the '[' starting the array range designator.
3054    unsigned LBracketLoc;
3055    /// The location of the ellipsis separating the start and end
3056    /// indices. Only valid for GNU array-range designators.
3057    unsigned EllipsisLoc;
3058    /// The location of the ']' terminating the array range designator.
3059    unsigned RBracketLoc;
3060  };
3061
3062  /// @brief Represents a single C99 designator.
3063  ///
3064  /// @todo This class is infuriatingly similar to clang::Designator,
3065  /// but minor differences (storing indices vs. storing pointers)
3066  /// keep us from reusing it. Try harder, later, to rectify these
3067  /// differences.
3068  class Designator {
3069    /// @brief The kind of designator this describes.
3070    enum {
3071      FieldDesignator,
3072      ArrayDesignator,
3073      ArrayRangeDesignator
3074    } Kind;
3075
3076    union {
3077      /// A field designator, e.g., ".x".
3078      struct FieldDesignator Field;
3079      /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]".
3080      struct ArrayOrRangeDesignator ArrayOrRange;
3081    };
3082    friend class DesignatedInitExpr;
3083
3084  public:
3085    Designator() {}
3086
3087    /// @brief Initializes a field designator.
3088    Designator(const IdentifierInfo *FieldName, SourceLocation DotLoc,
3089               SourceLocation FieldLoc)
3090      : Kind(FieldDesignator) {
3091      Field.NameOrField = reinterpret_cast<uintptr_t>(FieldName) | 0x01;
3092      Field.DotLoc = DotLoc.getRawEncoding();
3093      Field.FieldLoc = FieldLoc.getRawEncoding();
3094    }
3095
3096    /// @brief Initializes an array designator.
3097    Designator(unsigned Index, SourceLocation LBracketLoc,
3098               SourceLocation RBracketLoc)
3099      : Kind(ArrayDesignator) {
3100      ArrayOrRange.Index = Index;
3101      ArrayOrRange.LBracketLoc = LBracketLoc.getRawEncoding();
3102      ArrayOrRange.EllipsisLoc = SourceLocation().getRawEncoding();
3103      ArrayOrRange.RBracketLoc = RBracketLoc.getRawEncoding();
3104    }
3105
3106    /// @brief Initializes a GNU array-range designator.
3107    Designator(unsigned Index, SourceLocation LBracketLoc,
3108               SourceLocation EllipsisLoc, SourceLocation RBracketLoc)
3109      : Kind(ArrayRangeDesignator) {
3110      ArrayOrRange.Index = Index;
3111      ArrayOrRange.LBracketLoc = LBracketLoc.getRawEncoding();
3112      ArrayOrRange.EllipsisLoc = EllipsisLoc.getRawEncoding();
3113      ArrayOrRange.RBracketLoc = RBracketLoc.getRawEncoding();
3114    }
3115
3116    bool isFieldDesignator() const { return Kind == FieldDesignator; }
3117    bool isArrayDesignator() const { return Kind == ArrayDesignator; }
3118    bool isArrayRangeDesignator() const { return Kind == ArrayRangeDesignator; }
3119
3120    IdentifierInfo * getFieldName();
3121
3122    FieldDecl *getField() {
3123      assert(Kind == FieldDesignator && "Only valid on a field designator");
3124      if (Field.NameOrField & 0x01)
3125        return 0;
3126      else
3127        return reinterpret_cast<FieldDecl *>(Field.NameOrField);
3128    }
3129
3130    void setField(FieldDecl *FD) {
3131      assert(Kind == FieldDesignator && "Only valid on a field designator");
3132      Field.NameOrField = reinterpret_cast<uintptr_t>(FD);
3133    }
3134
3135    SourceLocation getDotLoc() const {
3136      assert(Kind == FieldDesignator && "Only valid on a field designator");
3137      return SourceLocation::getFromRawEncoding(Field.DotLoc);
3138    }
3139
3140    SourceLocation getFieldLoc() const {
3141      assert(Kind == FieldDesignator && "Only valid on a field designator");
3142      return SourceLocation::getFromRawEncoding(Field.FieldLoc);
3143    }
3144
3145    SourceLocation getLBracketLoc() const {
3146      assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) &&
3147             "Only valid on an array or array-range designator");
3148      return SourceLocation::getFromRawEncoding(ArrayOrRange.LBracketLoc);
3149    }
3150
3151    SourceLocation getRBracketLoc() const {
3152      assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) &&
3153             "Only valid on an array or array-range designator");
3154      return SourceLocation::getFromRawEncoding(ArrayOrRange.RBracketLoc);
3155    }
3156
3157    SourceLocation getEllipsisLoc() const {
3158      assert(Kind == ArrayRangeDesignator &&
3159             "Only valid on an array-range designator");
3160      return SourceLocation::getFromRawEncoding(ArrayOrRange.EllipsisLoc);
3161    }
3162
3163    unsigned getFirstExprIndex() const {
3164      assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) &&
3165             "Only valid on an array or array-range designator");
3166      return ArrayOrRange.Index;
3167    }
3168
3169    SourceLocation getStartLocation() const {
3170      if (Kind == FieldDesignator)
3171        return getDotLoc().isInvalid()? getFieldLoc() : getDotLoc();
3172      else
3173        return getLBracketLoc();
3174    }
3175  };
3176
3177  static DesignatedInitExpr *Create(ASTContext &C, Designator *Designators,
3178                                    unsigned NumDesignators,
3179                                    Expr **IndexExprs, unsigned NumIndexExprs,
3180                                    SourceLocation EqualOrColonLoc,
3181                                    bool GNUSyntax, Expr *Init);
3182
3183  static DesignatedInitExpr *CreateEmpty(ASTContext &C, unsigned NumIndexExprs);
3184
3185  /// @brief Returns the number of designators in this initializer.
3186  unsigned size() const { return NumDesignators; }
3187
3188  // Iterator access to the designators.
3189  typedef Designator* designators_iterator;
3190  designators_iterator designators_begin() { return Designators; }
3191  designators_iterator designators_end() {
3192    return Designators + NumDesignators;
3193  }
3194
3195  Designator *getDesignator(unsigned Idx) { return &designators_begin()[Idx]; }
3196
3197  void setDesignators(ASTContext &C, const Designator *Desigs,
3198                      unsigned NumDesigs);
3199
3200  Expr *getArrayIndex(const Designator& D);
3201  Expr *getArrayRangeStart(const Designator& D);
3202  Expr *getArrayRangeEnd(const Designator& D);
3203
3204  /// @brief Retrieve the location of the '=' that precedes the
3205  /// initializer value itself, if present.
3206  SourceLocation getEqualOrColonLoc() const { return EqualOrColonLoc; }
3207  void setEqualOrColonLoc(SourceLocation L) { EqualOrColonLoc = L; }
3208
3209  /// @brief Determines whether this designated initializer used the
3210  /// deprecated GNU syntax for designated initializers.
3211  bool usesGNUSyntax() const { return GNUSyntax; }
3212  void setGNUSyntax(bool GNU) { GNUSyntax = GNU; }
3213
3214  /// @brief Retrieve the initializer value.
3215  Expr *getInit() const {
3216    return cast<Expr>(*const_cast<DesignatedInitExpr*>(this)->child_begin());
3217  }
3218
3219  void setInit(Expr *init) {
3220    *child_begin() = init;
3221  }
3222
3223  /// \brief Retrieve the total number of subexpressions in this
3224  /// designated initializer expression, including the actual
3225  /// initialized value and any expressions that occur within array
3226  /// and array-range designators.
3227  unsigned getNumSubExprs() const { return NumSubExprs; }
3228
3229  Expr *getSubExpr(unsigned Idx) {
3230    assert(Idx < NumSubExprs && "Subscript out of range");
3231    char* Ptr = static_cast<char*>(static_cast<void *>(this));
3232    Ptr += sizeof(DesignatedInitExpr);
3233    return reinterpret_cast<Expr**>(reinterpret_cast<void**>(Ptr))[Idx];
3234  }
3235
3236  void setSubExpr(unsigned Idx, Expr *E) {
3237    assert(Idx < NumSubExprs && "Subscript out of range");
3238    char* Ptr = static_cast<char*>(static_cast<void *>(this));
3239    Ptr += sizeof(DesignatedInitExpr);
3240    reinterpret_cast<Expr**>(reinterpret_cast<void**>(Ptr))[Idx] = E;
3241  }
3242
3243  /// \brief Replaces the designator at index @p Idx with the series
3244  /// of designators in [First, Last).
3245  void ExpandDesignator(ASTContext &C, unsigned Idx, const Designator *First,
3246                        const Designator *Last);
3247
3248  virtual SourceRange getSourceRange() const;
3249
3250  static bool classof(const Stmt *T) {
3251    return T->getStmtClass() == DesignatedInitExprClass;
3252  }
3253  static bool classof(const DesignatedInitExpr *) { return true; }
3254
3255  // Iterators
3256  virtual child_iterator child_begin();
3257  virtual child_iterator child_end();
3258};
3259
3260/// \brief Represents an implicitly-generated value initialization of
3261/// an object of a given type.
3262///
3263/// Implicit value initializations occur within semantic initializer
3264/// list expressions (InitListExpr) as placeholders for subobject
3265/// initializations not explicitly specified by the user.
3266///
3267/// \see InitListExpr
3268class ImplicitValueInitExpr : public Expr {
3269public:
3270  explicit ImplicitValueInitExpr(QualType ty)
3271    : Expr(ImplicitValueInitExprClass, ty, false, false) { }
3272
3273  /// \brief Construct an empty implicit value initialization.
3274  explicit ImplicitValueInitExpr(EmptyShell Empty)
3275    : Expr(ImplicitValueInitExprClass, Empty) { }
3276
3277  static bool classof(const Stmt *T) {
3278    return T->getStmtClass() == ImplicitValueInitExprClass;
3279  }
3280  static bool classof(const ImplicitValueInitExpr *) { return true; }
3281
3282  virtual SourceRange getSourceRange() const {
3283    return SourceRange();
3284  }
3285
3286  // Iterators
3287  virtual child_iterator child_begin();
3288  virtual child_iterator child_end();
3289};
3290
3291
3292class ParenListExpr : public Expr {
3293  Stmt **Exprs;
3294  unsigned NumExprs;
3295  SourceLocation LParenLoc, RParenLoc;
3296
3297public:
3298  ParenListExpr(ASTContext& C, SourceLocation lparenloc, Expr **exprs,
3299                unsigned numexprs, SourceLocation rparenloc);
3300
3301  /// \brief Build an empty paren list.
3302  explicit ParenListExpr(EmptyShell Empty) : Expr(ParenListExprClass, Empty) { }
3303
3304  unsigned getNumExprs() const { return NumExprs; }
3305
3306  const Expr* getExpr(unsigned Init) const {
3307    assert(Init < getNumExprs() && "Initializer access out of range!");
3308    return cast_or_null<Expr>(Exprs[Init]);
3309  }
3310
3311  Expr* getExpr(unsigned Init) {
3312    assert(Init < getNumExprs() && "Initializer access out of range!");
3313    return cast_or_null<Expr>(Exprs[Init]);
3314  }
3315
3316  Expr **getExprs() { return reinterpret_cast<Expr **>(Exprs); }
3317
3318  SourceLocation getLParenLoc() const { return LParenLoc; }
3319  SourceLocation getRParenLoc() const { return RParenLoc; }
3320
3321  virtual SourceRange getSourceRange() const {
3322    return SourceRange(LParenLoc, RParenLoc);
3323  }
3324  static bool classof(const Stmt *T) {
3325    return T->getStmtClass() == ParenListExprClass;
3326  }
3327  static bool classof(const ParenListExpr *) { return true; }
3328
3329  // Iterators
3330  virtual child_iterator child_begin();
3331  virtual child_iterator child_end();
3332
3333  friend class ASTStmtReader;
3334  friend class ASTStmtWriter;
3335};
3336
3337
3338//===----------------------------------------------------------------------===//
3339// Clang Extensions
3340//===----------------------------------------------------------------------===//
3341
3342
3343/// ExtVectorElementExpr - This represents access to specific elements of a
3344/// vector, and may occur on the left hand side or right hand side.  For example
3345/// the following is legal:  "V.xy = V.zw" if V is a 4 element extended vector.
3346///
3347/// Note that the base may have either vector or pointer to vector type, just
3348/// like a struct field reference.
3349///
3350class ExtVectorElementExpr : public Expr {
3351  Stmt *Base;
3352  IdentifierInfo *Accessor;
3353  SourceLocation AccessorLoc;
3354public:
3355  ExtVectorElementExpr(QualType ty, Expr *base, IdentifierInfo &accessor,
3356                       SourceLocation loc)
3357    : Expr(ExtVectorElementExprClass, ty, base->isTypeDependent(),
3358           base->isValueDependent()),
3359      Base(base), Accessor(&accessor), AccessorLoc(loc) {}
3360
3361  /// \brief Build an empty vector element expression.
3362  explicit ExtVectorElementExpr(EmptyShell Empty)
3363    : Expr(ExtVectorElementExprClass, Empty) { }
3364
3365  const Expr *getBase() const { return cast<Expr>(Base); }
3366  Expr *getBase() { return cast<Expr>(Base); }
3367  void setBase(Expr *E) { Base = E; }
3368
3369  IdentifierInfo &getAccessor() const { return *Accessor; }
3370  void setAccessor(IdentifierInfo *II) { Accessor = II; }
3371
3372  SourceLocation getAccessorLoc() const { return AccessorLoc; }
3373  void setAccessorLoc(SourceLocation L) { AccessorLoc = L; }
3374
3375  /// getNumElements - Get the number of components being selected.
3376  unsigned getNumElements() const;
3377
3378  /// containsDuplicateElements - Return true if any element access is
3379  /// repeated.
3380  bool containsDuplicateElements() const;
3381
3382  /// getEncodedElementAccess - Encode the elements accessed into an llvm
3383  /// aggregate Constant of ConstantInt(s).
3384  void getEncodedElementAccess(llvm::SmallVectorImpl<unsigned> &Elts) const;
3385
3386  virtual SourceRange getSourceRange() const {
3387    return SourceRange(getBase()->getLocStart(), AccessorLoc);
3388  }
3389
3390  /// isArrow - Return true if the base expression is a pointer to vector,
3391  /// return false if the base expression is a vector.
3392  bool isArrow() const;
3393
3394  static bool classof(const Stmt *T) {
3395    return T->getStmtClass() == ExtVectorElementExprClass;
3396  }
3397  static bool classof(const ExtVectorElementExpr *) { return true; }
3398
3399  // Iterators
3400  virtual child_iterator child_begin();
3401  virtual child_iterator child_end();
3402};
3403
3404
3405/// BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
3406/// ^{ statement-body }   or   ^(int arg1, float arg2){ statement-body }
3407class BlockExpr : public Expr {
3408protected:
3409  BlockDecl *TheBlock;
3410  bool HasBlockDeclRefExprs;
3411public:
3412  BlockExpr(BlockDecl *BD, QualType ty, bool hasBlockDeclRefExprs)
3413    : Expr(BlockExprClass, ty, ty->isDependentType(), false),
3414      TheBlock(BD), HasBlockDeclRefExprs(hasBlockDeclRefExprs) {}
3415
3416  /// \brief Build an empty block expression.
3417  explicit BlockExpr(EmptyShell Empty) : Expr(BlockExprClass, Empty) { }
3418
3419  const BlockDecl *getBlockDecl() const { return TheBlock; }
3420  BlockDecl *getBlockDecl() { return TheBlock; }
3421  void setBlockDecl(BlockDecl *BD) { TheBlock = BD; }
3422
3423  // Convenience functions for probing the underlying BlockDecl.
3424  SourceLocation getCaretLocation() const;
3425  const Stmt *getBody() const;
3426  Stmt *getBody();
3427
3428  virtual SourceRange getSourceRange() const {
3429    return SourceRange(getCaretLocation(), getBody()->getLocEnd());
3430  }
3431
3432  /// getFunctionType - Return the underlying function type for this block.
3433  const FunctionType *getFunctionType() const;
3434
3435  /// hasBlockDeclRefExprs - Return true iff the block has BlockDeclRefExpr
3436  /// inside of the block that reference values outside the block.
3437  bool hasBlockDeclRefExprs() const { return HasBlockDeclRefExprs; }
3438  void setHasBlockDeclRefExprs(bool BDRE) { HasBlockDeclRefExprs = BDRE; }
3439
3440  static bool classof(const Stmt *T) {
3441    return T->getStmtClass() == BlockExprClass;
3442  }
3443  static bool classof(const BlockExpr *) { return true; }
3444
3445  // Iterators
3446  virtual child_iterator child_begin();
3447  virtual child_iterator child_end();
3448};
3449
3450/// BlockDeclRefExpr - A reference to a declared variable, function,
3451/// enum, etc.
3452class BlockDeclRefExpr : public Expr {
3453  ValueDecl *D;
3454  SourceLocation Loc;
3455  bool IsByRef : 1;
3456  bool ConstQualAdded : 1;
3457  Stmt *CopyConstructorVal;
3458public:
3459  // FIXME: Fix type/value dependence!
3460  BlockDeclRefExpr(ValueDecl *d, QualType t, SourceLocation l, bool ByRef,
3461                   bool constAdded = false,
3462                   Stmt *copyConstructorVal = 0)
3463  : Expr(BlockDeclRefExprClass, t, (!t.isNull() && t->isDependentType()),false),
3464    D(d), Loc(l), IsByRef(ByRef),
3465    ConstQualAdded(constAdded),  CopyConstructorVal(copyConstructorVal) {}
3466
3467  // \brief Build an empty reference to a declared variable in a
3468  // block.
3469  explicit BlockDeclRefExpr(EmptyShell Empty)
3470    : Expr(BlockDeclRefExprClass, Empty) { }
3471
3472  ValueDecl *getDecl() { return D; }
3473  const ValueDecl *getDecl() const { return D; }
3474  void setDecl(ValueDecl *VD) { D = VD; }
3475
3476  SourceLocation getLocation() const { return Loc; }
3477  void setLocation(SourceLocation L) { Loc = L; }
3478
3479  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
3480
3481  bool isByRef() const { return IsByRef; }
3482  void setByRef(bool BR) { IsByRef = BR; }
3483
3484  bool isConstQualAdded() const { return ConstQualAdded; }
3485  void setConstQualAdded(bool C) { ConstQualAdded = C; }
3486
3487  const Expr *getCopyConstructorExpr() const
3488    { return cast_or_null<Expr>(CopyConstructorVal); }
3489  Expr *getCopyConstructorExpr()
3490    { return cast_or_null<Expr>(CopyConstructorVal); }
3491  void setCopyConstructorExpr(Expr *E) { CopyConstructorVal = E; }
3492
3493  static bool classof(const Stmt *T) {
3494    return T->getStmtClass() == BlockDeclRefExprClass;
3495  }
3496  static bool classof(const BlockDeclRefExpr *) { return true; }
3497
3498  // Iterators
3499  virtual child_iterator child_begin();
3500  virtual child_iterator child_end();
3501};
3502
3503}  // end namespace clang
3504
3505#endif
3506