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
2901aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    typedef llvm::DenseMap<const Expr*, APValue> MapTy;
291bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    typedef MapTy::const_iterator temp_iterator;
292bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    /// Temporaries - Temporary lvalues materialized within this stack frame.
293bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    MapTy Temporaries;
294bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith
29508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    CallStackFrame(EvalInfo &Info, SourceLocation CallLoc,
29608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith                   const FunctionDecl *Callee, const LValue *This,
2971aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith                   const APValue *Arguments);
298bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    ~CallStackFrame();
299bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith  };
300bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith
301dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith  /// A partial diagnostic which we might know in advance that we are not going
302dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith  /// to emit.
303dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith  class OptionalDiagnostic {
304dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    PartialDiagnostic *Diag;
305dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith
306dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith  public:
307dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    explicit OptionalDiagnostic(PartialDiagnostic *Diag = 0) : Diag(Diag) {}
308dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith
309dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    template<typename T>
310dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    OptionalDiagnostic &operator<<(const T &v) {
311dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith      if (Diag)
312dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith        *Diag << v;
313dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith      return *this;
314dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    }
315789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith
316789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    OptionalDiagnostic &operator<<(const APSInt &I) {
317789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith      if (Diag) {
318789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith        llvm::SmallVector<char, 32> Buffer;
319789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith        I.toString(Buffer);
320789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith        *Diag << StringRef(Buffer.data(), Buffer.size());
321789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith      }
322789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith      return *this;
323789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    }
324789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith
325789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    OptionalDiagnostic &operator<<(const APFloat &F) {
326789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith      if (Diag) {
327789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith        llvm::SmallVector<char, 32> Buffer;
328789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith        F.toString(Buffer);
329789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith        *Diag << StringRef(Buffer.data(), Buffer.size());
330789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith      }
331789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith      return *this;
332789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    }
333dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith  };
334dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith
33583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  /// EvalInfo - This is a private struct used by the evaluator to capture
33683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  /// information about a subexpression as it is folded.  It retains information
33783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  /// about the AST context, but also maintains information about the folded
33883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  /// expression.
33983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  ///
34083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  /// If an expression could be evaluated, it is still possible it is not a C
34183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  /// "integer constant expression" or constant expression.  If not, this struct
34283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  /// captures information about how and why not.
34383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  ///
34483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  /// One bit of information passed *into* the request for constant folding
34583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  /// indicates whether the subexpression is "evaluated" or not according to C
34683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  /// rules.  For example, the RHS of (0 && foo()) is not evaluated.  We can
34783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  /// evaluate the expression regardless of what the RHS is, but C only allows
34883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  /// certain things in certain situations.
349c54061a679d8db4169438b2f97f81e3f443c6a25Benjamin Kramer  struct EvalInfo {
350dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    ASTContext &Ctx;
351d411a4b23077b29e19c9371bbb19b054a374d922Argyrios Kyrtzidis
3521e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    /// EvalStatus - Contains information about the evaluation.
3531e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    Expr::EvalStatus &EvalStatus;
354f0c1e4b679e15c26bffb5892e35985bf3c52f77aAnders Carlsson
355d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    /// CurrentCall - The top of the constexpr call stack.
356bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    CallStackFrame *CurrentCall;
357d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
358d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    /// CallStackDepth - The number of calls in the call stack right now.
359d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    unsigned CallStackDepth;
360d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
36183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    /// NextCallIndex - The next call index to assign.
36283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    unsigned NextCallIndex;
36383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith
3641aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    typedef llvm::DenseMap<const OpaqueValueExpr*, APValue> MapTy;
365bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    /// OpaqueValues - Values used as the common expression in a
366bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    /// BinaryConditionalOperator.
367c54061a679d8db4169438b2f97f81e3f443c6a25Benjamin Kramer    MapTy OpaqueValues;
368bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith
369bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    /// BottomFrame - The frame in which evaluation started. This must be
370745f5147e065900267c85a5568785a1991d4838fRichard Smith    /// initialized after CurrentCall and CallStackDepth.
371bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    CallStackFrame BottomFrame;
372bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith
373180f47959a066795cc0f409433023af448bb0328Richard Smith    /// EvaluatingDecl - This is the declaration whose initializer is being
374180f47959a066795cc0f409433023af448bb0328Richard Smith    /// evaluated, if any.
375180f47959a066795cc0f409433023af448bb0328Richard Smith    const VarDecl *EvaluatingDecl;
376180f47959a066795cc0f409433023af448bb0328Richard Smith
377180f47959a066795cc0f409433023af448bb0328Richard Smith    /// EvaluatingDeclValue - This is the value being constructed for the
378180f47959a066795cc0f409433023af448bb0328Richard Smith    /// declaration whose initializer is being evaluated, if any.
379180f47959a066795cc0f409433023af448bb0328Richard Smith    APValue *EvaluatingDeclValue;
380180f47959a066795cc0f409433023af448bb0328Richard Smith
381c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    /// HasActiveDiagnostic - Was the previous diagnostic stored? If so, further
382c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    /// notes attached to it will also be stored, otherwise they will not be.
383c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    bool HasActiveDiagnostic;
384c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
385745f5147e065900267c85a5568785a1991d4838fRichard Smith    /// CheckingPotentialConstantExpression - Are we checking whether the
386745f5147e065900267c85a5568785a1991d4838fRichard Smith    /// expression is a potential constant expression? If so, some diagnostics
387745f5147e065900267c85a5568785a1991d4838fRichard Smith    /// are suppressed.
388745f5147e065900267c85a5568785a1991d4838fRichard Smith    bool CheckingPotentialConstantExpression;
389745f5147e065900267c85a5568785a1991d4838fRichard Smith
390bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    EvalInfo(const ASTContext &C, Expr::EvalStatus &S)
391dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith      : Ctx(const_cast<ASTContext&>(C)), EvalStatus(S), CurrentCall(0),
39283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith        CallStackDepth(0), NextCallIndex(1),
39383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith        BottomFrame(*this, SourceLocation(), 0, 0, 0),
394745f5147e065900267c85a5568785a1991d4838fRichard Smith        EvaluatingDecl(0), EvaluatingDeclValue(0), HasActiveDiagnostic(false),
395649dfbc389671d0c852ead5953da630d675a5d43Argyrios Kyrtzidis        CheckingPotentialConstantExpression(false) {}
396bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith
3971aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    const APValue *getOpaqueValue(const OpaqueValueExpr *e) const {
398c54061a679d8db4169438b2f97f81e3f443c6a25Benjamin Kramer      MapTy::const_iterator i = OpaqueValues.find(e);
399c54061a679d8db4169438b2f97f81e3f443c6a25Benjamin Kramer      if (i == OpaqueValues.end()) return 0;
400c54061a679d8db4169438b2f97f81e3f443c6a25Benjamin Kramer      return &i->second;
401c54061a679d8db4169438b2f97f81e3f443c6a25Benjamin Kramer    }
40256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
403180f47959a066795cc0f409433023af448bb0328Richard Smith    void setEvaluatingDecl(const VarDecl *VD, APValue &Value) {
404180f47959a066795cc0f409433023af448bb0328Richard Smith      EvaluatingDecl = VD;
405180f47959a066795cc0f409433023af448bb0328Richard Smith      EvaluatingDeclValue = &Value;
406180f47959a066795cc0f409433023af448bb0328Richard Smith    }
407180f47959a066795cc0f409433023af448bb0328Richard Smith
4084e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    const LangOptions &getLangOpts() const { return Ctx.getLangOpts(); }
409c18c42345636e2866fed75c7e434fb659d747672Richard Smith
410c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    bool CheckCallLimit(SourceLocation Loc) {
411745f5147e065900267c85a5568785a1991d4838fRichard Smith      // Don't perform any constexpr calls (other than the call we're checking)
412745f5147e065900267c85a5568785a1991d4838fRichard Smith      // when checking a potential constant expression.
413745f5147e065900267c85a5568785a1991d4838fRichard Smith      if (CheckingPotentialConstantExpression && CallStackDepth > 1)
414745f5147e065900267c85a5568785a1991d4838fRichard Smith        return false;
41583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      if (NextCallIndex == 0) {
41683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith        // NextCallIndex has wrapped around.
41783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith        Diag(Loc, diag::note_constexpr_call_limit_exceeded);
41883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith        return false;
41983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      }
420c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      if (CallStackDepth <= getLangOpts().ConstexprCallDepth)
421c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith        return true;
422c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      Diag(Loc, diag::note_constexpr_depth_limit_exceeded)
423c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith        << getLangOpts().ConstexprCallDepth;
424c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      return false;
425c18c42345636e2866fed75c7e434fb659d747672Richard Smith    }
426f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
42783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    CallStackFrame *getCallFrame(unsigned CallIndex) {
42883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      assert(CallIndex && "no call index in getCallFrame");
42983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      // We will eventually hit BottomFrame, which has Index 1, so Frame can't
43083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      // be null in this loop.
43183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      CallStackFrame *Frame = CurrentCall;
43283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      while (Frame->Index > CallIndex)
43383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith        Frame = Frame->Caller;
43483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      return (Frame->Index == CallIndex) ? Frame : 0;
43583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    }
43683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith
437c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  private:
438c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    /// Add a diagnostic to the diagnostics list.
439c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    PartialDiagnostic &addDiag(SourceLocation Loc, diag::kind DiagId) {
440c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      PartialDiagnostic PD(DiagId, Ctx.getDiagAllocator());
441c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      EvalStatus.Diag->push_back(std::make_pair(Loc, PD));
442c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      return EvalStatus.Diag->back().second;
443c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    }
444c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
44508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    /// Add notes containing a call stack to the current point of evaluation.
44608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    void addCallStack(unsigned Limit);
44708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
448c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  public:
449f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    /// Diagnose that the evaluation cannot be folded.
4507098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    OptionalDiagnostic Diag(SourceLocation Loc, diag::kind DiagId
4517098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith                              = diag::note_invalid_subexpr_in_const_expr,
452c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                            unsigned ExtraNotes = 0) {
453f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      // If we have a prior diagnostic, it will be noting that the expression
454f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      // isn't a constant expression. This diagnostic is more important.
455f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      // FIXME: We might want to show both diagnostics to the user.
456dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith      if (EvalStatus.Diag) {
45708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        unsigned CallStackNotes = CallStackDepth - 1;
45808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        unsigned Limit = Ctx.getDiagnostics().getConstexprBacktraceLimit();
45908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        if (Limit)
46008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith          CallStackNotes = std::min(CallStackNotes, Limit + 1);
461745f5147e065900267c85a5568785a1991d4838fRichard Smith        if (CheckingPotentialConstantExpression)
462745f5147e065900267c85a5568785a1991d4838fRichard Smith          CallStackNotes = 0;
46308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
464c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith        HasActiveDiagnostic = true;
465dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith        EvalStatus.Diag->clear();
46608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        EvalStatus.Diag->reserve(1 + ExtraNotes + CallStackNotes);
46708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        addDiag(Loc, DiagId);
468745f5147e065900267c85a5568785a1991d4838fRichard Smith        if (!CheckingPotentialConstantExpression)
469745f5147e065900267c85a5568785a1991d4838fRichard Smith          addCallStack(Limit);
47008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        return OptionalDiagnostic(&(*EvalStatus.Diag)[0].second);
471dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith      }
472c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      HasActiveDiagnostic = false;
473dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith      return OptionalDiagnostic();
474dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    }
475dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith
4765cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    OptionalDiagnostic Diag(const Expr *E, diag::kind DiagId
4775cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith                              = diag::note_invalid_subexpr_in_const_expr,
4785cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith                            unsigned ExtraNotes = 0) {
4795cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      if (EvalStatus.Diag)
4805cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith        return Diag(E->getExprLoc(), DiagId, ExtraNotes);
4815cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      HasActiveDiagnostic = false;
4825cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      return OptionalDiagnostic();
4835cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    }
4845cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith
485dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    /// Diagnose that the evaluation does not produce a C++11 core constant
486dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    /// expression.
4875cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    template<typename LocArg>
4885cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    OptionalDiagnostic CCEDiag(LocArg Loc, diag::kind DiagId
4897098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith                                 = diag::note_invalid_subexpr_in_const_expr,
490c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                               unsigned ExtraNotes = 0) {
491dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith      // Don't override a previous diagnostic.
49251e47df5a57430f1b691b04258e663cce68aef9dEli Friedman      if (!EvalStatus.Diag || !EvalStatus.Diag->empty()) {
49351e47df5a57430f1b691b04258e663cce68aef9dEli Friedman        HasActiveDiagnostic = false;
494dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith        return OptionalDiagnostic();
49551e47df5a57430f1b691b04258e663cce68aef9dEli Friedman      }
496c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      return Diag(Loc, DiagId, ExtraNotes);
497c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    }
498c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
499c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    /// Add a note to a prior diagnostic.
500c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    OptionalDiagnostic Note(SourceLocation Loc, diag::kind DiagId) {
501c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      if (!HasActiveDiagnostic)
502c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith        return OptionalDiagnostic();
503c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      return OptionalDiagnostic(&addDiag(Loc, DiagId));
504f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    }
505099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
506099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    /// Add a stack of notes to a prior diagnostic.
507099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    void addNotes(ArrayRef<PartialDiagnosticAt> Diags) {
508099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith      if (HasActiveDiagnostic) {
509099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith        EvalStatus.Diag->insert(EvalStatus.Diag->end(),
510099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith                                Diags.begin(), Diags.end());
511099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith      }
512099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    }
513745f5147e065900267c85a5568785a1991d4838fRichard Smith
514745f5147e065900267c85a5568785a1991d4838fRichard Smith    /// Should we continue evaluation as much as possible after encountering a
515745f5147e065900267c85a5568785a1991d4838fRichard Smith    /// construct which can't be folded?
516745f5147e065900267c85a5568785a1991d4838fRichard Smith    bool keepEvaluatingAfterFailure() {
51774e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith      return CheckingPotentialConstantExpression &&
51874e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith             EvalStatus.Diag && EvalStatus.Diag->empty();
519745f5147e065900267c85a5568785a1991d4838fRichard Smith    }
520c54061a679d8db4169438b2f97f81e3f443c6a25Benjamin Kramer  };
521f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
522f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  /// Object used to treat all foldable expressions as constant expressions.
523f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  struct FoldConstant {
524f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    bool Enabled;
525f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
526f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    explicit FoldConstant(EvalInfo &Info)
527f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      : Enabled(Info.EvalStatus.Diag && Info.EvalStatus.Diag->empty() &&
528f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith                !Info.EvalStatus.HasSideEffects) {
529f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    }
530f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    // Treat the value we've computed since this object was created as constant.
531f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    void Fold(EvalInfo &Info) {
532f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      if (Enabled && !Info.EvalStatus.Diag->empty() &&
533f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          !Info.EvalStatus.HasSideEffects)
534f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        Info.EvalStatus.Diag->clear();
535f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    }
536f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  };
53774e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith
53874e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  /// RAII object used to suppress diagnostics and side-effects from a
53974e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  /// speculative evaluation.
54074e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  class SpeculativeEvaluationRAII {
54174e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    EvalInfo &Info;
54274e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    Expr::EvalStatus Old;
54374e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith
54474e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  public:
54574e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    SpeculativeEvaluationRAII(EvalInfo &Info,
54674e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith                              llvm::SmallVectorImpl<PartialDiagnosticAt>
54774e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith                                *NewDiag = 0)
54874e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith      : Info(Info), Old(Info.EvalStatus) {
54974e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith      Info.EvalStatus.Diag = NewDiag;
55074e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    }
55174e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    ~SpeculativeEvaluationRAII() {
55274e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith      Info.EvalStatus = Old;
55374e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    }
55474e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  };
55508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith}
55608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
557b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithbool SubobjectDesignator::checkSubobject(EvalInfo &Info, const Expr *E,
558b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                                         CheckSubobjectKind CSK) {
559b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (Invalid)
560b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    return false;
561b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (isOnePastTheEnd()) {
5625cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.CCEDiag(E, diag::note_constexpr_past_end_subobject)
563b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      << CSK;
564b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    setInvalid();
565b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    return false;
566b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  }
567b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  return true;
568b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith}
569b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
570b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithvoid SubobjectDesignator::diagnosePointerArithmetic(EvalInfo &Info,
571b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                                                    const Expr *E, uint64_t N) {
572b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (MostDerivedPathLength == Entries.size() && MostDerivedArraySize)
5735cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.CCEDiag(E, diag::note_constexpr_array_index)
574b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      << static_cast<int>(N) << /*array*/ 0
575b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      << static_cast<unsigned>(MostDerivedArraySize);
576b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  else
5775cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.CCEDiag(E, diag::note_constexpr_array_index)
578b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      << static_cast<int>(N) << /*non-array*/ 1;
579b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  setInvalid();
580b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith}
581b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
58208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard SmithCallStackFrame::CallStackFrame(EvalInfo &Info, SourceLocation CallLoc,
58308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith                               const FunctionDecl *Callee, const LValue *This,
5841aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith                               const APValue *Arguments)
58508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    : Info(Info), Caller(Info.CurrentCall), CallLoc(CallLoc), Callee(Callee),
58683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      Index(Info.NextCallIndex++), This(This), Arguments(Arguments) {
58708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  Info.CurrentCall = this;
58808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  ++Info.CallStackDepth;
58908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith}
59008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
59108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard SmithCallStackFrame::~CallStackFrame() {
59208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  assert(Info.CurrentCall == this && "calls retired out of order");
59308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  --Info.CallStackDepth;
59408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  Info.CurrentCall = Caller;
59508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith}
59687eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner
59708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith/// Produce a string describing the given constexpr call.
59808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smithstatic void describeCall(CallStackFrame *Frame, llvm::raw_ostream &Out) {
59908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  unsigned ArgIndex = 0;
60008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  bool IsMemberCall = isa<CXXMethodDecl>(Frame->Callee) &&
6015ba73e1af8ef519161bd40063dc325457e21676aRichard Smith                      !isa<CXXConstructorDecl>(Frame->Callee) &&
6025ba73e1af8ef519161bd40063dc325457e21676aRichard Smith                      cast<CXXMethodDecl>(Frame->Callee)->isInstance();
60308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
60408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  if (!IsMemberCall)
60508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    Out << *Frame->Callee << '(';
60608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
60708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  for (FunctionDecl::param_const_iterator I = Frame->Callee->param_begin(),
60808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith       E = Frame->Callee->param_end(); I != E; ++I, ++ArgIndex) {
6095fe31228c883040cf016cfc71ad4bfeba462602eNAKAMURA Takumi    if (ArgIndex > (unsigned)IsMemberCall)
61008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      Out << ", ";
61108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
61208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    const ParmVarDecl *Param = *I;
6131aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    const APValue &Arg = Frame->Arguments[ArgIndex];
6141aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    Arg.printPretty(Out, Frame->Info.Ctx, Param->getType());
61508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
61608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    if (ArgIndex == 0 && IsMemberCall)
61708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      Out << "->" << *Frame->Callee << '(';
618bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith  }
619d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
62008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  Out << ')';
62108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith}
62208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
62308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smithvoid EvalInfo::addCallStack(unsigned Limit) {
62408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  // Determine which calls to skip, if any.
62508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  unsigned ActiveCalls = CallStackDepth - 1;
62608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  unsigned SkipStart = ActiveCalls, SkipEnd = SkipStart;
62708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  if (Limit && Limit < ActiveCalls) {
62808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    SkipStart = Limit / 2 + Limit % 2;
62908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    SkipEnd = ActiveCalls - Limit / 2;
63008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  }
63108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
63208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  // Walk the call stack and add the diagnostics.
63308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  unsigned CallIdx = 0;
63408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  for (CallStackFrame *Frame = CurrentCall; Frame != &BottomFrame;
63508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith       Frame = Frame->Caller, ++CallIdx) {
63608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    // Skip this call?
63708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    if (CallIdx >= SkipStart && CallIdx < SkipEnd) {
63808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      if (CallIdx == SkipStart) {
63908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        // Note that we're skipping calls.
64008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        addDiag(Frame->CallLoc, diag::note_constexpr_calls_suppressed)
64108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith          << unsigned(ActiveCalls - Limit);
64208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      }
64308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      continue;
64408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    }
64508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
64608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    llvm::SmallVector<char, 128> Buffer;
64708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    llvm::raw_svector_ostream Out(Buffer);
64808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    describeCall(Frame, Out);
64908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    addDiag(Frame->CallLoc, diag::note_constexpr_call_here) << Out.str();
650bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith  }
65108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith}
652d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
65308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smithnamespace {
654f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall  struct ComplexValue {
655f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall  private:
656f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    bool IsInt;
657f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall
658f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall  public:
659f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    APSInt IntReal, IntImag;
660f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    APFloat FloatReal, FloatImag;
661f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall
662f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    ComplexValue() : FloatReal(APFloat::Bogus), FloatImag(APFloat::Bogus) {}
663f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall
664f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    void makeComplexFloat() { IsInt = false; }
665f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    bool isComplexFloat() const { return !IsInt; }
666f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    APFloat &getComplexFloatReal() { return FloatReal; }
667f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    APFloat &getComplexFloatImag() { return FloatImag; }
668f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall
669f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    void makeComplexInt() { IsInt = true; }
670f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    bool isComplexInt() const { return IsInt; }
671f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    APSInt &getComplexIntReal() { return IntReal; }
672f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    APSInt &getComplexIntImag() { return IntImag; }
673f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall
6741aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    void moveInto(APValue &v) const {
675f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall      if (isComplexFloat())
6761aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith        v = APValue(FloatReal, FloatImag);
677f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall      else
6781aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith        v = APValue(IntReal, IntImag);
679f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    }
6801aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    void setFrom(const APValue &v) {
68156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      assert(v.isComplexFloat() || v.isComplexInt());
68256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      if (v.isComplexFloat()) {
68356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall        makeComplexFloat();
68456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall        FloatReal = v.getComplexFloatReal();
68556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall        FloatImag = v.getComplexFloatImag();
68656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      } else {
68756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall        makeComplexInt();
68856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall        IntReal = v.getComplexIntReal();
68956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall        IntImag = v.getComplexIntImag();
69056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      }
69156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    }
692f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall  };
693efdb83e26f9a1fd2566afe54461216cd84814d42John McCall
694efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  struct LValue {
6951bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    APValue::LValueBase Base;
696efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    CharUnits Offset;
69783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    unsigned CallIndex;
6980a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    SubobjectDesignator Designator;
699efdb83e26f9a1fd2566afe54461216cd84814d42John McCall
7001bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    const APValue::LValueBase getLValueBase() const { return Base; }
70147a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    CharUnits &getLValueOffset() { return Offset; }
702625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith    const CharUnits &getLValueOffset() const { return Offset; }
70383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    unsigned getLValueCallIndex() const { return CallIndex; }
7040a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    SubobjectDesignator &getLValueDesignator() { return Designator; }
7050a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    const SubobjectDesignator &getLValueDesignator() const { return Designator;}
706efdb83e26f9a1fd2566afe54461216cd84814d42John McCall
7071aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    void moveInto(APValue &V) const {
7081aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith      if (Designator.Invalid)
7091aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith        V = APValue(Base, Offset, APValue::NoLValuePath(), CallIndex);
7101aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith      else
7111aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith        V = APValue(Base, Offset, Designator.Entries,
7121aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith                    Designator.IsOnePastTheEnd, CallIndex);
713efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    }
7141aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    void setFrom(ASTContext &Ctx, const APValue &V) {
71547a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith      assert(V.isLValue());
71647a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith      Base = V.getLValueBase();
71747a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith      Offset = V.getLValueOffset();
71883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      CallIndex = V.getLValueCallIndex();
7191aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith      Designator = SubobjectDesignator(Ctx, V);
7200a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    }
7210a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith
72283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    void set(APValue::LValueBase B, unsigned I = 0) {
7231bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith      Base = B;
7240a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      Offset = CharUnits::Zero();
72583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      CallIndex = I;
726b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      Designator = SubobjectDesignator(getType(B));
727b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    }
728b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
729b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    // Check that this LValue is not based on a null pointer. If it is, produce
730b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    // a diagnostic and mark the designator as invalid.
731b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    bool checkNullPointer(EvalInfo &Info, const Expr *E,
732b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                          CheckSubobjectKind CSK) {
733b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      if (Designator.Invalid)
734b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        return false;
735b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      if (!Base) {
7365cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith        Info.CCEDiag(E, diag::note_constexpr_null_subobject)
737b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith          << CSK;
738b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        Designator.setInvalid();
739b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        return false;
740b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      }
741b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      return true;
742b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    }
743b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
744b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    // Check this LValue refers to an object. If not, set the designator to be
745b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    // invalid and emit a diagnostic.
746b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK) {
7475cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      // Outside C++11, do not build a designator referring to a subobject of
7485cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      // any object: we won't use such a designator for anything.
7495cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      if (!Info.getLangOpts().CPlusPlus0x)
7505cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith        Designator.setInvalid();
751b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      return checkNullPointer(Info, E, CSK) &&
752b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith             Designator.checkSubobject(Info, E, CSK);
753b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    }
754b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
755b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    void addDecl(EvalInfo &Info, const Expr *E,
756b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                 const Decl *D, bool Virtual = false) {
7575cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      if (checkSubobject(Info, E, isa<FieldDecl>(D) ? CSK_Field : CSK_Base))
7585cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith        Designator.addDeclUnchecked(D, Virtual);
759b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    }
760b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    void addArray(EvalInfo &Info, const Expr *E, const ConstantArrayType *CAT) {
7615cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      if (checkSubobject(Info, E, CSK_ArrayToPointer))
7625cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith        Designator.addArrayUnchecked(CAT);
763b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    }
76486024013d4c3728122c58fa07a2a67e6c15837efRichard Smith    void addComplex(EvalInfo &Info, const Expr *E, QualType EltTy, bool Imag) {
7655cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      if (checkSubobject(Info, E, Imag ? CSK_Imag : CSK_Real))
7665cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith        Designator.addComplexUnchecked(EltTy, Imag);
76786024013d4c3728122c58fa07a2a67e6c15837efRichard Smith    }
768b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    void adjustIndex(EvalInfo &Info, const Expr *E, uint64_t N) {
7695cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      if (checkNullPointer(Info, E, CSK_ArrayIndex))
7705cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith        Designator.adjustIndex(Info, E, N);
77156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    }
772efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  };
773e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
774e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  struct MemberPtr {
775e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    MemberPtr() {}
776e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    explicit MemberPtr(const ValueDecl *Decl) :
777e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      DeclAndIsDerivedMember(Decl, false), Path() {}
778e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
779e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// The member or (direct or indirect) field referred to by this member
780e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// pointer, or 0 if this is a null member pointer.
781e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    const ValueDecl *getDecl() const {
782e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return DeclAndIsDerivedMember.getPointer();
783e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
784e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// Is this actually a member of some type derived from the relevant class?
785e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    bool isDerivedMember() const {
786e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return DeclAndIsDerivedMember.getInt();
787e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
788e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// Get the class which the declaration actually lives in.
789e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    const CXXRecordDecl *getContainingRecord() const {
790e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return cast<CXXRecordDecl>(
791e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith          DeclAndIsDerivedMember.getPointer()->getDeclContext());
792e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
793e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
7941aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    void moveInto(APValue &V) const {
7951aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith      V = APValue(getDecl(), isDerivedMember(), Path);
796e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
7971aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    void setFrom(const APValue &V) {
798e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      assert(V.isMemberPointer());
799e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      DeclAndIsDerivedMember.setPointer(V.getMemberPointerDecl());
800e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      DeclAndIsDerivedMember.setInt(V.isMemberPointerToDerivedMember());
801e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      Path.clear();
802e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      ArrayRef<const CXXRecordDecl*> P = V.getMemberPointerPath();
803e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      Path.insert(Path.end(), P.begin(), P.end());
804e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
805e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
806e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// DeclAndIsDerivedMember - The member declaration, and a flag indicating
807e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// whether the member is a member of some class derived from the class type
808e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// of the member pointer.
809e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    llvm::PointerIntPair<const ValueDecl*, 1, bool> DeclAndIsDerivedMember;
810e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// Path - The path of base/derived classes from the member declaration's
811e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// class (exclusive) to the class type of the member pointer (inclusive).
812e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    SmallVector<const CXXRecordDecl*, 4> Path;
813e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
814e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// Perform a cast towards the class of the Decl (either up or down the
815e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// hierarchy).
816e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    bool castBack(const CXXRecordDecl *Class) {
817e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      assert(!Path.empty());
818e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      const CXXRecordDecl *Expected;
819e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (Path.size() >= 2)
820e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        Expected = Path[Path.size() - 2];
821e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      else
822e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        Expected = getContainingRecord();
823e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (Expected->getCanonicalDecl() != Class->getCanonicalDecl()) {
824e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        // C++11 [expr.static.cast]p12: In a conversion from (D::*) to (B::*),
825e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        // if B does not contain the original member and is not a base or
826e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        // derived class of the class containing the original member, the result
827e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        // of the cast is undefined.
828e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        // C++11 [conv.mem]p2 does not cover this case for a cast from (B::*) to
829e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        // (D::*). We consider that to be a language defect.
830e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
831e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      }
832e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      Path.pop_back();
833e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return true;
834e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
835e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// Perform a base-to-derived member pointer cast.
836e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    bool castToDerived(const CXXRecordDecl *Derived) {
837e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!getDecl())
838e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return true;
839e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!isDerivedMember()) {
840e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        Path.push_back(Derived);
841e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return true;
842e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      }
843e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!castBack(Derived))
844e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
845e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (Path.empty())
846e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        DeclAndIsDerivedMember.setInt(false);
847e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return true;
848e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
849e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// Perform a derived-to-base member pointer cast.
850e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    bool castToBase(const CXXRecordDecl *Base) {
851e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!getDecl())
852e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return true;
853e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (Path.empty())
854e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        DeclAndIsDerivedMember.setInt(true);
855e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (isDerivedMember()) {
856e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        Path.push_back(Base);
857e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return true;
858e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      }
859e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return castBack(Base);
860e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
861e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  };
862c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
863b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith  /// Compare two member pointers, which are assumed to be of the same type.
864b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith  static bool operator==(const MemberPtr &LHS, const MemberPtr &RHS) {
865b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    if (!LHS.getDecl() || !RHS.getDecl())
866b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith      return !LHS.getDecl() && !RHS.getDecl();
867b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    if (LHS.getDecl()->getCanonicalDecl() != RHS.getDecl()->getCanonicalDecl())
868b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith      return false;
869b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    return LHS.Path == RHS.Path;
870b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith  }
871b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith
872c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  /// Kinds of constant expression checking, for diagnostics.
873c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  enum CheckConstantExpressionKind {
874c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    CCEK_Constant,    ///< A normal constant.
875c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    CCEK_ReturnValue, ///< A constexpr function return value.
876c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    CCEK_MemberInit   ///< A constexpr constructor mem-initializer.
877c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
878f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall}
87987eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner
8801aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smithstatic bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E);
88183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smithstatic bool EvaluateInPlace(APValue &Result, EvalInfo &Info,
88283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                            const LValue &This, const Expr *E,
88383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                            CheckConstantExpressionKind CCEK = CCEK_Constant,
88483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                            bool AllowNonLiteralTypes = false);
885efdb83e26f9a1fd2566afe54461216cd84814d42John McCallstatic bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info);
886efdb83e26f9a1fd2566afe54461216cd84814d42John McCallstatic bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info);
887e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result,
888e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                                  EvalInfo &Info);
889e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info);
89087eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattnerstatic bool EvaluateInteger(const Expr *E, APSInt  &Result, EvalInfo &Info);
8911aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smithstatic bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,
892d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner                                    EvalInfo &Info);
893d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmanstatic bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
894f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCallstatic bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info);
895f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
896f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===//
8974efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman// Misc utilities
8984efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman//===----------------------------------------------------------------------===//
8994efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
900180f47959a066795cc0f409433023af448bb0328Richard Smith/// Should this call expression be treated as a string literal?
901180f47959a066795cc0f409433023af448bb0328Richard Smithstatic bool IsStringLiteralCall(const CallExpr *E) {
902180f47959a066795cc0f409433023af448bb0328Richard Smith  unsigned Builtin = E->isBuiltinCall();
903180f47959a066795cc0f409433023af448bb0328Richard Smith  return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString ||
904180f47959a066795cc0f409433023af448bb0328Richard Smith          Builtin == Builtin::BI__builtin___NSStringMakeConstantString);
905180f47959a066795cc0f409433023af448bb0328Richard Smith}
906180f47959a066795cc0f409433023af448bb0328Richard Smith
9071bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smithstatic bool IsGlobalLValue(APValue::LValueBase B) {
908180f47959a066795cc0f409433023af448bb0328Richard Smith  // C++11 [expr.const]p3 An address constant expression is a prvalue core
909180f47959a066795cc0f409433023af448bb0328Richard Smith  // constant expression of pointer type that evaluates to...
910180f47959a066795cc0f409433023af448bb0328Richard Smith
911180f47959a066795cc0f409433023af448bb0328Richard Smith  // ... a null pointer value, or a prvalue core constant expression of type
912180f47959a066795cc0f409433023af448bb0328Richard Smith  // std::nullptr_t.
9131bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  if (!B) return true;
91442c8f87eb60958170c46767273bf93e6c96125bfJohn McCall
9151bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) {
916180f47959a066795cc0f409433023af448bb0328Richard Smith    // ... the address of an object with static storage duration,
9171bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    if (const VarDecl *VD = dyn_cast<VarDecl>(D))
91842c8f87eb60958170c46767273bf93e6c96125bfJohn McCall      return VD->hasGlobalStorage();
9191bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    // ... the address of a function,
9201bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    return isa<FunctionDecl>(D);
92142c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  }
9221bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith
9231bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  const Expr *E = B.get<const Expr*>();
9241bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  switch (E->getStmtClass()) {
9251bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  default:
9261bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    return false;
927b78ae9716576399145786b93f687943f8b197170Richard Smith  case Expr::CompoundLiteralExprClass: {
928b78ae9716576399145786b93f687943f8b197170Richard Smith    const CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E);
929b78ae9716576399145786b93f687943f8b197170Richard Smith    return CLE->isFileScope() && CLE->isLValue();
930b78ae9716576399145786b93f687943f8b197170Richard Smith  }
931180f47959a066795cc0f409433023af448bb0328Richard Smith  // A string literal has static storage duration.
932180f47959a066795cc0f409433023af448bb0328Richard Smith  case Expr::StringLiteralClass:
933180f47959a066795cc0f409433023af448bb0328Richard Smith  case Expr::PredefinedExprClass:
934180f47959a066795cc0f409433023af448bb0328Richard Smith  case Expr::ObjCStringLiteralClass:
935180f47959a066795cc0f409433023af448bb0328Richard Smith  case Expr::ObjCEncodeExprClass:
93647d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith  case Expr::CXXTypeidExprClass:
937e275a1845b9e32bd3034f2593dee1780855c8fd6Francois Pichet  case Expr::CXXUuidofExprClass:
938180f47959a066795cc0f409433023af448bb0328Richard Smith    return true;
939180f47959a066795cc0f409433023af448bb0328Richard Smith  case Expr::CallExprClass:
940180f47959a066795cc0f409433023af448bb0328Richard Smith    return IsStringLiteralCall(cast<CallExpr>(E));
941180f47959a066795cc0f409433023af448bb0328Richard Smith  // For GCC compatibility, &&label has static storage duration.
942180f47959a066795cc0f409433023af448bb0328Richard Smith  case Expr::AddrLabelExprClass:
943180f47959a066795cc0f409433023af448bb0328Richard Smith    return true;
944180f47959a066795cc0f409433023af448bb0328Richard Smith  // A Block literal expression may be used as the initialization value for
945180f47959a066795cc0f409433023af448bb0328Richard Smith  // Block variables at global or local static scope.
946180f47959a066795cc0f409433023af448bb0328Richard Smith  case Expr::BlockExprClass:
947180f47959a066795cc0f409433023af448bb0328Richard Smith    return !cast<BlockExpr>(E)->getBlockDecl()->hasCaptures();
948745f5147e065900267c85a5568785a1991d4838fRichard Smith  case Expr::ImplicitValueInitExprClass:
949745f5147e065900267c85a5568785a1991d4838fRichard Smith    // FIXME:
950745f5147e065900267c85a5568785a1991d4838fRichard Smith    // We can never form an lvalue with an implicit value initialization as its
951745f5147e065900267c85a5568785a1991d4838fRichard Smith    // base through expression evaluation, so these only appear in one case: the
952745f5147e065900267c85a5568785a1991d4838fRichard Smith    // implicit variable declaration we invent when checking whether a constexpr
953745f5147e065900267c85a5568785a1991d4838fRichard Smith    // constructor can produce a constant expression. We must assume that such
954745f5147e065900267c85a5568785a1991d4838fRichard Smith    // an expression might be a global lvalue.
955745f5147e065900267c85a5568785a1991d4838fRichard Smith    return true;
956180f47959a066795cc0f409433023af448bb0328Richard Smith  }
95742c8f87eb60958170c46767273bf93e6c96125bfJohn McCall}
95842c8f87eb60958170c46767273bf93e6c96125bfJohn McCall
95983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smithstatic void NoteLValueLocation(EvalInfo &Info, APValue::LValueBase Base) {
96083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  assert(Base && "no location for a null lvalue");
96183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>();
96283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  if (VD)
96383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    Info.Note(VD->getLocation(), diag::note_declared_at);
96483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  else
96583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    Info.Note(Base.dyn_cast<const Expr*>()->getExprLoc(),
96683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith              diag::note_constexpr_temporary_here);
96783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith}
96883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith
9699a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith/// Check that this reference or pointer core constant expression is a valid
9701aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith/// value for an address or reference constant expression. Return true if we
9711aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith/// can fold this expression, whether or not it's a constant expression.
97283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smithstatic bool CheckLValueConstantExpression(EvalInfo &Info, SourceLocation Loc,
97383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                                          QualType Type, const LValue &LVal) {
97483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  bool IsReferenceType = Type->isReferenceType();
97583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith
976c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  APValue::LValueBase Base = LVal.getLValueBase();
977c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  const SubobjectDesignator &Designator = LVal.getLValueDesignator();
978c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
979b78ae9716576399145786b93f687943f8b197170Richard Smith  // Check that the object is a global. Note that the fake 'this' object we
980b78ae9716576399145786b93f687943f8b197170Richard Smith  // manufacture when checking potential constant expressions is conservatively
981b78ae9716576399145786b93f687943f8b197170Richard Smith  // assumed to be global here.
982c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  if (!IsGlobalLValue(Base)) {
983c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    if (Info.getLangOpts().CPlusPlus0x) {
984c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>();
98583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      Info.Diag(Loc, diag::note_constexpr_non_global, 1)
98683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith        << IsReferenceType << !Designator.Entries.empty()
98783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith        << !!VD << VD;
98883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      NoteLValueLocation(Info, Base);
989c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    } else {
99083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      Info.Diag(Loc);
991c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    }
99261e616206413d1779c7545c7a8ad1ce1129ad9c1Richard Smith    // Don't allow references to temporaries to escape.
99369c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith    return false;
994f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
99583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  assert((Info.CheckingPotentialConstantExpression ||
99683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith          LVal.getLValueCallIndex() == 0) &&
99783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith         "have call index for global lvalue");
998b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
999b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // Allow address constant expressions to be past-the-end pointers. This is
1000b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // an extension: the standard requires them to point to an object.
1001b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (!IsReferenceType)
1002b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    return true;
1003b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
1004b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // A reference constant expression must refer to an object.
1005b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (!Base) {
1006b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    // FIXME: diagnostic
100783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    Info.CCEDiag(Loc);
100861e616206413d1779c7545c7a8ad1ce1129ad9c1Richard Smith    return true;
1009b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  }
1010b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
1011c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  // Does this refer one past the end of some object?
1012b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (Designator.isOnePastTheEnd()) {
1013c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>();
101483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    Info.Diag(Loc, diag::note_constexpr_past_end, 1)
1015c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      << !Designator.Entries.empty() << !!VD << VD;
101683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    NoteLValueLocation(Info, Base);
1017c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  }
1018c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
101969c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith  return true;
102047a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith}
102147a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith
102251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// Check that this core constant expression is of literal type, and if not,
102351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// produce an appropriate diagnostic.
102451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smithstatic bool CheckLiteralType(EvalInfo &Info, const Expr *E) {
102551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (!E->isRValue() || E->getType()->isLiteralType())
102651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return true;
102751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
102851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  // Prvalue constant expressions must be of literal types.
102951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (Info.getLangOpts().CPlusPlus0x)
10305cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.Diag(E, diag::note_constexpr_nonliteral)
103151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      << E->getType();
103251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  else
10335cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.Diag(E, diag::note_invalid_subexpr_in_const_expr);
103451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  return false;
103551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith}
103651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
10379a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith/// Check that this core constant expression value is a valid value for a
103883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith/// constant expression. If not, report an appropriate diagnostic. Does not
103983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith/// check that the expression is of literal type.
104083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smithstatic bool CheckConstantExpression(EvalInfo &Info, SourceLocation DiagLoc,
104183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                                    QualType Type, const APValue &Value) {
104283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  // Core issue 1454: For a literal constant expression of array or class type,
104383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  // each subobject of its value shall have been initialized by a constant
104483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  // expression.
104583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  if (Value.isArray()) {
104683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    QualType EltTy = Type->castAsArrayTypeUnsafe()->getElementType();
104783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    for (unsigned I = 0, N = Value.getArrayInitializedElts(); I != N; ++I) {
104883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      if (!CheckConstantExpression(Info, DiagLoc, EltTy,
104983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                                   Value.getArrayInitializedElt(I)))
105083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith        return false;
105183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    }
105283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    if (!Value.hasArrayFiller())
105383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      return true;
105483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return CheckConstantExpression(Info, DiagLoc, EltTy,
105583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                                   Value.getArrayFiller());
105683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  }
105783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  if (Value.isUnion() && Value.getUnionField()) {
105883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return CheckConstantExpression(Info, DiagLoc,
105983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                                   Value.getUnionField()->getType(),
106083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                                   Value.getUnionValue());
106183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  }
106283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  if (Value.isStruct()) {
106383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    RecordDecl *RD = Type->castAs<RecordType>()->getDecl();
106483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD)) {
106583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      unsigned BaseIndex = 0;
106683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(),
106783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith             End = CD->bases_end(); I != End; ++I, ++BaseIndex) {
106883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith        if (!CheckConstantExpression(Info, DiagLoc, I->getType(),
106983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                                     Value.getStructBase(BaseIndex)))
107083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith          return false;
107183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      }
107283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    }
107383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
107483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith         I != E; ++I) {
107583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      if (!CheckConstantExpression(Info, DiagLoc, (*I)->getType(),
107683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                                   Value.getStructField((*I)->getFieldIndex())))
107783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith        return false;
107883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    }
107983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  }
108083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith
108183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  if (Value.isLValue()) {
108283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    LValue LVal;
10831aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    LVal.setFrom(Info.Ctx, Value);
108483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return CheckLValueConstantExpression(Info, DiagLoc, Type, LVal);
10859a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith  }
108683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith
108783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  // Everything else is fine.
108883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  return true;
10899a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith}
10909a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith
10919e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smithconst ValueDecl *GetLValueBaseDecl(const LValue &LVal) {
10921bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  return LVal.Base.dyn_cast<const ValueDecl*>();
10939e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith}
10949e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith
10959e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smithstatic bool IsLiteralLValue(const LValue &Value) {
109683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  return Value.Base.dyn_cast<const Expr*>() && !Value.CallIndex;
10979e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith}
10989e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith
109965ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smithstatic bool IsWeakLValue(const LValue &Value) {
110065ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith  const ValueDecl *Decl = GetLValueBaseDecl(Value);
11010dd7a25e8d679de1dc0ce788222d6dee0e879885Lang Hames  return Decl && Decl->isWeak();
110265ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith}
110365ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith
11041aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smithstatic bool EvalPointerValueAsBool(const APValue &Value, bool &Result) {
11053554283157190e67918fad4221a5e6faf9317362John McCall  // A null base expression indicates a null pointer.  These are always
11063554283157190e67918fad4221a5e6faf9317362John McCall  // evaluatable, and they are false unless the offset is zero.
1107e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (!Value.getLValueBase()) {
1108e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    Result = !Value.getLValueOffset().isZero();
11093554283157190e67918fad4221a5e6faf9317362John McCall    return true;
11103554283157190e67918fad4221a5e6faf9317362John McCall  }
11113554283157190e67918fad4221a5e6faf9317362John McCall
1112e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // We have a non-null base.  These are generally known to be true, but if it's
1113e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // a weak declaration it can be null at runtime.
11143554283157190e67918fad4221a5e6faf9317362John McCall  Result = true;
1115e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  const ValueDecl *Decl = Value.getLValueBase().dyn_cast<const ValueDecl*>();
11160dd7a25e8d679de1dc0ce788222d6dee0e879885Lang Hames  return !Decl || !Decl->isWeak();
11175bc86103767c2abcbfdd6518e0ccbbbb6aa59e0fEli Friedman}
11185bc86103767c2abcbfdd6518e0ccbbbb6aa59e0fEli Friedman
11191aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smithstatic bool HandleConversionToBool(const APValue &Val, bool &Result) {
1120c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  switch (Val.getKind()) {
1121c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  case APValue::Uninitialized:
1122c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return false;
1123c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  case APValue::Int:
1124c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    Result = Val.getInt().getBoolValue();
112541bf4f38348561a0f12c10d34f1673cd19a6eb04Richard Smith    return true;
1126c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  case APValue::Float:
1127c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    Result = !Val.getFloat().isZero();
1128a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman    return true;
1129c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  case APValue::ComplexInt:
1130c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    Result = Val.getComplexIntReal().getBoolValue() ||
1131c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith             Val.getComplexIntImag().getBoolValue();
1132c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return true;
1133c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  case APValue::ComplexFloat:
1134c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    Result = !Val.getComplexFloatReal().isZero() ||
1135c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith             !Val.getComplexFloatImag().isZero();
1136436c8898cd1c93c5bacd3fcc4ac586bc5cd77062Richard Smith    return true;
1137e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  case APValue::LValue:
1138e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return EvalPointerValueAsBool(Val, Result);
1139e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  case APValue::MemberPointer:
1140e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    Result = Val.getMemberPointerDecl();
1141e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
1142c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  case APValue::Vector:
1143cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  case APValue::Array:
1144180f47959a066795cc0f409433023af448bb0328Richard Smith  case APValue::Struct:
1145180f47959a066795cc0f409433023af448bb0328Richard Smith  case APValue::Union:
114665639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman  case APValue::AddrLabelDiff:
1147c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return false;
11484efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman  }
11494efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
1150c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  llvm_unreachable("unknown APValue kind");
1151c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith}
1152c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
1153c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smithstatic bool EvaluateAsBooleanCondition(const Expr *E, bool &Result,
1154c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith                                       EvalInfo &Info) {
1155c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  assert(E->isRValue() && "missing lvalue-to-rvalue conv in bool condition");
11561aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  APValue Val;
1157d411a4b23077b29e19c9371bbb19b054a374d922Argyrios Kyrtzidis  if (!Evaluate(Val, Info, E))
1158c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return false;
1159d411a4b23077b29e19c9371bbb19b054a374d922Argyrios Kyrtzidis  return HandleConversionToBool(Val, Result);
11604efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman}
11614efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
1162c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithtemplate<typename T>
1163c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstatic bool HandleOverflow(EvalInfo &Info, const Expr *E,
1164c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                           const T &SrcValue, QualType DestType) {
11655cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith  Info.Diag(E, diag::note_constexpr_overflow)
1166789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    << SrcValue << DestType;
1167c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  return false;
1168c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
1169c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
1170c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstatic bool HandleFloatToIntCast(EvalInfo &Info, const Expr *E,
1171c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                 QualType SrcType, const APFloat &Value,
1172c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                 QualType DestType, APSInt &Result) {
1173c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  unsigned DestWidth = Info.Ctx.getIntWidth(DestType);
1174a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar  // Determine whether we are converting to unsigned or signed.
1175575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor  bool DestSigned = DestType->isSignedIntegerOrEnumerationType();
11761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1177c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  Result = APSInt(DestWidth, !DestSigned);
1178a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar  bool ignored;
1179c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  if (Value.convertToInteger(Result, llvm::APFloat::rmTowardZero, &ignored)
1180c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      & APFloat::opInvalidOp)
1181c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return HandleOverflow(Info, E, Value, DestType);
1182c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  return true;
1183a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar}
1184a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar
1185c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstatic bool HandleFloatToFloatCast(EvalInfo &Info, const Expr *E,
1186c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                   QualType SrcType, QualType DestType,
1187c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                   APFloat &Result) {
1188c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  APFloat Value = Result;
1189a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar  bool ignored;
1190c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  if (Result.convert(Info.Ctx.getFloatTypeSemantics(DestType),
1191c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                     APFloat::rmNearestTiesToEven, &ignored)
1192c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      & APFloat::opOverflow)
1193c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return HandleOverflow(Info, E, Value, DestType);
1194c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  return true;
1195a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar}
1196a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar
1197f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smithstatic APSInt HandleIntToIntCast(EvalInfo &Info, const Expr *E,
1198f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith                                 QualType DestType, QualType SrcType,
1199f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith                                 APSInt &Value) {
1200f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith  unsigned DestWidth = Info.Ctx.getIntWidth(DestType);
1201a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar  APSInt Result = Value;
1202a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar  // Figure out if this is a truncate, extend or noop cast.
1203a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar  // If the input is signed, do a sign extend, noop, or truncate.
12049f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad  Result = Result.extOrTrunc(DestWidth);
1205575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor  Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType());
1206a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar  return Result;
1207a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar}
1208a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar
1209c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstatic bool HandleIntToFloatCast(EvalInfo &Info, const Expr *E,
1210c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                 QualType SrcType, const APSInt &Value,
1211c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                 QualType DestType, APFloat &Result) {
1212c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  Result = APFloat(Info.Ctx.getFloatTypeSemantics(DestType), 1);
1213c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  if (Result.convertFromAPInt(Value, Value.isSigned(),
1214c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                              APFloat::rmNearestTiesToEven)
1215c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      & APFloat::opOverflow)
1216c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return HandleOverflow(Info, E, Value, DestType);
1217c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  return true;
1218a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar}
1219a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar
1220e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedmanstatic bool EvalAndBitcastToAPInt(EvalInfo &Info, const Expr *E,
1221e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman                                  llvm::APInt &Res) {
12221aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  APValue SVal;
1223e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  if (!Evaluate(SVal, Info, E))
1224e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    return false;
1225e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  if (SVal.isInt()) {
1226e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    Res = SVal.getInt();
1227e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    return true;
1228e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  }
1229e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  if (SVal.isFloat()) {
1230e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    Res = SVal.getFloat().bitcastToAPInt();
1231e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    return true;
1232e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  }
1233e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  if (SVal.isVector()) {
1234e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    QualType VecTy = E->getType();
1235e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    unsigned VecSize = Info.Ctx.getTypeSize(VecTy);
1236e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    QualType EltTy = VecTy->castAs<VectorType>()->getElementType();
1237e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    unsigned EltSize = Info.Ctx.getTypeSize(EltTy);
1238e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian();
1239e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    Res = llvm::APInt::getNullValue(VecSize);
1240e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    for (unsigned i = 0; i < SVal.getVectorLength(); i++) {
1241e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      APValue &Elt = SVal.getVectorElt(i);
1242e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      llvm::APInt EltAsInt;
1243e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      if (Elt.isInt()) {
1244e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        EltAsInt = Elt.getInt();
1245e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      } else if (Elt.isFloat()) {
1246e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        EltAsInt = Elt.getFloat().bitcastToAPInt();
1247e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      } else {
1248e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        // Don't try to handle vectors of anything other than int or float
1249e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        // (not sure if it's possible to hit this case).
12505cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith        Info.Diag(E, diag::note_invalid_subexpr_in_const_expr);
1251e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        return false;
1252e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      }
1253e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      unsigned BaseEltSize = EltAsInt.getBitWidth();
1254e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      if (BigEndian)
1255e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        Res |= EltAsInt.zextOrTrunc(VecSize).rotr(i*EltSize+BaseEltSize);
1256e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      else
1257e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        Res |= EltAsInt.zextOrTrunc(VecSize).rotl(i*EltSize);
1258e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    }
1259e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    return true;
1260e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  }
1261e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  // Give up if the input isn't an int, float, or vector.  For example, we
1262e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  // reject "(v4i16)(intptr_t)&a".
12635cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith  Info.Diag(E, diag::note_invalid_subexpr_in_const_expr);
1264e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  return false;
1265e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman}
1266e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman
1267b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith/// Cast an lvalue referring to a base subobject to a derived class, by
1268b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith/// truncating the lvalue's path to the given length.
1269b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic bool CastToDerivedClass(EvalInfo &Info, const Expr *E, LValue &Result,
1270b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                               const RecordDecl *TruncatedType,
1271b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                               unsigned TruncatedElements) {
1272b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  SubobjectDesignator &D = Result.Designator;
1273180f47959a066795cc0f409433023af448bb0328Richard Smith
1274b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // Check we actually point to a derived class object.
1275b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (TruncatedElements == D.Entries.size())
1276b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    return true;
1277b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  assert(TruncatedElements >= D.MostDerivedPathLength &&
1278b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith         "not casting to a derived class");
1279b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (!Result.checkSubobject(Info, E, CSK_Derived))
1280180f47959a066795cc0f409433023af448bb0328Richard Smith    return false;
1281180f47959a066795cc0f409433023af448bb0328Richard Smith
1282b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // Truncate the path to the subobject, and remove any derived-to-base offsets.
1283e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  const RecordDecl *RD = TruncatedType;
1284e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  for (unsigned I = TruncatedElements, N = D.Entries.size(); I != N; ++I) {
1285180f47959a066795cc0f409433023af448bb0328Richard Smith    const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD);
1286180f47959a066795cc0f409433023af448bb0328Richard Smith    const CXXRecordDecl *Base = getAsBaseClass(D.Entries[I]);
1287e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (isVirtualBaseClass(D.Entries[I]))
1288180f47959a066795cc0f409433023af448bb0328Richard Smith      Result.Offset -= Layout.getVBaseClassOffset(Base);
1289e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    else
1290180f47959a066795cc0f409433023af448bb0328Richard Smith      Result.Offset -= Layout.getBaseClassOffset(Base);
1291180f47959a066795cc0f409433023af448bb0328Richard Smith    RD = Base;
1292180f47959a066795cc0f409433023af448bb0328Richard Smith  }
1293e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  D.Entries.resize(TruncatedElements);
1294180f47959a066795cc0f409433023af448bb0328Richard Smith  return true;
1295180f47959a066795cc0f409433023af448bb0328Richard Smith}
1296180f47959a066795cc0f409433023af448bb0328Richard Smith
1297b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic void HandleLValueDirectBase(EvalInfo &Info, const Expr *E, LValue &Obj,
1298180f47959a066795cc0f409433023af448bb0328Richard Smith                                   const CXXRecordDecl *Derived,
1299180f47959a066795cc0f409433023af448bb0328Richard Smith                                   const CXXRecordDecl *Base,
1300180f47959a066795cc0f409433023af448bb0328Richard Smith                                   const ASTRecordLayout *RL = 0) {
1301180f47959a066795cc0f409433023af448bb0328Richard Smith  if (!RL) RL = &Info.Ctx.getASTRecordLayout(Derived);
1302180f47959a066795cc0f409433023af448bb0328Richard Smith  Obj.getLValueOffset() += RL->getBaseClassOffset(Base);
1303b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  Obj.addDecl(Info, E, Base, /*Virtual*/ false);
1304180f47959a066795cc0f409433023af448bb0328Richard Smith}
1305180f47959a066795cc0f409433023af448bb0328Richard Smith
1306b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic bool HandleLValueBase(EvalInfo &Info, const Expr *E, LValue &Obj,
1307180f47959a066795cc0f409433023af448bb0328Richard Smith                             const CXXRecordDecl *DerivedDecl,
1308180f47959a066795cc0f409433023af448bb0328Richard Smith                             const CXXBaseSpecifier *Base) {
1309180f47959a066795cc0f409433023af448bb0328Richard Smith  const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl();
1310180f47959a066795cc0f409433023af448bb0328Richard Smith
1311180f47959a066795cc0f409433023af448bb0328Richard Smith  if (!Base->isVirtual()) {
1312b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    HandleLValueDirectBase(Info, E, Obj, DerivedDecl, BaseDecl);
1313180f47959a066795cc0f409433023af448bb0328Richard Smith    return true;
1314180f47959a066795cc0f409433023af448bb0328Richard Smith  }
1315180f47959a066795cc0f409433023af448bb0328Richard Smith
1316b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  SubobjectDesignator &D = Obj.Designator;
1317b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (D.Invalid)
1318b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    return false;
1319b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
1320180f47959a066795cc0f409433023af448bb0328Richard Smith  // Extract most-derived object and corresponding type.
1321b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  DerivedDecl = D.MostDerivedType->getAsCXXRecordDecl();
1322b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (!CastToDerivedClass(Info, E, Obj, DerivedDecl, D.MostDerivedPathLength))
1323180f47959a066795cc0f409433023af448bb0328Richard Smith    return false;
1324180f47959a066795cc0f409433023af448bb0328Richard Smith
1325b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // Find the virtual base class.
1326180f47959a066795cc0f409433023af448bb0328Richard Smith  const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl);
1327180f47959a066795cc0f409433023af448bb0328Richard Smith  Obj.getLValueOffset() += Layout.getVBaseClassOffset(BaseDecl);
1328b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  Obj.addDecl(Info, E, BaseDecl, /*Virtual*/ true);
1329180f47959a066795cc0f409433023af448bb0328Richard Smith  return true;
1330180f47959a066795cc0f409433023af448bb0328Richard Smith}
1331180f47959a066795cc0f409433023af448bb0328Richard Smith
1332180f47959a066795cc0f409433023af448bb0328Richard Smith/// Update LVal to refer to the given field, which must be a member of the type
1333180f47959a066795cc0f409433023af448bb0328Richard Smith/// currently described by LVal.
1334b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic void HandleLValueMember(EvalInfo &Info, const Expr *E, LValue &LVal,
1335180f47959a066795cc0f409433023af448bb0328Richard Smith                               const FieldDecl *FD,
1336180f47959a066795cc0f409433023af448bb0328Richard Smith                               const ASTRecordLayout *RL = 0) {
1337180f47959a066795cc0f409433023af448bb0328Richard Smith  if (!RL)
1338180f47959a066795cc0f409433023af448bb0328Richard Smith    RL = &Info.Ctx.getASTRecordLayout(FD->getParent());
1339180f47959a066795cc0f409433023af448bb0328Richard Smith
1340180f47959a066795cc0f409433023af448bb0328Richard Smith  unsigned I = FD->getFieldIndex();
1341180f47959a066795cc0f409433023af448bb0328Richard Smith  LVal.Offset += Info.Ctx.toCharUnitsFromBits(RL->getFieldOffset(I));
1342b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  LVal.addDecl(Info, E, FD);
1343180f47959a066795cc0f409433023af448bb0328Richard Smith}
1344180f47959a066795cc0f409433023af448bb0328Richard Smith
1345d9b02e726262e4009dda830998bb934172ac0020Richard Smith/// Update LVal to refer to the given indirect field.
1346d9b02e726262e4009dda830998bb934172ac0020Richard Smithstatic void HandleLValueIndirectMember(EvalInfo &Info, const Expr *E,
1347d9b02e726262e4009dda830998bb934172ac0020Richard Smith                                       LValue &LVal,
1348d9b02e726262e4009dda830998bb934172ac0020Richard Smith                                       const IndirectFieldDecl *IFD) {
1349d9b02e726262e4009dda830998bb934172ac0020Richard Smith  for (IndirectFieldDecl::chain_iterator C = IFD->chain_begin(),
1350d9b02e726262e4009dda830998bb934172ac0020Richard Smith                                         CE = IFD->chain_end(); C != CE; ++C)
1351d9b02e726262e4009dda830998bb934172ac0020Richard Smith    HandleLValueMember(Info, E, LVal, cast<FieldDecl>(*C));
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];
1955b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      HandleLValueDirectBase(Info, BO, LV, RD, Base);
1956e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      RD = Base;
1957e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
1958e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // Finally cast to the class containing the member.
1959b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    HandleLValueDirectBase(Info, BO, LV, RD, MemPtr.getContainingRecord());
1960e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
1961e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1962e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // Add the member. Note that we cannot build bound member functions here.
1963e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (IncludeMember) {
1964d9b02e726262e4009dda830998bb934172ac0020Richard Smith    if (const FieldDecl *FD = dyn_cast<FieldDecl>(MemPtr.getDecl()))
1965d9b02e726262e4009dda830998bb934172ac0020Richard Smith      HandleLValueMember(Info, BO, LV, FD);
1966d9b02e726262e4009dda830998bb934172ac0020Richard Smith    else if (const IndirectFieldDecl *IFD =
1967d9b02e726262e4009dda830998bb934172ac0020Richard Smith               dyn_cast<IndirectFieldDecl>(MemPtr.getDecl()))
1968d9b02e726262e4009dda830998bb934172ac0020Richard Smith      HandleLValueIndirectMember(Info, BO, LV, IFD);
1969d9b02e726262e4009dda830998bb934172ac0020Richard Smith    else
1970d9b02e726262e4009dda830998bb934172ac0020Richard Smith      llvm_unreachable("can't construct reference to bound member function");
1971e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
1972e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1973e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  return MemPtr.getDecl();
1974e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}
1975e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1976e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// HandleBaseToDerivedCast - Apply the given base-to-derived cast operation on
1977e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// the provided lvalue, which currently refers to the base object.
1978e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E,
1979e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                                    LValue &Result) {
1980e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  SubobjectDesignator &D = Result.Designator;
1981b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (D.Invalid || !Result.checkNullPointer(Info, E, CSK_Derived))
1982e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return false;
1983e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1984e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  QualType TargetQT = E->getType();
1985e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (const PointerType *PT = TargetQT->getAs<PointerType>())
1986e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    TargetQT = PT->getPointeeType();
1987b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
1988b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // Check this cast lands within the final derived-to-base subobject path.
1989b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (D.MostDerivedPathLength + E->path_size() > D.Entries.size()) {
19905cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.CCEDiag(E, diag::note_constexpr_invalid_downcast)
1991b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      << D.MostDerivedType << TargetQT;
1992b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    return false;
1993b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  }
1994b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
1995b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // Check the type of the final cast. We don't need to check the path,
1996b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // since a cast can only be formed if the path is unique.
1997b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  unsigned NewEntriesSize = D.Entries.size() - E->path_size();
1998e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  const CXXRecordDecl *TargetType = TargetQT->getAsCXXRecordDecl();
1999e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  const CXXRecordDecl *FinalType;
2000b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (NewEntriesSize == D.MostDerivedPathLength)
2001b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    FinalType = D.MostDerivedType->getAsCXXRecordDecl();
2002b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  else
2003e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    FinalType = getAsBaseClass(D.Entries[NewEntriesSize - 1]);
2004b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl()) {
20055cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.CCEDiag(E, diag::note_constexpr_invalid_downcast)
2006b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      << D.MostDerivedType << TargetQT;
2007e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return false;
2008b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  }
2009e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2010e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // Truncate the lvalue to the appropriate derived class.
2011b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  return CastToDerivedClass(Info, E, Result, TargetType, NewEntriesSize);
201259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
201359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
2014c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stumpnamespace {
2015d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smithenum EvalStmtResult {
2016d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  /// Evaluation failed.
2017d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  ESR_Failed,
2018d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  /// Hit a 'return' statement.
2019d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  ESR_Returned,
2020d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  /// Evaluation succeeded.
2021d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  ESR_Succeeded
2022d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith};
2023d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith}
2024d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
2025d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith// Evaluate a statement.
20261aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smithstatic EvalStmtResult EvaluateStmt(APValue &Result, EvalInfo &Info,
2027d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith                                   const Stmt *S) {
2028d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  switch (S->getStmtClass()) {
2029d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  default:
2030d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    return ESR_Failed;
2031d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
2032d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  case Stmt::NullStmtClass:
2033d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  case Stmt::DeclStmtClass:
2034d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    return ESR_Succeeded;
2035d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
2036c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  case Stmt::ReturnStmtClass: {
2037c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    const Expr *RetExpr = cast<ReturnStmt>(S)->getRetValue();
203883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    if (!Evaluate(Result, Info, RetExpr))
2039c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      return ESR_Failed;
2040c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return ESR_Returned;
2041c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  }
2042d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
2043d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  case Stmt::CompoundStmtClass: {
2044d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    const CompoundStmt *CS = cast<CompoundStmt>(S);
2045d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    for (CompoundStmt::const_body_iterator BI = CS->body_begin(),
2046d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith           BE = CS->body_end(); BI != BE; ++BI) {
2047d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith      EvalStmtResult ESR = EvaluateStmt(Result, Info, *BI);
2048d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith      if (ESR != ESR_Succeeded)
2049d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith        return ESR;
2050d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    }
2051d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    return ESR_Succeeded;
2052d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  }
2053d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  }
2054d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith}
2055d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
20566180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith/// CheckTrivialDefaultConstructor - Check whether a constructor is a trivial
20576180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith/// default constructor. If so, we'll fold it whether or not it's marked as
20586180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith/// constexpr. If it is marked as constexpr, we will never implicitly define it,
20596180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith/// so we need special handling.
20606180245e9f63d2927b185ec251fb75aba30f1cacRichard Smithstatic bool CheckTrivialDefaultConstructor(EvalInfo &Info, SourceLocation Loc,
206151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith                                           const CXXConstructorDecl *CD,
206251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith                                           bool IsValueInitialization) {
20636180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith  if (!CD->isTrivial() || !CD->isDefaultConstructor())
20646180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    return false;
20656180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith
20664c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  // Value-initialization does not call a trivial default constructor, so such a
20674c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  // call is a core constant expression whether or not the constructor is
20684c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  // constexpr.
20694c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  if (!CD->isConstexpr() && !IsValueInitialization) {
20706180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    if (Info.getLangOpts().CPlusPlus0x) {
20714c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith      // FIXME: If DiagDecl is an implicitly-declared special member function,
20724c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith      // we should be much more explicit about why it's not constexpr.
20734c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith      Info.CCEDiag(Loc, diag::note_constexpr_invalid_function, 1)
20744c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith        << /*IsConstexpr*/0 << /*IsConstructor*/1 << CD;
20754c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith      Info.Note(CD->getLocation(), diag::note_declared_at);
20766180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    } else {
20776180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith      Info.CCEDiag(Loc, diag::note_invalid_subexpr_in_const_expr);
20786180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    }
20796180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith  }
20806180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith  return true;
20816180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith}
20826180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith
2083c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith/// CheckConstexprFunction - Check that a function can be called in a constant
2084c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith/// expression.
2085c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstatic bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc,
2086c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                   const FunctionDecl *Declaration,
2087c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                   const FunctionDecl *Definition) {
2088745f5147e065900267c85a5568785a1991d4838fRichard Smith  // Potential constant expressions can contain calls to declared, but not yet
2089745f5147e065900267c85a5568785a1991d4838fRichard Smith  // defined, constexpr functions.
2090745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (Info.CheckingPotentialConstantExpression && !Definition &&
2091745f5147e065900267c85a5568785a1991d4838fRichard Smith      Declaration->isConstexpr())
2092745f5147e065900267c85a5568785a1991d4838fRichard Smith    return false;
2093745f5147e065900267c85a5568785a1991d4838fRichard Smith
2094c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  // Can we evaluate this function call?
2095c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  if (Definition && Definition->isConstexpr() && !Definition->isInvalidDecl())
2096c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return true;
2097c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
2098c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  if (Info.getLangOpts().CPlusPlus0x) {
2099c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    const FunctionDecl *DiagDecl = Definition ? Definition : Declaration;
2100099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    // FIXME: If DiagDecl is an implicitly-declared special member function, we
2101099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    // should be much more explicit about why it's not constexpr.
2102c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    Info.Diag(CallLoc, diag::note_constexpr_invalid_function, 1)
2103c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      << DiagDecl->isConstexpr() << isa<CXXConstructorDecl>(DiagDecl)
2104c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      << DiagDecl;
2105c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    Info.Note(DiagDecl->getLocation(), diag::note_declared_at);
2106c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  } else {
2107c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    Info.Diag(CallLoc, diag::note_invalid_subexpr_in_const_expr);
2108c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  }
2109c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  return false;
2110c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
2111c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
2112180f47959a066795cc0f409433023af448bb0328Richard Smithnamespace {
21131aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smithtypedef SmallVector<APValue, 8> ArgVector;
2114180f47959a066795cc0f409433023af448bb0328Richard Smith}
2115180f47959a066795cc0f409433023af448bb0328Richard Smith
2116180f47959a066795cc0f409433023af448bb0328Richard Smith/// EvaluateArgs - Evaluate the arguments to a function call.
2117180f47959a066795cc0f409433023af448bb0328Richard Smithstatic bool EvaluateArgs(ArrayRef<const Expr*> Args, ArgVector &ArgValues,
2118180f47959a066795cc0f409433023af448bb0328Richard Smith                         EvalInfo &Info) {
2119745f5147e065900267c85a5568785a1991d4838fRichard Smith  bool Success = true;
2120180f47959a066795cc0f409433023af448bb0328Richard Smith  for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end();
2121745f5147e065900267c85a5568785a1991d4838fRichard Smith       I != E; ++I) {
2122745f5147e065900267c85a5568785a1991d4838fRichard Smith    if (!Evaluate(ArgValues[I - Args.begin()], Info, *I)) {
2123745f5147e065900267c85a5568785a1991d4838fRichard Smith      // If we're checking for a potential constant expression, evaluate all
2124745f5147e065900267c85a5568785a1991d4838fRichard Smith      // initializers even if some of them fail.
2125745f5147e065900267c85a5568785a1991d4838fRichard Smith      if (!Info.keepEvaluatingAfterFailure())
2126745f5147e065900267c85a5568785a1991d4838fRichard Smith        return false;
2127745f5147e065900267c85a5568785a1991d4838fRichard Smith      Success = false;
2128745f5147e065900267c85a5568785a1991d4838fRichard Smith    }
2129745f5147e065900267c85a5568785a1991d4838fRichard Smith  }
2130745f5147e065900267c85a5568785a1991d4838fRichard Smith  return Success;
2131180f47959a066795cc0f409433023af448bb0328Richard Smith}
2132180f47959a066795cc0f409433023af448bb0328Richard Smith
2133d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith/// Evaluate a function call.
2134745f5147e065900267c85a5568785a1991d4838fRichard Smithstatic bool HandleFunctionCall(SourceLocation CallLoc,
2135745f5147e065900267c85a5568785a1991d4838fRichard Smith                               const FunctionDecl *Callee, const LValue *This,
2136f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                               ArrayRef<const Expr*> Args, const Stmt *Body,
21371aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith                               EvalInfo &Info, APValue &Result) {
2138180f47959a066795cc0f409433023af448bb0328Richard Smith  ArgVector ArgValues(Args.size());
2139180f47959a066795cc0f409433023af448bb0328Richard Smith  if (!EvaluateArgs(Args, ArgValues, Info))
2140180f47959a066795cc0f409433023af448bb0328Richard Smith    return false;
2141d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
2142745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!Info.CheckCallLimit(CallLoc))
2143745f5147e065900267c85a5568785a1991d4838fRichard Smith    return false;
2144745f5147e065900267c85a5568785a1991d4838fRichard Smith
2145745f5147e065900267c85a5568785a1991d4838fRichard Smith  CallStackFrame Frame(Info, CallLoc, Callee, This, ArgValues.data());
2146d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  return EvaluateStmt(Result, Info, Body) == ESR_Returned;
2147d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith}
2148d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
2149180f47959a066795cc0f409433023af448bb0328Richard Smith/// Evaluate a constructor call.
2150745f5147e065900267c85a5568785a1991d4838fRichard Smithstatic bool HandleConstructorCall(SourceLocation CallLoc, const LValue &This,
215159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith                                  ArrayRef<const Expr*> Args,
2152180f47959a066795cc0f409433023af448bb0328Richard Smith                                  const CXXConstructorDecl *Definition,
215351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith                                  EvalInfo &Info, APValue &Result) {
2154180f47959a066795cc0f409433023af448bb0328Richard Smith  ArgVector ArgValues(Args.size());
2155180f47959a066795cc0f409433023af448bb0328Richard Smith  if (!EvaluateArgs(Args, ArgValues, Info))
2156180f47959a066795cc0f409433023af448bb0328Richard Smith    return false;
2157180f47959a066795cc0f409433023af448bb0328Richard Smith
2158745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!Info.CheckCallLimit(CallLoc))
2159745f5147e065900267c85a5568785a1991d4838fRichard Smith    return false;
2160745f5147e065900267c85a5568785a1991d4838fRichard Smith
216186c3ae46250cdcc57778c27826060779a92f3815Richard Smith  const CXXRecordDecl *RD = Definition->getParent();
216286c3ae46250cdcc57778c27826060779a92f3815Richard Smith  if (RD->getNumVBases()) {
216386c3ae46250cdcc57778c27826060779a92f3815Richard Smith    Info.Diag(CallLoc, diag::note_constexpr_virtual_base) << RD;
216486c3ae46250cdcc57778c27826060779a92f3815Richard Smith    return false;
216586c3ae46250cdcc57778c27826060779a92f3815Richard Smith  }
216686c3ae46250cdcc57778c27826060779a92f3815Richard Smith
2167745f5147e065900267c85a5568785a1991d4838fRichard Smith  CallStackFrame Frame(Info, CallLoc, Definition, &This, ArgValues.data());
2168180f47959a066795cc0f409433023af448bb0328Richard Smith
2169180f47959a066795cc0f409433023af448bb0328Richard Smith  // If it's a delegating constructor, just delegate.
2170180f47959a066795cc0f409433023af448bb0328Richard Smith  if (Definition->isDelegatingConstructor()) {
2171180f47959a066795cc0f409433023af448bb0328Richard Smith    CXXConstructorDecl::init_const_iterator I = Definition->init_begin();
217283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return EvaluateInPlace(Result, Info, This, (*I)->getInit());
2173180f47959a066795cc0f409433023af448bb0328Richard Smith  }
2174180f47959a066795cc0f409433023af448bb0328Richard Smith
2175610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith  // For a trivial copy or move constructor, perform an APValue copy. This is
2176610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith  // essential for unions, where the operations performed by the constructor
2177610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith  // cannot be represented by ctor-initializers.
2178610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith  if (Definition->isDefaulted() &&
2179f6cfe8ba2b4d98c20181568e449edf0b60904b03Douglas Gregor      ((Definition->isCopyConstructor() && Definition->isTrivial()) ||
2180f6cfe8ba2b4d98c20181568e449edf0b60904b03Douglas Gregor       (Definition->isMoveConstructor() && Definition->isTrivial()))) {
2181610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith    LValue RHS;
21821aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    RHS.setFrom(Info.Ctx, ArgValues[0]);
21831aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    return HandleLValueToRValueConversion(Info, Args[0], Args[0]->getType(),
21841aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith                                          RHS, Result);
2185610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith  }
2186610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith
2187610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith  // Reserve space for the struct members.
218851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (!RD->isUnion() && Result.isUninit())
2189180f47959a066795cc0f409433023af448bb0328Richard Smith    Result = APValue(APValue::UninitStruct(), RD->getNumBases(),
2190180f47959a066795cc0f409433023af448bb0328Richard Smith                     std::distance(RD->field_begin(), RD->field_end()));
2191180f47959a066795cc0f409433023af448bb0328Richard Smith
2192180f47959a066795cc0f409433023af448bb0328Richard Smith  const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD);
2193180f47959a066795cc0f409433023af448bb0328Richard Smith
2194745f5147e065900267c85a5568785a1991d4838fRichard Smith  bool Success = true;
2195180f47959a066795cc0f409433023af448bb0328Richard Smith  unsigned BasesSeen = 0;
2196180f47959a066795cc0f409433023af448bb0328Richard Smith#ifndef NDEBUG
2197180f47959a066795cc0f409433023af448bb0328Richard Smith  CXXRecordDecl::base_class_const_iterator BaseIt = RD->bases_begin();
2198180f47959a066795cc0f409433023af448bb0328Richard Smith#endif
2199180f47959a066795cc0f409433023af448bb0328Richard Smith  for (CXXConstructorDecl::init_const_iterator I = Definition->init_begin(),
2200180f47959a066795cc0f409433023af448bb0328Richard Smith       E = Definition->init_end(); I != E; ++I) {
2201745f5147e065900267c85a5568785a1991d4838fRichard Smith    LValue Subobject = This;
2202745f5147e065900267c85a5568785a1991d4838fRichard Smith    APValue *Value = &Result;
2203745f5147e065900267c85a5568785a1991d4838fRichard Smith
2204745f5147e065900267c85a5568785a1991d4838fRichard Smith    // Determine the subobject to initialize.
2205180f47959a066795cc0f409433023af448bb0328Richard Smith    if ((*I)->isBaseInitializer()) {
2206180f47959a066795cc0f409433023af448bb0328Richard Smith      QualType BaseType((*I)->getBaseClass(), 0);
2207180f47959a066795cc0f409433023af448bb0328Richard Smith#ifndef NDEBUG
2208180f47959a066795cc0f409433023af448bb0328Richard Smith      // Non-virtual base classes are initialized in the order in the class
220986c3ae46250cdcc57778c27826060779a92f3815Richard Smith      // definition. We have already checked for virtual base classes.
2210180f47959a066795cc0f409433023af448bb0328Richard Smith      assert(!BaseIt->isVirtual() && "virtual base for literal type");
2211180f47959a066795cc0f409433023af448bb0328Richard Smith      assert(Info.Ctx.hasSameType(BaseIt->getType(), BaseType) &&
2212180f47959a066795cc0f409433023af448bb0328Richard Smith             "base class initializers not in expected order");
2213180f47959a066795cc0f409433023af448bb0328Richard Smith      ++BaseIt;
2214180f47959a066795cc0f409433023af448bb0328Richard Smith#endif
2215b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      HandleLValueDirectBase(Info, (*I)->getInit(), Subobject, RD,
2216180f47959a066795cc0f409433023af448bb0328Richard Smith                             BaseType->getAsCXXRecordDecl(), &Layout);
2217745f5147e065900267c85a5568785a1991d4838fRichard Smith      Value = &Result.getStructBase(BasesSeen++);
2218180f47959a066795cc0f409433023af448bb0328Richard Smith    } else if (FieldDecl *FD = (*I)->getMember()) {
2219b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      HandleLValueMember(Info, (*I)->getInit(), Subobject, FD, &Layout);
2220180f47959a066795cc0f409433023af448bb0328Richard Smith      if (RD->isUnion()) {
2221180f47959a066795cc0f409433023af448bb0328Richard Smith        Result = APValue(FD);
2222745f5147e065900267c85a5568785a1991d4838fRichard Smith        Value = &Result.getUnionValue();
2223745f5147e065900267c85a5568785a1991d4838fRichard Smith      } else {
2224745f5147e065900267c85a5568785a1991d4838fRichard Smith        Value = &Result.getStructField(FD->getFieldIndex());
2225745f5147e065900267c85a5568785a1991d4838fRichard Smith      }
2226d9b02e726262e4009dda830998bb934172ac0020Richard Smith    } else if (IndirectFieldDecl *IFD = (*I)->getIndirectMember()) {
2227d9b02e726262e4009dda830998bb934172ac0020Richard Smith      // Walk the indirect field decl's chain to find the object to initialize,
2228d9b02e726262e4009dda830998bb934172ac0020Richard Smith      // and make sure we've initialized every step along it.
2229d9b02e726262e4009dda830998bb934172ac0020Richard Smith      for (IndirectFieldDecl::chain_iterator C = IFD->chain_begin(),
2230d9b02e726262e4009dda830998bb934172ac0020Richard Smith                                             CE = IFD->chain_end();
2231d9b02e726262e4009dda830998bb934172ac0020Richard Smith           C != CE; ++C) {
2232d9b02e726262e4009dda830998bb934172ac0020Richard Smith        FieldDecl *FD = cast<FieldDecl>(*C);
2233d9b02e726262e4009dda830998bb934172ac0020Richard Smith        CXXRecordDecl *CD = cast<CXXRecordDecl>(FD->getParent());
2234d9b02e726262e4009dda830998bb934172ac0020Richard Smith        // Switch the union field if it differs. This happens if we had
2235d9b02e726262e4009dda830998bb934172ac0020Richard Smith        // preceding zero-initialization, and we're now initializing a union
2236d9b02e726262e4009dda830998bb934172ac0020Richard Smith        // subobject other than the first.
2237d9b02e726262e4009dda830998bb934172ac0020Richard Smith        // FIXME: In this case, the values of the other subobjects are
2238d9b02e726262e4009dda830998bb934172ac0020Richard Smith        // specified, since zero-initialization sets all padding bits to zero.
2239d9b02e726262e4009dda830998bb934172ac0020Richard Smith        if (Value->isUninit() ||
2240d9b02e726262e4009dda830998bb934172ac0020Richard Smith            (Value->isUnion() && Value->getUnionField() != FD)) {
2241d9b02e726262e4009dda830998bb934172ac0020Richard Smith          if (CD->isUnion())
2242d9b02e726262e4009dda830998bb934172ac0020Richard Smith            *Value = APValue(FD);
2243d9b02e726262e4009dda830998bb934172ac0020Richard Smith          else
2244d9b02e726262e4009dda830998bb934172ac0020Richard Smith            *Value = APValue(APValue::UninitStruct(), CD->getNumBases(),
2245d9b02e726262e4009dda830998bb934172ac0020Richard Smith                             std::distance(CD->field_begin(), CD->field_end()));
2246d9b02e726262e4009dda830998bb934172ac0020Richard Smith        }
2247745f5147e065900267c85a5568785a1991d4838fRichard Smith        HandleLValueMember(Info, (*I)->getInit(), Subobject, FD);
2248d9b02e726262e4009dda830998bb934172ac0020Richard Smith        if (CD->isUnion())
2249d9b02e726262e4009dda830998bb934172ac0020Richard Smith          Value = &Value->getUnionValue();
2250d9b02e726262e4009dda830998bb934172ac0020Richard Smith        else
2251d9b02e726262e4009dda830998bb934172ac0020Richard Smith          Value = &Value->getStructField(FD->getFieldIndex());
2252d9b02e726262e4009dda830998bb934172ac0020Richard Smith      }
2253180f47959a066795cc0f409433023af448bb0328Richard Smith    } else {
2254d9b02e726262e4009dda830998bb934172ac0020Richard Smith      llvm_unreachable("unknown base initializer kind");
2255180f47959a066795cc0f409433023af448bb0328Richard Smith    }
2256745f5147e065900267c85a5568785a1991d4838fRichard Smith
225783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    if (!EvaluateInPlace(*Value, Info, Subobject, (*I)->getInit(),
225883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                         (*I)->isBaseInitializer()
2259745f5147e065900267c85a5568785a1991d4838fRichard Smith                                      ? CCEK_Constant : CCEK_MemberInit)) {
2260745f5147e065900267c85a5568785a1991d4838fRichard Smith      // If we're checking for a potential constant expression, evaluate all
2261745f5147e065900267c85a5568785a1991d4838fRichard Smith      // initializers even if some of them fail.
2262745f5147e065900267c85a5568785a1991d4838fRichard Smith      if (!Info.keepEvaluatingAfterFailure())
2263745f5147e065900267c85a5568785a1991d4838fRichard Smith        return false;
2264745f5147e065900267c85a5568785a1991d4838fRichard Smith      Success = false;
2265745f5147e065900267c85a5568785a1991d4838fRichard Smith    }
2266180f47959a066795cc0f409433023af448bb0328Richard Smith  }
2267180f47959a066795cc0f409433023af448bb0328Richard Smith
2268745f5147e065900267c85a5568785a1991d4838fRichard Smith  return Success;
2269180f47959a066795cc0f409433023af448bb0328Richard Smith}
2270180f47959a066795cc0f409433023af448bb0328Richard Smith
2271d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smithnamespace {
2272770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass HasSideEffect
22738cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  : public ConstStmtVisitor<HasSideEffect, bool> {
22741e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith  const ASTContext &Ctx;
2275c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stumppublic:
2276c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump
22771e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith  HasSideEffect(const ASTContext &C) : Ctx(C) {}
2278c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump
2279c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump  // Unhandled nodes conservatively default to having side effects.
22808cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitStmt(const Stmt *S) {
2281c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump    return true;
2282c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump  }
2283c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump
22848cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitParenExpr(const ParenExpr *E) { return Visit(E->getSubExpr()); }
22858cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitGenericSelectionExpr(const GenericSelectionExpr *E) {
2286f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return Visit(E->getResultExpr());
2287f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  }
22888cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitDeclRefExpr(const DeclRefExpr *E) {
22891e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    if (Ctx.getCanonicalType(E->getType()).isVolatileQualified())
2290c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump      return true;
2291c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump    return false;
2292c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump  }
2293f85e193739c953358c865005855253af4f68a497John McCall  bool VisitObjCIvarRefExpr(const ObjCIvarRefExpr *E) {
22941e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    if (Ctx.getCanonicalType(E->getType()).isVolatileQualified())
2295f85e193739c953358c865005855253af4f68a497John McCall      return true;
2296f85e193739c953358c865005855253af4f68a497John McCall    return false;
2297f85e193739c953358c865005855253af4f68a497John McCall  }
2298f85e193739c953358c865005855253af4f68a497John McCall
2299c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump  // We don't want to evaluate BlockExprs multiple times, as they generate
2300c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump  // a ton of code.
23018cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitBlockExpr(const BlockExpr *E) { return true; }
23028cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitPredefinedExpr(const PredefinedExpr *E) { return false; }
23038cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E)
2304c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump    { return Visit(E->getInitializer()); }
23058cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitMemberExpr(const MemberExpr *E) { return Visit(E->getBase()); }
23068cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitIntegerLiteral(const IntegerLiteral *E) { return false; }
23078cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitFloatingLiteral(const FloatingLiteral *E) { return false; }
23088cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitStringLiteral(const StringLiteral *E) { return false; }
23098cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCharacterLiteral(const CharacterLiteral *E) { return false; }
23108cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E)
2311f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    { return false; }
23128cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E)
2313980ca220848d27ef55ef4c2d37423461a8ed0da3Mike Stump    { return Visit(E->getLHS()) || Visit(E->getRHS()); }
23148cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitChooseExpr(const ChooseExpr *E)
23151e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    { return Visit(E->getChosenSubExpr(Ctx)); }
23168cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCastExpr(const CastExpr *E) { return Visit(E->getSubExpr()); }
23178cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitBinAssign(const BinaryOperator *E) { return true; }
23188cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCompoundAssignOperator(const BinaryOperator *E) { return true; }
23198cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitBinaryOperator(const BinaryOperator *E)
2320980ca220848d27ef55ef4c2d37423461a8ed0da3Mike Stump  { return Visit(E->getLHS()) || Visit(E->getRHS()); }
23218cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitUnaryPreInc(const UnaryOperator *E) { return true; }
23228cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitUnaryPostInc(const UnaryOperator *E) { return true; }
23238cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitUnaryPreDec(const UnaryOperator *E) { return true; }
23248cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitUnaryPostDec(const UnaryOperator *E) { return true; }
23258cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitUnaryDeref(const UnaryOperator *E) {
23261e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    if (Ctx.getCanonicalType(E->getType()).isVolatileQualified())
2327c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump      return true;
2328980ca220848d27ef55ef4c2d37423461a8ed0da3Mike Stump    return Visit(E->getSubExpr());
2329c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump  }
23308cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitUnaryOperator(const UnaryOperator *E) { return Visit(E->getSubExpr()); }
2331363ff23cfddb51abe4ee4212a6dd3c9b534fcc8bChris Lattner
2332363ff23cfddb51abe4ee4212a6dd3c9b534fcc8bChris Lattner  // Has side effects if any element does.
23338cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitInitListExpr(const InitListExpr *E) {
2334363ff23cfddb51abe4ee4212a6dd3c9b534fcc8bChris Lattner    for (unsigned i = 0, e = E->getNumInits(); i != e; ++i)
2335363ff23cfddb51abe4ee4212a6dd3c9b534fcc8bChris Lattner      if (Visit(E->getInit(i))) return true;
23368cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    if (const Expr *filler = E->getArrayFiller())
23374423ac0282acb8ba801eb05b38712438dc0c1e3eArgyrios Kyrtzidis      return Visit(filler);
2338363ff23cfddb51abe4ee4212a6dd3c9b534fcc8bChris Lattner    return false;
2339363ff23cfddb51abe4ee4212a6dd3c9b534fcc8bChris Lattner  }
2340ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor
23418cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitSizeOfPackExpr(const SizeOfPackExpr *) { return false; }
2342c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump};
2343c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump
234456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCallclass OpaqueValueEvaluation {
234556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  EvalInfo &info;
234656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  OpaqueValueExpr *opaqueValue;
234756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
234856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCallpublic:
234956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  OpaqueValueEvaluation(EvalInfo &info, OpaqueValueExpr *opaqueValue,
235056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall                        Expr *value)
235156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    : info(info), opaqueValue(opaqueValue) {
235256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
235356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    // If evaluation fails, fail immediately.
23541e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    if (!Evaluate(info.OpaqueValues[opaqueValue], info, value)) {
235556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      this->opaqueValue = 0;
235656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      return;
235756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    }
235856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  }
235956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
236056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  bool hasError() const { return opaqueValue == 0; }
236156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
236256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  ~OpaqueValueEvaluation() {
236374e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    // FIXME: For a recursive constexpr call, an outer stack frame might have
236474e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    // been using this opaque value too, and will now have to re-evaluate the
236574e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    // source expression.
236656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    if (opaqueValue) info.OpaqueValues.erase(opaqueValue);
236756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  }
236856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall};
236956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
2370c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump} // end anonymous namespace
2371c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump
23724efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman//===----------------------------------------------------------------------===//
23738cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne// Generic Evaluation
23748cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne//===----------------------------------------------------------------------===//
23758cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournenamespace {
23768cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
2377f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith// FIXME: RetTy is always bool. Remove it.
2378f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithtemplate <class Derived, typename RetTy=bool>
23798cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourneclass ExprEvaluatorBase
23808cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  : public ConstStmtVisitor<Derived, RetTy> {
23818cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourneprivate:
23821aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  RetTy DerivedSuccess(const APValue &V, const Expr *E) {
23838cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    return static_cast<Derived*>(this)->Success(V, E);
23848cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  }
238551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  RetTy DerivedZeroInitialization(const Expr *E) {
238651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return static_cast<Derived*>(this)->ZeroInitialization(E);
2387f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  }
23888cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
238974e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  // Check whether a conditional operator with a non-constant condition is a
239074e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  // potential constant expression. If neither arm is a potential constant
239174e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  // expression, then the conditional operator is not either.
239274e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  template<typename ConditionalOperator>
239374e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  void CheckPotentialConstantConditional(const ConditionalOperator *E) {
239474e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    assert(Info.CheckingPotentialConstantExpression);
239574e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith
239674e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    // Speculatively evaluate both arms.
239774e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    {
239874e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith      llvm::SmallVector<PartialDiagnosticAt, 8> Diag;
239974e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith      SpeculativeEvaluationRAII Speculate(Info, &Diag);
240074e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith
240174e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith      StmtVisitorTy::Visit(E->getFalseExpr());
240274e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith      if (Diag.empty())
240374e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith        return;
240474e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith
240574e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith      Diag.clear();
240674e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith      StmtVisitorTy::Visit(E->getTrueExpr());
240774e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith      if (Diag.empty())
240874e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith        return;
240974e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    }
241074e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith
241174e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    Error(E, diag::note_constexpr_conditional_never_const);
241274e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  }
241374e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith
241474e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith
241574e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  template<typename ConditionalOperator>
241674e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  bool HandleConditionalOperator(const ConditionalOperator *E) {
241774e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    bool BoolResult;
241874e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) {
241974e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith      if (Info.CheckingPotentialConstantExpression)
242074e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith        CheckPotentialConstantConditional(E);
242174e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith      return false;
242274e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    }
242374e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith
242474e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
242574e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    return StmtVisitorTy::Visit(EvalExpr);
242674e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith  }
242774e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith
24288cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourneprotected:
24298cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  EvalInfo &Info;
24308cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  typedef ConstStmtVisitor<Derived, RetTy> StmtVisitorTy;
24318cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  typedef ExprEvaluatorBase ExprEvaluatorBaseTy;
24328cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
2433dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith  OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) {
24345cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    return Info.CCEDiag(E, D);
2435f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
2436f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
2437cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  RetTy ZeroInitialization(const Expr *E) { return Error(E); }
2438cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
2439cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidispublic:
2440cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {}
2441cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
2442cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  EvalInfo &getEvalInfo() { return Info; }
2443cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
2444f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  /// Report an evaluation error. This should only be called when an error is
2445f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  /// first discovered. When propagating an error, just return false.
2446f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  bool Error(const Expr *E, diag::kind D) {
24475cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.Diag(E, D);
2448f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return false;
2449f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
2450f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  bool Error(const Expr *E) {
2451f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E, diag::note_invalid_subexpr_in_const_expr);
2452f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
2453f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
24548cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitStmt(const Stmt *) {
2455b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Expression evaluator should not be called on stmts");
24568cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  }
24578cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitExpr(const Expr *E) {
2458f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
24598cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  }
24608cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
24618cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitParenExpr(const ParenExpr *E)
24628cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    { return StmtVisitorTy::Visit(E->getSubExpr()); }
24638cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitUnaryExtension(const UnaryOperator *E)
24648cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    { return StmtVisitorTy::Visit(E->getSubExpr()); }
24658cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitUnaryPlus(const UnaryOperator *E)
24668cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    { return StmtVisitorTy::Visit(E->getSubExpr()); }
24678cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitChooseExpr(const ChooseExpr *E)
24688cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    { return StmtVisitorTy::Visit(E->getChosenSubExpr(Info.Ctx)); }
24698cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitGenericSelectionExpr(const GenericSelectionExpr *E)
24708cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    { return StmtVisitorTy::Visit(E->getResultExpr()); }
247191a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall  RetTy VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E)
247291a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall    { return StmtVisitorTy::Visit(E->getReplacement()); }
24733d75ca836205856077c18e30e9447accbd85f751Richard Smith  RetTy VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E)
24743d75ca836205856077c18e30e9447accbd85f751Richard Smith    { return StmtVisitorTy::Visit(E->getExpr()); }
2475bc6abe93a5d6b1305411f8b6f54c2caa686ddc69Richard Smith  // We cannot create any objects for which cleanups are required, so there is
2476bc6abe93a5d6b1305411f8b6f54c2caa686ddc69Richard Smith  // nothing to do here; all cleanups must come from unevaluated subexpressions.
2477bc6abe93a5d6b1305411f8b6f54c2caa686ddc69Richard Smith  RetTy VisitExprWithCleanups(const ExprWithCleanups *E)
2478bc6abe93a5d6b1305411f8b6f54c2caa686ddc69Richard Smith    { return StmtVisitorTy::Visit(E->getSubExpr()); }
24798cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
2480c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith  RetTy VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E) {
2481c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith    CCEDiag(E, diag::note_constexpr_invalid_cast) << 0;
2482c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith    return static_cast<Derived*>(this)->VisitCastExpr(E);
2483c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith  }
2484c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith  RetTy VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) {
2485c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith    CCEDiag(E, diag::note_constexpr_invalid_cast) << 1;
2486c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith    return static_cast<Derived*>(this)->VisitCastExpr(E);
2487c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith  }
2488c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith
2489e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  RetTy VisitBinaryOperator(const BinaryOperator *E) {
2490e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    switch (E->getOpcode()) {
2491e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    default:
2492f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E);
2493e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2494e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    case BO_Comma:
2495e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      VisitIgnoredValue(E->getLHS());
2496e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return StmtVisitorTy::Visit(E->getRHS());
2497e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2498e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    case BO_PtrMemD:
2499e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    case BO_PtrMemI: {
2500e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      LValue Obj;
2501e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!HandleMemberPointerAccess(Info, E, Obj))
2502e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
25031aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith      APValue Result;
2504f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      if (!HandleLValueToRValueConversion(Info, E, E->getType(), Obj, Result))
2505e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
2506e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return DerivedSuccess(Result, E);
2507e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
2508e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
2509e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
2510e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
25118cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) {
251274e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    // Cache the value of the common expression.
25138cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    OpaqueValueEvaluation opaque(Info, E->getOpaqueValue(), E->getCommon());
25148cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    if (opaque.hasError())
2515f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return false;
25168cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
251774e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    return HandleConditionalOperator(E);
25188cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  }
25198cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
25208cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitConditionalOperator(const ConditionalOperator *E) {
2521f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    bool IsBcpCall = false;
2522f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    // If the condition (ignoring parens) is a __builtin_constant_p call,
2523f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    // the result is a constant expression if it can be folded without
2524f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    // side-effects. This is an important GNU extension. See GCC PR38377
2525f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    // for discussion.
2526f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    if (const CallExpr *CallCE =
2527f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          dyn_cast<CallExpr>(E->getCond()->IgnoreParenCasts()))
2528f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p)
2529f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        IsBcpCall = true;
2530f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
2531f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    // Always assume __builtin_constant_p(...) ? ... : ... is a potential
2532f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    // constant expression; we can't check whether it's potentially foldable.
2533f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    if (Info.CheckingPotentialConstantExpression && IsBcpCall)
2534f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      return false;
2535f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
2536f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    FoldConstant Fold(Info);
2537f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
253874e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    if (!HandleConditionalOperator(E))
2539f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      return false;
2540f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
2541f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    if (IsBcpCall)
2542f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      Fold.Fold(Info);
2543f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
2544f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    return true;
25458cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  }
25468cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
25478cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitOpaqueValueExpr(const OpaqueValueExpr *E) {
25481aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    const APValue *Value = Info.getOpaqueValue(E);
254942786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis    if (!Value) {
255042786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis      const Expr *Source = E->getSourceExpr();
255142786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis      if (!Source)
2552f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(E);
255342786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis      if (Source == E) { // sanity checking.
255442786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis        assert(0 && "OpaqueValueExpr recursively refers to itself");
2555f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(E);
255642786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis      }
255742786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis      return StmtVisitorTy::Visit(Source);
255842786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis    }
255947a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    return DerivedSuccess(*Value, E);
25608cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  }
2561f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith
2562d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  RetTy VisitCallExpr(const CallExpr *E) {
2563e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    const Expr *Callee = E->getCallee()->IgnoreParens();
2564d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    QualType CalleeType = Callee->getType();
2565d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
25666142ca7790aa09a6e13592b70f142cc4bbcadcaeDevang Patel    const FunctionDecl *FD = 0;
256759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    LValue *This = 0, ThisVal;
256859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs());
256986c3ae46250cdcc57778c27826060779a92f3815Richard Smith    bool HasQualifier = false;
257059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
257159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    // Extract function decl and 'this' pointer from the callee.
257259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    if (CalleeType->isSpecificBuiltinType(BuiltinType::BoundMember)) {
2573f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      const ValueDecl *Member = 0;
2574e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (const MemberExpr *ME = dyn_cast<MemberExpr>(Callee)) {
2575e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        // Explicit bound member calls, such as x.f() or p->g();
2576e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        if (!EvaluateObjectArgument(Info, ME->getBase(), ThisVal))
2577f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith          return false;
2578f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        Member = ME->getMemberDecl();
2579e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        This = &ThisVal;
258086c3ae46250cdcc57778c27826060779a92f3815Richard Smith        HasQualifier = ME->hasQualifier();
2581e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(Callee)) {
2582e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        // Indirect bound member calls ('.*' or '->*').
2583f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        Member = HandleMemberPointerAccess(Info, BE, ThisVal, false);
2584f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        if (!Member) return false;
2585e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        This = &ThisVal;
2586e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      } else
2587f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(Callee);
2588f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
2589f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      FD = dyn_cast<FunctionDecl>(Member);
2590f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      if (!FD)
2591f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(Callee);
259259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    } else if (CalleeType->isFunctionPointerType()) {
2593b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      LValue Call;
2594b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      if (!EvaluatePointer(Callee, Call, Info))
2595f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return false;
259659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
2597b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      if (!Call.getLValueOffset().isZero())
2598f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(Callee);
25991bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith      FD = dyn_cast_or_null<FunctionDecl>(
26001bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith                             Call.getLValueBase().dyn_cast<const ValueDecl*>());
260159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      if (!FD)
2602f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(Callee);
260359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
260459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      // Overloaded operator calls to member functions are represented as normal
260559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      // calls with '*this' as the first argument.
260659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
260759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      if (MD && !MD->isStatic()) {
2608f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        // FIXME: When selecting an implicit conversion for an overloaded
2609f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        // operator delete, we sometimes try to evaluate calls to conversion
2610f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        // operators without a 'this' parameter!
2611f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        if (Args.empty())
2612f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith          return Error(E);
2613f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
261459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith        if (!EvaluateObjectArgument(Info, Args[0], ThisVal))
261559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith          return false;
261659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith        This = &ThisVal;
261759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith        Args = Args.slice(1);
261859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      }
2619d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
262059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      // Don't call function pointers which have been cast to some other type.
262159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      if (!Info.Ctx.hasSameType(CalleeType->getPointeeType(), FD->getType()))
2622f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(E);
262359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    } else
2624f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E);
2625d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
2626b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith    if (This && !This->checkSubobject(Info, E, CSK_This))
2627b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith      return false;
2628b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith
262986c3ae46250cdcc57778c27826060779a92f3815Richard Smith    // DR1358 allows virtual constexpr functions in some cases. Don't allow
263086c3ae46250cdcc57778c27826060779a92f3815Richard Smith    // calls to such functions in constant expressions.
263186c3ae46250cdcc57778c27826060779a92f3815Richard Smith    if (This && !HasQualifier &&
263286c3ae46250cdcc57778c27826060779a92f3815Richard Smith        isa<CXXMethodDecl>(FD) && cast<CXXMethodDecl>(FD)->isVirtual())
263386c3ae46250cdcc57778c27826060779a92f3815Richard Smith      return Error(E, diag::note_constexpr_virtual_call);
263486c3ae46250cdcc57778c27826060779a92f3815Richard Smith
2635c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    const FunctionDecl *Definition = 0;
2636d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    Stmt *Body = FD->getBody(Definition);
26371aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    APValue Result;
2638d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
2639c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition) ||
2640745f5147e065900267c85a5568785a1991d4838fRichard Smith        !HandleFunctionCall(E->getExprLoc(), Definition, This, Args, Body,
2641745f5147e065900267c85a5568785a1991d4838fRichard Smith                            Info, Result))
2642f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return false;
2643d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
264483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return DerivedSuccess(Result, E);
2645d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  }
2646d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
2647c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  RetTy VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
2648c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return StmtVisitorTy::Visit(E->getInitializer());
2649c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  }
2650f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  RetTy VisitInitListExpr(const InitListExpr *E) {
265171523d6c41e1599fc42f420d02dd2895fd8f65d4Eli Friedman    if (E->getNumInits() == 0)
265271523d6c41e1599fc42f420d02dd2895fd8f65d4Eli Friedman      return DerivedZeroInitialization(E);
265371523d6c41e1599fc42f420d02dd2895fd8f65d4Eli Friedman    if (E->getNumInits() == 1)
265471523d6c41e1599fc42f420d02dd2895fd8f65d4Eli Friedman      return StmtVisitorTy::Visit(E->getInit(0));
2655f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
2656f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  }
2657f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  RetTy VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
265851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return DerivedZeroInitialization(E);
2659f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  }
2660f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  RetTy VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) {
266151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return DerivedZeroInitialization(E);
2662f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  }
2663e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  RetTy VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) {
266451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return DerivedZeroInitialization(E);
2665e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
2666f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith
2667180f47959a066795cc0f409433023af448bb0328Richard Smith  /// A member expression where the object is a prvalue is itself a prvalue.
2668180f47959a066795cc0f409433023af448bb0328Richard Smith  RetTy VisitMemberExpr(const MemberExpr *E) {
2669180f47959a066795cc0f409433023af448bb0328Richard Smith    assert(!E->isArrow() && "missing call to bound member function?");
2670180f47959a066795cc0f409433023af448bb0328Richard Smith
26711aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    APValue Val;
2672180f47959a066795cc0f409433023af448bb0328Richard Smith    if (!Evaluate(Val, Info, E->getBase()))
2673180f47959a066795cc0f409433023af448bb0328Richard Smith      return false;
2674180f47959a066795cc0f409433023af448bb0328Richard Smith
2675180f47959a066795cc0f409433023af448bb0328Richard Smith    QualType BaseTy = E->getBase()->getType();
2676180f47959a066795cc0f409433023af448bb0328Richard Smith
2677180f47959a066795cc0f409433023af448bb0328Richard Smith    const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
2678f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!FD) return Error(E);
2679180f47959a066795cc0f409433023af448bb0328Richard Smith    assert(!FD->getType()->isReferenceType() && "prvalue reference?");
2680180f47959a066795cc0f409433023af448bb0328Richard Smith    assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() ==
2681180f47959a066795cc0f409433023af448bb0328Richard Smith           FD->getParent()->getCanonicalDecl() && "record / field mismatch");
2682180f47959a066795cc0f409433023af448bb0328Richard Smith
2683b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    SubobjectDesignator Designator(BaseTy);
2684b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    Designator.addDeclUnchecked(FD);
2685180f47959a066795cc0f409433023af448bb0328Richard Smith
2686f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return ExtractSubobject(Info, E, Val, BaseTy, Designator, E->getType()) &&
2687180f47959a066795cc0f409433023af448bb0328Richard Smith           DerivedSuccess(Val, E);
2688180f47959a066795cc0f409433023af448bb0328Richard Smith  }
2689180f47959a066795cc0f409433023af448bb0328Richard Smith
2690c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  RetTy VisitCastExpr(const CastExpr *E) {
2691c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    switch (E->getCastKind()) {
2692c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    default:
2693c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith      break;
2694c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
26957a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall    case CK_AtomicToNonAtomic:
26967a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall    case CK_NonAtomicToAtomic:
2697c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    case CK_NoOp:
26987d580a4e9e47dffc3c17aa2b957ac57ca3c4e451Richard Smith    case CK_UserDefinedConversion:
2699c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith      return StmtVisitorTy::Visit(E->getSubExpr());
2700c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
2701c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    case CK_LValueToRValue: {
2702c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith      LValue LVal;
2703f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      if (!EvaluateLValue(E->getSubExpr(), LVal, Info))
2704f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return false;
27051aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith      APValue RVal;
27069ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith      // Note, we use the subexpression's type in order to retain cv-qualifiers.
27079ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith      if (!HandleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(),
27089ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith                                          LVal, RVal))
2709f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return false;
2710f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return DerivedSuccess(RVal, E);
2711c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    }
2712c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    }
2713c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
2714f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
2715c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  }
2716c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
27178327fad71da34492d82c532f42a58cb4baff81a3Richard Smith  /// Visit a value which is evaluated, but whose value is ignored.
27188327fad71da34492d82c532f42a58cb4baff81a3Richard Smith  void VisitIgnoredValue(const Expr *E) {
27191aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    APValue Scratch;
27208327fad71da34492d82c532f42a58cb4baff81a3Richard Smith    if (!Evaluate(Scratch, Info, E))
27218327fad71da34492d82c532f42a58cb4baff81a3Richard Smith      Info.EvalStatus.HasSideEffects = true;
27228327fad71da34492d82c532f42a58cb4baff81a3Richard Smith  }
27238cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne};
27248cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
27258cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne}
27268cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
27278cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne//===----------------------------------------------------------------------===//
2728e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// Common base class for lvalue and temporary evaluation.
2729e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===//
2730e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithnamespace {
2731e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithtemplate<class Derived>
2732e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithclass LValueExprEvaluatorBase
2733e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  : public ExprEvaluatorBase<Derived, bool> {
2734e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithprotected:
2735e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  LValue &Result;
2736e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  typedef LValueExprEvaluatorBase LValueExprEvaluatorBaseTy;
2737e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  typedef ExprEvaluatorBase<Derived, bool> ExprEvaluatorBaseTy;
2738e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2739e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool Success(APValue::LValueBase B) {
2740e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    Result.set(B);
2741e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
2742e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
2743e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2744e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithpublic:
2745e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  LValueExprEvaluatorBase(EvalInfo &Info, LValue &Result) :
2746e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    ExprEvaluatorBaseTy(Info), Result(Result) {}
2747e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
27481aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  bool Success(const APValue &V, const Expr *E) {
27491aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    Result.setFrom(this->Info.Ctx, V);
2750e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
2751e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
2752e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2753e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitMemberExpr(const MemberExpr *E) {
2754e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // Handle non-static data members.
2755e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    QualType BaseTy;
2756e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (E->isArrow()) {
2757e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!EvaluatePointer(E->getBase(), Result, this->Info))
2758e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
2759e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      BaseTy = E->getBase()->getType()->getAs<PointerType>()->getPointeeType();
2760c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    } else if (E->getBase()->isRValue()) {
2761af2c7a194592401394233b7cbcdd3cfd0a7a38ddRichard Smith      assert(E->getBase()->getType()->isRecordType());
2762c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      if (!EvaluateTemporary(E->getBase(), Result, this->Info))
2763c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith        return false;
2764c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      BaseTy = E->getBase()->getType();
2765e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    } else {
2766e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!this->Visit(E->getBase()))
2767e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
2768e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      BaseTy = E->getBase()->getType();
2769e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
2770e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2771d9b02e726262e4009dda830998bb934172ac0020Richard Smith    const ValueDecl *MD = E->getMemberDecl();
2772d9b02e726262e4009dda830998bb934172ac0020Richard Smith    if (const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
2773d9b02e726262e4009dda830998bb934172ac0020Richard Smith      assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() ==
2774d9b02e726262e4009dda830998bb934172ac0020Richard Smith             FD->getParent()->getCanonicalDecl() && "record / field mismatch");
2775d9b02e726262e4009dda830998bb934172ac0020Richard Smith      (void)BaseTy;
2776d9b02e726262e4009dda830998bb934172ac0020Richard Smith      HandleLValueMember(this->Info, E, Result, FD);
2777d9b02e726262e4009dda830998bb934172ac0020Richard Smith    } else if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(MD)) {
2778d9b02e726262e4009dda830998bb934172ac0020Richard Smith      HandleLValueIndirectMember(this->Info, E, Result, IFD);
2779d9b02e726262e4009dda830998bb934172ac0020Richard Smith    } else
2780d9b02e726262e4009dda830998bb934172ac0020Richard Smith      return this->Error(E);
2781e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2782d9b02e726262e4009dda830998bb934172ac0020Richard Smith    if (MD->getType()->isReferenceType()) {
27831aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith      APValue RefValue;
2784d9b02e726262e4009dda830998bb934172ac0020Richard Smith      if (!HandleLValueToRValueConversion(this->Info, E, MD->getType(), Result,
2785e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                                          RefValue))
2786e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
2787e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return Success(RefValue, E);
2788e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
2789e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
2790e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
2791e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2792e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitBinaryOperator(const BinaryOperator *E) {
2793e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    switch (E->getOpcode()) {
2794e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    default:
2795e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
2796e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2797e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    case BO_PtrMemD:
2798e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    case BO_PtrMemI:
2799e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return HandleMemberPointerAccess(this->Info, E, Result);
2800e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
2801e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
2802e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2803e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitCastExpr(const CastExpr *E) {
2804e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    switch (E->getCastKind()) {
2805e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    default:
2806e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return ExprEvaluatorBaseTy::VisitCastExpr(E);
2807e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2808e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    case CK_DerivedToBase:
2809e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    case CK_UncheckedDerivedToBase: {
2810e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!this->Visit(E->getSubExpr()))
2811e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
2812e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2813e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      // Now figure out the necessary offset to add to the base LV to get from
2814e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      // the derived class to the base class.
2815e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      QualType Type = E->getSubExpr()->getType();
2816e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2817e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      for (CastExpr::path_const_iterator PathI = E->path_begin(),
2818e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith           PathE = E->path_end(); PathI != PathE; ++PathI) {
2819b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        if (!HandleLValueBase(this->Info, E, Result, Type->getAsCXXRecordDecl(),
2820e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                              *PathI))
2821e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith          return false;
2822e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        Type = (*PathI)->getType();
2823e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      }
2824e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2825e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return true;
2826e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
2827e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
2828e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
2829e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith};
2830e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}
2831e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2832e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===//
28334efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman// LValue Evaluation
2834c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//
2835c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// This is used for evaluating lvalues (in C and C++), xvalues (in C++11),
2836c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// function designators (in C), decl references to void objects (in C), and
2837c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// temporaries (if building with -Wno-address-of-temporary).
2838c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//
2839c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// LValue evaluation produces values comprising a base expression of one of the
2840c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// following types:
28411bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// - Declarations
28421bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith//  * VarDecl
28431bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith//  * FunctionDecl
28441bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// - Literals
2845c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//  * CompoundLiteralExpr in C
2846c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//  * StringLiteral
284747d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith//  * CXXTypeidExpr
2848c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//  * PredefinedExpr
2849180f47959a066795cc0f409433023af448bb0328Richard Smith//  * ObjCStringLiteralExpr
2850c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//  * ObjCEncodeExpr
2851c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//  * AddrLabelExpr
2852c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//  * BlockExpr
2853c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//  * CallExpr for a MakeStringConstant builtin
28541bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// - Locals and temporaries
285583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith//  * Any Expr, with a CallIndex indicating the function in which the temporary
285683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith//    was evaluated.
28571bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// plus an offset in bytes.
28584efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman//===----------------------------------------------------------------------===//
28594efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedmannamespace {
2860770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass LValueExprEvaluator
2861e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  : public LValueExprEvaluatorBase<LValueExprEvaluator> {
28624efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedmanpublic:
2863e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  LValueExprEvaluator(EvalInfo &Info, LValue &Result) :
2864e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    LValueExprEvaluatorBaseTy(Info, Result) {}
28651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2866c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  bool VisitVarDecl(const Expr *E, const VarDecl *VD);
2867c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
28688cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitDeclRefExpr(const DeclRefExpr *E);
28698cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(E); }
2870bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith  bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E);
28718cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
28728cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitMemberExpr(const MemberExpr *E);
28738cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitStringLiteral(const StringLiteral *E) { return Success(E); }
28748cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(E); }
287547d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith  bool VisitCXXTypeidExpr(const CXXTypeidExpr *E);
2876e275a1845b9e32bd3034f2593dee1780855c8fd6Francois Pichet  bool VisitCXXUuidofExpr(const CXXUuidofExpr *E);
28778cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E);
28788cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitUnaryDeref(const UnaryOperator *E);
287986024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  bool VisitUnaryReal(const UnaryOperator *E);
288086024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  bool VisitUnaryImag(const UnaryOperator *E);
28818cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
28828cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCastExpr(const CastExpr *E) {
288326bc220377705292a0519a71d3ea3aef68fcfec6Anders Carlsson    switch (E->getCastKind()) {
288426bc220377705292a0519a71d3ea3aef68fcfec6Anders Carlsson    default:
2885e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return LValueExprEvaluatorBaseTy::VisitCastExpr(E);
288626bc220377705292a0519a71d3ea3aef68fcfec6Anders Carlsson
2887db924224b51b153f24fbe492102d4edebcbbb7f4Eli Friedman    case CK_LValueBitCast:
2888c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith      this->CCEDiag(E, diag::note_constexpr_invalid_cast) << 2;
28890a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      if (!Visit(E->getSubExpr()))
28900a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith        return false;
28910a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      Result.Designator.setInvalid();
28920a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      return true;
2893db924224b51b153f24fbe492102d4edebcbbb7f4Eli Friedman
2894e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    case CK_BaseToDerived:
2895180f47959a066795cc0f409433023af448bb0328Richard Smith      if (!Visit(E->getSubExpr()))
2896180f47959a066795cc0f409433023af448bb0328Richard Smith        return false;
2897e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return HandleBaseToDerivedCast(Info, E, Result);
289826bc220377705292a0519a71d3ea3aef68fcfec6Anders Carlsson    }
289926bc220377705292a0519a71d3ea3aef68fcfec6Anders Carlsson  }
29004efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman};
29014efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman} // end anonymous namespace
29024efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
2903c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// Evaluate an expression as an lvalue. This can be legitimately called on
2904c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// expressions which are not glvalues, in a few cases:
2905c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith///  * function designators in C,
2906c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith///  * "extern void" objects,
2907c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith///  * temporaries, if building with -Wno-address-of-temporary.
2908efdb83e26f9a1fd2566afe54461216cd84814d42John McCallstatic bool EvaluateLValue(const Expr* E, LValue& Result, EvalInfo &Info) {
2909c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  assert((E->isGLValue() || E->getType()->isFunctionType() ||
2910c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith          E->getType()->isVoidType() || isa<CXXTemporaryObjectExpr>(E)) &&
2911c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith         "can't evaluate expression as an lvalue");
29128cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  return LValueExprEvaluator(Info, Result).Visit(E);
29134efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman}
29144efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
29158cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
29161bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl()))
29171bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    return Success(FD);
29181bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  if (const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl()))
2919c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return VisitVarDecl(E, VD);
2920c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  return Error(E);
2921c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith}
2922436c8898cd1c93c5bacd3fcc4ac586bc5cd77062Richard Smith
2923c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smithbool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) {
2924177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith  if (!VD->getType()->isReferenceType()) {
2925177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith    if (isa<ParmVarDecl>(VD)) {
292683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      Result.set(VD, Info.CurrentCall->Index);
2927177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith      return true;
2928177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith    }
29291bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    return Success(VD);
2930177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith  }
293150c39ea4858265f3f5f42a0c624557ce2281936bEli Friedman
29321aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  APValue V;
2933f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (!EvaluateVarDeclInit(Info, E, VD, Info.CurrentCall, V))
2934f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return false;
2935f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  return Success(V, E);
293635873c49adad211ff466e34342a52665742794f5Anders Carlsson}
293735873c49adad211ff466e34342a52665742794f5Anders Carlsson
2938bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smithbool LValueExprEvaluator::VisitMaterializeTemporaryExpr(
2939bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    const MaterializeTemporaryExpr *E) {
2940e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (E->GetTemporaryExpr()->isRValue()) {
2941af2c7a194592401394233b7cbcdd3cfd0a7a38ddRichard Smith    if (E->getType()->isRecordType())
2942e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return EvaluateTemporary(E->GetTemporaryExpr(), Result, Info);
2943e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
294483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    Result.set(E, Info.CurrentCall->Index);
294583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return EvaluateInPlace(Info.CurrentCall->Temporaries[E], Info,
294683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                           Result, E->GetTemporaryExpr());
2947e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
2948e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2949e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // Materialization of an lvalue temporary occurs when we need to force a copy
2950e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // (for instance, if it's a bitfield).
2951e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // FIXME: The AST should contain an lvalue-to-rvalue node for such cases.
2952e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (!Visit(E->GetTemporaryExpr()))
2953e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return false;
2954f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (!HandleLValueToRValueConversion(Info, E, E->getType(), Result,
2955e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                                      Info.CurrentCall->Temporaries[E]))
2956e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return false;
295783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  Result.set(E, Info.CurrentCall->Index);
2958e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  return true;
2959bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith}
2960bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith
29618cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool
29628cad3046be06ea73ff8892d947697a21d7a440d3Peter CollingbourneLValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
2963c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?");
2964c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  // Defer visiting the literal until the lvalue-to-rvalue conversion. We can
2965c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  // only see this when folding in C, so there's no standard to follow here.
2966efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  return Success(E);
29674efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman}
29684efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
296947d2145675099893d702be4bc06bd9f26d8ddd13Richard Smithbool LValueExprEvaluator::VisitCXXTypeidExpr(const CXXTypeidExpr *E) {
297047d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith  if (E->isTypeOperand())
297147d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith    return Success(E);
297247d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith  CXXRecordDecl *RD = E->getExprOperand()->getType()->getAsCXXRecordDecl();
297347d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith  if (RD && RD->isPolymorphic()) {
29745cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.Diag(E, diag::note_constexpr_typeid_polymorphic)
297547d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith      << E->getExprOperand()->getType()
297647d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith      << E->getExprOperand()->getSourceRange();
297747d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith    return false;
297847d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith  }
297947d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith  return Success(E);
298047d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith}
298147d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith
2982e275a1845b9e32bd3034f2593dee1780855c8fd6Francois Pichetbool LValueExprEvaluator::VisitCXXUuidofExpr(const CXXUuidofExpr *E) {
2983e275a1845b9e32bd3034f2593dee1780855c8fd6Francois Pichet  return Success(E);
2984e275a1845b9e32bd3034f2593dee1780855c8fd6Francois Pichet}
2985e275a1845b9e32bd3034f2593dee1780855c8fd6Francois Pichet
29868cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) {
2987c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  // Handle static data members.
2988c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  if (const VarDecl *VD = dyn_cast<VarDecl>(E->getMemberDecl())) {
2989c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    VisitIgnoredValue(E->getBase());
2990c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return VisitVarDecl(E, VD);
2991c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  }
2992c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
2993d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  // Handle static member functions.
2994d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) {
2995d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    if (MD->isStatic()) {
2996d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith      VisitIgnoredValue(E->getBase());
29971bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith      return Success(MD);
2998d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    }
2999d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  }
3000d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
3001180f47959a066795cc0f409433023af448bb0328Richard Smith  // Handle non-static data members.
3002e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  return LValueExprEvaluatorBaseTy::VisitMemberExpr(E);
30034efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman}
30044efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
30058cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) {
3006c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  // FIXME: Deal with vectors as array subscript bases.
3007c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  if (E->getBase()->getType()->isVectorType())
3008f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
3009c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
30103068d117951a8df54bae9db039b56201ab10962bAnders Carlsson  if (!EvaluatePointer(E->getBase(), Result, Info))
3011efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    return false;
30121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30133068d117951a8df54bae9db039b56201ab10962bAnders Carlsson  APSInt Index;
30143068d117951a8df54bae9db039b56201ab10962bAnders Carlsson  if (!EvaluateInteger(E->getIdx(), Index, Info))
3015efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    return false;
3016180f47959a066795cc0f409433023af448bb0328Richard Smith  int64_t IndexValue
3017180f47959a066795cc0f409433023af448bb0328Richard Smith    = Index.isSigned() ? Index.getSExtValue()
3018180f47959a066795cc0f409433023af448bb0328Richard Smith                       : static_cast<int64_t>(Index.getZExtValue());
30193068d117951a8df54bae9db039b56201ab10962bAnders Carlsson
3020b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  return HandleLValueArrayAdjustment(Info, E, Result, E->getType(), IndexValue);
30213068d117951a8df54bae9db039b56201ab10962bAnders Carlsson}
30224efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
30238cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) {
3024efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  return EvaluatePointer(E->getSubExpr(), Result, Info);
3025e8761c8fe2ee6b628104a0885f49fd3c21c08a4fEli Friedman}
3026e8761c8fe2ee6b628104a0885f49fd3c21c08a4fEli Friedman
302786024013d4c3728122c58fa07a2a67e6c15837efRichard Smithbool LValueExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
302886024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  if (!Visit(E->getSubExpr()))
302986024013d4c3728122c58fa07a2a67e6c15837efRichard Smith    return false;
303086024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  // __real is a no-op on scalar lvalues.
303186024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  if (E->getSubExpr()->getType()->isAnyComplexType())
303286024013d4c3728122c58fa07a2a67e6c15837efRichard Smith    HandleLValueComplexElement(Info, E, Result, E->getType(), false);
303386024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  return true;
303486024013d4c3728122c58fa07a2a67e6c15837efRichard Smith}
303586024013d4c3728122c58fa07a2a67e6c15837efRichard Smith
303686024013d4c3728122c58fa07a2a67e6c15837efRichard Smithbool LValueExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
303786024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  assert(E->getSubExpr()->getType()->isAnyComplexType() &&
303886024013d4c3728122c58fa07a2a67e6c15837efRichard Smith         "lvalue __imag__ on scalar?");
303986024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  if (!Visit(E->getSubExpr()))
304086024013d4c3728122c58fa07a2a67e6c15837efRichard Smith    return false;
304186024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  HandleLValueComplexElement(Info, E, Result, E->getType(), true);
304286024013d4c3728122c58fa07a2a67e6c15837efRichard Smith  return true;
304386024013d4c3728122c58fa07a2a67e6c15837efRichard Smith}
304486024013d4c3728122c58fa07a2a67e6c15837efRichard Smith
30454efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman//===----------------------------------------------------------------------===//
3046f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner// Pointer Evaluation
3047f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===//
3048f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
3049c754aa62643e66ab967ca32ae8b0b3fc419bba25Anders Carlssonnamespace {
3050770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass PointerExprEvaluator
30518cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  : public ExprEvaluatorBase<PointerExprEvaluator, bool> {
3052efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  LValue &Result;
3053efdb83e26f9a1fd2566afe54461216cd84814d42John McCall
30548cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool Success(const Expr *E) {
30551bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    Result.set(E);
3056efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    return true;
3057efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  }
30582bad1687fe6f00e10767a691a33b070b151902b6Anders Carlssonpublic:
30591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3060efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  PointerExprEvaluator(EvalInfo &info, LValue &Result)
30618cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    : ExprEvaluatorBaseTy(info), Result(Result) {}
3062f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
30631aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  bool Success(const APValue &V, const Expr *E) {
30641aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    Result.setFrom(Info.Ctx, V);
30658cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    return true;
30662bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson  }
306751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  bool ZeroInitialization(const Expr *E) {
3068f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith    return Success((Expr*)0);
3069f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  }
30702bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson
3071efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  bool VisitBinaryOperator(const BinaryOperator *E);
30728cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCastExpr(const CastExpr* E);
3073efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  bool VisitUnaryAddrOf(const UnaryOperator *E);
30748cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitObjCStringLiteral(const ObjCStringLiteral *E)
3075efdb83e26f9a1fd2566afe54461216cd84814d42John McCall      { return Success(E); }
3076eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  bool VisitObjCBoxedExpr(const ObjCBoxedExpr *E)
3077ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      { return Success(E); }
30788cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitAddrLabelExpr(const AddrLabelExpr *E)
3079efdb83e26f9a1fd2566afe54461216cd84814d42John McCall      { return Success(E); }
30808cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCallExpr(const CallExpr *E);
30818cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitBlockExpr(const BlockExpr *E) {
3082469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall    if (!E->getBlockDecl()->hasCaptures())
3083efdb83e26f9a1fd2566afe54461216cd84814d42John McCall      return Success(E);
3084f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
3085b83d287bc7f47d36fb0751a481e2ef9308b37252Mike Stump  }
3086180f47959a066795cc0f409433023af448bb0328Richard Smith  bool VisitCXXThisExpr(const CXXThisExpr *E) {
3087180f47959a066795cc0f409433023af448bb0328Richard Smith    if (!Info.CurrentCall->This)
3088f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E);
3089180f47959a066795cc0f409433023af448bb0328Richard Smith    Result = *Info.CurrentCall->This;
3090180f47959a066795cc0f409433023af448bb0328Richard Smith    return true;
3091180f47959a066795cc0f409433023af448bb0328Richard Smith  }
309256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
3093ba98d6bb414861965a1f22628494ea046785ecd4Eli Friedman  // FIXME: Missing: @protocol, @selector
30942bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson};
3095f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner} // end anonymous namespace
30962bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson
3097efdb83e26f9a1fd2566afe54461216cd84814d42John McCallstatic bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info) {
3098c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  assert(E->isRValue() && E->getType()->hasPointerRepresentation());
30998cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  return PointerExprEvaluator(Info, Result).Visit(E);
3100f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner}
3101650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson
3102efdb83e26f9a1fd2566afe54461216cd84814d42John McCallbool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
31032de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  if (E->getOpcode() != BO_Add &&
31042de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      E->getOpcode() != BO_Sub)
3105e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
31061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3107650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson  const Expr *PExp = E->getLHS();
3108650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson  const Expr *IExp = E->getRHS();
3109650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson  if (IExp->getType()->isPointerType())
3110f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner    std::swap(PExp, IExp);
31111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3112745f5147e065900267c85a5568785a1991d4838fRichard Smith  bool EvalPtrOK = EvaluatePointer(PExp, Result, Info);
3113745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!EvalPtrOK && !Info.keepEvaluatingAfterFailure())
3114efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    return false;
31151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3116efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  llvm::APSInt Offset;
3117745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!EvaluateInteger(IExp, Offset, Info) || !EvalPtrOK)
3118efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    return false;
3119efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  int64_t AdditionalOffset
3120efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    = Offset.isSigned() ? Offset.getSExtValue()
3121efdb83e26f9a1fd2566afe54461216cd84814d42John McCall                        : static_cast<int64_t>(Offset.getZExtValue());
31220a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith  if (E->getOpcode() == BO_Sub)
31230a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    AdditionalOffset = -AdditionalOffset;
3124650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson
3125180f47959a066795cc0f409433023af448bb0328Richard Smith  QualType Pointee = PExp->getType()->getAs<PointerType>()->getPointeeType();
3126b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  return HandleLValueArrayAdjustment(Info, E, Result, Pointee,
3127b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                                     AdditionalOffset);
3128650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson}
31294efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
3130efdb83e26f9a1fd2566afe54461216cd84814d42John McCallbool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
3131efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  return EvaluateLValue(E->getSubExpr(), Result, Info);
31324efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman}
31331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
31348cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
31358cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  const Expr* SubExpr = E->getSubExpr();
3136650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson
313709a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman  switch (E->getCastKind()) {
313809a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman  default:
313909a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman    break;
314009a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman
31412de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_BitCast:
31421d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_CPointerToObjCPointerCast:
31431d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_BlockPointerToObjCPointerCast:
31442de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_AnyPointerToBlockPointerCast:
314528c1ce789322ab99f9b5887015d63ec5f088957aRichard Smith    if (!Visit(SubExpr))
314628c1ce789322ab99f9b5887015d63ec5f088957aRichard Smith      return false;
3147c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith    // Bitcasts to cv void* are static_casts, not reinterpret_casts, so are
3148c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith    // permitted in constant expressions in C++11. Bitcasts from cv void* are
3149c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith    // also static_casts, but we disallow them as a resolution to DR1312.
31504cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith    if (!E->getType()->isVoidPointerType()) {
315128c1ce789322ab99f9b5887015d63ec5f088957aRichard Smith      Result.Designator.setInvalid();
31524cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith      if (SubExpr->getType()->isVoidPointerType())
31534cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith        CCEDiag(E, diag::note_constexpr_invalid_cast)
31544cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith          << 3 << SubExpr->getType();
31554cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith      else
31564cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith        CCEDiag(E, diag::note_constexpr_invalid_cast) << 2;
31574cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith    }
31580a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    return true;
315909a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman
31605c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson  case CK_DerivedToBase:
31615c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson  case CK_UncheckedDerivedToBase: {
316247a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    if (!EvaluatePointer(E->getSubExpr(), Result, Info))
31635c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson      return false;
3164e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (!Result.Base && Result.Offset.isZero())
3165e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return true;
31665c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson
3167180f47959a066795cc0f409433023af448bb0328Richard Smith    // Now figure out the necessary offset to add to the base LV to get from
31685c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson    // the derived class to the base class.
3169180f47959a066795cc0f409433023af448bb0328Richard Smith    QualType Type =
3170180f47959a066795cc0f409433023af448bb0328Richard Smith        E->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType();
31715c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson
3172180f47959a066795cc0f409433023af448bb0328Richard Smith    for (CastExpr::path_const_iterator PathI = E->path_begin(),
31735c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson         PathE = E->path_end(); PathI != PathE; ++PathI) {
3174b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      if (!HandleLValueBase(Info, E, Result, Type->getAsCXXRecordDecl(),
3175b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                            *PathI))
31765c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson        return false;
3177180f47959a066795cc0f409433023af448bb0328Richard Smith      Type = (*PathI)->getType();
31785c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson    }
31795c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson
31805c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson    return true;
31815c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson  }
31825c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson
3183e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  case CK_BaseToDerived:
3184e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (!Visit(E->getSubExpr()))
3185e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return false;
3186e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (!Result.Base && Result.Offset.isZero())
3187e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return true;
3188e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return HandleBaseToDerivedCast(Info, E, Result);
3189e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
319047a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith  case CK_NullToPointer:
319149149fe0d2be06ce1ceed1e9d2548a0b75a59c47Richard Smith    VisitIgnoredValue(E->getSubExpr());
319251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return ZeroInitialization(E);
3193404cd1669c3ba138a9ae0a619bd689cce5aae271John McCall
31942de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_IntegralToPointer: {
3195c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith    CCEDiag(E, diag::note_constexpr_invalid_cast) << 2;
3196c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith
31971aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    APValue Value;
3198efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    if (!EvaluateIntegerOrLValue(SubExpr, Value, Info))
319909a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman      break;
320069ab26a8623141f35e86817cfc6e0fbe7639a40fDaniel Dunbar
3201efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    if (Value.isInt()) {
320247a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith      unsigned Size = Info.Ctx.getTypeSize(E->getType());
320347a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith      uint64_t N = Value.getInt().extOrTrunc(Size).getZExtValue();
32041bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith      Result.Base = (Expr*)0;
320547a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith      Result.Offset = CharUnits::fromQuantity(N);
320683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      Result.CallIndex = 0;
32070a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      Result.Designator.setInvalid();
3208efdb83e26f9a1fd2566afe54461216cd84814d42John McCall      return true;
3209efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    } else {
3210efdb83e26f9a1fd2566afe54461216cd84814d42John McCall      // Cast is of an lvalue, no need to change value.
32111aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith      Result.setFrom(Info.Ctx, Value);
3212efdb83e26f9a1fd2566afe54461216cd84814d42John McCall      return true;
3213650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson    }
3214650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson  }
32152de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_ArrayToPointerDecay:
3216e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (SubExpr->isGLValue()) {
3217e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!EvaluateLValue(SubExpr, Result, Info))
3218e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
3219e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    } else {
322083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      Result.set(SubExpr, Info.CurrentCall->Index);
322183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      if (!EvaluateInPlace(Info.CurrentCall->Temporaries[SubExpr],
322283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                           Info, Result, SubExpr))
3223e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
3224e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
32250a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    // The result is a pointer to the first element of the array.
3226b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    if (const ConstantArrayType *CAT
3227b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith          = Info.Ctx.getAsConstantArrayType(SubExpr->getType()))
3228b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      Result.addArray(Info, E, CAT);
3229b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    else
3230b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      Result.Designator.setInvalid();
32310a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    return true;
32326a7c94af983717e2c2d6aebe42cb4737c1c7b9e6Richard Smith
32332de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_FunctionToPointerDecay:
32346a7c94af983717e2c2d6aebe42cb4737c1c7b9e6Richard Smith    return EvaluateLValue(SubExpr, Result, Info);
32354efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman  }
32364efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
3237c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  return ExprEvaluatorBaseTy::VisitCastExpr(E);
32381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
3239650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson
32408cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) {
3241180f47959a066795cc0f409433023af448bb0328Richard Smith  if (IsStringLiteralCall(E))
3242efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    return Success(E);
324356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
32448cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  return ExprEvaluatorBaseTy::VisitCallExpr(E);
32454efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman}
3246f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
3247f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===//
3248e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// Member Pointer Evaluation
3249e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===//
3250e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3251e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithnamespace {
3252e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithclass MemberPointerExprEvaluator
3253e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  : public ExprEvaluatorBase<MemberPointerExprEvaluator, bool> {
3254e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  MemberPtr &Result;
3255e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3256e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool Success(const ValueDecl *D) {
3257e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    Result = MemberPtr(D);
3258e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
3259e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
3260e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithpublic:
3261e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3262e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  MemberPointerExprEvaluator(EvalInfo &Info, MemberPtr &Result)
3263e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    : ExprEvaluatorBaseTy(Info), Result(Result) {}
3264e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
32651aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  bool Success(const APValue &V, const Expr *E) {
3266e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    Result.setFrom(V);
3267e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
3268e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
326951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  bool ZeroInitialization(const Expr *E) {
3270e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return Success((const ValueDecl*)0);
3271e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
3272e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3273e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitCastExpr(const CastExpr *E);
3274e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitUnaryAddrOf(const UnaryOperator *E);
3275e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith};
3276e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} // end anonymous namespace
3277e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3278e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result,
3279e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                                  EvalInfo &Info) {
3280e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  assert(E->isRValue() && E->getType()->isMemberPointerType());
3281e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  return MemberPointerExprEvaluator(Info, Result).Visit(E);
3282e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}
3283e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3284e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithbool MemberPointerExprEvaluator::VisitCastExpr(const CastExpr *E) {
3285e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  switch (E->getCastKind()) {
3286e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  default:
3287e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return ExprEvaluatorBaseTy::VisitCastExpr(E);
3288e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3289e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  case CK_NullToMemberPointer:
329049149fe0d2be06ce1ceed1e9d2548a0b75a59c47Richard Smith    VisitIgnoredValue(E->getSubExpr());
329151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return ZeroInitialization(E);
3292e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3293e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  case CK_BaseToDerivedMemberPointer: {
3294e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (!Visit(E->getSubExpr()))
3295e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return false;
3296e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (E->path_empty())
3297e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return true;
3298e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // Base-to-derived member pointer casts store the path in derived-to-base
3299e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // order, so iterate backwards. The CXXBaseSpecifier also provides us with
3300e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // the wrong end of the derived->base arc, so stagger the path by one class.
3301e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter;
3302e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin());
3303e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith         PathI != PathE; ++PathI) {
3304e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      assert(!(*PathI)->isVirtual() && "memptr cast through vbase");
3305e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      const CXXRecordDecl *Derived = (*PathI)->getType()->getAsCXXRecordDecl();
3306e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!Result.castToDerived(Derived))
3307f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(E);
3308e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
3309e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    const Type *FinalTy = E->getType()->castAs<MemberPointerType>()->getClass();
3310e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (!Result.castToDerived(FinalTy->getAsCXXRecordDecl()))
3311f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E);
3312e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
3313e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
3314e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3315e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  case CK_DerivedToBaseMemberPointer:
3316e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (!Visit(E->getSubExpr()))
3317e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return false;
3318e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    for (CastExpr::path_const_iterator PathI = E->path_begin(),
3319e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith         PathE = E->path_end(); PathI != PathE; ++PathI) {
3320e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      assert(!(*PathI)->isVirtual() && "memptr cast through vbase");
3321e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl();
3322e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!Result.castToBase(Base))
3323f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(E);
3324e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
3325e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
3326e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
3327e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}
3328e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3329e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithbool MemberPointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
3330e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // C++11 [expr.unary.op]p3 has very strict rules on how the address of a
3331e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // member can be formed.
3332e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  return Success(cast<DeclRefExpr>(E->getSubExpr())->getDecl());
3333e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}
3334e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3335e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===//
3336180f47959a066795cc0f409433023af448bb0328Richard Smith// Record Evaluation
3337180f47959a066795cc0f409433023af448bb0328Richard Smith//===----------------------------------------------------------------------===//
3338180f47959a066795cc0f409433023af448bb0328Richard Smith
3339180f47959a066795cc0f409433023af448bb0328Richard Smithnamespace {
3340180f47959a066795cc0f409433023af448bb0328Richard Smith  class RecordExprEvaluator
3341180f47959a066795cc0f409433023af448bb0328Richard Smith  : public ExprEvaluatorBase<RecordExprEvaluator, bool> {
3342180f47959a066795cc0f409433023af448bb0328Richard Smith    const LValue &This;
3343180f47959a066795cc0f409433023af448bb0328Richard Smith    APValue &Result;
3344180f47959a066795cc0f409433023af448bb0328Richard Smith  public:
3345180f47959a066795cc0f409433023af448bb0328Richard Smith
3346180f47959a066795cc0f409433023af448bb0328Richard Smith    RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result)
3347180f47959a066795cc0f409433023af448bb0328Richard Smith      : ExprEvaluatorBaseTy(info), This(This), Result(Result) {}
3348180f47959a066795cc0f409433023af448bb0328Richard Smith
33491aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    bool Success(const APValue &V, const Expr *E) {
335083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      Result = V;
335183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      return true;
3352180f47959a066795cc0f409433023af448bb0328Richard Smith    }
335351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    bool ZeroInitialization(const Expr *E);
3354180f47959a066795cc0f409433023af448bb0328Richard Smith
335559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    bool VisitCastExpr(const CastExpr *E);
3356180f47959a066795cc0f409433023af448bb0328Richard Smith    bool VisitInitListExpr(const InitListExpr *E);
3357180f47959a066795cc0f409433023af448bb0328Richard Smith    bool VisitCXXConstructExpr(const CXXConstructExpr *E);
3358180f47959a066795cc0f409433023af448bb0328Richard Smith  };
3359180f47959a066795cc0f409433023af448bb0328Richard Smith}
3360180f47959a066795cc0f409433023af448bb0328Richard Smith
336151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// Perform zero-initialization on an object of non-union class type.
336251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// C++11 [dcl.init]p5:
336351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith///  To zero-initialize an object or reference of type T means:
336451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith///    [...]
336551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith///    -- if T is a (possibly cv-qualified) non-union class type,
336651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith///       each non-static data member and each base-class subobject is
336751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith///       zero-initialized
3368b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E,
3369b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                                          const RecordDecl *RD,
337051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith                                          const LValue &This, APValue &Result) {
337151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  assert(!RD->isUnion() && "Expected non-union class type");
337251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD);
337351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  Result = APValue(APValue::UninitStruct(), CD ? CD->getNumBases() : 0,
337451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith                   std::distance(RD->field_begin(), RD->field_end()));
337551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
337651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD);
337751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
337851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (CD) {
337951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    unsigned Index = 0;
338051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(),
3381b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith           End = CD->bases_end(); I != End; ++I, ++Index) {
338251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      const CXXRecordDecl *Base = I->getType()->getAsCXXRecordDecl();
338351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      LValue Subobject = This;
3384b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      HandleLValueDirectBase(Info, E, Subobject, CD, Base, &Layout);
3385b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      if (!HandleClassZeroInitialization(Info, E, Base, Subobject,
338651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith                                         Result.getStructBase(Index)))
338751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith        return false;
338851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    }
338951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  }
339051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
3391b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  for (RecordDecl::field_iterator I = RD->field_begin(), End = RD->field_end();
3392b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith       I != End; ++I) {
339351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    // -- if T is a reference type, no initialization is performed.
339451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    if ((*I)->getType()->isReferenceType())
339551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      continue;
339651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
339751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    LValue Subobject = This;
3398b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    HandleLValueMember(Info, E, Subobject, *I, &Layout);
339951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
340051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    ImplicitValueInitExpr VIE((*I)->getType());
340183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    if (!EvaluateInPlace(
340251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith          Result.getStructField((*I)->getFieldIndex()), Info, Subobject, &VIE))
340351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      return false;
340451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  }
340551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
340651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  return true;
340751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith}
340851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
340951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smithbool RecordExprEvaluator::ZeroInitialization(const Expr *E) {
341051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl();
341151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (RD->isUnion()) {
341251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the
341351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    // object's first non-static named data member is zero-initialized
341451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    RecordDecl::field_iterator I = RD->field_begin();
341551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    if (I == RD->field_end()) {
341651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      Result = APValue((const FieldDecl*)0);
341751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      return true;
341851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    }
341951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
342051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    LValue Subobject = This;
3421b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    HandleLValueMember(Info, E, Subobject, *I);
342251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    Result = APValue(*I);
342351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    ImplicitValueInitExpr VIE((*I)->getType());
342483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, &VIE);
342551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  }
342651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
3427ce582fe2a7aad8b14b3636ad9cac0a3b8bbb219bRichard Smith  if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->getNumVBases()) {
34285cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.Diag(E, diag::note_constexpr_virtual_base) << RD;
3429ce582fe2a7aad8b14b3636ad9cac0a3b8bbb219bRichard Smith    return false;
3430ce582fe2a7aad8b14b3636ad9cac0a3b8bbb219bRichard Smith  }
3431ce582fe2a7aad8b14b3636ad9cac0a3b8bbb219bRichard Smith
3432b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  return HandleClassZeroInitialization(Info, E, RD, This, Result);
343351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith}
343451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
343559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithbool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) {
343659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  switch (E->getCastKind()) {
343759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  default:
343859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    return ExprEvaluatorBaseTy::VisitCastExpr(E);
343959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
344059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  case CK_ConstructorConversion:
344159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    return Visit(E->getSubExpr());
344259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
344359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  case CK_DerivedToBase:
344459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  case CK_UncheckedDerivedToBase: {
34451aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    APValue DerivedObject;
3446f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!Evaluate(DerivedObject, Info, E->getSubExpr()))
344759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      return false;
3448f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!DerivedObject.isStruct())
3449f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E->getSubExpr());
345059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
345159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    // Derived-to-base rvalue conversion: just slice off the derived part.
345259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    APValue *Value = &DerivedObject;
345359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl();
345459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    for (CastExpr::path_const_iterator PathI = E->path_begin(),
345559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith         PathE = E->path_end(); PathI != PathE; ++PathI) {
345659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      assert(!(*PathI)->isVirtual() && "record rvalue with virtual base");
345759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl();
345859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      Value = &Value->getStructBase(getBaseIndex(RD, Base));
345959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      RD = Base;
346059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    }
346159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    Result = *Value;
346259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    return true;
346359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  }
346459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  }
346559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
346659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
3467180f47959a066795cc0f409433023af448bb0328Richard Smithbool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
346824fe798fffc1748d8bce1321af42981c3719cb85Sebastian Redl  // Cannot constant-evaluate std::initializer_list inits.
346924fe798fffc1748d8bce1321af42981c3719cb85Sebastian Redl  if (E->initializesStdInitializerList())
347024fe798fffc1748d8bce1321af42981c3719cb85Sebastian Redl    return false;
347124fe798fffc1748d8bce1321af42981c3719cb85Sebastian Redl
3472180f47959a066795cc0f409433023af448bb0328Richard Smith  const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl();
3473180f47959a066795cc0f409433023af448bb0328Richard Smith  const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD);
3474180f47959a066795cc0f409433023af448bb0328Richard Smith
3475180f47959a066795cc0f409433023af448bb0328Richard Smith  if (RD->isUnion()) {
3476ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    const FieldDecl *Field = E->getInitializedFieldInUnion();
3477ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    Result = APValue(Field);
3478ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    if (!Field)
3479180f47959a066795cc0f409433023af448bb0328Richard Smith      return true;
3480ec789163a42a7be654ac34aadb750b508954d53cRichard Smith
3481ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    // If the initializer list for a union does not contain any elements, the
3482ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    // first element of the union is value-initialized.
3483ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    ImplicitValueInitExpr VIE(Field->getType());
3484ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    const Expr *InitExpr = E->getNumInits() ? E->getInit(0) : &VIE;
3485ec789163a42a7be654ac34aadb750b508954d53cRichard Smith
3486180f47959a066795cc0f409433023af448bb0328Richard Smith    LValue Subobject = This;
3487ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    HandleLValueMember(Info, InitExpr, Subobject, Field, &Layout);
348883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, InitExpr);
3489180f47959a066795cc0f409433023af448bb0328Richard Smith  }
3490180f47959a066795cc0f409433023af448bb0328Richard Smith
3491180f47959a066795cc0f409433023af448bb0328Richard Smith  assert((!isa<CXXRecordDecl>(RD) || !cast<CXXRecordDecl>(RD)->getNumBases()) &&
3492180f47959a066795cc0f409433023af448bb0328Richard Smith         "initializer list for class with base classes");
3493180f47959a066795cc0f409433023af448bb0328Richard Smith  Result = APValue(APValue::UninitStruct(), 0,
3494180f47959a066795cc0f409433023af448bb0328Richard Smith                   std::distance(RD->field_begin(), RD->field_end()));
3495180f47959a066795cc0f409433023af448bb0328Richard Smith  unsigned ElementNo = 0;
3496745f5147e065900267c85a5568785a1991d4838fRichard Smith  bool Success = true;
3497180f47959a066795cc0f409433023af448bb0328Richard Smith  for (RecordDecl::field_iterator Field = RD->field_begin(),
3498180f47959a066795cc0f409433023af448bb0328Richard Smith       FieldEnd = RD->field_end(); Field != FieldEnd; ++Field) {
3499180f47959a066795cc0f409433023af448bb0328Richard Smith    // Anonymous bit-fields are not considered members of the class for
3500180f47959a066795cc0f409433023af448bb0328Richard Smith    // purposes of aggregate initialization.
3501180f47959a066795cc0f409433023af448bb0328Richard Smith    if (Field->isUnnamedBitfield())
3502180f47959a066795cc0f409433023af448bb0328Richard Smith      continue;
3503180f47959a066795cc0f409433023af448bb0328Richard Smith
3504180f47959a066795cc0f409433023af448bb0328Richard Smith    LValue Subobject = This;
3505180f47959a066795cc0f409433023af448bb0328Richard Smith
3506745f5147e065900267c85a5568785a1991d4838fRichard Smith    bool HaveInit = ElementNo < E->getNumInits();
3507745f5147e065900267c85a5568785a1991d4838fRichard Smith
3508745f5147e065900267c85a5568785a1991d4838fRichard Smith    // FIXME: Diagnostics here should point to the end of the initializer
3509745f5147e065900267c85a5568785a1991d4838fRichard Smith    // list, not the start.
3510745f5147e065900267c85a5568785a1991d4838fRichard Smith    HandleLValueMember(Info, HaveInit ? E->getInit(ElementNo) : E, Subobject,
3511745f5147e065900267c85a5568785a1991d4838fRichard Smith                       *Field, &Layout);
3512745f5147e065900267c85a5568785a1991d4838fRichard Smith
3513745f5147e065900267c85a5568785a1991d4838fRichard Smith    // Perform an implicit value-initialization for members beyond the end of
3514745f5147e065900267c85a5568785a1991d4838fRichard Smith    // the initializer list.
3515745f5147e065900267c85a5568785a1991d4838fRichard Smith    ImplicitValueInitExpr VIE(HaveInit ? Info.Ctx.IntTy : Field->getType());
3516745f5147e065900267c85a5568785a1991d4838fRichard Smith
351783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    if (!EvaluateInPlace(
3518745f5147e065900267c85a5568785a1991d4838fRichard Smith          Result.getStructField((*Field)->getFieldIndex()),
3519745f5147e065900267c85a5568785a1991d4838fRichard Smith          Info, Subobject, HaveInit ? E->getInit(ElementNo++) : &VIE)) {
3520745f5147e065900267c85a5568785a1991d4838fRichard Smith      if (!Info.keepEvaluatingAfterFailure())
3521180f47959a066795cc0f409433023af448bb0328Richard Smith        return false;
3522745f5147e065900267c85a5568785a1991d4838fRichard Smith      Success = false;
3523180f47959a066795cc0f409433023af448bb0328Richard Smith    }
3524180f47959a066795cc0f409433023af448bb0328Richard Smith  }
3525180f47959a066795cc0f409433023af448bb0328Richard Smith
3526745f5147e065900267c85a5568785a1991d4838fRichard Smith  return Success;
3527180f47959a066795cc0f409433023af448bb0328Richard Smith}
3528180f47959a066795cc0f409433023af448bb0328Richard Smith
3529180f47959a066795cc0f409433023af448bb0328Richard Smithbool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) {
3530180f47959a066795cc0f409433023af448bb0328Richard Smith  const CXXConstructorDecl *FD = E->getConstructor();
353151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  bool ZeroInit = E->requiresZeroInitialization();
353251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) {
3533ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    // If we've already performed zero-initialization, we're already done.
3534ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    if (!Result.isUninit())
3535ec789163a42a7be654ac34aadb750b508954d53cRichard Smith      return true;
3536ec789163a42a7be654ac34aadb750b508954d53cRichard Smith
353751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    if (ZeroInit)
353851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      return ZeroInitialization(E);
353951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
35406180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    const CXXRecordDecl *RD = FD->getParent();
35416180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    if (RD->isUnion())
35426180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith      Result = APValue((FieldDecl*)0);
35436180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    else
35446180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith      Result = APValue(APValue::UninitStruct(), RD->getNumBases(),
35456180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith                       std::distance(RD->field_begin(), RD->field_end()));
35466180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    return true;
35476180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith  }
35486180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith
3549180f47959a066795cc0f409433023af448bb0328Richard Smith  const FunctionDecl *Definition = 0;
3550180f47959a066795cc0f409433023af448bb0328Richard Smith  FD->getBody(Definition);
3551180f47959a066795cc0f409433023af448bb0328Richard Smith
3552c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition))
3553c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return false;
3554180f47959a066795cc0f409433023af448bb0328Richard Smith
3555610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith  // Avoid materializing a temporary for an elidable copy/move constructor.
355651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (E->isElidable() && !ZeroInit)
3557180f47959a066795cc0f409433023af448bb0328Richard Smith    if (const MaterializeTemporaryExpr *ME
3558180f47959a066795cc0f409433023af448bb0328Richard Smith          = dyn_cast<MaterializeTemporaryExpr>(E->getArg(0)))
3559180f47959a066795cc0f409433023af448bb0328Richard Smith      return Visit(ME->GetTemporaryExpr());
3560180f47959a066795cc0f409433023af448bb0328Richard Smith
356151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (ZeroInit && !ZeroInitialization(E))
356251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return false;
356351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
3564180f47959a066795cc0f409433023af448bb0328Richard Smith  llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs());
3565745f5147e065900267c85a5568785a1991d4838fRichard Smith  return HandleConstructorCall(E->getExprLoc(), This, Args,
3566f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                               cast<CXXConstructorDecl>(Definition), Info,
3567f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                               Result);
3568180f47959a066795cc0f409433023af448bb0328Richard Smith}
3569180f47959a066795cc0f409433023af448bb0328Richard Smith
3570180f47959a066795cc0f409433023af448bb0328Richard Smithstatic bool EvaluateRecord(const Expr *E, const LValue &This,
3571180f47959a066795cc0f409433023af448bb0328Richard Smith                           APValue &Result, EvalInfo &Info) {
3572180f47959a066795cc0f409433023af448bb0328Richard Smith  assert(E->isRValue() && E->getType()->isRecordType() &&
3573180f47959a066795cc0f409433023af448bb0328Richard Smith         "can't evaluate expression as a record rvalue");
3574180f47959a066795cc0f409433023af448bb0328Richard Smith  return RecordExprEvaluator(Info, This, Result).Visit(E);
3575180f47959a066795cc0f409433023af448bb0328Richard Smith}
3576180f47959a066795cc0f409433023af448bb0328Richard Smith
3577180f47959a066795cc0f409433023af448bb0328Richard Smith//===----------------------------------------------------------------------===//
3578e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// Temporary Evaluation
3579e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//
3580e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// Temporaries are represented in the AST as rvalues, but generally behave like
3581e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// lvalues. The full-object of which the temporary is a subobject is implicitly
3582e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// materialized so that a reference can bind to it.
3583e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===//
3584e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithnamespace {
3585e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithclass TemporaryExprEvaluator
3586e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  : public LValueExprEvaluatorBase<TemporaryExprEvaluator> {
3587e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithpublic:
3588e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  TemporaryExprEvaluator(EvalInfo &Info, LValue &Result) :
3589e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    LValueExprEvaluatorBaseTy(Info, Result) {}
3590e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3591e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  /// Visit an expression which constructs the value of this temporary.
3592e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitConstructExpr(const Expr *E) {
359383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    Result.set(E, Info.CurrentCall->Index);
359483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return EvaluateInPlace(Info.CurrentCall->Temporaries[E], Info, Result, E);
3595e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
3596e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3597e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitCastExpr(const CastExpr *E) {
3598e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    switch (E->getCastKind()) {
3599e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    default:
3600e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return LValueExprEvaluatorBaseTy::VisitCastExpr(E);
3601e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3602e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    case CK_ConstructorConversion:
3603e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return VisitConstructExpr(E->getSubExpr());
3604e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
3605e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
3606e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitInitListExpr(const InitListExpr *E) {
3607e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return VisitConstructExpr(E);
3608e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
3609e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitCXXConstructExpr(const CXXConstructExpr *E) {
3610e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return VisitConstructExpr(E);
3611e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
3612e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitCallExpr(const CallExpr *E) {
3613e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return VisitConstructExpr(E);
3614e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
3615e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith};
3616e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} // end anonymous namespace
3617e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3618e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// Evaluate an expression of record type as a temporary.
3619e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) {
3620af2c7a194592401394233b7cbcdd3cfd0a7a38ddRichard Smith  assert(E->isRValue() && E->getType()->isRecordType());
3621e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  return TemporaryExprEvaluator(Info, Result).Visit(E);
3622e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}
3623e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3624e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===//
362559b5da6d853b4368b984700315adf7b37de05764Nate Begeman// Vector Evaluation
362659b5da6d853b4368b984700315adf7b37de05764Nate Begeman//===----------------------------------------------------------------------===//
362759b5da6d853b4368b984700315adf7b37de05764Nate Begeman
362859b5da6d853b4368b984700315adf7b37de05764Nate Begemannamespace {
3629770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramer  class VectorExprEvaluator
363007fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith  : public ExprEvaluatorBase<VectorExprEvaluator, bool> {
363107fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    APValue &Result;
363259b5da6d853b4368b984700315adf7b37de05764Nate Begeman  public:
36331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
363407fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    VectorExprEvaluator(EvalInfo &info, APValue &Result)
363507fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith      : ExprEvaluatorBaseTy(info), Result(Result) {}
36361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
363707fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    bool Success(const ArrayRef<APValue> &V, const Expr *E) {
363807fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith      assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements());
363907fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith      // FIXME: remove this APValue copy.
364007fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith      Result = APValue(V.data(), V.size());
364107fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith      return true;
364207fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    }
36431aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    bool Success(const APValue &V, const Expr *E) {
364469c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith      assert(V.isVector());
364507fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith      Result = V;
364607fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith      return true;
364707fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    }
364851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    bool ZeroInitialization(const Expr *E);
36491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
365007fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    bool VisitUnaryReal(const UnaryOperator *E)
365191110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman      { return Visit(E->getSubExpr()); }
365207fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    bool VisitCastExpr(const CastExpr* E);
365307fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    bool VisitInitListExpr(const InitListExpr *E);
365407fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    bool VisitUnaryImag(const UnaryOperator *E);
365591110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman    // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
36562217c87bdc5ab357046a5453bdb06f469c41024eEli Friedman    //                 binary comparisons, binary and/or/xor,
365791110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman    //                 shufflevector, ExtVectorElementExpr
365859b5da6d853b4368b984700315adf7b37de05764Nate Begeman  };
365959b5da6d853b4368b984700315adf7b37de05764Nate Begeman} // end anonymous namespace
366059b5da6d853b4368b984700315adf7b37de05764Nate Begeman
366159b5da6d853b4368b984700315adf7b37de05764Nate Begemanstatic bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
3662c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  assert(E->isRValue() && E->getType()->isVectorType() &&"not a vector rvalue");
366307fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith  return VectorExprEvaluator(Info, Result).Visit(E);
366459b5da6d853b4368b984700315adf7b37de05764Nate Begeman}
366559b5da6d853b4368b984700315adf7b37de05764Nate Begeman
366607fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smithbool VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
366707fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith  const VectorType *VTy = E->getType()->castAs<VectorType>();
3668c0b8b19bd8056d6b5d831623a0825cce150f4507Nate Begeman  unsigned NElts = VTy->getNumElements();
36691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3670d62ca370b03b8c6ad58002d3399383baf744e32bRichard Smith  const Expr *SE = E->getSubExpr();
3671e8c9e9218f215ec6089f12b076c7b9d310fd5194Nate Begeman  QualType SETy = SE->getType();
367259b5da6d853b4368b984700315adf7b37de05764Nate Begeman
367346a523285928aa07bf14803178dc04616ac85994Eli Friedman  switch (E->getCastKind()) {
367446a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_VectorSplat: {
367507fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    APValue Val = APValue();
367646a523285928aa07bf14803178dc04616ac85994Eli Friedman    if (SETy->isIntegerType()) {
367746a523285928aa07bf14803178dc04616ac85994Eli Friedman      APSInt IntResult;
367846a523285928aa07bf14803178dc04616ac85994Eli Friedman      if (!EvaluateInteger(SE, IntResult, Info))
3679f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith         return false;
368007fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith      Val = APValue(IntResult);
368146a523285928aa07bf14803178dc04616ac85994Eli Friedman    } else if (SETy->isRealFloatingType()) {
368246a523285928aa07bf14803178dc04616ac85994Eli Friedman       APFloat F(0.0);
368346a523285928aa07bf14803178dc04616ac85994Eli Friedman       if (!EvaluateFloat(SE, F, Info))
3684f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith         return false;
368507fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith       Val = APValue(F);
368646a523285928aa07bf14803178dc04616ac85994Eli Friedman    } else {
368707fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith      return Error(E);
368846a523285928aa07bf14803178dc04616ac85994Eli Friedman    }
3689c0b8b19bd8056d6b5d831623a0825cce150f4507Nate Begeman
3690c0b8b19bd8056d6b5d831623a0825cce150f4507Nate Begeman    // Splat and create vector APValue.
369107fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    SmallVector<APValue, 4> Elts(NElts, Val);
369207fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    return Success(Elts, E);
3693e8c9e9218f215ec6089f12b076c7b9d310fd5194Nate Begeman  }
3694e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  case CK_BitCast: {
3695e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    // Evaluate the operand into an APInt we can extract from.
3696e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    llvm::APInt SValInt;
3697e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    if (!EvalAndBitcastToAPInt(Info, SE, SValInt))
3698e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      return false;
3699e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    // Extract the elements
3700e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    QualType EltTy = VTy->getElementType();
3701e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    unsigned EltSize = Info.Ctx.getTypeSize(EltTy);
3702e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian();
3703e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    SmallVector<APValue, 4> Elts;
3704e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    if (EltTy->isRealFloatingType()) {
3705e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(EltTy);
3706e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      bool isIEESem = &Sem != &APFloat::PPCDoubleDouble;
3707e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      unsigned FloatEltSize = EltSize;
3708e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      if (&Sem == &APFloat::x87DoubleExtended)
3709e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        FloatEltSize = 80;
3710e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      for (unsigned i = 0; i < NElts; i++) {
3711e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        llvm::APInt Elt;
3712e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        if (BigEndian)
3713e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman          Elt = SValInt.rotl(i*EltSize+FloatEltSize).trunc(FloatEltSize);
3714e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        else
3715e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman          Elt = SValInt.rotr(i*EltSize).trunc(FloatEltSize);
3716e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        Elts.push_back(APValue(APFloat(Elt, isIEESem)));
3717e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      }
3718e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    } else if (EltTy->isIntegerType()) {
3719e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      for (unsigned i = 0; i < NElts; i++) {
3720e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        llvm::APInt Elt;
3721e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        if (BigEndian)
3722e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman          Elt = SValInt.rotl(i*EltSize+EltSize).zextOrTrunc(EltSize);
3723e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        else
3724e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman          Elt = SValInt.rotr(i*EltSize).zextOrTrunc(EltSize);
3725e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        Elts.push_back(APValue(APSInt(Elt, EltTy->isSignedIntegerType())));
3726e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      }
3727e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    } else {
3728e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      return Error(E);
3729e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    }
3730e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    return Success(Elts, E);
3731e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  }
373246a523285928aa07bf14803178dc04616ac85994Eli Friedman  default:
3733c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return ExprEvaluatorBaseTy::VisitCastExpr(E);
3734c0b8b19bd8056d6b5d831623a0825cce150f4507Nate Begeman  }
373559b5da6d853b4368b984700315adf7b37de05764Nate Begeman}
373659b5da6d853b4368b984700315adf7b37de05764Nate Begeman
373707fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smithbool
373859b5da6d853b4368b984700315adf7b37de05764Nate BegemanVectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
373907fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith  const VectorType *VT = E->getType()->castAs<VectorType>();
374059b5da6d853b4368b984700315adf7b37de05764Nate Begeman  unsigned NumInits = E->getNumInits();
374191110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman  unsigned NumElements = VT->getNumElements();
37421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
374359b5da6d853b4368b984700315adf7b37de05764Nate Begeman  QualType EltTy = VT->getElementType();
37445f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<APValue, 4> Elements;
374559b5da6d853b4368b984700315adf7b37de05764Nate Begeman
37463edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman  // The number of initializers can be less than the number of
37473edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman  // vector elements. For OpenCL, this can be due to nested vector
37483edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman  // initialization. For GCC compatibility, missing trailing elements
37493edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman  // should be initialized with zeroes.
37503edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman  unsigned CountInits = 0, CountElts = 0;
37513edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman  while (CountElts < NumElements) {
37523edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman    // Handle nested vector initialization.
37533edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman    if (CountInits < NumInits
37543edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman        && E->getInit(CountInits)->getType()->isExtVectorType()) {
37553edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      APValue v;
37563edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      if (!EvaluateVector(E->getInit(CountInits), v, Info))
37573edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman        return Error(E);
37583edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      unsigned vlen = v.getVectorLength();
37593edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      for (unsigned j = 0; j < vlen; j++)
37603edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman        Elements.push_back(v.getVectorElt(j));
37613edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      CountElts += vlen;
37623edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman    } else if (EltTy->isIntegerType()) {
376359b5da6d853b4368b984700315adf7b37de05764Nate Begeman      llvm::APSInt sInt(32);
37643edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      if (CountInits < NumInits) {
37653edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman        if (!EvaluateInteger(E->getInit(CountInits), sInt, Info))
37664b1f684416980ef6f1a7cb9e6af9c4fa4a164617Richard Smith          return false;
37673edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      } else // trailing integer zero.
37683edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman        sInt = Info.Ctx.MakeIntValue(0, EltTy);
37693edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      Elements.push_back(APValue(sInt));
37703edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      CountElts++;
377159b5da6d853b4368b984700315adf7b37de05764Nate Begeman    } else {
377259b5da6d853b4368b984700315adf7b37de05764Nate Begeman      llvm::APFloat f(0.0);
37733edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      if (CountInits < NumInits) {
37743edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman        if (!EvaluateFloat(E->getInit(CountInits), f, Info))
37754b1f684416980ef6f1a7cb9e6af9c4fa4a164617Richard Smith          return false;
37763edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      } else // trailing float zero.
37773edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman        f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
37783edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      Elements.push_back(APValue(f));
37793edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      CountElts++;
378059b5da6d853b4368b984700315adf7b37de05764Nate Begeman    }
37813edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman    CountInits++;
378259b5da6d853b4368b984700315adf7b37de05764Nate Begeman  }
378307fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith  return Success(Elements, E);
378459b5da6d853b4368b984700315adf7b37de05764Nate Begeman}
378559b5da6d853b4368b984700315adf7b37de05764Nate Begeman
378607fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smithbool
378751201882382fb40c9456a06c7f93d6ddd4a57712Richard SmithVectorExprEvaluator::ZeroInitialization(const Expr *E) {
378807fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith  const VectorType *VT = E->getType()->getAs<VectorType>();
378991110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman  QualType EltTy = VT->getElementType();
379091110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman  APValue ZeroElement;
379191110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman  if (EltTy->isIntegerType())
379291110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman    ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
379391110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman  else
379491110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman    ZeroElement =
379591110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman        APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
379691110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman
37975f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
379807fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith  return Success(Elements, E);
379991110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman}
380091110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman
380107fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smithbool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
38028327fad71da34492d82c532f42a58cb4baff81a3Richard Smith  VisitIgnoredValue(E->getSubExpr());
380351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  return ZeroInitialization(E);
380491110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman}
380591110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman
380659b5da6d853b4368b984700315adf7b37de05764Nate Begeman//===----------------------------------------------------------------------===//
3807cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith// Array Evaluation
3808cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith//===----------------------------------------------------------------------===//
3809cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
3810cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smithnamespace {
3811cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  class ArrayExprEvaluator
3812cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  : public ExprEvaluatorBase<ArrayExprEvaluator, bool> {
3813180f47959a066795cc0f409433023af448bb0328Richard Smith    const LValue &This;
3814cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    APValue &Result;
3815cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  public:
3816cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
3817180f47959a066795cc0f409433023af448bb0328Richard Smith    ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result)
3818180f47959a066795cc0f409433023af448bb0328Richard Smith      : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {}
3819cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
3820cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    bool Success(const APValue &V, const Expr *E) {
3821f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith      assert((V.isArray() || V.isLValue()) &&
3822f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith             "expected array or string literal");
3823cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith      Result = V;
3824cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith      return true;
3825cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    }
3826cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
382751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    bool ZeroInitialization(const Expr *E) {
3828180f47959a066795cc0f409433023af448bb0328Richard Smith      const ConstantArrayType *CAT =
3829180f47959a066795cc0f409433023af448bb0328Richard Smith          Info.Ctx.getAsConstantArrayType(E->getType());
3830180f47959a066795cc0f409433023af448bb0328Richard Smith      if (!CAT)
3831f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(E);
3832180f47959a066795cc0f409433023af448bb0328Richard Smith
3833180f47959a066795cc0f409433023af448bb0328Richard Smith      Result = APValue(APValue::UninitArray(), 0,
3834180f47959a066795cc0f409433023af448bb0328Richard Smith                       CAT->getSize().getZExtValue());
3835180f47959a066795cc0f409433023af448bb0328Richard Smith      if (!Result.hasArrayFiller()) return true;
3836180f47959a066795cc0f409433023af448bb0328Richard Smith
383751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      // Zero-initialize all elements.
3838180f47959a066795cc0f409433023af448bb0328Richard Smith      LValue Subobject = This;
3839b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      Subobject.addArray(Info, E, CAT);
3840180f47959a066795cc0f409433023af448bb0328Richard Smith      ImplicitValueInitExpr VIE(CAT->getElementType());
384183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, &VIE);
3842180f47959a066795cc0f409433023af448bb0328Richard Smith    }
3843180f47959a066795cc0f409433023af448bb0328Richard Smith
3844cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    bool VisitInitListExpr(const InitListExpr *E);
3845e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    bool VisitCXXConstructExpr(const CXXConstructExpr *E);
3846cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  };
3847cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith} // end anonymous namespace
3848cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
3849180f47959a066795cc0f409433023af448bb0328Richard Smithstatic bool EvaluateArray(const Expr *E, const LValue &This,
3850180f47959a066795cc0f409433023af448bb0328Richard Smith                          APValue &Result, EvalInfo &Info) {
385151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  assert(E->isRValue() && E->getType()->isArrayType() && "not an array rvalue");
3852180f47959a066795cc0f409433023af448bb0328Richard Smith  return ArrayExprEvaluator(Info, This, Result).Visit(E);
3853cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith}
3854cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
3855cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smithbool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
3856cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType());
3857cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  if (!CAT)
3858f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
3859cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
3860974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith  // C++11 [dcl.init.string]p1: A char array [...] can be initialized by [...]
3861974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith  // an appropriately-typed string literal enclosed in braces.
3862fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith  if (E->isStringLiteralInit()) {
3863974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith    LValue LV;
3864974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith    if (!EvaluateLValue(E->getInit(0), LV, Info))
3865974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith      return false;
38661aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    APValue Val;
3867f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith    LV.moveInto(Val);
3868f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith    return Success(Val, E);
3869974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith  }
3870974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith
3871745f5147e065900267c85a5568785a1991d4838fRichard Smith  bool Success = true;
3872745f5147e065900267c85a5568785a1991d4838fRichard Smith
3873cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  Result = APValue(APValue::UninitArray(), E->getNumInits(),
3874cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith                   CAT->getSize().getZExtValue());
3875180f47959a066795cc0f409433023af448bb0328Richard Smith  LValue Subobject = This;
3876b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  Subobject.addArray(Info, E, CAT);
3877180f47959a066795cc0f409433023af448bb0328Richard Smith  unsigned Index = 0;
3878cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  for (InitListExpr::const_iterator I = E->begin(), End = E->end();
3879180f47959a066795cc0f409433023af448bb0328Richard Smith       I != End; ++I, ++Index) {
388083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    if (!EvaluateInPlace(Result.getArrayInitializedElt(Index),
388183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                         Info, Subobject, cast<Expr>(*I)) ||
3882745f5147e065900267c85a5568785a1991d4838fRichard Smith        !HandleLValueArrayAdjustment(Info, cast<Expr>(*I), Subobject,
3883745f5147e065900267c85a5568785a1991d4838fRichard Smith                                     CAT->getElementType(), 1)) {
3884745f5147e065900267c85a5568785a1991d4838fRichard Smith      if (!Info.keepEvaluatingAfterFailure())
3885745f5147e065900267c85a5568785a1991d4838fRichard Smith        return false;
3886745f5147e065900267c85a5568785a1991d4838fRichard Smith      Success = false;
3887745f5147e065900267c85a5568785a1991d4838fRichard Smith    }
3888180f47959a066795cc0f409433023af448bb0328Richard Smith  }
3889cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
3890745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!Result.hasArrayFiller()) return Success;
3891cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  assert(E->hasArrayFiller() && "no array filler for incomplete init list");
3892180f47959a066795cc0f409433023af448bb0328Richard Smith  // FIXME: The Subobject here isn't necessarily right. This rarely matters,
3893180f47959a066795cc0f409433023af448bb0328Richard Smith  // but sometimes does:
3894180f47959a066795cc0f409433023af448bb0328Richard Smith  //   struct S { constexpr S() : p(&p) {} void *p; };
3895180f47959a066795cc0f409433023af448bb0328Richard Smith  //   S s[10] = {};
389683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  return EvaluateInPlace(Result.getArrayFiller(), Info,
389783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                         Subobject, E->getArrayFiller()) && Success;
3898cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith}
3899cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
3900e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithbool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) {
3901e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType());
3902e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (!CAT)
3903f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
3904e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3905ec789163a42a7be654ac34aadb750b508954d53cRichard Smith  bool HadZeroInit = !Result.isUninit();
3906ec789163a42a7be654ac34aadb750b508954d53cRichard Smith  if (!HadZeroInit)
3907ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    Result = APValue(APValue::UninitArray(), 0, CAT->getSize().getZExtValue());
3908e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (!Result.hasArrayFiller())
3909e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
3910e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3911e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  const CXXConstructorDecl *FD = E->getConstructor();
39126180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith
391351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  bool ZeroInit = E->requiresZeroInitialization();
391451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) {
3915ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    if (HadZeroInit)
3916ec789163a42a7be654ac34aadb750b508954d53cRichard Smith      return true;
3917ec789163a42a7be654ac34aadb750b508954d53cRichard Smith
391851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    if (ZeroInit) {
391951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      LValue Subobject = This;
3920b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      Subobject.addArray(Info, E, CAT);
392151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      ImplicitValueInitExpr VIE(CAT->getElementType());
392283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, &VIE);
392351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    }
392451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
39256180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    const CXXRecordDecl *RD = FD->getParent();
39266180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    if (RD->isUnion())
39276180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith      Result.getArrayFiller() = APValue((FieldDecl*)0);
39286180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    else
39296180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith      Result.getArrayFiller() =
39306180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith          APValue(APValue::UninitStruct(), RD->getNumBases(),
39316180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith                  std::distance(RD->field_begin(), RD->field_end()));
39326180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    return true;
39336180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith  }
39346180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith
3935e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  const FunctionDecl *Definition = 0;
3936e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  FD->getBody(Definition);
3937e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3938c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition))
3939c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return false;
3940e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3941e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // FIXME: The Subobject here isn't necessarily right. This rarely matters,
3942e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // but sometimes does:
3943e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  //   struct S { constexpr S() : p(&p) {} void *p; };
3944e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  //   S s[10];
3945e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  LValue Subobject = This;
3946b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  Subobject.addArray(Info, E, CAT);
394751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
3948ec789163a42a7be654ac34aadb750b508954d53cRichard Smith  if (ZeroInit && !HadZeroInit) {
394951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    ImplicitValueInitExpr VIE(CAT->getElementType());
395083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    if (!EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, &VIE))
395151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      return false;
395251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  }
395351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
3954e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs());
3955745f5147e065900267c85a5568785a1991d4838fRichard Smith  return HandleConstructorCall(E->getExprLoc(), Subobject, Args,
3956e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                               cast<CXXConstructorDecl>(Definition),
3957e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                               Info, Result.getArrayFiller());
3958e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}
3959e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3960cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith//===----------------------------------------------------------------------===//
3961f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner// Integer Evaluation
3962c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//
3963c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// As a GNU extension, we support casting pointers to sufficiently-wide integer
3964c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// types and back in constant folding. Integer values are thus represented
3965c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// either as an integer-valued APValue, or as an lvalue-valued APValue.
3966f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===//
3967f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
3968f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattnernamespace {
3969770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass IntExprEvaluator
39708cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  : public ExprEvaluatorBase<IntExprEvaluator, bool> {
39711aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  APValue &Result;
3972f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattnerpublic:
39731aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  IntExprEvaluator(EvalInfo &info, APValue &result)
39748cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    : ExprEvaluatorBaseTy(info), Result(result) {}
3975f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
3976cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  bool Success(const llvm::APSInt &SI, const Expr *E, APValue &Result) {
3977973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara    assert(E->getType()->isIntegralOrEnumerationType() &&
39782ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor           "Invalid evaluation result.");
3979973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara    assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() &&
39803f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar           "Invalid evaluation result.");
3981973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara    assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
39823f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar           "Invalid evaluation result.");
39831aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    Result = APValue(SI);
39843f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar    return true;
39853f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar  }
3986cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  bool Success(const llvm::APSInt &SI, const Expr *E) {
3987cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return Success(SI, E, Result);
3988cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
39893f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar
3990cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  bool Success(const llvm::APInt &I, const Expr *E, APValue &Result) {
39912ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor    assert(E->getType()->isIntegralOrEnumerationType() &&
39922ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor           "Invalid evaluation result.");
399330c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar    assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
39943f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar           "Invalid evaluation result.");
39951aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    Result = APValue(APSInt(I));
3996575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor    Result.getInt().setIsUnsigned(
3997575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor                            E->getType()->isUnsignedIntegerOrEnumerationType());
3998131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar    return true;
3999131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar  }
4000cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  bool Success(const llvm::APInt &I, const Expr *E) {
4001cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return Success(I, E, Result);
4002cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4003131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar
4004cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  bool Success(uint64_t Value, const Expr *E, APValue &Result) {
40052ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor    assert(E->getType()->isIntegralOrEnumerationType() &&
40062ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor           "Invalid evaluation result.");
40071aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
4008131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar    return true;
4009131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar  }
4010cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  bool Success(uint64_t Value, const Expr *E) {
4011cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return Success(Value, E, Result);
4012cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4013131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar
40144f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck  bool Success(CharUnits Size, const Expr *E) {
40154f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck    return Success(Size.getQuantity(), E);
40164f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck  }
40174f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck
40181aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  bool Success(const APValue &V, const Expr *E) {
40195930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman    if (V.isLValue() || V.isAddrLabelDiff()) {
4020342f1f8b0a402c5a7f8c5055db7f60a7808f1687Richard Smith      Result = V;
4021342f1f8b0a402c5a7f8c5055db7f60a7808f1687Richard Smith      return true;
4022342f1f8b0a402c5a7f8c5055db7f60a7808f1687Richard Smith    }
40238cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    return Success(V.getInt(), E);
402432fea9d18cc3658a1b01df5ca6f2ac302625c61dChris Lattner  }
40251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
402651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  bool ZeroInitialization(const Expr *E) { return Success(0, E); }
4027f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith
40288cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  //===--------------------------------------------------------------------===//
40298cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  //                            Visitor Methods
40308cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  //===--------------------------------------------------------------------===//
4031f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
40324c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner  bool VisitIntegerLiteral(const IntegerLiteral *E) {
4033131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar    return Success(E->getValue(), E);
40344c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner  }
40354c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner  bool VisitCharacterLiteral(const CharacterLiteral *E) {
4036131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar    return Success(E->getValue(), E);
40374c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner  }
4038043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman
4039043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman  bool CheckReferencedDecl(const Expr *E, const Decl *D);
4040043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman  bool VisitDeclRefExpr(const DeclRefExpr *E) {
40418cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    if (CheckReferencedDecl(E, E->getDecl()))
40428cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne      return true;
40438cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
40448cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    return ExprEvaluatorBaseTy::VisitDeclRefExpr(E);
4045043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman  }
4046043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman  bool VisitMemberExpr(const MemberExpr *E) {
4047043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman    if (CheckReferencedDecl(E, E->getMemberDecl())) {
4048c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith      VisitIgnoredValue(E->getBase());
4049043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman      return true;
4050043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman    }
40518cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
40528cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    return ExprEvaluatorBaseTy::VisitMemberExpr(E);
4053043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman  }
4054043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman
40558cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCallExpr(const CallExpr *E);
4056b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattner  bool VisitBinaryOperator(const BinaryOperator *E);
40578ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  bool VisitOffsetOfExpr(const OffsetOfExpr *E);
4058b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattner  bool VisitUnaryOperator(const UnaryOperator *E);
4059f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
40608cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCastExpr(const CastExpr* E);
4061f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E);
40620518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl
40633068d117951a8df54bae9db039b56201ab10962bAnders Carlsson  bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
4064131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar    return Success(E->getValue(), E);
40653068d117951a8df54bae9db039b56201ab10962bAnders Carlsson  }
40661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4067ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  bool VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) {
4068ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    return Success(E->getValue(), E);
4069ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  }
4070ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
4071f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  // Note, GNU defines __null as an integer, not a pointer.
40723f70456b8adb0405ef2a47d51f9fc2d5937ae8aeAnders Carlsson  bool VisitGNUNullExpr(const GNUNullExpr *E) {
407351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return ZeroInitialization(E);
4074664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman  }
4075664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman
407664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
40770dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    return Success(E->getValue(), E);
407864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  }
407964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
40806ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  bool VisitBinaryTypeTraitExpr(const BinaryTypeTraitExpr *E) {
40816ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    return Success(E->getValue(), E);
40826ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  }
40836ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
40844ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  bool VisitTypeTraitExpr(const TypeTraitExpr *E) {
40854ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    return Success(E->getValue(), E);
40864ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  }
40874ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
408821ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) {
408921ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    return Success(E->getValue(), E);
409021ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  }
409121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
4092552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) {
4093552622067dc45013d240f73952fece703f5e63bdJohn Wiegley    return Success(E->getValue(), E);
4094552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  }
4095552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
4096722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman  bool VisitUnaryReal(const UnaryOperator *E);
4097664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman  bool VisitUnaryImag(const UnaryOperator *E);
4098664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman
4099295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl  bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E);
4100ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor  bool VisitSizeOfPackExpr(const SizeOfPackExpr *E);
4101cea8d966f826554f0679595e9371e314e8dbc1cfSebastian Redl
4102fcee0019b76f9f368f2b3d6d4048a98232593f29Chris Lattnerprivate:
41038b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck  CharUnits GetAlignOfExpr(const Expr *E);
41048b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck  CharUnits GetAlignOfType(QualType T);
41051bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  static QualType GetObjectType(APValue::LValueBase B);
41068cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool TryEvaluateBuiltinObjectSize(const CallExpr *E);
4107664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman  // FIXME: Missing: array subscript of vector, member of vector
4108f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner};
4109f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner} // end anonymous namespace
4110f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
4111c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// EvaluateIntegerOrLValue - Evaluate an rvalue integral-typed expression, and
4112c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// produce either the integer value or a pointer.
4113c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith///
4114c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// GCC has a heinous extension which folds casts between pointer types and
4115c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// pointer-sized integral types. We support this by allowing the evaluation of
4116c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// an integer rvalue to produce a pointer (represented as an lvalue) instead.
4117c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// Some simple arithmetic on such values is supported (they are treated much
4118c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// like char*).
41191aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smithstatic bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,
412047a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith                                    EvalInfo &Info) {
4121c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  assert(E->isRValue() && E->getType()->isIntegralOrEnumerationType());
41228cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  return IntExprEvaluator(Info, Result).Visit(E);
412369ab26a8623141f35e86817cfc6e0fbe7639a40fDaniel Dunbar}
412469ab26a8623141f35e86817cfc6e0fbe7639a40fDaniel Dunbar
4125f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info) {
41261aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  APValue Val;
4127f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (!EvaluateIntegerOrLValue(E, Val, Info))
4128f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return false;
4129f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (!Val.isInt()) {
4130f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    // FIXME: It would be better to produce the diagnostic for casting
4131f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    //        a pointer to an integer.
41325cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.Diag(E, diag::note_invalid_subexpr_in_const_expr);
413330c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar    return false;
4134f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
413530c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar  Result = Val.getInt();
413630c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar  return true;
4137f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner}
4138f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
4139f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// Check whether the given declaration can be directly converted to an integral
4140f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// rvalue. If not, no diagnostic is produced; there are other things we can
4141f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// try.
4142043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedmanbool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) {
41434c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner  // Enums are integer constant exprs.
4144bfbdcd861a4364bfc21a9e5047bdbd56812d6693Abramo Bagnara  if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) {
4145973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara    // Check for signedness/width mismatches between E type and ECD value.
4146973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara    bool SameSign = (ECD->getInitVal().isSigned()
4147973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara                     == E->getType()->isSignedIntegerOrEnumerationType());
4148973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara    bool SameWidth = (ECD->getInitVal().getBitWidth()
4149973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara                      == Info.Ctx.getIntWidth(E->getType()));
4150973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara    if (SameSign && SameWidth)
4151973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara      return Success(ECD->getInitVal(), E);
4152973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara    else {
4153973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara      // Get rid of mismatch (otherwise Success assertions will fail)
4154973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara      // by computing a new value matching the type of E.
4155973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara      llvm::APSInt Val = ECD->getInitVal();
4156973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara      if (!SameSign)
4157973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara        Val.setIsSigned(!ECD->getInitVal().isSigned());
4158973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara      if (!SameWidth)
4159973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara        Val = Val.extOrTrunc(Info.Ctx.getIntWidth(E->getType()));
4160973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara      return Success(Val, E);
4161973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara    }
4162bfbdcd861a4364bfc21a9e5047bdbd56812d6693Abramo Bagnara  }
41638cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  return false;
41644c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner}
41654c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner
4166a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
4167a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner/// as GCC.
4168a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattnerstatic int EvaluateBuiltinClassifyType(const CallExpr *E) {
4169a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  // The following enum mimics the values returned by GCC.
41707c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // FIXME: Does GCC differ between lvalue and rvalue references here?
4171a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  enum gcc_type_class {
4172a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    no_type_class = -1,
4173a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    void_type_class, integer_type_class, char_type_class,
4174a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    enumeral_type_class, boolean_type_class,
4175a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    pointer_type_class, reference_type_class, offset_type_class,
4176a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    real_type_class, complex_type_class,
4177a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    function_type_class, method_type_class,
4178a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    record_type_class, union_type_class,
4179a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    array_type_class, string_type_class,
4180a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    lang_type_class
4181a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  };
41821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
41831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If no argument was supplied, default to "no_type_class". This isn't
4184a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  // ideal, however it is what gcc does.
4185a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  if (E->getNumArgs() == 0)
4186a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return no_type_class;
41871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4188a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  QualType ArgTy = E->getArg(0)->getType();
4189a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  if (ArgTy->isVoidType())
4190a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return void_type_class;
4191a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isEnumeralType())
4192a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return enumeral_type_class;
4193a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isBooleanType())
4194a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return boolean_type_class;
4195a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isCharType())
4196a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return string_type_class; // gcc doesn't appear to use char_type_class
4197a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isIntegerType())
4198a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return integer_type_class;
4199a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isPointerType())
4200a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return pointer_type_class;
4201a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isReferenceType())
4202a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return reference_type_class;
4203a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isRealType())
4204a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return real_type_class;
4205a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isComplexType())
4206a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return complex_type_class;
4207a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isFunctionType())
4208a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return function_type_class;
4209fb87b89fc9eb103e19fb8e4b925c23f0bd091b99Douglas Gregor  else if (ArgTy->isStructureOrClassType())
4210a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return record_type_class;
4211a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isUnionType())
4212a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return union_type_class;
4213a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isArrayType())
4214a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return array_type_class;
4215a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isUnionType())
4216a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return union_type_class;
4217a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else  // FIXME: offset_type_class, method_type_class, & lang_type_class?
4218b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("CallExpr::isBuiltinClassifyType(): unimplemented type");
4219a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner}
4220a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner
422180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// EvaluateBuiltinConstantPForLValue - Determine the result of
422280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// __builtin_constant_p when applied to the given lvalue.
422380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith///
422480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// An lvalue is only "constant" if it is a pointer or reference to the first
422580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// character of a string literal.
422680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smithtemplate<typename LValue>
422780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smithstatic bool EvaluateBuiltinConstantPForLValue(const LValue &LV) {
42288e55ed1ad3acf9c7f8424aa7d326b3b2c18e943bDouglas Gregor  const Expr *E = LV.getLValueBase().template dyn_cast<const Expr*>();
422980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  return E && isa<StringLiteral>(E) && LV.getLValueOffset().isZero();
423080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith}
423180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith
423280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// EvaluateBuiltinConstantP - Evaluate __builtin_constant_p as similarly to
423380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// GCC as we can manage.
423480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smithstatic bool EvaluateBuiltinConstantP(ASTContext &Ctx, const Expr *Arg) {
423580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  QualType ArgType = Arg->getType();
423680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith
423780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  // __builtin_constant_p always has one operand. The rules which gcc follows
423880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  // are not precisely documented, but are as follows:
423980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  //
424080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  //  - If the operand is of integral, floating, complex or enumeration type,
424180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  //    and can be folded to a known value of that type, it returns 1.
424280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  //  - If the operand and can be folded to a pointer to the first character
424380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  //    of a string literal (or such a pointer cast to an integral type), it
424480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  //    returns 1.
424580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  //
424680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  // Otherwise, it returns 0.
424780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  //
424880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  // FIXME: GCC also intends to return 1 for literals of aggregate types, but
424980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  // its support for this does not currently work.
425080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  if (ArgType->isIntegralOrEnumerationType()) {
425180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    Expr::EvalResult Result;
425280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    if (!Arg->EvaluateAsRValue(Result, Ctx) || Result.HasSideEffects)
425380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith      return false;
425480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith
425580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    APValue &V = Result.Val;
425680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    if (V.getKind() == APValue::Int)
425780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith      return true;
425880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith
425980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    return EvaluateBuiltinConstantPForLValue(V);
426080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  } else if (ArgType->isFloatingType() || ArgType->isAnyComplexType()) {
426180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    return Arg->isEvaluatable(Ctx);
426280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  } else if (ArgType->isPointerType() || Arg->isGLValue()) {
426380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    LValue LV;
426480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    Expr::EvalStatus Status;
426580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    EvalInfo Info(Ctx, Status);
426680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    if ((Arg->isGLValue() ? EvaluateLValue(Arg, LV, Info)
426780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith                          : EvaluatePointer(Arg, LV, Info)) &&
426880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith        !Status.HasSideEffects)
426980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith      return EvaluateBuiltinConstantPForLValue(LV);
427080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  }
427180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith
427280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  // Anything else isn't considered to be sufficiently constant.
427380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  return false;
427480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith}
427580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith
427642c8f87eb60958170c46767273bf93e6c96125bfJohn McCall/// Retrieves the "underlying object type" of the given expression,
427742c8f87eb60958170c46767273bf93e6c96125bfJohn McCall/// as used by __builtin_object_size.
42781bf9a9e6a5bdc0de7939908855dcddf46b661800Richard SmithQualType IntExprEvaluator::GetObjectType(APValue::LValueBase B) {
42791bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) {
42801bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    if (const VarDecl *VD = dyn_cast<VarDecl>(D))
428142c8f87eb60958170c46767273bf93e6c96125bfJohn McCall      return VD->getType();
42821bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  } else if (const Expr *E = B.get<const Expr*>()) {
42831bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    if (isa<CompoundLiteralExpr>(E))
42841bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith      return E->getType();
428542c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  }
428642c8f87eb60958170c46767273bf93e6c96125bfJohn McCall
428742c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  return QualType();
428842c8f87eb60958170c46767273bf93e6c96125bfJohn McCall}
428942c8f87eb60958170c46767273bf93e6c96125bfJohn McCall
42908cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool IntExprEvaluator::TryEvaluateBuiltinObjectSize(const CallExpr *E) {
429142c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  // TODO: Perhaps we should let LLVM lower this?
429242c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  LValue Base;
429342c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  if (!EvaluatePointer(E->getArg(0), Base, Info))
429442c8f87eb60958170c46767273bf93e6c96125bfJohn McCall    return false;
429542c8f87eb60958170c46767273bf93e6c96125bfJohn McCall
429642c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  // If we can prove the base is null, lower to zero now.
42971bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  if (!Base.getLValueBase()) return Success(0, E);
429842c8f87eb60958170c46767273bf93e6c96125bfJohn McCall
42991bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  QualType T = GetObjectType(Base.getLValueBase());
430042c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  if (T.isNull() ||
430142c8f87eb60958170c46767273bf93e6c96125bfJohn McCall      T->isIncompleteType() ||
43021357869bc5983cdfbc986db1f3d18265bb34cb0eEli Friedman      T->isFunctionType() ||
430342c8f87eb60958170c46767273bf93e6c96125bfJohn McCall      T->isVariablyModifiedType() ||
430442c8f87eb60958170c46767273bf93e6c96125bfJohn McCall      T->isDependentType())
4305f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
430642c8f87eb60958170c46767273bf93e6c96125bfJohn McCall
430742c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  CharUnits Size = Info.Ctx.getTypeSizeInChars(T);
430842c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  CharUnits Offset = Base.getLValueOffset();
430942c8f87eb60958170c46767273bf93e6c96125bfJohn McCall
431042c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  if (!Offset.isNegative() && Offset <= Size)
431142c8f87eb60958170c46767273bf93e6c96125bfJohn McCall    Size -= Offset;
431242c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  else
431342c8f87eb60958170c46767273bf93e6c96125bfJohn McCall    Size = CharUnits::Zero();
43144f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck  return Success(Size, E);
431542c8f87eb60958170c46767273bf93e6c96125bfJohn McCall}
431642c8f87eb60958170c46767273bf93e6c96125bfJohn McCall
43178cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
43182c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith  switch (unsigned BuiltinOp = E->isBuiltinCall()) {
4319019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  default:
43208cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    return ExprEvaluatorBaseTy::VisitCallExpr(E);
432164eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump
432264eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump  case Builtin::BI__builtin_object_size: {
432342c8f87eb60958170c46767273bf93e6c96125bfJohn McCall    if (TryEvaluateBuiltinObjectSize(E))
432442c8f87eb60958170c46767273bf93e6c96125bfJohn McCall      return true;
432564eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump
4326b2aaf51ed73a4774322a39a0dd59d0fe7e3258c0Eric Christopher    // If evaluating the argument has side-effects we can't determine
4327b2aaf51ed73a4774322a39a0dd59d0fe7e3258c0Eric Christopher    // the size of the object and lower it to unknown now.
4328393c247fe025ccb5f914e37e948192ea86faef8cFariborz Jahanian    if (E->getArg(0)->HasSideEffects(Info.Ctx)) {
4329a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith      if (E->getArg(1)->EvaluateKnownConstInt(Info.Ctx).getZExtValue() <= 1)
4330cf184655319cf7a5b811067cff9d26a5741fd161Chris Lattner        return Success(-1ULL, E);
433164eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump      return Success(0, E);
433264eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump    }
4333c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump
4334f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
433564eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump  }
433664eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump
4337019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  case Builtin::BI__builtin_classify_type:
4338131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar    return Success(EvaluateBuiltinClassifyType(E), E);
43391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
434080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  case Builtin::BI__builtin_constant_p:
434180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    return Success(EvaluateBuiltinConstantP(Info.Ctx, E->getArg(0)), E);
4342e052d46f4db91f9ba572859ffc984e85cbf5d5ffRichard Smith
434321fb98ee003e992b0c4e204d98a19e0ef544cae3Chris Lattner  case Builtin::BI__builtin_eh_return_data_regno: {
4344a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith    int Operand = E->getArg(0)->EvaluateKnownConstInt(Info.Ctx).getZExtValue();
4345bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(Operand);
434621fb98ee003e992b0c4e204d98a19e0ef544cae3Chris Lattner    return Success(Operand, E);
434721fb98ee003e992b0c4e204d98a19e0ef544cae3Chris Lattner  }
4348c4a2638b5ef3e2d35d872614ceb655a7a22c58beEli Friedman
4349c4a2638b5ef3e2d35d872614ceb655a7a22c58beEli Friedman  case Builtin::BI__builtin_expect:
4350c4a2638b5ef3e2d35d872614ceb655a7a22c58beEli Friedman    return Visit(E->getArg(0));
435140b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith
43525726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor  case Builtin::BIstrlen:
435340b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith    // A call to strlen is not a constant expression.
435440b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith    if (Info.getLangOpts().CPlusPlus0x)
43555cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      Info.CCEDiag(E, diag::note_constexpr_invalid_function)
435640b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith        << /*isConstexpr*/0 << /*isConstructor*/0 << "'strlen'";
435740b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith    else
43585cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr);
435940b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith    // Fall through.
43605726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor  case Builtin::BI__builtin_strlen:
43615726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor    // As an extension, we support strlen() and __builtin_strlen() as constant
43625726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor    // expressions when the argument is a string literal.
43638cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    if (const StringLiteral *S
43645726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor               = dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenImpCasts())) {
43655726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor      // The string literal may have embedded null characters. Find the first
43665726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor      // one and truncate there.
43675f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      StringRef Str = S->getString();
43685f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      StringRef::size_type Pos = Str.find(0);
43695f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      if (Pos != StringRef::npos)
43705726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor        Str = Str.substr(0, Pos);
43715726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor
43725726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor      return Success(Str.size(), E);
43735726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor    }
43745726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor
4375f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
4376454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman
43772c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith  case Builtin::BI__atomic_always_lock_free:
4378fafbf06732746f3ceca21d452d77b144ba8652aeRichard Smith  case Builtin::BI__atomic_is_lock_free:
4379fafbf06732746f3ceca21d452d77b144ba8652aeRichard Smith  case Builtin::BI__c11_atomic_is_lock_free: {
4380454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    APSInt SizeVal;
4381454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    if (!EvaluateInteger(E->getArg(0), SizeVal, Info))
4382454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman      return false;
4383454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman
4384454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // For __atomic_is_lock_free(sizeof(_Atomic(T))), if the size is a power
4385454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // of two less than the maximum inline atomic width, we know it is
4386454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // lock-free.  If the size isn't a power of two, or greater than the
4387454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // maximum alignment where we promote atomics, we know it is not lock-free
4388454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // (at least not in the sense of atomic_is_lock_free).  Otherwise,
4389454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // the answer can only be determined at runtime; for example, 16-byte
4390454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // atomics have lock-free implementations on some, but not all,
4391454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // x86-64 processors.
4392454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman
4393454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // Check power-of-two.
4394454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    CharUnits Size = CharUnits::fromQuantity(SizeVal.getZExtValue());
43952c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith    if (Size.isPowerOfTwo()) {
43962c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith      // Check against inlining width.
43972c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith      unsigned InlineWidthBits =
43982c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith          Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth();
43992c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith      if (Size <= Info.Ctx.toCharUnitsFromBits(InlineWidthBits)) {
44002c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith        if (BuiltinOp == Builtin::BI__c11_atomic_is_lock_free ||
44012c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith            Size == CharUnits::One() ||
44022c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith            E->getArg(1)->isNullPointerConstant(Info.Ctx,
44032c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith                                                Expr::NPC_NeverValueDependent))
44042c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith          // OK, we will inline appropriately-aligned operations of this size,
44052c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith          // and _Atomic(T) is appropriately-aligned.
44062c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith          return Success(1, E);
44072c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith
44082c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith        QualType PointeeType = E->getArg(1)->IgnoreImpCasts()->getType()->
44092c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith          castAs<PointerType>()->getPointeeType();
44102c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith        if (!PointeeType->isIncompleteType() &&
44112c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith            Info.Ctx.getTypeAlignInChars(PointeeType) >= Size) {
44122c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith          // OK, we will inline operations on this object.
44132c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith          return Success(1, E);
44142c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith        }
44152c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith      }
44162c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith    }
4417454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman
44182c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith    return BuiltinOp == Builtin::BI__atomic_always_lock_free ?
44192c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith        Success(0, E) : Error(E);
4420454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman  }
4421019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  }
44224c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner}
4423f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
4424625b80755b603d28f36fb4212c81484d87ad08d3Richard Smithstatic bool HasSameBase(const LValue &A, const LValue &B) {
4425625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith  if (!A.getLValueBase())
4426625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith    return !B.getLValueBase();
4427625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith  if (!B.getLValueBase())
4428625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith    return false;
4429625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith
44301bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  if (A.getLValueBase().getOpaqueValue() !=
44311bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith      B.getLValueBase().getOpaqueValue()) {
4432625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith    const Decl *ADecl = GetLValueBaseDecl(A);
4433625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith    if (!ADecl)
4434625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith      return false;
4435625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith    const Decl *BDecl = GetLValueBaseDecl(B);
44369a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith    if (!BDecl || ADecl->getCanonicalDecl() != BDecl->getCanonicalDecl())
4437625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith      return false;
4438625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith  }
4439625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith
4440625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith  return IsGlobalLValue(A.getLValueBase()) ||
444183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith         A.getLValueCallIndex() == B.getLValueCallIndex();
4442625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith}
4443625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith
44447b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith/// Perform the given integer operation, which is known to need at most BitWidth
44457b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith/// bits, and check for overflow in the original type (if that type was not an
44467b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith/// unsigned type).
44477b48a2986345480241f3b8209f71bb21b0530b4fRichard Smithtemplate<typename Operation>
44487b48a2986345480241f3b8209f71bb21b0530b4fRichard Smithstatic APSInt CheckedIntArithmetic(EvalInfo &Info, const Expr *E,
44497b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith                                   const APSInt &LHS, const APSInt &RHS,
44507b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith                                   unsigned BitWidth, Operation Op) {
44517b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith  if (LHS.isUnsigned())
44527b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    return Op(LHS, RHS);
44537b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith
44547b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith  APSInt Value(Op(LHS.extend(BitWidth), RHS.extend(BitWidth)), false);
44557b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith  APSInt Result = Value.trunc(LHS.getBitWidth());
44567b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith  if (Result.extend(BitWidth) != Value)
44577b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    HandleOverflow(Info, E, Value, E->getType());
44587b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith  return Result;
44597b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith}
44607b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith
4461cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidisnamespace {
4462c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
4463cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis/// \brief Data recursive integer evaluator of certain binary operators.
4464cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis///
4465cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis/// We use a data recursive algorithm for binary operators so that we are able
4466cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis/// to handle extreme cases of chained binary operators without causing stack
4467cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis/// overflow.
4468cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidisclass DataRecursiveIntBinOpEvaluator {
4469cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  struct EvalResult {
4470cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    APValue Val;
4471cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    bool Failed;
4472cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4473cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    EvalResult() : Failed(false) { }
4474cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4475cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    void swap(EvalResult &RHS) {
4476cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      Val.swap(RHS.Val);
4477cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      Failed = RHS.Failed;
4478cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      RHS.Failed = false;
4479cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    }
4480cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  };
4481cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4482cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  struct Job {
4483cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    const Expr *E;
4484cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    EvalResult LHSResult; // meaningful only for binary operator expression.
4485cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    enum { AnyExprKind, BinOpKind, BinOpVisitedLHSKind } Kind;
4486cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4487cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    Job() : StoredInfo(0) { }
4488cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    void startSpeculativeEval(EvalInfo &Info) {
4489cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      OldEvalStatus = Info.EvalStatus;
4490cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      Info.EvalStatus.Diag = 0;
4491cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      StoredInfo = &Info;
4492cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    }
4493cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    ~Job() {
4494cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      if (StoredInfo) {
4495cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        StoredInfo->EvalStatus = OldEvalStatus;
4496cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      }
4497cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    }
4498cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  private:
4499cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    EvalInfo *StoredInfo; // non-null if status changed.
4500cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    Expr::EvalStatus OldEvalStatus;
4501cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  };
4502cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4503cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  SmallVector<Job, 16> Queue;
4504cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4505cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  IntExprEvaluator &IntEval;
4506cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  EvalInfo &Info;
4507cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  APValue &FinalResult;
4508cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4509cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidispublic:
4510cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  DataRecursiveIntBinOpEvaluator(IntExprEvaluator &IntEval, APValue &Result)
4511cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    : IntEval(IntEval), Info(IntEval.getEvalInfo()), FinalResult(Result) { }
4512cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4513cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  /// \brief True if \param E is a binary operator that we are going to handle
4514cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  /// data recursively.
4515cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  /// We handle binary operators that are comma, logical, or that have operands
4516cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  /// with integral or enumeration type.
4517cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  static bool shouldEnqueue(const BinaryOperator *E) {
4518cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return E->getOpcode() == BO_Comma ||
4519cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis           E->isLogicalOp() ||
4520cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis           (E->getLHS()->getType()->isIntegralOrEnumerationType() &&
4521cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis            E->getRHS()->getType()->isIntegralOrEnumerationType());
4522cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4523cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4524cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  bool Traverse(const BinaryOperator *E) {
4525cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    enqueue(E);
4526cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    EvalResult PrevResult;
4527b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu    while (!Queue.empty())
4528b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu      process(PrevResult);
4529b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu
4530b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu    if (PrevResult.Failed) return false;
4531cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4532cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    FinalResult.swap(PrevResult.Val);
4533cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return true;
4534a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman  }
4535a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman
4536cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidisprivate:
4537cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  bool Success(uint64_t Value, const Expr *E, APValue &Result) {
4538cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return IntEval.Success(Value, E, Result);
4539cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4540cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  bool Success(const APSInt &Value, const Expr *E, APValue &Result) {
4541cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return IntEval.Success(Value, E, Result);
4542cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4543cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  bool Error(const Expr *E) {
4544cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return IntEval.Error(E);
4545cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4546cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  bool Error(const Expr *E, diag::kind D) {
4547cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return IntEval.Error(E, D);
4548cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4549cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4550cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) {
4551cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return Info.CCEDiag(E, D);
4552cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4553cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
45549293fff58aa0198fa7a6bb504169bcac01dbbff7Argyrios Kyrtzidis  // \brief Returns true if visiting the RHS is necessary, false otherwise.
45559293fff58aa0198fa7a6bb504169bcac01dbbff7Argyrios Kyrtzidis  bool VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E,
4556cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                         bool &SuppressRHSDiags);
45572fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis
4558cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  bool VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult,
4559cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                  const BinaryOperator *E, APValue &Result);
4560cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4561cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  void EvaluateExpr(const Expr *E, EvalResult &Result) {
4562cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    Result.Failed = !Evaluate(Result.Val, Info, E);
4563cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    if (Result.Failed)
4564cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      Result.Val = APValue();
4565cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4566cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4567b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu  void process(EvalResult &Result);
4568cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4569cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  void enqueue(const Expr *E) {
4570cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    E = E->IgnoreParens();
4571cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    Queue.resize(Queue.size()+1);
4572cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    Queue.back().E = E;
4573cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    Queue.back().Kind = Job::AnyExprKind;
4574cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4575cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis};
4576cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4577cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis}
4578cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4579cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidisbool DataRecursiveIntBinOpEvaluator::
45809293fff58aa0198fa7a6bb504169bcac01dbbff7Argyrios Kyrtzidis       VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E,
4581cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                         bool &SuppressRHSDiags) {
4582cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  if (E->getOpcode() == BO_Comma) {
4583cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    // Ignore LHS but note if we could not evaluate it.
4584cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    if (LHSResult.Failed)
4585cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      Info.EvalStatus.HasSideEffects = true;
4586cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return true;
4587cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4588cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4589cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  if (E->isLogicalOp()) {
4590cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    bool lhsResult;
4591cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    if (HandleConversionToBool(LHSResult.Val, lhsResult)) {
45922fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis      // We were able to evaluate the LHS, see if we can get away with not
45932fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis      // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
4594cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      if (lhsResult == (E->getOpcode() == BO_LOr)) {
45959293fff58aa0198fa7a6bb504169bcac01dbbff7Argyrios Kyrtzidis        Success(lhsResult, E, LHSResult.Val);
45969293fff58aa0198fa7a6bb504169bcac01dbbff7Argyrios Kyrtzidis        return false; // Ignore RHS
45972fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis      }
45982fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis    } else {
45992fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis      // Since we weren't able to evaluate the left hand side, it
46002fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis      // must have had side effects.
46012fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis      Info.EvalStatus.HasSideEffects = true;
4602cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4603cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // We can't evaluate the LHS; however, sometimes the result
4604cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
4605cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // Don't ignore RHS and suppress diagnostics from this arm.
4606cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      SuppressRHSDiags = true;
4607cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    }
4608cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4609cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return true;
4610cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4611cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4612cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  assert(E->getLHS()->getType()->isIntegralOrEnumerationType() &&
4613cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis         E->getRHS()->getType()->isIntegralOrEnumerationType());
4614cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4615cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  if (LHSResult.Failed && !Info.keepEvaluatingAfterFailure())
46169293fff58aa0198fa7a6bb504169bcac01dbbff7Argyrios Kyrtzidis    return false; // Ignore RHS;
46179293fff58aa0198fa7a6bb504169bcac01dbbff7Argyrios Kyrtzidis
4618cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  return true;
4619cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis}
46202fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis
4621cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidisbool DataRecursiveIntBinOpEvaluator::
4622cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis       VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult,
4623cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                  const BinaryOperator *E, APValue &Result) {
4624cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  if (E->getOpcode() == BO_Comma) {
4625cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    if (RHSResult.Failed)
4626cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      return false;
4627cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    Result = RHSResult.Val;
4628cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return true;
4629cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4630cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4631cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  if (E->isLogicalOp()) {
4632cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    bool lhsResult, rhsResult;
4633cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    bool LHSIsOK = HandleConversionToBool(LHSResult.Val, lhsResult);
4634cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    bool RHSIsOK = HandleConversionToBool(RHSResult.Val, rhsResult);
4635cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4636cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    if (LHSIsOK) {
4637cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      if (RHSIsOK) {
4638cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        if (E->getOpcode() == BO_LOr)
4639cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis          return Success(lhsResult || rhsResult, E, Result);
4640cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        else
4641cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis          return Success(lhsResult && rhsResult, E, Result);
4642cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      }
4643cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    } else {
4644cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      if (RHSIsOK) {
46452fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis        // We can't evaluate the LHS; however, sometimes the result
46462fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis        // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
46472fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis        if (rhsResult == (E->getOpcode() == BO_LOr))
4648cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis          return Success(rhsResult, E, Result);
46492fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis      }
46502fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis    }
4651cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
46522fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis    return false;
46532fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis  }
4654cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4655cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  assert(E->getLHS()->getType()->isIntegralOrEnumerationType() &&
4656cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis         E->getRHS()->getType()->isIntegralOrEnumerationType());
4657cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4658cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  if (LHSResult.Failed || RHSResult.Failed)
4659cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return false;
4660cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4661cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  const APValue &LHSVal = LHSResult.Val;
4662cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  const APValue &RHSVal = RHSResult.Val;
4663cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4664cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  // Handle cases like (unsigned long)&a + 4.
4665cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) {
4666cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    Result = LHSVal;
4667cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    CharUnits AdditionalOffset = CharUnits::fromQuantity(
4668cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                                                         RHSVal.getInt().getZExtValue());
4669cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    if (E->getOpcode() == BO_Add)
4670cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      Result.getLValueOffset() += AdditionalOffset;
4671cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    else
4672cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      Result.getLValueOffset() -= AdditionalOffset;
4673cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return true;
4674cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4675cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4676cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  // Handle cases like 4 + (unsigned long)&a
4677cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  if (E->getOpcode() == BO_Add &&
4678cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      RHSVal.isLValue() && LHSVal.isInt()) {
4679cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    Result = RHSVal;
4680cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    Result.getLValueOffset() += CharUnits::fromQuantity(
4681cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                                                        LHSVal.getInt().getZExtValue());
4682cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return true;
4683cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4684cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4685cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  if (E->getOpcode() == BO_Sub && LHSVal.isLValue() && RHSVal.isLValue()) {
4686cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    // Handle (intptr_t)&&A - (intptr_t)&&B.
4687cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    if (!LHSVal.getLValueOffset().isZero() ||
4688cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        !RHSVal.getLValueOffset().isZero())
4689cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      return false;
4690cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    const Expr *LHSExpr = LHSVal.getLValueBase().dyn_cast<const Expr*>();
4691cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    const Expr *RHSExpr = RHSVal.getLValueBase().dyn_cast<const Expr*>();
4692cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    if (!LHSExpr || !RHSExpr)
4693cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      return false;
4694cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr);
4695cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr);
4696cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    if (!LHSAddrExpr || !RHSAddrExpr)
4697cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      return false;
4698cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    // Make sure both labels come from the same function.
4699cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    if (LHSAddrExpr->getLabel()->getDeclContext() !=
4700cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        RHSAddrExpr->getLabel()->getDeclContext())
4701cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      return false;
4702cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    Result = APValue(LHSAddrExpr, RHSAddrExpr);
4703cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return true;
4704cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4705cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4706cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  // All the following cases expect both operands to be an integer
4707cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  if (!LHSVal.isInt() || !RHSVal.isInt())
4708cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return Error(E);
4709cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4710cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  const APSInt &LHS = LHSVal.getInt();
4711cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  APSInt RHS = RHSVal.getInt();
4712cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4713cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  switch (E->getOpcode()) {
4714cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    default:
4715cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      return Error(E);
4716cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_Mul:
4717cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      return Success(CheckedIntArithmetic(Info, E, LHS, RHS,
4718cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                                          LHS.getBitWidth() * 2,
4719cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                                          std::multiplies<APSInt>()), E,
4720cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                     Result);
4721cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_Add:
4722cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      return Success(CheckedIntArithmetic(Info, E, LHS, RHS,
4723cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                                          LHS.getBitWidth() + 1,
4724cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                                          std::plus<APSInt>()), E, Result);
4725cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_Sub:
4726cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      return Success(CheckedIntArithmetic(Info, E, LHS, RHS,
4727cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                                          LHS.getBitWidth() + 1,
4728cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                                          std::minus<APSInt>()), E, Result);
4729cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_And: return Success(LHS & RHS, E, Result);
4730cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_Xor: return Success(LHS ^ RHS, E, Result);
4731cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_Or:  return Success(LHS | RHS, E, Result);
4732cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_Div:
4733cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_Rem:
4734cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      if (RHS == 0)
4735cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        return Error(E, diag::note_expr_divide_by_zero);
4736cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // Check for overflow case: INT_MIN / -1 or INT_MIN % -1. The latter is
4737cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // not actually undefined behavior in C++11 due to a language defect.
4738cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      if (RHS.isNegative() && RHS.isAllOnesValue() &&
4739cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis          LHS.isSigned() && LHS.isMinSignedValue())
4740cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        HandleOverflow(Info, E, -LHS.extend(LHS.getBitWidth() + 1), E->getType());
4741cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      return Success(E->getOpcode() == BO_Rem ? LHS % RHS : LHS / RHS, E,
4742cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis                     Result);
4743cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_Shl: {
4744cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // During constant-folding, a negative shift is an opposite shift. Such
4745cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // a shift is not a constant expression.
4746cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      if (RHS.isSigned() && RHS.isNegative()) {
4747cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        CCEDiag(E, diag::note_constexpr_negative_shift) << RHS;
4748cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        RHS = -RHS;
4749cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        goto shift_right;
4750cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      }
4751cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4752cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    shift_left:
4753cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // C++11 [expr.shift]p1: Shift width must be less than the bit width of
4754cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // the shifted type.
4755cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1);
4756cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      if (SA != RHS) {
4757cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        CCEDiag(E, diag::note_constexpr_large_shift)
4758cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        << RHS << E->getType() << LHS.getBitWidth();
4759cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      } else if (LHS.isSigned()) {
4760cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        // C++11 [expr.shift]p2: A signed left shift must have a non-negative
4761cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        // operand, and must not overflow the corresponding unsigned type.
4762cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        if (LHS.isNegative())
4763cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis          CCEDiag(E, diag::note_constexpr_lshift_of_negative) << LHS;
4764cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        else if (LHS.countLeadingZeros() < SA)
4765cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis          CCEDiag(E, diag::note_constexpr_lshift_discards);
4766cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      }
4767cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4768cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      return Success(LHS << SA, E, Result);
4769cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    }
4770cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_Shr: {
4771cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // During constant-folding, a negative shift is an opposite shift. Such a
4772cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // shift is not a constant expression.
4773cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      if (RHS.isSigned() && RHS.isNegative()) {
4774cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        CCEDiag(E, diag::note_constexpr_negative_shift) << RHS;
4775cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        RHS = -RHS;
4776cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        goto shift_left;
4777cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      }
4778cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4779cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    shift_right:
4780cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // C++11 [expr.shift]p1: Shift width must be less than the bit width of the
4781cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      // shifted type.
4782cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1);
4783cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      if (SA != RHS)
4784cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        CCEDiag(E, diag::note_constexpr_large_shift)
4785cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        << RHS << E->getType() << LHS.getBitWidth();
4786cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4787cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      return Success(LHS >> SA, E, Result);
4788cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    }
4789cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4790cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_LT: return Success(LHS < RHS, E, Result);
4791cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_GT: return Success(LHS > RHS, E, Result);
4792cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_LE: return Success(LHS <= RHS, E, Result);
4793cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_GE: return Success(LHS >= RHS, E, Result);
4794cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_EQ: return Success(LHS == RHS, E, Result);
4795cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case BO_NE: return Success(LHS != RHS, E, Result);
4796cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4797cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis}
4798cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4799b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieuvoid DataRecursiveIntBinOpEvaluator::process(EvalResult &Result) {
4800cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  Job &job = Queue.back();
4801cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4802cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  switch (job.Kind) {
4803cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case Job::AnyExprKind: {
4804cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(job.E)) {
4805cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        if (shouldEnqueue(Bop)) {
4806cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis          job.Kind = Job::BinOpKind;
4807cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis          enqueue(Bop->getLHS());
4808b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu          return;
4809cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        }
4810cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      }
4811cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4812cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      EvaluateExpr(job.E, Result);
4813cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      Queue.pop_back();
4814b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu      return;
4815cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    }
4816cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4817cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case Job::BinOpKind: {
4818cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      const BinaryOperator *Bop = cast<BinaryOperator>(job.E);
4819cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      bool SuppressRHSDiags = false;
48209293fff58aa0198fa7a6bb504169bcac01dbbff7Argyrios Kyrtzidis      if (!VisitBinOpLHSOnly(Result, Bop, SuppressRHSDiags)) {
4821cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        Queue.pop_back();
4822b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu        return;
4823cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      }
4824cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      if (SuppressRHSDiags)
4825cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis        job.startSpeculativeEval(Info);
48269293fff58aa0198fa7a6bb504169bcac01dbbff7Argyrios Kyrtzidis      job.LHSResult.swap(Result);
4827cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      job.Kind = Job::BinOpVisitedLHSKind;
4828cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      enqueue(Bop->getRHS());
4829b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu      return;
4830cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    }
4831cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4832cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    case Job::BinOpVisitedLHSKind: {
4833cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      const BinaryOperator *Bop = cast<BinaryOperator>(job.E);
4834cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      EvalResult RHS;
4835cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      RHS.swap(Result);
4836b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu      Result.Failed = !VisitBinOp(job.LHSResult, RHS, Bop, Result.Val);
4837cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis      Queue.pop_back();
4838b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu      return;
4839cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    }
4840cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  }
4841cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4842cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  llvm_unreachable("Invalid Job::Kind!");
4843cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis}
4844cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4845cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidisbool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
4846cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  if (E->isAssignmentOp())
4847cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return Error(E);
4848cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis
4849cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  if (DataRecursiveIntBinOpEvaluator::shouldEnqueue(E))
4850cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis    return DataRecursiveIntBinOpEvaluator(*this, Result).Traverse(E);
485154176fdb044312b4b77c3da6682d3575b3728d30Chris Lattner
4852286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson  QualType LHSTy = E->getLHS()->getType();
4853286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson  QualType RHSTy = E->getRHS()->getType();
48544087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar
48554087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar  if (LHSTy->isAnyComplexType()) {
48564087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar    assert(RHSTy->isAnyComplexType() && "Invalid comparison");
4857f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    ComplexValue LHS, RHS;
48584087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar
4859745f5147e065900267c85a5568785a1991d4838fRichard Smith    bool LHSOK = EvaluateComplex(E->getLHS(), LHS, Info);
4860745f5147e065900267c85a5568785a1991d4838fRichard Smith    if (!LHSOK && !Info.keepEvaluatingAfterFailure())
48614087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar      return false;
48624087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar
4863745f5147e065900267c85a5568785a1991d4838fRichard Smith    if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK)
48644087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar      return false;
48654087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar
48664087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar    if (LHS.isComplexFloat()) {
48671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      APFloat::cmpResult CR_r =
48684087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar        LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
48691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      APFloat::cmpResult CR_i =
48704087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar        LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
48714087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar
48722de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      if (E->getOpcode() == BO_EQ)
4873131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar        return Success((CR_r == APFloat::cmpEqual &&
4874131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar                        CR_i == APFloat::cmpEqual), E);
4875131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar      else {
48762de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall        assert(E->getOpcode() == BO_NE &&
4877131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar               "Invalid complex comparison.");
48781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        return Success(((CR_r == APFloat::cmpGreaterThan ||
4879fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang                         CR_r == APFloat::cmpLessThan ||
4880fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang                         CR_r == APFloat::cmpUnordered) ||
48811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                        (CR_i == APFloat::cmpGreaterThan ||
4882fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang                         CR_i == APFloat::cmpLessThan ||
4883fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang                         CR_i == APFloat::cmpUnordered)), E);
4884131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar      }
48854087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar    } else {
48862de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      if (E->getOpcode() == BO_EQ)
4887131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar        return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
4888131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar                        LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
4889131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar      else {
48902de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall        assert(E->getOpcode() == BO_NE &&
4891131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar               "Invalid compex comparison.");
4892131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar        return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
4893131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar                        LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
4894131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar      }
48954087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar    }
48964087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar  }
48971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4898286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson  if (LHSTy->isRealFloatingType() &&
4899286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson      RHSTy->isRealFloatingType()) {
4900286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson    APFloat RHS(0.0), LHS(0.0);
49011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4902745f5147e065900267c85a5568785a1991d4838fRichard Smith    bool LHSOK = EvaluateFloat(E->getRHS(), RHS, Info);
4903745f5147e065900267c85a5568785a1991d4838fRichard Smith    if (!LHSOK && !Info.keepEvaluatingAfterFailure())
4904286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson      return false;
49051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4906745f5147e065900267c85a5568785a1991d4838fRichard Smith    if (!EvaluateFloat(E->getLHS(), LHS, Info) || !LHSOK)
4907286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson      return false;
49081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4909286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson    APFloat::cmpResult CR = LHS.compare(RHS);
4910529569e68d10b0fd3750fd2124faf742249b846bAnders Carlsson
4911286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson    switch (E->getOpcode()) {
4912286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson    default:
4913b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("Invalid binary operator!");
49142de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LT:
4915131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar      return Success(CR == APFloat::cmpLessThan, E);
49162de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_GT:
4917131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar      return Success(CR == APFloat::cmpGreaterThan, E);
49182de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LE:
4919131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar      return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
49202de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_GE:
49211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
4922131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar                     E);
49232de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_EQ:
4924131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar      return Success(CR == APFloat::cmpEqual, E);
49252de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_NE:
49261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return Success(CR == APFloat::cmpGreaterThan
4927fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang                     || CR == APFloat::cmpLessThan
4928fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang                     || CR == APFloat::cmpUnordered, E);
4929286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson    }
4930286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson  }
49311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4932ad02d7debd03ff275ac8ea27891a4ecccdb78068Eli Friedman  if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
4933625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith    if (E->getOpcode() == BO_Sub || E->isComparisonOp()) {
4934745f5147e065900267c85a5568785a1991d4838fRichard Smith      LValue LHSValue, RHSValue;
4935745f5147e065900267c85a5568785a1991d4838fRichard Smith
4936745f5147e065900267c85a5568785a1991d4838fRichard Smith      bool LHSOK = EvaluatePointer(E->getLHS(), LHSValue, Info);
4937745f5147e065900267c85a5568785a1991d4838fRichard Smith      if (!LHSOK && Info.keepEvaluatingAfterFailure())
49383068d117951a8df54bae9db039b56201ab10962bAnders Carlsson        return false;
4939a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman
4940745f5147e065900267c85a5568785a1991d4838fRichard Smith      if (!EvaluatePointer(E->getRHS(), RHSValue, Info) || !LHSOK)
49413068d117951a8df54bae9db039b56201ab10962bAnders Carlsson        return false;
4942a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman
4943625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith      // Reject differing bases from the normal codepath; we special-case
4944625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith      // comparisons to null.
4945625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith      if (!HasSameBase(LHSValue, RHSValue)) {
494665639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman        if (E->getOpcode() == BO_Sub) {
494765639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman          // Handle &&A - &&B.
494865639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman          if (!LHSValue.Offset.isZero() || !RHSValue.Offset.isZero())
494965639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman            return false;
495065639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman          const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr*>();
495165639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman          const Expr *RHSExpr = LHSValue.Base.dyn_cast<const Expr*>();
495265639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman          if (!LHSExpr || !RHSExpr)
495365639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman            return false;
495465639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman          const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr);
495565639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman          const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr);
495665639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman          if (!LHSAddrExpr || !RHSAddrExpr)
495765639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman            return false;
49585930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman          // Make sure both labels come from the same function.
49595930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman          if (LHSAddrExpr->getLabel()->getDeclContext() !=
49605930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman              RHSAddrExpr->getLabel()->getDeclContext())
49615930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman            return false;
49621aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith          Result = APValue(LHSAddrExpr, RHSAddrExpr);
496365639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman          return true;
496465639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman        }
49659e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith        // Inequalities and subtractions between unrelated pointers have
49669e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith        // unspecified or undefined behavior.
49675bc86103767c2abcbfdd6518e0ccbbbb6aa59e0fEli Friedman        if (!E->isEqualityOp())
4968f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith          return Error(E);
4969ffbda40a1fb7169591dc01771f3511178a2f727cEli Friedman        // A constant address may compare equal to the address of a symbol.
4970ffbda40a1fb7169591dc01771f3511178a2f727cEli Friedman        // The one exception is that address of an object cannot compare equal
4971c45061bd0c0fdad4df8eea7e9e5af186d11427e5Eli Friedman        // to a null pointer constant.
4972ffbda40a1fb7169591dc01771f3511178a2f727cEli Friedman        if ((!LHSValue.Base && !LHSValue.Offset.isZero()) ||
4973ffbda40a1fb7169591dc01771f3511178a2f727cEli Friedman            (!RHSValue.Base && !RHSValue.Offset.isZero()))
4974f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith          return Error(E);
49759e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith        // It's implementation-defined whether distinct literals will have
4976b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith        // distinct addresses. In clang, the result of such a comparison is
4977b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith        // unspecified, so it is not a constant expression. However, we do know
4978b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith        // that the address of a literal will be non-null.
497974f4634781cee06e28eb741bda5d0f936fdd1948Richard Smith        if ((IsLiteralLValue(LHSValue) || IsLiteralLValue(RHSValue)) &&
498074f4634781cee06e28eb741bda5d0f936fdd1948Richard Smith            LHSValue.Base && RHSValue.Base)
4981f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith          return Error(E);
49829e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith        // We can't tell whether weak symbols will end up pointing to the same
49839e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith        // object.
49849e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith        if (IsWeakLValue(LHSValue) || IsWeakLValue(RHSValue))
4985f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith          return Error(E);
49869e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith        // Pointers with different bases cannot represent the same object.
4987c45061bd0c0fdad4df8eea7e9e5af186d11427e5Eli Friedman        // (Note that clang defaults to -fmerge-all-constants, which can
4988c45061bd0c0fdad4df8eea7e9e5af186d11427e5Eli Friedman        // lead to inconsistent results for comparisons involving the address
4989c45061bd0c0fdad4df8eea7e9e5af186d11427e5Eli Friedman        // of a constant; this generally doesn't matter in practice.)
49909e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith        return Success(E->getOpcode() == BO_NE, E);
49915bc86103767c2abcbfdd6518e0ccbbbb6aa59e0fEli Friedman      }
4992a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman
499315efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith      const CharUnits &LHSOffset = LHSValue.getLValueOffset();
499415efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith      const CharUnits &RHSOffset = RHSValue.getLValueOffset();
499515efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith
4996f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator();
4997f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator();
4998f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
49992de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      if (E->getOpcode() == BO_Sub) {
5000f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        // C++11 [expr.add]p6:
5001f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        //   Unless both pointers point to elements of the same array object, or
5002f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        //   one past the last element of the array object, the behavior is
5003f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        //   undefined.
5004f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        if (!LHSDesignator.Invalid && !RHSDesignator.Invalid &&
5005f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith            !AreElementsOfSameArray(getType(LHSValue.Base),
5006f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith                                    LHSDesignator, RHSDesignator))
5007f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          CCEDiag(E, diag::note_constexpr_pointer_subtraction_not_same_array);
5008f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
50094992bdde387c5f033bb450a716eaabc0fda52688Chris Lattner        QualType Type = E->getLHS()->getType();
50104992bdde387c5f033bb450a716eaabc0fda52688Chris Lattner        QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
50113068d117951a8df54bae9db039b56201ab10962bAnders Carlsson
5012180f47959a066795cc0f409433023af448bb0328Richard Smith        CharUnits ElementSize;
501374e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith        if (!HandleSizeof(Info, E->getExprLoc(), ElementType, ElementSize))
5014180f47959a066795cc0f409433023af448bb0328Richard Smith          return false;
5015a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman
501615efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        // FIXME: LLVM and GCC both compute LHSOffset - RHSOffset at runtime,
501715efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        // and produce incorrect results when it overflows. Such behavior
501815efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        // appears to be non-conforming, but is common, so perhaps we should
501915efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        // assume the standard intended for such cases to be undefined behavior
502015efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        // and check for them.
502115efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith
502215efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        // Compute (LHSOffset - RHSOffset) / Size carefully, checking for
502315efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        // overflow in the final conversion to ptrdiff_t.
502415efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        APSInt LHS(
502515efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith          llvm::APInt(65, (int64_t)LHSOffset.getQuantity(), true), false);
502615efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        APSInt RHS(
502715efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith          llvm::APInt(65, (int64_t)RHSOffset.getQuantity(), true), false);
502815efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        APSInt ElemSize(
502915efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith          llvm::APInt(65, (int64_t)ElementSize.getQuantity(), true), false);
503015efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        APSInt TrueResult = (LHS - RHS) / ElemSize;
503115efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        APSInt Result = TrueResult.trunc(Info.Ctx.getIntWidth(E->getType()));
503215efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith
503315efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        if (Result.extend(65) != TrueResult)
503415efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith          HandleOverflow(Info, E, TrueResult, E->getType());
503515efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        return Success(Result, E);
5036ad02d7debd03ff275ac8ea27891a4ecccdb78068Eli Friedman      }
5037625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith
503882f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith      // C++11 [expr.rel]p3:
503982f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith      //   Pointers to void (after pointer conversions) can be compared, with a
504082f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith      //   result defined as follows: If both pointers represent the same
504182f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith      //   address or are both the null pointer value, the result is true if the
504282f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith      //   operator is <= or >= and false otherwise; otherwise the result is
504382f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith      //   unspecified.
504482f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith      // We interpret this as applying to pointers to *cv* void.
504582f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith      if (LHSTy->isVoidPointerType() && LHSOffset != RHSOffset &&
5046f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          E->isRelationalOp())
504782f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith        CCEDiag(E, diag::note_constexpr_void_comparison);
504882f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith
5049f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      // C++11 [expr.rel]p2:
5050f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      // - If two pointers point to non-static data members of the same object,
5051f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      //   or to subobjects or array elements fo such members, recursively, the
5052f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      //   pointer to the later declared member compares greater provided the
5053f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      //   two members have the same access control and provided their class is
5054f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      //   not a union.
5055f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      //   [...]
5056f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      // - Otherwise pointer comparisons are unspecified.
5057f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      if (!LHSDesignator.Invalid && !RHSDesignator.Invalid &&
5058f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          E->isRelationalOp()) {
5059f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        bool WasArrayIndex;
5060f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        unsigned Mismatch =
5061f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          FindDesignatorMismatch(getType(LHSValue.Base), LHSDesignator,
5062f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith                                 RHSDesignator, WasArrayIndex);
5063f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        // At the point where the designators diverge, the comparison has a
5064f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        // specified value if:
5065f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        //  - we are comparing array indices
5066f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        //  - we are comparing fields of a union, or fields with the same access
5067f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        // Otherwise, the result is unspecified and thus the comparison is not a
5068f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        // constant expression.
5069f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        if (!WasArrayIndex && Mismatch < LHSDesignator.Entries.size() &&
5070f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith            Mismatch < RHSDesignator.Entries.size()) {
5071f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          const FieldDecl *LF = getAsField(LHSDesignator.Entries[Mismatch]);
5072f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          const FieldDecl *RF = getAsField(RHSDesignator.Entries[Mismatch]);
5073f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          if (!LF && !RF)
5074f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith            CCEDiag(E, diag::note_constexpr_pointer_comparison_base_classes);
5075f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          else if (!LF)
5076f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith            CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field)
5077f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith              << getAsBaseClass(LHSDesignator.Entries[Mismatch])
5078f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith              << RF->getParent() << RF;
5079f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          else if (!RF)
5080f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith            CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field)
5081f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith              << getAsBaseClass(RHSDesignator.Entries[Mismatch])
5082f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith              << LF->getParent() << LF;
5083f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          else if (!LF->getParent()->isUnion() &&
5084f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith                   LF->getAccess() != RF->getAccess())
5085f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith            CCEDiag(E, diag::note_constexpr_pointer_comparison_differing_access)
5086f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith              << LF << LF->getAccess() << RF << RF->getAccess()
5087f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith              << LF->getParent();
5088f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        }
5089f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      }
5090f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
5091a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      // The comparison here must be unsigned, and performed with the same
5092a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      // width as the pointer.
5093a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      unsigned PtrSize = Info.Ctx.getTypeSize(LHSTy);
5094a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      uint64_t CompareLHS = LHSOffset.getQuantity();
5095a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      uint64_t CompareRHS = RHSOffset.getQuantity();
5096a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      assert(PtrSize <= 64 && "Unexpected pointer width");
5097a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      uint64_t Mask = ~0ULL >> (64 - PtrSize);
5098a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      CompareLHS &= Mask;
5099a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      CompareRHS &= Mask;
5100a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman
51012850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman      // If there is a base and this is a relational operator, we can only
51022850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman      // compare pointers within the object in question; otherwise, the result
51032850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman      // depends on where the object is located in memory.
51042850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman      if (!LHSValue.Base.isNull() && E->isRelationalOp()) {
51052850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman        QualType BaseTy = getType(LHSValue.Base);
51062850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman        if (BaseTy->isIncompleteType())
51072850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman          return Error(E);
51082850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman        CharUnits Size = Info.Ctx.getTypeSizeInChars(BaseTy);
51092850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman        uint64_t OffsetLimit = Size.getQuantity();
51102850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman        if (CompareLHS > OffsetLimit || CompareRHS > OffsetLimit)
51112850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman          return Error(E);
51122850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman      }
51132850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman
5114625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith      switch (E->getOpcode()) {
5115625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith      default: llvm_unreachable("missing comparison operator");
5116a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      case BO_LT: return Success(CompareLHS < CompareRHS, E);
5117a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      case BO_GT: return Success(CompareLHS > CompareRHS, E);
5118a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      case BO_LE: return Success(CompareLHS <= CompareRHS, E);
5119a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      case BO_GE: return Success(CompareLHS >= CompareRHS, E);
5120a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      case BO_EQ: return Success(CompareLHS == CompareRHS, E);
5121a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman      case BO_NE: return Success(CompareLHS != CompareRHS, E);
5122ad02d7debd03ff275ac8ea27891a4ecccdb78068Eli Friedman      }
51233068d117951a8df54bae9db039b56201ab10962bAnders Carlsson    }
51243068d117951a8df54bae9db039b56201ab10962bAnders Carlsson  }
5125b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith
5126b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith  if (LHSTy->isMemberPointerType()) {
5127b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    assert(E->isEqualityOp() && "unexpected member pointer operation");
5128b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    assert(RHSTy->isMemberPointerType() && "invalid comparison");
5129b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith
5130b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    MemberPtr LHSValue, RHSValue;
5131b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith
5132b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    bool LHSOK = EvaluateMemberPointer(E->getLHS(), LHSValue, Info);
5133b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    if (!LHSOK && Info.keepEvaluatingAfterFailure())
5134b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith      return false;
5135b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith
5136b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    if (!EvaluateMemberPointer(E->getRHS(), RHSValue, Info) || !LHSOK)
5137b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith      return false;
5138b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith
5139b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    // C++11 [expr.eq]p2:
5140b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    //   If both operands are null, they compare equal. Otherwise if only one is
5141b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    //   null, they compare unequal.
5142b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    if (!LHSValue.getDecl() || !RHSValue.getDecl()) {
5143b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith      bool Equal = !LHSValue.getDecl() && !RHSValue.getDecl();
5144b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith      return Success(E->getOpcode() == BO_EQ ? Equal : !Equal, E);
5145b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    }
5146b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith
5147b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    //   Otherwise if either is a pointer to a virtual member function, the
5148b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    //   result is unspecified.
5149b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(LHSValue.getDecl()))
5150b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith      if (MD->isVirtual())
5151b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith        CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD;
5152b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(RHSValue.getDecl()))
5153b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith      if (MD->isVirtual())
5154b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith        CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD;
5155b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith
5156b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    //   Otherwise they compare equal if and only if they would refer to the
5157b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    //   same member of the same most derived object or the same subobject if
5158b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    //   they were dereferenced with a hypothetical object of the associated
5159b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    //   class type.
5160b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    bool Equal = LHSValue == RHSValue;
5161b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    return Success(E->getOpcode() == BO_EQ ? Equal : !Equal, E);
5162b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith  }
5163b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith
516426f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith  if (LHSTy->isNullPtrType()) {
516526f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith    assert(E->isComparisonOp() && "unexpected nullptr operation");
516626f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith    assert(RHSTy->isNullPtrType() && "missing pointer conversion");
516726f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith    // C++11 [expr.rel]p4, [expr.eq]p3: If two operands of type std::nullptr_t
516826f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith    // are compared, the result is true of the operator is <=, >= or ==, and
516926f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith    // false otherwise.
517026f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith    BinaryOperator::Opcode Opcode = E->getOpcode();
517126f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith    return Success(Opcode == BO_EQ || Opcode == BO_LE || Opcode == BO_GE, E);
517226f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith  }
517326f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith
5174cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  assert((!LHSTy->isIntegralOrEnumerationType() ||
5175cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis          !RHSTy->isIntegralOrEnumerationType()) &&
5176cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis         "DataRecursiveIntBinOpEvaluator should have handled integral types");
5177cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  // We can't continue from here for non-integral types.
5178cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis  return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
5179a25ae3d68d84d2b89907f998df6a396549589da5Anders Carlsson}
5180a25ae3d68d84d2b89907f998df6a396549589da5Anders Carlsson
51818b752f10c394b140f9ef89e049cbad1a7676fc25Ken DyckCharUnits IntExprEvaluator::GetAlignOfType(QualType T) {
51825d484e8cf710207010720589d89602233de61d01Sebastian Redl  // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
51835d484e8cf710207010720589d89602233de61d01Sebastian Redl  //   result shall be the alignment of the referenced type."
51845d484e8cf710207010720589d89602233de61d01Sebastian Redl  if (const ReferenceType *Ref = T->getAs<ReferenceType>())
51855d484e8cf710207010720589d89602233de61d01Sebastian Redl    T = Ref->getPointeeType();
51869f1210c3280104417a4ad30f0a00825ac8fa718aChad Rosier
51879f1210c3280104417a4ad30f0a00825ac8fa718aChad Rosier  // __alignof is defined to return the preferred alignment.
51889f1210c3280104417a4ad30f0a00825ac8fa718aChad Rosier  return Info.Ctx.toCharUnitsFromBits(
51899f1210c3280104417a4ad30f0a00825ac8fa718aChad Rosier    Info.Ctx.getPreferredTypeAlign(T.getTypePtr()));
5190e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner}
5191e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner
51928b752f10c394b140f9ef89e049cbad1a7676fc25Ken DyckCharUnits IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
5193af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  E = E->IgnoreParens();
5194af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner
5195af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  // alignof decl is always accepted, even if it doesn't make sense: we default
51961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // to 1 in those cases.
5197af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
51988b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck    return Info.Ctx.getDeclAlign(DRE->getDecl(),
51998b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck                                 /*RefAsPointee*/true);
5200a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman
5201af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
52028b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck    return Info.Ctx.getDeclAlign(ME->getMemberDecl(),
52038b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck                                 /*RefAsPointee*/true);
5204af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner
5205e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner  return GetAlignOfType(E->getType());
5206e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner}
5207e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner
5208e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner
5209f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne/// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with
5210f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne/// a result as the expression's type.
5211f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbournebool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr(
5212f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne                                    const UnaryExprOrTypeTraitExpr *E) {
5213f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  switch(E->getKind()) {
5214f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  case UETT_AlignOf: {
5215e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner    if (E->isArgumentType())
52164f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck      return Success(GetAlignOfType(E->getArgumentType()), E);
5217e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner    else
52184f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck      return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
5219e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner  }
5220a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman
5221f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  case UETT_VecStep: {
5222f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    QualType Ty = E->getTypeOfArgument();
52230518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl
5224f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    if (Ty->isVectorType()) {
5225f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne      unsigned n = Ty->getAs<VectorType>()->getNumElements();
5226a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman
5227f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne      // The vec_step built-in functions that take a 3-component
5228f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne      // vector return 4. (OpenCL 1.1 spec 6.11.12)
5229f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne      if (n == 3)
5230f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne        n = 4;
5231f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne
5232f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne      return Success(n, E);
5233f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    } else
5234f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne      return Success(1, E);
5235f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  }
5236f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne
5237f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  case UETT_SizeOf: {
5238f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    QualType SrcTy = E->getTypeOfArgument();
5239f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
5240f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    //   the result is the size of the referenced type."
5241f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>())
5242f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne      SrcTy = Ref->getPointeeType();
5243f2da9dfef96dc11b7b5effb1d02cb427b2d71599Eli Friedman
5244180f47959a066795cc0f409433023af448bb0328Richard Smith    CharUnits Sizeof;
524574e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith    if (!HandleSizeof(Info, E->getExprLoc(), SrcTy, Sizeof))
5246f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne      return false;
5247180f47959a066795cc0f409433023af448bb0328Richard Smith    return Success(Sizeof, E);
5248f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  }
5249f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  }
5250f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne
5251f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  llvm_unreachable("unknown expr/type trait");
5252fcee0019b76f9f368f2b3d6d4048a98232593f29Chris Lattner}
5253fcee0019b76f9f368f2b3d6d4048a98232593f29Chris Lattner
52548cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) {
52558ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  CharUnits Result;
52568cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  unsigned n = OOE->getNumComponents();
52578ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  if (n == 0)
5258f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(OOE);
52598cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  QualType CurrentType = OOE->getTypeSourceInfo()->getType();
52608ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  for (unsigned i = 0; i != n; ++i) {
52618ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    OffsetOfExpr::OffsetOfNode ON = OOE->getComponent(i);
52628ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    switch (ON.getKind()) {
52638ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    case OffsetOfExpr::OffsetOfNode::Array: {
52648cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne      const Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex());
52658ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      APSInt IdxResult;
52668ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      if (!EvaluateInteger(Idx, IdxResult, Info))
52678ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor        return false;
52688ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType);
52698ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      if (!AT)
5270f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(OOE);
52718ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      CurrentType = AT->getElementType();
52728ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType);
52738ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      Result += IdxResult.getSExtValue() * ElementSize;
52748ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor        break;
52758ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    }
5276f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
52778ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    case OffsetOfExpr::OffsetOfNode::Field: {
52788ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      FieldDecl *MemberDecl = ON.getField();
52798ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      const RecordType *RT = CurrentType->getAs<RecordType>();
5280f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      if (!RT)
5281f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(OOE);
52828ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      RecordDecl *RD = RT->getDecl();
52838ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
5284ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      unsigned i = MemberDecl->getFieldIndex();
5285cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      assert(i < RL.getFieldCount() && "offsetof field in wrong type");
5286fb1e3bc29b667f4275e1d5a43d64ec173f4f9a7dKen Dyck      Result += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i));
52878ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      CurrentType = MemberDecl->getType().getNonReferenceType();
52888ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      break;
52898ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    }
5290f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
52918ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    case OffsetOfExpr::OffsetOfNode::Identifier:
52928ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      llvm_unreachable("dependent __builtin_offsetof");
5293f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
5294cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor    case OffsetOfExpr::OffsetOfNode::Base: {
5295cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      CXXBaseSpecifier *BaseSpec = ON.getBase();
5296cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      if (BaseSpec->isVirtual())
5297f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(OOE);
5298cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor
5299cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      // Find the layout of the class whose base we are looking into.
5300cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      const RecordType *RT = CurrentType->getAs<RecordType>();
5301f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      if (!RT)
5302f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(OOE);
5303cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      RecordDecl *RD = RT->getDecl();
5304cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
5305cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor
5306cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      // Find the base class itself.
5307cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      CurrentType = BaseSpec->getType();
5308cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      const RecordType *BaseRT = CurrentType->getAs<RecordType>();
5309cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      if (!BaseRT)
5310f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(OOE);
5311cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor
5312cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      // Add the offset to the base.
53137c7f820d70c925b29290a8563b59615816a827fcKen Dyck      Result += RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl()));
5314cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      break;
5315cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor    }
53168ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    }
53178ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  }
53188cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  return Success(Result, OOE);
53198ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor}
53208ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor
5321b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattnerbool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
532275a4881047deeb3a300ff9293dc6ba8570048bb5Chris Lattner  switch (E->getOpcode()) {
53234c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner  default:
532475a4881047deeb3a300ff9293dc6ba8570048bb5Chris Lattner    // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
532575a4881047deeb3a300ff9293dc6ba8570048bb5Chris Lattner    // See C99 6.6p3.
5326f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
53272de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Extension:
53284c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner    // FIXME: Should extension allow i-c-e extension expressions in its scope?
53294c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner    // If so, we could clear the diagnostic ID.
5330f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Visit(E->getSubExpr());
53312de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Plus:
5332c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    // The result is just the value.
5333f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Visit(E->getSubExpr());
5334f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  case UO_Minus: {
5335f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!Visit(E->getSubExpr()))
5336f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return false;
5337f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!Result.isInt()) return Error(E);
5338789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    const APSInt &Value = Result.getInt();
5339789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    if (Value.isSigned() && Value.isMinSignedValue())
5340789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith      HandleOverflow(Info, E, -Value.extend(Value.getBitWidth() + 1),
5341789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith                     E->getType());
5342789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    return Success(-Value, E);
5343f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
5344f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  case UO_Not: {
5345f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!Visit(E->getSubExpr()))
5346f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return false;
5347f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!Result.isInt()) return Error(E);
5348f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Success(~Result.getInt(), E);
5349f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
5350f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  case UO_LNot: {
5351f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    bool bres;
5352f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!EvaluateAsBooleanCondition(E->getSubExpr(), bres, Info))
5353f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return false;
5354f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Success(!bres, E);
5355f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
535606a3675627e3b3c47b49c689c8e404a33144194aAnders Carlsson  }
5357a25ae3d68d84d2b89907f998df6a396549589da5Anders Carlsson}
53581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5359732b2236ae4a4f11e7642677cebbd169c07ea877Chris Lattner/// HandleCast - This is used to evaluate implicit or explicit casts where the
5360732b2236ae4a4f11e7642677cebbd169c07ea877Chris Lattner/// result type is integer.
53618cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool IntExprEvaluator::VisitCastExpr(const CastExpr *E) {
53628cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  const Expr *SubExpr = E->getSubExpr();
536382206e267ce6cc709797127616f64672d255b310Anders Carlsson  QualType DestType = E->getType();
5364b92dac8bc2f6f73919825f9af693a8a7e89ae1d4Daniel Dunbar  QualType SrcType = SubExpr->getType();
536582206e267ce6cc709797127616f64672d255b310Anders Carlsson
536646a523285928aa07bf14803178dc04616ac85994Eli Friedman  switch (E->getCastKind()) {
536746a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_BaseToDerived:
536846a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_DerivedToBase:
536946a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_UncheckedDerivedToBase:
537046a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_Dynamic:
537146a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_ToUnion:
537246a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_ArrayToPointerDecay:
537346a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_FunctionToPointerDecay:
537446a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_NullToPointer:
537546a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_NullToMemberPointer:
537646a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_BaseToDerivedMemberPointer:
537746a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_DerivedToBaseMemberPointer:
53784d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  case CK_ReinterpretMemberPointer:
537946a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_ConstructorConversion:
538046a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_IntegralToPointer:
538146a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_ToVoid:
538246a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_VectorSplat:
538346a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_IntegralToFloating:
538446a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_FloatingCast:
53851d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_CPointerToObjCPointerCast:
53861d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_BlockPointerToObjCPointerCast:
538746a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_AnyPointerToBlockPointerCast:
538846a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_ObjCObjectLValueCast:
538946a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_FloatingRealToComplex:
539046a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_FloatingComplexToReal:
539146a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_FloatingComplexCast:
539246a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_FloatingComplexToIntegralComplex:
539346a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_IntegralRealToComplex:
539446a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_IntegralComplexCast:
539546a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_IntegralComplexToFloatingComplex:
539646a523285928aa07bf14803178dc04616ac85994Eli Friedman    llvm_unreachable("invalid cast kind for integral value");
539746a523285928aa07bf14803178dc04616ac85994Eli Friedman
5398e50c297f92914ca996deb8b597624193273b62e4Eli Friedman  case CK_BitCast:
539946a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_Dependent:
540046a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_LValueBitCast:
540133e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCProduceObject:
540233e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCConsumeObject:
540333e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCReclaimReturnedObject:
540433e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCExtendBlockObject:
5405ac1303eca6cbe3e623fb5ec6fe7ec184ef4b0dfaDouglas Gregor  case CK_CopyAndAutoreleaseBlockObject:
5406f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
540746a523285928aa07bf14803178dc04616ac85994Eli Friedman
54087d580a4e9e47dffc3c17aa2b957ac57ca3c4e451Richard Smith  case CK_UserDefinedConversion:
540946a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_LValueToRValue:
54107a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall  case CK_AtomicToNonAtomic:
54117a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall  case CK_NonAtomicToAtomic:
541246a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_NoOp:
5413c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return ExprEvaluatorBaseTy::VisitCastExpr(E);
541446a523285928aa07bf14803178dc04616ac85994Eli Friedman
541546a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_MemberPointerToBoolean:
541646a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_PointerToBoolean:
541746a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_IntegralToBoolean:
541846a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_FloatingToBoolean:
541946a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_FloatingComplexToBoolean:
542046a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_IntegralComplexToBoolean: {
54214efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman    bool BoolResult;
5422c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    if (!EvaluateAsBooleanCondition(SubExpr, BoolResult, Info))
54234efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman      return false;
5424131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar    return Success(BoolResult, E);
54254efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman  }
54264efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
542746a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_IntegralCast: {
5428732b2236ae4a4f11e7642677cebbd169c07ea877Chris Lattner    if (!Visit(SubExpr))
5429b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattner      return false;
5430a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar
5431be26570e3faa009bdcefedfaf04473e518940520Eli Friedman    if (!Result.isInt()) {
543265639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman      // Allow casts of address-of-label differences if they are no-ops
543365639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman      // or narrowing.  (The narrowing case isn't actually guaranteed to
543465639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman      // be constant-evaluatable except in some narrow cases which are hard
543565639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman      // to detect here.  We let it through on the assumption the user knows
543665639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman      // what they are doing.)
543765639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman      if (Result.isAddrLabelDiff())
543865639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman        return Info.Ctx.getTypeSize(DestType) <= Info.Ctx.getTypeSize(SrcType);
5439be26570e3faa009bdcefedfaf04473e518940520Eli Friedman      // Only allow casts of lvalues if they are lossless.
5440be26570e3faa009bdcefedfaf04473e518940520Eli Friedman      return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
5441be26570e3faa009bdcefedfaf04473e518940520Eli Friedman    }
544230c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar
5443f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith    return Success(HandleIntToIntCast(Info, E, DestType, SrcType,
5444f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith                                      Result.getInt()), E);
5445732b2236ae4a4f11e7642677cebbd169c07ea877Chris Lattner  }
54461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
544746a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_PointerToIntegral: {
5448c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith    CCEDiag(E, diag::note_constexpr_invalid_cast) << 2;
5449c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith
5450efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    LValue LV;
545187eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner    if (!EvaluatePointer(SubExpr, LV, Info))
5452b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattner      return false;
54534efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
5454dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar    if (LV.getLValueBase()) {
5455dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar      // Only allow based lvalue casts if they are lossless.
5456f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith      // FIXME: Allow a larger integer size than the pointer size, and allow
5457f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith      // narrowing back down to pointer width in subsequent integral casts.
5458f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith      // FIXME: Check integer type's active bits, not its type size.
5459dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar      if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
5460f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(E);
5461dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar
5462b755a9da095d2f2f04444797f1e1a9511693815bRichard Smith      LV.Designator.setInvalid();
5463efdb83e26f9a1fd2566afe54461216cd84814d42John McCall      LV.moveInto(Result);
5464dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar      return true;
5465dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar    }
54664efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
5467a73058324197b7bdfd19307965954f626e26199dKen Dyck    APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(),
5468a73058324197b7bdfd19307965954f626e26199dKen Dyck                                         SrcType);
5469f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith    return Success(HandleIntToIntCast(Info, E, DestType, SrcType, AsInt), E);
54702bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson  }
54714efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
547246a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_IntegralComplexToReal: {
5473f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    ComplexValue C;
54741725f683432715e5afe34d476024bd6f16eac3fcEli Friedman    if (!EvaluateComplex(SubExpr, C, Info))
54751725f683432715e5afe34d476024bd6f16eac3fcEli Friedman      return false;
547646a523285928aa07bf14803178dc04616ac85994Eli Friedman    return Success(C.getComplexIntReal(), E);
54771725f683432715e5afe34d476024bd6f16eac3fcEli Friedman  }
54782217c87bdc5ab357046a5453bdb06f469c41024eEli Friedman
547946a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_FloatingToIntegral: {
548046a523285928aa07bf14803178dc04616ac85994Eli Friedman    APFloat F(0.0);
548146a523285928aa07bf14803178dc04616ac85994Eli Friedman    if (!EvaluateFloat(SubExpr, F, Info))
548246a523285928aa07bf14803178dc04616ac85994Eli Friedman      return false;
5483732b2236ae4a4f11e7642677cebbd169c07ea877Chris Lattner
5484c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    APSInt Value;
5485c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    if (!HandleFloatToIntCast(Info, E, SrcType, F, DestType, Value))
5486c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      return false;
5487c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return Success(Value, E);
548846a523285928aa07bf14803178dc04616ac85994Eli Friedman  }
548946a523285928aa07bf14803178dc04616ac85994Eli Friedman  }
54901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
549146a523285928aa07bf14803178dc04616ac85994Eli Friedman  llvm_unreachable("unknown cast resulting in integral value");
5492a25ae3d68d84d2b89907f998df6a396549589da5Anders Carlsson}
54932bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson
5494722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedmanbool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
5495722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman  if (E->getSubExpr()->getType()->isAnyComplexType()) {
5496f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    ComplexValue LV;
5497f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!EvaluateComplex(E->getSubExpr(), LV, Info))
5498f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return false;
5499f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!LV.isComplexInt())
5500f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E);
5501722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman    return Success(LV.getComplexIntReal(), E);
5502722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman  }
5503722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman
5504722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman  return Visit(E->getSubExpr());
5505722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman}
5506722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman
5507664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedmanbool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
5508722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman  if (E->getSubExpr()->getType()->isComplexIntegerType()) {
5509f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    ComplexValue LV;
5510f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!EvaluateComplex(E->getSubExpr(), LV, Info))
5511f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return false;
5512f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!LV.isComplexInt())
5513f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E);
5514722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman    return Success(LV.getComplexIntImag(), E);
5515722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman  }
5516722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman
55178327fad71da34492d82c532f42a58cb4baff81a3Richard Smith  VisitIgnoredValue(E->getSubExpr());
5518664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman  return Success(0, E);
5519664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman}
5520664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman
5521ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregorbool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) {
5522ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor  return Success(E->getPackLength(), E);
5523ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor}
5524ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor
5525295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redlbool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) {
5526295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl  return Success(E->getValue(), E);
5527295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl}
5528295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl
5529f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===//
5530d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman// Float Evaluation
5531d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman//===----------------------------------------------------------------------===//
5532d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman
5533d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmannamespace {
5534770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass FloatExprEvaluator
55358cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  : public ExprEvaluatorBase<FloatExprEvaluator, bool> {
5536d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman  APFloat &Result;
5537d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmanpublic:
5538d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman  FloatExprEvaluator(EvalInfo &info, APFloat &result)
55398cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    : ExprEvaluatorBaseTy(info), Result(result) {}
5540d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman
55411aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  bool Success(const APValue &V, const Expr *e) {
55428cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    Result = V.getFloat();
55438cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    return true;
55448cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  }
5545d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman
554651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  bool ZeroInitialization(const Expr *E) {
5547f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith    Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
5548f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith    return true;
5549f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  }
5550f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith
5551019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  bool VisitCallExpr(const CallExpr *E);
5552d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman
55535db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar  bool VisitUnaryOperator(const UnaryOperator *E);
5554d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman  bool VisitBinaryOperator(const BinaryOperator *E);
5555d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman  bool VisitFloatingLiteral(const FloatingLiteral *E);
55568cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCastExpr(const CastExpr *E);
55572217c87bdc5ab357046a5453bdb06f469c41024eEli Friedman
5558abd3a857ace59100305790545d1baae5877b8945John McCall  bool VisitUnaryReal(const UnaryOperator *E);
5559abd3a857ace59100305790545d1baae5877b8945John McCall  bool VisitUnaryImag(const UnaryOperator *E);
5560ba98d6bb414861965a1f22628494ea046785ecd4Eli Friedman
556151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  // FIXME: Missing: array subscript of vector, member of vector
5562d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman};
5563d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman} // end anonymous namespace
5564d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman
5565d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmanstatic bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
5566c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  assert(E->isRValue() && E->getType()->isRealFloatingType());
55678cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  return FloatExprEvaluator(Info, Result).Visit(E);
5568d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman}
5569d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman
55704ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadstatic bool TryEvaluateBuiltinNaN(const ASTContext &Context,
5571db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall                                  QualType ResultTy,
5572db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall                                  const Expr *Arg,
5573db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall                                  bool SNaN,
5574db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall                                  llvm::APFloat &Result) {
5575db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts());
5576db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  if (!S) return false;
5577db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall
5578db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy);
5579db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall
5580db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  llvm::APInt fill;
5581db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall
5582db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  // Treat empty strings as if they were zero.
5583db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  if (S->getString().empty())
5584db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall    fill = llvm::APInt(32, 0);
5585db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  else if (S->getString().getAsInteger(0, fill))
5586db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall    return false;
5587db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall
5588db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  if (SNaN)
5589db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall    Result = llvm::APFloat::getSNaN(Sem, false, &fill);
5590db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  else
5591db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall    Result = llvm::APFloat::getQNaN(Sem, false, &fill);
5592db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  return true;
5593db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall}
5594db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall
5595019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattnerbool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
5596180f47959a066795cc0f409433023af448bb0328Richard Smith  switch (E->isBuiltinCall()) {
55978cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  default:
55988cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    return ExprEvaluatorBaseTy::VisitCallExpr(E);
55998cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
5600019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  case Builtin::BI__builtin_huge_val:
5601019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  case Builtin::BI__builtin_huge_valf:
5602019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  case Builtin::BI__builtin_huge_vall:
5603019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  case Builtin::BI__builtin_inf:
5604019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  case Builtin::BI__builtin_inff:
56057cbed03c00e246682e5292785d01e1c120ce54bdDaniel Dunbar  case Builtin::BI__builtin_infl: {
56067cbed03c00e246682e5292785d01e1c120ce54bdDaniel Dunbar    const llvm::fltSemantics &Sem =
56077cbed03c00e246682e5292785d01e1c120ce54bdDaniel Dunbar      Info.Ctx.getFloatTypeSemantics(E->getType());
560834a74ab81600a40c6324fd76adb724b803dfaf91Chris Lattner    Result = llvm::APFloat::getInf(Sem);
560934a74ab81600a40c6324fd76adb724b803dfaf91Chris Lattner    return true;
56107cbed03c00e246682e5292785d01e1c120ce54bdDaniel Dunbar  }
56111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5612db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  case Builtin::BI__builtin_nans:
5613db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  case Builtin::BI__builtin_nansf:
5614db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  case Builtin::BI__builtin_nansl:
5615f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
5616f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                               true, Result))
5617f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E);
5618f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return true;
5619db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall
56209e62171a25e3a08fb5c49fb370f83faf5ae786f5Chris Lattner  case Builtin::BI__builtin_nan:
56219e62171a25e3a08fb5c49fb370f83faf5ae786f5Chris Lattner  case Builtin::BI__builtin_nanf:
56229e62171a25e3a08fb5c49fb370f83faf5ae786f5Chris Lattner  case Builtin::BI__builtin_nanl:
56234572baba9d18c275968ac113fd73b0e3c77cccb8Mike Stump    // If this is __builtin_nan() turn this into a nan, otherwise we
56249e62171a25e3a08fb5c49fb370f83faf5ae786f5Chris Lattner    // can't constant fold it.
5625f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
5626f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                               false, Result))
5627f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E);
5628f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return true;
56295db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar
56305db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar  case Builtin::BI__builtin_fabs:
56315db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar  case Builtin::BI__builtin_fabsf:
56325db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar  case Builtin::BI__builtin_fabsl:
56335db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar    if (!EvaluateFloat(E->getArg(0), Result, Info))
56345db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar      return false;
56351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
56365db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar    if (Result.isNegative())
56375db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar      Result.changeSign();
56385db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar    return true;
56395db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar
56401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  case Builtin::BI__builtin_copysign:
56411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  case Builtin::BI__builtin_copysignf:
56425db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar  case Builtin::BI__builtin_copysignl: {
56435db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar    APFloat RHS(0.);
56445db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar    if (!EvaluateFloat(E->getArg(0), Result, Info) ||
56455db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar        !EvaluateFloat(E->getArg(1), RHS, Info))
56465db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar      return false;
56475db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar    Result.copySign(RHS);
56485db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar    return true;
56495db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar  }
5650019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  }
5651019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner}
5652019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner
5653abd3a857ace59100305790545d1baae5877b8945John McCallbool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
565443efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman  if (E->getSubExpr()->getType()->isAnyComplexType()) {
565543efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman    ComplexValue CV;
565643efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman    if (!EvaluateComplex(E->getSubExpr(), CV, Info))
565743efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman      return false;
565843efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman    Result = CV.FloatReal;
565943efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman    return true;
566043efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman  }
566143efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman
566243efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman  return Visit(E->getSubExpr());
5663abd3a857ace59100305790545d1baae5877b8945John McCall}
5664abd3a857ace59100305790545d1baae5877b8945John McCall
5665abd3a857ace59100305790545d1baae5877b8945John McCallbool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
566643efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman  if (E->getSubExpr()->getType()->isAnyComplexType()) {
566743efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman    ComplexValue CV;
566843efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman    if (!EvaluateComplex(E->getSubExpr(), CV, Info))
566943efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman      return false;
567043efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman    Result = CV.FloatImag;
567143efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman    return true;
567243efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman  }
567343efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman
56748327fad71da34492d82c532f42a58cb4baff81a3Richard Smith  VisitIgnoredValue(E->getSubExpr());
567543efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman  const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(E->getType());
567643efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman  Result = llvm::APFloat::getZero(Sem);
5677abd3a857ace59100305790545d1baae5877b8945John McCall  return true;
5678abd3a857ace59100305790545d1baae5877b8945John McCall}
5679abd3a857ace59100305790545d1baae5877b8945John McCall
56805db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbarbool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
56815db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar  switch (E->getOpcode()) {
5682f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  default: return Error(E);
56832de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Plus:
56847993e8a2a26bf408c2a6d45f24fffa12db336b2bRichard Smith    return EvaluateFloat(E->getSubExpr(), Result, Info);
56852de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Minus:
56867993e8a2a26bf408c2a6d45f24fffa12db336b2bRichard Smith    if (!EvaluateFloat(E->getSubExpr(), Result, Info))
56877993e8a2a26bf408c2a6d45f24fffa12db336b2bRichard Smith      return false;
56885db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar    Result.changeSign();
56895db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar    return true;
56905db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar  }
56915db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar}
5692019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner
5693d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmanbool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
5694e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma)
5695e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
569696e93660124c8028a4c3bcc038ab0cdd18cd7ab2Anders Carlsson
56975db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar  APFloat RHS(0.0);
5698745f5147e065900267c85a5568785a1991d4838fRichard Smith  bool LHSOK = EvaluateFloat(E->getLHS(), Result, Info);
5699745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!LHSOK && !Info.keepEvaluatingAfterFailure())
5700d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman    return false;
5701745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!EvaluateFloat(E->getRHS(), RHS, Info) || !LHSOK)
5702d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman    return false;
5703d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman
5704d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman  switch (E->getOpcode()) {
5705f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  default: return Error(E);
57062de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Mul:
5707d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman    Result.multiply(RHS, APFloat::rmNearestTiesToEven);
57087b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    break;
57092de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Add:
5710d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman    Result.add(RHS, APFloat::rmNearestTiesToEven);
57117b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    break;
57122de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Sub:
5713d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman    Result.subtract(RHS, APFloat::rmNearestTiesToEven);
57147b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    break;
57152de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Div:
5716d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman    Result.divide(RHS, APFloat::rmNearestTiesToEven);
57177b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    break;
5718d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman  }
57197b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith
57207b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith  if (Result.isInfinity() || Result.isNaN())
57217b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    CCEDiag(E, diag::note_constexpr_float_arithmetic) << Result.isNaN();
57227b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith  return true;
5723d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman}
5724d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman
5725d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmanbool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
5726d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman  Result = E->getValue();
5727d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman  return true;
5728d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman}
5729d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman
57308cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) {
57318cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  const Expr* SubExpr = E->getSubExpr();
57321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
57332a523eec6a31955be876625819b89e8dc5def707Eli Friedman  switch (E->getCastKind()) {
57342a523eec6a31955be876625819b89e8dc5def707Eli Friedman  default:
5735c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return ExprEvaluatorBaseTy::VisitCastExpr(E);
57362a523eec6a31955be876625819b89e8dc5def707Eli Friedman
57372a523eec6a31955be876625819b89e8dc5def707Eli Friedman  case CK_IntegralToFloating: {
57384efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman    APSInt IntResult;
5739c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return EvaluateInteger(SubExpr, IntResult, Info) &&
5740c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith           HandleIntToFloatCast(Info, E, SubExpr->getType(), IntResult,
5741c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                E->getType(), Result);
57424efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman  }
57432a523eec6a31955be876625819b89e8dc5def707Eli Friedman
57442a523eec6a31955be876625819b89e8dc5def707Eli Friedman  case CK_FloatingCast: {
57454efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman    if (!Visit(SubExpr))
57464efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman      return false;
5747c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return HandleFloatToFloatCast(Info, E, SubExpr->getType(), E->getType(),
5748c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                  Result);
57494efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman  }
5750f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall
57512a523eec6a31955be876625819b89e8dc5def707Eli Friedman  case CK_FloatingComplexToReal: {
5752f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    ComplexValue V;
5753f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    if (!EvaluateComplex(SubExpr, V, Info))
5754f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall      return false;
5755f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    Result = V.getComplexFloatReal();
5756f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    return true;
5757f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall  }
57582a523eec6a31955be876625819b89e8dc5def707Eli Friedman  }
57594efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman}
57604efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
5761d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman//===----------------------------------------------------------------------===//
5762a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar// Complex Evaluation (for float and integer)
57639ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson//===----------------------------------------------------------------------===//
57649ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson
57659ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlssonnamespace {
5766770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass ComplexExprEvaluator
57678cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  : public ExprEvaluatorBase<ComplexExprEvaluator, bool> {
5768f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall  ComplexValue &Result;
57691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
57709ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlssonpublic:
5771f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall  ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result)
57728cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    : ExprEvaluatorBaseTy(info), Result(Result) {}
57731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
57741aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  bool Success(const APValue &V, const Expr *e) {
57758cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    Result.setFrom(V);
57768cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    return true;
57778cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  }
57781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
57797ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  bool ZeroInitialization(const Expr *E);
57807ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman
57818cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  //===--------------------------------------------------------------------===//
57828cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  //                            Visitor Methods
57838cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  //===--------------------------------------------------------------------===//
57849ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson
57858cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitImaginaryLiteral(const ImaginaryLiteral *E);
57868cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCastExpr(const CastExpr *E);
5787b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman  bool VisitBinaryOperator(const BinaryOperator *E);
578896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  bool VisitUnaryOperator(const UnaryOperator *E);
57897ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  bool VisitInitListExpr(const InitListExpr *E);
5790b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman};
5791b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman} // end anonymous namespace
57921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5793b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedmanstatic bool EvaluateComplex(const Expr *E, ComplexValue &Result,
5794b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman                            EvalInfo &Info) {
5795c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  assert(E->isRValue() && E->getType()->isAnyComplexType());
57968cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  return ComplexExprEvaluator(Info, Result).Visit(E);
5797b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman}
5798b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman
57997ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedmanbool ComplexExprEvaluator::ZeroInitialization(const Expr *E) {
5800f6c17a439f3320ac620639a3ee66dbdabb93810cEli Friedman  QualType ElemTy = E->getType()->getAs<ComplexType>()->getElementType();
58017ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  if (ElemTy->isRealFloatingType()) {
58027ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    Result.makeComplexFloat();
58037ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    APFloat Zero = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(ElemTy));
58047ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    Result.FloatReal = Zero;
58057ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    Result.FloatImag = Zero;
58067ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  } else {
58077ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    Result.makeComplexInt();
58087ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    APSInt Zero = Info.Ctx.MakeIntValue(0, ElemTy);
58097ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    Result.IntReal = Zero;
58107ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    Result.IntImag = Zero;
58117ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  }
58127ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  return true;
58137ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman}
58147ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman
58158cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) {
58168cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  const Expr* SubExpr = E->getSubExpr();
5817b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman
5818b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman  if (SubExpr->getType()->isRealFloatingType()) {
5819b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    Result.makeComplexFloat();
5820b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    APFloat &Imag = Result.FloatImag;
5821b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    if (!EvaluateFloat(SubExpr, Imag, Info))
5822b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman      return false;
5823b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman
5824b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    Result.FloatReal = APFloat(Imag.getSemantics());
5825b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    return true;
5826b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman  } else {
5827b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    assert(SubExpr->getType()->isIntegerType() &&
5828b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman           "Unexpected imaginary literal.");
5829b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman
5830b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    Result.makeComplexInt();
5831b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    APSInt &Imag = Result.IntImag;
5832b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    if (!EvaluateInteger(SubExpr, Imag, Info))
5833b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman      return false;
5834b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman
5835b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned());
5836b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    return true;
5837b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman  }
5838b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman}
5839b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman
58408cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) {
5841b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman
58428786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  switch (E->getCastKind()) {
58438786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_BitCast:
58448786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_BaseToDerived:
58458786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_DerivedToBase:
58468786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_UncheckedDerivedToBase:
58478786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_Dynamic:
58488786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_ToUnion:
58498786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_ArrayToPointerDecay:
58508786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_FunctionToPointerDecay:
58518786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_NullToPointer:
58528786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_NullToMemberPointer:
58538786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_BaseToDerivedMemberPointer:
58548786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_DerivedToBaseMemberPointer:
58558786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_MemberPointerToBoolean:
58564d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  case CK_ReinterpretMemberPointer:
58578786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_ConstructorConversion:
58588786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_IntegralToPointer:
58598786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_PointerToIntegral:
58608786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_PointerToBoolean:
58618786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_ToVoid:
58628786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_VectorSplat:
58638786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_IntegralCast:
58648786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_IntegralToBoolean:
58658786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_IntegralToFloating:
58668786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_FloatingToIntegral:
58678786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_FloatingToBoolean:
58688786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_FloatingCast:
58691d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_CPointerToObjCPointerCast:
58701d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_BlockPointerToObjCPointerCast:
58718786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_AnyPointerToBlockPointerCast:
58728786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_ObjCObjectLValueCast:
58738786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_FloatingComplexToReal:
58748786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_FloatingComplexToBoolean:
58758786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_IntegralComplexToReal:
58768786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_IntegralComplexToBoolean:
587733e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCProduceObject:
587833e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCConsumeObject:
587933e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCReclaimReturnedObject:
588033e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCExtendBlockObject:
5881ac1303eca6cbe3e623fb5ec6fe7ec184ef4b0dfaDouglas Gregor  case CK_CopyAndAutoreleaseBlockObject:
58828786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    llvm_unreachable("invalid cast kind for complex value");
58838786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
58848786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_LValueToRValue:
58857a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall  case CK_AtomicToNonAtomic:
58867a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall  case CK_NonAtomicToAtomic:
58878786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_NoOp:
5888c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return ExprEvaluatorBaseTy::VisitCastExpr(E);
58892bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall
58908786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_Dependent:
589146a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_LValueBitCast:
58928786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_UserDefinedConversion:
5893f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
58948786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
58958786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_FloatingRealToComplex: {
5896b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    APFloat &Real = Result.FloatReal;
58978786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    if (!EvaluateFloat(E->getSubExpr(), Real, Info))
5898b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman      return false;
5899b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman
59008786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    Result.makeComplexFloat();
59018786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    Result.FloatImag = APFloat(Real.getSemantics());
59028786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    return true;
59038786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  }
59048786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
59058786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_FloatingComplexCast: {
59068786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    if (!Visit(E->getSubExpr()))
59078786da77984e81d48e0e1b2bd339809b1efc19f3John McCall      return false;
59088786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
59098786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    QualType To = E->getType()->getAs<ComplexType>()->getElementType();
59108786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    QualType From
59118786da77984e81d48e0e1b2bd339809b1efc19f3John McCall      = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType();
59128786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
5913c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return HandleFloatToFloatCast(Info, E, From, To, Result.FloatReal) &&
5914c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith           HandleFloatToFloatCast(Info, E, From, To, Result.FloatImag);
59158786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  }
59168786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
59178786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_FloatingComplexToIntegralComplex: {
59188786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    if (!Visit(E->getSubExpr()))
59198786da77984e81d48e0e1b2bd339809b1efc19f3John McCall      return false;
59208786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
59218786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    QualType To = E->getType()->getAs<ComplexType>()->getElementType();
59228786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    QualType From
59238786da77984e81d48e0e1b2bd339809b1efc19f3John McCall      = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType();
59248786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    Result.makeComplexInt();
5925c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return HandleFloatToIntCast(Info, E, From, Result.FloatReal,
5926c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                To, Result.IntReal) &&
5927c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith           HandleFloatToIntCast(Info, E, From, Result.FloatImag,
5928c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                To, Result.IntImag);
59298786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  }
59308786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
59318786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_IntegralRealToComplex: {
5932b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    APSInt &Real = Result.IntReal;
59338786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    if (!EvaluateInteger(E->getSubExpr(), Real, Info))
5934b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman      return false;
59359ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson
59368786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    Result.makeComplexInt();
59378786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned());
59388786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    return true;
59398786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  }
59408786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
59418786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_IntegralComplexCast: {
59428786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    if (!Visit(E->getSubExpr()))
5943b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman      return false;
5944ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson
59458786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    QualType To = E->getType()->getAs<ComplexType>()->getElementType();
59468786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    QualType From
59478786da77984e81d48e0e1b2bd339809b1efc19f3John McCall      = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType();
59481725f683432715e5afe34d476024bd6f16eac3fcEli Friedman
5949f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith    Result.IntReal = HandleIntToIntCast(Info, E, To, From, Result.IntReal);
5950f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith    Result.IntImag = HandleIntToIntCast(Info, E, To, From, Result.IntImag);
59518786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    return true;
59528786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  }
59538786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
59548786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_IntegralComplexToFloatingComplex: {
59558786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    if (!Visit(E->getSubExpr()))
59568786da77984e81d48e0e1b2bd339809b1efc19f3John McCall      return false;
59578786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
59588786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    QualType To = E->getType()->getAs<ComplexType>()->getElementType();
59598786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    QualType From
59608786da77984e81d48e0e1b2bd339809b1efc19f3John McCall      = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType();
59618786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    Result.makeComplexFloat();
5962c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return HandleIntToFloatCast(Info, E, From, Result.IntReal,
5963c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                To, Result.FloatReal) &&
5964c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith           HandleIntToFloatCast(Info, E, From, Result.IntImag,
5965c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                To, Result.FloatImag);
59668786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  }
5967ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson  }
59681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
59698786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  llvm_unreachable("unknown cast resulting in complex value");
59709ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson}
59719ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson
5972f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCallbool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
5973e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma)
59742ad226bdc847df6b6b6e4f832856478ab63bb3dcRichard Smith    return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
59752ad226bdc847df6b6b6e4f832856478ab63bb3dcRichard Smith
5976745f5147e065900267c85a5568785a1991d4838fRichard Smith  bool LHSOK = Visit(E->getLHS());
5977745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!LHSOK && !Info.keepEvaluatingAfterFailure())
5978f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    return false;
59791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5980f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall  ComplexValue RHS;
5981745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK)
5982f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    return false;
5983a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar
59843f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar  assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
59853f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar         "Invalid operands to binary operator.");
5986ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson  switch (E->getOpcode()) {
5987f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  default: return Error(E);
59882de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Add:
5989a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar    if (Result.isComplexFloat()) {
5990a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar      Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
5991a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar                                       APFloat::rmNearestTiesToEven);
5992a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar      Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
5993a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar                                       APFloat::rmNearestTiesToEven);
5994a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar    } else {
5995a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar      Result.getComplexIntReal() += RHS.getComplexIntReal();
5996a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar      Result.getComplexIntImag() += RHS.getComplexIntImag();
5997a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar    }
59983f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar    break;
59992de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Sub:
6000a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar    if (Result.isComplexFloat()) {
6001a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar      Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
6002a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar                                            APFloat::rmNearestTiesToEven);
6003a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar      Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
6004a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar                                            APFloat::rmNearestTiesToEven);
6005a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar    } else {
6006a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar      Result.getComplexIntReal() -= RHS.getComplexIntReal();
6007a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar      Result.getComplexIntImag() -= RHS.getComplexIntImag();
6008a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar    }
60093f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar    break;
60102de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Mul:
60113f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar    if (Result.isComplexFloat()) {
6012f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall      ComplexValue LHS = Result;
60133f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      APFloat &LHS_r = LHS.getComplexFloatReal();
60143f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      APFloat &LHS_i = LHS.getComplexFloatImag();
60153f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      APFloat &RHS_r = RHS.getComplexFloatReal();
60163f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      APFloat &RHS_i = RHS.getComplexFloatImag();
60171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
60183f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      APFloat Tmp = LHS_r;
60193f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
60203f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Result.getComplexFloatReal() = Tmp;
60213f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Tmp = LHS_i;
60223f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
60233f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
60243f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar
60253f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Tmp = LHS_r;
60263f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
60273f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Result.getComplexFloatImag() = Tmp;
60283f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Tmp = LHS_i;
60293f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
60303f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
60313f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar    } else {
6032f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall      ComplexValue LHS = Result;
60331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result.getComplexIntReal() =
60343f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar        (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
60353f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar         LHS.getComplexIntImag() * RHS.getComplexIntImag());
60361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result.getComplexIntImag() =
60373f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar        (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
60383f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar         LHS.getComplexIntImag() * RHS.getComplexIntReal());
60393f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar    }
60403f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar    break;
604196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  case BO_Div:
604296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    if (Result.isComplexFloat()) {
604396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      ComplexValue LHS = Result;
604496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      APFloat &LHS_r = LHS.getComplexFloatReal();
604596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      APFloat &LHS_i = LHS.getComplexFloatImag();
604696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      APFloat &RHS_r = RHS.getComplexFloatReal();
604796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      APFloat &RHS_i = RHS.getComplexFloatImag();
604896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      APFloat &Res_r = Result.getComplexFloatReal();
604996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      APFloat &Res_i = Result.getComplexFloatImag();
605096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara
605196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      APFloat Den = RHS_r;
605296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Den.multiply(RHS_r, APFloat::rmNearestTiesToEven);
605396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      APFloat Tmp = RHS_i;
605496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
605596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Den.add(Tmp, APFloat::rmNearestTiesToEven);
605696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara
605796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Res_r = LHS_r;
605896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Res_r.multiply(RHS_r, APFloat::rmNearestTiesToEven);
605996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Tmp = LHS_i;
606096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
606196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Res_r.add(Tmp, APFloat::rmNearestTiesToEven);
606296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Res_r.divide(Den, APFloat::rmNearestTiesToEven);
606396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara
606496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Res_i = LHS_i;
606596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Res_i.multiply(RHS_r, APFloat::rmNearestTiesToEven);
606696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Tmp = LHS_r;
606796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
606896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Res_i.subtract(Tmp, APFloat::rmNearestTiesToEven);
606996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Res_i.divide(Den, APFloat::rmNearestTiesToEven);
607096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    } else {
6071f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0)
6072f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(E, diag::note_expr_divide_by_zero);
6073f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
607496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      ComplexValue LHS = Result;
607596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() +
607696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara        RHS.getComplexIntImag() * RHS.getComplexIntImag();
607796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Result.getComplexIntReal() =
607896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara        (LHS.getComplexIntReal() * RHS.getComplexIntReal() +
607996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara         LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den;
608096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Result.getComplexIntImag() =
608196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara        (LHS.getComplexIntImag() * RHS.getComplexIntReal() -
608296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara         LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den;
608396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    }
608496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    break;
6085ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson  }
6086ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson
6087f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall  return true;
6088ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson}
6089ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson
609096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnarabool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
609196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  // Get the operand value into 'Result'.
609296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  if (!Visit(E->getSubExpr()))
609396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    return false;
609496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara
609596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  switch (E->getOpcode()) {
609696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  default:
6097f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
609896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  case UO_Extension:
609996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    return true;
610096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  case UO_Plus:
610196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    // The result is always just the subexpr.
610296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    return true;
610396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  case UO_Minus:
610496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    if (Result.isComplexFloat()) {
610596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Result.getComplexFloatReal().changeSign();
610696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Result.getComplexFloatImag().changeSign();
610796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    }
610896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    else {
610996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Result.getComplexIntReal() = -Result.getComplexIntReal();
611096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Result.getComplexIntImag() = -Result.getComplexIntImag();
611196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    }
611296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    return true;
611396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  case UO_Not:
611496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    if (Result.isComplexFloat())
611596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Result.getComplexFloatImag().changeSign();
611696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    else
611796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Result.getComplexIntImag() = -Result.getComplexIntImag();
611896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    return true;
611996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  }
612096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara}
612196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara
61227ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedmanbool ComplexExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
61237ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  if (E->getNumInits() == 2) {
61247ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    if (E->getType()->isComplexType()) {
61257ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman      Result.makeComplexFloat();
61267ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman      if (!EvaluateFloat(E->getInit(0), Result.FloatReal, Info))
61277ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman        return false;
61287ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman      if (!EvaluateFloat(E->getInit(1), Result.FloatImag, Info))
61297ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman        return false;
61307ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    } else {
61317ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman      Result.makeComplexInt();
61327ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman      if (!EvaluateInteger(E->getInit(0), Result.IntReal, Info))
61337ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman        return false;
61347ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman      if (!EvaluateInteger(E->getInit(1), Result.IntImag, Info))
61357ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman        return false;
61367ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    }
61377ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    return true;
61387ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  }
61397ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  return ExprEvaluatorBaseTy::VisitInitListExpr(E);
61407ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman}
61417ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman
61429ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson//===----------------------------------------------------------------------===//
6143aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith// Void expression evaluation, primarily for a cast to void on the LHS of a
6144aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith// comma operator
6145aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith//===----------------------------------------------------------------------===//
6146aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith
6147aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smithnamespace {
6148aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smithclass VoidExprEvaluator
6149aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith  : public ExprEvaluatorBase<VoidExprEvaluator, bool> {
6150aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smithpublic:
6151aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith  VoidExprEvaluator(EvalInfo &Info) : ExprEvaluatorBaseTy(Info) {}
6152aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith
61531aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  bool Success(const APValue &V, const Expr *e) { return true; }
6154aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith
6155aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith  bool VisitCastExpr(const CastExpr *E) {
6156aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith    switch (E->getCastKind()) {
6157aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith    default:
6158aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith      return ExprEvaluatorBaseTy::VisitCastExpr(E);
6159aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith    case CK_ToVoid:
6160aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith      VisitIgnoredValue(E->getSubExpr());
6161aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith      return true;
6162aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith    }
6163aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith  }
6164aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith};
6165aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith} // end anonymous namespace
6166aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith
6167aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smithstatic bool EvaluateVoid(const Expr *E, EvalInfo &Info) {
6168aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith  assert(E->isRValue() && E->getType()->isVoidType());
6169aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith  return VoidExprEvaluator(Info).Visit(E);
6170aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith}
6171aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith
6172aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith//===----------------------------------------------------------------------===//
617351f4708c00110940ca3f337961915f2ca1668375Richard Smith// Top level Expr::EvaluateAsRValue method.
6174f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===//
6175f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
61761aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smithstatic bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E) {
6177c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  // In C, function designators are not lvalues, but we evaluate them as if they
6178c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  // are.
6179c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  if (E->isGLValue() || E->getType()->isFunctionType()) {
6180c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    LValue LV;
6181c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    if (!EvaluateLValue(E, LV, Info))
6182c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith      return false;
6183c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    LV.moveInto(Result);
6184c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  } else if (E->getType()->isVectorType()) {
61851e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    if (!EvaluateVector(E, Result, Info))
618659b5da6d853b4368b984700315adf7b37de05764Nate Begeman      return false;
6187575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor  } else if (E->getType()->isIntegralOrEnumerationType()) {
61881e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    if (!IntExprEvaluator(Info, Result).Visit(E))
61896dde0d5dc09f45f4d9508c964703e36fef1a0198Anders Carlsson      return false;
6190efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  } else if (E->getType()->hasPointerRepresentation()) {
6191efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    LValue LV;
6192efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    if (!EvaluatePointer(E, LV, Info))
61936dde0d5dc09f45f4d9508c964703e36fef1a0198Anders Carlsson      return false;
61941e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    LV.moveInto(Result);
6195efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  } else if (E->getType()->isRealFloatingType()) {
6196efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    llvm::APFloat F(0.0);
6197efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    if (!EvaluateFloat(E, F, Info))
61986dde0d5dc09f45f4d9508c964703e36fef1a0198Anders Carlsson      return false;
61991aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    Result = APValue(F);
6200efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  } else if (E->getType()->isAnyComplexType()) {
6201efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    ComplexValue C;
6202efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    if (!EvaluateComplex(E, C, Info))
6203660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump      return false;
62041e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    C.moveInto(Result);
620569c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith  } else if (E->getType()->isMemberPointerType()) {
6206e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    MemberPtr P;
6207e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (!EvaluateMemberPointer(E, P, Info))
6208e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return false;
6209e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    P.moveInto(Result);
6210e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
621151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  } else if (E->getType()->isArrayType()) {
6212180f47959a066795cc0f409433023af448bb0328Richard Smith    LValue LV;
621383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    LV.set(E, Info.CurrentCall->Index);
6214180f47959a066795cc0f409433023af448bb0328Richard Smith    if (!EvaluateArray(E, LV, Info.CurrentCall->Temporaries[E], Info))
6215cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith      return false;
6216180f47959a066795cc0f409433023af448bb0328Richard Smith    Result = Info.CurrentCall->Temporaries[E];
621751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  } else if (E->getType()->isRecordType()) {
6218180f47959a066795cc0f409433023af448bb0328Richard Smith    LValue LV;
621983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    LV.set(E, Info.CurrentCall->Index);
6220180f47959a066795cc0f409433023af448bb0328Richard Smith    if (!EvaluateRecord(E, LV, Info.CurrentCall->Temporaries[E], Info))
6221180f47959a066795cc0f409433023af448bb0328Richard Smith      return false;
6222180f47959a066795cc0f409433023af448bb0328Richard Smith    Result = Info.CurrentCall->Temporaries[E];
6223aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith  } else if (E->getType()->isVoidType()) {
6224c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    if (Info.getLangOpts().CPlusPlus0x)
62255cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      Info.CCEDiag(E, diag::note_constexpr_nonliteral)
6226c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith        << E->getType();
6227c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    else
62285cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith      Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr);
6229aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith    if (!EvaluateVoid(E, Info))
6230aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith      return false;
6231c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  } else if (Info.getLangOpts().CPlusPlus0x) {
62325cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.Diag(E, diag::note_constexpr_nonliteral) << E->getType();
6233c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return false;
6234f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  } else {
62355cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith    Info.Diag(E, diag::note_invalid_subexpr_in_const_expr);
6236660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump    return false;
6237f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
6238660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump
6239660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump  return true;
6240660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump}
6241660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump
624283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith/// EvaluateInPlace - Evaluate an expression in-place in an APValue. In some
624383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith/// cases, the in-place evaluation is essential, since later initializers for
624483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith/// an object can indirectly refer to subobjects which were initialized earlier.
624583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smithstatic bool EvaluateInPlace(APValue &Result, EvalInfo &Info, const LValue &This,
624683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                            const Expr *E, CheckConstantExpressionKind CCEK,
624783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                            bool AllowNonLiteralTypes) {
62487ca4850a3e3530fa6c93b64b740446e32c97f992Richard Smith  if (!AllowNonLiteralTypes && !CheckLiteralType(Info, E))
624951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return false;
625051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
625151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (E->isRValue()) {
625269c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith    // Evaluate arrays and record types in-place, so that later initializers can
625369c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith    // refer to earlier-initialized members of the object.
6254180f47959a066795cc0f409433023af448bb0328Richard Smith    if (E->getType()->isArrayType())
6255180f47959a066795cc0f409433023af448bb0328Richard Smith      return EvaluateArray(E, This, Result, Info);
6256180f47959a066795cc0f409433023af448bb0328Richard Smith    else if (E->getType()->isRecordType())
6257180f47959a066795cc0f409433023af448bb0328Richard Smith      return EvaluateRecord(E, This, Result, Info);
625869c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith  }
625969c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith
626069c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith  // For any other type, in-place evaluation is unimportant.
62611aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  return Evaluate(Result, Info, E);
626269c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith}
626369c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith
6264f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// EvaluateAsRValue - Try to evaluate this expression, performing an implicit
6265f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// lvalue-to-rvalue cast if it is an lvalue.
6266f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) {
626751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (!CheckLiteralType(Info, E))
626851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return false;
626951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
62701aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  if (!::Evaluate(Result, Info, E))
6271f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return false;
6272f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
6273f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (E->isGLValue()) {
6274f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    LValue LV;
62751aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    LV.setFrom(Info.Ctx, Result);
62761aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith    if (!HandleLValueToRValueConversion(Info, E, E->getType(), LV, Result))
6277f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return false;
6278f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
6279f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
62801aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  // Check this core constant expression is a constant expression.
628183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  return CheckConstantExpression(Info, E->getExprLoc(), E->getType(), Result);
6282f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith}
6283c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
628451f4708c00110940ca3f337961915f2ca1668375Richard Smith/// EvaluateAsRValue - Return true if this is a constant which we can fold using
628556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall/// any crazy technique (that has nothing to do with language standards) that
628656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall/// we want to.  If this function returns true, it returns the folded constant
6287c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// in Result. If this expression is a glvalue, an lvalue-to-rvalue conversion
6288c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// will be applied to the result.
628951f4708c00110940ca3f337961915f2ca1668375Richard Smithbool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx) const {
6290ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith  // Fast-path evaluations of integer literals, since we sometimes see files
6291ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith  // containing vast quantities of these.
6292ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith  if (const IntegerLiteral *L = dyn_cast<IntegerLiteral>(this)) {
6293ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith    Result.Val = APValue(APSInt(L->getValue(),
6294ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith                                L->getType()->isUnsignedIntegerType()));
6295ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith    return true;
6296ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith  }
6297ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith
62982d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  // FIXME: Evaluating values of large array and record types can cause
62992d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  // performance problems. Only do so in C++11 for now.
6300e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) &&
63014e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      !Ctx.getLangOpts().CPlusPlus0x)
63021445bbacf4c8de5f208ff4ccb302424a4d9e233eRichard Smith    return false;
63031445bbacf4c8de5f208ff4ccb302424a4d9e233eRichard Smith
6304f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  EvalInfo Info(Ctx, Result);
6305f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  return ::EvaluateAsRValue(Info, this, Result.Val);
630656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall}
630756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
63084ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool Expr::EvaluateAsBooleanCondition(bool &Result,
63094ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                      const ASTContext &Ctx) const {
6310c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  EvalResult Scratch;
631151f4708c00110940ca3f337961915f2ca1668375Richard Smith  return EvaluateAsRValue(Scratch, Ctx) &&
63121aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith         HandleConversionToBool(Scratch.Val, Result);
6313cd7a445c6b46c5585580dfb652300c8483c0cb6bJohn McCall}
6314cd7a445c6b46c5585580dfb652300c8483c0cb6bJohn McCall
631580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smithbool Expr::EvaluateAsInt(APSInt &Result, const ASTContext &Ctx,
631680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith                         SideEffectsKind AllowSideEffects) const {
631780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  if (!getType()->isIntegralOrEnumerationType())
631880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    return false;
631980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith
6320c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  EvalResult ExprResult;
632180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  if (!EvaluateAsRValue(ExprResult, Ctx) || !ExprResult.Val.isInt() ||
632280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith      (!AllowSideEffects && ExprResult.HasSideEffects))
6323c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return false;
6324f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
6325c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  Result = ExprResult.Val.getInt();
6326c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  return true;
6327a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith}
6328a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith
63294ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx) const {
63301b78276a75a5a0f496a82429c1ff9604d622a76dAnders Carlsson  EvalInfo Info(Ctx, Result);
63311b78276a75a5a0f496a82429c1ff9604d622a76dAnders Carlsson
6332efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  LValue LV;
633383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  if (!EvaluateLValue(this, LV, Info) || Result.HasSideEffects ||
633483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      !CheckLValueConstantExpression(Info, getExprLoc(),
633583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                                     Ctx.getLValueReferenceType(getType()), LV))
633683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return false;
633783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith
63381aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  LV.moveInto(Result.Val);
633983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  return true;
6340b2f295c8050fb8c141bf2cf38eed0a56e99d0092Eli Friedman}
6341b2f295c8050fb8c141bf2cf38eed0a56e99d0092Eli Friedman
6342099e7f647ccda915513f2b2ec53352dc756082d3Richard Smithbool Expr::EvaluateAsInitializer(APValue &Value, const ASTContext &Ctx,
6343099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith                                 const VarDecl *VD,
6344099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith                      llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
63452d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  // FIXME: Evaluating initializers for large array and record types can cause
63462d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  // performance problems. Only do so in C++11 for now.
63472d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) &&
63484e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      !Ctx.getLangOpts().CPlusPlus0x)
63492d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith    return false;
63502d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith
6351099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  Expr::EvalStatus EStatus;
6352099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  EStatus.Diag = &Notes;
6353099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
6354099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  EvalInfo InitInfo(Ctx, EStatus);
6355099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  InitInfo.setEvaluatingDecl(VD, Value);
6356099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
6357099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  LValue LVal;
6358099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  LVal.set(VD);
6359099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
636051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  // C++11 [basic.start.init]p2:
636151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  //  Variables with static storage duration or thread storage duration shall be
636251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  //  zero-initialized before any other initialization takes place.
636351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  // This behavior is not present in C.
63644e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (Ctx.getLangOpts().CPlusPlus && !VD->hasLocalStorage() &&
636551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      !VD->getType()->isReferenceType()) {
636651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    ImplicitValueInitExpr VIE(VD->getType());
636783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    if (!EvaluateInPlace(Value, InitInfo, LVal, &VIE, CCEK_Constant,
636883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                         /*AllowNonLiteralTypes=*/true))
636951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      return false;
637051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  }
637151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
637283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  if (!EvaluateInPlace(Value, InitInfo, LVal, this, CCEK_Constant,
637383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                         /*AllowNonLiteralTypes=*/true) ||
637483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith      EStatus.HasSideEffects)
637583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith    return false;
637683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith
637783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  return CheckConstantExpression(InitInfo, VD->getLocation(), VD->getType(),
637883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith                                 Value);
6379099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith}
6380099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
638151f4708c00110940ca3f337961915f2ca1668375Richard Smith/// isEvaluatable - Call EvaluateAsRValue to see if this expression can be
638251f4708c00110940ca3f337961915f2ca1668375Richard Smith/// constant folded, but discard the result.
63834ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool Expr::isEvaluatable(const ASTContext &Ctx) const {
63844fdfb0965b396f2778091f7e6c051d17ff9791baAnders Carlsson  EvalResult Result;
638551f4708c00110940ca3f337961915f2ca1668375Richard Smith  return EvaluateAsRValue(Result, Ctx) && !Result.HasSideEffects;
638645b6b9d080ac56917337d73d8f1cd6374b27b05dChris Lattner}
638751fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson
63884ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool Expr::HasSideEffects(const ASTContext &Ctx) const {
63891e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith  return HasSideEffect(Ctx).Visit(this);
6390393c247fe025ccb5f914e37e948192ea86faef8cFariborz Jahanian}
6391393c247fe025ccb5f914e37e948192ea86faef8cFariborz Jahanian
6392a6b8b2c09610b8bc4330e948ece8b940c2386406Richard SmithAPSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx) const {
63931c0cfd4599e816cfd7a8f348286bf0ad79652ffcAnders Carlsson  EvalResult EvalResult;
639451f4708c00110940ca3f337961915f2ca1668375Richard Smith  bool Result = EvaluateAsRValue(EvalResult, Ctx);
6395c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin  (void)Result;
639651fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson  assert(Result && "Could not evaluate expression");
63971c0cfd4599e816cfd7a8f348286bf0ad79652ffcAnders Carlsson  assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
639851fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson
63991c0cfd4599e816cfd7a8f348286bf0ad79652ffcAnders Carlsson  return EvalResult.Val.getInt();
640051fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson}
6401d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6402e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara bool Expr::EvalResult::isGlobalLValue() const {
6403e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara   assert(Val.isLValue());
6404e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara   return IsGlobalLValue(Val.getLValueBase());
6405e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara }
6406e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara
6407e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara
6408d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// isIntegerConstantExpr - this recursive routine will test if an expression is
6409d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// an integer constant expression.
6410d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6411d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
6412d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// comma, etc
6413d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall///
6414d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// FIXME: Handle offsetof.  Two things to do:  Handle GCC's __builtin_offsetof
6415d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// to support gcc 4.0+  and handle the idiom GCC recognizes with a null pointer
6416d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// cast+dereference.
6417d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6418d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// CheckICE - This function does the fundamental ICE checking: the returned
6419d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
6420d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// Note that to reduce code duplication, this helper does no evaluation
6421d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// itself; the caller checks whether the expression is evaluatable, and
6422d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// in the rare cases where CheckICE actually cares about the evaluated
6423d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// value, it calls into Evalute.
6424d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall//
6425d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// Meanings of Val:
642651f4708c00110940ca3f337961915f2ca1668375Richard Smith// 0: This expression is an ICE.
6427d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// 1: This expression is not an ICE, but if it isn't evaluated, it's
6428d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall//    a legal subexpression for an ICE. This return value is used to handle
6429d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall//    the comma operator in C99 mode.
6430d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// 2: This expression is not an ICE, and is not a legal subexpression for one.
6431d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
64323c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohmannamespace {
64333c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman
6434d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCallstruct ICEDiag {
6435d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  unsigned Val;
6436d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  SourceLocation Loc;
6437d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6438d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  public:
6439d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
6440d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  ICEDiag() : Val(0) {}
6441d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall};
6442d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
64433c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman}
64443c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman
64453c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohmanstatic ICEDiag NoDiag() { return ICEDiag(); }
6446d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6447d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCallstatic ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
6448d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  Expr::EvalResult EVResult;
644951f4708c00110940ca3f337961915f2ca1668375Richard Smith  if (!E->EvaluateAsRValue(EVResult, Ctx) || EVResult.HasSideEffects ||
6450d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      !EVResult.Val.isInt()) {
6451d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return ICEDiag(2, E->getLocStart());
6452d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6453d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  return NoDiag();
6454d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall}
6455d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6456d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCallstatic ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
6457d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  assert(!E->isValueDependent() && "Should not see value dependent exprs!");
64582ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor  if (!E->getType()->isIntegralOrEnumerationType()) {
6459d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return ICEDiag(2, E->getLocStart());
6460d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6461d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6462d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  switch (E->getStmtClass()) {
646363c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall#define ABSTRACT_STMT(Node)
6464d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall#define STMT(Node, Base) case Expr::Node##Class:
6465d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall#define EXPR(Node, Base)
6466d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall#include "clang/AST/StmtNodes.inc"
6467d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::PredefinedExprClass:
6468d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::FloatingLiteralClass:
6469d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ImaginaryLiteralClass:
6470d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::StringLiteralClass:
6471d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ArraySubscriptExprClass:
6472d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::MemberExprClass:
6473d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CompoundAssignOperatorClass:
6474d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CompoundLiteralExprClass:
6475d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ExtVectorElementExprClass:
6476d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::DesignatedInitExprClass:
6477d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ImplicitValueInitExprClass:
6478d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ParenListExprClass:
6479d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::VAArgExprClass:
6480d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::AddrLabelExprClass:
6481d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::StmtExprClass:
6482d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXMemberCallExprClass:
6483e08ce650a2b02410eddd1f60a4aa6b3d4be71e73Peter Collingbourne  case Expr::CUDAKernelCallExprClass:
6484d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXDynamicCastExprClass:
6485d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXTypeidExprClass:
64869be88403e965cc49af76c9d33d818781d44b333eFrancois Pichet  case Expr::CXXUuidofExprClass:
6487d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXNullPtrLiteralExprClass:
64889fcce65e7e1307b5b8da9be13e4092d6bb94dc1dRichard Smith  case Expr::UserDefinedLiteralClass:
6489d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXThisExprClass:
6490d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXThrowExprClass:
6491d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXNewExprClass:
6492d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXDeleteExprClass:
6493d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXPseudoDestructorExprClass:
6494d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::UnresolvedLookupExprClass:
6495d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::DependentScopeDeclRefExprClass:
6496d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXConstructExprClass:
6497d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXBindTemporaryExprClass:
64984765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  case Expr::ExprWithCleanupsClass:
6499d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXTemporaryObjectExprClass:
6500d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXUnresolvedConstructExprClass:
6501d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXDependentScopeMemberExprClass:
6502d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::UnresolvedMemberExprClass:
6503d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ObjCStringLiteralClass:
6504eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  case Expr::ObjCBoxedExprClass:
6505ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  case Expr::ObjCArrayLiteralClass:
6506ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  case Expr::ObjCDictionaryLiteralClass:
6507d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ObjCEncodeExprClass:
6508d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ObjCMessageExprClass:
6509d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ObjCSelectorExprClass:
6510d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ObjCProtocolExprClass:
6511d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ObjCIvarRefExprClass:
6512d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ObjCPropertyRefExprClass:
6513ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  case Expr::ObjCSubscriptRefExprClass:
6514d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ObjCIsaExprClass:
6515d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ShuffleVectorExprClass:
6516d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::BlockExprClass:
6517d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::NoStmtClass:
65187cd7d1ad33fdf49eef83942e8855fe20d95aa1b9John McCall  case Expr::OpaqueValueExprClass:
6519be230c36e32142cbdcdbe9c97511d097beeecbabDouglas Gregor  case Expr::PackExpansionExprClass:
6520c7793c73ba8a343de3f2552d984851985a46f159Douglas Gregor  case Expr::SubstNonTypeTemplateParmPackExprClass:
652161eee0ca33b29e102f11bab77c8b74cc00e2392bTanya Lattner  case Expr::AsTypeExprClass:
6522f85e193739c953358c865005855253af4f68a497John McCall  case Expr::ObjCIndirectCopyRestoreExprClass:
652303e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  case Expr::MaterializeTemporaryExprClass:
65244b9c2d235fb9449e249d74f48ecfec601650de93John McCall  case Expr::PseudoObjectExprClass:
6525276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman  case Expr::AtomicExprClass:
6526cea8d966f826554f0679595e9371e314e8dbc1cfSebastian Redl  case Expr::InitListExprClass:
652701d08018b7cf5ce1601707cfd7a84d22015fc04eDouglas Gregor  case Expr::LambdaExprClass:
6528cea8d966f826554f0679595e9371e314e8dbc1cfSebastian Redl    return ICEDiag(2, E->getLocStart());
6529cea8d966f826554f0679595e9371e314e8dbc1cfSebastian Redl
6530ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor  case Expr::SizeOfPackExprClass:
6531d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::GNUNullExprClass:
6532d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    // GCC considers the GNU __null value to be an integral constant expression.
6533d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return NoDiag();
6534d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
653591a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall  case Expr::SubstNonTypeTemplateParmExprClass:
653691a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall    return
653791a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall      CheckICE(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(), Ctx);
653891a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall
6539d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ParenExprClass:
6540d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
6541f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  case Expr::GenericSelectionExprClass:
6542f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return CheckICE(cast<GenericSelectionExpr>(E)->getResultExpr(), Ctx);
6543d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::IntegerLiteralClass:
6544d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CharacterLiteralClass:
6545ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  case Expr::ObjCBoolLiteralExprClass:
6546d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXBoolLiteralExprClass:
6547ed8abf18329df67b0abcbb3a10458bd8c1d2a595Douglas Gregor  case Expr::CXXScalarValueInitExprClass:
6548d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::UnaryTypeTraitExprClass:
65496ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  case Expr::BinaryTypeTraitExprClass:
65504ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  case Expr::TypeTraitExprClass:
655121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  case Expr::ArrayTypeTraitExprClass:
6552552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  case Expr::ExpressionTraitExprClass:
65532e156225a29407a50dd19041aa5750171ad44ea3Sebastian Redl  case Expr::CXXNoexceptExprClass:
6554d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return NoDiag();
6555d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CallExprClass:
65566cf750298d3621d8a10a6dd07fcee8e274b9d94dSean Hunt  case Expr::CXXOperatorCallExprClass: {
655705830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith    // C99 6.6/3 allows function calls within unevaluated subexpressions of
655805830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith    // constant expressions, but they can never be ICEs because an ICE cannot
655905830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith    // contain an operand of (pointer to) function type.
6560d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    const CallExpr *CE = cast<CallExpr>(E);
6561180f47959a066795cc0f409433023af448bb0328Richard Smith    if (CE->isBuiltinCall())
6562d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return CheckEvalInICE(E, Ctx);
6563d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return ICEDiag(2, E->getLocStart());
6564d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6565359c89df5479810c9d4784fc0b6ab592eb136777Richard Smith  case Expr::DeclRefExprClass: {
6566d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
6567d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return NoDiag();
6568359c89df5479810c9d4784fc0b6ab592eb136777Richard Smith    const ValueDecl *D = dyn_cast<ValueDecl>(cast<DeclRefExpr>(E)->getDecl());
65694e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (Ctx.getLangOpts().CPlusPlus &&
6570359c89df5479810c9d4784fc0b6ab592eb136777Richard Smith        D && IsConstNonVolatile(D->getType())) {
6571d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      // Parameter variables are never constants.  Without this check,
6572d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      // getAnyInitializer() can find a default argument, which leads
6573d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      // to chaos.
6574d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      if (isa<ParmVarDecl>(D))
6575d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
6576d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6577d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      // C++ 7.1.5.1p2
6578d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      //   A variable of non-volatile const-qualified integral or enumeration
6579d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      //   type initialized by an ICE can be used in ICEs.
6580d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) {
6581db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith        if (!Dcl->getType()->isIntegralOrEnumerationType())
6582db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith          return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
6583db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith
6584099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith        const VarDecl *VD;
6585099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith        // Look for a declaration of this variable that has an initializer, and
6586099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith        // check whether it is an ICE.
6587099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith        if (Dcl->getAnyInitializer(VD) && VD->checkInitIsICE())
6588099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith          return NoDiag();
6589099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith        else
6590099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith          return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
6591d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      }
6592d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    }
6593d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return ICEDiag(2, E->getLocStart());
6594359c89df5479810c9d4784fc0b6ab592eb136777Richard Smith  }
6595d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::UnaryOperatorClass: {
6596d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    const UnaryOperator *Exp = cast<UnaryOperator>(E);
6597d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    switch (Exp->getOpcode()) {
65982de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PostInc:
65992de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PostDec:
66002de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PreInc:
66012de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PreDec:
66022de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_AddrOf:
66032de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Deref:
660405830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith      // C99 6.6/3 allows increment and decrement within unevaluated
660505830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith      // subexpressions of constant expressions, but they can never be ICEs
660605830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith      // because an ICE cannot contain an lvalue operand.
6607d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return ICEDiag(2, E->getLocStart());
66082de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Extension:
66092de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_LNot:
66102de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Plus:
66112de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Minus:
66122de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Not:
66132de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Real:
66142de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Imag:
6615d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return CheckICE(Exp->getSubExpr(), Ctx);
6616d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    }
6617d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6618d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    // OffsetOf falls through here.
6619d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6620d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::OffsetOfExprClass: {
6621d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      // Note that per C99, offsetof must be an ICE. And AFAIK, using
662251f4708c00110940ca3f337961915f2ca1668375Richard Smith      // EvaluateAsRValue matches the proposed gcc behavior for cases like
662305830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith      // "offsetof(struct s{int x[4];}, x[1.0])".  This doesn't affect
6624d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      // compliance: we should warn earlier for offsetof expressions with
6625d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      // array subscripts that aren't ICEs, and if the array subscripts
6626d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      // are ICEs, the value of the offsetof must be an integer constant.
6627d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return CheckEvalInICE(E, Ctx);
6628d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6629f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  case Expr::UnaryExprOrTypeTraitExprClass: {
6630f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E);
6631f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    if ((Exp->getKind() ==  UETT_SizeOf) &&
6632f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne        Exp->getTypeOfArgument()->isVariableArrayType())
6633d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return ICEDiag(2, E->getLocStart());
6634d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return NoDiag();
6635d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6636d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::BinaryOperatorClass: {
6637d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    const BinaryOperator *Exp = cast<BinaryOperator>(E);
6638d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    switch (Exp->getOpcode()) {
66392de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_PtrMemD:
66402de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_PtrMemI:
66412de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Assign:
66422de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_MulAssign:
66432de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_DivAssign:
66442de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_RemAssign:
66452de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_AddAssign:
66462de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_SubAssign:
66472de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_ShlAssign:
66482de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_ShrAssign:
66492de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_AndAssign:
66502de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_XorAssign:
66512de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_OrAssign:
665205830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith      // C99 6.6/3 allows assignments within unevaluated subexpressions of
665305830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith      // constant expressions, but they can never be ICEs because an ICE cannot
665405830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith      // contain an lvalue operand.
6655d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return ICEDiag(2, E->getLocStart());
6656d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
66572de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Mul:
66582de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Div:
66592de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Rem:
66602de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Add:
66612de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Sub:
66622de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Shl:
66632de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Shr:
66642de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LT:
66652de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_GT:
66662de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LE:
66672de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_GE:
66682de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_EQ:
66692de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_NE:
66702de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_And:
66712de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Xor:
66722de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Or:
66732de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Comma: {
6674d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
6675d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
66762de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      if (Exp->getOpcode() == BO_Div ||
66772de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall          Exp->getOpcode() == BO_Rem) {
667851f4708c00110940ca3f337961915f2ca1668375Richard Smith        // EvaluateAsRValue gives an error for undefined Div/Rem, so make sure
6679d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        // we don't evaluate one.
66803b332ab132fa85c83833d74d400f6e126f52fbd2John McCall        if (LHSResult.Val == 0 && RHSResult.Val == 0) {
6681a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx);
6682d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall          if (REval == 0)
6683d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall            return ICEDiag(1, E->getLocStart());
6684d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall          if (REval.isSigned() && REval.isAllOnesValue()) {
6685a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith            llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx);
6686d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall            if (LEval.isMinSignedValue())
6687d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall              return ICEDiag(1, E->getLocStart());
6688d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall          }
6689d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        }
6690d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      }
66912de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      if (Exp->getOpcode() == BO_Comma) {
66924e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie        if (Ctx.getLangOpts().C99) {
6693d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall          // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
6694d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall          // if it isn't evaluated.
6695d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall          if (LHSResult.Val == 0 && RHSResult.Val == 0)
6696d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall            return ICEDiag(1, E->getLocStart());
6697d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        } else {
6698d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall          // In both C89 and C++, commas in ICEs are illegal.
6699d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall          return ICEDiag(2, E->getLocStart());
6700d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        }
6701d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      }
6702d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      if (LHSResult.Val >= RHSResult.Val)
6703d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        return LHSResult;
6704d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return RHSResult;
6705d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    }
67062de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LAnd:
67072de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LOr: {
6708d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
6709d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
6710d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      if (LHSResult.Val == 0 && RHSResult.Val == 1) {
6711d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        // Rare case where the RHS has a comma "side-effect"; we need
6712d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        // to actually check the condition to see whether the side
6713d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        // with the comma is evaluated.
67142de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall        if ((Exp->getOpcode() == BO_LAnd) !=
6715a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith            (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0))
6716d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall          return RHSResult;
6717d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        return NoDiag();
6718d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      }
6719d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6720d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      if (LHSResult.Val >= RHSResult.Val)
6721d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        return LHSResult;
6722d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return RHSResult;
6723d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    }
6724d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    }
6725d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6726d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ImplicitCastExprClass:
6727d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CStyleCastExprClass:
6728d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXFunctionalCastExprClass:
6729d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXStaticCastExprClass:
6730d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXReinterpretCastExprClass:
673132cb47174304bc7ec11478b9497c4e10f48273d9Richard Smith  case Expr::CXXConstCastExprClass:
6732f85e193739c953358c865005855253af4f68a497John McCall  case Expr::ObjCBridgedCastExprClass: {
6733d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
67342116b144cf07f2574d20517187eb8863645376ebRichard Smith    if (isa<ExplicitCastExpr>(E)) {
67352116b144cf07f2574d20517187eb8863645376ebRichard Smith      if (const FloatingLiteral *FL
67362116b144cf07f2574d20517187eb8863645376ebRichard Smith            = dyn_cast<FloatingLiteral>(SubExpr->IgnoreParenImpCasts())) {
67372116b144cf07f2574d20517187eb8863645376ebRichard Smith        unsigned DestWidth = Ctx.getIntWidth(E->getType());
67382116b144cf07f2574d20517187eb8863645376ebRichard Smith        bool DestSigned = E->getType()->isSignedIntegerOrEnumerationType();
67392116b144cf07f2574d20517187eb8863645376ebRichard Smith        APSInt IgnoredVal(DestWidth, !DestSigned);
67402116b144cf07f2574d20517187eb8863645376ebRichard Smith        bool Ignored;
67412116b144cf07f2574d20517187eb8863645376ebRichard Smith        // If the value does not fit in the destination type, the behavior is
67422116b144cf07f2574d20517187eb8863645376ebRichard Smith        // undefined, so we are not required to treat it as a constant
67432116b144cf07f2574d20517187eb8863645376ebRichard Smith        // expression.
67442116b144cf07f2574d20517187eb8863645376ebRichard Smith        if (FL->getValue().convertToInteger(IgnoredVal,
67452116b144cf07f2574d20517187eb8863645376ebRichard Smith                                            llvm::APFloat::rmTowardZero,
67462116b144cf07f2574d20517187eb8863645376ebRichard Smith                                            &Ignored) & APFloat::opInvalidOp)
67472116b144cf07f2574d20517187eb8863645376ebRichard Smith          return ICEDiag(2, E->getLocStart());
67482116b144cf07f2574d20517187eb8863645376ebRichard Smith        return NoDiag();
67492116b144cf07f2574d20517187eb8863645376ebRichard Smith      }
67502116b144cf07f2574d20517187eb8863645376ebRichard Smith    }
6751eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman    switch (cast<CastExpr>(E)->getCastKind()) {
6752eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman    case CK_LValueToRValue:
67537a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall    case CK_AtomicToNonAtomic:
67547a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall    case CK_NonAtomicToAtomic:
6755eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman    case CK_NoOp:
6756eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman    case CK_IntegralToBoolean:
6757eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman    case CK_IntegralCast:
6758d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return CheckICE(SubExpr, Ctx);
6759eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman    default:
6760eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman      return ICEDiag(2, E->getLocStart());
6761eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman    }
6762d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
676356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  case Expr::BinaryConditionalOperatorClass: {
676456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E);
676556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    ICEDiag CommonResult = CheckICE(Exp->getCommon(), Ctx);
676656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    if (CommonResult.Val == 2) return CommonResult;
676756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
676856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    if (FalseResult.Val == 2) return FalseResult;
676956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    if (CommonResult.Val == 1) return CommonResult;
677056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    if (FalseResult.Val == 1 &&
6771a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith        Exp->getCommon()->EvaluateKnownConstInt(Ctx) == 0) return NoDiag();
677256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    return FalseResult;
677356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  }
6774d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ConditionalOperatorClass: {
6775d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
6776d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    // If the condition (ignoring parens) is a __builtin_constant_p call,
6777d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    // then only the true side is actually considered in an integer constant
6778d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    // expression, and it is fully evaluated.  This is an important GNU
6779d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    // extension.  See GCC PR38377 for discussion.
6780d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    if (const CallExpr *CallCE
6781d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
678280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith      if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p)
678380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith        return CheckEvalInICE(E, Ctx);
6784d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
6785d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    if (CondResult.Val == 2)
6786d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return CondResult;
678763fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor
6788f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
6789f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
679063fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor
6791d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    if (TrueResult.Val == 2)
6792d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return TrueResult;
6793d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    if (FalseResult.Val == 2)
6794d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return FalseResult;
6795d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    if (CondResult.Val == 1)
6796d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return CondResult;
6797d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    if (TrueResult.Val == 0 && FalseResult.Val == 0)
6798d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return NoDiag();
6799d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    // Rare case where the diagnostics depend on which side is evaluated
6800d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    // Note that if we get here, CondResult is 0, and at least one of
6801d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    // TrueResult and FalseResult is non-zero.
6802a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith    if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0) {
6803d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return FalseResult;
6804d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    }
6805d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return TrueResult;
6806d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6807d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXDefaultArgExprClass:
6808d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
6809d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ChooseExprClass: {
6810d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
6811d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6812d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6813d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
68143026348bd4c13a0f83b59839f64065e0fcbea253David Blaikie  llvm_unreachable("Invalid StmtClass!");
6815d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall}
6816d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6817f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// Evaluate an expression as a C++11 integral constant expression.
6818f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool EvaluateCPlusPlus11IntegralConstantExpr(ASTContext &Ctx,
6819f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                                                    const Expr *E,
6820f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                                                    llvm::APSInt *Value,
6821f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                                                    SourceLocation *Loc) {
6822f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (!E->getType()->isIntegralOrEnumerationType()) {
6823f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (Loc) *Loc = E->getExprLoc();
6824f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return false;
6825f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
6826f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
68274c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  APValue Result;
68284c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  if (!E->isCXX11ConstantExpr(Ctx, &Result, Loc))
6829dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    return false;
6830dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith
68314c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  assert(Result.isInt() && "pointer cast to int is not an ICE");
68324c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  if (Value) *Value = Result.getInt();
6833dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith  return true;
6834f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith}
6835f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
6836dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smithbool Expr::isIntegerConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
68374e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (Ctx.getLangOpts().CPlusPlus0x)
6838f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, 0, Loc);
6839f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
6840d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  ICEDiag d = CheckICE(this, Ctx);
6841d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  if (d.Val != 0) {
6842d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    if (Loc) *Loc = d.Loc;
6843d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return false;
6844d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6845f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  return true;
6846f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith}
6847f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
6848f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithbool Expr::isIntegerConstantExpr(llvm::APSInt &Value, ASTContext &Ctx,
6849f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                                 SourceLocation *Loc, bool isEvaluated) const {
68504e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (Ctx.getLangOpts().CPlusPlus0x)
6851f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, &Value, Loc);
6852f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
6853f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (!isIntegerConstantExpr(Ctx, Loc))
6854f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return false;
6855f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (!EvaluateAsInt(Value, Ctx))
6856d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    llvm_unreachable("ICE cannot be evaluated!");
6857d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  return true;
6858d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall}
68594c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith
686070488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smithbool Expr::isCXX98IntegralConstantExpr(ASTContext &Ctx) const {
686170488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smith  return CheckICE(this, Ctx).Val == 0;
686270488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smith}
686370488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smith
68644c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smithbool Expr::isCXX11ConstantExpr(ASTContext &Ctx, APValue *Result,
68654c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith                               SourceLocation *Loc) const {
68664c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  // We support this checking in C++98 mode in order to diagnose compatibility
68674c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  // issues.
68684e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  assert(Ctx.getLangOpts().CPlusPlus);
68694c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith
687070488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smith  // Build evaluation settings.
68714c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  Expr::EvalStatus Status;
68724c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  llvm::SmallVector<PartialDiagnosticAt, 8> Diags;
68734c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  Status.Diag = &Diags;
68744c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  EvalInfo Info(Ctx, Status);
68754c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith
68764c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  APValue Scratch;
68774c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  bool IsConstExpr = ::EvaluateAsRValue(Info, this, Result ? *Result : Scratch);
68784c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith
68794c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  if (!Diags.empty()) {
68804c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    IsConstExpr = false;
68814c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    if (Loc) *Loc = Diags[0].first;
68824c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  } else if (!IsConstExpr) {
68834c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    // FIXME: This shouldn't happen.
68844c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    if (Loc) *Loc = getExprLoc();
68854c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  }
68864c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith
68874c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  return IsConstExpr;
68884c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith}
6889745f5147e065900267c85a5568785a1991d4838fRichard Smith
6890745f5147e065900267c85a5568785a1991d4838fRichard Smithbool Expr::isPotentialConstantExpr(const FunctionDecl *FD,
6891745f5147e065900267c85a5568785a1991d4838fRichard Smith                                   llvm::SmallVectorImpl<
6892745f5147e065900267c85a5568785a1991d4838fRichard Smith                                     PartialDiagnosticAt> &Diags) {
6893745f5147e065900267c85a5568785a1991d4838fRichard Smith  // FIXME: It would be useful to check constexpr function templates, but at the
6894745f5147e065900267c85a5568785a1991d4838fRichard Smith  // moment the constant expression evaluator cannot cope with the non-rigorous
6895745f5147e065900267c85a5568785a1991d4838fRichard Smith  // ASTs which we build for dependent expressions.
6896745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (FD->isDependentContext())
6897745f5147e065900267c85a5568785a1991d4838fRichard Smith    return true;
6898745f5147e065900267c85a5568785a1991d4838fRichard Smith
6899745f5147e065900267c85a5568785a1991d4838fRichard Smith  Expr::EvalStatus Status;
6900745f5147e065900267c85a5568785a1991d4838fRichard Smith  Status.Diag = &Diags;
6901745f5147e065900267c85a5568785a1991d4838fRichard Smith
6902745f5147e065900267c85a5568785a1991d4838fRichard Smith  EvalInfo Info(FD->getASTContext(), Status);
6903745f5147e065900267c85a5568785a1991d4838fRichard Smith  Info.CheckingPotentialConstantExpression = true;
6904745f5147e065900267c85a5568785a1991d4838fRichard Smith
6905745f5147e065900267c85a5568785a1991d4838fRichard Smith  const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
6906745f5147e065900267c85a5568785a1991d4838fRichard Smith  const CXXRecordDecl *RD = MD ? MD->getParent()->getCanonicalDecl() : 0;
6907745f5147e065900267c85a5568785a1991d4838fRichard Smith
6908745f5147e065900267c85a5568785a1991d4838fRichard Smith  // FIXME: Fabricate an arbitrary expression on the stack and pretend that it
6909745f5147e065900267c85a5568785a1991d4838fRichard Smith  // is a temporary being used as the 'this' pointer.
6910745f5147e065900267c85a5568785a1991d4838fRichard Smith  LValue This;
6911745f5147e065900267c85a5568785a1991d4838fRichard Smith  ImplicitValueInitExpr VIE(RD ? Info.Ctx.getRecordType(RD) : Info.Ctx.IntTy);
691283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith  This.set(&VIE, Info.CurrentCall->Index);
6913745f5147e065900267c85a5568785a1991d4838fRichard Smith
6914745f5147e065900267c85a5568785a1991d4838fRichard Smith  ArrayRef<const Expr*> Args;
6915745f5147e065900267c85a5568785a1991d4838fRichard Smith
6916745f5147e065900267c85a5568785a1991d4838fRichard Smith  SourceLocation Loc = FD->getLocation();
6917745f5147e065900267c85a5568785a1991d4838fRichard Smith
69181aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  APValue Scratch;
69191aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
6920745f5147e065900267c85a5568785a1991d4838fRichard Smith    HandleConstructorCall(Loc, This, Args, CD, Info, Scratch);
69211aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith  else
6922745f5147e065900267c85a5568785a1991d4838fRichard Smith    HandleFunctionCall(Loc, FD, (MD && MD->isInstance()) ? &This : 0,
6923745f5147e065900267c85a5568785a1991d4838fRichard Smith                       Args, FD->getBody(), Info, Scratch);
6924745f5147e065900267c85a5568785a1991d4838fRichard Smith
6925745f5147e065900267c85a5568785a1991d4838fRichard Smith  return Diags.empty();
6926745f5147e065900267c85a5568785a1991d4838fRichard Smith}
6927