ExprConstant.cpp revision 26dc97cbeba8ced19972a259720a71aefa01ef43
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
95683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    Info.Note(Base.dyn_cast<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
990b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // Allow address constant expressions to be past-the-end pointers. This is
991b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // an extension: the standard requires them to point to an object.
992b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (!IsReferenceType)
993b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    return true;
994b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
995b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // A reference constant expression must refer to an object.
996b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (!Base) {
997b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    // FIXME: diagnostic
99883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    Info.CCEDiag(Loc);
99961e616206413d1779c7545c7a8ad1ce1129ad9c1Richard Smith    return true;
1000b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  }
1001b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
1002c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  // Does this refer one past the end of some object?
1003b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (Designator.isOnePastTheEnd()) {
1004c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>();
100583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    Info.Diag(Loc, diag::note_constexpr_past_end, 1)
1006c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      << !Designator.Entries.empty() << !!VD << VD;
100783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    NoteLValueLocation(Info, Base);
1008c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  }
1009c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
101069c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith  return true;
101147a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith}
101247a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith
101351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// Check that this core constant expression is of literal type, and if not,
101451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// produce an appropriate diagnostic.
101551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smithstatic bool CheckLiteralType(EvalInfo &Info, const Expr *E) {
101651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (!E->isRValue() || E->getType()->isLiteralType())
101751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return true;
101851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
101951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  // Prvalue constant expressions must be of literal types.
102051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (Info.getLangOpts().CPlusPlus0x)
10215cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.Diag(E, diag::note_constexpr_nonliteral)
102251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      << E->getType();
102351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  else
10245cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.Diag(E, diag::note_invalid_subexpr_in_const_expr);
102551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  return false;
102651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith}
102751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
10289a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith/// Check that this core constant expression value is a valid value for a
102983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith/// constant expression. If not, report an appropriate diagnostic. Does not
103083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith/// check that the expression is of literal type.
103183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smithstatic bool CheckConstantExpression(EvalInfo &Info, SourceLocation DiagLoc,
103283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                                    QualType Type, const APValue &Value) {
103383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  // Core issue 1454: For a literal constant expression of array or class type,
103483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  // each subobject of its value shall have been initialized by a constant
103583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  // expression.
103683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  if (Value.isArray()) {
103783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    QualType EltTy = Type->castAsArrayTypeUnsafe()->getElementType();
103883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    for (unsigned I = 0, N = Value.getArrayInitializedElts(); I != N; ++I) {
103983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      if (!CheckConstantExpression(Info, DiagLoc, EltTy,
104083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                                   Value.getArrayInitializedElt(I)))
104183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith        return false;
104283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    }
104383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    if (!Value.hasArrayFiller())
104483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      return true;
104583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return CheckConstantExpression(Info, DiagLoc, EltTy,
104683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                                   Value.getArrayFiller());
104783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  }
104883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  if (Value.isUnion() && Value.getUnionField()) {
104983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return CheckConstantExpression(Info, DiagLoc,
105083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                                   Value.getUnionField()->getType(),
105183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                                   Value.getUnionValue());
105283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  }
105383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  if (Value.isStruct()) {
105483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    RecordDecl *RD = Type->castAs<RecordType>()->getDecl();
105583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD)) {
105683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      unsigned BaseIndex = 0;
105783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(),
105883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith             End = CD->bases_end(); I != End; ++I, ++BaseIndex) {
105983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith        if (!CheckConstantExpression(Info, DiagLoc, I->getType(),
106083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                                     Value.getStructBase(BaseIndex)))
106183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith          return false;
106283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      }
106383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    }
106483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
106583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith         I != E; ++I) {
1066262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie      if (!CheckConstantExpression(Info, DiagLoc, I->getType(),
1067262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie                                   Value.getStructField(I->getFieldIndex())))
106883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith        return false;
106983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    }
107083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  }
107183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith
107283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  if (Value.isLValue()) {
107383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    LValue LVal;
10741aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    LVal.setFrom(Info.Ctx, Value);
107583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return CheckLValueConstantExpression(Info, DiagLoc, Type, LVal);
10769a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith  }
107783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith
107883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  // Everything else is fine.
107983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  return true;
10809a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith}
10819a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith
10829e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smithconst ValueDecl *GetLValueBaseDecl(const LValue &LVal) {
10831bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  return LVal.Base.dyn_cast<const ValueDecl*>();
10849e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith}
10859e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith
10869e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smithstatic bool IsLiteralLValue(const LValue &Value) {
108783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  return Value.Base.dyn_cast<const Expr*>() && !Value.CallIndex;
10889e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith}
10899e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith
109065ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smithstatic bool IsWeakLValue(const LValue &Value) {
109165ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith  const ValueDecl *Decl = GetLValueBaseDecl(Value);
10920dd7a25e8d679de1dc0ce788222d6dee0e879885Lang Hames  return Decl && Decl->isWeak();
109365ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith}
109465ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith
10951aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smithstatic bool EvalPointerValueAsBool(const APValue &Value, bool &Result) {
10963554283157190e67918fad4221a5e6faf9317362John McCall  // A null base expression indicates a null pointer.  These are always
10973554283157190e67918fad4221a5e6faf9317362John McCall  // evaluatable, and they are false unless the offset is zero.
1098e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (!Value.getLValueBase()) {
1099e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    Result = !Value.getLValueOffset().isZero();
11003554283157190e67918fad4221a5e6faf9317362John McCall    return true;
11013554283157190e67918fad4221a5e6faf9317362John McCall  }
11023554283157190e67918fad4221a5e6faf9317362John McCall
1103e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // We have a non-null base.  These are generally known to be true, but if it's
1104e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // a weak declaration it can be null at runtime.
11053554283157190e67918fad4221a5e6faf9317362John McCall  Result = true;
1106e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  const ValueDecl *Decl = Value.getLValueBase().dyn_cast<const ValueDecl*>();
11070dd7a25e8d679de1dc0ce788222d6dee0e879885Lang Hames  return !Decl || !Decl->isWeak();
11085bc86103767c2abcbfdd6518e0ccbbbb6aa59e0fEli Friedman}
11095bc86103767c2abcbfdd6518e0ccbbbb6aa59e0fEli Friedman
11101aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smithstatic bool HandleConversionToBool(const APValue &Val, bool &Result) {
1111c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  switch (Val.getKind()) {
1112c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  case APValue::Uninitialized:
1113c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return false;
1114c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  case APValue::Int:
1115c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    Result = Val.getInt().getBoolValue();
111641bf4f38348561a0f12c10d34f1673cd19a6eb04Richard Smith    return true;
1117c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  case APValue::Float:
1118c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    Result = !Val.getFloat().isZero();
1119a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman    return true;
1120c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  case APValue::ComplexInt:
1121c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    Result = Val.getComplexIntReal().getBoolValue() ||
1122c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith             Val.getComplexIntImag().getBoolValue();
1123c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return true;
1124c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  case APValue::ComplexFloat:
1125c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    Result = !Val.getComplexFloatReal().isZero() ||
1126c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith             !Val.getComplexFloatImag().isZero();
1127436c8898cd1c93c5bacd3fcc4ac586bc5cd77062Richard Smith    return true;
1128e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  case APValue::LValue:
1129e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return EvalPointerValueAsBool(Val, Result);
1130e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  case APValue::MemberPointer:
1131e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    Result = Val.getMemberPointerDecl();
1132e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
1133c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  case APValue::Vector:
1134cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  case APValue::Array:
1135180f47959a066795cc0f409433023af448bb0328Richard Smith  case APValue::Struct:
1136180f47959a066795cc0f409433023af448bb0328Richard Smith  case APValue::Union:
113765639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman  case APValue::AddrLabelDiff:
1138c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return false;
11394efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman  }
11404efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
1141c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  llvm_unreachable("unknown APValue kind");
1142c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith}
1143c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
1144c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smithstatic bool EvaluateAsBooleanCondition(const Expr *E, bool &Result,
1145c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith                                       EvalInfo &Info) {
1146c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  assert(E->isRValue() && "missing lvalue-to-rvalue conv in bool condition");
11471aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  APValue Val;
1148d411a4b23077b29e19c9371bbb19b054a374d922Argyrios Kyrtzidis  if (!Evaluate(Val, Info, E))
1149c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return false;
1150d411a4b23077b29e19c9371bbb19b054a374d922Argyrios Kyrtzidis  return HandleConversionToBool(Val, Result);
11514efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman}
11524efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
1153c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithtemplate<typename T>
115426dc97cbeba8ced19972a259720a71aefa01ef43Eli Friedmanstatic void HandleOverflow(EvalInfo &Info, const Expr *E,
1155c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                           const T &SrcValue, QualType DestType) {
115626dc97cbeba8ced19972a259720a71aefa01ef43Eli Friedman  Info.CCEDiag(E, diag::note_constexpr_overflow)
1157789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    << SrcValue << DestType;
1158c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
1159c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
1160c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstatic bool HandleFloatToIntCast(EvalInfo &Info, const Expr *E,
1161c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                 QualType SrcType, const APFloat &Value,
1162c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                 QualType DestType, APSInt &Result) {
1163c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  unsigned DestWidth = Info.Ctx.getIntWidth(DestType);
1164a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar  // Determine whether we are converting to unsigned or signed.
1165575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor  bool DestSigned = DestType->isSignedIntegerOrEnumerationType();
11661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1167c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  Result = APSInt(DestWidth, !DestSigned);
1168a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar  bool ignored;
1169c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  if (Value.convertToInteger(Result, llvm::APFloat::rmTowardZero, &ignored)
1170c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      & APFloat::opInvalidOp)
117126dc97cbeba8ced19972a259720a71aefa01ef43Eli Friedman    HandleOverflow(Info, E, Value, DestType);
1172c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  return true;
1173a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar}
1174a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar
1175c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstatic bool HandleFloatToFloatCast(EvalInfo &Info, const Expr *E,
1176c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                   QualType SrcType, QualType DestType,
1177c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                   APFloat &Result) {
1178c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  APFloat Value = Result;
1179a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar  bool ignored;
1180c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  if (Result.convert(Info.Ctx.getFloatTypeSemantics(DestType),
1181c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                     APFloat::rmNearestTiesToEven, &ignored)
1182c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      & APFloat::opOverflow)
118326dc97cbeba8ced19972a259720a71aefa01ef43Eli Friedman    HandleOverflow(Info, E, Value, DestType);
1184c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  return true;
1185a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar}
1186a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar
1187f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smithstatic APSInt HandleIntToIntCast(EvalInfo &Info, const Expr *E,
1188f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith                                 QualType DestType, QualType SrcType,
1189f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith                                 APSInt &Value) {
1190f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith  unsigned DestWidth = Info.Ctx.getIntWidth(DestType);
1191a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar  APSInt Result = Value;
1192a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar  // Figure out if this is a truncate, extend or noop cast.
1193a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar  // If the input is signed, do a sign extend, noop, or truncate.
11949f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad  Result = Result.extOrTrunc(DestWidth);
1195575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor  Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType());
1196a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar  return Result;
1197a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar}
1198a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar
1199c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstatic bool HandleIntToFloatCast(EvalInfo &Info, const Expr *E,
1200c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                 QualType SrcType, const APSInt &Value,
1201c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                 QualType DestType, APFloat &Result) {
1202c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  Result = APFloat(Info.Ctx.getFloatTypeSemantics(DestType), 1);
1203c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  if (Result.convertFromAPInt(Value, Value.isSigned(),
1204c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                              APFloat::rmNearestTiesToEven)
1205c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      & APFloat::opOverflow)
120626dc97cbeba8ced19972a259720a71aefa01ef43Eli Friedman    HandleOverflow(Info, E, Value, DestType);
1207c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  return true;
1208a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar}
1209a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar
1210e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedmanstatic bool EvalAndBitcastToAPInt(EvalInfo &Info, const Expr *E,
1211e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman                                  llvm::APInt &Res) {
12121aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  APValue SVal;
1213e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  if (!Evaluate(SVal, Info, E))
1214e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    return false;
1215e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  if (SVal.isInt()) {
1216e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    Res = SVal.getInt();
1217e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    return true;
1218e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  }
1219e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  if (SVal.isFloat()) {
1220e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    Res = SVal.getFloat().bitcastToAPInt();
1221e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    return true;
1222e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  }
1223e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  if (SVal.isVector()) {
1224e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    QualType VecTy = E->getType();
1225e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    unsigned VecSize = Info.Ctx.getTypeSize(VecTy);
1226e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    QualType EltTy = VecTy->castAs<VectorType>()->getElementType();
1227e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    unsigned EltSize = Info.Ctx.getTypeSize(EltTy);
1228e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian();
1229e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    Res = llvm::APInt::getNullValue(VecSize);
1230e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    for (unsigned i = 0; i < SVal.getVectorLength(); i++) {
1231e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      APValue &Elt = SVal.getVectorElt(i);
1232e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      llvm::APInt EltAsInt;
1233e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      if (Elt.isInt()) {
1234e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        EltAsInt = Elt.getInt();
1235e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      } else if (Elt.isFloat()) {
1236e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        EltAsInt = Elt.getFloat().bitcastToAPInt();
1237e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      } else {
1238e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        // Don't try to handle vectors of anything other than int or float
1239e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        // (not sure if it's possible to hit this case).
12405cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith        Info.Diag(E, diag::note_invalid_subexpr_in_const_expr);
1241e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        return false;
1242e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      }
1243e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      unsigned BaseEltSize = EltAsInt.getBitWidth();
1244e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      if (BigEndian)
1245e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        Res |= EltAsInt.zextOrTrunc(VecSize).rotr(i*EltSize+BaseEltSize);
1246e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      else
1247e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        Res |= EltAsInt.zextOrTrunc(VecSize).rotl(i*EltSize);
1248e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    }
1249e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    return true;
1250e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  }
1251e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  // Give up if the input isn't an int, float, or vector.  For example, we
1252e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  // reject "(v4i16)(intptr_t)&a".
12535cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith  Info.Diag(E, diag::note_invalid_subexpr_in_const_expr);
1254e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  return false;
1255e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman}
1256e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman
1257b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith/// Cast an lvalue referring to a base subobject to a derived class, by
1258b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith/// truncating the lvalue's path to the given length.
1259b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic bool CastToDerivedClass(EvalInfo &Info, const Expr *E, LValue &Result,
1260b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                               const RecordDecl *TruncatedType,
1261b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                               unsigned TruncatedElements) {
1262b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  SubobjectDesignator &D = Result.Designator;
1263180f47959a066795cc0f409433023af448bb0328Richard Smith
1264b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // Check we actually point to a derived class object.
1265b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (TruncatedElements == D.Entries.size())
1266b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    return true;
1267b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  assert(TruncatedElements >= D.MostDerivedPathLength &&
1268b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith         "not casting to a derived class");
1269b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (!Result.checkSubobject(Info, E, CSK_Derived))
1270180f47959a066795cc0f409433023af448bb0328Richard Smith    return false;
1271180f47959a066795cc0f409433023af448bb0328Richard Smith
1272b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // Truncate the path to the subobject, and remove any derived-to-base offsets.
1273e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  const RecordDecl *RD = TruncatedType;
1274e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  for (unsigned I = TruncatedElements, N = D.Entries.size(); I != N; ++I) {
12758d59deec807ed53efcd07855199cdc9c979f447fJohn McCall    if (RD->isInvalidDecl()) return false;
1276180f47959a066795cc0f409433023af448bb0328Richard Smith    const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD);
1277180f47959a066795cc0f409433023af448bb0328Richard Smith    const CXXRecordDecl *Base = getAsBaseClass(D.Entries[I]);
1278e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (isVirtualBaseClass(D.Entries[I]))
1279180f47959a066795cc0f409433023af448bb0328Richard Smith      Result.Offset -= Layout.getVBaseClassOffset(Base);
1280e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    else
1281180f47959a066795cc0f409433023af448bb0328Richard Smith      Result.Offset -= Layout.getBaseClassOffset(Base);
1282180f47959a066795cc0f409433023af448bb0328Richard Smith    RD = Base;
1283180f47959a066795cc0f409433023af448bb0328Richard Smith  }
1284e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  D.Entries.resize(TruncatedElements);
1285180f47959a066795cc0f409433023af448bb0328Richard Smith  return true;
1286180f47959a066795cc0f409433023af448bb0328Richard Smith}
1287180f47959a066795cc0f409433023af448bb0328Richard Smith
12888d59deec807ed53efcd07855199cdc9c979f447fJohn McCallstatic bool HandleLValueDirectBase(EvalInfo &Info, const Expr *E, LValue &Obj,
1289180f47959a066795cc0f409433023af448bb0328Richard Smith                                   const CXXRecordDecl *Derived,
1290180f47959a066795cc0f409433023af448bb0328Richard Smith                                   const CXXRecordDecl *Base,
1291180f47959a066795cc0f409433023af448bb0328Richard Smith                                   const ASTRecordLayout *RL = 0) {
12928d59deec807ed53efcd07855199cdc9c979f447fJohn McCall  if (!RL) {
12938d59deec807ed53efcd07855199cdc9c979f447fJohn McCall    if (Derived->isInvalidDecl()) return false;
12948d59deec807ed53efcd07855199cdc9c979f447fJohn McCall    RL = &Info.Ctx.getASTRecordLayout(Derived);
12958d59deec807ed53efcd07855199cdc9c979f447fJohn McCall  }
12968d59deec807ed53efcd07855199cdc9c979f447fJohn McCall
1297180f47959a066795cc0f409433023af448bb0328Richard Smith  Obj.getLValueOffset() += RL->getBaseClassOffset(Base);
1298b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  Obj.addDecl(Info, E, Base, /*Virtual*/ false);
12998d59deec807ed53efcd07855199cdc9c979f447fJohn McCall  return true;
1300180f47959a066795cc0f409433023af448bb0328Richard Smith}
1301180f47959a066795cc0f409433023af448bb0328Richard Smith
1302b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic bool HandleLValueBase(EvalInfo &Info, const Expr *E, LValue &Obj,
1303180f47959a066795cc0f409433023af448bb0328Richard Smith                             const CXXRecordDecl *DerivedDecl,
1304180f47959a066795cc0f409433023af448bb0328Richard Smith                             const CXXBaseSpecifier *Base) {
1305180f47959a066795cc0f409433023af448bb0328Richard Smith  const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl();
1306180f47959a066795cc0f409433023af448bb0328Richard Smith
13078d59deec807ed53efcd07855199cdc9c979f447fJohn McCall  if (!Base->isVirtual())
13088d59deec807ed53efcd07855199cdc9c979f447fJohn McCall    return HandleLValueDirectBase(Info, E, Obj, DerivedDecl, BaseDecl);
1309180f47959a066795cc0f409433023af448bb0328Richard Smith
1310b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  SubobjectDesignator &D = Obj.Designator;
1311b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (D.Invalid)
1312b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    return false;
1313b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
1314180f47959a066795cc0f409433023af448bb0328Richard Smith  // Extract most-derived object and corresponding type.
1315b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  DerivedDecl = D.MostDerivedType->getAsCXXRecordDecl();
1316b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (!CastToDerivedClass(Info, E, Obj, DerivedDecl, D.MostDerivedPathLength))
1317180f47959a066795cc0f409433023af448bb0328Richard Smith    return false;
1318180f47959a066795cc0f409433023af448bb0328Richard Smith
1319b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // Find the virtual base class.
13208d59deec807ed53efcd07855199cdc9c979f447fJohn McCall  if (DerivedDecl->isInvalidDecl()) return false;
1321180f47959a066795cc0f409433023af448bb0328Richard Smith  const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl);
1322180f47959a066795cc0f409433023af448bb0328Richard Smith  Obj.getLValueOffset() += Layout.getVBaseClassOffset(BaseDecl);
1323b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  Obj.addDecl(Info, E, BaseDecl, /*Virtual*/ true);
1324180f47959a066795cc0f409433023af448bb0328Richard Smith  return true;
1325180f47959a066795cc0f409433023af448bb0328Richard Smith}
1326180f47959a066795cc0f409433023af448bb0328Richard Smith
1327180f47959a066795cc0f409433023af448bb0328Richard Smith/// Update LVal to refer to the given field, which must be a member of the type
1328180f47959a066795cc0f409433023af448bb0328Richard Smith/// currently described by LVal.
13298d59deec807ed53efcd07855199cdc9c979f447fJohn McCallstatic bool HandleLValueMember(EvalInfo &Info, const Expr *E, LValue &LVal,
1330180f47959a066795cc0f409433023af448bb0328Richard Smith                               const FieldDecl *FD,
1331180f47959a066795cc0f409433023af448bb0328Richard Smith                               const ASTRecordLayout *RL = 0) {
13328d59deec807ed53efcd07855199cdc9c979f447fJohn McCall  if (!RL) {
13338d59deec807ed53efcd07855199cdc9c979f447fJohn McCall    if (FD->getParent()->isInvalidDecl()) return false;
1334180f47959a066795cc0f409433023af448bb0328Richard Smith    RL = &Info.Ctx.getASTRecordLayout(FD->getParent());
13358d59deec807ed53efcd07855199cdc9c979f447fJohn McCall  }
1336180f47959a066795cc0f409433023af448bb0328Richard Smith
1337180f47959a066795cc0f409433023af448bb0328Richard Smith  unsigned I = FD->getFieldIndex();
1338180f47959a066795cc0f409433023af448bb0328Richard Smith  LVal.Offset += Info.Ctx.toCharUnitsFromBits(RL->getFieldOffset(I));
1339b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  LVal.addDecl(Info, E, FD);
13408d59deec807ed53efcd07855199cdc9c979f447fJohn McCall  return true;
1341180f47959a066795cc0f409433023af448bb0328Richard Smith}
1342180f47959a066795cc0f409433023af448bb0328Richard Smith
1343d9b02e726262e4009dda830998bb934172ac0020Richard Smith/// Update LVal to refer to the given indirect field.
13448d59deec807ed53efcd07855199cdc9c979f447fJohn McCallstatic bool HandleLValueIndirectMember(EvalInfo &Info, const Expr *E,
1345d9b02e726262e4009dda830998bb934172ac0020Richard Smith                                       LValue &LVal,
1346d9b02e726262e4009dda830998bb934172ac0020Richard Smith                                       const IndirectFieldDecl *IFD) {
1347d9b02e726262e4009dda830998bb934172ac0020Richard Smith  for (IndirectFieldDecl::chain_iterator C = IFD->chain_begin(),
1348d9b02e726262e4009dda830998bb934172ac0020Richard Smith                                         CE = IFD->chain_end(); C != CE; ++C)
13498d59deec807ed53efcd07855199cdc9c979f447fJohn McCall    if (!HandleLValueMember(Info, E, LVal, cast<FieldDecl>(*C)))
13508d59deec807ed53efcd07855199cdc9c979f447fJohn McCall      return false;
13518d59deec807ed53efcd07855199cdc9c979f447fJohn McCall  return true;
1352d9b02e726262e4009dda830998bb934172ac0020Richard Smith}
1353d9b02e726262e4009dda830998bb934172ac0020Richard Smith
1354180f47959a066795cc0f409433023af448bb0328Richard Smith/// Get the size of the given type in char units.
135574e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smithstatic bool HandleSizeof(EvalInfo &Info, SourceLocation Loc,
135674e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith                         QualType Type, CharUnits &Size) {
1357180f47959a066795cc0f409433023af448bb0328Richard Smith  // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1358180f47959a066795cc0f409433023af448bb0328Richard Smith  // extension.
1359180f47959a066795cc0f409433023af448bb0328Richard Smith  if (Type->isVoidType() || Type->isFunctionType()) {
1360180f47959a066795cc0f409433023af448bb0328Richard Smith    Size = CharUnits::One();
1361180f47959a066795cc0f409433023af448bb0328Richard Smith    return true;
1362180f47959a066795cc0f409433023af448bb0328Richard Smith  }
1363180f47959a066795cc0f409433023af448bb0328Richard Smith
1364180f47959a066795cc0f409433023af448bb0328Richard Smith  if (!Type->isConstantSizeType()) {
1365180f47959a066795cc0f409433023af448bb0328Richard Smith    // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
136674e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    // FIXME: Better diagnostic.
136774e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    Info.Diag(Loc);
1368180f47959a066795cc0f409433023af448bb0328Richard Smith    return false;
1369180f47959a066795cc0f409433023af448bb0328Richard Smith  }
1370180f47959a066795cc0f409433023af448bb0328Richard Smith
1371180f47959a066795cc0f409433023af448bb0328Richard Smith  Size = Info.Ctx.getTypeSizeInChars(Type);
1372180f47959a066795cc0f409433023af448bb0328Richard Smith  return true;
1373180f47959a066795cc0f409433023af448bb0328Richard Smith}
1374180f47959a066795cc0f409433023af448bb0328Richard Smith
1375180f47959a066795cc0f409433023af448bb0328Richard Smith/// Update a pointer value to model pointer arithmetic.
1376180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param Info - Information about the ongoing evaluation.
1377b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith/// \param E - The expression being evaluated, for diagnostic purposes.
1378180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param LVal - The pointer value to be updated.
1379180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param EltTy - The pointee type represented by LVal.
1380180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param Adjustment - The adjustment, in objects of type EltTy, to add.
1381b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E,
1382b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                                        LValue &LVal, QualType EltTy,
1383b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                                        int64_t Adjustment) {
1384180f47959a066795cc0f409433023af448bb0328Richard Smith  CharUnits SizeOfPointee;
138574e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfPointee))
1386180f47959a066795cc0f409433023af448bb0328Richard Smith    return false;
1387180f47959a066795cc0f409433023af448bb0328Richard Smith
1388180f47959a066795cc0f409433023af448bb0328Richard Smith  // Compute the new offset in the appropriate width.
1389180f47959a066795cc0f409433023af448bb0328Richard Smith  LVal.Offset += Adjustment * SizeOfPointee;
1390b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  LVal.adjustIndex(Info, E, Adjustment);
1391180f47959a066795cc0f409433023af448bb0328Richard Smith  return true;
1392180f47959a066795cc0f409433023af448bb0328Richard Smith}
1393180f47959a066795cc0f409433023af448bb0328Richard Smith
139486024013d4c3728122c58fa07a2a67e6c15837efRichard Smith/// Update an lvalue to refer to a component of a complex number.
139586024013d4c3728122c58fa07a2a67e6c15837efRichard Smith/// \param Info - Information about the ongoing evaluation.
139686024013d4c3728122c58fa07a2a67e6c15837efRichard Smith/// \param LVal - The lvalue to be updated.
139786024013d4c3728122c58fa07a2a67e6c15837efRichard Smith/// \param EltTy - The complex number's component type.
139886024013d4c3728122c58fa07a2a67e6c15837efRichard Smith/// \param Imag - False for the real component, true for the imaginary.
139986024013d4c3728122c58fa07a2a67e6c15837efRichard Smithstatic bool HandleLValueComplexElement(EvalInfo &Info, const Expr *E,
140086024013d4c3728122c58fa07a2a67e6c15837efRichard Smith                                       LValue &LVal, QualType EltTy,
140186024013d4c3728122c58fa07a2a67e6c15837efRichard Smith                                       bool Imag) {
140286024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  if (Imag) {
140386024013d4c3728122c58fa07a2a67e6c15837efRichard Smith    CharUnits SizeOfComponent;
140486024013d4c3728122c58fa07a2a67e6c15837efRichard Smith    if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfComponent))
140586024013d4c3728122c58fa07a2a67e6c15837efRichard Smith      return false;
140686024013d4c3728122c58fa07a2a67e6c15837efRichard Smith    LVal.Offset += SizeOfComponent;
140786024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  }
140886024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  LVal.addComplex(Info, E, EltTy, Imag);
140986024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  return true;
141086024013d4c3728122c58fa07a2a67e6c15837efRichard Smith}
141186024013d4c3728122c58fa07a2a67e6c15837efRichard Smith
141203f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith/// Try to evaluate the initializer for a variable declaration.
1413f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool EvaluateVarDeclInit(EvalInfo &Info, const Expr *E,
1414f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                                const VarDecl *VD,
14151aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith                                CallStackFrame *Frame, APValue &Result) {
1416d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  // If this is a parameter to an active constexpr function call, perform
1417d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  // argument substitution.
1418d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(VD)) {
1419745f5147e065900267c85a5568785a1991d4838fRichard Smith    // Assume arguments of a potential constant expression are unknown
1420745f5147e065900267c85a5568785a1991d4838fRichard Smith    // constant expressions.
1421745f5147e065900267c85a5568785a1991d4838fRichard Smith    if (Info.CheckingPotentialConstantExpression)
1422745f5147e065900267c85a5568785a1991d4838fRichard Smith      return false;
1423f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!Frame || !Frame->Arguments) {
14245cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      Info.Diag(E, diag::note_invalid_subexpr_in_const_expr);
1425177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith      return false;
1426f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    }
1427177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith    Result = Frame->Arguments[PVD->getFunctionScopeIndex()];
1428177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith    return true;
1429d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  }
143003f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith
1431099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  // Dig out the initializer, and use the declaration which it's attached to.
1432099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  const Expr *Init = VD->getAnyInitializer(VD);
1433099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  if (!Init || Init->isValueDependent()) {
1434745f5147e065900267c85a5568785a1991d4838fRichard Smith    // If we're checking a potential constant expression, the variable could be
1435745f5147e065900267c85a5568785a1991d4838fRichard Smith    // initialized later.
1436745f5147e065900267c85a5568785a1991d4838fRichard Smith    if (!Info.CheckingPotentialConstantExpression)
14375cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      Info.Diag(E, diag::note_invalid_subexpr_in_const_expr);
1438099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    return false;
1439099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  }
1440099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
1441180f47959a066795cc0f409433023af448bb0328Richard Smith  // If we're currently evaluating the initializer of this declaration, use that
1442180f47959a066795cc0f409433023af448bb0328Richard Smith  // in-flight value.
1443180f47959a066795cc0f409433023af448bb0328Richard Smith  if (Info.EvaluatingDecl == VD) {
14441aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    Result = *Info.EvaluatingDeclValue;
1445180f47959a066795cc0f409433023af448bb0328Richard Smith    return !Result.isUninit();
1446180f47959a066795cc0f409433023af448bb0328Richard Smith  }
1447180f47959a066795cc0f409433023af448bb0328Richard Smith
144865ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith  // Never evaluate the initializer of a weak variable. We can't be sure that
144965ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith  // this is the definition which will be used.
1450f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (VD->isWeak()) {
14515cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.Diag(E, diag::note_invalid_subexpr_in_const_expr);
145265ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith    return false;
1453f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
145465ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith
1455099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  // Check that we can fold the initializer. In C++, we will have already done
1456099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  // this in the cases where it matters for conformance.
1457099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1458099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  if (!VD->evaluateValue(Notes)) {
14595cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.Diag(E, diag::note_constexpr_var_init_non_constant,
1460099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith              Notes.size() + 1) << VD;
1461099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    Info.Note(VD->getLocation(), diag::note_declared_at);
1462099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    Info.addNotes(Notes);
146347a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    return false;
1464099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  } else if (!VD->checkInitIsICE()) {
14655cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.CCEDiag(E, diag::note_constexpr_var_init_non_constant,
1466099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith                 Notes.size() + 1) << VD;
1467099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    Info.Note(VD->getLocation(), diag::note_declared_at);
1468099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    Info.addNotes(Notes);
1469f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
147003f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith
14711aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  Result = *VD->getEvaluatedValue();
147247a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith  return true;
147303f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith}
147403f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith
1475c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smithstatic bool IsConstNonVolatile(QualType T) {
147603f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith  Qualifiers Quals = T.getQualifiers();
147703f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith  return Quals.hasConst() && !Quals.hasVolatile();
147803f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith}
147903f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith
148059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith/// Get the base index of the given base class within an APValue representing
148159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith/// the given derived class.
148259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithstatic unsigned getBaseIndex(const CXXRecordDecl *Derived,
148359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith                             const CXXRecordDecl *Base) {
148459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  Base = Base->getCanonicalDecl();
148559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  unsigned Index = 0;
148659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  for (CXXRecordDecl::base_class_const_iterator I = Derived->bases_begin(),
148759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith         E = Derived->bases_end(); I != E; ++I, ++Index) {
148859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    if (I->getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base)
148959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      return Index;
149059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  }
149159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
149259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  llvm_unreachable("base class missing from derived class's bases list");
149359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
149459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
1495fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith/// Extract the value of a character from a string literal. CharType is used to
1496fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith/// determine the expected signedness of the result -- a string literal used to
1497fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith/// initialize an array of 'signed char' or 'unsigned char' might contain chars
1498fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith/// of the wrong signedness.
1499f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smithstatic APSInt ExtractStringLiteralCharacter(EvalInfo &Info, const Expr *Lit,
1500fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith                                            uint64_t Index, QualType CharType) {
1501f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith  // FIXME: Support PredefinedExpr, ObjCEncodeExpr, MakeStringConstant
1502f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith  const StringLiteral *S = dyn_cast<StringLiteral>(Lit);
1503f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith  assert(S && "unexpected string literal expression kind");
1504fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith  assert(CharType->isIntegerType() && "unexpected character type");
1505f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith
1506f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith  APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(),
1507fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith               CharType->isUnsignedIntegerType());
1508f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith  if (Index < S->getLength())
1509f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith    Value = S->getCodeUnit(Index);
1510f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith  return Value;
1511f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith}
1512f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith
1513cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith/// Extract the designated sub-object of an rvalue.
1514f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool ExtractSubobject(EvalInfo &Info, const Expr *E,
15151aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith                             APValue &Obj, QualType ObjType,
1516cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith                             const SubobjectDesignator &Sub, QualType SubType) {
1517b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (Sub.Invalid)
1518b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    // A diagnostic will have already been produced.
1519cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    return false;
1520b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (Sub.isOnePastTheEnd()) {
15215cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.Diag(E, Info.getLangOpts().CPlusPlus0x ?
1522aa5d533427d803e52ee42b250ffd6645ef5ccb0fMatt Beaumont-Gay                (unsigned)diag::note_constexpr_read_past_end :
1523aa5d533427d803e52ee42b250ffd6645ef5ccb0fMatt Beaumont-Gay                (unsigned)diag::note_invalid_subexpr_in_const_expr);
15247098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    return false;
15257098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  }
1526f64699e8db3946e21b5f4a0421cbc58a3e439599Richard Smith  if (Sub.Entries.empty())
1527cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    return true;
1528745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (Info.CheckingPotentialConstantExpression && Obj.isUninit())
1529745f5147e065900267c85a5568785a1991d4838fRichard Smith    // This object might be initialized later.
1530745f5147e065900267c85a5568785a1991d4838fRichard Smith    return false;
1531cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
15320069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith  APValue *O = &Obj;
1533180f47959a066795cc0f409433023af448bb0328Richard Smith  // Walk the designator's path to find the subobject.
1534cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  for (unsigned I = 0, N = Sub.Entries.size(); I != N; ++I) {
1535cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    if (ObjType->isArrayType()) {
1536180f47959a066795cc0f409433023af448bb0328Richard Smith      // Next subobject is an array element.
1537cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith      const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(ObjType);
1538f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      assert(CAT && "vla in literal type?");
1539cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith      uint64_t Index = Sub.Entries[I].ArrayIndex;
1540f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      if (CAT->getSize().ule(Index)) {
15417098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        // Note, it should not be possible to form a pointer with a valid
15427098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        // designator which points more than one past the end of the array.
15435cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith        Info.Diag(E, Info.getLangOpts().CPlusPlus0x ?
1544aa5d533427d803e52ee42b250ffd6645ef5ccb0fMatt Beaumont-Gay                    (unsigned)diag::note_constexpr_read_past_end :
1545aa5d533427d803e52ee42b250ffd6645ef5ccb0fMatt Beaumont-Gay                    (unsigned)diag::note_invalid_subexpr_in_const_expr);
1546cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith        return false;
1547f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      }
1548f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith      // An array object is represented as either an Array APValue or as an
1549f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith      // LValue which refers to a string literal.
1550f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith      if (O->isLValue()) {
1551f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith        assert(I == N - 1 && "extracting subobject of character?");
1552f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith        assert(!O->hasLValuePath() || O->getLValuePath().empty());
15531aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith        Obj = APValue(ExtractStringLiteralCharacter(
1554fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith          Info, O->getLValueBase().get<const Expr*>(), Index, SubType));
1555f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith        return true;
1556f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith      } else if (O->getArrayInitializedElts() > Index)
1557cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith        O = &O->getArrayInitializedElt(Index);
1558cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith      else
1559cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith        O = &O->getArrayFiller();
1560cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith      ObjType = CAT->getElementType();
156186024013d4c3728122c58fa07a2a67e6c15837efRichard Smith    } else if (ObjType->isAnyComplexType()) {
156286024013d4c3728122c58fa07a2a67e6c15837efRichard Smith      // Next subobject is a complex number.
156386024013d4c3728122c58fa07a2a67e6c15837efRichard Smith      uint64_t Index = Sub.Entries[I].ArrayIndex;
156486024013d4c3728122c58fa07a2a67e6c15837efRichard Smith      if (Index > 1) {
15655cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith        Info.Diag(E, Info.getLangOpts().CPlusPlus0x ?
156686024013d4c3728122c58fa07a2a67e6c15837efRichard Smith                    (unsigned)diag::note_constexpr_read_past_end :
156786024013d4c3728122c58fa07a2a67e6c15837efRichard Smith                    (unsigned)diag::note_invalid_subexpr_in_const_expr);
156886024013d4c3728122c58fa07a2a67e6c15837efRichard Smith        return false;
156986024013d4c3728122c58fa07a2a67e6c15837efRichard Smith      }
157086024013d4c3728122c58fa07a2a67e6c15837efRichard Smith      assert(I == N - 1 && "extracting subobject of scalar?");
157186024013d4c3728122c58fa07a2a67e6c15837efRichard Smith      if (O->isComplexInt()) {
15721aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith        Obj = APValue(Index ? O->getComplexIntImag()
157386024013d4c3728122c58fa07a2a67e6c15837efRichard Smith                            : O->getComplexIntReal());
157486024013d4c3728122c58fa07a2a67e6c15837efRichard Smith      } else {
157586024013d4c3728122c58fa07a2a67e6c15837efRichard Smith        assert(O->isComplexFloat());
15761aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith        Obj = APValue(Index ? O->getComplexFloatImag()
157786024013d4c3728122c58fa07a2a67e6c15837efRichard Smith                            : O->getComplexFloatReal());
157886024013d4c3728122c58fa07a2a67e6c15837efRichard Smith      }
157986024013d4c3728122c58fa07a2a67e6c15837efRichard Smith      return true;
1580180f47959a066795cc0f409433023af448bb0328Richard Smith    } else if (const FieldDecl *Field = getAsField(Sub.Entries[I])) {
1581b4e5e286a5cd156247720b1eb204abaa8e09568dRichard Smith      if (Field->isMutable()) {
15825cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith        Info.Diag(E, diag::note_constexpr_ltor_mutable, 1)
1583b4e5e286a5cd156247720b1eb204abaa8e09568dRichard Smith          << Field;
1584b4e5e286a5cd156247720b1eb204abaa8e09568dRichard Smith        Info.Note(Field->getLocation(), diag::note_declared_at);
1585b4e5e286a5cd156247720b1eb204abaa8e09568dRichard Smith        return false;
1586b4e5e286a5cd156247720b1eb204abaa8e09568dRichard Smith      }
1587b4e5e286a5cd156247720b1eb204abaa8e09568dRichard Smith
1588180f47959a066795cc0f409433023af448bb0328Richard Smith      // Next subobject is a class, struct or union field.
1589180f47959a066795cc0f409433023af448bb0328Richard Smith      RecordDecl *RD = ObjType->castAs<RecordType>()->getDecl();
1590180f47959a066795cc0f409433023af448bb0328Richard Smith      if (RD->isUnion()) {
1591180f47959a066795cc0f409433023af448bb0328Richard Smith        const FieldDecl *UnionField = O->getUnionField();
1592180f47959a066795cc0f409433023af448bb0328Richard Smith        if (!UnionField ||
1593f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith            UnionField->getCanonicalDecl() != Field->getCanonicalDecl()) {
15945cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith          Info.Diag(E, diag::note_constexpr_read_inactive_union_member)
15957098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith            << Field << !UnionField << UnionField;
1596180f47959a066795cc0f409433023af448bb0328Richard Smith          return false;
1597f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        }
1598180f47959a066795cc0f409433023af448bb0328Richard Smith        O = &O->getUnionValue();
1599180f47959a066795cc0f409433023af448bb0328Richard Smith      } else
1600180f47959a066795cc0f409433023af448bb0328Richard Smith        O = &O->getStructField(Field->getFieldIndex());
1601180f47959a066795cc0f409433023af448bb0328Richard Smith      ObjType = Field->getType();
16027098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith
16037098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      if (ObjType.isVolatileQualified()) {
16047098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        if (Info.getLangOpts().CPlusPlus) {
16057098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith          // FIXME: Include a description of the path to the volatile subobject.
16065cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith          Info.Diag(E, diag::note_constexpr_ltor_volatile_obj, 1)
16077098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith            << 2 << Field;
16087098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith          Info.Note(Field->getLocation(), diag::note_declared_at);
16097098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        } else {
16105cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith          Info.Diag(E, diag::note_invalid_subexpr_in_const_expr);
16117098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        }
16127098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        return false;
16137098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      }
1614cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    } else {
1615180f47959a066795cc0f409433023af448bb0328Richard Smith      // Next subobject is a base class.
161659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      const CXXRecordDecl *Derived = ObjType->getAsCXXRecordDecl();
161759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      const CXXRecordDecl *Base = getAsBaseClass(Sub.Entries[I]);
161859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      O = &O->getStructBase(getBaseIndex(Derived, Base));
161959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      ObjType = Info.Ctx.getRecordType(Base);
1620cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    }
1621180f47959a066795cc0f409433023af448bb0328Richard Smith
1622f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (O->isUninit()) {
1623745f5147e065900267c85a5568785a1991d4838fRichard Smith      if (!Info.CheckingPotentialConstantExpression)
16245cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith        Info.Diag(E, diag::note_constexpr_read_uninit);
1625180f47959a066795cc0f409433023af448bb0328Richard Smith      return false;
1626f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    }
1627cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  }
1628cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
16290069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith  // This may look super-stupid, but it serves an important purpose: if we just
16300069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith  // swapped Obj and *O, we'd create an object which had itself as a subobject.
16310069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith  // To avoid the leak, we ensure that Tmp ends up owning the original complete
16320069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith  // object, which is destroyed by Tmp's destructor.
16330069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith  APValue Tmp;
16340069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith  O->swap(Tmp);
16350069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith  Obj.swap(Tmp);
1636cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  return true;
1637cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith}
1638cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
1639f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith/// Find the position where two subobject designators diverge, or equivalently
1640f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith/// the length of the common initial subsequence.
1641f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smithstatic unsigned FindDesignatorMismatch(QualType ObjType,
1642f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith                                       const SubobjectDesignator &A,
1643f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith                                       const SubobjectDesignator &B,
1644f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith                                       bool &WasArrayIndex) {
1645f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  unsigned I = 0, N = std::min(A.Entries.size(), B.Entries.size());
1646f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  for (/**/; I != N; ++I) {
164786024013d4c3728122c58fa07a2a67e6c15837efRichard Smith    if (!ObjType.isNull() &&
164886024013d4c3728122c58fa07a2a67e6c15837efRichard Smith        (ObjType->isArrayType() || ObjType->isAnyComplexType())) {
1649f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      // Next subobject is an array element.
1650f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      if (A.Entries[I].ArrayIndex != B.Entries[I].ArrayIndex) {
1651f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        WasArrayIndex = true;
1652f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        return I;
1653f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      }
165486024013d4c3728122c58fa07a2a67e6c15837efRichard Smith      if (ObjType->isAnyComplexType())
165586024013d4c3728122c58fa07a2a67e6c15837efRichard Smith        ObjType = ObjType->castAs<ComplexType>()->getElementType();
165686024013d4c3728122c58fa07a2a67e6c15837efRichard Smith      else
165786024013d4c3728122c58fa07a2a67e6c15837efRichard Smith        ObjType = ObjType->castAsArrayTypeUnsafe()->getElementType();
1658f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    } else {
1659f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      if (A.Entries[I].BaseOrMember != B.Entries[I].BaseOrMember) {
1660f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        WasArrayIndex = false;
1661f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        return I;
1662f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      }
1663f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      if (const FieldDecl *FD = getAsField(A.Entries[I]))
1664f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        // Next subobject is a field.
1665f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        ObjType = FD->getType();
1666f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      else
1667f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        // Next subobject is a base class.
1668f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        ObjType = QualType();
1669f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    }
1670f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  }
1671f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  WasArrayIndex = false;
1672f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  return I;
1673f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith}
1674f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
1675f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith/// Determine whether the given subobject designators refer to elements of the
1676f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith/// same array object.
1677f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smithstatic bool AreElementsOfSameArray(QualType ObjType,
1678f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith                                   const SubobjectDesignator &A,
1679f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith                                   const SubobjectDesignator &B) {
1680f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  if (A.Entries.size() != B.Entries.size())
1681f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    return false;
1682f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
1683f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  bool IsArray = A.MostDerivedArraySize != 0;
1684f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  if (IsArray && A.MostDerivedPathLength != A.Entries.size())
1685f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    // A is a subobject of the array element.
1686f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    return false;
1687f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
1688f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  // If A (and B) designates an array element, the last entry will be the array
1689f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  // index. That doesn't have to match. Otherwise, we're in the 'implicit array
1690f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  // of length 1' case, and the entire path must match.
1691f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  bool WasArrayIndex;
1692f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  unsigned CommonLength = FindDesignatorMismatch(ObjType, A, B, WasArrayIndex);
1693f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  return CommonLength >= A.Entries.size() - IsArray;
1694f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith}
1695f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
1696180f47959a066795cc0f409433023af448bb0328Richard Smith/// HandleLValueToRValueConversion - Perform an lvalue-to-rvalue conversion on
1697180f47959a066795cc0f409433023af448bb0328Richard Smith/// the given lvalue. This can also be used for 'lvalue-to-lvalue' conversions
1698180f47959a066795cc0f409433023af448bb0328Richard Smith/// for looking up the glvalue referred to by an entity of reference type.
1699180f47959a066795cc0f409433023af448bb0328Richard Smith///
1700180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param Info - Information about the ongoing evaluation.
1701f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// \param Conv - The expression for which we are performing the conversion.
1702f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith///               Used for diagnostics.
17039ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith/// \param Type - The type we expect this conversion to produce, before
17049ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith///               stripping cv-qualifiers in the case of a non-clas type.
1705180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param LVal - The glvalue on which we are attempting to perform this action.
1706180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param RVal - The produced value will be placed here.
1707f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool HandleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv,
1708f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                                           QualType Type,
17091aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith                                           const LValue &LVal, APValue &RVal) {
1710b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (LVal.Designator.Invalid)
1711b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    // A diagnostic will have already been produced.
1712b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    return false;
1713b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
17141bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  const Expr *Base = LVal.Base.dyn_cast<const Expr*>();
1715c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
1716f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (!LVal.Base) {
1717f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    // FIXME: Indirection through a null pointer deserves a specific diagnostic.
17185cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.Diag(Conv, diag::note_invalid_subexpr_in_const_expr);
17197098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    return false;
17207098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  }
17217098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith
172283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  CallStackFrame *Frame = 0;
172383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  if (LVal.CallIndex) {
172483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    Frame = Info.getCallFrame(LVal.CallIndex);
172583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    if (!Frame) {
17265cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      Info.Diag(Conv, diag::note_constexpr_lifetime_ended, 1) << !Base;
172783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      NoteLValueLocation(Info, LVal.Base);
172883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      return false;
172983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    }
173083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  }
173183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith
17327098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  // C++11 DR1311: An lvalue-to-rvalue conversion on a volatile-qualified type
17337098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  // is not a constant expression (even if the object is non-volatile). We also
17347098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  // apply this rule to C++98, in order to conform to the expected 'volatile'
17357098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  // semantics.
17367098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  if (Type.isVolatileQualified()) {
17377098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    if (Info.getLangOpts().CPlusPlus)
17385cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      Info.Diag(Conv, diag::note_constexpr_ltor_volatile_type) << Type;
17397098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    else
17405cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      Info.Diag(Conv);
1741c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return false;
1742f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
1743c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
17441bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  if (const ValueDecl *D = LVal.Base.dyn_cast<const ValueDecl*>()) {
1745c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    // In C++98, const, non-volatile integers initialized with ICEs are ICEs.
1746c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    // In C++11, constexpr, non-volatile variables initialized with constant
1747d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    // expressions are constant expressions too. Inside constexpr functions,
1748d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    // parameters are constant expressions even if they're non-const.
1749c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    // In C, such things can also be folded, although they are not ICEs.
1750c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    const VarDecl *VD = dyn_cast<VarDecl>(D);
1751d2008e2c80d6c9282044ec873a937a17a0f33579Douglas Gregor    if (VD) {
1752d2008e2c80d6c9282044ec873a937a17a0f33579Douglas Gregor      if (const VarDecl *VDef = VD->getDefinition(Info.Ctx))
1753d2008e2c80d6c9282044ec873a937a17a0f33579Douglas Gregor        VD = VDef;
1754d2008e2c80d6c9282044ec873a937a17a0f33579Douglas Gregor    }
1755f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!VD || VD->isInvalidDecl()) {
17565cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      Info.Diag(Conv);
17570a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      return false;
1758f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    }
1759f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
17607098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    // DR1313: If the object is volatile-qualified but the glvalue was not,
17617098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    // behavior is undefined so the result is not a constant expression.
17621bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    QualType VT = VD->getType();
17637098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    if (VT.isVolatileQualified()) {
17647098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      if (Info.getLangOpts().CPlusPlus) {
17655cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith        Info.Diag(Conv, diag::note_constexpr_ltor_volatile_obj, 1) << 1 << VD;
17667098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        Info.Note(VD->getLocation(), diag::note_declared_at);
17677098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      } else {
17685cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith        Info.Diag(Conv);
1769f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      }
17707098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      return false;
17717098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    }
17727098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith
17737098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    if (!isa<ParmVarDecl>(VD)) {
17747098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      if (VD->isConstexpr()) {
17757098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        // OK, we can read this variable.
17767098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      } else if (VT->isIntegralOrEnumerationType()) {
17777098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        if (!VT.isConstQualified()) {
17787098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith          if (Info.getLangOpts().CPlusPlus) {
17795cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith            Info.Diag(Conv, diag::note_constexpr_ltor_non_const_int, 1) << VD;
17807098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith            Info.Note(VD->getLocation(), diag::note_declared_at);
17817098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith          } else {
17825cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith            Info.Diag(Conv);
17837098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith          }
17847098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith          return false;
17857098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        }
17867098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      } else if (VT->isFloatingType() && VT.isConstQualified()) {
17877098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        // We support folding of const floating-point types, in order to make
17887098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        // static const data members of such types (supported as an extension)
17897098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        // more useful.
17907098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        if (Info.getLangOpts().CPlusPlus0x) {
17915cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith          Info.CCEDiag(Conv, diag::note_constexpr_ltor_non_constexpr, 1) << VD;
17927098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith          Info.Note(VD->getLocation(), diag::note_declared_at);
17937098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        } else {
17945cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith          Info.CCEDiag(Conv);
17957098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        }
17967098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      } else {
17977098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        // FIXME: Allow folding of values of any literal type in all languages.
17987098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        if (Info.getLangOpts().CPlusPlus0x) {
17995cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith          Info.Diag(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.Diag(Conv);
18037098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        }
18040a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith        return false;
1805f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      }
18060a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    }
18077098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith
1808f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!EvaluateVarDeclInit(Info, Conv, VD, Frame, RVal))
1809c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith      return false;
1810c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
181147a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    if (isa<ParmVarDecl>(VD) || !VD->getAnyInitializer()->isLValue())
1812f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return ExtractSubobject(Info, Conv, RVal, VT, LVal.Designator, Type);
1813c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
1814c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    // The declaration was initialized by an lvalue, with no lvalue-to-rvalue
1815c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    // conversion. This happens when the declaration and the lvalue should be
1816c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    // considered synonymous, for instance when initializing an array of char
1817c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    // from a string literal. Continue as if the initializer lvalue was the
1818c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    // value we were originally given.
18190a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    assert(RVal.getLValueOffset().isZero() &&
18200a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith           "offset for lvalue init of non-reference");
18211bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    Base = RVal.getLValueBase().get<const Expr*>();
182283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith
182383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    if (unsigned CallIndex = RVal.getLValueCallIndex()) {
182483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      Frame = Info.getCallFrame(CallIndex);
182583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      if (!Frame) {
18265cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith        Info.Diag(Conv, diag::note_constexpr_lifetime_ended, 1) << !Base;
182783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith        NoteLValueLocation(Info, RVal.getLValueBase());
182883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith        return false;
182983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      }
183083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    } else {
183183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      Frame = 0;
183283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    }
1833c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  }
1834c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
18357098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  // Volatile temporary objects cannot be read in constant expressions.
18367098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  if (Base->getType().isVolatileQualified()) {
18377098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    if (Info.getLangOpts().CPlusPlus) {
18385cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      Info.Diag(Conv, diag::note_constexpr_ltor_volatile_obj, 1) << 0;
18397098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      Info.Note(Base->getExprLoc(), diag::note_constexpr_temporary_here);
18407098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    } else {
18415cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      Info.Diag(Conv);
18427098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    }
18437098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    return false;
18447098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  }
18457098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith
1846177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith  if (Frame) {
1847cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    // If this is a temporary expression with a nontrivial initializer, grab the
1848cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    // value from the relevant stack frame.
1849177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith    RVal = Frame->Temporaries[Base];
1850cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  } else if (const CompoundLiteralExpr *CLE
1851cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith             = dyn_cast<CompoundLiteralExpr>(Base)) {
1852cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    // In C99, a CompoundLiteralExpr is an lvalue, and we defer evaluating the
1853cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    // initializer until now for such expressions. Such an expression can't be
1854cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    // an ICE in C, so this only matters for fold.
1855c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?");
1856cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    if (!Evaluate(RVal, Info, CLE->getInitializer()))
1857cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith      return false;
1858f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith  } else if (isa<StringLiteral>(Base)) {
1859f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith    // We represent a string literal array as an lvalue pointing at the
1860f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith    // corresponding expression, rather than building an array of chars.
1861f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith    // FIXME: Support PredefinedExpr, ObjCEncodeExpr, MakeStringConstant
18621aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    RVal = APValue(Base, CharUnits::Zero(), APValue::NoLValuePath(), 0);
1863f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  } else {
18645cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.Diag(Conv, diag::note_invalid_subexpr_in_const_expr);
1865cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    return false;
1866f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
1867c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
1868f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  return ExtractSubobject(Info, Conv, RVal, Base->getType(), LVal.Designator,
1869f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                          Type);
1870c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith}
1871c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
187259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith/// Build an lvalue for the object argument of a member function call.
187359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithstatic bool EvaluateObjectArgument(EvalInfo &Info, const Expr *Object,
187459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith                                   LValue &This) {
187559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  if (Object->getType()->isPointerType())
187659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    return EvaluatePointer(Object, This, Info);
187759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
187859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  if (Object->isGLValue())
187959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    return EvaluateLValue(Object, This, Info);
188059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
1881e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (Object->getType()->isLiteralType())
1882e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return EvaluateTemporary(Object, This, Info);
1883e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1884e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  return false;
1885e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}
1886e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1887e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// HandleMemberPointerAccess - Evaluate a member access operation and build an
1888e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// lvalue referring to the result.
1889e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith///
1890e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// \param Info - Information about the ongoing evaluation.
1891e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// \param BO - The member pointer access operation.
1892e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// \param LV - Filled in with a reference to the resulting object.
1893e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// \param IncludeMember - Specifies whether the member itself is included in
1894e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith///        the resulting LValue subobject designator. This is not possible when
1895e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith///        creating a bound member function.
1896e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// \return The field or method declaration to which the member pointer refers,
1897e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith///         or 0 if evaluation fails.
1898e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info,
1899e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                                                  const BinaryOperator *BO,
1900e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                                                  LValue &LV,
1901e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                                                  bool IncludeMember = true) {
1902e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  assert(BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI);
1903e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1904745f5147e065900267c85a5568785a1991d4838fRichard Smith  bool EvalObjOK = EvaluateObjectArgument(Info, BO->getLHS(), LV);
1905745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!EvalObjOK && !Info.keepEvaluatingAfterFailure())
1906e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return 0;
1907e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1908e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  MemberPtr MemPtr;
1909e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (!EvaluateMemberPointer(BO->getRHS(), MemPtr, Info))
1910e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return 0;
1911e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1912e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // C++11 [expr.mptr.oper]p6: If the second operand is the null pointer to
1913e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // member value, the behavior is undefined.
1914e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (!MemPtr.getDecl())
1915e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return 0;
1916e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1917745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!EvalObjOK)
1918745f5147e065900267c85a5568785a1991d4838fRichard Smith    return 0;
1919745f5147e065900267c85a5568785a1991d4838fRichard Smith
1920e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (MemPtr.isDerivedMember()) {
1921e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // This is a member of some derived class. Truncate LV appropriately.
1922e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // The end of the derived-to-base path for the base object must match the
1923e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // derived-to-base path for the member pointer.
1924b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    if (LV.Designator.MostDerivedPathLength + MemPtr.Path.size() >
1925e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        LV.Designator.Entries.size())
1926e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return 0;
1927e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    unsigned PathLengthToMember =
1928e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        LV.Designator.Entries.size() - MemPtr.Path.size();
1929e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    for (unsigned I = 0, N = MemPtr.Path.size(); I != N; ++I) {
1930e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      const CXXRecordDecl *LVDecl = getAsBaseClass(
1931e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith          LV.Designator.Entries[PathLengthToMember + I]);
1932e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      const CXXRecordDecl *MPDecl = MemPtr.Path[I];
1933e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (LVDecl->getCanonicalDecl() != MPDecl->getCanonicalDecl())
1934e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return 0;
1935e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
1936e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1937e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // Truncate the lvalue to the appropriate derived class.
1938b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    if (!CastToDerivedClass(Info, BO, LV, MemPtr.getContainingRecord(),
1939b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                            PathLengthToMember))
1940b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      return 0;
1941e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  } else if (!MemPtr.Path.empty()) {
1942e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // Extend the LValue path with the member pointer's path.
1943e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    LV.Designator.Entries.reserve(LV.Designator.Entries.size() +
1944e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                                  MemPtr.Path.size() + IncludeMember);
1945e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1946e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // Walk down to the appropriate base class.
1947e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    QualType LVType = BO->getLHS()->getType();
1948e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (const PointerType *PT = LVType->getAs<PointerType>())
1949e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      LVType = PT->getPointeeType();
1950e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    const CXXRecordDecl *RD = LVType->getAsCXXRecordDecl();
1951e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    assert(RD && "member pointer access on non-class-type expression");
1952e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // The first class in the path is that of the lvalue.
1953e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    for (unsigned I = 1, N = MemPtr.Path.size(); I != N; ++I) {
1954e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      const CXXRecordDecl *Base = MemPtr.Path[N - I - 1];
19558d59deec807ed53efcd07855199cdc9c979f447fJohn McCall      if (!HandleLValueDirectBase(Info, BO, LV, RD, Base))
19568d59deec807ed53efcd07855199cdc9c979f447fJohn McCall        return 0;
1957e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      RD = Base;
1958e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
1959e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // Finally cast to the class containing the member.
19608d59deec807ed53efcd07855199cdc9c979f447fJohn McCall    if (!HandleLValueDirectBase(Info, BO, LV, RD, MemPtr.getContainingRecord()))
19618d59deec807ed53efcd07855199cdc9c979f447fJohn McCall      return 0;
1962e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
1963e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1964e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // Add the member. Note that we cannot build bound member functions here.
1965e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (IncludeMember) {
19668d59deec807ed53efcd07855199cdc9c979f447fJohn McCall    if (const FieldDecl *FD = dyn_cast<FieldDecl>(MemPtr.getDecl())) {
19678d59deec807ed53efcd07855199cdc9c979f447fJohn McCall      if (!HandleLValueMember(Info, BO, LV, FD))
19688d59deec807ed53efcd07855199cdc9c979f447fJohn McCall        return 0;
19698d59deec807ed53efcd07855199cdc9c979f447fJohn McCall    } else if (const IndirectFieldDecl *IFD =
19708d59deec807ed53efcd07855199cdc9c979f447fJohn McCall                 dyn_cast<IndirectFieldDecl>(MemPtr.getDecl())) {
19718d59deec807ed53efcd07855199cdc9c979f447fJohn McCall      if (!HandleLValueIndirectMember(Info, BO, LV, IFD))
19728d59deec807ed53efcd07855199cdc9c979f447fJohn McCall        return 0;
19738d59deec807ed53efcd07855199cdc9c979f447fJohn McCall    } else {
1974d9b02e726262e4009dda830998bb934172ac0020Richard Smith      llvm_unreachable("can't construct reference to bound member function");
19758d59deec807ed53efcd07855199cdc9c979f447fJohn McCall    }
1976e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
1977e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1978e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  return MemPtr.getDecl();
1979e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}
1980e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1981e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// HandleBaseToDerivedCast - Apply the given base-to-derived cast operation on
1982e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// the provided lvalue, which currently refers to the base object.
1983e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E,
1984e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                                    LValue &Result) {
1985e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  SubobjectDesignator &D = Result.Designator;
1986b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (D.Invalid || !Result.checkNullPointer(Info, E, CSK_Derived))
1987e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return false;
1988e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1989e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  QualType TargetQT = E->getType();
1990e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (const PointerType *PT = TargetQT->getAs<PointerType>())
1991e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    TargetQT = PT->getPointeeType();
1992b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
1993b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // Check this cast lands within the final derived-to-base subobject path.
1994b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (D.MostDerivedPathLength + E->path_size() > D.Entries.size()) {
19955cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.CCEDiag(E, diag::note_constexpr_invalid_downcast)
1996b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      << D.MostDerivedType << TargetQT;
1997b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    return false;
1998b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  }
1999b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
2000b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // Check the type of the final cast. We don't need to check the path,
2001b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // since a cast can only be formed if the path is unique.
2002b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  unsigned NewEntriesSize = D.Entries.size() - E->path_size();
2003e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  const CXXRecordDecl *TargetType = TargetQT->getAsCXXRecordDecl();
2004e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  const CXXRecordDecl *FinalType;
2005b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (NewEntriesSize == D.MostDerivedPathLength)
2006b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    FinalType = D.MostDerivedType->getAsCXXRecordDecl();
2007b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  else
2008e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    FinalType = getAsBaseClass(D.Entries[NewEntriesSize - 1]);
2009b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl()) {
20105cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.CCEDiag(E, diag::note_constexpr_invalid_downcast)
2011b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      << D.MostDerivedType << TargetQT;
2012e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return false;
2013b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  }
2014e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2015e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // Truncate the lvalue to the appropriate derived class.
2016b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  return CastToDerivedClass(Info, E, Result, TargetType, NewEntriesSize);
201759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
201859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
2019c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stumpnamespace {
2020d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smithenum EvalStmtResult {
2021d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  /// Evaluation failed.
2022d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  ESR_Failed,
2023d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  /// Hit a 'return' statement.
2024d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  ESR_Returned,
2025d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  /// Evaluation succeeded.
2026d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  ESR_Succeeded
2027d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith};
2028d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith}
2029d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
2030d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith// Evaluate a statement.
20311aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smithstatic EvalStmtResult EvaluateStmt(APValue &Result, EvalInfo &Info,
2032d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith                                   const Stmt *S) {
2033d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  switch (S->getStmtClass()) {
2034d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  default:
2035d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    return ESR_Failed;
2036d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
2037d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  case Stmt::NullStmtClass:
2038d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  case Stmt::DeclStmtClass:
2039d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    return ESR_Succeeded;
2040d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
2041c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  case Stmt::ReturnStmtClass: {
2042c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    const Expr *RetExpr = cast<ReturnStmt>(S)->getRetValue();
204383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    if (!Evaluate(Result, Info, RetExpr))
2044c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      return ESR_Failed;
2045c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return ESR_Returned;
2046c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  }
2047d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
2048d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  case Stmt::CompoundStmtClass: {
2049d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    const CompoundStmt *CS = cast<CompoundStmt>(S);
2050d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    for (CompoundStmt::const_body_iterator BI = CS->body_begin(),
2051d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith           BE = CS->body_end(); BI != BE; ++BI) {
2052d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith      EvalStmtResult ESR = EvaluateStmt(Result, Info, *BI);
2053d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith      if (ESR != ESR_Succeeded)
2054d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith        return ESR;
2055d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    }
2056d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    return ESR_Succeeded;
2057d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  }
2058d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  }
2059d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith}
2060d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
20616180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith/// CheckTrivialDefaultConstructor - Check whether a constructor is a trivial
20626180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith/// default constructor. If so, we'll fold it whether or not it's marked as
20636180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith/// constexpr. If it is marked as constexpr, we will never implicitly define it,
20646180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith/// so we need special handling.
20656180245e9f63d2927b185ec251fb75aba30f1cacRichard Smithstatic bool CheckTrivialDefaultConstructor(EvalInfo &Info, SourceLocation Loc,
206651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith                                           const CXXConstructorDecl *CD,
206751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith                                           bool IsValueInitialization) {
20686180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith  if (!CD->isTrivial() || !CD->isDefaultConstructor())
20696180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    return false;
20706180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith
20714c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  // Value-initialization does not call a trivial default constructor, so such a
20724c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  // call is a core constant expression whether or not the constructor is
20734c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  // constexpr.
20744c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  if (!CD->isConstexpr() && !IsValueInitialization) {
20756180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    if (Info.getLangOpts().CPlusPlus0x) {
20764c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith      // FIXME: If DiagDecl is an implicitly-declared special member function,
20774c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith      // we should be much more explicit about why it's not constexpr.
20784c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith      Info.CCEDiag(Loc, diag::note_constexpr_invalid_function, 1)
20794c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith        << /*IsConstexpr*/0 << /*IsConstructor*/1 << CD;
20804c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith      Info.Note(CD->getLocation(), diag::note_declared_at);
20816180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    } else {
20826180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith      Info.CCEDiag(Loc, diag::note_invalid_subexpr_in_const_expr);
20836180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    }
20846180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith  }
20856180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith  return true;
20866180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith}
20876180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith
2088c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith/// CheckConstexprFunction - Check that a function can be called in a constant
2089c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith/// expression.
2090c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstatic bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc,
2091c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                   const FunctionDecl *Declaration,
2092c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                   const FunctionDecl *Definition) {
2093745f5147e065900267c85a5568785a1991d4838fRichard Smith  // Potential constant expressions can contain calls to declared, but not yet
2094745f5147e065900267c85a5568785a1991d4838fRichard Smith  // defined, constexpr functions.
2095745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (Info.CheckingPotentialConstantExpression && !Definition &&
2096745f5147e065900267c85a5568785a1991d4838fRichard Smith      Declaration->isConstexpr())
2097745f5147e065900267c85a5568785a1991d4838fRichard Smith    return false;
2098745f5147e065900267c85a5568785a1991d4838fRichard Smith
2099c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  // Can we evaluate this function call?
2100c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  if (Definition && Definition->isConstexpr() && !Definition->isInvalidDecl())
2101c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return true;
2102c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
2103c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  if (Info.getLangOpts().CPlusPlus0x) {
2104c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    const FunctionDecl *DiagDecl = Definition ? Definition : Declaration;
2105099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    // FIXME: If DiagDecl is an implicitly-declared special member function, we
2106099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    // should be much more explicit about why it's not constexpr.
2107c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    Info.Diag(CallLoc, diag::note_constexpr_invalid_function, 1)
2108c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      << DiagDecl->isConstexpr() << isa<CXXConstructorDecl>(DiagDecl)
2109c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      << DiagDecl;
2110c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    Info.Note(DiagDecl->getLocation(), diag::note_declared_at);
2111c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  } else {
2112c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    Info.Diag(CallLoc, diag::note_invalid_subexpr_in_const_expr);
2113c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  }
2114c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  return false;
2115c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
2116c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
2117180f47959a066795cc0f409433023af448bb0328Richard Smithnamespace {
21181aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smithtypedef SmallVector<APValue, 8> ArgVector;
2119180f47959a066795cc0f409433023af448bb0328Richard Smith}
2120180f47959a066795cc0f409433023af448bb0328Richard Smith
2121180f47959a066795cc0f409433023af448bb0328Richard Smith/// EvaluateArgs - Evaluate the arguments to a function call.
2122180f47959a066795cc0f409433023af448bb0328Richard Smithstatic bool EvaluateArgs(ArrayRef<const Expr*> Args, ArgVector &ArgValues,
2123180f47959a066795cc0f409433023af448bb0328Richard Smith                         EvalInfo &Info) {
2124745f5147e065900267c85a5568785a1991d4838fRichard Smith  bool Success = true;
2125180f47959a066795cc0f409433023af448bb0328Richard Smith  for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end();
2126745f5147e065900267c85a5568785a1991d4838fRichard Smith       I != E; ++I) {
2127745f5147e065900267c85a5568785a1991d4838fRichard Smith    if (!Evaluate(ArgValues[I - Args.begin()], Info, *I)) {
2128745f5147e065900267c85a5568785a1991d4838fRichard Smith      // If we're checking for a potential constant expression, evaluate all
2129745f5147e065900267c85a5568785a1991d4838fRichard Smith      // initializers even if some of them fail.
2130745f5147e065900267c85a5568785a1991d4838fRichard Smith      if (!Info.keepEvaluatingAfterFailure())
2131745f5147e065900267c85a5568785a1991d4838fRichard Smith        return false;
2132745f5147e065900267c85a5568785a1991d4838fRichard Smith      Success = false;
2133745f5147e065900267c85a5568785a1991d4838fRichard Smith    }
2134745f5147e065900267c85a5568785a1991d4838fRichard Smith  }
2135745f5147e065900267c85a5568785a1991d4838fRichard Smith  return Success;
2136180f47959a066795cc0f409433023af448bb0328Richard Smith}
2137180f47959a066795cc0f409433023af448bb0328Richard Smith
2138d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith/// Evaluate a function call.
2139745f5147e065900267c85a5568785a1991d4838fRichard Smithstatic bool HandleFunctionCall(SourceLocation CallLoc,
2140745f5147e065900267c85a5568785a1991d4838fRichard Smith                               const FunctionDecl *Callee, const LValue *This,
2141f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                               ArrayRef<const Expr*> Args, const Stmt *Body,
21421aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith                               EvalInfo &Info, APValue &Result) {
2143180f47959a066795cc0f409433023af448bb0328Richard Smith  ArgVector ArgValues(Args.size());
2144180f47959a066795cc0f409433023af448bb0328Richard Smith  if (!EvaluateArgs(Args, ArgValues, Info))
2145180f47959a066795cc0f409433023af448bb0328Richard Smith    return false;
2146d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
2147745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!Info.CheckCallLimit(CallLoc))
2148745f5147e065900267c85a5568785a1991d4838fRichard Smith    return false;
2149745f5147e065900267c85a5568785a1991d4838fRichard Smith
2150745f5147e065900267c85a5568785a1991d4838fRichard Smith  CallStackFrame Frame(Info, CallLoc, Callee, This, ArgValues.data());
2151d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  return EvaluateStmt(Result, Info, Body) == ESR_Returned;
2152d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith}
2153d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
2154180f47959a066795cc0f409433023af448bb0328Richard Smith/// Evaluate a constructor call.
2155745f5147e065900267c85a5568785a1991d4838fRichard Smithstatic bool HandleConstructorCall(SourceLocation CallLoc, const LValue &This,
215659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith                                  ArrayRef<const Expr*> Args,
2157180f47959a066795cc0f409433023af448bb0328Richard Smith                                  const CXXConstructorDecl *Definition,
215851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith                                  EvalInfo &Info, APValue &Result) {
2159180f47959a066795cc0f409433023af448bb0328Richard Smith  ArgVector ArgValues(Args.size());
2160180f47959a066795cc0f409433023af448bb0328Richard Smith  if (!EvaluateArgs(Args, ArgValues, Info))
2161180f47959a066795cc0f409433023af448bb0328Richard Smith    return false;
2162180f47959a066795cc0f409433023af448bb0328Richard Smith
2163745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!Info.CheckCallLimit(CallLoc))
2164745f5147e065900267c85a5568785a1991d4838fRichard Smith    return false;
2165745f5147e065900267c85a5568785a1991d4838fRichard Smith
216686c3ae46250cdcc57778c27826060779a92f3815Richard Smith  const CXXRecordDecl *RD = Definition->getParent();
216786c3ae46250cdcc57778c27826060779a92f3815Richard Smith  if (RD->getNumVBases()) {
216886c3ae46250cdcc57778c27826060779a92f3815Richard Smith    Info.Diag(CallLoc, diag::note_constexpr_virtual_base) << RD;
216986c3ae46250cdcc57778c27826060779a92f3815Richard Smith    return false;
217086c3ae46250cdcc57778c27826060779a92f3815Richard Smith  }
217186c3ae46250cdcc57778c27826060779a92f3815Richard Smith
2172745f5147e065900267c85a5568785a1991d4838fRichard Smith  CallStackFrame Frame(Info, CallLoc, Definition, &This, ArgValues.data());
2173180f47959a066795cc0f409433023af448bb0328Richard Smith
2174180f47959a066795cc0f409433023af448bb0328Richard Smith  // If it's a delegating constructor, just delegate.
2175180f47959a066795cc0f409433023af448bb0328Richard Smith  if (Definition->isDelegatingConstructor()) {
2176180f47959a066795cc0f409433023af448bb0328Richard Smith    CXXConstructorDecl::init_const_iterator I = Definition->init_begin();
217783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return EvaluateInPlace(Result, Info, This, (*I)->getInit());
2178180f47959a066795cc0f409433023af448bb0328Richard Smith  }
2179180f47959a066795cc0f409433023af448bb0328Richard Smith
2180610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith  // For a trivial copy or move constructor, perform an APValue copy. This is
2181610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith  // essential for unions, where the operations performed by the constructor
2182610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith  // cannot be represented by ctor-initializers.
2183610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith  if (Definition->isDefaulted() &&
2184f6cfe8ba2b4d98c20181568e449edf0b60904b03Douglas Gregor      ((Definition->isCopyConstructor() && Definition->isTrivial()) ||
2185f6cfe8ba2b4d98c20181568e449edf0b60904b03Douglas Gregor       (Definition->isMoveConstructor() && Definition->isTrivial()))) {
2186610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith    LValue RHS;
21871aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    RHS.setFrom(Info.Ctx, ArgValues[0]);
21881aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    return HandleLValueToRValueConversion(Info, Args[0], Args[0]->getType(),
21891aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith                                          RHS, Result);
2190610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith  }
2191610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith
2192610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith  // Reserve space for the struct members.
219351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (!RD->isUnion() && Result.isUninit())
2194180f47959a066795cc0f409433023af448bb0328Richard Smith    Result = APValue(APValue::UninitStruct(), RD->getNumBases(),
2195180f47959a066795cc0f409433023af448bb0328Richard Smith                     std::distance(RD->field_begin(), RD->field_end()));
2196180f47959a066795cc0f409433023af448bb0328Richard Smith
21978d59deec807ed53efcd07855199cdc9c979f447fJohn McCall  if (RD->isInvalidDecl()) return false;
2198180f47959a066795cc0f409433023af448bb0328Richard Smith  const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD);
2199180f47959a066795cc0f409433023af448bb0328Richard Smith
2200745f5147e065900267c85a5568785a1991d4838fRichard Smith  bool Success = true;
2201180f47959a066795cc0f409433023af448bb0328Richard Smith  unsigned BasesSeen = 0;
2202180f47959a066795cc0f409433023af448bb0328Richard Smith#ifndef NDEBUG
2203180f47959a066795cc0f409433023af448bb0328Richard Smith  CXXRecordDecl::base_class_const_iterator BaseIt = RD->bases_begin();
2204180f47959a066795cc0f409433023af448bb0328Richard Smith#endif
2205180f47959a066795cc0f409433023af448bb0328Richard Smith  for (CXXConstructorDecl::init_const_iterator I = Definition->init_begin(),
2206180f47959a066795cc0f409433023af448bb0328Richard Smith       E = Definition->init_end(); I != E; ++I) {
2207745f5147e065900267c85a5568785a1991d4838fRichard Smith    LValue Subobject = This;
2208745f5147e065900267c85a5568785a1991d4838fRichard Smith    APValue *Value = &Result;
2209745f5147e065900267c85a5568785a1991d4838fRichard Smith
2210745f5147e065900267c85a5568785a1991d4838fRichard Smith    // Determine the subobject to initialize.
2211180f47959a066795cc0f409433023af448bb0328Richard Smith    if ((*I)->isBaseInitializer()) {
2212180f47959a066795cc0f409433023af448bb0328Richard Smith      QualType BaseType((*I)->getBaseClass(), 0);
2213180f47959a066795cc0f409433023af448bb0328Richard Smith#ifndef NDEBUG
2214180f47959a066795cc0f409433023af448bb0328Richard Smith      // Non-virtual base classes are initialized in the order in the class
221586c3ae46250cdcc57778c27826060779a92f3815Richard Smith      // definition. We have already checked for virtual base classes.
2216180f47959a066795cc0f409433023af448bb0328Richard Smith      assert(!BaseIt->isVirtual() && "virtual base for literal type");
2217180f47959a066795cc0f409433023af448bb0328Richard Smith      assert(Info.Ctx.hasSameType(BaseIt->getType(), BaseType) &&
2218180f47959a066795cc0f409433023af448bb0328Richard Smith             "base class initializers not in expected order");
2219180f47959a066795cc0f409433023af448bb0328Richard Smith      ++BaseIt;
2220180f47959a066795cc0f409433023af448bb0328Richard Smith#endif
22218d59deec807ed53efcd07855199cdc9c979f447fJohn McCall      if (!HandleLValueDirectBase(Info, (*I)->getInit(), Subobject, RD,
22228d59deec807ed53efcd07855199cdc9c979f447fJohn McCall                                  BaseType->getAsCXXRecordDecl(), &Layout))
22238d59deec807ed53efcd07855199cdc9c979f447fJohn McCall        return false;
2224745f5147e065900267c85a5568785a1991d4838fRichard Smith      Value = &Result.getStructBase(BasesSeen++);
2225180f47959a066795cc0f409433023af448bb0328Richard Smith    } else if (FieldDecl *FD = (*I)->getMember()) {
22268d59deec807ed53efcd07855199cdc9c979f447fJohn McCall      if (!HandleLValueMember(Info, (*I)->getInit(), Subobject, FD, &Layout))
22278d59deec807ed53efcd07855199cdc9c979f447fJohn McCall        return false;
2228180f47959a066795cc0f409433023af448bb0328Richard Smith      if (RD->isUnion()) {
2229180f47959a066795cc0f409433023af448bb0328Richard Smith        Result = APValue(FD);
2230745f5147e065900267c85a5568785a1991d4838fRichard Smith        Value = &Result.getUnionValue();
2231745f5147e065900267c85a5568785a1991d4838fRichard Smith      } else {
2232745f5147e065900267c85a5568785a1991d4838fRichard Smith        Value = &Result.getStructField(FD->getFieldIndex());
2233745f5147e065900267c85a5568785a1991d4838fRichard Smith      }
2234d9b02e726262e4009dda830998bb934172ac0020Richard Smith    } else if (IndirectFieldDecl *IFD = (*I)->getIndirectMember()) {
2235d9b02e726262e4009dda830998bb934172ac0020Richard Smith      // Walk the indirect field decl's chain to find the object to initialize,
2236d9b02e726262e4009dda830998bb934172ac0020Richard Smith      // and make sure we've initialized every step along it.
2237d9b02e726262e4009dda830998bb934172ac0020Richard Smith      for (IndirectFieldDecl::chain_iterator C = IFD->chain_begin(),
2238d9b02e726262e4009dda830998bb934172ac0020Richard Smith                                             CE = IFD->chain_end();
2239d9b02e726262e4009dda830998bb934172ac0020Richard Smith           C != CE; ++C) {
2240d9b02e726262e4009dda830998bb934172ac0020Richard Smith        FieldDecl *FD = cast<FieldDecl>(*C);
2241d9b02e726262e4009dda830998bb934172ac0020Richard Smith        CXXRecordDecl *CD = cast<CXXRecordDecl>(FD->getParent());
2242d9b02e726262e4009dda830998bb934172ac0020Richard Smith        // Switch the union field if it differs. This happens if we had
2243d9b02e726262e4009dda830998bb934172ac0020Richard Smith        // preceding zero-initialization, and we're now initializing a union
2244d9b02e726262e4009dda830998bb934172ac0020Richard Smith        // subobject other than the first.
2245d9b02e726262e4009dda830998bb934172ac0020Richard Smith        // FIXME: In this case, the values of the other subobjects are
2246d9b02e726262e4009dda830998bb934172ac0020Richard Smith        // specified, since zero-initialization sets all padding bits to zero.
2247d9b02e726262e4009dda830998bb934172ac0020Richard Smith        if (Value->isUninit() ||
2248d9b02e726262e4009dda830998bb934172ac0020Richard Smith            (Value->isUnion() && Value->getUnionField() != FD)) {
2249d9b02e726262e4009dda830998bb934172ac0020Richard Smith          if (CD->isUnion())
2250d9b02e726262e4009dda830998bb934172ac0020Richard Smith            *Value = APValue(FD);
2251d9b02e726262e4009dda830998bb934172ac0020Richard Smith          else
2252d9b02e726262e4009dda830998bb934172ac0020Richard Smith            *Value = APValue(APValue::UninitStruct(), CD->getNumBases(),
2253d9b02e726262e4009dda830998bb934172ac0020Richard Smith                             std::distance(CD->field_begin(), CD->field_end()));
2254d9b02e726262e4009dda830998bb934172ac0020Richard Smith        }
22558d59deec807ed53efcd07855199cdc9c979f447fJohn McCall        if (!HandleLValueMember(Info, (*I)->getInit(), Subobject, FD))
22568d59deec807ed53efcd07855199cdc9c979f447fJohn McCall          return false;
2257d9b02e726262e4009dda830998bb934172ac0020Richard Smith        if (CD->isUnion())
2258d9b02e726262e4009dda830998bb934172ac0020Richard Smith          Value = &Value->getUnionValue();
2259d9b02e726262e4009dda830998bb934172ac0020Richard Smith        else
2260d9b02e726262e4009dda830998bb934172ac0020Richard Smith          Value = &Value->getStructField(FD->getFieldIndex());
2261d9b02e726262e4009dda830998bb934172ac0020Richard Smith      }
2262180f47959a066795cc0f409433023af448bb0328Richard Smith    } else {
2263d9b02e726262e4009dda830998bb934172ac0020Richard Smith      llvm_unreachable("unknown base initializer kind");
2264180f47959a066795cc0f409433023af448bb0328Richard Smith    }
2265745f5147e065900267c85a5568785a1991d4838fRichard Smith
226683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    if (!EvaluateInPlace(*Value, Info, Subobject, (*I)->getInit(),
226783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                         (*I)->isBaseInitializer()
2268745f5147e065900267c85a5568785a1991d4838fRichard Smith                                      ? CCEK_Constant : CCEK_MemberInit)) {
2269745f5147e065900267c85a5568785a1991d4838fRichard Smith      // If we're checking for a potential constant expression, evaluate all
2270745f5147e065900267c85a5568785a1991d4838fRichard Smith      // initializers even if some of them fail.
2271745f5147e065900267c85a5568785a1991d4838fRichard Smith      if (!Info.keepEvaluatingAfterFailure())
2272745f5147e065900267c85a5568785a1991d4838fRichard Smith        return false;
2273745f5147e065900267c85a5568785a1991d4838fRichard Smith      Success = false;
2274745f5147e065900267c85a5568785a1991d4838fRichard Smith    }
2275180f47959a066795cc0f409433023af448bb0328Richard Smith  }
2276180f47959a066795cc0f409433023af448bb0328Richard Smith
2277745f5147e065900267c85a5568785a1991d4838fRichard Smith  return Success;
2278180f47959a066795cc0f409433023af448bb0328Richard Smith}
2279180f47959a066795cc0f409433023af448bb0328Richard Smith
2280d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smithnamespace {
2281770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass HasSideEffect
22828cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  : public ConstStmtVisitor<HasSideEffect, bool> {
22831e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith  const ASTContext &Ctx;
2284c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stumppublic:
2285c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump
22861e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith  HasSideEffect(const ASTContext &C) : Ctx(C) {}
2287c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump
2288c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump  // Unhandled nodes conservatively default to having side effects.
22898cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitStmt(const Stmt *S) {
2290c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump    return true;
2291c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump  }
2292c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump
22938cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitParenExpr(const ParenExpr *E) { return Visit(E->getSubExpr()); }
22948cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitGenericSelectionExpr(const GenericSelectionExpr *E) {
2295f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return Visit(E->getResultExpr());
2296f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  }
22978cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitDeclRefExpr(const DeclRefExpr *E) {
22981e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    if (Ctx.getCanonicalType(E->getType()).isVolatileQualified())
2299c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump      return true;
2300c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump    return false;
2301c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump  }
2302f85e193739c953358c865005855253af4f68a497John McCall  bool VisitObjCIvarRefExpr(const ObjCIvarRefExpr *E) {
23031e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    if (Ctx.getCanonicalType(E->getType()).isVolatileQualified())
2304f85e193739c953358c865005855253af4f68a497John McCall      return true;
2305f85e193739c953358c865005855253af4f68a497John McCall    return false;
2306f85e193739c953358c865005855253af4f68a497John McCall  }
2307f85e193739c953358c865005855253af4f68a497John McCall
2308c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump  // We don't want to evaluate BlockExprs multiple times, as they generate
2309c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump  // a ton of code.
23108cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitBlockExpr(const BlockExpr *E) { return true; }
23118cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitPredefinedExpr(const PredefinedExpr *E) { return false; }
23128cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E)
2313c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump    { return Visit(E->getInitializer()); }
23148cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitMemberExpr(const MemberExpr *E) { return Visit(E->getBase()); }
23158cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitIntegerLiteral(const IntegerLiteral *E) { return false; }
23168cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitFloatingLiteral(const FloatingLiteral *E) { return false; }
23178cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitStringLiteral(const StringLiteral *E) { return false; }
23188cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCharacterLiteral(const CharacterLiteral *E) { return false; }
23198cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E)
2320f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    { return false; }
23218cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E)
2322980ca220848d27ef55ef4c2d37423461a8ed0da3Mike Stump    { return Visit(E->getLHS()) || Visit(E->getRHS()); }
23238cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitChooseExpr(const ChooseExpr *E)
23241e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    { return Visit(E->getChosenSubExpr(Ctx)); }
2325f195f2cacf149286232d66578fad3370efa5f567Nuno Lopes  bool VisitAbstractConditionalOperator(const AbstractConditionalOperator *E)
2326f195f2cacf149286232d66578fad3370efa5f567Nuno Lopes    { return Visit(E->getCond()) || Visit(E->getTrueExpr())
2327f195f2cacf149286232d66578fad3370efa5f567Nuno Lopes      || Visit(E->getFalseExpr()); }
23288cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCastExpr(const CastExpr *E) { return Visit(E->getSubExpr()); }
23298cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitBinAssign(const BinaryOperator *E) { return true; }
23308cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCompoundAssignOperator(const BinaryOperator *E) { return true; }
23318cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitBinaryOperator(const BinaryOperator *E)
2332980ca220848d27ef55ef4c2d37423461a8ed0da3Mike Stump  { return Visit(E->getLHS()) || Visit(E->getRHS()); }
23338cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitUnaryPreInc(const UnaryOperator *E) { return true; }
23348cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitUnaryPostInc(const UnaryOperator *E) { return true; }
23358cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitUnaryPreDec(const UnaryOperator *E) { return true; }
23368cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitUnaryPostDec(const UnaryOperator *E) { return true; }
23378cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitUnaryDeref(const UnaryOperator *E) {
23381e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    if (Ctx.getCanonicalType(E->getType()).isVolatileQualified())
2339c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump      return true;
2340980ca220848d27ef55ef4c2d37423461a8ed0da3Mike Stump    return Visit(E->getSubExpr());
2341c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump  }
23428cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitUnaryOperator(const UnaryOperator *E) { return Visit(E->getSubExpr()); }
2343363ff23cfddb51abe4ee4212a6dd3c9b534fcc8bChris Lattner
2344363ff23cfddb51abe4ee4212a6dd3c9b534fcc8bChris Lattner  // Has side effects if any element does.
23458cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitInitListExpr(const InitListExpr *E) {
2346363ff23cfddb51abe4ee4212a6dd3c9b534fcc8bChris Lattner    for (unsigned i = 0, e = E->getNumInits(); i != e; ++i)
2347363ff23cfddb51abe4ee4212a6dd3c9b534fcc8bChris Lattner      if (Visit(E->getInit(i))) return true;
23488cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    if (const Expr *filler = E->getArrayFiller())
23494423ac0282acb8ba801eb05b38712438dc0c1e3eArgyrios Kyrtzidis      return Visit(filler);
2350363ff23cfddb51abe4ee4212a6dd3c9b534fcc8bChris Lattner    return false;
2351363ff23cfddb51abe4ee4212a6dd3c9b534fcc8bChris Lattner  }
2352ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor
23538cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitSizeOfPackExpr(const SizeOfPackExpr *) { return false; }
2354c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump};
2355c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump
2356c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump} // end anonymous namespace
2357c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump
23584efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman//===----------------------------------------------------------------------===//
23598cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne// Generic Evaluation
23608cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne//===----------------------------------------------------------------------===//
23618cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournenamespace {
23628cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
2363f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith// FIXME: RetTy is always bool. Remove it.
2364f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithtemplate <class Derived, typename RetTy=bool>
23658cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourneclass ExprEvaluatorBase
23668cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  : public ConstStmtVisitor<Derived, RetTy> {
23678cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourneprivate:
23681aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  RetTy DerivedSuccess(const APValue &V, const Expr *E) {
23698cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    return static_cast<Derived*>(this)->Success(V, E);
23708cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  }
237151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  RetTy DerivedZeroInitialization(const Expr *E) {
237251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return static_cast<Derived*>(this)->ZeroInitialization(E);
2373f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  }
23748cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
237574e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  // Check whether a conditional operator with a non-constant condition is a
237674e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  // potential constant expression. If neither arm is a potential constant
237774e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  // expression, then the conditional operator is not either.
237874e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  template<typename ConditionalOperator>
237974e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  void CheckPotentialConstantConditional(const ConditionalOperator *E) {
238074e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    assert(Info.CheckingPotentialConstantExpression);
238174e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith
238274e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    // Speculatively evaluate both arms.
238374e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    {
238474e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith      llvm::SmallVector<PartialDiagnosticAt, 8> Diag;
238574e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith      SpeculativeEvaluationRAII Speculate(Info, &Diag);
238674e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith
238774e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith      StmtVisitorTy::Visit(E->getFalseExpr());
238874e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith      if (Diag.empty())
238974e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith        return;
239074e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith
239174e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith      Diag.clear();
239274e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith      StmtVisitorTy::Visit(E->getTrueExpr());
239374e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith      if (Diag.empty())
239474e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith        return;
239574e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    }
239674e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith
239774e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    Error(E, diag::note_constexpr_conditional_never_const);
239874e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  }
239974e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith
240074e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith
240174e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  template<typename ConditionalOperator>
240274e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  bool HandleConditionalOperator(const ConditionalOperator *E) {
240374e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    bool BoolResult;
240474e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) {
240574e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith      if (Info.CheckingPotentialConstantExpression)
240674e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith        CheckPotentialConstantConditional(E);
240774e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith      return false;
240874e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    }
240974e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith
241074e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
241174e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    return StmtVisitorTy::Visit(EvalExpr);
241274e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  }
241374e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith
24148cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourneprotected:
24158cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  EvalInfo &Info;
24168cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  typedef ConstStmtVisitor<Derived, RetTy> StmtVisitorTy;
24178cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  typedef ExprEvaluatorBase ExprEvaluatorBaseTy;
24188cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
2419dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith  OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) {
24205cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    return Info.CCEDiag(E, D);
2421f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
2422f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
2423cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  RetTy ZeroInitialization(const Expr *E) { return Error(E); }
2424cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
2425cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidispublic:
2426cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {}
2427cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
2428cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  EvalInfo &getEvalInfo() { return Info; }
2429cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
2430f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  /// Report an evaluation error. This should only be called when an error is
2431f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  /// first discovered. When propagating an error, just return false.
2432f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  bool Error(const Expr *E, diag::kind D) {
24335cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.Diag(E, D);
2434f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return false;
2435f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
2436f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  bool Error(const Expr *E) {
2437f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E, diag::note_invalid_subexpr_in_const_expr);
2438f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
2439f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
24408cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitStmt(const Stmt *) {
2441b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Expression evaluator should not be called on stmts");
24428cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  }
24438cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitExpr(const Expr *E) {
2444f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
24458cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  }
24468cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
24478cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitParenExpr(const ParenExpr *E)
24488cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    { return StmtVisitorTy::Visit(E->getSubExpr()); }
24498cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitUnaryExtension(const UnaryOperator *E)
24508cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    { return StmtVisitorTy::Visit(E->getSubExpr()); }
24518cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitUnaryPlus(const UnaryOperator *E)
24528cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    { return StmtVisitorTy::Visit(E->getSubExpr()); }
24538cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitChooseExpr(const ChooseExpr *E)
24548cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    { return StmtVisitorTy::Visit(E->getChosenSubExpr(Info.Ctx)); }
24558cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitGenericSelectionExpr(const GenericSelectionExpr *E)
24568cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    { return StmtVisitorTy::Visit(E->getResultExpr()); }
245791a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall  RetTy VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E)
245891a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall    { return StmtVisitorTy::Visit(E->getReplacement()); }
24593d75ca836205856077c18e30e9447accbd85f751Richard Smith  RetTy VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E)
24603d75ca836205856077c18e30e9447accbd85f751Richard Smith    { return StmtVisitorTy::Visit(E->getExpr()); }
2461bc6abe93a5d6b1305411f8b6f54c2caa686ddc69Richard Smith  // We cannot create any objects for which cleanups are required, so there is
2462bc6abe93a5d6b1305411f8b6f54c2caa686ddc69Richard Smith  // nothing to do here; all cleanups must come from unevaluated subexpressions.
2463bc6abe93a5d6b1305411f8b6f54c2caa686ddc69Richard Smith  RetTy VisitExprWithCleanups(const ExprWithCleanups *E)
2464bc6abe93a5d6b1305411f8b6f54c2caa686ddc69Richard Smith    { return StmtVisitorTy::Visit(E->getSubExpr()); }
24658cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
2466c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith  RetTy VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E) {
2467c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith    CCEDiag(E, diag::note_constexpr_invalid_cast) << 0;
2468c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith    return static_cast<Derived*>(this)->VisitCastExpr(E);
2469c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith  }
2470c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith  RetTy VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) {
2471c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith    CCEDiag(E, diag::note_constexpr_invalid_cast) << 1;
2472c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith    return static_cast<Derived*>(this)->VisitCastExpr(E);
2473c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith  }
2474c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith
2475e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  RetTy VisitBinaryOperator(const BinaryOperator *E) {
2476e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    switch (E->getOpcode()) {
2477e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    default:
2478f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E);
2479e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2480e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    case BO_Comma:
2481e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      VisitIgnoredValue(E->getLHS());
2482e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return StmtVisitorTy::Visit(E->getRHS());
2483e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2484e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    case BO_PtrMemD:
2485e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    case BO_PtrMemI: {
2486e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      LValue Obj;
2487e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!HandleMemberPointerAccess(Info, E, Obj))
2488e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
24891aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith      APValue Result;
2490f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      if (!HandleLValueToRValueConversion(Info, E, E->getType(), Obj, Result))
2491e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
2492e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return DerivedSuccess(Result, E);
2493e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
2494e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
2495e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
2496e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
24978cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) {
2498e92b1f4917bfb669a09d220dc979fc3676df4da8Richard Smith    // Evaluate and cache the common expression. We treat it as a temporary,
2499e92b1f4917bfb669a09d220dc979fc3676df4da8Richard Smith    // even though it's not quite the same thing.
2500e92b1f4917bfb669a09d220dc979fc3676df4da8Richard Smith    if (!Evaluate(Info.CurrentCall->Temporaries[E->getOpaqueValue()],
2501e92b1f4917bfb669a09d220dc979fc3676df4da8Richard Smith                  Info, E->getCommon()))
2502f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return false;
25038cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
250474e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    return HandleConditionalOperator(E);
25058cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  }
25068cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
25078cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitConditionalOperator(const ConditionalOperator *E) {
2508f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    bool IsBcpCall = false;
2509f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    // If the condition (ignoring parens) is a __builtin_constant_p call,
2510f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    // the result is a constant expression if it can be folded without
2511f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    // side-effects. This is an important GNU extension. See GCC PR38377
2512f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    // for discussion.
2513f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    if (const CallExpr *CallCE =
2514f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          dyn_cast<CallExpr>(E->getCond()->IgnoreParenCasts()))
2515f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p)
2516f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        IsBcpCall = true;
2517f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
2518f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    // Always assume __builtin_constant_p(...) ? ... : ... is a potential
2519f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    // constant expression; we can't check whether it's potentially foldable.
2520f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    if (Info.CheckingPotentialConstantExpression && IsBcpCall)
2521f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      return false;
2522f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
2523f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    FoldConstant Fold(Info);
2524f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
252574e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    if (!HandleConditionalOperator(E))
2526f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      return false;
2527f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
2528f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    if (IsBcpCall)
2529f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      Fold.Fold(Info);
2530f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
2531f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    return true;
25328cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  }
25338cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
25348cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitOpaqueValueExpr(const OpaqueValueExpr *E) {
2535e92b1f4917bfb669a09d220dc979fc3676df4da8Richard Smith    APValue &Value = Info.CurrentCall->Temporaries[E];
2536e92b1f4917bfb669a09d220dc979fc3676df4da8Richard Smith    if (Value.isUninit()) {
253742786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis      const Expr *Source = E->getSourceExpr();
253842786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis      if (!Source)
2539f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(E);
254042786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis      if (Source == E) { // sanity checking.
254142786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis        assert(0 && "OpaqueValueExpr recursively refers to itself");
2542f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(E);
254342786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis      }
254442786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis      return StmtVisitorTy::Visit(Source);
254542786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis    }
2546e92b1f4917bfb669a09d220dc979fc3676df4da8Richard Smith    return DerivedSuccess(Value, E);
25478cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  }
2548f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith
2549d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  RetTy VisitCallExpr(const CallExpr *E) {
2550e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    const Expr *Callee = E->getCallee()->IgnoreParens();
2551d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    QualType CalleeType = Callee->getType();
2552d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
25536142ca7790aa09a6e13592b70f142cc4bbcadcaeDevang Patel    const FunctionDecl *FD = 0;
255459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    LValue *This = 0, ThisVal;
255559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs());
255686c3ae46250cdcc57778c27826060779a92f3815Richard Smith    bool HasQualifier = false;
255759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
255859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    // Extract function decl and 'this' pointer from the callee.
255959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    if (CalleeType->isSpecificBuiltinType(BuiltinType::BoundMember)) {
2560f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      const ValueDecl *Member = 0;
2561e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (const MemberExpr *ME = dyn_cast<MemberExpr>(Callee)) {
2562e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        // Explicit bound member calls, such as x.f() or p->g();
2563e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        if (!EvaluateObjectArgument(Info, ME->getBase(), ThisVal))
2564f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith          return false;
2565f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        Member = ME->getMemberDecl();
2566e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        This = &ThisVal;
256786c3ae46250cdcc57778c27826060779a92f3815Richard Smith        HasQualifier = ME->hasQualifier();
2568e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(Callee)) {
2569e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        // Indirect bound member calls ('.*' or '->*').
2570f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        Member = HandleMemberPointerAccess(Info, BE, ThisVal, false);
2571f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        if (!Member) return false;
2572e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        This = &ThisVal;
2573e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      } else
2574f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(Callee);
2575f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
2576f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      FD = dyn_cast<FunctionDecl>(Member);
2577f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      if (!FD)
2578f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(Callee);
257959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    } else if (CalleeType->isFunctionPointerType()) {
2580b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      LValue Call;
2581b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      if (!EvaluatePointer(Callee, Call, Info))
2582f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return false;
258359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
2584b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      if (!Call.getLValueOffset().isZero())
2585f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(Callee);
25861bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith      FD = dyn_cast_or_null<FunctionDecl>(
25871bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith                             Call.getLValueBase().dyn_cast<const ValueDecl*>());
258859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      if (!FD)
2589f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(Callee);
259059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
259159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      // Overloaded operator calls to member functions are represented as normal
259259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      // calls with '*this' as the first argument.
259359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
259459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      if (MD && !MD->isStatic()) {
2595f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        // FIXME: When selecting an implicit conversion for an overloaded
2596f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        // operator delete, we sometimes try to evaluate calls to conversion
2597f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        // operators without a 'this' parameter!
2598f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        if (Args.empty())
2599f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith          return Error(E);
2600f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
260159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith        if (!EvaluateObjectArgument(Info, Args[0], ThisVal))
260259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith          return false;
260359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith        This = &ThisVal;
260459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith        Args = Args.slice(1);
260559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      }
2606d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
260759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      // Don't call function pointers which have been cast to some other type.
260859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      if (!Info.Ctx.hasSameType(CalleeType->getPointeeType(), FD->getType()))
2609f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(E);
261059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    } else
2611f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E);
2612d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
2613b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith    if (This && !This->checkSubobject(Info, E, CSK_This))
2614b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith      return false;
2615b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith
261686c3ae46250cdcc57778c27826060779a92f3815Richard Smith    // DR1358 allows virtual constexpr functions in some cases. Don't allow
261786c3ae46250cdcc57778c27826060779a92f3815Richard Smith    // calls to such functions in constant expressions.
261886c3ae46250cdcc57778c27826060779a92f3815Richard Smith    if (This && !HasQualifier &&
261986c3ae46250cdcc57778c27826060779a92f3815Richard Smith        isa<CXXMethodDecl>(FD) && cast<CXXMethodDecl>(FD)->isVirtual())
262086c3ae46250cdcc57778c27826060779a92f3815Richard Smith      return Error(E, diag::note_constexpr_virtual_call);
262186c3ae46250cdcc57778c27826060779a92f3815Richard Smith
2622c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    const FunctionDecl *Definition = 0;
2623d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    Stmt *Body = FD->getBody(Definition);
26241aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    APValue Result;
2625d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
2626c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition) ||
2627745f5147e065900267c85a5568785a1991d4838fRichard Smith        !HandleFunctionCall(E->getExprLoc(), Definition, This, Args, Body,
2628745f5147e065900267c85a5568785a1991d4838fRichard Smith                            Info, Result))
2629f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return false;
2630d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
263183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return DerivedSuccess(Result, E);
2632d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  }
2633d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
2634c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  RetTy VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
2635c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return StmtVisitorTy::Visit(E->getInitializer());
2636c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  }
2637f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  RetTy VisitInitListExpr(const InitListExpr *E) {
263871523d6c41e1599fc42f420d02dd2895fd8f65d4Eli Friedman    if (E->getNumInits() == 0)
263971523d6c41e1599fc42f420d02dd2895fd8f65d4Eli Friedman      return DerivedZeroInitialization(E);
264071523d6c41e1599fc42f420d02dd2895fd8f65d4Eli Friedman    if (E->getNumInits() == 1)
264171523d6c41e1599fc42f420d02dd2895fd8f65d4Eli Friedman      return StmtVisitorTy::Visit(E->getInit(0));
2642f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
2643f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  }
2644f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  RetTy VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
264551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return DerivedZeroInitialization(E);
2646f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  }
2647f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  RetTy VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) {
264851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return DerivedZeroInitialization(E);
2649f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  }
2650e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  RetTy VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) {
265151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return DerivedZeroInitialization(E);
2652e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
2653f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith
2654180f47959a066795cc0f409433023af448bb0328Richard Smith  /// A member expression where the object is a prvalue is itself a prvalue.
2655180f47959a066795cc0f409433023af448bb0328Richard Smith  RetTy VisitMemberExpr(const MemberExpr *E) {
2656180f47959a066795cc0f409433023af448bb0328Richard Smith    assert(!E->isArrow() && "missing call to bound member function?");
2657180f47959a066795cc0f409433023af448bb0328Richard Smith
26581aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    APValue Val;
2659180f47959a066795cc0f409433023af448bb0328Richard Smith    if (!Evaluate(Val, Info, E->getBase()))
2660180f47959a066795cc0f409433023af448bb0328Richard Smith      return false;
2661180f47959a066795cc0f409433023af448bb0328Richard Smith
2662180f47959a066795cc0f409433023af448bb0328Richard Smith    QualType BaseTy = E->getBase()->getType();
2663180f47959a066795cc0f409433023af448bb0328Richard Smith
2664180f47959a066795cc0f409433023af448bb0328Richard Smith    const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
2665f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!FD) return Error(E);
2666180f47959a066795cc0f409433023af448bb0328Richard Smith    assert(!FD->getType()->isReferenceType() && "prvalue reference?");
2667180f47959a066795cc0f409433023af448bb0328Richard Smith    assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() ==
2668180f47959a066795cc0f409433023af448bb0328Richard Smith           FD->getParent()->getCanonicalDecl() && "record / field mismatch");
2669180f47959a066795cc0f409433023af448bb0328Richard Smith
2670b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    SubobjectDesignator Designator(BaseTy);
2671b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    Designator.addDeclUnchecked(FD);
2672180f47959a066795cc0f409433023af448bb0328Richard Smith
2673f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return ExtractSubobject(Info, E, Val, BaseTy, Designator, E->getType()) &&
2674180f47959a066795cc0f409433023af448bb0328Richard Smith           DerivedSuccess(Val, E);
2675180f47959a066795cc0f409433023af448bb0328Richard Smith  }
2676180f47959a066795cc0f409433023af448bb0328Richard Smith
2677c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  RetTy VisitCastExpr(const CastExpr *E) {
2678c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    switch (E->getCastKind()) {
2679c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    default:
2680c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith      break;
2681c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
26827a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall    case CK_AtomicToNonAtomic:
26837a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall    case CK_NonAtomicToAtomic:
2684c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    case CK_NoOp:
26857d580a4e9e47dffc3c17aa2b957ac57ca3c4e451Richard Smith    case CK_UserDefinedConversion:
2686c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith      return StmtVisitorTy::Visit(E->getSubExpr());
2687c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
2688c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    case CK_LValueToRValue: {
2689c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith      LValue LVal;
2690f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      if (!EvaluateLValue(E->getSubExpr(), LVal, Info))
2691f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return false;
26921aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith      APValue RVal;
26939ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith      // Note, we use the subexpression's type in order to retain cv-qualifiers.
26949ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith      if (!HandleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(),
26959ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith                                          LVal, RVal))
2696f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return false;
2697f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return DerivedSuccess(RVal, E);
2698c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    }
2699c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    }
2700c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
2701f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
2702c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  }
2703c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
27048327fad71da34492d82c532f42a58cb4baff81a3Richard Smith  /// Visit a value which is evaluated, but whose value is ignored.
27058327fad71da34492d82c532f42a58cb4baff81a3Richard Smith  void VisitIgnoredValue(const Expr *E) {
27061aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    APValue Scratch;
27078327fad71da34492d82c532f42a58cb4baff81a3Richard Smith    if (!Evaluate(Scratch, Info, E))
27088327fad71da34492d82c532f42a58cb4baff81a3Richard Smith      Info.EvalStatus.HasSideEffects = true;
27098327fad71da34492d82c532f42a58cb4baff81a3Richard Smith  }
27108cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne};
27118cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
27128cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne}
27138cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
27148cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne//===----------------------------------------------------------------------===//
2715e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// Common base class for lvalue and temporary evaluation.
2716e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===//
2717e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithnamespace {
2718e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithtemplate<class Derived>
2719e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithclass LValueExprEvaluatorBase
2720e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  : public ExprEvaluatorBase<Derived, bool> {
2721e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithprotected:
2722e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  LValue &Result;
2723e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  typedef LValueExprEvaluatorBase LValueExprEvaluatorBaseTy;
2724e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  typedef ExprEvaluatorBase<Derived, bool> ExprEvaluatorBaseTy;
2725e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2726e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool Success(APValue::LValueBase B) {
2727e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    Result.set(B);
2728e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
2729e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
2730e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2731e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithpublic:
2732e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  LValueExprEvaluatorBase(EvalInfo &Info, LValue &Result) :
2733e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    ExprEvaluatorBaseTy(Info), Result(Result) {}
2734e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
27351aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  bool Success(const APValue &V, const Expr *E) {
27361aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    Result.setFrom(this->Info.Ctx, V);
2737e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
2738e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
2739e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2740e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitMemberExpr(const MemberExpr *E) {
2741e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // Handle non-static data members.
2742e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    QualType BaseTy;
2743e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (E->isArrow()) {
2744e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!EvaluatePointer(E->getBase(), Result, this->Info))
2745e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
2746e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      BaseTy = E->getBase()->getType()->getAs<PointerType>()->getPointeeType();
2747c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    } else if (E->getBase()->isRValue()) {
2748af2c7a194592401394233b7cbcdd3cfd0a7a38ddRichard Smith      assert(E->getBase()->getType()->isRecordType());
2749c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      if (!EvaluateTemporary(E->getBase(), Result, this->Info))
2750c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith        return false;
2751c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      BaseTy = E->getBase()->getType();
2752e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    } else {
2753e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!this->Visit(E->getBase()))
2754e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
2755e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      BaseTy = E->getBase()->getType();
2756e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
2757e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2758d9b02e726262e4009dda830998bb934172ac0020Richard Smith    const ValueDecl *MD = E->getMemberDecl();
2759d9b02e726262e4009dda830998bb934172ac0020Richard Smith    if (const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
2760d9b02e726262e4009dda830998bb934172ac0020Richard Smith      assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() ==
2761d9b02e726262e4009dda830998bb934172ac0020Richard Smith             FD->getParent()->getCanonicalDecl() && "record / field mismatch");
2762d9b02e726262e4009dda830998bb934172ac0020Richard Smith      (void)BaseTy;
27638d59deec807ed53efcd07855199cdc9c979f447fJohn McCall      if (!HandleLValueMember(this->Info, E, Result, FD))
27648d59deec807ed53efcd07855199cdc9c979f447fJohn McCall        return false;
2765d9b02e726262e4009dda830998bb934172ac0020Richard Smith    } else if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(MD)) {
27668d59deec807ed53efcd07855199cdc9c979f447fJohn McCall      if (!HandleLValueIndirectMember(this->Info, E, Result, IFD))
27678d59deec807ed53efcd07855199cdc9c979f447fJohn McCall        return false;
2768d9b02e726262e4009dda830998bb934172ac0020Richard Smith    } else
2769d9b02e726262e4009dda830998bb934172ac0020Richard Smith      return this->Error(E);
2770e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2771d9b02e726262e4009dda830998bb934172ac0020Richard Smith    if (MD->getType()->isReferenceType()) {
27721aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith      APValue RefValue;
2773d9b02e726262e4009dda830998bb934172ac0020Richard Smith      if (!HandleLValueToRValueConversion(this->Info, E, MD->getType(), Result,
2774e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                                          RefValue))
2775e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
2776e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return Success(RefValue, E);
2777e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
2778e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
2779e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
2780e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2781e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitBinaryOperator(const BinaryOperator *E) {
2782e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    switch (E->getOpcode()) {
2783e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    default:
2784e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
2785e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2786e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    case BO_PtrMemD:
2787e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    case BO_PtrMemI:
2788e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return HandleMemberPointerAccess(this->Info, E, Result);
2789e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
2790e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
2791e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2792e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitCastExpr(const CastExpr *E) {
2793e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    switch (E->getCastKind()) {
2794e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    default:
2795e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return ExprEvaluatorBaseTy::VisitCastExpr(E);
2796e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2797e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    case CK_DerivedToBase:
2798e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    case CK_UncheckedDerivedToBase: {
2799e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!this->Visit(E->getSubExpr()))
2800e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
2801e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2802e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      // Now figure out the necessary offset to add to the base LV to get from
2803e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      // the derived class to the base class.
2804e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      QualType Type = E->getSubExpr()->getType();
2805e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2806e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      for (CastExpr::path_const_iterator PathI = E->path_begin(),
2807e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith           PathE = E->path_end(); PathI != PathE; ++PathI) {
2808b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        if (!HandleLValueBase(this->Info, E, Result, Type->getAsCXXRecordDecl(),
2809e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                              *PathI))
2810e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith          return false;
2811e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        Type = (*PathI)->getType();
2812e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      }
2813e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2814e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return true;
2815e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
2816e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
2817e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
2818e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith};
2819e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}
2820e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2821e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===//
28224efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman// LValue Evaluation
2823c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//
2824c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// This is used for evaluating lvalues (in C and C++), xvalues (in C++11),
2825c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// function designators (in C), decl references to void objects (in C), and
2826c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// temporaries (if building with -Wno-address-of-temporary).
2827c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//
2828c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// LValue evaluation produces values comprising a base expression of one of the
2829c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// following types:
28301bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// - Declarations
28311bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith//  * VarDecl
28321bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith//  * FunctionDecl
28331bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// - Literals
2834c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//  * CompoundLiteralExpr in C
2835c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//  * StringLiteral
283647d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith//  * CXXTypeidExpr
2837c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//  * PredefinedExpr
2838180f47959a066795cc0f409433023af448bb0328Richard Smith//  * ObjCStringLiteralExpr
2839c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//  * ObjCEncodeExpr
2840c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//  * AddrLabelExpr
2841c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//  * BlockExpr
2842c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//  * CallExpr for a MakeStringConstant builtin
28431bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// - Locals and temporaries
284483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith//  * Any Expr, with a CallIndex indicating the function in which the temporary
284583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith//    was evaluated.
28461bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// plus an offset in bytes.
28474efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman//===----------------------------------------------------------------------===//
28484efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedmannamespace {
2849770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass LValueExprEvaluator
2850e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  : public LValueExprEvaluatorBase<LValueExprEvaluator> {
28514efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedmanpublic:
2852e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  LValueExprEvaluator(EvalInfo &Info, LValue &Result) :
2853e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    LValueExprEvaluatorBaseTy(Info, Result) {}
28541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2855c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  bool VisitVarDecl(const Expr *E, const VarDecl *VD);
2856c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
28578cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitDeclRefExpr(const DeclRefExpr *E);
28588cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(E); }
2859bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith  bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E);
28608cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
28618cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitMemberExpr(const MemberExpr *E);
28628cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitStringLiteral(const StringLiteral *E) { return Success(E); }
28638cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(E); }
286447d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith  bool VisitCXXTypeidExpr(const CXXTypeidExpr *E);
2865e275a1845b9e32bd3034f2593dee1780855c8fd6Francois Pichet  bool VisitCXXUuidofExpr(const CXXUuidofExpr *E);
28668cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E);
28678cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitUnaryDeref(const UnaryOperator *E);
286886024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  bool VisitUnaryReal(const UnaryOperator *E);
286986024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  bool VisitUnaryImag(const UnaryOperator *E);
28708cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
28718cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCastExpr(const CastExpr *E) {
287226bc220377705292a0519a71d3ea3aef68fcfec6Anders Carlsson    switch (E->getCastKind()) {
287326bc220377705292a0519a71d3ea3aef68fcfec6Anders Carlsson    default:
2874e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return LValueExprEvaluatorBaseTy::VisitCastExpr(E);
287526bc220377705292a0519a71d3ea3aef68fcfec6Anders Carlsson
2876db924224b51b153f24fbe492102d4edebcbbb7f4Eli Friedman    case CK_LValueBitCast:
2877c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith      this->CCEDiag(E, diag::note_constexpr_invalid_cast) << 2;
28780a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      if (!Visit(E->getSubExpr()))
28790a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith        return false;
28800a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      Result.Designator.setInvalid();
28810a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      return true;
2882db924224b51b153f24fbe492102d4edebcbbb7f4Eli Friedman
2883e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    case CK_BaseToDerived:
2884180f47959a066795cc0f409433023af448bb0328Richard Smith      if (!Visit(E->getSubExpr()))
2885180f47959a066795cc0f409433023af448bb0328Richard Smith        return false;
2886e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return HandleBaseToDerivedCast(Info, E, Result);
288726bc220377705292a0519a71d3ea3aef68fcfec6Anders Carlsson    }
288826bc220377705292a0519a71d3ea3aef68fcfec6Anders Carlsson  }
28894efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman};
28904efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman} // end anonymous namespace
28914efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
2892c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// Evaluate an expression as an lvalue. This can be legitimately called on
2893c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// expressions which are not glvalues, in a few cases:
2894c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith///  * function designators in C,
2895c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith///  * "extern void" objects,
2896c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith///  * temporaries, if building with -Wno-address-of-temporary.
2897efdb83e26f9a1fd2566afe54461216cd84814d42John McCallstatic bool EvaluateLValue(const Expr* E, LValue& Result, EvalInfo &Info) {
2898c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  assert((E->isGLValue() || E->getType()->isFunctionType() ||
2899c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith          E->getType()->isVoidType() || isa<CXXTemporaryObjectExpr>(E)) &&
2900c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith         "can't evaluate expression as an lvalue");
29018cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  return LValueExprEvaluator(Info, Result).Visit(E);
29024efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman}
29034efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
29048cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
29051bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl()))
29061bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    return Success(FD);
29071bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  if (const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl()))
2908c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return VisitVarDecl(E, VD);
2909c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  return Error(E);
2910c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith}
2911436c8898cd1c93c5bacd3fcc4ac586bc5cd77062Richard Smith
2912c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smithbool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) {
2913177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith  if (!VD->getType()->isReferenceType()) {
2914177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith    if (isa<ParmVarDecl>(VD)) {
291583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      Result.set(VD, Info.CurrentCall->Index);
2916177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith      return true;
2917177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith    }
29181bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    return Success(VD);
2919177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith  }
292050c39ea4858265f3f5f42a0c624557ce2281936bEli Friedman
29211aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  APValue V;
2922f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (!EvaluateVarDeclInit(Info, E, VD, Info.CurrentCall, V))
2923f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return false;
2924f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  return Success(V, E);
292535873c49adad211ff466e34342a52665742794f5Anders Carlsson}
292635873c49adad211ff466e34342a52665742794f5Anders Carlsson
2927bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smithbool LValueExprEvaluator::VisitMaterializeTemporaryExpr(
2928bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    const MaterializeTemporaryExpr *E) {
2929e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (E->GetTemporaryExpr()->isRValue()) {
2930af2c7a194592401394233b7cbcdd3cfd0a7a38ddRichard Smith    if (E->getType()->isRecordType())
2931e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return EvaluateTemporary(E->GetTemporaryExpr(), Result, Info);
2932e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
293383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    Result.set(E, Info.CurrentCall->Index);
293483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return EvaluateInPlace(Info.CurrentCall->Temporaries[E], Info,
293583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                           Result, E->GetTemporaryExpr());
2936e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
2937e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2938e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // Materialization of an lvalue temporary occurs when we need to force a copy
2939e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // (for instance, if it's a bitfield).
2940e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // FIXME: The AST should contain an lvalue-to-rvalue node for such cases.
2941e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (!Visit(E->GetTemporaryExpr()))
2942e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return false;
2943f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (!HandleLValueToRValueConversion(Info, E, E->getType(), Result,
2944e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                                      Info.CurrentCall->Temporaries[E]))
2945e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return false;
294683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  Result.set(E, Info.CurrentCall->Index);
2947e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  return true;
2948bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith}
2949bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith
29508cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool
29518cad3046be06ea73ff8892d947697a21d7a440d3Peter CollingbourneLValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
2952c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?");
2953c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  // Defer visiting the literal until the lvalue-to-rvalue conversion. We can
2954c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  // only see this when folding in C, so there's no standard to follow here.
2955efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  return Success(E);
29564efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman}
29574efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
295847d2145675099893d702be4bc06bd9f26d8ddd13Richard Smithbool LValueExprEvaluator::VisitCXXTypeidExpr(const CXXTypeidExpr *E) {
295947d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith  if (E->isTypeOperand())
296047d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith    return Success(E);
296147d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith  CXXRecordDecl *RD = E->getExprOperand()->getType()->getAsCXXRecordDecl();
296247d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith  if (RD && RD->isPolymorphic()) {
29635cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.Diag(E, diag::note_constexpr_typeid_polymorphic)
296447d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith      << E->getExprOperand()->getType()
296547d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith      << E->getExprOperand()->getSourceRange();
296647d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith    return false;
296747d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith  }
296847d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith  return Success(E);
296947d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith}
297047d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith
2971e275a1845b9e32bd3034f2593dee1780855c8fd6Francois Pichetbool LValueExprEvaluator::VisitCXXUuidofExpr(const CXXUuidofExpr *E) {
2972e275a1845b9e32bd3034f2593dee1780855c8fd6Francois Pichet  return Success(E);
2973e275a1845b9e32bd3034f2593dee1780855c8fd6Francois Pichet}
2974e275a1845b9e32bd3034f2593dee1780855c8fd6Francois Pichet
29758cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) {
2976c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  // Handle static data members.
2977c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  if (const VarDecl *VD = dyn_cast<VarDecl>(E->getMemberDecl())) {
2978c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    VisitIgnoredValue(E->getBase());
2979c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return VisitVarDecl(E, VD);
2980c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  }
2981c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
2982d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  // Handle static member functions.
2983d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) {
2984d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    if (MD->isStatic()) {
2985d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith      VisitIgnoredValue(E->getBase());
29861bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith      return Success(MD);
2987d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    }
2988d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  }
2989d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
2990180f47959a066795cc0f409433023af448bb0328Richard Smith  // Handle non-static data members.
2991e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  return LValueExprEvaluatorBaseTy::VisitMemberExpr(E);
29924efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman}
29934efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
29948cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) {
2995c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  // FIXME: Deal with vectors as array subscript bases.
2996c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  if (E->getBase()->getType()->isVectorType())
2997f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
2998c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
29993068d117951a8df54bae9db039b56201ab10962bAnders Carlsson  if (!EvaluatePointer(E->getBase(), Result, Info))
3000efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    return false;
30011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30023068d117951a8df54bae9db039b56201ab10962bAnders Carlsson  APSInt Index;
30033068d117951a8df54bae9db039b56201ab10962bAnders Carlsson  if (!EvaluateInteger(E->getIdx(), Index, Info))
3004efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    return false;
3005180f47959a066795cc0f409433023af448bb0328Richard Smith  int64_t IndexValue
3006180f47959a066795cc0f409433023af448bb0328Richard Smith    = Index.isSigned() ? Index.getSExtValue()
3007180f47959a066795cc0f409433023af448bb0328Richard Smith                       : static_cast<int64_t>(Index.getZExtValue());
30083068d117951a8df54bae9db039b56201ab10962bAnders Carlsson
3009b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  return HandleLValueArrayAdjustment(Info, E, Result, E->getType(), IndexValue);
30103068d117951a8df54bae9db039b56201ab10962bAnders Carlsson}
30114efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
30128cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) {
3013efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  return EvaluatePointer(E->getSubExpr(), Result, Info);
3014e8761c8fe2ee6b628104a0885f49fd3c21c08a4fEli Friedman}
3015e8761c8fe2ee6b628104a0885f49fd3c21c08a4fEli Friedman
301686024013d4c3728122c58fa07a2a67e6c15837efRichard Smithbool LValueExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
301786024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  if (!Visit(E->getSubExpr()))
301886024013d4c3728122c58fa07a2a67e6c15837efRichard Smith    return false;
301986024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  // __real is a no-op on scalar lvalues.
302086024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  if (E->getSubExpr()->getType()->isAnyComplexType())
302186024013d4c3728122c58fa07a2a67e6c15837efRichard Smith    HandleLValueComplexElement(Info, E, Result, E->getType(), false);
302286024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  return true;
302386024013d4c3728122c58fa07a2a67e6c15837efRichard Smith}
302486024013d4c3728122c58fa07a2a67e6c15837efRichard Smith
302586024013d4c3728122c58fa07a2a67e6c15837efRichard Smithbool LValueExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
302686024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  assert(E->getSubExpr()->getType()->isAnyComplexType() &&
302786024013d4c3728122c58fa07a2a67e6c15837efRichard Smith         "lvalue __imag__ on scalar?");
302886024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  if (!Visit(E->getSubExpr()))
302986024013d4c3728122c58fa07a2a67e6c15837efRichard Smith    return false;
303086024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  HandleLValueComplexElement(Info, E, Result, E->getType(), true);
303186024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  return true;
303286024013d4c3728122c58fa07a2a67e6c15837efRichard Smith}
303386024013d4c3728122c58fa07a2a67e6c15837efRichard Smith
30344efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman//===----------------------------------------------------------------------===//
3035f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner// Pointer Evaluation
3036f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===//
3037f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
3038c754aa62643e66ab967ca32ae8b0b3fc419bba25Anders Carlssonnamespace {
3039770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass PointerExprEvaluator
30408cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  : public ExprEvaluatorBase<PointerExprEvaluator, bool> {
3041efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  LValue &Result;
3042efdb83e26f9a1fd2566afe54461216cd84814d42John McCall
30438cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool Success(const Expr *E) {
30441bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    Result.set(E);
3045efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    return true;
3046efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  }
30472bad1687fe6f00e10767a691a33b070b151902b6Anders Carlssonpublic:
30481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3049efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  PointerExprEvaluator(EvalInfo &info, LValue &Result)
30508cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    : ExprEvaluatorBaseTy(info), Result(Result) {}
3051f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
30521aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  bool Success(const APValue &V, const Expr *E) {
30531aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    Result.setFrom(Info.Ctx, V);
30548cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    return true;
30552bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson  }
305651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  bool ZeroInitialization(const Expr *E) {
3057f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith    return Success((Expr*)0);
3058f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  }
30592bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson
3060efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  bool VisitBinaryOperator(const BinaryOperator *E);
30618cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCastExpr(const CastExpr* E);
3062efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  bool VisitUnaryAddrOf(const UnaryOperator *E);
30638cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitObjCStringLiteral(const ObjCStringLiteral *E)
3064efdb83e26f9a1fd2566afe54461216cd84814d42John McCall      { return Success(E); }
3065eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  bool VisitObjCBoxedExpr(const ObjCBoxedExpr *E)
3066ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      { return Success(E); }
30678cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitAddrLabelExpr(const AddrLabelExpr *E)
3068efdb83e26f9a1fd2566afe54461216cd84814d42John McCall      { return Success(E); }
30698cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCallExpr(const CallExpr *E);
30708cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitBlockExpr(const BlockExpr *E) {
3071469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall    if (!E->getBlockDecl()->hasCaptures())
3072efdb83e26f9a1fd2566afe54461216cd84814d42John McCall      return Success(E);
3073f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
3074b83d287bc7f47d36fb0751a481e2ef9308b37252Mike Stump  }
3075180f47959a066795cc0f409433023af448bb0328Richard Smith  bool VisitCXXThisExpr(const CXXThisExpr *E) {
3076180f47959a066795cc0f409433023af448bb0328Richard Smith    if (!Info.CurrentCall->This)
3077f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E);
3078180f47959a066795cc0f409433023af448bb0328Richard Smith    Result = *Info.CurrentCall->This;
3079180f47959a066795cc0f409433023af448bb0328Richard Smith    return true;
3080180f47959a066795cc0f409433023af448bb0328Richard Smith  }
308156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
3082ba98d6bb414861965a1f22628494ea046785ecd4Eli Friedman  // FIXME: Missing: @protocol, @selector
30832bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson};
3084f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner} // end anonymous namespace
30852bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson
3086efdb83e26f9a1fd2566afe54461216cd84814d42John McCallstatic bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info) {
3087c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  assert(E->isRValue() && E->getType()->hasPointerRepresentation());
30888cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  return PointerExprEvaluator(Info, Result).Visit(E);
3089f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner}
3090650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson
3091efdb83e26f9a1fd2566afe54461216cd84814d42John McCallbool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
30922de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  if (E->getOpcode() != BO_Add &&
30932de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      E->getOpcode() != BO_Sub)
3094e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
30951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3096650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson  const Expr *PExp = E->getLHS();
3097650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson  const Expr *IExp = E->getRHS();
3098650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson  if (IExp->getType()->isPointerType())
3099f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner    std::swap(PExp, IExp);
31001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3101745f5147e065900267c85a5568785a1991d4838fRichard Smith  bool EvalPtrOK = EvaluatePointer(PExp, Result, Info);
3102745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!EvalPtrOK && !Info.keepEvaluatingAfterFailure())
3103efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    return false;
31041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3105efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  llvm::APSInt Offset;
3106745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!EvaluateInteger(IExp, Offset, Info) || !EvalPtrOK)
3107efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    return false;
3108efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  int64_t AdditionalOffset
3109efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    = Offset.isSigned() ? Offset.getSExtValue()
3110efdb83e26f9a1fd2566afe54461216cd84814d42John McCall                        : static_cast<int64_t>(Offset.getZExtValue());
31110a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith  if (E->getOpcode() == BO_Sub)
31120a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    AdditionalOffset = -AdditionalOffset;
3113650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson
3114180f47959a066795cc0f409433023af448bb0328Richard Smith  QualType Pointee = PExp->getType()->getAs<PointerType>()->getPointeeType();
3115b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  return HandleLValueArrayAdjustment(Info, E, Result, Pointee,
3116b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                                     AdditionalOffset);
3117650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson}
31184efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
3119efdb83e26f9a1fd2566afe54461216cd84814d42John McCallbool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
3120efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  return EvaluateLValue(E->getSubExpr(), Result, Info);
31214efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman}
31221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
31238cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
31248cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  const Expr* SubExpr = E->getSubExpr();
3125650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson
312609a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman  switch (E->getCastKind()) {
312709a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman  default:
312809a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman    break;
312909a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman
31302de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_BitCast:
31311d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_CPointerToObjCPointerCast:
31321d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_BlockPointerToObjCPointerCast:
31332de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_AnyPointerToBlockPointerCast:
313428c1ce789322ab99f9b5887015d63ec5f088957aRichard Smith    if (!Visit(SubExpr))
313528c1ce789322ab99f9b5887015d63ec5f088957aRichard Smith      return false;
3136c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith    // Bitcasts to cv void* are static_casts, not reinterpret_casts, so are
3137c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith    // permitted in constant expressions in C++11. Bitcasts from cv void* are
3138c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith    // also static_casts, but we disallow them as a resolution to DR1312.
31394cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith    if (!E->getType()->isVoidPointerType()) {
314028c1ce789322ab99f9b5887015d63ec5f088957aRichard Smith      Result.Designator.setInvalid();
31414cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith      if (SubExpr->getType()->isVoidPointerType())
31424cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith        CCEDiag(E, diag::note_constexpr_invalid_cast)
31434cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith          << 3 << SubExpr->getType();
31444cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith      else
31454cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith        CCEDiag(E, diag::note_constexpr_invalid_cast) << 2;
31464cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith    }
31470a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    return true;
314809a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman
31495c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson  case CK_DerivedToBase:
31505c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson  case CK_UncheckedDerivedToBase: {
315147a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    if (!EvaluatePointer(E->getSubExpr(), Result, Info))
31525c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson      return false;
3153e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (!Result.Base && Result.Offset.isZero())
3154e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return true;
31555c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson
3156180f47959a066795cc0f409433023af448bb0328Richard Smith    // Now figure out the necessary offset to add to the base LV to get from
31575c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson    // the derived class to the base class.
3158180f47959a066795cc0f409433023af448bb0328Richard Smith    QualType Type =
3159180f47959a066795cc0f409433023af448bb0328Richard Smith        E->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType();
31605c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson
3161180f47959a066795cc0f409433023af448bb0328Richard Smith    for (CastExpr::path_const_iterator PathI = E->path_begin(),
31625c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson         PathE = E->path_end(); PathI != PathE; ++PathI) {
3163b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      if (!HandleLValueBase(Info, E, Result, Type->getAsCXXRecordDecl(),
3164b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                            *PathI))
31655c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson        return false;
3166180f47959a066795cc0f409433023af448bb0328Richard Smith      Type = (*PathI)->getType();
31675c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson    }
31685c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson
31695c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson    return true;
31705c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson  }
31715c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson
3172e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  case CK_BaseToDerived:
3173e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (!Visit(E->getSubExpr()))
3174e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return false;
3175e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (!Result.Base && Result.Offset.isZero())
3176e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return true;
3177e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return HandleBaseToDerivedCast(Info, E, Result);
3178e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
317947a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith  case CK_NullToPointer:
318049149fe0d2be06ce1ceed1e9d2548a0b75a59c47Richard Smith    VisitIgnoredValue(E->getSubExpr());
318151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return ZeroInitialization(E);
3182404cd1669c3ba138a9ae0a619bd689cce5aae271John McCall
31832de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_IntegralToPointer: {
3184c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith    CCEDiag(E, diag::note_constexpr_invalid_cast) << 2;
3185c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith
31861aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    APValue Value;
3187efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    if (!EvaluateIntegerOrLValue(SubExpr, Value, Info))
318809a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman      break;
318969ab26a8623141f35e86817cfc6e0fbe7639a40fDaniel Dunbar
3190efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    if (Value.isInt()) {
319147a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith      unsigned Size = Info.Ctx.getTypeSize(E->getType());
319247a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith      uint64_t N = Value.getInt().extOrTrunc(Size).getZExtValue();
31931bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith      Result.Base = (Expr*)0;
319447a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith      Result.Offset = CharUnits::fromQuantity(N);
319583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      Result.CallIndex = 0;
31960a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      Result.Designator.setInvalid();
3197efdb83e26f9a1fd2566afe54461216cd84814d42John McCall      return true;
3198efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    } else {
3199efdb83e26f9a1fd2566afe54461216cd84814d42John McCall      // Cast is of an lvalue, no need to change value.
32001aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith      Result.setFrom(Info.Ctx, Value);
3201efdb83e26f9a1fd2566afe54461216cd84814d42John McCall      return true;
3202650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson    }
3203650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson  }
32042de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_ArrayToPointerDecay:
3205e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (SubExpr->isGLValue()) {
3206e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!EvaluateLValue(SubExpr, Result, Info))
3207e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
3208e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    } else {
320983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      Result.set(SubExpr, Info.CurrentCall->Index);
321083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      if (!EvaluateInPlace(Info.CurrentCall->Temporaries[SubExpr],
321183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                           Info, Result, SubExpr))
3212e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
3213e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
32140a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    // The result is a pointer to the first element of the array.
3215b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    if (const ConstantArrayType *CAT
3216b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith          = Info.Ctx.getAsConstantArrayType(SubExpr->getType()))
3217b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      Result.addArray(Info, E, CAT);
3218b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    else
3219b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      Result.Designator.setInvalid();
32200a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    return true;
32216a7c94af983717e2c2d6aebe42cb4737c1c7b9e6Richard Smith
32222de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_FunctionToPointerDecay:
32236a7c94af983717e2c2d6aebe42cb4737c1c7b9e6Richard Smith    return EvaluateLValue(SubExpr, Result, Info);
32244efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman  }
32254efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
3226c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  return ExprEvaluatorBaseTy::VisitCastExpr(E);
32271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
3228650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson
32298cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) {
3230180f47959a066795cc0f409433023af448bb0328Richard Smith  if (IsStringLiteralCall(E))
3231efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    return Success(E);
323256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
32338cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  return ExprEvaluatorBaseTy::VisitCallExpr(E);
32344efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman}
3235f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
3236f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===//
3237e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// Member Pointer Evaluation
3238e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===//
3239e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3240e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithnamespace {
3241e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithclass MemberPointerExprEvaluator
3242e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  : public ExprEvaluatorBase<MemberPointerExprEvaluator, bool> {
3243e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  MemberPtr &Result;
3244e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3245e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool Success(const ValueDecl *D) {
3246e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    Result = MemberPtr(D);
3247e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
3248e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
3249e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithpublic:
3250e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3251e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  MemberPointerExprEvaluator(EvalInfo &Info, MemberPtr &Result)
3252e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    : ExprEvaluatorBaseTy(Info), Result(Result) {}
3253e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
32541aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  bool Success(const APValue &V, const Expr *E) {
3255e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    Result.setFrom(V);
3256e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
3257e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
325851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  bool ZeroInitialization(const Expr *E) {
3259e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return Success((const ValueDecl*)0);
3260e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
3261e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3262e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitCastExpr(const CastExpr *E);
3263e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitUnaryAddrOf(const UnaryOperator *E);
3264e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith};
3265e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} // end anonymous namespace
3266e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3267e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result,
3268e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                                  EvalInfo &Info) {
3269e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  assert(E->isRValue() && E->getType()->isMemberPointerType());
3270e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  return MemberPointerExprEvaluator(Info, Result).Visit(E);
3271e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}
3272e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3273e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithbool MemberPointerExprEvaluator::VisitCastExpr(const CastExpr *E) {
3274e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  switch (E->getCastKind()) {
3275e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  default:
3276e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return ExprEvaluatorBaseTy::VisitCastExpr(E);
3277e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3278e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  case CK_NullToMemberPointer:
327949149fe0d2be06ce1ceed1e9d2548a0b75a59c47Richard Smith    VisitIgnoredValue(E->getSubExpr());
328051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return ZeroInitialization(E);
3281e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3282e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  case CK_BaseToDerivedMemberPointer: {
3283e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (!Visit(E->getSubExpr()))
3284e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return false;
3285e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (E->path_empty())
3286e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return true;
3287e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // Base-to-derived member pointer casts store the path in derived-to-base
3288e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // order, so iterate backwards. The CXXBaseSpecifier also provides us with
3289e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // the wrong end of the derived->base arc, so stagger the path by one class.
3290e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter;
3291e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin());
3292e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith         PathI != PathE; ++PathI) {
3293e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      assert(!(*PathI)->isVirtual() && "memptr cast through vbase");
3294e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      const CXXRecordDecl *Derived = (*PathI)->getType()->getAsCXXRecordDecl();
3295e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!Result.castToDerived(Derived))
3296f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(E);
3297e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
3298e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    const Type *FinalTy = E->getType()->castAs<MemberPointerType>()->getClass();
3299e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (!Result.castToDerived(FinalTy->getAsCXXRecordDecl()))
3300f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E);
3301e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
3302e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
3303e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3304e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  case CK_DerivedToBaseMemberPointer:
3305e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (!Visit(E->getSubExpr()))
3306e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return false;
3307e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    for (CastExpr::path_const_iterator PathI = E->path_begin(),
3308e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith         PathE = E->path_end(); PathI != PathE; ++PathI) {
3309e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      assert(!(*PathI)->isVirtual() && "memptr cast through vbase");
3310e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl();
3311e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!Result.castToBase(Base))
3312f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(E);
3313e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
3314e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
3315e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
3316e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}
3317e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3318e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithbool MemberPointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
3319e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // C++11 [expr.unary.op]p3 has very strict rules on how the address of a
3320e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // member can be formed.
3321e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  return Success(cast<DeclRefExpr>(E->getSubExpr())->getDecl());
3322e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}
3323e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3324e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===//
3325180f47959a066795cc0f409433023af448bb0328Richard Smith// Record Evaluation
3326180f47959a066795cc0f409433023af448bb0328Richard Smith//===----------------------------------------------------------------------===//
3327180f47959a066795cc0f409433023af448bb0328Richard Smith
3328180f47959a066795cc0f409433023af448bb0328Richard Smithnamespace {
3329180f47959a066795cc0f409433023af448bb0328Richard Smith  class RecordExprEvaluator
3330180f47959a066795cc0f409433023af448bb0328Richard Smith  : public ExprEvaluatorBase<RecordExprEvaluator, bool> {
3331180f47959a066795cc0f409433023af448bb0328Richard Smith    const LValue &This;
3332180f47959a066795cc0f409433023af448bb0328Richard Smith    APValue &Result;
3333180f47959a066795cc0f409433023af448bb0328Richard Smith  public:
3334180f47959a066795cc0f409433023af448bb0328Richard Smith
3335180f47959a066795cc0f409433023af448bb0328Richard Smith    RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result)
3336180f47959a066795cc0f409433023af448bb0328Richard Smith      : ExprEvaluatorBaseTy(info), This(This), Result(Result) {}
3337180f47959a066795cc0f409433023af448bb0328Richard Smith
33381aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    bool Success(const APValue &V, const Expr *E) {
333983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      Result = V;
334083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      return true;
3341180f47959a066795cc0f409433023af448bb0328Richard Smith    }
334251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    bool ZeroInitialization(const Expr *E);
3343180f47959a066795cc0f409433023af448bb0328Richard Smith
334459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    bool VisitCastExpr(const CastExpr *E);
3345180f47959a066795cc0f409433023af448bb0328Richard Smith    bool VisitInitListExpr(const InitListExpr *E);
3346180f47959a066795cc0f409433023af448bb0328Richard Smith    bool VisitCXXConstructExpr(const CXXConstructExpr *E);
3347180f47959a066795cc0f409433023af448bb0328Richard Smith  };
3348180f47959a066795cc0f409433023af448bb0328Richard Smith}
3349180f47959a066795cc0f409433023af448bb0328Richard Smith
335051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// Perform zero-initialization on an object of non-union class type.
335151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// C++11 [dcl.init]p5:
335251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith///  To zero-initialize an object or reference of type T means:
335351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith///    [...]
335451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith///    -- if T is a (possibly cv-qualified) non-union class type,
335551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith///       each non-static data member and each base-class subobject is
335651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith///       zero-initialized
3357b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E,
3358b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                                          const RecordDecl *RD,
335951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith                                          const LValue &This, APValue &Result) {
336051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  assert(!RD->isUnion() && "Expected non-union class type");
336151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD);
336251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  Result = APValue(APValue::UninitStruct(), CD ? CD->getNumBases() : 0,
336351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith                   std::distance(RD->field_begin(), RD->field_end()));
336451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
33658d59deec807ed53efcd07855199cdc9c979f447fJohn McCall  if (RD->isInvalidDecl()) return false;
336651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD);
336751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
336851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (CD) {
336951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    unsigned Index = 0;
337051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(),
3371b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith           End = CD->bases_end(); I != End; ++I, ++Index) {
337251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      const CXXRecordDecl *Base = I->getType()->getAsCXXRecordDecl();
337351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      LValue Subobject = This;
33748d59deec807ed53efcd07855199cdc9c979f447fJohn McCall      if (!HandleLValueDirectBase(Info, E, Subobject, CD, Base, &Layout))
33758d59deec807ed53efcd07855199cdc9c979f447fJohn McCall        return false;
3376b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      if (!HandleClassZeroInitialization(Info, E, Base, Subobject,
337751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith                                         Result.getStructBase(Index)))
337851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith        return false;
337951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    }
338051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  }
338151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
3382b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  for (RecordDecl::field_iterator I = RD->field_begin(), End = RD->field_end();
3383b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith       I != End; ++I) {
338451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    // -- if T is a reference type, no initialization is performed.
3385262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie    if (I->getType()->isReferenceType())
338651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      continue;
338751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
338851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    LValue Subobject = This;
3389581deb3da481053c4993c7600f97acf7768caac5David Blaikie    if (!HandleLValueMember(Info, E, Subobject, *I, &Layout))
33908d59deec807ed53efcd07855199cdc9c979f447fJohn McCall      return false;
339151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
3392262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie    ImplicitValueInitExpr VIE(I->getType());
339383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    if (!EvaluateInPlace(
3394262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie          Result.getStructField(I->getFieldIndex()), Info, Subobject, &VIE))
339551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      return false;
339651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  }
339751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
339851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  return true;
339951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith}
340051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
340151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smithbool RecordExprEvaluator::ZeroInitialization(const Expr *E) {
340251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl();
34031de9d7de172379d6af75fd11dda2a713e4f36f62John McCall  if (RD->isInvalidDecl()) return false;
340451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (RD->isUnion()) {
340551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the
340651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    // object's first non-static named data member is zero-initialized
340751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    RecordDecl::field_iterator I = RD->field_begin();
340851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    if (I == RD->field_end()) {
340951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      Result = APValue((const FieldDecl*)0);
341051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      return true;
341151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    }
341251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
341351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    LValue Subobject = This;
3414581deb3da481053c4993c7600f97acf7768caac5David Blaikie    if (!HandleLValueMember(Info, E, Subobject, *I))
34158d59deec807ed53efcd07855199cdc9c979f447fJohn McCall      return false;
3416581deb3da481053c4993c7600f97acf7768caac5David Blaikie    Result = APValue(*I);
3417262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie    ImplicitValueInitExpr VIE(I->getType());
341883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, &VIE);
341951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  }
342051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
3421ce582fe2a7aad8b14b3636ad9cac0a3b8bbb219bRichard Smith  if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->getNumVBases()) {
34225cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.Diag(E, diag::note_constexpr_virtual_base) << RD;
3423ce582fe2a7aad8b14b3636ad9cac0a3b8bbb219bRichard Smith    return false;
3424ce582fe2a7aad8b14b3636ad9cac0a3b8bbb219bRichard Smith  }
3425ce582fe2a7aad8b14b3636ad9cac0a3b8bbb219bRichard Smith
3426b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  return HandleClassZeroInitialization(Info, E, RD, This, Result);
342751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith}
342851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
342959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithbool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) {
343059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  switch (E->getCastKind()) {
343159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  default:
343259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    return ExprEvaluatorBaseTy::VisitCastExpr(E);
343359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
343459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  case CK_ConstructorConversion:
343559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    return Visit(E->getSubExpr());
343659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
343759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  case CK_DerivedToBase:
343859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  case CK_UncheckedDerivedToBase: {
34391aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    APValue DerivedObject;
3440f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!Evaluate(DerivedObject, Info, E->getSubExpr()))
344159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      return false;
3442f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!DerivedObject.isStruct())
3443f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E->getSubExpr());
344459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
344559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    // Derived-to-base rvalue conversion: just slice off the derived part.
344659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    APValue *Value = &DerivedObject;
344759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl();
344859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    for (CastExpr::path_const_iterator PathI = E->path_begin(),
344959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith         PathE = E->path_end(); PathI != PathE; ++PathI) {
345059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      assert(!(*PathI)->isVirtual() && "record rvalue with virtual base");
345159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl();
345259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      Value = &Value->getStructBase(getBaseIndex(RD, Base));
345359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      RD = Base;
345459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    }
345559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    Result = *Value;
345659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    return true;
345759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  }
345859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  }
345959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
346059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
3461180f47959a066795cc0f409433023af448bb0328Richard Smithbool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
346224fe798fffc1748d8bce1321af42981c3719cb85Sebastian Redl  // Cannot constant-evaluate std::initializer_list inits.
346324fe798fffc1748d8bce1321af42981c3719cb85Sebastian Redl  if (E->initializesStdInitializerList())
346424fe798fffc1748d8bce1321af42981c3719cb85Sebastian Redl    return false;
346524fe798fffc1748d8bce1321af42981c3719cb85Sebastian Redl
3466180f47959a066795cc0f409433023af448bb0328Richard Smith  const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl();
34671de9d7de172379d6af75fd11dda2a713e4f36f62John McCall  if (RD->isInvalidDecl()) return false;
3468180f47959a066795cc0f409433023af448bb0328Richard Smith  const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD);
3469180f47959a066795cc0f409433023af448bb0328Richard Smith
3470180f47959a066795cc0f409433023af448bb0328Richard Smith  if (RD->isUnion()) {
3471ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    const FieldDecl *Field = E->getInitializedFieldInUnion();
3472ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    Result = APValue(Field);
3473ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    if (!Field)
3474180f47959a066795cc0f409433023af448bb0328Richard Smith      return true;
3475ec789163a42a7be654ac34aadb750b508954d53cRichard Smith
3476ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    // If the initializer list for a union does not contain any elements, the
3477ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    // first element of the union is value-initialized.
3478ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    ImplicitValueInitExpr VIE(Field->getType());
3479ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    const Expr *InitExpr = E->getNumInits() ? E->getInit(0) : &VIE;
3480ec789163a42a7be654ac34aadb750b508954d53cRichard Smith
3481180f47959a066795cc0f409433023af448bb0328Richard Smith    LValue Subobject = This;
34828d59deec807ed53efcd07855199cdc9c979f447fJohn McCall    if (!HandleLValueMember(Info, InitExpr, Subobject, Field, &Layout))
34838d59deec807ed53efcd07855199cdc9c979f447fJohn McCall      return false;
348483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, InitExpr);
3485180f47959a066795cc0f409433023af448bb0328Richard Smith  }
3486180f47959a066795cc0f409433023af448bb0328Richard Smith
3487180f47959a066795cc0f409433023af448bb0328Richard Smith  assert((!isa<CXXRecordDecl>(RD) || !cast<CXXRecordDecl>(RD)->getNumBases()) &&
3488180f47959a066795cc0f409433023af448bb0328Richard Smith         "initializer list for class with base classes");
3489180f47959a066795cc0f409433023af448bb0328Richard Smith  Result = APValue(APValue::UninitStruct(), 0,
3490180f47959a066795cc0f409433023af448bb0328Richard Smith                   std::distance(RD->field_begin(), RD->field_end()));
3491180f47959a066795cc0f409433023af448bb0328Richard Smith  unsigned ElementNo = 0;
3492745f5147e065900267c85a5568785a1991d4838fRichard Smith  bool Success = true;
3493180f47959a066795cc0f409433023af448bb0328Richard Smith  for (RecordDecl::field_iterator Field = RD->field_begin(),
3494180f47959a066795cc0f409433023af448bb0328Richard Smith       FieldEnd = RD->field_end(); Field != FieldEnd; ++Field) {
3495180f47959a066795cc0f409433023af448bb0328Richard Smith    // Anonymous bit-fields are not considered members of the class for
3496180f47959a066795cc0f409433023af448bb0328Richard Smith    // purposes of aggregate initialization.
3497180f47959a066795cc0f409433023af448bb0328Richard Smith    if (Field->isUnnamedBitfield())
3498180f47959a066795cc0f409433023af448bb0328Richard Smith      continue;
3499180f47959a066795cc0f409433023af448bb0328Richard Smith
3500180f47959a066795cc0f409433023af448bb0328Richard Smith    LValue Subobject = This;
3501180f47959a066795cc0f409433023af448bb0328Richard Smith
3502745f5147e065900267c85a5568785a1991d4838fRichard Smith    bool HaveInit = ElementNo < E->getNumInits();
3503745f5147e065900267c85a5568785a1991d4838fRichard Smith
3504745f5147e065900267c85a5568785a1991d4838fRichard Smith    // FIXME: Diagnostics here should point to the end of the initializer
3505745f5147e065900267c85a5568785a1991d4838fRichard Smith    // list, not the start.
35068d59deec807ed53efcd07855199cdc9c979f447fJohn McCall    if (!HandleLValueMember(Info, HaveInit ? E->getInit(ElementNo) : E,
3507581deb3da481053c4993c7600f97acf7768caac5David Blaikie                            Subobject, *Field, &Layout))
35088d59deec807ed53efcd07855199cdc9c979f447fJohn McCall      return false;
3509745f5147e065900267c85a5568785a1991d4838fRichard Smith
3510745f5147e065900267c85a5568785a1991d4838fRichard Smith    // Perform an implicit value-initialization for members beyond the end of
3511745f5147e065900267c85a5568785a1991d4838fRichard Smith    // the initializer list.
3512745f5147e065900267c85a5568785a1991d4838fRichard Smith    ImplicitValueInitExpr VIE(HaveInit ? Info.Ctx.IntTy : Field->getType());
3513745f5147e065900267c85a5568785a1991d4838fRichard Smith
351483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    if (!EvaluateInPlace(
3515262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie          Result.getStructField(Field->getFieldIndex()),
3516745f5147e065900267c85a5568785a1991d4838fRichard Smith          Info, Subobject, HaveInit ? E->getInit(ElementNo++) : &VIE)) {
3517745f5147e065900267c85a5568785a1991d4838fRichard Smith      if (!Info.keepEvaluatingAfterFailure())
3518180f47959a066795cc0f409433023af448bb0328Richard Smith        return false;
3519745f5147e065900267c85a5568785a1991d4838fRichard Smith      Success = false;
3520180f47959a066795cc0f409433023af448bb0328Richard Smith    }
3521180f47959a066795cc0f409433023af448bb0328Richard Smith  }
3522180f47959a066795cc0f409433023af448bb0328Richard Smith
3523745f5147e065900267c85a5568785a1991d4838fRichard Smith  return Success;
3524180f47959a066795cc0f409433023af448bb0328Richard Smith}
3525180f47959a066795cc0f409433023af448bb0328Richard Smith
3526180f47959a066795cc0f409433023af448bb0328Richard Smithbool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) {
3527180f47959a066795cc0f409433023af448bb0328Richard Smith  const CXXConstructorDecl *FD = E->getConstructor();
35281de9d7de172379d6af75fd11dda2a713e4f36f62John McCall  if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl()) return false;
35291de9d7de172379d6af75fd11dda2a713e4f36f62John McCall
353051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  bool ZeroInit = E->requiresZeroInitialization();
353151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) {
3532ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    // If we've already performed zero-initialization, we're already done.
3533ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    if (!Result.isUninit())
3534ec789163a42a7be654ac34aadb750b508954d53cRichard Smith      return true;
3535ec789163a42a7be654ac34aadb750b508954d53cRichard Smith
353651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    if (ZeroInit)
353751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      return ZeroInitialization(E);
353851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
35396180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    const CXXRecordDecl *RD = FD->getParent();
35406180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    if (RD->isUnion())
35416180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith      Result = APValue((FieldDecl*)0);
35426180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    else
35436180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith      Result = APValue(APValue::UninitStruct(), RD->getNumBases(),
35446180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith                       std::distance(RD->field_begin(), RD->field_end()));
35456180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    return true;
35466180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith  }
35476180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith
3548180f47959a066795cc0f409433023af448bb0328Richard Smith  const FunctionDecl *Definition = 0;
3549180f47959a066795cc0f409433023af448bb0328Richard Smith  FD->getBody(Definition);
3550180f47959a066795cc0f409433023af448bb0328Richard Smith
3551c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition))
3552c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return false;
3553180f47959a066795cc0f409433023af448bb0328Richard Smith
3554610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith  // Avoid materializing a temporary for an elidable copy/move constructor.
355551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (E->isElidable() && !ZeroInit)
3556180f47959a066795cc0f409433023af448bb0328Richard Smith    if (const MaterializeTemporaryExpr *ME
3557180f47959a066795cc0f409433023af448bb0328Richard Smith          = dyn_cast<MaterializeTemporaryExpr>(E->getArg(0)))
3558180f47959a066795cc0f409433023af448bb0328Richard Smith      return Visit(ME->GetTemporaryExpr());
3559180f47959a066795cc0f409433023af448bb0328Richard Smith
356051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (ZeroInit && !ZeroInitialization(E))
356151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return false;
356251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
3563180f47959a066795cc0f409433023af448bb0328Richard Smith  llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs());
3564745f5147e065900267c85a5568785a1991d4838fRichard Smith  return HandleConstructorCall(E->getExprLoc(), This, Args,
3565f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                               cast<CXXConstructorDecl>(Definition), Info,
3566f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                               Result);
3567180f47959a066795cc0f409433023af448bb0328Richard Smith}
3568180f47959a066795cc0f409433023af448bb0328Richard Smith
3569180f47959a066795cc0f409433023af448bb0328Richard Smithstatic bool EvaluateRecord(const Expr *E, const LValue &This,
3570180f47959a066795cc0f409433023af448bb0328Richard Smith                           APValue &Result, EvalInfo &Info) {
3571180f47959a066795cc0f409433023af448bb0328Richard Smith  assert(E->isRValue() && E->getType()->isRecordType() &&
3572180f47959a066795cc0f409433023af448bb0328Richard Smith         "can't evaluate expression as a record rvalue");
3573180f47959a066795cc0f409433023af448bb0328Richard Smith  return RecordExprEvaluator(Info, This, Result).Visit(E);
3574180f47959a066795cc0f409433023af448bb0328Richard Smith}
3575180f47959a066795cc0f409433023af448bb0328Richard Smith
3576180f47959a066795cc0f409433023af448bb0328Richard Smith//===----------------------------------------------------------------------===//
3577e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// Temporary Evaluation
3578e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//
3579e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// Temporaries are represented in the AST as rvalues, but generally behave like
3580e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// lvalues. The full-object of which the temporary is a subobject is implicitly
3581e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// materialized so that a reference can bind to it.
3582e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===//
3583e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithnamespace {
3584e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithclass TemporaryExprEvaluator
3585e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  : public LValueExprEvaluatorBase<TemporaryExprEvaluator> {
3586e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithpublic:
3587e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  TemporaryExprEvaluator(EvalInfo &Info, LValue &Result) :
3588e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    LValueExprEvaluatorBaseTy(Info, Result) {}
3589e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3590e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  /// Visit an expression which constructs the value of this temporary.
3591e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitConstructExpr(const Expr *E) {
359283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    Result.set(E, Info.CurrentCall->Index);
359383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return EvaluateInPlace(Info.CurrentCall->Temporaries[E], Info, Result, E);
3594e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
3595e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3596e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitCastExpr(const CastExpr *E) {
3597e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    switch (E->getCastKind()) {
3598e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    default:
3599e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return LValueExprEvaluatorBaseTy::VisitCastExpr(E);
3600e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3601e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    case CK_ConstructorConversion:
3602e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return VisitConstructExpr(E->getSubExpr());
3603e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
3604e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
3605e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitInitListExpr(const InitListExpr *E) {
3606e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return VisitConstructExpr(E);
3607e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
3608e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitCXXConstructExpr(const CXXConstructExpr *E) {
3609e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return VisitConstructExpr(E);
3610e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
3611e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitCallExpr(const CallExpr *E) {
3612e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return VisitConstructExpr(E);
3613e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
3614e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith};
3615e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} // end anonymous namespace
3616e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3617e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// Evaluate an expression of record type as a temporary.
3618e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) {
3619af2c7a194592401394233b7cbcdd3cfd0a7a38ddRichard Smith  assert(E->isRValue() && E->getType()->isRecordType());
3620e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  return TemporaryExprEvaluator(Info, Result).Visit(E);
3621e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}
3622e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3623e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===//
362459b5da6d853b4368b984700315adf7b37de05764Nate Begeman// Vector Evaluation
362559b5da6d853b4368b984700315adf7b37de05764Nate Begeman//===----------------------------------------------------------------------===//
362659b5da6d853b4368b984700315adf7b37de05764Nate Begeman
362759b5da6d853b4368b984700315adf7b37de05764Nate Begemannamespace {
3628770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramer  class VectorExprEvaluator
362907fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith  : public ExprEvaluatorBase<VectorExprEvaluator, bool> {
363007fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    APValue &Result;
363159b5da6d853b4368b984700315adf7b37de05764Nate Begeman  public:
36321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
363307fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    VectorExprEvaluator(EvalInfo &info, APValue &Result)
363407fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith      : ExprEvaluatorBaseTy(info), Result(Result) {}
36351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
363607fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    bool Success(const ArrayRef<APValue> &V, const Expr *E) {
363707fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith      assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements());
363807fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith      // FIXME: remove this APValue copy.
363907fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith      Result = APValue(V.data(), V.size());
364007fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith      return true;
364107fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    }
36421aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    bool Success(const APValue &V, const Expr *E) {
364369c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith      assert(V.isVector());
364407fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith      Result = V;
364507fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith      return true;
364607fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    }
364751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    bool ZeroInitialization(const Expr *E);
36481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
364907fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    bool VisitUnaryReal(const UnaryOperator *E)
365091110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman      { return Visit(E->getSubExpr()); }
365107fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    bool VisitCastExpr(const CastExpr* E);
365207fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    bool VisitInitListExpr(const InitListExpr *E);
365307fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    bool VisitUnaryImag(const UnaryOperator *E);
365491110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman    // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
36552217c87bdc5ab357046a5453bdb06f469c41024eEli Friedman    //                 binary comparisons, binary and/or/xor,
365691110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman    //                 shufflevector, ExtVectorElementExpr
365759b5da6d853b4368b984700315adf7b37de05764Nate Begeman  };
365859b5da6d853b4368b984700315adf7b37de05764Nate Begeman} // end anonymous namespace
365959b5da6d853b4368b984700315adf7b37de05764Nate Begeman
366059b5da6d853b4368b984700315adf7b37de05764Nate Begemanstatic bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
3661c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  assert(E->isRValue() && E->getType()->isVectorType() &&"not a vector rvalue");
366207fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith  return VectorExprEvaluator(Info, Result).Visit(E);
366359b5da6d853b4368b984700315adf7b37de05764Nate Begeman}
366459b5da6d853b4368b984700315adf7b37de05764Nate Begeman
366507fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smithbool VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
366607fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith  const VectorType *VTy = E->getType()->castAs<VectorType>();
3667c0b8b19bd8056d6b5d831623a0825cce150f4507Nate Begeman  unsigned NElts = VTy->getNumElements();
36681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3669d62ca370b03b8c6ad58002d3399383baf744e32bRichard Smith  const Expr *SE = E->getSubExpr();
3670e8c9e9218f215ec6089f12b076c7b9d310fd5194Nate Begeman  QualType SETy = SE->getType();
367159b5da6d853b4368b984700315adf7b37de05764Nate Begeman
367246a523285928aa07bf14803178dc04616ac85994Eli Friedman  switch (E->getCastKind()) {
367346a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_VectorSplat: {
367407fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    APValue Val = APValue();
367546a523285928aa07bf14803178dc04616ac85994Eli Friedman    if (SETy->isIntegerType()) {
367646a523285928aa07bf14803178dc04616ac85994Eli Friedman      APSInt IntResult;
367746a523285928aa07bf14803178dc04616ac85994Eli Friedman      if (!EvaluateInteger(SE, IntResult, Info))
3678f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith         return false;
367907fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith      Val = APValue(IntResult);
368046a523285928aa07bf14803178dc04616ac85994Eli Friedman    } else if (SETy->isRealFloatingType()) {
368146a523285928aa07bf14803178dc04616ac85994Eli Friedman       APFloat F(0.0);
368246a523285928aa07bf14803178dc04616ac85994Eli Friedman       if (!EvaluateFloat(SE, F, Info))
3683f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith         return false;
368407fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith       Val = APValue(F);
368546a523285928aa07bf14803178dc04616ac85994Eli Friedman    } else {
368607fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith      return Error(E);
368746a523285928aa07bf14803178dc04616ac85994Eli Friedman    }
3688c0b8b19bd8056d6b5d831623a0825cce150f4507Nate Begeman
3689c0b8b19bd8056d6b5d831623a0825cce150f4507Nate Begeman    // Splat and create vector APValue.
369007fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    SmallVector<APValue, 4> Elts(NElts, Val);
369107fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    return Success(Elts, E);
3692e8c9e9218f215ec6089f12b076c7b9d310fd5194Nate Begeman  }
3693e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  case CK_BitCast: {
3694e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    // Evaluate the operand into an APInt we can extract from.
3695e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    llvm::APInt SValInt;
3696e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    if (!EvalAndBitcastToAPInt(Info, SE, SValInt))
3697e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      return false;
3698e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    // Extract the elements
3699e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    QualType EltTy = VTy->getElementType();
3700e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    unsigned EltSize = Info.Ctx.getTypeSize(EltTy);
3701e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian();
3702e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    SmallVector<APValue, 4> Elts;
3703e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    if (EltTy->isRealFloatingType()) {
3704e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(EltTy);
3705e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      bool isIEESem = &Sem != &APFloat::PPCDoubleDouble;
3706e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      unsigned FloatEltSize = EltSize;
3707e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      if (&Sem == &APFloat::x87DoubleExtended)
3708e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        FloatEltSize = 80;
3709e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      for (unsigned i = 0; i < NElts; i++) {
3710e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        llvm::APInt Elt;
3711e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        if (BigEndian)
3712e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman          Elt = SValInt.rotl(i*EltSize+FloatEltSize).trunc(FloatEltSize);
3713e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        else
3714e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman          Elt = SValInt.rotr(i*EltSize).trunc(FloatEltSize);
3715e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        Elts.push_back(APValue(APFloat(Elt, isIEESem)));
3716e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      }
3717e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    } else if (EltTy->isIntegerType()) {
3718e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      for (unsigned i = 0; i < NElts; i++) {
3719e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        llvm::APInt Elt;
3720e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        if (BigEndian)
3721e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman          Elt = SValInt.rotl(i*EltSize+EltSize).zextOrTrunc(EltSize);
3722e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        else
3723e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman          Elt = SValInt.rotr(i*EltSize).zextOrTrunc(EltSize);
3724e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        Elts.push_back(APValue(APSInt(Elt, EltTy->isSignedIntegerType())));
3725e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      }
3726e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    } else {
3727e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      return Error(E);
3728e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    }
3729e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    return Success(Elts, E);
3730e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  }
373146a523285928aa07bf14803178dc04616ac85994Eli Friedman  default:
3732c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return ExprEvaluatorBaseTy::VisitCastExpr(E);
3733c0b8b19bd8056d6b5d831623a0825cce150f4507Nate Begeman  }
373459b5da6d853b4368b984700315adf7b37de05764Nate Begeman}
373559b5da6d853b4368b984700315adf7b37de05764Nate Begeman
373607fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smithbool
373759b5da6d853b4368b984700315adf7b37de05764Nate BegemanVectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
373807fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith  const VectorType *VT = E->getType()->castAs<VectorType>();
373959b5da6d853b4368b984700315adf7b37de05764Nate Begeman  unsigned NumInits = E->getNumInits();
374091110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman  unsigned NumElements = VT->getNumElements();
37411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
374259b5da6d853b4368b984700315adf7b37de05764Nate Begeman  QualType EltTy = VT->getElementType();
37435f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<APValue, 4> Elements;
374459b5da6d853b4368b984700315adf7b37de05764Nate Begeman
37453edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman  // The number of initializers can be less than the number of
37463edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman  // vector elements. For OpenCL, this can be due to nested vector
37473edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman  // initialization. For GCC compatibility, missing trailing elements
37483edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman  // should be initialized with zeroes.
37493edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman  unsigned CountInits = 0, CountElts = 0;
37503edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman  while (CountElts < NumElements) {
37513edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman    // Handle nested vector initialization.
37523edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman    if (CountInits < NumInits
37533edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman        && E->getInit(CountInits)->getType()->isExtVectorType()) {
37543edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      APValue v;
37553edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      if (!EvaluateVector(E->getInit(CountInits), v, Info))
37563edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman        return Error(E);
37573edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      unsigned vlen = v.getVectorLength();
37583edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      for (unsigned j = 0; j < vlen; j++)
37593edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman        Elements.push_back(v.getVectorElt(j));
37603edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      CountElts += vlen;
37613edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman    } else if (EltTy->isIntegerType()) {
376259b5da6d853b4368b984700315adf7b37de05764Nate Begeman      llvm::APSInt sInt(32);
37633edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      if (CountInits < NumInits) {
37643edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman        if (!EvaluateInteger(E->getInit(CountInits), sInt, Info))
37654b1f684416980ef6f1a7cb9e6af9c4fa4a164617Richard Smith          return false;
37663edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      } else // trailing integer zero.
37673edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman        sInt = Info.Ctx.MakeIntValue(0, EltTy);
37683edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      Elements.push_back(APValue(sInt));
37693edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      CountElts++;
377059b5da6d853b4368b984700315adf7b37de05764Nate Begeman    } else {
377159b5da6d853b4368b984700315adf7b37de05764Nate Begeman      llvm::APFloat f(0.0);
37723edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      if (CountInits < NumInits) {
37733edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman        if (!EvaluateFloat(E->getInit(CountInits), f, Info))
37744b1f684416980ef6f1a7cb9e6af9c4fa4a164617Richard Smith          return false;
37753edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      } else // trailing float zero.
37763edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman        f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
37773edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      Elements.push_back(APValue(f));
37783edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      CountElts++;
377959b5da6d853b4368b984700315adf7b37de05764Nate Begeman    }
37803edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman    CountInits++;
378159b5da6d853b4368b984700315adf7b37de05764Nate Begeman  }
378207fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith  return Success(Elements, E);
378359b5da6d853b4368b984700315adf7b37de05764Nate Begeman}
378459b5da6d853b4368b984700315adf7b37de05764Nate Begeman
378507fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smithbool
378651201882382fb40c9456a06c7f93d6ddd4a57712Richard SmithVectorExprEvaluator::ZeroInitialization(const Expr *E) {
378707fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith  const VectorType *VT = E->getType()->getAs<VectorType>();
378891110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman  QualType EltTy = VT->getElementType();
378991110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman  APValue ZeroElement;
379091110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman  if (EltTy->isIntegerType())
379191110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman    ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
379291110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman  else
379391110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman    ZeroElement =
379491110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman        APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
379591110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman
37965f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
379707fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith  return Success(Elements, E);
379891110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman}
379991110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman
380007fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smithbool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
38018327fad71da34492d82c532f42a58cb4baff81a3Richard Smith  VisitIgnoredValue(E->getSubExpr());
380251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  return ZeroInitialization(E);
380391110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman}
380491110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman
380559b5da6d853b4368b984700315adf7b37de05764Nate Begeman//===----------------------------------------------------------------------===//
3806cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith// Array Evaluation
3807cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith//===----------------------------------------------------------------------===//
3808cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
3809cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smithnamespace {
3810cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  class ArrayExprEvaluator
3811cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  : public ExprEvaluatorBase<ArrayExprEvaluator, bool> {
3812180f47959a066795cc0f409433023af448bb0328Richard Smith    const LValue &This;
3813cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    APValue &Result;
3814cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  public:
3815cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
3816180f47959a066795cc0f409433023af448bb0328Richard Smith    ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result)
3817180f47959a066795cc0f409433023af448bb0328Richard Smith      : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {}
3818cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
3819cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    bool Success(const APValue &V, const Expr *E) {
3820f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith      assert((V.isArray() || V.isLValue()) &&
3821f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith             "expected array or string literal");
3822cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith      Result = V;
3823cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith      return true;
3824cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    }
3825cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
382651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    bool ZeroInitialization(const Expr *E) {
3827180f47959a066795cc0f409433023af448bb0328Richard Smith      const ConstantArrayType *CAT =
3828180f47959a066795cc0f409433023af448bb0328Richard Smith          Info.Ctx.getAsConstantArrayType(E->getType());
3829180f47959a066795cc0f409433023af448bb0328Richard Smith      if (!CAT)
3830f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(E);
3831180f47959a066795cc0f409433023af448bb0328Richard Smith
3832180f47959a066795cc0f409433023af448bb0328Richard Smith      Result = APValue(APValue::UninitArray(), 0,
3833180f47959a066795cc0f409433023af448bb0328Richard Smith                       CAT->getSize().getZExtValue());
3834180f47959a066795cc0f409433023af448bb0328Richard Smith      if (!Result.hasArrayFiller()) return true;
3835180f47959a066795cc0f409433023af448bb0328Richard Smith
383651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      // Zero-initialize all elements.
3837180f47959a066795cc0f409433023af448bb0328Richard Smith      LValue Subobject = This;
3838b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      Subobject.addArray(Info, E, CAT);
3839180f47959a066795cc0f409433023af448bb0328Richard Smith      ImplicitValueInitExpr VIE(CAT->getElementType());
384083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, &VIE);
3841180f47959a066795cc0f409433023af448bb0328Richard Smith    }
3842180f47959a066795cc0f409433023af448bb0328Richard Smith
3843cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    bool VisitInitListExpr(const InitListExpr *E);
3844e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    bool VisitCXXConstructExpr(const CXXConstructExpr *E);
3845cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  };
3846cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith} // end anonymous namespace
3847cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
3848180f47959a066795cc0f409433023af448bb0328Richard Smithstatic bool EvaluateArray(const Expr *E, const LValue &This,
3849180f47959a066795cc0f409433023af448bb0328Richard Smith                          APValue &Result, EvalInfo &Info) {
385051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  assert(E->isRValue() && E->getType()->isArrayType() && "not an array rvalue");
3851180f47959a066795cc0f409433023af448bb0328Richard Smith  return ArrayExprEvaluator(Info, This, Result).Visit(E);
3852cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith}
3853cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
3854cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smithbool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
3855cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType());
3856cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  if (!CAT)
3857f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
3858cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
3859974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith  // C++11 [dcl.init.string]p1: A char array [...] can be initialized by [...]
3860974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith  // an appropriately-typed string literal enclosed in braces.
3861fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith  if (E->isStringLiteralInit()) {
3862974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith    LValue LV;
3863974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith    if (!EvaluateLValue(E->getInit(0), LV, Info))
3864974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith      return false;
38651aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    APValue Val;
3866f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith    LV.moveInto(Val);
3867f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith    return Success(Val, E);
3868974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith  }
3869974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith
3870745f5147e065900267c85a5568785a1991d4838fRichard Smith  bool Success = true;
3871745f5147e065900267c85a5568785a1991d4838fRichard Smith
3872de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith  assert((!Result.isArray() || Result.getArrayInitializedElts() == 0) &&
3873de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith         "zero-initialized array shouldn't have any initialized elts");
3874de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith  APValue Filler;
3875de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith  if (Result.isArray() && Result.hasArrayFiller())
3876de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith    Filler = Result.getArrayFiller();
3877de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith
3878cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  Result = APValue(APValue::UninitArray(), E->getNumInits(),
3879cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith                   CAT->getSize().getZExtValue());
3880de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith
3881de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith  // If the array was previously zero-initialized, preserve the
3882de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith  // zero-initialized values.
3883de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith  if (!Filler.isUninit()) {
3884de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith    for (unsigned I = 0, E = Result.getArrayInitializedElts(); I != E; ++I)
3885de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith      Result.getArrayInitializedElt(I) = Filler;
3886de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith    if (Result.hasArrayFiller())
3887de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith      Result.getArrayFiller() = Filler;
3888de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith  }
3889de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith
3890180f47959a066795cc0f409433023af448bb0328Richard Smith  LValue Subobject = This;
3891b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  Subobject.addArray(Info, E, CAT);
3892180f47959a066795cc0f409433023af448bb0328Richard Smith  unsigned Index = 0;
3893cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  for (InitListExpr::const_iterator I = E->begin(), End = E->end();
3894180f47959a066795cc0f409433023af448bb0328Richard Smith       I != End; ++I, ++Index) {
389583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    if (!EvaluateInPlace(Result.getArrayInitializedElt(Index),
389683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                         Info, Subobject, cast<Expr>(*I)) ||
3897745f5147e065900267c85a5568785a1991d4838fRichard Smith        !HandleLValueArrayAdjustment(Info, cast<Expr>(*I), Subobject,
3898745f5147e065900267c85a5568785a1991d4838fRichard Smith                                     CAT->getElementType(), 1)) {
3899745f5147e065900267c85a5568785a1991d4838fRichard Smith      if (!Info.keepEvaluatingAfterFailure())
3900745f5147e065900267c85a5568785a1991d4838fRichard Smith        return false;
3901745f5147e065900267c85a5568785a1991d4838fRichard Smith      Success = false;
3902745f5147e065900267c85a5568785a1991d4838fRichard Smith    }
3903180f47959a066795cc0f409433023af448bb0328Richard Smith  }
3904cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
3905745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!Result.hasArrayFiller()) return Success;
3906cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  assert(E->hasArrayFiller() && "no array filler for incomplete init list");
3907180f47959a066795cc0f409433023af448bb0328Richard Smith  // FIXME: The Subobject here isn't necessarily right. This rarely matters,
3908180f47959a066795cc0f409433023af448bb0328Richard Smith  // but sometimes does:
3909180f47959a066795cc0f409433023af448bb0328Richard Smith  //   struct S { constexpr S() : p(&p) {} void *p; };
3910180f47959a066795cc0f409433023af448bb0328Richard Smith  //   S s[10] = {};
391183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  return EvaluateInPlace(Result.getArrayFiller(), Info,
391283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                         Subobject, E->getArrayFiller()) && Success;
3913cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith}
3914cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
3915e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithbool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) {
3916de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith  // FIXME: The Subobject here isn't necessarily right. This rarely matters,
3917de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith  // but sometimes does:
3918de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith  //   struct S { constexpr S() : p(&p) {} void *p; };
3919de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith  //   S s[10];
3920de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith  LValue Subobject = This;
3921de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith
3922de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith  APValue *Value = &Result;
3923de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith  bool HadZeroInit = true;
3924a4334dffde250c22c339a974a7131914fe723180Richard Smith  QualType ElemTy = E->getType();
3925a4334dffde250c22c339a974a7131914fe723180Richard Smith  while (const ConstantArrayType *CAT =
3926a4334dffde250c22c339a974a7131914fe723180Richard Smith           Info.Ctx.getAsConstantArrayType(ElemTy)) {
3927de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith    Subobject.addArray(Info, E, CAT);
3928de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith    HadZeroInit &= !Value->isUninit();
3929de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith    if (!HadZeroInit)
3930de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith      *Value = APValue(APValue::UninitArray(), 0, CAT->getSize().getZExtValue());
3931de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith    if (!Value->hasArrayFiller())
3932de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith      return true;
3933de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith    Value = &Value->getArrayFiller();
3934a4334dffde250c22c339a974a7131914fe723180Richard Smith    ElemTy = CAT->getElementType();
3935de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith  }
3936e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3937a4334dffde250c22c339a974a7131914fe723180Richard Smith  if (!ElemTy->isRecordType())
3938a4334dffde250c22c339a974a7131914fe723180Richard Smith    return Error(E);
3939a4334dffde250c22c339a974a7131914fe723180Richard Smith
3940e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  const CXXConstructorDecl *FD = E->getConstructor();
39416180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith
394251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  bool ZeroInit = E->requiresZeroInitialization();
394351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) {
3944ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    if (HadZeroInit)
3945ec789163a42a7be654ac34aadb750b508954d53cRichard Smith      return true;
3946ec789163a42a7be654ac34aadb750b508954d53cRichard Smith
394751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    if (ZeroInit) {
3948a4334dffde250c22c339a974a7131914fe723180Richard Smith      ImplicitValueInitExpr VIE(ElemTy);
3949de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith      return EvaluateInPlace(*Value, Info, Subobject, &VIE);
395051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    }
395151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
39526180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    const CXXRecordDecl *RD = FD->getParent();
39536180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    if (RD->isUnion())
3954de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith      *Value = APValue((FieldDecl*)0);
39556180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    else
3956de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith      *Value =
39576180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith          APValue(APValue::UninitStruct(), RD->getNumBases(),
39586180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith                  std::distance(RD->field_begin(), RD->field_end()));
39596180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    return true;
39606180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith  }
39616180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith
3962e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  const FunctionDecl *Definition = 0;
3963e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  FD->getBody(Definition);
3964e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3965c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition))
3966c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return false;
3967e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3968ec789163a42a7be654ac34aadb750b508954d53cRichard Smith  if (ZeroInit && !HadZeroInit) {
3969a4334dffde250c22c339a974a7131914fe723180Richard Smith    ImplicitValueInitExpr VIE(ElemTy);
3970de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith    if (!EvaluateInPlace(*Value, Info, Subobject, &VIE))
397151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      return false;
397251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  }
397351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
3974e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs());
3975745f5147e065900267c85a5568785a1991d4838fRichard Smith  return HandleConstructorCall(E->getExprLoc(), Subobject, Args,
3976e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                               cast<CXXConstructorDecl>(Definition),
3977de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith                               Info, *Value);
3978e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}
3979e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3980cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith//===----------------------------------------------------------------------===//
3981f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner// Integer Evaluation
3982c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//
3983c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// As a GNU extension, we support casting pointers to sufficiently-wide integer
3984c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// types and back in constant folding. Integer values are thus represented
3985c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// either as an integer-valued APValue, or as an lvalue-valued APValue.
3986f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===//
3987f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
3988f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattnernamespace {
3989770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass IntExprEvaluator
39908cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  : public ExprEvaluatorBase<IntExprEvaluator, bool> {
39911aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  APValue &Result;
3992f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattnerpublic:
39931aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  IntExprEvaluator(EvalInfo &info, APValue &result)
39948cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    : ExprEvaluatorBaseTy(info), Result(result) {}
3995f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
3996cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  bool Success(const llvm::APSInt &SI, const Expr *E, APValue &Result) {
3997973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara    assert(E->getType()->isIntegralOrEnumerationType() &&
39982ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor           "Invalid evaluation result.");
3999973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara    assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() &&
40003f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar           "Invalid evaluation result.");
4001973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara    assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
40023f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar           "Invalid evaluation result.");
40031aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    Result = APValue(SI);
40043f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar    return true;
40053f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar  }
4006cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  bool Success(const llvm::APSInt &SI, const Expr *E) {
4007cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return Success(SI, E, Result);
4008cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
40093f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar
4010cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  bool Success(const llvm::APInt &I, const Expr *E, APValue &Result) {
40112ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor    assert(E->getType()->isIntegralOrEnumerationType() &&
40122ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor           "Invalid evaluation result.");
401330c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar    assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
40143f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar           "Invalid evaluation result.");
40151aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    Result = APValue(APSInt(I));
4016575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor    Result.getInt().setIsUnsigned(
4017575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor                            E->getType()->isUnsignedIntegerOrEnumerationType());
4018131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar    return true;
4019131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar  }
4020cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  bool Success(const llvm::APInt &I, const Expr *E) {
4021cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return Success(I, E, Result);
4022cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4023131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar
4024cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  bool Success(uint64_t Value, const Expr *E, APValue &Result) {
40252ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor    assert(E->getType()->isIntegralOrEnumerationType() &&
40262ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor           "Invalid evaluation result.");
40271aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
4028131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar    return true;
4029131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar  }
4030cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  bool Success(uint64_t Value, const Expr *E) {
4031cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return Success(Value, E, Result);
4032cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4033131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar
40344f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck  bool Success(CharUnits Size, const Expr *E) {
40354f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck    return Success(Size.getQuantity(), E);
40364f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck  }
40374f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck
40381aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  bool Success(const APValue &V, const Expr *E) {
40395930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman    if (V.isLValue() || V.isAddrLabelDiff()) {
4040342f1f8b0a402c5a7f8c5055db7f60a7808f1687Richard Smith      Result = V;
4041342f1f8b0a402c5a7f8c5055db7f60a7808f1687Richard Smith      return true;
4042342f1f8b0a402c5a7f8c5055db7f60a7808f1687Richard Smith    }
40438cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    return Success(V.getInt(), E);
404432fea9d18cc3658a1b01df5ca6f2ac302625c61dChris Lattner  }
40451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
404651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  bool ZeroInitialization(const Expr *E) { return Success(0, E); }
4047f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith
40488cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  //===--------------------------------------------------------------------===//
40498cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  //                            Visitor Methods
40508cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  //===--------------------------------------------------------------------===//
4051f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
40524c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner  bool VisitIntegerLiteral(const IntegerLiteral *E) {
4053131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar    return Success(E->getValue(), E);
40544c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner  }
40554c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner  bool VisitCharacterLiteral(const CharacterLiteral *E) {
4056131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar    return Success(E->getValue(), E);
40574c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner  }
4058043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman
4059043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman  bool CheckReferencedDecl(const Expr *E, const Decl *D);
4060043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman  bool VisitDeclRefExpr(const DeclRefExpr *E) {
40618cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    if (CheckReferencedDecl(E, E->getDecl()))
40628cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne      return true;
40638cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
40648cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    return ExprEvaluatorBaseTy::VisitDeclRefExpr(E);
4065043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman  }
4066043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman  bool VisitMemberExpr(const MemberExpr *E) {
4067043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman    if (CheckReferencedDecl(E, E->getMemberDecl())) {
4068c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith      VisitIgnoredValue(E->getBase());
4069043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman      return true;
4070043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman    }
40718cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
40728cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    return ExprEvaluatorBaseTy::VisitMemberExpr(E);
4073043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman  }
4074043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman
40758cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCallExpr(const CallExpr *E);
4076b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattner  bool VisitBinaryOperator(const BinaryOperator *E);
40778ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  bool VisitOffsetOfExpr(const OffsetOfExpr *E);
4078b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattner  bool VisitUnaryOperator(const UnaryOperator *E);
4079f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
40808cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCastExpr(const CastExpr* E);
4081f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E);
40820518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl
40833068d117951a8df54bae9db039b56201ab10962bAnders Carlsson  bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
4084131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar    return Success(E->getValue(), E);
40853068d117951a8df54bae9db039b56201ab10962bAnders Carlsson  }
40861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4087ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  bool VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) {
4088ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    return Success(E->getValue(), E);
4089ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  }
4090ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
4091f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  // Note, GNU defines __null as an integer, not a pointer.
40923f70456b8adb0405ef2a47d51f9fc2d5937ae8aeAnders Carlsson  bool VisitGNUNullExpr(const GNUNullExpr *E) {
409351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return ZeroInitialization(E);
4094664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman  }
4095664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman
409664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
40970dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    return Success(E->getValue(), E);
409864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  }
409964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
41006ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  bool VisitBinaryTypeTraitExpr(const BinaryTypeTraitExpr *E) {
41016ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    return Success(E->getValue(), E);
41026ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  }
41036ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
41044ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  bool VisitTypeTraitExpr(const TypeTraitExpr *E) {
41054ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    return Success(E->getValue(), E);
41064ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  }
41074ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
410821ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) {
410921ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    return Success(E->getValue(), E);
411021ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  }
411121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
4112552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) {
4113552622067dc45013d240f73952fece703f5e63bdJohn Wiegley    return Success(E->getValue(), E);
4114552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  }
4115552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
4116722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman  bool VisitUnaryReal(const UnaryOperator *E);
4117664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman  bool VisitUnaryImag(const UnaryOperator *E);
4118664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman
4119295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl  bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E);
4120ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor  bool VisitSizeOfPackExpr(const SizeOfPackExpr *E);
4121cea8d966f826554f0679595e9371e314e8dbc1cfSebastian Redl
4122fcee0019b76f9f368f2b3d6d4048a98232593f29Chris Lattnerprivate:
41238b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck  CharUnits GetAlignOfExpr(const Expr *E);
41248b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck  CharUnits GetAlignOfType(QualType T);
41251bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  static QualType GetObjectType(APValue::LValueBase B);
41268cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool TryEvaluateBuiltinObjectSize(const CallExpr *E);
4127664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman  // FIXME: Missing: array subscript of vector, member of vector
4128f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner};
4129f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner} // end anonymous namespace
4130f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
4131c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// EvaluateIntegerOrLValue - Evaluate an rvalue integral-typed expression, and
4132c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// produce either the integer value or a pointer.
4133c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith///
4134c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// GCC has a heinous extension which folds casts between pointer types and
4135c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// pointer-sized integral types. We support this by allowing the evaluation of
4136c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// an integer rvalue to produce a pointer (represented as an lvalue) instead.
4137c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// Some simple arithmetic on such values is supported (they are treated much
4138c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// like char*).
41391aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smithstatic bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,
414047a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith                                    EvalInfo &Info) {
4141c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  assert(E->isRValue() && E->getType()->isIntegralOrEnumerationType());
41428cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  return IntExprEvaluator(Info, Result).Visit(E);
414369ab26a8623141f35e86817cfc6e0fbe7639a40fDaniel Dunbar}
414469ab26a8623141f35e86817cfc6e0fbe7639a40fDaniel Dunbar
4145f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info) {
41461aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  APValue Val;
4147f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (!EvaluateIntegerOrLValue(E, Val, Info))
4148f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return false;
4149f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (!Val.isInt()) {
4150f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    // FIXME: It would be better to produce the diagnostic for casting
4151f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    //        a pointer to an integer.
41525cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.Diag(E, diag::note_invalid_subexpr_in_const_expr);
415330c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar    return false;
4154f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
415530c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar  Result = Val.getInt();
415630c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar  return true;
4157f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner}
4158f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
4159f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// Check whether the given declaration can be directly converted to an integral
4160f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// rvalue. If not, no diagnostic is produced; there are other things we can
4161f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// try.
4162043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedmanbool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) {
41634c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner  // Enums are integer constant exprs.
4164bfbdcd861a4364bfc21a9e5047bdbd56812d6693Abramo Bagnara  if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) {
4165973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara    // Check for signedness/width mismatches between E type and ECD value.
4166973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara    bool SameSign = (ECD->getInitVal().isSigned()
4167973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara                     == E->getType()->isSignedIntegerOrEnumerationType());
4168973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara    bool SameWidth = (ECD->getInitVal().getBitWidth()
4169973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara                      == Info.Ctx.getIntWidth(E->getType()));
4170973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara    if (SameSign && SameWidth)
4171973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara      return Success(ECD->getInitVal(), E);
4172973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara    else {
4173973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara      // Get rid of mismatch (otherwise Success assertions will fail)
4174973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara      // by computing a new value matching the type of E.
4175973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara      llvm::APSInt Val = ECD->getInitVal();
4176973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara      if (!SameSign)
4177973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara        Val.setIsSigned(!ECD->getInitVal().isSigned());
4178973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara      if (!SameWidth)
4179973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara        Val = Val.extOrTrunc(Info.Ctx.getIntWidth(E->getType()));
4180973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara      return Success(Val, E);
4181973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara    }
4182bfbdcd861a4364bfc21a9e5047bdbd56812d6693Abramo Bagnara  }
41838cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  return false;
41844c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner}
41854c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner
4186a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
4187a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner/// as GCC.
4188a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattnerstatic int EvaluateBuiltinClassifyType(const CallExpr *E) {
4189a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  // The following enum mimics the values returned by GCC.
41907c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // FIXME: Does GCC differ between lvalue and rvalue references here?
4191a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  enum gcc_type_class {
4192a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    no_type_class = -1,
4193a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    void_type_class, integer_type_class, char_type_class,
4194a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    enumeral_type_class, boolean_type_class,
4195a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    pointer_type_class, reference_type_class, offset_type_class,
4196a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    real_type_class, complex_type_class,
4197a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    function_type_class, method_type_class,
4198a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    record_type_class, union_type_class,
4199a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    array_type_class, string_type_class,
4200a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    lang_type_class
4201a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  };
42021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
42031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If no argument was supplied, default to "no_type_class". This isn't
4204a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  // ideal, however it is what gcc does.
4205a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  if (E->getNumArgs() == 0)
4206a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return no_type_class;
42071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4208a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  QualType ArgTy = E->getArg(0)->getType();
4209a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  if (ArgTy->isVoidType())
4210a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return void_type_class;
4211a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isEnumeralType())
4212a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return enumeral_type_class;
4213a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isBooleanType())
4214a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return boolean_type_class;
4215a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isCharType())
4216a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return string_type_class; // gcc doesn't appear to use char_type_class
4217a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isIntegerType())
4218a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return integer_type_class;
4219a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isPointerType())
4220a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return pointer_type_class;
4221a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isReferenceType())
4222a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return reference_type_class;
4223a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isRealType())
4224a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return real_type_class;
4225a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isComplexType())
4226a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return complex_type_class;
4227a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isFunctionType())
4228a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return function_type_class;
4229fb87b89fc9eb103e19fb8e4b925c23f0bd091b99Douglas Gregor  else if (ArgTy->isStructureOrClassType())
4230a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return record_type_class;
4231a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isUnionType())
4232a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return union_type_class;
4233a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isArrayType())
4234a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return array_type_class;
4235a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isUnionType())
4236a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return union_type_class;
4237a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else  // FIXME: offset_type_class, method_type_class, & lang_type_class?
4238b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("CallExpr::isBuiltinClassifyType(): unimplemented type");
4239a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner}
4240a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner
424180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// EvaluateBuiltinConstantPForLValue - Determine the result of
424280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// __builtin_constant_p when applied to the given lvalue.
424380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith///
424480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// An lvalue is only "constant" if it is a pointer or reference to the first
424580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// character of a string literal.
424680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smithtemplate<typename LValue>
424780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smithstatic bool EvaluateBuiltinConstantPForLValue(const LValue &LV) {
42488e55ed1ad3acf9c7f8424aa7d326b3b2c18e943bDouglas Gregor  const Expr *E = LV.getLValueBase().template dyn_cast<const Expr*>();
424980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  return E && isa<StringLiteral>(E) && LV.getLValueOffset().isZero();
425080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith}
425180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith
425280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// EvaluateBuiltinConstantP - Evaluate __builtin_constant_p as similarly to
425380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// GCC as we can manage.
425480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smithstatic bool EvaluateBuiltinConstantP(ASTContext &Ctx, const Expr *Arg) {
425580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  QualType ArgType = Arg->getType();
425680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith
425780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  // __builtin_constant_p always has one operand. The rules which gcc follows
425880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  // are not precisely documented, but are as follows:
425980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  //
426080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  //  - If the operand is of integral, floating, complex or enumeration type,
426180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  //    and can be folded to a known value of that type, it returns 1.
426280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  //  - If the operand and can be folded to a pointer to the first character
426380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  //    of a string literal (or such a pointer cast to an integral type), it
426480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  //    returns 1.
426580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  //
426680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  // Otherwise, it returns 0.
426780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  //
426880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  // FIXME: GCC also intends to return 1 for literals of aggregate types, but
426980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  // its support for this does not currently work.
427080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  if (ArgType->isIntegralOrEnumerationType()) {
427180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    Expr::EvalResult Result;
427280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    if (!Arg->EvaluateAsRValue(Result, Ctx) || Result.HasSideEffects)
427380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith      return false;
427480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith
427580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    APValue &V = Result.Val;
427680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    if (V.getKind() == APValue::Int)
427780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith      return true;
427880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith
427980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    return EvaluateBuiltinConstantPForLValue(V);
428080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  } else if (ArgType->isFloatingType() || ArgType->isAnyComplexType()) {
428180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    return Arg->isEvaluatable(Ctx);
428280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  } else if (ArgType->isPointerType() || Arg->isGLValue()) {
428380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    LValue LV;
428480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    Expr::EvalStatus Status;
428580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    EvalInfo Info(Ctx, Status);
428680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    if ((Arg->isGLValue() ? EvaluateLValue(Arg, LV, Info)
428780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith                          : EvaluatePointer(Arg, LV, Info)) &&
428880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith        !Status.HasSideEffects)
428980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith      return EvaluateBuiltinConstantPForLValue(LV);
429080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  }
429180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith
429280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  // Anything else isn't considered to be sufficiently constant.
429380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  return false;
429480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith}
429580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith
429642c8f87eb60958170c46767273bf93e6c96125bfJohn McCall/// Retrieves the "underlying object type" of the given expression,
429742c8f87eb60958170c46767273bf93e6c96125bfJohn McCall/// as used by __builtin_object_size.
42981bf9a9e6a5bdc0de7939908855dcddf46b661800Richard SmithQualType IntExprEvaluator::GetObjectType(APValue::LValueBase B) {
42991bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) {
43001bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    if (const VarDecl *VD = dyn_cast<VarDecl>(D))
430142c8f87eb60958170c46767273bf93e6c96125bfJohn McCall      return VD->getType();
43021bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  } else if (const Expr *E = B.get<const Expr*>()) {
43031bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    if (isa<CompoundLiteralExpr>(E))
43041bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith      return E->getType();
430542c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  }
430642c8f87eb60958170c46767273bf93e6c96125bfJohn McCall
430742c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  return QualType();
430842c8f87eb60958170c46767273bf93e6c96125bfJohn McCall}
430942c8f87eb60958170c46767273bf93e6c96125bfJohn McCall
43108cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool IntExprEvaluator::TryEvaluateBuiltinObjectSize(const CallExpr *E) {
431142c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  LValue Base;
4312c6794850a570a91c5f224b6f0293db9f560f4213Richard Smith
4313c6794850a570a91c5f224b6f0293db9f560f4213Richard Smith  {
4314c6794850a570a91c5f224b6f0293db9f560f4213Richard Smith    // The operand of __builtin_object_size is never evaluated for side-effects.
4315c6794850a570a91c5f224b6f0293db9f560f4213Richard Smith    // If there are any, but we can determine the pointed-to object anyway, then
4316c6794850a570a91c5f224b6f0293db9f560f4213Richard Smith    // ignore the side-effects.
4317c6794850a570a91c5f224b6f0293db9f560f4213Richard Smith    SpeculativeEvaluationRAII SpeculativeEval(Info);
4318c6794850a570a91c5f224b6f0293db9f560f4213Richard Smith    if (!EvaluatePointer(E->getArg(0), Base, Info))
4319c6794850a570a91c5f224b6f0293db9f560f4213Richard Smith      return false;
4320c6794850a570a91c5f224b6f0293db9f560f4213Richard Smith  }
432142c8f87eb60958170c46767273bf93e6c96125bfJohn McCall
432242c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  // If we can prove the base is null, lower to zero now.
43231bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  if (!Base.getLValueBase()) return Success(0, E);
432442c8f87eb60958170c46767273bf93e6c96125bfJohn McCall
43251bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  QualType T = GetObjectType(Base.getLValueBase());
432642c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  if (T.isNull() ||
432742c8f87eb60958170c46767273bf93e6c96125bfJohn McCall      T->isIncompleteType() ||
43281357869bc5983cdfbc986db1f3d18265bb34cb0eEli Friedman      T->isFunctionType() ||
432942c8f87eb60958170c46767273bf93e6c96125bfJohn McCall      T->isVariablyModifiedType() ||
433042c8f87eb60958170c46767273bf93e6c96125bfJohn McCall      T->isDependentType())
4331f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
433242c8f87eb60958170c46767273bf93e6c96125bfJohn McCall
433342c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  CharUnits Size = Info.Ctx.getTypeSizeInChars(T);
433442c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  CharUnits Offset = Base.getLValueOffset();
433542c8f87eb60958170c46767273bf93e6c96125bfJohn McCall
433642c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  if (!Offset.isNegative() && Offset <= Size)
433742c8f87eb60958170c46767273bf93e6c96125bfJohn McCall    Size -= Offset;
433842c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  else
433942c8f87eb60958170c46767273bf93e6c96125bfJohn McCall    Size = CharUnits::Zero();
43404f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck  return Success(Size, E);
434142c8f87eb60958170c46767273bf93e6c96125bfJohn McCall}
434242c8f87eb60958170c46767273bf93e6c96125bfJohn McCall
43438cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
43442c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith  switch (unsigned BuiltinOp = E->isBuiltinCall()) {
4345019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  default:
43468cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    return ExprEvaluatorBaseTy::VisitCallExpr(E);
434764eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump
434864eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump  case Builtin::BI__builtin_object_size: {
434942c8f87eb60958170c46767273bf93e6c96125bfJohn McCall    if (TryEvaluateBuiltinObjectSize(E))
435042c8f87eb60958170c46767273bf93e6c96125bfJohn McCall      return true;
435164eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump
4352b2aaf51ed73a4774322a39a0dd59d0fe7e3258c0Eric Christopher    // If evaluating the argument has side-effects we can't determine
4353c6794850a570a91c5f224b6f0293db9f560f4213Richard Smith    // the size of the object and lower it to unknown now. CodeGen relies on
4354c6794850a570a91c5f224b6f0293db9f560f4213Richard Smith    // us to handle all cases where the expression has side-effects.
4355393c247fe025ccb5f914e37e948192ea86faef8cFariborz Jahanian    if (E->getArg(0)->HasSideEffects(Info.Ctx)) {
4356a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith      if (E->getArg(1)->EvaluateKnownConstInt(Info.Ctx).getZExtValue() <= 1)
4357cf184655319cf7a5b811067cff9d26a5741fd161Chris Lattner        return Success(-1ULL, E);
435864eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump      return Success(0, E);
435964eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump    }
4360c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump
4361c6794850a570a91c5f224b6f0293db9f560f4213Richard Smith    // Expression had no side effects, but we couldn't statically determine the
4362c6794850a570a91c5f224b6f0293db9f560f4213Richard Smith    // size of the referenced object.
4363f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
436464eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump  }
436564eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump
4366019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  case Builtin::BI__builtin_classify_type:
4367131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar    return Success(EvaluateBuiltinClassifyType(E), E);
43681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
436980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  case Builtin::BI__builtin_constant_p:
437080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    return Success(EvaluateBuiltinConstantP(Info.Ctx, E->getArg(0)), E);
4371e052d46f4db91f9ba572859ffc984e85cbf5d5ffRichard Smith
437221fb98ee003e992b0c4e204d98a19e0ef544cae3Chris Lattner  case Builtin::BI__builtin_eh_return_data_regno: {
4373a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith    int Operand = E->getArg(0)->EvaluateKnownConstInt(Info.Ctx).getZExtValue();
4374bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(Operand);
437521fb98ee003e992b0c4e204d98a19e0ef544cae3Chris Lattner    return Success(Operand, E);
437621fb98ee003e992b0c4e204d98a19e0ef544cae3Chris Lattner  }
4377c4a2638b5ef3e2d35d872614ceb655a7a22c58beEli Friedman
4378c4a2638b5ef3e2d35d872614ceb655a7a22c58beEli Friedman  case Builtin::BI__builtin_expect:
4379c4a2638b5ef3e2d35d872614ceb655a7a22c58beEli Friedman    return Visit(E->getArg(0));
438040b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith
43815726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor  case Builtin::BIstrlen:
438240b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith    // A call to strlen is not a constant expression.
438340b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith    if (Info.getLangOpts().CPlusPlus0x)
43845cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      Info.CCEDiag(E, diag::note_constexpr_invalid_function)
438540b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith        << /*isConstexpr*/0 << /*isConstructor*/0 << "'strlen'";
438640b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith    else
43875cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr);
438840b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith    // Fall through.
43895726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor  case Builtin::BI__builtin_strlen:
43905726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor    // As an extension, we support strlen() and __builtin_strlen() as constant
43915726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor    // expressions when the argument is a string literal.
43928cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    if (const StringLiteral *S
43935726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor               = dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenImpCasts())) {
43945726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor      // The string literal may have embedded null characters. Find the first
43955726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor      // one and truncate there.
43965f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      StringRef Str = S->getString();
43975f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      StringRef::size_type Pos = Str.find(0);
43985f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      if (Pos != StringRef::npos)
43995726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor        Str = Str.substr(0, Pos);
44005726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor
44015726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor      return Success(Str.size(), E);
44025726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor    }
44035726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor
4404f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
4405454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman
44062c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith  case Builtin::BI__atomic_always_lock_free:
4407fafbf06732746f3ceca21d452d77b144ba8652aeRichard Smith  case Builtin::BI__atomic_is_lock_free:
4408fafbf06732746f3ceca21d452d77b144ba8652aeRichard Smith  case Builtin::BI__c11_atomic_is_lock_free: {
4409454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    APSInt SizeVal;
4410454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    if (!EvaluateInteger(E->getArg(0), SizeVal, Info))
4411454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman      return false;
4412454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman
4413454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // For __atomic_is_lock_free(sizeof(_Atomic(T))), if the size is a power
4414454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // of two less than the maximum inline atomic width, we know it is
4415454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // lock-free.  If the size isn't a power of two, or greater than the
4416454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // maximum alignment where we promote atomics, we know it is not lock-free
4417454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // (at least not in the sense of atomic_is_lock_free).  Otherwise,
4418454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // the answer can only be determined at runtime; for example, 16-byte
4419454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // atomics have lock-free implementations on some, but not all,
4420454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // x86-64 processors.
4421454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman
4422454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // Check power-of-two.
4423454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    CharUnits Size = CharUnits::fromQuantity(SizeVal.getZExtValue());
44242c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith    if (Size.isPowerOfTwo()) {
44252c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith      // Check against inlining width.
44262c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith      unsigned InlineWidthBits =
44272c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith          Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth();
44282c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith      if (Size <= Info.Ctx.toCharUnitsFromBits(InlineWidthBits)) {
44292c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith        if (BuiltinOp == Builtin::BI__c11_atomic_is_lock_free ||
44302c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith            Size == CharUnits::One() ||
44312c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith            E->getArg(1)->isNullPointerConstant(Info.Ctx,
44322c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith                                                Expr::NPC_NeverValueDependent))
44332c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith          // OK, we will inline appropriately-aligned operations of this size,
44342c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith          // and _Atomic(T) is appropriately-aligned.
44352c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith          return Success(1, E);
44362c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith
44372c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith        QualType PointeeType = E->getArg(1)->IgnoreImpCasts()->getType()->
44382c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith          castAs<PointerType>()->getPointeeType();
44392c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith        if (!PointeeType->isIncompleteType() &&
44402c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith            Info.Ctx.getTypeAlignInChars(PointeeType) >= Size) {
44412c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith          // OK, we will inline operations on this object.
44422c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith          return Success(1, E);
44432c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith        }
44442c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith      }
44452c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith    }
4446454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman
44472c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith    return BuiltinOp == Builtin::BI__atomic_always_lock_free ?
44482c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith        Success(0, E) : Error(E);
4449454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman  }
4450019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  }
44514c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner}
4452f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
4453625b80755b603d28f36fb4212c81484d87ad08d3Richard Smithstatic bool HasSameBase(const LValue &A, const LValue &B) {
4454625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith  if (!A.getLValueBase())
4455625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith    return !B.getLValueBase();
4456625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith  if (!B.getLValueBase())
4457625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith    return false;
4458625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith
44591bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  if (A.getLValueBase().getOpaqueValue() !=
44601bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith      B.getLValueBase().getOpaqueValue()) {
4461625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith    const Decl *ADecl = GetLValueBaseDecl(A);
4462625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith    if (!ADecl)
4463625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith      return false;
4464625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith    const Decl *BDecl = GetLValueBaseDecl(B);
44659a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith    if (!BDecl || ADecl->getCanonicalDecl() != BDecl->getCanonicalDecl())
4466625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith      return false;
4467625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith  }
4468625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith
4469625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith  return IsGlobalLValue(A.getLValueBase()) ||
447083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith         A.getLValueCallIndex() == B.getLValueCallIndex();
4471625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith}
4472625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith
44737b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith/// Perform the given integer operation, which is known to need at most BitWidth
44747b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith/// bits, and check for overflow in the original type (if that type was not an
44757b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith/// unsigned type).
44767b48a2986345480241f3b8209f71bb21b0530b4fRichard Smithtemplate<typename Operation>
44777b48a2986345480241f3b8209f71bb21b0530b4fRichard Smithstatic APSInt CheckedIntArithmetic(EvalInfo &Info, const Expr *E,
44787b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith                                   const APSInt &LHS, const APSInt &RHS,
44797b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith                                   unsigned BitWidth, Operation Op) {
44807b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith  if (LHS.isUnsigned())
44817b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    return Op(LHS, RHS);
44827b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith
44837b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith  APSInt Value(Op(LHS.extend(BitWidth), RHS.extend(BitWidth)), false);
44847b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith  APSInt Result = Value.trunc(LHS.getBitWidth());
44857b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith  if (Result.extend(BitWidth) != Value)
44867b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    HandleOverflow(Info, E, Value, E->getType());
44877b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith  return Result;
44887b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith}
44897b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith
4490cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidisnamespace {
4491c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
4492cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis/// \brief Data recursive integer evaluator of certain binary operators.
4493cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis///
4494cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis/// We use a data recursive algorithm for binary operators so that we are able
4495cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis/// to handle extreme cases of chained binary operators without causing stack
4496cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis/// overflow.
4497cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidisclass DataRecursiveIntBinOpEvaluator {
4498cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  struct EvalResult {
4499cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    APValue Val;
4500cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    bool Failed;
4501cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4502cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    EvalResult() : Failed(false) { }
4503cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4504cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    void swap(EvalResult &RHS) {
4505cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      Val.swap(RHS.Val);
4506cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      Failed = RHS.Failed;
4507cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      RHS.Failed = false;
4508cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    }
4509cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  };
4510cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4511cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  struct Job {
4512cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    const Expr *E;
4513cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    EvalResult LHSResult; // meaningful only for binary operator expression.
4514cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    enum { AnyExprKind, BinOpKind, BinOpVisitedLHSKind } Kind;
4515cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4516cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    Job() : StoredInfo(0) { }
4517cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    void startSpeculativeEval(EvalInfo &Info) {
4518cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      OldEvalStatus = Info.EvalStatus;
4519cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      Info.EvalStatus.Diag = 0;
4520cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      StoredInfo = &Info;
4521cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    }
4522cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    ~Job() {
4523cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      if (StoredInfo) {
4524cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        StoredInfo->EvalStatus = OldEvalStatus;
4525cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      }
4526cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    }
4527cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  private:
4528cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    EvalInfo *StoredInfo; // non-null if status changed.
4529cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    Expr::EvalStatus OldEvalStatus;
4530cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  };
4531cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4532cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  SmallVector<Job, 16> Queue;
4533cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4534cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  IntExprEvaluator &IntEval;
4535cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  EvalInfo &Info;
4536cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  APValue &FinalResult;
4537cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4538cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidispublic:
4539cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  DataRecursiveIntBinOpEvaluator(IntExprEvaluator &IntEval, APValue &Result)
4540cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    : IntEval(IntEval), Info(IntEval.getEvalInfo()), FinalResult(Result) { }
4541cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4542cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  /// \brief True if \param E is a binary operator that we are going to handle
4543cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  /// data recursively.
4544cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  /// We handle binary operators that are comma, logical, or that have operands
4545cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  /// with integral or enumeration type.
4546cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  static bool shouldEnqueue(const BinaryOperator *E) {
4547cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return E->getOpcode() == BO_Comma ||
4548cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis           E->isLogicalOp() ||
4549cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis           (E->getLHS()->getType()->isIntegralOrEnumerationType() &&
4550cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis            E->getRHS()->getType()->isIntegralOrEnumerationType());
4551cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4552cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4553cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  bool Traverse(const BinaryOperator *E) {
4554cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    enqueue(E);
4555cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    EvalResult PrevResult;
4556b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu    while (!Queue.empty())
4557b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu      process(PrevResult);
4558b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu
4559b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu    if (PrevResult.Failed) return false;
4560cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4561cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    FinalResult.swap(PrevResult.Val);
4562cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return true;
4563a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman  }
4564a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman
4565cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidisprivate:
4566cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  bool Success(uint64_t Value, const Expr *E, APValue &Result) {
4567cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return IntEval.Success(Value, E, Result);
4568cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4569cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  bool Success(const APSInt &Value, const Expr *E, APValue &Result) {
4570cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return IntEval.Success(Value, E, Result);
4571cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4572cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  bool Error(const Expr *E) {
4573cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return IntEval.Error(E);
4574cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4575cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  bool Error(const Expr *E, diag::kind D) {
4576cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return IntEval.Error(E, D);
4577cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4578cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4579cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) {
4580cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return Info.CCEDiag(E, D);
4581cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4582cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
45839293fff58aa0198fa7a6bb504169bcac01dbbff7Argyrios Kyrtzidis  // \brief Returns true if visiting the RHS is necessary, false otherwise.
45849293fff58aa0198fa7a6bb504169bcac01dbbff7Argyrios Kyrtzidis  bool VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E,
4585cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                         bool &SuppressRHSDiags);
45862fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis
4587cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  bool VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult,
4588cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                  const BinaryOperator *E, APValue &Result);
4589cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4590cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  void EvaluateExpr(const Expr *E, EvalResult &Result) {
4591cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    Result.Failed = !Evaluate(Result.Val, Info, E);
4592cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    if (Result.Failed)
4593cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      Result.Val = APValue();
4594cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4595cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4596b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu  void process(EvalResult &Result);
4597cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4598cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  void enqueue(const Expr *E) {
4599cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    E = E->IgnoreParens();
4600cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    Queue.resize(Queue.size()+1);
4601cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    Queue.back().E = E;
4602cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    Queue.back().Kind = Job::AnyExprKind;
4603cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4604cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis};
4605cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4606cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis}
4607cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4608cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidisbool DataRecursiveIntBinOpEvaluator::
46099293fff58aa0198fa7a6bb504169bcac01dbbff7Argyrios Kyrtzidis       VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E,
4610cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                         bool &SuppressRHSDiags) {
4611cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  if (E->getOpcode() == BO_Comma) {
4612cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    // Ignore LHS but note if we could not evaluate it.
4613cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    if (LHSResult.Failed)
4614cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      Info.EvalStatus.HasSideEffects = true;
4615cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return true;
4616cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4617cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4618cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  if (E->isLogicalOp()) {
4619cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    bool lhsResult;
4620cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    if (HandleConversionToBool(LHSResult.Val, lhsResult)) {
46212fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis      // We were able to evaluate the LHS, see if we can get away with not
46222fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis      // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
4623cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      if (lhsResult == (E->getOpcode() == BO_LOr)) {
46249293fff58aa0198fa7a6bb504169bcac01dbbff7Argyrios Kyrtzidis        Success(lhsResult, E, LHSResult.Val);
46259293fff58aa0198fa7a6bb504169bcac01dbbff7Argyrios Kyrtzidis        return false; // Ignore RHS
46262fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis      }
46272fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis    } else {
46282fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis      // Since we weren't able to evaluate the left hand side, it
46292fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis      // must have had side effects.
46302fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis      Info.EvalStatus.HasSideEffects = true;
4631cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4632cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // We can't evaluate the LHS; however, sometimes the result
4633cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
4634cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // Don't ignore RHS and suppress diagnostics from this arm.
4635cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      SuppressRHSDiags = true;
4636cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    }
4637cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4638cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return true;
4639cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4640cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4641cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  assert(E->getLHS()->getType()->isIntegralOrEnumerationType() &&
4642cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis         E->getRHS()->getType()->isIntegralOrEnumerationType());
4643cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4644cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  if (LHSResult.Failed && !Info.keepEvaluatingAfterFailure())
46459293fff58aa0198fa7a6bb504169bcac01dbbff7Argyrios Kyrtzidis    return false; // Ignore RHS;
46469293fff58aa0198fa7a6bb504169bcac01dbbff7Argyrios Kyrtzidis
4647cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  return true;
4648cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis}
46492fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis
4650cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidisbool DataRecursiveIntBinOpEvaluator::
4651cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis       VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult,
4652cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                  const BinaryOperator *E, APValue &Result) {
4653cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  if (E->getOpcode() == BO_Comma) {
4654cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    if (RHSResult.Failed)
4655cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      return false;
4656cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    Result = RHSResult.Val;
4657cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return true;
4658cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4659cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4660cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  if (E->isLogicalOp()) {
4661cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    bool lhsResult, rhsResult;
4662cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    bool LHSIsOK = HandleConversionToBool(LHSResult.Val, lhsResult);
4663cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    bool RHSIsOK = HandleConversionToBool(RHSResult.Val, rhsResult);
4664cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4665cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    if (LHSIsOK) {
4666cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      if (RHSIsOK) {
4667cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        if (E->getOpcode() == BO_LOr)
4668cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis          return Success(lhsResult || rhsResult, E, Result);
4669cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        else
4670cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis          return Success(lhsResult && rhsResult, E, Result);
4671cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      }
4672cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    } else {
4673cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      if (RHSIsOK) {
46742fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis        // We can't evaluate the LHS; however, sometimes the result
46752fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis        // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
46762fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis        if (rhsResult == (E->getOpcode() == BO_LOr))
4677cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis          return Success(rhsResult, E, Result);
46782fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis      }
46792fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis    }
4680cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
46812fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis    return false;
46822fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis  }
4683cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4684cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  assert(E->getLHS()->getType()->isIntegralOrEnumerationType() &&
4685cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis         E->getRHS()->getType()->isIntegralOrEnumerationType());
4686cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4687cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  if (LHSResult.Failed || RHSResult.Failed)
4688cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return false;
4689cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4690cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  const APValue &LHSVal = LHSResult.Val;
4691cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  const APValue &RHSVal = RHSResult.Val;
4692cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4693cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  // Handle cases like (unsigned long)&a + 4.
4694cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) {
4695cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    Result = LHSVal;
4696cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    CharUnits AdditionalOffset = CharUnits::fromQuantity(
4697cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                                                         RHSVal.getInt().getZExtValue());
4698cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    if (E->getOpcode() == BO_Add)
4699cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      Result.getLValueOffset() += AdditionalOffset;
4700cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    else
4701cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      Result.getLValueOffset() -= AdditionalOffset;
4702cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return true;
4703cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4704cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4705cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  // Handle cases like 4 + (unsigned long)&a
4706cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  if (E->getOpcode() == BO_Add &&
4707cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      RHSVal.isLValue() && LHSVal.isInt()) {
4708cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    Result = RHSVal;
4709cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    Result.getLValueOffset() += CharUnits::fromQuantity(
4710cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                                                        LHSVal.getInt().getZExtValue());
4711cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return true;
4712cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4713cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4714cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  if (E->getOpcode() == BO_Sub && LHSVal.isLValue() && RHSVal.isLValue()) {
4715cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    // Handle (intptr_t)&&A - (intptr_t)&&B.
4716cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    if (!LHSVal.getLValueOffset().isZero() ||
4717cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        !RHSVal.getLValueOffset().isZero())
4718cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      return false;
4719cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    const Expr *LHSExpr = LHSVal.getLValueBase().dyn_cast<const Expr*>();
4720cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    const Expr *RHSExpr = RHSVal.getLValueBase().dyn_cast<const Expr*>();
4721cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    if (!LHSExpr || !RHSExpr)
4722cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      return false;
4723cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr);
4724cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr);
4725cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    if (!LHSAddrExpr || !RHSAddrExpr)
4726cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      return false;
4727cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    // Make sure both labels come from the same function.
4728cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    if (LHSAddrExpr->getLabel()->getDeclContext() !=
4729cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        RHSAddrExpr->getLabel()->getDeclContext())
4730cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      return false;
4731cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    Result = APValue(LHSAddrExpr, RHSAddrExpr);
4732cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return true;
4733cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4734cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4735cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  // All the following cases expect both operands to be an integer
4736cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  if (!LHSVal.isInt() || !RHSVal.isInt())
4737cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return Error(E);
4738cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4739cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  const APSInt &LHS = LHSVal.getInt();
4740cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  APSInt RHS = RHSVal.getInt();
4741cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4742cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  switch (E->getOpcode()) {
4743cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    default:
4744cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      return Error(E);
4745cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_Mul:
4746cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      return Success(CheckedIntArithmetic(Info, E, LHS, RHS,
4747cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                                          LHS.getBitWidth() * 2,
4748cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                                          std::multiplies<APSInt>()), E,
4749cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                     Result);
4750cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_Add:
4751cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      return Success(CheckedIntArithmetic(Info, E, LHS, RHS,
4752cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                                          LHS.getBitWidth() + 1,
4753cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                                          std::plus<APSInt>()), E, Result);
4754cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_Sub:
4755cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      return Success(CheckedIntArithmetic(Info, E, LHS, RHS,
4756cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                                          LHS.getBitWidth() + 1,
4757cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                                          std::minus<APSInt>()), E, Result);
4758cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_And: return Success(LHS & RHS, E, Result);
4759cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_Xor: return Success(LHS ^ RHS, E, Result);
4760cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_Or:  return Success(LHS | RHS, E, Result);
4761cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_Div:
4762cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_Rem:
4763cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      if (RHS == 0)
4764cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        return Error(E, diag::note_expr_divide_by_zero);
4765cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // Check for overflow case: INT_MIN / -1 or INT_MIN % -1. The latter is
4766cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // not actually undefined behavior in C++11 due to a language defect.
4767cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      if (RHS.isNegative() && RHS.isAllOnesValue() &&
4768cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis          LHS.isSigned() && LHS.isMinSignedValue())
4769cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        HandleOverflow(Info, E, -LHS.extend(LHS.getBitWidth() + 1), E->getType());
4770cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      return Success(E->getOpcode() == BO_Rem ? LHS % RHS : LHS / RHS, E,
4771cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                     Result);
4772cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_Shl: {
4773cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // During constant-folding, a negative shift is an opposite shift. Such
4774cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // a shift is not a constant expression.
4775cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      if (RHS.isSigned() && RHS.isNegative()) {
4776cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        CCEDiag(E, diag::note_constexpr_negative_shift) << RHS;
4777cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        RHS = -RHS;
4778cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        goto shift_right;
4779cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      }
4780cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4781cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    shift_left:
4782cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // C++11 [expr.shift]p1: Shift width must be less than the bit width of
4783cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // the shifted type.
4784cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1);
4785cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      if (SA != RHS) {
4786cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        CCEDiag(E, diag::note_constexpr_large_shift)
4787cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        << RHS << E->getType() << LHS.getBitWidth();
4788cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      } else if (LHS.isSigned()) {
4789cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        // C++11 [expr.shift]p2: A signed left shift must have a non-negative
4790cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        // operand, and must not overflow the corresponding unsigned type.
4791cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        if (LHS.isNegative())
4792cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis          CCEDiag(E, diag::note_constexpr_lshift_of_negative) << LHS;
4793cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        else if (LHS.countLeadingZeros() < SA)
4794cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis          CCEDiag(E, diag::note_constexpr_lshift_discards);
4795cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      }
4796cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4797cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      return Success(LHS << SA, E, Result);
4798cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    }
4799cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_Shr: {
4800cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // During constant-folding, a negative shift is an opposite shift. Such a
4801cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // shift is not a constant expression.
4802cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      if (RHS.isSigned() && RHS.isNegative()) {
4803cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        CCEDiag(E, diag::note_constexpr_negative_shift) << RHS;
4804cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        RHS = -RHS;
4805cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        goto shift_left;
4806cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      }
4807cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4808cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    shift_right:
4809cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // C++11 [expr.shift]p1: Shift width must be less than the bit width of the
4810cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // shifted type.
4811cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1);
4812cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      if (SA != RHS)
4813cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        CCEDiag(E, diag::note_constexpr_large_shift)
4814cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        << RHS << E->getType() << LHS.getBitWidth();
4815cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4816cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      return Success(LHS >> SA, E, Result);
4817cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    }
4818cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4819cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_LT: return Success(LHS < RHS, E, Result);
4820cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_GT: return Success(LHS > RHS, E, Result);
4821cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_LE: return Success(LHS <= RHS, E, Result);
4822cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_GE: return Success(LHS >= RHS, E, Result);
4823cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_EQ: return Success(LHS == RHS, E, Result);
4824cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_NE: return Success(LHS != RHS, E, Result);
4825cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4826cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis}
4827cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4828b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieuvoid DataRecursiveIntBinOpEvaluator::process(EvalResult &Result) {
4829cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  Job &job = Queue.back();
4830cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4831cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  switch (job.Kind) {
4832cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case Job::AnyExprKind: {
4833cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(job.E)) {
4834cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        if (shouldEnqueue(Bop)) {
4835cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis          job.Kind = Job::BinOpKind;
4836cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis          enqueue(Bop->getLHS());
4837b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu          return;
4838cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        }
4839cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      }
4840cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4841cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      EvaluateExpr(job.E, Result);
4842cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      Queue.pop_back();
4843b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu      return;
4844cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    }
4845cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4846cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case Job::BinOpKind: {
4847cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      const BinaryOperator *Bop = cast<BinaryOperator>(job.E);
4848cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      bool SuppressRHSDiags = false;
48499293fff58aa0198fa7a6bb504169bcac01dbbff7Argyrios Kyrtzidis      if (!VisitBinOpLHSOnly(Result, Bop, SuppressRHSDiags)) {
4850cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        Queue.pop_back();
4851b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu        return;
4852cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      }
4853cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      if (SuppressRHSDiags)
4854cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        job.startSpeculativeEval(Info);
48559293fff58aa0198fa7a6bb504169bcac01dbbff7Argyrios Kyrtzidis      job.LHSResult.swap(Result);
4856cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      job.Kind = Job::BinOpVisitedLHSKind;
4857cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      enqueue(Bop->getRHS());
4858b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu      return;
4859cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    }
4860cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4861cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case Job::BinOpVisitedLHSKind: {
4862cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      const BinaryOperator *Bop = cast<BinaryOperator>(job.E);
4863cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      EvalResult RHS;
4864cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      RHS.swap(Result);
4865b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu      Result.Failed = !VisitBinOp(job.LHSResult, RHS, Bop, Result.Val);
4866cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      Queue.pop_back();
4867b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu      return;
4868cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    }
4869cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4870cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4871cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  llvm_unreachable("Invalid Job::Kind!");
4872cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis}
4873cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4874cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidisbool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
4875cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  if (E->isAssignmentOp())
4876cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return Error(E);
4877cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4878cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  if (DataRecursiveIntBinOpEvaluator::shouldEnqueue(E))
4879cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return DataRecursiveIntBinOpEvaluator(*this, Result).Traverse(E);
488054176fdb044312b4b77c3da6682d3575b3728d30Chris Lattner
4881286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson  QualType LHSTy = E->getLHS()->getType();
4882286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson  QualType RHSTy = E->getRHS()->getType();
48834087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar
48844087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar  if (LHSTy->isAnyComplexType()) {
48854087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar    assert(RHSTy->isAnyComplexType() && "Invalid comparison");
4886f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    ComplexValue LHS, RHS;
48874087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar
4888745f5147e065900267c85a5568785a1991d4838fRichard Smith    bool LHSOK = EvaluateComplex(E->getLHS(), LHS, Info);
4889745f5147e065900267c85a5568785a1991d4838fRichard Smith    if (!LHSOK && !Info.keepEvaluatingAfterFailure())
48904087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar      return false;
48914087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar
4892745f5147e065900267c85a5568785a1991d4838fRichard Smith    if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK)
48934087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar      return false;
48944087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar
48954087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar    if (LHS.isComplexFloat()) {
48961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      APFloat::cmpResult CR_r =
48974087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar        LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
48981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      APFloat::cmpResult CR_i =
48994087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar        LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
49004087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar
49012de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      if (E->getOpcode() == BO_EQ)
4902131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar        return Success((CR_r == APFloat::cmpEqual &&
4903131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar                        CR_i == APFloat::cmpEqual), E);
4904131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar      else {
49052de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall        assert(E->getOpcode() == BO_NE &&
4906131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar               "Invalid complex comparison.");
49071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        return Success(((CR_r == APFloat::cmpGreaterThan ||
4908fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang                         CR_r == APFloat::cmpLessThan ||
4909fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang                         CR_r == APFloat::cmpUnordered) ||
49101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                        (CR_i == APFloat::cmpGreaterThan ||
4911fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang                         CR_i == APFloat::cmpLessThan ||
4912fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang                         CR_i == APFloat::cmpUnordered)), E);
4913131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar      }
49144087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar    } else {
49152de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      if (E->getOpcode() == BO_EQ)
4916131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar        return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
4917131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar                        LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
4918131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar      else {
49192de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall        assert(E->getOpcode() == BO_NE &&
4920131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar               "Invalid compex comparison.");
4921131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar        return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
4922131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar                        LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
4923131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar      }
49244087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar    }
49254087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar  }
49261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4927286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson  if (LHSTy->isRealFloatingType() &&
4928286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson      RHSTy->isRealFloatingType()) {
4929286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson    APFloat RHS(0.0), LHS(0.0);
49301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4931745f5147e065900267c85a5568785a1991d4838fRichard Smith    bool LHSOK = EvaluateFloat(E->getRHS(), RHS, Info);
4932745f5147e065900267c85a5568785a1991d4838fRichard Smith    if (!LHSOK && !Info.keepEvaluatingAfterFailure())
4933286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson      return false;
49341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4935745f5147e065900267c85a5568785a1991d4838fRichard Smith    if (!EvaluateFloat(E->getLHS(), LHS, Info) || !LHSOK)
4936286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson      return false;
49371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4938286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson    APFloat::cmpResult CR = LHS.compare(RHS);
4939529569e68d10b0fd3750fd2124faf742249b846bAnders Carlsson
4940286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson    switch (E->getOpcode()) {
4941286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson    default:
4942b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("Invalid binary operator!");
49432de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LT:
4944131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar      return Success(CR == APFloat::cmpLessThan, E);
49452de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_GT:
4946131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar      return Success(CR == APFloat::cmpGreaterThan, E);
49472de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LE:
4948131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar      return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
49492de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_GE:
49501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
4951131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar                     E);
49522de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_EQ:
4953131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar      return Success(CR == APFloat::cmpEqual, E);
49542de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_NE:
49551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return Success(CR == APFloat::cmpGreaterThan
4956fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang                     || CR == APFloat::cmpLessThan
4957fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang                     || CR == APFloat::cmpUnordered, E);
4958286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson    }
4959286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson  }
49601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4961ad02d7debd03ff275ac8ea27891a4ecccdb78068Eli Friedman  if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
4962625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith    if (E->getOpcode() == BO_Sub || E->isComparisonOp()) {
4963745f5147e065900267c85a5568785a1991d4838fRichard Smith      LValue LHSValue, RHSValue;
4964745f5147e065900267c85a5568785a1991d4838fRichard Smith
4965745f5147e065900267c85a5568785a1991d4838fRichard Smith      bool LHSOK = EvaluatePointer(E->getLHS(), LHSValue, Info);
4966745f5147e065900267c85a5568785a1991d4838fRichard Smith      if (!LHSOK && Info.keepEvaluatingAfterFailure())
49673068d117951a8df54bae9db039b56201ab10962bAnders Carlsson        return false;
4968a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman
4969745f5147e065900267c85a5568785a1991d4838fRichard Smith      if (!EvaluatePointer(E->getRHS(), RHSValue, Info) || !LHSOK)
49703068d117951a8df54bae9db039b56201ab10962bAnders Carlsson        return false;
4971a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman
4972625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith      // Reject differing bases from the normal codepath; we special-case
4973625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith      // comparisons to null.
4974625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith      if (!HasSameBase(LHSValue, RHSValue)) {
497565639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman        if (E->getOpcode() == BO_Sub) {
497665639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman          // Handle &&A - &&B.
497765639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman          if (!LHSValue.Offset.isZero() || !RHSValue.Offset.isZero())
497865639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman            return false;
497965639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman          const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr*>();
498065639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman          const Expr *RHSExpr = LHSValue.Base.dyn_cast<const Expr*>();
498165639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman          if (!LHSExpr || !RHSExpr)
498265639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman            return false;
498365639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman          const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr);
498465639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman          const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr);
498565639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman          if (!LHSAddrExpr || !RHSAddrExpr)
498665639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman            return false;
49875930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman          // Make sure both labels come from the same function.
49885930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman          if (LHSAddrExpr->getLabel()->getDeclContext() !=
49895930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman              RHSAddrExpr->getLabel()->getDeclContext())
49905930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman            return false;
49911aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith          Result = APValue(LHSAddrExpr, RHSAddrExpr);
499265639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman          return true;
499365639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman        }
49949e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith        // Inequalities and subtractions between unrelated pointers have
49959e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith        // unspecified or undefined behavior.
49965bc86103767c2abcbfdd6518e0ccbbbb6aa59e0fEli Friedman        if (!E->isEqualityOp())
4997f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith          return Error(E);
4998ffbda40a1fb7169591dc01771f3511178a2f727cEli Friedman        // A constant address may compare equal to the address of a symbol.
4999ffbda40a1fb7169591dc01771f3511178a2f727cEli Friedman        // The one exception is that address of an object cannot compare equal
5000c45061bd0c0fdad4df8eea7e9e5af186d11427e5Eli Friedman        // to a null pointer constant.
5001ffbda40a1fb7169591dc01771f3511178a2f727cEli Friedman        if ((!LHSValue.Base && !LHSValue.Offset.isZero()) ||
5002ffbda40a1fb7169591dc01771f3511178a2f727cEli Friedman            (!RHSValue.Base && !RHSValue.Offset.isZero()))
5003f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith          return Error(E);
50049e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith        // It's implementation-defined whether distinct literals will have
5005b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith        // distinct addresses. In clang, the result of such a comparison is
5006b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith        // unspecified, so it is not a constant expression. However, we do know
5007b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith        // that the address of a literal will be non-null.
500874f4634781cee06e28eb741bda5d0f936fdd1948Richard Smith        if ((IsLiteralLValue(LHSValue) || IsLiteralLValue(RHSValue)) &&
500974f4634781cee06e28eb741bda5d0f936fdd1948Richard Smith            LHSValue.Base && RHSValue.Base)
5010f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith          return Error(E);
50119e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith        // We can't tell whether weak symbols will end up pointing to the same
50129e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith        // object.
50139e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith        if (IsWeakLValue(LHSValue) || IsWeakLValue(RHSValue))
5014f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith          return Error(E);
50159e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith        // Pointers with different bases cannot represent the same object.
5016c45061bd0c0fdad4df8eea7e9e5af186d11427e5Eli Friedman        // (Note that clang defaults to -fmerge-all-constants, which can
5017c45061bd0c0fdad4df8eea7e9e5af186d11427e5Eli Friedman        // lead to inconsistent results for comparisons involving the address
5018c45061bd0c0fdad4df8eea7e9e5af186d11427e5Eli Friedman        // of a constant; this generally doesn't matter in practice.)
50199e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith        return Success(E->getOpcode() == BO_NE, E);
50205bc86103767c2abcbfdd6518e0ccbbbb6aa59e0fEli Friedman      }
5021a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman
502215efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith      const CharUnits &LHSOffset = LHSValue.getLValueOffset();
502315efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith      const CharUnits &RHSOffset = RHSValue.getLValueOffset();
502415efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith
5025f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator();
5026f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator();
5027f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
50282de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      if (E->getOpcode() == BO_Sub) {
5029f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        // C++11 [expr.add]p6:
5030f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        //   Unless both pointers point to elements of the same array object, or
5031f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        //   one past the last element of the array object, the behavior is
5032f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        //   undefined.
5033f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        if (!LHSDesignator.Invalid && !RHSDesignator.Invalid &&
5034f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith            !AreElementsOfSameArray(getType(LHSValue.Base),
5035f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith                                    LHSDesignator, RHSDesignator))
5036f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          CCEDiag(E, diag::note_constexpr_pointer_subtraction_not_same_array);
5037f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
50384992bdde387c5f033bb450a716eaabc0fda52688Chris Lattner        QualType Type = E->getLHS()->getType();
50394992bdde387c5f033bb450a716eaabc0fda52688Chris Lattner        QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
50403068d117951a8df54bae9db039b56201ab10962bAnders Carlsson
5041180f47959a066795cc0f409433023af448bb0328Richard Smith        CharUnits ElementSize;
504274e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith        if (!HandleSizeof(Info, E->getExprLoc(), ElementType, ElementSize))
5043180f47959a066795cc0f409433023af448bb0328Richard Smith          return false;
5044a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman
504515efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        // FIXME: LLVM and GCC both compute LHSOffset - RHSOffset at runtime,
504615efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        // and produce incorrect results when it overflows. Such behavior
504715efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        // appears to be non-conforming, but is common, so perhaps we should
504815efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        // assume the standard intended for such cases to be undefined behavior
504915efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        // and check for them.
505015efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith
505115efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        // Compute (LHSOffset - RHSOffset) / Size carefully, checking for
505215efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        // overflow in the final conversion to ptrdiff_t.
505315efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        APSInt LHS(
505415efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith          llvm::APInt(65, (int64_t)LHSOffset.getQuantity(), true), false);
505515efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        APSInt RHS(
505615efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith          llvm::APInt(65, (int64_t)RHSOffset.getQuantity(), true), false);
505715efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        APSInt ElemSize(
505815efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith          llvm::APInt(65, (int64_t)ElementSize.getQuantity(), true), false);
505915efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        APSInt TrueResult = (LHS - RHS) / ElemSize;
506015efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        APSInt Result = TrueResult.trunc(Info.Ctx.getIntWidth(E->getType()));
506115efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith
506215efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        if (Result.extend(65) != TrueResult)
506315efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith          HandleOverflow(Info, E, TrueResult, E->getType());
506415efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        return Success(Result, E);
5065ad02d7debd03ff275ac8ea27891a4ecccdb78068Eli Friedman      }
5066625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith
506782f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith      // C++11 [expr.rel]p3:
506882f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith      //   Pointers to void (after pointer conversions) can be compared, with a
506982f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith      //   result defined as follows: If both pointers represent the same
507082f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith      //   address or are both the null pointer value, the result is true if the
507182f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith      //   operator is <= or >= and false otherwise; otherwise the result is
507282f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith      //   unspecified.
507382f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith      // We interpret this as applying to pointers to *cv* void.
507482f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith      if (LHSTy->isVoidPointerType() && LHSOffset != RHSOffset &&
5075f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          E->isRelationalOp())
507682f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith        CCEDiag(E, diag::note_constexpr_void_comparison);
507782f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith
5078f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      // C++11 [expr.rel]p2:
5079f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      // - If two pointers point to non-static data members of the same object,
5080f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      //   or to subobjects or array elements fo such members, recursively, the
5081f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      //   pointer to the later declared member compares greater provided the
5082f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      //   two members have the same access control and provided their class is
5083f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      //   not a union.
5084f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      //   [...]
5085f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      // - Otherwise pointer comparisons are unspecified.
5086f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      if (!LHSDesignator.Invalid && !RHSDesignator.Invalid &&
5087f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          E->isRelationalOp()) {
5088f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        bool WasArrayIndex;
5089f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        unsigned Mismatch =
5090f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          FindDesignatorMismatch(getType(LHSValue.Base), LHSDesignator,
5091f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith                                 RHSDesignator, WasArrayIndex);
5092f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        // At the point where the designators diverge, the comparison has a
5093f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        // specified value if:
5094f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        //  - we are comparing array indices
5095f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        //  - we are comparing fields of a union, or fields with the same access
5096f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        // Otherwise, the result is unspecified and thus the comparison is not a
5097f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        // constant expression.
5098f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        if (!WasArrayIndex && Mismatch < LHSDesignator.Entries.size() &&
5099f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith            Mismatch < RHSDesignator.Entries.size()) {
5100f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          const FieldDecl *LF = getAsField(LHSDesignator.Entries[Mismatch]);
5101f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          const FieldDecl *RF = getAsField(RHSDesignator.Entries[Mismatch]);
5102f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          if (!LF && !RF)
5103f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith            CCEDiag(E, diag::note_constexpr_pointer_comparison_base_classes);
5104f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          else if (!LF)
5105f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith            CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field)
5106f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith              << getAsBaseClass(LHSDesignator.Entries[Mismatch])
5107f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith              << RF->getParent() << RF;
5108f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          else if (!RF)
5109f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith            CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field)
5110f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith              << getAsBaseClass(RHSDesignator.Entries[Mismatch])
5111f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith              << LF->getParent() << LF;
5112f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          else if (!LF->getParent()->isUnion() &&
5113f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith                   LF->getAccess() != RF->getAccess())
5114f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith            CCEDiag(E, diag::note_constexpr_pointer_comparison_differing_access)
5115f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith              << LF << LF->getAccess() << RF << RF->getAccess()
5116f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith              << LF->getParent();
5117f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        }
5118f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      }
5119f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
5120a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      // The comparison here must be unsigned, and performed with the same
5121a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      // width as the pointer.
5122a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      unsigned PtrSize = Info.Ctx.getTypeSize(LHSTy);
5123a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      uint64_t CompareLHS = LHSOffset.getQuantity();
5124a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      uint64_t CompareRHS = RHSOffset.getQuantity();
5125a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      assert(PtrSize <= 64 && "Unexpected pointer width");
5126a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      uint64_t Mask = ~0ULL >> (64 - PtrSize);
5127a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      CompareLHS &= Mask;
5128a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      CompareRHS &= Mask;
5129a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman
51302850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman      // If there is a base and this is a relational operator, we can only
51312850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman      // compare pointers within the object in question; otherwise, the result
51322850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman      // depends on where the object is located in memory.
51332850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman      if (!LHSValue.Base.isNull() && E->isRelationalOp()) {
51342850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman        QualType BaseTy = getType(LHSValue.Base);
51352850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman        if (BaseTy->isIncompleteType())
51362850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman          return Error(E);
51372850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman        CharUnits Size = Info.Ctx.getTypeSizeInChars(BaseTy);
51382850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman        uint64_t OffsetLimit = Size.getQuantity();
51392850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman        if (CompareLHS > OffsetLimit || CompareRHS > OffsetLimit)
51402850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman          return Error(E);
51412850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman      }
51422850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman
5143625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith      switch (E->getOpcode()) {
5144625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith      default: llvm_unreachable("missing comparison operator");
5145a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      case BO_LT: return Success(CompareLHS < CompareRHS, E);
5146a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      case BO_GT: return Success(CompareLHS > CompareRHS, E);
5147a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      case BO_LE: return Success(CompareLHS <= CompareRHS, E);
5148a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      case BO_GE: return Success(CompareLHS >= CompareRHS, E);
5149a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      case BO_EQ: return Success(CompareLHS == CompareRHS, E);
5150a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      case BO_NE: return Success(CompareLHS != CompareRHS, E);
5151ad02d7debd03ff275ac8ea27891a4ecccdb78068Eli Friedman      }
51523068d117951a8df54bae9db039b56201ab10962bAnders Carlsson    }
51533068d117951a8df54bae9db039b56201ab10962bAnders Carlsson  }
5154b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith
5155b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith  if (LHSTy->isMemberPointerType()) {
5156b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    assert(E->isEqualityOp() && "unexpected member pointer operation");
5157b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    assert(RHSTy->isMemberPointerType() && "invalid comparison");
5158b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith
5159b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    MemberPtr LHSValue, RHSValue;
5160b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith
5161b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    bool LHSOK = EvaluateMemberPointer(E->getLHS(), LHSValue, Info);
5162b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    if (!LHSOK && Info.keepEvaluatingAfterFailure())
5163b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith      return false;
5164b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith
5165b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    if (!EvaluateMemberPointer(E->getRHS(), RHSValue, Info) || !LHSOK)
5166b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith      return false;
5167b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith
5168b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    // C++11 [expr.eq]p2:
5169b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    //   If both operands are null, they compare equal. Otherwise if only one is
5170b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    //   null, they compare unequal.
5171b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    if (!LHSValue.getDecl() || !RHSValue.getDecl()) {
5172b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith      bool Equal = !LHSValue.getDecl() && !RHSValue.getDecl();
5173b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith      return Success(E->getOpcode() == BO_EQ ? Equal : !Equal, E);
5174b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    }
5175b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith
5176b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    //   Otherwise if either is a pointer to a virtual member function, the
5177b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    //   result is unspecified.
5178b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(LHSValue.getDecl()))
5179b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith      if (MD->isVirtual())
5180b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith        CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD;
5181b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(RHSValue.getDecl()))
5182b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith      if (MD->isVirtual())
5183b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith        CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD;
5184b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith
5185b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    //   Otherwise they compare equal if and only if they would refer to the
5186b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    //   same member of the same most derived object or the same subobject if
5187b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    //   they were dereferenced with a hypothetical object of the associated
5188b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    //   class type.
5189b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    bool Equal = LHSValue == RHSValue;
5190b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    return Success(E->getOpcode() == BO_EQ ? Equal : !Equal, E);
5191b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith  }
5192b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith
519326f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith  if (LHSTy->isNullPtrType()) {
519426f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith    assert(E->isComparisonOp() && "unexpected nullptr operation");
519526f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith    assert(RHSTy->isNullPtrType() && "missing pointer conversion");
519626f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith    // C++11 [expr.rel]p4, [expr.eq]p3: If two operands of type std::nullptr_t
519726f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith    // are compared, the result is true of the operator is <=, >= or ==, and
519826f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith    // false otherwise.
519926f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith    BinaryOperator::Opcode Opcode = E->getOpcode();
520026f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith    return Success(Opcode == BO_EQ || Opcode == BO_LE || Opcode == BO_GE, E);
520126f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith  }
520226f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith
5203cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  assert((!LHSTy->isIntegralOrEnumerationType() ||
5204cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis          !RHSTy->isIntegralOrEnumerationType()) &&
5205cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis         "DataRecursiveIntBinOpEvaluator should have handled integral types");
5206cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  // We can't continue from here for non-integral types.
5207cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
5208a25ae3d68d84d2b89907f998df6a396549589da5Anders Carlsson}
5209a25ae3d68d84d2b89907f998df6a396549589da5Anders Carlsson
52108b752f10c394b140f9ef89e049cbad1a7676fc25Ken DyckCharUnits IntExprEvaluator::GetAlignOfType(QualType T) {
52115d484e8cf710207010720589d89602233de61d01Sebastian Redl  // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
52125d484e8cf710207010720589d89602233de61d01Sebastian Redl  //   result shall be the alignment of the referenced type."
52135d484e8cf710207010720589d89602233de61d01Sebastian Redl  if (const ReferenceType *Ref = T->getAs<ReferenceType>())
52145d484e8cf710207010720589d89602233de61d01Sebastian Redl    T = Ref->getPointeeType();
52159f1210c3280104417a4ad30f0a00825ac8fa718aChad Rosier
52169f1210c3280104417a4ad30f0a00825ac8fa718aChad Rosier  // __alignof is defined to return the preferred alignment.
52179f1210c3280104417a4ad30f0a00825ac8fa718aChad Rosier  return Info.Ctx.toCharUnitsFromBits(
52189f1210c3280104417a4ad30f0a00825ac8fa718aChad Rosier    Info.Ctx.getPreferredTypeAlign(T.getTypePtr()));
5219e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner}
5220e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner
52218b752f10c394b140f9ef89e049cbad1a7676fc25Ken DyckCharUnits IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
5222af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  E = E->IgnoreParens();
5223af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner
5224af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  // alignof decl is always accepted, even if it doesn't make sense: we default
52251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // to 1 in those cases.
5226af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
52278b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck    return Info.Ctx.getDeclAlign(DRE->getDecl(),
52288b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck                                 /*RefAsPointee*/true);
5229a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman
5230af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
52318b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck    return Info.Ctx.getDeclAlign(ME->getMemberDecl(),
52328b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck                                 /*RefAsPointee*/true);
5233af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner
5234e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner  return GetAlignOfType(E->getType());
5235e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner}
5236e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner
5237e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner
5238f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne/// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with
5239f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne/// a result as the expression's type.
5240f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbournebool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr(
5241f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne                                    const UnaryExprOrTypeTraitExpr *E) {
5242f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  switch(E->getKind()) {
5243f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  case UETT_AlignOf: {
5244e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner    if (E->isArgumentType())
52454f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck      return Success(GetAlignOfType(E->getArgumentType()), E);
5246e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner    else
52474f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck      return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
5248e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner  }
5249a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman
5250f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  case UETT_VecStep: {
5251f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    QualType Ty = E->getTypeOfArgument();
52520518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl
5253f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    if (Ty->isVectorType()) {
5254f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne      unsigned n = Ty->getAs<VectorType>()->getNumElements();
5255a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman
5256f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne      // The vec_step built-in functions that take a 3-component
5257f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne      // vector return 4. (OpenCL 1.1 spec 6.11.12)
5258f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne      if (n == 3)
5259f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne        n = 4;
5260f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne
5261f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne      return Success(n, E);
5262f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    } else
5263f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne      return Success(1, E);
5264f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  }
5265f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne
5266f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  case UETT_SizeOf: {
5267f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    QualType SrcTy = E->getTypeOfArgument();
5268f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
5269f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    //   the result is the size of the referenced type."
5270f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>())
5271f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne      SrcTy = Ref->getPointeeType();
5272f2da9dfef96dc11b7b5effb1d02cb427b2d71599Eli Friedman
5273180f47959a066795cc0f409433023af448bb0328Richard Smith    CharUnits Sizeof;
527474e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    if (!HandleSizeof(Info, E->getExprLoc(), SrcTy, Sizeof))
5275f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne      return false;
5276180f47959a066795cc0f409433023af448bb0328Richard Smith    return Success(Sizeof, E);
5277f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  }
5278f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  }
5279f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne
5280f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  llvm_unreachable("unknown expr/type trait");
5281fcee0019b76f9f368f2b3d6d4048a98232593f29Chris Lattner}
5282fcee0019b76f9f368f2b3d6d4048a98232593f29Chris Lattner
52838cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) {
52848ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  CharUnits Result;
52858cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  unsigned n = OOE->getNumComponents();
52868ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  if (n == 0)
5287f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(OOE);
52888cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  QualType CurrentType = OOE->getTypeSourceInfo()->getType();
52898ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  for (unsigned i = 0; i != n; ++i) {
52908ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    OffsetOfExpr::OffsetOfNode ON = OOE->getComponent(i);
52918ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    switch (ON.getKind()) {
52928ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    case OffsetOfExpr::OffsetOfNode::Array: {
52938cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne      const Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex());
52948ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      APSInt IdxResult;
52958ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      if (!EvaluateInteger(Idx, IdxResult, Info))
52968ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor        return false;
52978ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType);
52988ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      if (!AT)
5299f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(OOE);
53008ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      CurrentType = AT->getElementType();
53018ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType);
53028ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      Result += IdxResult.getSExtValue() * ElementSize;
53038ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor        break;
53048ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    }
5305f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
53068ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    case OffsetOfExpr::OffsetOfNode::Field: {
53078ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      FieldDecl *MemberDecl = ON.getField();
53088ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      const RecordType *RT = CurrentType->getAs<RecordType>();
5309f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      if (!RT)
5310f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(OOE);
53118ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      RecordDecl *RD = RT->getDecl();
53128d59deec807ed53efcd07855199cdc9c979f447fJohn McCall      if (RD->isInvalidDecl()) return false;
53138ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
5314ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      unsigned i = MemberDecl->getFieldIndex();
5315cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      assert(i < RL.getFieldCount() && "offsetof field in wrong type");
5316fb1e3bc29b667f4275e1d5a43d64ec173f4f9a7dKen Dyck      Result += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i));
53178ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      CurrentType = MemberDecl->getType().getNonReferenceType();
53188ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      break;
53198ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    }
5320f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
53218ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    case OffsetOfExpr::OffsetOfNode::Identifier:
53228ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      llvm_unreachable("dependent __builtin_offsetof");
5323f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
5324cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor    case OffsetOfExpr::OffsetOfNode::Base: {
5325cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      CXXBaseSpecifier *BaseSpec = ON.getBase();
5326cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      if (BaseSpec->isVirtual())
5327f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(OOE);
5328cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor
5329cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      // Find the layout of the class whose base we are looking into.
5330cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      const RecordType *RT = CurrentType->getAs<RecordType>();
5331f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      if (!RT)
5332f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(OOE);
5333cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      RecordDecl *RD = RT->getDecl();
53348d59deec807ed53efcd07855199cdc9c979f447fJohn McCall      if (RD->isInvalidDecl()) return false;
5335cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
5336cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor
5337cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      // Find the base class itself.
5338cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      CurrentType = BaseSpec->getType();
5339cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      const RecordType *BaseRT = CurrentType->getAs<RecordType>();
5340cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      if (!BaseRT)
5341f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(OOE);
5342cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor
5343cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      // Add the offset to the base.
53447c7f820d70c925b29290a8563b59615816a827fcKen Dyck      Result += RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl()));
5345cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      break;
5346cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor    }
53478ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    }
53488ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  }
53498cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  return Success(Result, OOE);
53508ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor}
53518ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor
5352b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattnerbool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
535375a4881047deeb3a300ff9293dc6ba8570048bb5Chris Lattner  switch (E->getOpcode()) {
53544c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner  default:
535575a4881047deeb3a300ff9293dc6ba8570048bb5Chris Lattner    // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
535675a4881047deeb3a300ff9293dc6ba8570048bb5Chris Lattner    // See C99 6.6p3.
5357f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
53582de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Extension:
53594c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner    // FIXME: Should extension allow i-c-e extension expressions in its scope?
53604c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner    // If so, we could clear the diagnostic ID.
5361f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Visit(E->getSubExpr());
53622de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Plus:
5363c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    // The result is just the value.
5364f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Visit(E->getSubExpr());
5365f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  case UO_Minus: {
5366f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!Visit(E->getSubExpr()))
5367f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return false;
5368f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!Result.isInt()) return Error(E);
5369789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    const APSInt &Value = Result.getInt();
5370789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    if (Value.isSigned() && Value.isMinSignedValue())
5371789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith      HandleOverflow(Info, E, -Value.extend(Value.getBitWidth() + 1),
5372789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith                     E->getType());
5373789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    return Success(-Value, E);
5374f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
5375f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  case UO_Not: {
5376f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!Visit(E->getSubExpr()))
5377f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return false;
5378f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!Result.isInt()) return Error(E);
5379f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Success(~Result.getInt(), E);
5380f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
5381f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  case UO_LNot: {
5382f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    bool bres;
5383f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!EvaluateAsBooleanCondition(E->getSubExpr(), bres, Info))
5384f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return false;
5385f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Success(!bres, E);
5386f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
538706a3675627e3b3c47b49c689c8e404a33144194aAnders Carlsson  }
5388a25ae3d68d84d2b89907f998df6a396549589da5Anders Carlsson}
53891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5390732b2236ae4a4f11e7642677cebbd169c07ea877Chris Lattner/// HandleCast - This is used to evaluate implicit or explicit casts where the
5391732b2236ae4a4f11e7642677cebbd169c07ea877Chris Lattner/// result type is integer.
53928cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool IntExprEvaluator::VisitCastExpr(const CastExpr *E) {
53938cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  const Expr *SubExpr = E->getSubExpr();
539482206e267ce6cc709797127616f64672d255b310Anders Carlsson  QualType DestType = E->getType();
5395b92dac8bc2f6f73919825f9af693a8a7e89ae1d4Daniel Dunbar  QualType SrcType = SubExpr->getType();
539682206e267ce6cc709797127616f64672d255b310Anders Carlsson
539746a523285928aa07bf14803178dc04616ac85994Eli Friedman  switch (E->getCastKind()) {
539846a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_BaseToDerived:
539946a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_DerivedToBase:
540046a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_UncheckedDerivedToBase:
540146a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_Dynamic:
540246a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_ToUnion:
540346a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_ArrayToPointerDecay:
540446a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_FunctionToPointerDecay:
540546a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_NullToPointer:
540646a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_NullToMemberPointer:
540746a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_BaseToDerivedMemberPointer:
540846a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_DerivedToBaseMemberPointer:
54094d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  case CK_ReinterpretMemberPointer:
541046a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_ConstructorConversion:
541146a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_IntegralToPointer:
541246a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_ToVoid:
541346a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_VectorSplat:
541446a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_IntegralToFloating:
541546a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_FloatingCast:
54161d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_CPointerToObjCPointerCast:
54171d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_BlockPointerToObjCPointerCast:
541846a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_AnyPointerToBlockPointerCast:
541946a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_ObjCObjectLValueCast:
542046a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_FloatingRealToComplex:
542146a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_FloatingComplexToReal:
542246a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_FloatingComplexCast:
542346a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_FloatingComplexToIntegralComplex:
542446a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_IntegralRealToComplex:
542546a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_IntegralComplexCast:
542646a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_IntegralComplexToFloatingComplex:
542746a523285928aa07bf14803178dc04616ac85994Eli Friedman    llvm_unreachable("invalid cast kind for integral value");
542846a523285928aa07bf14803178dc04616ac85994Eli Friedman
5429e50c297f92914ca996deb8b597624193273b62e4Eli Friedman  case CK_BitCast:
543046a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_Dependent:
543146a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_LValueBitCast:
543233e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCProduceObject:
543333e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCConsumeObject:
543433e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCReclaimReturnedObject:
543533e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCExtendBlockObject:
5436ac1303eca6cbe3e623fb5ec6fe7ec184ef4b0dfaDouglas Gregor  case CK_CopyAndAutoreleaseBlockObject:
5437f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
543846a523285928aa07bf14803178dc04616ac85994Eli Friedman
54397d580a4e9e47dffc3c17aa2b957ac57ca3c4e451Richard Smith  case CK_UserDefinedConversion:
544046a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_LValueToRValue:
54417a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall  case CK_AtomicToNonAtomic:
54427a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall  case CK_NonAtomicToAtomic:
544346a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_NoOp:
5444c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return ExprEvaluatorBaseTy::VisitCastExpr(E);
544546a523285928aa07bf14803178dc04616ac85994Eli Friedman
544646a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_MemberPointerToBoolean:
544746a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_PointerToBoolean:
544846a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_IntegralToBoolean:
544946a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_FloatingToBoolean:
545046a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_FloatingComplexToBoolean:
545146a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_IntegralComplexToBoolean: {
54524efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman    bool BoolResult;
5453c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    if (!EvaluateAsBooleanCondition(SubExpr, BoolResult, Info))
54544efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman      return false;
5455131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar    return Success(BoolResult, E);
54564efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman  }
54574efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
545846a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_IntegralCast: {
5459732b2236ae4a4f11e7642677cebbd169c07ea877Chris Lattner    if (!Visit(SubExpr))
5460b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattner      return false;
5461a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar
5462be26570e3faa009bdcefedfaf04473e518940520Eli Friedman    if (!Result.isInt()) {
546365639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman      // Allow casts of address-of-label differences if they are no-ops
546465639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman      // or narrowing.  (The narrowing case isn't actually guaranteed to
546565639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman      // be constant-evaluatable except in some narrow cases which are hard
546665639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman      // to detect here.  We let it through on the assumption the user knows
546765639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman      // what they are doing.)
546865639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman      if (Result.isAddrLabelDiff())
546965639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman        return Info.Ctx.getTypeSize(DestType) <= Info.Ctx.getTypeSize(SrcType);
5470be26570e3faa009bdcefedfaf04473e518940520Eli Friedman      // Only allow casts of lvalues if they are lossless.
5471be26570e3faa009bdcefedfaf04473e518940520Eli Friedman      return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
5472be26570e3faa009bdcefedfaf04473e518940520Eli Friedman    }
547330c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar
5474f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith    return Success(HandleIntToIntCast(Info, E, DestType, SrcType,
5475f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith                                      Result.getInt()), E);
5476732b2236ae4a4f11e7642677cebbd169c07ea877Chris Lattner  }
54771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
547846a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_PointerToIntegral: {
5479c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith    CCEDiag(E, diag::note_constexpr_invalid_cast) << 2;
5480c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith
5481efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    LValue LV;
548287eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner    if (!EvaluatePointer(SubExpr, LV, Info))
5483b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattner      return false;
54844efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
5485dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar    if (LV.getLValueBase()) {
5486dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar      // Only allow based lvalue casts if they are lossless.
5487f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith      // FIXME: Allow a larger integer size than the pointer size, and allow
5488f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith      // narrowing back down to pointer width in subsequent integral casts.
5489f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith      // FIXME: Check integer type's active bits, not its type size.
5490dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar      if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
5491f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(E);
5492dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar
5493b755a9da095d2f2f04444797f1e1a9511693815bRichard Smith      LV.Designator.setInvalid();
5494efdb83e26f9a1fd2566afe54461216cd84814d42John McCall      LV.moveInto(Result);
5495dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar      return true;
5496dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar    }
54974efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
5498a73058324197b7bdfd19307965954f626e26199dKen Dyck    APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(),
5499a73058324197b7bdfd19307965954f626e26199dKen Dyck                                         SrcType);
5500f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith    return Success(HandleIntToIntCast(Info, E, DestType, SrcType, AsInt), E);
55012bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson  }
55024efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
550346a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_IntegralComplexToReal: {
5504f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    ComplexValue C;
55051725f683432715e5afe34d476024bd6f16eac3fcEli Friedman    if (!EvaluateComplex(SubExpr, C, Info))
55061725f683432715e5afe34d476024bd6f16eac3fcEli Friedman      return false;
550746a523285928aa07bf14803178dc04616ac85994Eli Friedman    return Success(C.getComplexIntReal(), E);
55081725f683432715e5afe34d476024bd6f16eac3fcEli Friedman  }
55092217c87bdc5ab357046a5453bdb06f469c41024eEli Friedman
551046a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_FloatingToIntegral: {
551146a523285928aa07bf14803178dc04616ac85994Eli Friedman    APFloat F(0.0);
551246a523285928aa07bf14803178dc04616ac85994Eli Friedman    if (!EvaluateFloat(SubExpr, F, Info))
551346a523285928aa07bf14803178dc04616ac85994Eli Friedman      return false;
5514732b2236ae4a4f11e7642677cebbd169c07ea877Chris Lattner
5515c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    APSInt Value;
5516c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    if (!HandleFloatToIntCast(Info, E, SrcType, F, DestType, Value))
5517c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      return false;
5518c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return Success(Value, E);
551946a523285928aa07bf14803178dc04616ac85994Eli Friedman  }
552046a523285928aa07bf14803178dc04616ac85994Eli Friedman  }
55211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
552246a523285928aa07bf14803178dc04616ac85994Eli Friedman  llvm_unreachable("unknown cast resulting in integral value");
5523a25ae3d68d84d2b89907f998df6a396549589da5Anders Carlsson}
55242bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson
5525722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedmanbool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
5526722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman  if (E->getSubExpr()->getType()->isAnyComplexType()) {
5527f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    ComplexValue LV;
5528f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!EvaluateComplex(E->getSubExpr(), LV, Info))
5529f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return false;
5530f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!LV.isComplexInt())
5531f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E);
5532722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman    return Success(LV.getComplexIntReal(), E);
5533722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman  }
5534722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman
5535722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman  return Visit(E->getSubExpr());
5536722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman}
5537722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman
5538664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedmanbool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
5539722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman  if (E->getSubExpr()->getType()->isComplexIntegerType()) {
5540f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    ComplexValue LV;
5541f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!EvaluateComplex(E->getSubExpr(), LV, Info))
5542f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return false;
5543f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!LV.isComplexInt())
5544f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E);
5545722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman    return Success(LV.getComplexIntImag(), E);
5546722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman  }
5547722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman
55488327fad71da34492d82c532f42a58cb4baff81a3Richard Smith  VisitIgnoredValue(E->getSubExpr());
5549664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman  return Success(0, E);
5550664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman}
5551664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman
5552ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregorbool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) {
5553ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor  return Success(E->getPackLength(), E);
5554ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor}
5555ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor
5556295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redlbool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) {
5557295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl  return Success(E->getValue(), E);
5558295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl}
5559295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl
5560f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===//
5561d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman// Float Evaluation
5562d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman//===----------------------------------------------------------------------===//
5563d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman
5564d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmannamespace {
5565770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass FloatExprEvaluator
55668cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  : public ExprEvaluatorBase<FloatExprEvaluator, bool> {
5567d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman  APFloat &Result;
5568d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmanpublic:
5569d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman  FloatExprEvaluator(EvalInfo &info, APFloat &result)
55708cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    : ExprEvaluatorBaseTy(info), Result(result) {}
5571d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman
55721aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  bool Success(const APValue &V, const Expr *e) {
55738cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    Result = V.getFloat();
55748cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    return true;
55758cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  }
5576d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman
557751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  bool ZeroInitialization(const Expr *E) {
5578f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith    Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
5579f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith    return true;
5580f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  }
5581f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith
5582019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  bool VisitCallExpr(const CallExpr *E);
5583d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman
55845db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar  bool VisitUnaryOperator(const UnaryOperator *E);
5585d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman  bool VisitBinaryOperator(const BinaryOperator *E);
5586d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman  bool VisitFloatingLiteral(const FloatingLiteral *E);
55878cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCastExpr(const CastExpr *E);
55882217c87bdc5ab357046a5453bdb06f469c41024eEli Friedman
5589abd3a857ace59100305790545d1baae5877b8945John McCall  bool VisitUnaryReal(const UnaryOperator *E);
5590abd3a857ace59100305790545d1baae5877b8945John McCall  bool VisitUnaryImag(const UnaryOperator *E);
5591ba98d6bb414861965a1f22628494ea046785ecd4Eli Friedman
559251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  // FIXME: Missing: array subscript of vector, member of vector
5593d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman};
5594d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman} // end anonymous namespace
5595d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman
5596d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmanstatic bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
5597c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  assert(E->isRValue() && E->getType()->isRealFloatingType());
55988cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  return FloatExprEvaluator(Info, Result).Visit(E);
5599d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman}
5600d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman
56014ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadstatic bool TryEvaluateBuiltinNaN(const ASTContext &Context,
5602db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall                                  QualType ResultTy,
5603db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall                                  const Expr *Arg,
5604db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall                                  bool SNaN,
5605db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall                                  llvm::APFloat &Result) {
5606db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts());
5607db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  if (!S) return false;
5608db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall
5609db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy);
5610db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall
5611db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  llvm::APInt fill;
5612db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall
5613db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  // Treat empty strings as if they were zero.
5614db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  if (S->getString().empty())
5615db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall    fill = llvm::APInt(32, 0);
5616db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  else if (S->getString().getAsInteger(0, fill))
5617db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall    return false;
5618db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall
5619db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  if (SNaN)
5620db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall    Result = llvm::APFloat::getSNaN(Sem, false, &fill);
5621db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  else
5622db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall    Result = llvm::APFloat::getQNaN(Sem, false, &fill);
5623db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  return true;
5624db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall}
5625db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall
5626019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattnerbool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
5627180f47959a066795cc0f409433023af448bb0328Richard Smith  switch (E->isBuiltinCall()) {
56288cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  default:
56298cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    return ExprEvaluatorBaseTy::VisitCallExpr(E);
56308cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
5631019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  case Builtin::BI__builtin_huge_val:
5632019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  case Builtin::BI__builtin_huge_valf:
5633019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  case Builtin::BI__builtin_huge_vall:
5634019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  case Builtin::BI__builtin_inf:
5635019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  case Builtin::BI__builtin_inff:
56367cbed03c00e246682e5292785d01e1c120ce54bdDaniel Dunbar  case Builtin::BI__builtin_infl: {
56377cbed03c00e246682e5292785d01e1c120ce54bdDaniel Dunbar    const llvm::fltSemantics &Sem =
56387cbed03c00e246682e5292785d01e1c120ce54bdDaniel Dunbar      Info.Ctx.getFloatTypeSemantics(E->getType());
563934a74ab81600a40c6324fd76adb724b803dfaf91Chris Lattner    Result = llvm::APFloat::getInf(Sem);
564034a74ab81600a40c6324fd76adb724b803dfaf91Chris Lattner    return true;
56417cbed03c00e246682e5292785d01e1c120ce54bdDaniel Dunbar  }
56421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5643db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  case Builtin::BI__builtin_nans:
5644db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  case Builtin::BI__builtin_nansf:
5645db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  case Builtin::BI__builtin_nansl:
5646f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
5647f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                               true, Result))
5648f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E);
5649f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return true;
5650db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall
56519e62171a25e3a08fb5c49fb370f83faf5ae786f5Chris Lattner  case Builtin::BI__builtin_nan:
56529e62171a25e3a08fb5c49fb370f83faf5ae786f5Chris Lattner  case Builtin::BI__builtin_nanf:
56539e62171a25e3a08fb5c49fb370f83faf5ae786f5Chris Lattner  case Builtin::BI__builtin_nanl:
56544572baba9d18c275968ac113fd73b0e3c77cccb8Mike Stump    // If this is __builtin_nan() turn this into a nan, otherwise we
56559e62171a25e3a08fb5c49fb370f83faf5ae786f5Chris Lattner    // can't constant fold it.
5656f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
5657f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                               false, Result))
5658f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E);
5659f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return true;
56605db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar
56615db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar  case Builtin::BI__builtin_fabs:
56625db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar  case Builtin::BI__builtin_fabsf:
56635db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar  case Builtin::BI__builtin_fabsl:
56645db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar    if (!EvaluateFloat(E->getArg(0), Result, Info))
56655db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar      return false;
56661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
56675db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar    if (Result.isNegative())
56685db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar      Result.changeSign();
56695db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar    return true;
56705db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar
56711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  case Builtin::BI__builtin_copysign:
56721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  case Builtin::BI__builtin_copysignf:
56735db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar  case Builtin::BI__builtin_copysignl: {
56745db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar    APFloat RHS(0.);
56755db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar    if (!EvaluateFloat(E->getArg(0), Result, Info) ||
56765db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar        !EvaluateFloat(E->getArg(1), RHS, Info))
56775db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar      return false;
56785db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar    Result.copySign(RHS);
56795db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar    return true;
56805db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar  }
5681019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  }
5682019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner}
5683019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner
5684abd3a857ace59100305790545d1baae5877b8945John McCallbool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
568543efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman  if (E->getSubExpr()->getType()->isAnyComplexType()) {
568643efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman    ComplexValue CV;
568743efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman    if (!EvaluateComplex(E->getSubExpr(), CV, Info))
568843efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman      return false;
568943efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman    Result = CV.FloatReal;
569043efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman    return true;
569143efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman  }
569243efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman
569343efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman  return Visit(E->getSubExpr());
5694abd3a857ace59100305790545d1baae5877b8945John McCall}
5695abd3a857ace59100305790545d1baae5877b8945John McCall
5696abd3a857ace59100305790545d1baae5877b8945John McCallbool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
569743efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman  if (E->getSubExpr()->getType()->isAnyComplexType()) {
569843efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman    ComplexValue CV;
569943efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman    if (!EvaluateComplex(E->getSubExpr(), CV, Info))
570043efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman      return false;
570143efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman    Result = CV.FloatImag;
570243efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman    return true;
570343efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman  }
570443efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman
57058327fad71da34492d82c532f42a58cb4baff81a3Richard Smith  VisitIgnoredValue(E->getSubExpr());
570643efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman  const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(E->getType());
570743efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman  Result = llvm::APFloat::getZero(Sem);
5708abd3a857ace59100305790545d1baae5877b8945John McCall  return true;
5709abd3a857ace59100305790545d1baae5877b8945John McCall}
5710abd3a857ace59100305790545d1baae5877b8945John McCall
57115db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbarbool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
57125db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar  switch (E->getOpcode()) {
5713f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  default: return Error(E);
57142de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Plus:
57157993e8a2a26bf408c2a6d45f24fffa12db336b2bRichard Smith    return EvaluateFloat(E->getSubExpr(), Result, Info);
57162de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Minus:
57177993e8a2a26bf408c2a6d45f24fffa12db336b2bRichard Smith    if (!EvaluateFloat(E->getSubExpr(), Result, Info))
57187993e8a2a26bf408c2a6d45f24fffa12db336b2bRichard Smith      return false;
57195db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar    Result.changeSign();
57205db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar    return true;
57215db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar  }
57225db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar}
5723019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner
5724d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmanbool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
5725e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma)
5726e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
572796e93660124c8028a4c3bcc038ab0cdd18cd7ab2Anders Carlsson
57285db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar  APFloat RHS(0.0);
5729745f5147e065900267c85a5568785a1991d4838fRichard Smith  bool LHSOK = EvaluateFloat(E->getLHS(), Result, Info);
5730745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!LHSOK && !Info.keepEvaluatingAfterFailure())
5731d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman    return false;
5732745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!EvaluateFloat(E->getRHS(), RHS, Info) || !LHSOK)
5733d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman    return false;
5734d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman
5735d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman  switch (E->getOpcode()) {
5736f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  default: return Error(E);
57372de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Mul:
5738d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman    Result.multiply(RHS, APFloat::rmNearestTiesToEven);
57397b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    break;
57402de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Add:
5741d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman    Result.add(RHS, APFloat::rmNearestTiesToEven);
57427b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    break;
57432de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Sub:
5744d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman    Result.subtract(RHS, APFloat::rmNearestTiesToEven);
57457b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    break;
57462de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Div:
5747d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman    Result.divide(RHS, APFloat::rmNearestTiesToEven);
57487b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    break;
5749d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman  }
57507b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith
57517b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith  if (Result.isInfinity() || Result.isNaN())
57527b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    CCEDiag(E, diag::note_constexpr_float_arithmetic) << Result.isNaN();
57537b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith  return true;
5754d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman}
5755d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman
5756d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmanbool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
5757d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman  Result = E->getValue();
5758d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman  return true;
5759d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman}
5760d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman
57618cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) {
57628cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  const Expr* SubExpr = E->getSubExpr();
57631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
57642a523eec6a31955be876625819b89e8dc5def707Eli Friedman  switch (E->getCastKind()) {
57652a523eec6a31955be876625819b89e8dc5def707Eli Friedman  default:
5766c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return ExprEvaluatorBaseTy::VisitCastExpr(E);
57672a523eec6a31955be876625819b89e8dc5def707Eli Friedman
57682a523eec6a31955be876625819b89e8dc5def707Eli Friedman  case CK_IntegralToFloating: {
57694efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman    APSInt IntResult;
5770c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return EvaluateInteger(SubExpr, IntResult, Info) &&
5771c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith           HandleIntToFloatCast(Info, E, SubExpr->getType(), IntResult,
5772c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                E->getType(), Result);
57734efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman  }
57742a523eec6a31955be876625819b89e8dc5def707Eli Friedman
57752a523eec6a31955be876625819b89e8dc5def707Eli Friedman  case CK_FloatingCast: {
57764efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman    if (!Visit(SubExpr))
57774efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman      return false;
5778c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return HandleFloatToFloatCast(Info, E, SubExpr->getType(), E->getType(),
5779c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                  Result);
57804efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman  }
5781f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall
57822a523eec6a31955be876625819b89e8dc5def707Eli Friedman  case CK_FloatingComplexToReal: {
5783f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    ComplexValue V;
5784f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    if (!EvaluateComplex(SubExpr, V, Info))
5785f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall      return false;
5786f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    Result = V.getComplexFloatReal();
5787f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    return true;
5788f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall  }
57892a523eec6a31955be876625819b89e8dc5def707Eli Friedman  }
57904efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman}
57914efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
5792d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman//===----------------------------------------------------------------------===//
5793a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar// Complex Evaluation (for float and integer)
57949ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson//===----------------------------------------------------------------------===//
57959ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson
57969ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlssonnamespace {
5797770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass ComplexExprEvaluator
57988cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  : public ExprEvaluatorBase<ComplexExprEvaluator, bool> {
5799f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall  ComplexValue &Result;
58001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
58019ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlssonpublic:
5802f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall  ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result)
58038cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    : ExprEvaluatorBaseTy(info), Result(Result) {}
58041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
58051aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  bool Success(const APValue &V, const Expr *e) {
58068cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    Result.setFrom(V);
58078cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    return true;
58088cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  }
58091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
58107ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  bool ZeroInitialization(const Expr *E);
58117ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman
58128cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  //===--------------------------------------------------------------------===//
58138cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  //                            Visitor Methods
58148cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  //===--------------------------------------------------------------------===//
58159ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson
58168cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitImaginaryLiteral(const ImaginaryLiteral *E);
58178cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCastExpr(const CastExpr *E);
5818b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman  bool VisitBinaryOperator(const BinaryOperator *E);
581996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  bool VisitUnaryOperator(const UnaryOperator *E);
58207ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  bool VisitInitListExpr(const InitListExpr *E);
5821b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman};
5822b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman} // end anonymous namespace
58231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5824b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedmanstatic bool EvaluateComplex(const Expr *E, ComplexValue &Result,
5825b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman                            EvalInfo &Info) {
5826c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  assert(E->isRValue() && E->getType()->isAnyComplexType());
58278cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  return ComplexExprEvaluator(Info, Result).Visit(E);
5828b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman}
5829b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman
58307ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedmanbool ComplexExprEvaluator::ZeroInitialization(const Expr *E) {
5831f6c17a439f3320ac620639a3ee66dbdabb93810cEli Friedman  QualType ElemTy = E->getType()->getAs<ComplexType>()->getElementType();
58327ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  if (ElemTy->isRealFloatingType()) {
58337ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    Result.makeComplexFloat();
58347ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    APFloat Zero = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(ElemTy));
58357ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    Result.FloatReal = Zero;
58367ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    Result.FloatImag = Zero;
58377ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  } else {
58387ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    Result.makeComplexInt();
58397ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    APSInt Zero = Info.Ctx.MakeIntValue(0, ElemTy);
58407ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    Result.IntReal = Zero;
58417ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    Result.IntImag = Zero;
58427ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  }
58437ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  return true;
58447ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman}
58457ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman
58468cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) {
58478cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  const Expr* SubExpr = E->getSubExpr();
5848b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman
5849b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman  if (SubExpr->getType()->isRealFloatingType()) {
5850b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    Result.makeComplexFloat();
5851b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    APFloat &Imag = Result.FloatImag;
5852b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    if (!EvaluateFloat(SubExpr, Imag, Info))
5853b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman      return false;
5854b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman
5855b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    Result.FloatReal = APFloat(Imag.getSemantics());
5856b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    return true;
5857b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman  } else {
5858b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    assert(SubExpr->getType()->isIntegerType() &&
5859b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman           "Unexpected imaginary literal.");
5860b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman
5861b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    Result.makeComplexInt();
5862b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    APSInt &Imag = Result.IntImag;
5863b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    if (!EvaluateInteger(SubExpr, Imag, Info))
5864b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman      return false;
5865b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman
5866b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned());
5867b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    return true;
5868b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman  }
5869b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman}
5870b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman
58718cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) {
5872b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman
58738786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  switch (E->getCastKind()) {
58748786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_BitCast:
58758786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_BaseToDerived:
58768786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_DerivedToBase:
58778786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_UncheckedDerivedToBase:
58788786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_Dynamic:
58798786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_ToUnion:
58808786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_ArrayToPointerDecay:
58818786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_FunctionToPointerDecay:
58828786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_NullToPointer:
58838786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_NullToMemberPointer:
58848786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_BaseToDerivedMemberPointer:
58858786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_DerivedToBaseMemberPointer:
58868786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_MemberPointerToBoolean:
58874d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  case CK_ReinterpretMemberPointer:
58888786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_ConstructorConversion:
58898786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_IntegralToPointer:
58908786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_PointerToIntegral:
58918786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_PointerToBoolean:
58928786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_ToVoid:
58938786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_VectorSplat:
58948786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_IntegralCast:
58958786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_IntegralToBoolean:
58968786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_IntegralToFloating:
58978786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_FloatingToIntegral:
58988786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_FloatingToBoolean:
58998786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_FloatingCast:
59001d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_CPointerToObjCPointerCast:
59011d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_BlockPointerToObjCPointerCast:
59028786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_AnyPointerToBlockPointerCast:
59038786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_ObjCObjectLValueCast:
59048786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_FloatingComplexToReal:
59058786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_FloatingComplexToBoolean:
59068786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_IntegralComplexToReal:
59078786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_IntegralComplexToBoolean:
590833e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCProduceObject:
590933e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCConsumeObject:
591033e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCReclaimReturnedObject:
591133e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCExtendBlockObject:
5912ac1303eca6cbe3e623fb5ec6fe7ec184ef4b0dfaDouglas Gregor  case CK_CopyAndAutoreleaseBlockObject:
59138786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    llvm_unreachable("invalid cast kind for complex value");
59148786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
59158786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_LValueToRValue:
59167a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall  case CK_AtomicToNonAtomic:
59177a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall  case CK_NonAtomicToAtomic:
59188786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_NoOp:
5919c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return ExprEvaluatorBaseTy::VisitCastExpr(E);
59202bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall
59218786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_Dependent:
592246a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_LValueBitCast:
59238786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_UserDefinedConversion:
5924f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
59258786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
59268786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_FloatingRealToComplex: {
5927b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    APFloat &Real = Result.FloatReal;
59288786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    if (!EvaluateFloat(E->getSubExpr(), Real, Info))
5929b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman      return false;
5930b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman
59318786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    Result.makeComplexFloat();
59328786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    Result.FloatImag = APFloat(Real.getSemantics());
59338786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    return true;
59348786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  }
59358786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
59368786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_FloatingComplexCast: {
59378786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    if (!Visit(E->getSubExpr()))
59388786da77984e81d48e0e1b2bd339809b1efc19f3John McCall      return false;
59398786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
59408786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    QualType To = E->getType()->getAs<ComplexType>()->getElementType();
59418786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    QualType From
59428786da77984e81d48e0e1b2bd339809b1efc19f3John McCall      = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType();
59438786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
5944c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return HandleFloatToFloatCast(Info, E, From, To, Result.FloatReal) &&
5945c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith           HandleFloatToFloatCast(Info, E, From, To, Result.FloatImag);
59468786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  }
59478786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
59488786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_FloatingComplexToIntegralComplex: {
59498786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    if (!Visit(E->getSubExpr()))
59508786da77984e81d48e0e1b2bd339809b1efc19f3John McCall      return false;
59518786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
59528786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    QualType To = E->getType()->getAs<ComplexType>()->getElementType();
59538786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    QualType From
59548786da77984e81d48e0e1b2bd339809b1efc19f3John McCall      = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType();
59558786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    Result.makeComplexInt();
5956c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return HandleFloatToIntCast(Info, E, From, Result.FloatReal,
5957c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                To, Result.IntReal) &&
5958c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith           HandleFloatToIntCast(Info, E, From, Result.FloatImag,
5959c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                To, Result.IntImag);
59608786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  }
59618786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
59628786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_IntegralRealToComplex: {
5963b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    APSInt &Real = Result.IntReal;
59648786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    if (!EvaluateInteger(E->getSubExpr(), Real, Info))
5965b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman      return false;
59669ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson
59678786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    Result.makeComplexInt();
59688786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned());
59698786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    return true;
59708786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  }
59718786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
59728786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_IntegralComplexCast: {
59738786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    if (!Visit(E->getSubExpr()))
5974b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman      return false;
5975ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson
59768786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    QualType To = E->getType()->getAs<ComplexType>()->getElementType();
59778786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    QualType From
59788786da77984e81d48e0e1b2bd339809b1efc19f3John McCall      = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType();
59791725f683432715e5afe34d476024bd6f16eac3fcEli Friedman
5980f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith    Result.IntReal = HandleIntToIntCast(Info, E, To, From, Result.IntReal);
5981f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith    Result.IntImag = HandleIntToIntCast(Info, E, To, From, Result.IntImag);
59828786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    return true;
59838786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  }
59848786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
59858786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_IntegralComplexToFloatingComplex: {
59868786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    if (!Visit(E->getSubExpr()))
59878786da77984e81d48e0e1b2bd339809b1efc19f3John McCall      return false;
59888786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
59898786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    QualType To = E->getType()->getAs<ComplexType>()->getElementType();
59908786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    QualType From
59918786da77984e81d48e0e1b2bd339809b1efc19f3John McCall      = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType();
59928786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    Result.makeComplexFloat();
5993c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return HandleIntToFloatCast(Info, E, From, Result.IntReal,
5994c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                To, Result.FloatReal) &&
5995c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith           HandleIntToFloatCast(Info, E, From, Result.IntImag,
5996c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                To, Result.FloatImag);
59978786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  }
5998ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson  }
59991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
60008786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  llvm_unreachable("unknown cast resulting in complex value");
60019ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson}
60029ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson
6003f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCallbool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
6004e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma)
60052ad226bdc847df6b6b6e4f832856478ab63bb3dcRichard Smith    return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
60062ad226bdc847df6b6b6e4f832856478ab63bb3dcRichard Smith
6007745f5147e065900267c85a5568785a1991d4838fRichard Smith  bool LHSOK = Visit(E->getLHS());
6008745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!LHSOK && !Info.keepEvaluatingAfterFailure())
6009f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    return false;
60101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6011f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall  ComplexValue RHS;
6012745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK)
6013f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    return false;
6014a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar
60153f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar  assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
60163f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar         "Invalid operands to binary operator.");
6017ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson  switch (E->getOpcode()) {
6018f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  default: return Error(E);
60192de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Add:
6020a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar    if (Result.isComplexFloat()) {
6021a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar      Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
6022a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar                                       APFloat::rmNearestTiesToEven);
6023a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar      Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
6024a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar                                       APFloat::rmNearestTiesToEven);
6025a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar    } else {
6026a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar      Result.getComplexIntReal() += RHS.getComplexIntReal();
6027a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar      Result.getComplexIntImag() += RHS.getComplexIntImag();
6028a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar    }
60293f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar    break;
60302de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Sub:
6031a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar    if (Result.isComplexFloat()) {
6032a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar      Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
6033a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar                                            APFloat::rmNearestTiesToEven);
6034a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar      Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
6035a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar                                            APFloat::rmNearestTiesToEven);
6036a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar    } else {
6037a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar      Result.getComplexIntReal() -= RHS.getComplexIntReal();
6038a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar      Result.getComplexIntImag() -= RHS.getComplexIntImag();
6039a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar    }
60403f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar    break;
60412de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Mul:
60423f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar    if (Result.isComplexFloat()) {
6043f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall      ComplexValue LHS = Result;
60443f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      APFloat &LHS_r = LHS.getComplexFloatReal();
60453f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      APFloat &LHS_i = LHS.getComplexFloatImag();
60463f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      APFloat &RHS_r = RHS.getComplexFloatReal();
60473f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      APFloat &RHS_i = RHS.getComplexFloatImag();
60481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
60493f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      APFloat Tmp = LHS_r;
60503f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
60513f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Result.getComplexFloatReal() = Tmp;
60523f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Tmp = LHS_i;
60533f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
60543f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
60553f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar
60563f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Tmp = LHS_r;
60573f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
60583f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Result.getComplexFloatImag() = Tmp;
60593f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Tmp = LHS_i;
60603f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
60613f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
60623f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar    } else {
6063f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall      ComplexValue LHS = Result;
60641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result.getComplexIntReal() =
60653f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar        (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
60663f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar         LHS.getComplexIntImag() * RHS.getComplexIntImag());
60671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result.getComplexIntImag() =
60683f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar        (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
60693f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar         LHS.getComplexIntImag() * RHS.getComplexIntReal());
60703f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar    }
60713f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar    break;
607296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  case BO_Div:
607396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    if (Result.isComplexFloat()) {
607496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      ComplexValue LHS = Result;
607596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      APFloat &LHS_r = LHS.getComplexFloatReal();
607696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      APFloat &LHS_i = LHS.getComplexFloatImag();
607796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      APFloat &RHS_r = RHS.getComplexFloatReal();
607896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      APFloat &RHS_i = RHS.getComplexFloatImag();
607996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      APFloat &Res_r = Result.getComplexFloatReal();
608096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      APFloat &Res_i = Result.getComplexFloatImag();
608196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara
608296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      APFloat Den = RHS_r;
608396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Den.multiply(RHS_r, APFloat::rmNearestTiesToEven);
608496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      APFloat Tmp = RHS_i;
608596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
608696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Den.add(Tmp, APFloat::rmNearestTiesToEven);
608796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara
608896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Res_r = LHS_r;
608996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Res_r.multiply(RHS_r, APFloat::rmNearestTiesToEven);
609096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Tmp = LHS_i;
609196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
609296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Res_r.add(Tmp, APFloat::rmNearestTiesToEven);
609396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Res_r.divide(Den, APFloat::rmNearestTiesToEven);
609496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara
609596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Res_i = LHS_i;
609696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Res_i.multiply(RHS_r, APFloat::rmNearestTiesToEven);
609796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Tmp = LHS_r;
609896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
609996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Res_i.subtract(Tmp, APFloat::rmNearestTiesToEven);
610096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Res_i.divide(Den, APFloat::rmNearestTiesToEven);
610196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    } else {
6102f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0)
6103f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(E, diag::note_expr_divide_by_zero);
6104f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
610596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      ComplexValue LHS = Result;
610696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() +
610796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara        RHS.getComplexIntImag() * RHS.getComplexIntImag();
610896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Result.getComplexIntReal() =
610996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara        (LHS.getComplexIntReal() * RHS.getComplexIntReal() +
611096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara         LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den;
611196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Result.getComplexIntImag() =
611296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara        (LHS.getComplexIntImag() * RHS.getComplexIntReal() -
611396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara         LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den;
611496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    }
611596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    break;
6116ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson  }
6117ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson
6118f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall  return true;
6119ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson}
6120ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson
612196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnarabool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
612296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  // Get the operand value into 'Result'.
612396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  if (!Visit(E->getSubExpr()))
612496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    return false;
612596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara
612696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  switch (E->getOpcode()) {
612796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  default:
6128f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
612996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  case UO_Extension:
613096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    return true;
613196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  case UO_Plus:
613296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    // The result is always just the subexpr.
613396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    return true;
613496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  case UO_Minus:
613596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    if (Result.isComplexFloat()) {
613696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Result.getComplexFloatReal().changeSign();
613796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Result.getComplexFloatImag().changeSign();
613896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    }
613996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    else {
614096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Result.getComplexIntReal() = -Result.getComplexIntReal();
614196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Result.getComplexIntImag() = -Result.getComplexIntImag();
614296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    }
614396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    return true;
614496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  case UO_Not:
614596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    if (Result.isComplexFloat())
614696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Result.getComplexFloatImag().changeSign();
614796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    else
614896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Result.getComplexIntImag() = -Result.getComplexIntImag();
614996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    return true;
615096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  }
615196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara}
615296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara
61537ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedmanbool ComplexExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
61547ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  if (E->getNumInits() == 2) {
61557ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    if (E->getType()->isComplexType()) {
61567ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman      Result.makeComplexFloat();
61577ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman      if (!EvaluateFloat(E->getInit(0), Result.FloatReal, Info))
61587ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman        return false;
61597ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman      if (!EvaluateFloat(E->getInit(1), Result.FloatImag, Info))
61607ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman        return false;
61617ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    } else {
61627ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman      Result.makeComplexInt();
61637ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman      if (!EvaluateInteger(E->getInit(0), Result.IntReal, Info))
61647ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman        return false;
61657ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman      if (!EvaluateInteger(E->getInit(1), Result.IntImag, Info))
61667ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman        return false;
61677ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    }
61687ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    return true;
61697ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  }
61707ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  return ExprEvaluatorBaseTy::VisitInitListExpr(E);
61717ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman}
61727ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman
61739ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson//===----------------------------------------------------------------------===//
6174aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith// Void expression evaluation, primarily for a cast to void on the LHS of a
6175aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith// comma operator
6176aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith//===----------------------------------------------------------------------===//
6177aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith
6178aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smithnamespace {
6179aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smithclass VoidExprEvaluator
6180aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith  : public ExprEvaluatorBase<VoidExprEvaluator, bool> {
6181aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smithpublic:
6182aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith  VoidExprEvaluator(EvalInfo &Info) : ExprEvaluatorBaseTy(Info) {}
6183aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith
61841aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  bool Success(const APValue &V, const Expr *e) { return true; }
6185aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith
6186aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith  bool VisitCastExpr(const CastExpr *E) {
6187aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith    switch (E->getCastKind()) {
6188aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith    default:
6189aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith      return ExprEvaluatorBaseTy::VisitCastExpr(E);
6190aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith    case CK_ToVoid:
6191aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith      VisitIgnoredValue(E->getSubExpr());
6192aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith      return true;
6193aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith    }
6194aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith  }
6195aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith};
6196aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith} // end anonymous namespace
6197aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith
6198aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smithstatic bool EvaluateVoid(const Expr *E, EvalInfo &Info) {
6199aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith  assert(E->isRValue() && E->getType()->isVoidType());
6200aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith  return VoidExprEvaluator(Info).Visit(E);
6201aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith}
6202aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith
6203aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith//===----------------------------------------------------------------------===//
620451f4708c00110940ca3f337961915f2ca1668375Richard Smith// Top level Expr::EvaluateAsRValue method.
6205f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===//
6206f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
62071aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smithstatic bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E) {
6208c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  // In C, function designators are not lvalues, but we evaluate them as if they
6209c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  // are.
6210c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  if (E->isGLValue() || E->getType()->isFunctionType()) {
6211c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    LValue LV;
6212c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    if (!EvaluateLValue(E, LV, Info))
6213c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith      return false;
6214c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    LV.moveInto(Result);
6215c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  } else if (E->getType()->isVectorType()) {
62161e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    if (!EvaluateVector(E, Result, Info))
621759b5da6d853b4368b984700315adf7b37de05764Nate Begeman      return false;
6218575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor  } else if (E->getType()->isIntegralOrEnumerationType()) {
62191e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    if (!IntExprEvaluator(Info, Result).Visit(E))
62206dde0d5dc09f45f4d9508c964703e36fef1a0198Anders Carlsson      return false;
6221efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  } else if (E->getType()->hasPointerRepresentation()) {
6222efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    LValue LV;
6223efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    if (!EvaluatePointer(E, LV, Info))
62246dde0d5dc09f45f4d9508c964703e36fef1a0198Anders Carlsson      return false;
62251e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    LV.moveInto(Result);
6226efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  } else if (E->getType()->isRealFloatingType()) {
6227efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    llvm::APFloat F(0.0);
6228efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    if (!EvaluateFloat(E, F, Info))
62296dde0d5dc09f45f4d9508c964703e36fef1a0198Anders Carlsson      return false;
62301aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    Result = APValue(F);
6231efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  } else if (E->getType()->isAnyComplexType()) {
6232efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    ComplexValue C;
6233efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    if (!EvaluateComplex(E, C, Info))
6234660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump      return false;
62351e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    C.moveInto(Result);
623669c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith  } else if (E->getType()->isMemberPointerType()) {
6237e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    MemberPtr P;
6238e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (!EvaluateMemberPointer(E, P, Info))
6239e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return false;
6240e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    P.moveInto(Result);
6241e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
624251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  } else if (E->getType()->isArrayType()) {
6243180f47959a066795cc0f409433023af448bb0328Richard Smith    LValue LV;
624483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    LV.set(E, Info.CurrentCall->Index);
6245180f47959a066795cc0f409433023af448bb0328Richard Smith    if (!EvaluateArray(E, LV, Info.CurrentCall->Temporaries[E], Info))
6246cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith      return false;
6247180f47959a066795cc0f409433023af448bb0328Richard Smith    Result = Info.CurrentCall->Temporaries[E];
624851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  } else if (E->getType()->isRecordType()) {
6249180f47959a066795cc0f409433023af448bb0328Richard Smith    LValue LV;
625083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    LV.set(E, Info.CurrentCall->Index);
6251180f47959a066795cc0f409433023af448bb0328Richard Smith    if (!EvaluateRecord(E, LV, Info.CurrentCall->Temporaries[E], Info))
6252180f47959a066795cc0f409433023af448bb0328Richard Smith      return false;
6253180f47959a066795cc0f409433023af448bb0328Richard Smith    Result = Info.CurrentCall->Temporaries[E];
6254aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith  } else if (E->getType()->isVoidType()) {
6255c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    if (Info.getLangOpts().CPlusPlus0x)
62565cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      Info.CCEDiag(E, diag::note_constexpr_nonliteral)
6257c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith        << E->getType();
6258c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    else
62595cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr);
6260aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith    if (!EvaluateVoid(E, Info))
6261aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith      return false;
6262c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  } else if (Info.getLangOpts().CPlusPlus0x) {
62635cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.Diag(E, diag::note_constexpr_nonliteral) << E->getType();
6264c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return false;
6265f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  } else {
62665cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.Diag(E, diag::note_invalid_subexpr_in_const_expr);
6267660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump    return false;
6268f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
6269660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump
6270660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump  return true;
6271660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump}
6272660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump
627383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith/// EvaluateInPlace - Evaluate an expression in-place in an APValue. In some
627483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith/// cases, the in-place evaluation is essential, since later initializers for
627583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith/// an object can indirectly refer to subobjects which were initialized earlier.
627683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smithstatic bool EvaluateInPlace(APValue &Result, EvalInfo &Info, const LValue &This,
627783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                            const Expr *E, CheckConstantExpressionKind CCEK,
627883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                            bool AllowNonLiteralTypes) {
62797ca4850a3e3530fa6c93b64b740446e32c97f992Richard Smith  if (!AllowNonLiteralTypes && !CheckLiteralType(Info, E))
628051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return false;
628151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
628251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (E->isRValue()) {
628369c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith    // Evaluate arrays and record types in-place, so that later initializers can
628469c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith    // refer to earlier-initialized members of the object.
6285180f47959a066795cc0f409433023af448bb0328Richard Smith    if (E->getType()->isArrayType())
6286180f47959a066795cc0f409433023af448bb0328Richard Smith      return EvaluateArray(E, This, Result, Info);
6287180f47959a066795cc0f409433023af448bb0328Richard Smith    else if (E->getType()->isRecordType())
6288180f47959a066795cc0f409433023af448bb0328Richard Smith      return EvaluateRecord(E, This, Result, Info);
628969c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith  }
629069c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith
629169c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith  // For any other type, in-place evaluation is unimportant.
62921aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  return Evaluate(Result, Info, E);
629369c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith}
629469c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith
6295f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// EvaluateAsRValue - Try to evaluate this expression, performing an implicit
6296f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// lvalue-to-rvalue cast if it is an lvalue.
6297f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) {
629851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (!CheckLiteralType(Info, E))
629951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return false;
630051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
63011aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  if (!::Evaluate(Result, Info, E))
6302f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return false;
6303f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
6304f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (E->isGLValue()) {
6305f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    LValue LV;
63061aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    LV.setFrom(Info.Ctx, Result);
63071aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    if (!HandleLValueToRValueConversion(Info, E, E->getType(), LV, Result))
6308f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return false;
6309f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
6310f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
63111aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  // Check this core constant expression is a constant expression.
631283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  return CheckConstantExpression(Info, E->getExprLoc(), E->getType(), Result);
6313f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith}
6314c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
631551f4708c00110940ca3f337961915f2ca1668375Richard Smith/// EvaluateAsRValue - Return true if this is a constant which we can fold using
631656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall/// any crazy technique (that has nothing to do with language standards) that
631756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall/// we want to.  If this function returns true, it returns the folded constant
6318c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// in Result. If this expression is a glvalue, an lvalue-to-rvalue conversion
6319c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// will be applied to the result.
632051f4708c00110940ca3f337961915f2ca1668375Richard Smithbool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx) const {
6321ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith  // Fast-path evaluations of integer literals, since we sometimes see files
6322ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith  // containing vast quantities of these.
6323ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith  if (const IntegerLiteral *L = dyn_cast<IntegerLiteral>(this)) {
6324ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith    Result.Val = APValue(APSInt(L->getValue(),
6325ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith                                L->getType()->isUnsignedIntegerType()));
6326ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith    return true;
6327ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith  }
6328ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith
63292d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  // FIXME: Evaluating values of large array and record types can cause
63302d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  // performance problems. Only do so in C++11 for now.
6331e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) &&
63324e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      !Ctx.getLangOpts().CPlusPlus0x)
63331445bbacf4c8de5f208ff4ccb302424a4d9e233eRichard Smith    return false;
63341445bbacf4c8de5f208ff4ccb302424a4d9e233eRichard Smith
6335f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  EvalInfo Info(Ctx, Result);
6336f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  return ::EvaluateAsRValue(Info, this, Result.Val);
633756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall}
633856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
63394ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool Expr::EvaluateAsBooleanCondition(bool &Result,
63404ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                      const ASTContext &Ctx) const {
6341c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  EvalResult Scratch;
634251f4708c00110940ca3f337961915f2ca1668375Richard Smith  return EvaluateAsRValue(Scratch, Ctx) &&
63431aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith         HandleConversionToBool(Scratch.Val, Result);
6344cd7a445c6b46c5585580dfb652300c8483c0cb6bJohn McCall}
6345cd7a445c6b46c5585580dfb652300c8483c0cb6bJohn McCall
634680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smithbool Expr::EvaluateAsInt(APSInt &Result, const ASTContext &Ctx,
634780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith                         SideEffectsKind AllowSideEffects) const {
634880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  if (!getType()->isIntegralOrEnumerationType())
634980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    return false;
635080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith
6351c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  EvalResult ExprResult;
635280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  if (!EvaluateAsRValue(ExprResult, Ctx) || !ExprResult.Val.isInt() ||
635380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith      (!AllowSideEffects && ExprResult.HasSideEffects))
6354c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return false;
6355f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
6356c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  Result = ExprResult.Val.getInt();
6357c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  return true;
6358a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith}
6359a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith
63604ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx) const {
63611b78276a75a5a0f496a82429c1ff9604d622a76dAnders Carlsson  EvalInfo Info(Ctx, Result);
63621b78276a75a5a0f496a82429c1ff9604d622a76dAnders Carlsson
6363efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  LValue LV;
636483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  if (!EvaluateLValue(this, LV, Info) || Result.HasSideEffects ||
636583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      !CheckLValueConstantExpression(Info, getExprLoc(),
636683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                                     Ctx.getLValueReferenceType(getType()), LV))
636783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return false;
636883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith
63691aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  LV.moveInto(Result.Val);
637083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  return true;
6371b2f295c8050fb8c141bf2cf38eed0a56e99d0092Eli Friedman}
6372b2f295c8050fb8c141bf2cf38eed0a56e99d0092Eli Friedman
6373099e7f647ccda915513f2b2ec53352dc756082d3Richard Smithbool Expr::EvaluateAsInitializer(APValue &Value, const ASTContext &Ctx,
6374099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith                                 const VarDecl *VD,
6375099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith                      llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
63762d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  // FIXME: Evaluating initializers for large array and record types can cause
63772d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  // performance problems. Only do so in C++11 for now.
63782d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) &&
63794e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      !Ctx.getLangOpts().CPlusPlus0x)
63802d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith    return false;
63812d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith
6382099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  Expr::EvalStatus EStatus;
6383099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  EStatus.Diag = &Notes;
6384099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
6385099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  EvalInfo InitInfo(Ctx, EStatus);
6386099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  InitInfo.setEvaluatingDecl(VD, Value);
6387099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
6388099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  LValue LVal;
6389099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  LVal.set(VD);
6390099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
639151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  // C++11 [basic.start.init]p2:
639251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  //  Variables with static storage duration or thread storage duration shall be
639351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  //  zero-initialized before any other initialization takes place.
639451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  // This behavior is not present in C.
63954e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (Ctx.getLangOpts().CPlusPlus && !VD->hasLocalStorage() &&
639651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      !VD->getType()->isReferenceType()) {
639751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    ImplicitValueInitExpr VIE(VD->getType());
639883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    if (!EvaluateInPlace(Value, InitInfo, LVal, &VIE, CCEK_Constant,
639983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                         /*AllowNonLiteralTypes=*/true))
640051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      return false;
640151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  }
640251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
640383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  if (!EvaluateInPlace(Value, InitInfo, LVal, this, CCEK_Constant,
640483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                         /*AllowNonLiteralTypes=*/true) ||
640583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      EStatus.HasSideEffects)
640683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return false;
640783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith
640883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  return CheckConstantExpression(InitInfo, VD->getLocation(), VD->getType(),
640983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                                 Value);
6410099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith}
6411099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
641251f4708c00110940ca3f337961915f2ca1668375Richard Smith/// isEvaluatable - Call EvaluateAsRValue to see if this expression can be
641351f4708c00110940ca3f337961915f2ca1668375Richard Smith/// constant folded, but discard the result.
64144ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool Expr::isEvaluatable(const ASTContext &Ctx) const {
64154fdfb0965b396f2778091f7e6c051d17ff9791baAnders Carlsson  EvalResult Result;
641651f4708c00110940ca3f337961915f2ca1668375Richard Smith  return EvaluateAsRValue(Result, Ctx) && !Result.HasSideEffects;
641745b6b9d080ac56917337d73d8f1cd6374b27b05dChris Lattner}
641851fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson
64194ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool Expr::HasSideEffects(const ASTContext &Ctx) const {
64201e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith  return HasSideEffect(Ctx).Visit(this);
6421393c247fe025ccb5f914e37e948192ea86faef8cFariborz Jahanian}
6422393c247fe025ccb5f914e37e948192ea86faef8cFariborz Jahanian
6423a6b8b2c09610b8bc4330e948ece8b940c2386406Richard SmithAPSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx) const {
64241c0cfd4599e816cfd7a8f348286bf0ad79652ffcAnders Carlsson  EvalResult EvalResult;
642551f4708c00110940ca3f337961915f2ca1668375Richard Smith  bool Result = EvaluateAsRValue(EvalResult, Ctx);
6426c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin  (void)Result;
642751fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson  assert(Result && "Could not evaluate expression");
64281c0cfd4599e816cfd7a8f348286bf0ad79652ffcAnders Carlsson  assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
642951fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson
64301c0cfd4599e816cfd7a8f348286bf0ad79652ffcAnders Carlsson  return EvalResult.Val.getInt();
643151fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson}
6432d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6433e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara bool Expr::EvalResult::isGlobalLValue() const {
6434e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara   assert(Val.isLValue());
6435e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara   return IsGlobalLValue(Val.getLValueBase());
6436e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara }
6437e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara
6438e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara
6439d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// isIntegerConstantExpr - this recursive routine will test if an expression is
6440d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// an integer constant expression.
6441d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6442d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
6443d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// comma, etc
6444d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall///
6445d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// FIXME: Handle offsetof.  Two things to do:  Handle GCC's __builtin_offsetof
6446d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// to support gcc 4.0+  and handle the idiom GCC recognizes with a null pointer
6447d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// cast+dereference.
6448d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6449d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// CheckICE - This function does the fundamental ICE checking: the returned
6450d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
6451d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// Note that to reduce code duplication, this helper does no evaluation
6452d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// itself; the caller checks whether the expression is evaluatable, and
6453d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// in the rare cases where CheckICE actually cares about the evaluated
6454d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// value, it calls into Evalute.
6455d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall//
6456d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// Meanings of Val:
645751f4708c00110940ca3f337961915f2ca1668375Richard Smith// 0: This expression is an ICE.
6458d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// 1: This expression is not an ICE, but if it isn't evaluated, it's
6459d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall//    a legal subexpression for an ICE. This return value is used to handle
6460d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall//    the comma operator in C99 mode.
6461d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// 2: This expression is not an ICE, and is not a legal subexpression for one.
6462d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
64633c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohmannamespace {
64643c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman
6465d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCallstruct ICEDiag {
6466d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  unsigned Val;
6467d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  SourceLocation Loc;
6468d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6469d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  public:
6470d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
6471d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  ICEDiag() : Val(0) {}
6472d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall};
6473d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
64743c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman}
64753c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman
64763c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohmanstatic ICEDiag NoDiag() { return ICEDiag(); }
6477d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6478d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCallstatic ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
6479d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  Expr::EvalResult EVResult;
648051f4708c00110940ca3f337961915f2ca1668375Richard Smith  if (!E->EvaluateAsRValue(EVResult, Ctx) || EVResult.HasSideEffects ||
6481d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      !EVResult.Val.isInt()) {
6482d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return ICEDiag(2, E->getLocStart());
6483d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6484d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  return NoDiag();
6485d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall}
6486d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6487d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCallstatic ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
6488d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  assert(!E->isValueDependent() && "Should not see value dependent exprs!");
64892ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor  if (!E->getType()->isIntegralOrEnumerationType()) {
6490d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return ICEDiag(2, E->getLocStart());
6491d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6492d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6493d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  switch (E->getStmtClass()) {
649463c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall#define ABSTRACT_STMT(Node)
6495d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall#define STMT(Node, Base) case Expr::Node##Class:
6496d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall#define EXPR(Node, Base)
6497d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall#include "clang/AST/StmtNodes.inc"
6498d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::PredefinedExprClass:
6499d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::FloatingLiteralClass:
6500d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ImaginaryLiteralClass:
6501d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::StringLiteralClass:
6502d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ArraySubscriptExprClass:
6503d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::MemberExprClass:
6504d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CompoundAssignOperatorClass:
6505d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CompoundLiteralExprClass:
6506d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ExtVectorElementExprClass:
6507d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::DesignatedInitExprClass:
6508d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ImplicitValueInitExprClass:
6509d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ParenListExprClass:
6510d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::VAArgExprClass:
6511d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::AddrLabelExprClass:
6512d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::StmtExprClass:
6513d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXMemberCallExprClass:
6514e08ce650a2b02410eddd1f60a4aa6b3d4be71e73Peter Collingbourne  case Expr::CUDAKernelCallExprClass:
6515d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXDynamicCastExprClass:
6516d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXTypeidExprClass:
65179be88403e965cc49af76c9d33d818781d44b333eFrancois Pichet  case Expr::CXXUuidofExprClass:
6518d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXNullPtrLiteralExprClass:
65199fcce65e7e1307b5b8da9be13e4092d6bb94dc1dRichard Smith  case Expr::UserDefinedLiteralClass:
6520d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXThisExprClass:
6521d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXThrowExprClass:
6522d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXNewExprClass:
6523d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXDeleteExprClass:
6524d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXPseudoDestructorExprClass:
6525d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::UnresolvedLookupExprClass:
6526d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::DependentScopeDeclRefExprClass:
6527d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXConstructExprClass:
6528d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXBindTemporaryExprClass:
65294765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  case Expr::ExprWithCleanupsClass:
6530d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXTemporaryObjectExprClass:
6531d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXUnresolvedConstructExprClass:
6532d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXDependentScopeMemberExprClass:
6533d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::UnresolvedMemberExprClass:
6534d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ObjCStringLiteralClass:
6535eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  case Expr::ObjCBoxedExprClass:
6536ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  case Expr::ObjCArrayLiteralClass:
6537ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  case Expr::ObjCDictionaryLiteralClass:
6538d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ObjCEncodeExprClass:
6539d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ObjCMessageExprClass:
6540d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ObjCSelectorExprClass:
6541d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ObjCProtocolExprClass:
6542d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ObjCIvarRefExprClass:
6543d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ObjCPropertyRefExprClass:
6544ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  case Expr::ObjCSubscriptRefExprClass:
6545d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ObjCIsaExprClass:
6546d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ShuffleVectorExprClass:
6547d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::BlockExprClass:
6548d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::NoStmtClass:
65497cd7d1ad33fdf49eef83942e8855fe20d95aa1b9John McCall  case Expr::OpaqueValueExprClass:
6550be230c36e32142cbdcdbe9c97511d097beeecbabDouglas Gregor  case Expr::PackExpansionExprClass:
6551c7793c73ba8a343de3f2552d984851985a46f159Douglas Gregor  case Expr::SubstNonTypeTemplateParmPackExprClass:
655261eee0ca33b29e102f11bab77c8b74cc00e2392bTanya Lattner  case Expr::AsTypeExprClass:
6553f85e193739c953358c865005855253af4f68a497John McCall  case Expr::ObjCIndirectCopyRestoreExprClass:
655403e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  case Expr::MaterializeTemporaryExprClass:
65554b9c2d235fb9449e249d74f48ecfec601650de93John McCall  case Expr::PseudoObjectExprClass:
6556276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman  case Expr::AtomicExprClass:
6557cea8d966f826554f0679595e9371e314e8dbc1cfSebastian Redl  case Expr::InitListExprClass:
655801d08018b7cf5ce1601707cfd7a84d22015fc04eDouglas Gregor  case Expr::LambdaExprClass:
6559cea8d966f826554f0679595e9371e314e8dbc1cfSebastian Redl    return ICEDiag(2, E->getLocStart());
6560cea8d966f826554f0679595e9371e314e8dbc1cfSebastian Redl
6561ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor  case Expr::SizeOfPackExprClass:
6562d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::GNUNullExprClass:
6563d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    // GCC considers the GNU __null value to be an integral constant expression.
6564d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return NoDiag();
6565d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
656691a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall  case Expr::SubstNonTypeTemplateParmExprClass:
656791a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall    return
656891a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall      CheckICE(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(), Ctx);
656991a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall
6570d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ParenExprClass:
6571d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
6572f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  case Expr::GenericSelectionExprClass:
6573f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return CheckICE(cast<GenericSelectionExpr>(E)->getResultExpr(), Ctx);
6574d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::IntegerLiteralClass:
6575d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CharacterLiteralClass:
6576ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  case Expr::ObjCBoolLiteralExprClass:
6577d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXBoolLiteralExprClass:
6578ed8abf18329df67b0abcbb3a10458bd8c1d2a595Douglas Gregor  case Expr::CXXScalarValueInitExprClass:
6579d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::UnaryTypeTraitExprClass:
65806ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  case Expr::BinaryTypeTraitExprClass:
65814ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  case Expr::TypeTraitExprClass:
658221ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  case Expr::ArrayTypeTraitExprClass:
6583552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  case Expr::ExpressionTraitExprClass:
65842e156225a29407a50dd19041aa5750171ad44ea3Sebastian Redl  case Expr::CXXNoexceptExprClass:
6585d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return NoDiag();
6586d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CallExprClass:
65876cf750298d3621d8a10a6dd07fcee8e274b9d94dSean Hunt  case Expr::CXXOperatorCallExprClass: {
658805830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith    // C99 6.6/3 allows function calls within unevaluated subexpressions of
658905830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith    // constant expressions, but they can never be ICEs because an ICE cannot
659005830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith    // contain an operand of (pointer to) function type.
6591d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    const CallExpr *CE = cast<CallExpr>(E);
6592180f47959a066795cc0f409433023af448bb0328Richard Smith    if (CE->isBuiltinCall())
6593d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return CheckEvalInICE(E, Ctx);
6594d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return ICEDiag(2, E->getLocStart());
6595d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6596359c89df5479810c9d4784fc0b6ab592eb136777Richard Smith  case Expr::DeclRefExprClass: {
6597d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
6598d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return NoDiag();
6599359c89df5479810c9d4784fc0b6ab592eb136777Richard Smith    const ValueDecl *D = dyn_cast<ValueDecl>(cast<DeclRefExpr>(E)->getDecl());
66004e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (Ctx.getLangOpts().CPlusPlus &&
6601359c89df5479810c9d4784fc0b6ab592eb136777Richard Smith        D && IsConstNonVolatile(D->getType())) {
6602d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      // Parameter variables are never constants.  Without this check,
6603d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      // getAnyInitializer() can find a default argument, which leads
6604d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      // to chaos.
6605d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      if (isa<ParmVarDecl>(D))
6606d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
6607d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6608d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      // C++ 7.1.5.1p2
6609d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      //   A variable of non-volatile const-qualified integral or enumeration
6610d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      //   type initialized by an ICE can be used in ICEs.
6611d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) {
6612db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith        if (!Dcl->getType()->isIntegralOrEnumerationType())
6613db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith          return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
6614db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith
6615099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith        const VarDecl *VD;
6616099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith        // Look for a declaration of this variable that has an initializer, and
6617099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith        // check whether it is an ICE.
6618099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith        if (Dcl->getAnyInitializer(VD) && VD->checkInitIsICE())
6619099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith          return NoDiag();
6620099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith        else
6621099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith          return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
6622d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      }
6623d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    }
6624d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return ICEDiag(2, E->getLocStart());
6625359c89df5479810c9d4784fc0b6ab592eb136777Richard Smith  }
6626d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::UnaryOperatorClass: {
6627d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    const UnaryOperator *Exp = cast<UnaryOperator>(E);
6628d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    switch (Exp->getOpcode()) {
66292de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PostInc:
66302de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PostDec:
66312de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PreInc:
66322de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PreDec:
66332de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_AddrOf:
66342de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Deref:
663505830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith      // C99 6.6/3 allows increment and decrement within unevaluated
663605830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith      // subexpressions of constant expressions, but they can never be ICEs
663705830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith      // because an ICE cannot contain an lvalue operand.
6638d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return ICEDiag(2, E->getLocStart());
66392de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Extension:
66402de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_LNot:
66412de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Plus:
66422de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Minus:
66432de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Not:
66442de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Real:
66452de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Imag:
6646d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return CheckICE(Exp->getSubExpr(), Ctx);
6647d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    }
6648d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6649d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    // OffsetOf falls through here.
6650d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6651d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::OffsetOfExprClass: {
6652d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      // Note that per C99, offsetof must be an ICE. And AFAIK, using
665351f4708c00110940ca3f337961915f2ca1668375Richard Smith      // EvaluateAsRValue matches the proposed gcc behavior for cases like
665405830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith      // "offsetof(struct s{int x[4];}, x[1.0])".  This doesn't affect
6655d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      // compliance: we should warn earlier for offsetof expressions with
6656d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      // array subscripts that aren't ICEs, and if the array subscripts
6657d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      // are ICEs, the value of the offsetof must be an integer constant.
6658d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return CheckEvalInICE(E, Ctx);
6659d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6660f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  case Expr::UnaryExprOrTypeTraitExprClass: {
6661f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E);
6662f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    if ((Exp->getKind() ==  UETT_SizeOf) &&
6663f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne        Exp->getTypeOfArgument()->isVariableArrayType())
6664d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return ICEDiag(2, E->getLocStart());
6665d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return NoDiag();
6666d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6667d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::BinaryOperatorClass: {
6668d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    const BinaryOperator *Exp = cast<BinaryOperator>(E);
6669d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    switch (Exp->getOpcode()) {
66702de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_PtrMemD:
66712de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_PtrMemI:
66722de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Assign:
66732de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_MulAssign:
66742de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_DivAssign:
66752de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_RemAssign:
66762de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_AddAssign:
66772de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_SubAssign:
66782de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_ShlAssign:
66792de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_ShrAssign:
66802de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_AndAssign:
66812de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_XorAssign:
66822de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_OrAssign:
668305830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith      // C99 6.6/3 allows assignments within unevaluated subexpressions of
668405830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith      // constant expressions, but they can never be ICEs because an ICE cannot
668505830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith      // contain an lvalue operand.
6686d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return ICEDiag(2, E->getLocStart());
6687d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
66882de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Mul:
66892de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Div:
66902de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Rem:
66912de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Add:
66922de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Sub:
66932de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Shl:
66942de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Shr:
66952de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LT:
66962de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_GT:
66972de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LE:
66982de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_GE:
66992de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_EQ:
67002de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_NE:
67012de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_And:
67022de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Xor:
67032de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Or:
67042de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Comma: {
6705d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
6706d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
67072de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      if (Exp->getOpcode() == BO_Div ||
67082de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall          Exp->getOpcode() == BO_Rem) {
670951f4708c00110940ca3f337961915f2ca1668375Richard Smith        // EvaluateAsRValue gives an error for undefined Div/Rem, so make sure
6710d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        // we don't evaluate one.
67113b332ab132fa85c83833d74d400f6e126f52fbd2John McCall        if (LHSResult.Val == 0 && RHSResult.Val == 0) {
6712a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx);
6713d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall          if (REval == 0)
6714d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall            return ICEDiag(1, E->getLocStart());
6715d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall          if (REval.isSigned() && REval.isAllOnesValue()) {
6716a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith            llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx);
6717d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall            if (LEval.isMinSignedValue())
6718d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall              return ICEDiag(1, E->getLocStart());
6719d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall          }
6720d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        }
6721d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      }
67222de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      if (Exp->getOpcode() == BO_Comma) {
67234e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie        if (Ctx.getLangOpts().C99) {
6724d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall          // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
6725d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall          // if it isn't evaluated.
6726d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall          if (LHSResult.Val == 0 && RHSResult.Val == 0)
6727d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall            return ICEDiag(1, E->getLocStart());
6728d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        } else {
6729d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall          // In both C89 and C++, commas in ICEs are illegal.
6730d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall          return ICEDiag(2, E->getLocStart());
6731d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        }
6732d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      }
6733d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      if (LHSResult.Val >= RHSResult.Val)
6734d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        return LHSResult;
6735d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return RHSResult;
6736d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    }
67372de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LAnd:
67382de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LOr: {
6739d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
6740d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
6741d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      if (LHSResult.Val == 0 && RHSResult.Val == 1) {
6742d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        // Rare case where the RHS has a comma "side-effect"; we need
6743d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        // to actually check the condition to see whether the side
6744d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        // with the comma is evaluated.
67452de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall        if ((Exp->getOpcode() == BO_LAnd) !=
6746a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith            (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0))
6747d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall          return RHSResult;
6748d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        return NoDiag();
6749d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      }
6750d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6751d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      if (LHSResult.Val >= RHSResult.Val)
6752d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        return LHSResult;
6753d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return RHSResult;
6754d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    }
6755d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    }
6756d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6757d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ImplicitCastExprClass:
6758d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CStyleCastExprClass:
6759d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXFunctionalCastExprClass:
6760d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXStaticCastExprClass:
6761d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXReinterpretCastExprClass:
676232cb47174304bc7ec11478b9497c4e10f48273d9Richard Smith  case Expr::CXXConstCastExprClass:
6763f85e193739c953358c865005855253af4f68a497John McCall  case Expr::ObjCBridgedCastExprClass: {
6764d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
67652116b144cf07f2574d20517187eb8863645376ebRichard Smith    if (isa<ExplicitCastExpr>(E)) {
67662116b144cf07f2574d20517187eb8863645376ebRichard Smith      if (const FloatingLiteral *FL
67672116b144cf07f2574d20517187eb8863645376ebRichard Smith            = dyn_cast<FloatingLiteral>(SubExpr->IgnoreParenImpCasts())) {
67682116b144cf07f2574d20517187eb8863645376ebRichard Smith        unsigned DestWidth = Ctx.getIntWidth(E->getType());
67692116b144cf07f2574d20517187eb8863645376ebRichard Smith        bool DestSigned = E->getType()->isSignedIntegerOrEnumerationType();
67702116b144cf07f2574d20517187eb8863645376ebRichard Smith        APSInt IgnoredVal(DestWidth, !DestSigned);
67712116b144cf07f2574d20517187eb8863645376ebRichard Smith        bool Ignored;
67722116b144cf07f2574d20517187eb8863645376ebRichard Smith        // If the value does not fit in the destination type, the behavior is
67732116b144cf07f2574d20517187eb8863645376ebRichard Smith        // undefined, so we are not required to treat it as a constant
67742116b144cf07f2574d20517187eb8863645376ebRichard Smith        // expression.
67752116b144cf07f2574d20517187eb8863645376ebRichard Smith        if (FL->getValue().convertToInteger(IgnoredVal,
67762116b144cf07f2574d20517187eb8863645376ebRichard Smith                                            llvm::APFloat::rmTowardZero,
67772116b144cf07f2574d20517187eb8863645376ebRichard Smith                                            &Ignored) & APFloat::opInvalidOp)
67782116b144cf07f2574d20517187eb8863645376ebRichard Smith          return ICEDiag(2, E->getLocStart());
67792116b144cf07f2574d20517187eb8863645376ebRichard Smith        return NoDiag();
67802116b144cf07f2574d20517187eb8863645376ebRichard Smith      }
67812116b144cf07f2574d20517187eb8863645376ebRichard Smith    }
6782eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman    switch (cast<CastExpr>(E)->getCastKind()) {
6783eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman    case CK_LValueToRValue:
67847a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall    case CK_AtomicToNonAtomic:
67857a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall    case CK_NonAtomicToAtomic:
6786eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman    case CK_NoOp:
6787eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman    case CK_IntegralToBoolean:
6788eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman    case CK_IntegralCast:
6789d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return CheckICE(SubExpr, Ctx);
6790eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman    default:
6791eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman      return ICEDiag(2, E->getLocStart());
6792eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman    }
6793d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
679456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  case Expr::BinaryConditionalOperatorClass: {
679556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E);
679656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    ICEDiag CommonResult = CheckICE(Exp->getCommon(), Ctx);
679756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    if (CommonResult.Val == 2) return CommonResult;
679856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
679956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    if (FalseResult.Val == 2) return FalseResult;
680056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    if (CommonResult.Val == 1) return CommonResult;
680156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    if (FalseResult.Val == 1 &&
6802a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith        Exp->getCommon()->EvaluateKnownConstInt(Ctx) == 0) return NoDiag();
680356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    return FalseResult;
680456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  }
6805d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ConditionalOperatorClass: {
6806d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
6807d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    // If the condition (ignoring parens) is a __builtin_constant_p call,
6808d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    // then only the true side is actually considered in an integer constant
6809d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    // expression, and it is fully evaluated.  This is an important GNU
6810d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    // extension.  See GCC PR38377 for discussion.
6811d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    if (const CallExpr *CallCE
6812d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
681380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith      if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p)
681480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith        return CheckEvalInICE(E, Ctx);
6815d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
6816d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    if (CondResult.Val == 2)
6817d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return CondResult;
681863fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor
6819f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
6820f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
682163fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor
6822d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    if (TrueResult.Val == 2)
6823d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return TrueResult;
6824d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    if (FalseResult.Val == 2)
6825d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return FalseResult;
6826d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    if (CondResult.Val == 1)
6827d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return CondResult;
6828d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    if (TrueResult.Val == 0 && FalseResult.Val == 0)
6829d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return NoDiag();
6830d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    // Rare case where the diagnostics depend on which side is evaluated
6831d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    // Note that if we get here, CondResult is 0, and at least one of
6832d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    // TrueResult and FalseResult is non-zero.
6833a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith    if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0) {
6834d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return FalseResult;
6835d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    }
6836d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return TrueResult;
6837d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6838d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXDefaultArgExprClass:
6839d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
6840d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ChooseExprClass: {
6841d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
6842d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6843d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6844d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
68453026348bd4c13a0f83b59839f64065e0fcbea253David Blaikie  llvm_unreachable("Invalid StmtClass!");
6846d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall}
6847d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6848f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// Evaluate an expression as a C++11 integral constant expression.
6849f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool EvaluateCPlusPlus11IntegralConstantExpr(ASTContext &Ctx,
6850f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                                                    const Expr *E,
6851f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                                                    llvm::APSInt *Value,
6852f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                                                    SourceLocation *Loc) {
6853f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (!E->getType()->isIntegralOrEnumerationType()) {
6854f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (Loc) *Loc = E->getExprLoc();
6855f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return false;
6856f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
6857f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
68584c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  APValue Result;
68594c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  if (!E->isCXX11ConstantExpr(Ctx, &Result, Loc))
6860dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    return false;
6861dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith
68624c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  assert(Result.isInt() && "pointer cast to int is not an ICE");
68634c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  if (Value) *Value = Result.getInt();
6864dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith  return true;
6865f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith}
6866f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
6867dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smithbool Expr::isIntegerConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
68684e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (Ctx.getLangOpts().CPlusPlus0x)
6869f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, 0, Loc);
6870f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
6871d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  ICEDiag d = CheckICE(this, Ctx);
6872d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  if (d.Val != 0) {
6873d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    if (Loc) *Loc = d.Loc;
6874d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return false;
6875d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6876f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  return true;
6877f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith}
6878f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
6879f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithbool Expr::isIntegerConstantExpr(llvm::APSInt &Value, ASTContext &Ctx,
6880f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                                 SourceLocation *Loc, bool isEvaluated) const {
68814e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (Ctx.getLangOpts().CPlusPlus0x)
6882f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, &Value, Loc);
6883f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
6884f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (!isIntegerConstantExpr(Ctx, Loc))
6885f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return false;
6886f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (!EvaluateAsInt(Value, Ctx))
6887d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    llvm_unreachable("ICE cannot be evaluated!");
6888d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  return true;
6889d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall}
68904c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith
689170488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smithbool Expr::isCXX98IntegralConstantExpr(ASTContext &Ctx) const {
689270488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smith  return CheckICE(this, Ctx).Val == 0;
689370488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smith}
689470488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smith
68954c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smithbool Expr::isCXX11ConstantExpr(ASTContext &Ctx, APValue *Result,
68964c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith                               SourceLocation *Loc) const {
68974c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  // We support this checking in C++98 mode in order to diagnose compatibility
68984c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  // issues.
68994e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  assert(Ctx.getLangOpts().CPlusPlus);
69004c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith
690170488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smith  // Build evaluation settings.
69024c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  Expr::EvalStatus Status;
69034c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  llvm::SmallVector<PartialDiagnosticAt, 8> Diags;
69044c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  Status.Diag = &Diags;
69054c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  EvalInfo Info(Ctx, Status);
69064c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith
69074c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  APValue Scratch;
69084c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  bool IsConstExpr = ::EvaluateAsRValue(Info, this, Result ? *Result : Scratch);
69094c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith
69104c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  if (!Diags.empty()) {
69114c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    IsConstExpr = false;
69124c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    if (Loc) *Loc = Diags[0].first;
69134c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  } else if (!IsConstExpr) {
69144c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    // FIXME: This shouldn't happen.
69154c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    if (Loc) *Loc = getExprLoc();
69164c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  }
69174c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith
69184c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  return IsConstExpr;
69194c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith}
6920745f5147e065900267c85a5568785a1991d4838fRichard Smith
6921745f5147e065900267c85a5568785a1991d4838fRichard Smithbool Expr::isPotentialConstantExpr(const FunctionDecl *FD,
6922745f5147e065900267c85a5568785a1991d4838fRichard Smith                                   llvm::SmallVectorImpl<
6923745f5147e065900267c85a5568785a1991d4838fRichard Smith                                     PartialDiagnosticAt> &Diags) {
6924745f5147e065900267c85a5568785a1991d4838fRichard Smith  // FIXME: It would be useful to check constexpr function templates, but at the
6925745f5147e065900267c85a5568785a1991d4838fRichard Smith  // moment the constant expression evaluator cannot cope with the non-rigorous
6926745f5147e065900267c85a5568785a1991d4838fRichard Smith  // ASTs which we build for dependent expressions.
6927745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (FD->isDependentContext())
6928745f5147e065900267c85a5568785a1991d4838fRichard Smith    return true;
6929745f5147e065900267c85a5568785a1991d4838fRichard Smith
6930745f5147e065900267c85a5568785a1991d4838fRichard Smith  Expr::EvalStatus Status;
6931745f5147e065900267c85a5568785a1991d4838fRichard Smith  Status.Diag = &Diags;
6932745f5147e065900267c85a5568785a1991d4838fRichard Smith
6933745f5147e065900267c85a5568785a1991d4838fRichard Smith  EvalInfo Info(FD->getASTContext(), Status);
6934745f5147e065900267c85a5568785a1991d4838fRichard Smith  Info.CheckingPotentialConstantExpression = true;
6935745f5147e065900267c85a5568785a1991d4838fRichard Smith
6936745f5147e065900267c85a5568785a1991d4838fRichard Smith  const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
6937745f5147e065900267c85a5568785a1991d4838fRichard Smith  const CXXRecordDecl *RD = MD ? MD->getParent()->getCanonicalDecl() : 0;
6938745f5147e065900267c85a5568785a1991d4838fRichard Smith
6939745f5147e065900267c85a5568785a1991d4838fRichard Smith  // FIXME: Fabricate an arbitrary expression on the stack and pretend that it
6940745f5147e065900267c85a5568785a1991d4838fRichard Smith  // is a temporary being used as the 'this' pointer.
6941745f5147e065900267c85a5568785a1991d4838fRichard Smith  LValue This;
6942745f5147e065900267c85a5568785a1991d4838fRichard Smith  ImplicitValueInitExpr VIE(RD ? Info.Ctx.getRecordType(RD) : Info.Ctx.IntTy);
694383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  This.set(&VIE, Info.CurrentCall->Index);
6944745f5147e065900267c85a5568785a1991d4838fRichard Smith
6945745f5147e065900267c85a5568785a1991d4838fRichard Smith  ArrayRef<const Expr*> Args;
6946745f5147e065900267c85a5568785a1991d4838fRichard Smith
6947745f5147e065900267c85a5568785a1991d4838fRichard Smith  SourceLocation Loc = FD->getLocation();
6948745f5147e065900267c85a5568785a1991d4838fRichard Smith
69491aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  APValue Scratch;
69501aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
6951745f5147e065900267c85a5568785a1991d4838fRichard Smith    HandleConstructorCall(Loc, This, Args, CD, Info, Scratch);
69521aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  else
6953745f5147e065900267c85a5568785a1991d4838fRichard Smith    HandleFunctionCall(Loc, FD, (MD && MD->isInstance()) ? &This : 0,
6954745f5147e065900267c85a5568785a1991d4838fRichard Smith                       Args, FD->getBody(), Info, Scratch);
6955745f5147e065900267c85a5568785a1991d4838fRichard Smith
6956745f5147e065900267c85a5568785a1991d4838fRichard Smith  return Diags.empty();
6957745f5147e065900267c85a5568785a1991d4838fRichard Smith}
6958