15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- Expr.h - Classes for representing expressions ----------*- C++ -*-===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//  This file defines the Expr interface and subclasses.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#ifndef LLVM_CLANG_AST_EXPR_H
155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#define LLVM_CLANG_AST_EXPR_H
165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1794deaf675ae60e11c2d9475c6dbfd0c7123160f5Anders Carlsson#include "clang/AST/APValue.h"
1830a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "clang/AST/ASTVector.h"
19478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer#include "clang/AST/Decl.h"
20161755a09898c95d21bfff33707da9ca41cd53c5John McCall#include "clang/AST/DeclAccessPair.h"
215baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall#include "clang/AST/OperationKinds.h"
2230a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "clang/AST/Stmt.h"
2371a7605977113c795edd44fcbd2302ad49506653Argyrios Kyrtzidis#include "clang/AST/TemplateBase.h"
2430a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "clang/AST/Type.h"
25223f0ff6a9a5d0eaf63b98b3aa92888b4c088868Jordan Rose#include "clang/Basic/CharInfo.h"
26f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne#include "clang/Basic/TypeTraits.h"
27525a05093a4816af961fe2bc6b8a81c17e2e26c2Chris Lattner#include "llvm/ADT/APFloat.h"
2830a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "llvm/ADT/APSInt.h"
293b8d116703db8018f855cbb4733ace426422623bNate Begeman#include "llvm/ADT/SmallVector.h"
30b648023da23e8b227cdda57a241db4c6f368726bDaniel Dunbar#include "llvm/ADT/StringRef.h"
31aa49a7d70e58dac2aeb40664ba16d2ea571b8c95Daniel Dunbar#include "llvm/Support/Compiler.h"
325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace clang {
34c44eec6dd29ee9415cbd38a35deff4c8b67abb6aAnders Carlsson  class APValue;
359852f58f50b4fc20914fbce5b4454135a42343f4Benjamin Kramer  class ASTContext;
3656ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff  class BlockDecl;
37409c99edb8b623403fade6f3a9e9c86acda74455Anders Carlsson  class CXXBaseSpecifier;
389852f58f50b4fc20914fbce5b4454135a42343f4Benjamin Kramer  class CXXMemberCallExpr;
3988a3514f36de96b19cdf50141c640df1a5f13f6cDouglas Gregor  class CXXOperatorCallExpr;
409852f58f50b4fc20914fbce5b4454135a42343f4Benjamin Kramer  class CastExpr;
419852f58f50b4fc20914fbce5b4454135a42343f4Benjamin Kramer  class Decl;
429852f58f50b4fc20914fbce5b4454135a42343f4Benjamin Kramer  class IdentifierInfo;
430a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola  class MaterializeTemporaryExpr;
449852f58f50b4fc20914fbce5b4454135a42343f4Benjamin Kramer  class NamedDecl;
45f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  class ObjCPropertyRefExpr;
4656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  class OpaqueValueExpr;
479852f58f50b4fc20914fbce5b4454135a42343f4Benjamin Kramer  class ParmVarDecl;
489852f58f50b4fc20914fbce5b4454135a42343f4Benjamin Kramer  class TargetInfo;
499852f58f50b4fc20914fbce5b4454135a42343f4Benjamin Kramer  class ValueDecl;
5088a3514f36de96b19cdf50141c640df1a5f13f6cDouglas Gregor
517ab9d574d27ecee1f130e5755aa403e5ab529b6bAnders Carlsson/// \brief A simple array of base specifiers.
52686775deca8b8685eb90801495880e3abdd844c2Chris Lattnertypedef SmallVector<CXXBaseSpecifier*, 4> CXXCastPath;
537ab9d574d27ecee1f130e5755aa403e5ab529b6bAnders Carlsson
540a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola/// \brief An adjustment to be made to the temporary created when emitting a
550a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola/// reference binding, which accesses a particular subobject of that temporary.
560a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindolastruct SubobjectAdjustment {
570a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola  enum {
580a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola    DerivedToBaseAdjustment,
590a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola    FieldAdjustment,
600a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola    MemberPointerAdjustment
610a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola  } Kind;
620a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola
630a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola
64e462c60ac3365d3302b7d0a566c5cb7dbe0e5ae3Eric Christopher  struct DTB {
65e462c60ac3365d3302b7d0a566c5cb7dbe0e5ae3Eric Christopher    const CastExpr *BasePath;
66e462c60ac3365d3302b7d0a566c5cb7dbe0e5ae3Eric Christopher    const CXXRecordDecl *DerivedClass;
67e462c60ac3365d3302b7d0a566c5cb7dbe0e5ae3Eric Christopher  };
680a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola
69e462c60ac3365d3302b7d0a566c5cb7dbe0e5ae3Eric Christopher  struct P {
70e462c60ac3365d3302b7d0a566c5cb7dbe0e5ae3Eric Christopher    const MemberPointerType *MPT;
71e462c60ac3365d3302b7d0a566c5cb7dbe0e5ae3Eric Christopher    Expr *RHS;
72e462c60ac3365d3302b7d0a566c5cb7dbe0e5ae3Eric Christopher  };
73e462c60ac3365d3302b7d0a566c5cb7dbe0e5ae3Eric Christopher
74e462c60ac3365d3302b7d0a566c5cb7dbe0e5ae3Eric Christopher  union {
75e462c60ac3365d3302b7d0a566c5cb7dbe0e5ae3Eric Christopher    struct DTB DerivedToBase;
76e462c60ac3365d3302b7d0a566c5cb7dbe0e5ae3Eric Christopher    FieldDecl *Field;
77e462c60ac3365d3302b7d0a566c5cb7dbe0e5ae3Eric Christopher    struct P Ptr;
780a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola  };
790a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola
800a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola  SubobjectAdjustment(const CastExpr *BasePath,
810a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola                      const CXXRecordDecl *DerivedClass)
820a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola    : Kind(DerivedToBaseAdjustment) {
830a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola    DerivedToBase.BasePath = BasePath;
840a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola    DerivedToBase.DerivedClass = DerivedClass;
850a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola  }
860a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola
870a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola  SubobjectAdjustment(FieldDecl *Field)
880a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola    : Kind(FieldAdjustment) {
890a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola    this->Field = Field;
900a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola  }
910a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola
920a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola  SubobjectAdjustment(const MemberPointerType *MPT, Expr *RHS)
930a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola    : Kind(MemberPointerAdjustment) {
940a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola    this->Ptr.MPT = MPT;
950a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola    this->Ptr.RHS = RHS;
960a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola  }
970a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola};
980a7dd835184343ec9149277b668ecdc5d49fe8b0Rafael Espindola
995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Expr - This represents one expression.  Note that Expr's are subclasses of
1005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Stmt.  This allows an expression to be transparently used any place a Stmt
1015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// is required.
1025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
1035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass Expr : public Stmt {
1045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType TR;
105898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
1068e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCallprotected:
107f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  Expr(StmtClass SC, QualType T, ExprValueKind VK, ExprObjectKind OK,
108ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie       bool TD, bool VD, bool ID, bool ContainsUnexpandedParameterPack)
109ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    : Stmt(SC)
110561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  {
1118e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall    ExprBits.TypeDependent = TD;
1128e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall    ExprBits.ValueDependent = VD;
113561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    ExprBits.InstantiationDependent = ID;
114f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall    ExprBits.ValueKind = VK;
115f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall    ExprBits.ObjectKind = OK;
116bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
117898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    setType(T);
118898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  }
119898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
1200b7489194f9f89fac39d57211c1e7953ae50251fDouglas Gregor  /// \brief Construct an empty expression.
1210b7489194f9f89fac39d57211c1e7953ae50251fDouglas Gregor  explicit Expr(StmtClass SC, EmptyShell) : Stmt(SC) { }
1220b7489194f9f89fac39d57211c1e7953ae50251fDouglas Gregor
1231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumppublic:
124f7c2aa0b049272d8f318988c1965760dcb852578Douglas Gregor  QualType getType() const { return TR; }
1251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  void setType(QualType t) {
1269d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor    // In C++, the type of an expression is always adjusted so that it
1279d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor    // will not have reference type an expression will never have
1289d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor    // reference type (C++ [expr]p6). Use
1299d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor    // QualType::getNonReferenceType() to retrieve the non-reference
1309d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor    // type. Additionally, inspect Expr::isLvalue to determine whether
1319d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor    // an expression that is adjusted in this manner should be
1329d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor    // considered an lvalue.
133905d11d53aeb6b26744f44fedc2b2820c7a62df6Douglas Gregor    assert((t.isNull() || !t->isReferenceType()) &&
1348320aaaa01d931aa234fc3bce05b399ef41898d5Daniel Dunbar           "Expressions can't have reference type");
135f7c2aa0b049272d8f318988c1965760dcb852578Douglas Gregor
1361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    TR = t;
1379d293dfc0ad7c44ae0b5eb9517f1ed8c8d8b7ff7Douglas Gregor  }
13877ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek
139898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  /// isValueDependent - Determines whether this expression is
140898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  /// value-dependent (C++ [temp.dep.constexpr]). For example, the
141898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  /// array bound of "Chars" in the following example is
1421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// value-dependent.
143898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  /// @code
144898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  /// template<int Size, char (&Chars)[Size]> struct meta_string;
145898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  /// @endcode
1468e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall  bool isValueDependent() const { return ExprBits.ValueDependent; }
147898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
1480b7489194f9f89fac39d57211c1e7953ae50251fDouglas Gregor  /// \brief Set whether this expression is value-dependent or not.
149ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  void setValueDependent(bool VD) {
150ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    ExprBits.ValueDependent = VD;
151561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (VD)
152561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      ExprBits.InstantiationDependent = true;
153561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  }
1540b7489194f9f89fac39d57211c1e7953ae50251fDouglas Gregor
155898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  /// isTypeDependent - Determines whether this expression is
156898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  /// type-dependent (C++ [temp.dep.expr]), which means that its type
157898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  /// could change from one template instantiation to the next. For
158898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  /// example, the expressions "x" and "x + y" are type-dependent in
159898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  /// the following code, but "y" is not type-dependent:
160898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  /// @code
1611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// template<typename T>
162898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  /// void add(T x, int y) {
163898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  ///   x + y;
164898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  /// }
165898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  /// @endcode
1668e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall  bool isTypeDependent() const { return ExprBits.TypeDependent; }
167898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
1680b7489194f9f89fac39d57211c1e7953ae50251fDouglas Gregor  /// \brief Set whether this expression is type-dependent or not.
169ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  void setTypeDependent(bool TD) {
170ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    ExprBits.TypeDependent = TD;
171561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (TD)
172561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      ExprBits.InstantiationDependent = true;
173561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  }
174561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor
175561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  /// \brief Whether this expression is instantiation-dependent, meaning that
176561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  /// it depends in some way on a template parameter, even if neither its type
177561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  /// nor (constant) value can change due to the template instantiation.
178561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  ///
179561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  /// In the following example, the expression \c sizeof(sizeof(T() + T())) is
180561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  /// instantiation-dependent (since it involves a template parameter \c T), but
181561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  /// is neither type- nor value-dependent, since the type of the inner
182561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  /// \c sizeof is known (\c std::size_t) and therefore the size of the outer
183561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  /// \c sizeof is known.
184561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  ///
185561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  /// \code
186561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  /// template<typename T>
187561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  /// void f(T x, T y) {
188561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  ///   sizeof(sizeof(T() + T());
189561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  /// }
190561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  /// \endcode
191561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  ///
192ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  bool isInstantiationDependent() const {
193ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    return ExprBits.InstantiationDependent;
194561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  }
195ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
196561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  /// \brief Set whether this expression is instantiation-dependent or not.
197ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  void setInstantiationDependent(bool ID) {
198561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    ExprBits.InstantiationDependent = ID;
199561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  }
2000b7489194f9f89fac39d57211c1e7953ae50251fDouglas Gregor
201d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor  /// \brief Whether this expression contains an unexpanded parameter
2024e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith  /// pack (for C++11 variadic templates).
203d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor  ///
204d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor  /// Given the following function template:
205d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor  ///
206d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor  /// \code
207d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor  /// template<typename F, typename ...Types>
208d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor  /// void forward(const F &f, Types &&...args) {
209d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor  ///   f(static_cast<Types&&>(args)...);
210d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor  /// }
211d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor  /// \endcode
212d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor  ///
213d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor  /// The expressions \c args and \c static_cast<Types&&>(args) both
214d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor  /// contain parameter packs.
215ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  bool containsUnexpandedParameterPack() const {
216ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    return ExprBits.ContainsUnexpandedParameterPack;
217d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor  }
218d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor
219bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  /// \brief Set the bit that describes whether this expression
220bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  /// contains an unexpanded parameter pack.
221bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  void setContainsUnexpandedParameterPack(bool PP = true) {
222bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    ExprBits.ContainsUnexpandedParameterPack = PP;
223bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor  }
224bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor
2255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getExprLoc - Return the preferred location for the arrow when diagnosing
2265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// a problem with a generic expression.
2274f311183be3d923da9fbe8702c453688b4c426a9Daniel Dunbar  SourceLocation getExprLoc() const LLVM_READONLY;
2281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
229026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner  /// isUnusedResultAWarning - Return true if this immediate expression should
230a6115068cde719142eb394db88612c185cabd05bEli Friedman  /// be warned about if the result is unused.  If so, fill in expr, location,
231a6115068cde719142eb394db88612c185cabd05bEli Friedman  /// and ranges with expr to warn on and source locations/ranges appropriate
232a6115068cde719142eb394db88612c185cabd05bEli Friedman  /// for a warning.
233a6115068cde719142eb394db88612c185cabd05bEli Friedman  bool isUnusedResultAWarning(const Expr *&WarnExpr, SourceLocation &Loc,
234a6115068cde719142eb394db88612c185cabd05bEli Friedman                              SourceRange &R1, SourceRange &R2,
235a6115068cde719142eb394db88612c185cabd05bEli Friedman                              ASTContext &Ctx) const;
2361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2377eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall  /// isLValue - True if this expression is an "l-value" according to
2387eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall  /// the rules of the current language.  C and C++ give somewhat
2397eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall  /// different rules for this concept, but in general, the result of
2407eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall  /// an l-value expression identifies a specific object whereas the
2417eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall  /// result of an r-value expression is a value detached from any
2427eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall  /// specific storage.
2435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
2444e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith  /// C++11 divides the concept of "r-value" into pure r-values
2457eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall  /// ("pr-values") and so-called expiring values ("x-values"), which
2467eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall  /// identify specific objects that can be safely cannibalized for
2477eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall  /// their resources.  This is an unfortunate abuse of terminology on
2487eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall  /// the part of the C++ committee.  In Clang, when we say "r-value",
2497eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall  /// we generally mean a pr-value.
2507eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall  bool isLValue() const { return getValueKind() == VK_LValue; }
2517eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall  bool isRValue() const { return getValueKind() == VK_RValue; }
2527eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall  bool isXValue() const { return getValueKind() == VK_XValue; }
2537eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall  bool isGLValue() const { return getValueKind() != VK_RValue; }
2547eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall
2557eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall  enum LValueClassification {
2565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    LV_Valid,
2575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    LV_NotObjectType,
2585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    LV_IncompleteVoidType,
259fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff    LV_DuplicateVectorComponents,
26086f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor    LV_InvalidExpression,
261077f490d0a514dcc448475f33f799934252b85a7Fariborz Jahanian    LV_InvalidMessageExpression,
2622514a309204341798f96912ce7a90841bea59727Fariborz Jahanian    LV_MemberFunction,
263e9ff443040cb571ae2c5c2626c4dc9a9a812d84aFariborz Jahanian    LV_SubObjCPropertySetting,
26436d02af300a207242f0486b4255420d8be796b21Richard Smith    LV_ClassTemporary,
26536d02af300a207242f0486b4255420d8be796b21Richard Smith    LV_ArrayTemporary
2665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  };
2677eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall  /// Reasons why an expression might not be an l-value.
2687eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall  LValueClassification ClassifyLValue(ASTContext &Ctx) const;
26953202857c60214d80950a975e6e52aebf30bd16aEli Friedman
2705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  enum isModifiableLvalueResult {
2715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    MLV_Valid,
2725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    MLV_NotObjectType,
2735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    MLV_IncompleteVoidType,
274fec0b49c3fa621fbf63e420f3d54a5bb3a0265d2Steve Naroff    MLV_DuplicateVectorComponents,
2755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    MLV_InvalidExpression,
276ca354faa7e9b99af17070c82b9662a5fca76422cChris Lattner    MLV_LValueCast,           // Specialized form of MLV_InvalidExpression.
2775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    MLV_IncompleteType,
2785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    MLV_ConstQualified,
2794f6a7d7ead09b439216c32f2de806a998aeb222aSteve Naroff    MLV_ArrayType,
28086f194083504938df72135b5b66bf0c5cafd9498Douglas Gregor    MLV_NoSetterProperty,
2812514a309204341798f96912ce7a90841bea59727Fariborz Jahanian    MLV_MemberFunction,
282e9ff443040cb571ae2c5c2626c4dc9a9a812d84aFariborz Jahanian    MLV_SubObjCPropertySetting,
283077f490d0a514dcc448475f33f799934252b85a7Fariborz Jahanian    MLV_InvalidMessageExpression,
28436d02af300a207242f0486b4255420d8be796b21Richard Smith    MLV_ClassTemporary,
28536d02af300a207242f0486b4255420d8be796b21Richard Smith    MLV_ArrayTemporary
2865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  };
287ad0fe03b897f9486191e75c8d90c3ffa9b4fd6a5Ted Kremenek  /// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
288ad0fe03b897f9486191e75c8d90c3ffa9b4fd6a5Ted Kremenek  /// does not have an incomplete type, does not have a const-qualified type,
289ad0fe03b897f9486191e75c8d90c3ffa9b4fd6a5Ted Kremenek  /// and if it is a structure or union, does not have any member (including,
290ad0fe03b897f9486191e75c8d90c3ffa9b4fd6a5Ted Kremenek  /// recursively, any member or element of all contained aggregates or unions)
291ad0fe03b897f9486191e75c8d90c3ffa9b4fd6a5Ted Kremenek  /// with a const-qualified type.
292ad0fe03b897f9486191e75c8d90c3ffa9b4fd6a5Ted Kremenek  ///
293ad0fe03b897f9486191e75c8d90c3ffa9b4fd6a5Ted Kremenek  /// \param Loc [in,out] - A source location which *may* be filled
294ad0fe03b897f9486191e75c8d90c3ffa9b4fd6a5Ted Kremenek  /// in with the location of the expression making this a
295ad0fe03b897f9486191e75c8d90c3ffa9b4fd6a5Ted Kremenek  /// non-modifiable lvalue, if specified.
2966bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  isModifiableLvalueResult
2976bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc = nullptr) const;
2981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2994e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith  /// \brief The return type of classify(). Represents the C++11 expression
3002111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl  ///        taxonomy.
3012111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl  class Classification {
3022111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl  public:
3032111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl    /// \brief The various classification results. Most of these mean prvalue.
3042111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl    enum Kinds {
3052111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl      CL_LValue,
3062111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl      CL_XValue,
3072111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl      CL_Function, // Functions cannot be lvalues in C.
3082111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl      CL_Void, // Void cannot be an lvalue in C.
3091c860d5640e8eebb11a5d515a761242b66761b0bPeter Collingbourne      CL_AddressableVoid, // Void expression whose address can be taken in C.
3102111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl      CL_DuplicateVectorComponents, // A vector shuffle with dupes.
3112111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl      CL_MemberFunction, // An expression referring to a member function
3122111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl      CL_SubObjCPropertySetting,
31336d02af300a207242f0486b4255420d8be796b21Richard Smith      CL_ClassTemporary, // A temporary of class type, or subobject thereof.
31436d02af300a207242f0486b4255420d8be796b21Richard Smith      CL_ArrayTemporary, // A temporary of array type.
315077f490d0a514dcc448475f33f799934252b85a7Fariborz Jahanian      CL_ObjCMessageRValue, // ObjC message is an rvalue
3162111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl      CL_PRValue // A prvalue for any other reason, of any other type
3172111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl    };
3182111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl    /// \brief The results of modification testing.
3192111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl    enum ModifiableType {
3202111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl      CM_Untested, // testModifiable was false.
3212111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl      CM_Modifiable,
3222111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl      CM_RValue, // Not modifiable because it's an rvalue
3232111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl      CM_Function, // Not modifiable because it's a function; C++ only
3242111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl      CM_LValueCast, // Same as CM_RValue, but indicates GCC cast-as-lvalue ext
3252111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl      CM_NoSetterProperty,// Implicit assignment to ObjC property without setter
3262111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl      CM_ConstQualified,
3272111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl      CM_ArrayType,
3282111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl      CM_IncompleteType
3292111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl    };
3302111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl
3312111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl  private:
3322111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl    friend class Expr;
3332111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl
3342111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl    unsigned short Kind;
3352111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl    unsigned short Modifiable;
3362111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl
3372111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl    explicit Classification(Kinds k, ModifiableType m)
3382111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl      : Kind(k), Modifiable(m)
3392111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl    {}
3402111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl
3412111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl  public:
3422111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl    Classification() {}
3432111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl
3442111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl    Kinds getKind() const { return static_cast<Kinds>(Kind); }
3452111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl    ModifiableType getModifiable() const {
3462111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl      assert(Modifiable != CM_Untested && "Did not test for modifiability.");
3472111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl      return static_cast<ModifiableType>(Modifiable);
3482111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl    }
3492111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl    bool isLValue() const { return Kind == CL_LValue; }
3502111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl    bool isXValue() const { return Kind == CL_XValue; }
3512111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl    bool isGLValue() const { return Kind <= CL_XValue; }
3522111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl    bool isPRValue() const { return Kind >= CL_Function; }
3532111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl    bool isRValue() const { return Kind >= CL_XValue; }
3542111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl    bool isModifiable() const { return getModifiable() == CM_Modifiable; }
355ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
3562c9a03f3b249e4d9d76eadf758a33142adc4d0a4Douglas Gregor    /// \brief Create a simple, modifiably lvalue
3572c9a03f3b249e4d9d76eadf758a33142adc4d0a4Douglas Gregor    static Classification makeSimpleLValue() {
3582c9a03f3b249e4d9d76eadf758a33142adc4d0a4Douglas Gregor      return Classification(CL_LValue, CM_Modifiable);
3592c9a03f3b249e4d9d76eadf758a33142adc4d0a4Douglas Gregor    }
360ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
3612111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl  };
3624e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith  /// \brief Classify - Classify this expression according to the C++11
3632111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl  ///        expression taxonomy.
3642111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl  ///
3654e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith  /// C++11 defines ([basic.lval]) a new taxonomy of expressions to replace the
3662111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl  /// old lvalue vs rvalue. This function determines the type of expression this
3672111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl  /// is. There are three expression types:
3682111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl  /// - lvalues are classical lvalues as in C++03.
3692111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl  /// - prvalues are equivalent to rvalues in C++03.
3702111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl  /// - xvalues are expressions yielding unnamed rvalue references, e.g. a
3712111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl  ///   function returning an rvalue reference.
3722111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl  /// lvalues and xvalues are collectively referred to as glvalues, while
3732111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl  /// prvalues and xvalues together form rvalues.
3742111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl  Classification Classify(ASTContext &Ctx) const {
3756bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return ClassifyImpl(Ctx, nullptr);
3762111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl  }
3772111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl
378369e51fa400aeb5835bb9af4634ea516c11429a7Sebastian Redl  /// \brief ClassifyModifiable - Classify this expression according to the
3794e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith  ///        C++11 expression taxonomy, and see if it is valid on the left side
3802111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl  ///        of an assignment.
3812111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl  ///
3822111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl  /// This function extends classify in that it also tests whether the
3832111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl  /// expression is modifiable (C99 6.3.2.1p1).
3842111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl  /// \param Loc A source location that might be filled with a relevant location
3852111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl  ///            if the expression is not modifiable.
3862111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl  Classification ClassifyModifiable(ASTContext &Ctx, SourceLocation &Loc) const{
3872111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl    return ClassifyImpl(Ctx, &Loc);
3882111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl  }
3892111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl
390f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  /// getValueKindForType - Given a formal return or parameter type,
391f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  /// give its value kind.
392f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  static ExprValueKind getValueKindForType(QualType T) {
393f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall    if (const ReferenceType *RT = T->getAs<ReferenceType>())
3940943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall      return (isa<LValueReferenceType>(RT)
3950943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall                ? VK_LValue
3960943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall                : (RT->getPointeeType()->isFunctionType()
3970943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall                     ? VK_LValue : VK_XValue));
398f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall    return VK_RValue;
399f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  }
400f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall
401f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  /// getValueKind - The value kind that this expression produces.
402f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  ExprValueKind getValueKind() const {
403f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall    return static_cast<ExprValueKind>(ExprBits.ValueKind);
404f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  }
405f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall
406f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  /// getObjectKind - The object kind that this expression produces.
407f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  /// Object kinds are meaningful only for expressions that yield an
408f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  /// l-value or x-value.
409f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  ExprObjectKind getObjectKind() const {
410f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall    return static_cast<ExprObjectKind>(ExprBits.ObjectKind);
411f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  }
412f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall
41356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  bool isOrdinaryOrBitFieldObject() const {
41456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    ExprObjectKind OK = getObjectKind();
41556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    return (OK == OK_Ordinary || OK == OK_BitField);
41656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  }
41756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
418f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  /// setValueKind - Set the value kind produced by this expression.
419f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  void setValueKind(ExprValueKind Cat) { ExprBits.ValueKind = Cat; }
420f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall
421f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  /// setObjectKind - Set the object kind produced by this expression.
422f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  void setObjectKind(ExprObjectKind Cat) { ExprBits.ObjectKind = Cat; }
423f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall
4242111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redlprivate:
4252111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl  Classification ClassifyImpl(ASTContext &Ctx, SourceLocation *Loc) const;
4262111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl
4272111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redlpublic:
4282111c855343a0530e236bf0862358ec8d67b28f3Sebastian Redl
429993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall  /// \brief Returns true if this expression is a gl-value that
430993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall  /// potentially refers to a bit-field.
431993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall  ///
432993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall  /// In C++, whether a gl-value refers to a bitfield is essentially
433993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall  /// an aspect of the value-kind type system.
434993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall  bool refersToBitField() const { return getObjectKind() == OK_BitField; }
435993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall
43633bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor  /// \brief If this expression refers to a bit-field, retrieve the
43733bbbc5ec8269bc2cde5b84f970fa49319a30267Douglas Gregor  /// declaration of that bit-field.
438993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall  ///
439993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall  /// Note that this returns a non-null pointer in subtly different
440993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall  /// places than refersToBitField returns true.  In particular, this can
441993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall  /// return a non-null pointer even for r-values loaded from
442993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall  /// bit-fields, but it will return null for a conditional bit-field.
443993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall  FieldDecl *getSourceBitField();
444993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall
445993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall  const FieldDecl *getSourceBitField() const {
446993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall    return const_cast<Expr*>(this)->getSourceBitField();
44738d068e8f13a119b89a3b8b0f79f35cab1ffd09aAnders Carlsson  }
4481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
449f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  /// \brief If this expression is an l-value for an Objective C
450f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  /// property, find the underlying property reference expression.
451f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  const ObjCPropertyRefExpr *getObjCProperty() const;
452f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall
453bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaks  /// \brief Check if this expression is the ObjC 'self' implicit parameter.
454bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaks  bool isObjCSelfExpr() const;
455bbff82f302a1dd67589f65912351978905f0c5a7Anna Zaks
456093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  /// \brief Returns whether this expression refers to a vector element.
457093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  bool refersToVectorElement() const;
4584919dfd54e2296ca997e3d1c9dab85976bba8e95John McCall
4594919dfd54e2296ca997e3d1c9dab85976bba8e95John McCall  /// \brief Returns whether this expression has a placeholder type.
4604919dfd54e2296ca997e3d1c9dab85976bba8e95John McCall  bool hasPlaceholderType() const {
4614919dfd54e2296ca997e3d1c9dab85976bba8e95John McCall    return getType()->isPlaceholderType();
4624919dfd54e2296ca997e3d1c9dab85976bba8e95John McCall  }
4634919dfd54e2296ca997e3d1c9dab85976bba8e95John McCall
4644919dfd54e2296ca997e3d1c9dab85976bba8e95John McCall  /// \brief Returns whether this expression has a specific placeholder type.
4654919dfd54e2296ca997e3d1c9dab85976bba8e95John McCall  bool hasPlaceholderType(BuiltinType::Kind K) const {
46685def357129b6cdfd69a66ad0e6994506418a2a9John McCall    assert(BuiltinType::isPlaceholderTypeKind(K));
4674919dfd54e2296ca997e3d1c9dab85976bba8e95John McCall    if (const BuiltinType *BT = dyn_cast<BuiltinType>(getType()))
4684919dfd54e2296ca997e3d1c9dab85976bba8e95John McCall      return BT->getKind() == K;
4694919dfd54e2296ca997e3d1c9dab85976bba8e95John McCall    return false;
4704919dfd54e2296ca997e3d1c9dab85976bba8e95John McCall  }
471ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
4722b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner  /// isKnownToHaveBooleanValue - Return true if this is an integer expression
4732b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner  /// that is known to return 0 or 1.  This happens for _Bool/bool expressions
4742b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner  /// but also int expressions which are produced by things like comparisons in
4752b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner  /// C.
4762b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner  bool isKnownToHaveBooleanValue() const;
477ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
4785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// isIntegerConstantExpr - Return true if this expression is a valid integer
4795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// constant expression, and, if so, return its value in Result.  If not a
4805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// valid i-c-e, return false and fill in Loc (if specified) with the location
4815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// of the invalid expression.
482282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  ///
483282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  /// Note: This does not perform the implicit conversions required by C++11
484282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  /// [expr.const]p5.
48532b5a1e82f535d43e94332183cd330f4a39b2dbdCraig Topper  bool isIntegerConstantExpr(llvm::APSInt &Result, const ASTContext &Ctx,
4866bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                             SourceLocation *Loc = nullptr,
4875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                             bool isEvaluated = true) const;
48832b5a1e82f535d43e94332183cd330f4a39b2dbdCraig Topper  bool isIntegerConstantExpr(const ASTContext &Ctx,
4896bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                             SourceLocation *Loc = nullptr) const;
4900e35b4ecee380c2b4c33d75da6bc2fb6f6bc7df3Richard Smith
49170488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smith  /// isCXX98IntegralConstantExpr - Return true if this expression is an
49270488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smith  /// integral constant expression in C++98. Can only be used in C++.
49332b5a1e82f535d43e94332183cd330f4a39b2dbdCraig Topper  bool isCXX98IntegralConstantExpr(const ASTContext &Ctx) const;
49470488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smith
4954c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  /// isCXX11ConstantExpr - Return true if this expression is a constant
4964c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  /// expression in C++11. Can only be used in C++.
497282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  ///
498282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  /// Note: This does not perform the implicit conversions required by C++11
499282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  /// [expr.const]p5.
5006bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  bool isCXX11ConstantExpr(const ASTContext &Ctx, APValue *Result = nullptr,
5016bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                           SourceLocation *Loc = nullptr) const;
5024c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith
503745f5147e065900267c85a5568785a1991d4838fRichard Smith  /// isPotentialConstantExpr - Return true if this function's definition
504745f5147e065900267c85a5568785a1991d4838fRichard Smith  /// might be usable in a constant expression in C++11, if it were marked
505745f5147e065900267c85a5568785a1991d4838fRichard Smith  /// constexpr. Return false if the function can never produce a constant
506745f5147e065900267c85a5568785a1991d4838fRichard Smith  /// expression, along with diagnostics describing why not.
507745f5147e065900267c85a5568785a1991d4838fRichard Smith  static bool isPotentialConstantExpr(const FunctionDecl *FD,
508cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko                                      SmallVectorImpl<
509745f5147e065900267c85a5568785a1991d4838fRichard Smith                                        PartialDiagnosticAt> &Diags);
510745f5147e065900267c85a5568785a1991d4838fRichard Smith
511651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// isPotentialConstantExprUnevaluted - Return true if this expression might
512651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// be usable in a constant expression in C++11 in an unevaluated context, if
513651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// it were in function FD marked constexpr. Return false if the function can
514651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// never produce a constant expression, along with diagnostics describing
515651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// why not.
516651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  static bool isPotentialConstantExprUnevaluated(Expr *E,
517651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                                 const FunctionDecl *FD,
518651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                                 SmallVectorImpl<
519651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                                   PartialDiagnosticAt> &Diags);
520651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
5210e35b4ecee380c2b4c33d75da6bc2fb6f6bc7df3Richard Smith  /// isConstantInitializer - Returns true if this expression can be emitted to
5220e35b4ecee380c2b4c33d75da6bc2fb6f6bc7df3Richard Smith  /// IR as a constant, and thus can be used as a constant initializer in C.
5236bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  /// If this expression is not constant and Culprit is non-null,
5246bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  /// it is used to store the address of first non constant expr.
5256bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  bool isConstantInitializer(ASTContext &Ctx, bool ForRef,
5266bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                             const Expr **Culprit = nullptr) const;
5271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5281e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith  /// EvalStatus is a struct with detailed info about an evaluation in progress.
5291e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith  struct EvalStatus {
53094deaf675ae60e11c2d9475c6dbfd0c7123160f5Anders Carlsson    /// HasSideEffects - Whether the evaluated expression has side effects.
53194deaf675ae60e11c2d9475c6dbfd0c7123160f5Anders Carlsson    /// For example, (f() && 0) can be folded, but it still has side effects.
53294deaf675ae60e11c2d9475c6dbfd0c7123160f5Anders Carlsson    bool HasSideEffects;
5331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
534dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    /// Diag - If this is non-null, it will be filled in with a stack of notes
535dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    /// indicating why evaluation failed (or why it failed to produce a constant
536dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    /// expression).
537dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    /// If the expression is unfoldable, the notes will indicate why it's not
538dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    /// foldable. If the expression is foldable, but not a constant expression,
539dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    /// the notes will describes why it isn't a constant expression. If the
540dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    /// expression *is* a constant expression, no notes will be produced.
541cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko    SmallVectorImpl<PartialDiagnosticAt> *Diag;
542dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith
5436bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    EvalStatus() : HasSideEffects(false), Diag(nullptr) {}
544e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara
545e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara    // hasSideEffects - Return true if the evaluated expression has
546e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara    // side effects.
547e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara    bool hasSideEffects() const {
548e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara      return HasSideEffects;
549e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara    }
55094deaf675ae60e11c2d9475c6dbfd0c7123160f5Anders Carlsson  };
55194deaf675ae60e11c2d9475c6dbfd0c7123160f5Anders Carlsson
5521e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith  /// EvalResult is a struct with detailed info about an evaluated expression.
5531e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith  struct EvalResult : EvalStatus {
5541e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    /// Val - This is the value the expression can be folded to.
5551e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    APValue Val;
5561e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith
5571e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    // isGlobalLValue - Return true if the evaluated lvalue expression
5581e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    // is global.
5591e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    bool isGlobalLValue() const;
5601e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith  };
5611e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith
56251f4708c00110940ca3f337961915f2ca1668375Richard Smith  /// EvaluateAsRValue - Return true if this is a constant which we can fold to
56351f4708c00110940ca3f337961915f2ca1668375Richard Smith  /// an rvalue using any crazy technique (that has nothing to do with language
56451f4708c00110940ca3f337961915f2ca1668375Richard Smith  /// standards) that we want to, even if the expression has side-effects. If
56551f4708c00110940ca3f337961915f2ca1668375Richard Smith  /// this function returns true, it returns the folded constant in Result. If
56651f4708c00110940ca3f337961915f2ca1668375Richard Smith  /// the expression is a glvalue, an lvalue-to-rvalue conversion will be
56751f4708c00110940ca3f337961915f2ca1668375Richard Smith  /// applied.
56851f4708c00110940ca3f337961915f2ca1668375Richard Smith  bool EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx) const;
5695b45d4ef1ea3f04ec863daf8aa29be6c6e021750Anders Carlsson
570cd7a445c6b46c5585580dfb652300c8483c0cb6bJohn McCall  /// EvaluateAsBooleanCondition - Return true if this is a constant
571cd7a445c6b46c5585580dfb652300c8483c0cb6bJohn McCall  /// which we we can fold and convert to a boolean condition using
572a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith  /// any crazy technique that we want to, even if the expression has
573a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith  /// side-effects.
5744ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad  bool EvaluateAsBooleanCondition(bool &Result, const ASTContext &Ctx) const;
575cd7a445c6b46c5585580dfb652300c8483c0cb6bJohn McCall
57680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  enum SideEffectsKind { SE_NoSideEffects, SE_AllowSideEffects };
57780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith
578a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith  /// EvaluateAsInt - Return true if this is a constant which we can fold and
57980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  /// convert to an integer, using any crazy technique that we want to.
58080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  bool EvaluateAsInt(llvm::APSInt &Result, const ASTContext &Ctx,
58180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith                     SideEffectsKind AllowSideEffects = SE_NoSideEffects) const;
582a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith
58351f4708c00110940ca3f337961915f2ca1668375Richard Smith  /// isEvaluatable - Call EvaluateAsRValue to see if this expression can be
58451f4708c00110940ca3f337961915f2ca1668375Richard Smith  /// constant folded without side-effects, but discard the result.
5854ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad  bool isEvaluatable(const ASTContext &Ctx) const;
586c44eec6dd29ee9415cbd38a35deff4c8b67abb6aAnders Carlsson
5876ddf4784a22b994b954ed74c6061d4603d474639Fariborz Jahanian  /// HasSideEffects - This routine returns true for all those expressions
58860b70388e43d146d968a1cc0705b30cb2d7263feRichard Smith  /// which have any effect other than producing a value. Example is a function
58960b70388e43d146d968a1cc0705b30cb2d7263feRichard Smith  /// call, volatile variable read, or throwing an exception.
5904ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad  bool HasSideEffects(const ASTContext &Ctx) const;
59151f4708c00110940ca3f337961915f2ca1668375Richard Smith
59225d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor  /// \brief Determine whether this expression involves a call to any function
59325d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor  /// that is not trivial.
59425d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor  bool hasNonTrivialCall(ASTContext &Ctx);
5950004329758b99d2b92096b353e35c427ebbee622Richard Smith
59651f4708c00110940ca3f337961915f2ca1668375Richard Smith  /// EvaluateKnownConstInt - Call EvaluateAsRValue and return the folded
59751f4708c00110940ca3f337961915f2ca1668375Richard Smith  /// integer. This must be called on an expression that constant folds to an
59851f4708c00110940ca3f337961915f2ca1668375Richard Smith  /// integer.
599a18e70b25c85d7e653e642b5e6e58d6063af3d83Fariborz Jahanian  llvm::APSInt EvaluateKnownConstInt(const ASTContext &Ctx,
6006bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                    SmallVectorImpl<PartialDiagnosticAt> *Diag = nullptr) const;
6010004329758b99d2b92096b353e35c427ebbee622Richard Smith
6020004329758b99d2b92096b353e35c427ebbee622Richard Smith  void EvaluateForOverflow(const ASTContext &Ctx) const;
60351fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson
60451f4708c00110940ca3f337961915f2ca1668375Richard Smith  /// EvaluateAsLValue - Evaluate an expression to see if we can fold it to an
60551f4708c00110940ca3f337961915f2ca1668375Richard Smith  /// lvalue with link time known address, with no side-effects.
6064ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad  bool EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx) const;
6071b78276a75a5a0f496a82429c1ff9604d622a76dAnders Carlsson
608099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  /// EvaluateAsInitializer - Evaluate an expression as if it were the
609099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  /// initializer of the given declaration. Returns true if the initializer
610099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  /// can be folded to a constant, and produces any relevant notes. In C++11,
611099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  /// notes will be produced if the expression is not a constant expression.
612099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  bool EvaluateAsInitializer(APValue &Result, const ASTContext &Ctx,
613099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith                             const VarDecl *VD,
614cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko                             SmallVectorImpl<PartialDiagnosticAt> &Notes) const;
615099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
616651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// EvaluateWithSubstitution - Evaluate an expression as if from the context
617651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// of a call to the given function with the given arguments, inside an
618651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// unevaluated context. Returns true if the expression could be folded to a
619651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// constant.
620651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  bool EvaluateWithSubstitution(APValue &Value, ASTContext &Ctx,
621651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                const FunctionDecl *Callee,
622ef8225444452a1486bd721f3285301fe84643b00Stephen Hines                                ArrayRef<const Expr*> Args) const;
623651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
62482214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth  /// \brief Enumeration used to describe the kind of Null pointer constant
62582214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth  /// returned from \c isNullPointerConstant().
62682214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth  enum NullPointerConstantKind {
62782214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth    /// \brief Expression is not a Null pointer constant.
62882214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth    NPCK_NotNull = 0,
62982214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth
63050800fc551ac6b8a95cca662223e7f061bbd169aDavid Blaikie    /// \brief Expression is a Null pointer constant built from a zero integer
63150800fc551ac6b8a95cca662223e7f061bbd169aDavid Blaikie    /// expression that is not a simple, possibly parenthesized, zero literal.
63250800fc551ac6b8a95cca662223e7f061bbd169aDavid Blaikie    /// C++ Core Issue 903 will classify these expressions as "not pointers"
63350800fc551ac6b8a95cca662223e7f061bbd169aDavid Blaikie    /// once it is adopted.
63450800fc551ac6b8a95cca662223e7f061bbd169aDavid Blaikie    /// http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903
63550800fc551ac6b8a95cca662223e7f061bbd169aDavid Blaikie    NPCK_ZeroExpression,
63650800fc551ac6b8a95cca662223e7f061bbd169aDavid Blaikie
63750800fc551ac6b8a95cca662223e7f061bbd169aDavid Blaikie    /// \brief Expression is a Null pointer constant built from a literal zero.
63850800fc551ac6b8a95cca662223e7f061bbd169aDavid Blaikie    NPCK_ZeroLiteral,
63982214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth
6404e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith    /// \brief Expression is a C++11 nullptr.
6414e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith    NPCK_CXX11_nullptr,
64282214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth
64382214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth    /// \brief Expression is a GNU-style __null constant.
64482214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth    NPCK_GNUNull
64582214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth  };
64682214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth
647ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor  /// \brief Enumeration used to describe how \c isNullPointerConstant()
648ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor  /// should cope with value-dependent expressions.
649ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor  enum NullPointerConstantValueDependence {
650ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    /// \brief Specifies that the expression should never be value-dependent.
651ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    NPC_NeverValueDependent = 0,
652ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
653ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    /// \brief Specifies that a value-dependent expression of integral or
654ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    /// dependent type should be considered a null pointer constant.
655ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    NPC_ValueDependentIsNull,
656ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
657ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    /// \brief Specifies that a value-dependent expression should be considered
658ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    /// to never be a null pointer constant.
659ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor    NPC_ValueDependentIsNotNull
660ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor  };
661ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
66282214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth  /// isNullPointerConstant - C99 6.3.2.3p3 - Test if this reduces down to
66382214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth  /// a Null pointer constant. The return value can further distinguish the
66482214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth  /// kind of NULL pointer constant that was detected.
66582214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth  NullPointerConstantKind isNullPointerConstant(
66682214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth      ASTContext &Ctx,
66782214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth      NullPointerConstantValueDependence NPC) const;
668efa9b3877ef298bcb792600ac33521827e1f7fafAnders Carlsson
66944baa8abba2a1552b6b50bf750a8750ab9da9f76Fariborz Jahanian  /// isOBJCGCCandidate - Return true if this expression may be used in a read/
6701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// write barrier.
671102e390bcb5a1fb1a8fdbc8505e6dfd905374bbdFariborz Jahanian  bool isOBJCGCCandidate(ASTContext &Ctx) const;
6721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
67311ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidis  /// \brief Returns true if this expression is a bound member function.
67411ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidis  bool isBoundMemberFunction(ASTContext &Ctx) const;
67511ab79030938209f50691acae0ddb65e72a58ca9Argyrios Kyrtzidis
676864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  /// \brief Given an expression of bound-member type, find the type
677864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  /// of the member.  Returns null if this is an *overloaded* bound
678864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  /// member expression.
679864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  static QualType findBoundMemberType(const Expr *expr);
680864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall
68191a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall  /// IgnoreImpCasts - Skip past any implicit casts which might
68291a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall  /// surround this expression.  Only skips ImplicitCastExprs.
683f9ff5876289d5228c299b57416a62c8c3b848287Daniel Dunbar  Expr *IgnoreImpCasts() LLVM_READONLY;
68491a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall
6857e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall  /// IgnoreImplicit - Skip past any implicit AST nodes which might
686