ExprConstant.cpp revision 48def65d1cfbd020c5d4a7e542a00d63808c6060
1b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattner//===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===//
2c44eec6dd29ee9415cbd38a35deff4c8b67abb6aAnders Carlsson//
3c44eec6dd29ee9415cbd38a35deff4c8b67abb6aAnders Carlsson//                     The LLVM Compiler Infrastructure
4c44eec6dd29ee9415cbd38a35deff4c8b67abb6aAnders Carlsson//
5c44eec6dd29ee9415cbd38a35deff4c8b67abb6aAnders Carlsson// This file is distributed under the University of Illinois Open Source
6c44eec6dd29ee9415cbd38a35deff4c8b67abb6aAnders Carlsson// License. See LICENSE.TXT for details.
7c44eec6dd29ee9415cbd38a35deff4c8b67abb6aAnders Carlsson//
8c44eec6dd29ee9415cbd38a35deff4c8b67abb6aAnders Carlsson//===----------------------------------------------------------------------===//
9c44eec6dd29ee9415cbd38a35deff4c8b67abb6aAnders Carlsson//
10c44eec6dd29ee9415cbd38a35deff4c8b67abb6aAnders Carlsson// This file implements the Expr constant evaluator.
11c44eec6dd29ee9415cbd38a35deff4c8b67abb6aAnders Carlsson//
12745f5147e065900267c85a5568785a1991d4838fRichard Smith// Constant expression evaluation produces four main results:
13745f5147e065900267c85a5568785a1991d4838fRichard Smith//
14745f5147e065900267c85a5568785a1991d4838fRichard Smith//  * A success/failure flag indicating whether constant folding was successful.
15745f5147e065900267c85a5568785a1991d4838fRichard Smith//    This is the 'bool' return value used by most of the code in this file. A
16745f5147e065900267c85a5568785a1991d4838fRichard Smith//    'false' return value indicates that constant folding has failed, and any
17745f5147e065900267c85a5568785a1991d4838fRichard Smith//    appropriate diagnostic has already been produced.
18745f5147e065900267c85a5568785a1991d4838fRichard Smith//
19745f5147e065900267c85a5568785a1991d4838fRichard Smith//  * An evaluated result, valid only if constant folding has not failed.
20745f5147e065900267c85a5568785a1991d4838fRichard Smith//
21745f5147e065900267c85a5568785a1991d4838fRichard Smith//  * A flag indicating if evaluation encountered (unevaluated) side-effects.
22745f5147e065900267c85a5568785a1991d4838fRichard Smith//    These arise in cases such as (sideEffect(), 0) and (sideEffect() || 1),
23745f5147e065900267c85a5568785a1991d4838fRichard Smith//    where it is possible to determine the evaluated result regardless.
24745f5147e065900267c85a5568785a1991d4838fRichard Smith//
25745f5147e065900267c85a5568785a1991d4838fRichard Smith//  * A set of notes indicating why the evaluation was not a constant expression
26745f5147e065900267c85a5568785a1991d4838fRichard Smith//    (under the C++11 rules only, at the moment), or, if folding failed too,
27745f5147e065900267c85a5568785a1991d4838fRichard Smith//    why the expression could not be folded.
28745f5147e065900267c85a5568785a1991d4838fRichard Smith//
29745f5147e065900267c85a5568785a1991d4838fRichard Smith// If we are checking for a potential constant expression, failure to constant
30745f5147e065900267c85a5568785a1991d4838fRichard Smith// fold a potential constant sub-expression will be indicated by a 'false'
31745f5147e065900267c85a5568785a1991d4838fRichard Smith// return value (the expression could not be folded) and no diagnostic (the
32745f5147e065900267c85a5568785a1991d4838fRichard Smith// expression is not necessarily non-constant).
33745f5147e065900267c85a5568785a1991d4838fRichard Smith//
34c44eec6dd29ee9415cbd38a35deff4c8b67abb6aAnders Carlsson//===----------------------------------------------------------------------===//
35c44eec6dd29ee9415cbd38a35deff4c8b67abb6aAnders Carlsson
36c44eec6dd29ee9415cbd38a35deff4c8b67abb6aAnders Carlsson#include "clang/AST/APValue.h"
37c44eec6dd29ee9415cbd38a35deff4c8b67abb6aAnders Carlsson#include "clang/AST/ASTContext.h"
38199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck#include "clang/AST/CharUnits.h"
3919cc4abea06a9b49e0e16a50d335c064cd723572Anders Carlsson#include "clang/AST/RecordLayout.h"
400fe52e1bcaa69ba127f1bda036f057fec1f478deSeo Sanghyeon#include "clang/AST/StmtVisitor.h"
418ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor#include "clang/AST/TypeLoc.h"
42500d3297d2a21edeac4d46cbcbe21bc2352c2a28Chris Lattner#include "clang/AST/ASTDiagnostic.h"
438ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor#include "clang/AST/Expr.h"
441b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner#include "clang/Basic/Builtins.h"
4506a3675627e3b3c47b49c689c8e404a33144194aAnders Carlsson#include "clang/Basic/TargetInfo.h"
467462b39a9bccaf4392687831036713f09f9c0681Mike Stump#include "llvm/ADT/SmallString.h"
474572baba9d18c275968ac113fd73b0e3c77cccb8Mike Stump#include <cstring>
487b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith#include <functional>
494572baba9d18c275968ac113fd73b0e3c77cccb8Mike Stump
50c44eec6dd29ee9415cbd38a35deff4c8b67abb6aAnders Carlssonusing namespace clang;
51f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattnerusing llvm::APSInt;
52d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmanusing llvm::APFloat;
53c44eec6dd29ee9415cbd38a35deff4c8b67abb6aAnders Carlsson
5483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smithstatic bool IsGlobalLValue(APValue::LValueBase B);
5583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith
56c54061a679d8db4169438b2f97f81e3f443c6a25Benjamin Kramernamespace {
57180f47959a066795cc0f409433023af448bb0328Richard Smith  struct LValue;
58d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  struct CallStackFrame;
59bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith  struct EvalInfo;
60d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
6183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  static QualType getType(APValue::LValueBase B) {
621bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    if (!B) return QualType();
631bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>())
641bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith      return D->getType();
651bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    return B.get<const Expr*>()->getType();
661bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  }
671bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith
68180f47959a066795cc0f409433023af448bb0328Richard Smith  /// Get an LValue path entry, which is known to not be an array index, as a
69f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  /// field or base class.
7083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  static
71f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  APValue::BaseOrMemberType getAsBaseOrMember(APValue::LValuePathEntry E) {
72180f47959a066795cc0f409433023af448bb0328Richard Smith    APValue::BaseOrMemberType Value;
73180f47959a066795cc0f409433023af448bb0328Richard Smith    Value.setFromOpaqueValue(E.BaseOrMember);
74f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    return Value;
75f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  }
76f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
77f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  /// Get an LValue path entry, which is known to not be an array index, as a
78f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  /// field declaration.
7983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  static const FieldDecl *getAsField(APValue::LValuePathEntry E) {
80f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    return dyn_cast<FieldDecl>(getAsBaseOrMember(E).getPointer());
81180f47959a066795cc0f409433023af448bb0328Richard Smith  }
82180f47959a066795cc0f409433023af448bb0328Richard Smith  /// Get an LValue path entry, which is known to not be an array index, as a
83180f47959a066795cc0f409433023af448bb0328Richard Smith  /// base class declaration.
8483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  static const CXXRecordDecl *getAsBaseClass(APValue::LValuePathEntry E) {
85f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    return dyn_cast<CXXRecordDecl>(getAsBaseOrMember(E).getPointer());
86180f47959a066795cc0f409433023af448bb0328Richard Smith  }
87180f47959a066795cc0f409433023af448bb0328Richard Smith  /// Determine whether this LValue path entry for a base class names a virtual
88180f47959a066795cc0f409433023af448bb0328Richard Smith  /// base class.
8983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  static bool isVirtualBaseClass(APValue::LValuePathEntry E) {
90f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    return getAsBaseOrMember(E).getInt();
91180f47959a066795cc0f409433023af448bb0328Richard Smith  }
92180f47959a066795cc0f409433023af448bb0328Richard Smith
93b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  /// Find the path length and type of the most-derived subobject in the given
94b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  /// path, and find the size of the containing array, if any.
95b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  static
96b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  unsigned findMostDerivedSubobject(ASTContext &Ctx, QualType Base,
97b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                                    ArrayRef<APValue::LValuePathEntry> Path,
98b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                                    uint64_t &ArraySize, QualType &Type) {
99b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    unsigned MostDerivedLength = 0;
100b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    Type = Base;
1019a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith    for (unsigned I = 0, N = Path.size(); I != N; ++I) {
102b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      if (Type->isArrayType()) {
103b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        const ConstantArrayType *CAT =
104b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith          cast<ConstantArrayType>(Ctx.getAsArrayType(Type));
105b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        Type = CAT->getElementType();
106b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        ArraySize = CAT->getSize().getZExtValue();
107b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        MostDerivedLength = I + 1;
10886024013d4c3728122c58fa07a2a67e6c15837efRichard Smith      } else if (Type->isAnyComplexType()) {
10986024013d4c3728122c58fa07a2a67e6c15837efRichard Smith        const ComplexType *CT = Type->castAs<ComplexType>();
11086024013d4c3728122c58fa07a2a67e6c15837efRichard Smith        Type = CT->getElementType();
11186024013d4c3728122c58fa07a2a67e6c15837efRichard Smith        ArraySize = 2;
11286024013d4c3728122c58fa07a2a67e6c15837efRichard Smith        MostDerivedLength = I + 1;
113b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      } else if (const FieldDecl *FD = getAsField(Path[I])) {
114b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        Type = FD->getType();
115b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        ArraySize = 0;
116b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        MostDerivedLength = I + 1;
117b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      } else {
1189a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith        // Path[I] describes a base class.
119b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        ArraySize = 0;
120b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      }
1219a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith    }
122b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    return MostDerivedLength;
1239a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith  }
1249a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith
125b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // The order of this enum is important for diagnostics.
126b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  enum CheckSubobjectKind {
127b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith    CSK_Base, CSK_Derived, CSK_Field, CSK_ArrayToPointer, CSK_ArrayIndex,
12886024013d4c3728122c58fa07a2a67e6c15837efRichard Smith    CSK_This, CSK_Real, CSK_Imag
129b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  };
130b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
1310a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith  /// A path from a glvalue to a subobject of that glvalue.
1320a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith  struct SubobjectDesignator {
1330a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    /// True if the subobject was named in a manner not supported by C++11. Such
1340a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    /// lvalues can still be folded, but they are not core constant expressions
1350a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    /// and we cannot perform lvalue-to-rvalue conversions on them.
1360a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    bool Invalid : 1;
1370a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith
138b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    /// Is this a pointer one past the end of an object?
139b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    bool IsOnePastTheEnd : 1;
140b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
141b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    /// The length of the path to the most-derived object of which this is a
142b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    /// subobject.
143b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    unsigned MostDerivedPathLength : 30;
144b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
145b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    /// The size of the array of which the most-derived object is an element, or
146b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    /// 0 if the most-derived object is not an array element.
147b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    uint64_t MostDerivedArraySize;
1480a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith
149b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    /// The type of the most derived object referred to by this address.
150b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    QualType MostDerivedType;
1510a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith
1529a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith    typedef APValue::LValuePathEntry PathEntry;
1539a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith
1540a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    /// The entries on the path from the glvalue to the designated subobject.
1550a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    SmallVector<PathEntry, 8> Entries;
1560a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith
157b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    SubobjectDesignator() : Invalid(true) {}
1580a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith
159b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    explicit SubobjectDesignator(QualType T)
160b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      : Invalid(false), IsOnePastTheEnd(false), MostDerivedPathLength(0),
161b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        MostDerivedArraySize(0), MostDerivedType(T) {}
162b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
163b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    SubobjectDesignator(ASTContext &Ctx, const APValue &V)
164b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      : Invalid(!V.isLValue() || !V.hasLValuePath()), IsOnePastTheEnd(false),
165b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        MostDerivedPathLength(0), MostDerivedArraySize(0) {
1669a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith      if (!Invalid) {
167b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        IsOnePastTheEnd = V.isLValueOnePastTheEnd();
1689a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith        ArrayRef<PathEntry> VEntries = V.getLValuePath();
1699a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith        Entries.insert(Entries.end(), VEntries.begin(), VEntries.end());
1709a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith        if (V.getLValueBase())
171b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith          MostDerivedPathLength =
172b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith              findMostDerivedSubobject(Ctx, getType(V.getLValueBase()),
173b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                                       V.getLValuePath(), MostDerivedArraySize,
174b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                                       MostDerivedType);
1759a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith      }
1769a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith    }
1779a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith
1780a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    void setInvalid() {
1790a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      Invalid = true;
1800a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      Entries.clear();
1810a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    }
182b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
183b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    /// Determine whether this is a one-past-the-end pointer.
184b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    bool isOnePastTheEnd() const {
185b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      if (IsOnePastTheEnd)
186b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        return true;
187b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      if (MostDerivedArraySize &&
188b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith          Entries[MostDerivedPathLength - 1].ArrayIndex == MostDerivedArraySize)
189b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        return true;
190b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      return false;
191b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    }
192b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
193b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    /// Check that this refers to a valid subobject.
194b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    bool isValidSubobject() const {
195b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      if (Invalid)
196b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        return false;
197b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      return !isOnePastTheEnd();
198b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    }
199b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    /// Check that this refers to a valid subobject, and if not, produce a
200b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    /// relevant diagnostic and set the designator as invalid.
201b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK);
202b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
203b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    /// Update this designator to refer to the first element within this array.
204b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    void addArrayUnchecked(const ConstantArrayType *CAT) {
2050a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      PathEntry Entry;
206b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      Entry.ArrayIndex = 0;
2070a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      Entries.push_back(Entry);
208b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
209b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      // This is a most-derived object.
210b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      MostDerivedType = CAT->getElementType();
211b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      MostDerivedArraySize = CAT->getSize().getZExtValue();
212b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      MostDerivedPathLength = Entries.size();
2130a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    }
2140a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    /// Update this designator to refer to the given base or member of this
2150a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    /// object.
216b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    void addDeclUnchecked(const Decl *D, bool Virtual = false) {
2170a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      PathEntry Entry;
218180f47959a066795cc0f409433023af448bb0328Richard Smith      APValue::BaseOrMemberType Value(D, Virtual);
219180f47959a066795cc0f409433023af448bb0328Richard Smith      Entry.BaseOrMember = Value.getOpaqueValue();
2200a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      Entries.push_back(Entry);
221b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
222b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      // If this isn't a base class, it's a new most-derived object.
223b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      if (const FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
224b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        MostDerivedType = FD->getType();
225b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        MostDerivedArraySize = 0;
226b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        MostDerivedPathLength = Entries.size();
227b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      }
2280a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    }
22986024013d4c3728122c58fa07a2a67e6c15837efRichard Smith    /// Update this designator to refer to the given complex component.
23086024013d4c3728122c58fa07a2a67e6c15837efRichard Smith    void addComplexUnchecked(QualType EltTy, bool Imag) {
23186024013d4c3728122c58fa07a2a67e6c15837efRichard Smith      PathEntry Entry;
23286024013d4c3728122c58fa07a2a67e6c15837efRichard Smith      Entry.ArrayIndex = Imag;
23386024013d4c3728122c58fa07a2a67e6c15837efRichard Smith      Entries.push_back(Entry);
23486024013d4c3728122c58fa07a2a67e6c15837efRichard Smith
23586024013d4c3728122c58fa07a2a67e6c15837efRichard Smith      // This is technically a most-derived object, though in practice this
23686024013d4c3728122c58fa07a2a67e6c15837efRichard Smith      // is unlikely to matter.
23786024013d4c3728122c58fa07a2a67e6c15837efRichard Smith      MostDerivedType = EltTy;
23886024013d4c3728122c58fa07a2a67e6c15837efRichard Smith      MostDerivedArraySize = 2;
23986024013d4c3728122c58fa07a2a67e6c15837efRichard Smith      MostDerivedPathLength = Entries.size();
24086024013d4c3728122c58fa07a2a67e6c15837efRichard Smith    }
241b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    void diagnosePointerArithmetic(EvalInfo &Info, const Expr *E, uint64_t N);
2420a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    /// Add N to the address of this subobject.
243b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    void adjustIndex(EvalInfo &Info, const Expr *E, uint64_t N) {
2440a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      if (Invalid) return;
245b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      if (MostDerivedPathLength == Entries.size() && MostDerivedArraySize) {
2469a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith        Entries.back().ArrayIndex += N;
247b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        if (Entries.back().ArrayIndex > MostDerivedArraySize) {
248b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith          diagnosePointerArithmetic(Info, E, Entries.back().ArrayIndex);
249b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith          setInvalid();
250b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        }
2510a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith        return;
2520a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      }
253b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      // [expr.add]p4: For the purposes of these operators, a pointer to a
254b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      // nonarray object behaves the same as a pointer to the first element of
255b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      // an array of length one with the type of the object as its element type.
256b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      if (IsOnePastTheEnd && N == (uint64_t)-1)
257b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        IsOnePastTheEnd = false;
258b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      else if (!IsOnePastTheEnd && N == 1)
259b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        IsOnePastTheEnd = true;
260b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      else if (N != 0) {
261b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        diagnosePointerArithmetic(Info, E, uint64_t(IsOnePastTheEnd) + N);
2620a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith        setInvalid();
263b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      }
2640a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    }
2650a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith  };
2660a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith
267bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith  /// A stack frame in the constexpr call stack.
268bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith  struct CallStackFrame {
269bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    EvalInfo &Info;
270bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith
271bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    /// Parent - The caller of this stack frame.
272bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    CallStackFrame *Caller;
273bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith
27408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    /// CallLoc - The location of the call expression for this call.
27508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    SourceLocation CallLoc;
27608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
27708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    /// Callee - The function which was called.
27808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    const FunctionDecl *Callee;
27908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
28083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    /// Index - The call index of this call.
28183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    unsigned Index;
28283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith
283180f47959a066795cc0f409433023af448bb0328Richard Smith    /// This - The binding for the this pointer in this call, if any.
284180f47959a066795cc0f409433023af448bb0328Richard Smith    const LValue *This;
285180f47959a066795cc0f409433023af448bb0328Richard Smith
286bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    /// ParmBindings - Parameter bindings for this function call, indexed by
287bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    /// parameters' function scope indices.
2881aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    const APValue *Arguments;
289bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith
290f6172aee547241427e6dabdd0bd6fcaf1c046689Eli Friedman    // Note that we intentionally use std::map here so that references to
291f6172aee547241427e6dabdd0bd6fcaf1c046689Eli Friedman    // values are stable.
292f6172aee547241427e6dabdd0bd6fcaf1c046689Eli Friedman    typedef std::map<const Expr*, APValue> MapTy;
293bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    typedef MapTy::const_iterator temp_iterator;
294bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    /// Temporaries - Temporary lvalues materialized within this stack frame.
295bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    MapTy Temporaries;
296bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith
29708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    CallStackFrame(EvalInfo &Info, SourceLocation CallLoc,
29808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith                   const FunctionDecl *Callee, const LValue *This,
2991aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith                   const APValue *Arguments);
300bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    ~CallStackFrame();
301bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith  };
302bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith
303dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith  /// A partial diagnostic which we might know in advance that we are not going
304dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith  /// to emit.
305dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith  class OptionalDiagnostic {
306dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    PartialDiagnostic *Diag;
307dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith
308dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith  public:
309dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    explicit OptionalDiagnostic(PartialDiagnostic *Diag = 0) : Diag(Diag) {}
310dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith
311dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    template<typename T>
312dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    OptionalDiagnostic &operator<<(const T &v) {
313dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith      if (Diag)
314dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith        *Diag << v;
315dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith      return *this;
316dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    }
317789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith
318789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    OptionalDiagnostic &operator<<(const APSInt &I) {
319789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith      if (Diag) {
320789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith        llvm::SmallVector<char, 32> Buffer;
321789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith        I.toString(Buffer);
322789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith        *Diag << StringRef(Buffer.data(), Buffer.size());
323789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith      }
324789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith      return *this;
325789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    }
326789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith
327789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    OptionalDiagnostic &operator<<(const APFloat &F) {
328789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith      if (Diag) {
329789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith        llvm::SmallVector<char, 32> Buffer;
330789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith        F.toString(Buffer);
331789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith        *Diag << StringRef(Buffer.data(), Buffer.size());
332789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith      }
333789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith      return *this;
334789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    }
335dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith  };
336dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith
33783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  /// EvalInfo - This is a private struct used by the evaluator to capture
33883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  /// information about a subexpression as it is folded.  It retains information
33983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  /// about the AST context, but also maintains information about the folded
34083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  /// expression.
34183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  ///
34283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  /// If an expression could be evaluated, it is still possible it is not a C
34383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  /// "integer constant expression" or constant expression.  If not, this struct
34483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  /// captures information about how and why not.
34583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  ///
34683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  /// One bit of information passed *into* the request for constant folding
34783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  /// indicates whether the subexpression is "evaluated" or not according to C
34883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  /// rules.  For example, the RHS of (0 && foo()) is not evaluated.  We can
34983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  /// evaluate the expression regardless of what the RHS is, but C only allows
35083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  /// certain things in certain situations.
351c54061a679d8db4169438b2f97f81e3f443c6a25Benjamin Kramer  struct EvalInfo {
352dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    ASTContext &Ctx;
353d411a4b23077b29e19c9371bbb19b054a374d922Argyrios Kyrtzidis
3541e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    /// EvalStatus - Contains information about the evaluation.
3551e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    Expr::EvalStatus &EvalStatus;
356f0c1e4b679e15c26bffb5892e35985bf3c52f77aAnders Carlsson
357d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    /// CurrentCall - The top of the constexpr call stack.
358bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    CallStackFrame *CurrentCall;
359d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
360d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    /// CallStackDepth - The number of calls in the call stack right now.
361d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    unsigned CallStackDepth;
362d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
36383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    /// NextCallIndex - The next call index to assign.
36483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    unsigned NextCallIndex;
36583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith
366bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    /// BottomFrame - The frame in which evaluation started. This must be
367745f5147e065900267c85a5568785a1991d4838fRichard Smith    /// initialized after CurrentCall and CallStackDepth.
368bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    CallStackFrame BottomFrame;
369bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith
370180f47959a066795cc0f409433023af448bb0328Richard Smith    /// EvaluatingDecl - This is the declaration whose initializer is being
371180f47959a066795cc0f409433023af448bb0328Richard Smith    /// evaluated, if any.
372180f47959a066795cc0f409433023af448bb0328Richard Smith    const VarDecl *EvaluatingDecl;
373180f47959a066795cc0f409433023af448bb0328Richard Smith
374180f47959a066795cc0f409433023af448bb0328Richard Smith    /// EvaluatingDeclValue - This is the value being constructed for the
375180f47959a066795cc0f409433023af448bb0328Richard Smith    /// declaration whose initializer is being evaluated, if any.
376180f47959a066795cc0f409433023af448bb0328Richard Smith    APValue *EvaluatingDeclValue;
377180f47959a066795cc0f409433023af448bb0328Richard Smith
378c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    /// HasActiveDiagnostic - Was the previous diagnostic stored? If so, further
379c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    /// notes attached to it will also be stored, otherwise they will not be.
380c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    bool HasActiveDiagnostic;
381c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
382745f5147e065900267c85a5568785a1991d4838fRichard Smith    /// CheckingPotentialConstantExpression - Are we checking whether the
383745f5147e065900267c85a5568785a1991d4838fRichard Smith    /// expression is a potential constant expression? If so, some diagnostics
384745f5147e065900267c85a5568785a1991d4838fRichard Smith    /// are suppressed.
385745f5147e065900267c85a5568785a1991d4838fRichard Smith    bool CheckingPotentialConstantExpression;
386745f5147e065900267c85a5568785a1991d4838fRichard Smith
387bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    EvalInfo(const ASTContext &C, Expr::EvalStatus &S)
388dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith      : Ctx(const_cast<ASTContext&>(C)), EvalStatus(S), CurrentCall(0),
38983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith        CallStackDepth(0), NextCallIndex(1),
39083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith        BottomFrame(*this, SourceLocation(), 0, 0, 0),
391745f5147e065900267c85a5568785a1991d4838fRichard Smith        EvaluatingDecl(0), EvaluatingDeclValue(0), HasActiveDiagnostic(false),
392649dfbc389671d0c852ead5953da630d675a5d43Argyrios Kyrtzidis        CheckingPotentialConstantExpression(false) {}
393bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith
394180f47959a066795cc0f409433023af448bb0328Richard Smith    void setEvaluatingDecl(const VarDecl *VD, APValue &Value) {
395180f47959a066795cc0f409433023af448bb0328Richard Smith      EvaluatingDecl = VD;
396180f47959a066795cc0f409433023af448bb0328Richard Smith      EvaluatingDeclValue = &Value;
397180f47959a066795cc0f409433023af448bb0328Richard Smith    }
398180f47959a066795cc0f409433023af448bb0328Richard Smith
3994e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    const LangOptions &getLangOpts() const { return Ctx.getLangOpts(); }
400c18c42345636e2866fed75c7e434fb659d747672Richard Smith
401c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    bool CheckCallLimit(SourceLocation Loc) {
402745f5147e065900267c85a5568785a1991d4838fRichard Smith      // Don't perform any constexpr calls (other than the call we're checking)
403745f5147e065900267c85a5568785a1991d4838fRichard Smith      // when checking a potential constant expression.
404745f5147e065900267c85a5568785a1991d4838fRichard Smith      if (CheckingPotentialConstantExpression && CallStackDepth > 1)
405745f5147e065900267c85a5568785a1991d4838fRichard Smith        return false;
40683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      if (NextCallIndex == 0) {
40783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith        // NextCallIndex has wrapped around.
40883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith        Diag(Loc, diag::note_constexpr_call_limit_exceeded);
40983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith        return false;
41083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      }
411c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      if (CallStackDepth <= getLangOpts().ConstexprCallDepth)
412c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith        return true;
413c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      Diag(Loc, diag::note_constexpr_depth_limit_exceeded)
414c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith        << getLangOpts().ConstexprCallDepth;
415c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      return false;
416c18c42345636e2866fed75c7e434fb659d747672Richard Smith    }
417f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
41883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    CallStackFrame *getCallFrame(unsigned CallIndex) {
41983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      assert(CallIndex && "no call index in getCallFrame");
42083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      // We will eventually hit BottomFrame, which has Index 1, so Frame can't
42183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      // be null in this loop.
42283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      CallStackFrame *Frame = CurrentCall;
42383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      while (Frame->Index > CallIndex)
42483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith        Frame = Frame->Caller;
42583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      return (Frame->Index == CallIndex) ? Frame : 0;
42683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    }
42783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith
428c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  private:
429c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    /// Add a diagnostic to the diagnostics list.
430c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    PartialDiagnostic &addDiag(SourceLocation Loc, diag::kind DiagId) {
431c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      PartialDiagnostic PD(DiagId, Ctx.getDiagAllocator());
432c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      EvalStatus.Diag->push_back(std::make_pair(Loc, PD));
433c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      return EvalStatus.Diag->back().second;
434c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    }
435c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
43608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    /// Add notes containing a call stack to the current point of evaluation.
43708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    void addCallStack(unsigned Limit);
43808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
439c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  public:
440f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    /// Diagnose that the evaluation cannot be folded.
4417098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    OptionalDiagnostic Diag(SourceLocation Loc, diag::kind DiagId
4427098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith                              = diag::note_invalid_subexpr_in_const_expr,
443c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                            unsigned ExtraNotes = 0) {
444f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      // If we have a prior diagnostic, it will be noting that the expression
445f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      // isn't a constant expression. This diagnostic is more important.
446f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      // FIXME: We might want to show both diagnostics to the user.
447dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith      if (EvalStatus.Diag) {
44808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        unsigned CallStackNotes = CallStackDepth - 1;
44908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        unsigned Limit = Ctx.getDiagnostics().getConstexprBacktraceLimit();
45008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        if (Limit)
45108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith          CallStackNotes = std::min(CallStackNotes, Limit + 1);
452745f5147e065900267c85a5568785a1991d4838fRichard Smith        if (CheckingPotentialConstantExpression)
453745f5147e065900267c85a5568785a1991d4838fRichard Smith          CallStackNotes = 0;
45408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
455c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith        HasActiveDiagnostic = true;
456dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith        EvalStatus.Diag->clear();
45708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        EvalStatus.Diag->reserve(1 + ExtraNotes + CallStackNotes);
45808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        addDiag(Loc, DiagId);
459745f5147e065900267c85a5568785a1991d4838fRichard Smith        if (!CheckingPotentialConstantExpression)
460745f5147e065900267c85a5568785a1991d4838fRichard Smith          addCallStack(Limit);
46108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        return OptionalDiagnostic(&(*EvalStatus.Diag)[0].second);
462dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith      }
463c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      HasActiveDiagnostic = false;
464dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith      return OptionalDiagnostic();
465dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    }
466dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith
4675cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    OptionalDiagnostic Diag(const Expr *E, diag::kind DiagId
4685cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith                              = diag::note_invalid_subexpr_in_const_expr,
4695cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith                            unsigned ExtraNotes = 0) {
4705cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      if (EvalStatus.Diag)
4715cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith        return Diag(E->getExprLoc(), DiagId, ExtraNotes);
4725cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      HasActiveDiagnostic = false;
4735cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      return OptionalDiagnostic();
4745cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    }
4755cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith
476dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    /// Diagnose that the evaluation does not produce a C++11 core constant
477dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    /// expression.
4785cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    template<typename LocArg>
4795cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    OptionalDiagnostic CCEDiag(LocArg Loc, diag::kind DiagId
4807098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith                                 = diag::note_invalid_subexpr_in_const_expr,
481c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                               unsigned ExtraNotes = 0) {
482dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith      // Don't override a previous diagnostic.
48351e47df5a57430f1b691b04258e663cce68aef9dEli Friedman      if (!EvalStatus.Diag || !EvalStatus.Diag->empty()) {
48451e47df5a57430f1b691b04258e663cce68aef9dEli Friedman        HasActiveDiagnostic = false;
485dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith        return OptionalDiagnostic();
48651e47df5a57430f1b691b04258e663cce68aef9dEli Friedman      }
487c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      return Diag(Loc, DiagId, ExtraNotes);
488c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    }
489c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
490c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    /// Add a note to a prior diagnostic.
491c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    OptionalDiagnostic Note(SourceLocation Loc, diag::kind DiagId) {
492c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      if (!HasActiveDiagnostic)
493c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith        return OptionalDiagnostic();
494c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      return OptionalDiagnostic(&addDiag(Loc, DiagId));
495f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    }
496099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
497099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    /// Add a stack of notes to a prior diagnostic.
498099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    void addNotes(ArrayRef<PartialDiagnosticAt> Diags) {
499099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith      if (HasActiveDiagnostic) {
500099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith        EvalStatus.Diag->insert(EvalStatus.Diag->end(),
501099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith                                Diags.begin(), Diags.end());
502099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith      }
503099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    }
504745f5147e065900267c85a5568785a1991d4838fRichard Smith
505745f5147e065900267c85a5568785a1991d4838fRichard Smith    /// Should we continue evaluation as much as possible after encountering a
506745f5147e065900267c85a5568785a1991d4838fRichard Smith    /// construct which can't be folded?
507745f5147e065900267c85a5568785a1991d4838fRichard Smith    bool keepEvaluatingAfterFailure() {
50874e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith      return CheckingPotentialConstantExpression &&
50974e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith             EvalStatus.Diag && EvalStatus.Diag->empty();
510745f5147e065900267c85a5568785a1991d4838fRichard Smith    }
511c54061a679d8db4169438b2f97f81e3f443c6a25Benjamin Kramer  };
512f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
513f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  /// Object used to treat all foldable expressions as constant expressions.
514f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  struct FoldConstant {
515f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    bool Enabled;
516f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
517f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    explicit FoldConstant(EvalInfo &Info)
518f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      : Enabled(Info.EvalStatus.Diag && Info.EvalStatus.Diag->empty() &&
519f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith                !Info.EvalStatus.HasSideEffects) {
520f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    }
521f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    // Treat the value we've computed since this object was created as constant.
522f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    void Fold(EvalInfo &Info) {
523f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      if (Enabled && !Info.EvalStatus.Diag->empty() &&
524f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          !Info.EvalStatus.HasSideEffects)
525f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        Info.EvalStatus.Diag->clear();
526f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    }
527f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  };
52874e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith
52974e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  /// RAII object used to suppress diagnostics and side-effects from a
53074e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  /// speculative evaluation.
53174e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  class SpeculativeEvaluationRAII {
53274e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    EvalInfo &Info;
53374e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    Expr::EvalStatus Old;
53474e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith
53574e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  public:
53674e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    SpeculativeEvaluationRAII(EvalInfo &Info,
53774e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith                              llvm::SmallVectorImpl<PartialDiagnosticAt>
53874e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith                                *NewDiag = 0)
53974e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith      : Info(Info), Old(Info.EvalStatus) {
54074e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith      Info.EvalStatus.Diag = NewDiag;
54174e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    }
54274e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    ~SpeculativeEvaluationRAII() {
54374e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith      Info.EvalStatus = Old;
54474e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    }
54574e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  };
54608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith}
54708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
548b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithbool SubobjectDesignator::checkSubobject(EvalInfo &Info, const Expr *E,
549b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                                         CheckSubobjectKind CSK) {
550b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (Invalid)
551b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    return false;
552b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (isOnePastTheEnd()) {
5535cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.CCEDiag(E, diag::note_constexpr_past_end_subobject)
554b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      << CSK;
555b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    setInvalid();
556b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    return false;
557b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  }
558b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  return true;
559b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith}
560b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
561b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithvoid SubobjectDesignator::diagnosePointerArithmetic(EvalInfo &Info,
562b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                                                    const Expr *E, uint64_t N) {
563b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (MostDerivedPathLength == Entries.size() && MostDerivedArraySize)
5645cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.CCEDiag(E, diag::note_constexpr_array_index)
565b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      << static_cast<int>(N) << /*array*/ 0
566b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      << static_cast<unsigned>(MostDerivedArraySize);
567b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  else
5685cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.CCEDiag(E, diag::note_constexpr_array_index)
569b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      << static_cast<int>(N) << /*non-array*/ 1;
570b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  setInvalid();
571b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith}
572b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
57308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard SmithCallStackFrame::CallStackFrame(EvalInfo &Info, SourceLocation CallLoc,
57408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith                               const FunctionDecl *Callee, const LValue *This,
5751aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith                               const APValue *Arguments)
57608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    : Info(Info), Caller(Info.CurrentCall), CallLoc(CallLoc), Callee(Callee),
57783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      Index(Info.NextCallIndex++), This(This), Arguments(Arguments) {
57808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  Info.CurrentCall = this;
57908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  ++Info.CallStackDepth;
58008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith}
58108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
58208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard SmithCallStackFrame::~CallStackFrame() {
58308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  assert(Info.CurrentCall == this && "calls retired out of order");
58408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  --Info.CallStackDepth;
58508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  Info.CurrentCall = Caller;
58608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith}
58787eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner
58808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith/// Produce a string describing the given constexpr call.
58908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smithstatic void describeCall(CallStackFrame *Frame, llvm::raw_ostream &Out) {
59008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  unsigned ArgIndex = 0;
59108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  bool IsMemberCall = isa<CXXMethodDecl>(Frame->Callee) &&
5925ba73e1af8ef519161bd40063dc325457e21676aRichard Smith                      !isa<CXXConstructorDecl>(Frame->Callee) &&
5935ba73e1af8ef519161bd40063dc325457e21676aRichard Smith                      cast<CXXMethodDecl>(Frame->Callee)->isInstance();
59408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
59508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  if (!IsMemberCall)
59608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    Out << *Frame->Callee << '(';
59708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
59808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  for (FunctionDecl::param_const_iterator I = Frame->Callee->param_begin(),
59908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith       E = Frame->Callee->param_end(); I != E; ++I, ++ArgIndex) {
6005fe31228c883040cf016cfc71ad4bfeba462602eNAKAMURA Takumi    if (ArgIndex > (unsigned)IsMemberCall)
60108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      Out << ", ";
60208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
60308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    const ParmVarDecl *Param = *I;
6041aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    const APValue &Arg = Frame->Arguments[ArgIndex];
6051aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    Arg.printPretty(Out, Frame->Info.Ctx, Param->getType());
60608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
60708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    if (ArgIndex == 0 && IsMemberCall)
60808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      Out << "->" << *Frame->Callee << '(';
609bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith  }
610d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
61108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  Out << ')';
61208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith}
61308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
61408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smithvoid EvalInfo::addCallStack(unsigned Limit) {
61508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  // Determine which calls to skip, if any.
61608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  unsigned ActiveCalls = CallStackDepth - 1;
61708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  unsigned SkipStart = ActiveCalls, SkipEnd = SkipStart;
61808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  if (Limit && Limit < ActiveCalls) {
61908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    SkipStart = Limit / 2 + Limit % 2;
62008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    SkipEnd = ActiveCalls - Limit / 2;
62108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  }
62208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
62308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  // Walk the call stack and add the diagnostics.
62408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  unsigned CallIdx = 0;
62508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  for (CallStackFrame *Frame = CurrentCall; Frame != &BottomFrame;
62608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith       Frame = Frame->Caller, ++CallIdx) {
62708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    // Skip this call?
62808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    if (CallIdx >= SkipStart && CallIdx < SkipEnd) {
62908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      if (CallIdx == SkipStart) {
63008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        // Note that we're skipping calls.
63108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        addDiag(Frame->CallLoc, diag::note_constexpr_calls_suppressed)
63208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith          << unsigned(ActiveCalls - Limit);
63308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      }
63408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      continue;
63508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    }
63608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
63708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    llvm::SmallVector<char, 128> Buffer;
63808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    llvm::raw_svector_ostream Out(Buffer);
63908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    describeCall(Frame, Out);
64008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    addDiag(Frame->CallLoc, diag::note_constexpr_call_here) << Out.str();
641bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith  }
64208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith}
643d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
64408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smithnamespace {
645f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall  struct ComplexValue {
646f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall  private:
647f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    bool IsInt;
648f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall
649f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall  public:
650f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    APSInt IntReal, IntImag;
651f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    APFloat FloatReal, FloatImag;
652f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall
653f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    ComplexValue() : FloatReal(APFloat::Bogus), FloatImag(APFloat::Bogus) {}
654f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall
655f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    void makeComplexFloat() { IsInt = false; }
656f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    bool isComplexFloat() const { return !IsInt; }
657f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    APFloat &getComplexFloatReal() { return FloatReal; }
658f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    APFloat &getComplexFloatImag() { return FloatImag; }
659f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall
660f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    void makeComplexInt() { IsInt = true; }
661f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    bool isComplexInt() const { return IsInt; }
662f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    APSInt &getComplexIntReal() { return IntReal; }
663f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    APSInt &getComplexIntImag() { return IntImag; }
664f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall
6651aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    void moveInto(APValue &v) const {
666f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall      if (isComplexFloat())
6671aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith        v = APValue(FloatReal, FloatImag);
668f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall      else
6691aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith        v = APValue(IntReal, IntImag);
670f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    }
6711aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    void setFrom(const APValue &v) {
67256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      assert(v.isComplexFloat() || v.isComplexInt());
67356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      if (v.isComplexFloat()) {
67456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall        makeComplexFloat();
67556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall        FloatReal = v.getComplexFloatReal();
67656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall        FloatImag = v.getComplexFloatImag();
67756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      } else {
67856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall        makeComplexInt();
67956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall        IntReal = v.getComplexIntReal();
68056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall        IntImag = v.getComplexIntImag();
68156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      }
68256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    }
683f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall  };
684efdb83e26f9a1fd2566afe54461216cd84814d42John McCall
685efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  struct LValue {
6861bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    APValue::LValueBase Base;
687efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    CharUnits Offset;
68883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    unsigned CallIndex;
6890a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    SubobjectDesignator Designator;
690efdb83e26f9a1fd2566afe54461216cd84814d42John McCall
6911bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    const APValue::LValueBase getLValueBase() const { return Base; }
69247a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    CharUnits &getLValueOffset() { return Offset; }
693625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith    const CharUnits &getLValueOffset() const { return Offset; }
69483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    unsigned getLValueCallIndex() const { return CallIndex; }
6950a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    SubobjectDesignator &getLValueDesignator() { return Designator; }
6960a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    const SubobjectDesignator &getLValueDesignator() const { return Designator;}
697efdb83e26f9a1fd2566afe54461216cd84814d42John McCall
6981aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    void moveInto(APValue &V) const {
6991aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith      if (Designator.Invalid)
7001aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith        V = APValue(Base, Offset, APValue::NoLValuePath(), CallIndex);
7011aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith      else
7021aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith        V = APValue(Base, Offset, Designator.Entries,
7031aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith                    Designator.IsOnePastTheEnd, CallIndex);
704efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    }
7051aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    void setFrom(ASTContext &Ctx, const APValue &V) {
70647a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith      assert(V.isLValue());
70747a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith      Base = V.getLValueBase();
70847a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith      Offset = V.getLValueOffset();
70983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      CallIndex = V.getLValueCallIndex();
7101aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith      Designator = SubobjectDesignator(Ctx, V);
7110a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    }
7120a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith
71383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    void set(APValue::LValueBase B, unsigned I = 0) {
7141bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith      Base = B;
7150a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      Offset = CharUnits::Zero();
71683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      CallIndex = I;
717b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      Designator = SubobjectDesignator(getType(B));
718b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    }
719b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
720b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    // Check that this LValue is not based on a null pointer. If it is, produce
721b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    // a diagnostic and mark the designator as invalid.
722b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    bool checkNullPointer(EvalInfo &Info, const Expr *E,
723b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                          CheckSubobjectKind CSK) {
724b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      if (Designator.Invalid)
725b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        return false;
726b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      if (!Base) {
7275cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith        Info.CCEDiag(E, diag::note_constexpr_null_subobject)
728b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith          << CSK;
729b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        Designator.setInvalid();
730b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        return false;
731b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      }
732b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      return true;
733b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    }
734b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
735b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    // Check this LValue refers to an object. If not, set the designator to be
736b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    // invalid and emit a diagnostic.
737b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK) {
7385cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      // Outside C++11, do not build a designator referring to a subobject of
7395cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      // any object: we won't use such a designator for anything.
7405cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      if (!Info.getLangOpts().CPlusPlus0x)
7415cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith        Designator.setInvalid();
742b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      return checkNullPointer(Info, E, CSK) &&
743b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith             Designator.checkSubobject(Info, E, CSK);
744b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    }
745b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
746b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    void addDecl(EvalInfo &Info, const Expr *E,
747b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                 const Decl *D, bool Virtual = false) {
7485cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      if (checkSubobject(Info, E, isa<FieldDecl>(D) ? CSK_Field : CSK_Base))
7495cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith        Designator.addDeclUnchecked(D, Virtual);
750b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    }
751b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    void addArray(EvalInfo &Info, const Expr *E, const ConstantArrayType *CAT) {
7525cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      if (checkSubobject(Info, E, CSK_ArrayToPointer))
7535cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith        Designator.addArrayUnchecked(CAT);
754b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    }
75586024013d4c3728122c58fa07a2a67e6c15837efRichard Smith    void addComplex(EvalInfo &Info, const Expr *E, QualType EltTy, bool Imag) {
7565cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      if (checkSubobject(Info, E, Imag ? CSK_Imag : CSK_Real))
7575cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith        Designator.addComplexUnchecked(EltTy, Imag);
75886024013d4c3728122c58fa07a2a67e6c15837efRichard Smith    }
759b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    void adjustIndex(EvalInfo &Info, const Expr *E, uint64_t N) {
7605cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      if (checkNullPointer(Info, E, CSK_ArrayIndex))
7615cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith        Designator.adjustIndex(Info, E, N);
76256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    }
763efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  };
764e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
765e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  struct MemberPtr {
766e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    MemberPtr() {}
767e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    explicit MemberPtr(const ValueDecl *Decl) :
768e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      DeclAndIsDerivedMember(Decl, false), Path() {}
769e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
770e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// The member or (direct or indirect) field referred to by this member
771e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// pointer, or 0 if this is a null member pointer.
772e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    const ValueDecl *getDecl() const {
773e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return DeclAndIsDerivedMember.getPointer();
774e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
775e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// Is this actually a member of some type derived from the relevant class?
776e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    bool isDerivedMember() const {
777e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return DeclAndIsDerivedMember.getInt();
778e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
779e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// Get the class which the declaration actually lives in.
780e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    const CXXRecordDecl *getContainingRecord() const {
781e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return cast<CXXRecordDecl>(
782e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith          DeclAndIsDerivedMember.getPointer()->getDeclContext());
783e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
784e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
7851aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    void moveInto(APValue &V) const {
7861aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith      V = APValue(getDecl(), isDerivedMember(), Path);
787e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
7881aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    void setFrom(const APValue &V) {
789e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      assert(V.isMemberPointer());
790e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      DeclAndIsDerivedMember.setPointer(V.getMemberPointerDecl());
791e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      DeclAndIsDerivedMember.setInt(V.isMemberPointerToDerivedMember());
792e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      Path.clear();
793e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      ArrayRef<const CXXRecordDecl*> P = V.getMemberPointerPath();
794e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      Path.insert(Path.end(), P.begin(), P.end());
795e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
796e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
797e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// DeclAndIsDerivedMember - The member declaration, and a flag indicating
798e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// whether the member is a member of some class derived from the class type
799e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// of the member pointer.
800e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    llvm::PointerIntPair<const ValueDecl*, 1, bool> DeclAndIsDerivedMember;
801e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// Path - The path of base/derived classes from the member declaration's
802e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// class (exclusive) to the class type of the member pointer (inclusive).
803e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    SmallVector<const CXXRecordDecl*, 4> Path;
804e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
805e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// Perform a cast towards the class of the Decl (either up or down the
806e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// hierarchy).
807e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    bool castBack(const CXXRecordDecl *Class) {
808e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      assert(!Path.empty());
809e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      const CXXRecordDecl *Expected;
810e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (Path.size() >= 2)
811e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        Expected = Path[Path.size() - 2];
812e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      else
813e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        Expected = getContainingRecord();
814e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (Expected->getCanonicalDecl() != Class->getCanonicalDecl()) {
815e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        // C++11 [expr.static.cast]p12: In a conversion from (D::*) to (B::*),
816e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        // if B does not contain the original member and is not a base or
817e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        // derived class of the class containing the original member, the result
818e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        // of the cast is undefined.
819e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        // C++11 [conv.mem]p2 does not cover this case for a cast from (B::*) to
820e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        // (D::*). We consider that to be a language defect.
821e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
822e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      }
823e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      Path.pop_back();
824e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return true;
825e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
826e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// Perform a base-to-derived member pointer cast.
827e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    bool castToDerived(const CXXRecordDecl *Derived) {
828e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!getDecl())
829e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return true;
830e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!isDerivedMember()) {
831e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        Path.push_back(Derived);
832e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return true;
833e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      }
834e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!castBack(Derived))
835e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
836e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (Path.empty())
837e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        DeclAndIsDerivedMember.setInt(false);
838e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return true;
839e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
840e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// Perform a derived-to-base member pointer cast.
841e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    bool castToBase(const CXXRecordDecl *Base) {
842e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!getDecl())
843e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return true;
844e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (Path.empty())
845e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        DeclAndIsDerivedMember.setInt(true);
846e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (isDerivedMember()) {
847e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        Path.push_back(Base);
848e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return true;
849e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      }
850e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return castBack(Base);
851e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
852e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  };
853c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
854b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith  /// Compare two member pointers, which are assumed to be of the same type.
855b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith  static bool operator==(const MemberPtr &LHS, const MemberPtr &RHS) {
856b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    if (!LHS.getDecl() || !RHS.getDecl())
857b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith      return !LHS.getDecl() && !RHS.getDecl();
858b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    if (LHS.getDecl()->getCanonicalDecl() != RHS.getDecl()->getCanonicalDecl())
859b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith      return false;
860b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    return LHS.Path == RHS.Path;
861b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith  }
862b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith
863c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  /// Kinds of constant expression checking, for diagnostics.
864c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  enum CheckConstantExpressionKind {
865c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    CCEK_Constant,    ///< A normal constant.
866c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    CCEK_ReturnValue, ///< A constexpr function return value.
867c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    CCEK_MemberInit   ///< A constexpr constructor mem-initializer.
868c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
869f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall}
87087eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner
8711aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smithstatic bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E);
87283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smithstatic bool EvaluateInPlace(APValue &Result, EvalInfo &Info,
87383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                            const LValue &This, const Expr *E,
87483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                            CheckConstantExpressionKind CCEK = CCEK_Constant,
87583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                            bool AllowNonLiteralTypes = false);
876efdb83e26f9a1fd2566afe54461216cd84814d42John McCallstatic bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info);
877efdb83e26f9a1fd2566afe54461216cd84814d42John McCallstatic bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info);
878e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result,
879e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                                  EvalInfo &Info);
880e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info);
88187eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattnerstatic bool EvaluateInteger(const Expr *E, APSInt  &Result, EvalInfo &Info);
8821aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smithstatic bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,
883d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner                                    EvalInfo &Info);
884d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmanstatic bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
885f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCallstatic bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info);
886f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
887f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===//
8884efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman// Misc utilities
8894efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman//===----------------------------------------------------------------------===//
8904efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
891180f47959a066795cc0f409433023af448bb0328Richard Smith/// Should this call expression be treated as a string literal?
892180f47959a066795cc0f409433023af448bb0328Richard Smithstatic bool IsStringLiteralCall(const CallExpr *E) {
893180f47959a066795cc0f409433023af448bb0328Richard Smith  unsigned Builtin = E->isBuiltinCall();
894180f47959a066795cc0f409433023af448bb0328Richard Smith  return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString ||
895180f47959a066795cc0f409433023af448bb0328Richard Smith          Builtin == Builtin::BI__builtin___NSStringMakeConstantString);
896180f47959a066795cc0f409433023af448bb0328Richard Smith}
897180f47959a066795cc0f409433023af448bb0328Richard Smith
8981bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smithstatic bool IsGlobalLValue(APValue::LValueBase B) {
899180f47959a066795cc0f409433023af448bb0328Richard Smith  // C++11 [expr.const]p3 An address constant expression is a prvalue core
900180f47959a066795cc0f409433023af448bb0328Richard Smith  // constant expression of pointer type that evaluates to...
901180f47959a066795cc0f409433023af448bb0328Richard Smith
902180f47959a066795cc0f409433023af448bb0328Richard Smith  // ... a null pointer value, or a prvalue core constant expression of type
903180f47959a066795cc0f409433023af448bb0328Richard Smith  // std::nullptr_t.
9041bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  if (!B) return true;
90542c8f87eb60958170c46767273bf93e6c96125bfJohn McCall
9061bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) {
907180f47959a066795cc0f409433023af448bb0328Richard Smith    // ... the address of an object with static storage duration,
9081bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    if (const VarDecl *VD = dyn_cast<VarDecl>(D))
90942c8f87eb60958170c46767273bf93e6c96125bfJohn McCall      return VD->hasGlobalStorage();
9101bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    // ... the address of a function,
9111bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    return isa<FunctionDecl>(D);
91242c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  }
9131bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith
9141bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  const Expr *E = B.get<const Expr*>();
9151bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  switch (E->getStmtClass()) {
9161bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  default:
9171bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    return false;
918b78ae9716576399145786b93f687943f8b197170Richard Smith  case Expr::CompoundLiteralExprClass: {
919b78ae9716576399145786b93f687943f8b197170Richard Smith    const CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E);
920b78ae9716576399145786b93f687943f8b197170Richard Smith    return CLE->isFileScope() && CLE->isLValue();
921b78ae9716576399145786b93f687943f8b197170Richard Smith  }
922180f47959a066795cc0f409433023af448bb0328Richard Smith  // A string literal has static storage duration.
923180f47959a066795cc0f409433023af448bb0328Richard Smith  case Expr::StringLiteralClass:
924180f47959a066795cc0f409433023af448bb0328Richard Smith  case Expr::PredefinedExprClass:
925180f47959a066795cc0f409433023af448bb0328Richard Smith  case Expr::ObjCStringLiteralClass:
926180f47959a066795cc0f409433023af448bb0328Richard Smith  case Expr::ObjCEncodeExprClass:
92747d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith  case Expr::CXXTypeidExprClass:
928e275a1845b9e32bd3034f2593dee1780855c8fd6Francois Pichet  case Expr::CXXUuidofExprClass:
929180f47959a066795cc0f409433023af448bb0328Richard Smith    return true;
930180f47959a066795cc0f409433023af448bb0328Richard Smith  case Expr::CallExprClass:
931180f47959a066795cc0f409433023af448bb0328Richard Smith    return IsStringLiteralCall(cast<CallExpr>(E));
932180f47959a066795cc0f409433023af448bb0328Richard Smith  // For GCC compatibility, &&label has static storage duration.
933180f47959a066795cc0f409433023af448bb0328Richard Smith  case Expr::AddrLabelExprClass:
934180f47959a066795cc0f409433023af448bb0328Richard Smith    return true;
935180f47959a066795cc0f409433023af448bb0328Richard Smith  // A Block literal expression may be used as the initialization value for
936180f47959a066795cc0f409433023af448bb0328Richard Smith  // Block variables at global or local static scope.
937180f47959a066795cc0f409433023af448bb0328Richard Smith  case Expr::BlockExprClass:
938180f47959a066795cc0f409433023af448bb0328Richard Smith    return !cast<BlockExpr>(E)->getBlockDecl()->hasCaptures();
939745f5147e065900267c85a5568785a1991d4838fRichard Smith  case Expr::ImplicitValueInitExprClass:
940745f5147e065900267c85a5568785a1991d4838fRichard Smith    // FIXME:
941745f5147e065900267c85a5568785a1991d4838fRichard Smith    // We can never form an lvalue with an implicit value initialization as its
942745f5147e065900267c85a5568785a1991d4838fRichard Smith    // base through expression evaluation, so these only appear in one case: the
943745f5147e065900267c85a5568785a1991d4838fRichard Smith    // implicit variable declaration we invent when checking whether a constexpr
944745f5147e065900267c85a5568785a1991d4838fRichard Smith    // constructor can produce a constant expression. We must assume that such
945745f5147e065900267c85a5568785a1991d4838fRichard Smith    // an expression might be a global lvalue.
946745f5147e065900267c85a5568785a1991d4838fRichard Smith    return true;
947180f47959a066795cc0f409433023af448bb0328Richard Smith  }
94842c8f87eb60958170c46767273bf93e6c96125bfJohn McCall}
94942c8f87eb60958170c46767273bf93e6c96125bfJohn McCall
95083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smithstatic void NoteLValueLocation(EvalInfo &Info, APValue::LValueBase Base) {
95183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  assert(Base && "no location for a null lvalue");
95283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>();
95383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  if (VD)
95483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    Info.Note(VD->getLocation(), diag::note_declared_at);
95583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  else
956890f0f1612b0028b8b16730ae7ed07e295600c76Ted Kremenek    Info.Note(Base.get<const Expr*>()->getExprLoc(),
95783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith              diag::note_constexpr_temporary_here);
95883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith}
95983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith
9609a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith/// Check that this reference or pointer core constant expression is a valid
9611aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith/// value for an address or reference constant expression. Return true if we
9621aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith/// can fold this expression, whether or not it's a constant expression.
96383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smithstatic bool CheckLValueConstantExpression(EvalInfo &Info, SourceLocation Loc,
96483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                                          QualType Type, const LValue &LVal) {
96583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  bool IsReferenceType = Type->isReferenceType();
96683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith
967c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  APValue::LValueBase Base = LVal.getLValueBase();
968c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  const SubobjectDesignator &Designator = LVal.getLValueDesignator();
969c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
970b78ae9716576399145786b93f687943f8b197170Richard Smith  // Check that the object is a global. Note that the fake 'this' object we
971b78ae9716576399145786b93f687943f8b197170Richard Smith  // manufacture when checking potential constant expressions is conservatively
972b78ae9716576399145786b93f687943f8b197170Richard Smith  // assumed to be global here.
973c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  if (!IsGlobalLValue(Base)) {
974c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    if (Info.getLangOpts().CPlusPlus0x) {
975c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>();
97683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      Info.Diag(Loc, diag::note_constexpr_non_global, 1)
97783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith        << IsReferenceType << !Designator.Entries.empty()
97883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith        << !!VD << VD;
97983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      NoteLValueLocation(Info, Base);
980c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    } else {
98183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      Info.Diag(Loc);
982c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    }
98361e616206413d1779c7545c7a8ad1ce1129ad9c1Richard Smith    // Don't allow references to temporaries to escape.
98469c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith    return false;
985f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
98683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  assert((Info.CheckingPotentialConstantExpression ||
98783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith          LVal.getLValueCallIndex() == 0) &&
98883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith         "have call index for global lvalue");
989b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
99048def65d1cfbd020c5d4a7e542a00d63808c6060Hans Wennborg  // Check if this is a thread-local variable.
99148def65d1cfbd020c5d4a7e542a00d63808c6060Hans Wennborg  if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) {
99248def65d1cfbd020c5d4a7e542a00d63808c6060Hans Wennborg    if (const VarDecl *Var = dyn_cast<const VarDecl>(VD)) {
99348def65d1cfbd020c5d4a7e542a00d63808c6060Hans Wennborg      if (Var->isThreadSpecified())
99448def65d1cfbd020c5d4a7e542a00d63808c6060Hans Wennborg        return false;
99548def65d1cfbd020c5d4a7e542a00d63808c6060Hans Wennborg    }
99648def65d1cfbd020c5d4a7e542a00d63808c6060Hans Wennborg  }
99748def65d1cfbd020c5d4a7e542a00d63808c6060Hans Wennborg
998b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // Allow address constant expressions to be past-the-end pointers. This is
999b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // an extension: the standard requires them to point to an object.
1000b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (!IsReferenceType)
1001b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    return true;
1002b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
1003b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // A reference constant expression must refer to an object.
1004b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (!Base) {
1005b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    // FIXME: diagnostic
100683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    Info.CCEDiag(Loc);
100761e616206413d1779c7545c7a8ad1ce1129ad9c1Richard Smith    return true;
1008b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  }
1009b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
1010c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  // Does this refer one past the end of some object?
1011b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (Designator.isOnePastTheEnd()) {
1012c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>();
101383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    Info.Diag(Loc, diag::note_constexpr_past_end, 1)
1014c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      << !Designator.Entries.empty() << !!VD << VD;
101583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    NoteLValueLocation(Info, Base);
1016c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  }
1017c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
101869c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith  return true;
101947a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith}
102047a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith
102151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// Check that this core constant expression is of literal type, and if not,
102251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// produce an appropriate diagnostic.
102351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smithstatic bool CheckLiteralType(EvalInfo &Info, const Expr *E) {
102451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (!E->isRValue() || E->getType()->isLiteralType())
102551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return true;
102651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
102751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  // Prvalue constant expressions must be of literal types.
102851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (Info.getLangOpts().CPlusPlus0x)
10295cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.Diag(E, diag::note_constexpr_nonliteral)
103051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      << E->getType();
103151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  else
10325cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.Diag(E, diag::note_invalid_subexpr_in_const_expr);
103351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  return false;
103451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith}
103551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
10369a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith/// Check that this core constant expression value is a valid value for a
103783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith/// constant expression. If not, report an appropriate diagnostic. Does not
103883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith/// check that the expression is of literal type.
103983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smithstatic bool CheckConstantExpression(EvalInfo &Info, SourceLocation DiagLoc,
104083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                                    QualType Type, const APValue &Value) {
104183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  // Core issue 1454: For a literal constant expression of array or class type,
104283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  // each subobject of its value shall have been initialized by a constant
104383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  // expression.
104483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  if (Value.isArray()) {
104583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    QualType EltTy = Type->castAsArrayTypeUnsafe()->getElementType();
104683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    for (unsigned I = 0, N = Value.getArrayInitializedElts(); I != N; ++I) {
104783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      if (!CheckConstantExpression(Info, DiagLoc, EltTy,
104883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                                   Value.getArrayInitializedElt(I)))
104983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith        return false;
105083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    }
105183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    if (!Value.hasArrayFiller())
105283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      return true;
105383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return CheckConstantExpression(Info, DiagLoc, EltTy,
105483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                                   Value.getArrayFiller());
105583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  }
105683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  if (Value.isUnion() && Value.getUnionField()) {
105783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return CheckConstantExpression(Info, DiagLoc,
105883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                                   Value.getUnionField()->getType(),
105983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                                   Value.getUnionValue());
106083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  }
106183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  if (Value.isStruct()) {
106283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    RecordDecl *RD = Type->castAs<RecordType>()->getDecl();
106383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD)) {
106483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      unsigned BaseIndex = 0;
106583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(),
106683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith             End = CD->bases_end(); I != End; ++I, ++BaseIndex) {
106783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith        if (!CheckConstantExpression(Info, DiagLoc, I->getType(),
106883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                                     Value.getStructBase(BaseIndex)))
106983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith          return false;
107083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      }
107183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    }
107283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
107383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith         I != E; ++I) {
1074262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie      if (!CheckConstantExpression(Info, DiagLoc, I->getType(),
1075262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie                                   Value.getStructField(I->getFieldIndex())))
107683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith        return false;
107783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    }
107883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  }
107983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith
108083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  if (Value.isLValue()) {
108183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    LValue LVal;
10821aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    LVal.setFrom(Info.Ctx, Value);
108383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return CheckLValueConstantExpression(Info, DiagLoc, Type, LVal);
10849a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith  }
108583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith
108683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  // Everything else is fine.
108783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  return true;
10889a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith}
10899a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith
10909e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smithconst ValueDecl *GetLValueBaseDecl(const LValue &LVal) {
10911bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  return LVal.Base.dyn_cast<const ValueDecl*>();
10929e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith}
10939e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith
10949e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smithstatic bool IsLiteralLValue(const LValue &Value) {
109583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  return Value.Base.dyn_cast<const Expr*>() && !Value.CallIndex;
10969e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith}
10979e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith
109865ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smithstatic bool IsWeakLValue(const LValue &Value) {
109965ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith  const ValueDecl *Decl = GetLValueBaseDecl(Value);
11000dd7a25e8d679de1dc0ce788222d6dee0e879885Lang Hames  return Decl && Decl->isWeak();
110165ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith}
110265ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith
11031aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smithstatic bool EvalPointerValueAsBool(const APValue &Value, bool &Result) {
11043554283157190e67918fad4221a5e6faf9317362John McCall  // A null base expression indicates a null pointer.  These are always
11053554283157190e67918fad4221a5e6faf9317362John McCall  // evaluatable, and they are false unless the offset is zero.
1106e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (!Value.getLValueBase()) {
1107e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    Result = !Value.getLValueOffset().isZero();
11083554283157190e67918fad4221a5e6faf9317362John McCall    return true;
11093554283157190e67918fad4221a5e6faf9317362John McCall  }
11103554283157190e67918fad4221a5e6faf9317362John McCall
1111e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // We have a non-null base.  These are generally known to be true, but if it's
1112e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // a weak declaration it can be null at runtime.
11133554283157190e67918fad4221a5e6faf9317362John McCall  Result = true;
1114e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  const ValueDecl *Decl = Value.getLValueBase().dyn_cast<const ValueDecl*>();
11150dd7a25e8d679de1dc0ce788222d6dee0e879885Lang Hames  return !Decl || !Decl->isWeak();
11165bc86103767c2abcbfdd6518e0ccbbbb6aa59e0fEli Friedman}
11175bc86103767c2abcbfdd6518e0ccbbbb6aa59e0fEli Friedman
11181aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smithstatic bool HandleConversionToBool(const APValue &Val, bool &Result) {
1119c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  switch (Val.getKind()) {
1120c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  case APValue::Uninitialized:
1121c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return false;
1122c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  case APValue::Int:
1123c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    Result = Val.getInt().getBoolValue();
112441bf4f38348561a0f12c10d34f1673cd19a6eb04Richard Smith    return true;
1125c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  case APValue::Float:
1126c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    Result = !Val.getFloat().isZero();
1127a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman    return true;
1128c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  case APValue::ComplexInt:
1129c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    Result = Val.getComplexIntReal().getBoolValue() ||
1130c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith             Val.getComplexIntImag().getBoolValue();
1131c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return true;
1132c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  case APValue::ComplexFloat:
1133c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    Result = !Val.getComplexFloatReal().isZero() ||
1134c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith             !Val.getComplexFloatImag().isZero();
1135436c8898cd1c93c5bacd3fcc4ac586bc5cd77062Richard Smith    return true;
1136e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  case APValue::LValue:
1137e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return EvalPointerValueAsBool(Val, Result);
1138e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  case APValue::MemberPointer:
1139e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    Result = Val.getMemberPointerDecl();
1140e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
1141c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  case APValue::Vector:
1142cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  case APValue::Array:
1143180f47959a066795cc0f409433023af448bb0328Richard Smith  case APValue::Struct:
1144180f47959a066795cc0f409433023af448bb0328Richard Smith  case APValue::Union:
114565639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman  case APValue::AddrLabelDiff:
1146c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return false;
11474efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman  }
11484efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
1149c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  llvm_unreachable("unknown APValue kind");
1150c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith}
1151c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
1152c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smithstatic bool EvaluateAsBooleanCondition(const Expr *E, bool &Result,
1153c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith                                       EvalInfo &Info) {
1154c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  assert(E->isRValue() && "missing lvalue-to-rvalue conv in bool condition");
11551aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  APValue Val;
1156d411a4b23077b29e19c9371bbb19b054a374d922Argyrios Kyrtzidis  if (!Evaluate(Val, Info, E))
1157c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return false;
1158d411a4b23077b29e19c9371bbb19b054a374d922Argyrios Kyrtzidis  return HandleConversionToBool(Val, Result);
11594efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman}
11604efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
1161c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithtemplate<typename T>
116226dc97cbeba8ced19972a259720a71aefa01ef43Eli Friedmanstatic void HandleOverflow(EvalInfo &Info, const Expr *E,
1163c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                           const T &SrcValue, QualType DestType) {
116426dc97cbeba8ced19972a259720a71aefa01ef43Eli Friedman  Info.CCEDiag(E, diag::note_constexpr_overflow)
1165789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    << SrcValue << DestType;
1166c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
1167c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
1168c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstatic bool HandleFloatToIntCast(EvalInfo &Info, const Expr *E,
1169c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                 QualType SrcType, const APFloat &Value,
1170c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                 QualType DestType, APSInt &Result) {
1171c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  unsigned DestWidth = Info.Ctx.getIntWidth(DestType);
1172a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar  // Determine whether we are converting to unsigned or signed.
1173575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor  bool DestSigned = DestType->isSignedIntegerOrEnumerationType();
11741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1175c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  Result = APSInt(DestWidth, !DestSigned);
1176a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar  bool ignored;
1177c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  if (Value.convertToInteger(Result, llvm::APFloat::rmTowardZero, &ignored)
1178c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      & APFloat::opInvalidOp)
117926dc97cbeba8ced19972a259720a71aefa01ef43Eli Friedman    HandleOverflow(Info, E, Value, DestType);
1180c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  return true;
1181a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar}
1182a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar
1183c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstatic bool HandleFloatToFloatCast(EvalInfo &Info, const Expr *E,
1184c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                   QualType SrcType, QualType DestType,
1185c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                   APFloat &Result) {
1186c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  APFloat Value = Result;
1187a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar  bool ignored;
1188c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  if (Result.convert(Info.Ctx.getFloatTypeSemantics(DestType),
1189c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                     APFloat::rmNearestTiesToEven, &ignored)
1190c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      & APFloat::opOverflow)
119126dc97cbeba8ced19972a259720a71aefa01ef43Eli Friedman    HandleOverflow(Info, E, Value, DestType);
1192c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  return true;
1193a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar}
1194a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar
1195f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smithstatic APSInt HandleIntToIntCast(EvalInfo &Info, const Expr *E,
1196f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith                                 QualType DestType, QualType SrcType,
1197f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith                                 APSInt &Value) {
1198f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith  unsigned DestWidth = Info.Ctx.getIntWidth(DestType);
1199a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar  APSInt Result = Value;
1200a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar  // Figure out if this is a truncate, extend or noop cast.
1201a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar  // If the input is signed, do a sign extend, noop, or truncate.
12029f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad  Result = Result.extOrTrunc(DestWidth);
1203575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor  Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType());
1204a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar  return Result;
1205a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar}
1206a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar
1207c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstatic bool HandleIntToFloatCast(EvalInfo &Info, const Expr *E,
1208c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                 QualType SrcType, const APSInt &Value,
1209c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                 QualType DestType, APFloat &Result) {
1210c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  Result = APFloat(Info.Ctx.getFloatTypeSemantics(DestType), 1);
1211c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  if (Result.convertFromAPInt(Value, Value.isSigned(),
1212c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                              APFloat::rmNearestTiesToEven)
1213c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      & APFloat::opOverflow)
121426dc97cbeba8ced19972a259720a71aefa01ef43Eli Friedman    HandleOverflow(Info, E, Value, DestType);
1215c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  return true;
1216a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar}
1217a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar
1218e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedmanstatic bool EvalAndBitcastToAPInt(EvalInfo &Info, const Expr *E,
1219e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman                                  llvm::APInt &Res) {
12201aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  APValue SVal;
1221e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  if (!Evaluate(SVal, Info, E))
1222e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    return false;
1223e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  if (SVal.isInt()) {
1224e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    Res = SVal.getInt();
1225e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    return true;
1226e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  }
1227e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  if (SVal.isFloat()) {
1228e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    Res = SVal.getFloat().bitcastToAPInt();
1229e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    return true;
1230e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  }
1231e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  if (SVal.isVector()) {
1232e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    QualType VecTy = E->getType();
1233e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    unsigned VecSize = Info.Ctx.getTypeSize(VecTy);
1234e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    QualType EltTy = VecTy->castAs<VectorType>()->getElementType();
1235e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    unsigned EltSize = Info.Ctx.getTypeSize(EltTy);
1236e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian();
1237e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    Res = llvm::APInt::getNullValue(VecSize);
1238e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    for (unsigned i = 0; i < SVal.getVectorLength(); i++) {
1239e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      APValue &Elt = SVal.getVectorElt(i);
1240e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      llvm::APInt EltAsInt;
1241e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      if (Elt.isInt()) {
1242e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        EltAsInt = Elt.getInt();
1243e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      } else if (Elt.isFloat()) {
1244e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        EltAsInt = Elt.getFloat().bitcastToAPInt();
1245e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      } else {
1246e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        // Don't try to handle vectors of anything other than int or float
1247e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        // (not sure if it's possible to hit this case).
12485cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith        Info.Diag(E, diag::note_invalid_subexpr_in_const_expr);
1249e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        return false;
1250e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      }
1251e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      unsigned BaseEltSize = EltAsInt.getBitWidth();
1252e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      if (BigEndian)
1253e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        Res |= EltAsInt.zextOrTrunc(VecSize).rotr(i*EltSize+BaseEltSize);
1254e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      else
1255e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        Res |= EltAsInt.zextOrTrunc(VecSize).rotl(i*EltSize);
1256e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    }
1257e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    return true;
1258e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  }
1259e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  // Give up if the input isn't an int, float, or vector.  For example, we
1260e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  // reject "(v4i16)(intptr_t)&a".
12615cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith  Info.Diag(E, diag::note_invalid_subexpr_in_const_expr);
1262e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  return false;
1263e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman}
1264e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman
1265b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith/// Cast an lvalue referring to a base subobject to a derived class, by
1266b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith/// truncating the lvalue's path to the given length.
1267b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic bool CastToDerivedClass(EvalInfo &Info, const Expr *E, LValue &Result,
1268b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                               const RecordDecl *TruncatedType,
1269b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                               unsigned TruncatedElements) {
1270b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  SubobjectDesignator &D = Result.Designator;
1271180f47959a066795cc0f409433023af448bb0328Richard Smith
1272b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // Check we actually point to a derived class object.
1273b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (TruncatedElements == D.Entries.size())
1274b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    return true;
1275b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  assert(TruncatedElements >= D.MostDerivedPathLength &&
1276b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith         "not casting to a derived class");
1277b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (!Result.checkSubobject(Info, E, CSK_Derived))
1278180f47959a066795cc0f409433023af448bb0328Richard Smith    return false;
1279180f47959a066795cc0f409433023af448bb0328Richard Smith
1280b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // Truncate the path to the subobject, and remove any derived-to-base offsets.
1281e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  const RecordDecl *RD = TruncatedType;
1282e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  for (unsigned I = TruncatedElements, N = D.Entries.size(); I != N; ++I) {
12838d59deec807ed53efcd07855199cdc9c979f447fJohn McCall    if (RD->isInvalidDecl()) return false;
1284180f47959a066795cc0f409433023af448bb0328Richard Smith    const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD);
1285180f47959a066795cc0f409433023af448bb0328Richard Smith    const CXXRecordDecl *Base = getAsBaseClass(D.Entries[I]);
1286e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (isVirtualBaseClass(D.Entries[I]))
1287180f47959a066795cc0f409433023af448bb0328Richard Smith      Result.Offset -= Layout.getVBaseClassOffset(Base);
1288e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    else
1289180f47959a066795cc0f409433023af448bb0328Richard Smith      Result.Offset -= Layout.getBaseClassOffset(Base);
1290180f47959a066795cc0f409433023af448bb0328Richard Smith    RD = Base;
1291180f47959a066795cc0f409433023af448bb0328Richard Smith  }
1292e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  D.Entries.resize(TruncatedElements);
1293180f47959a066795cc0f409433023af448bb0328Richard Smith  return true;
1294180f47959a066795cc0f409433023af448bb0328Richard Smith}
1295180f47959a066795cc0f409433023af448bb0328Richard Smith
12968d59deec807ed53efcd07855199cdc9c979f447fJohn McCallstatic bool HandleLValueDirectBase(EvalInfo &Info, const Expr *E, LValue &Obj,
1297180f47959a066795cc0f409433023af448bb0328Richard Smith                                   const CXXRecordDecl *Derived,
1298180f47959a066795cc0f409433023af448bb0328Richard Smith                                   const CXXRecordDecl *Base,
1299180f47959a066795cc0f409433023af448bb0328Richard Smith                                   const ASTRecordLayout *RL = 0) {
13008d59deec807ed53efcd07855199cdc9c979f447fJohn McCall  if (!RL) {
13018d59deec807ed53efcd07855199cdc9c979f447fJohn McCall    if (Derived->isInvalidDecl()) return false;
13028d59deec807ed53efcd07855199cdc9c979f447fJohn McCall    RL = &Info.Ctx.getASTRecordLayout(Derived);
13038d59deec807ed53efcd07855199cdc9c979f447fJohn McCall  }
13048d59deec807ed53efcd07855199cdc9c979f447fJohn McCall
1305180f47959a066795cc0f409433023af448bb0328Richard Smith  Obj.getLValueOffset() += RL->getBaseClassOffset(Base);
1306b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  Obj.addDecl(Info, E, Base, /*Virtual*/ false);
13078d59deec807ed53efcd07855199cdc9c979f447fJohn McCall  return true;
1308180f47959a066795cc0f409433023af448bb0328Richard Smith}
1309180f47959a066795cc0f409433023af448bb0328Richard Smith
1310b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic bool HandleLValueBase(EvalInfo &Info, const Expr *E, LValue &Obj,
1311180f47959a066795cc0f409433023af448bb0328Richard Smith                             const CXXRecordDecl *DerivedDecl,
1312180f47959a066795cc0f409433023af448bb0328Richard Smith                             const CXXBaseSpecifier *Base) {
1313180f47959a066795cc0f409433023af448bb0328Richard Smith  const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl();
1314180f47959a066795cc0f409433023af448bb0328Richard Smith
13158d59deec807ed53efcd07855199cdc9c979f447fJohn McCall  if (!Base->isVirtual())
13168d59deec807ed53efcd07855199cdc9c979f447fJohn McCall    return HandleLValueDirectBase(Info, E, Obj, DerivedDecl, BaseDecl);
1317180f47959a066795cc0f409433023af448bb0328Richard Smith
1318b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  SubobjectDesignator &D = Obj.Designator;
1319b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (D.Invalid)
1320b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    return false;
1321b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
1322180f47959a066795cc0f409433023af448bb0328Richard Smith  // Extract most-derived object and corresponding type.
1323b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  DerivedDecl = D.MostDerivedType->getAsCXXRecordDecl();
1324b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (!CastToDerivedClass(Info, E, Obj, DerivedDecl, D.MostDerivedPathLength))
1325180f47959a066795cc0f409433023af448bb0328Richard Smith    return false;
1326180f47959a066795cc0f409433023af448bb0328Richard Smith
1327b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // Find the virtual base class.
13288d59deec807ed53efcd07855199cdc9c979f447fJohn McCall  if (DerivedDecl->isInvalidDecl()) return false;
1329180f47959a066795cc0f409433023af448bb0328Richard Smith  const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl);
1330180f47959a066795cc0f409433023af448bb0328Richard Smith  Obj.getLValueOffset() += Layout.getVBaseClassOffset(BaseDecl);
1331b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  Obj.addDecl(Info, E, BaseDecl, /*Virtual*/ true);
1332180f47959a066795cc0f409433023af448bb0328Richard Smith  return true;
1333180f47959a066795cc0f409433023af448bb0328Richard Smith}
1334180f47959a066795cc0f409433023af448bb0328Richard Smith
1335180f47959a066795cc0f409433023af448bb0328Richard Smith/// Update LVal to refer to the given field, which must be a member of the type
1336180f47959a066795cc0f409433023af448bb0328Richard Smith/// currently described by LVal.
13378d59deec807ed53efcd07855199cdc9c979f447fJohn McCallstatic bool HandleLValueMember(EvalInfo &Info, const Expr *E, LValue &LVal,
1338180f47959a066795cc0f409433023af448bb0328Richard Smith                               const FieldDecl *FD,
1339180f47959a066795cc0f409433023af448bb0328Richard Smith                               const ASTRecordLayout *RL = 0) {
13408d59deec807ed53efcd07855199cdc9c979f447fJohn McCall  if (!RL) {
13418d59deec807ed53efcd07855199cdc9c979f447fJohn McCall    if (FD->getParent()->isInvalidDecl()) return false;
1342180f47959a066795cc0f409433023af448bb0328Richard Smith    RL = &Info.Ctx.getASTRecordLayout(FD->getParent());
13438d59deec807ed53efcd07855199cdc9c979f447fJohn McCall  }
1344180f47959a066795cc0f409433023af448bb0328Richard Smith
1345180f47959a066795cc0f409433023af448bb0328Richard Smith  unsigned I = FD->getFieldIndex();
1346180f47959a066795cc0f409433023af448bb0328Richard Smith  LVal.Offset += Info.Ctx.toCharUnitsFromBits(RL->getFieldOffset(I));
1347b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  LVal.addDecl(Info, E, FD);
13488d59deec807ed53efcd07855199cdc9c979f447fJohn McCall  return true;
1349180f47959a066795cc0f409433023af448bb0328Richard Smith}
1350180f47959a066795cc0f409433023af448bb0328Richard Smith
1351d9b02e726262e4009dda830998bb934172ac0020Richard Smith/// Update LVal to refer to the given indirect field.
13528d59deec807ed53efcd07855199cdc9c979f447fJohn McCallstatic bool HandleLValueIndirectMember(EvalInfo &Info, const Expr *E,
1353d9b02e726262e4009dda830998bb934172ac0020Richard Smith                                       LValue &LVal,
1354d9b02e726262e4009dda830998bb934172ac0020Richard Smith                                       const IndirectFieldDecl *IFD) {
1355d9b02e726262e4009dda830998bb934172ac0020Richard Smith  for (IndirectFieldDecl::chain_iterator C = IFD->chain_begin(),
1356d9b02e726262e4009dda830998bb934172ac0020Richard Smith                                         CE = IFD->chain_end(); C != CE; ++C)
13578d59deec807ed53efcd07855199cdc9c979f447fJohn McCall    if (!HandleLValueMember(Info, E, LVal, cast<FieldDecl>(*C)))
13588d59deec807ed53efcd07855199cdc9c979f447fJohn McCall      return false;
13598d59deec807ed53efcd07855199cdc9c979f447fJohn McCall  return true;
1360d9b02e726262e4009dda830998bb934172ac0020Richard Smith}
1361d9b02e726262e4009dda830998bb934172ac0020Richard Smith
1362180f47959a066795cc0f409433023af448bb0328Richard Smith/// Get the size of the given type in char units.
136374e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smithstatic bool HandleSizeof(EvalInfo &Info, SourceLocation Loc,
136474e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith                         QualType Type, CharUnits &Size) {
1365180f47959a066795cc0f409433023af448bb0328Richard Smith  // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1366180f47959a066795cc0f409433023af448bb0328Richard Smith  // extension.
1367180f47959a066795cc0f409433023af448bb0328Richard Smith  if (Type->isVoidType() || Type->isFunctionType()) {
1368180f47959a066795cc0f409433023af448bb0328Richard Smith    Size = CharUnits::One();
1369180f47959a066795cc0f409433023af448bb0328Richard Smith    return true;
1370180f47959a066795cc0f409433023af448bb0328Richard Smith  }
1371180f47959a066795cc0f409433023af448bb0328Richard Smith
1372180f47959a066795cc0f409433023af448bb0328Richard Smith  if (!Type->isConstantSizeType()) {
1373180f47959a066795cc0f409433023af448bb0328Richard Smith    // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
137474e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    // FIXME: Better diagnostic.
137574e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    Info.Diag(Loc);
1376180f47959a066795cc0f409433023af448bb0328Richard Smith    return false;
1377180f47959a066795cc0f409433023af448bb0328Richard Smith  }
1378180f47959a066795cc0f409433023af448bb0328Richard Smith
1379180f47959a066795cc0f409433023af448bb0328Richard Smith  Size = Info.Ctx.getTypeSizeInChars(Type);
1380180f47959a066795cc0f409433023af448bb0328Richard Smith  return true;
1381180f47959a066795cc0f409433023af448bb0328Richard Smith}
1382180f47959a066795cc0f409433023af448bb0328Richard Smith
1383180f47959a066795cc0f409433023af448bb0328Richard Smith/// Update a pointer value to model pointer arithmetic.
1384180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param Info - Information about the ongoing evaluation.
1385b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith/// \param E - The expression being evaluated, for diagnostic purposes.
1386180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param LVal - The pointer value to be updated.
1387180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param EltTy - The pointee type represented by LVal.
1388180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param Adjustment - The adjustment, in objects of type EltTy, to add.
1389b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E,
1390b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                                        LValue &LVal, QualType EltTy,
1391b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                                        int64_t Adjustment) {
1392180f47959a066795cc0f409433023af448bb0328Richard Smith  CharUnits SizeOfPointee;
139374e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfPointee))
1394180f47959a066795cc0f409433023af448bb0328Richard Smith    return false;
1395180f47959a066795cc0f409433023af448bb0328Richard Smith
1396180f47959a066795cc0f409433023af448bb0328Richard Smith  // Compute the new offset in the appropriate width.
1397180f47959a066795cc0f409433023af448bb0328Richard Smith  LVal.Offset += Adjustment * SizeOfPointee;
1398b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  LVal.adjustIndex(Info, E, Adjustment);
1399180f47959a066795cc0f409433023af448bb0328Richard Smith  return true;
1400180f47959a066795cc0f409433023af448bb0328Richard Smith}
1401180f47959a066795cc0f409433023af448bb0328Richard Smith
140286024013d4c3728122c58fa07a2a67e6c15837efRichard Smith/// Update an lvalue to refer to a component of a complex number.
140386024013d4c3728122c58fa07a2a67e6c15837efRichard Smith/// \param Info - Information about the ongoing evaluation.
140486024013d4c3728122c58fa07a2a67e6c15837efRichard Smith/// \param LVal - The lvalue to be updated.
140586024013d4c3728122c58fa07a2a67e6c15837efRichard Smith/// \param EltTy - The complex number's component type.
140686024013d4c3728122c58fa07a2a67e6c15837efRichard Smith/// \param Imag - False for the real component, true for the imaginary.
140786024013d4c3728122c58fa07a2a67e6c15837efRichard Smithstatic bool HandleLValueComplexElement(EvalInfo &Info, const Expr *E,
140886024013d4c3728122c58fa07a2a67e6c15837efRichard Smith                                       LValue &LVal, QualType EltTy,
140986024013d4c3728122c58fa07a2a67e6c15837efRichard Smith                                       bool Imag) {
141086024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  if (Imag) {
141186024013d4c3728122c58fa07a2a67e6c15837efRichard Smith    CharUnits SizeOfComponent;
141286024013d4c3728122c58fa07a2a67e6c15837efRichard Smith    if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfComponent))
141386024013d4c3728122c58fa07a2a67e6c15837efRichard Smith      return false;
141486024013d4c3728122c58fa07a2a67e6c15837efRichard Smith    LVal.Offset += SizeOfComponent;
141586024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  }
141686024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  LVal.addComplex(Info, E, EltTy, Imag);
141786024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  return true;
141886024013d4c3728122c58fa07a2a67e6c15837efRichard Smith}
141986024013d4c3728122c58fa07a2a67e6c15837efRichard Smith
142003f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith/// Try to evaluate the initializer for a variable declaration.
1421f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool EvaluateVarDeclInit(EvalInfo &Info, const Expr *E,
1422f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                                const VarDecl *VD,
14231aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith                                CallStackFrame *Frame, APValue &Result) {
1424d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  // If this is a parameter to an active constexpr function call, perform
1425d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  // argument substitution.
1426d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(VD)) {
1427745f5147e065900267c85a5568785a1991d4838fRichard Smith    // Assume arguments of a potential constant expression are unknown
1428745f5147e065900267c85a5568785a1991d4838fRichard Smith    // constant expressions.
1429745f5147e065900267c85a5568785a1991d4838fRichard Smith    if (Info.CheckingPotentialConstantExpression)
1430745f5147e065900267c85a5568785a1991d4838fRichard Smith      return false;
1431f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!Frame || !Frame->Arguments) {
14325cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      Info.Diag(E, diag::note_invalid_subexpr_in_const_expr);
1433177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith      return false;
1434f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    }
1435177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith    Result = Frame->Arguments[PVD->getFunctionScopeIndex()];
1436177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith    return true;
1437d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  }
143803f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith
1439099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  // Dig out the initializer, and use the declaration which it's attached to.
1440099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  const Expr *Init = VD->getAnyInitializer(VD);
1441099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  if (!Init || Init->isValueDependent()) {
1442745f5147e065900267c85a5568785a1991d4838fRichard Smith    // If we're checking a potential constant expression, the variable could be
1443745f5147e065900267c85a5568785a1991d4838fRichard Smith    // initialized later.
1444745f5147e065900267c85a5568785a1991d4838fRichard Smith    if (!Info.CheckingPotentialConstantExpression)
14455cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      Info.Diag(E, diag::note_invalid_subexpr_in_const_expr);
1446099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    return false;
1447099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  }
1448099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
1449180f47959a066795cc0f409433023af448bb0328Richard Smith  // If we're currently evaluating the initializer of this declaration, use that
1450180f47959a066795cc0f409433023af448bb0328Richard Smith  // in-flight value.
1451180f47959a066795cc0f409433023af448bb0328Richard Smith  if (Info.EvaluatingDecl == VD) {
14521aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    Result = *Info.EvaluatingDeclValue;
1453180f47959a066795cc0f409433023af448bb0328Richard Smith    return !Result.isUninit();
1454180f47959a066795cc0f409433023af448bb0328Richard Smith  }
1455180f47959a066795cc0f409433023af448bb0328Richard Smith
145665ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith  // Never evaluate the initializer of a weak variable. We can't be sure that
145765ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith  // this is the definition which will be used.
1458f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (VD->isWeak()) {
14595cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.Diag(E, diag::note_invalid_subexpr_in_const_expr);
146065ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith    return false;
1461f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
146265ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith
1463099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  // Check that we can fold the initializer. In C++, we will have already done
1464099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  // this in the cases where it matters for conformance.
1465099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1466099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  if (!VD->evaluateValue(Notes)) {
14675cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.Diag(E, diag::note_constexpr_var_init_non_constant,
1468099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith              Notes.size() + 1) << VD;
1469099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    Info.Note(VD->getLocation(), diag::note_declared_at);
1470099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    Info.addNotes(Notes);
147147a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    return false;
1472099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  } else if (!VD->checkInitIsICE()) {
14735cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.CCEDiag(E, diag::note_constexpr_var_init_non_constant,
1474099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith                 Notes.size() + 1) << VD;
1475099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    Info.Note(VD->getLocation(), diag::note_declared_at);
1476099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    Info.addNotes(Notes);
1477f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
147803f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith
14791aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  Result = *VD->getEvaluatedValue();
148047a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith  return true;
148103f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith}
148203f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith
1483c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smithstatic bool IsConstNonVolatile(QualType T) {
148403f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith  Qualifiers Quals = T.getQualifiers();
148503f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith  return Quals.hasConst() && !Quals.hasVolatile();
148603f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith}
148703f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith
148859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith/// Get the base index of the given base class within an APValue representing
148959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith/// the given derived class.
149059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithstatic unsigned getBaseIndex(const CXXRecordDecl *Derived,
149159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith                             const CXXRecordDecl *Base) {
149259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  Base = Base->getCanonicalDecl();
149359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  unsigned Index = 0;
149459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  for (CXXRecordDecl::base_class_const_iterator I = Derived->bases_begin(),
149559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith         E = Derived->bases_end(); I != E; ++I, ++Index) {
149659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    if (I->getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base)
149759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      return Index;
149859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  }
149959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
150059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  llvm_unreachable("base class missing from derived class's bases list");
150159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
150259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
1503fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith/// Extract the value of a character from a string literal. CharType is used to
1504fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith/// determine the expected signedness of the result -- a string literal used to
1505fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith/// initialize an array of 'signed char' or 'unsigned char' might contain chars
1506fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith/// of the wrong signedness.
1507f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smithstatic APSInt ExtractStringLiteralCharacter(EvalInfo &Info, const Expr *Lit,
1508fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith                                            uint64_t Index, QualType CharType) {
1509f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith  // FIXME: Support PredefinedExpr, ObjCEncodeExpr, MakeStringConstant
1510f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith  const StringLiteral *S = dyn_cast<StringLiteral>(Lit);
1511f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith  assert(S && "unexpected string literal expression kind");
1512fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith  assert(CharType->isIntegerType() && "unexpected character type");
1513f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith
1514f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith  APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(),
1515fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith               CharType->isUnsignedIntegerType());
1516f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith  if (Index < S->getLength())
1517f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith    Value = S->getCodeUnit(Index);
1518f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith  return Value;
1519f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith}
1520f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith
1521cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith/// Extract the designated sub-object of an rvalue.
1522f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool ExtractSubobject(EvalInfo &Info, const Expr *E,
15231aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith                             APValue &Obj, QualType ObjType,
1524cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith                             const SubobjectDesignator &Sub, QualType SubType) {
1525b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (Sub.Invalid)
1526b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    // A diagnostic will have already been produced.
1527cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    return false;
1528b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (Sub.isOnePastTheEnd()) {
15295cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.Diag(E, Info.getLangOpts().CPlusPlus0x ?
1530aa5d533427d803e52ee42b250ffd6645ef5ccb0fMatt Beaumont-Gay                (unsigned)diag::note_constexpr_read_past_end :
1531aa5d533427d803e52ee42b250ffd6645ef5ccb0fMatt Beaumont-Gay                (unsigned)diag::note_invalid_subexpr_in_const_expr);
15327098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    return false;
15337098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  }
1534f64699e8db3946e21b5f4a0421cbc58a3e439599Richard Smith  if (Sub.Entries.empty())
1535cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    return true;
1536745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (Info.CheckingPotentialConstantExpression && Obj.isUninit())
1537745f5147e065900267c85a5568785a1991d4838fRichard Smith    // This object might be initialized later.
1538745f5147e065900267c85a5568785a1991d4838fRichard Smith    return false;
1539cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
15400069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith  APValue *O = &Obj;
1541180f47959a066795cc0f409433023af448bb0328Richard Smith  // Walk the designator's path to find the subobject.
1542cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  for (unsigned I = 0, N = Sub.Entries.size(); I != N; ++I) {
1543cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    if (ObjType->isArrayType()) {
1544180f47959a066795cc0f409433023af448bb0328Richard Smith      // Next subobject is an array element.
1545cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith      const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(ObjType);
1546f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      assert(CAT && "vla in literal type?");
1547cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith      uint64_t Index = Sub.Entries[I].ArrayIndex;
1548f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      if (CAT->getSize().ule(Index)) {
15497098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        // Note, it should not be possible to form a pointer with a valid
15507098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        // designator which points more than one past the end of the array.
15515cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith        Info.Diag(E, Info.getLangOpts().CPlusPlus0x ?
1552aa5d533427d803e52ee42b250ffd6645ef5ccb0fMatt Beaumont-Gay                    (unsigned)diag::note_constexpr_read_past_end :
1553aa5d533427d803e52ee42b250ffd6645ef5ccb0fMatt Beaumont-Gay                    (unsigned)diag::note_invalid_subexpr_in_const_expr);
1554cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith        return false;
1555f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      }
1556f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith      // An array object is represented as either an Array APValue or as an
1557f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith      // LValue which refers to a string literal.
1558f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith      if (O->isLValue()) {
1559f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith        assert(I == N - 1 && "extracting subobject of character?");
1560f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith        assert(!O->hasLValuePath() || O->getLValuePath().empty());
15611aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith        Obj = APValue(ExtractStringLiteralCharacter(
1562fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith          Info, O->getLValueBase().get<const Expr*>(), Index, SubType));
1563f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith        return true;
1564f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith      } else if (O->getArrayInitializedElts() > Index)
1565cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith        O = &O->getArrayInitializedElt(Index);
1566cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith      else
1567cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith        O = &O->getArrayFiller();
1568cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith      ObjType = CAT->getElementType();
156986024013d4c3728122c58fa07a2a67e6c15837efRichard Smith    } else if (ObjType->isAnyComplexType()) {
157086024013d4c3728122c58fa07a2a67e6c15837efRichard Smith      // Next subobject is a complex number.
157186024013d4c3728122c58fa07a2a67e6c15837efRichard Smith      uint64_t Index = Sub.Entries[I].ArrayIndex;
157286024013d4c3728122c58fa07a2a67e6c15837efRichard Smith      if (Index > 1) {
15735cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith        Info.Diag(E, Info.getLangOpts().CPlusPlus0x ?
157486024013d4c3728122c58fa07a2a67e6c15837efRichard Smith                    (unsigned)diag::note_constexpr_read_past_end :
157586024013d4c3728122c58fa07a2a67e6c15837efRichard Smith                    (unsigned)diag::note_invalid_subexpr_in_const_expr);
157686024013d4c3728122c58fa07a2a67e6c15837efRichard Smith        return false;
157786024013d4c3728122c58fa07a2a67e6c15837efRichard Smith      }
157886024013d4c3728122c58fa07a2a67e6c15837efRichard Smith      assert(I == N - 1 && "extracting subobject of scalar?");
157986024013d4c3728122c58fa07a2a67e6c15837efRichard Smith      if (O->isComplexInt()) {
15801aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith        Obj = APValue(Index ? O->getComplexIntImag()
158186024013d4c3728122c58fa07a2a67e6c15837efRichard Smith                            : O->getComplexIntReal());
158286024013d4c3728122c58fa07a2a67e6c15837efRichard Smith      } else {
158386024013d4c3728122c58fa07a2a67e6c15837efRichard Smith        assert(O->isComplexFloat());
15841aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith        Obj = APValue(Index ? O->getComplexFloatImag()
158586024013d4c3728122c58fa07a2a67e6c15837efRichard Smith                            : O->getComplexFloatReal());
158686024013d4c3728122c58fa07a2a67e6c15837efRichard Smith      }
158786024013d4c3728122c58fa07a2a67e6c15837efRichard Smith      return true;
1588180f47959a066795cc0f409433023af448bb0328Richard Smith    } else if (const FieldDecl *Field = getAsField(Sub.Entries[I])) {
1589b4e5e286a5cd156247720b1eb204abaa8e09568dRichard Smith      if (Field->isMutable()) {
15905cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith        Info.Diag(E, diag::note_constexpr_ltor_mutable, 1)
1591b4e5e286a5cd156247720b1eb204abaa8e09568dRichard Smith          << Field;
1592b4e5e286a5cd156247720b1eb204abaa8e09568dRichard Smith        Info.Note(Field->getLocation(), diag::note_declared_at);
1593b4e5e286a5cd156247720b1eb204abaa8e09568dRichard Smith        return false;
1594b4e5e286a5cd156247720b1eb204abaa8e09568dRichard Smith      }
1595b4e5e286a5cd156247720b1eb204abaa8e09568dRichard Smith
1596180f47959a066795cc0f409433023af448bb0328Richard Smith      // Next subobject is a class, struct or union field.
1597180f47959a066795cc0f409433023af448bb0328Richard Smith      RecordDecl *RD = ObjType->castAs<RecordType>()->getDecl();
1598180f47959a066795cc0f409433023af448bb0328Richard Smith      if (RD->isUnion()) {
1599180f47959a066795cc0f409433023af448bb0328Richard Smith        const FieldDecl *UnionField = O->getUnionField();
1600180f47959a066795cc0f409433023af448bb0328Richard Smith        if (!UnionField ||
1601f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith            UnionField->getCanonicalDecl() != Field->getCanonicalDecl()) {
16025cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith          Info.Diag(E, diag::note_constexpr_read_inactive_union_member)
16037098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith            << Field << !UnionField << UnionField;
1604180f47959a066795cc0f409433023af448bb0328Richard Smith          return false;
1605f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        }
1606180f47959a066795cc0f409433023af448bb0328Richard Smith        O = &O->getUnionValue();
1607180f47959a066795cc0f409433023af448bb0328Richard Smith      } else
1608180f47959a066795cc0f409433023af448bb0328Richard Smith        O = &O->getStructField(Field->getFieldIndex());
1609180f47959a066795cc0f409433023af448bb0328Richard Smith      ObjType = Field->getType();
16107098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith
16117098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      if (ObjType.isVolatileQualified()) {
16127098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        if (Info.getLangOpts().CPlusPlus) {
16137098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith          // FIXME: Include a description of the path to the volatile subobject.
16145cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith          Info.Diag(E, diag::note_constexpr_ltor_volatile_obj, 1)
16157098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith            << 2 << Field;
16167098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith          Info.Note(Field->getLocation(), diag::note_declared_at);
16177098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        } else {
16185cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith          Info.Diag(E, diag::note_invalid_subexpr_in_const_expr);
16197098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        }
16207098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        return false;
16217098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      }
1622cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    } else {
1623180f47959a066795cc0f409433023af448bb0328Richard Smith      // Next subobject is a base class.
162459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      const CXXRecordDecl *Derived = ObjType->getAsCXXRecordDecl();
162559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      const CXXRecordDecl *Base = getAsBaseClass(Sub.Entries[I]);
162659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      O = &O->getStructBase(getBaseIndex(Derived, Base));
162759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      ObjType = Info.Ctx.getRecordType(Base);
1628cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    }
1629180f47959a066795cc0f409433023af448bb0328Richard Smith
1630f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (O->isUninit()) {
1631745f5147e065900267c85a5568785a1991d4838fRichard Smith      if (!Info.CheckingPotentialConstantExpression)
16325cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith        Info.Diag(E, diag::note_constexpr_read_uninit);
1633180f47959a066795cc0f409433023af448bb0328Richard Smith      return false;
1634f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    }
1635cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  }
1636cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
16370069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith  // This may look super-stupid, but it serves an important purpose: if we just
16380069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith  // swapped Obj and *O, we'd create an object which had itself as a subobject.
16390069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith  // To avoid the leak, we ensure that Tmp ends up owning the original complete
16400069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith  // object, which is destroyed by Tmp's destructor.
16410069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith  APValue Tmp;
16420069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith  O->swap(Tmp);
16430069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith  Obj.swap(Tmp);
1644cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  return true;
1645cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith}
1646cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
1647f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith/// Find the position where two subobject designators diverge, or equivalently
1648f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith/// the length of the common initial subsequence.
1649f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smithstatic unsigned FindDesignatorMismatch(QualType ObjType,
1650f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith                                       const SubobjectDesignator &A,
1651f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith                                       const SubobjectDesignator &B,
1652f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith                                       bool &WasArrayIndex) {
1653f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  unsigned I = 0, N = std::min(A.Entries.size(), B.Entries.size());
1654f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  for (/**/; I != N; ++I) {
165586024013d4c3728122c58fa07a2a67e6c15837efRichard Smith    if (!ObjType.isNull() &&
165686024013d4c3728122c58fa07a2a67e6c15837efRichard Smith        (ObjType->isArrayType() || ObjType->isAnyComplexType())) {
1657f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      // Next subobject is an array element.
1658f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      if (A.Entries[I].ArrayIndex != B.Entries[I].ArrayIndex) {
1659f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        WasArrayIndex = true;
1660f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        return I;
1661f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      }
166286024013d4c3728122c58fa07a2a67e6c15837efRichard Smith      if (ObjType->isAnyComplexType())
166386024013d4c3728122c58fa07a2a67e6c15837efRichard Smith        ObjType = ObjType->castAs<ComplexType>()->getElementType();
166486024013d4c3728122c58fa07a2a67e6c15837efRichard Smith      else
166586024013d4c3728122c58fa07a2a67e6c15837efRichard Smith        ObjType = ObjType->castAsArrayTypeUnsafe()->getElementType();
1666f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    } else {
1667f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      if (A.Entries[I].BaseOrMember != B.Entries[I].BaseOrMember) {
1668f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        WasArrayIndex = false;
1669f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        return I;
1670f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      }
1671f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      if (const FieldDecl *FD = getAsField(A.Entries[I]))
1672f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        // Next subobject is a field.
1673f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        ObjType = FD->getType();
1674f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      else
1675f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        // Next subobject is a base class.
1676f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        ObjType = QualType();
1677f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    }
1678f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  }
1679f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  WasArrayIndex = false;
1680f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  return I;
1681f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith}
1682f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
1683f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith/// Determine whether the given subobject designators refer to elements of the
1684f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith/// same array object.
1685f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smithstatic bool AreElementsOfSameArray(QualType ObjType,
1686f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith                                   const SubobjectDesignator &A,
1687f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith                                   const SubobjectDesignator &B) {
1688f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  if (A.Entries.size() != B.Entries.size())
1689f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    return false;
1690f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
1691f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  bool IsArray = A.MostDerivedArraySize != 0;
1692f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  if (IsArray && A.MostDerivedPathLength != A.Entries.size())
1693f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    // A is a subobject of the array element.
1694f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    return false;
1695f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
1696f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  // If A (and B) designates an array element, the last entry will be the array
1697f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  // index. That doesn't have to match. Otherwise, we're in the 'implicit array
1698f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  // of length 1' case, and the entire path must match.
1699f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  bool WasArrayIndex;
1700f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  unsigned CommonLength = FindDesignatorMismatch(ObjType, A, B, WasArrayIndex);
1701f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  return CommonLength >= A.Entries.size() - IsArray;
1702f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith}
1703f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
1704180f47959a066795cc0f409433023af448bb0328Richard Smith/// HandleLValueToRValueConversion - Perform an lvalue-to-rvalue conversion on
1705180f47959a066795cc0f409433023af448bb0328Richard Smith/// the given lvalue. This can also be used for 'lvalue-to-lvalue' conversions
1706180f47959a066795cc0f409433023af448bb0328Richard Smith/// for looking up the glvalue referred to by an entity of reference type.
1707180f47959a066795cc0f409433023af448bb0328Richard Smith///
1708180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param Info - Information about the ongoing evaluation.
1709f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// \param Conv - The expression for which we are performing the conversion.
1710f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith///               Used for diagnostics.
17119ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith/// \param Type - The type we expect this conversion to produce, before
17129ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith///               stripping cv-qualifiers in the case of a non-clas type.
1713180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param LVal - The glvalue on which we are attempting to perform this action.
1714180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param RVal - The produced value will be placed here.
1715f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool HandleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv,
1716f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                                           QualType Type,
17171aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith                                           const LValue &LVal, APValue &RVal) {
1718b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (LVal.Designator.Invalid)
1719b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    // A diagnostic will have already been produced.
1720b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    return false;
1721b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
17221bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  const Expr *Base = LVal.Base.dyn_cast<const Expr*>();
1723c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
1724f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (!LVal.Base) {
1725f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    // FIXME: Indirection through a null pointer deserves a specific diagnostic.
17265cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.Diag(Conv, diag::note_invalid_subexpr_in_const_expr);
17277098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    return false;
17287098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  }
17297098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith
173083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  CallStackFrame *Frame = 0;
173183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  if (LVal.CallIndex) {
173283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    Frame = Info.getCallFrame(LVal.CallIndex);
173383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    if (!Frame) {
17345cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      Info.Diag(Conv, diag::note_constexpr_lifetime_ended, 1) << !Base;
173583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      NoteLValueLocation(Info, LVal.Base);
173683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      return false;
173783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    }
173883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  }
173983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith
17407098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  // C++11 DR1311: An lvalue-to-rvalue conversion on a volatile-qualified type
17417098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  // is not a constant expression (even if the object is non-volatile). We also
17427098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  // apply this rule to C++98, in order to conform to the expected 'volatile'
17437098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  // semantics.
17447098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  if (Type.isVolatileQualified()) {
17457098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    if (Info.getLangOpts().CPlusPlus)
17465cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      Info.Diag(Conv, diag::note_constexpr_ltor_volatile_type) << Type;
17477098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    else
17485cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      Info.Diag(Conv);
1749c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return false;
1750f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
1751c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
17521bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  if (const ValueDecl *D = LVal.Base.dyn_cast<const ValueDecl*>()) {
1753c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    // In C++98, const, non-volatile integers initialized with ICEs are ICEs.
1754c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    // In C++11, constexpr, non-volatile variables initialized with constant
1755d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    // expressions are constant expressions too. Inside constexpr functions,
1756d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    // parameters are constant expressions even if they're non-const.
1757c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    // In C, such things can also be folded, although they are not ICEs.
1758c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    const VarDecl *VD = dyn_cast<VarDecl>(D);
1759d2008e2c80d6c9282044ec873a937a17a0f33579Douglas Gregor    if (VD) {
1760d2008e2c80d6c9282044ec873a937a17a0f33579Douglas Gregor      if (const VarDecl *VDef = VD->getDefinition(Info.Ctx))
1761d2008e2c80d6c9282044ec873a937a17a0f33579Douglas Gregor        VD = VDef;
1762d2008e2c80d6c9282044ec873a937a17a0f33579Douglas Gregor    }
1763f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!VD || VD->isInvalidDecl()) {
17645cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      Info.Diag(Conv);
17650a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      return false;
1766f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    }
1767f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
17687098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    // DR1313: If the object is volatile-qualified but the glvalue was not,
17697098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    // behavior is undefined so the result is not a constant expression.
17701bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    QualType VT = VD->getType();
17717098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    if (VT.isVolatileQualified()) {
17727098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      if (Info.getLangOpts().CPlusPlus) {
17735cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith        Info.Diag(Conv, diag::note_constexpr_ltor_volatile_obj, 1) << 1 << VD;
17747098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        Info.Note(VD->getLocation(), diag::note_declared_at);
17757098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      } else {
17765cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith        Info.Diag(Conv);
1777f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      }
17787098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      return false;
17797098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    }
17807098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith
17817098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    if (!isa<ParmVarDecl>(VD)) {
17827098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      if (VD->isConstexpr()) {
17837098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        // OK, we can read this variable.
17847098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      } else if (VT->isIntegralOrEnumerationType()) {
17857098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        if (!VT.isConstQualified()) {
17867098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith          if (Info.getLangOpts().CPlusPlus) {
17875cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith            Info.Diag(Conv, diag::note_constexpr_ltor_non_const_int, 1) << VD;
17887098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith            Info.Note(VD->getLocation(), diag::note_declared_at);
17897098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith          } else {
17905cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith            Info.Diag(Conv);
17917098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith          }
17927098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith          return false;
17937098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        }
17947098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      } else if (VT->isFloatingType() && VT.isConstQualified()) {
17957098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        // We support folding of const floating-point types, in order to make
17967098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        // static const data members of such types (supported as an extension)
17977098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        // more useful.
17987098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        if (Info.getLangOpts().CPlusPlus0x) {
17995cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith          Info.CCEDiag(Conv, diag::note_constexpr_ltor_non_constexpr, 1) << VD;
18007098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith          Info.Note(VD->getLocation(), diag::note_declared_at);
18017098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        } else {
18025cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith          Info.CCEDiag(Conv);
18037098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        }
18047098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      } else {
18057098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        // FIXME: Allow folding of values of any literal type in all languages.
18067098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        if (Info.getLangOpts().CPlusPlus0x) {
18075cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith          Info.Diag(Conv, diag::note_constexpr_ltor_non_constexpr, 1) << VD;
18087098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith          Info.Note(VD->getLocation(), diag::note_declared_at);
18097098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        } else {
18105cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith          Info.Diag(Conv);
18117098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        }
18120a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith        return false;
1813f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      }
18140a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    }
18157098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith
1816f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!EvaluateVarDeclInit(Info, Conv, VD, Frame, RVal))
1817c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith      return false;
1818c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
181947a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    if (isa<ParmVarDecl>(VD) || !VD->getAnyInitializer()->isLValue())
1820f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return ExtractSubobject(Info, Conv, RVal, VT, LVal.Designator, Type);
1821c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
1822c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    // The declaration was initialized by an lvalue, with no lvalue-to-rvalue
1823c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    // conversion. This happens when the declaration and the lvalue should be
1824c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    // considered synonymous, for instance when initializing an array of char
1825c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    // from a string literal. Continue as if the initializer lvalue was the
1826c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    // value we were originally given.
18270a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    assert(RVal.getLValueOffset().isZero() &&
18280a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith           "offset for lvalue init of non-reference");
18291bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    Base = RVal.getLValueBase().get<const Expr*>();
183083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith
183183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    if (unsigned CallIndex = RVal.getLValueCallIndex()) {
183283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      Frame = Info.getCallFrame(CallIndex);
183383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      if (!Frame) {
18345cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith        Info.Diag(Conv, diag::note_constexpr_lifetime_ended, 1) << !Base;
183583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith        NoteLValueLocation(Info, RVal.getLValueBase());
183683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith        return false;
183783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      }
183883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    } else {
183983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      Frame = 0;
184083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    }
1841c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  }
1842c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
18437098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  // Volatile temporary objects cannot be read in constant expressions.
18447098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  if (Base->getType().isVolatileQualified()) {
18457098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    if (Info.getLangOpts().CPlusPlus) {
18465cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      Info.Diag(Conv, diag::note_constexpr_ltor_volatile_obj, 1) << 0;
18477098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      Info.Note(Base->getExprLoc(), diag::note_constexpr_temporary_here);
18487098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    } else {
18495cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      Info.Diag(Conv);
18507098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    }
18517098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    return false;
18527098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  }
18537098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith
1854177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith  if (Frame) {
1855cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    // If this is a temporary expression with a nontrivial initializer, grab the
1856cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    // value from the relevant stack frame.
1857177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith    RVal = Frame->Temporaries[Base];
1858cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  } else if (const CompoundLiteralExpr *CLE
1859cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith             = dyn_cast<CompoundLiteralExpr>(Base)) {
1860cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    // In C99, a CompoundLiteralExpr is an lvalue, and we defer evaluating the
1861cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    // initializer until now for such expressions. Such an expression can't be
1862cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    // an ICE in C, so this only matters for fold.
1863c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?");
1864cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    if (!Evaluate(RVal, Info, CLE->getInitializer()))
1865cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith      return false;
1866f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith  } else if (isa<StringLiteral>(Base)) {
1867f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith    // We represent a string literal array as an lvalue pointing at the
1868f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith    // corresponding expression, rather than building an array of chars.
1869f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith    // FIXME: Support PredefinedExpr, ObjCEncodeExpr, MakeStringConstant
18701aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    RVal = APValue(Base, CharUnits::Zero(), APValue::NoLValuePath(), 0);
1871f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  } else {
18725cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.Diag(Conv, diag::note_invalid_subexpr_in_const_expr);
1873cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    return false;
1874f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
1875c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
1876f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  return ExtractSubobject(Info, Conv, RVal, Base->getType(), LVal.Designator,
1877f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                          Type);
1878c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith}
1879c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
188059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith/// Build an lvalue for the object argument of a member function call.
188159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithstatic bool EvaluateObjectArgument(EvalInfo &Info, const Expr *Object,
188259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith                                   LValue &This) {
188359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  if (Object->getType()->isPointerType())
188459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    return EvaluatePointer(Object, This, Info);
188559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
188659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  if (Object->isGLValue())
188759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    return EvaluateLValue(Object, This, Info);
188859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
1889e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (Object->getType()->isLiteralType())
1890e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return EvaluateTemporary(Object, This, Info);
1891e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1892e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  return false;
1893e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}
1894e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1895e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// HandleMemberPointerAccess - Evaluate a member access operation and build an
1896e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// lvalue referring to the result.
1897e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith///
1898e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// \param Info - Information about the ongoing evaluation.
1899e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// \param BO - The member pointer access operation.
1900e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// \param LV - Filled in with a reference to the resulting object.
1901e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// \param IncludeMember - Specifies whether the member itself is included in
1902e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith///        the resulting LValue subobject designator. This is not possible when
1903e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith///        creating a bound member function.
1904e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// \return The field or method declaration to which the member pointer refers,
1905e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith///         or 0 if evaluation fails.
1906e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info,
1907e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                                                  const BinaryOperator *BO,
1908e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                                                  LValue &LV,
1909e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                                                  bool IncludeMember = true) {
1910e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  assert(BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI);
1911e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1912745f5147e065900267c85a5568785a1991d4838fRichard Smith  bool EvalObjOK = EvaluateObjectArgument(Info, BO->getLHS(), LV);
1913745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!EvalObjOK && !Info.keepEvaluatingAfterFailure())
1914e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return 0;
1915e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1916e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  MemberPtr MemPtr;
1917e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (!EvaluateMemberPointer(BO->getRHS(), MemPtr, Info))
1918e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return 0;
1919e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1920e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // C++11 [expr.mptr.oper]p6: If the second operand is the null pointer to
1921e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // member value, the behavior is undefined.
1922e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (!MemPtr.getDecl())
1923e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return 0;
1924e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1925745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!EvalObjOK)
1926745f5147e065900267c85a5568785a1991d4838fRichard Smith    return 0;
1927745f5147e065900267c85a5568785a1991d4838fRichard Smith
1928e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (MemPtr.isDerivedMember()) {
1929e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // This is a member of some derived class. Truncate LV appropriately.
1930e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // The end of the derived-to-base path for the base object must match the
1931e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // derived-to-base path for the member pointer.
1932b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    if (LV.Designator.MostDerivedPathLength + MemPtr.Path.size() >
1933e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        LV.Designator.Entries.size())
1934e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return 0;
1935e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    unsigned PathLengthToMember =
1936e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        LV.Designator.Entries.size() - MemPtr.Path.size();
1937e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    for (unsigned I = 0, N = MemPtr.Path.size(); I != N; ++I) {
1938e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      const CXXRecordDecl *LVDecl = getAsBaseClass(
1939e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith          LV.Designator.Entries[PathLengthToMember + I]);
1940e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      const CXXRecordDecl *MPDecl = MemPtr.Path[I];
1941e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (LVDecl->getCanonicalDecl() != MPDecl->getCanonicalDecl())
1942e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return 0;
1943e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
1944e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1945e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // Truncate the lvalue to the appropriate derived class.
1946b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    if (!CastToDerivedClass(Info, BO, LV, MemPtr.getContainingRecord(),
1947b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                            PathLengthToMember))
1948b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      return 0;
1949e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  } else if (!MemPtr.Path.empty()) {
1950e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // Extend the LValue path with the member pointer's path.
1951e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    LV.Designator.Entries.reserve(LV.Designator.Entries.size() +
1952e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                                  MemPtr.Path.size() + IncludeMember);
1953e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1954e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // Walk down to the appropriate base class.
1955e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    QualType LVType = BO->getLHS()->getType();
1956e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (const PointerType *PT = LVType->getAs<PointerType>())
1957e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      LVType = PT->getPointeeType();
1958e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    const CXXRecordDecl *RD = LVType->getAsCXXRecordDecl();
1959e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    assert(RD && "member pointer access on non-class-type expression");
1960e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // The first class in the path is that of the lvalue.
1961e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    for (unsigned I = 1, N = MemPtr.Path.size(); I != N; ++I) {
1962e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      const CXXRecordDecl *Base = MemPtr.Path[N - I - 1];
19638d59deec807ed53efcd07855199cdc9c979f447fJohn McCall      if (!HandleLValueDirectBase(Info, BO, LV, RD, Base))
19648d59deec807ed53efcd07855199cdc9c979f447fJohn McCall        return 0;
1965e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      RD = Base;
1966e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
1967e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // Finally cast to the class containing the member.
19688d59deec807ed53efcd07855199cdc9c979f447fJohn McCall    if (!HandleLValueDirectBase(Info, BO, LV, RD, MemPtr.getContainingRecord()))
19698d59deec807ed53efcd07855199cdc9c979f447fJohn McCall      return 0;
1970e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
1971e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1972e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // Add the member. Note that we cannot build bound member functions here.
1973e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (IncludeMember) {
19748d59deec807ed53efcd07855199cdc9c979f447fJohn McCall    if (const FieldDecl *FD = dyn_cast<FieldDecl>(MemPtr.getDecl())) {
19758d59deec807ed53efcd07855199cdc9c979f447fJohn McCall      if (!HandleLValueMember(Info, BO, LV, FD))
19768d59deec807ed53efcd07855199cdc9c979f447fJohn McCall        return 0;
19778d59deec807ed53efcd07855199cdc9c979f447fJohn McCall    } else if (const IndirectFieldDecl *IFD =
19788d59deec807ed53efcd07855199cdc9c979f447fJohn McCall                 dyn_cast<IndirectFieldDecl>(MemPtr.getDecl())) {
19798d59deec807ed53efcd07855199cdc9c979f447fJohn McCall      if (!HandleLValueIndirectMember(Info, BO, LV, IFD))
19808d59deec807ed53efcd07855199cdc9c979f447fJohn McCall        return 0;
19818d59deec807ed53efcd07855199cdc9c979f447fJohn McCall    } else {
1982d9b02e726262e4009dda830998bb934172ac0020Richard Smith      llvm_unreachable("can't construct reference to bound member function");
19838d59deec807ed53efcd07855199cdc9c979f447fJohn McCall    }
1984e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
1985e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1986e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  return MemPtr.getDecl();
1987e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}
1988e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1989e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// HandleBaseToDerivedCast - Apply the given base-to-derived cast operation on
1990e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// the provided lvalue, which currently refers to the base object.
1991e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E,
1992e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                                    LValue &Result) {
1993e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  SubobjectDesignator &D = Result.Designator;
1994b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (D.Invalid || !Result.checkNullPointer(Info, E, CSK_Derived))
1995e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return false;
1996e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1997e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  QualType TargetQT = E->getType();
1998e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (const PointerType *PT = TargetQT->getAs<PointerType>())
1999e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    TargetQT = PT->getPointeeType();
2000b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
2001b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // Check this cast lands within the final derived-to-base subobject path.
2002b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (D.MostDerivedPathLength + E->path_size() > D.Entries.size()) {
20035cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.CCEDiag(E, diag::note_constexpr_invalid_downcast)
2004b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      << D.MostDerivedType << TargetQT;
2005b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    return false;
2006b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  }
2007b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
2008b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // Check the type of the final cast. We don't need to check the path,
2009b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // since a cast can only be formed if the path is unique.
2010b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  unsigned NewEntriesSize = D.Entries.size() - E->path_size();
2011e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  const CXXRecordDecl *TargetType = TargetQT->getAsCXXRecordDecl();
2012e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  const CXXRecordDecl *FinalType;
2013b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (NewEntriesSize == D.MostDerivedPathLength)
2014b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    FinalType = D.MostDerivedType->getAsCXXRecordDecl();
2015b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  else
2016e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    FinalType = getAsBaseClass(D.Entries[NewEntriesSize - 1]);
2017b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl()) {
20185cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.CCEDiag(E, diag::note_constexpr_invalid_downcast)
2019b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      << D.MostDerivedType << TargetQT;
2020e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return false;
2021b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  }
2022e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2023e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // Truncate the lvalue to the appropriate derived class.
2024b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  return CastToDerivedClass(Info, E, Result, TargetType, NewEntriesSize);
202559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
202659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
2027c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stumpnamespace {
2028d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smithenum EvalStmtResult {
2029d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  /// Evaluation failed.
2030d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  ESR_Failed,
2031d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  /// Hit a 'return' statement.
2032d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  ESR_Returned,
2033d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  /// Evaluation succeeded.
2034d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  ESR_Succeeded
2035d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith};
2036d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith}
2037d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
2038d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith// Evaluate a statement.
20391aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smithstatic EvalStmtResult EvaluateStmt(APValue &Result, EvalInfo &Info,
2040d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith                                   const Stmt *S) {
2041d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  switch (S->getStmtClass()) {
2042d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  default:
2043d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    return ESR_Failed;
2044d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
2045d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  case Stmt::NullStmtClass:
2046d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  case Stmt::DeclStmtClass:
2047d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    return ESR_Succeeded;
2048d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
2049c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  case Stmt::ReturnStmtClass: {
2050c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    const Expr *RetExpr = cast<ReturnStmt>(S)->getRetValue();
205183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    if (!Evaluate(Result, Info, RetExpr))
2052c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      return ESR_Failed;
2053c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return ESR_Returned;
2054c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  }
2055d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
2056d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  case Stmt::CompoundStmtClass: {
2057d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    const CompoundStmt *CS = cast<CompoundStmt>(S);
2058d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    for (CompoundStmt::const_body_iterator BI = CS->body_begin(),
2059d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith           BE = CS->body_end(); BI != BE; ++BI) {
2060d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith      EvalStmtResult ESR = EvaluateStmt(Result, Info, *BI);
2061d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith      if (ESR != ESR_Succeeded)
2062d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith        return ESR;
2063d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    }
2064d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    return ESR_Succeeded;
2065d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  }
2066d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  }
2067d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith}
2068d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
20696180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith/// CheckTrivialDefaultConstructor - Check whether a constructor is a trivial
20706180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith/// default constructor. If so, we'll fold it whether or not it's marked as
20716180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith/// constexpr. If it is marked as constexpr, we will never implicitly define it,
20726180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith/// so we need special handling.
20736180245e9f63d2927b185ec251fb75aba30f1cacRichard Smithstatic bool CheckTrivialDefaultConstructor(EvalInfo &Info, SourceLocation Loc,
207451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith                                           const CXXConstructorDecl *CD,
207551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith                                           bool IsValueInitialization) {
20766180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith  if (!CD->isTrivial() || !CD->isDefaultConstructor())
20776180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    return false;
20786180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith
20794c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  // Value-initialization does not call a trivial default constructor, so such a
20804c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  // call is a core constant expression whether or not the constructor is
20814c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  // constexpr.
20824c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  if (!CD->isConstexpr() && !IsValueInitialization) {
20836180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    if (Info.getLangOpts().CPlusPlus0x) {
20844c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith      // FIXME: If DiagDecl is an implicitly-declared special member function,
20854c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith      // we should be much more explicit about why it's not constexpr.
20864c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith      Info.CCEDiag(Loc, diag::note_constexpr_invalid_function, 1)
20874c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith        << /*IsConstexpr*/0 << /*IsConstructor*/1 << CD;
20884c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith      Info.Note(CD->getLocation(), diag::note_declared_at);
20896180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    } else {
20906180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith      Info.CCEDiag(Loc, diag::note_invalid_subexpr_in_const_expr);
20916180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    }
20926180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith  }
20936180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith  return true;
20946180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith}
20956180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith
2096c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith/// CheckConstexprFunction - Check that a function can be called in a constant
2097c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith/// expression.
2098c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstatic bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc,
2099c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                   const FunctionDecl *Declaration,
2100c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                   const FunctionDecl *Definition) {
2101745f5147e065900267c85a5568785a1991d4838fRichard Smith  // Potential constant expressions can contain calls to declared, but not yet
2102745f5147e065900267c85a5568785a1991d4838fRichard Smith  // defined, constexpr functions.
2103745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (Info.CheckingPotentialConstantExpression && !Definition &&
2104745f5147e065900267c85a5568785a1991d4838fRichard Smith      Declaration->isConstexpr())
2105745f5147e065900267c85a5568785a1991d4838fRichard Smith    return false;
2106745f5147e065900267c85a5568785a1991d4838fRichard Smith
2107c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  // Can we evaluate this function call?
2108c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  if (Definition && Definition->isConstexpr() && !Definition->isInvalidDecl())
2109c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return true;
2110c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
2111c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  if (Info.getLangOpts().CPlusPlus0x) {
2112c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    const FunctionDecl *DiagDecl = Definition ? Definition : Declaration;
2113099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    // FIXME: If DiagDecl is an implicitly-declared special member function, we
2114099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    // should be much more explicit about why it's not constexpr.
2115c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    Info.Diag(CallLoc, diag::note_constexpr_invalid_function, 1)
2116c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      << DiagDecl->isConstexpr() << isa<CXXConstructorDecl>(DiagDecl)
2117c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      << DiagDecl;
2118c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    Info.Note(DiagDecl->getLocation(), diag::note_declared_at);
2119c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  } else {
2120c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    Info.Diag(CallLoc, diag::note_invalid_subexpr_in_const_expr);
2121c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  }
2122c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  return false;
2123c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
2124c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
2125180f47959a066795cc0f409433023af448bb0328Richard Smithnamespace {
21261aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smithtypedef SmallVector<APValue, 8> ArgVector;
2127180f47959a066795cc0f409433023af448bb0328Richard Smith}
2128180f47959a066795cc0f409433023af448bb0328Richard Smith
2129180f47959a066795cc0f409433023af448bb0328Richard Smith/// EvaluateArgs - Evaluate the arguments to a function call.
2130180f47959a066795cc0f409433023af448bb0328Richard Smithstatic bool EvaluateArgs(ArrayRef<const Expr*> Args, ArgVector &ArgValues,
2131180f47959a066795cc0f409433023af448bb0328Richard Smith                         EvalInfo &Info) {
2132745f5147e065900267c85a5568785a1991d4838fRichard Smith  bool Success = true;
2133180f47959a066795cc0f409433023af448bb0328Richard Smith  for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end();
2134745f5147e065900267c85a5568785a1991d4838fRichard Smith       I != E; ++I) {
2135745f5147e065900267c85a5568785a1991d4838fRichard Smith    if (!Evaluate(ArgValues[I - Args.begin()], Info, *I)) {
2136745f5147e065900267c85a5568785a1991d4838fRichard Smith      // If we're checking for a potential constant expression, evaluate all
2137745f5147e065900267c85a5568785a1991d4838fRichard Smith      // initializers even if some of them fail.
2138745f5147e065900267c85a5568785a1991d4838fRichard Smith      if (!Info.keepEvaluatingAfterFailure())
2139745f5147e065900267c85a5568785a1991d4838fRichard Smith        return false;
2140745f5147e065900267c85a5568785a1991d4838fRichard Smith      Success = false;
2141745f5147e065900267c85a5568785a1991d4838fRichard Smith    }
2142745f5147e065900267c85a5568785a1991d4838fRichard Smith  }
2143745f5147e065900267c85a5568785a1991d4838fRichard Smith  return Success;
2144180f47959a066795cc0f409433023af448bb0328Richard Smith}
2145180f47959a066795cc0f409433023af448bb0328Richard Smith
2146d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith/// Evaluate a function call.
2147745f5147e065900267c85a5568785a1991d4838fRichard Smithstatic bool HandleFunctionCall(SourceLocation CallLoc,
2148745f5147e065900267c85a5568785a1991d4838fRichard Smith                               const FunctionDecl *Callee, const LValue *This,
2149f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                               ArrayRef<const Expr*> Args, const Stmt *Body,
21501aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith                               EvalInfo &Info, APValue &Result) {
2151180f47959a066795cc0f409433023af448bb0328Richard Smith  ArgVector ArgValues(Args.size());
2152180f47959a066795cc0f409433023af448bb0328Richard Smith  if (!EvaluateArgs(Args, ArgValues, Info))
2153180f47959a066795cc0f409433023af448bb0328Richard Smith    return false;
2154d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
2155745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!Info.CheckCallLimit(CallLoc))
2156745f5147e065900267c85a5568785a1991d4838fRichard Smith    return false;
2157745f5147e065900267c85a5568785a1991d4838fRichard Smith
2158745f5147e065900267c85a5568785a1991d4838fRichard Smith  CallStackFrame Frame(Info, CallLoc, Callee, This, ArgValues.data());
2159d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  return EvaluateStmt(Result, Info, Body) == ESR_Returned;
2160d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith}
2161d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
2162180f47959a066795cc0f409433023af448bb0328Richard Smith/// Evaluate a constructor call.
2163745f5147e065900267c85a5568785a1991d4838fRichard Smithstatic bool HandleConstructorCall(SourceLocation CallLoc, const LValue &This,
216459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith                                  ArrayRef<const Expr*> Args,
2165180f47959a066795cc0f409433023af448bb0328Richard Smith                                  const CXXConstructorDecl *Definition,
216651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith                                  EvalInfo &Info, APValue &Result) {
2167180f47959a066795cc0f409433023af448bb0328Richard Smith  ArgVector ArgValues(Args.size());
2168180f47959a066795cc0f409433023af448bb0328Richard Smith  if (!EvaluateArgs(Args, ArgValues, Info))
2169180f47959a066795cc0f409433023af448bb0328Richard Smith    return false;
2170180f47959a066795cc0f409433023af448bb0328Richard Smith
2171745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!Info.CheckCallLimit(CallLoc))
2172745f5147e065900267c85a5568785a1991d4838fRichard Smith    return false;
2173745f5147e065900267c85a5568785a1991d4838fRichard Smith
217486c3ae46250cdcc57778c27826060779a92f3815Richard Smith  const CXXRecordDecl *RD = Definition->getParent();
217586c3ae46250cdcc57778c27826060779a92f3815Richard Smith  if (RD->getNumVBases()) {
217686c3ae46250cdcc57778c27826060779a92f3815Richard Smith    Info.Diag(CallLoc, diag::note_constexpr_virtual_base) << RD;
217786c3ae46250cdcc57778c27826060779a92f3815Richard Smith    return false;
217886c3ae46250cdcc57778c27826060779a92f3815Richard Smith  }
217986c3ae46250cdcc57778c27826060779a92f3815Richard Smith
2180745f5147e065900267c85a5568785a1991d4838fRichard Smith  CallStackFrame Frame(Info, CallLoc, Definition, &This, ArgValues.data());
2181180f47959a066795cc0f409433023af448bb0328Richard Smith
2182180f47959a066795cc0f409433023af448bb0328Richard Smith  // If it's a delegating constructor, just delegate.
2183180f47959a066795cc0f409433023af448bb0328Richard Smith  if (Definition->isDelegatingConstructor()) {
2184180f47959a066795cc0f409433023af448bb0328Richard Smith    CXXConstructorDecl::init_const_iterator I = Definition->init_begin();
218583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return EvaluateInPlace(Result, Info, This, (*I)->getInit());
2186180f47959a066795cc0f409433023af448bb0328Richard Smith  }
2187180f47959a066795cc0f409433023af448bb0328Richard Smith
2188610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith  // For a trivial copy or move constructor, perform an APValue copy. This is
2189610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith  // essential for unions, where the operations performed by the constructor
2190610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith  // cannot be represented by ctor-initializers.
2191610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith  if (Definition->isDefaulted() &&
2192f6cfe8ba2b4d98c20181568e449edf0b60904b03Douglas Gregor      ((Definition->isCopyConstructor() && Definition->isTrivial()) ||
2193f6cfe8ba2b4d98c20181568e449edf0b60904b03Douglas Gregor       (Definition->isMoveConstructor() && Definition->isTrivial()))) {
2194610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith    LValue RHS;
21951aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    RHS.setFrom(Info.Ctx, ArgValues[0]);
21961aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    return HandleLValueToRValueConversion(Info, Args[0], Args[0]->getType(),
21971aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith                                          RHS, Result);
2198610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith  }
2199610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith
2200610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith  // Reserve space for the struct members.
220151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (!RD->isUnion() && Result.isUninit())
2202180f47959a066795cc0f409433023af448bb0328Richard Smith    Result = APValue(APValue::UninitStruct(), RD->getNumBases(),
2203180f47959a066795cc0f409433023af448bb0328Richard Smith                     std::distance(RD->field_begin(), RD->field_end()));
2204180f47959a066795cc0f409433023af448bb0328Richard Smith
22058d59deec807ed53efcd07855199cdc9c979f447fJohn McCall  if (RD->isInvalidDecl()) return false;
2206180f47959a066795cc0f409433023af448bb0328Richard Smith  const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD);
2207180f47959a066795cc0f409433023af448bb0328Richard Smith
2208745f5147e065900267c85a5568785a1991d4838fRichard Smith  bool Success = true;
2209180f47959a066795cc0f409433023af448bb0328Richard Smith  unsigned BasesSeen = 0;
2210180f47959a066795cc0f409433023af448bb0328Richard Smith#ifndef NDEBUG
2211180f47959a066795cc0f409433023af448bb0328Richard Smith  CXXRecordDecl::base_class_const_iterator BaseIt = RD->bases_begin();
2212180f47959a066795cc0f409433023af448bb0328Richard Smith#endif
2213180f47959a066795cc0f409433023af448bb0328Richard Smith  for (CXXConstructorDecl::init_const_iterator I = Definition->init_begin(),
2214180f47959a066795cc0f409433023af448bb0328Richard Smith       E = Definition->init_end(); I != E; ++I) {
2215745f5147e065900267c85a5568785a1991d4838fRichard Smith    LValue Subobject = This;
2216745f5147e065900267c85a5568785a1991d4838fRichard Smith    APValue *Value = &Result;
2217745f5147e065900267c85a5568785a1991d4838fRichard Smith
2218745f5147e065900267c85a5568785a1991d4838fRichard Smith    // Determine the subobject to initialize.
2219180f47959a066795cc0f409433023af448bb0328Richard Smith    if ((*I)->isBaseInitializer()) {
2220180f47959a066795cc0f409433023af448bb0328Richard Smith      QualType BaseType((*I)->getBaseClass(), 0);
2221180f47959a066795cc0f409433023af448bb0328Richard Smith#ifndef NDEBUG
2222180f47959a066795cc0f409433023af448bb0328Richard Smith      // Non-virtual base classes are initialized in the order in the class
222386c3ae46250cdcc57778c27826060779a92f3815Richard Smith      // definition. We have already checked for virtual base classes.
2224180f47959a066795cc0f409433023af448bb0328Richard Smith      assert(!BaseIt->isVirtual() && "virtual base for literal type");
2225180f47959a066795cc0f409433023af448bb0328Richard Smith      assert(Info.Ctx.hasSameType(BaseIt->getType(), BaseType) &&
2226180f47959a066795cc0f409433023af448bb0328Richard Smith             "base class initializers not in expected order");
2227180f47959a066795cc0f409433023af448bb0328Richard Smith      ++BaseIt;
2228180f47959a066795cc0f409433023af448bb0328Richard Smith#endif
22298d59deec807ed53efcd07855199cdc9c979f447fJohn McCall      if (!HandleLValueDirectBase(Info, (*I)->getInit(), Subobject, RD,
22308d59deec807ed53efcd07855199cdc9c979f447fJohn McCall                                  BaseType->getAsCXXRecordDecl(), &Layout))
22318d59deec807ed53efcd07855199cdc9c979f447fJohn McCall        return false;
2232745f5147e065900267c85a5568785a1991d4838fRichard Smith      Value = &Result.getStructBase(BasesSeen++);
2233180f47959a066795cc0f409433023af448bb0328Richard Smith    } else if (FieldDecl *FD = (*I)->getMember()) {
22348d59deec807ed53efcd07855199cdc9c979f447fJohn McCall      if (!HandleLValueMember(Info, (*I)->getInit(), Subobject, FD, &Layout))
22358d59deec807ed53efcd07855199cdc9c979f447fJohn McCall        return false;
2236180f47959a066795cc0f409433023af448bb0328Richard Smith      if (RD->isUnion()) {
2237180f47959a066795cc0f409433023af448bb0328Richard Smith        Result = APValue(FD);
2238745f5147e065900267c85a5568785a1991d4838fRichard Smith        Value = &Result.getUnionValue();
2239745f5147e065900267c85a5568785a1991d4838fRichard Smith      } else {
2240745f5147e065900267c85a5568785a1991d4838fRichard Smith        Value = &Result.getStructField(FD->getFieldIndex());
2241745f5147e065900267c85a5568785a1991d4838fRichard Smith      }
2242d9b02e726262e4009dda830998bb934172ac0020Richard Smith    } else if (IndirectFieldDecl *IFD = (*I)->getIndirectMember()) {
2243d9b02e726262e4009dda830998bb934172ac0020Richard Smith      // Walk the indirect field decl's chain to find the object to initialize,
2244d9b02e726262e4009dda830998bb934172ac0020Richard Smith      // and make sure we've initialized every step along it.
2245d9b02e726262e4009dda830998bb934172ac0020Richard Smith      for (IndirectFieldDecl::chain_iterator C = IFD->chain_begin(),
2246d9b02e726262e4009dda830998bb934172ac0020Richard Smith                                             CE = IFD->chain_end();
2247d9b02e726262e4009dda830998bb934172ac0020Richard Smith           C != CE; ++C) {
2248d9b02e726262e4009dda830998bb934172ac0020Richard Smith        FieldDecl *FD = cast<FieldDecl>(*C);
2249d9b02e726262e4009dda830998bb934172ac0020Richard Smith        CXXRecordDecl *CD = cast<CXXRecordDecl>(FD->getParent());
2250d9b02e726262e4009dda830998bb934172ac0020Richard Smith        // Switch the union field if it differs. This happens if we had
2251d9b02e726262e4009dda830998bb934172ac0020Richard Smith        // preceding zero-initialization, and we're now initializing a union
2252d9b02e726262e4009dda830998bb934172ac0020Richard Smith        // subobject other than the first.
2253d9b02e726262e4009dda830998bb934172ac0020Richard Smith        // FIXME: In this case, the values of the other subobjects are
2254d9b02e726262e4009dda830998bb934172ac0020Richard Smith        // specified, since zero-initialization sets all padding bits to zero.
2255d9b02e726262e4009dda830998bb934172ac0020Richard Smith        if (Value->isUninit() ||
2256d9b02e726262e4009dda830998bb934172ac0020Richard Smith            (Value->isUnion() && Value->getUnionField() != FD)) {
2257d9b02e726262e4009dda830998bb934172ac0020Richard Smith          if (CD->isUnion())
2258d9b02e726262e4009dda830998bb934172ac0020Richard Smith            *Value = APValue(FD);
2259d9b02e726262e4009dda830998bb934172ac0020Richard Smith          else
2260d9b02e726262e4009dda830998bb934172ac0020Richard Smith            *Value = APValue(APValue::UninitStruct(), CD->getNumBases(),
2261d9b02e726262e4009dda830998bb934172ac0020Richard Smith                             std::distance(CD->field_begin(), CD->field_end()));
2262d9b02e726262e4009dda830998bb934172ac0020Richard Smith        }
22638d59deec807ed53efcd07855199cdc9c979f447fJohn McCall        if (!HandleLValueMember(Info, (*I)->getInit(), Subobject, FD))
22648d59deec807ed53efcd07855199cdc9c979f447fJohn McCall          return false;
2265d9b02e726262e4009dda830998bb934172ac0020Richard Smith        if (CD->isUnion())
2266d9b02e726262e4009dda830998bb934172ac0020Richard Smith          Value = &Value->getUnionValue();
2267d9b02e726262e4009dda830998bb934172ac0020Richard Smith        else
2268d9b02e726262e4009dda830998bb934172ac0020Richard Smith          Value = &Value->getStructField(FD->getFieldIndex());
2269d9b02e726262e4009dda830998bb934172ac0020Richard Smith      }
2270180f47959a066795cc0f409433023af448bb0328Richard Smith    } else {
2271d9b02e726262e4009dda830998bb934172ac0020Richard Smith      llvm_unreachable("unknown base initializer kind");
2272180f47959a066795cc0f409433023af448bb0328Richard Smith    }
2273745f5147e065900267c85a5568785a1991d4838fRichard Smith
227483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    if (!EvaluateInPlace(*Value, Info, Subobject, (*I)->getInit(),
227583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                         (*I)->isBaseInitializer()
2276745f5147e065900267c85a5568785a1991d4838fRichard Smith                                      ? CCEK_Constant : CCEK_MemberInit)) {
2277745f5147e065900267c85a5568785a1991d4838fRichard Smith      // If we're checking for a potential constant expression, evaluate all
2278745f5147e065900267c85a5568785a1991d4838fRichard Smith      // initializers even if some of them fail.
2279745f5147e065900267c85a5568785a1991d4838fRichard Smith      if (!Info.keepEvaluatingAfterFailure())
2280745f5147e065900267c85a5568785a1991d4838fRichard Smith        return false;
2281745f5147e065900267c85a5568785a1991d4838fRichard Smith      Success = false;
2282745f5147e065900267c85a5568785a1991d4838fRichard Smith    }
2283180f47959a066795cc0f409433023af448bb0328Richard Smith  }
2284180f47959a066795cc0f409433023af448bb0328Richard Smith
2285745f5147e065900267c85a5568785a1991d4838fRichard Smith  return Success;
2286180f47959a066795cc0f409433023af448bb0328Richard Smith}
2287180f47959a066795cc0f409433023af448bb0328Richard Smith
22884efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman//===----------------------------------------------------------------------===//
22898cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne// Generic Evaluation
22908cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne//===----------------------------------------------------------------------===//
22918cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournenamespace {
22928cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
2293f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith// FIXME: RetTy is always bool. Remove it.
2294f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithtemplate <class Derived, typename RetTy=bool>
22958cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourneclass ExprEvaluatorBase
22968cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  : public ConstStmtVisitor<Derived, RetTy> {
22978cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourneprivate:
22981aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  RetTy DerivedSuccess(const APValue &V, const Expr *E) {
22998cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    return static_cast<Derived*>(this)->Success(V, E);
23008cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  }
230151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  RetTy DerivedZeroInitialization(const Expr *E) {
230251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return static_cast<Derived*>(this)->ZeroInitialization(E);
2303f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  }
23048cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
230574e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  // Check whether a conditional operator with a non-constant condition is a
230674e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  // potential constant expression. If neither arm is a potential constant
230774e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  // expression, then the conditional operator is not either.
230874e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  template<typename ConditionalOperator>
230974e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  void CheckPotentialConstantConditional(const ConditionalOperator *E) {
231074e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    assert(Info.CheckingPotentialConstantExpression);
231174e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith
231274e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    // Speculatively evaluate both arms.
231374e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    {
231474e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith      llvm::SmallVector<PartialDiagnosticAt, 8> Diag;
231574e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith      SpeculativeEvaluationRAII Speculate(Info, &Diag);
231674e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith
231774e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith      StmtVisitorTy::Visit(E->getFalseExpr());
231874e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith      if (Diag.empty())
231974e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith        return;
232074e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith
232174e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith      Diag.clear();
232274e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith      StmtVisitorTy::Visit(E->getTrueExpr());
232374e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith      if (Diag.empty())
232474e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith        return;
232574e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    }
232674e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith
232774e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    Error(E, diag::note_constexpr_conditional_never_const);
232874e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  }
232974e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith
233074e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith
233174e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  template<typename ConditionalOperator>
233274e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  bool HandleConditionalOperator(const ConditionalOperator *E) {
233374e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    bool BoolResult;
233474e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) {
233574e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith      if (Info.CheckingPotentialConstantExpression)
233674e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith        CheckPotentialConstantConditional(E);
233774e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith      return false;
233874e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    }
233974e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith
234074e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
234174e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    return StmtVisitorTy::Visit(EvalExpr);
234274e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  }
234374e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith
23448cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourneprotected:
23458cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  EvalInfo &Info;
23468cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  typedef ConstStmtVisitor<Derived, RetTy> StmtVisitorTy;
23478cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  typedef ExprEvaluatorBase ExprEvaluatorBaseTy;
23488cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
2349dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith  OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) {
23505cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    return Info.CCEDiag(E, D);
2351f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
2352f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
2353cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  RetTy ZeroInitialization(const Expr *E) { return Error(E); }
2354cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
2355cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidispublic:
2356cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {}
2357cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
2358cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  EvalInfo &getEvalInfo() { return Info; }
2359cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
2360f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  /// Report an evaluation error. This should only be called when an error is
2361f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  /// first discovered. When propagating an error, just return false.
2362f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  bool Error(const Expr *E, diag::kind D) {
23635cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.Diag(E, D);
2364f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return false;
2365f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
2366f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  bool Error(const Expr *E) {
2367f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E, diag::note_invalid_subexpr_in_const_expr);
2368f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
2369f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
23708cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitStmt(const Stmt *) {
2371b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Expression evaluator should not be called on stmts");
23728cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  }
23738cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitExpr(const Expr *E) {
2374f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
23758cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  }
23768cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
23778cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitParenExpr(const ParenExpr *E)
23788cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    { return StmtVisitorTy::Visit(E->getSubExpr()); }
23798cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitUnaryExtension(const UnaryOperator *E)
23808cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    { return StmtVisitorTy::Visit(E->getSubExpr()); }
23818cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitUnaryPlus(const UnaryOperator *E)
23828cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    { return StmtVisitorTy::Visit(E->getSubExpr()); }
23838cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitChooseExpr(const ChooseExpr *E)
23848cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    { return StmtVisitorTy::Visit(E->getChosenSubExpr(Info.Ctx)); }
23858cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitGenericSelectionExpr(const GenericSelectionExpr *E)
23868cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    { return StmtVisitorTy::Visit(E->getResultExpr()); }
238791a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall  RetTy VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E)
238891a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall    { return StmtVisitorTy::Visit(E->getReplacement()); }
23893d75ca836205856077c18e30e9447accbd85f751Richard Smith  RetTy VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E)
23903d75ca836205856077c18e30e9447accbd85f751Richard Smith    { return StmtVisitorTy::Visit(E->getExpr()); }
2391bc6abe93a5d6b1305411f8b6f54c2caa686ddc69Richard Smith  // We cannot create any objects for which cleanups are required, so there is
2392bc6abe93a5d6b1305411f8b6f54c2caa686ddc69Richard Smith  // nothing to do here; all cleanups must come from unevaluated subexpressions.
2393bc6abe93a5d6b1305411f8b6f54c2caa686ddc69Richard Smith  RetTy VisitExprWithCleanups(const ExprWithCleanups *E)
2394bc6abe93a5d6b1305411f8b6f54c2caa686ddc69Richard Smith    { return StmtVisitorTy::Visit(E->getSubExpr()); }
23958cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
2396c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith  RetTy VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E) {
2397c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith    CCEDiag(E, diag::note_constexpr_invalid_cast) << 0;
2398c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith    return static_cast<Derived*>(this)->VisitCastExpr(E);
2399c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith  }
2400c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith  RetTy VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) {
2401c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith    CCEDiag(E, diag::note_constexpr_invalid_cast) << 1;
2402c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith    return static_cast<Derived*>(this)->VisitCastExpr(E);
2403c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith  }
2404c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith
2405e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  RetTy VisitBinaryOperator(const BinaryOperator *E) {
2406e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    switch (E->getOpcode()) {
2407e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    default:
2408f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E);
2409e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2410e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    case BO_Comma:
2411e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      VisitIgnoredValue(E->getLHS());
2412e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return StmtVisitorTy::Visit(E->getRHS());
2413e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2414e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    case BO_PtrMemD:
2415e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    case BO_PtrMemI: {
2416e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      LValue Obj;
2417e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!HandleMemberPointerAccess(Info, E, Obj))
2418e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
24191aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith      APValue Result;
2420f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      if (!HandleLValueToRValueConversion(Info, E, E->getType(), Obj, Result))
2421e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
2422e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return DerivedSuccess(Result, E);
2423e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
2424e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
2425e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
2426e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
24278cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) {
2428e92b1f4917bfb669a09d220dc979fc3676df4da8Richard Smith    // Evaluate and cache the common expression. We treat it as a temporary,
2429e92b1f4917bfb669a09d220dc979fc3676df4da8Richard Smith    // even though it's not quite the same thing.
2430e92b1f4917bfb669a09d220dc979fc3676df4da8Richard Smith    if (!Evaluate(Info.CurrentCall->Temporaries[E->getOpaqueValue()],
2431e92b1f4917bfb669a09d220dc979fc3676df4da8Richard Smith                  Info, E->getCommon()))
2432f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return false;
24338cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
243474e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    return HandleConditionalOperator(E);
24358cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  }
24368cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
24378cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitConditionalOperator(const ConditionalOperator *E) {
2438f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    bool IsBcpCall = false;
2439f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    // If the condition (ignoring parens) is a __builtin_constant_p call,
2440f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    // the result is a constant expression if it can be folded without
2441f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    // side-effects. This is an important GNU extension. See GCC PR38377
2442f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    // for discussion.
2443f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    if (const CallExpr *CallCE =
2444f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          dyn_cast<CallExpr>(E->getCond()->IgnoreParenCasts()))
2445f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p)
2446f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        IsBcpCall = true;
2447f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
2448f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    // Always assume __builtin_constant_p(...) ? ... : ... is a potential
2449f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    // constant expression; we can't check whether it's potentially foldable.
2450f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    if (Info.CheckingPotentialConstantExpression && IsBcpCall)
2451f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      return false;
2452f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
2453f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    FoldConstant Fold(Info);
2454f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
245574e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    if (!HandleConditionalOperator(E))
2456f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      return false;
2457f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
2458f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    if (IsBcpCall)
2459f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      Fold.Fold(Info);
2460f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
2461f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    return true;
24628cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  }
24638cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
24648cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitOpaqueValueExpr(const OpaqueValueExpr *E) {
2465e92b1f4917bfb669a09d220dc979fc3676df4da8Richard Smith    APValue &Value = Info.CurrentCall->Temporaries[E];
2466e92b1f4917bfb669a09d220dc979fc3676df4da8Richard Smith    if (Value.isUninit()) {
246742786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis      const Expr *Source = E->getSourceExpr();
246842786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis      if (!Source)
2469f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(E);
247042786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis      if (Source == E) { // sanity checking.
247142786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis        assert(0 && "OpaqueValueExpr recursively refers to itself");
2472f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(E);
247342786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis      }
247442786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis      return StmtVisitorTy::Visit(Source);
247542786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis    }
2476e92b1f4917bfb669a09d220dc979fc3676df4da8Richard Smith    return DerivedSuccess(Value, E);
24778cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  }
2478f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith
2479d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  RetTy VisitCallExpr(const CallExpr *E) {
2480e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    const Expr *Callee = E->getCallee()->IgnoreParens();
2481d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    QualType CalleeType = Callee->getType();
2482d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
24836142ca7790aa09a6e13592b70f142cc4bbcadcaeDevang Patel    const FunctionDecl *FD = 0;
248459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    LValue *This = 0, ThisVal;
248559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs());
248686c3ae46250cdcc57778c27826060779a92f3815Richard Smith    bool HasQualifier = false;
248759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
248859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    // Extract function decl and 'this' pointer from the callee.
248959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    if (CalleeType->isSpecificBuiltinType(BuiltinType::BoundMember)) {
2490f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      const ValueDecl *Member = 0;
2491e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (const MemberExpr *ME = dyn_cast<MemberExpr>(Callee)) {
2492e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        // Explicit bound member calls, such as x.f() or p->g();
2493e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        if (!EvaluateObjectArgument(Info, ME->getBase(), ThisVal))
2494f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith          return false;
2495f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        Member = ME->getMemberDecl();
2496e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        This = &ThisVal;
249786c3ae46250cdcc57778c27826060779a92f3815Richard Smith        HasQualifier = ME->hasQualifier();
2498e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(Callee)) {
2499e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        // Indirect bound member calls ('.*' or '->*').
2500f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        Member = HandleMemberPointerAccess(Info, BE, ThisVal, false);
2501f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        if (!Member) return false;
2502e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        This = &ThisVal;
2503e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      } else
2504f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(Callee);
2505f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
2506f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      FD = dyn_cast<FunctionDecl>(Member);
2507f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      if (!FD)
2508f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(Callee);
250959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    } else if (CalleeType->isFunctionPointerType()) {
2510b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      LValue Call;
2511b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      if (!EvaluatePointer(Callee, Call, Info))
2512f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return false;
251359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
2514b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      if (!Call.getLValueOffset().isZero())
2515f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(Callee);
25161bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith      FD = dyn_cast_or_null<FunctionDecl>(
25171bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith                             Call.getLValueBase().dyn_cast<const ValueDecl*>());
251859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      if (!FD)
2519f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(Callee);
252059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
252159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      // Overloaded operator calls to member functions are represented as normal
252259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      // calls with '*this' as the first argument.
252359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
252459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      if (MD && !MD->isStatic()) {
2525f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        // FIXME: When selecting an implicit conversion for an overloaded
2526f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        // operator delete, we sometimes try to evaluate calls to conversion
2527f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        // operators without a 'this' parameter!
2528f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        if (Args.empty())
2529f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith          return Error(E);
2530f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
253159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith        if (!EvaluateObjectArgument(Info, Args[0], ThisVal))
253259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith          return false;
253359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith        This = &ThisVal;
253459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith        Args = Args.slice(1);
253559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      }
2536d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
253759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      // Don't call function pointers which have been cast to some other type.
253859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      if (!Info.Ctx.hasSameType(CalleeType->getPointeeType(), FD->getType()))
2539f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(E);
254059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    } else
2541f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E);
2542d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
2543b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith    if (This && !This->checkSubobject(Info, E, CSK_This))
2544b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith      return false;
2545b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith
254686c3ae46250cdcc57778c27826060779a92f3815Richard Smith    // DR1358 allows virtual constexpr functions in some cases. Don't allow
254786c3ae46250cdcc57778c27826060779a92f3815Richard Smith    // calls to such functions in constant expressions.
254886c3ae46250cdcc57778c27826060779a92f3815Richard Smith    if (This && !HasQualifier &&
254986c3ae46250cdcc57778c27826060779a92f3815Richard Smith        isa<CXXMethodDecl>(FD) && cast<CXXMethodDecl>(FD)->isVirtual())
255086c3ae46250cdcc57778c27826060779a92f3815Richard Smith      return Error(E, diag::note_constexpr_virtual_call);
255186c3ae46250cdcc57778c27826060779a92f3815Richard Smith
2552c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    const FunctionDecl *Definition = 0;
2553d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    Stmt *Body = FD->getBody(Definition);
25541aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    APValue Result;
2555d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
2556c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition) ||
2557745f5147e065900267c85a5568785a1991d4838fRichard Smith        !HandleFunctionCall(E->getExprLoc(), Definition, This, Args, Body,
2558745f5147e065900267c85a5568785a1991d4838fRichard Smith                            Info, Result))
2559f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return false;
2560d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
256183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return DerivedSuccess(Result, E);
2562d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  }
2563d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
2564c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  RetTy VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
2565c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return StmtVisitorTy::Visit(E->getInitializer());
2566c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  }
2567f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  RetTy VisitInitListExpr(const InitListExpr *E) {
256871523d6c41e1599fc42f420d02dd2895fd8f65d4Eli Friedman    if (E->getNumInits() == 0)
256971523d6c41e1599fc42f420d02dd2895fd8f65d4Eli Friedman      return DerivedZeroInitialization(E);
257071523d6c41e1599fc42f420d02dd2895fd8f65d4Eli Friedman    if (E->getNumInits() == 1)
257171523d6c41e1599fc42f420d02dd2895fd8f65d4Eli Friedman      return StmtVisitorTy::Visit(E->getInit(0));
2572f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
2573f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  }
2574f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  RetTy VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
257551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return DerivedZeroInitialization(E);
2576f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  }
2577f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  RetTy VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) {
257851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return DerivedZeroInitialization(E);
2579f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  }
2580e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  RetTy VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) {
258151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return DerivedZeroInitialization(E);
2582e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
2583f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith
2584180f47959a066795cc0f409433023af448bb0328Richard Smith  /// A member expression where the object is a prvalue is itself a prvalue.
2585180f47959a066795cc0f409433023af448bb0328Richard Smith  RetTy VisitMemberExpr(const MemberExpr *E) {
2586180f47959a066795cc0f409433023af448bb0328Richard Smith    assert(!E->isArrow() && "missing call to bound member function?");
2587180f47959a066795cc0f409433023af448bb0328Richard Smith
25881aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    APValue Val;
2589180f47959a066795cc0f409433023af448bb0328Richard Smith    if (!Evaluate(Val, Info, E->getBase()))
2590180f47959a066795cc0f409433023af448bb0328Richard Smith      return false;
2591180f47959a066795cc0f409433023af448bb0328Richard Smith
2592180f47959a066795cc0f409433023af448bb0328Richard Smith    QualType BaseTy = E->getBase()->getType();
2593180f47959a066795cc0f409433023af448bb0328Richard Smith
2594180f47959a066795cc0f409433023af448bb0328Richard Smith    const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
2595f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!FD) return Error(E);
2596180f47959a066795cc0f409433023af448bb0328Richard Smith    assert(!FD->getType()->isReferenceType() && "prvalue reference?");
2597890f0f1612b0028b8b16730ae7ed07e295600c76Ted Kremenek    assert(BaseTy->castAs<RecordType>()->getDecl()->getCanonicalDecl() ==
2598180f47959a066795cc0f409433023af448bb0328Richard Smith           FD->getParent()->getCanonicalDecl() && "record / field mismatch");
2599180f47959a066795cc0f409433023af448bb0328Richard Smith
2600b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    SubobjectDesignator Designator(BaseTy);
2601b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    Designator.addDeclUnchecked(FD);
2602180f47959a066795cc0f409433023af448bb0328Richard Smith
2603f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return ExtractSubobject(Info, E, Val, BaseTy, Designator, E->getType()) &&
2604180f47959a066795cc0f409433023af448bb0328Richard Smith           DerivedSuccess(Val, E);
2605180f47959a066795cc0f409433023af448bb0328Richard Smith  }
2606180f47959a066795cc0f409433023af448bb0328Richard Smith
2607c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  RetTy VisitCastExpr(const CastExpr *E) {
2608c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    switch (E->getCastKind()) {
2609c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    default:
2610c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith      break;
2611c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
26127a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall    case CK_AtomicToNonAtomic:
26137a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall    case CK_NonAtomicToAtomic:
2614c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    case CK_NoOp:
26157d580a4e9e47dffc3c17aa2b957ac57ca3c4e451Richard Smith    case CK_UserDefinedConversion:
2616c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith      return StmtVisitorTy::Visit(E->getSubExpr());
2617c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
2618c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    case CK_LValueToRValue: {
2619c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith      LValue LVal;
2620f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      if (!EvaluateLValue(E->getSubExpr(), LVal, Info))
2621f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return false;
26221aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith      APValue RVal;
26239ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith      // Note, we use the subexpression's type in order to retain cv-qualifiers.
26249ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith      if (!HandleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(),
26259ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith                                          LVal, RVal))
2626f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return false;
2627f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return DerivedSuccess(RVal, E);
2628c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    }
2629c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    }
2630c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
2631f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
2632c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  }
2633c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
26348327fad71da34492d82c532f42a58cb4baff81a3Richard Smith  /// Visit a value which is evaluated, but whose value is ignored.
26358327fad71da34492d82c532f42a58cb4baff81a3Richard Smith  void VisitIgnoredValue(const Expr *E) {
26361aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    APValue Scratch;
26378327fad71da34492d82c532f42a58cb4baff81a3Richard Smith    if (!Evaluate(Scratch, Info, E))
26388327fad71da34492d82c532f42a58cb4baff81a3Richard Smith      Info.EvalStatus.HasSideEffects = true;
26398327fad71da34492d82c532f42a58cb4baff81a3Richard Smith  }
26408cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne};
26418cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
26428cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne}
26438cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
26448cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne//===----------------------------------------------------------------------===//
2645e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// Common base class for lvalue and temporary evaluation.
2646e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===//
2647e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithnamespace {
2648e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithtemplate<class Derived>
2649e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithclass LValueExprEvaluatorBase
2650e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  : public ExprEvaluatorBase<Derived, bool> {
2651e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithprotected:
2652e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  LValue &Result;
2653e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  typedef LValueExprEvaluatorBase LValueExprEvaluatorBaseTy;
2654e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  typedef ExprEvaluatorBase<Derived, bool> ExprEvaluatorBaseTy;
2655e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2656e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool Success(APValue::LValueBase B) {
2657e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    Result.set(B);
2658e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
2659e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
2660e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2661e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithpublic:
2662e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  LValueExprEvaluatorBase(EvalInfo &Info, LValue &Result) :
2663e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    ExprEvaluatorBaseTy(Info), Result(Result) {}
2664e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
26651aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  bool Success(const APValue &V, const Expr *E) {
26661aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    Result.setFrom(this->Info.Ctx, V);
2667e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
2668e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
2669e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2670e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitMemberExpr(const MemberExpr *E) {
2671e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // Handle non-static data members.
2672e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    QualType BaseTy;
2673e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (E->isArrow()) {
2674e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!EvaluatePointer(E->getBase(), Result, this->Info))
2675e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
2676890f0f1612b0028b8b16730ae7ed07e295600c76Ted Kremenek      BaseTy = E->getBase()->getType()->castAs<PointerType>()->getPointeeType();
2677c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    } else if (E->getBase()->isRValue()) {
2678af2c7a194592401394233b7cbcdd3cfd0a7a38ddRichard Smith      assert(E->getBase()->getType()->isRecordType());
2679c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      if (!EvaluateTemporary(E->getBase(), Result, this->Info))
2680c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith        return false;
2681c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      BaseTy = E->getBase()->getType();
2682e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    } else {
2683e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!this->Visit(E->getBase()))
2684e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
2685e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      BaseTy = E->getBase()->getType();
2686e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
2687e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2688d9b02e726262e4009dda830998bb934172ac0020Richard Smith    const ValueDecl *MD = E->getMemberDecl();
2689d9b02e726262e4009dda830998bb934172ac0020Richard Smith    if (const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
2690d9b02e726262e4009dda830998bb934172ac0020Richard Smith      assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() ==
2691d9b02e726262e4009dda830998bb934172ac0020Richard Smith             FD->getParent()->getCanonicalDecl() && "record / field mismatch");
2692d9b02e726262e4009dda830998bb934172ac0020Richard Smith      (void)BaseTy;
26938d59deec807ed53efcd07855199cdc9c979f447fJohn McCall      if (!HandleLValueMember(this->Info, E, Result, FD))
26948d59deec807ed53efcd07855199cdc9c979f447fJohn McCall        return false;
2695d9b02e726262e4009dda830998bb934172ac0020Richard Smith    } else if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(MD)) {
26968d59deec807ed53efcd07855199cdc9c979f447fJohn McCall      if (!HandleLValueIndirectMember(this->Info, E, Result, IFD))
26978d59deec807ed53efcd07855199cdc9c979f447fJohn McCall        return false;
2698d9b02e726262e4009dda830998bb934172ac0020Richard Smith    } else
2699d9b02e726262e4009dda830998bb934172ac0020Richard Smith      return this->Error(E);
2700e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2701d9b02e726262e4009dda830998bb934172ac0020Richard Smith    if (MD->getType()->isReferenceType()) {
27021aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith      APValue RefValue;
2703d9b02e726262e4009dda830998bb934172ac0020Richard Smith      if (!HandleLValueToRValueConversion(this->Info, E, MD->getType(), Result,
2704e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                                          RefValue))
2705e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
2706e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return Success(RefValue, E);
2707e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
2708e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
2709e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
2710e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2711e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitBinaryOperator(const BinaryOperator *E) {
2712e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    switch (E->getOpcode()) {
2713e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    default:
2714e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
2715e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2716e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    case BO_PtrMemD:
2717e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    case BO_PtrMemI:
2718e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return HandleMemberPointerAccess(this->Info, E, Result);
2719e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
2720e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
2721e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2722e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitCastExpr(const CastExpr *E) {
2723e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    switch (E->getCastKind()) {
2724e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    default:
2725e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return ExprEvaluatorBaseTy::VisitCastExpr(E);
2726e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2727e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    case CK_DerivedToBase:
2728e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    case CK_UncheckedDerivedToBase: {
2729e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!this->Visit(E->getSubExpr()))
2730e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
2731e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2732e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      // Now figure out the necessary offset to add to the base LV to get from
2733e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      // the derived class to the base class.
2734e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      QualType Type = E->getSubExpr()->getType();
2735e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2736e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      for (CastExpr::path_const_iterator PathI = E->path_begin(),
2737e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith           PathE = E->path_end(); PathI != PathE; ++PathI) {
2738b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        if (!HandleLValueBase(this->Info, E, Result, Type->getAsCXXRecordDecl(),
2739e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                              *PathI))
2740e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith          return false;
2741e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        Type = (*PathI)->getType();
2742e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      }
2743e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2744e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return true;
2745e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
2746e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
2747e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
2748e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith};
2749e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}
2750e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2751e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===//
27524efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman// LValue Evaluation
2753c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//
2754c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// This is used for evaluating lvalues (in C and C++), xvalues (in C++11),
2755c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// function designators (in C), decl references to void objects (in C), and
2756c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// temporaries (if building with -Wno-address-of-temporary).
2757c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//
2758c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// LValue evaluation produces values comprising a base expression of one of the
2759c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// following types:
27601bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// - Declarations
27611bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith//  * VarDecl
27621bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith//  * FunctionDecl
27631bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// - Literals
2764c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//  * CompoundLiteralExpr in C
2765c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//  * StringLiteral
276647d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith//  * CXXTypeidExpr
2767c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//  * PredefinedExpr
2768180f47959a066795cc0f409433023af448bb0328Richard Smith//  * ObjCStringLiteralExpr
2769c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//  * ObjCEncodeExpr
2770c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//  * AddrLabelExpr
2771c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//  * BlockExpr
2772c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//  * CallExpr for a MakeStringConstant builtin
27731bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// - Locals and temporaries
277483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith//  * Any Expr, with a CallIndex indicating the function in which the temporary
277583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith//    was evaluated.
27761bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// plus an offset in bytes.
27774efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman//===----------------------------------------------------------------------===//
27784efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedmannamespace {
2779770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass LValueExprEvaluator
2780e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  : public LValueExprEvaluatorBase<LValueExprEvaluator> {
27814efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedmanpublic:
2782e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  LValueExprEvaluator(EvalInfo &Info, LValue &Result) :
2783e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    LValueExprEvaluatorBaseTy(Info, Result) {}
27841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2785c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  bool VisitVarDecl(const Expr *E, const VarDecl *VD);
2786c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
27878cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitDeclRefExpr(const DeclRefExpr *E);
27888cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(E); }
2789bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith  bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E);
27908cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
27918cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitMemberExpr(const MemberExpr *E);
27928cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitStringLiteral(const StringLiteral *E) { return Success(E); }
27938cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(E); }
279447d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith  bool VisitCXXTypeidExpr(const CXXTypeidExpr *E);
2795e275a1845b9e32bd3034f2593dee1780855c8fd6Francois Pichet  bool VisitCXXUuidofExpr(const CXXUuidofExpr *E);
27968cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E);
27978cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitUnaryDeref(const UnaryOperator *E);
279886024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  bool VisitUnaryReal(const UnaryOperator *E);
279986024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  bool VisitUnaryImag(const UnaryOperator *E);
28008cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
28018cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCastExpr(const CastExpr *E) {
280226bc220377705292a0519a71d3ea3aef68fcfec6Anders Carlsson    switch (E->getCastKind()) {
280326bc220377705292a0519a71d3ea3aef68fcfec6Anders Carlsson    default:
2804e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return LValueExprEvaluatorBaseTy::VisitCastExpr(E);
280526bc220377705292a0519a71d3ea3aef68fcfec6Anders Carlsson
2806db924224b51b153f24fbe492102d4edebcbbb7f4Eli Friedman    case CK_LValueBitCast:
2807c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith      this->CCEDiag(E, diag::note_constexpr_invalid_cast) << 2;
28080a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      if (!Visit(E->getSubExpr()))
28090a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith        return false;
28100a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      Result.Designator.setInvalid();
28110a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      return true;
2812db924224b51b153f24fbe492102d4edebcbbb7f4Eli Friedman
2813e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    case CK_BaseToDerived:
2814180f47959a066795cc0f409433023af448bb0328Richard Smith      if (!Visit(E->getSubExpr()))
2815180f47959a066795cc0f409433023af448bb0328Richard Smith        return false;
2816e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return HandleBaseToDerivedCast(Info, E, Result);
281726bc220377705292a0519a71d3ea3aef68fcfec6Anders Carlsson    }
281826bc220377705292a0519a71d3ea3aef68fcfec6Anders Carlsson  }
28194efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman};
28204efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman} // end anonymous namespace
28214efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
2822c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// Evaluate an expression as an lvalue. This can be legitimately called on
2823c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// expressions which are not glvalues, in a few cases:
2824c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith///  * function designators in C,
2825c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith///  * "extern void" objects,
2826c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith///  * temporaries, if building with -Wno-address-of-temporary.
2827efdb83e26f9a1fd2566afe54461216cd84814d42John McCallstatic bool EvaluateLValue(const Expr* E, LValue& Result, EvalInfo &Info) {
2828c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  assert((E->isGLValue() || E->getType()->isFunctionType() ||
2829c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith          E->getType()->isVoidType() || isa<CXXTemporaryObjectExpr>(E)) &&
2830c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith         "can't evaluate expression as an lvalue");
28318cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  return LValueExprEvaluator(Info, Result).Visit(E);
28324efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman}
28334efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
28348cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
28351bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl()))
28361bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    return Success(FD);
28371bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  if (const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl()))
2838c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return VisitVarDecl(E, VD);
2839c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  return Error(E);
2840c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith}
2841436c8898cd1c93c5bacd3fcc4ac586bc5cd77062Richard Smith
2842c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smithbool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) {
2843177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith  if (!VD->getType()->isReferenceType()) {
2844177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith    if (isa<ParmVarDecl>(VD)) {
284583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      Result.set(VD, Info.CurrentCall->Index);
2846177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith      return true;
2847177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith    }
28481bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    return Success(VD);
2849177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith  }
285050c39ea4858265f3f5f42a0c624557ce2281936bEli Friedman
28511aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  APValue V;
2852f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (!EvaluateVarDeclInit(Info, E, VD, Info.CurrentCall, V))
2853f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return false;
2854f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  return Success(V, E);
285535873c49adad211ff466e34342a52665742794f5Anders Carlsson}
285635873c49adad211ff466e34342a52665742794f5Anders Carlsson
2857bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smithbool LValueExprEvaluator::VisitMaterializeTemporaryExpr(
2858bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    const MaterializeTemporaryExpr *E) {
2859e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (E->GetTemporaryExpr()->isRValue()) {
2860af2c7a194592401394233b7cbcdd3cfd0a7a38ddRichard Smith    if (E->getType()->isRecordType())
2861e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return EvaluateTemporary(E->GetTemporaryExpr(), Result, Info);
2862e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
286383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    Result.set(E, Info.CurrentCall->Index);
286483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return EvaluateInPlace(Info.CurrentCall->Temporaries[E], Info,
286583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                           Result, E->GetTemporaryExpr());
2866e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
2867e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2868e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // Materialization of an lvalue temporary occurs when we need to force a copy
2869e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // (for instance, if it's a bitfield).
2870e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // FIXME: The AST should contain an lvalue-to-rvalue node for such cases.
2871e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (!Visit(E->GetTemporaryExpr()))
2872e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return false;
2873f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (!HandleLValueToRValueConversion(Info, E, E->getType(), Result,
2874e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                                      Info.CurrentCall->Temporaries[E]))
2875e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return false;
287683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  Result.set(E, Info.CurrentCall->Index);
2877e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  return true;
2878bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith}
2879bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith
28808cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool
28818cad3046be06ea73ff8892d947697a21d7a440d3Peter CollingbourneLValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
2882c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?");
2883c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  // Defer visiting the literal until the lvalue-to-rvalue conversion. We can
2884c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  // only see this when folding in C, so there's no standard to follow here.
2885efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  return Success(E);
28864efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman}
28874efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
288847d2145675099893d702be4bc06bd9f26d8ddd13Richard Smithbool LValueExprEvaluator::VisitCXXTypeidExpr(const CXXTypeidExpr *E) {
288947d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith  if (E->isTypeOperand())
289047d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith    return Success(E);
289147d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith  CXXRecordDecl *RD = E->getExprOperand()->getType()->getAsCXXRecordDecl();
28920d729105ecb50a7e3cbe6e57c29149edfa5cf05aRichard Smith  // FIXME: The standard says "a typeid expression whose operand is of a
28930d729105ecb50a7e3cbe6e57c29149edfa5cf05aRichard Smith  // polymorphic class type" is not a constant expression, but it probably
28940d729105ecb50a7e3cbe6e57c29149edfa5cf05aRichard Smith  // means "a typeid expression whose operand is potentially evaluated".
289547d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith  if (RD && RD->isPolymorphic()) {
28965cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.Diag(E, diag::note_constexpr_typeid_polymorphic)
289747d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith      << E->getExprOperand()->getType()
289847d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith      << E->getExprOperand()->getSourceRange();
289947d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith    return false;
290047d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith  }
290147d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith  return Success(E);
290247d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith}
290347d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith
2904e275a1845b9e32bd3034f2593dee1780855c8fd6Francois Pichetbool LValueExprEvaluator::VisitCXXUuidofExpr(const CXXUuidofExpr *E) {
2905e275a1845b9e32bd3034f2593dee1780855c8fd6Francois Pichet  return Success(E);
2906e275a1845b9e32bd3034f2593dee1780855c8fd6Francois Pichet}
2907e275a1845b9e32bd3034f2593dee1780855c8fd6Francois Pichet
29088cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) {
2909c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  // Handle static data members.
2910c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  if (const VarDecl *VD = dyn_cast<VarDecl>(E->getMemberDecl())) {
2911c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    VisitIgnoredValue(E->getBase());
2912c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return VisitVarDecl(E, VD);
2913c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  }
2914c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
2915d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  // Handle static member functions.
2916d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) {
2917d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    if (MD->isStatic()) {
2918d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith      VisitIgnoredValue(E->getBase());
29191bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith      return Success(MD);
2920d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    }
2921d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  }
2922d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
2923180f47959a066795cc0f409433023af448bb0328Richard Smith  // Handle non-static data members.
2924e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  return LValueExprEvaluatorBaseTy::VisitMemberExpr(E);
29254efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman}
29264efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
29278cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) {
2928c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  // FIXME: Deal with vectors as array subscript bases.
2929c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  if (E->getBase()->getType()->isVectorType())
2930f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
2931c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
29323068d117951a8df54bae9db039b56201ab10962bAnders Carlsson  if (!EvaluatePointer(E->getBase(), Result, Info))
2933efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    return false;
29341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
29353068d117951a8df54bae9db039b56201ab10962bAnders Carlsson  APSInt Index;
29363068d117951a8df54bae9db039b56201ab10962bAnders Carlsson  if (!EvaluateInteger(E->getIdx(), Index, Info))
2937efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    return false;
2938180f47959a066795cc0f409433023af448bb0328Richard Smith  int64_t IndexValue
2939180f47959a066795cc0f409433023af448bb0328Richard Smith    = Index.isSigned() ? Index.getSExtValue()
2940180f47959a066795cc0f409433023af448bb0328Richard Smith                       : static_cast<int64_t>(Index.getZExtValue());
29413068d117951a8df54bae9db039b56201ab10962bAnders Carlsson
2942b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  return HandleLValueArrayAdjustment(Info, E, Result, E->getType(), IndexValue);
29433068d117951a8df54bae9db039b56201ab10962bAnders Carlsson}
29444efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
29458cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) {
2946efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  return EvaluatePointer(E->getSubExpr(), Result, Info);
2947e8761c8fe2ee6b628104a0885f49fd3c21c08a4fEli Friedman}
2948e8761c8fe2ee6b628104a0885f49fd3c21c08a4fEli Friedman
294986024013d4c3728122c58fa07a2a67e6c15837efRichard Smithbool LValueExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
295086024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  if (!Visit(E->getSubExpr()))
295186024013d4c3728122c58fa07a2a67e6c15837efRichard Smith    return false;
295286024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  // __real is a no-op on scalar lvalues.
295386024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  if (E->getSubExpr()->getType()->isAnyComplexType())
295486024013d4c3728122c58fa07a2a67e6c15837efRichard Smith    HandleLValueComplexElement(Info, E, Result, E->getType(), false);
295586024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  return true;
295686024013d4c3728122c58fa07a2a67e6c15837efRichard Smith}
295786024013d4c3728122c58fa07a2a67e6c15837efRichard Smith
295886024013d4c3728122c58fa07a2a67e6c15837efRichard Smithbool LValueExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
295986024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  assert(E->getSubExpr()->getType()->isAnyComplexType() &&
296086024013d4c3728122c58fa07a2a67e6c15837efRichard Smith         "lvalue __imag__ on scalar?");
296186024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  if (!Visit(E->getSubExpr()))
296286024013d4c3728122c58fa07a2a67e6c15837efRichard Smith    return false;
296386024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  HandleLValueComplexElement(Info, E, Result, E->getType(), true);
296486024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  return true;
296586024013d4c3728122c58fa07a2a67e6c15837efRichard Smith}
296686024013d4c3728122c58fa07a2a67e6c15837efRichard Smith
29674efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman//===----------------------------------------------------------------------===//
2968f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner// Pointer Evaluation
2969f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===//
2970f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
2971c754aa62643e66ab967ca32ae8b0b3fc419bba25Anders Carlssonnamespace {
2972770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass PointerExprEvaluator
29738cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  : public ExprEvaluatorBase<PointerExprEvaluator, bool> {
2974efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  LValue &Result;
2975efdb83e26f9a1fd2566afe54461216cd84814d42John McCall
29768cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool Success(const Expr *E) {
29771bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    Result.set(E);
2978efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    return true;
2979efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  }
29802bad1687fe6f00e10767a691a33b070b151902b6Anders Carlssonpublic:
29811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2982efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  PointerExprEvaluator(EvalInfo &info, LValue &Result)
29838cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    : ExprEvaluatorBaseTy(info), Result(Result) {}
2984f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
29851aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  bool Success(const APValue &V, const Expr *E) {
29861aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    Result.setFrom(Info.Ctx, V);
29878cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    return true;
29882bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson  }
298951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  bool ZeroInitialization(const Expr *E) {
2990f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith    return Success((Expr*)0);
2991f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  }
29922bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson
2993efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  bool VisitBinaryOperator(const BinaryOperator *E);
29948cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCastExpr(const CastExpr* E);
2995efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  bool VisitUnaryAddrOf(const UnaryOperator *E);
29968cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitObjCStringLiteral(const ObjCStringLiteral *E)
2997efdb83e26f9a1fd2566afe54461216cd84814d42John McCall      { return Success(E); }
2998eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  bool VisitObjCBoxedExpr(const ObjCBoxedExpr *E)
2999ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      { return Success(E); }
30008cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitAddrLabelExpr(const AddrLabelExpr *E)
3001efdb83e26f9a1fd2566afe54461216cd84814d42John McCall      { return Success(E); }
30028cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCallExpr(const CallExpr *E);
30038cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitBlockExpr(const BlockExpr *E) {
3004469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall    if (!E->getBlockDecl()->hasCaptures())
3005efdb83e26f9a1fd2566afe54461216cd84814d42John McCall      return Success(E);
3006f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
3007b83d287bc7f47d36fb0751a481e2ef9308b37252Mike Stump  }
3008180f47959a066795cc0f409433023af448bb0328Richard Smith  bool VisitCXXThisExpr(const CXXThisExpr *E) {
3009180f47959a066795cc0f409433023af448bb0328Richard Smith    if (!Info.CurrentCall->This)
3010f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E);
3011180f47959a066795cc0f409433023af448bb0328Richard Smith    Result = *Info.CurrentCall->This;
3012180f47959a066795cc0f409433023af448bb0328Richard Smith    return true;
3013180f47959a066795cc0f409433023af448bb0328Richard Smith  }
301456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
3015ba98d6bb414861965a1f22628494ea046785ecd4Eli Friedman  // FIXME: Missing: @protocol, @selector
30162bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson};
3017f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner} // end anonymous namespace
30182bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson
3019efdb83e26f9a1fd2566afe54461216cd84814d42John McCallstatic bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info) {
3020c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  assert(E->isRValue() && E->getType()->hasPointerRepresentation());
30218cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  return PointerExprEvaluator(Info, Result).Visit(E);
3022f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner}
3023650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson
3024efdb83e26f9a1fd2566afe54461216cd84814d42John McCallbool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
30252de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  if (E->getOpcode() != BO_Add &&
30262de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      E->getOpcode() != BO_Sub)
3027e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
30281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3029650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson  const Expr *PExp = E->getLHS();
3030650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson  const Expr *IExp = E->getRHS();
3031650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson  if (IExp->getType()->isPointerType())
3032f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner    std::swap(PExp, IExp);
30331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3034745f5147e065900267c85a5568785a1991d4838fRichard Smith  bool EvalPtrOK = EvaluatePointer(PExp, Result, Info);
3035745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!EvalPtrOK && !Info.keepEvaluatingAfterFailure())
3036efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    return false;
30371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3038efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  llvm::APSInt Offset;
3039745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!EvaluateInteger(IExp, Offset, Info) || !EvalPtrOK)
3040efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    return false;
3041efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  int64_t AdditionalOffset
3042efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    = Offset.isSigned() ? Offset.getSExtValue()
3043efdb83e26f9a1fd2566afe54461216cd84814d42John McCall                        : static_cast<int64_t>(Offset.getZExtValue());
30440a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith  if (E->getOpcode() == BO_Sub)
30450a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    AdditionalOffset = -AdditionalOffset;
3046650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson
3047890f0f1612b0028b8b16730ae7ed07e295600c76Ted Kremenek  QualType Pointee = PExp->getType()->castAs<PointerType>()->getPointeeType();
3048b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  return HandleLValueArrayAdjustment(Info, E, Result, Pointee,
3049b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                                     AdditionalOffset);
3050650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson}
30514efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
3052efdb83e26f9a1fd2566afe54461216cd84814d42John McCallbool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
3053efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  return EvaluateLValue(E->getSubExpr(), Result, Info);
30544efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman}
30551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30568cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
30578cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  const Expr* SubExpr = E->getSubExpr();
3058650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson
305909a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman  switch (E->getCastKind()) {
306009a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman  default:
306109a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman    break;
306209a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman
30632de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_BitCast:
30641d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_CPointerToObjCPointerCast:
30651d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_BlockPointerToObjCPointerCast:
30662de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_AnyPointerToBlockPointerCast:
306728c1ce789322ab99f9b5887015d63ec5f088957aRichard Smith    if (!Visit(SubExpr))
306828c1ce789322ab99f9b5887015d63ec5f088957aRichard Smith      return false;
3069c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith    // Bitcasts to cv void* are static_casts, not reinterpret_casts, so are
3070c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith    // permitted in constant expressions in C++11. Bitcasts from cv void* are
3071c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith    // also static_casts, but we disallow them as a resolution to DR1312.
30724cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith    if (!E->getType()->isVoidPointerType()) {
307328c1ce789322ab99f9b5887015d63ec5f088957aRichard Smith      Result.Designator.setInvalid();
30744cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith      if (SubExpr->getType()->isVoidPointerType())
30754cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith        CCEDiag(E, diag::note_constexpr_invalid_cast)
30764cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith          << 3 << SubExpr->getType();
30774cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith      else
30784cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith        CCEDiag(E, diag::note_constexpr_invalid_cast) << 2;
30794cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith    }
30800a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    return true;
308109a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman
30825c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson  case CK_DerivedToBase:
30835c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson  case CK_UncheckedDerivedToBase: {
308447a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    if (!EvaluatePointer(E->getSubExpr(), Result, Info))
30855c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson      return false;
3086e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (!Result.Base && Result.Offset.isZero())
3087e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return true;
30885c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson
3089180f47959a066795cc0f409433023af448bb0328Richard Smith    // Now figure out the necessary offset to add to the base LV to get from
30905c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson    // the derived class to the base class.
3091180f47959a066795cc0f409433023af448bb0328Richard Smith    QualType Type =
3092180f47959a066795cc0f409433023af448bb0328Richard Smith        E->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType();
30935c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson
3094180f47959a066795cc0f409433023af448bb0328Richard Smith    for (CastExpr::path_const_iterator PathI = E->path_begin(),
30955c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson         PathE = E->path_end(); PathI != PathE; ++PathI) {
3096b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      if (!HandleLValueBase(Info, E, Result, Type->getAsCXXRecordDecl(),
3097b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                            *PathI))
30985c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson        return false;
3099180f47959a066795cc0f409433023af448bb0328Richard Smith      Type = (*PathI)->getType();
31005c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson    }
31015c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson
31025c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson    return true;
31035c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson  }
31045c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson
3105e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  case CK_BaseToDerived:
3106e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (!Visit(E->getSubExpr()))
3107e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return false;
3108e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (!Result.Base && Result.Offset.isZero())
3109e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return true;
3110e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return HandleBaseToDerivedCast(Info, E, Result);
3111e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
311247a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith  case CK_NullToPointer:
311349149fe0d2be06ce1ceed1e9d2548a0b75a59c47Richard Smith    VisitIgnoredValue(E->getSubExpr());
311451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return ZeroInitialization(E);
3115404cd1669c3ba138a9ae0a619bd689cce5aae271John McCall
31162de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_IntegralToPointer: {
3117c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith    CCEDiag(E, diag::note_constexpr_invalid_cast) << 2;
3118c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith
31191aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    APValue Value;
3120efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    if (!EvaluateIntegerOrLValue(SubExpr, Value, Info))
312109a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman      break;
312269ab26a8623141f35e86817cfc6e0fbe7639a40fDaniel Dunbar
3123efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    if (Value.isInt()) {
312447a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith      unsigned Size = Info.Ctx.getTypeSize(E->getType());
312547a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith      uint64_t N = Value.getInt().extOrTrunc(Size).getZExtValue();
31261bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith      Result.Base = (Expr*)0;
312747a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith      Result.Offset = CharUnits::fromQuantity(N);
312883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      Result.CallIndex = 0;
31290a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      Result.Designator.setInvalid();
3130efdb83e26f9a1fd2566afe54461216cd84814d42John McCall      return true;
3131efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    } else {
3132efdb83e26f9a1fd2566afe54461216cd84814d42John McCall      // Cast is of an lvalue, no need to change value.
31331aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith      Result.setFrom(Info.Ctx, Value);
3134efdb83e26f9a1fd2566afe54461216cd84814d42John McCall      return true;
3135650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson    }
3136650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson  }
31372de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_ArrayToPointerDecay:
3138e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (SubExpr->isGLValue()) {
3139e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!EvaluateLValue(SubExpr, Result, Info))
3140e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
3141e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    } else {
314283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      Result.set(SubExpr, Info.CurrentCall->Index);
314383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      if (!EvaluateInPlace(Info.CurrentCall->Temporaries[SubExpr],
314483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                           Info, Result, SubExpr))
3145e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
3146e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
31470a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    // The result is a pointer to the first element of the array.
3148b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    if (const ConstantArrayType *CAT
3149b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith          = Info.Ctx.getAsConstantArrayType(SubExpr->getType()))
3150b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      Result.addArray(Info, E, CAT);
3151b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    else
3152b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      Result.Designator.setInvalid();
31530a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    return true;
31546a7c94af983717e2c2d6aebe42cb4737c1c7b9e6Richard Smith
31552de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_FunctionToPointerDecay:
31566a7c94af983717e2c2d6aebe42cb4737c1c7b9e6Richard Smith    return EvaluateLValue(SubExpr, Result, Info);
31574efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman  }
31584efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
3159c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  return ExprEvaluatorBaseTy::VisitCastExpr(E);
31601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
3161650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson
31628cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) {
3163180f47959a066795cc0f409433023af448bb0328Richard Smith  if (IsStringLiteralCall(E))
3164efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    return Success(E);
316556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
31668cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  return ExprEvaluatorBaseTy::VisitCallExpr(E);
31674efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman}
3168f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
3169f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===//
3170e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// Member Pointer Evaluation
3171e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===//
3172e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3173e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithnamespace {
3174e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithclass MemberPointerExprEvaluator
3175e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  : public ExprEvaluatorBase<MemberPointerExprEvaluator, bool> {
3176e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  MemberPtr &Result;
3177e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3178e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool Success(const ValueDecl *D) {
3179e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    Result = MemberPtr(D);
3180e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
3181e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
3182e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithpublic:
3183e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3184e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  MemberPointerExprEvaluator(EvalInfo &Info, MemberPtr &Result)
3185e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    : ExprEvaluatorBaseTy(Info), Result(Result) {}
3186e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
31871aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  bool Success(const APValue &V, const Expr *E) {
3188e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    Result.setFrom(V);
3189e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
3190e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
319151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  bool ZeroInitialization(const Expr *E) {
3192e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return Success((const ValueDecl*)0);
3193e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
3194e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3195e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitCastExpr(const CastExpr *E);
3196e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitUnaryAddrOf(const UnaryOperator *E);
3197e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith};
3198e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} // end anonymous namespace
3199e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3200e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result,
3201e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                                  EvalInfo &Info) {
3202e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  assert(E->isRValue() && E->getType()->isMemberPointerType());
3203e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  return MemberPointerExprEvaluator(Info, Result).Visit(E);
3204e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}
3205e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3206e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithbool MemberPointerExprEvaluator::VisitCastExpr(const CastExpr *E) {
3207e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  switch (E->getCastKind()) {
3208e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  default:
3209e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return ExprEvaluatorBaseTy::VisitCastExpr(E);
3210e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3211e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  case CK_NullToMemberPointer:
321249149fe0d2be06ce1ceed1e9d2548a0b75a59c47Richard Smith    VisitIgnoredValue(E->getSubExpr());
321351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return ZeroInitialization(E);
3214e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3215e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  case CK_BaseToDerivedMemberPointer: {
3216e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (!Visit(E->getSubExpr()))
3217e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return false;
3218e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (E->path_empty())
3219e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return true;
3220e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // Base-to-derived member pointer casts store the path in derived-to-base
3221e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // order, so iterate backwards. The CXXBaseSpecifier also provides us with
3222e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // the wrong end of the derived->base arc, so stagger the path by one class.
3223e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter;
3224e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin());
3225e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith         PathI != PathE; ++PathI) {
3226e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      assert(!(*PathI)->isVirtual() && "memptr cast through vbase");
3227e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      const CXXRecordDecl *Derived = (*PathI)->getType()->getAsCXXRecordDecl();
3228e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!Result.castToDerived(Derived))
3229f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(E);
3230e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
3231e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    const Type *FinalTy = E->getType()->castAs<MemberPointerType>()->getClass();
3232e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (!Result.castToDerived(FinalTy->getAsCXXRecordDecl()))
3233f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E);
3234e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
3235e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
3236e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3237e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  case CK_DerivedToBaseMemberPointer:
3238e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (!Visit(E->getSubExpr()))
3239e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return false;
3240e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    for (CastExpr::path_const_iterator PathI = E->path_begin(),
3241e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith         PathE = E->path_end(); PathI != PathE; ++PathI) {
3242e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      assert(!(*PathI)->isVirtual() && "memptr cast through vbase");
3243e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl();
3244e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!Result.castToBase(Base))
3245f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(E);
3246e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
3247e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
3248e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
3249e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}
3250e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3251e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithbool MemberPointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
3252e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // C++11 [expr.unary.op]p3 has very strict rules on how the address of a
3253e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // member can be formed.
3254e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  return Success(cast<DeclRefExpr>(E->getSubExpr())->getDecl());
3255e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}
3256e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3257e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===//
3258180f47959a066795cc0f409433023af448bb0328Richard Smith// Record Evaluation
3259180f47959a066795cc0f409433023af448bb0328Richard Smith//===----------------------------------------------------------------------===//
3260180f47959a066795cc0f409433023af448bb0328Richard Smith
3261180f47959a066795cc0f409433023af448bb0328Richard Smithnamespace {
3262180f47959a066795cc0f409433023af448bb0328Richard Smith  class RecordExprEvaluator
3263180f47959a066795cc0f409433023af448bb0328Richard Smith  : public ExprEvaluatorBase<RecordExprEvaluator, bool> {
3264180f47959a066795cc0f409433023af448bb0328Richard Smith    const LValue &This;
3265180f47959a066795cc0f409433023af448bb0328Richard Smith    APValue &Result;
3266180f47959a066795cc0f409433023af448bb0328Richard Smith  public:
3267180f47959a066795cc0f409433023af448bb0328Richard Smith
3268180f47959a066795cc0f409433023af448bb0328Richard Smith    RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result)
3269180f47959a066795cc0f409433023af448bb0328Richard Smith      : ExprEvaluatorBaseTy(info), This(This), Result(Result) {}
3270180f47959a066795cc0f409433023af448bb0328Richard Smith
32711aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    bool Success(const APValue &V, const Expr *E) {
327283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      Result = V;
327383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      return true;
3274180f47959a066795cc0f409433023af448bb0328Richard Smith    }
327551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    bool ZeroInitialization(const Expr *E);
3276180f47959a066795cc0f409433023af448bb0328Richard Smith
327759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    bool VisitCastExpr(const CastExpr *E);
3278180f47959a066795cc0f409433023af448bb0328Richard Smith    bool VisitInitListExpr(const InitListExpr *E);
3279180f47959a066795cc0f409433023af448bb0328Richard Smith    bool VisitCXXConstructExpr(const CXXConstructExpr *E);
3280180f47959a066795cc0f409433023af448bb0328Richard Smith  };
3281180f47959a066795cc0f409433023af448bb0328Richard Smith}
3282180f47959a066795cc0f409433023af448bb0328Richard Smith
328351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// Perform zero-initialization on an object of non-union class type.
328451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// C++11 [dcl.init]p5:
328551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith///  To zero-initialize an object or reference of type T means:
328651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith///    [...]
328751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith///    -- if T is a (possibly cv-qualified) non-union class type,
328851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith///       each non-static data member and each base-class subobject is
328951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith///       zero-initialized
3290b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E,
3291b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                                          const RecordDecl *RD,
329251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith                                          const LValue &This, APValue &Result) {
329351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  assert(!RD->isUnion() && "Expected non-union class type");
329451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD);
329551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  Result = APValue(APValue::UninitStruct(), CD ? CD->getNumBases() : 0,
329651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith                   std::distance(RD->field_begin(), RD->field_end()));
329751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
32988d59deec807ed53efcd07855199cdc9c979f447fJohn McCall  if (RD->isInvalidDecl()) return false;
329951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD);
330051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
330151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (CD) {
330251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    unsigned Index = 0;
330351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(),
3304b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith           End = CD->bases_end(); I != End; ++I, ++Index) {
330551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      const CXXRecordDecl *Base = I->getType()->getAsCXXRecordDecl();
330651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      LValue Subobject = This;
33078d59deec807ed53efcd07855199cdc9c979f447fJohn McCall      if (!HandleLValueDirectBase(Info, E, Subobject, CD, Base, &Layout))
33088d59deec807ed53efcd07855199cdc9c979f447fJohn McCall        return false;
3309b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      if (!HandleClassZeroInitialization(Info, E, Base, Subobject,
331051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith                                         Result.getStructBase(Index)))
331151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith        return false;
331251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    }
331351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  }
331451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
3315b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  for (RecordDecl::field_iterator I = RD->field_begin(), End = RD->field_end();
3316b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith       I != End; ++I) {
331751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    // -- if T is a reference type, no initialization is performed.
3318262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie    if (I->getType()->isReferenceType())
331951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      continue;
332051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
332151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    LValue Subobject = This;
3322581deb3da481053c4993c7600f97acf7768caac5David Blaikie    if (!HandleLValueMember(Info, E, Subobject, *I, &Layout))
33238d59deec807ed53efcd07855199cdc9c979f447fJohn McCall      return false;
332451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
3325262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie    ImplicitValueInitExpr VIE(I->getType());
332683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    if (!EvaluateInPlace(
3327262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie          Result.getStructField(I->getFieldIndex()), Info, Subobject, &VIE))
332851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      return false;
332951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  }
333051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
333151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  return true;
333251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith}
333351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
333451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smithbool RecordExprEvaluator::ZeroInitialization(const Expr *E) {
333551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl();
33361de9d7de172379d6af75fd11dda2a713e4f36f62John McCall  if (RD->isInvalidDecl()) return false;
333751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (RD->isUnion()) {
333851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the
333951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    // object's first non-static named data member is zero-initialized
334051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    RecordDecl::field_iterator I = RD->field_begin();
334151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    if (I == RD->field_end()) {
334251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      Result = APValue((const FieldDecl*)0);
334351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      return true;
334451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    }
334551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
334651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    LValue Subobject = This;
3347581deb3da481053c4993c7600f97acf7768caac5David Blaikie    if (!HandleLValueMember(Info, E, Subobject, *I))
33488d59deec807ed53efcd07855199cdc9c979f447fJohn McCall      return false;
3349581deb3da481053c4993c7600f97acf7768caac5David Blaikie    Result = APValue(*I);
3350262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie    ImplicitValueInitExpr VIE(I->getType());
335183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, &VIE);
335251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  }
335351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
3354ce582fe2a7aad8b14b3636ad9cac0a3b8bbb219bRichard Smith  if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->getNumVBases()) {
33555cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.Diag(E, diag::note_constexpr_virtual_base) << RD;
3356ce582fe2a7aad8b14b3636ad9cac0a3b8bbb219bRichard Smith    return false;
3357ce582fe2a7aad8b14b3636ad9cac0a3b8bbb219bRichard Smith  }
3358ce582fe2a7aad8b14b3636ad9cac0a3b8bbb219bRichard Smith
3359b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  return HandleClassZeroInitialization(Info, E, RD, This, Result);
336051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith}
336151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
336259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithbool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) {
336359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  switch (E->getCastKind()) {
336459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  default:
336559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    return ExprEvaluatorBaseTy::VisitCastExpr(E);
336659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
336759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  case CK_ConstructorConversion:
336859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    return Visit(E->getSubExpr());
336959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
337059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  case CK_DerivedToBase:
337159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  case CK_UncheckedDerivedToBase: {
33721aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    APValue DerivedObject;
3373f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!Evaluate(DerivedObject, Info, E->getSubExpr()))
337459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      return false;
3375f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!DerivedObject.isStruct())
3376f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E->getSubExpr());
337759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
337859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    // Derived-to-base rvalue conversion: just slice off the derived part.
337959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    APValue *Value = &DerivedObject;
338059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl();
338159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    for (CastExpr::path_const_iterator PathI = E->path_begin(),
338259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith         PathE = E->path_end(); PathI != PathE; ++PathI) {
338359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      assert(!(*PathI)->isVirtual() && "record rvalue with virtual base");
338459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl();
338559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      Value = &Value->getStructBase(getBaseIndex(RD, Base));
338659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      RD = Base;
338759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    }
338859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    Result = *Value;
338959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    return true;
339059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  }
339159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  }
339259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
339359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
3394180f47959a066795cc0f409433023af448bb0328Richard Smithbool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
339524fe798fffc1748d8bce1321af42981c3719cb85Sebastian Redl  // Cannot constant-evaluate std::initializer_list inits.
339624fe798fffc1748d8bce1321af42981c3719cb85Sebastian Redl  if (E->initializesStdInitializerList())
339724fe798fffc1748d8bce1321af42981c3719cb85Sebastian Redl    return false;
339824fe798fffc1748d8bce1321af42981c3719cb85Sebastian Redl
3399180f47959a066795cc0f409433023af448bb0328Richard Smith  const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl();
34001de9d7de172379d6af75fd11dda2a713e4f36f62John McCall  if (RD->isInvalidDecl()) return false;
3401180f47959a066795cc0f409433023af448bb0328Richard Smith  const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD);
3402180f47959a066795cc0f409433023af448bb0328Richard Smith
3403180f47959a066795cc0f409433023af448bb0328Richard Smith  if (RD->isUnion()) {
3404ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    const FieldDecl *Field = E->getInitializedFieldInUnion();
3405ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    Result = APValue(Field);
3406ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    if (!Field)
3407180f47959a066795cc0f409433023af448bb0328Richard Smith      return true;
3408ec789163a42a7be654ac34aadb750b508954d53cRichard Smith
3409ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    // If the initializer list for a union does not contain any elements, the
3410ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    // first element of the union is value-initialized.
3411ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    ImplicitValueInitExpr VIE(Field->getType());
3412ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    const Expr *InitExpr = E->getNumInits() ? E->getInit(0) : &VIE;
3413ec789163a42a7be654ac34aadb750b508954d53cRichard Smith
3414180f47959a066795cc0f409433023af448bb0328Richard Smith    LValue Subobject = This;
34158d59deec807ed53efcd07855199cdc9c979f447fJohn McCall    if (!HandleLValueMember(Info, InitExpr, Subobject, Field, &Layout))
34168d59deec807ed53efcd07855199cdc9c979f447fJohn McCall      return false;
341783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, InitExpr);
3418180f47959a066795cc0f409433023af448bb0328Richard Smith  }
3419180f47959a066795cc0f409433023af448bb0328Richard Smith
3420180f47959a066795cc0f409433023af448bb0328Richard Smith  assert((!isa<CXXRecordDecl>(RD) || !cast<CXXRecordDecl>(RD)->getNumBases()) &&
3421180f47959a066795cc0f409433023af448bb0328Richard Smith         "initializer list for class with base classes");
3422180f47959a066795cc0f409433023af448bb0328Richard Smith  Result = APValue(APValue::UninitStruct(), 0,
3423180f47959a066795cc0f409433023af448bb0328Richard Smith                   std::distance(RD->field_begin(), RD->field_end()));
3424180f47959a066795cc0f409433023af448bb0328Richard Smith  unsigned ElementNo = 0;
3425745f5147e065900267c85a5568785a1991d4838fRichard Smith  bool Success = true;
3426180f47959a066795cc0f409433023af448bb0328Richard Smith  for (RecordDecl::field_iterator Field = RD->field_begin(),
3427180f47959a066795cc0f409433023af448bb0328Richard Smith       FieldEnd = RD->field_end(); Field != FieldEnd; ++Field) {
3428180f47959a066795cc0f409433023af448bb0328Richard Smith    // Anonymous bit-fields are not considered members of the class for
3429180f47959a066795cc0f409433023af448bb0328Richard Smith    // purposes of aggregate initialization.
3430180f47959a066795cc0f409433023af448bb0328Richard Smith    if (Field->isUnnamedBitfield())
3431180f47959a066795cc0f409433023af448bb0328Richard Smith      continue;
3432180f47959a066795cc0f409433023af448bb0328Richard Smith
3433180f47959a066795cc0f409433023af448bb0328Richard Smith    LValue Subobject = This;
3434180f47959a066795cc0f409433023af448bb0328Richard Smith
3435745f5147e065900267c85a5568785a1991d4838fRichard Smith    bool HaveInit = ElementNo < E->getNumInits();
3436745f5147e065900267c85a5568785a1991d4838fRichard Smith
3437745f5147e065900267c85a5568785a1991d4838fRichard Smith    // FIXME: Diagnostics here should point to the end of the initializer
3438745f5147e065900267c85a5568785a1991d4838fRichard Smith    // list, not the start.
34398d59deec807ed53efcd07855199cdc9c979f447fJohn McCall    if (!HandleLValueMember(Info, HaveInit ? E->getInit(ElementNo) : E,
3440581deb3da481053c4993c7600f97acf7768caac5David Blaikie                            Subobject, *Field, &Layout))
34418d59deec807ed53efcd07855199cdc9c979f447fJohn McCall      return false;
3442745f5147e065900267c85a5568785a1991d4838fRichard Smith
3443745f5147e065900267c85a5568785a1991d4838fRichard Smith    // Perform an implicit value-initialization for members beyond the end of
3444745f5147e065900267c85a5568785a1991d4838fRichard Smith    // the initializer list.
3445745f5147e065900267c85a5568785a1991d4838fRichard Smith    ImplicitValueInitExpr VIE(HaveInit ? Info.Ctx.IntTy : Field->getType());
3446745f5147e065900267c85a5568785a1991d4838fRichard Smith
344783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    if (!EvaluateInPlace(
3448262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie          Result.getStructField(Field->getFieldIndex()),
3449745f5147e065900267c85a5568785a1991d4838fRichard Smith          Info, Subobject, HaveInit ? E->getInit(ElementNo++) : &VIE)) {
3450745f5147e065900267c85a5568785a1991d4838fRichard Smith      if (!Info.keepEvaluatingAfterFailure())
3451180f47959a066795cc0f409433023af448bb0328Richard Smith        return false;
3452745f5147e065900267c85a5568785a1991d4838fRichard Smith      Success = false;
3453180f47959a066795cc0f409433023af448bb0328Richard Smith    }
3454180f47959a066795cc0f409433023af448bb0328Richard Smith  }
3455180f47959a066795cc0f409433023af448bb0328Richard Smith
3456745f5147e065900267c85a5568785a1991d4838fRichard Smith  return Success;
3457180f47959a066795cc0f409433023af448bb0328Richard Smith}
3458180f47959a066795cc0f409433023af448bb0328Richard Smith
3459180f47959a066795cc0f409433023af448bb0328Richard Smithbool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) {
3460180f47959a066795cc0f409433023af448bb0328Richard Smith  const CXXConstructorDecl *FD = E->getConstructor();
34611de9d7de172379d6af75fd11dda2a713e4f36f62John McCall  if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl()) return false;
34621de9d7de172379d6af75fd11dda2a713e4f36f62John McCall
346351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  bool ZeroInit = E->requiresZeroInitialization();
346451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) {
3465ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    // If we've already performed zero-initialization, we're already done.
3466ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    if (!Result.isUninit())
3467ec789163a42a7be654ac34aadb750b508954d53cRichard Smith      return true;
3468ec789163a42a7be654ac34aadb750b508954d53cRichard Smith
346951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    if (ZeroInit)
347051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      return ZeroInitialization(E);
347151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
34726180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    const CXXRecordDecl *RD = FD->getParent();
34736180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    if (RD->isUnion())
34746180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith      Result = APValue((FieldDecl*)0);
34756180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    else
34766180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith      Result = APValue(APValue::UninitStruct(), RD->getNumBases(),
34776180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith                       std::distance(RD->field_begin(), RD->field_end()));
34786180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    return true;
34796180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith  }
34806180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith
3481180f47959a066795cc0f409433023af448bb0328Richard Smith  const FunctionDecl *Definition = 0;
3482180f47959a066795cc0f409433023af448bb0328Richard Smith  FD->getBody(Definition);
3483180f47959a066795cc0f409433023af448bb0328Richard Smith
3484c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition))
3485c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return false;
3486180f47959a066795cc0f409433023af448bb0328Richard Smith
3487610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith  // Avoid materializing a temporary for an elidable copy/move constructor.
348851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (E->isElidable() && !ZeroInit)
3489180f47959a066795cc0f409433023af448bb0328Richard Smith    if (const MaterializeTemporaryExpr *ME
3490180f47959a066795cc0f409433023af448bb0328Richard Smith          = dyn_cast<MaterializeTemporaryExpr>(E->getArg(0)))
3491180f47959a066795cc0f409433023af448bb0328Richard Smith      return Visit(ME->GetTemporaryExpr());
3492180f47959a066795cc0f409433023af448bb0328Richard Smith
349351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (ZeroInit && !ZeroInitialization(E))
349451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return false;
349551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
3496180f47959a066795cc0f409433023af448bb0328Richard Smith  llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs());
3497745f5147e065900267c85a5568785a1991d4838fRichard Smith  return HandleConstructorCall(E->getExprLoc(), This, Args,
3498f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                               cast<CXXConstructorDecl>(Definition), Info,
3499f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                               Result);
3500180f47959a066795cc0f409433023af448bb0328Richard Smith}
3501180f47959a066795cc0f409433023af448bb0328Richard Smith
3502180f47959a066795cc0f409433023af448bb0328Richard Smithstatic bool EvaluateRecord(const Expr *E, const LValue &This,
3503180f47959a066795cc0f409433023af448bb0328Richard Smith                           APValue &Result, EvalInfo &Info) {
3504180f47959a066795cc0f409433023af448bb0328Richard Smith  assert(E->isRValue() && E->getType()->isRecordType() &&
3505180f47959a066795cc0f409433023af448bb0328Richard Smith         "can't evaluate expression as a record rvalue");
3506180f47959a066795cc0f409433023af448bb0328Richard Smith  return RecordExprEvaluator(Info, This, Result).Visit(E);
3507180f47959a066795cc0f409433023af448bb0328Richard Smith}
3508180f47959a066795cc0f409433023af448bb0328Richard Smith
3509180f47959a066795cc0f409433023af448bb0328Richard Smith//===----------------------------------------------------------------------===//
3510e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// Temporary Evaluation
3511e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//
3512e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// Temporaries are represented in the AST as rvalues, but generally behave like
3513e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// lvalues. The full-object of which the temporary is a subobject is implicitly
3514e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// materialized so that a reference can bind to it.
3515e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===//
3516e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithnamespace {
3517e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithclass TemporaryExprEvaluator
3518e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  : public LValueExprEvaluatorBase<TemporaryExprEvaluator> {
3519e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithpublic:
3520e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  TemporaryExprEvaluator(EvalInfo &Info, LValue &Result) :
3521e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    LValueExprEvaluatorBaseTy(Info, Result) {}
3522e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3523e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  /// Visit an expression which constructs the value of this temporary.
3524e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitConstructExpr(const Expr *E) {
352583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    Result.set(E, Info.CurrentCall->Index);
352683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return EvaluateInPlace(Info.CurrentCall->Temporaries[E], Info, Result, E);
3527e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
3528e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3529e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitCastExpr(const CastExpr *E) {
3530e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    switch (E->getCastKind()) {
3531e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    default:
3532e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return LValueExprEvaluatorBaseTy::VisitCastExpr(E);
3533e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3534e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    case CK_ConstructorConversion:
3535e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return VisitConstructExpr(E->getSubExpr());
3536e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
3537e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
3538e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitInitListExpr(const InitListExpr *E) {
3539e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return VisitConstructExpr(E);
3540e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
3541e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitCXXConstructExpr(const CXXConstructExpr *E) {
3542e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return VisitConstructExpr(E);
3543e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
3544e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitCallExpr(const CallExpr *E) {
3545e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return VisitConstructExpr(E);
3546e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
3547e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith};
3548e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} // end anonymous namespace
3549e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3550e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// Evaluate an expression of record type as a temporary.
3551e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) {
3552af2c7a194592401394233b7cbcdd3cfd0a7a38ddRichard Smith  assert(E->isRValue() && E->getType()->isRecordType());
3553e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  return TemporaryExprEvaluator(Info, Result).Visit(E);
3554e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}
3555e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3556e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===//
355759b5da6d853b4368b984700315adf7b37de05764Nate Begeman// Vector Evaluation
355859b5da6d853b4368b984700315adf7b37de05764Nate Begeman//===----------------------------------------------------------------------===//
355959b5da6d853b4368b984700315adf7b37de05764Nate Begeman
356059b5da6d853b4368b984700315adf7b37de05764Nate Begemannamespace {
3561770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramer  class VectorExprEvaluator
356207fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith  : public ExprEvaluatorBase<VectorExprEvaluator, bool> {
356307fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    APValue &Result;
356459b5da6d853b4368b984700315adf7b37de05764Nate Begeman  public:
35651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
356607fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    VectorExprEvaluator(EvalInfo &info, APValue &Result)
356707fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith      : ExprEvaluatorBaseTy(info), Result(Result) {}
35681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
356907fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    bool Success(const ArrayRef<APValue> &V, const Expr *E) {
357007fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith      assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements());
357107fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith      // FIXME: remove this APValue copy.
357207fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith      Result = APValue(V.data(), V.size());
357307fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith      return true;
357407fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    }
35751aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    bool Success(const APValue &V, const Expr *E) {
357669c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith      assert(V.isVector());
357707fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith      Result = V;
357807fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith      return true;
357907fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    }
358051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    bool ZeroInitialization(const Expr *E);
35811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
358207fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    bool VisitUnaryReal(const UnaryOperator *E)
358391110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman      { return Visit(E->getSubExpr()); }
358407fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    bool VisitCastExpr(const CastExpr* E);
358507fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    bool VisitInitListExpr(const InitListExpr *E);
358607fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    bool VisitUnaryImag(const UnaryOperator *E);
358791110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman    // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
35882217c87bdc5ab357046a5453bdb06f469c41024eEli Friedman    //                 binary comparisons, binary and/or/xor,
358991110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman    //                 shufflevector, ExtVectorElementExpr
359059b5da6d853b4368b984700315adf7b37de05764Nate Begeman  };
359159b5da6d853b4368b984700315adf7b37de05764Nate Begeman} // end anonymous namespace
359259b5da6d853b4368b984700315adf7b37de05764Nate Begeman
359359b5da6d853b4368b984700315adf7b37de05764Nate Begemanstatic bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
3594c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  assert(E->isRValue() && E->getType()->isVectorType() &&"not a vector rvalue");
359507fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith  return VectorExprEvaluator(Info, Result).Visit(E);
359659b5da6d853b4368b984700315adf7b37de05764Nate Begeman}
359759b5da6d853b4368b984700315adf7b37de05764Nate Begeman
359807fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smithbool VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
359907fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith  const VectorType *VTy = E->getType()->castAs<VectorType>();
3600c0b8b19bd8056d6b5d831623a0825cce150f4507Nate Begeman  unsigned NElts = VTy->getNumElements();
36011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3602d62ca370b03b8c6ad58002d3399383baf744e32bRichard Smith  const Expr *SE = E->getSubExpr();
3603e8c9e9218f215ec6089f12b076c7b9d310fd5194Nate Begeman  QualType SETy = SE->getType();
360459b5da6d853b4368b984700315adf7b37de05764Nate Begeman
360546a523285928aa07bf14803178dc04616ac85994Eli Friedman  switch (E->getCastKind()) {
360646a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_VectorSplat: {
360707fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    APValue Val = APValue();
360846a523285928aa07bf14803178dc04616ac85994Eli Friedman    if (SETy->isIntegerType()) {
360946a523285928aa07bf14803178dc04616ac85994Eli Friedman      APSInt IntResult;
361046a523285928aa07bf14803178dc04616ac85994Eli Friedman      if (!EvaluateInteger(SE, IntResult, Info))
3611f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith         return false;
361207fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith      Val = APValue(IntResult);
361346a523285928aa07bf14803178dc04616ac85994Eli Friedman    } else if (SETy->isRealFloatingType()) {
361446a523285928aa07bf14803178dc04616ac85994Eli Friedman       APFloat F(0.0);
361546a523285928aa07bf14803178dc04616ac85994Eli Friedman       if (!EvaluateFloat(SE, F, Info))
3616f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith         return false;
361707fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith       Val = APValue(F);
361846a523285928aa07bf14803178dc04616ac85994Eli Friedman    } else {
361907fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith      return Error(E);
362046a523285928aa07bf14803178dc04616ac85994Eli Friedman    }
3621c0b8b19bd8056d6b5d831623a0825cce150f4507Nate Begeman
3622c0b8b19bd8056d6b5d831623a0825cce150f4507Nate Begeman    // Splat and create vector APValue.
362307fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    SmallVector<APValue, 4> Elts(NElts, Val);
362407fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    return Success(Elts, E);
3625e8c9e9218f215ec6089f12b076c7b9d310fd5194Nate Begeman  }
3626e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  case CK_BitCast: {
3627e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    // Evaluate the operand into an APInt we can extract from.
3628e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    llvm::APInt SValInt;
3629e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    if (!EvalAndBitcastToAPInt(Info, SE, SValInt))
3630e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      return false;
3631e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    // Extract the elements
3632e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    QualType EltTy = VTy->getElementType();
3633e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    unsigned EltSize = Info.Ctx.getTypeSize(EltTy);
3634e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian();
3635e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    SmallVector<APValue, 4> Elts;
3636e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    if (EltTy->isRealFloatingType()) {
3637e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(EltTy);
3638e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      bool isIEESem = &Sem != &APFloat::PPCDoubleDouble;
3639e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      unsigned FloatEltSize = EltSize;
3640e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      if (&Sem == &APFloat::x87DoubleExtended)
3641e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        FloatEltSize = 80;
3642e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      for (unsigned i = 0; i < NElts; i++) {
3643e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        llvm::APInt Elt;
3644e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        if (BigEndian)
3645e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman          Elt = SValInt.rotl(i*EltSize+FloatEltSize).trunc(FloatEltSize);
3646e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        else
3647e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman          Elt = SValInt.rotr(i*EltSize).trunc(FloatEltSize);
3648e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        Elts.push_back(APValue(APFloat(Elt, isIEESem)));
3649e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      }
3650e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    } else if (EltTy->isIntegerType()) {
3651e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      for (unsigned i = 0; i < NElts; i++) {
3652e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        llvm::APInt Elt;
3653e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        if (BigEndian)
3654e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman          Elt = SValInt.rotl(i*EltSize+EltSize).zextOrTrunc(EltSize);
3655e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        else
3656e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman          Elt = SValInt.rotr(i*EltSize).zextOrTrunc(EltSize);
3657e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        Elts.push_back(APValue(APSInt(Elt, EltTy->isSignedIntegerType())));
3658e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      }
3659e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    } else {
3660e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      return Error(E);
3661e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    }
3662e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    return Success(Elts, E);
3663e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  }
366446a523285928aa07bf14803178dc04616ac85994Eli Friedman  default:
3665c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return ExprEvaluatorBaseTy::VisitCastExpr(E);
3666c0b8b19bd8056d6b5d831623a0825cce150f4507Nate Begeman  }
366759b5da6d853b4368b984700315adf7b37de05764Nate Begeman}
366859b5da6d853b4368b984700315adf7b37de05764Nate Begeman
366907fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smithbool
367059b5da6d853b4368b984700315adf7b37de05764Nate BegemanVectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
367107fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith  const VectorType *VT = E->getType()->castAs<VectorType>();
367259b5da6d853b4368b984700315adf7b37de05764Nate Begeman  unsigned NumInits = E->getNumInits();
367391110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman  unsigned NumElements = VT->getNumElements();
36741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
367559b5da6d853b4368b984700315adf7b37de05764Nate Begeman  QualType EltTy = VT->getElementType();
36765f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<APValue, 4> Elements;
367759b5da6d853b4368b984700315adf7b37de05764Nate Begeman
36783edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman  // The number of initializers can be less than the number of
36793edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman  // vector elements. For OpenCL, this can be due to nested vector
36803edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman  // initialization. For GCC compatibility, missing trailing elements
36813edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman  // should be initialized with zeroes.
36823edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman  unsigned CountInits = 0, CountElts = 0;
36833edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman  while (CountElts < NumElements) {
36843edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman    // Handle nested vector initialization.
36853edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman    if (CountInits < NumInits
36863edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman        && E->getInit(CountInits)->getType()->isExtVectorType()) {
36873edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      APValue v;
36883edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      if (!EvaluateVector(E->getInit(CountInits), v, Info))
36893edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman        return Error(E);
36903edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      unsigned vlen = v.getVectorLength();
36913edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      for (unsigned j = 0; j < vlen; j++)
36923edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman        Elements.push_back(v.getVectorElt(j));
36933edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      CountElts += vlen;
36943edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman    } else if (EltTy->isIntegerType()) {
369559b5da6d853b4368b984700315adf7b37de05764Nate Begeman      llvm::APSInt sInt(32);
36963edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      if (CountInits < NumInits) {
36973edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman        if (!EvaluateInteger(E->getInit(CountInits), sInt, Info))
36984b1f684416980ef6f1a7cb9e6af9c4fa4a164617Richard Smith          return false;
36993edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      } else // trailing integer zero.
37003edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman        sInt = Info.Ctx.MakeIntValue(0, EltTy);
37013edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      Elements.push_back(APValue(sInt));
37023edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      CountElts++;
370359b5da6d853b4368b984700315adf7b37de05764Nate Begeman    } else {
370459b5da6d853b4368b984700315adf7b37de05764Nate Begeman      llvm::APFloat f(0.0);
37053edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      if (CountInits < NumInits) {
37063edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman        if (!EvaluateFloat(E->getInit(CountInits), f, Info))
37074b1f684416980ef6f1a7cb9e6af9c4fa4a164617Richard Smith          return false;
37083edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      } else // trailing float zero.
37093edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman        f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
37103edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      Elements.push_back(APValue(f));
37113edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      CountElts++;
371259b5da6d853b4368b984700315adf7b37de05764Nate Begeman    }
37133edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman    CountInits++;
371459b5da6d853b4368b984700315adf7b37de05764Nate Begeman  }
371507fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith  return Success(Elements, E);
371659b5da6d853b4368b984700315adf7b37de05764Nate Begeman}
371759b5da6d853b4368b984700315adf7b37de05764Nate Begeman
371807fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smithbool
371951201882382fb40c9456a06c7f93d6ddd4a57712Richard SmithVectorExprEvaluator::ZeroInitialization(const Expr *E) {
372007fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith  const VectorType *VT = E->getType()->getAs<VectorType>();
372191110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman  QualType EltTy = VT->getElementType();
372291110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman  APValue ZeroElement;
372391110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman  if (EltTy->isIntegerType())
372491110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman    ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
372591110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman  else
372691110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman    ZeroElement =
372791110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman        APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
372891110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman
37295f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
373007fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith  return Success(Elements, E);
373191110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman}
373291110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman
373307fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smithbool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
37348327fad71da34492d82c532f42a58cb4baff81a3Richard Smith  VisitIgnoredValue(E->getSubExpr());
373551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  return ZeroInitialization(E);
373691110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman}
373791110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman
373859b5da6d853b4368b984700315adf7b37de05764Nate Begeman//===----------------------------------------------------------------------===//
3739cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith// Array Evaluation
3740cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith//===----------------------------------------------------------------------===//
3741cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
3742cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smithnamespace {
3743cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  class ArrayExprEvaluator
3744cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  : public ExprEvaluatorBase<ArrayExprEvaluator, bool> {
3745180f47959a066795cc0f409433023af448bb0328Richard Smith    const LValue &This;
3746cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    APValue &Result;
3747cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  public:
3748cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
3749180f47959a066795cc0f409433023af448bb0328Richard Smith    ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result)
3750180f47959a066795cc0f409433023af448bb0328Richard Smith      : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {}
3751cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
3752cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    bool Success(const APValue &V, const Expr *E) {
3753f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith      assert((V.isArray() || V.isLValue()) &&
3754f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith             "expected array or string literal");
3755cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith      Result = V;
3756cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith      return true;
3757cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    }
3758cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
375951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    bool ZeroInitialization(const Expr *E) {
3760180f47959a066795cc0f409433023af448bb0328Richard Smith      const ConstantArrayType *CAT =
3761180f47959a066795cc0f409433023af448bb0328Richard Smith          Info.Ctx.getAsConstantArrayType(E->getType());
3762180f47959a066795cc0f409433023af448bb0328Richard Smith      if (!CAT)
3763f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(E);
3764180f47959a066795cc0f409433023af448bb0328Richard Smith
3765180f47959a066795cc0f409433023af448bb0328Richard Smith      Result = APValue(APValue::UninitArray(), 0,
3766180f47959a066795cc0f409433023af448bb0328Richard Smith                       CAT->getSize().getZExtValue());
3767180f47959a066795cc0f409433023af448bb0328Richard Smith      if (!Result.hasArrayFiller()) return true;
3768180f47959a066795cc0f409433023af448bb0328Richard Smith
376951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      // Zero-initialize all elements.
3770180f47959a066795cc0f409433023af448bb0328Richard Smith      LValue Subobject = This;
3771b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      Subobject.addArray(Info, E, CAT);
3772180f47959a066795cc0f409433023af448bb0328Richard Smith      ImplicitValueInitExpr VIE(CAT->getElementType());
377383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, &VIE);
3774180f47959a066795cc0f409433023af448bb0328Richard Smith    }
3775180f47959a066795cc0f409433023af448bb0328Richard Smith
3776cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    bool VisitInitListExpr(const InitListExpr *E);
3777e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    bool VisitCXXConstructExpr(const CXXConstructExpr *E);
3778cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  };
3779cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith} // end anonymous namespace
3780cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
3781180f47959a066795cc0f409433023af448bb0328Richard Smithstatic bool EvaluateArray(const Expr *E, const LValue &This,
3782180f47959a066795cc0f409433023af448bb0328Richard Smith                          APValue &Result, EvalInfo &Info) {
378351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  assert(E->isRValue() && E->getType()->isArrayType() && "not an array rvalue");
3784180f47959a066795cc0f409433023af448bb0328Richard Smith  return ArrayExprEvaluator(Info, This, Result).Visit(E);
3785cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith}
3786cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
3787cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smithbool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
3788cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType());
3789cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  if (!CAT)
3790f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
3791cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
3792974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith  // C++11 [dcl.init.string]p1: A char array [...] can be initialized by [...]
3793974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith  // an appropriately-typed string literal enclosed in braces.
3794fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith  if (E->isStringLiteralInit()) {
3795974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith    LValue LV;
3796974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith    if (!EvaluateLValue(E->getInit(0), LV, Info))
3797974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith      return false;
37981aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    APValue Val;
3799f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith    LV.moveInto(Val);
3800f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith    return Success(Val, E);
3801974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith  }
3802974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith
3803745f5147e065900267c85a5568785a1991d4838fRichard Smith  bool Success = true;
3804745f5147e065900267c85a5568785a1991d4838fRichard Smith
3805de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith  assert((!Result.isArray() || Result.getArrayInitializedElts() == 0) &&
3806de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith         "zero-initialized array shouldn't have any initialized elts");
3807de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith  APValue Filler;
3808de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith  if (Result.isArray() && Result.hasArrayFiller())
3809de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith    Filler = Result.getArrayFiller();
3810de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith
3811cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  Result = APValue(APValue::UninitArray(), E->getNumInits(),
3812cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith                   CAT->getSize().getZExtValue());
3813de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith
3814de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith  // If the array was previously zero-initialized, preserve the
3815de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith  // zero-initialized values.
3816de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith  if (!Filler.isUninit()) {
3817de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith    for (unsigned I = 0, E = Result.getArrayInitializedElts(); I != E; ++I)
3818de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith      Result.getArrayInitializedElt(I) = Filler;
3819de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith    if (Result.hasArrayFiller())
3820de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith      Result.getArrayFiller() = Filler;
3821de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith  }
3822de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith
3823180f47959a066795cc0f409433023af448bb0328Richard Smith  LValue Subobject = This;
3824b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  Subobject.addArray(Info, E, CAT);
3825180f47959a066795cc0f409433023af448bb0328Richard Smith  unsigned Index = 0;
3826cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  for (InitListExpr::const_iterator I = E->begin(), End = E->end();
3827180f47959a066795cc0f409433023af448bb0328Richard Smith       I != End; ++I, ++Index) {
382883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    if (!EvaluateInPlace(Result.getArrayInitializedElt(Index),
382983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                         Info, Subobject, cast<Expr>(*I)) ||
3830745f5147e065900267c85a5568785a1991d4838fRichard Smith        !HandleLValueArrayAdjustment(Info, cast<Expr>(*I), Subobject,
3831745f5147e065900267c85a5568785a1991d4838fRichard Smith                                     CAT->getElementType(), 1)) {
3832745f5147e065900267c85a5568785a1991d4838fRichard Smith      if (!Info.keepEvaluatingAfterFailure())
3833745f5147e065900267c85a5568785a1991d4838fRichard Smith        return false;
3834745f5147e065900267c85a5568785a1991d4838fRichard Smith      Success = false;
3835745f5147e065900267c85a5568785a1991d4838fRichard Smith    }
3836180f47959a066795cc0f409433023af448bb0328Richard Smith  }
3837cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
3838745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!Result.hasArrayFiller()) return Success;
3839cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  assert(E->hasArrayFiller() && "no array filler for incomplete init list");
3840180f47959a066795cc0f409433023af448bb0328Richard Smith  // FIXME: The Subobject here isn't necessarily right. This rarely matters,
3841180f47959a066795cc0f409433023af448bb0328Richard Smith  // but sometimes does:
3842180f47959a066795cc0f409433023af448bb0328Richard Smith  //   struct S { constexpr S() : p(&p) {} void *p; };
3843180f47959a066795cc0f409433023af448bb0328Richard Smith  //   S s[10] = {};
384483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  return EvaluateInPlace(Result.getArrayFiller(), Info,
384583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                         Subobject, E->getArrayFiller()) && Success;
3846cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith}
3847cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
3848e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithbool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) {
3849de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith  // FIXME: The Subobject here isn't necessarily right. This rarely matters,
3850de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith  // but sometimes does:
3851de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith  //   struct S { constexpr S() : p(&p) {} void *p; };
3852de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith  //   S s[10];
3853de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith  LValue Subobject = This;
3854de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith
3855de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith  APValue *Value = &Result;
3856de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith  bool HadZeroInit = true;
3857a4334dffde250c22c339a974a7131914fe723180Richard Smith  QualType ElemTy = E->getType();
3858a4334dffde250c22c339a974a7131914fe723180Richard Smith  while (const ConstantArrayType *CAT =
3859a4334dffde250c22c339a974a7131914fe723180Richard Smith           Info.Ctx.getAsConstantArrayType(ElemTy)) {
3860de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith    Subobject.addArray(Info, E, CAT);
3861de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith    HadZeroInit &= !Value->isUninit();
3862de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith    if (!HadZeroInit)
3863de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith      *Value = APValue(APValue::UninitArray(), 0, CAT->getSize().getZExtValue());
3864de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith    if (!Value->hasArrayFiller())
3865de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith      return true;
3866de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith    Value = &Value->getArrayFiller();
3867a4334dffde250c22c339a974a7131914fe723180Richard Smith    ElemTy = CAT->getElementType();
3868de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith  }
3869e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3870a4334dffde250c22c339a974a7131914fe723180Richard Smith  if (!ElemTy->isRecordType())
3871a4334dffde250c22c339a974a7131914fe723180Richard Smith    return Error(E);
3872a4334dffde250c22c339a974a7131914fe723180Richard Smith
3873e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  const CXXConstructorDecl *FD = E->getConstructor();
38746180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith
387551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  bool ZeroInit = E->requiresZeroInitialization();
387651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) {
3877ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    if (HadZeroInit)
3878ec789163a42a7be654ac34aadb750b508954d53cRichard Smith      return true;
3879ec789163a42a7be654ac34aadb750b508954d53cRichard Smith
388051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    if (ZeroInit) {
3881a4334dffde250c22c339a974a7131914fe723180Richard Smith      ImplicitValueInitExpr VIE(ElemTy);
3882de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith      return EvaluateInPlace(*Value, Info, Subobject, &VIE);
388351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    }
388451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
38856180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    const CXXRecordDecl *RD = FD->getParent();
38866180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    if (RD->isUnion())
3887de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith      *Value = APValue((FieldDecl*)0);
38886180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    else
3889de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith      *Value =
38906180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith          APValue(APValue::UninitStruct(), RD->getNumBases(),
38916180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith                  std::distance(RD->field_begin(), RD->field_end()));
38926180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    return true;
38936180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith  }
38946180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith
3895e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  const FunctionDecl *Definition = 0;
3896e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  FD->getBody(Definition);
3897e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3898c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition))
3899c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return false;
3900e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3901ec789163a42a7be654ac34aadb750b508954d53cRichard Smith  if (ZeroInit && !HadZeroInit) {
3902a4334dffde250c22c339a974a7131914fe723180Richard Smith    ImplicitValueInitExpr VIE(ElemTy);
3903de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith    if (!EvaluateInPlace(*Value, Info, Subobject, &VIE))
390451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      return false;
390551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  }
390651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
3907e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs());
3908745f5147e065900267c85a5568785a1991d4838fRichard Smith  return HandleConstructorCall(E->getExprLoc(), Subobject, Args,
3909e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                               cast<CXXConstructorDecl>(Definition),
3910de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith                               Info, *Value);
3911e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}
3912e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3913cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith//===----------------------------------------------------------------------===//
3914f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner// Integer Evaluation
3915c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//
3916c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// As a GNU extension, we support casting pointers to sufficiently-wide integer
3917c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// types and back in constant folding. Integer values are thus represented
3918c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// either as an integer-valued APValue, or as an lvalue-valued APValue.
3919f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===//
3920f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
3921f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattnernamespace {
3922770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass IntExprEvaluator
39238cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  : public ExprEvaluatorBase<IntExprEvaluator, bool> {
39241aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  APValue &Result;
3925f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattnerpublic:
39261aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  IntExprEvaluator(EvalInfo &info, APValue &result)
39278cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    : ExprEvaluatorBaseTy(info), Result(result) {}
3928f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
3929cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  bool Success(const llvm::APSInt &SI, const Expr *E, APValue &Result) {
3930973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara    assert(E->getType()->isIntegralOrEnumerationType() &&
39312ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor           "Invalid evaluation result.");
3932973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara    assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() &&
39333f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar           "Invalid evaluation result.");
3934973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara    assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
39353f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar           "Invalid evaluation result.");
39361aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    Result = APValue(SI);
39373f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar    return true;
39383f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar  }
3939cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  bool Success(const llvm::APSInt &SI, const Expr *E) {
3940cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return Success(SI, E, Result);
3941cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
39423f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar
3943cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  bool Success(const llvm::APInt &I, const Expr *E, APValue &Result) {
39442ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor    assert(E->getType()->isIntegralOrEnumerationType() &&
39452ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor           "Invalid evaluation result.");
394630c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar    assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
39473f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar           "Invalid evaluation result.");
39481aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    Result = APValue(APSInt(I));
3949575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor    Result.getInt().setIsUnsigned(
3950575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor                            E->getType()->isUnsignedIntegerOrEnumerationType());
3951131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar    return true;
3952131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar  }
3953cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  bool Success(const llvm::APInt &I, const Expr *E) {
3954cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return Success(I, E, Result);
3955cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
3956131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar
3957cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  bool Success(uint64_t Value, const Expr *E, APValue &Result) {
39582ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor    assert(E->getType()->isIntegralOrEnumerationType() &&
39592ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor           "Invalid evaluation result.");
39601aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
3961131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar    return true;
3962131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar  }
3963cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  bool Success(uint64_t Value, const Expr *E) {
3964cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return Success(Value, E, Result);
3965cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
3966131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar
39674f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck  bool Success(CharUnits Size, const Expr *E) {
39684f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck    return Success(Size.getQuantity(), E);
39694f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck  }
39704f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck
39711aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  bool Success(const APValue &V, const Expr *E) {
39725930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman    if (V.isLValue() || V.isAddrLabelDiff()) {
3973342f1f8b0a402c5a7f8c5055db7f60a7808f1687Richard Smith      Result = V;
3974342f1f8b0a402c5a7f8c5055db7f60a7808f1687Richard Smith      return true;
3975342f1f8b0a402c5a7f8c5055db7f60a7808f1687Richard Smith    }
39768cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    return Success(V.getInt(), E);
397732fea9d18cc3658a1b01df5ca6f2ac302625c61dChris Lattner  }
39781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
397951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  bool ZeroInitialization(const Expr *E) { return Success(0, E); }
3980f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith
39818cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  //===--------------------------------------------------------------------===//
39828cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  //                            Visitor Methods
39838cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  //===--------------------------------------------------------------------===//
3984f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
39854c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner  bool VisitIntegerLiteral(const IntegerLiteral *E) {
3986131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar    return Success(E->getValue(), E);
39874c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner  }
39884c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner  bool VisitCharacterLiteral(const CharacterLiteral *E) {
3989131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar    return Success(E->getValue(), E);
39904c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner  }
3991043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman
3992043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman  bool CheckReferencedDecl(const Expr *E, const Decl *D);
3993043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman  bool VisitDeclRefExpr(const DeclRefExpr *E) {
39948cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    if (CheckReferencedDecl(E, E->getDecl()))
39958cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne      return true;
39968cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
39978cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    return ExprEvaluatorBaseTy::VisitDeclRefExpr(E);
3998043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman  }
3999043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman  bool VisitMemberExpr(const MemberExpr *E) {
4000043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman    if (CheckReferencedDecl(E, E->getMemberDecl())) {
4001c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith      VisitIgnoredValue(E->getBase());
4002043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman      return true;
4003043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman    }
40048cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
40058cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    return ExprEvaluatorBaseTy::VisitMemberExpr(E);
4006043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman  }
4007043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman
40088cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCallExpr(const CallExpr *E);
4009b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattner  bool VisitBinaryOperator(const BinaryOperator *E);
40108ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  bool VisitOffsetOfExpr(const OffsetOfExpr *E);
4011b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattner  bool VisitUnaryOperator(const UnaryOperator *E);
4012f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
40138cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCastExpr(const CastExpr* E);
4014f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E);
40150518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl
40163068d117951a8df54bae9db039b56201ab10962bAnders Carlsson  bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
4017131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar    return Success(E->getValue(), E);
40183068d117951a8df54bae9db039b56201ab10962bAnders Carlsson  }
40191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4020ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  bool VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) {
4021ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    return Success(E->getValue(), E);
4022ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  }
4023ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
4024f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  // Note, GNU defines __null as an integer, not a pointer.
40253f70456b8adb0405ef2a47d51f9fc2d5937ae8aeAnders Carlsson  bool VisitGNUNullExpr(const GNUNullExpr *E) {
402651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return ZeroInitialization(E);
4027664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman  }
4028664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman
402964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
40300dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    return Success(E->getValue(), E);
403164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  }
403264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
40336ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  bool VisitBinaryTypeTraitExpr(const BinaryTypeTraitExpr *E) {
40346ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    return Success(E->getValue(), E);
40356ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  }
40366ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
40374ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  bool VisitTypeTraitExpr(const TypeTraitExpr *E) {
40384ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    return Success(E->getValue(), E);
40394ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  }
40404ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
404121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) {
404221ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    return Success(E->getValue(), E);
404321ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  }
404421ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
4045552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) {
4046552622067dc45013d240f73952fece703f5e63bdJohn Wiegley    return Success(E->getValue(), E);
4047552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  }
4048552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
4049722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman  bool VisitUnaryReal(const UnaryOperator *E);
4050664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman  bool VisitUnaryImag(const UnaryOperator *E);
4051664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman
4052295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl  bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E);
4053ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor  bool VisitSizeOfPackExpr(const SizeOfPackExpr *E);
4054cea8d966f826554f0679595e9371e314e8dbc1cfSebastian Redl
4055fcee0019b76f9f368f2b3d6d4048a98232593f29Chris Lattnerprivate:
40568b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck  CharUnits GetAlignOfExpr(const Expr *E);
40578b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck  CharUnits GetAlignOfType(QualType T);
40581bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  static QualType GetObjectType(APValue::LValueBase B);
40598cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool TryEvaluateBuiltinObjectSize(const CallExpr *E);
4060664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman  // FIXME: Missing: array subscript of vector, member of vector
4061f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner};
4062f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner} // end anonymous namespace
4063f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
4064c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// EvaluateIntegerOrLValue - Evaluate an rvalue integral-typed expression, and
4065c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// produce either the integer value or a pointer.
4066c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith///
4067c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// GCC has a heinous extension which folds casts between pointer types and
4068c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// pointer-sized integral types. We support this by allowing the evaluation of
4069c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// an integer rvalue to produce a pointer (represented as an lvalue) instead.
4070c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// Some simple arithmetic on such values is supported (they are treated much
4071c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// like char*).
40721aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smithstatic bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,
407347a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith                                    EvalInfo &Info) {
4074c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  assert(E->isRValue() && E->getType()->isIntegralOrEnumerationType());
40758cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  return IntExprEvaluator(Info, Result).Visit(E);
407669ab26a8623141f35e86817cfc6e0fbe7639a40fDaniel Dunbar}
407769ab26a8623141f35e86817cfc6e0fbe7639a40fDaniel Dunbar
4078f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info) {
40791aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  APValue Val;
4080f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (!EvaluateIntegerOrLValue(E, Val, Info))
4081f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return false;
4082f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (!Val.isInt()) {
4083f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    // FIXME: It would be better to produce the diagnostic for casting
4084f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    //        a pointer to an integer.
40855cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.Diag(E, diag::note_invalid_subexpr_in_const_expr);
408630c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar    return false;
4087f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
408830c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar  Result = Val.getInt();
408930c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar  return true;
4090f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner}
4091f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
4092f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// Check whether the given declaration can be directly converted to an integral
4093f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// rvalue. If not, no diagnostic is produced; there are other things we can
4094f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// try.
4095043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedmanbool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) {
40964c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner  // Enums are integer constant exprs.
4097bfbdcd861a4364bfc21a9e5047bdbd56812d6693Abramo Bagnara  if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) {
4098973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara    // Check for signedness/width mismatches between E type and ECD value.
4099973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara    bool SameSign = (ECD->getInitVal().isSigned()
4100973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara                     == E->getType()->isSignedIntegerOrEnumerationType());
4101973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara    bool SameWidth = (ECD->getInitVal().getBitWidth()
4102973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara                      == Info.Ctx.getIntWidth(E->getType()));
4103973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara    if (SameSign && SameWidth)
4104973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara      return Success(ECD->getInitVal(), E);
4105973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara    else {
4106973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara      // Get rid of mismatch (otherwise Success assertions will fail)
4107973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara      // by computing a new value matching the type of E.
4108973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara      llvm::APSInt Val = ECD->getInitVal();
4109973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara      if (!SameSign)
4110973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara        Val.setIsSigned(!ECD->getInitVal().isSigned());
4111973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara      if (!SameWidth)
4112973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara        Val = Val.extOrTrunc(Info.Ctx.getIntWidth(E->getType()));
4113973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara      return Success(Val, E);
4114973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara    }
4115bfbdcd861a4364bfc21a9e5047bdbd56812d6693Abramo Bagnara  }
41168cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  return false;
41174c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner}
41184c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner
4119a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
4120a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner/// as GCC.
4121a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattnerstatic int EvaluateBuiltinClassifyType(const CallExpr *E) {
4122a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  // The following enum mimics the values returned by GCC.
41237c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // FIXME: Does GCC differ between lvalue and rvalue references here?
4124a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  enum gcc_type_class {
4125a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    no_type_class = -1,
4126a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    void_type_class, integer_type_class, char_type_class,
4127a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    enumeral_type_class, boolean_type_class,
4128a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    pointer_type_class, reference_type_class, offset_type_class,
4129a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    real_type_class, complex_type_class,
4130a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    function_type_class, method_type_class,
4131a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    record_type_class, union_type_class,
4132a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    array_type_class, string_type_class,
4133a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    lang_type_class
4134a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  };
41351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
41361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If no argument was supplied, default to "no_type_class". This isn't
4137a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  // ideal, however it is what gcc does.
4138a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  if (E->getNumArgs() == 0)
4139a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return no_type_class;
41401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4141a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  QualType ArgTy = E->getArg(0)->getType();
4142a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  if (ArgTy->isVoidType())
4143a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return void_type_class;
4144a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isEnumeralType())
4145a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return enumeral_type_class;
4146a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isBooleanType())
4147a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return boolean_type_class;
4148a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isCharType())
4149a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return string_type_class; // gcc doesn't appear to use char_type_class
4150a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isIntegerType())
4151a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return integer_type_class;
4152a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isPointerType())
4153a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return pointer_type_class;
4154a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isReferenceType())
4155a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return reference_type_class;
4156a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isRealType())
4157a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return real_type_class;
4158a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isComplexType())
4159a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return complex_type_class;
4160a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isFunctionType())
4161a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return function_type_class;
4162fb87b89fc9eb103e19fb8e4b925c23f0bd091b99Douglas Gregor  else if (ArgTy->isStructureOrClassType())
4163a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return record_type_class;
4164a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isUnionType())
4165a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return union_type_class;
4166a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isArrayType())
4167a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return array_type_class;
4168a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isUnionType())
4169a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return union_type_class;
4170a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else  // FIXME: offset_type_class, method_type_class, & lang_type_class?
4171b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("CallExpr::isBuiltinClassifyType(): unimplemented type");
4172a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner}
4173a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner
417480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// EvaluateBuiltinConstantPForLValue - Determine the result of
417580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// __builtin_constant_p when applied to the given lvalue.
417680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith///
417780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// An lvalue is only "constant" if it is a pointer or reference to the first
417880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// character of a string literal.
417980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smithtemplate<typename LValue>
418080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smithstatic bool EvaluateBuiltinConstantPForLValue(const LValue &LV) {
41818e55ed1ad3acf9c7f8424aa7d326b3b2c18e943bDouglas Gregor  const Expr *E = LV.getLValueBase().template dyn_cast<const Expr*>();
418280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  return E && isa<StringLiteral>(E) && LV.getLValueOffset().isZero();
418380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith}
418480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith
418580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// EvaluateBuiltinConstantP - Evaluate __builtin_constant_p as similarly to
418680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// GCC as we can manage.
418780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smithstatic bool EvaluateBuiltinConstantP(ASTContext &Ctx, const Expr *Arg) {
418880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  QualType ArgType = Arg->getType();
418980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith
419080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  // __builtin_constant_p always has one operand. The rules which gcc follows
419180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  // are not precisely documented, but are as follows:
419280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  //
419380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  //  - If the operand is of integral, floating, complex or enumeration type,
419480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  //    and can be folded to a known value of that type, it returns 1.
419580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  //  - If the operand and can be folded to a pointer to the first character
419680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  //    of a string literal (or such a pointer cast to an integral type), it
419780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  //    returns 1.
419880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  //
419980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  // Otherwise, it returns 0.
420080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  //
420180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  // FIXME: GCC also intends to return 1 for literals of aggregate types, but
420280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  // its support for this does not currently work.
420380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  if (ArgType->isIntegralOrEnumerationType()) {
420480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    Expr::EvalResult Result;
420580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    if (!Arg->EvaluateAsRValue(Result, Ctx) || Result.HasSideEffects)
420680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith      return false;
420780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith
420880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    APValue &V = Result.Val;
420980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    if (V.getKind() == APValue::Int)
421080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith      return true;
421180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith
421280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    return EvaluateBuiltinConstantPForLValue(V);
421380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  } else if (ArgType->isFloatingType() || ArgType->isAnyComplexType()) {
421480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    return Arg->isEvaluatable(Ctx);
421580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  } else if (ArgType->isPointerType() || Arg->isGLValue()) {
421680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    LValue LV;
421780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    Expr::EvalStatus Status;
421880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    EvalInfo Info(Ctx, Status);
421980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    if ((Arg->isGLValue() ? EvaluateLValue(Arg, LV, Info)
422080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith                          : EvaluatePointer(Arg, LV, Info)) &&
422180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith        !Status.HasSideEffects)
422280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith      return EvaluateBuiltinConstantPForLValue(LV);
422380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  }
422480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith
422580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  // Anything else isn't considered to be sufficiently constant.
422680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  return false;
422780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith}
422880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith
422942c8f87eb60958170c46767273bf93e6c96125bfJohn McCall/// Retrieves the "underlying object type" of the given expression,
423042c8f87eb60958170c46767273bf93e6c96125bfJohn McCall/// as used by __builtin_object_size.
42311bf9a9e6a5bdc0de7939908855dcddf46b661800Richard SmithQualType IntExprEvaluator::GetObjectType(APValue::LValueBase B) {
42321bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) {
42331bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    if (const VarDecl *VD = dyn_cast<VarDecl>(D))
423442c8f87eb60958170c46767273bf93e6c96125bfJohn McCall      return VD->getType();
42351bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  } else if (const Expr *E = B.get<const Expr*>()) {
42361bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    if (isa<CompoundLiteralExpr>(E))
42371bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith      return E->getType();
423842c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  }
423942c8f87eb60958170c46767273bf93e6c96125bfJohn McCall
424042c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  return QualType();
424142c8f87eb60958170c46767273bf93e6c96125bfJohn McCall}
424242c8f87eb60958170c46767273bf93e6c96125bfJohn McCall
42438cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool IntExprEvaluator::TryEvaluateBuiltinObjectSize(const CallExpr *E) {
424442c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  LValue Base;
4245c6794850a570a91c5f224b6f0293db9f560f4213Richard Smith
4246c6794850a570a91c5f224b6f0293db9f560f4213Richard Smith  {
4247c6794850a570a91c5f224b6f0293db9f560f4213Richard Smith    // The operand of __builtin_object_size is never evaluated for side-effects.
4248c6794850a570a91c5f224b6f0293db9f560f4213Richard Smith    // If there are any, but we can determine the pointed-to object anyway, then
4249c6794850a570a91c5f224b6f0293db9f560f4213Richard Smith    // ignore the side-effects.
4250c6794850a570a91c5f224b6f0293db9f560f4213Richard Smith    SpeculativeEvaluationRAII SpeculativeEval(Info);
4251c6794850a570a91c5f224b6f0293db9f560f4213Richard Smith    if (!EvaluatePointer(E->getArg(0), Base, Info))
4252c6794850a570a91c5f224b6f0293db9f560f4213Richard Smith      return false;
4253c6794850a570a91c5f224b6f0293db9f560f4213Richard Smith  }
425442c8f87eb60958170c46767273bf93e6c96125bfJohn McCall
425542c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  // If we can prove the base is null, lower to zero now.
42561bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  if (!Base.getLValueBase()) return Success(0, E);
425742c8f87eb60958170c46767273bf93e6c96125bfJohn McCall
42581bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  QualType T = GetObjectType(Base.getLValueBase());
425942c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  if (T.isNull() ||
426042c8f87eb60958170c46767273bf93e6c96125bfJohn McCall      T->isIncompleteType() ||
42611357869bc5983cdfbc986db1f3d18265bb34cb0eEli Friedman      T->isFunctionType() ||
426242c8f87eb60958170c46767273bf93e6c96125bfJohn McCall      T->isVariablyModifiedType() ||
426342c8f87eb60958170c46767273bf93e6c96125bfJohn McCall      T->isDependentType())
4264f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
426542c8f87eb60958170c46767273bf93e6c96125bfJohn McCall
426642c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  CharUnits Size = Info.Ctx.getTypeSizeInChars(T);
426742c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  CharUnits Offset = Base.getLValueOffset();
426842c8f87eb60958170c46767273bf93e6c96125bfJohn McCall
426942c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  if (!Offset.isNegative() && Offset <= Size)
427042c8f87eb60958170c46767273bf93e6c96125bfJohn McCall    Size -= Offset;
427142c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  else
427242c8f87eb60958170c46767273bf93e6c96125bfJohn McCall    Size = CharUnits::Zero();
42734f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck  return Success(Size, E);
427442c8f87eb60958170c46767273bf93e6c96125bfJohn McCall}
427542c8f87eb60958170c46767273bf93e6c96125bfJohn McCall
42768cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
42772c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith  switch (unsigned BuiltinOp = E->isBuiltinCall()) {
4278019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  default:
42798cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    return ExprEvaluatorBaseTy::VisitCallExpr(E);
428064eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump
428164eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump  case Builtin::BI__builtin_object_size: {
428242c8f87eb60958170c46767273bf93e6c96125bfJohn McCall    if (TryEvaluateBuiltinObjectSize(E))
428342c8f87eb60958170c46767273bf93e6c96125bfJohn McCall      return true;
428464eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump
42858ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    // If evaluating the argument has side-effects, we can't determine the size
42868ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    // of the object, and so we lower it to unknown now. CodeGen relies on us to
42878ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith    // handle all cases where the expression has side-effects.
4288393c247fe025ccb5f914e37e948192ea86faef8cFariborz Jahanian    if (E->getArg(0)->HasSideEffects(Info.Ctx)) {
4289a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith      if (E->getArg(1)->EvaluateKnownConstInt(Info.Ctx).getZExtValue() <= 1)
4290cf184655319cf7a5b811067cff9d26a5741fd161Chris Lattner        return Success(-1ULL, E);
429164eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump      return Success(0, E);
429264eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump    }
4293c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump
4294c6794850a570a91c5f224b6f0293db9f560f4213Richard Smith    // Expression had no side effects, but we couldn't statically determine the
4295c6794850a570a91c5f224b6f0293db9f560f4213Richard Smith    // size of the referenced object.
4296f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
429764eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump  }
429864eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump
4299019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  case Builtin::BI__builtin_classify_type:
4300131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar    return Success(EvaluateBuiltinClassifyType(E), E);
43011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
430280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  case Builtin::BI__builtin_constant_p:
430380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    return Success(EvaluateBuiltinConstantP(Info.Ctx, E->getArg(0)), E);
4304e052d46f4db91f9ba572859ffc984e85cbf5d5ffRichard Smith
430521fb98ee003e992b0c4e204d98a19e0ef544cae3Chris Lattner  case Builtin::BI__builtin_eh_return_data_regno: {
4306a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith    int Operand = E->getArg(0)->EvaluateKnownConstInt(Info.Ctx).getZExtValue();
4307bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(Operand);
430821fb98ee003e992b0c4e204d98a19e0ef544cae3Chris Lattner    return Success(Operand, E);
430921fb98ee003e992b0c4e204d98a19e0ef544cae3Chris Lattner  }
4310c4a2638b5ef3e2d35d872614ceb655a7a22c58beEli Friedman
4311c4a2638b5ef3e2d35d872614ceb655a7a22c58beEli Friedman  case Builtin::BI__builtin_expect:
4312c4a2638b5ef3e2d35d872614ceb655a7a22c58beEli Friedman    return Visit(E->getArg(0));
431340b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith
43145726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor  case Builtin::BIstrlen:
431540b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith    // A call to strlen is not a constant expression.
431640b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith    if (Info.getLangOpts().CPlusPlus0x)
43175cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      Info.CCEDiag(E, diag::note_constexpr_invalid_function)
431840b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith        << /*isConstexpr*/0 << /*isConstructor*/0 << "'strlen'";
431940b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith    else
43205cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr);
432140b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith    // Fall through.
43225726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor  case Builtin::BI__builtin_strlen:
43235726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor    // As an extension, we support strlen() and __builtin_strlen() as constant
43245726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor    // expressions when the argument is a string literal.
43258cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    if (const StringLiteral *S
43265726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor               = dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenImpCasts())) {
43275726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor      // The string literal may have embedded null characters. Find the first
43285726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor      // one and truncate there.
43295f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      StringRef Str = S->getString();
43305f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      StringRef::size_type Pos = Str.find(0);
43315f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      if (Pos != StringRef::npos)
43325726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor        Str = Str.substr(0, Pos);
43335726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor
43345726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor      return Success(Str.size(), E);
43355726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor    }
43365726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor
4337f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
4338454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman
43392c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith  case Builtin::BI__atomic_always_lock_free:
4340fafbf06732746f3ceca21d452d77b144ba8652aeRichard Smith  case Builtin::BI__atomic_is_lock_free:
4341fafbf06732746f3ceca21d452d77b144ba8652aeRichard Smith  case Builtin::BI__c11_atomic_is_lock_free: {
4342454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    APSInt SizeVal;
4343454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    if (!EvaluateInteger(E->getArg(0), SizeVal, Info))
4344454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman      return false;
4345454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman
4346454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // For __atomic_is_lock_free(sizeof(_Atomic(T))), if the size is a power
4347454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // of two less than the maximum inline atomic width, we know it is
4348454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // lock-free.  If the size isn't a power of two, or greater than the
4349454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // maximum alignment where we promote atomics, we know it is not lock-free
4350454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // (at least not in the sense of atomic_is_lock_free).  Otherwise,
4351454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // the answer can only be determined at runtime; for example, 16-byte
4352454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // atomics have lock-free implementations on some, but not all,
4353454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // x86-64 processors.
4354454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman
4355454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // Check power-of-two.
4356454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    CharUnits Size = CharUnits::fromQuantity(SizeVal.getZExtValue());
43572c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith    if (Size.isPowerOfTwo()) {
43582c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith      // Check against inlining width.
43592c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith      unsigned InlineWidthBits =
43602c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith          Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth();
43612c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith      if (Size <= Info.Ctx.toCharUnitsFromBits(InlineWidthBits)) {
43622c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith        if (BuiltinOp == Builtin::BI__c11_atomic_is_lock_free ||
43632c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith            Size == CharUnits::One() ||
43642c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith            E->getArg(1)->isNullPointerConstant(Info.Ctx,
43652c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith                                                Expr::NPC_NeverValueDependent))
43662c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith          // OK, we will inline appropriately-aligned operations of this size,
43672c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith          // and _Atomic(T) is appropriately-aligned.
43682c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith          return Success(1, E);
43692c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith
43702c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith        QualType PointeeType = E->getArg(1)->IgnoreImpCasts()->getType()->
43712c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith          castAs<PointerType>()->getPointeeType();
43722c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith        if (!PointeeType->isIncompleteType() &&
43732c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith            Info.Ctx.getTypeAlignInChars(PointeeType) >= Size) {
43742c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith          // OK, we will inline operations on this object.
43752c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith          return Success(1, E);
43762c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith        }
43772c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith      }
43782c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith    }
4379454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman
43802c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith    return BuiltinOp == Builtin::BI__atomic_always_lock_free ?
43812c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith        Success(0, E) : Error(E);
4382454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman  }
4383019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  }
43844c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner}
4385f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
4386625b80755b603d28f36fb4212c81484d87ad08d3Richard Smithstatic bool HasSameBase(const LValue &A, const LValue &B) {
4387625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith  if (!A.getLValueBase())
4388625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith    return !B.getLValueBase();
4389625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith  if (!B.getLValueBase())
4390625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith    return false;
4391625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith
43921bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  if (A.getLValueBase().getOpaqueValue() !=
43931bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith      B.getLValueBase().getOpaqueValue()) {
4394625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith    const Decl *ADecl = GetLValueBaseDecl(A);
4395625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith    if (!ADecl)
4396625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith      return false;
4397625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith    const Decl *BDecl = GetLValueBaseDecl(B);
43989a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith    if (!BDecl || ADecl->getCanonicalDecl() != BDecl->getCanonicalDecl())
4399625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith      return false;
4400625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith  }
4401625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith
4402625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith  return IsGlobalLValue(A.getLValueBase()) ||
440383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith         A.getLValueCallIndex() == B.getLValueCallIndex();
4404625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith}
4405625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith
44067b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith/// Perform the given integer operation, which is known to need at most BitWidth
44077b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith/// bits, and check for overflow in the original type (if that type was not an
44087b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith/// unsigned type).
44097b48a2986345480241f3b8209f71bb21b0530b4fRichard Smithtemplate<typename Operation>
44107b48a2986345480241f3b8209f71bb21b0530b4fRichard Smithstatic APSInt CheckedIntArithmetic(EvalInfo &Info, const Expr *E,
44117b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith                                   const APSInt &LHS, const APSInt &RHS,
44127b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith                                   unsigned BitWidth, Operation Op) {
44137b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith  if (LHS.isUnsigned())
44147b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    return Op(LHS, RHS);
44157b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith
44167b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith  APSInt Value(Op(LHS.extend(BitWidth), RHS.extend(BitWidth)), false);
44177b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith  APSInt Result = Value.trunc(LHS.getBitWidth());
44187b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith  if (Result.extend(BitWidth) != Value)
44197b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    HandleOverflow(Info, E, Value, E->getType());
44207b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith  return Result;
44217b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith}
44227b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith
4423cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidisnamespace {
4424c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
4425cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis/// \brief Data recursive integer evaluator of certain binary operators.
4426cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis///
4427cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis/// We use a data recursive algorithm for binary operators so that we are able
4428cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis/// to handle extreme cases of chained binary operators without causing stack
4429cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis/// overflow.
4430cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidisclass DataRecursiveIntBinOpEvaluator {
4431cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  struct EvalResult {
4432cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    APValue Val;
4433cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    bool Failed;
4434cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4435cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    EvalResult() : Failed(false) { }
4436cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4437cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    void swap(EvalResult &RHS) {
4438cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      Val.swap(RHS.Val);
4439cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      Failed = RHS.Failed;
4440cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      RHS.Failed = false;
4441cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    }
4442cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  };
4443cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4444cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  struct Job {
4445cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    const Expr *E;
4446cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    EvalResult LHSResult; // meaningful only for binary operator expression.
4447cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    enum { AnyExprKind, BinOpKind, BinOpVisitedLHSKind } Kind;
4448cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4449cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    Job() : StoredInfo(0) { }
4450cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    void startSpeculativeEval(EvalInfo &Info) {
4451cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      OldEvalStatus = Info.EvalStatus;
4452cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      Info.EvalStatus.Diag = 0;
4453cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      StoredInfo = &Info;
4454cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    }
4455cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    ~Job() {
4456cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      if (StoredInfo) {
4457cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        StoredInfo->EvalStatus = OldEvalStatus;
4458cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      }
4459cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    }
4460cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  private:
4461cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    EvalInfo *StoredInfo; // non-null if status changed.
4462cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    Expr::EvalStatus OldEvalStatus;
4463cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  };
4464cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4465cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  SmallVector<Job, 16> Queue;
4466cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4467cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  IntExprEvaluator &IntEval;
4468cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  EvalInfo &Info;
4469cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  APValue &FinalResult;
4470cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4471cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidispublic:
4472cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  DataRecursiveIntBinOpEvaluator(IntExprEvaluator &IntEval, APValue &Result)
4473cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    : IntEval(IntEval), Info(IntEval.getEvalInfo()), FinalResult(Result) { }
4474cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4475cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  /// \brief True if \param E is a binary operator that we are going to handle
4476cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  /// data recursively.
4477cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  /// We handle binary operators that are comma, logical, or that have operands
4478cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  /// with integral or enumeration type.
4479cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  static bool shouldEnqueue(const BinaryOperator *E) {
4480cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return E->getOpcode() == BO_Comma ||
4481cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis           E->isLogicalOp() ||
4482cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis           (E->getLHS()->getType()->isIntegralOrEnumerationType() &&
4483cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis            E->getRHS()->getType()->isIntegralOrEnumerationType());
4484cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4485cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4486cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  bool Traverse(const BinaryOperator *E) {
4487cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    enqueue(E);
4488cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    EvalResult PrevResult;
4489b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu    while (!Queue.empty())
4490b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu      process(PrevResult);
4491b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu
4492b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu    if (PrevResult.Failed) return false;
4493cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4494cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    FinalResult.swap(PrevResult.Val);
4495cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return true;
4496a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman  }
4497a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman
4498cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidisprivate:
4499cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  bool Success(uint64_t Value, const Expr *E, APValue &Result) {
4500cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return IntEval.Success(Value, E, Result);
4501cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4502cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  bool Success(const APSInt &Value, const Expr *E, APValue &Result) {
4503cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return IntEval.Success(Value, E, Result);
4504cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4505cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  bool Error(const Expr *E) {
4506cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return IntEval.Error(E);
4507cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4508cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  bool Error(const Expr *E, diag::kind D) {
4509cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return IntEval.Error(E, D);
4510cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4511cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4512cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) {
4513cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return Info.CCEDiag(E, D);
4514cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4515cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
45169293fff58aa0198fa7a6bb504169bcac01dbbff7Argyrios Kyrtzidis  // \brief Returns true if visiting the RHS is necessary, false otherwise.
45179293fff58aa0198fa7a6bb504169bcac01dbbff7Argyrios Kyrtzidis  bool VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E,
4518cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                         bool &SuppressRHSDiags);
45192fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis
4520cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  bool VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult,
4521cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                  const BinaryOperator *E, APValue &Result);
4522cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4523cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  void EvaluateExpr(const Expr *E, EvalResult &Result) {
4524cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    Result.Failed = !Evaluate(Result.Val, Info, E);
4525cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    if (Result.Failed)
4526cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      Result.Val = APValue();
4527cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4528cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4529b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu  void process(EvalResult &Result);
4530cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4531cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  void enqueue(const Expr *E) {
4532cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    E = E->IgnoreParens();
4533cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    Queue.resize(Queue.size()+1);
4534cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    Queue.back().E = E;
4535cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    Queue.back().Kind = Job::AnyExprKind;
4536cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4537cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis};
4538cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4539cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis}
4540cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4541cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidisbool DataRecursiveIntBinOpEvaluator::
45429293fff58aa0198fa7a6bb504169bcac01dbbff7Argyrios Kyrtzidis       VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E,
4543cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                         bool &SuppressRHSDiags) {
4544cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  if (E->getOpcode() == BO_Comma) {
4545cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    // Ignore LHS but note if we could not evaluate it.
4546cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    if (LHSResult.Failed)
4547cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      Info.EvalStatus.HasSideEffects = true;
4548cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return true;
4549cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4550cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4551cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  if (E->isLogicalOp()) {
4552cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    bool lhsResult;
4553cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    if (HandleConversionToBool(LHSResult.Val, lhsResult)) {
45542fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis      // We were able to evaluate the LHS, see if we can get away with not
45552fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis      // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
4556cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      if (lhsResult == (E->getOpcode() == BO_LOr)) {
45579293fff58aa0198fa7a6bb504169bcac01dbbff7Argyrios Kyrtzidis        Success(lhsResult, E, LHSResult.Val);
45589293fff58aa0198fa7a6bb504169bcac01dbbff7Argyrios Kyrtzidis        return false; // Ignore RHS
45592fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis      }
45602fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis    } else {
45612fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis      // Since we weren't able to evaluate the left hand side, it
45622fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis      // must have had side effects.
45632fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis      Info.EvalStatus.HasSideEffects = true;
4564cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4565cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // We can't evaluate the LHS; however, sometimes the result
4566cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
4567cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // Don't ignore RHS and suppress diagnostics from this arm.
4568cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      SuppressRHSDiags = true;
4569cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    }
4570cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4571cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return true;
4572cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4573cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4574cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  assert(E->getLHS()->getType()->isIntegralOrEnumerationType() &&
4575cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis         E->getRHS()->getType()->isIntegralOrEnumerationType());
4576cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4577cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  if (LHSResult.Failed && !Info.keepEvaluatingAfterFailure())
45789293fff58aa0198fa7a6bb504169bcac01dbbff7Argyrios Kyrtzidis    return false; // Ignore RHS;
45799293fff58aa0198fa7a6bb504169bcac01dbbff7Argyrios Kyrtzidis
4580cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  return true;
4581cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis}
45822fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis
4583cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidisbool DataRecursiveIntBinOpEvaluator::
4584cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis       VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult,
4585cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                  const BinaryOperator *E, APValue &Result) {
4586cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  if (E->getOpcode() == BO_Comma) {
4587cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    if (RHSResult.Failed)
4588cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      return false;
4589cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    Result = RHSResult.Val;
4590cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return true;
4591cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4592cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4593cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  if (E->isLogicalOp()) {
4594cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    bool lhsResult, rhsResult;
4595cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    bool LHSIsOK = HandleConversionToBool(LHSResult.Val, lhsResult);
4596cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    bool RHSIsOK = HandleConversionToBool(RHSResult.Val, rhsResult);
4597cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4598cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    if (LHSIsOK) {
4599cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      if (RHSIsOK) {
4600cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        if (E->getOpcode() == BO_LOr)
4601cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis          return Success(lhsResult || rhsResult, E, Result);
4602cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        else
4603cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis          return Success(lhsResult && rhsResult, E, Result);
4604cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      }
4605cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    } else {
4606cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      if (RHSIsOK) {
46072fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis        // We can't evaluate the LHS; however, sometimes the result
46082fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis        // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
46092fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis        if (rhsResult == (E->getOpcode() == BO_LOr))
4610cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis          return Success(rhsResult, E, Result);
46112fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis      }
46122fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis    }
4613cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
46142fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis    return false;
46152fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis  }
4616cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4617cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  assert(E->getLHS()->getType()->isIntegralOrEnumerationType() &&
4618cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis         E->getRHS()->getType()->isIntegralOrEnumerationType());
4619cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4620cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  if (LHSResult.Failed || RHSResult.Failed)
4621cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return false;
4622cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4623cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  const APValue &LHSVal = LHSResult.Val;
4624cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  const APValue &RHSVal = RHSResult.Val;
4625cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4626cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  // Handle cases like (unsigned long)&a + 4.
4627cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) {
4628cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    Result = LHSVal;
4629cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    CharUnits AdditionalOffset = CharUnits::fromQuantity(
4630cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                                                         RHSVal.getInt().getZExtValue());
4631cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    if (E->getOpcode() == BO_Add)
4632cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      Result.getLValueOffset() += AdditionalOffset;
4633cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    else
4634cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      Result.getLValueOffset() -= AdditionalOffset;
4635cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return true;
4636cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4637cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4638cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  // Handle cases like 4 + (unsigned long)&a
4639cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  if (E->getOpcode() == BO_Add &&
4640cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      RHSVal.isLValue() && LHSVal.isInt()) {
4641cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    Result = RHSVal;
4642cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    Result.getLValueOffset() += CharUnits::fromQuantity(
4643cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                                                        LHSVal.getInt().getZExtValue());
4644cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return true;
4645cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4646cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4647cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  if (E->getOpcode() == BO_Sub && LHSVal.isLValue() && RHSVal.isLValue()) {
4648cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    // Handle (intptr_t)&&A - (intptr_t)&&B.
4649cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    if (!LHSVal.getLValueOffset().isZero() ||
4650cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        !RHSVal.getLValueOffset().isZero())
4651cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      return false;
4652cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    const Expr *LHSExpr = LHSVal.getLValueBase().dyn_cast<const Expr*>();
4653cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    const Expr *RHSExpr = RHSVal.getLValueBase().dyn_cast<const Expr*>();
4654cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    if (!LHSExpr || !RHSExpr)
4655cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      return false;
4656cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr);
4657cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr);
4658cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    if (!LHSAddrExpr || !RHSAddrExpr)
4659cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      return false;
4660cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    // Make sure both labels come from the same function.
4661cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    if (LHSAddrExpr->getLabel()->getDeclContext() !=
4662cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        RHSAddrExpr->getLabel()->getDeclContext())
4663cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      return false;
4664cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    Result = APValue(LHSAddrExpr, RHSAddrExpr);
4665cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return true;
4666cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4667cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4668cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  // All the following cases expect both operands to be an integer
4669cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  if (!LHSVal.isInt() || !RHSVal.isInt())
4670cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return Error(E);
4671cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4672cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  const APSInt &LHS = LHSVal.getInt();
4673cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  APSInt RHS = RHSVal.getInt();
4674cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4675cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  switch (E->getOpcode()) {
4676cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    default:
4677cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      return Error(E);
4678cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_Mul:
4679cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      return Success(CheckedIntArithmetic(Info, E, LHS, RHS,
4680cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                                          LHS.getBitWidth() * 2,
4681cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                                          std::multiplies<APSInt>()), E,
4682cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                     Result);
4683cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_Add:
4684cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      return Success(CheckedIntArithmetic(Info, E, LHS, RHS,
4685cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                                          LHS.getBitWidth() + 1,
4686cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                                          std::plus<APSInt>()), E, Result);
4687cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_Sub:
4688cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      return Success(CheckedIntArithmetic(Info, E, LHS, RHS,
4689cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                                          LHS.getBitWidth() + 1,
4690cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                                          std::minus<APSInt>()), E, Result);
4691cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_And: return Success(LHS & RHS, E, Result);
4692cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_Xor: return Success(LHS ^ RHS, E, Result);
4693cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_Or:  return Success(LHS | RHS, E, Result);
4694cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_Div:
4695cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_Rem:
4696cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      if (RHS == 0)
4697cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        return Error(E, diag::note_expr_divide_by_zero);
4698cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // Check for overflow case: INT_MIN / -1 or INT_MIN % -1. The latter is
4699cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // not actually undefined behavior in C++11 due to a language defect.
4700cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      if (RHS.isNegative() && RHS.isAllOnesValue() &&
4701cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis          LHS.isSigned() && LHS.isMinSignedValue())
4702cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        HandleOverflow(Info, E, -LHS.extend(LHS.getBitWidth() + 1), E->getType());
4703cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      return Success(E->getOpcode() == BO_Rem ? LHS % RHS : LHS / RHS, E,
4704cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                     Result);
4705cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_Shl: {
4706cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // During constant-folding, a negative shift is an opposite shift. Such
4707cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // a shift is not a constant expression.
4708cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      if (RHS.isSigned() && RHS.isNegative()) {
4709cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        CCEDiag(E, diag::note_constexpr_negative_shift) << RHS;
4710cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        RHS = -RHS;
4711cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        goto shift_right;
4712cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      }
4713cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4714cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    shift_left:
4715cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // C++11 [expr.shift]p1: Shift width must be less than the bit width of
4716cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // the shifted type.
4717cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1);
4718cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      if (SA != RHS) {
4719cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        CCEDiag(E, diag::note_constexpr_large_shift)
4720cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        << RHS << E->getType() << LHS.getBitWidth();
4721cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      } else if (LHS.isSigned()) {
4722cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        // C++11 [expr.shift]p2: A signed left shift must have a non-negative
4723cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        // operand, and must not overflow the corresponding unsigned type.
4724cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        if (LHS.isNegative())
4725cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis          CCEDiag(E, diag::note_constexpr_lshift_of_negative) << LHS;
4726cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        else if (LHS.countLeadingZeros() < SA)
4727cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis          CCEDiag(E, diag::note_constexpr_lshift_discards);
4728cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      }
4729cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4730cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      return Success(LHS << SA, E, Result);
4731cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    }
4732cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_Shr: {
4733cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // During constant-folding, a negative shift is an opposite shift. Such a
4734cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // shift is not a constant expression.
4735cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      if (RHS.isSigned() && RHS.isNegative()) {
4736cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        CCEDiag(E, diag::note_constexpr_negative_shift) << RHS;
4737cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        RHS = -RHS;
4738cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        goto shift_left;
4739cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      }
4740cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4741cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    shift_right:
4742cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // C++11 [expr.shift]p1: Shift width must be less than the bit width of the
4743cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // shifted type.
4744cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1);
4745cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      if (SA != RHS)
4746cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        CCEDiag(E, diag::note_constexpr_large_shift)
4747cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        << RHS << E->getType() << LHS.getBitWidth();
4748cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4749cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      return Success(LHS >> SA, E, Result);
4750cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    }
4751cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4752cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_LT: return Success(LHS < RHS, E, Result);
4753cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_GT: return Success(LHS > RHS, E, Result);
4754cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_LE: return Success(LHS <= RHS, E, Result);
4755cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_GE: return Success(LHS >= RHS, E, Result);
4756cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_EQ: return Success(LHS == RHS, E, Result);
4757cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_NE: return Success(LHS != RHS, E, Result);
4758cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4759cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis}
4760cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4761b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieuvoid DataRecursiveIntBinOpEvaluator::process(EvalResult &Result) {
4762cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  Job &job = Queue.back();
4763cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4764cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  switch (job.Kind) {
4765cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case Job::AnyExprKind: {
4766cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(job.E)) {
4767cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        if (shouldEnqueue(Bop)) {
4768cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis          job.Kind = Job::BinOpKind;
4769cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis          enqueue(Bop->getLHS());
4770b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu          return;
4771cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        }
4772cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      }
4773cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4774cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      EvaluateExpr(job.E, Result);
4775cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      Queue.pop_back();
4776b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu      return;
4777cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    }
4778cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4779cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case Job::BinOpKind: {
4780cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      const BinaryOperator *Bop = cast<BinaryOperator>(job.E);
4781cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      bool SuppressRHSDiags = false;
47829293fff58aa0198fa7a6bb504169bcac01dbbff7Argyrios Kyrtzidis      if (!VisitBinOpLHSOnly(Result, Bop, SuppressRHSDiags)) {
4783cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        Queue.pop_back();
4784b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu        return;
4785cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      }
4786cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      if (SuppressRHSDiags)
4787cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        job.startSpeculativeEval(Info);
47889293fff58aa0198fa7a6bb504169bcac01dbbff7Argyrios Kyrtzidis      job.LHSResult.swap(Result);
4789cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      job.Kind = Job::BinOpVisitedLHSKind;
4790cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      enqueue(Bop->getRHS());
4791b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu      return;
4792cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    }
4793cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4794cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case Job::BinOpVisitedLHSKind: {
4795cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      const BinaryOperator *Bop = cast<BinaryOperator>(job.E);
4796cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      EvalResult RHS;
4797cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      RHS.swap(Result);
4798b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu      Result.Failed = !VisitBinOp(job.LHSResult, RHS, Bop, Result.Val);
4799cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      Queue.pop_back();
4800b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu      return;
4801cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    }
4802cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4803cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4804cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  llvm_unreachable("Invalid Job::Kind!");
4805cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis}
4806cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4807cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidisbool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
4808cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  if (E->isAssignmentOp())
4809cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return Error(E);
4810cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4811cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  if (DataRecursiveIntBinOpEvaluator::shouldEnqueue(E))
4812cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return DataRecursiveIntBinOpEvaluator(*this, Result).Traverse(E);
481354176fdb044312b4b77c3da6682d3575b3728d30Chris Lattner
4814286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson  QualType LHSTy = E->getLHS()->getType();
4815286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson  QualType RHSTy = E->getRHS()->getType();
48164087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar
48174087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar  if (LHSTy->isAnyComplexType()) {
48184087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar    assert(RHSTy->isAnyComplexType() && "Invalid comparison");
4819f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    ComplexValue LHS, RHS;
48204087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar
4821745f5147e065900267c85a5568785a1991d4838fRichard Smith    bool LHSOK = EvaluateComplex(E->getLHS(), LHS, Info);
4822745f5147e065900267c85a5568785a1991d4838fRichard Smith    if (!LHSOK && !Info.keepEvaluatingAfterFailure())
48234087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar      return false;
48244087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar
4825745f5147e065900267c85a5568785a1991d4838fRichard Smith    if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK)
48264087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar      return false;
48274087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar
48284087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar    if (LHS.isComplexFloat()) {
48291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      APFloat::cmpResult CR_r =
48304087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar        LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
48311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      APFloat::cmpResult CR_i =
48324087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar        LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
48334087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar
48342de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      if (E->getOpcode() == BO_EQ)
4835131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar        return Success((CR_r == APFloat::cmpEqual &&
4836131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar                        CR_i == APFloat::cmpEqual), E);
4837131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar      else {
48382de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall        assert(E->getOpcode() == BO_NE &&
4839131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar               "Invalid complex comparison.");
48401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        return Success(((CR_r == APFloat::cmpGreaterThan ||
4841fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang                         CR_r == APFloat::cmpLessThan ||
4842fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang                         CR_r == APFloat::cmpUnordered) ||
48431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                        (CR_i == APFloat::cmpGreaterThan ||
4844fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang                         CR_i == APFloat::cmpLessThan ||
4845fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang                         CR_i == APFloat::cmpUnordered)), E);
4846131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar      }
48474087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar    } else {
48482de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      if (E->getOpcode() == BO_EQ)
4849131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar        return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
4850131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar                        LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
4851131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar      else {
48522de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall        assert(E->getOpcode() == BO_NE &&
4853131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar               "Invalid compex comparison.");
4854131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar        return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
4855131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar                        LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
4856131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar      }
48574087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar    }
48584087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar  }
48591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4860286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson  if (LHSTy->isRealFloatingType() &&
4861286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson      RHSTy->isRealFloatingType()) {
4862286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson    APFloat RHS(0.0), LHS(0.0);
48631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4864745f5147e065900267c85a5568785a1991d4838fRichard Smith    bool LHSOK = EvaluateFloat(E->getRHS(), RHS, Info);
4865745f5147e065900267c85a5568785a1991d4838fRichard Smith    if (!LHSOK && !Info.keepEvaluatingAfterFailure())
4866286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson      return false;
48671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4868745f5147e065900267c85a5568785a1991d4838fRichard Smith    if (!EvaluateFloat(E->getLHS(), LHS, Info) || !LHSOK)
4869286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson      return false;
48701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4871286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson    APFloat::cmpResult CR = LHS.compare(RHS);
4872529569e68d10b0fd3750fd2124faf742249b846bAnders Carlsson
4873286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson    switch (E->getOpcode()) {
4874286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson    default:
4875b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("Invalid binary operator!");
48762de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LT:
4877131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar      return Success(CR == APFloat::cmpLessThan, E);
48782de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_GT:
4879131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar      return Success(CR == APFloat::cmpGreaterThan, E);
48802de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LE:
4881131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar      return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
48822de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_GE:
48831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
4884131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar                     E);
48852de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_EQ:
4886131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar      return Success(CR == APFloat::cmpEqual, E);
48872de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_NE:
48881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return Success(CR == APFloat::cmpGreaterThan
4889fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang                     || CR == APFloat::cmpLessThan
4890fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang                     || CR == APFloat::cmpUnordered, E);
4891286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson    }
4892286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson  }
48931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4894ad02d7debd03ff275ac8ea27891a4ecccdb78068Eli Friedman  if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
4895625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith    if (E->getOpcode() == BO_Sub || E->isComparisonOp()) {
4896745f5147e065900267c85a5568785a1991d4838fRichard Smith      LValue LHSValue, RHSValue;
4897745f5147e065900267c85a5568785a1991d4838fRichard Smith
4898745f5147e065900267c85a5568785a1991d4838fRichard Smith      bool LHSOK = EvaluatePointer(E->getLHS(), LHSValue, Info);
4899745f5147e065900267c85a5568785a1991d4838fRichard Smith      if (!LHSOK && Info.keepEvaluatingAfterFailure())
49003068d117951a8df54bae9db039b56201ab10962bAnders Carlsson        return false;
4901a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman
4902745f5147e065900267c85a5568785a1991d4838fRichard Smith      if (!EvaluatePointer(E->getRHS(), RHSValue, Info) || !LHSOK)
49033068d117951a8df54bae9db039b56201ab10962bAnders Carlsson        return false;
4904a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman
4905625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith      // Reject differing bases from the normal codepath; we special-case
4906625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith      // comparisons to null.
4907625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith      if (!HasSameBase(LHSValue, RHSValue)) {
490865639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman        if (E->getOpcode() == BO_Sub) {
490965639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman          // Handle &&A - &&B.
491065639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman          if (!LHSValue.Offset.isZero() || !RHSValue.Offset.isZero())
491165639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman            return false;
491265639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman          const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr*>();
491365639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman          const Expr *RHSExpr = LHSValue.Base.dyn_cast<const Expr*>();
491465639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman          if (!LHSExpr || !RHSExpr)
491565639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman            return false;
491665639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman          const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr);
491765639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman          const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr);
491865639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman          if (!LHSAddrExpr || !RHSAddrExpr)
491965639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman            return false;
49205930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman          // Make sure both labels come from the same function.
49215930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman          if (LHSAddrExpr->getLabel()->getDeclContext() !=
49225930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman              RHSAddrExpr->getLabel()->getDeclContext())
49235930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman            return false;
49241aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith          Result = APValue(LHSAddrExpr, RHSAddrExpr);
492565639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman          return true;
492665639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman        }
49279e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith        // Inequalities and subtractions between unrelated pointers have
49289e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith        // unspecified or undefined behavior.
49295bc86103767c2abcbfdd6518e0ccbbbb6aa59e0fEli Friedman        if (!E->isEqualityOp())
4930f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith          return Error(E);
4931ffbda40a1fb7169591dc01771f3511178a2f727cEli Friedman        // A constant address may compare equal to the address of a symbol.
4932ffbda40a1fb7169591dc01771f3511178a2f727cEli Friedman        // The one exception is that address of an object cannot compare equal
4933c45061bd0c0fdad4df8eea7e9e5af186d11427e5Eli Friedman        // to a null pointer constant.
4934ffbda40a1fb7169591dc01771f3511178a2f727cEli Friedman        if ((!LHSValue.Base && !LHSValue.Offset.isZero()) ||
4935ffbda40a1fb7169591dc01771f3511178a2f727cEli Friedman            (!RHSValue.Base && !RHSValue.Offset.isZero()))
4936f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith          return Error(E);
49379e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith        // It's implementation-defined whether distinct literals will have
4938b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith        // distinct addresses. In clang, the result of such a comparison is
4939b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith        // unspecified, so it is not a constant expression. However, we do know
4940b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith        // that the address of a literal will be non-null.
494174f4634781cee06e28eb741bda5d0f936fdd1948Richard Smith        if ((IsLiteralLValue(LHSValue) || IsLiteralLValue(RHSValue)) &&
494274f4634781cee06e28eb741bda5d0f936fdd1948Richard Smith            LHSValue.Base && RHSValue.Base)
4943f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith          return Error(E);
49449e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith        // We can't tell whether weak symbols will end up pointing to the same
49459e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith        // object.
49469e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith        if (IsWeakLValue(LHSValue) || IsWeakLValue(RHSValue))
4947f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith          return Error(E);
49489e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith        // Pointers with different bases cannot represent the same object.
4949c45061bd0c0fdad4df8eea7e9e5af186d11427e5Eli Friedman        // (Note that clang defaults to -fmerge-all-constants, which can
4950c45061bd0c0fdad4df8eea7e9e5af186d11427e5Eli Friedman        // lead to inconsistent results for comparisons involving the address
4951c45061bd0c0fdad4df8eea7e9e5af186d11427e5Eli Friedman        // of a constant; this generally doesn't matter in practice.)
49529e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith        return Success(E->getOpcode() == BO_NE, E);
49535bc86103767c2abcbfdd6518e0ccbbbb6aa59e0fEli Friedman      }
4954a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman
495515efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith      const CharUnits &LHSOffset = LHSValue.getLValueOffset();
495615efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith      const CharUnits &RHSOffset = RHSValue.getLValueOffset();
495715efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith
4958f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator();
4959f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator();
4960f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
49612de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      if (E->getOpcode() == BO_Sub) {
4962f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        // C++11 [expr.add]p6:
4963f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        //   Unless both pointers point to elements of the same array object, or
4964f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        //   one past the last element of the array object, the behavior is
4965f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        //   undefined.
4966f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        if (!LHSDesignator.Invalid && !RHSDesignator.Invalid &&
4967f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith            !AreElementsOfSameArray(getType(LHSValue.Base),
4968f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith                                    LHSDesignator, RHSDesignator))
4969f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          CCEDiag(E, diag::note_constexpr_pointer_subtraction_not_same_array);
4970f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
49714992bdde387c5f033bb450a716eaabc0fda52688Chris Lattner        QualType Type = E->getLHS()->getType();
49724992bdde387c5f033bb450a716eaabc0fda52688Chris Lattner        QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
49733068d117951a8df54bae9db039b56201ab10962bAnders Carlsson
4974180f47959a066795cc0f409433023af448bb0328Richard Smith        CharUnits ElementSize;
497574e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith        if (!HandleSizeof(Info, E->getExprLoc(), ElementType, ElementSize))
4976180f47959a066795cc0f409433023af448bb0328Richard Smith          return false;
4977a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman
497815efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        // FIXME: LLVM and GCC both compute LHSOffset - RHSOffset at runtime,
497915efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        // and produce incorrect results when it overflows. Such behavior
498015efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        // appears to be non-conforming, but is common, so perhaps we should
498115efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        // assume the standard intended for such cases to be undefined behavior
498215efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        // and check for them.
498315efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith
498415efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        // Compute (LHSOffset - RHSOffset) / Size carefully, checking for
498515efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        // overflow in the final conversion to ptrdiff_t.
498615efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        APSInt LHS(
498715efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith          llvm::APInt(65, (int64_t)LHSOffset.getQuantity(), true), false);
498815efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        APSInt RHS(
498915efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith          llvm::APInt(65, (int64_t)RHSOffset.getQuantity(), true), false);
499015efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        APSInt ElemSize(
499115efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith          llvm::APInt(65, (int64_t)ElementSize.getQuantity(), true), false);
499215efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        APSInt TrueResult = (LHS - RHS) / ElemSize;
499315efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        APSInt Result = TrueResult.trunc(Info.Ctx.getIntWidth(E->getType()));
499415efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith
499515efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        if (Result.extend(65) != TrueResult)
499615efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith          HandleOverflow(Info, E, TrueResult, E->getType());
499715efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        return Success(Result, E);
4998ad02d7debd03ff275ac8ea27891a4ecccdb78068Eli Friedman      }
4999625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith
500082f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith      // C++11 [expr.rel]p3:
500182f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith      //   Pointers to void (after pointer conversions) can be compared, with a
500282f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith      //   result defined as follows: If both pointers represent the same
500382f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith      //   address or are both the null pointer value, the result is true if the
500482f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith      //   operator is <= or >= and false otherwise; otherwise the result is
500582f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith      //   unspecified.
500682f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith      // We interpret this as applying to pointers to *cv* void.
500782f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith      if (LHSTy->isVoidPointerType() && LHSOffset != RHSOffset &&
5008f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          E->isRelationalOp())
500982f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith        CCEDiag(E, diag::note_constexpr_void_comparison);
501082f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith
5011f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      // C++11 [expr.rel]p2:
5012f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      // - If two pointers point to non-static data members of the same object,
5013f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      //   or to subobjects or array elements fo such members, recursively, the
5014f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      //   pointer to the later declared member compares greater provided the
5015f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      //   two members have the same access control and provided their class is
5016f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      //   not a union.
5017f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      //   [...]
5018f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      // - Otherwise pointer comparisons are unspecified.
5019f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      if (!LHSDesignator.Invalid && !RHSDesignator.Invalid &&
5020f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          E->isRelationalOp()) {
5021f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        bool WasArrayIndex;
5022f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        unsigned Mismatch =
5023f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          FindDesignatorMismatch(getType(LHSValue.Base), LHSDesignator,
5024f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith                                 RHSDesignator, WasArrayIndex);
5025f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        // At the point where the designators diverge, the comparison has a
5026f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        // specified value if:
5027f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        //  - we are comparing array indices
5028f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        //  - we are comparing fields of a union, or fields with the same access
5029f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        // Otherwise, the result is unspecified and thus the comparison is not a
5030f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        // constant expression.
5031f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        if (!WasArrayIndex && Mismatch < LHSDesignator.Entries.size() &&
5032f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith            Mismatch < RHSDesignator.Entries.size()) {
5033f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          const FieldDecl *LF = getAsField(LHSDesignator.Entries[Mismatch]);
5034f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          const FieldDecl *RF = getAsField(RHSDesignator.Entries[Mismatch]);
5035f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          if (!LF && !RF)
5036f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith            CCEDiag(E, diag::note_constexpr_pointer_comparison_base_classes);
5037f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          else if (!LF)
5038f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith            CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field)
5039f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith              << getAsBaseClass(LHSDesignator.Entries[Mismatch])
5040f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith              << RF->getParent() << RF;
5041f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          else if (!RF)
5042f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith            CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field)
5043f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith              << getAsBaseClass(RHSDesignator.Entries[Mismatch])
5044f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith              << LF->getParent() << LF;
5045f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          else if (!LF->getParent()->isUnion() &&
5046f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith                   LF->getAccess() != RF->getAccess())
5047f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith            CCEDiag(E, diag::note_constexpr_pointer_comparison_differing_access)
5048f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith              << LF << LF->getAccess() << RF << RF->getAccess()
5049f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith              << LF->getParent();
5050f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        }
5051f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      }
5052f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
5053a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      // The comparison here must be unsigned, and performed with the same
5054a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      // width as the pointer.
5055a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      unsigned PtrSize = Info.Ctx.getTypeSize(LHSTy);
5056a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      uint64_t CompareLHS = LHSOffset.getQuantity();
5057a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      uint64_t CompareRHS = RHSOffset.getQuantity();
5058a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      assert(PtrSize <= 64 && "Unexpected pointer width");
5059a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      uint64_t Mask = ~0ULL >> (64 - PtrSize);
5060a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      CompareLHS &= Mask;
5061a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      CompareRHS &= Mask;
5062a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman
50632850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman      // If there is a base and this is a relational operator, we can only
50642850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman      // compare pointers within the object in question; otherwise, the result
50652850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman      // depends on where the object is located in memory.
50662850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman      if (!LHSValue.Base.isNull() && E->isRelationalOp()) {
50672850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman        QualType BaseTy = getType(LHSValue.Base);
50682850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman        if (BaseTy->isIncompleteType())
50692850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman          return Error(E);
50702850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman        CharUnits Size = Info.Ctx.getTypeSizeInChars(BaseTy);
50712850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman        uint64_t OffsetLimit = Size.getQuantity();
50722850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman        if (CompareLHS > OffsetLimit || CompareRHS > OffsetLimit)
50732850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman          return Error(E);
50742850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman      }
50752850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman
5076625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith      switch (E->getOpcode()) {
5077625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith      default: llvm_unreachable("missing comparison operator");
5078a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      case BO_LT: return Success(CompareLHS < CompareRHS, E);
5079a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      case BO_GT: return Success(CompareLHS > CompareRHS, E);
5080a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      case BO_LE: return Success(CompareLHS <= CompareRHS, E);
5081a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      case BO_GE: return Success(CompareLHS >= CompareRHS, E);
5082a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      case BO_EQ: return Success(CompareLHS == CompareRHS, E);
5083a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      case BO_NE: return Success(CompareLHS != CompareRHS, E);
5084ad02d7debd03ff275ac8ea27891a4ecccdb78068Eli Friedman      }
50853068d117951a8df54bae9db039b56201ab10962bAnders Carlsson    }
50863068d117951a8df54bae9db039b56201ab10962bAnders Carlsson  }
5087b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith
5088b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith  if (LHSTy->isMemberPointerType()) {
5089b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    assert(E->isEqualityOp() && "unexpected member pointer operation");
5090b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    assert(RHSTy->isMemberPointerType() && "invalid comparison");
5091b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith
5092b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    MemberPtr LHSValue, RHSValue;
5093b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith
5094b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    bool LHSOK = EvaluateMemberPointer(E->getLHS(), LHSValue, Info);
5095b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    if (!LHSOK && Info.keepEvaluatingAfterFailure())
5096b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith      return false;
5097b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith
5098b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    if (!EvaluateMemberPointer(E->getRHS(), RHSValue, Info) || !LHSOK)
5099b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith      return false;
5100b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith
5101b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    // C++11 [expr.eq]p2:
5102b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    //   If both operands are null, they compare equal. Otherwise if only one is
5103b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    //   null, they compare unequal.
5104b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    if (!LHSValue.getDecl() || !RHSValue.getDecl()) {
5105b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith      bool Equal = !LHSValue.getDecl() && !RHSValue.getDecl();
5106b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith      return Success(E->getOpcode() == BO_EQ ? Equal : !Equal, E);
5107b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    }
5108b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith
5109b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    //   Otherwise if either is a pointer to a virtual member function, the
5110b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    //   result is unspecified.
5111b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(LHSValue.getDecl()))
5112b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith      if (MD->isVirtual())
5113b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith        CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD;
5114b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(RHSValue.getDecl()))
5115b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith      if (MD->isVirtual())
5116b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith        CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD;
5117b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith
5118b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    //   Otherwise they compare equal if and only if they would refer to the
5119b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    //   same member of the same most derived object or the same subobject if
5120b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    //   they were dereferenced with a hypothetical object of the associated
5121b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    //   class type.
5122b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    bool Equal = LHSValue == RHSValue;
5123b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    return Success(E->getOpcode() == BO_EQ ? Equal : !Equal, E);
5124b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith  }
5125b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith
512626f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith  if (LHSTy->isNullPtrType()) {
512726f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith    assert(E->isComparisonOp() && "unexpected nullptr operation");
512826f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith    assert(RHSTy->isNullPtrType() && "missing pointer conversion");
512926f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith    // C++11 [expr.rel]p4, [expr.eq]p3: If two operands of type std::nullptr_t
513026f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith    // are compared, the result is true of the operator is <=, >= or ==, and
513126f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith    // false otherwise.
513226f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith    BinaryOperator::Opcode Opcode = E->getOpcode();
513326f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith    return Success(Opcode == BO_EQ || Opcode == BO_LE || Opcode == BO_GE, E);
513426f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith  }
513526f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith
5136cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  assert((!LHSTy->isIntegralOrEnumerationType() ||
5137cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis          !RHSTy->isIntegralOrEnumerationType()) &&
5138cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis         "DataRecursiveIntBinOpEvaluator should have handled integral types");
5139cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  // We can't continue from here for non-integral types.
5140cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
5141a25ae3d68d84d2b89907f998df6a396549589da5Anders Carlsson}
5142a25ae3d68d84d2b89907f998df6a396549589da5Anders Carlsson
51438b752f10c394b140f9ef89e049cbad1a7676fc25Ken DyckCharUnits IntExprEvaluator::GetAlignOfType(QualType T) {
51445d484e8cf710207010720589d89602233de61d01Sebastian Redl  // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
51455d484e8cf710207010720589d89602233de61d01Sebastian Redl  //   result shall be the alignment of the referenced type."
51465d484e8cf710207010720589d89602233de61d01Sebastian Redl  if (const ReferenceType *Ref = T->getAs<ReferenceType>())
51475d484e8cf710207010720589d89602233de61d01Sebastian Redl    T = Ref->getPointeeType();
51489f1210c3280104417a4ad30f0a00825ac8fa718aChad Rosier
51499f1210c3280104417a4ad30f0a00825ac8fa718aChad Rosier  // __alignof is defined to return the preferred alignment.
51509f1210c3280104417a4ad30f0a00825ac8fa718aChad Rosier  return Info.Ctx.toCharUnitsFromBits(
51519f1210c3280104417a4ad30f0a00825ac8fa718aChad Rosier    Info.Ctx.getPreferredTypeAlign(T.getTypePtr()));
5152e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner}
5153e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner
51548b752f10c394b140f9ef89e049cbad1a7676fc25Ken DyckCharUnits IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
5155af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  E = E->IgnoreParens();
5156af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner
5157af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  // alignof decl is always accepted, even if it doesn't make sense: we default
51581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // to 1 in those cases.
5159af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
51608b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck    return Info.Ctx.getDeclAlign(DRE->getDecl(),
51618b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck                                 /*RefAsPointee*/true);
5162a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman
5163af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
51648b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck    return Info.Ctx.getDeclAlign(ME->getMemberDecl(),
51658b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck                                 /*RefAsPointee*/true);
5166af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner
5167e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner  return GetAlignOfType(E->getType());
5168e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner}
5169e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner
5170e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner
5171f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne/// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with
5172f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne/// a result as the expression's type.
5173f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbournebool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr(
5174f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne                                    const UnaryExprOrTypeTraitExpr *E) {
5175f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  switch(E->getKind()) {
5176f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  case UETT_AlignOf: {
5177e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner    if (E->isArgumentType())
51784f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck      return Success(GetAlignOfType(E->getArgumentType()), E);
5179e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner    else
51804f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck      return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
5181e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner  }
5182a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman
5183f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  case UETT_VecStep: {
5184f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    QualType Ty = E->getTypeOfArgument();
51850518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl
5186f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    if (Ty->isVectorType()) {
5187890f0f1612b0028b8b16730ae7ed07e295600c76Ted Kremenek      unsigned n = Ty->castAs<VectorType>()->getNumElements();
5188a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman
5189f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne      // The vec_step built-in functions that take a 3-component
5190f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne      // vector return 4. (OpenCL 1.1 spec 6.11.12)
5191f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne      if (n == 3)
5192f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne        n = 4;
5193f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne
5194f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne      return Success(n, E);
5195f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    } else
5196f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne      return Success(1, E);
5197f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  }
5198f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne
5199f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  case UETT_SizeOf: {
5200f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    QualType SrcTy = E->getTypeOfArgument();
5201f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
5202f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    //   the result is the size of the referenced type."
5203f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>())
5204f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne      SrcTy = Ref->getPointeeType();
5205f2da9dfef96dc11b7b5effb1d02cb427b2d71599Eli Friedman
5206180f47959a066795cc0f409433023af448bb0328Richard Smith    CharUnits Sizeof;
520774e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    if (!HandleSizeof(Info, E->getExprLoc(), SrcTy, Sizeof))
5208f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne      return false;
5209180f47959a066795cc0f409433023af448bb0328Richard Smith    return Success(Sizeof, E);
5210f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  }
5211f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  }
5212f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne
5213f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  llvm_unreachable("unknown expr/type trait");
5214fcee0019b76f9f368f2b3d6d4048a98232593f29Chris Lattner}
5215fcee0019b76f9f368f2b3d6d4048a98232593f29Chris Lattner
52168cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) {
52178ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  CharUnits Result;
52188cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  unsigned n = OOE->getNumComponents();
52198ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  if (n == 0)
5220f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(OOE);
52218cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  QualType CurrentType = OOE->getTypeSourceInfo()->getType();
52228ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  for (unsigned i = 0; i != n; ++i) {
52238ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    OffsetOfExpr::OffsetOfNode ON = OOE->getComponent(i);
52248ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    switch (ON.getKind()) {
52258ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    case OffsetOfExpr::OffsetOfNode::Array: {
52268cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne      const Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex());
52278ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      APSInt IdxResult;
52288ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      if (!EvaluateInteger(Idx, IdxResult, Info))
52298ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor        return false;
52308ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType);
52318ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      if (!AT)
5232f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(OOE);
52338ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      CurrentType = AT->getElementType();
52348ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType);
52358ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      Result += IdxResult.getSExtValue() * ElementSize;
52368ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor        break;
52378ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    }
5238f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
52398ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    case OffsetOfExpr::OffsetOfNode::Field: {
52408ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      FieldDecl *MemberDecl = ON.getField();
52418ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      const RecordType *RT = CurrentType->getAs<RecordType>();
5242f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      if (!RT)
5243f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(OOE);
52448ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      RecordDecl *RD = RT->getDecl();
52458d59deec807ed53efcd07855199cdc9c979f447fJohn McCall      if (RD->isInvalidDecl()) return false;
52468ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
5247ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      unsigned i = MemberDecl->getFieldIndex();
5248cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      assert(i < RL.getFieldCount() && "offsetof field in wrong type");
5249fb1e3bc29b667f4275e1d5a43d64ec173f4f9a7dKen Dyck      Result += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i));
52508ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      CurrentType = MemberDecl->getType().getNonReferenceType();
52518ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      break;
52528ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    }
5253f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
52548ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    case OffsetOfExpr::OffsetOfNode::Identifier:
52558ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      llvm_unreachable("dependent __builtin_offsetof");
5256f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
5257cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor    case OffsetOfExpr::OffsetOfNode::Base: {
5258cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      CXXBaseSpecifier *BaseSpec = ON.getBase();
5259cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      if (BaseSpec->isVirtual())
5260f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(OOE);
5261cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor
5262cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      // Find the layout of the class whose base we are looking into.
5263cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      const RecordType *RT = CurrentType->getAs<RecordType>();
5264f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      if (!RT)
5265f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(OOE);
5266cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      RecordDecl *RD = RT->getDecl();
52678d59deec807ed53efcd07855199cdc9c979f447fJohn McCall      if (RD->isInvalidDecl()) return false;
5268cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
5269cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor
5270cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      // Find the base class itself.
5271cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      CurrentType = BaseSpec->getType();
5272cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      const RecordType *BaseRT = CurrentType->getAs<RecordType>();
5273cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      if (!BaseRT)
5274f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(OOE);
5275cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor
5276cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      // Add the offset to the base.
52777c7f820d70c925b29290a8563b59615816a827fcKen Dyck      Result += RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl()));
5278cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      break;
5279cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor    }
52808ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    }
52818ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  }
52828cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  return Success(Result, OOE);
52838ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor}
52848ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor
5285b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattnerbool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
528675a4881047deeb3a300ff9293dc6ba8570048bb5Chris Lattner  switch (E->getOpcode()) {
52874c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner  default:
528875a4881047deeb3a300ff9293dc6ba8570048bb5Chris Lattner    // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
528975a4881047deeb3a300ff9293dc6ba8570048bb5Chris Lattner    // See C99 6.6p3.
5290f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
52912de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Extension:
52924c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner    // FIXME: Should extension allow i-c-e extension expressions in its scope?
52934c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner    // If so, we could clear the diagnostic ID.
5294f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Visit(E->getSubExpr());
52952de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Plus:
5296c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    // The result is just the value.
5297f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Visit(E->getSubExpr());
5298f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  case UO_Minus: {
5299f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!Visit(E->getSubExpr()))
5300f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return false;
5301f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!Result.isInt()) return Error(E);
5302789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    const APSInt &Value = Result.getInt();
5303789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    if (Value.isSigned() && Value.isMinSignedValue())
5304789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith      HandleOverflow(Info, E, -Value.extend(Value.getBitWidth() + 1),
5305789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith                     E->getType());
5306789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    return Success(-Value, E);
5307f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
5308f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  case UO_Not: {
5309f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!Visit(E->getSubExpr()))
5310f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return false;
5311f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!Result.isInt()) return Error(E);
5312f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Success(~Result.getInt(), E);
5313f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
5314f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  case UO_LNot: {
5315f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    bool bres;
5316f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!EvaluateAsBooleanCondition(E->getSubExpr(), bres, Info))
5317f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return false;
5318f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Success(!bres, E);
5319f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
532006a3675627e3b3c47b49c689c8e404a33144194aAnders Carlsson  }
5321a25ae3d68d84d2b89907f998df6a396549589da5Anders Carlsson}
53221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5323732b2236ae4a4f11e7642677cebbd169c07ea877Chris Lattner/// HandleCast - This is used to evaluate implicit or explicit casts where the
5324732b2236ae4a4f11e7642677cebbd169c07ea877Chris Lattner/// result type is integer.
53258cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool IntExprEvaluator::VisitCastExpr(const CastExpr *E) {
53268cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  const Expr *SubExpr = E->getSubExpr();
532782206e267ce6cc709797127616f64672d255b310Anders Carlsson  QualType DestType = E->getType();
5328b92dac8bc2f6f73919825f9af693a8a7e89ae1d4Daniel Dunbar  QualType SrcType = SubExpr->getType();
532982206e267ce6cc709797127616f64672d255b310Anders Carlsson
533046a523285928aa07bf14803178dc04616ac85994Eli Friedman  switch (E->getCastKind()) {
533146a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_BaseToDerived:
533246a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_DerivedToBase:
533346a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_UncheckedDerivedToBase:
533446a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_Dynamic:
533546a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_ToUnion:
533646a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_ArrayToPointerDecay:
533746a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_FunctionToPointerDecay:
533846a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_NullToPointer:
533946a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_NullToMemberPointer:
534046a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_BaseToDerivedMemberPointer:
534146a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_DerivedToBaseMemberPointer:
53424d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  case CK_ReinterpretMemberPointer:
534346a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_ConstructorConversion:
534446a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_IntegralToPointer:
534546a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_ToVoid:
534646a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_VectorSplat:
534746a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_IntegralToFloating:
534846a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_FloatingCast:
53491d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_CPointerToObjCPointerCast:
53501d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_BlockPointerToObjCPointerCast:
535146a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_AnyPointerToBlockPointerCast:
535246a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_ObjCObjectLValueCast:
535346a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_FloatingRealToComplex:
535446a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_FloatingComplexToReal:
535546a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_FloatingComplexCast:
535646a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_FloatingComplexToIntegralComplex:
535746a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_IntegralRealToComplex:
535846a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_IntegralComplexCast:
535946a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_IntegralComplexToFloatingComplex:
536046a523285928aa07bf14803178dc04616ac85994Eli Friedman    llvm_unreachable("invalid cast kind for integral value");
536146a523285928aa07bf14803178dc04616ac85994Eli Friedman
5362e50c297f92914ca996deb8b597624193273b62e4Eli Friedman  case CK_BitCast:
536346a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_Dependent:
536446a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_LValueBitCast:
536533e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCProduceObject:
536633e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCConsumeObject:
536733e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCReclaimReturnedObject:
536833e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCExtendBlockObject:
5369ac1303eca6cbe3e623fb5ec6fe7ec184ef4b0dfaDouglas Gregor  case CK_CopyAndAutoreleaseBlockObject:
5370f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
537146a523285928aa07bf14803178dc04616ac85994Eli Friedman
53727d580a4e9e47dffc3c17aa2b957ac57ca3c4e451Richard Smith  case CK_UserDefinedConversion:
537346a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_LValueToRValue:
53747a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall  case CK_AtomicToNonAtomic:
53757a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall  case CK_NonAtomicToAtomic:
537646a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_NoOp:
5377c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return ExprEvaluatorBaseTy::VisitCastExpr(E);
537846a523285928aa07bf14803178dc04616ac85994Eli Friedman
537946a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_MemberPointerToBoolean:
538046a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_PointerToBoolean:
538146a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_IntegralToBoolean:
538246a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_FloatingToBoolean:
538346a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_FloatingComplexToBoolean:
538446a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_IntegralComplexToBoolean: {
53854efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman    bool BoolResult;
5386c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    if (!EvaluateAsBooleanCondition(SubExpr, BoolResult, Info))
53874efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman      return false;
5388131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar    return Success(BoolResult, E);
53894efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman  }
53904efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
539146a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_IntegralCast: {
5392732b2236ae4a4f11e7642677cebbd169c07ea877Chris Lattner    if (!Visit(SubExpr))
5393b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattner      return false;
5394a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar
5395be26570e3faa009bdcefedfaf04473e518940520Eli Friedman    if (!Result.isInt()) {
539665639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman      // Allow casts of address-of-label differences if they are no-ops
539765639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman      // or narrowing.  (The narrowing case isn't actually guaranteed to
539865639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman      // be constant-evaluatable except in some narrow cases which are hard
539965639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman      // to detect here.  We let it through on the assumption the user knows
540065639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman      // what they are doing.)
540165639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman      if (Result.isAddrLabelDiff())
540265639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman        return Info.Ctx.getTypeSize(DestType) <= Info.Ctx.getTypeSize(SrcType);
5403be26570e3faa009bdcefedfaf04473e518940520Eli Friedman      // Only allow casts of lvalues if they are lossless.
5404be26570e3faa009bdcefedfaf04473e518940520Eli Friedman      return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
5405be26570e3faa009bdcefedfaf04473e518940520Eli Friedman    }
540630c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar
5407f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith    return Success(HandleIntToIntCast(Info, E, DestType, SrcType,
5408f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith                                      Result.getInt()), E);
5409732b2236ae4a4f11e7642677cebbd169c07ea877Chris Lattner  }
54101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
541146a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_PointerToIntegral: {
5412c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith    CCEDiag(E, diag::note_constexpr_invalid_cast) << 2;
5413c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith
5414efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    LValue LV;
541587eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner    if (!EvaluatePointer(SubExpr, LV, Info))
5416b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattner      return false;
54174efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
5418dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar    if (LV.getLValueBase()) {
5419dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar      // Only allow based lvalue casts if they are lossless.
5420f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith      // FIXME: Allow a larger integer size than the pointer size, and allow
5421f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith      // narrowing back down to pointer width in subsequent integral casts.
5422f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith      // FIXME: Check integer type's active bits, not its type size.
5423dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar      if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
5424f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(E);
5425dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar
5426b755a9da095d2f2f04444797f1e1a9511693815bRichard Smith      LV.Designator.setInvalid();
5427efdb83e26f9a1fd2566afe54461216cd84814d42John McCall      LV.moveInto(Result);
5428dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar      return true;
5429dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar    }
54304efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
5431a73058324197b7bdfd19307965954f626e26199dKen Dyck    APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(),
5432a73058324197b7bdfd19307965954f626e26199dKen Dyck                                         SrcType);
5433f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith    return Success(HandleIntToIntCast(Info, E, DestType, SrcType, AsInt), E);
54342bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson  }
54354efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
543646a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_IntegralComplexToReal: {
5437f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    ComplexValue C;
54381725f683432715e5afe34d476024bd6f16eac3fcEli Friedman    if (!EvaluateComplex(SubExpr, C, Info))
54391725f683432715e5afe34d476024bd6f16eac3fcEli Friedman      return false;
544046a523285928aa07bf14803178dc04616ac85994Eli Friedman    return Success(C.getComplexIntReal(), E);
54411725f683432715e5afe34d476024bd6f16eac3fcEli Friedman  }
54422217c87bdc5ab357046a5453bdb06f469c41024eEli Friedman
544346a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_FloatingToIntegral: {
544446a523285928aa07bf14803178dc04616ac85994Eli Friedman    APFloat F(0.0);
544546a523285928aa07bf14803178dc04616ac85994Eli Friedman    if (!EvaluateFloat(SubExpr, F, Info))
544646a523285928aa07bf14803178dc04616ac85994Eli Friedman      return false;
5447732b2236ae4a4f11e7642677cebbd169c07ea877Chris Lattner
5448c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    APSInt Value;
5449c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    if (!HandleFloatToIntCast(Info, E, SrcType, F, DestType, Value))
5450c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      return false;
5451c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return Success(Value, E);
545246a523285928aa07bf14803178dc04616ac85994Eli Friedman  }
545346a523285928aa07bf14803178dc04616ac85994Eli Friedman  }
54541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
545546a523285928aa07bf14803178dc04616ac85994Eli Friedman  llvm_unreachable("unknown cast resulting in integral value");
5456a25ae3d68d84d2b89907f998df6a396549589da5Anders Carlsson}
54572bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson
5458722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedmanbool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
5459722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman  if (E->getSubExpr()->getType()->isAnyComplexType()) {
5460f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    ComplexValue LV;
5461f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!EvaluateComplex(E->getSubExpr(), LV, Info))
5462f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return false;
5463f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!LV.isComplexInt())
5464f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E);
5465722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman    return Success(LV.getComplexIntReal(), E);
5466722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman  }
5467722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman
5468722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman  return Visit(E->getSubExpr());
5469722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman}
5470722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman
5471664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedmanbool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
5472722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman  if (E->getSubExpr()->getType()->isComplexIntegerType()) {
5473f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    ComplexValue LV;
5474f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!EvaluateComplex(E->getSubExpr(), LV, Info))
5475f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return false;
5476f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!LV.isComplexInt())
5477f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E);
5478722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman    return Success(LV.getComplexIntImag(), E);
5479722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman  }
5480722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman
54818327fad71da34492d82c532f42a58cb4baff81a3Richard Smith  VisitIgnoredValue(E->getSubExpr());
5482664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman  return Success(0, E);
5483664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman}
5484664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman
5485ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregorbool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) {
5486ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor  return Success(E->getPackLength(), E);
5487ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor}
5488ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor
5489295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redlbool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) {
5490295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl  return Success(E->getValue(), E);
5491295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl}
5492295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl
5493f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===//
5494d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman// Float Evaluation
5495d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman//===----------------------------------------------------------------------===//
5496d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman
5497d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmannamespace {
5498770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass FloatExprEvaluator
54998cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  : public ExprEvaluatorBase<FloatExprEvaluator, bool> {
5500d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman  APFloat &Result;
5501d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmanpublic:
5502d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman  FloatExprEvaluator(EvalInfo &info, APFloat &result)
55038cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    : ExprEvaluatorBaseTy(info), Result(result) {}
5504d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman
55051aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  bool Success(const APValue &V, const Expr *e) {
55068cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    Result = V.getFloat();
55078cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    return true;
55088cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  }
5509d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman
551051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  bool ZeroInitialization(const Expr *E) {
5511f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith    Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
5512f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith    return true;
5513f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  }
5514f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith
5515019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  bool VisitCallExpr(const CallExpr *E);
5516d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman
55175db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar  bool VisitUnaryOperator(const UnaryOperator *E);
5518d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman  bool VisitBinaryOperator(const BinaryOperator *E);
5519d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman  bool VisitFloatingLiteral(const FloatingLiteral *E);
55208cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCastExpr(const CastExpr *E);
55212217c87bdc5ab357046a5453bdb06f469c41024eEli Friedman
5522abd3a857ace59100305790545d1baae5877b8945John McCall  bool VisitUnaryReal(const UnaryOperator *E);
5523abd3a857ace59100305790545d1baae5877b8945John McCall  bool VisitUnaryImag(const UnaryOperator *E);
5524ba98d6bb414861965a1f22628494ea046785ecd4Eli Friedman
552551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  // FIXME: Missing: array subscript of vector, member of vector
5526d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman};
5527d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman} // end anonymous namespace
5528d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman
5529d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmanstatic bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
5530c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  assert(E->isRValue() && E->getType()->isRealFloatingType());
55318cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  return FloatExprEvaluator(Info, Result).Visit(E);
5532d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman}
5533d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman
55344ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadstatic bool TryEvaluateBuiltinNaN(const ASTContext &Context,
5535db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall                                  QualType ResultTy,
5536db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall                                  const Expr *Arg,
5537db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall                                  bool SNaN,
5538db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall                                  llvm::APFloat &Result) {
5539db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts());
5540db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  if (!S) return false;
5541db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall
5542db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy);
5543db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall
5544db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  llvm::APInt fill;
5545db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall
5546db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  // Treat empty strings as if they were zero.
5547db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  if (S->getString().empty())
5548db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall    fill = llvm::APInt(32, 0);
5549db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  else if (S->getString().getAsInteger(0, fill))
5550db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall    return false;
5551db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall
5552db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  if (SNaN)
5553db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall    Result = llvm::APFloat::getSNaN(Sem, false, &fill);
5554db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  else
5555db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall    Result = llvm::APFloat::getQNaN(Sem, false, &fill);
5556db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  return true;
5557db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall}
5558db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall
5559019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattnerbool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
5560180f47959a066795cc0f409433023af448bb0328Richard Smith  switch (E->isBuiltinCall()) {
55618cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  default:
55628cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    return ExprEvaluatorBaseTy::VisitCallExpr(E);
55638cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
5564019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  case Builtin::BI__builtin_huge_val:
5565019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  case Builtin::BI__builtin_huge_valf:
5566019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  case Builtin::BI__builtin_huge_vall:
5567019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  case Builtin::BI__builtin_inf:
5568019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  case Builtin::BI__builtin_inff:
55697cbed03c00e246682e5292785d01e1c120ce54bdDaniel Dunbar  case Builtin::BI__builtin_infl: {
55707cbed03c00e246682e5292785d01e1c120ce54bdDaniel Dunbar    const llvm::fltSemantics &Sem =
55717cbed03c00e246682e5292785d01e1c120ce54bdDaniel Dunbar      Info.Ctx.getFloatTypeSemantics(E->getType());
557234a74ab81600a40c6324fd76adb724b803dfaf91Chris Lattner    Result = llvm::APFloat::getInf(Sem);
557334a74ab81600a40c6324fd76adb724b803dfaf91Chris Lattner    return true;
55747cbed03c00e246682e5292785d01e1c120ce54bdDaniel Dunbar  }
55751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5576db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  case Builtin::BI__builtin_nans:
5577db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  case Builtin::BI__builtin_nansf:
5578db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  case Builtin::BI__builtin_nansl:
5579f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
5580f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                               true, Result))
5581f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E);
5582f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return true;
5583db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall
55849e62171a25e3a08fb5c49fb370f83faf5ae786f5Chris Lattner  case Builtin::BI__builtin_nan:
55859e62171a25e3a08fb5c49fb370f83faf5ae786f5Chris Lattner  case Builtin::BI__builtin_nanf:
55869e62171a25e3a08fb5c49fb370f83faf5ae786f5Chris Lattner  case Builtin::BI__builtin_nanl:
55874572baba9d18c275968ac113fd73b0e3c77cccb8Mike Stump    // If this is __builtin_nan() turn this into a nan, otherwise we
55889e62171a25e3a08fb5c49fb370f83faf5ae786f5Chris Lattner    // can't constant fold it.
5589f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
5590f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                               false, Result))
5591f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E);
5592f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return true;
55935db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar
55945db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar  case Builtin::BI__builtin_fabs:
55955db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar  case Builtin::BI__builtin_fabsf:
55965db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar  case Builtin::BI__builtin_fabsl:
55975db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar    if (!EvaluateFloat(E->getArg(0), Result, Info))
55985db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar      return false;
55991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
56005db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar    if (Result.isNegative())
56015db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar      Result.changeSign();
56025db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar    return true;
56035db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar
56041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  case Builtin::BI__builtin_copysign:
56051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  case Builtin::BI__builtin_copysignf:
56065db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar  case Builtin::BI__builtin_copysignl: {
56075db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar    APFloat RHS(0.);
56085db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar    if (!EvaluateFloat(E->getArg(0), Result, Info) ||
56095db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar        !EvaluateFloat(E->getArg(1), RHS, Info))
56105db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar      return false;
56115db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar    Result.copySign(RHS);
56125db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar    return true;
56135db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar  }
5614019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  }
5615019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner}
5616019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner
5617abd3a857ace59100305790545d1baae5877b8945John McCallbool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
561843efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman  if (E->getSubExpr()->getType()->isAnyComplexType()) {
561943efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman    ComplexValue CV;
562043efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman    if (!EvaluateComplex(E->getSubExpr(), CV, Info))
562143efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman      return false;
562243efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman    Result = CV.FloatReal;
562343efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman    return true;
562443efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman  }
562543efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman
562643efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman  return Visit(E->getSubExpr());
5627abd3a857ace59100305790545d1baae5877b8945John McCall}
5628abd3a857ace59100305790545d1baae5877b8945John McCall
5629abd3a857ace59100305790545d1baae5877b8945John McCallbool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
563043efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman  if (E->getSubExpr()->getType()->isAnyComplexType()) {
563143efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman    ComplexValue CV;
563243efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman    if (!EvaluateComplex(E->getSubExpr(), CV, Info))
563343efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman      return false;
563443efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman    Result = CV.FloatImag;
563543efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman    return true;
563643efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman  }
563743efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman
56388327fad71da34492d82c532f42a58cb4baff81a3Richard Smith  VisitIgnoredValue(E->getSubExpr());
563943efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman  const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(E->getType());
564043efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman  Result = llvm::APFloat::getZero(Sem);
5641abd3a857ace59100305790545d1baae5877b8945John McCall  return true;
5642abd3a857ace59100305790545d1baae5877b8945John McCall}
5643abd3a857ace59100305790545d1baae5877b8945John McCall
56445db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbarbool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
56455db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar  switch (E->getOpcode()) {
5646f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  default: return Error(E);
56472de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Plus:
56487993e8a2a26bf408c2a6d45f24fffa12db336b2bRichard Smith    return EvaluateFloat(E->getSubExpr(), Result, Info);
56492de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Minus:
56507993e8a2a26bf408c2a6d45f24fffa12db336b2bRichard Smith    if (!EvaluateFloat(E->getSubExpr(), Result, Info))
56517993e8a2a26bf408c2a6d45f24fffa12db336b2bRichard Smith      return false;
56525db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar    Result.changeSign();
56535db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar    return true;
56545db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar  }
56555db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar}
5656019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner
5657d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmanbool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
5658e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma)
5659e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
566096e93660124c8028a4c3bcc038ab0cdd18cd7ab2Anders Carlsson
56615db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar  APFloat RHS(0.0);
5662745f5147e065900267c85a5568785a1991d4838fRichard Smith  bool LHSOK = EvaluateFloat(E->getLHS(), Result, Info);
5663745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!LHSOK && !Info.keepEvaluatingAfterFailure())
5664d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman    return false;
5665745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!EvaluateFloat(E->getRHS(), RHS, Info) || !LHSOK)
5666d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman    return false;
5667d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman
5668d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman  switch (E->getOpcode()) {
5669f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  default: return Error(E);
56702de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Mul:
5671d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman    Result.multiply(RHS, APFloat::rmNearestTiesToEven);
56727b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    break;
56732de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Add:
5674d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman    Result.add(RHS, APFloat::rmNearestTiesToEven);
56757b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    break;
56762de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Sub:
5677d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman    Result.subtract(RHS, APFloat::rmNearestTiesToEven);
56787b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    break;
56792de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Div:
5680d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman    Result.divide(RHS, APFloat::rmNearestTiesToEven);
56817b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    break;
5682d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman  }
56837b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith
56847b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith  if (Result.isInfinity() || Result.isNaN())
56857b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    CCEDiag(E, diag::note_constexpr_float_arithmetic) << Result.isNaN();
56867b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith  return true;
5687d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman}
5688d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman
5689d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmanbool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
5690d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman  Result = E->getValue();
5691d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman  return true;
5692d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman}
5693d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman
56948cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) {
56958cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  const Expr* SubExpr = E->getSubExpr();
56961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
56972a523eec6a31955be876625819b89e8dc5def707Eli Friedman  switch (E->getCastKind()) {
56982a523eec6a31955be876625819b89e8dc5def707Eli Friedman  default:
5699c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return ExprEvaluatorBaseTy::VisitCastExpr(E);
57002a523eec6a31955be876625819b89e8dc5def707Eli Friedman
57012a523eec6a31955be876625819b89e8dc5def707Eli Friedman  case CK_IntegralToFloating: {
57024efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman    APSInt IntResult;
5703c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return EvaluateInteger(SubExpr, IntResult, Info) &&
5704c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith           HandleIntToFloatCast(Info, E, SubExpr->getType(), IntResult,
5705c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                E->getType(), Result);
57064efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman  }
57072a523eec6a31955be876625819b89e8dc5def707Eli Friedman
57082a523eec6a31955be876625819b89e8dc5def707Eli Friedman  case CK_FloatingCast: {
57094efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman    if (!Visit(SubExpr))
57104efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman      return false;
5711c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return HandleFloatToFloatCast(Info, E, SubExpr->getType(), E->getType(),
5712c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                  Result);
57134efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman  }
5714f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall
57152a523eec6a31955be876625819b89e8dc5def707Eli Friedman  case CK_FloatingComplexToReal: {
5716f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    ComplexValue V;
5717f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    if (!EvaluateComplex(SubExpr, V, Info))
5718f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall      return false;
5719f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    Result = V.getComplexFloatReal();
5720f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    return true;
5721f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall  }
57222a523eec6a31955be876625819b89e8dc5def707Eli Friedman  }
57234efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman}
57244efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
5725d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman//===----------------------------------------------------------------------===//
5726a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar// Complex Evaluation (for float and integer)
57279ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson//===----------------------------------------------------------------------===//
57289ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson
57299ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlssonnamespace {
5730770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass ComplexExprEvaluator
57318cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  : public ExprEvaluatorBase<ComplexExprEvaluator, bool> {
5732f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall  ComplexValue &Result;
57331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
57349ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlssonpublic:
5735f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall  ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result)
57368cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    : ExprEvaluatorBaseTy(info), Result(Result) {}
57371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
57381aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  bool Success(const APValue &V, const Expr *e) {
57398cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    Result.setFrom(V);
57408cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    return true;
57418cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  }
57421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
57437ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  bool ZeroInitialization(const Expr *E);
57447ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman
57458cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  //===--------------------------------------------------------------------===//
57468cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  //                            Visitor Methods
57478cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  //===--------------------------------------------------------------------===//
57489ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson
57498cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitImaginaryLiteral(const ImaginaryLiteral *E);
57508cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCastExpr(const CastExpr *E);
5751b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman  bool VisitBinaryOperator(const BinaryOperator *E);
575296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  bool VisitUnaryOperator(const UnaryOperator *E);
57537ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  bool VisitInitListExpr(const InitListExpr *E);
5754b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman};
5755b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman} // end anonymous namespace
57561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5757b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedmanstatic bool EvaluateComplex(const Expr *E, ComplexValue &Result,
5758b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman                            EvalInfo &Info) {
5759c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  assert(E->isRValue() && E->getType()->isAnyComplexType());
57608cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  return ComplexExprEvaluator(Info, Result).Visit(E);
5761b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman}
5762b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman
57637ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedmanbool ComplexExprEvaluator::ZeroInitialization(const Expr *E) {
5764890f0f1612b0028b8b16730ae7ed07e295600c76Ted Kremenek  QualType ElemTy = E->getType()->castAs<ComplexType>()->getElementType();
57657ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  if (ElemTy->isRealFloatingType()) {
57667ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    Result.makeComplexFloat();
57677ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    APFloat Zero = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(ElemTy));
57687ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    Result.FloatReal = Zero;
57697ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    Result.FloatImag = Zero;
57707ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  } else {
57717ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    Result.makeComplexInt();
57727ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    APSInt Zero = Info.Ctx.MakeIntValue(0, ElemTy);
57737ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    Result.IntReal = Zero;
57747ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    Result.IntImag = Zero;
57757ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  }
57767ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  return true;
57777ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman}
57787ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman
57798cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) {
57808cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  const Expr* SubExpr = E->getSubExpr();
5781b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman
5782b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman  if (SubExpr->getType()->isRealFloatingType()) {
5783b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    Result.makeComplexFloat();
5784b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    APFloat &Imag = Result.FloatImag;
5785b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    if (!EvaluateFloat(SubExpr, Imag, Info))
5786b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman      return false;
5787b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman
5788b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    Result.FloatReal = APFloat(Imag.getSemantics());
5789b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    return true;
5790b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman  } else {
5791b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    assert(SubExpr->getType()->isIntegerType() &&
5792b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman           "Unexpected imaginary literal.");
5793b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman
5794b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    Result.makeComplexInt();
5795b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    APSInt &Imag = Result.IntImag;
5796b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    if (!EvaluateInteger(SubExpr, Imag, Info))
5797b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman      return false;
5798b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman
5799b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned());
5800b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    return true;
5801b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman  }
5802b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman}
5803b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman
58048cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) {
5805b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman
58068786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  switch (E->getCastKind()) {
58078786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_BitCast:
58088786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_BaseToDerived:
58098786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_DerivedToBase:
58108786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_UncheckedDerivedToBase:
58118786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_Dynamic:
58128786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_ToUnion:
58138786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_ArrayToPointerDecay:
58148786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_FunctionToPointerDecay:
58158786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_NullToPointer:
58168786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_NullToMemberPointer:
58178786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_BaseToDerivedMemberPointer:
58188786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_DerivedToBaseMemberPointer:
58198786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_MemberPointerToBoolean:
58204d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  case CK_ReinterpretMemberPointer:
58218786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_ConstructorConversion:
58228786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_IntegralToPointer:
58238786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_PointerToIntegral:
58248786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_PointerToBoolean:
58258786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_ToVoid:
58268786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_VectorSplat:
58278786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_IntegralCast:
58288786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_IntegralToBoolean:
58298786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_IntegralToFloating:
58308786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_FloatingToIntegral:
58318786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_FloatingToBoolean:
58328786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_FloatingCast:
58331d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_CPointerToObjCPointerCast:
58341d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_BlockPointerToObjCPointerCast:
58358786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_AnyPointerToBlockPointerCast:
58368786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_ObjCObjectLValueCast:
58378786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_FloatingComplexToReal:
58388786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_FloatingComplexToBoolean:
58398786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_IntegralComplexToReal:
58408786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_IntegralComplexToBoolean:
584133e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCProduceObject:
584233e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCConsumeObject:
584333e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCReclaimReturnedObject:
584433e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCExtendBlockObject:
5845ac1303eca6cbe3e623fb5ec6fe7ec184ef4b0dfaDouglas Gregor  case CK_CopyAndAutoreleaseBlockObject:
58468786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    llvm_unreachable("invalid cast kind for complex value");
58478786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
58488786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_LValueToRValue:
58497a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall  case CK_AtomicToNonAtomic:
58507a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall  case CK_NonAtomicToAtomic:
58518786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_NoOp:
5852c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return ExprEvaluatorBaseTy::VisitCastExpr(E);
58532bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall
58548786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_Dependent:
585546a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_LValueBitCast:
58568786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_UserDefinedConversion:
5857f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
58588786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
58598786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_FloatingRealToComplex: {
5860b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    APFloat &Real = Result.FloatReal;
58618786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    if (!EvaluateFloat(E->getSubExpr(), Real, Info))
5862b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman      return false;
5863b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman
58648786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    Result.makeComplexFloat();
58658786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    Result.FloatImag = APFloat(Real.getSemantics());
58668786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    return true;
58678786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  }
58688786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
58698786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_FloatingComplexCast: {
58708786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    if (!Visit(E->getSubExpr()))
58718786da77984e81d48e0e1b2bd339809b1efc19f3John McCall      return false;
58728786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
58738786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    QualType To = E->getType()->getAs<ComplexType>()->getElementType();
58748786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    QualType From
58758786da77984e81d48e0e1b2bd339809b1efc19f3John McCall      = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType();
58768786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
5877c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return HandleFloatToFloatCast(Info, E, From, To, Result.FloatReal) &&
5878c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith           HandleFloatToFloatCast(Info, E, From, To, Result.FloatImag);
58798786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  }
58808786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
58818786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_FloatingComplexToIntegralComplex: {
58828786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    if (!Visit(E->getSubExpr()))
58838786da77984e81d48e0e1b2bd339809b1efc19f3John McCall      return false;
58848786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
58858786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    QualType To = E->getType()->getAs<ComplexType>()->getElementType();
58868786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    QualType From
58878786da77984e81d48e0e1b2bd339809b1efc19f3John McCall      = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType();
58888786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    Result.makeComplexInt();
5889c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return HandleFloatToIntCast(Info, E, From, Result.FloatReal,
5890c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                To, Result.IntReal) &&
5891c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith           HandleFloatToIntCast(Info, E, From, Result.FloatImag,
5892c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                To, Result.IntImag);
58938786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  }
58948786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
58958786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_IntegralRealToComplex: {
5896b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    APSInt &Real = Result.IntReal;
58978786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    if (!EvaluateInteger(E->getSubExpr(), Real, Info))
5898b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman      return false;
58999ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson
59008786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    Result.makeComplexInt();
59018786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned());
59028786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    return true;
59038786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  }
59048786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
59058786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_IntegralComplexCast: {
59068786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    if (!Visit(E->getSubExpr()))
5907b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman      return false;
5908ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson
59098786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    QualType To = E->getType()->getAs<ComplexType>()->getElementType();
59108786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    QualType From
59118786da77984e81d48e0e1b2bd339809b1efc19f3John McCall      = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType();
59121725f683432715e5afe34d476024bd6f16eac3fcEli Friedman
5913f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith    Result.IntReal = HandleIntToIntCast(Info, E, To, From, Result.IntReal);
5914f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith    Result.IntImag = HandleIntToIntCast(Info, E, To, From, Result.IntImag);
59158786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    return true;
59168786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  }
59178786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
59188786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_IntegralComplexToFloatingComplex: {
59198786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    if (!Visit(E->getSubExpr()))
59208786da77984e81d48e0e1b2bd339809b1efc19f3John McCall      return false;
59218786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
5922890f0f1612b0028b8b16730ae7ed07e295600c76Ted Kremenek    QualType To = E->getType()->castAs<ComplexType>()->getElementType();
59238786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    QualType From
5924890f0f1612b0028b8b16730ae7ed07e295600c76Ted Kremenek      = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType();
59258786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    Result.makeComplexFloat();
5926c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return HandleIntToFloatCast(Info, E, From, Result.IntReal,
5927c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                To, Result.FloatReal) &&
5928c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith           HandleIntToFloatCast(Info, E, From, Result.IntImag,
5929c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                To, Result.FloatImag);
59308786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  }
5931ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson  }
59321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
59338786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  llvm_unreachable("unknown cast resulting in complex value");
59349ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson}
59359ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson
5936f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCallbool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
5937e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma)
59382ad226bdc847df6b6b6e4f832856478ab63bb3dcRichard Smith    return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
59392ad226bdc847df6b6b6e4f832856478ab63bb3dcRichard Smith
5940745f5147e065900267c85a5568785a1991d4838fRichard Smith  bool LHSOK = Visit(E->getLHS());
5941745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!LHSOK && !Info.keepEvaluatingAfterFailure())
5942f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    return false;
59431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5944f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall  ComplexValue RHS;
5945745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK)
5946f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    return false;
5947a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar
59483f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar  assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
59493f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar         "Invalid operands to binary operator.");
5950ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson  switch (E->getOpcode()) {
5951f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  default: return Error(E);
59522de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Add:
5953a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar    if (Result.isComplexFloat()) {
5954a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar      Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
5955a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar                                       APFloat::rmNearestTiesToEven);
5956a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar      Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
5957a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar                                       APFloat::rmNearestTiesToEven);
5958a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar    } else {
5959a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar      Result.getComplexIntReal() += RHS.getComplexIntReal();
5960a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar      Result.getComplexIntImag() += RHS.getComplexIntImag();
5961a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar    }
59623f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar    break;
59632de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Sub:
5964a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar    if (Result.isComplexFloat()) {
5965a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar      Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
5966a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar                                            APFloat::rmNearestTiesToEven);
5967a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar      Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
5968a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar                                            APFloat::rmNearestTiesToEven);
5969a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar    } else {
5970a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar      Result.getComplexIntReal() -= RHS.getComplexIntReal();
5971a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar      Result.getComplexIntImag() -= RHS.getComplexIntImag();
5972a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar    }
59733f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar    break;
59742de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Mul:
59753f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar    if (Result.isComplexFloat()) {
5976f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall      ComplexValue LHS = Result;
59773f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      APFloat &LHS_r = LHS.getComplexFloatReal();
59783f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      APFloat &LHS_i = LHS.getComplexFloatImag();
59793f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      APFloat &RHS_r = RHS.getComplexFloatReal();
59803f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      APFloat &RHS_i = RHS.getComplexFloatImag();
59811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
59823f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      APFloat Tmp = LHS_r;
59833f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
59843f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Result.getComplexFloatReal() = Tmp;
59853f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Tmp = LHS_i;
59863f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
59873f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
59883f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar
59893f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Tmp = LHS_r;
59903f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
59913f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Result.getComplexFloatImag() = Tmp;
59923f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Tmp = LHS_i;
59933f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
59943f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
59953f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar    } else {
5996f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall      ComplexValue LHS = Result;
59971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result.getComplexIntReal() =
59983f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar        (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
59993f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar         LHS.getComplexIntImag() * RHS.getComplexIntImag());
60001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result.getComplexIntImag() =
60013f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar        (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
60023f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar         LHS.getComplexIntImag() * RHS.getComplexIntReal());
60033f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar    }
60043f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar    break;
600596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  case BO_Div:
600696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    if (Result.isComplexFloat()) {
600796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      ComplexValue LHS = Result;
600896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      APFloat &LHS_r = LHS.getComplexFloatReal();
600996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      APFloat &LHS_i = LHS.getComplexFloatImag();
601096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      APFloat &RHS_r = RHS.getComplexFloatReal();
601196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      APFloat &RHS_i = RHS.getComplexFloatImag();
601296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      APFloat &Res_r = Result.getComplexFloatReal();
601396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      APFloat &Res_i = Result.getComplexFloatImag();
601496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara
601596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      APFloat Den = RHS_r;
601696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Den.multiply(RHS_r, APFloat::rmNearestTiesToEven);
601796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      APFloat Tmp = RHS_i;
601896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
601996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Den.add(Tmp, APFloat::rmNearestTiesToEven);
602096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara
602196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Res_r = LHS_r;
602296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Res_r.multiply(RHS_r, APFloat::rmNearestTiesToEven);
602396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Tmp = LHS_i;
602496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
602596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Res_r.add(Tmp, APFloat::rmNearestTiesToEven);
602696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Res_r.divide(Den, APFloat::rmNearestTiesToEven);
602796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara
602896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Res_i = LHS_i;
602996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Res_i.multiply(RHS_r, APFloat::rmNearestTiesToEven);
603096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Tmp = LHS_r;
603196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
603296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Res_i.subtract(Tmp, APFloat::rmNearestTiesToEven);
603396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Res_i.divide(Den, APFloat::rmNearestTiesToEven);
603496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    } else {
6035f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0)
6036f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(E, diag::note_expr_divide_by_zero);
6037f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
603896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      ComplexValue LHS = Result;
603996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() +
604096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara        RHS.getComplexIntImag() * RHS.getComplexIntImag();
604196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Result.getComplexIntReal() =
604296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara        (LHS.getComplexIntReal() * RHS.getComplexIntReal() +
604396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara         LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den;
604496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Result.getComplexIntImag() =
604596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara        (LHS.getComplexIntImag() * RHS.getComplexIntReal() -
604696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara         LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den;
604796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    }
604896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    break;
6049ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson  }
6050ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson
6051f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall  return true;
6052ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson}
6053ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson
605496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnarabool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
605596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  // Get the operand value into 'Result'.
605696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  if (!Visit(E->getSubExpr()))
605796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    return false;
605896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara
605996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  switch (E->getOpcode()) {
606096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  default:
6061f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
606296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  case UO_Extension:
606396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    return true;
606496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  case UO_Plus:
606596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    // The result is always just the subexpr.
606696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    return true;
606796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  case UO_Minus:
606896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    if (Result.isComplexFloat()) {
606996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Result.getComplexFloatReal().changeSign();
607096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Result.getComplexFloatImag().changeSign();
607196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    }
607296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    else {
607396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Result.getComplexIntReal() = -Result.getComplexIntReal();
607496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Result.getComplexIntImag() = -Result.getComplexIntImag();
607596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    }
607696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    return true;
607796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  case UO_Not:
607896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    if (Result.isComplexFloat())
607996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Result.getComplexFloatImag().changeSign();
608096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    else
608196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Result.getComplexIntImag() = -Result.getComplexIntImag();
608296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    return true;
608396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  }
608496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara}
608596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara
60867ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedmanbool ComplexExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
60877ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  if (E->getNumInits() == 2) {
60887ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    if (E->getType()->isComplexType()) {
60897ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman      Result.makeComplexFloat();
60907ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman      if (!EvaluateFloat(E->getInit(0), Result.FloatReal, Info))
60917ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman        return false;
60927ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman      if (!EvaluateFloat(E->getInit(1), Result.FloatImag, Info))
60937ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman        return false;
60947ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    } else {
60957ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman      Result.makeComplexInt();
60967ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman      if (!EvaluateInteger(E->getInit(0), Result.IntReal, Info))
60977ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman        return false;
60987ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman      if (!EvaluateInteger(E->getInit(1), Result.IntImag, Info))
60997ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman        return false;
61007ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    }
61017ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    return true;
61027ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  }
61037ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  return ExprEvaluatorBaseTy::VisitInitListExpr(E);
61047ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman}
61057ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman
61069ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson//===----------------------------------------------------------------------===//
6107aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith// Void expression evaluation, primarily for a cast to void on the LHS of a
6108aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith// comma operator
6109aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith//===----------------------------------------------------------------------===//
6110aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith
6111aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smithnamespace {
6112aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smithclass VoidExprEvaluator
6113aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith  : public ExprEvaluatorBase<VoidExprEvaluator, bool> {
6114aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smithpublic:
6115aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith  VoidExprEvaluator(EvalInfo &Info) : ExprEvaluatorBaseTy(Info) {}
6116aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith
61171aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  bool Success(const APValue &V, const Expr *e) { return true; }
6118aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith
6119aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith  bool VisitCastExpr(const CastExpr *E) {
6120aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith    switch (E->getCastKind()) {
6121aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith    default:
6122aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith      return ExprEvaluatorBaseTy::VisitCastExpr(E);
6123aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith    case CK_ToVoid:
6124aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith      VisitIgnoredValue(E->getSubExpr());
6125aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith      return true;
6126aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith    }
6127aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith  }
6128aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith};
6129aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith} // end anonymous namespace
6130aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith
6131aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smithstatic bool EvaluateVoid(const Expr *E, EvalInfo &Info) {
6132aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith  assert(E->isRValue() && E->getType()->isVoidType());
6133aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith  return VoidExprEvaluator(Info).Visit(E);
6134aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith}
6135aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith
6136aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith//===----------------------------------------------------------------------===//
613751f4708c00110940ca3f337961915f2ca1668375Richard Smith// Top level Expr::EvaluateAsRValue method.
6138f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===//
6139f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
61401aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smithstatic bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E) {
6141c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  // In C, function designators are not lvalues, but we evaluate them as if they
6142c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  // are.
6143c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  if (E->isGLValue() || E->getType()->isFunctionType()) {
6144c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    LValue LV;
6145c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    if (!EvaluateLValue(E, LV, Info))
6146c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith      return false;
6147c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    LV.moveInto(Result);
6148c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  } else if (E->getType()->isVectorType()) {
61491e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    if (!EvaluateVector(E, Result, Info))
615059b5da6d853b4368b984700315adf7b37de05764Nate Begeman      return false;
6151575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor  } else if (E->getType()->isIntegralOrEnumerationType()) {
61521e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    if (!IntExprEvaluator(Info, Result).Visit(E))
61536dde0d5dc09f45f4d9508c964703e36fef1a0198Anders Carlsson      return false;
6154efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  } else if (E->getType()->hasPointerRepresentation()) {
6155efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    LValue LV;
6156efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    if (!EvaluatePointer(E, LV, Info))
61576dde0d5dc09f45f4d9508c964703e36fef1a0198Anders Carlsson      return false;
61581e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    LV.moveInto(Result);
6159efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  } else if (E->getType()->isRealFloatingType()) {
6160efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    llvm::APFloat F(0.0);
6161efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    if (!EvaluateFloat(E, F, Info))
61626dde0d5dc09f45f4d9508c964703e36fef1a0198Anders Carlsson      return false;
61631aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    Result = APValue(F);
6164efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  } else if (E->getType()->isAnyComplexType()) {
6165efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    ComplexValue C;
6166efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    if (!EvaluateComplex(E, C, Info))
6167660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump      return false;
61681e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    C.moveInto(Result);
616969c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith  } else if (E->getType()->isMemberPointerType()) {
6170e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    MemberPtr P;
6171e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (!EvaluateMemberPointer(E, P, Info))
6172e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return false;
6173e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    P.moveInto(Result);
6174e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
617551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  } else if (E->getType()->isArrayType()) {
6176180f47959a066795cc0f409433023af448bb0328Richard Smith    LValue LV;
617783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    LV.set(E, Info.CurrentCall->Index);
6178180f47959a066795cc0f409433023af448bb0328Richard Smith    if (!EvaluateArray(E, LV, Info.CurrentCall->Temporaries[E], Info))
6179cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith      return false;
6180180f47959a066795cc0f409433023af448bb0328Richard Smith    Result = Info.CurrentCall->Temporaries[E];
618151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  } else if (E->getType()->isRecordType()) {
6182180f47959a066795cc0f409433023af448bb0328Richard Smith    LValue LV;
618383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    LV.set(E, Info.CurrentCall->Index);
6184180f47959a066795cc0f409433023af448bb0328Richard Smith    if (!EvaluateRecord(E, LV, Info.CurrentCall->Temporaries[E], Info))
6185180f47959a066795cc0f409433023af448bb0328Richard Smith      return false;
6186180f47959a066795cc0f409433023af448bb0328Richard Smith    Result = Info.CurrentCall->Temporaries[E];
6187aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith  } else if (E->getType()->isVoidType()) {
6188c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    if (Info.getLangOpts().CPlusPlus0x)
61895cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      Info.CCEDiag(E, diag::note_constexpr_nonliteral)
6190c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith        << E->getType();
6191c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    else
61925cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr);
6193aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith    if (!EvaluateVoid(E, Info))
6194aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith      return false;
6195c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  } else if (Info.getLangOpts().CPlusPlus0x) {
61965cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.Diag(E, diag::note_constexpr_nonliteral) << E->getType();
6197c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return false;
6198f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  } else {
61995cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.Diag(E, diag::note_invalid_subexpr_in_const_expr);
6200660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump    return false;
6201f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
6202660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump
6203660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump  return true;
6204660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump}
6205660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump
620683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith/// EvaluateInPlace - Evaluate an expression in-place in an APValue. In some
620783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith/// cases, the in-place evaluation is essential, since later initializers for
620883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith/// an object can indirectly refer to subobjects which were initialized earlier.
620983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smithstatic bool EvaluateInPlace(APValue &Result, EvalInfo &Info, const LValue &This,
621083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                            const Expr *E, CheckConstantExpressionKind CCEK,
621183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                            bool AllowNonLiteralTypes) {
62127ca4850a3e3530fa6c93b64b740446e32c97f992Richard Smith  if (!AllowNonLiteralTypes && !CheckLiteralType(Info, E))
621351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return false;
621451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
621551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (E->isRValue()) {
621669c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith    // Evaluate arrays and record types in-place, so that later initializers can
621769c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith    // refer to earlier-initialized members of the object.
6218180f47959a066795cc0f409433023af448bb0328Richard Smith    if (E->getType()->isArrayType())
6219180f47959a066795cc0f409433023af448bb0328Richard Smith      return EvaluateArray(E, This, Result, Info);
6220180f47959a066795cc0f409433023af448bb0328Richard Smith    else if (E->getType()->isRecordType())
6221180f47959a066795cc0f409433023af448bb0328Richard Smith      return EvaluateRecord(E, This, Result, Info);
622269c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith  }
622369c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith
622469c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith  // For any other type, in-place evaluation is unimportant.
62251aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  return Evaluate(Result, Info, E);
622669c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith}
622769c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith
6228f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// EvaluateAsRValue - Try to evaluate this expression, performing an implicit
6229f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// lvalue-to-rvalue cast if it is an lvalue.
6230f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) {
623151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (!CheckLiteralType(Info, E))
623251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return false;
623351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
62341aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  if (!::Evaluate(Result, Info, E))
6235f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return false;
6236f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
6237f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (E->isGLValue()) {
6238f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    LValue LV;
62391aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    LV.setFrom(Info.Ctx, Result);
62401aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    if (!HandleLValueToRValueConversion(Info, E, E->getType(), LV, Result))
6241f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return false;
6242f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
6243f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
62441aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  // Check this core constant expression is a constant expression.
624583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  return CheckConstantExpression(Info, E->getExprLoc(), E->getType(), Result);
6246f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith}
6247c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
624851f4708c00110940ca3f337961915f2ca1668375Richard Smith/// EvaluateAsRValue - Return true if this is a constant which we can fold using
624956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall/// any crazy technique (that has nothing to do with language standards) that
625056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall/// we want to.  If this function returns true, it returns the folded constant
6251c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// in Result. If this expression is a glvalue, an lvalue-to-rvalue conversion
6252c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// will be applied to the result.
625351f4708c00110940ca3f337961915f2ca1668375Richard Smithbool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx) const {
6254ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith  // Fast-path evaluations of integer literals, since we sometimes see files
6255ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith  // containing vast quantities of these.
6256ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith  if (const IntegerLiteral *L = dyn_cast<IntegerLiteral>(this)) {
6257ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith    Result.Val = APValue(APSInt(L->getValue(),
6258ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith                                L->getType()->isUnsignedIntegerType()));
6259ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith    return true;
6260ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith  }
6261ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith
62622d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  // FIXME: Evaluating values of large array and record types can cause
62632d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  // performance problems. Only do so in C++11 for now.
6264e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) &&
62654e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      !Ctx.getLangOpts().CPlusPlus0x)
62661445bbacf4c8de5f208ff4ccb302424a4d9e233eRichard Smith    return false;
62671445bbacf4c8de5f208ff4ccb302424a4d9e233eRichard Smith
6268f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  EvalInfo Info(Ctx, Result);
6269f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  return ::EvaluateAsRValue(Info, this, Result.Val);
627056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall}
627156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
62724ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool Expr::EvaluateAsBooleanCondition(bool &Result,
62734ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                      const ASTContext &Ctx) const {
6274c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  EvalResult Scratch;
627551f4708c00110940ca3f337961915f2ca1668375Richard Smith  return EvaluateAsRValue(Scratch, Ctx) &&
62761aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith         HandleConversionToBool(Scratch.Val, Result);
6277cd7a445c6b46c5585580dfb652300c8483c0cb6bJohn McCall}
6278cd7a445c6b46c5585580dfb652300c8483c0cb6bJohn McCall
627980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smithbool Expr::EvaluateAsInt(APSInt &Result, const ASTContext &Ctx,
628080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith                         SideEffectsKind AllowSideEffects) const {
628180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  if (!getType()->isIntegralOrEnumerationType())
628280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    return false;
628380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith
6284c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  EvalResult ExprResult;
628580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  if (!EvaluateAsRValue(ExprResult, Ctx) || !ExprResult.Val.isInt() ||
628680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith      (!AllowSideEffects && ExprResult.HasSideEffects))
6287c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return false;
6288f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
6289c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  Result = ExprResult.Val.getInt();
6290c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  return true;
6291a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith}
6292a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith
62934ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx) const {
62941b78276a75a5a0f496a82429c1ff9604d622a76dAnders Carlsson  EvalInfo Info(Ctx, Result);
62951b78276a75a5a0f496a82429c1ff9604d622a76dAnders Carlsson
6296efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  LValue LV;
629783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  if (!EvaluateLValue(this, LV, Info) || Result.HasSideEffects ||
629883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      !CheckLValueConstantExpression(Info, getExprLoc(),
629983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                                     Ctx.getLValueReferenceType(getType()), LV))
630083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return false;
630183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith
63021aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  LV.moveInto(Result.Val);
630383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  return true;
6304b2f295c8050fb8c141bf2cf38eed0a56e99d0092Eli Friedman}
6305b2f295c8050fb8c141bf2cf38eed0a56e99d0092Eli Friedman
6306099e7f647ccda915513f2b2ec53352dc756082d3Richard Smithbool Expr::EvaluateAsInitializer(APValue &Value, const ASTContext &Ctx,
6307099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith                                 const VarDecl *VD,
6308099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith                      llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
63092d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  // FIXME: Evaluating initializers for large array and record types can cause
63102d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  // performance problems. Only do so in C++11 for now.
63112d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) &&
63124e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      !Ctx.getLangOpts().CPlusPlus0x)
63132d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith    return false;
63142d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith
6315099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  Expr::EvalStatus EStatus;
6316099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  EStatus.Diag = &Notes;
6317099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
6318099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  EvalInfo InitInfo(Ctx, EStatus);
6319099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  InitInfo.setEvaluatingDecl(VD, Value);
6320099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
6321099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  LValue LVal;
6322099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  LVal.set(VD);
6323099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
632451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  // C++11 [basic.start.init]p2:
632551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  //  Variables with static storage duration or thread storage duration shall be
632651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  //  zero-initialized before any other initialization takes place.
632751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  // This behavior is not present in C.
63284e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (Ctx.getLangOpts().CPlusPlus && !VD->hasLocalStorage() &&
632951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      !VD->getType()->isReferenceType()) {
633051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    ImplicitValueInitExpr VIE(VD->getType());
633183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    if (!EvaluateInPlace(Value, InitInfo, LVal, &VIE, CCEK_Constant,
633283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                         /*AllowNonLiteralTypes=*/true))
633351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      return false;
633451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  }
633551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
633683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  if (!EvaluateInPlace(Value, InitInfo, LVal, this, CCEK_Constant,
633783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                         /*AllowNonLiteralTypes=*/true) ||
633883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      EStatus.HasSideEffects)
633983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return false;
634083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith
634183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  return CheckConstantExpression(InitInfo, VD->getLocation(), VD->getType(),
634283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                                 Value);
6343099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith}
6344099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
634551f4708c00110940ca3f337961915f2ca1668375Richard Smith/// isEvaluatable - Call EvaluateAsRValue to see if this expression can be
634651f4708c00110940ca3f337961915f2ca1668375Richard Smith/// constant folded, but discard the result.
63474ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool Expr::isEvaluatable(const ASTContext &Ctx) const {
63484fdfb0965b396f2778091f7e6c051d17ff9791baAnders Carlsson  EvalResult Result;
634951f4708c00110940ca3f337961915f2ca1668375Richard Smith  return EvaluateAsRValue(Result, Ctx) && !Result.HasSideEffects;
635045b6b9d080ac56917337d73d8f1cd6374b27b05dChris Lattner}
635151fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson
6352a6b8b2c09610b8bc4330e948ece8b940c2386406Richard SmithAPSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx) const {
63531c0cfd4599e816cfd7a8f348286bf0ad79652ffcAnders Carlsson  EvalResult EvalResult;
635451f4708c00110940ca3f337961915f2ca1668375Richard Smith  bool Result = EvaluateAsRValue(EvalResult, Ctx);
6355c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin  (void)Result;
635651fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson  assert(Result && "Could not evaluate expression");
63571c0cfd4599e816cfd7a8f348286bf0ad79652ffcAnders Carlsson  assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
635851fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson
63591c0cfd4599e816cfd7a8f348286bf0ad79652ffcAnders Carlsson  return EvalResult.Val.getInt();
636051fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson}
6361d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6362e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara bool Expr::EvalResult::isGlobalLValue() const {
6363e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara   assert(Val.isLValue());
6364e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara   return IsGlobalLValue(Val.getLValueBase());
6365e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara }
6366e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara
6367e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara
6368d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// isIntegerConstantExpr - this recursive routine will test if an expression is
6369d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// an integer constant expression.
6370d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6371d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
6372d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// comma, etc
6373d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall///
6374d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// FIXME: Handle offsetof.  Two things to do:  Handle GCC's __builtin_offsetof
6375d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// to support gcc 4.0+  and handle the idiom GCC recognizes with a null pointer
6376d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// cast+dereference.
6377d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6378d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// CheckICE - This function does the fundamental ICE checking: the returned
6379d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
6380d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// Note that to reduce code duplication, this helper does no evaluation
6381d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// itself; the caller checks whether the expression is evaluatable, and
6382d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// in the rare cases where CheckICE actually cares about the evaluated
6383d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// value, it calls into Evalute.
6384d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall//
6385d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// Meanings of Val:
638651f4708c00110940ca3f337961915f2ca1668375Richard Smith// 0: This expression is an ICE.
6387d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// 1: This expression is not an ICE, but if it isn't evaluated, it's
6388d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall//    a legal subexpression for an ICE. This return value is used to handle
6389d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall//    the comma operator in C99 mode.
6390d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// 2: This expression is not an ICE, and is not a legal subexpression for one.
6391d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
63923c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohmannamespace {
63933c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman
6394d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCallstruct ICEDiag {
6395d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  unsigned Val;
6396d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  SourceLocation Loc;
6397d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6398d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  public:
6399d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
6400d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  ICEDiag() : Val(0) {}
6401d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall};
6402d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
64033c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman}
64043c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman
64053c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohmanstatic ICEDiag NoDiag() { return ICEDiag(); }
6406d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6407d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCallstatic ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
6408d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  Expr::EvalResult EVResult;
640951f4708c00110940ca3f337961915f2ca1668375Richard Smith  if (!E->EvaluateAsRValue(EVResult, Ctx) || EVResult.HasSideEffects ||
6410d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      !EVResult.Val.isInt()) {
6411d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return ICEDiag(2, E->getLocStart());
6412d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6413d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  return NoDiag();
6414d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall}
6415d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6416d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCallstatic ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
6417d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  assert(!E->isValueDependent() && "Should not see value dependent exprs!");
64182ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor  if (!E->getType()->isIntegralOrEnumerationType()) {
6419d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return ICEDiag(2, E->getLocStart());
6420d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6421d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6422d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  switch (E->getStmtClass()) {
642363c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall#define ABSTRACT_STMT(Node)
6424d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall#define STMT(Node, Base) case Expr::Node##Class:
6425d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall#define EXPR(Node, Base)
6426d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall#include "clang/AST/StmtNodes.inc"
6427d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::PredefinedExprClass:
6428d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::FloatingLiteralClass:
6429d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ImaginaryLiteralClass:
6430d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::StringLiteralClass:
6431d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ArraySubscriptExprClass:
6432d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::MemberExprClass:
6433d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CompoundAssignOperatorClass:
6434d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CompoundLiteralExprClass:
6435d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ExtVectorElementExprClass:
6436d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::DesignatedInitExprClass:
6437d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ImplicitValueInitExprClass:
6438d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ParenListExprClass:
6439d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::VAArgExprClass:
6440d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::AddrLabelExprClass:
6441d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::StmtExprClass:
6442d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXMemberCallExprClass:
6443e08ce650a2b02410eddd1f60a4aa6b3d4be71e73Peter Collingbourne  case Expr::CUDAKernelCallExprClass:
6444d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXDynamicCastExprClass:
6445d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXTypeidExprClass:
64469be88403e965cc49af76c9d33d818781d44b333eFrancois Pichet  case Expr::CXXUuidofExprClass:
6447d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXNullPtrLiteralExprClass:
64489fcce65e7e1307b5b8da9be13e4092d6bb94dc1dRichard Smith  case Expr::UserDefinedLiteralClass:
6449d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXThisExprClass:
6450d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXThrowExprClass:
6451d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXNewExprClass:
6452d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXDeleteExprClass:
6453d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXPseudoDestructorExprClass:
6454d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::UnresolvedLookupExprClass:
6455d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::DependentScopeDeclRefExprClass:
6456d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXConstructExprClass:
6457d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXBindTemporaryExprClass:
64584765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  case Expr::ExprWithCleanupsClass:
6459d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXTemporaryObjectExprClass:
6460d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXUnresolvedConstructExprClass:
6461d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXDependentScopeMemberExprClass:
6462d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::UnresolvedMemberExprClass:
6463d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ObjCStringLiteralClass:
6464eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  case Expr::ObjCBoxedExprClass:
6465ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  case Expr::ObjCArrayLiteralClass:
6466ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  case Expr::ObjCDictionaryLiteralClass:
6467d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ObjCEncodeExprClass:
6468d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ObjCMessageExprClass:
6469d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ObjCSelectorExprClass:
6470d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ObjCProtocolExprClass:
6471d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ObjCIvarRefExprClass:
6472d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ObjCPropertyRefExprClass:
6473ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  case Expr::ObjCSubscriptRefExprClass:
6474d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ObjCIsaExprClass:
6475d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ShuffleVectorExprClass:
6476d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::BlockExprClass:
6477d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::NoStmtClass:
64787cd7d1ad33fdf49eef83942e8855fe20d95aa1b9John McCall  case Expr::OpaqueValueExprClass:
6479be230c36e32142cbdcdbe9c97511d097beeecbabDouglas Gregor  case Expr::PackExpansionExprClass:
6480c7793c73ba8a343de3f2552d984851985a46f159Douglas Gregor  case Expr::SubstNonTypeTemplateParmPackExprClass:
648161eee0ca33b29e102f11bab77c8b74cc00e2392bTanya Lattner  case Expr::AsTypeExprClass:
6482f85e193739c953358c865005855253af4f68a497John McCall  case Expr::ObjCIndirectCopyRestoreExprClass:
648303e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  case Expr::MaterializeTemporaryExprClass:
64844b9c2d235fb9449e249d74f48ecfec601650de93John McCall  case Expr::PseudoObjectExprClass:
6485276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman  case Expr::AtomicExprClass:
6486cea8d966f826554f0679595e9371e314e8dbc1cfSebastian Redl  case Expr::InitListExprClass:
648701d08018b7cf5ce1601707cfd7a84d22015fc04eDouglas Gregor  case Expr::LambdaExprClass:
6488cea8d966f826554f0679595e9371e314e8dbc1cfSebastian Redl    return ICEDiag(2, E->getLocStart());
6489cea8d966f826554f0679595e9371e314e8dbc1cfSebastian Redl
6490ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor  case Expr::SizeOfPackExprClass:
6491d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::GNUNullExprClass:
6492d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    // GCC considers the GNU __null value to be an integral constant expression.
6493d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return NoDiag();
6494d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
649591a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall  case Expr::SubstNonTypeTemplateParmExprClass:
649691a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall    return
649791a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall      CheckICE(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(), Ctx);
649891a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall
6499d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ParenExprClass:
6500d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
6501f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  case Expr::GenericSelectionExprClass:
6502f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return CheckICE(cast<GenericSelectionExpr>(E)->getResultExpr(), Ctx);
6503d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::IntegerLiteralClass:
6504d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CharacterLiteralClass:
6505ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  case Expr::ObjCBoolLiteralExprClass:
6506d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXBoolLiteralExprClass:
6507ed8abf18329df67b0abcbb3a10458bd8c1d2a595Douglas Gregor  case Expr::CXXScalarValueInitExprClass:
6508d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::UnaryTypeTraitExprClass:
65096ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  case Expr::BinaryTypeTraitExprClass:
65104ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  case Expr::TypeTraitExprClass:
651121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  case Expr::ArrayTypeTraitExprClass:
6512552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  case Expr::ExpressionTraitExprClass:
65132e156225a29407a50dd19041aa5750171ad44ea3Sebastian Redl  case Expr::CXXNoexceptExprClass:
6514d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return NoDiag();
6515d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CallExprClass:
65166cf750298d3621d8a10a6dd07fcee8e274b9d94dSean Hunt  case Expr::CXXOperatorCallExprClass: {
651705830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith    // C99 6.6/3 allows function calls within unevaluated subexpressions of
651805830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith    // constant expressions, but they can never be ICEs because an ICE cannot
651905830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith    // contain an operand of (pointer to) function type.
6520d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    const CallExpr *CE = cast<CallExpr>(E);
6521180f47959a066795cc0f409433023af448bb0328Richard Smith    if (CE->isBuiltinCall())
6522d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return CheckEvalInICE(E, Ctx);
6523d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return ICEDiag(2, E->getLocStart());
6524d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6525359c89df5479810c9d4784fc0b6ab592eb136777Richard Smith  case Expr::DeclRefExprClass: {
6526d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
6527d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return NoDiag();
6528359c89df5479810c9d4784fc0b6ab592eb136777Richard Smith    const ValueDecl *D = dyn_cast<ValueDecl>(cast<DeclRefExpr>(E)->getDecl());
65294e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (Ctx.getLangOpts().CPlusPlus &&
6530359c89df5479810c9d4784fc0b6ab592eb136777Richard Smith        D && IsConstNonVolatile(D->getType())) {
6531d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      // Parameter variables are never constants.  Without this check,
6532d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      // getAnyInitializer() can find a default argument, which leads
6533d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      // to chaos.
6534d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      if (isa<ParmVarDecl>(D))
6535d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
6536d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6537d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      // C++ 7.1.5.1p2
6538d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      //   A variable of non-volatile const-qualified integral or enumeration
6539d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      //   type initialized by an ICE can be used in ICEs.
6540d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) {
6541db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith        if (!Dcl->getType()->isIntegralOrEnumerationType())
6542db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith          return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
6543db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith
6544099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith        const VarDecl *VD;
6545099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith        // Look for a declaration of this variable that has an initializer, and
6546099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith        // check whether it is an ICE.
6547099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith        if (Dcl->getAnyInitializer(VD) && VD->checkInitIsICE())
6548099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith          return NoDiag();
6549099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith        else
6550099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith          return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
6551d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      }
6552d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    }
6553d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return ICEDiag(2, E->getLocStart());
6554359c89df5479810c9d4784fc0b6ab592eb136777Richard Smith  }
6555d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::UnaryOperatorClass: {
6556d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    const UnaryOperator *Exp = cast<UnaryOperator>(E);
6557d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    switch (Exp->getOpcode()) {
65582de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PostInc:
65592de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PostDec:
65602de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PreInc:
65612de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PreDec:
65622de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_AddrOf:
65632de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Deref:
656405830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith      // C99 6.6/3 allows increment and decrement within unevaluated
656505830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith      // subexpressions of constant expressions, but they can never be ICEs
656605830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith      // because an ICE cannot contain an lvalue operand.
6567d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return ICEDiag(2, E->getLocStart());
65682de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Extension:
65692de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_LNot:
65702de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Plus:
65712de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Minus:
65722de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Not:
65732de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Real:
65742de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Imag:
6575d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return CheckICE(Exp->getSubExpr(), Ctx);
6576d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    }
6577d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6578d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    // OffsetOf falls through here.
6579d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6580d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::OffsetOfExprClass: {
6581d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      // Note that per C99, offsetof must be an ICE. And AFAIK, using
658251f4708c00110940ca3f337961915f2ca1668375Richard Smith      // EvaluateAsRValue matches the proposed gcc behavior for cases like
658305830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith      // "offsetof(struct s{int x[4];}, x[1.0])".  This doesn't affect
6584d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      // compliance: we should warn earlier for offsetof expressions with
6585d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      // array subscripts that aren't ICEs, and if the array subscripts
6586d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      // are ICEs, the value of the offsetof must be an integer constant.
6587d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return CheckEvalInICE(E, Ctx);
6588d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6589f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  case Expr::UnaryExprOrTypeTraitExprClass: {
6590f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E);
6591f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    if ((Exp->getKind() ==  UETT_SizeOf) &&
6592f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne        Exp->getTypeOfArgument()->isVariableArrayType())
6593d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return ICEDiag(2, E->getLocStart());
6594d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return NoDiag();
6595d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6596d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::BinaryOperatorClass: {
6597d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    const BinaryOperator *Exp = cast<BinaryOperator>(E);
6598d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    switch (Exp->getOpcode()) {
65992de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_PtrMemD:
66002de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_PtrMemI:
66012de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Assign:
66022de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_MulAssign:
66032de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_DivAssign:
66042de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_RemAssign:
66052de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_AddAssign:
66062de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_SubAssign:
66072de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_ShlAssign:
66082de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_ShrAssign:
66092de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_AndAssign:
66102de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_XorAssign:
66112de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_OrAssign:
661205830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith      // C99 6.6/3 allows assignments within unevaluated subexpressions of
661305830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith      // constant expressions, but they can never be ICEs because an ICE cannot
661405830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith      // contain an lvalue operand.
6615d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return ICEDiag(2, E->getLocStart());
6616d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
66172de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Mul:
66182de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Div:
66192de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Rem:
66202de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Add:
66212de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Sub:
66222de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Shl:
66232de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Shr:
66242de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LT:
66252de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_GT:
66262de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LE:
66272de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_GE:
66282de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_EQ:
66292de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_NE:
66302de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_And:
66312de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Xor:
66322de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Or:
66332de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Comma: {
6634d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
6635d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
66362de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      if (Exp->getOpcode() == BO_Div ||
66372de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall          Exp->getOpcode() == BO_Rem) {
663851f4708c00110940ca3f337961915f2ca1668375Richard Smith        // EvaluateAsRValue gives an error for undefined Div/Rem, so make sure
6639d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        // we don't evaluate one.
66403b332ab132fa85c83833d74d400f6e126f52fbd2John McCall        if (LHSResult.Val == 0 && RHSResult.Val == 0) {
6641a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx);
6642d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall          if (REval == 0)
6643d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall            return ICEDiag(1, E->getLocStart());
6644d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall          if (REval.isSigned() && REval.isAllOnesValue()) {
6645a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith            llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx);
6646d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall            if (LEval.isMinSignedValue())
6647d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall              return ICEDiag(1, E->getLocStart());
6648d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall          }
6649d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        }
6650d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      }
66512de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      if (Exp->getOpcode() == BO_Comma) {
66524e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie        if (Ctx.getLangOpts().C99) {
6653d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall          // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
6654d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall          // if it isn't evaluated.
6655d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall          if (LHSResult.Val == 0 && RHSResult.Val == 0)
6656d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall            return ICEDiag(1, E->getLocStart());
6657d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        } else {
6658d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall          // In both C89 and C++, commas in ICEs are illegal.
6659d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall          return ICEDiag(2, E->getLocStart());
6660d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        }
6661d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      }
6662d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      if (LHSResult.Val >= RHSResult.Val)
6663d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        return LHSResult;
6664d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return RHSResult;
6665d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    }
66662de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LAnd:
66672de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LOr: {
6668d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
6669d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
6670d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      if (LHSResult.Val == 0 && RHSResult.Val == 1) {
6671d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        // Rare case where the RHS has a comma "side-effect"; we need
6672d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        // to actually check the condition to see whether the side
6673d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        // with the comma is evaluated.
66742de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall        if ((Exp->getOpcode() == BO_LAnd) !=
6675a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith            (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0))
6676d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall          return RHSResult;
6677d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        return NoDiag();
6678d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      }
6679d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6680d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      if (LHSResult.Val >= RHSResult.Val)
6681d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        return LHSResult;
6682d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return RHSResult;
6683d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    }
6684d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    }
6685d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6686d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ImplicitCastExprClass:
6687d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CStyleCastExprClass:
6688d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXFunctionalCastExprClass:
6689d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXStaticCastExprClass:
6690d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXReinterpretCastExprClass:
669132cb47174304bc7ec11478b9497c4e10f48273d9Richard Smith  case Expr::CXXConstCastExprClass:
6692f85e193739c953358c865005855253af4f68a497John McCall  case Expr::ObjCBridgedCastExprClass: {
6693d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
66942116b144cf07f2574d20517187eb8863645376ebRichard Smith    if (isa<ExplicitCastExpr>(E)) {
66952116b144cf07f2574d20517187eb8863645376ebRichard Smith      if (const FloatingLiteral *FL
66962116b144cf07f2574d20517187eb8863645376ebRichard Smith            = dyn_cast<FloatingLiteral>(SubExpr->IgnoreParenImpCasts())) {
66972116b144cf07f2574d20517187eb8863645376ebRichard Smith        unsigned DestWidth = Ctx.getIntWidth(E->getType());
66982116b144cf07f2574d20517187eb8863645376ebRichard Smith        bool DestSigned = E->getType()->isSignedIntegerOrEnumerationType();
66992116b144cf07f2574d20517187eb8863645376ebRichard Smith        APSInt IgnoredVal(DestWidth, !DestSigned);
67002116b144cf07f2574d20517187eb8863645376ebRichard Smith        bool Ignored;
67012116b144cf07f2574d20517187eb8863645376ebRichard Smith        // If the value does not fit in the destination type, the behavior is
67022116b144cf07f2574d20517187eb8863645376ebRichard Smith        // undefined, so we are not required to treat it as a constant
67032116b144cf07f2574d20517187eb8863645376ebRichard Smith        // expression.
67042116b144cf07f2574d20517187eb8863645376ebRichard Smith        if (FL->getValue().convertToInteger(IgnoredVal,
67052116b144cf07f2574d20517187eb8863645376ebRichard Smith                                            llvm::APFloat::rmTowardZero,
67062116b144cf07f2574d20517187eb8863645376ebRichard Smith                                            &Ignored) & APFloat::opInvalidOp)
67072116b144cf07f2574d20517187eb8863645376ebRichard Smith          return ICEDiag(2, E->getLocStart());
67082116b144cf07f2574d20517187eb8863645376ebRichard Smith        return NoDiag();
67092116b144cf07f2574d20517187eb8863645376ebRichard Smith      }
67102116b144cf07f2574d20517187eb8863645376ebRichard Smith    }
6711eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman    switch (cast<CastExpr>(E)->getCastKind()) {
6712eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman    case CK_LValueToRValue:
67137a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall    case CK_AtomicToNonAtomic:
67147a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall    case CK_NonAtomicToAtomic:
6715eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman    case CK_NoOp:
6716eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman    case CK_IntegralToBoolean:
6717eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman    case CK_IntegralCast:
6718d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return CheckICE(SubExpr, Ctx);
6719eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman    default:
6720eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman      return ICEDiag(2, E->getLocStart());
6721eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman    }
6722d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
672356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  case Expr::BinaryConditionalOperatorClass: {
672456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E);
672556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    ICEDiag CommonResult = CheckICE(Exp->getCommon(), Ctx);
672656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    if (CommonResult.Val == 2) return CommonResult;
672756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
672856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    if (FalseResult.Val == 2) return FalseResult;
672956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    if (CommonResult.Val == 1) return CommonResult;
673056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    if (FalseResult.Val == 1 &&
6731a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith        Exp->getCommon()->EvaluateKnownConstInt(Ctx) == 0) return NoDiag();
673256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    return FalseResult;
673356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  }
6734d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ConditionalOperatorClass: {
6735d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
6736d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    // If the condition (ignoring parens) is a __builtin_constant_p call,
6737d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    // then only the true side is actually considered in an integer constant
6738d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    // expression, and it is fully evaluated.  This is an important GNU
6739d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    // extension.  See GCC PR38377 for discussion.
6740d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    if (const CallExpr *CallCE
6741d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
674280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith      if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p)
674380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith        return CheckEvalInICE(E, Ctx);
6744d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
6745d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    if (CondResult.Val == 2)
6746d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return CondResult;
674763fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor
6748f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
6749f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
675063fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor
6751d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    if (TrueResult.Val == 2)
6752d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return TrueResult;
6753d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    if (FalseResult.Val == 2)
6754d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return FalseResult;
6755d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    if (CondResult.Val == 1)
6756d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return CondResult;
6757d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    if (TrueResult.Val == 0 && FalseResult.Val == 0)
6758d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return NoDiag();
6759d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    // Rare case where the diagnostics depend on which side is evaluated
6760d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    // Note that if we get here, CondResult is 0, and at least one of
6761d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    // TrueResult and FalseResult is non-zero.
6762a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith    if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0) {
6763d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return FalseResult;
6764d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    }
6765d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return TrueResult;
6766d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6767d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXDefaultArgExprClass:
6768d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
6769d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ChooseExprClass: {
6770d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
6771d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6772d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6773d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
67743026348bd4c13a0f83b59839f64065e0fcbea253David Blaikie  llvm_unreachable("Invalid StmtClass!");
6775d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall}
6776d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6777f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// Evaluate an expression as a C++11 integral constant expression.
6778f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool EvaluateCPlusPlus11IntegralConstantExpr(ASTContext &Ctx,
6779f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                                                    const Expr *E,
6780f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                                                    llvm::APSInt *Value,
6781f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                                                    SourceLocation *Loc) {
6782f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (!E->getType()->isIntegralOrEnumerationType()) {
6783f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (Loc) *Loc = E->getExprLoc();
6784f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return false;
6785f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
6786f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
67874c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  APValue Result;
67884c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  if (!E->isCXX11ConstantExpr(Ctx, &Result, Loc))
6789dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    return false;
6790dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith
67914c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  assert(Result.isInt() && "pointer cast to int is not an ICE");
67924c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  if (Value) *Value = Result.getInt();
6793dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith  return true;
6794f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith}
6795f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
6796dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smithbool Expr::isIntegerConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
67974e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (Ctx.getLangOpts().CPlusPlus0x)
6798f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, 0, Loc);
6799f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
6800d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  ICEDiag d = CheckICE(this, Ctx);
6801d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  if (d.Val != 0) {
6802d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    if (Loc) *Loc = d.Loc;
6803d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return false;
6804d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6805f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  return true;
6806f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith}
6807f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
6808f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithbool Expr::isIntegerConstantExpr(llvm::APSInt &Value, ASTContext &Ctx,
6809f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                                 SourceLocation *Loc, bool isEvaluated) const {
68104e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (Ctx.getLangOpts().CPlusPlus0x)
6811f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, &Value, Loc);
6812f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
6813f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (!isIntegerConstantExpr(Ctx, Loc))
6814f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return false;
6815f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (!EvaluateAsInt(Value, Ctx))
6816d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    llvm_unreachable("ICE cannot be evaluated!");
6817d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  return true;
6818d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall}
68194c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith
682070488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smithbool Expr::isCXX98IntegralConstantExpr(ASTContext &Ctx) const {
682170488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smith  return CheckICE(this, Ctx).Val == 0;
682270488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smith}
682370488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smith
68244c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smithbool Expr::isCXX11ConstantExpr(ASTContext &Ctx, APValue *Result,
68254c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith                               SourceLocation *Loc) const {
68264c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  // We support this checking in C++98 mode in order to diagnose compatibility
68274c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  // issues.
68284e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  assert(Ctx.getLangOpts().CPlusPlus);
68294c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith
683070488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smith  // Build evaluation settings.
68314c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  Expr::EvalStatus Status;
68324c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  llvm::SmallVector<PartialDiagnosticAt, 8> Diags;
68334c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  Status.Diag = &Diags;
68344c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  EvalInfo Info(Ctx, Status);
68354c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith
68364c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  APValue Scratch;
68374c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  bool IsConstExpr = ::EvaluateAsRValue(Info, this, Result ? *Result : Scratch);
68384c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith
68394c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  if (!Diags.empty()) {
68404c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    IsConstExpr = false;
68414c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    if (Loc) *Loc = Diags[0].first;
68424c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  } else if (!IsConstExpr) {
68434c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    // FIXME: This shouldn't happen.
68444c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    if (Loc) *Loc = getExprLoc();
68454c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  }
68464c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith
68474c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  return IsConstExpr;
68484c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith}
6849745f5147e065900267c85a5568785a1991d4838fRichard Smith
6850745f5147e065900267c85a5568785a1991d4838fRichard Smithbool Expr::isPotentialConstantExpr(const FunctionDecl *FD,
6851745f5147e065900267c85a5568785a1991d4838fRichard Smith                                   llvm::SmallVectorImpl<
6852745f5147e065900267c85a5568785a1991d4838fRichard Smith                                     PartialDiagnosticAt> &Diags) {
6853745f5147e065900267c85a5568785a1991d4838fRichard Smith  // FIXME: It would be useful to check constexpr function templates, but at the
6854745f5147e065900267c85a5568785a1991d4838fRichard Smith  // moment the constant expression evaluator cannot cope with the non-rigorous
6855745f5147e065900267c85a5568785a1991d4838fRichard Smith  // ASTs which we build for dependent expressions.
6856745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (FD->isDependentContext())
6857745f5147e065900267c85a5568785a1991d4838fRichard Smith    return true;
6858745f5147e065900267c85a5568785a1991d4838fRichard Smith
6859745f5147e065900267c85a5568785a1991d4838fRichard Smith  Expr::EvalStatus Status;
6860745f5147e065900267c85a5568785a1991d4838fRichard Smith  Status.Diag = &Diags;
6861745f5147e065900267c85a5568785a1991d4838fRichard Smith
6862745f5147e065900267c85a5568785a1991d4838fRichard Smith  EvalInfo Info(FD->getASTContext(), Status);
6863745f5147e065900267c85a5568785a1991d4838fRichard Smith  Info.CheckingPotentialConstantExpression = true;
6864745f5147e065900267c85a5568785a1991d4838fRichard Smith
6865745f5147e065900267c85a5568785a1991d4838fRichard Smith  const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
6866745f5147e065900267c85a5568785a1991d4838fRichard Smith  const CXXRecordDecl *RD = MD ? MD->getParent()->getCanonicalDecl() : 0;
6867745f5147e065900267c85a5568785a1991d4838fRichard Smith
6868745f5147e065900267c85a5568785a1991d4838fRichard Smith  // FIXME: Fabricate an arbitrary expression on the stack and pretend that it
6869745f5147e065900267c85a5568785a1991d4838fRichard Smith  // is a temporary being used as the 'this' pointer.
6870745f5147e065900267c85a5568785a1991d4838fRichard Smith  LValue This;
6871745f5147e065900267c85a5568785a1991d4838fRichard Smith  ImplicitValueInitExpr VIE(RD ? Info.Ctx.getRecordType(RD) : Info.Ctx.IntTy);
687283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  This.set(&VIE, Info.CurrentCall->Index);
6873745f5147e065900267c85a5568785a1991d4838fRichard Smith
6874745f5147e065900267c85a5568785a1991d4838fRichard Smith  ArrayRef<const Expr*> Args;
6875745f5147e065900267c85a5568785a1991d4838fRichard Smith
6876745f5147e065900267c85a5568785a1991d4838fRichard Smith  SourceLocation Loc = FD->getLocation();
6877745f5147e065900267c85a5568785a1991d4838fRichard Smith
68781aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  APValue Scratch;
68791aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
6880745f5147e065900267c85a5568785a1991d4838fRichard Smith    HandleConstructorCall(Loc, This, Args, CD, Info, Scratch);
68811aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  else
6882745f5147e065900267c85a5568785a1991d4838fRichard Smith    HandleFunctionCall(Loc, FD, (MD && MD->isInstance()) ? &This : 0,
6883745f5147e065900267c85a5568785a1991d4838fRichard Smith                       Args, FD->getBody(), Info, Scratch);
6884745f5147e065900267c85a5568785a1991d4838fRichard Smith
6885745f5147e065900267c85a5568785a1991d4838fRichard Smith  return Diags.empty();
6886745f5147e065900267c85a5568785a1991d4838fRichard Smith}
6887