ExprConstant.cpp revision 9ec7197796a2730d54ae7f632553b5311b2ba3b5
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"
4500bd44d5677783527d7517c1ffe45e4d75a0f56fBenjamin Kramer#include "clang/Basic/PartialDiagnostic.h"
4606a3675627e3b3c47b49c689c8e404a33144194aAnders Carlsson#include "clang/Basic/TargetInfo.h"
477462b39a9bccaf4392687831036713f09f9c0681Mike Stump#include "llvm/ADT/SmallString.h"
484572baba9d18c275968ac113fd73b0e3c77cccb8Mike Stump#include <cstring>
497b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith#include <functional>
504572baba9d18c275968ac113fd73b0e3c77cccb8Mike Stump
51c44eec6dd29ee9415cbd38a35deff4c8b67abb6aAnders Carlssonusing namespace clang;
52f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattnerusing llvm::APSInt;
53d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmanusing llvm::APFloat;
54c44eec6dd29ee9415cbd38a35deff4c8b67abb6aAnders Carlsson
5587eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner/// EvalInfo - This is a private struct used by the evaluator to capture
5687eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner/// information about a subexpression as it is folded.  It retains information
5787eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner/// about the AST context, but also maintains information about the folded
5887eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner/// expression.
5987eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner///
6087eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner/// If an expression could be evaluated, it is still possible it is not a C
6187eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner/// "integer constant expression" or constant expression.  If not, this struct
6287eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner/// captures information about how and why not.
6387eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner///
6487eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner/// One bit of information passed *into* the request for constant folding
6587eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner/// indicates whether the subexpression is "evaluated" or not according to C
6687eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner/// rules.  For example, the RHS of (0 && foo()) is not evaluated.  We can
6787eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner/// evaluate the expression regardless of what the RHS is, but C only allows
6887eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner/// certain things in certain situations.
69c54061a679d8db4169438b2f97f81e3f443c6a25Benjamin Kramernamespace {
70180f47959a066795cc0f409433023af448bb0328Richard Smith  struct LValue;
71d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  struct CallStackFrame;
72bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith  struct EvalInfo;
73d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
741bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  QualType getType(APValue::LValueBase B) {
751bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    if (!B) return QualType();
761bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>())
771bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith      return D->getType();
781bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    return B.get<const Expr*>()->getType();
791bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  }
801bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith
81180f47959a066795cc0f409433023af448bb0328Richard Smith  /// Get an LValue path entry, which is known to not be an array index, as a
82f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  /// field or base class.
83f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  APValue::BaseOrMemberType getAsBaseOrMember(APValue::LValuePathEntry E) {
84180f47959a066795cc0f409433023af448bb0328Richard Smith    APValue::BaseOrMemberType Value;
85180f47959a066795cc0f409433023af448bb0328Richard Smith    Value.setFromOpaqueValue(E.BaseOrMember);
86f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    return Value;
87f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  }
88f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
89f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  /// Get an LValue path entry, which is known to not be an array index, as a
90f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  /// field declaration.
91f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  const FieldDecl *getAsField(APValue::LValuePathEntry E) {
92f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    return dyn_cast<FieldDecl>(getAsBaseOrMember(E).getPointer());
93180f47959a066795cc0f409433023af448bb0328Richard Smith  }
94180f47959a066795cc0f409433023af448bb0328Richard Smith  /// Get an LValue path entry, which is known to not be an array index, as a
95180f47959a066795cc0f409433023af448bb0328Richard Smith  /// base class declaration.
96180f47959a066795cc0f409433023af448bb0328Richard Smith  const CXXRecordDecl *getAsBaseClass(APValue::LValuePathEntry E) {
97f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    return dyn_cast<CXXRecordDecl>(getAsBaseOrMember(E).getPointer());
98180f47959a066795cc0f409433023af448bb0328Richard Smith  }
99180f47959a066795cc0f409433023af448bb0328Richard Smith  /// Determine whether this LValue path entry for a base class names a virtual
100180f47959a066795cc0f409433023af448bb0328Richard Smith  /// base class.
101180f47959a066795cc0f409433023af448bb0328Richard Smith  bool isVirtualBaseClass(APValue::LValuePathEntry E) {
102f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    return getAsBaseOrMember(E).getInt();
103180f47959a066795cc0f409433023af448bb0328Richard Smith  }
104180f47959a066795cc0f409433023af448bb0328Richard Smith
105b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  /// Find the path length and type of the most-derived subobject in the given
106b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  /// path, and find the size of the containing array, if any.
107b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  static
108b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  unsigned findMostDerivedSubobject(ASTContext &Ctx, QualType Base,
109b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                                    ArrayRef<APValue::LValuePathEntry> Path,
110b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                                    uint64_t &ArraySize, QualType &Type) {
111b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    unsigned MostDerivedLength = 0;
112b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    Type = Base;
1139a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith    for (unsigned I = 0, N = Path.size(); I != N; ++I) {
114b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      if (Type->isArrayType()) {
115b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        const ConstantArrayType *CAT =
116b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith          cast<ConstantArrayType>(Ctx.getAsArrayType(Type));
117b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        Type = CAT->getElementType();
118b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        ArraySize = CAT->getSize().getZExtValue();
119b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        MostDerivedLength = I + 1;
120b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      } else if (const FieldDecl *FD = getAsField(Path[I])) {
121b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        Type = FD->getType();
122b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        ArraySize = 0;
123b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        MostDerivedLength = I + 1;
124b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      } else {
1259a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith        // Path[I] describes a base class.
126b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        ArraySize = 0;
127b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      }
1289a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith    }
129b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    return MostDerivedLength;
1309a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith  }
1319a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith
132b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // The order of this enum is important for diagnostics.
133b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  enum CheckSubobjectKind {
134b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith    CSK_Base, CSK_Derived, CSK_Field, CSK_ArrayToPointer, CSK_ArrayIndex,
135b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith    CSK_This
136b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  };
137b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
1380a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith  /// A path from a glvalue to a subobject of that glvalue.
1390a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith  struct SubobjectDesignator {
1400a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    /// True if the subobject was named in a manner not supported by C++11. Such
1410a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    /// lvalues can still be folded, but they are not core constant expressions
1420a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    /// and we cannot perform lvalue-to-rvalue conversions on them.
1430a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    bool Invalid : 1;
1440a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith
145b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    /// Is this a pointer one past the end of an object?
146b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    bool IsOnePastTheEnd : 1;
147b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
148b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    /// The length of the path to the most-derived object of which this is a
149b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    /// subobject.
150b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    unsigned MostDerivedPathLength : 30;
151b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
152b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    /// The size of the array of which the most-derived object is an element, or
153b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    /// 0 if the most-derived object is not an array element.
154b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    uint64_t MostDerivedArraySize;
1550a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith
156b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    /// The type of the most derived object referred to by this address.
157b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    QualType MostDerivedType;
1580a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith
1599a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith    typedef APValue::LValuePathEntry PathEntry;
1609a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith
1610a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    /// The entries on the path from the glvalue to the designated subobject.
1620a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    SmallVector<PathEntry, 8> Entries;
1630a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith
164b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    SubobjectDesignator() : Invalid(true) {}
1650a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith
166b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    explicit SubobjectDesignator(QualType T)
167b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      : Invalid(false), IsOnePastTheEnd(false), MostDerivedPathLength(0),
168b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        MostDerivedArraySize(0), MostDerivedType(T) {}
169b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
170b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    SubobjectDesignator(ASTContext &Ctx, const APValue &V)
171b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      : Invalid(!V.isLValue() || !V.hasLValuePath()), IsOnePastTheEnd(false),
172b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        MostDerivedPathLength(0), MostDerivedArraySize(0) {
1739a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith      if (!Invalid) {
174b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        IsOnePastTheEnd = V.isLValueOnePastTheEnd();
1759a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith        ArrayRef<PathEntry> VEntries = V.getLValuePath();
1769a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith        Entries.insert(Entries.end(), VEntries.begin(), VEntries.end());
1779a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith        if (V.getLValueBase())
178b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith          MostDerivedPathLength =
179b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith              findMostDerivedSubobject(Ctx, getType(V.getLValueBase()),
180b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                                       V.getLValuePath(), MostDerivedArraySize,
181b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                                       MostDerivedType);
1829a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith      }
1839a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith    }
1849a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith
1850a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    void setInvalid() {
1860a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      Invalid = true;
1870a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      Entries.clear();
1880a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    }
189b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
190b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    /// Determine whether this is a one-past-the-end pointer.
191b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    bool isOnePastTheEnd() const {
192b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      if (IsOnePastTheEnd)
193b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        return true;
194b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      if (MostDerivedArraySize &&
195b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith          Entries[MostDerivedPathLength - 1].ArrayIndex == MostDerivedArraySize)
196b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        return true;
197b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      return false;
198b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    }
199b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
200b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    /// Check that this refers to a valid subobject.
201b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    bool isValidSubobject() const {
202b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      if (Invalid)
203b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        return false;
204b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      return !isOnePastTheEnd();
205b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    }
206b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    /// Check that this refers to a valid subobject, and if not, produce a
207b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    /// relevant diagnostic and set the designator as invalid.
208b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK);
209b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
210b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    /// Update this designator to refer to the first element within this array.
211b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    void addArrayUnchecked(const ConstantArrayType *CAT) {
2120a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      PathEntry Entry;
213b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      Entry.ArrayIndex = 0;
2140a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      Entries.push_back(Entry);
215b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
216b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      // This is a most-derived object.
217b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      MostDerivedType = CAT->getElementType();
218b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      MostDerivedArraySize = CAT->getSize().getZExtValue();
219b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      MostDerivedPathLength = Entries.size();
2200a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    }
2210a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    /// Update this designator to refer to the given base or member of this
2220a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    /// object.
223b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    void addDeclUnchecked(const Decl *D, bool Virtual = false) {
2240a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      PathEntry Entry;
225180f47959a066795cc0f409433023af448bb0328Richard Smith      APValue::BaseOrMemberType Value(D, Virtual);
226180f47959a066795cc0f409433023af448bb0328Richard Smith      Entry.BaseOrMember = Value.getOpaqueValue();
2270a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      Entries.push_back(Entry);
228b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
229b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      // If this isn't a base class, it's a new most-derived object.
230b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      if (const FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
231b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        MostDerivedType = FD->getType();
232b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        MostDerivedArraySize = 0;
233b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        MostDerivedPathLength = Entries.size();
234b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      }
2350a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    }
236b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    void diagnosePointerArithmetic(EvalInfo &Info, const Expr *E, uint64_t N);
2370a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    /// Add N to the address of this subobject.
238b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    void adjustIndex(EvalInfo &Info, const Expr *E, uint64_t N) {
2390a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      if (Invalid) return;
240b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      if (MostDerivedPathLength == Entries.size() && MostDerivedArraySize) {
2419a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith        Entries.back().ArrayIndex += N;
242b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        if (Entries.back().ArrayIndex > MostDerivedArraySize) {
243b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith          diagnosePointerArithmetic(Info, E, Entries.back().ArrayIndex);
244b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith          setInvalid();
245b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        }
2460a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith        return;
2470a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      }
248b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      // [expr.add]p4: For the purposes of these operators, a pointer to a
249b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      // nonarray object behaves the same as a pointer to the first element of
250b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      // an array of length one with the type of the object as its element type.
251b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      if (IsOnePastTheEnd && N == (uint64_t)-1)
252b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        IsOnePastTheEnd = false;
253b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      else if (!IsOnePastTheEnd && N == 1)
254b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        IsOnePastTheEnd = true;
255b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      else if (N != 0) {
256b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        diagnosePointerArithmetic(Info, E, uint64_t(IsOnePastTheEnd) + N);
2570a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith        setInvalid();
258b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      }
2590a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    }
2600a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith  };
2610a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith
26247a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith  /// A core constant value. This can be the value of any constant expression,
26347a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith  /// or a pointer or reference to a non-static object or function parameter.
264e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  ///
265e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  /// For an LValue, the base and offset are stored in the APValue subobject,
266e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  /// but the other information is stored in the SubobjectDesignator. For all
267e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  /// other value kinds, the value is stored directly in the APValue subobject.
26847a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith  class CCValue : public APValue {
26947a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    typedef llvm::APSInt APSInt;
27047a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    typedef llvm::APFloat APFloat;
271177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith    /// If the value is a reference or pointer into a parameter or temporary,
272177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith    /// this is the corresponding call stack frame.
273177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith    CallStackFrame *CallFrame;
2740a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    /// If the value is a reference or pointer, this is a description of how the
2750a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    /// subobject was specified.
2760a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    SubobjectDesignator Designator;
27747a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith  public:
278177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith    struct GlobalValue {};
279177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith
28047a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    CCValue() {}
28147a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    explicit CCValue(const APSInt &I) : APValue(I) {}
28247a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    explicit CCValue(const APFloat &F) : APValue(F) {}
28347a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    CCValue(const APValue *E, unsigned N) : APValue(E, N) {}
28447a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    CCValue(const APSInt &R, const APSInt &I) : APValue(R, I) {}
28547a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    CCValue(const APFloat &R, const APFloat &I) : APValue(R, I) {}
286177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith    CCValue(const CCValue &V) : APValue(V), CallFrame(V.CallFrame) {}
2871bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    CCValue(LValueBase B, const CharUnits &O, CallStackFrame *F,
2880a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith            const SubobjectDesignator &D) :
2899a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith      APValue(B, O, APValue::NoLValuePath()), CallFrame(F), Designator(D) {}
290b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    CCValue(ASTContext &Ctx, const APValue &V, GlobalValue) :
291b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      APValue(V), CallFrame(0), Designator(Ctx, V) {}
292e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    CCValue(const ValueDecl *D, bool IsDerivedMember,
293e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith            ArrayRef<const CXXRecordDecl*> Path) :
294e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      APValue(D, IsDerivedMember, Path) {}
29565639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman    CCValue(const AddrLabelExpr* LHSExpr, const AddrLabelExpr* RHSExpr) :
29665639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman      APValue(LHSExpr, RHSExpr) {}
29747a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith
298177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith    CallStackFrame *getLValueFrame() const {
29947a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith      assert(getKind() == LValue);
300177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith      return CallFrame;
30147a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    }
3020a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    SubobjectDesignator &getLValueDesignator() {
3030a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      assert(getKind() == LValue);
3040a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      return Designator;
3050a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    }
3060a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    const SubobjectDesignator &getLValueDesignator() const {
3070a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      return const_cast<CCValue*>(this)->getLValueDesignator();
3080a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    }
30947a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith  };
31047a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith
311bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith  /// A stack frame in the constexpr call stack.
312bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith  struct CallStackFrame {
313bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    EvalInfo &Info;
314bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith
315bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    /// Parent - The caller of this stack frame.
316bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    CallStackFrame *Caller;
317bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith
31808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    /// CallLoc - The location of the call expression for this call.
31908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    SourceLocation CallLoc;
32008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
32108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    /// Callee - The function which was called.
32208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    const FunctionDecl *Callee;
32308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
324180f47959a066795cc0f409433023af448bb0328Richard Smith    /// This - The binding for the this pointer in this call, if any.
325180f47959a066795cc0f409433023af448bb0328Richard Smith    const LValue *This;
326180f47959a066795cc0f409433023af448bb0328Richard Smith
327bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    /// ParmBindings - Parameter bindings for this function call, indexed by
328bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    /// parameters' function scope indices.
329bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    const CCValue *Arguments;
330bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith
331bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    typedef llvm::DenseMap<const Expr*, CCValue> MapTy;
332bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    typedef MapTy::const_iterator temp_iterator;
333bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    /// Temporaries - Temporary lvalues materialized within this stack frame.
334bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    MapTy Temporaries;
335bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith
33608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    CallStackFrame(EvalInfo &Info, SourceLocation CallLoc,
33708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith                   const FunctionDecl *Callee, const LValue *This,
338180f47959a066795cc0f409433023af448bb0328Richard Smith                   const CCValue *Arguments);
339bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    ~CallStackFrame();
340bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith  };
341bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith
342dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith  /// A partial diagnostic which we might know in advance that we are not going
343dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith  /// to emit.
344dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith  class OptionalDiagnostic {
345dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    PartialDiagnostic *Diag;
346dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith
347dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith  public:
348dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    explicit OptionalDiagnostic(PartialDiagnostic *Diag = 0) : Diag(Diag) {}
349dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith
350dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    template<typename T>
351dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    OptionalDiagnostic &operator<<(const T &v) {
352dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith      if (Diag)
353dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith        *Diag << v;
354dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith      return *this;
355dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    }
356789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith
357789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    OptionalDiagnostic &operator<<(const APSInt &I) {
358789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith      if (Diag) {
359789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith        llvm::SmallVector<char, 32> Buffer;
360789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith        I.toString(Buffer);
361789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith        *Diag << StringRef(Buffer.data(), Buffer.size());
362789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith      }
363789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith      return *this;
364789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    }
365789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith
366789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    OptionalDiagnostic &operator<<(const APFloat &F) {
367789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith      if (Diag) {
368789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith        llvm::SmallVector<char, 32> Buffer;
369789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith        F.toString(Buffer);
370789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith        *Diag << StringRef(Buffer.data(), Buffer.size());
371789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith      }
372789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith      return *this;
373789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    }
374dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith  };
375dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith
376c54061a679d8db4169438b2f97f81e3f443c6a25Benjamin Kramer  struct EvalInfo {
377dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    ASTContext &Ctx;
3781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3791e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    /// EvalStatus - Contains information about the evaluation.
3801e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    Expr::EvalStatus &EvalStatus;
381f0c1e4b679e15c26bffb5892e35985bf3c52f77aAnders Carlsson
382d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    /// CurrentCall - The top of the constexpr call stack.
383bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    CallStackFrame *CurrentCall;
384d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
385d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    /// CallStackDepth - The number of calls in the call stack right now.
386d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    unsigned CallStackDepth;
387d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
38847a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    typedef llvm::DenseMap<const OpaqueValueExpr*, CCValue> MapTy;
389bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    /// OpaqueValues - Values used as the common expression in a
390bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    /// BinaryConditionalOperator.
391c54061a679d8db4169438b2f97f81e3f443c6a25Benjamin Kramer    MapTy OpaqueValues;
392bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith
393bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    /// BottomFrame - The frame in which evaluation started. This must be
394745f5147e065900267c85a5568785a1991d4838fRichard Smith    /// initialized after CurrentCall and CallStackDepth.
395bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    CallStackFrame BottomFrame;
396bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith
397180f47959a066795cc0f409433023af448bb0328Richard Smith    /// EvaluatingDecl - This is the declaration whose initializer is being
398180f47959a066795cc0f409433023af448bb0328Richard Smith    /// evaluated, if any.
399180f47959a066795cc0f409433023af448bb0328Richard Smith    const VarDecl *EvaluatingDecl;
400180f47959a066795cc0f409433023af448bb0328Richard Smith
401180f47959a066795cc0f409433023af448bb0328Richard Smith    /// EvaluatingDeclValue - This is the value being constructed for the
402180f47959a066795cc0f409433023af448bb0328Richard Smith    /// declaration whose initializer is being evaluated, if any.
403180f47959a066795cc0f409433023af448bb0328Richard Smith    APValue *EvaluatingDeclValue;
404180f47959a066795cc0f409433023af448bb0328Richard Smith
405c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    /// HasActiveDiagnostic - Was the previous diagnostic stored? If so, further
406c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    /// notes attached to it will also be stored, otherwise they will not be.
407c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    bool HasActiveDiagnostic;
408c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
409745f5147e065900267c85a5568785a1991d4838fRichard Smith    /// CheckingPotentialConstantExpression - Are we checking whether the
410745f5147e065900267c85a5568785a1991d4838fRichard Smith    /// expression is a potential constant expression? If so, some diagnostics
411745f5147e065900267c85a5568785a1991d4838fRichard Smith    /// are suppressed.
412745f5147e065900267c85a5568785a1991d4838fRichard Smith    bool CheckingPotentialConstantExpression;
413745f5147e065900267c85a5568785a1991d4838fRichard Smith
414bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith
415bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    EvalInfo(const ASTContext &C, Expr::EvalStatus &S)
416dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith      : Ctx(const_cast<ASTContext&>(C)), EvalStatus(S), CurrentCall(0),
41708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        CallStackDepth(0), BottomFrame(*this, SourceLocation(), 0, 0, 0),
418745f5147e065900267c85a5568785a1991d4838fRichard Smith        EvaluatingDecl(0), EvaluatingDeclValue(0), HasActiveDiagnostic(false),
419745f5147e065900267c85a5568785a1991d4838fRichard Smith        CheckingPotentialConstantExpression(false) {}
420bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith
42147a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    const CCValue *getOpaqueValue(const OpaqueValueExpr *e) const {
422c54061a679d8db4169438b2f97f81e3f443c6a25Benjamin Kramer      MapTy::const_iterator i = OpaqueValues.find(e);
423c54061a679d8db4169438b2f97f81e3f443c6a25Benjamin Kramer      if (i == OpaqueValues.end()) return 0;
424c54061a679d8db4169438b2f97f81e3f443c6a25Benjamin Kramer      return &i->second;
425c54061a679d8db4169438b2f97f81e3f443c6a25Benjamin Kramer    }
42656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
427180f47959a066795cc0f409433023af448bb0328Richard Smith    void setEvaluatingDecl(const VarDecl *VD, APValue &Value) {
428180f47959a066795cc0f409433023af448bb0328Richard Smith      EvaluatingDecl = VD;
429180f47959a066795cc0f409433023af448bb0328Richard Smith      EvaluatingDeclValue = &Value;
430180f47959a066795cc0f409433023af448bb0328Richard Smith    }
431180f47959a066795cc0f409433023af448bb0328Richard Smith
432c18c42345636e2866fed75c7e434fb659d747672Richard Smith    const LangOptions &getLangOpts() const { return Ctx.getLangOptions(); }
433c18c42345636e2866fed75c7e434fb659d747672Richard Smith
434c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    bool CheckCallLimit(SourceLocation Loc) {
435745f5147e065900267c85a5568785a1991d4838fRichard Smith      // Don't perform any constexpr calls (other than the call we're checking)
436745f5147e065900267c85a5568785a1991d4838fRichard Smith      // when checking a potential constant expression.
437745f5147e065900267c85a5568785a1991d4838fRichard Smith      if (CheckingPotentialConstantExpression && CallStackDepth > 1)
438745f5147e065900267c85a5568785a1991d4838fRichard Smith        return false;
439c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      if (CallStackDepth <= getLangOpts().ConstexprCallDepth)
440c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith        return true;
441c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      Diag(Loc, diag::note_constexpr_depth_limit_exceeded)
442c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith        << getLangOpts().ConstexprCallDepth;
443c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      return false;
444c18c42345636e2866fed75c7e434fb659d747672Richard Smith    }
445f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
446c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  private:
447c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    /// Add a diagnostic to the diagnostics list.
448c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    PartialDiagnostic &addDiag(SourceLocation Loc, diag::kind DiagId) {
449c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      PartialDiagnostic PD(DiagId, Ctx.getDiagAllocator());
450c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      EvalStatus.Diag->push_back(std::make_pair(Loc, PD));
451c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      return EvalStatus.Diag->back().second;
452c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    }
453c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
45408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    /// Add notes containing a call stack to the current point of evaluation.
45508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    void addCallStack(unsigned Limit);
45608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
457c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  public:
458f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    /// Diagnose that the evaluation cannot be folded.
4597098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    OptionalDiagnostic Diag(SourceLocation Loc, diag::kind DiagId
4607098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith                              = diag::note_invalid_subexpr_in_const_expr,
461c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                            unsigned ExtraNotes = 0) {
462f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      // If we have a prior diagnostic, it will be noting that the expression
463f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      // isn't a constant expression. This diagnostic is more important.
464f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      // FIXME: We might want to show both diagnostics to the user.
465dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith      if (EvalStatus.Diag) {
46608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        unsigned CallStackNotes = CallStackDepth - 1;
46708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        unsigned Limit = Ctx.getDiagnostics().getConstexprBacktraceLimit();
46808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        if (Limit)
46908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith          CallStackNotes = std::min(CallStackNotes, Limit + 1);
470745f5147e065900267c85a5568785a1991d4838fRichard Smith        if (CheckingPotentialConstantExpression)
471745f5147e065900267c85a5568785a1991d4838fRichard Smith          CallStackNotes = 0;
47208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
473c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith        HasActiveDiagnostic = true;
474dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith        EvalStatus.Diag->clear();
47508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        EvalStatus.Diag->reserve(1 + ExtraNotes + CallStackNotes);
47608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        addDiag(Loc, DiagId);
477745f5147e065900267c85a5568785a1991d4838fRichard Smith        if (!CheckingPotentialConstantExpression)
478745f5147e065900267c85a5568785a1991d4838fRichard Smith          addCallStack(Limit);
47908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        return OptionalDiagnostic(&(*EvalStatus.Diag)[0].second);
480dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith      }
481c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      HasActiveDiagnostic = false;
482dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith      return OptionalDiagnostic();
483dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    }
484dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith
485dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    /// Diagnose that the evaluation does not produce a C++11 core constant
486dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    /// expression.
4877098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    OptionalDiagnostic CCEDiag(SourceLocation Loc, diag::kind DiagId
4887098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith                                 = diag::note_invalid_subexpr_in_const_expr,
489c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                               unsigned ExtraNotes = 0) {
490dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith      // Don't override a previous diagnostic.
491dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith      if (!EvalStatus.Diag || !EvalStatus.Diag->empty())
492dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith        return OptionalDiagnostic();
493c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      return Diag(Loc, DiagId, ExtraNotes);
494c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    }
495c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
496c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    /// Add a note to a prior diagnostic.
497c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    OptionalDiagnostic Note(SourceLocation Loc, diag::kind DiagId) {
498c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      if (!HasActiveDiagnostic)
499c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith        return OptionalDiagnostic();
500c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      return OptionalDiagnostic(&addDiag(Loc, DiagId));
501f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    }
502099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
503099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    /// Add a stack of notes to a prior diagnostic.
504099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    void addNotes(ArrayRef<PartialDiagnosticAt> Diags) {
505099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith      if (HasActiveDiagnostic) {
506099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith        EvalStatus.Diag->insert(EvalStatus.Diag->end(),
507099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith                                Diags.begin(), Diags.end());
508099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith      }
509099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    }
510745f5147e065900267c85a5568785a1991d4838fRichard Smith
511745f5147e065900267c85a5568785a1991d4838fRichard Smith    /// Should we continue evaluation as much as possible after encountering a
512745f5147e065900267c85a5568785a1991d4838fRichard Smith    /// construct which can't be folded?
513745f5147e065900267c85a5568785a1991d4838fRichard Smith    bool keepEvaluatingAfterFailure() {
514745f5147e065900267c85a5568785a1991d4838fRichard Smith      return CheckingPotentialConstantExpression && EvalStatus.Diag->empty();
515745f5147e065900267c85a5568785a1991d4838fRichard Smith    }
516c54061a679d8db4169438b2f97f81e3f443c6a25Benjamin Kramer  };
517f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
518f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  /// Object used to treat all foldable expressions as constant expressions.
519f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  struct FoldConstant {
520f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    bool Enabled;
521f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
522f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    explicit FoldConstant(EvalInfo &Info)
523f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      : Enabled(Info.EvalStatus.Diag && Info.EvalStatus.Diag->empty() &&
524f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith                !Info.EvalStatus.HasSideEffects) {
525f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    }
526f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    // Treat the value we've computed since this object was created as constant.
527f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    void Fold(EvalInfo &Info) {
528f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      if (Enabled && !Info.EvalStatus.Diag->empty() &&
529f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          !Info.EvalStatus.HasSideEffects)
530f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        Info.EvalStatus.Diag->clear();
531f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    }
532f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  };
53308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith}
53408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
535b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithbool SubobjectDesignator::checkSubobject(EvalInfo &Info, const Expr *E,
536b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                                         CheckSubobjectKind CSK) {
537b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (Invalid)
538b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    return false;
539b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (isOnePastTheEnd()) {
540b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_past_end_subobject)
541b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      << CSK;
542b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    setInvalid();
543b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    return false;
544b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  }
545b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  return true;
546b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith}
547b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
548b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithvoid SubobjectDesignator::diagnosePointerArithmetic(EvalInfo &Info,
549b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                                                    const Expr *E, uint64_t N) {
550b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (MostDerivedPathLength == Entries.size() && MostDerivedArraySize)
551b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_array_index)
552b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      << static_cast<int>(N) << /*array*/ 0
553b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      << static_cast<unsigned>(MostDerivedArraySize);
554b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  else
555b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_array_index)
556b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      << static_cast<int>(N) << /*non-array*/ 1;
557b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  setInvalid();
558b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith}
559b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
56008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard SmithCallStackFrame::CallStackFrame(EvalInfo &Info, SourceLocation CallLoc,
56108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith                               const FunctionDecl *Callee, const LValue *This,
56208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith                               const CCValue *Arguments)
56308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    : Info(Info), Caller(Info.CurrentCall), CallLoc(CallLoc), Callee(Callee),
56408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      This(This), Arguments(Arguments) {
56508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  Info.CurrentCall = this;
56608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  ++Info.CallStackDepth;
56708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith}
56808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
56908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard SmithCallStackFrame::~CallStackFrame() {
57008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  assert(Info.CurrentCall == this && "calls retired out of order");
57108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  --Info.CallStackDepth;
57208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  Info.CurrentCall = Caller;
57308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith}
57487eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner
57508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith/// Produce a string describing the given constexpr call.
57608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smithstatic void describeCall(CallStackFrame *Frame, llvm::raw_ostream &Out) {
57708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  unsigned ArgIndex = 0;
57808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  bool IsMemberCall = isa<CXXMethodDecl>(Frame->Callee) &&
5795ba73e1af8ef519161bd40063dc325457e21676aRichard Smith                      !isa<CXXConstructorDecl>(Frame->Callee) &&
5805ba73e1af8ef519161bd40063dc325457e21676aRichard Smith                      cast<CXXMethodDecl>(Frame->Callee)->isInstance();
58108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
58208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  if (!IsMemberCall)
58308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    Out << *Frame->Callee << '(';
58408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
58508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  for (FunctionDecl::param_const_iterator I = Frame->Callee->param_begin(),
58608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith       E = Frame->Callee->param_end(); I != E; ++I, ++ArgIndex) {
5875fe31228c883040cf016cfc71ad4bfeba462602eNAKAMURA Takumi    if (ArgIndex > (unsigned)IsMemberCall)
58808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      Out << ", ";
58908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
59008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    const ParmVarDecl *Param = *I;
59108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    const CCValue &Arg = Frame->Arguments[ArgIndex];
59208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    if (!Arg.isLValue() || Arg.getLValueDesignator().Invalid)
59308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      Arg.printPretty(Out, Frame->Info.Ctx, Param->getType());
59408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    else {
59508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      // Deliberately slice off the frame to form an APValue we can print.
59608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      APValue Value(Arg.getLValueBase(), Arg.getLValueOffset(),
59708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith                    Arg.getLValueDesignator().Entries,
598b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                    Arg.getLValueDesignator().IsOnePastTheEnd);
59908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      Value.printPretty(Out, Frame->Info.Ctx, Param->getType());
60008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    }
60108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
60208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    if (ArgIndex == 0 && IsMemberCall)
60308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      Out << "->" << *Frame->Callee << '(';
604bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith  }
605d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
60608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  Out << ')';
60708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith}
60808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
60908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smithvoid EvalInfo::addCallStack(unsigned Limit) {
61008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  // Determine which calls to skip, if any.
61108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  unsigned ActiveCalls = CallStackDepth - 1;
61208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  unsigned SkipStart = ActiveCalls, SkipEnd = SkipStart;
61308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  if (Limit && Limit < ActiveCalls) {
61408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    SkipStart = Limit / 2 + Limit % 2;
61508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    SkipEnd = ActiveCalls - Limit / 2;
61608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  }
61708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
61808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  // Walk the call stack and add the diagnostics.
61908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  unsigned CallIdx = 0;
62008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  for (CallStackFrame *Frame = CurrentCall; Frame != &BottomFrame;
62108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith       Frame = Frame->Caller, ++CallIdx) {
62208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    // Skip this call?
62308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    if (CallIdx >= SkipStart && CallIdx < SkipEnd) {
62408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      if (CallIdx == SkipStart) {
62508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        // Note that we're skipping calls.
62608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith        addDiag(Frame->CallLoc, diag::note_constexpr_calls_suppressed)
62708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith          << unsigned(ActiveCalls - Limit);
62808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      }
62908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith      continue;
63008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    }
63108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith
63208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    llvm::SmallVector<char, 128> Buffer;
63308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    llvm::raw_svector_ostream Out(Buffer);
63408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    describeCall(Frame, Out);
63508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith    addDiag(Frame->CallLoc, diag::note_constexpr_call_here) << Out.str();
636bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith  }
63708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith}
638d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
63908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smithnamespace {
640f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall  struct ComplexValue {
641f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall  private:
642f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    bool IsInt;
643f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall
644f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall  public:
645f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    APSInt IntReal, IntImag;
646f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    APFloat FloatReal, FloatImag;
647f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall
648f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    ComplexValue() : FloatReal(APFloat::Bogus), FloatImag(APFloat::Bogus) {}
649f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall
650f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    void makeComplexFloat() { IsInt = false; }
651f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    bool isComplexFloat() const { return !IsInt; }
652f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    APFloat &getComplexFloatReal() { return FloatReal; }
653f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    APFloat &getComplexFloatImag() { return FloatImag; }
654f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall
655f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    void makeComplexInt() { IsInt = true; }
656f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    bool isComplexInt() const { return IsInt; }
657f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    APSInt &getComplexIntReal() { return IntReal; }
658f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    APSInt &getComplexIntImag() { return IntImag; }
659f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall
66047a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    void moveInto(CCValue &v) const {
661f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall      if (isComplexFloat())
66247a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith        v = CCValue(FloatReal, FloatImag);
663f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall      else
66447a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith        v = CCValue(IntReal, IntImag);
665f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    }
66647a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    void setFrom(const CCValue &v) {
66756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      assert(v.isComplexFloat() || v.isComplexInt());
66856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      if (v.isComplexFloat()) {
66956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall        makeComplexFloat();
67056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall        FloatReal = v.getComplexFloatReal();
67156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall        FloatImag = v.getComplexFloatImag();
67256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      } else {
67356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall        makeComplexInt();
67456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall        IntReal = v.getComplexIntReal();
67556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall        IntImag = v.getComplexIntImag();
67656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      }
67756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    }
678f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall  };
679efdb83e26f9a1fd2566afe54461216cd84814d42John McCall
680efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  struct LValue {
6811bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    APValue::LValueBase Base;
682efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    CharUnits Offset;
683177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith    CallStackFrame *Frame;
6840a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    SubobjectDesignator Designator;
685efdb83e26f9a1fd2566afe54461216cd84814d42John McCall
6861bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    const APValue::LValueBase getLValueBase() const { return Base; }
68747a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    CharUnits &getLValueOffset() { return Offset; }
688625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith    const CharUnits &getLValueOffset() const { return Offset; }
689177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith    CallStackFrame *getLValueFrame() const { return Frame; }
6900a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    SubobjectDesignator &getLValueDesignator() { return Designator; }
6910a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    const SubobjectDesignator &getLValueDesignator() const { return Designator;}
692efdb83e26f9a1fd2566afe54461216cd84814d42John McCall
69347a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    void moveInto(CCValue &V) const {
6940a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      V = CCValue(Base, Offset, Frame, Designator);
695efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    }
69647a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    void setFrom(const CCValue &V) {
69747a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith      assert(V.isLValue());
69847a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith      Base = V.getLValueBase();
69947a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith      Offset = V.getLValueOffset();
700177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith      Frame = V.getLValueFrame();
7010a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      Designator = V.getLValueDesignator();
7020a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    }
7030a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith
7041bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    void set(APValue::LValueBase B, CallStackFrame *F = 0) {
7051bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith      Base = B;
7060a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      Offset = CharUnits::Zero();
7070a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      Frame = F;
708b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      Designator = SubobjectDesignator(getType(B));
709b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    }
710b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
711b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    // Check that this LValue is not based on a null pointer. If it is, produce
712b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    // a diagnostic and mark the designator as invalid.
713b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    bool checkNullPointer(EvalInfo &Info, const Expr *E,
714b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                          CheckSubobjectKind CSK) {
715b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      if (Designator.Invalid)
716b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        return false;
717b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      if (!Base) {
718b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_null_subobject)
719b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith          << CSK;
720b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        Designator.setInvalid();
721b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        return false;
722b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      }
723b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      return true;
724b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    }
725b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
726b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    // Check this LValue refers to an object. If not, set the designator to be
727b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    // invalid and emit a diagnostic.
728b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK) {
729b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      return checkNullPointer(Info, E, CSK) &&
730b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith             Designator.checkSubobject(Info, E, CSK);
731b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    }
732b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
733b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    void addDecl(EvalInfo &Info, const Expr *E,
734b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                 const Decl *D, bool Virtual = false) {
735b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      checkSubobject(Info, E, isa<FieldDecl>(D) ? CSK_Field : CSK_Base);
736b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      Designator.addDeclUnchecked(D, Virtual);
737b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    }
738b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    void addArray(EvalInfo &Info, const Expr *E, const ConstantArrayType *CAT) {
739b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      checkSubobject(Info, E, CSK_ArrayToPointer);
740b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      Designator.addArrayUnchecked(CAT);
741b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    }
742b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    void adjustIndex(EvalInfo &Info, const Expr *E, uint64_t N) {
743b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      if (!checkNullPointer(Info, E, CSK_ArrayIndex))
744b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        return;
745b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      Designator.adjustIndex(Info, E, N);
74656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    }
747efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  };
748e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
749e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  struct MemberPtr {
750e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    MemberPtr() {}
751e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    explicit MemberPtr(const ValueDecl *Decl) :
752e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      DeclAndIsDerivedMember(Decl, false), Path() {}
753e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
754e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// The member or (direct or indirect) field referred to by this member
755e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// pointer, or 0 if this is a null member pointer.
756e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    const ValueDecl *getDecl() const {
757e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return DeclAndIsDerivedMember.getPointer();
758e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
759e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// Is this actually a member of some type derived from the relevant class?
760e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    bool isDerivedMember() const {
761e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return DeclAndIsDerivedMember.getInt();
762e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
763e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// Get the class which the declaration actually lives in.
764e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    const CXXRecordDecl *getContainingRecord() const {
765e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return cast<CXXRecordDecl>(
766e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith          DeclAndIsDerivedMember.getPointer()->getDeclContext());
767e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
768e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
769e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    void moveInto(CCValue &V) const {
770e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      V = CCValue(getDecl(), isDerivedMember(), Path);
771e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
772e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    void setFrom(const CCValue &V) {
773e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      assert(V.isMemberPointer());
774e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      DeclAndIsDerivedMember.setPointer(V.getMemberPointerDecl());
775e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      DeclAndIsDerivedMember.setInt(V.isMemberPointerToDerivedMember());
776e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      Path.clear();
777e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      ArrayRef<const CXXRecordDecl*> P = V.getMemberPointerPath();
778e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      Path.insert(Path.end(), P.begin(), P.end());
779e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
780e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
781e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// DeclAndIsDerivedMember - The member declaration, and a flag indicating
782e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// whether the member is a member of some class derived from the class type
783e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// of the member pointer.
784e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    llvm::PointerIntPair<const ValueDecl*, 1, bool> DeclAndIsDerivedMember;
785e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// Path - The path of base/derived classes from the member declaration's
786e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// class (exclusive) to the class type of the member pointer (inclusive).
787e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    SmallVector<const CXXRecordDecl*, 4> Path;
788e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
789e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// Perform a cast towards the class of the Decl (either up or down the
790e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// hierarchy).
791e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    bool castBack(const CXXRecordDecl *Class) {
792e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      assert(!Path.empty());
793e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      const CXXRecordDecl *Expected;
794e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (Path.size() >= 2)
795e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        Expected = Path[Path.size() - 2];
796e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      else
797e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        Expected = getContainingRecord();
798e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (Expected->getCanonicalDecl() != Class->getCanonicalDecl()) {
799e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        // C++11 [expr.static.cast]p12: In a conversion from (D::*) to (B::*),
800e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        // if B does not contain the original member and is not a base or
801e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        // derived class of the class containing the original member, the result
802e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        // of the cast is undefined.
803e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        // C++11 [conv.mem]p2 does not cover this case for a cast from (B::*) to
804e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        // (D::*). We consider that to be a language defect.
805e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
806e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      }
807e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      Path.pop_back();
808e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return true;
809e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
810e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// Perform a base-to-derived member pointer cast.
811e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    bool castToDerived(const CXXRecordDecl *Derived) {
812e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!getDecl())
813e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return true;
814e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!isDerivedMember()) {
815e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        Path.push_back(Derived);
816e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return true;
817e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      }
818e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!castBack(Derived))
819e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
820e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (Path.empty())
821e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        DeclAndIsDerivedMember.setInt(false);
822e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return true;
823e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
824e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    /// Perform a derived-to-base member pointer cast.
825e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    bool castToBase(const CXXRecordDecl *Base) {
826e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!getDecl())
827e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return true;
828e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (Path.empty())
829e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        DeclAndIsDerivedMember.setInt(true);
830e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (isDerivedMember()) {
831e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        Path.push_back(Base);
832e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return true;
833e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      }
834e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return castBack(Base);
835e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
836e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  };
837c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
838b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith  /// Compare two member pointers, which are assumed to be of the same type.
839b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith  static bool operator==(const MemberPtr &LHS, const MemberPtr &RHS) {
840b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    if (!LHS.getDecl() || !RHS.getDecl())
841b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith      return !LHS.getDecl() && !RHS.getDecl();
842b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    if (LHS.getDecl()->getCanonicalDecl() != RHS.getDecl()->getCanonicalDecl())
843b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith      return false;
844b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    return LHS.Path == RHS.Path;
845b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith  }
846b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith
847c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  /// Kinds of constant expression checking, for diagnostics.
848c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  enum CheckConstantExpressionKind {
849c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    CCEK_Constant,    ///< A normal constant.
850c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    CCEK_ReturnValue, ///< A constexpr function return value.
851c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    CCEK_MemberInit   ///< A constexpr constructor mem-initializer.
852c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  };
853f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall}
85487eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner
85547a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smithstatic bool Evaluate(CCValue &Result, EvalInfo &Info, const Expr *E);
85669c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smithstatic bool EvaluateConstantExpression(APValue &Result, EvalInfo &Info,
857c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                       const LValue &This, const Expr *E,
858c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                       CheckConstantExpressionKind CCEK
859c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                        = CCEK_Constant);
860efdb83e26f9a1fd2566afe54461216cd84814d42John McCallstatic bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info);
861efdb83e26f9a1fd2566afe54461216cd84814d42John McCallstatic bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info);
862e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result,
863e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                                  EvalInfo &Info);
864e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info);
86587eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattnerstatic bool EvaluateInteger(const Expr *E, APSInt  &Result, EvalInfo &Info);
86647a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smithstatic bool EvaluateIntegerOrLValue(const Expr *E, CCValue &Result,
867d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner                                    EvalInfo &Info);
868d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmanstatic bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
869f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCallstatic bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info);
870f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
871f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===//
8724efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman// Misc utilities
8734efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman//===----------------------------------------------------------------------===//
8744efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
875180f47959a066795cc0f409433023af448bb0328Richard Smith/// Should this call expression be treated as a string literal?
876180f47959a066795cc0f409433023af448bb0328Richard Smithstatic bool IsStringLiteralCall(const CallExpr *E) {
877180f47959a066795cc0f409433023af448bb0328Richard Smith  unsigned Builtin = E->isBuiltinCall();
878180f47959a066795cc0f409433023af448bb0328Richard Smith  return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString ||
879180f47959a066795cc0f409433023af448bb0328Richard Smith          Builtin == Builtin::BI__builtin___NSStringMakeConstantString);
880180f47959a066795cc0f409433023af448bb0328Richard Smith}
881180f47959a066795cc0f409433023af448bb0328Richard Smith
8821bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smithstatic bool IsGlobalLValue(APValue::LValueBase B) {
883180f47959a066795cc0f409433023af448bb0328Richard Smith  // C++11 [expr.const]p3 An address constant expression is a prvalue core
884180f47959a066795cc0f409433023af448bb0328Richard Smith  // constant expression of pointer type that evaluates to...
885180f47959a066795cc0f409433023af448bb0328Richard Smith
886180f47959a066795cc0f409433023af448bb0328Richard Smith  // ... a null pointer value, or a prvalue core constant expression of type
887180f47959a066795cc0f409433023af448bb0328Richard Smith  // std::nullptr_t.
8881bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  if (!B) return true;
88942c8f87eb60958170c46767273bf93e6c96125bfJohn McCall
8901bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) {
891180f47959a066795cc0f409433023af448bb0328Richard Smith    // ... the address of an object with static storage duration,
8921bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    if (const VarDecl *VD = dyn_cast<VarDecl>(D))
89342c8f87eb60958170c46767273bf93e6c96125bfJohn McCall      return VD->hasGlobalStorage();
8941bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    // ... the address of a function,
8951bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    return isa<FunctionDecl>(D);
89642c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  }
8971bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith
8981bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  const Expr *E = B.get<const Expr*>();
8991bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  switch (E->getStmtClass()) {
9001bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  default:
9011bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    return false;
902180f47959a066795cc0f409433023af448bb0328Richard Smith  case Expr::CompoundLiteralExprClass:
903180f47959a066795cc0f409433023af448bb0328Richard Smith    return cast<CompoundLiteralExpr>(E)->isFileScope();
904180f47959a066795cc0f409433023af448bb0328Richard Smith  // A string literal has static storage duration.
905180f47959a066795cc0f409433023af448bb0328Richard Smith  case Expr::StringLiteralClass:
906180f47959a066795cc0f409433023af448bb0328Richard Smith  case Expr::PredefinedExprClass:
907180f47959a066795cc0f409433023af448bb0328Richard Smith  case Expr::ObjCStringLiteralClass:
908180f47959a066795cc0f409433023af448bb0328Richard Smith  case Expr::ObjCEncodeExprClass:
90947d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith  case Expr::CXXTypeidExprClass:
910180f47959a066795cc0f409433023af448bb0328Richard Smith    return true;
911180f47959a066795cc0f409433023af448bb0328Richard Smith  case Expr::CallExprClass:
912180f47959a066795cc0f409433023af448bb0328Richard Smith    return IsStringLiteralCall(cast<CallExpr>(E));
913180f47959a066795cc0f409433023af448bb0328Richard Smith  // For GCC compatibility, &&label has static storage duration.
914180f47959a066795cc0f409433023af448bb0328Richard Smith  case Expr::AddrLabelExprClass:
915180f47959a066795cc0f409433023af448bb0328Richard Smith    return true;
916180f47959a066795cc0f409433023af448bb0328Richard Smith  // A Block literal expression may be used as the initialization value for
917180f47959a066795cc0f409433023af448bb0328Richard Smith  // Block variables at global or local static scope.
918180f47959a066795cc0f409433023af448bb0328Richard Smith  case Expr::BlockExprClass:
919180f47959a066795cc0f409433023af448bb0328Richard Smith    return !cast<BlockExpr>(E)->getBlockDecl()->hasCaptures();
920745f5147e065900267c85a5568785a1991d4838fRichard Smith  case Expr::ImplicitValueInitExprClass:
921745f5147e065900267c85a5568785a1991d4838fRichard Smith    // FIXME:
922745f5147e065900267c85a5568785a1991d4838fRichard Smith    // We can never form an lvalue with an implicit value initialization as its
923745f5147e065900267c85a5568785a1991d4838fRichard Smith    // base through expression evaluation, so these only appear in one case: the
924745f5147e065900267c85a5568785a1991d4838fRichard Smith    // implicit variable declaration we invent when checking whether a constexpr
925745f5147e065900267c85a5568785a1991d4838fRichard Smith    // constructor can produce a constant expression. We must assume that such
926745f5147e065900267c85a5568785a1991d4838fRichard Smith    // an expression might be a global lvalue.
927745f5147e065900267c85a5568785a1991d4838fRichard Smith    return true;
928180f47959a066795cc0f409433023af448bb0328Richard Smith  }
92942c8f87eb60958170c46767273bf93e6c96125bfJohn McCall}
93042c8f87eb60958170c46767273bf93e6c96125bfJohn McCall
9319a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith/// Check that this reference or pointer core constant expression is a valid
932b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith/// value for an address or reference constant expression. Type T should be
93361e616206413d1779c7545c7a8ad1ce1129ad9c1Richard Smith/// either LValue or CCValue. Return true if we can fold this expression,
93461e616206413d1779c7545c7a8ad1ce1129ad9c1Richard Smith/// whether or not it's a constant expression.
9359a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smithtemplate<typename T>
936f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool CheckLValueConstantExpression(EvalInfo &Info, const Expr *E,
937c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                          const T &LVal, APValue &Value,
938c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                          CheckConstantExpressionKind CCEK) {
939c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  APValue::LValueBase Base = LVal.getLValueBase();
940c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  const SubobjectDesignator &Designator = LVal.getLValueDesignator();
941c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
942c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  if (!IsGlobalLValue(Base)) {
943c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    if (Info.getLangOpts().CPlusPlus0x) {
944c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>();
945c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      Info.Diag(E->getExprLoc(), diag::note_constexpr_non_global, 1)
946c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith        << E->isGLValue() << !Designator.Entries.empty()
947c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith        << !!VD << CCEK << VD;
948c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      if (VD)
949c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith        Info.Note(VD->getLocation(), diag::note_declared_at);
950c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      else
951c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith        Info.Note(Base.dyn_cast<const Expr*>()->getExprLoc(),
952c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                  diag::note_constexpr_temporary_here);
953c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    } else {
9547098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      Info.Diag(E->getExprLoc());
955c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    }
95661e616206413d1779c7545c7a8ad1ce1129ad9c1Richard Smith    // Don't allow references to temporaries to escape.
95769c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith    return false;
958f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
95969c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith
960b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  bool IsReferenceType = E->isGLValue();
961b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
962b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (Designator.Invalid) {
96361e616206413d1779c7545c7a8ad1ce1129ad9c1Richard Smith    // This is not a core constant expression. An appropriate diagnostic will
96461e616206413d1779c7545c7a8ad1ce1129ad9c1Richard Smith    // have already been produced.
9659a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith    Value = APValue(LVal.getLValueBase(), LVal.getLValueOffset(),
9669a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith                    APValue::NoLValuePath());
9679a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith    return true;
9689a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith  }
9699a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith
970b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  Value = APValue(LVal.getLValueBase(), LVal.getLValueOffset(),
971b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                  Designator.Entries, Designator.IsOnePastTheEnd);
972b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
973b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // Allow address constant expressions to be past-the-end pointers. This is
974b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // an extension: the standard requires them to point to an object.
975b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (!IsReferenceType)
976b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    return true;
977b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
978b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // A reference constant expression must refer to an object.
979b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (!Base) {
980b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    // FIXME: diagnostic
981b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    Info.CCEDiag(E->getExprLoc());
98261e616206413d1779c7545c7a8ad1ce1129ad9c1Richard Smith    return true;
983b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  }
984b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
985c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  // Does this refer one past the end of some object?
986b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (Designator.isOnePastTheEnd()) {
987c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>();
988c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    Info.Diag(E->getExprLoc(), diag::note_constexpr_past_end, 1)
989c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      << !Designator.Entries.empty() << !!VD << VD;
990c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    if (VD)
991c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      Info.Note(VD->getLocation(), diag::note_declared_at);
992c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    else
993c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      Info.Note(Base.dyn_cast<const Expr*>()->getExprLoc(),
994c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                diag::note_constexpr_temporary_here);
995c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  }
996c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
99769c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith  return true;
99847a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith}
99947a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith
100051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// Check that this core constant expression is of literal type, and if not,
100151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// produce an appropriate diagnostic.
100251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smithstatic bool CheckLiteralType(EvalInfo &Info, const Expr *E) {
100351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (!E->isRValue() || E->getType()->isLiteralType())
100451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return true;
100551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
100651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  // Prvalue constant expressions must be of literal types.
100751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (Info.getLangOpts().CPlusPlus0x)
100851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    Info.Diag(E->getExprLoc(), diag::note_constexpr_nonliteral)
100951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      << E->getType();
101051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  else
101151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr);
101251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  return false;
101351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith}
101451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
10159a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith/// Check that this core constant expression value is a valid value for a
10169a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith/// constant expression, and if it is, produce the corresponding constant value.
101751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// If not, report an appropriate diagnostic. Does not check that the expression
101851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// is of literal type.
1019f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool CheckConstantExpression(EvalInfo &Info, const Expr *E,
1020c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                    const CCValue &CCValue, APValue &Value,
1021c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                    CheckConstantExpressionKind CCEK
1022c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                      = CCEK_Constant) {
10239a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith  if (!CCValue.isLValue()) {
10249a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith    Value = CCValue;
10259a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith    return true;
10269a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith  }
1027c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  return CheckLValueConstantExpression(Info, E, CCValue, Value, CCEK);
10289a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith}
10299a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith
10309e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smithconst ValueDecl *GetLValueBaseDecl(const LValue &LVal) {
10311bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  return LVal.Base.dyn_cast<const ValueDecl*>();
10329e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith}
10339e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith
10349e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smithstatic bool IsLiteralLValue(const LValue &Value) {
10351bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  return Value.Base.dyn_cast<const Expr*>() && !Value.Frame;
10369e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith}
10379e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith
103865ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smithstatic bool IsWeakLValue(const LValue &Value) {
103965ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith  const ValueDecl *Decl = GetLValueBaseDecl(Value);
10400dd7a25e8d679de1dc0ce788222d6dee0e879885Lang Hames  return Decl && Decl->isWeak();
104165ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith}
104265ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith
1043e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic bool EvalPointerValueAsBool(const CCValue &Value, bool &Result) {
10443554283157190e67918fad4221a5e6faf9317362John McCall  // A null base expression indicates a null pointer.  These are always
10453554283157190e67918fad4221a5e6faf9317362John McCall  // evaluatable, and they are false unless the offset is zero.
1046e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (!Value.getLValueBase()) {
1047e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    Result = !Value.getLValueOffset().isZero();
10483554283157190e67918fad4221a5e6faf9317362John McCall    return true;
10493554283157190e67918fad4221a5e6faf9317362John McCall  }
10503554283157190e67918fad4221a5e6faf9317362John McCall
1051e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // We have a non-null base.  These are generally known to be true, but if it's
1052e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // a weak declaration it can be null at runtime.
10533554283157190e67918fad4221a5e6faf9317362John McCall  Result = true;
1054e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  const ValueDecl *Decl = Value.getLValueBase().dyn_cast<const ValueDecl*>();
10550dd7a25e8d679de1dc0ce788222d6dee0e879885Lang Hames  return !Decl || !Decl->isWeak();
10565bc86103767c2abcbfdd6518e0ccbbbb6aa59e0fEli Friedman}
10575bc86103767c2abcbfdd6518e0ccbbbb6aa59e0fEli Friedman
105847a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smithstatic bool HandleConversionToBool(const CCValue &Val, bool &Result) {
1059c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  switch (Val.getKind()) {
1060c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  case APValue::Uninitialized:
1061c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return false;
1062c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  case APValue::Int:
1063c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    Result = Val.getInt().getBoolValue();
106441bf4f38348561a0f12c10d34f1673cd19a6eb04Richard Smith    return true;
1065c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  case APValue::Float:
1066c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    Result = !Val.getFloat().isZero();
1067a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman    return true;
1068c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  case APValue::ComplexInt:
1069c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    Result = Val.getComplexIntReal().getBoolValue() ||
1070c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith             Val.getComplexIntImag().getBoolValue();
1071c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return true;
1072c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  case APValue::ComplexFloat:
1073c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    Result = !Val.getComplexFloatReal().isZero() ||
1074c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith             !Val.getComplexFloatImag().isZero();
1075436c8898cd1c93c5bacd3fcc4ac586bc5cd77062Richard Smith    return true;
1076e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  case APValue::LValue:
1077e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return EvalPointerValueAsBool(Val, Result);
1078e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  case APValue::MemberPointer:
1079e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    Result = Val.getMemberPointerDecl();
1080e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
1081c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  case APValue::Vector:
1082cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  case APValue::Array:
1083180f47959a066795cc0f409433023af448bb0328Richard Smith  case APValue::Struct:
1084180f47959a066795cc0f409433023af448bb0328Richard Smith  case APValue::Union:
108565639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman  case APValue::AddrLabelDiff:
1086c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return false;
10874efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman  }
10884efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
1089c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  llvm_unreachable("unknown APValue kind");
1090c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith}
1091c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
1092c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smithstatic bool EvaluateAsBooleanCondition(const Expr *E, bool &Result,
1093c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith                                       EvalInfo &Info) {
1094c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  assert(E->isRValue() && "missing lvalue-to-rvalue conv in bool condition");
109547a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith  CCValue Val;
1096c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  if (!Evaluate(Val, Info, E))
1097c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return false;
1098c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  return HandleConversionToBool(Val, Result);
10994efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman}
11004efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
1101c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithtemplate<typename T>
1102c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstatic bool HandleOverflow(EvalInfo &Info, const Expr *E,
1103c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                           const T &SrcValue, QualType DestType) {
1104c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  Info.Diag(E->getExprLoc(), diag::note_constexpr_overflow)
1105789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    << SrcValue << DestType;
1106c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  return false;
1107c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
1108c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
1109c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstatic bool HandleFloatToIntCast(EvalInfo &Info, const Expr *E,
1110c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                 QualType SrcType, const APFloat &Value,
1111c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                 QualType DestType, APSInt &Result) {
1112c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  unsigned DestWidth = Info.Ctx.getIntWidth(DestType);
1113a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar  // Determine whether we are converting to unsigned or signed.
1114575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor  bool DestSigned = DestType->isSignedIntegerOrEnumerationType();
11151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1116c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  Result = APSInt(DestWidth, !DestSigned);
1117a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar  bool ignored;
1118c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  if (Value.convertToInteger(Result, llvm::APFloat::rmTowardZero, &ignored)
1119c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      & APFloat::opInvalidOp)
1120c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return HandleOverflow(Info, E, Value, DestType);
1121c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  return true;
1122a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar}
1123a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar
1124c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstatic bool HandleFloatToFloatCast(EvalInfo &Info, const Expr *E,
1125c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                   QualType SrcType, QualType DestType,
1126c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                   APFloat &Result) {
1127c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  APFloat Value = Result;
1128a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar  bool ignored;
1129c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  if (Result.convert(Info.Ctx.getFloatTypeSemantics(DestType),
1130c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                     APFloat::rmNearestTiesToEven, &ignored)
1131c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      & APFloat::opOverflow)
1132c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return HandleOverflow(Info, E, Value, DestType);
1133c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  return true;
1134a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar}
1135a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar
1136f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smithstatic APSInt HandleIntToIntCast(EvalInfo &Info, const Expr *E,
1137f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith                                 QualType DestType, QualType SrcType,
1138f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith                                 APSInt &Value) {
1139f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith  unsigned DestWidth = Info.Ctx.getIntWidth(DestType);
1140a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar  APSInt Result = Value;
1141a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar  // Figure out if this is a truncate, extend or noop cast.
1142a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar  // If the input is signed, do a sign extend, noop, or truncate.
11439f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad  Result = Result.extOrTrunc(DestWidth);
1144575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor  Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType());
1145a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar  return Result;
1146a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar}
1147a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar
1148c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstatic bool HandleIntToFloatCast(EvalInfo &Info, const Expr *E,
1149c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                 QualType SrcType, const APSInt &Value,
1150c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                 QualType DestType, APFloat &Result) {
1151c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  Result = APFloat(Info.Ctx.getFloatTypeSemantics(DestType), 1);
1152c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  if (Result.convertFromAPInt(Value, Value.isSigned(),
1153c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                              APFloat::rmNearestTiesToEven)
1154c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      & APFloat::opOverflow)
1155c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return HandleOverflow(Info, E, Value, DestType);
1156c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  return true;
1157a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar}
1158a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar
1159e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedmanstatic bool EvalAndBitcastToAPInt(EvalInfo &Info, const Expr *E,
1160e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman                                  llvm::APInt &Res) {
1161e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  CCValue SVal;
1162e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  if (!Evaluate(SVal, Info, E))
1163e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    return false;
1164e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  if (SVal.isInt()) {
1165e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    Res = SVal.getInt();
1166e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    return true;
1167e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  }
1168e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  if (SVal.isFloat()) {
1169e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    Res = SVal.getFloat().bitcastToAPInt();
1170e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    return true;
1171e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  }
1172e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  if (SVal.isVector()) {
1173e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    QualType VecTy = E->getType();
1174e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    unsigned VecSize = Info.Ctx.getTypeSize(VecTy);
1175e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    QualType EltTy = VecTy->castAs<VectorType>()->getElementType();
1176e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    unsigned EltSize = Info.Ctx.getTypeSize(EltTy);
1177e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian();
1178e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    Res = llvm::APInt::getNullValue(VecSize);
1179e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    for (unsigned i = 0; i < SVal.getVectorLength(); i++) {
1180e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      APValue &Elt = SVal.getVectorElt(i);
1181e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      llvm::APInt EltAsInt;
1182e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      if (Elt.isInt()) {
1183e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        EltAsInt = Elt.getInt();
1184e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      } else if (Elt.isFloat()) {
1185e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        EltAsInt = Elt.getFloat().bitcastToAPInt();
1186e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      } else {
1187e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        // Don't try to handle vectors of anything other than int or float
1188e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        // (not sure if it's possible to hit this case).
1189e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr);
1190e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        return false;
1191e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      }
1192e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      unsigned BaseEltSize = EltAsInt.getBitWidth();
1193e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      if (BigEndian)
1194e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        Res |= EltAsInt.zextOrTrunc(VecSize).rotr(i*EltSize+BaseEltSize);
1195e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      else
1196e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        Res |= EltAsInt.zextOrTrunc(VecSize).rotl(i*EltSize);
1197e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    }
1198e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    return true;
1199e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  }
1200e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  // Give up if the input isn't an int, float, or vector.  For example, we
1201e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  // reject "(v4i16)(intptr_t)&a".
1202e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr);
1203e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  return false;
1204e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman}
1205e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman
1206b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith/// Cast an lvalue referring to a base subobject to a derived class, by
1207b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith/// truncating the lvalue's path to the given length.
1208b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic bool CastToDerivedClass(EvalInfo &Info, const Expr *E, LValue &Result,
1209b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                               const RecordDecl *TruncatedType,
1210b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                               unsigned TruncatedElements) {
1211b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  SubobjectDesignator &D = Result.Designator;
1212180f47959a066795cc0f409433023af448bb0328Richard Smith
1213b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // Check we actually point to a derived class object.
1214b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (TruncatedElements == D.Entries.size())
1215b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    return true;
1216b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  assert(TruncatedElements >= D.MostDerivedPathLength &&
1217b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith         "not casting to a derived class");
1218b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (!Result.checkSubobject(Info, E, CSK_Derived))
1219180f47959a066795cc0f409433023af448bb0328Richard Smith    return false;
1220180f47959a066795cc0f409433023af448bb0328Richard Smith
1221b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // Truncate the path to the subobject, and remove any derived-to-base offsets.
1222e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  const RecordDecl *RD = TruncatedType;
1223e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  for (unsigned I = TruncatedElements, N = D.Entries.size(); I != N; ++I) {
1224180f47959a066795cc0f409433023af448bb0328Richard Smith    const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD);
1225180f47959a066795cc0f409433023af448bb0328Richard Smith    const CXXRecordDecl *Base = getAsBaseClass(D.Entries[I]);
1226e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (isVirtualBaseClass(D.Entries[I]))
1227180f47959a066795cc0f409433023af448bb0328Richard Smith      Result.Offset -= Layout.getVBaseClassOffset(Base);
1228e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    else
1229180f47959a066795cc0f409433023af448bb0328Richard Smith      Result.Offset -= Layout.getBaseClassOffset(Base);
1230180f47959a066795cc0f409433023af448bb0328Richard Smith    RD = Base;
1231180f47959a066795cc0f409433023af448bb0328Richard Smith  }
1232e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  D.Entries.resize(TruncatedElements);
1233180f47959a066795cc0f409433023af448bb0328Richard Smith  return true;
1234180f47959a066795cc0f409433023af448bb0328Richard Smith}
1235180f47959a066795cc0f409433023af448bb0328Richard Smith
1236b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic void HandleLValueDirectBase(EvalInfo &Info, const Expr *E, LValue &Obj,
1237180f47959a066795cc0f409433023af448bb0328Richard Smith                                   const CXXRecordDecl *Derived,
1238180f47959a066795cc0f409433023af448bb0328Richard Smith                                   const CXXRecordDecl *Base,
1239180f47959a066795cc0f409433023af448bb0328Richard Smith                                   const ASTRecordLayout *RL = 0) {
1240180f47959a066795cc0f409433023af448bb0328Richard Smith  if (!RL) RL = &Info.Ctx.getASTRecordLayout(Derived);
1241180f47959a066795cc0f409433023af448bb0328Richard Smith  Obj.getLValueOffset() += RL->getBaseClassOffset(Base);
1242b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  Obj.addDecl(Info, E, Base, /*Virtual*/ false);
1243180f47959a066795cc0f409433023af448bb0328Richard Smith}
1244180f47959a066795cc0f409433023af448bb0328Richard Smith
1245b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic bool HandleLValueBase(EvalInfo &Info, const Expr *E, LValue &Obj,
1246180f47959a066795cc0f409433023af448bb0328Richard Smith                             const CXXRecordDecl *DerivedDecl,
1247180f47959a066795cc0f409433023af448bb0328Richard Smith                             const CXXBaseSpecifier *Base) {
1248180f47959a066795cc0f409433023af448bb0328Richard Smith  const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl();
1249180f47959a066795cc0f409433023af448bb0328Richard Smith
1250180f47959a066795cc0f409433023af448bb0328Richard Smith  if (!Base->isVirtual()) {
1251b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    HandleLValueDirectBase(Info, E, Obj, DerivedDecl, BaseDecl);
1252180f47959a066795cc0f409433023af448bb0328Richard Smith    return true;
1253180f47959a066795cc0f409433023af448bb0328Richard Smith  }
1254180f47959a066795cc0f409433023af448bb0328Richard Smith
1255b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  SubobjectDesignator &D = Obj.Designator;
1256b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (D.Invalid)
1257b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    return false;
1258b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
1259180f47959a066795cc0f409433023af448bb0328Richard Smith  // Extract most-derived object and corresponding type.
1260b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  DerivedDecl = D.MostDerivedType->getAsCXXRecordDecl();
1261b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (!CastToDerivedClass(Info, E, Obj, DerivedDecl, D.MostDerivedPathLength))
1262180f47959a066795cc0f409433023af448bb0328Richard Smith    return false;
1263180f47959a066795cc0f409433023af448bb0328Richard Smith
1264b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // Find the virtual base class.
1265180f47959a066795cc0f409433023af448bb0328Richard Smith  const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl);
1266180f47959a066795cc0f409433023af448bb0328Richard Smith  Obj.getLValueOffset() += Layout.getVBaseClassOffset(BaseDecl);
1267b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  Obj.addDecl(Info, E, BaseDecl, /*Virtual*/ true);
1268180f47959a066795cc0f409433023af448bb0328Richard Smith  return true;
1269180f47959a066795cc0f409433023af448bb0328Richard Smith}
1270180f47959a066795cc0f409433023af448bb0328Richard Smith
1271180f47959a066795cc0f409433023af448bb0328Richard Smith/// Update LVal to refer to the given field, which must be a member of the type
1272180f47959a066795cc0f409433023af448bb0328Richard Smith/// currently described by LVal.
1273b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic void HandleLValueMember(EvalInfo &Info, const Expr *E, LValue &LVal,
1274180f47959a066795cc0f409433023af448bb0328Richard Smith                               const FieldDecl *FD,
1275180f47959a066795cc0f409433023af448bb0328Richard Smith                               const ASTRecordLayout *RL = 0) {
1276180f47959a066795cc0f409433023af448bb0328Richard Smith  if (!RL)
1277180f47959a066795cc0f409433023af448bb0328Richard Smith    RL = &Info.Ctx.getASTRecordLayout(FD->getParent());
1278180f47959a066795cc0f409433023af448bb0328Richard Smith
1279180f47959a066795cc0f409433023af448bb0328Richard Smith  unsigned I = FD->getFieldIndex();
1280180f47959a066795cc0f409433023af448bb0328Richard Smith  LVal.Offset += Info.Ctx.toCharUnitsFromBits(RL->getFieldOffset(I));
1281b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  LVal.addDecl(Info, E, FD);
1282180f47959a066795cc0f409433023af448bb0328Richard Smith}
1283180f47959a066795cc0f409433023af448bb0328Richard Smith
1284d9b02e726262e4009dda830998bb934172ac0020Richard Smith/// Update LVal to refer to the given indirect field.
1285d9b02e726262e4009dda830998bb934172ac0020Richard Smithstatic void HandleLValueIndirectMember(EvalInfo &Info, const Expr *E,
1286d9b02e726262e4009dda830998bb934172ac0020Richard Smith                                       LValue &LVal,
1287d9b02e726262e4009dda830998bb934172ac0020Richard Smith                                       const IndirectFieldDecl *IFD) {
1288d9b02e726262e4009dda830998bb934172ac0020Richard Smith  for (IndirectFieldDecl::chain_iterator C = IFD->chain_begin(),
1289d9b02e726262e4009dda830998bb934172ac0020Richard Smith                                         CE = IFD->chain_end(); C != CE; ++C)
1290d9b02e726262e4009dda830998bb934172ac0020Richard Smith    HandleLValueMember(Info, E, LVal, cast<FieldDecl>(*C));
1291d9b02e726262e4009dda830998bb934172ac0020Richard Smith}
1292d9b02e726262e4009dda830998bb934172ac0020Richard Smith
1293180f47959a066795cc0f409433023af448bb0328Richard Smith/// Get the size of the given type in char units.
1294180f47959a066795cc0f409433023af448bb0328Richard Smithstatic bool HandleSizeof(EvalInfo &Info, QualType Type, CharUnits &Size) {
1295180f47959a066795cc0f409433023af448bb0328Richard Smith  // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1296180f47959a066795cc0f409433023af448bb0328Richard Smith  // extension.
1297180f47959a066795cc0f409433023af448bb0328Richard Smith  if (Type->isVoidType() || Type->isFunctionType()) {
1298180f47959a066795cc0f409433023af448bb0328Richard Smith    Size = CharUnits::One();
1299180f47959a066795cc0f409433023af448bb0328Richard Smith    return true;
1300180f47959a066795cc0f409433023af448bb0328Richard Smith  }
1301180f47959a066795cc0f409433023af448bb0328Richard Smith
1302180f47959a066795cc0f409433023af448bb0328Richard Smith  if (!Type->isConstantSizeType()) {
1303180f47959a066795cc0f409433023af448bb0328Richard Smith    // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
1304b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    // FIXME: Diagnostic.
1305180f47959a066795cc0f409433023af448bb0328Richard Smith    return false;
1306180f47959a066795cc0f409433023af448bb0328Richard Smith  }
1307180f47959a066795cc0f409433023af448bb0328Richard Smith
1308180f47959a066795cc0f409433023af448bb0328Richard Smith  Size = Info.Ctx.getTypeSizeInChars(Type);
1309180f47959a066795cc0f409433023af448bb0328Richard Smith  return true;
1310180f47959a066795cc0f409433023af448bb0328Richard Smith}
1311180f47959a066795cc0f409433023af448bb0328Richard Smith
1312180f47959a066795cc0f409433023af448bb0328Richard Smith/// Update a pointer value to model pointer arithmetic.
1313180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param Info - Information about the ongoing evaluation.
1314b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith/// \param E - The expression being evaluated, for diagnostic purposes.
1315180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param LVal - The pointer value to be updated.
1316180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param EltTy - The pointee type represented by LVal.
1317180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param Adjustment - The adjustment, in objects of type EltTy, to add.
1318b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E,
1319b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                                        LValue &LVal, QualType EltTy,
1320b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                                        int64_t Adjustment) {
1321180f47959a066795cc0f409433023af448bb0328Richard Smith  CharUnits SizeOfPointee;
1322180f47959a066795cc0f409433023af448bb0328Richard Smith  if (!HandleSizeof(Info, EltTy, SizeOfPointee))
1323180f47959a066795cc0f409433023af448bb0328Richard Smith    return false;
1324180f47959a066795cc0f409433023af448bb0328Richard Smith
1325180f47959a066795cc0f409433023af448bb0328Richard Smith  // Compute the new offset in the appropriate width.
1326180f47959a066795cc0f409433023af448bb0328Richard Smith  LVal.Offset += Adjustment * SizeOfPointee;
1327b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  LVal.adjustIndex(Info, E, Adjustment);
1328180f47959a066795cc0f409433023af448bb0328Richard Smith  return true;
1329180f47959a066795cc0f409433023af448bb0328Richard Smith}
1330180f47959a066795cc0f409433023af448bb0328Richard Smith
133103f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith/// Try to evaluate the initializer for a variable declaration.
1332f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool EvaluateVarDeclInit(EvalInfo &Info, const Expr *E,
1333f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                                const VarDecl *VD,
1334177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith                                CallStackFrame *Frame, CCValue &Result) {
1335d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  // If this is a parameter to an active constexpr function call, perform
1336d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  // argument substitution.
1337d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(VD)) {
1338745f5147e065900267c85a5568785a1991d4838fRichard Smith    // Assume arguments of a potential constant expression are unknown
1339745f5147e065900267c85a5568785a1991d4838fRichard Smith    // constant expressions.
1340745f5147e065900267c85a5568785a1991d4838fRichard Smith    if (Info.CheckingPotentialConstantExpression)
1341745f5147e065900267c85a5568785a1991d4838fRichard Smith      return false;
1342f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!Frame || !Frame->Arguments) {
1343dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith      Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr);
1344177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith      return false;
1345f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    }
1346177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith    Result = Frame->Arguments[PVD->getFunctionScopeIndex()];
1347177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith    return true;
1348d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  }
134903f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith
1350099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  // Dig out the initializer, and use the declaration which it's attached to.
1351099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  const Expr *Init = VD->getAnyInitializer(VD);
1352099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  if (!Init || Init->isValueDependent()) {
1353745f5147e065900267c85a5568785a1991d4838fRichard Smith    // If we're checking a potential constant expression, the variable could be
1354745f5147e065900267c85a5568785a1991d4838fRichard Smith    // initialized later.
1355745f5147e065900267c85a5568785a1991d4838fRichard Smith    if (!Info.CheckingPotentialConstantExpression)
1356745f5147e065900267c85a5568785a1991d4838fRichard Smith      Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr);
1357099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    return false;
1358099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  }
1359099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
1360180f47959a066795cc0f409433023af448bb0328Richard Smith  // If we're currently evaluating the initializer of this declaration, use that
1361180f47959a066795cc0f409433023af448bb0328Richard Smith  // in-flight value.
1362180f47959a066795cc0f409433023af448bb0328Richard Smith  if (Info.EvaluatingDecl == VD) {
1363b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    Result = CCValue(Info.Ctx, *Info.EvaluatingDeclValue,
1364b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                     CCValue::GlobalValue());
1365180f47959a066795cc0f409433023af448bb0328Richard Smith    return !Result.isUninit();
1366180f47959a066795cc0f409433023af448bb0328Richard Smith  }
1367180f47959a066795cc0f409433023af448bb0328Richard Smith
136865ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith  // Never evaluate the initializer of a weak variable. We can't be sure that
136965ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith  // this is the definition which will be used.
1370f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (VD->isWeak()) {
1371dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr);
137265ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith    return false;
1373f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
137465ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith
1375099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  // Check that we can fold the initializer. In C++, we will have already done
1376099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  // this in the cases where it matters for conformance.
1377099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1378099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  if (!VD->evaluateValue(Notes)) {
1379099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    Info.Diag(E->getExprLoc(), diag::note_constexpr_var_init_non_constant,
1380099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith              Notes.size() + 1) << VD;
1381099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    Info.Note(VD->getLocation(), diag::note_declared_at);
1382099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    Info.addNotes(Notes);
138347a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    return false;
1384099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  } else if (!VD->checkInitIsICE()) {
1385099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_var_init_non_constant,
1386099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith                 Notes.size() + 1) << VD;
1387099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    Info.Note(VD->getLocation(), diag::note_declared_at);
1388099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    Info.addNotes(Notes);
1389f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
139003f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith
1391b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  Result = CCValue(Info.Ctx, *VD->getEvaluatedValue(), CCValue::GlobalValue());
139247a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith  return true;
139303f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith}
139403f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith
1395c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smithstatic bool IsConstNonVolatile(QualType T) {
139603f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith  Qualifiers Quals = T.getQualifiers();
139703f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith  return Quals.hasConst() && !Quals.hasVolatile();
139803f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith}
139903f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith
140059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith/// Get the base index of the given base class within an APValue representing
140159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith/// the given derived class.
140259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithstatic unsigned getBaseIndex(const CXXRecordDecl *Derived,
140359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith                             const CXXRecordDecl *Base) {
140459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  Base = Base->getCanonicalDecl();
140559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  unsigned Index = 0;
140659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  for (CXXRecordDecl::base_class_const_iterator I = Derived->bases_begin(),
140759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith         E = Derived->bases_end(); I != E; ++I, ++Index) {
140859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    if (I->getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base)
140959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      return Index;
141059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  }
141159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
141259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  llvm_unreachable("base class missing from derived class's bases list");
141359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
141459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
1415cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith/// Extract the designated sub-object of an rvalue.
1416f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool ExtractSubobject(EvalInfo &Info, const Expr *E,
1417f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                             CCValue &Obj, QualType ObjType,
1418cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith                             const SubobjectDesignator &Sub, QualType SubType) {
1419b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (Sub.Invalid)
1420b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    // A diagnostic will have already been produced.
1421cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    return false;
1422b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (Sub.isOnePastTheEnd()) {
14237098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    Info.Diag(E->getExprLoc(), Info.getLangOpts().CPlusPlus0x ?
1424aa5d533427d803e52ee42b250ffd6645ef5ccb0fMatt Beaumont-Gay                (unsigned)diag::note_constexpr_read_past_end :
1425aa5d533427d803e52ee42b250ffd6645ef5ccb0fMatt Beaumont-Gay                (unsigned)diag::note_invalid_subexpr_in_const_expr);
14267098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    return false;
14277098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  }
1428f64699e8db3946e21b5f4a0421cbc58a3e439599Richard Smith  if (Sub.Entries.empty())
1429cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    return true;
1430745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (Info.CheckingPotentialConstantExpression && Obj.isUninit())
1431745f5147e065900267c85a5568785a1991d4838fRichard Smith    // This object might be initialized later.
1432745f5147e065900267c85a5568785a1991d4838fRichard Smith    return false;
1433cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
1434cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  assert(!Obj.isLValue() && "extracting subobject of lvalue");
1435cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  const APValue *O = &Obj;
1436180f47959a066795cc0f409433023af448bb0328Richard Smith  // Walk the designator's path to find the subobject.
1437cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  for (unsigned I = 0, N = Sub.Entries.size(); I != N; ++I) {
1438cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    if (ObjType->isArrayType()) {
1439180f47959a066795cc0f409433023af448bb0328Richard Smith      // Next subobject is an array element.
1440cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith      const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(ObjType);
1441f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      assert(CAT && "vla in literal type?");
1442cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith      uint64_t Index = Sub.Entries[I].ArrayIndex;
1443f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      if (CAT->getSize().ule(Index)) {
14447098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        // Note, it should not be possible to form a pointer with a valid
14457098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        // designator which points more than one past the end of the array.
14467098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        Info.Diag(E->getExprLoc(), Info.getLangOpts().CPlusPlus0x ?
1447aa5d533427d803e52ee42b250ffd6645ef5ccb0fMatt Beaumont-Gay                    (unsigned)diag::note_constexpr_read_past_end :
1448aa5d533427d803e52ee42b250ffd6645ef5ccb0fMatt Beaumont-Gay                    (unsigned)diag::note_invalid_subexpr_in_const_expr);
1449cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith        return false;
1450f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      }
1451cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith      if (O->getArrayInitializedElts() > Index)
1452cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith        O = &O->getArrayInitializedElt(Index);
1453cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith      else
1454cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith        O = &O->getArrayFiller();
1455cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith      ObjType = CAT->getElementType();
1456180f47959a066795cc0f409433023af448bb0328Richard Smith    } else if (const FieldDecl *Field = getAsField(Sub.Entries[I])) {
1457180f47959a066795cc0f409433023af448bb0328Richard Smith      // Next subobject is a class, struct or union field.
1458180f47959a066795cc0f409433023af448bb0328Richard Smith      RecordDecl *RD = ObjType->castAs<RecordType>()->getDecl();
1459180f47959a066795cc0f409433023af448bb0328Richard Smith      if (RD->isUnion()) {
1460180f47959a066795cc0f409433023af448bb0328Richard Smith        const FieldDecl *UnionField = O->getUnionField();
1461180f47959a066795cc0f409433023af448bb0328Richard Smith        if (!UnionField ||
1462f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith            UnionField->getCanonicalDecl() != Field->getCanonicalDecl()) {
14637098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith          Info.Diag(E->getExprLoc(),
14647098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith                    diag::note_constexpr_read_inactive_union_member)
14657098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith            << Field << !UnionField << UnionField;
1466180f47959a066795cc0f409433023af448bb0328Richard Smith          return false;
1467f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        }
1468180f47959a066795cc0f409433023af448bb0328Richard Smith        O = &O->getUnionValue();
1469180f47959a066795cc0f409433023af448bb0328Richard Smith      } else
1470180f47959a066795cc0f409433023af448bb0328Richard Smith        O = &O->getStructField(Field->getFieldIndex());
1471180f47959a066795cc0f409433023af448bb0328Richard Smith      ObjType = Field->getType();
14727098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith
14737098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      if (ObjType.isVolatileQualified()) {
14747098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        if (Info.getLangOpts().CPlusPlus) {
14757098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith          // FIXME: Include a description of the path to the volatile subobject.
14767098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith          Info.Diag(E->getExprLoc(), diag::note_constexpr_ltor_volatile_obj, 1)
14777098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith            << 2 << Field;
14787098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith          Info.Note(Field->getLocation(), diag::note_declared_at);
14797098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        } else {
14807098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith          Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr);
14817098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        }
14827098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        return false;
14837098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      }
1484cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    } else {
1485180f47959a066795cc0f409433023af448bb0328Richard Smith      // Next subobject is a base class.
148659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      const CXXRecordDecl *Derived = ObjType->getAsCXXRecordDecl();
148759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      const CXXRecordDecl *Base = getAsBaseClass(Sub.Entries[I]);
148859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      O = &O->getStructBase(getBaseIndex(Derived, Base));
148959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      ObjType = Info.Ctx.getRecordType(Base);
1490cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    }
1491180f47959a066795cc0f409433023af448bb0328Richard Smith
1492f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (O->isUninit()) {
1493745f5147e065900267c85a5568785a1991d4838fRichard Smith      if (!Info.CheckingPotentialConstantExpression)
1494745f5147e065900267c85a5568785a1991d4838fRichard Smith        Info.Diag(E->getExprLoc(), diag::note_constexpr_read_uninit);
1495180f47959a066795cc0f409433023af448bb0328Richard Smith      return false;
1496f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    }
1497cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  }
1498cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
1499b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  Obj = CCValue(Info.Ctx, *O, CCValue::GlobalValue());
1500cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  return true;
1501cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith}
1502cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
1503f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith/// Find the position where two subobject designators diverge, or equivalently
1504f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith/// the length of the common initial subsequence.
1505f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smithstatic unsigned FindDesignatorMismatch(QualType ObjType,
1506f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith                                       const SubobjectDesignator &A,
1507f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith                                       const SubobjectDesignator &B,
1508f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith                                       bool &WasArrayIndex) {
1509f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  unsigned I = 0, N = std::min(A.Entries.size(), B.Entries.size());
1510f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  for (/**/; I != N; ++I) {
1511f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    if (!ObjType.isNull() && ObjType->isArrayType()) {
1512f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      // Next subobject is an array element.
1513f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      if (A.Entries[I].ArrayIndex != B.Entries[I].ArrayIndex) {
1514f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        WasArrayIndex = true;
1515f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        return I;
1516f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      }
1517f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      ObjType = ObjType->castAsArrayTypeUnsafe()->getElementType();
1518f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    } else {
1519f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      if (A.Entries[I].BaseOrMember != B.Entries[I].BaseOrMember) {
1520f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        WasArrayIndex = false;
1521f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        return I;
1522f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      }
1523f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      if (const FieldDecl *FD = getAsField(A.Entries[I]))
1524f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        // Next subobject is a field.
1525f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        ObjType = FD->getType();
1526f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      else
1527f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        // Next subobject is a base class.
1528f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        ObjType = QualType();
1529f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    }
1530f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  }
1531f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  WasArrayIndex = false;
1532f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  return I;
1533f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith}
1534f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
1535f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith/// Determine whether the given subobject designators refer to elements of the
1536f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith/// same array object.
1537f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smithstatic bool AreElementsOfSameArray(QualType ObjType,
1538f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith                                   const SubobjectDesignator &A,
1539f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith                                   const SubobjectDesignator &B) {
1540f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  if (A.Entries.size() != B.Entries.size())
1541f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    return false;
1542f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
1543f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  bool IsArray = A.MostDerivedArraySize != 0;
1544f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  if (IsArray && A.MostDerivedPathLength != A.Entries.size())
1545f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    // A is a subobject of the array element.
1546f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    return false;
1547f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
1548f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  // If A (and B) designates an array element, the last entry will be the array
1549f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  // index. That doesn't have to match. Otherwise, we're in the 'implicit array
1550f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  // of length 1' case, and the entire path must match.
1551f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  bool WasArrayIndex;
1552f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  unsigned CommonLength = FindDesignatorMismatch(ObjType, A, B, WasArrayIndex);
1553f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith  return CommonLength >= A.Entries.size() - IsArray;
1554f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith}
1555f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
1556180f47959a066795cc0f409433023af448bb0328Richard Smith/// HandleLValueToRValueConversion - Perform an lvalue-to-rvalue conversion on
1557180f47959a066795cc0f409433023af448bb0328Richard Smith/// the given lvalue. This can also be used for 'lvalue-to-lvalue' conversions
1558180f47959a066795cc0f409433023af448bb0328Richard Smith/// for looking up the glvalue referred to by an entity of reference type.
1559180f47959a066795cc0f409433023af448bb0328Richard Smith///
1560180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param Info - Information about the ongoing evaluation.
1561f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// \param Conv - The expression for which we are performing the conversion.
1562f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith///               Used for diagnostics.
15639ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith/// \param Type - The type we expect this conversion to produce, before
15649ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith///               stripping cv-qualifiers in the case of a non-clas type.
1565180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param LVal - The glvalue on which we are attempting to perform this action.
1566180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param RVal - The produced value will be placed here.
1567f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool HandleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv,
1568f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                                           QualType Type,
1569cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith                                           const LValue &LVal, CCValue &RVal) {
15707098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  // In C, an lvalue-to-rvalue conversion is never a constant expression.
15717098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  if (!Info.getLangOpts().CPlusPlus)
15727098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    Info.CCEDiag(Conv->getExprLoc(), diag::note_invalid_subexpr_in_const_expr);
15737098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith
1574b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (LVal.Designator.Invalid)
1575b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    // A diagnostic will have already been produced.
1576b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    return false;
1577b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
15781bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  const Expr *Base = LVal.Base.dyn_cast<const Expr*>();
1579177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith  CallStackFrame *Frame = LVal.Frame;
15807098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  SourceLocation Loc = Conv->getExprLoc();
1581c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
1582f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (!LVal.Base) {
1583f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    // FIXME: Indirection through a null pointer deserves a specific diagnostic.
15847098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    Info.Diag(Loc, diag::note_invalid_subexpr_in_const_expr);
15857098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    return false;
15867098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  }
15877098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith
15887098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  // C++11 DR1311: An lvalue-to-rvalue conversion on a volatile-qualified type
15897098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  // is not a constant expression (even if the object is non-volatile). We also
15907098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  // apply this rule to C++98, in order to conform to the expected 'volatile'
15917098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  // semantics.
15927098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  if (Type.isVolatileQualified()) {
15937098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    if (Info.getLangOpts().CPlusPlus)
15947098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      Info.Diag(Loc, diag::note_constexpr_ltor_volatile_type) << Type;
15957098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    else
15967098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      Info.Diag(Loc);
1597c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return false;
1598f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
1599c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
16001bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  if (const ValueDecl *D = LVal.Base.dyn_cast<const ValueDecl*>()) {
1601c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    // In C++98, const, non-volatile integers initialized with ICEs are ICEs.
1602c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    // In C++11, constexpr, non-volatile variables initialized with constant
1603d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    // expressions are constant expressions too. Inside constexpr functions,
1604d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    // parameters are constant expressions even if they're non-const.
1605c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    // In C, such things can also be folded, although they are not ICEs.
1606c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    const VarDecl *VD = dyn_cast<VarDecl>(D);
1607f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    if (const VarDecl *VDef = VD->getDefinition())
1608f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      VD = VDef;
1609f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!VD || VD->isInvalidDecl()) {
16107098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      Info.Diag(Loc);
16110a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      return false;
1612f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    }
1613f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
16147098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    // DR1313: If the object is volatile-qualified but the glvalue was not,
16157098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    // behavior is undefined so the result is not a constant expression.
16161bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    QualType VT = VD->getType();
16177098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    if (VT.isVolatileQualified()) {
16187098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      if (Info.getLangOpts().CPlusPlus) {
16197098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        Info.Diag(Loc, diag::note_constexpr_ltor_volatile_obj, 1) << 1 << VD;
16207098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        Info.Note(VD->getLocation(), diag::note_declared_at);
16217098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      } else {
16227098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        Info.Diag(Loc);
1623f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      }
16247098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      return false;
16257098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    }
16267098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith
16277098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    if (!isa<ParmVarDecl>(VD)) {
16287098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      if (VD->isConstexpr()) {
16297098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        // OK, we can read this variable.
16307098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      } else if (VT->isIntegralOrEnumerationType()) {
16317098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        if (!VT.isConstQualified()) {
16327098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith          if (Info.getLangOpts().CPlusPlus) {
16337098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith            Info.Diag(Loc, diag::note_constexpr_ltor_non_const_int, 1) << VD;
16347098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith            Info.Note(VD->getLocation(), diag::note_declared_at);
16357098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith          } else {
16367098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith            Info.Diag(Loc);
16377098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith          }
16387098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith          return false;
16397098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        }
16407098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      } else if (VT->isFloatingType() && VT.isConstQualified()) {
16417098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        // We support folding of const floating-point types, in order to make
16427098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        // static const data members of such types (supported as an extension)
16437098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        // more useful.
16447098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        if (Info.getLangOpts().CPlusPlus0x) {
16457098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith          Info.CCEDiag(Loc, diag::note_constexpr_ltor_non_constexpr, 1) << VD;
16467098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith          Info.Note(VD->getLocation(), diag::note_declared_at);
16477098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        } else {
16487098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith          Info.CCEDiag(Loc);
16497098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        }
16507098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      } else {
16517098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        // FIXME: Allow folding of values of any literal type in all languages.
16527098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        if (Info.getLangOpts().CPlusPlus0x) {
16537098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith          Info.Diag(Loc, diag::note_constexpr_ltor_non_constexpr, 1) << VD;
16547098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith          Info.Note(VD->getLocation(), diag::note_declared_at);
16557098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        } else {
16567098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith          Info.Diag(Loc);
16577098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        }
16580a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith        return false;
1659f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      }
16600a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    }
16617098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith
1662f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!EvaluateVarDeclInit(Info, Conv, VD, Frame, RVal))
1663c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith      return false;
1664c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
166547a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    if (isa<ParmVarDecl>(VD) || !VD->getAnyInitializer()->isLValue())
1666f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return ExtractSubobject(Info, Conv, RVal, VT, LVal.Designator, Type);
1667c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
1668c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    // The declaration was initialized by an lvalue, with no lvalue-to-rvalue
1669c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    // conversion. This happens when the declaration and the lvalue should be
1670c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    // considered synonymous, for instance when initializing an array of char
1671c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    // from a string literal. Continue as if the initializer lvalue was the
1672c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    // value we were originally given.
16730a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    assert(RVal.getLValueOffset().isZero() &&
16740a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith           "offset for lvalue init of non-reference");
16751bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    Base = RVal.getLValueBase().get<const Expr*>();
1676177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith    Frame = RVal.getLValueFrame();
1677c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  }
1678c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
16797098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  // Volatile temporary objects cannot be read in constant expressions.
16807098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  if (Base->getType().isVolatileQualified()) {
16817098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    if (Info.getLangOpts().CPlusPlus) {
16827098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      Info.Diag(Loc, diag::note_constexpr_ltor_volatile_obj, 1) << 0;
16837098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      Info.Note(Base->getExprLoc(), diag::note_constexpr_temporary_here);
16847098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    } else {
16857098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      Info.Diag(Loc);
16867098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    }
16877098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    return false;
16887098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith  }
16897098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith
16900a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith  // FIXME: Support PredefinedExpr, ObjCEncodeExpr, MakeStringConstant
16910a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith  if (const StringLiteral *S = dyn_cast<StringLiteral>(Base)) {
16920a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    const SubobjectDesignator &Designator = LVal.Designator;
1693f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (Designator.Invalid || Designator.Entries.size() != 1) {
1694dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith      Info.Diag(Conv->getExprLoc(), diag::note_invalid_subexpr_in_const_expr);
16950a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      return false;
1696f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    }
16970a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith
16980a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    assert(Type->isIntegerType() && "string element not integer type");
16999a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith    uint64_t Index = Designator.Entries[0].ArrayIndex;
17007098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    const ConstantArrayType *CAT =
17017098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith        Info.Ctx.getAsConstantArrayType(S->getType());
17027098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith    if (Index >= CAT->getSize().getZExtValue()) {
17037098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      // Note, it should not be possible to form a pointer which points more
17047098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      // than one past the end of the array without producing a prior const expr
17057098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      // diagnostic.
17067098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith      Info.Diag(Loc, diag::note_constexpr_read_past_end);
17070a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      return false;
1708f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    }
17090a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(),
17100a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith                 Type->isUnsignedIntegerType());
17110a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    if (Index < S->getLength())
17120a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      Value = S->getCodeUnit(Index);
17130a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    RVal = CCValue(Value);
17140a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    return true;
17150a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith  }
17160a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith
1717177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith  if (Frame) {
1718cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    // If this is a temporary expression with a nontrivial initializer, grab the
1719cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    // value from the relevant stack frame.
1720177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith    RVal = Frame->Temporaries[Base];
1721cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  } else if (const CompoundLiteralExpr *CLE
1722cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith             = dyn_cast<CompoundLiteralExpr>(Base)) {
1723cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    // In C99, a CompoundLiteralExpr is an lvalue, and we defer evaluating the
1724cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    // initializer until now for such expressions. Such an expression can't be
1725cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    // an ICE in C, so this only matters for fold.
1726c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?");
1727cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    if (!Evaluate(RVal, Info, CLE->getInitializer()))
1728cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith      return false;
1729f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  } else {
1730dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    Info.Diag(Conv->getExprLoc(), diag::note_invalid_subexpr_in_const_expr);
1731cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    return false;
1732f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
1733c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
1734f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  return ExtractSubobject(Info, Conv, RVal, Base->getType(), LVal.Designator,
1735f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                          Type);
1736c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith}
1737c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
173859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith/// Build an lvalue for the object argument of a member function call.
173959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithstatic bool EvaluateObjectArgument(EvalInfo &Info, const Expr *Object,
174059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith                                   LValue &This) {
174159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  if (Object->getType()->isPointerType())
174259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    return EvaluatePointer(Object, This, Info);
174359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
174459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  if (Object->isGLValue())
174559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    return EvaluateLValue(Object, This, Info);
174659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
1747e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (Object->getType()->isLiteralType())
1748e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return EvaluateTemporary(Object, This, Info);
1749e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1750e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  return false;
1751e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}
1752e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1753e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// HandleMemberPointerAccess - Evaluate a member access operation and build an
1754e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// lvalue referring to the result.
1755e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith///
1756e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// \param Info - Information about the ongoing evaluation.
1757e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// \param BO - The member pointer access operation.
1758e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// \param LV - Filled in with a reference to the resulting object.
1759e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// \param IncludeMember - Specifies whether the member itself is included in
1760e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith///        the resulting LValue subobject designator. This is not possible when
1761e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith///        creating a bound member function.
1762e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// \return The field or method declaration to which the member pointer refers,
1763e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith///         or 0 if evaluation fails.
1764e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info,
1765e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                                                  const BinaryOperator *BO,
1766e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                                                  LValue &LV,
1767e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                                                  bool IncludeMember = true) {
1768e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  assert(BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI);
1769e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1770745f5147e065900267c85a5568785a1991d4838fRichard Smith  bool EvalObjOK = EvaluateObjectArgument(Info, BO->getLHS(), LV);
1771745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!EvalObjOK && !Info.keepEvaluatingAfterFailure())
1772e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return 0;
1773e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1774e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  MemberPtr MemPtr;
1775e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (!EvaluateMemberPointer(BO->getRHS(), MemPtr, Info))
1776e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return 0;
1777e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1778e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // C++11 [expr.mptr.oper]p6: If the second operand is the null pointer to
1779e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // member value, the behavior is undefined.
1780e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (!MemPtr.getDecl())
1781e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return 0;
1782e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1783745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!EvalObjOK)
1784745f5147e065900267c85a5568785a1991d4838fRichard Smith    return 0;
1785745f5147e065900267c85a5568785a1991d4838fRichard Smith
1786e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (MemPtr.isDerivedMember()) {
1787e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // This is a member of some derived class. Truncate LV appropriately.
1788e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // The end of the derived-to-base path for the base object must match the
1789e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // derived-to-base path for the member pointer.
1790b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    if (LV.Designator.MostDerivedPathLength + MemPtr.Path.size() >
1791e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        LV.Designator.Entries.size())
1792e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return 0;
1793e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    unsigned PathLengthToMember =
1794e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        LV.Designator.Entries.size() - MemPtr.Path.size();
1795e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    for (unsigned I = 0, N = MemPtr.Path.size(); I != N; ++I) {
1796e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      const CXXRecordDecl *LVDecl = getAsBaseClass(
1797e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith          LV.Designator.Entries[PathLengthToMember + I]);
1798e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      const CXXRecordDecl *MPDecl = MemPtr.Path[I];
1799e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (LVDecl->getCanonicalDecl() != MPDecl->getCanonicalDecl())
1800e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return 0;
1801e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
1802e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1803e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // Truncate the lvalue to the appropriate derived class.
1804b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    if (!CastToDerivedClass(Info, BO, LV, MemPtr.getContainingRecord(),
1805b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                            PathLengthToMember))
1806b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      return 0;
1807e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  } else if (!MemPtr.Path.empty()) {
1808e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // Extend the LValue path with the member pointer's path.
1809e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    LV.Designator.Entries.reserve(LV.Designator.Entries.size() +
1810e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                                  MemPtr.Path.size() + IncludeMember);
1811e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1812e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // Walk down to the appropriate base class.
1813e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    QualType LVType = BO->getLHS()->getType();
1814e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (const PointerType *PT = LVType->getAs<PointerType>())
1815e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      LVType = PT->getPointeeType();
1816e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    const CXXRecordDecl *RD = LVType->getAsCXXRecordDecl();
1817e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    assert(RD && "member pointer access on non-class-type expression");
1818e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // The first class in the path is that of the lvalue.
1819e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    for (unsigned I = 1, N = MemPtr.Path.size(); I != N; ++I) {
1820e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      const CXXRecordDecl *Base = MemPtr.Path[N - I - 1];
1821b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      HandleLValueDirectBase(Info, BO, LV, RD, Base);
1822e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      RD = Base;
1823e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
1824e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // Finally cast to the class containing the member.
1825b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    HandleLValueDirectBase(Info, BO, LV, RD, MemPtr.getContainingRecord());
1826e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
1827e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1828e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // Add the member. Note that we cannot build bound member functions here.
1829e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (IncludeMember) {
1830d9b02e726262e4009dda830998bb934172ac0020Richard Smith    if (const FieldDecl *FD = dyn_cast<FieldDecl>(MemPtr.getDecl()))
1831d9b02e726262e4009dda830998bb934172ac0020Richard Smith      HandleLValueMember(Info, BO, LV, FD);
1832d9b02e726262e4009dda830998bb934172ac0020Richard Smith    else if (const IndirectFieldDecl *IFD =
1833d9b02e726262e4009dda830998bb934172ac0020Richard Smith               dyn_cast<IndirectFieldDecl>(MemPtr.getDecl()))
1834d9b02e726262e4009dda830998bb934172ac0020Richard Smith      HandleLValueIndirectMember(Info, BO, LV, IFD);
1835d9b02e726262e4009dda830998bb934172ac0020Richard Smith    else
1836d9b02e726262e4009dda830998bb934172ac0020Richard Smith      llvm_unreachable("can't construct reference to bound member function");
1837e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
1838e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1839e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  return MemPtr.getDecl();
1840e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}
1841e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1842e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// HandleBaseToDerivedCast - Apply the given base-to-derived cast operation on
1843e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// the provided lvalue, which currently refers to the base object.
1844e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E,
1845e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                                    LValue &Result) {
1846e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  SubobjectDesignator &D = Result.Designator;
1847b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (D.Invalid || !Result.checkNullPointer(Info, E, CSK_Derived))
1848e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return false;
1849e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1850e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  QualType TargetQT = E->getType();
1851e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (const PointerType *PT = TargetQT->getAs<PointerType>())
1852e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    TargetQT = PT->getPointeeType();
1853b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
1854b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // Check this cast lands within the final derived-to-base subobject path.
1855b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (D.MostDerivedPathLength + E->path_size() > D.Entries.size()) {
1856b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_invalid_downcast)
1857b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      << D.MostDerivedType << TargetQT;
1858b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    return false;
1859b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  }
1860b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith
1861b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // Check the type of the final cast. We don't need to check the path,
1862b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  // since a cast can only be formed if the path is unique.
1863b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  unsigned NewEntriesSize = D.Entries.size() - E->path_size();
1864e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  const CXXRecordDecl *TargetType = TargetQT->getAsCXXRecordDecl();
1865e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  const CXXRecordDecl *FinalType;
1866b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (NewEntriesSize == D.MostDerivedPathLength)
1867b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    FinalType = D.MostDerivedType->getAsCXXRecordDecl();
1868b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  else
1869e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    FinalType = getAsBaseClass(D.Entries[NewEntriesSize - 1]);
1870b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl()) {
1871b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_invalid_downcast)
1872b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      << D.MostDerivedType << TargetQT;
1873e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return false;
1874b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  }
1875e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
1876e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // Truncate the lvalue to the appropriate derived class.
1877b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  return CastToDerivedClass(Info, E, Result, TargetType, NewEntriesSize);
187859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
187959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
1880c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stumpnamespace {
1881d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smithenum EvalStmtResult {
1882d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  /// Evaluation failed.
1883d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  ESR_Failed,
1884d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  /// Hit a 'return' statement.
1885d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  ESR_Returned,
1886d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  /// Evaluation succeeded.
1887d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  ESR_Succeeded
1888d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith};
1889d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith}
1890d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
1891d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith// Evaluate a statement.
1892c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstatic EvalStmtResult EvaluateStmt(APValue &Result, EvalInfo &Info,
1893d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith                                   const Stmt *S) {
1894d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  switch (S->getStmtClass()) {
1895d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  default:
1896d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    return ESR_Failed;
1897d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
1898d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  case Stmt::NullStmtClass:
1899d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  case Stmt::DeclStmtClass:
1900d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    return ESR_Succeeded;
1901d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
1902c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  case Stmt::ReturnStmtClass: {
1903c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    CCValue CCResult;
1904c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    const Expr *RetExpr = cast<ReturnStmt>(S)->getRetValue();
1905c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    if (!Evaluate(CCResult, Info, RetExpr) ||
1906c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith        !CheckConstantExpression(Info, RetExpr, CCResult, Result,
1907c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                 CCEK_ReturnValue))
1908c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      return ESR_Failed;
1909c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return ESR_Returned;
1910c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  }
1911d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
1912d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  case Stmt::CompoundStmtClass: {
1913d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    const CompoundStmt *CS = cast<CompoundStmt>(S);
1914d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    for (CompoundStmt::const_body_iterator BI = CS->body_begin(),
1915d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith           BE = CS->body_end(); BI != BE; ++BI) {
1916d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith      EvalStmtResult ESR = EvaluateStmt(Result, Info, *BI);
1917d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith      if (ESR != ESR_Succeeded)
1918d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith        return ESR;
1919d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    }
1920d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    return ESR_Succeeded;
1921d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  }
1922d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  }
1923d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith}
1924d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
19256180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith/// CheckTrivialDefaultConstructor - Check whether a constructor is a trivial
19266180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith/// default constructor. If so, we'll fold it whether or not it's marked as
19276180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith/// constexpr. If it is marked as constexpr, we will never implicitly define it,
19286180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith/// so we need special handling.
19296180245e9f63d2927b185ec251fb75aba30f1cacRichard Smithstatic bool CheckTrivialDefaultConstructor(EvalInfo &Info, SourceLocation Loc,
193051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith                                           const CXXConstructorDecl *CD,
193151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith                                           bool IsValueInitialization) {
19326180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith  if (!CD->isTrivial() || !CD->isDefaultConstructor())
19336180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    return false;
19346180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith
19354c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  // Value-initialization does not call a trivial default constructor, so such a
19364c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  // call is a core constant expression whether or not the constructor is
19374c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  // constexpr.
19384c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  if (!CD->isConstexpr() && !IsValueInitialization) {
19396180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    if (Info.getLangOpts().CPlusPlus0x) {
19404c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith      // FIXME: If DiagDecl is an implicitly-declared special member function,
19414c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith      // we should be much more explicit about why it's not constexpr.
19424c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith      Info.CCEDiag(Loc, diag::note_constexpr_invalid_function, 1)
19434c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith        << /*IsConstexpr*/0 << /*IsConstructor*/1 << CD;
19444c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith      Info.Note(CD->getLocation(), diag::note_declared_at);
19456180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    } else {
19466180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith      Info.CCEDiag(Loc, diag::note_invalid_subexpr_in_const_expr);
19476180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    }
19486180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith  }
19496180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith  return true;
19506180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith}
19516180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith
1952c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith/// CheckConstexprFunction - Check that a function can be called in a constant
1953c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith/// expression.
1954c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstatic bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc,
1955c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                   const FunctionDecl *Declaration,
1956c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                   const FunctionDecl *Definition) {
1957745f5147e065900267c85a5568785a1991d4838fRichard Smith  // Potential constant expressions can contain calls to declared, but not yet
1958745f5147e065900267c85a5568785a1991d4838fRichard Smith  // defined, constexpr functions.
1959745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (Info.CheckingPotentialConstantExpression && !Definition &&
1960745f5147e065900267c85a5568785a1991d4838fRichard Smith      Declaration->isConstexpr())
1961745f5147e065900267c85a5568785a1991d4838fRichard Smith    return false;
1962745f5147e065900267c85a5568785a1991d4838fRichard Smith
1963c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  // Can we evaluate this function call?
1964c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  if (Definition && Definition->isConstexpr() && !Definition->isInvalidDecl())
1965c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return true;
1966c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
1967c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  if (Info.getLangOpts().CPlusPlus0x) {
1968c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    const FunctionDecl *DiagDecl = Definition ? Definition : Declaration;
1969099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    // FIXME: If DiagDecl is an implicitly-declared special member function, we
1970099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    // should be much more explicit about why it's not constexpr.
1971c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    Info.Diag(CallLoc, diag::note_constexpr_invalid_function, 1)
1972c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      << DiagDecl->isConstexpr() << isa<CXXConstructorDecl>(DiagDecl)
1973c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      << DiagDecl;
1974c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    Info.Note(DiagDecl->getLocation(), diag::note_declared_at);
1975c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  } else {
1976c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    Info.Diag(CallLoc, diag::note_invalid_subexpr_in_const_expr);
1977c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  }
1978c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  return false;
1979c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}
1980c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith
1981180f47959a066795cc0f409433023af448bb0328Richard Smithnamespace {
1982cd99b072e8b32075da6233cbf14ea58a5c82de4fRichard Smithtypedef SmallVector<CCValue, 8> ArgVector;
1983180f47959a066795cc0f409433023af448bb0328Richard Smith}
1984180f47959a066795cc0f409433023af448bb0328Richard Smith
1985180f47959a066795cc0f409433023af448bb0328Richard Smith/// EvaluateArgs - Evaluate the arguments to a function call.
1986180f47959a066795cc0f409433023af448bb0328Richard Smithstatic bool EvaluateArgs(ArrayRef<const Expr*> Args, ArgVector &ArgValues,
1987180f47959a066795cc0f409433023af448bb0328Richard Smith                         EvalInfo &Info) {
1988745f5147e065900267c85a5568785a1991d4838fRichard Smith  bool Success = true;
1989180f47959a066795cc0f409433023af448bb0328Richard Smith  for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end();
1990745f5147e065900267c85a5568785a1991d4838fRichard Smith       I != E; ++I) {
1991745f5147e065900267c85a5568785a1991d4838fRichard Smith    if (!Evaluate(ArgValues[I - Args.begin()], Info, *I)) {
1992745f5147e065900267c85a5568785a1991d4838fRichard Smith      // If we're checking for a potential constant expression, evaluate all
1993745f5147e065900267c85a5568785a1991d4838fRichard Smith      // initializers even if some of them fail.
1994745f5147e065900267c85a5568785a1991d4838fRichard Smith      if (!Info.keepEvaluatingAfterFailure())
1995745f5147e065900267c85a5568785a1991d4838fRichard Smith        return false;
1996745f5147e065900267c85a5568785a1991d4838fRichard Smith      Success = false;
1997745f5147e065900267c85a5568785a1991d4838fRichard Smith    }
1998745f5147e065900267c85a5568785a1991d4838fRichard Smith  }
1999745f5147e065900267c85a5568785a1991d4838fRichard Smith  return Success;
2000180f47959a066795cc0f409433023af448bb0328Richard Smith}
2001180f47959a066795cc0f409433023af448bb0328Richard Smith
2002d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith/// Evaluate a function call.
2003745f5147e065900267c85a5568785a1991d4838fRichard Smithstatic bool HandleFunctionCall(SourceLocation CallLoc,
2004745f5147e065900267c85a5568785a1991d4838fRichard Smith                               const FunctionDecl *Callee, const LValue *This,
2005f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                               ArrayRef<const Expr*> Args, const Stmt *Body,
2006c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                               EvalInfo &Info, APValue &Result) {
2007180f47959a066795cc0f409433023af448bb0328Richard Smith  ArgVector ArgValues(Args.size());
2008180f47959a066795cc0f409433023af448bb0328Richard Smith  if (!EvaluateArgs(Args, ArgValues, Info))
2009180f47959a066795cc0f409433023af448bb0328Richard Smith    return false;
2010d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
2011745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!Info.CheckCallLimit(CallLoc))
2012745f5147e065900267c85a5568785a1991d4838fRichard Smith    return false;
2013745f5147e065900267c85a5568785a1991d4838fRichard Smith
2014745f5147e065900267c85a5568785a1991d4838fRichard Smith  CallStackFrame Frame(Info, CallLoc, Callee, This, ArgValues.data());
2015d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  return EvaluateStmt(Result, Info, Body) == ESR_Returned;
2016d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith}
2017d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
2018180f47959a066795cc0f409433023af448bb0328Richard Smith/// Evaluate a constructor call.
2019745f5147e065900267c85a5568785a1991d4838fRichard Smithstatic bool HandleConstructorCall(SourceLocation CallLoc, const LValue &This,
202059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith                                  ArrayRef<const Expr*> Args,
2021180f47959a066795cc0f409433023af448bb0328Richard Smith                                  const CXXConstructorDecl *Definition,
202251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith                                  EvalInfo &Info, APValue &Result) {
2023180f47959a066795cc0f409433023af448bb0328Richard Smith  ArgVector ArgValues(Args.size());
2024180f47959a066795cc0f409433023af448bb0328Richard Smith  if (!EvaluateArgs(Args, ArgValues, Info))
2025180f47959a066795cc0f409433023af448bb0328Richard Smith    return false;
2026180f47959a066795cc0f409433023af448bb0328Richard Smith
2027745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!Info.CheckCallLimit(CallLoc))
2028745f5147e065900267c85a5568785a1991d4838fRichard Smith    return false;
2029745f5147e065900267c85a5568785a1991d4838fRichard Smith
2030745f5147e065900267c85a5568785a1991d4838fRichard Smith  CallStackFrame Frame(Info, CallLoc, Definition, &This, ArgValues.data());
2031180f47959a066795cc0f409433023af448bb0328Richard Smith
2032180f47959a066795cc0f409433023af448bb0328Richard Smith  // If it's a delegating constructor, just delegate.
2033180f47959a066795cc0f409433023af448bb0328Richard Smith  if (Definition->isDelegatingConstructor()) {
2034180f47959a066795cc0f409433023af448bb0328Richard Smith    CXXConstructorDecl::init_const_iterator I = Definition->init_begin();
2035180f47959a066795cc0f409433023af448bb0328Richard Smith    return EvaluateConstantExpression(Result, Info, This, (*I)->getInit());
2036180f47959a066795cc0f409433023af448bb0328Richard Smith  }
2037180f47959a066795cc0f409433023af448bb0328Richard Smith
2038610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith  // For a trivial copy or move constructor, perform an APValue copy. This is
2039610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith  // essential for unions, where the operations performed by the constructor
2040610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith  // cannot be represented by ctor-initializers.
2041180f47959a066795cc0f409433023af448bb0328Richard Smith  const CXXRecordDecl *RD = Definition->getParent();
2042610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith  if (Definition->isDefaulted() &&
2043610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith      ((Definition->isCopyConstructor() && RD->hasTrivialCopyConstructor()) ||
2044610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith       (Definition->isMoveConstructor() && RD->hasTrivialMoveConstructor()))) {
2045610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith    LValue RHS;
2046610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith    RHS.setFrom(ArgValues[0]);
2047610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith    CCValue Value;
2048745f5147e065900267c85a5568785a1991d4838fRichard Smith    if (!HandleLValueToRValueConversion(Info, Args[0], Args[0]->getType(),
2049745f5147e065900267c85a5568785a1991d4838fRichard Smith                                        RHS, Value))
2050745f5147e065900267c85a5568785a1991d4838fRichard Smith      return false;
2051745f5147e065900267c85a5568785a1991d4838fRichard Smith    assert((Value.isStruct() || Value.isUnion()) &&
2052745f5147e065900267c85a5568785a1991d4838fRichard Smith           "trivial copy/move from non-class type?");
2053745f5147e065900267c85a5568785a1991d4838fRichard Smith    // Any CCValue of class type must already be a constant expression.
2054745f5147e065900267c85a5568785a1991d4838fRichard Smith    Result = Value;
2055745f5147e065900267c85a5568785a1991d4838fRichard Smith    return true;
2056610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith  }
2057610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith
2058610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith  // Reserve space for the struct members.
205951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (!RD->isUnion() && Result.isUninit())
2060180f47959a066795cc0f409433023af448bb0328Richard Smith    Result = APValue(APValue::UninitStruct(), RD->getNumBases(),
2061180f47959a066795cc0f409433023af448bb0328Richard Smith                     std::distance(RD->field_begin(), RD->field_end()));
2062180f47959a066795cc0f409433023af448bb0328Richard Smith
2063180f47959a066795cc0f409433023af448bb0328Richard Smith  const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD);
2064180f47959a066795cc0f409433023af448bb0328Richard Smith
2065745f5147e065900267c85a5568785a1991d4838fRichard Smith  bool Success = true;
2066180f47959a066795cc0f409433023af448bb0328Richard Smith  unsigned BasesSeen = 0;
2067180f47959a066795cc0f409433023af448bb0328Richard Smith#ifndef NDEBUG
2068180f47959a066795cc0f409433023af448bb0328Richard Smith  CXXRecordDecl::base_class_const_iterator BaseIt = RD->bases_begin();
2069180f47959a066795cc0f409433023af448bb0328Richard Smith#endif
2070180f47959a066795cc0f409433023af448bb0328Richard Smith  for (CXXConstructorDecl::init_const_iterator I = Definition->init_begin(),
2071180f47959a066795cc0f409433023af448bb0328Richard Smith       E = Definition->init_end(); I != E; ++I) {
2072745f5147e065900267c85a5568785a1991d4838fRichard Smith    LValue Subobject = This;
2073745f5147e065900267c85a5568785a1991d4838fRichard Smith    APValue *Value = &Result;
2074745f5147e065900267c85a5568785a1991d4838fRichard Smith
2075745f5147e065900267c85a5568785a1991d4838fRichard Smith    // Determine the subobject to initialize.
2076180f47959a066795cc0f409433023af448bb0328Richard Smith    if ((*I)->isBaseInitializer()) {
2077180f47959a066795cc0f409433023af448bb0328Richard Smith      QualType BaseType((*I)->getBaseClass(), 0);
2078180f47959a066795cc0f409433023af448bb0328Richard Smith#ifndef NDEBUG
2079180f47959a066795cc0f409433023af448bb0328Richard Smith      // Non-virtual base classes are initialized in the order in the class
2080180f47959a066795cc0f409433023af448bb0328Richard Smith      // definition. We cannot have a virtual base class for a literal type.
2081180f47959a066795cc0f409433023af448bb0328Richard Smith      assert(!BaseIt->isVirtual() && "virtual base for literal type");
2082180f47959a066795cc0f409433023af448bb0328Richard Smith      assert(Info.Ctx.hasSameType(BaseIt->getType(), BaseType) &&
2083180f47959a066795cc0f409433023af448bb0328Richard Smith             "base class initializers not in expected order");
2084180f47959a066795cc0f409433023af448bb0328Richard Smith      ++BaseIt;
2085180f47959a066795cc0f409433023af448bb0328Richard Smith#endif
2086b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      HandleLValueDirectBase(Info, (*I)->getInit(), Subobject, RD,
2087180f47959a066795cc0f409433023af448bb0328Richard Smith                             BaseType->getAsCXXRecordDecl(), &Layout);
2088745f5147e065900267c85a5568785a1991d4838fRichard Smith      Value = &Result.getStructBase(BasesSeen++);
2089180f47959a066795cc0f409433023af448bb0328Richard Smith    } else if (FieldDecl *FD = (*I)->getMember()) {
2090b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      HandleLValueMember(Info, (*I)->getInit(), Subobject, FD, &Layout);
2091180f47959a066795cc0f409433023af448bb0328Richard Smith      if (RD->isUnion()) {
2092180f47959a066795cc0f409433023af448bb0328Richard Smith        Result = APValue(FD);
2093745f5147e065900267c85a5568785a1991d4838fRichard Smith        Value = &Result.getUnionValue();
2094745f5147e065900267c85a5568785a1991d4838fRichard Smith      } else {
2095745f5147e065900267c85a5568785a1991d4838fRichard Smith        Value = &Result.getStructField(FD->getFieldIndex());
2096745f5147e065900267c85a5568785a1991d4838fRichard Smith      }
2097d9b02e726262e4009dda830998bb934172ac0020Richard Smith    } else if (IndirectFieldDecl *IFD = (*I)->getIndirectMember()) {
2098d9b02e726262e4009dda830998bb934172ac0020Richard Smith      // Walk the indirect field decl's chain to find the object to initialize,
2099d9b02e726262e4009dda830998bb934172ac0020Richard Smith      // and make sure we've initialized every step along it.
2100d9b02e726262e4009dda830998bb934172ac0020Richard Smith      for (IndirectFieldDecl::chain_iterator C = IFD->chain_begin(),
2101d9b02e726262e4009dda830998bb934172ac0020Richard Smith                                             CE = IFD->chain_end();
2102d9b02e726262e4009dda830998bb934172ac0020Richard Smith           C != CE; ++C) {
2103d9b02e726262e4009dda830998bb934172ac0020Richard Smith        FieldDecl *FD = cast<FieldDecl>(*C);
2104d9b02e726262e4009dda830998bb934172ac0020Richard Smith        CXXRecordDecl *CD = cast<CXXRecordDecl>(FD->getParent());
2105d9b02e726262e4009dda830998bb934172ac0020Richard Smith        // Switch the union field if it differs. This happens if we had
2106d9b02e726262e4009dda830998bb934172ac0020Richard Smith        // preceding zero-initialization, and we're now initializing a union
2107d9b02e726262e4009dda830998bb934172ac0020Richard Smith        // subobject other than the first.
2108d9b02e726262e4009dda830998bb934172ac0020Richard Smith        // FIXME: In this case, the values of the other subobjects are
2109d9b02e726262e4009dda830998bb934172ac0020Richard Smith        // specified, since zero-initialization sets all padding bits to zero.
2110d9b02e726262e4009dda830998bb934172ac0020Richard Smith        if (Value->isUninit() ||
2111d9b02e726262e4009dda830998bb934172ac0020Richard Smith            (Value->isUnion() && Value->getUnionField() != FD)) {
2112d9b02e726262e4009dda830998bb934172ac0020Richard Smith          if (CD->isUnion())
2113d9b02e726262e4009dda830998bb934172ac0020Richard Smith            *Value = APValue(FD);
2114d9b02e726262e4009dda830998bb934172ac0020Richard Smith          else
2115d9b02e726262e4009dda830998bb934172ac0020Richard Smith            *Value = APValue(APValue::UninitStruct(), CD->getNumBases(),
2116d9b02e726262e4009dda830998bb934172ac0020Richard Smith                             std::distance(CD->field_begin(), CD->field_end()));
2117d9b02e726262e4009dda830998bb934172ac0020Richard Smith        }
2118745f5147e065900267c85a5568785a1991d4838fRichard Smith        HandleLValueMember(Info, (*I)->getInit(), Subobject, FD);
2119d9b02e726262e4009dda830998bb934172ac0020Richard Smith        if (CD->isUnion())
2120d9b02e726262e4009dda830998bb934172ac0020Richard Smith          Value = &Value->getUnionValue();
2121d9b02e726262e4009dda830998bb934172ac0020Richard Smith        else
2122d9b02e726262e4009dda830998bb934172ac0020Richard Smith          Value = &Value->getStructField(FD->getFieldIndex());
2123d9b02e726262e4009dda830998bb934172ac0020Richard Smith      }
2124180f47959a066795cc0f409433023af448bb0328Richard Smith    } else {
2125d9b02e726262e4009dda830998bb934172ac0020Richard Smith      llvm_unreachable("unknown base initializer kind");
2126180f47959a066795cc0f409433023af448bb0328Richard Smith    }
2127745f5147e065900267c85a5568785a1991d4838fRichard Smith
2128745f5147e065900267c85a5568785a1991d4838fRichard Smith    if (!EvaluateConstantExpression(*Value, Info, Subobject, (*I)->getInit(),
2129745f5147e065900267c85a5568785a1991d4838fRichard Smith                                    (*I)->isBaseInitializer()
2130745f5147e065900267c85a5568785a1991d4838fRichard Smith                                      ? CCEK_Constant : CCEK_MemberInit)) {
2131745f5147e065900267c85a5568785a1991d4838fRichard Smith      // If we're checking for a potential constant expression, evaluate all
2132745f5147e065900267c85a5568785a1991d4838fRichard Smith      // initializers even if some of them fail.
2133745f5147e065900267c85a5568785a1991d4838fRichard Smith      if (!Info.keepEvaluatingAfterFailure())
2134745f5147e065900267c85a5568785a1991d4838fRichard Smith        return false;
2135745f5147e065900267c85a5568785a1991d4838fRichard Smith      Success = false;
2136745f5147e065900267c85a5568785a1991d4838fRichard Smith    }
2137180f47959a066795cc0f409433023af448bb0328Richard Smith  }
2138180f47959a066795cc0f409433023af448bb0328Richard Smith
2139745f5147e065900267c85a5568785a1991d4838fRichard Smith  return Success;
2140180f47959a066795cc0f409433023af448bb0328Richard Smith}
2141180f47959a066795cc0f409433023af448bb0328Richard Smith
2142d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smithnamespace {
2143770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass HasSideEffect
21448cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  : public ConstStmtVisitor<HasSideEffect, bool> {
21451e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith  const ASTContext &Ctx;
2146c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stumppublic:
2147c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump
21481e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith  HasSideEffect(const ASTContext &C) : Ctx(C) {}
2149c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump
2150c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump  // Unhandled nodes conservatively default to having side effects.
21518cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitStmt(const Stmt *S) {
2152c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump    return true;
2153c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump  }
2154c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump
21558cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitParenExpr(const ParenExpr *E) { return Visit(E->getSubExpr()); }
21568cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitGenericSelectionExpr(const GenericSelectionExpr *E) {
2157f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return Visit(E->getResultExpr());
2158f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  }
21598cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitDeclRefExpr(const DeclRefExpr *E) {
21601e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    if (Ctx.getCanonicalType(E->getType()).isVolatileQualified())
2161c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump      return true;
2162c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump    return false;
2163c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump  }
2164f85e193739c953358c865005855253af4f68a497John McCall  bool VisitObjCIvarRefExpr(const ObjCIvarRefExpr *E) {
21651e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    if (Ctx.getCanonicalType(E->getType()).isVolatileQualified())
2166f85e193739c953358c865005855253af4f68a497John McCall      return true;
2167f85e193739c953358c865005855253af4f68a497John McCall    return false;
2168f85e193739c953358c865005855253af4f68a497John McCall  }
2169f85e193739c953358c865005855253af4f68a497John McCall  bool VisitBlockDeclRefExpr (const BlockDeclRefExpr *E) {
21701e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    if (Ctx.getCanonicalType(E->getType()).isVolatileQualified())
2171f85e193739c953358c865005855253af4f68a497John McCall      return true;
2172f85e193739c953358c865005855253af4f68a497John McCall    return false;
2173f85e193739c953358c865005855253af4f68a497John McCall  }
2174f85e193739c953358c865005855253af4f68a497John McCall
2175c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump  // We don't want to evaluate BlockExprs multiple times, as they generate
2176c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump  // a ton of code.
21778cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitBlockExpr(const BlockExpr *E) { return true; }
21788cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitPredefinedExpr(const PredefinedExpr *E) { return false; }
21798cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E)
2180c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump    { return Visit(E->getInitializer()); }
21818cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitMemberExpr(const MemberExpr *E) { return Visit(E->getBase()); }
21828cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitIntegerLiteral(const IntegerLiteral *E) { return false; }
21838cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitFloatingLiteral(const FloatingLiteral *E) { return false; }
21848cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitStringLiteral(const StringLiteral *E) { return false; }
21858cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCharacterLiteral(const CharacterLiteral *E) { return false; }
21868cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E)
2187f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    { return false; }
21888cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E)
2189980ca220848d27ef55ef4c2d37423461a8ed0da3Mike Stump    { return Visit(E->getLHS()) || Visit(E->getRHS()); }
21908cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitChooseExpr(const ChooseExpr *E)
21911e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    { return Visit(E->getChosenSubExpr(Ctx)); }
21928cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCastExpr(const CastExpr *E) { return Visit(E->getSubExpr()); }
21938cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitBinAssign(const BinaryOperator *E) { return true; }
21948cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCompoundAssignOperator(const BinaryOperator *E) { return true; }
21958cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitBinaryOperator(const BinaryOperator *E)
2196980ca220848d27ef55ef4c2d37423461a8ed0da3Mike Stump  { return Visit(E->getLHS()) || Visit(E->getRHS()); }
21978cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitUnaryPreInc(const UnaryOperator *E) { return true; }
21988cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitUnaryPostInc(const UnaryOperator *E) { return true; }
21998cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitUnaryPreDec(const UnaryOperator *E) { return true; }
22008cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitUnaryPostDec(const UnaryOperator *E) { return true; }
22018cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitUnaryDeref(const UnaryOperator *E) {
22021e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    if (Ctx.getCanonicalType(E->getType()).isVolatileQualified())
2203c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump      return true;
2204980ca220848d27ef55ef4c2d37423461a8ed0da3Mike Stump    return Visit(E->getSubExpr());
2205c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump  }
22068cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitUnaryOperator(const UnaryOperator *E) { return Visit(E->getSubExpr()); }
2207363ff23cfddb51abe4ee4212a6dd3c9b534fcc8bChris Lattner
2208363ff23cfddb51abe4ee4212a6dd3c9b534fcc8bChris Lattner  // Has side effects if any element does.
22098cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitInitListExpr(const InitListExpr *E) {
2210363ff23cfddb51abe4ee4212a6dd3c9b534fcc8bChris Lattner    for (unsigned i = 0, e = E->getNumInits(); i != e; ++i)
2211363ff23cfddb51abe4ee4212a6dd3c9b534fcc8bChris Lattner      if (Visit(E->getInit(i))) return true;
22128cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    if (const Expr *filler = E->getArrayFiller())
22134423ac0282acb8ba801eb05b38712438dc0c1e3eArgyrios Kyrtzidis      return Visit(filler);
2214363ff23cfddb51abe4ee4212a6dd3c9b534fcc8bChris Lattner    return false;
2215363ff23cfddb51abe4ee4212a6dd3c9b534fcc8bChris Lattner  }
2216ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor
22178cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitSizeOfPackExpr(const SizeOfPackExpr *) { return false; }
2218c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump};
2219c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump
222056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCallclass OpaqueValueEvaluation {
222156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  EvalInfo &info;
222256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  OpaqueValueExpr *opaqueValue;
222356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
222456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCallpublic:
222556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  OpaqueValueEvaluation(EvalInfo &info, OpaqueValueExpr *opaqueValue,
222656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall                        Expr *value)
222756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    : info(info), opaqueValue(opaqueValue) {
222856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
222956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    // If evaluation fails, fail immediately.
22301e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    if (!Evaluate(info.OpaqueValues[opaqueValue], info, value)) {
223156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      this->opaqueValue = 0;
223256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      return;
223356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    }
223456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  }
223556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
223656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  bool hasError() const { return opaqueValue == 0; }
223756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
223856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  ~OpaqueValueEvaluation() {
22391e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    // FIXME: This will not work for recursive constexpr functions using opaque
22401e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    // values. Restore the former value.
224156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    if (opaqueValue) info.OpaqueValues.erase(opaqueValue);
224256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  }
224356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall};
224456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
2245c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump} // end anonymous namespace
2246c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump
22474efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman//===----------------------------------------------------------------------===//
22488cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne// Generic Evaluation
22498cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne//===----------------------------------------------------------------------===//
22508cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournenamespace {
22518cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
2252f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith// FIXME: RetTy is always bool. Remove it.
2253f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithtemplate <class Derived, typename RetTy=bool>
22548cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourneclass ExprEvaluatorBase
22558cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  : public ConstStmtVisitor<Derived, RetTy> {
22568cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourneprivate:
225747a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith  RetTy DerivedSuccess(const CCValue &V, const Expr *E) {
22588cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    return static_cast<Derived*>(this)->Success(V, E);
22598cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  }
226051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  RetTy DerivedZeroInitialization(const Expr *E) {
226151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return static_cast<Derived*>(this)->ZeroInitialization(E);
2262f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  }
22638cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
22648cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourneprotected:
22658cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  EvalInfo &Info;
22668cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  typedef ConstStmtVisitor<Derived, RetTy> StmtVisitorTy;
22678cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  typedef ExprEvaluatorBase ExprEvaluatorBaseTy;
22688cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
2269dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith  OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) {
2270d5093420308784e31071c8cd723a58d74159f393Richard Smith    return Info.CCEDiag(E->getExprLoc(), D);
2271f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
2272f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
2273f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  /// Report an evaluation error. This should only be called when an error is
2274f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  /// first discovered. When propagating an error, just return false.
2275f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  bool Error(const Expr *E, diag::kind D) {
2276dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    Info.Diag(E->getExprLoc(), D);
2277f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return false;
2278f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
2279f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  bool Error(const Expr *E) {
2280f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E, diag::note_invalid_subexpr_in_const_expr);
2281f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
2282f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
228351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  RetTy ZeroInitialization(const Expr *E) { return Error(E); }
2284f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith
22858cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournepublic:
22868cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {}
22878cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
22888cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitStmt(const Stmt *) {
2289b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Expression evaluator should not be called on stmts");
22908cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  }
22918cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitExpr(const Expr *E) {
2292f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
22938cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  }
22948cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
22958cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitParenExpr(const ParenExpr *E)
22968cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    { return StmtVisitorTy::Visit(E->getSubExpr()); }
22978cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitUnaryExtension(const UnaryOperator *E)
22988cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    { return StmtVisitorTy::Visit(E->getSubExpr()); }
22998cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitUnaryPlus(const UnaryOperator *E)
23008cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    { return StmtVisitorTy::Visit(E->getSubExpr()); }
23018cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitChooseExpr(const ChooseExpr *E)
23028cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    { return StmtVisitorTy::Visit(E->getChosenSubExpr(Info.Ctx)); }
23038cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitGenericSelectionExpr(const GenericSelectionExpr *E)
23048cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    { return StmtVisitorTy::Visit(E->getResultExpr()); }
230591a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall  RetTy VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E)
230691a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall    { return StmtVisitorTy::Visit(E->getReplacement()); }
23073d75ca836205856077c18e30e9447accbd85f751Richard Smith  RetTy VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E)
23083d75ca836205856077c18e30e9447accbd85f751Richard Smith    { return StmtVisitorTy::Visit(E->getExpr()); }
2309bc6abe93a5d6b1305411f8b6f54c2caa686ddc69Richard Smith  // We cannot create any objects for which cleanups are required, so there is
2310bc6abe93a5d6b1305411f8b6f54c2caa686ddc69Richard Smith  // nothing to do here; all cleanups must come from unevaluated subexpressions.
2311bc6abe93a5d6b1305411f8b6f54c2caa686ddc69Richard Smith  RetTy VisitExprWithCleanups(const ExprWithCleanups *E)
2312bc6abe93a5d6b1305411f8b6f54c2caa686ddc69Richard Smith    { return StmtVisitorTy::Visit(E->getSubExpr()); }
23138cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
2314c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith  RetTy VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E) {
2315c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith    CCEDiag(E, diag::note_constexpr_invalid_cast) << 0;
2316c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith    return static_cast<Derived*>(this)->VisitCastExpr(E);
2317c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith  }
2318c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith  RetTy VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) {
2319c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith    CCEDiag(E, diag::note_constexpr_invalid_cast) << 1;
2320c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith    return static_cast<Derived*>(this)->VisitCastExpr(E);
2321c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith  }
2322c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith
2323e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  RetTy VisitBinaryOperator(const BinaryOperator *E) {
2324e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    switch (E->getOpcode()) {
2325e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    default:
2326f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E);
2327e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2328e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    case BO_Comma:
2329e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      VisitIgnoredValue(E->getLHS());
2330e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return StmtVisitorTy::Visit(E->getRHS());
2331e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2332e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    case BO_PtrMemD:
2333e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    case BO_PtrMemI: {
2334e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      LValue Obj;
2335e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!HandleMemberPointerAccess(Info, E, Obj))
2336e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
2337e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      CCValue Result;
2338f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      if (!HandleLValueToRValueConversion(Info, E, E->getType(), Obj, Result))
2339e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
2340e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return DerivedSuccess(Result, E);
2341e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
2342e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
2343e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
2344e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
23458cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) {
23468cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    OpaqueValueEvaluation opaque(Info, E->getOpaqueValue(), E->getCommon());
23478cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    if (opaque.hasError())
2348f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return false;
23498cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
23508cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    bool cond;
2351c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    if (!EvaluateAsBooleanCondition(E->getCond(), cond, Info))
2352f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return false;
23538cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
23548cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    return StmtVisitorTy::Visit(cond ? E->getTrueExpr() : E->getFalseExpr());
23558cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  }
23568cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
23578cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitConditionalOperator(const ConditionalOperator *E) {
2358f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    bool IsBcpCall = false;
2359f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    // If the condition (ignoring parens) is a __builtin_constant_p call,
2360f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    // the result is a constant expression if it can be folded without
2361f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    // side-effects. This is an important GNU extension. See GCC PR38377
2362f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    // for discussion.
2363f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    if (const CallExpr *CallCE =
2364f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          dyn_cast<CallExpr>(E->getCond()->IgnoreParenCasts()))
2365f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p)
2366f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        IsBcpCall = true;
2367f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
2368f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    // Always assume __builtin_constant_p(...) ? ... : ... is a potential
2369f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    // constant expression; we can't check whether it's potentially foldable.
2370f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    if (Info.CheckingPotentialConstantExpression && IsBcpCall)
2371f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      return false;
2372f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
2373f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    FoldConstant Fold(Info);
2374f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
23758cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    bool BoolResult;
2376c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info))
2377f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return false;
23788cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
2379c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
2380f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    if (!StmtVisitorTy::Visit(EvalExpr))
2381f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      return false;
2382f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
2383f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    if (IsBcpCall)
2384f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      Fold.Fold(Info);
2385f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
2386f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith    return true;
23878cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  }
23888cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
23898cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  RetTy VisitOpaqueValueExpr(const OpaqueValueExpr *E) {
239047a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    const CCValue *Value = Info.getOpaqueValue(E);
239142786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis    if (!Value) {
239242786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis      const Expr *Source = E->getSourceExpr();
239342786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis      if (!Source)
2394f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(E);
239542786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis      if (Source == E) { // sanity checking.
239642786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis        assert(0 && "OpaqueValueExpr recursively refers to itself");
2397f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(E);
239842786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis      }
239942786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis      return StmtVisitorTy::Visit(Source);
240042786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis    }
240147a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    return DerivedSuccess(*Value, E);
24028cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  }
2403f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith
2404d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  RetTy VisitCallExpr(const CallExpr *E) {
2405e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    const Expr *Callee = E->getCallee()->IgnoreParens();
2406d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    QualType CalleeType = Callee->getType();
2407d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
24086142ca7790aa09a6e13592b70f142cc4bbcadcaeDevang Patel    const FunctionDecl *FD = 0;
240959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    LValue *This = 0, ThisVal;
241059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs());
241159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
241259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    // Extract function decl and 'this' pointer from the callee.
241359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    if (CalleeType->isSpecificBuiltinType(BuiltinType::BoundMember)) {
2414f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      const ValueDecl *Member = 0;
2415e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (const MemberExpr *ME = dyn_cast<MemberExpr>(Callee)) {
2416e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        // Explicit bound member calls, such as x.f() or p->g();
2417e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        if (!EvaluateObjectArgument(Info, ME->getBase(), ThisVal))
2418f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith          return false;
2419f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        Member = ME->getMemberDecl();
2420e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        This = &ThisVal;
2421e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(Callee)) {
2422e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        // Indirect bound member calls ('.*' or '->*').
2423f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        Member = HandleMemberPointerAccess(Info, BE, ThisVal, false);
2424f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        if (!Member) return false;
2425e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        This = &ThisVal;
2426e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      } else
2427f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(Callee);
2428f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
2429f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      FD = dyn_cast<FunctionDecl>(Member);
2430f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      if (!FD)
2431f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(Callee);
243259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    } else if (CalleeType->isFunctionPointerType()) {
2433b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      LValue Call;
2434b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      if (!EvaluatePointer(Callee, Call, Info))
2435f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return false;
243659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
2437b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      if (!Call.getLValueOffset().isZero())
2438f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(Callee);
24391bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith      FD = dyn_cast_or_null<FunctionDecl>(
24401bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith                             Call.getLValueBase().dyn_cast<const ValueDecl*>());
244159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      if (!FD)
2442f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(Callee);
244359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
244459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      // Overloaded operator calls to member functions are represented as normal
244559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      // calls with '*this' as the first argument.
244659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
244759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      if (MD && !MD->isStatic()) {
2448f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        // FIXME: When selecting an implicit conversion for an overloaded
2449f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        // operator delete, we sometimes try to evaluate calls to conversion
2450f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        // operators without a 'this' parameter!
2451f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        if (Args.empty())
2452f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith          return Error(E);
2453f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
245459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith        if (!EvaluateObjectArgument(Info, Args[0], ThisVal))
245559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith          return false;
245659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith        This = &ThisVal;
245759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith        Args = Args.slice(1);
245859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      }
2459d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
246059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      // Don't call function pointers which have been cast to some other type.
246159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      if (!Info.Ctx.hasSameType(CalleeType->getPointeeType(), FD->getType()))
2462f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(E);
246359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    } else
2464f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E);
2465d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
2466b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith    if (This && !This->checkSubobject(Info, E, CSK_This))
2467b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith      return false;
2468b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith
2469c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    const FunctionDecl *Definition = 0;
2470d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    Stmt *Body = FD->getBody(Definition);
247169c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith    APValue Result;
2472d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
2473c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition) ||
2474745f5147e065900267c85a5568785a1991d4838fRichard Smith        !HandleFunctionCall(E->getExprLoc(), Definition, This, Args, Body,
2475745f5147e065900267c85a5568785a1991d4838fRichard Smith                            Info, Result))
2476f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return false;
2477d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
2478b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    return DerivedSuccess(CCValue(Info.Ctx, Result, CCValue::GlobalValue()), E);
2479d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  }
2480d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
2481c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  RetTy VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
2482c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return StmtVisitorTy::Visit(E->getInitializer());
2483c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  }
2484f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  RetTy VisitInitListExpr(const InitListExpr *E) {
248571523d6c41e1599fc42f420d02dd2895fd8f65d4Eli Friedman    if (E->getNumInits() == 0)
248671523d6c41e1599fc42f420d02dd2895fd8f65d4Eli Friedman      return DerivedZeroInitialization(E);
248771523d6c41e1599fc42f420d02dd2895fd8f65d4Eli Friedman    if (E->getNumInits() == 1)
248871523d6c41e1599fc42f420d02dd2895fd8f65d4Eli Friedman      return StmtVisitorTy::Visit(E->getInit(0));
2489f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
2490f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  }
2491f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  RetTy VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
249251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return DerivedZeroInitialization(E);
2493f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  }
2494f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  RetTy VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) {
249551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return DerivedZeroInitialization(E);
2496f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  }
2497e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  RetTy VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) {
249851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return DerivedZeroInitialization(E);
2499e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
2500f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith
2501180f47959a066795cc0f409433023af448bb0328Richard Smith  /// A member expression where the object is a prvalue is itself a prvalue.
2502180f47959a066795cc0f409433023af448bb0328Richard Smith  RetTy VisitMemberExpr(const MemberExpr *E) {
2503180f47959a066795cc0f409433023af448bb0328Richard Smith    assert(!E->isArrow() && "missing call to bound member function?");
2504180f47959a066795cc0f409433023af448bb0328Richard Smith
2505180f47959a066795cc0f409433023af448bb0328Richard Smith    CCValue Val;
2506180f47959a066795cc0f409433023af448bb0328Richard Smith    if (!Evaluate(Val, Info, E->getBase()))
2507180f47959a066795cc0f409433023af448bb0328Richard Smith      return false;
2508180f47959a066795cc0f409433023af448bb0328Richard Smith
2509180f47959a066795cc0f409433023af448bb0328Richard Smith    QualType BaseTy = E->getBase()->getType();
2510180f47959a066795cc0f409433023af448bb0328Richard Smith
2511180f47959a066795cc0f409433023af448bb0328Richard Smith    const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
2512f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!FD) return Error(E);
2513180f47959a066795cc0f409433023af448bb0328Richard Smith    assert(!FD->getType()->isReferenceType() && "prvalue reference?");
2514180f47959a066795cc0f409433023af448bb0328Richard Smith    assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() ==
2515180f47959a066795cc0f409433023af448bb0328Richard Smith           FD->getParent()->getCanonicalDecl() && "record / field mismatch");
2516180f47959a066795cc0f409433023af448bb0328Richard Smith
2517b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    SubobjectDesignator Designator(BaseTy);
2518b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    Designator.addDeclUnchecked(FD);
2519180f47959a066795cc0f409433023af448bb0328Richard Smith
2520f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return ExtractSubobject(Info, E, Val, BaseTy, Designator, E->getType()) &&
2521180f47959a066795cc0f409433023af448bb0328Richard Smith           DerivedSuccess(Val, E);
2522180f47959a066795cc0f409433023af448bb0328Richard Smith  }
2523180f47959a066795cc0f409433023af448bb0328Richard Smith
2524c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  RetTy VisitCastExpr(const CastExpr *E) {
2525c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    switch (E->getCastKind()) {
2526c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    default:
2527c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith      break;
2528c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
25297a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall    case CK_AtomicToNonAtomic:
25307a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall    case CK_NonAtomicToAtomic:
2531c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    case CK_NoOp:
25327d580a4e9e47dffc3c17aa2b957ac57ca3c4e451Richard Smith    case CK_UserDefinedConversion:
2533c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith      return StmtVisitorTy::Visit(E->getSubExpr());
2534c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
2535c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    case CK_LValueToRValue: {
2536c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith      LValue LVal;
2537f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      if (!EvaluateLValue(E->getSubExpr(), LVal, Info))
2538f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return false;
2539f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      CCValue RVal;
25409ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith      // Note, we use the subexpression's type in order to retain cv-qualifiers.
25419ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith      if (!HandleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(),
25429ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith                                          LVal, RVal))
2543f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return false;
2544f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return DerivedSuccess(RVal, E);
2545c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    }
2546c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    }
2547c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
2548f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
2549c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  }
2550c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
25518327fad71da34492d82c532f42a58cb4baff81a3Richard Smith  /// Visit a value which is evaluated, but whose value is ignored.
25528327fad71da34492d82c532f42a58cb4baff81a3Richard Smith  void VisitIgnoredValue(const Expr *E) {
255347a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    CCValue Scratch;
25548327fad71da34492d82c532f42a58cb4baff81a3Richard Smith    if (!Evaluate(Scratch, Info, E))
25558327fad71da34492d82c532f42a58cb4baff81a3Richard Smith      Info.EvalStatus.HasSideEffects = true;
25568327fad71da34492d82c532f42a58cb4baff81a3Richard Smith  }
25578cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne};
25588cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
25598cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne}
25608cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
25618cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne//===----------------------------------------------------------------------===//
2562e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// Common base class for lvalue and temporary evaluation.
2563e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===//
2564e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithnamespace {
2565e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithtemplate<class Derived>
2566e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithclass LValueExprEvaluatorBase
2567e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  : public ExprEvaluatorBase<Derived, bool> {
2568e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithprotected:
2569e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  LValue &Result;
2570e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  typedef LValueExprEvaluatorBase LValueExprEvaluatorBaseTy;
2571e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  typedef ExprEvaluatorBase<Derived, bool> ExprEvaluatorBaseTy;
2572e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2573e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool Success(APValue::LValueBase B) {
2574e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    Result.set(B);
2575e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
2576e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
2577e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2578e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithpublic:
2579e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  LValueExprEvaluatorBase(EvalInfo &Info, LValue &Result) :
2580e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    ExprEvaluatorBaseTy(Info), Result(Result) {}
2581e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2582e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool Success(const CCValue &V, const Expr *E) {
2583e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    Result.setFrom(V);
2584e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
2585e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
2586e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2587e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitMemberExpr(const MemberExpr *E) {
2588e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // Handle non-static data members.
2589e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    QualType BaseTy;
2590e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (E->isArrow()) {
2591e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!EvaluatePointer(E->getBase(), Result, this->Info))
2592e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
2593e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      BaseTy = E->getBase()->getType()->getAs<PointerType>()->getPointeeType();
2594c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    } else if (E->getBase()->isRValue()) {
2595af2c7a194592401394233b7cbcdd3cfd0a7a38ddRichard Smith      assert(E->getBase()->getType()->isRecordType());
2596c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      if (!EvaluateTemporary(E->getBase(), Result, this->Info))
2597c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith        return false;
2598c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      BaseTy = E->getBase()->getType();
2599e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    } else {
2600e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!this->Visit(E->getBase()))
2601e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
2602e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      BaseTy = E->getBase()->getType();
2603e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
2604e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2605d9b02e726262e4009dda830998bb934172ac0020Richard Smith    const ValueDecl *MD = E->getMemberDecl();
2606d9b02e726262e4009dda830998bb934172ac0020Richard Smith    if (const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
2607d9b02e726262e4009dda830998bb934172ac0020Richard Smith      assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() ==
2608d9b02e726262e4009dda830998bb934172ac0020Richard Smith             FD->getParent()->getCanonicalDecl() && "record / field mismatch");
2609d9b02e726262e4009dda830998bb934172ac0020Richard Smith      (void)BaseTy;
2610d9b02e726262e4009dda830998bb934172ac0020Richard Smith      HandleLValueMember(this->Info, E, Result, FD);
2611d9b02e726262e4009dda830998bb934172ac0020Richard Smith    } else if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(MD)) {
2612d9b02e726262e4009dda830998bb934172ac0020Richard Smith      HandleLValueIndirectMember(this->Info, E, Result, IFD);
2613d9b02e726262e4009dda830998bb934172ac0020Richard Smith    } else
2614d9b02e726262e4009dda830998bb934172ac0020Richard Smith      return this->Error(E);
2615e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2616d9b02e726262e4009dda830998bb934172ac0020Richard Smith    if (MD->getType()->isReferenceType()) {
2617e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      CCValue RefValue;
2618d9b02e726262e4009dda830998bb934172ac0020Richard Smith      if (!HandleLValueToRValueConversion(this->Info, E, MD->getType(), Result,
2619e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                                          RefValue))
2620e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
2621e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return Success(RefValue, E);
2622e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
2623e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
2624e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
2625e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2626e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitBinaryOperator(const BinaryOperator *E) {
2627e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    switch (E->getOpcode()) {
2628e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    default:
2629e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
2630e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2631e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    case BO_PtrMemD:
2632e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    case BO_PtrMemI:
2633e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return HandleMemberPointerAccess(this->Info, E, Result);
2634e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
2635e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
2636e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2637e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitCastExpr(const CastExpr *E) {
2638e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    switch (E->getCastKind()) {
2639e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    default:
2640e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return ExprEvaluatorBaseTy::VisitCastExpr(E);
2641e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2642e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    case CK_DerivedToBase:
2643e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    case CK_UncheckedDerivedToBase: {
2644e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!this->Visit(E->getSubExpr()))
2645e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
2646e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2647e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      // Now figure out the necessary offset to add to the base LV to get from
2648e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      // the derived class to the base class.
2649e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      QualType Type = E->getSubExpr()->getType();
2650e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2651e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      for (CastExpr::path_const_iterator PathI = E->path_begin(),
2652e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith           PathE = E->path_end(); PathI != PathE; ++PathI) {
2653b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith        if (!HandleLValueBase(this->Info, E, Result, Type->getAsCXXRecordDecl(),
2654e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                              *PathI))
2655e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith          return false;
2656e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        Type = (*PathI)->getType();
2657e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      }
2658e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2659e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return true;
2660e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
2661e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
2662e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
2663e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith};
2664e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}
2665e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2666e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===//
26674efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman// LValue Evaluation
2668c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//
2669c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// This is used for evaluating lvalues (in C and C++), xvalues (in C++11),
2670c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// function designators (in C), decl references to void objects (in C), and
2671c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// temporaries (if building with -Wno-address-of-temporary).
2672c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//
2673c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// LValue evaluation produces values comprising a base expression of one of the
2674c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// following types:
26751bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// - Declarations
26761bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith//  * VarDecl
26771bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith//  * FunctionDecl
26781bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// - Literals
2679c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//  * CompoundLiteralExpr in C
2680c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//  * StringLiteral
268147d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith//  * CXXTypeidExpr
2682c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//  * PredefinedExpr
2683180f47959a066795cc0f409433023af448bb0328Richard Smith//  * ObjCStringLiteralExpr
2684c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//  * ObjCEncodeExpr
2685c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//  * AddrLabelExpr
2686c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//  * BlockExpr
2687c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//  * CallExpr for a MakeStringConstant builtin
26881bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// - Locals and temporaries
26891bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith//  * Any Expr, with a Frame indicating the function in which the temporary was
26901bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith//    evaluated.
26911bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// plus an offset in bytes.
26924efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman//===----------------------------------------------------------------------===//
26934efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedmannamespace {
2694770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass LValueExprEvaluator
2695e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  : public LValueExprEvaluatorBase<LValueExprEvaluator> {
26964efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedmanpublic:
2697e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  LValueExprEvaluator(EvalInfo &Info, LValue &Result) :
2698e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    LValueExprEvaluatorBaseTy(Info, Result) {}
26991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2700c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  bool VisitVarDecl(const Expr *E, const VarDecl *VD);
2701c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
27028cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitDeclRefExpr(const DeclRefExpr *E);
27038cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(E); }
2704bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith  bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E);
27058cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
27068cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitMemberExpr(const MemberExpr *E);
27078cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitStringLiteral(const StringLiteral *E) { return Success(E); }
27088cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(E); }
270947d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith  bool VisitCXXTypeidExpr(const CXXTypeidExpr *E);
27108cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E);
27118cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitUnaryDeref(const UnaryOperator *E);
27128cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
27138cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCastExpr(const CastExpr *E) {
271426bc220377705292a0519a71d3ea3aef68fcfec6Anders Carlsson    switch (E->getCastKind()) {
271526bc220377705292a0519a71d3ea3aef68fcfec6Anders Carlsson    default:
2716e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return LValueExprEvaluatorBaseTy::VisitCastExpr(E);
271726bc220377705292a0519a71d3ea3aef68fcfec6Anders Carlsson
2718db924224b51b153f24fbe492102d4edebcbbb7f4Eli Friedman    case CK_LValueBitCast:
2719c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith      this->CCEDiag(E, diag::note_constexpr_invalid_cast) << 2;
27200a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      if (!Visit(E->getSubExpr()))
27210a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith        return false;
27220a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      Result.Designator.setInvalid();
27230a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      return true;
2724db924224b51b153f24fbe492102d4edebcbbb7f4Eli Friedman
2725e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    case CK_BaseToDerived:
2726180f47959a066795cc0f409433023af448bb0328Richard Smith      if (!Visit(E->getSubExpr()))
2727180f47959a066795cc0f409433023af448bb0328Richard Smith        return false;
2728e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return HandleBaseToDerivedCast(Info, E, Result);
272926bc220377705292a0519a71d3ea3aef68fcfec6Anders Carlsson    }
273026bc220377705292a0519a71d3ea3aef68fcfec6Anders Carlsson  }
2731cea8d966f826554f0679595e9371e314e8dbc1cfSebastian Redl
2732ba98d6bb414861965a1f22628494ea046785ecd4Eli Friedman  // FIXME: Missing: __real__, __imag__
27338cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
27344efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman};
27354efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman} // end anonymous namespace
27364efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
2737c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// Evaluate an expression as an lvalue. This can be legitimately called on
2738c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// expressions which are not glvalues, in a few cases:
2739c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith///  * function designators in C,
2740c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith///  * "extern void" objects,
2741c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith///  * temporaries, if building with -Wno-address-of-temporary.
2742efdb83e26f9a1fd2566afe54461216cd84814d42John McCallstatic bool EvaluateLValue(const Expr* E, LValue& Result, EvalInfo &Info) {
2743c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  assert((E->isGLValue() || E->getType()->isFunctionType() ||
2744c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith          E->getType()->isVoidType() || isa<CXXTemporaryObjectExpr>(E)) &&
2745c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith         "can't evaluate expression as an lvalue");
27468cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  return LValueExprEvaluator(Info, Result).Visit(E);
27474efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman}
27484efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
27498cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
27501bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl()))
27511bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    return Success(FD);
27521bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  if (const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl()))
2753c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return VisitVarDecl(E, VD);
2754c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  return Error(E);
2755c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith}
2756436c8898cd1c93c5bacd3fcc4ac586bc5cd77062Richard Smith
2757c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smithbool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) {
2758177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith  if (!VD->getType()->isReferenceType()) {
2759177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith    if (isa<ParmVarDecl>(VD)) {
27601bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith      Result.set(VD, Info.CurrentCall);
2761177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith      return true;
2762177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith    }
27631bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    return Success(VD);
2764177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith  }
276550c39ea4858265f3f5f42a0c624557ce2281936bEli Friedman
276647a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith  CCValue V;
2767f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (!EvaluateVarDeclInit(Info, E, VD, Info.CurrentCall, V))
2768f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return false;
2769f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  return Success(V, E);
277035873c49adad211ff466e34342a52665742794f5Anders Carlsson}
277135873c49adad211ff466e34342a52665742794f5Anders Carlsson
2772bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smithbool LValueExprEvaluator::VisitMaterializeTemporaryExpr(
2773bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith    const MaterializeTemporaryExpr *E) {
2774e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (E->GetTemporaryExpr()->isRValue()) {
2775af2c7a194592401394233b7cbcdd3cfd0a7a38ddRichard Smith    if (E->getType()->isRecordType())
2776e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return EvaluateTemporary(E->GetTemporaryExpr(), Result, Info);
2777e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2778e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    Result.set(E, Info.CurrentCall);
2779e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return EvaluateConstantExpression(Info.CurrentCall->Temporaries[E], Info,
2780e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                                      Result, E->GetTemporaryExpr());
2781e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
2782e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
2783e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // Materialization of an lvalue temporary occurs when we need to force a copy
2784e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // (for instance, if it's a bitfield).
2785e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // FIXME: The AST should contain an lvalue-to-rvalue node for such cases.
2786e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (!Visit(E->GetTemporaryExpr()))
2787e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return false;
2788f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (!HandleLValueToRValueConversion(Info, E, E->getType(), Result,
2789e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                                      Info.CurrentCall->Temporaries[E]))
2790e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return false;
27911bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  Result.set(E, Info.CurrentCall);
2792e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  return true;
2793bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith}
2794bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith
27958cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool
27968cad3046be06ea73ff8892d947697a21d7a440d3Peter CollingbourneLValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
2797c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?");
2798c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  // Defer visiting the literal until the lvalue-to-rvalue conversion. We can
2799c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  // only see this when folding in C, so there's no standard to follow here.
2800efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  return Success(E);
28014efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman}
28024efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
280347d2145675099893d702be4bc06bd9f26d8ddd13Richard Smithbool LValueExprEvaluator::VisitCXXTypeidExpr(const CXXTypeidExpr *E) {
280447d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith  if (E->isTypeOperand())
280547d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith    return Success(E);
280647d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith  CXXRecordDecl *RD = E->getExprOperand()->getType()->getAsCXXRecordDecl();
280747d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith  if (RD && RD->isPolymorphic()) {
280847d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith    Info.Diag(E->getExprLoc(), diag::note_constexpr_typeid_polymorphic)
280947d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith      << E->getExprOperand()->getType()
281047d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith      << E->getExprOperand()->getSourceRange();
281147d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith    return false;
281247d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith  }
281347d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith  return Success(E);
281447d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith}
281547d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith
28168cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) {
2817c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  // Handle static data members.
2818c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  if (const VarDecl *VD = dyn_cast<VarDecl>(E->getMemberDecl())) {
2819c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    VisitIgnoredValue(E->getBase());
2820c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return VisitVarDecl(E, VD);
2821c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  }
2822c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
2823d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  // Handle static member functions.
2824d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) {
2825d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    if (MD->isStatic()) {
2826d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith      VisitIgnoredValue(E->getBase());
28271bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith      return Success(MD);
2828d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith    }
2829d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith  }
2830d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith
2831180f47959a066795cc0f409433023af448bb0328Richard Smith  // Handle non-static data members.
2832e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  return LValueExprEvaluatorBaseTy::VisitMemberExpr(E);
28334efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman}
28344efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
28358cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) {
2836c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  // FIXME: Deal with vectors as array subscript bases.
2837c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  if (E->getBase()->getType()->isVectorType())
2838f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
2839c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
28403068d117951a8df54bae9db039b56201ab10962bAnders Carlsson  if (!EvaluatePointer(E->getBase(), Result, Info))
2841efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    return false;
28421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28433068d117951a8df54bae9db039b56201ab10962bAnders Carlsson  APSInt Index;
28443068d117951a8df54bae9db039b56201ab10962bAnders Carlsson  if (!EvaluateInteger(E->getIdx(), Index, Info))
2845efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    return false;
2846180f47959a066795cc0f409433023af448bb0328Richard Smith  int64_t IndexValue
2847180f47959a066795cc0f409433023af448bb0328Richard Smith    = Index.isSigned() ? Index.getSExtValue()
2848180f47959a066795cc0f409433023af448bb0328Richard Smith                       : static_cast<int64_t>(Index.getZExtValue());
28493068d117951a8df54bae9db039b56201ab10962bAnders Carlsson
2850b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  return HandleLValueArrayAdjustment(Info, E, Result, E->getType(), IndexValue);
28513068d117951a8df54bae9db039b56201ab10962bAnders Carlsson}
28524efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
28538cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) {
2854efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  return EvaluatePointer(E->getSubExpr(), Result, Info);
2855e8761c8fe2ee6b628104a0885f49fd3c21c08a4fEli Friedman}
2856e8761c8fe2ee6b628104a0885f49fd3c21c08a4fEli Friedman
28574efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman//===----------------------------------------------------------------------===//
2858f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner// Pointer Evaluation
2859f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===//
2860f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
2861c754aa62643e66ab967ca32ae8b0b3fc419bba25Anders Carlssonnamespace {
2862770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass PointerExprEvaluator
28638cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  : public ExprEvaluatorBase<PointerExprEvaluator, bool> {
2864efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  LValue &Result;
2865efdb83e26f9a1fd2566afe54461216cd84814d42John McCall
28668cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool Success(const Expr *E) {
28671bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    Result.set(E);
2868efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    return true;
2869efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  }
28702bad1687fe6f00e10767a691a33b070b151902b6Anders Carlssonpublic:
28711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2872efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  PointerExprEvaluator(EvalInfo &info, LValue &Result)
28738cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    : ExprEvaluatorBaseTy(info), Result(Result) {}
2874f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
287547a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith  bool Success(const CCValue &V, const Expr *E) {
28768cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    Result.setFrom(V);
28778cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    return true;
28782bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson  }
287951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  bool ZeroInitialization(const Expr *E) {
2880f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith    return Success((Expr*)0);
2881f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  }
28822bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson
2883efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  bool VisitBinaryOperator(const BinaryOperator *E);
28848cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCastExpr(const CastExpr* E);
2885efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  bool VisitUnaryAddrOf(const UnaryOperator *E);
28868cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitObjCStringLiteral(const ObjCStringLiteral *E)
2887efdb83e26f9a1fd2566afe54461216cd84814d42John McCall      { return Success(E); }
28888cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitAddrLabelExpr(const AddrLabelExpr *E)
2889efdb83e26f9a1fd2566afe54461216cd84814d42John McCall      { return Success(E); }
28908cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCallExpr(const CallExpr *E);
28918cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitBlockExpr(const BlockExpr *E) {
2892469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall    if (!E->getBlockDecl()->hasCaptures())
2893efdb83e26f9a1fd2566afe54461216cd84814d42John McCall      return Success(E);
2894f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
2895b83d287bc7f47d36fb0751a481e2ef9308b37252Mike Stump  }
2896180f47959a066795cc0f409433023af448bb0328Richard Smith  bool VisitCXXThisExpr(const CXXThisExpr *E) {
2897180f47959a066795cc0f409433023af448bb0328Richard Smith    if (!Info.CurrentCall->This)
2898f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E);
2899180f47959a066795cc0f409433023af448bb0328Richard Smith    Result = *Info.CurrentCall->This;
2900180f47959a066795cc0f409433023af448bb0328Richard Smith    return true;
2901180f47959a066795cc0f409433023af448bb0328Richard Smith  }
290256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
2903ba98d6bb414861965a1f22628494ea046785ecd4Eli Friedman  // FIXME: Missing: @protocol, @selector
29042bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson};
2905f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner} // end anonymous namespace
29062bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson
2907efdb83e26f9a1fd2566afe54461216cd84814d42John McCallstatic bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info) {
2908c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  assert(E->isRValue() && E->getType()->hasPointerRepresentation());
29098cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  return PointerExprEvaluator(Info, Result).Visit(E);
2910f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner}
2911650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson
2912efdb83e26f9a1fd2566afe54461216cd84814d42John McCallbool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
29132de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  if (E->getOpcode() != BO_Add &&
29142de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      E->getOpcode() != BO_Sub)
2915e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
29161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2917650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson  const Expr *PExp = E->getLHS();
2918650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson  const Expr *IExp = E->getRHS();
2919650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson  if (IExp->getType()->isPointerType())
2920f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner    std::swap(PExp, IExp);
29211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2922745f5147e065900267c85a5568785a1991d4838fRichard Smith  bool EvalPtrOK = EvaluatePointer(PExp, Result, Info);
2923745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!EvalPtrOK && !Info.keepEvaluatingAfterFailure())
2924efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    return false;
29251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2926efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  llvm::APSInt Offset;
2927745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!EvaluateInteger(IExp, Offset, Info) || !EvalPtrOK)
2928efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    return false;
2929efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  int64_t AdditionalOffset
2930efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    = Offset.isSigned() ? Offset.getSExtValue()
2931efdb83e26f9a1fd2566afe54461216cd84814d42John McCall                        : static_cast<int64_t>(Offset.getZExtValue());
29320a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith  if (E->getOpcode() == BO_Sub)
29330a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    AdditionalOffset = -AdditionalOffset;
2934650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson
2935180f47959a066795cc0f409433023af448bb0328Richard Smith  QualType Pointee = PExp->getType()->getAs<PointerType>()->getPointeeType();
2936b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  return HandleLValueArrayAdjustment(Info, E, Result, Pointee,
2937b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                                     AdditionalOffset);
2938650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson}
29394efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
2940efdb83e26f9a1fd2566afe54461216cd84814d42John McCallbool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
2941efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  return EvaluateLValue(E->getSubExpr(), Result, Info);
29424efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman}
29431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
29448cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
29458cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  const Expr* SubExpr = E->getSubExpr();
2946650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson
294709a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman  switch (E->getCastKind()) {
294809a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman  default:
294909a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman    break;
295009a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman
29512de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_BitCast:
29521d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_CPointerToObjCPointerCast:
29531d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_BlockPointerToObjCPointerCast:
29542de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_AnyPointerToBlockPointerCast:
295528c1ce789322ab99f9b5887015d63ec5f088957aRichard Smith    if (!Visit(SubExpr))
295628c1ce789322ab99f9b5887015d63ec5f088957aRichard Smith      return false;
2957c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith    // Bitcasts to cv void* are static_casts, not reinterpret_casts, so are
2958c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith    // permitted in constant expressions in C++11. Bitcasts from cv void* are
2959c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith    // also static_casts, but we disallow them as a resolution to DR1312.
29604cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith    if (!E->getType()->isVoidPointerType()) {
296128c1ce789322ab99f9b5887015d63ec5f088957aRichard Smith      Result.Designator.setInvalid();
29624cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith      if (SubExpr->getType()->isVoidPointerType())
29634cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith        CCEDiag(E, diag::note_constexpr_invalid_cast)
29644cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith          << 3 << SubExpr->getType();
29654cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith      else
29664cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith        CCEDiag(E, diag::note_constexpr_invalid_cast) << 2;
29674cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith    }
29680a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    return true;
296909a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman
29705c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson  case CK_DerivedToBase:
29715c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson  case CK_UncheckedDerivedToBase: {
297247a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    if (!EvaluatePointer(E->getSubExpr(), Result, Info))
29735c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson      return false;
2974e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (!Result.Base && Result.Offset.isZero())
2975e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return true;
29765c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson
2977180f47959a066795cc0f409433023af448bb0328Richard Smith    // Now figure out the necessary offset to add to the base LV to get from
29785c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson    // the derived class to the base class.
2979180f47959a066795cc0f409433023af448bb0328Richard Smith    QualType Type =
2980180f47959a066795cc0f409433023af448bb0328Richard Smith        E->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType();
29815c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson
2982180f47959a066795cc0f409433023af448bb0328Richard Smith    for (CastExpr::path_const_iterator PathI = E->path_begin(),
29835c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson         PathE = E->path_end(); PathI != PathE; ++PathI) {
2984b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      if (!HandleLValueBase(Info, E, Result, Type->getAsCXXRecordDecl(),
2985b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                            *PathI))
29865c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson        return false;
2987180f47959a066795cc0f409433023af448bb0328Richard Smith      Type = (*PathI)->getType();
29885c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson    }
29895c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson
29905c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson    return true;
29915c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson  }
29925c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson
2993e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  case CK_BaseToDerived:
2994e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (!Visit(E->getSubExpr()))
2995e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return false;
2996e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (!Result.Base && Result.Offset.isZero())
2997e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return true;
2998e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return HandleBaseToDerivedCast(Info, E, Result);
2999e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
300047a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith  case CK_NullToPointer:
300151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return ZeroInitialization(E);
3002404cd1669c3ba138a9ae0a619bd689cce5aae271John McCall
30032de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_IntegralToPointer: {
3004c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith    CCEDiag(E, diag::note_constexpr_invalid_cast) << 2;
3005c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith
300647a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    CCValue Value;
3007efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    if (!EvaluateIntegerOrLValue(SubExpr, Value, Info))
300809a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman      break;
300969ab26a8623141f35e86817cfc6e0fbe7639a40fDaniel Dunbar
3010efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    if (Value.isInt()) {
301147a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith      unsigned Size = Info.Ctx.getTypeSize(E->getType());
301247a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith      uint64_t N = Value.getInt().extOrTrunc(Size).getZExtValue();
30131bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith      Result.Base = (Expr*)0;
301447a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith      Result.Offset = CharUnits::fromQuantity(N);
3015177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith      Result.Frame = 0;
30160a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith      Result.Designator.setInvalid();
3017efdb83e26f9a1fd2566afe54461216cd84814d42John McCall      return true;
3018efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    } else {
3019efdb83e26f9a1fd2566afe54461216cd84814d42John McCall      // Cast is of an lvalue, no need to change value.
302047a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith      Result.setFrom(Value);
3021efdb83e26f9a1fd2566afe54461216cd84814d42John McCall      return true;
3022650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson    }
3023650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson  }
30242de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_ArrayToPointerDecay:
3025e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (SubExpr->isGLValue()) {
3026e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!EvaluateLValue(SubExpr, Result, Info))
3027e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
3028e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    } else {
3029e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      Result.set(SubExpr, Info.CurrentCall);
3030e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!EvaluateConstantExpression(Info.CurrentCall->Temporaries[SubExpr],
3031e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                                      Info, Result, SubExpr))
3032e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith        return false;
3033e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
30340a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    // The result is a pointer to the first element of the array.
3035b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    if (const ConstantArrayType *CAT
3036b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith          = Info.Ctx.getAsConstantArrayType(SubExpr->getType()))
3037b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      Result.addArray(Info, E, CAT);
3038b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    else
3039b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      Result.Designator.setInvalid();
30400a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith    return true;
30416a7c94af983717e2c2d6aebe42cb4737c1c7b9e6Richard Smith
30422de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_FunctionToPointerDecay:
30436a7c94af983717e2c2d6aebe42cb4737c1c7b9e6Richard Smith    return EvaluateLValue(SubExpr, Result, Info);
30444efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman  }
30454efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
3046c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  return ExprEvaluatorBaseTy::VisitCastExpr(E);
30471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
3048650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson
30498cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) {
3050180f47959a066795cc0f409433023af448bb0328Richard Smith  if (IsStringLiteralCall(E))
3051efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    return Success(E);
305256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
30538cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  return ExprEvaluatorBaseTy::VisitCallExpr(E);
30544efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman}
3055f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
3056f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===//
3057e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// Member Pointer Evaluation
3058e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===//
3059e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3060e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithnamespace {
3061e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithclass MemberPointerExprEvaluator
3062e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  : public ExprEvaluatorBase<MemberPointerExprEvaluator, bool> {
3063e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  MemberPtr &Result;
3064e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3065e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool Success(const ValueDecl *D) {
3066e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    Result = MemberPtr(D);
3067e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
3068e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
3069e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithpublic:
3070e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3071e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  MemberPointerExprEvaluator(EvalInfo &Info, MemberPtr &Result)
3072e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    : ExprEvaluatorBaseTy(Info), Result(Result) {}
3073e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3074e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool Success(const CCValue &V, const Expr *E) {
3075e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    Result.setFrom(V);
3076e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
3077e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
307851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  bool ZeroInitialization(const Expr *E) {
3079e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return Success((const ValueDecl*)0);
3080e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
3081e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3082e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitCastExpr(const CastExpr *E);
3083e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitUnaryAddrOf(const UnaryOperator *E);
3084e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith};
3085e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} // end anonymous namespace
3086e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3087e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result,
3088e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                                  EvalInfo &Info) {
3089e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  assert(E->isRValue() && E->getType()->isMemberPointerType());
3090e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  return MemberPointerExprEvaluator(Info, Result).Visit(E);
3091e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}
3092e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3093e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithbool MemberPointerExprEvaluator::VisitCastExpr(const CastExpr *E) {
3094e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  switch (E->getCastKind()) {
3095e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  default:
3096e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return ExprEvaluatorBaseTy::VisitCastExpr(E);
3097e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3098e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  case CK_NullToMemberPointer:
309951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return ZeroInitialization(E);
3100e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3101e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  case CK_BaseToDerivedMemberPointer: {
3102e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (!Visit(E->getSubExpr()))
3103e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return false;
3104e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (E->path_empty())
3105e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return true;
3106e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // Base-to-derived member pointer casts store the path in derived-to-base
3107e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // order, so iterate backwards. The CXXBaseSpecifier also provides us with
3108e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // the wrong end of the derived->base arc, so stagger the path by one class.
3109e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter;
3110e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin());
3111e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith         PathI != PathE; ++PathI) {
3112e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      assert(!(*PathI)->isVirtual() && "memptr cast through vbase");
3113e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      const CXXRecordDecl *Derived = (*PathI)->getType()->getAsCXXRecordDecl();
3114e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!Result.castToDerived(Derived))
3115f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(E);
3116e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
3117e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    const Type *FinalTy = E->getType()->castAs<MemberPointerType>()->getClass();
3118e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (!Result.castToDerived(FinalTy->getAsCXXRecordDecl()))
3119f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E);
3120e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
3121e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
3122e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3123e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  case CK_DerivedToBaseMemberPointer:
3124e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (!Visit(E->getSubExpr()))
3125e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return false;
3126e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    for (CastExpr::path_const_iterator PathI = E->path_begin(),
3127e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith         PathE = E->path_end(); PathI != PathE; ++PathI) {
3128e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      assert(!(*PathI)->isVirtual() && "memptr cast through vbase");
3129e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl();
3130e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      if (!Result.castToBase(Base))
3131f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(E);
3132e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
3133e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
3134e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
3135e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}
3136e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3137e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithbool MemberPointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
3138e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // C++11 [expr.unary.op]p3 has very strict rules on how the address of a
3139e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // member can be formed.
3140e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  return Success(cast<DeclRefExpr>(E->getSubExpr())->getDecl());
3141e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}
3142e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3143e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===//
3144180f47959a066795cc0f409433023af448bb0328Richard Smith// Record Evaluation
3145180f47959a066795cc0f409433023af448bb0328Richard Smith//===----------------------------------------------------------------------===//
3146180f47959a066795cc0f409433023af448bb0328Richard Smith
3147180f47959a066795cc0f409433023af448bb0328Richard Smithnamespace {
3148180f47959a066795cc0f409433023af448bb0328Richard Smith  class RecordExprEvaluator
3149180f47959a066795cc0f409433023af448bb0328Richard Smith  : public ExprEvaluatorBase<RecordExprEvaluator, bool> {
3150180f47959a066795cc0f409433023af448bb0328Richard Smith    const LValue &This;
3151180f47959a066795cc0f409433023af448bb0328Richard Smith    APValue &Result;
3152180f47959a066795cc0f409433023af448bb0328Richard Smith  public:
3153180f47959a066795cc0f409433023af448bb0328Richard Smith
3154180f47959a066795cc0f409433023af448bb0328Richard Smith    RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result)
3155180f47959a066795cc0f409433023af448bb0328Richard Smith      : ExprEvaluatorBaseTy(info), This(This), Result(Result) {}
3156180f47959a066795cc0f409433023af448bb0328Richard Smith
3157180f47959a066795cc0f409433023af448bb0328Richard Smith    bool Success(const CCValue &V, const Expr *E) {
3158f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return CheckConstantExpression(Info, E, V, Result);
3159180f47959a066795cc0f409433023af448bb0328Richard Smith    }
316051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    bool ZeroInitialization(const Expr *E);
3161180f47959a066795cc0f409433023af448bb0328Richard Smith
316259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    bool VisitCastExpr(const CastExpr *E);
3163180f47959a066795cc0f409433023af448bb0328Richard Smith    bool VisitInitListExpr(const InitListExpr *E);
3164180f47959a066795cc0f409433023af448bb0328Richard Smith    bool VisitCXXConstructExpr(const CXXConstructExpr *E);
3165180f47959a066795cc0f409433023af448bb0328Richard Smith  };
3166180f47959a066795cc0f409433023af448bb0328Richard Smith}
3167180f47959a066795cc0f409433023af448bb0328Richard Smith
316851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// Perform zero-initialization on an object of non-union class type.
316951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// C++11 [dcl.init]p5:
317051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith///  To zero-initialize an object or reference of type T means:
317151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith///    [...]
317251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith///    -- if T is a (possibly cv-qualified) non-union class type,
317351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith///       each non-static data member and each base-class subobject is
317451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith///       zero-initialized
3175b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E,
3176b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                                          const RecordDecl *RD,
317751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith                                          const LValue &This, APValue &Result) {
317851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  assert(!RD->isUnion() && "Expected non-union class type");
317951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD);
318051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  Result = APValue(APValue::UninitStruct(), CD ? CD->getNumBases() : 0,
318151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith                   std::distance(RD->field_begin(), RD->field_end()));
318251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
318351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD);
318451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
318551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (CD) {
318651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    unsigned Index = 0;
318751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(),
3188b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith           End = CD->bases_end(); I != End; ++I, ++Index) {
318951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      const CXXRecordDecl *Base = I->getType()->getAsCXXRecordDecl();
319051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      LValue Subobject = This;
3191b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      HandleLValueDirectBase(Info, E, Subobject, CD, Base, &Layout);
3192b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      if (!HandleClassZeroInitialization(Info, E, Base, Subobject,
319351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith                                         Result.getStructBase(Index)))
319451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith        return false;
319551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    }
319651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  }
319751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
3198b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  for (RecordDecl::field_iterator I = RD->field_begin(), End = RD->field_end();
3199b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith       I != End; ++I) {
320051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    // -- if T is a reference type, no initialization is performed.
320151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    if ((*I)->getType()->isReferenceType())
320251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      continue;
320351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
320451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    LValue Subobject = This;
3205b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    HandleLValueMember(Info, E, Subobject, *I, &Layout);
320651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
320751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    ImplicitValueInitExpr VIE((*I)->getType());
320851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    if (!EvaluateConstantExpression(
320951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith          Result.getStructField((*I)->getFieldIndex()), Info, Subobject, &VIE))
321051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      return false;
321151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  }
321251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
321351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  return true;
321451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith}
321551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
321651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smithbool RecordExprEvaluator::ZeroInitialization(const Expr *E) {
321751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl();
321851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (RD->isUnion()) {
321951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the
322051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    // object's first non-static named data member is zero-initialized
322151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    RecordDecl::field_iterator I = RD->field_begin();
322251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    if (I == RD->field_end()) {
322351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      Result = APValue((const FieldDecl*)0);
322451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      return true;
322551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    }
322651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
322751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    LValue Subobject = This;
3228b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    HandleLValueMember(Info, E, Subobject, *I);
322951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    Result = APValue(*I);
323051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    ImplicitValueInitExpr VIE((*I)->getType());
323151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return EvaluateConstantExpression(Result.getUnionValue(), Info,
323251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith                                      Subobject, &VIE);
323351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  }
323451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
3235b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  return HandleClassZeroInitialization(Info, E, RD, This, Result);
323651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith}
323751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
323859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithbool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) {
323959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  switch (E->getCastKind()) {
324059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  default:
324159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    return ExprEvaluatorBaseTy::VisitCastExpr(E);
324259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
324359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  case CK_ConstructorConversion:
324459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    return Visit(E->getSubExpr());
324559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
324659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  case CK_DerivedToBase:
324759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  case CK_UncheckedDerivedToBase: {
324859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    CCValue DerivedObject;
3249f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!Evaluate(DerivedObject, Info, E->getSubExpr()))
325059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      return false;
3251f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!DerivedObject.isStruct())
3252f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E->getSubExpr());
325359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
325459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    // Derived-to-base rvalue conversion: just slice off the derived part.
325559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    APValue *Value = &DerivedObject;
325659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl();
325759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    for (CastExpr::path_const_iterator PathI = E->path_begin(),
325859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith         PathE = E->path_end(); PathI != PathE; ++PathI) {
325959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      assert(!(*PathI)->isVirtual() && "record rvalue with virtual base");
326059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl();
326159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      Value = &Value->getStructBase(getBaseIndex(RD, Base));
326259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith      RD = Base;
326359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    }
326459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    Result = *Value;
326559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith    return true;
326659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  }
326759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith  }
326859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith}
326959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith
3270180f47959a066795cc0f409433023af448bb0328Richard Smithbool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
3271180f47959a066795cc0f409433023af448bb0328Richard Smith  const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl();
3272180f47959a066795cc0f409433023af448bb0328Richard Smith  const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD);
3273180f47959a066795cc0f409433023af448bb0328Richard Smith
3274180f47959a066795cc0f409433023af448bb0328Richard Smith  if (RD->isUnion()) {
3275ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    const FieldDecl *Field = E->getInitializedFieldInUnion();
3276ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    Result = APValue(Field);
3277ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    if (!Field)
3278180f47959a066795cc0f409433023af448bb0328Richard Smith      return true;
3279ec789163a42a7be654ac34aadb750b508954d53cRichard Smith
3280ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    // If the initializer list for a union does not contain any elements, the
3281ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    // first element of the union is value-initialized.
3282ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    ImplicitValueInitExpr VIE(Field->getType());
3283ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    const Expr *InitExpr = E->getNumInits() ? E->getInit(0) : &VIE;
3284ec789163a42a7be654ac34aadb750b508954d53cRichard Smith
3285180f47959a066795cc0f409433023af448bb0328Richard Smith    LValue Subobject = This;
3286ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    HandleLValueMember(Info, InitExpr, Subobject, Field, &Layout);
3287180f47959a066795cc0f409433023af448bb0328Richard Smith    return EvaluateConstantExpression(Result.getUnionValue(), Info,
3288ec789163a42a7be654ac34aadb750b508954d53cRichard Smith                                      Subobject, InitExpr);
3289180f47959a066795cc0f409433023af448bb0328Richard Smith  }
3290180f47959a066795cc0f409433023af448bb0328Richard Smith
3291180f47959a066795cc0f409433023af448bb0328Richard Smith  assert((!isa<CXXRecordDecl>(RD) || !cast<CXXRecordDecl>(RD)->getNumBases()) &&
3292180f47959a066795cc0f409433023af448bb0328Richard Smith         "initializer list for class with base classes");
3293180f47959a066795cc0f409433023af448bb0328Richard Smith  Result = APValue(APValue::UninitStruct(), 0,
3294180f47959a066795cc0f409433023af448bb0328Richard Smith                   std::distance(RD->field_begin(), RD->field_end()));
3295180f47959a066795cc0f409433023af448bb0328Richard Smith  unsigned ElementNo = 0;
3296745f5147e065900267c85a5568785a1991d4838fRichard Smith  bool Success = true;
3297180f47959a066795cc0f409433023af448bb0328Richard Smith  for (RecordDecl::field_iterator Field = RD->field_begin(),
3298180f47959a066795cc0f409433023af448bb0328Richard Smith       FieldEnd = RD->field_end(); Field != FieldEnd; ++Field) {
3299180f47959a066795cc0f409433023af448bb0328Richard Smith    // Anonymous bit-fields are not considered members of the class for
3300180f47959a066795cc0f409433023af448bb0328Richard Smith    // purposes of aggregate initialization.
3301180f47959a066795cc0f409433023af448bb0328Richard Smith    if (Field->isUnnamedBitfield())
3302180f47959a066795cc0f409433023af448bb0328Richard Smith      continue;
3303180f47959a066795cc0f409433023af448bb0328Richard Smith
3304180f47959a066795cc0f409433023af448bb0328Richard Smith    LValue Subobject = This;
3305180f47959a066795cc0f409433023af448bb0328Richard Smith
3306745f5147e065900267c85a5568785a1991d4838fRichard Smith    bool HaveInit = ElementNo < E->getNumInits();
3307745f5147e065900267c85a5568785a1991d4838fRichard Smith
3308745f5147e065900267c85a5568785a1991d4838fRichard Smith    // FIXME: Diagnostics here should point to the end of the initializer
3309745f5147e065900267c85a5568785a1991d4838fRichard Smith    // list, not the start.
3310745f5147e065900267c85a5568785a1991d4838fRichard Smith    HandleLValueMember(Info, HaveInit ? E->getInit(ElementNo) : E, Subobject,
3311745f5147e065900267c85a5568785a1991d4838fRichard Smith                       *Field, &Layout);
3312745f5147e065900267c85a5568785a1991d4838fRichard Smith
3313745f5147e065900267c85a5568785a1991d4838fRichard Smith    // Perform an implicit value-initialization for members beyond the end of
3314745f5147e065900267c85a5568785a1991d4838fRichard Smith    // the initializer list.
3315745f5147e065900267c85a5568785a1991d4838fRichard Smith    ImplicitValueInitExpr VIE(HaveInit ? Info.Ctx.IntTy : Field->getType());
3316745f5147e065900267c85a5568785a1991d4838fRichard Smith
3317745f5147e065900267c85a5568785a1991d4838fRichard Smith    if (!EvaluateConstantExpression(
3318745f5147e065900267c85a5568785a1991d4838fRichard Smith          Result.getStructField((*Field)->getFieldIndex()),
3319745f5147e065900267c85a5568785a1991d4838fRichard Smith          Info, Subobject, HaveInit ? E->getInit(ElementNo++) : &VIE)) {
3320745f5147e065900267c85a5568785a1991d4838fRichard Smith      if (!Info.keepEvaluatingAfterFailure())
3321180f47959a066795cc0f409433023af448bb0328Richard Smith        return false;
3322745f5147e065900267c85a5568785a1991d4838fRichard Smith      Success = false;
3323180f47959a066795cc0f409433023af448bb0328Richard Smith    }
3324180f47959a066795cc0f409433023af448bb0328Richard Smith  }
3325180f47959a066795cc0f409433023af448bb0328Richard Smith
3326745f5147e065900267c85a5568785a1991d4838fRichard Smith  return Success;
3327180f47959a066795cc0f409433023af448bb0328Richard Smith}
3328180f47959a066795cc0f409433023af448bb0328Richard Smith
3329180f47959a066795cc0f409433023af448bb0328Richard Smithbool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) {
3330180f47959a066795cc0f409433023af448bb0328Richard Smith  const CXXConstructorDecl *FD = E->getConstructor();
333151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  bool ZeroInit = E->requiresZeroInitialization();
333251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) {
3333ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    // If we've already performed zero-initialization, we're already done.
3334ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    if (!Result.isUninit())
3335ec789163a42a7be654ac34aadb750b508954d53cRichard Smith      return true;
3336ec789163a42a7be654ac34aadb750b508954d53cRichard Smith
333751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    if (ZeroInit)
333851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      return ZeroInitialization(E);
333951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
33406180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    const CXXRecordDecl *RD = FD->getParent();
33416180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    if (RD->isUnion())
33426180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith      Result = APValue((FieldDecl*)0);
33436180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    else
33446180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith      Result = APValue(APValue::UninitStruct(), RD->getNumBases(),
33456180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith                       std::distance(RD->field_begin(), RD->field_end()));
33466180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    return true;
33476180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith  }
33486180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith
3349180f47959a066795cc0f409433023af448bb0328Richard Smith  const FunctionDecl *Definition = 0;
3350180f47959a066795cc0f409433023af448bb0328Richard Smith  FD->getBody(Definition);
3351180f47959a066795cc0f409433023af448bb0328Richard Smith
3352c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition))
3353c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return false;
3354180f47959a066795cc0f409433023af448bb0328Richard Smith
3355610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith  // Avoid materializing a temporary for an elidable copy/move constructor.
335651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (E->isElidable() && !ZeroInit)
3357180f47959a066795cc0f409433023af448bb0328Richard Smith    if (const MaterializeTemporaryExpr *ME
3358180f47959a066795cc0f409433023af448bb0328Richard Smith          = dyn_cast<MaterializeTemporaryExpr>(E->getArg(0)))
3359180f47959a066795cc0f409433023af448bb0328Richard Smith      return Visit(ME->GetTemporaryExpr());
3360180f47959a066795cc0f409433023af448bb0328Richard Smith
336151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (ZeroInit && !ZeroInitialization(E))
336251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return false;
336351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
3364180f47959a066795cc0f409433023af448bb0328Richard Smith  llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs());
3365745f5147e065900267c85a5568785a1991d4838fRichard Smith  return HandleConstructorCall(E->getExprLoc(), This, Args,
3366f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                               cast<CXXConstructorDecl>(Definition), Info,
3367f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                               Result);
3368180f47959a066795cc0f409433023af448bb0328Richard Smith}
3369180f47959a066795cc0f409433023af448bb0328Richard Smith
3370180f47959a066795cc0f409433023af448bb0328Richard Smithstatic bool EvaluateRecord(const Expr *E, const LValue &This,
3371180f47959a066795cc0f409433023af448bb0328Richard Smith                           APValue &Result, EvalInfo &Info) {
3372180f47959a066795cc0f409433023af448bb0328Richard Smith  assert(E->isRValue() && E->getType()->isRecordType() &&
3373180f47959a066795cc0f409433023af448bb0328Richard Smith         "can't evaluate expression as a record rvalue");
3374180f47959a066795cc0f409433023af448bb0328Richard Smith  return RecordExprEvaluator(Info, This, Result).Visit(E);
3375180f47959a066795cc0f409433023af448bb0328Richard Smith}
3376180f47959a066795cc0f409433023af448bb0328Richard Smith
3377180f47959a066795cc0f409433023af448bb0328Richard Smith//===----------------------------------------------------------------------===//
3378e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// Temporary Evaluation
3379e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//
3380e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// Temporaries are represented in the AST as rvalues, but generally behave like
3381e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// lvalues. The full-object of which the temporary is a subobject is implicitly
3382e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// materialized so that a reference can bind to it.
3383e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===//
3384e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithnamespace {
3385e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithclass TemporaryExprEvaluator
3386e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  : public LValueExprEvaluatorBase<TemporaryExprEvaluator> {
3387e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithpublic:
3388e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  TemporaryExprEvaluator(EvalInfo &Info, LValue &Result) :
3389e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    LValueExprEvaluatorBaseTy(Info, Result) {}
3390e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3391e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  /// Visit an expression which constructs the value of this temporary.
3392e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitConstructExpr(const Expr *E) {
3393e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    Result.set(E, Info.CurrentCall);
3394e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return EvaluateConstantExpression(Info.CurrentCall->Temporaries[E], Info,
3395e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                                      Result, E);
3396e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
3397e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3398e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitCastExpr(const CastExpr *E) {
3399e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    switch (E->getCastKind()) {
3400e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    default:
3401e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return LValueExprEvaluatorBaseTy::VisitCastExpr(E);
3402e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3403e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    case CK_ConstructorConversion:
3404e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return VisitConstructExpr(E->getSubExpr());
3405e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    }
3406e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
3407e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitInitListExpr(const InitListExpr *E) {
3408e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return VisitConstructExpr(E);
3409e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
3410e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitCXXConstructExpr(const CXXConstructExpr *E) {
3411e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return VisitConstructExpr(E);
3412e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
3413e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  bool VisitCallExpr(const CallExpr *E) {
3414e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return VisitConstructExpr(E);
3415e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  }
3416e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith};
3417e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} // end anonymous namespace
3418e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3419e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// Evaluate an expression of record type as a temporary.
3420e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) {
3421af2c7a194592401394233b7cbcdd3cfd0a7a38ddRichard Smith  assert(E->isRValue() && E->getType()->isRecordType());
3422e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  return TemporaryExprEvaluator(Info, Result).Visit(E);
3423e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}
3424e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3425e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===//
342659b5da6d853b4368b984700315adf7b37de05764Nate Begeman// Vector Evaluation
342759b5da6d853b4368b984700315adf7b37de05764Nate Begeman//===----------------------------------------------------------------------===//
342859b5da6d853b4368b984700315adf7b37de05764Nate Begeman
342959b5da6d853b4368b984700315adf7b37de05764Nate Begemannamespace {
3430770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramer  class VectorExprEvaluator
343107fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith  : public ExprEvaluatorBase<VectorExprEvaluator, bool> {
343207fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    APValue &Result;
343359b5da6d853b4368b984700315adf7b37de05764Nate Begeman  public:
34341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
343507fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    VectorExprEvaluator(EvalInfo &info, APValue &Result)
343607fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith      : ExprEvaluatorBaseTy(info), Result(Result) {}
34371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
343807fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    bool Success(const ArrayRef<APValue> &V, const Expr *E) {
343907fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith      assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements());
344007fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith      // FIXME: remove this APValue copy.
344107fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith      Result = APValue(V.data(), V.size());
344207fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith      return true;
344307fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    }
344469c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith    bool Success(const CCValue &V, const Expr *E) {
344569c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith      assert(V.isVector());
344607fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith      Result = V;
344707fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith      return true;
344807fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    }
344951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    bool ZeroInitialization(const Expr *E);
34501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
345107fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    bool VisitUnaryReal(const UnaryOperator *E)
345291110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman      { return Visit(E->getSubExpr()); }
345307fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    bool VisitCastExpr(const CastExpr* E);
345407fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    bool VisitInitListExpr(const InitListExpr *E);
345507fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    bool VisitUnaryImag(const UnaryOperator *E);
345691110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman    // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
34572217c87bdc5ab357046a5453bdb06f469c41024eEli Friedman    //                 binary comparisons, binary and/or/xor,
345891110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman    //                 shufflevector, ExtVectorElementExpr
345959b5da6d853b4368b984700315adf7b37de05764Nate Begeman  };
346059b5da6d853b4368b984700315adf7b37de05764Nate Begeman} // end anonymous namespace
346159b5da6d853b4368b984700315adf7b37de05764Nate Begeman
346259b5da6d853b4368b984700315adf7b37de05764Nate Begemanstatic bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
3463c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  assert(E->isRValue() && E->getType()->isVectorType() &&"not a vector rvalue");
346407fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith  return VectorExprEvaluator(Info, Result).Visit(E);
346559b5da6d853b4368b984700315adf7b37de05764Nate Begeman}
346659b5da6d853b4368b984700315adf7b37de05764Nate Begeman
346707fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smithbool VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
346807fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith  const VectorType *VTy = E->getType()->castAs<VectorType>();
3469c0b8b19bd8056d6b5d831623a0825cce150f4507Nate Begeman  unsigned NElts = VTy->getNumElements();
34701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3471d62ca370b03b8c6ad58002d3399383baf744e32bRichard Smith  const Expr *SE = E->getSubExpr();
3472e8c9e9218f215ec6089f12b076c7b9d310fd5194Nate Begeman  QualType SETy = SE->getType();
347359b5da6d853b4368b984700315adf7b37de05764Nate Begeman
347446a523285928aa07bf14803178dc04616ac85994Eli Friedman  switch (E->getCastKind()) {
347546a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_VectorSplat: {
347607fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    APValue Val = APValue();
347746a523285928aa07bf14803178dc04616ac85994Eli Friedman    if (SETy->isIntegerType()) {
347846a523285928aa07bf14803178dc04616ac85994Eli Friedman      APSInt IntResult;
347946a523285928aa07bf14803178dc04616ac85994Eli Friedman      if (!EvaluateInteger(SE, IntResult, Info))
3480f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith         return false;
348107fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith      Val = APValue(IntResult);
348246a523285928aa07bf14803178dc04616ac85994Eli Friedman    } else if (SETy->isRealFloatingType()) {
348346a523285928aa07bf14803178dc04616ac85994Eli Friedman       APFloat F(0.0);
348446a523285928aa07bf14803178dc04616ac85994Eli Friedman       if (!EvaluateFloat(SE, F, Info))
3485f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith         return false;
348607fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith       Val = APValue(F);
348746a523285928aa07bf14803178dc04616ac85994Eli Friedman    } else {
348807fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith      return Error(E);
348946a523285928aa07bf14803178dc04616ac85994Eli Friedman    }
3490c0b8b19bd8056d6b5d831623a0825cce150f4507Nate Begeman
3491c0b8b19bd8056d6b5d831623a0825cce150f4507Nate Begeman    // Splat and create vector APValue.
349207fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    SmallVector<APValue, 4> Elts(NElts, Val);
349307fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith    return Success(Elts, E);
3494e8c9e9218f215ec6089f12b076c7b9d310fd5194Nate Begeman  }
3495e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  case CK_BitCast: {
3496e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    // Evaluate the operand into an APInt we can extract from.
3497e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    llvm::APInt SValInt;
3498e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    if (!EvalAndBitcastToAPInt(Info, SE, SValInt))
3499e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      return false;
3500e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    // Extract the elements
3501e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    QualType EltTy = VTy->getElementType();
3502e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    unsigned EltSize = Info.Ctx.getTypeSize(EltTy);
3503e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian();
3504e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    SmallVector<APValue, 4> Elts;
3505e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    if (EltTy->isRealFloatingType()) {
3506e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(EltTy);
3507e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      bool isIEESem = &Sem != &APFloat::PPCDoubleDouble;
3508e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      unsigned FloatEltSize = EltSize;
3509e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      if (&Sem == &APFloat::x87DoubleExtended)
3510e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        FloatEltSize = 80;
3511e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      for (unsigned i = 0; i < NElts; i++) {
3512e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        llvm::APInt Elt;
3513e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        if (BigEndian)
3514e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman          Elt = SValInt.rotl(i*EltSize+FloatEltSize).trunc(FloatEltSize);
3515e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        else
3516e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman          Elt = SValInt.rotr(i*EltSize).trunc(FloatEltSize);
3517e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        Elts.push_back(APValue(APFloat(Elt, isIEESem)));
3518e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      }
3519e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    } else if (EltTy->isIntegerType()) {
3520e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      for (unsigned i = 0; i < NElts; i++) {
3521e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        llvm::APInt Elt;
3522e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        if (BigEndian)
3523e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman          Elt = SValInt.rotl(i*EltSize+EltSize).zextOrTrunc(EltSize);
3524e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        else
3525e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman          Elt = SValInt.rotr(i*EltSize).zextOrTrunc(EltSize);
3526e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman        Elts.push_back(APValue(APSInt(Elt, EltTy->isSignedIntegerType())));
3527e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      }
3528e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    } else {
3529e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman      return Error(E);
3530e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    }
3531e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman    return Success(Elts, E);
3532e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman  }
353346a523285928aa07bf14803178dc04616ac85994Eli Friedman  default:
3534c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return ExprEvaluatorBaseTy::VisitCastExpr(E);
3535c0b8b19bd8056d6b5d831623a0825cce150f4507Nate Begeman  }
353659b5da6d853b4368b984700315adf7b37de05764Nate Begeman}
353759b5da6d853b4368b984700315adf7b37de05764Nate Begeman
353807fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smithbool
353959b5da6d853b4368b984700315adf7b37de05764Nate BegemanVectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
354007fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith  const VectorType *VT = E->getType()->castAs<VectorType>();
354159b5da6d853b4368b984700315adf7b37de05764Nate Begeman  unsigned NumInits = E->getNumInits();
354291110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman  unsigned NumElements = VT->getNumElements();
35431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
354459b5da6d853b4368b984700315adf7b37de05764Nate Begeman  QualType EltTy = VT->getElementType();
35455f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<APValue, 4> Elements;
354659b5da6d853b4368b984700315adf7b37de05764Nate Begeman
35473edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman  // The number of initializers can be less than the number of
35483edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman  // vector elements. For OpenCL, this can be due to nested vector
35493edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman  // initialization. For GCC compatibility, missing trailing elements
35503edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman  // should be initialized with zeroes.
35513edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman  unsigned CountInits = 0, CountElts = 0;
35523edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman  while (CountElts < NumElements) {
35533edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman    // Handle nested vector initialization.
35543edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman    if (CountInits < NumInits
35553edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman        && E->getInit(CountInits)->getType()->isExtVectorType()) {
35563edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      APValue v;
35573edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      if (!EvaluateVector(E->getInit(CountInits), v, Info))
35583edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman        return Error(E);
35593edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      unsigned vlen = v.getVectorLength();
35603edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      for (unsigned j = 0; j < vlen; j++)
35613edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman        Elements.push_back(v.getVectorElt(j));
35623edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      CountElts += vlen;
35633edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman    } else if (EltTy->isIntegerType()) {
356459b5da6d853b4368b984700315adf7b37de05764Nate Begeman      llvm::APSInt sInt(32);
35653edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      if (CountInits < NumInits) {
35663edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman        if (!EvaluateInteger(E->getInit(CountInits), sInt, Info))
35673edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman          return Error(E);
35683edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      } else // trailing integer zero.
35693edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman        sInt = Info.Ctx.MakeIntValue(0, EltTy);
35703edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      Elements.push_back(APValue(sInt));
35713edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      CountElts++;
357259b5da6d853b4368b984700315adf7b37de05764Nate Begeman    } else {
357359b5da6d853b4368b984700315adf7b37de05764Nate Begeman      llvm::APFloat f(0.0);
35743edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      if (CountInits < NumInits) {
35753edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman        if (!EvaluateFloat(E->getInit(CountInits), f, Info))
35763edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman          return Error(E);
35773edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      } else // trailing float zero.
35783edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman        f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
35793edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      Elements.push_back(APValue(f));
35803edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman      CountElts++;
358159b5da6d853b4368b984700315adf7b37de05764Nate Begeman    }
35823edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman    CountInits++;
358359b5da6d853b4368b984700315adf7b37de05764Nate Begeman  }
358407fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith  return Success(Elements, E);
358559b5da6d853b4368b984700315adf7b37de05764Nate Begeman}
358659b5da6d853b4368b984700315adf7b37de05764Nate Begeman
358707fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smithbool
358851201882382fb40c9456a06c7f93d6ddd4a57712Richard SmithVectorExprEvaluator::ZeroInitialization(const Expr *E) {
358907fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith  const VectorType *VT = E->getType()->getAs<VectorType>();
359091110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman  QualType EltTy = VT->getElementType();
359191110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman  APValue ZeroElement;
359291110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman  if (EltTy->isIntegerType())
359391110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman    ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
359491110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman  else
359591110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman    ZeroElement =
359691110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman        APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
359791110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman
35985f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
359907fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith  return Success(Elements, E);
360091110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman}
360191110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman
360207fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smithbool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
36038327fad71da34492d82c532f42a58cb4baff81a3Richard Smith  VisitIgnoredValue(E->getSubExpr());
360451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  return ZeroInitialization(E);
360591110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman}
360691110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman
360759b5da6d853b4368b984700315adf7b37de05764Nate Begeman//===----------------------------------------------------------------------===//
3608cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith// Array Evaluation
3609cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith//===----------------------------------------------------------------------===//
3610cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
3611cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smithnamespace {
3612cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  class ArrayExprEvaluator
3613cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  : public ExprEvaluatorBase<ArrayExprEvaluator, bool> {
3614180f47959a066795cc0f409433023af448bb0328Richard Smith    const LValue &This;
3615cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    APValue &Result;
3616cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  public:
3617cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
3618180f47959a066795cc0f409433023af448bb0328Richard Smith    ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result)
3619180f47959a066795cc0f409433023af448bb0328Richard Smith      : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {}
3620cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
3621cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    bool Success(const APValue &V, const Expr *E) {
3622cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith      assert(V.isArray() && "Expected array type");
3623cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith      Result = V;
3624cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith      return true;
3625cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    }
3626cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
362751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    bool ZeroInitialization(const Expr *E) {
3628180f47959a066795cc0f409433023af448bb0328Richard Smith      const ConstantArrayType *CAT =
3629180f47959a066795cc0f409433023af448bb0328Richard Smith          Info.Ctx.getAsConstantArrayType(E->getType());
3630180f47959a066795cc0f409433023af448bb0328Richard Smith      if (!CAT)
3631f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(E);
3632180f47959a066795cc0f409433023af448bb0328Richard Smith
3633180f47959a066795cc0f409433023af448bb0328Richard Smith      Result = APValue(APValue::UninitArray(), 0,
3634180f47959a066795cc0f409433023af448bb0328Richard Smith                       CAT->getSize().getZExtValue());
3635180f47959a066795cc0f409433023af448bb0328Richard Smith      if (!Result.hasArrayFiller()) return true;
3636180f47959a066795cc0f409433023af448bb0328Richard Smith
363751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      // Zero-initialize all elements.
3638180f47959a066795cc0f409433023af448bb0328Richard Smith      LValue Subobject = This;
3639b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      Subobject.addArray(Info, E, CAT);
3640180f47959a066795cc0f409433023af448bb0328Richard Smith      ImplicitValueInitExpr VIE(CAT->getElementType());
3641180f47959a066795cc0f409433023af448bb0328Richard Smith      return EvaluateConstantExpression(Result.getArrayFiller(), Info,
3642180f47959a066795cc0f409433023af448bb0328Richard Smith                                        Subobject, &VIE);
3643180f47959a066795cc0f409433023af448bb0328Richard Smith    }
3644180f47959a066795cc0f409433023af448bb0328Richard Smith
3645cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith    bool VisitInitListExpr(const InitListExpr *E);
3646e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    bool VisitCXXConstructExpr(const CXXConstructExpr *E);
3647cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  };
3648cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith} // end anonymous namespace
3649cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
3650180f47959a066795cc0f409433023af448bb0328Richard Smithstatic bool EvaluateArray(const Expr *E, const LValue &This,
3651180f47959a066795cc0f409433023af448bb0328Richard Smith                          APValue &Result, EvalInfo &Info) {
365251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  assert(E->isRValue() && E->getType()->isArrayType() && "not an array rvalue");
3653180f47959a066795cc0f409433023af448bb0328Richard Smith  return ArrayExprEvaluator(Info, This, Result).Visit(E);
3654cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith}
3655cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
3656cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smithbool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
3657cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType());
3658cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  if (!CAT)
3659f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
3660cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
3661974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith  // C++11 [dcl.init.string]p1: A char array [...] can be initialized by [...]
3662974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith  // an appropriately-typed string literal enclosed in braces.
3663ec789163a42a7be654ac34aadb750b508954d53cRichard Smith  if (E->getNumInits() == 1 && E->getInit(0)->isGLValue() &&
3664974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith      Info.Ctx.hasSameUnqualifiedType(E->getType(), E->getInit(0)->getType())) {
3665974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith    LValue LV;
3666974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith    if (!EvaluateLValue(E->getInit(0), LV, Info))
3667974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith      return false;
3668974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith    uint64_t NumElements = CAT->getSize().getZExtValue();
3669974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith    Result = APValue(APValue::UninitArray(), NumElements, NumElements);
3670974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith
3671974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith    // Copy the string literal into the array. FIXME: Do this better.
3672b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith    LV.addArray(Info, E, CAT);
3673974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith    for (uint64_t I = 0; I < NumElements; ++I) {
3674974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith      CCValue Char;
3675974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith      if (!HandleLValueToRValueConversion(Info, E->getInit(0),
3676745f5147e065900267c85a5568785a1991d4838fRichard Smith                                          CAT->getElementType(), LV, Char) ||
3677745f5147e065900267c85a5568785a1991d4838fRichard Smith          !CheckConstantExpression(Info, E->getInit(0), Char,
3678745f5147e065900267c85a5568785a1991d4838fRichard Smith                                   Result.getArrayInitializedElt(I)) ||
3679745f5147e065900267c85a5568785a1991d4838fRichard Smith          !HandleLValueArrayAdjustment(Info, E->getInit(0), LV,
3680b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                                       CAT->getElementType(), 1))
3681974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith        return false;
3682974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith    }
3683974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith    return true;
3684974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith  }
3685974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith
3686745f5147e065900267c85a5568785a1991d4838fRichard Smith  bool Success = true;
3687745f5147e065900267c85a5568785a1991d4838fRichard Smith
3688cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  Result = APValue(APValue::UninitArray(), E->getNumInits(),
3689cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith                   CAT->getSize().getZExtValue());
3690180f47959a066795cc0f409433023af448bb0328Richard Smith  LValue Subobject = This;
3691b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  Subobject.addArray(Info, E, CAT);
3692180f47959a066795cc0f409433023af448bb0328Richard Smith  unsigned Index = 0;
3693cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  for (InitListExpr::const_iterator I = E->begin(), End = E->end();
3694180f47959a066795cc0f409433023af448bb0328Richard Smith       I != End; ++I, ++Index) {
3695180f47959a066795cc0f409433023af448bb0328Richard Smith    if (!EvaluateConstantExpression(Result.getArrayInitializedElt(Index),
3696745f5147e065900267c85a5568785a1991d4838fRichard Smith                                    Info, Subobject, cast<Expr>(*I)) ||
3697745f5147e065900267c85a5568785a1991d4838fRichard Smith        !HandleLValueArrayAdjustment(Info, cast<Expr>(*I), Subobject,
3698745f5147e065900267c85a5568785a1991d4838fRichard Smith                                     CAT->getElementType(), 1)) {
3699745f5147e065900267c85a5568785a1991d4838fRichard Smith      if (!Info.keepEvaluatingAfterFailure())
3700745f5147e065900267c85a5568785a1991d4838fRichard Smith        return false;
3701745f5147e065900267c85a5568785a1991d4838fRichard Smith      Success = false;
3702745f5147e065900267c85a5568785a1991d4838fRichard Smith    }
3703180f47959a066795cc0f409433023af448bb0328Richard Smith  }
3704cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
3705745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!Result.hasArrayFiller()) return Success;
3706cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  assert(E->hasArrayFiller() && "no array filler for incomplete init list");
3707180f47959a066795cc0f409433023af448bb0328Richard Smith  // FIXME: The Subobject here isn't necessarily right. This rarely matters,
3708180f47959a066795cc0f409433023af448bb0328Richard Smith  // but sometimes does:
3709180f47959a066795cc0f409433023af448bb0328Richard Smith  //   struct S { constexpr S() : p(&p) {} void *p; };
3710180f47959a066795cc0f409433023af448bb0328Richard Smith  //   S s[10] = {};
3711cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith  return EvaluateConstantExpression(Result.getArrayFiller(), Info,
3712745f5147e065900267c85a5568785a1991d4838fRichard Smith                                    Subobject, E->getArrayFiller()) && Success;
3713cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith}
3714cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith
3715e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithbool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) {
3716e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType());
3717e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (!CAT)
3718f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
3719e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3720ec789163a42a7be654ac34aadb750b508954d53cRichard Smith  bool HadZeroInit = !Result.isUninit();
3721ec789163a42a7be654ac34aadb750b508954d53cRichard Smith  if (!HadZeroInit)
3722ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    Result = APValue(APValue::UninitArray(), 0, CAT->getSize().getZExtValue());
3723e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (!Result.hasArrayFiller())
3724e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
3725e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3726e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  const CXXConstructorDecl *FD = E->getConstructor();
37276180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith
372851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  bool ZeroInit = E->requiresZeroInitialization();
372951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) {
3730ec789163a42a7be654ac34aadb750b508954d53cRichard Smith    if (HadZeroInit)
3731ec789163a42a7be654ac34aadb750b508954d53cRichard Smith      return true;
3732ec789163a42a7be654ac34aadb750b508954d53cRichard Smith
373351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    if (ZeroInit) {
373451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      LValue Subobject = This;
3735b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith      Subobject.addArray(Info, E, CAT);
373651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      ImplicitValueInitExpr VIE(CAT->getElementType());
373751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      return EvaluateConstantExpression(Result.getArrayFiller(), Info,
373851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith                                        Subobject, &VIE);
373951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    }
374051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
37416180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    const CXXRecordDecl *RD = FD->getParent();
37426180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    if (RD->isUnion())
37436180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith      Result.getArrayFiller() = APValue((FieldDecl*)0);
37446180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    else
37456180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith      Result.getArrayFiller() =
37466180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith          APValue(APValue::UninitStruct(), RD->getNumBases(),
37476180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith                  std::distance(RD->field_begin(), RD->field_end()));
37486180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith    return true;
37496180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith  }
37506180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith
3751e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  const FunctionDecl *Definition = 0;
3752e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  FD->getBody(Definition);
3753e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3754c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition))
3755c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return false;
3756e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3757e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // FIXME: The Subobject here isn't necessarily right. This rarely matters,
3758e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  // but sometimes does:
3759e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  //   struct S { constexpr S() : p(&p) {} void *p; };
3760e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  //   S s[10];
3761e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  LValue Subobject = This;
3762b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith  Subobject.addArray(Info, E, CAT);
376351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
3764ec789163a42a7be654ac34aadb750b508954d53cRichard Smith  if (ZeroInit && !HadZeroInit) {
376551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    ImplicitValueInitExpr VIE(CAT->getElementType());
376651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    if (!EvaluateConstantExpression(Result.getArrayFiller(), Info, Subobject,
376751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith                                    &VIE))
376851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      return false;
376951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  }
377051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
3771e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs());
3772745f5147e065900267c85a5568785a1991d4838fRichard Smith  return HandleConstructorCall(E->getExprLoc(), Subobject, Args,
3773e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                               cast<CXXConstructorDecl>(Definition),
3774e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith                               Info, Result.getArrayFiller());
3775e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}
3776e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith
3777cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith//===----------------------------------------------------------------------===//
3778f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner// Integer Evaluation
3779c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith//
3780c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// As a GNU extension, we support casting pointers to sufficiently-wide integer
3781c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// types and back in constant folding. Integer values are thus represented
3782c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// either as an integer-valued APValue, or as an lvalue-valued APValue.
3783f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===//
3784f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
3785f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattnernamespace {
3786770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass IntExprEvaluator
37878cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  : public ExprEvaluatorBase<IntExprEvaluator, bool> {
378847a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith  CCValue &Result;
3789f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattnerpublic:
379047a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith  IntExprEvaluator(EvalInfo &info, CCValue &result)
37918cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    : ExprEvaluatorBaseTy(info), Result(result) {}
3792f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
3793973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara  bool Success(const llvm::APSInt &SI, const Expr *E) {
3794973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara    assert(E->getType()->isIntegralOrEnumerationType() &&
37952ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor           "Invalid evaluation result.");
3796973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara    assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() &&
37973f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar           "Invalid evaluation result.");
3798973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara    assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
37993f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar           "Invalid evaluation result.");
380047a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    Result = CCValue(SI);
38013f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar    return true;
38023f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar  }
38033f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar
3804131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar  bool Success(const llvm::APInt &I, const Expr *E) {
38052ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor    assert(E->getType()->isIntegralOrEnumerationType() &&
38062ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor           "Invalid evaluation result.");
380730c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar    assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
38083f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar           "Invalid evaluation result.");
380947a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    Result = CCValue(APSInt(I));
3810575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor    Result.getInt().setIsUnsigned(
3811575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor                            E->getType()->isUnsignedIntegerOrEnumerationType());
3812131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar    return true;
3813131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar  }
3814131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar
3815131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar  bool Success(uint64_t Value, const Expr *E) {
38162ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor    assert(E->getType()->isIntegralOrEnumerationType() &&
38172ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor           "Invalid evaluation result.");
381847a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    Result = CCValue(Info.Ctx.MakeIntValue(Value, E->getType()));
3819131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar    return true;
3820131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar  }
3821131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar
38224f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck  bool Success(CharUnits Size, const Expr *E) {
38234f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck    return Success(Size.getQuantity(), E);
38244f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck  }
38254f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck
382647a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith  bool Success(const CCValue &V, const Expr *E) {
38275930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman    if (V.isLValue() || V.isAddrLabelDiff()) {
3828342f1f8b0a402c5a7f8c5055db7f60a7808f1687Richard Smith      Result = V;
3829342f1f8b0a402c5a7f8c5055db7f60a7808f1687Richard Smith      return true;
3830342f1f8b0a402c5a7f8c5055db7f60a7808f1687Richard Smith    }
38318cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    return Success(V.getInt(), E);
383232fea9d18cc3658a1b01df5ca6f2ac302625c61dChris Lattner  }
38331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
383451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  bool ZeroInitialization(const Expr *E) { return Success(0, E); }
3835f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith
38368cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  //===--------------------------------------------------------------------===//
38378cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  //                            Visitor Methods
38388cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  //===--------------------------------------------------------------------===//
3839f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
38404c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner  bool VisitIntegerLiteral(const IntegerLiteral *E) {
3841131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar    return Success(E->getValue(), E);
38424c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner  }
38434c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner  bool VisitCharacterLiteral(const CharacterLiteral *E) {
3844131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar    return Success(E->getValue(), E);
38454c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner  }
3846043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman
3847043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman  bool CheckReferencedDecl(const Expr *E, const Decl *D);
3848043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman  bool VisitDeclRefExpr(const DeclRefExpr *E) {
38498cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    if (CheckReferencedDecl(E, E->getDecl()))
38508cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne      return true;
38518cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
38528cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    return ExprEvaluatorBaseTy::VisitDeclRefExpr(E);
3853043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman  }
3854043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman  bool VisitMemberExpr(const MemberExpr *E) {
3855043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman    if (CheckReferencedDecl(E, E->getMemberDecl())) {
3856c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith      VisitIgnoredValue(E->getBase());
3857043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman      return true;
3858043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman    }
38598cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
38608cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    return ExprEvaluatorBaseTy::VisitMemberExpr(E);
3861043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman  }
3862043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman
38638cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCallExpr(const CallExpr *E);
3864b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattner  bool VisitBinaryOperator(const BinaryOperator *E);
38658ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  bool VisitOffsetOfExpr(const OffsetOfExpr *E);
3866b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattner  bool VisitUnaryOperator(const UnaryOperator *E);
3867f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
38688cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCastExpr(const CastExpr* E);
3869f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E);
38700518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl
38713068d117951a8df54bae9db039b56201ab10962bAnders Carlsson  bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
3872131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar    return Success(E->getValue(), E);
38733068d117951a8df54bae9db039b56201ab10962bAnders Carlsson  }
38741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3875f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  // Note, GNU defines __null as an integer, not a pointer.
38763f70456b8adb0405ef2a47d51f9fc2d5937ae8aeAnders Carlsson  bool VisitGNUNullExpr(const GNUNullExpr *E) {
387751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return ZeroInitialization(E);
3878664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman  }
3879664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman
388064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
38810dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl    return Success(E->getValue(), E);
388264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  }
388364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
38846ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  bool VisitBinaryTypeTraitExpr(const BinaryTypeTraitExpr *E) {
38856ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet    return Success(E->getValue(), E);
38866ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  }
38876ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet
388821ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) {
388921ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley    return Success(E->getValue(), E);
389021ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  }
389121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley
3892552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) {
3893552622067dc45013d240f73952fece703f5e63bdJohn Wiegley    return Success(E->getValue(), E);
3894552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  }
3895552622067dc45013d240f73952fece703f5e63bdJohn Wiegley
3896722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman  bool VisitUnaryReal(const UnaryOperator *E);
3897664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman  bool VisitUnaryImag(const UnaryOperator *E);
3898664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman
3899295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl  bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E);
3900ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor  bool VisitSizeOfPackExpr(const SizeOfPackExpr *E);
3901cea8d966f826554f0679595e9371e314e8dbc1cfSebastian Redl
3902fcee0019b76f9f368f2b3d6d4048a98232593f29Chris Lattnerprivate:
39038b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck  CharUnits GetAlignOfExpr(const Expr *E);
39048b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck  CharUnits GetAlignOfType(QualType T);
39051bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  static QualType GetObjectType(APValue::LValueBase B);
39068cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool TryEvaluateBuiltinObjectSize(const CallExpr *E);
3907664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman  // FIXME: Missing: array subscript of vector, member of vector
3908f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner};
3909f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner} // end anonymous namespace
3910f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
3911c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// EvaluateIntegerOrLValue - Evaluate an rvalue integral-typed expression, and
3912c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// produce either the integer value or a pointer.
3913c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith///
3914c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// GCC has a heinous extension which folds casts between pointer types and
3915c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// pointer-sized integral types. We support this by allowing the evaluation of
3916c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// an integer rvalue to produce a pointer (represented as an lvalue) instead.
3917c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// Some simple arithmetic on such values is supported (they are treated much
3918c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// like char*).
3919f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool EvaluateIntegerOrLValue(const Expr *E, CCValue &Result,
392047a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith                                    EvalInfo &Info) {
3921c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  assert(E->isRValue() && E->getType()->isIntegralOrEnumerationType());
39228cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  return IntExprEvaluator(Info, Result).Visit(E);
392369ab26a8623141f35e86817cfc6e0fbe7639a40fDaniel Dunbar}
392469ab26a8623141f35e86817cfc6e0fbe7639a40fDaniel Dunbar
3925f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info) {
392647a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith  CCValue Val;
3927f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (!EvaluateIntegerOrLValue(E, Val, Info))
3928f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return false;
3929f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (!Val.isInt()) {
3930f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    // FIXME: It would be better to produce the diagnostic for casting
3931f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    //        a pointer to an integer.
3932dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr);
393330c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar    return false;
3934f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
393530c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar  Result = Val.getInt();
393630c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar  return true;
3937f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner}
3938f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
3939f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// Check whether the given declaration can be directly converted to an integral
3940f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// rvalue. If not, no diagnostic is produced; there are other things we can
3941f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// try.
3942043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedmanbool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) {
39434c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner  // Enums are integer constant exprs.
3944bfbdcd861a4364bfc21a9e5047bdbd56812d6693Abramo Bagnara  if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) {
3945973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara    // Check for signedness/width mismatches between E type and ECD value.
3946973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara    bool SameSign = (ECD->getInitVal().isSigned()
3947973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara                     == E->getType()->isSignedIntegerOrEnumerationType());
3948973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara    bool SameWidth = (ECD->getInitVal().getBitWidth()
3949973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara                      == Info.Ctx.getIntWidth(E->getType()));
3950973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara    if (SameSign && SameWidth)
3951973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara      return Success(ECD->getInitVal(), E);
3952973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara    else {
3953973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara      // Get rid of mismatch (otherwise Success assertions will fail)
3954973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara      // by computing a new value matching the type of E.
3955973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara      llvm::APSInt Val = ECD->getInitVal();
3956973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara      if (!SameSign)
3957973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara        Val.setIsSigned(!ECD->getInitVal().isSigned());
3958973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara      if (!SameWidth)
3959973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara        Val = Val.extOrTrunc(Info.Ctx.getIntWidth(E->getType()));
3960973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara      return Success(Val, E);
3961973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara    }
3962bfbdcd861a4364bfc21a9e5047bdbd56812d6693Abramo Bagnara  }
39638cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  return false;
39644c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner}
39654c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner
3966a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
3967a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner/// as GCC.
3968a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattnerstatic int EvaluateBuiltinClassifyType(const CallExpr *E) {
3969a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  // The following enum mimics the values returned by GCC.
39707c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // FIXME: Does GCC differ between lvalue and rvalue references here?
3971a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  enum gcc_type_class {
3972a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    no_type_class = -1,
3973a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    void_type_class, integer_type_class, char_type_class,
3974a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    enumeral_type_class, boolean_type_class,
3975a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    pointer_type_class, reference_type_class, offset_type_class,
3976a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    real_type_class, complex_type_class,
3977a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    function_type_class, method_type_class,
3978a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    record_type_class, union_type_class,
3979a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    array_type_class, string_type_class,
3980a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    lang_type_class
3981a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  };
39821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
39831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If no argument was supplied, default to "no_type_class". This isn't
3984a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  // ideal, however it is what gcc does.
3985a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  if (E->getNumArgs() == 0)
3986a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return no_type_class;
39871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3988a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  QualType ArgTy = E->getArg(0)->getType();
3989a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  if (ArgTy->isVoidType())
3990a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return void_type_class;
3991a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isEnumeralType())
3992a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return enumeral_type_class;
3993a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isBooleanType())
3994a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return boolean_type_class;
3995a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isCharType())
3996a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return string_type_class; // gcc doesn't appear to use char_type_class
3997a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isIntegerType())
3998a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return integer_type_class;
3999a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isPointerType())
4000a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return pointer_type_class;
4001a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isReferenceType())
4002a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return reference_type_class;
4003a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isRealType())
4004a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return real_type_class;
4005a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isComplexType())
4006a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return complex_type_class;
4007a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isFunctionType())
4008a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return function_type_class;
4009fb87b89fc9eb103e19fb8e4b925c23f0bd091b99Douglas Gregor  else if (ArgTy->isStructureOrClassType())
4010a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return record_type_class;
4011a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isUnionType())
4012a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return union_type_class;
4013a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isArrayType())
4014a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return array_type_class;
4015a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else if (ArgTy->isUnionType())
4016a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner    return union_type_class;
4017a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner  else  // FIXME: offset_type_class, method_type_class, & lang_type_class?
4018b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("CallExpr::isBuiltinClassifyType(): unimplemented type");
4019a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner}
4020a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner
402180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// EvaluateBuiltinConstantPForLValue - Determine the result of
402280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// __builtin_constant_p when applied to the given lvalue.
402380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith///
402480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// An lvalue is only "constant" if it is a pointer or reference to the first
402580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// character of a string literal.
402680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smithtemplate<typename LValue>
402780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smithstatic bool EvaluateBuiltinConstantPForLValue(const LValue &LV) {
402880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  const Expr *E = LV.getLValueBase().dyn_cast<const Expr*>();
402980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  return E && isa<StringLiteral>(E) && LV.getLValueOffset().isZero();
403080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith}
403180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith
403280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// EvaluateBuiltinConstantP - Evaluate __builtin_constant_p as similarly to
403380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// GCC as we can manage.
403480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smithstatic bool EvaluateBuiltinConstantP(ASTContext &Ctx, const Expr *Arg) {
403580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  QualType ArgType = Arg->getType();
403680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith
403780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  // __builtin_constant_p always has one operand. The rules which gcc follows
403880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  // are not precisely documented, but are as follows:
403980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  //
404080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  //  - If the operand is of integral, floating, complex or enumeration type,
404180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  //    and can be folded to a known value of that type, it returns 1.
404280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  //  - If the operand and can be folded to a pointer to the first character
404380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  //    of a string literal (or such a pointer cast to an integral type), it
404480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  //    returns 1.
404580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  //
404680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  // Otherwise, it returns 0.
404780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  //
404880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  // FIXME: GCC also intends to return 1 for literals of aggregate types, but
404980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  // its support for this does not currently work.
405080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  if (ArgType->isIntegralOrEnumerationType()) {
405180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    Expr::EvalResult Result;
405280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    if (!Arg->EvaluateAsRValue(Result, Ctx) || Result.HasSideEffects)
405380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith      return false;
405480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith
405580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    APValue &V = Result.Val;
405680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    if (V.getKind() == APValue::Int)
405780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith      return true;
405880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith
405980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    return EvaluateBuiltinConstantPForLValue(V);
406080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  } else if (ArgType->isFloatingType() || ArgType->isAnyComplexType()) {
406180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    return Arg->isEvaluatable(Ctx);
406280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  } else if (ArgType->isPointerType() || Arg->isGLValue()) {
406380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    LValue LV;
406480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    Expr::EvalStatus Status;
406580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    EvalInfo Info(Ctx, Status);
406680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    if ((Arg->isGLValue() ? EvaluateLValue(Arg, LV, Info)
406780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith                          : EvaluatePointer(Arg, LV, Info)) &&
406880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith        !Status.HasSideEffects)
406980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith      return EvaluateBuiltinConstantPForLValue(LV);
407080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  }
407180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith
407280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  // Anything else isn't considered to be sufficiently constant.
407380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  return false;
407480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith}
407580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith
407642c8f87eb60958170c46767273bf93e6c96125bfJohn McCall/// Retrieves the "underlying object type" of the given expression,
407742c8f87eb60958170c46767273bf93e6c96125bfJohn McCall/// as used by __builtin_object_size.
40781bf9a9e6a5bdc0de7939908855dcddf46b661800Richard SmithQualType IntExprEvaluator::GetObjectType(APValue::LValueBase B) {
40791bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) {
40801bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    if (const VarDecl *VD = dyn_cast<VarDecl>(D))
408142c8f87eb60958170c46767273bf93e6c96125bfJohn McCall      return VD->getType();
40821bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  } else if (const Expr *E = B.get<const Expr*>()) {
40831bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    if (isa<CompoundLiteralExpr>(E))
40841bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith      return E->getType();
408542c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  }
408642c8f87eb60958170c46767273bf93e6c96125bfJohn McCall
408742c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  return QualType();
408842c8f87eb60958170c46767273bf93e6c96125bfJohn McCall}
408942c8f87eb60958170c46767273bf93e6c96125bfJohn McCall
40908cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool IntExprEvaluator::TryEvaluateBuiltinObjectSize(const CallExpr *E) {
409142c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  // TODO: Perhaps we should let LLVM lower this?
409242c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  LValue Base;
409342c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  if (!EvaluatePointer(E->getArg(0), Base, Info))
409442c8f87eb60958170c46767273bf93e6c96125bfJohn McCall    return false;
409542c8f87eb60958170c46767273bf93e6c96125bfJohn McCall
409642c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  // If we can prove the base is null, lower to zero now.
40971bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  if (!Base.getLValueBase()) return Success(0, E);
409842c8f87eb60958170c46767273bf93e6c96125bfJohn McCall
40991bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  QualType T = GetObjectType(Base.getLValueBase());
410042c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  if (T.isNull() ||
410142c8f87eb60958170c46767273bf93e6c96125bfJohn McCall      T->isIncompleteType() ||
41021357869bc5983cdfbc986db1f3d18265bb34cb0eEli Friedman      T->isFunctionType() ||
410342c8f87eb60958170c46767273bf93e6c96125bfJohn McCall      T->isVariablyModifiedType() ||
410442c8f87eb60958170c46767273bf93e6c96125bfJohn McCall      T->isDependentType())
4105f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
410642c8f87eb60958170c46767273bf93e6c96125bfJohn McCall
410742c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  CharUnits Size = Info.Ctx.getTypeSizeInChars(T);
410842c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  CharUnits Offset = Base.getLValueOffset();
410942c8f87eb60958170c46767273bf93e6c96125bfJohn McCall
411042c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  if (!Offset.isNegative() && Offset <= Size)
411142c8f87eb60958170c46767273bf93e6c96125bfJohn McCall    Size -= Offset;
411242c8f87eb60958170c46767273bf93e6c96125bfJohn McCall  else
411342c8f87eb60958170c46767273bf93e6c96125bfJohn McCall    Size = CharUnits::Zero();
41144f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck  return Success(Size, E);
411542c8f87eb60958170c46767273bf93e6c96125bfJohn McCall}
411642c8f87eb60958170c46767273bf93e6c96125bfJohn McCall
41178cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
4118180f47959a066795cc0f409433023af448bb0328Richard Smith  switch (E->isBuiltinCall()) {
4119019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  default:
41208cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    return ExprEvaluatorBaseTy::VisitCallExpr(E);
412164eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump
412264eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump  case Builtin::BI__builtin_object_size: {
412342c8f87eb60958170c46767273bf93e6c96125bfJohn McCall    if (TryEvaluateBuiltinObjectSize(E))
412442c8f87eb60958170c46767273bf93e6c96125bfJohn McCall      return true;
412564eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump
4126b2aaf51ed73a4774322a39a0dd59d0fe7e3258c0Eric Christopher    // If evaluating the argument has side-effects we can't determine
4127b2aaf51ed73a4774322a39a0dd59d0fe7e3258c0Eric Christopher    // the size of the object and lower it to unknown now.
4128393c247fe025ccb5f914e37e948192ea86faef8cFariborz Jahanian    if (E->getArg(0)->HasSideEffects(Info.Ctx)) {
4129a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith      if (E->getArg(1)->EvaluateKnownConstInt(Info.Ctx).getZExtValue() <= 1)
4130cf184655319cf7a5b811067cff9d26a5741fd161Chris Lattner        return Success(-1ULL, E);
413164eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump      return Success(0, E);
413264eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump    }
4133c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump
4134f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
413564eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump  }
413664eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump
4137019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  case Builtin::BI__builtin_classify_type:
4138131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar    return Success(EvaluateBuiltinClassifyType(E), E);
41391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
414080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  case Builtin::BI__builtin_constant_p:
414180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    return Success(EvaluateBuiltinConstantP(Info.Ctx, E->getArg(0)), E);
4142e052d46f4db91f9ba572859ffc984e85cbf5d5ffRichard Smith
414321fb98ee003e992b0c4e204d98a19e0ef544cae3Chris Lattner  case Builtin::BI__builtin_eh_return_data_regno: {
4144a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith    int Operand = E->getArg(0)->EvaluateKnownConstInt(Info.Ctx).getZExtValue();
4145bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(Operand);
414621fb98ee003e992b0c4e204d98a19e0ef544cae3Chris Lattner    return Success(Operand, E);
414721fb98ee003e992b0c4e204d98a19e0ef544cae3Chris Lattner  }
4148c4a2638b5ef3e2d35d872614ceb655a7a22c58beEli Friedman
4149c4a2638b5ef3e2d35d872614ceb655a7a22c58beEli Friedman  case Builtin::BI__builtin_expect:
4150c4a2638b5ef3e2d35d872614ceb655a7a22c58beEli Friedman    return Visit(E->getArg(0));
415140b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith
41525726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor  case Builtin::BIstrlen:
415340b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith    // A call to strlen is not a constant expression.
415440b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith    if (Info.getLangOpts().CPlusPlus0x)
415540b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith      Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_invalid_function)
415640b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith        << /*isConstexpr*/0 << /*isConstructor*/0 << "'strlen'";
415740b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith    else
415840b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith      Info.CCEDiag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr);
415940b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith    // Fall through.
41605726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor  case Builtin::BI__builtin_strlen:
41615726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor    // As an extension, we support strlen() and __builtin_strlen() as constant
41625726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor    // expressions when the argument is a string literal.
41638cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    if (const StringLiteral *S
41645726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor               = dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenImpCasts())) {
41655726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor      // The string literal may have embedded null characters. Find the first
41665726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor      // one and truncate there.
41675f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      StringRef Str = S->getString();
41685f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      StringRef::size_type Pos = Str.find(0);
41695f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      if (Pos != StringRef::npos)
41705726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor        Str = Str.substr(0, Pos);
41715726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor
41725726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor      return Success(Str.size(), E);
41735726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor    }
41745726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor
4175f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
4176454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman
4177454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman  case Builtin::BI__atomic_is_lock_free: {
4178454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    APSInt SizeVal;
4179454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    if (!EvaluateInteger(E->getArg(0), SizeVal, Info))
4180454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman      return false;
4181454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman
4182454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // For __atomic_is_lock_free(sizeof(_Atomic(T))), if the size is a power
4183454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // of two less than the maximum inline atomic width, we know it is
4184454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // lock-free.  If the size isn't a power of two, or greater than the
4185454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // maximum alignment where we promote atomics, we know it is not lock-free
4186454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // (at least not in the sense of atomic_is_lock_free).  Otherwise,
4187454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // the answer can only be determined at runtime; for example, 16-byte
4188454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // atomics have lock-free implementations on some, but not all,
4189454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // x86-64 processors.
4190454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman
4191454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // Check power-of-two.
4192454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    CharUnits Size = CharUnits::fromQuantity(SizeVal.getZExtValue());
4193454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    if (!Size.isPowerOfTwo())
4194454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman#if 0
4195454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman      // FIXME: Suppress this folding until the ABI for the promotion width
4196454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman      // settles.
4197454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman      return Success(0, E);
4198454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman#else
4199f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E);
4200454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman#endif
4201454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman
4202454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman#if 0
4203454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // Check against promotion width.
4204454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // FIXME: Suppress this folding until the ABI for the promotion width
4205454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // settles.
4206454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    unsigned PromoteWidthBits =
4207454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman        Info.Ctx.getTargetInfo().getMaxAtomicPromoteWidth();
4208454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    if (Size > Info.Ctx.toCharUnitsFromBits(PromoteWidthBits))
4209454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman      return Success(0, E);
4210454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman#endif
4211454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman
4212454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    // Check against inlining width.
4213454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    unsigned InlineWidthBits =
4214454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman        Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth();
4215454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman    if (Size <= Info.Ctx.toCharUnitsFromBits(InlineWidthBits))
4216454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman      return Success(1, E);
4217454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman
4218f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
4219454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman  }
4220019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  }
42214c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner}
4222f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
4223625b80755b603d28f36fb4212c81484d87ad08d3Richard Smithstatic bool HasSameBase(const LValue &A, const LValue &B) {
4224625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith  if (!A.getLValueBase())
4225625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith    return !B.getLValueBase();
4226625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith  if (!B.getLValueBase())
4227625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith    return false;
4228625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith
42291bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith  if (A.getLValueBase().getOpaqueValue() !=
42301bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith      B.getLValueBase().getOpaqueValue()) {
4231625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith    const Decl *ADecl = GetLValueBaseDecl(A);
4232625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith    if (!ADecl)
4233625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith      return false;
4234625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith    const Decl *BDecl = GetLValueBaseDecl(B);
42359a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith    if (!BDecl || ADecl->getCanonicalDecl() != BDecl->getCanonicalDecl())
4236625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith      return false;
4237625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith  }
4238625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith
4239625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith  return IsGlobalLValue(A.getLValueBase()) ||
4240177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith         A.getLValueFrame() == B.getLValueFrame();
4241625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith}
4242625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith
42437b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith/// Perform the given integer operation, which is known to need at most BitWidth
42447b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith/// bits, and check for overflow in the original type (if that type was not an
42457b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith/// unsigned type).
42467b48a2986345480241f3b8209f71bb21b0530b4fRichard Smithtemplate<typename Operation>
42477b48a2986345480241f3b8209f71bb21b0530b4fRichard Smithstatic APSInt CheckedIntArithmetic(EvalInfo &Info, const Expr *E,
42487b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith                                   const APSInt &LHS, const APSInt &RHS,
42497b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith                                   unsigned BitWidth, Operation Op) {
42507b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith  if (LHS.isUnsigned())
42517b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    return Op(LHS, RHS);
42527b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith
42537b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith  APSInt Value(Op(LHS.extend(BitWidth), RHS.extend(BitWidth)), false);
42547b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith  APSInt Result = Value.trunc(LHS.getBitWidth());
42557b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith  if (Result.extend(BitWidth) != Value)
42567b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    HandleOverflow(Info, E, Value, E->getType());
42577b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith  return Result;
42587b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith}
42597b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith
4260b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattnerbool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
4261c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  if (E->isAssignmentOp())
4262f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
4263c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
42642de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  if (E->getOpcode() == BO_Comma) {
42658327fad71da34492d82c532f42a58cb4baff81a3Richard Smith    VisitIgnoredValue(E->getLHS());
42668327fad71da34492d82c532f42a58cb4baff81a3Richard Smith    return Visit(E->getRHS());
4267a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman  }
4268a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman
4269a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman  if (E->isLogicalOp()) {
4270a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman    // These need to be handled specially because the operands aren't
4271a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman    // necessarily integral
4272fcb4d09531abbf2a5cf398c2f946fb3bc2875f64Anders Carlsson    bool lhsResult, rhsResult;
42731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4274c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    if (EvaluateAsBooleanCondition(E->getLHS(), lhsResult, Info)) {
427551fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson      // We were able to evaluate the LHS, see if we can get away with not
427651fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson      // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
42772de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      if (lhsResult == (E->getOpcode() == BO_LOr))
42783f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar        return Success(lhsResult, E);
4279a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman
4280c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith      if (EvaluateAsBooleanCondition(E->getRHS(), rhsResult, Info)) {
42812de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall        if (E->getOpcode() == BO_LOr)
4282131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar          return Success(lhsResult || rhsResult, E);
42834bbc0e05a714d4ee4918a92a4a7049dd6ef33ad0Anders Carlsson        else
4284131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar          return Success(lhsResult && rhsResult, E);
42854bbc0e05a714d4ee4918a92a4a7049dd6ef33ad0Anders Carlsson      }
42864bbc0e05a714d4ee4918a92a4a7049dd6ef33ad0Anders Carlsson    } else {
4287f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      // FIXME: If both evaluations fail, we should produce the diagnostic from
4288f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      // the LHS. If the LHS is non-constant and the RHS is unevaluatable, it's
4289f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      // less clear how to diagnose this.
4290c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith      if (EvaluateAsBooleanCondition(E->getRHS(), rhsResult, Info)) {
42914bbc0e05a714d4ee4918a92a4a7049dd6ef33ad0Anders Carlsson        // We can't evaluate the LHS; however, sometimes the result
42924bbc0e05a714d4ee4918a92a4a7049dd6ef33ad0Anders Carlsson        // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
4293f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        if (rhsResult == (E->getOpcode() == BO_LOr)) {
4294131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar          // Since we weren't able to evaluate the left hand side, it
4295fcb4d09531abbf2a5cf398c2f946fb3bc2875f64Anders Carlsson          // must have had side effects.
42961e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith          Info.EvalStatus.HasSideEffects = true;
4297131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar
4298131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar          return Success(rhsResult, E);
42994bbc0e05a714d4ee4918a92a4a7049dd6ef33ad0Anders Carlsson        }
43004bbc0e05a714d4ee4918a92a4a7049dd6ef33ad0Anders Carlsson      }
4301a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman    }
4302a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman
4303a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman    return false;
4304c8cc9ccc7b87a7ed1749b074f6b670bcec49abc1Chris Lattner  }
430554176fdb044312b4b77c3da6682d3575b3728d30Chris Lattner
4306286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson  QualType LHSTy = E->getLHS()->getType();
4307286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson  QualType RHSTy = E->getRHS()->getType();
43084087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar
43094087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar  if (LHSTy->isAnyComplexType()) {
43104087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar    assert(RHSTy->isAnyComplexType() && "Invalid comparison");
4311f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    ComplexValue LHS, RHS;
43124087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar
4313745f5147e065900267c85a5568785a1991d4838fRichard Smith    bool LHSOK = EvaluateComplex(E->getLHS(), LHS, Info);
4314745f5147e065900267c85a5568785a1991d4838fRichard Smith    if (!LHSOK && !Info.keepEvaluatingAfterFailure())
43154087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar      return false;
43164087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar
4317745f5147e065900267c85a5568785a1991d4838fRichard Smith    if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK)
43184087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar      return false;
43194087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar
43204087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar    if (LHS.isComplexFloat()) {
43211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      APFloat::cmpResult CR_r =
43224087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar        LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
43231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      APFloat::cmpResult CR_i =
43244087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar        LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
43254087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar
43262de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      if (E->getOpcode() == BO_EQ)
4327131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar        return Success((CR_r == APFloat::cmpEqual &&
4328131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar                        CR_i == APFloat::cmpEqual), E);
4329131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar      else {
43302de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall        assert(E->getOpcode() == BO_NE &&
4331131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar               "Invalid complex comparison.");
43321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        return Success(((CR_r == APFloat::cmpGreaterThan ||
4333fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang                         CR_r == APFloat::cmpLessThan ||
4334fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang                         CR_r == APFloat::cmpUnordered) ||
43351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                        (CR_i == APFloat::cmpGreaterThan ||
4336fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang                         CR_i == APFloat::cmpLessThan ||
4337fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang                         CR_i == APFloat::cmpUnordered)), E);
4338131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar      }
43394087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar    } else {
43402de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      if (E->getOpcode() == BO_EQ)
4341131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar        return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
4342131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar                        LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
4343131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar      else {
43442de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall        assert(E->getOpcode() == BO_NE &&
4345131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar               "Invalid compex comparison.");
4346131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar        return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
4347131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar                        LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
4348131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar      }
43494087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar    }
43504087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar  }
43511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4352286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson  if (LHSTy->isRealFloatingType() &&
4353286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson      RHSTy->isRealFloatingType()) {
4354286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson    APFloat RHS(0.0), LHS(0.0);
43551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4356745f5147e065900267c85a5568785a1991d4838fRichard Smith    bool LHSOK = EvaluateFloat(E->getRHS(), RHS, Info);
4357745f5147e065900267c85a5568785a1991d4838fRichard Smith    if (!LHSOK && !Info.keepEvaluatingAfterFailure())
4358286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson      return false;
43591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4360745f5147e065900267c85a5568785a1991d4838fRichard Smith    if (!EvaluateFloat(E->getLHS(), LHS, Info) || !LHSOK)
4361286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson      return false;
43621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4363286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson    APFloat::cmpResult CR = LHS.compare(RHS);
4364529569e68d10b0fd3750fd2124faf742249b846bAnders Carlsson
4365286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson    switch (E->getOpcode()) {
4366286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson    default:
4367b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("Invalid binary operator!");
43682de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LT:
4369131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar      return Success(CR == APFloat::cmpLessThan, E);
43702de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_GT:
4371131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar      return Success(CR == APFloat::cmpGreaterThan, E);
43722de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LE:
4373131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar      return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
43742de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_GE:
43751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
4376131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar                     E);
43772de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_EQ:
4378131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar      return Success(CR == APFloat::cmpEqual, E);
43792de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_NE:
43801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return Success(CR == APFloat::cmpGreaterThan
4381fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang                     || CR == APFloat::cmpLessThan
4382fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang                     || CR == APFloat::cmpUnordered, E);
4383286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson    }
4384286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson  }
43851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4386ad02d7debd03ff275ac8ea27891a4ecccdb78068Eli Friedman  if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
4387625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith    if (E->getOpcode() == BO_Sub || E->isComparisonOp()) {
4388745f5147e065900267c85a5568785a1991d4838fRichard Smith      LValue LHSValue, RHSValue;
4389745f5147e065900267c85a5568785a1991d4838fRichard Smith
4390745f5147e065900267c85a5568785a1991d4838fRichard Smith      bool LHSOK = EvaluatePointer(E->getLHS(), LHSValue, Info);
4391745f5147e065900267c85a5568785a1991d4838fRichard Smith      if (!LHSOK && Info.keepEvaluatingAfterFailure())
43923068d117951a8df54bae9db039b56201ab10962bAnders Carlsson        return false;
4393a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman
4394745f5147e065900267c85a5568785a1991d4838fRichard Smith      if (!EvaluatePointer(E->getRHS(), RHSValue, Info) || !LHSOK)
43953068d117951a8df54bae9db039b56201ab10962bAnders Carlsson        return false;
4396a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman
4397625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith      // Reject differing bases from the normal codepath; we special-case
4398625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith      // comparisons to null.
4399625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith      if (!HasSameBase(LHSValue, RHSValue)) {
440065639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman        if (E->getOpcode() == BO_Sub) {
440165639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman          // Handle &&A - &&B.
440265639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman          if (!LHSValue.Offset.isZero() || !RHSValue.Offset.isZero())
440365639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman            return false;
440465639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman          const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr*>();
440565639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman          const Expr *RHSExpr = LHSValue.Base.dyn_cast<const Expr*>();
440665639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman          if (!LHSExpr || !RHSExpr)
440765639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman            return false;
440865639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman          const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr);
440965639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman          const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr);
441065639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman          if (!LHSAddrExpr || !RHSAddrExpr)
441165639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman            return false;
44125930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman          // Make sure both labels come from the same function.
44135930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman          if (LHSAddrExpr->getLabel()->getDeclContext() !=
44145930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman              RHSAddrExpr->getLabel()->getDeclContext())
44155930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman            return false;
441665639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman          Result = CCValue(LHSAddrExpr, RHSAddrExpr);
441765639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman          return true;
441865639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman        }
44199e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith        // Inequalities and subtractions between unrelated pointers have
44209e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith        // unspecified or undefined behavior.
44215bc86103767c2abcbfdd6518e0ccbbbb6aa59e0fEli Friedman        if (!E->isEqualityOp())
4422f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith          return Error(E);
4423ffbda40a1fb7169591dc01771f3511178a2f727cEli Friedman        // A constant address may compare equal to the address of a symbol.
4424ffbda40a1fb7169591dc01771f3511178a2f727cEli Friedman        // The one exception is that address of an object cannot compare equal
4425c45061bd0c0fdad4df8eea7e9e5af186d11427e5Eli Friedman        // to a null pointer constant.
4426ffbda40a1fb7169591dc01771f3511178a2f727cEli Friedman        if ((!LHSValue.Base && !LHSValue.Offset.isZero()) ||
4427ffbda40a1fb7169591dc01771f3511178a2f727cEli Friedman            (!RHSValue.Base && !RHSValue.Offset.isZero()))
4428f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith          return Error(E);
44299e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith        // It's implementation-defined whether distinct literals will have
4430b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith        // distinct addresses. In clang, the result of such a comparison is
4431b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith        // unspecified, so it is not a constant expression. However, we do know
4432b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith        // that the address of a literal will be non-null.
443374f4634781cee06e28eb741bda5d0f936fdd1948Richard Smith        if ((IsLiteralLValue(LHSValue) || IsLiteralLValue(RHSValue)) &&
443474f4634781cee06e28eb741bda5d0f936fdd1948Richard Smith            LHSValue.Base && RHSValue.Base)
4435f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith          return Error(E);
44369e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith        // We can't tell whether weak symbols will end up pointing to the same
44379e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith        // object.
44389e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith        if (IsWeakLValue(LHSValue) || IsWeakLValue(RHSValue))
4439f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith          return Error(E);
44409e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith        // Pointers with different bases cannot represent the same object.
4441c45061bd0c0fdad4df8eea7e9e5af186d11427e5Eli Friedman        // (Note that clang defaults to -fmerge-all-constants, which can
4442c45061bd0c0fdad4df8eea7e9e5af186d11427e5Eli Friedman        // lead to inconsistent results for comparisons involving the address
4443c45061bd0c0fdad4df8eea7e9e5af186d11427e5Eli Friedman        // of a constant; this generally doesn't matter in practice.)
44449e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith        return Success(E->getOpcode() == BO_NE, E);
44455bc86103767c2abcbfdd6518e0ccbbbb6aa59e0fEli Friedman      }
4446a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman
444715efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith      const CharUnits &LHSOffset = LHSValue.getLValueOffset();
444815efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith      const CharUnits &RHSOffset = RHSValue.getLValueOffset();
444915efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith
4450f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator();
4451f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator();
4452f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
44532de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      if (E->getOpcode() == BO_Sub) {
4454f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        // C++11 [expr.add]p6:
4455f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        //   Unless both pointers point to elements of the same array object, or
4456f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        //   one past the last element of the array object, the behavior is
4457f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        //   undefined.
4458f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        if (!LHSDesignator.Invalid && !RHSDesignator.Invalid &&
4459f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith            !AreElementsOfSameArray(getType(LHSValue.Base),
4460f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith                                    LHSDesignator, RHSDesignator))
4461f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          CCEDiag(E, diag::note_constexpr_pointer_subtraction_not_same_array);
4462f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
44634992bdde387c5f033bb450a716eaabc0fda52688Chris Lattner        QualType Type = E->getLHS()->getType();
44644992bdde387c5f033bb450a716eaabc0fda52688Chris Lattner        QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
44653068d117951a8df54bae9db039b56201ab10962bAnders Carlsson
4466180f47959a066795cc0f409433023af448bb0328Richard Smith        CharUnits ElementSize;
4467180f47959a066795cc0f409433023af448bb0328Richard Smith        if (!HandleSizeof(Info, ElementType, ElementSize))
4468180f47959a066795cc0f409433023af448bb0328Richard Smith          return false;
4469a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman
447015efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        // FIXME: LLVM and GCC both compute LHSOffset - RHSOffset at runtime,
447115efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        // and produce incorrect results when it overflows. Such behavior
447215efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        // appears to be non-conforming, but is common, so perhaps we should
447315efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        // assume the standard intended for such cases to be undefined behavior
447415efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        // and check for them.
447515efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith
447615efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        // Compute (LHSOffset - RHSOffset) / Size carefully, checking for
447715efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        // overflow in the final conversion to ptrdiff_t.
447815efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        APSInt LHS(
447915efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith          llvm::APInt(65, (int64_t)LHSOffset.getQuantity(), true), false);
448015efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        APSInt RHS(
448115efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith          llvm::APInt(65, (int64_t)RHSOffset.getQuantity(), true), false);
448215efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        APSInt ElemSize(
448315efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith          llvm::APInt(65, (int64_t)ElementSize.getQuantity(), true), false);
448415efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        APSInt TrueResult = (LHS - RHS) / ElemSize;
448515efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        APSInt Result = TrueResult.trunc(Info.Ctx.getIntWidth(E->getType()));
448615efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith
448715efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        if (Result.extend(65) != TrueResult)
448815efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith          HandleOverflow(Info, E, TrueResult, E->getType());
448915efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith        return Success(Result, E);
4490ad02d7debd03ff275ac8ea27891a4ecccdb78068Eli Friedman      }
4491625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith
449282f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith      // C++11 [expr.rel]p3:
449382f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith      //   Pointers to void (after pointer conversions) can be compared, with a
449482f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith      //   result defined as follows: If both pointers represent the same
449582f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith      //   address or are both the null pointer value, the result is true if the
449682f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith      //   operator is <= or >= and false otherwise; otherwise the result is
449782f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith      //   unspecified.
449882f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith      // We interpret this as applying to pointers to *cv* void.
449982f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith      if (LHSTy->isVoidPointerType() && LHSOffset != RHSOffset &&
4500f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          E->isRelationalOp())
450182f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith        CCEDiag(E, diag::note_constexpr_void_comparison);
450282f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith
4503f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      // C++11 [expr.rel]p2:
4504f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      // - If two pointers point to non-static data members of the same object,
4505f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      //   or to subobjects or array elements fo such members, recursively, the
4506f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      //   pointer to the later declared member compares greater provided the
4507f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      //   two members have the same access control and provided their class is
4508f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      //   not a union.
4509f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      //   [...]
4510f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      // - Otherwise pointer comparisons are unspecified.
4511f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      if (!LHSDesignator.Invalid && !RHSDesignator.Invalid &&
4512f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          E->isRelationalOp()) {
4513f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        bool WasArrayIndex;
4514f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        unsigned Mismatch =
4515f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          FindDesignatorMismatch(getType(LHSValue.Base), LHSDesignator,
4516f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith                                 RHSDesignator, WasArrayIndex);
4517f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        // At the point where the designators diverge, the comparison has a
4518f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        // specified value if:
4519f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        //  - we are comparing array indices
4520f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        //  - we are comparing fields of a union, or fields with the same access
4521f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        // Otherwise, the result is unspecified and thus the comparison is not a
4522f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        // constant expression.
4523f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        if (!WasArrayIndex && Mismatch < LHSDesignator.Entries.size() &&
4524f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith            Mismatch < RHSDesignator.Entries.size()) {
4525f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          const FieldDecl *LF = getAsField(LHSDesignator.Entries[Mismatch]);
4526f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          const FieldDecl *RF = getAsField(RHSDesignator.Entries[Mismatch]);
4527f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          if (!LF && !RF)
4528f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith            CCEDiag(E, diag::note_constexpr_pointer_comparison_base_classes);
4529f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          else if (!LF)
4530f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith            CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field)
4531f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith              << getAsBaseClass(LHSDesignator.Entries[Mismatch])
4532f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith              << RF->getParent() << RF;
4533f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          else if (!RF)
4534f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith            CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field)
4535f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith              << getAsBaseClass(RHSDesignator.Entries[Mismatch])
4536f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith              << LF->getParent() << LF;
4537f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith          else if (!LF->getParent()->isUnion() &&
4538f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith                   LF->getAccess() != RF->getAccess())
4539f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith            CCEDiag(E, diag::note_constexpr_pointer_comparison_differing_access)
4540f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith              << LF << LF->getAccess() << RF << RF->getAccess()
4541f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith              << LF->getParent();
4542f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith        }
4543f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith      }
4544f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith
4545625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith      switch (E->getOpcode()) {
4546625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith      default: llvm_unreachable("missing comparison operator");
4547625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith      case BO_LT: return Success(LHSOffset < RHSOffset, E);
4548625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith      case BO_GT: return Success(LHSOffset > RHSOffset, E);
4549625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith      case BO_LE: return Success(LHSOffset <= RHSOffset, E);
4550625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith      case BO_GE: return Success(LHSOffset >= RHSOffset, E);
4551625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith      case BO_EQ: return Success(LHSOffset == RHSOffset, E);
4552625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith      case BO_NE: return Success(LHSOffset != RHSOffset, E);
4553ad02d7debd03ff275ac8ea27891a4ecccdb78068Eli Friedman      }
45543068d117951a8df54bae9db039b56201ab10962bAnders Carlsson    }
45553068d117951a8df54bae9db039b56201ab10962bAnders Carlsson  }
4556b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith
4557b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith  if (LHSTy->isMemberPointerType()) {
4558b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    assert(E->isEqualityOp() && "unexpected member pointer operation");
4559b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    assert(RHSTy->isMemberPointerType() && "invalid comparison");
4560b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith
4561b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    MemberPtr LHSValue, RHSValue;
4562b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith
4563b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    bool LHSOK = EvaluateMemberPointer(E->getLHS(), LHSValue, Info);
4564b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    if (!LHSOK && Info.keepEvaluatingAfterFailure())
4565b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith      return false;
4566b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith
4567b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    if (!EvaluateMemberPointer(E->getRHS(), RHSValue, Info) || !LHSOK)
4568b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith      return false;
4569b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith
4570b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    // C++11 [expr.eq]p2:
4571b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    //   If both operands are null, they compare equal. Otherwise if only one is
4572b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    //   null, they compare unequal.
4573b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    if (!LHSValue.getDecl() || !RHSValue.getDecl()) {
4574b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith      bool Equal = !LHSValue.getDecl() && !RHSValue.getDecl();
4575b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith      return Success(E->getOpcode() == BO_EQ ? Equal : !Equal, E);
4576b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    }
4577b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith
4578b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    //   Otherwise if either is a pointer to a virtual member function, the
4579b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    //   result is unspecified.
4580b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(LHSValue.getDecl()))
4581b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith      if (MD->isVirtual())
4582b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith        CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD;
4583b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(RHSValue.getDecl()))
4584b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith      if (MD->isVirtual())
4585b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith        CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD;
4586b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith
4587b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    //   Otherwise they compare equal if and only if they would refer to the
4588b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    //   same member of the same most derived object or the same subobject if
4589b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    //   they were dereferenced with a hypothetical object of the associated
4590b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    //   class type.
4591b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    bool Equal = LHSValue == RHSValue;
4592b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith    return Success(E->getOpcode() == BO_EQ ? Equal : !Equal, E);
4593b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith  }
4594b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith
45952ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor  if (!LHSTy->isIntegralOrEnumerationType() ||
45962ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor      !RHSTy->isIntegralOrEnumerationType()) {
4597e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    // We can't continue from here for non-integral types.
4598e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
4599a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman  }
4600a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman
4601a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman  // The LHS of a constant expr is always evaluated and needed.
460247a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith  CCValue LHSVal;
4603745f5147e065900267c85a5568785a1991d4838fRichard Smith
4604745f5147e065900267c85a5568785a1991d4838fRichard Smith  bool LHSOK = EvaluateIntegerOrLValue(E->getLHS(), LHSVal, Info);
4605745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!LHSOK && !Info.keepEvaluatingAfterFailure())
4606f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return false;
4607d9f4bcda18bfbf79341edd9d381d4b6a3cffe655Eli Friedman
4608745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!Visit(E->getRHS()) || !LHSOK)
460930c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar    return false;
4610745f5147e065900267c85a5568785a1991d4838fRichard Smith
461147a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith  CCValue &RHSVal = Result;
461242edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedman
461342edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedman  // Handle cases like (unsigned long)&a + 4.
4614c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) {
4615a73058324197b7bdfd19307965954f626e26199dKen Dyck    CharUnits AdditionalOffset = CharUnits::fromQuantity(
4616a73058324197b7bdfd19307965954f626e26199dKen Dyck                                     RHSVal.getInt().getZExtValue());
46172de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (E->getOpcode() == BO_Add)
461847a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith      LHSVal.getLValueOffset() += AdditionalOffset;
461942edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedman    else
462047a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith      LHSVal.getLValueOffset() -= AdditionalOffset;
462147a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    Result = LHSVal;
462242edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedman    return true;
462342edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedman  }
462442edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedman
462542edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedman  // Handle cases like 4 + (unsigned long)&a
46262de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  if (E->getOpcode() == BO_Add &&
4627c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith        RHSVal.isLValue() && LHSVal.isInt()) {
462847a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    RHSVal.getLValueOffset() += CharUnits::fromQuantity(
462947a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith                                    LHSVal.getInt().getZExtValue());
463047a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    // Note that RHSVal is Result.
463142edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedman    return true;
463242edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedman  }
463342edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedman
463465639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman  if (E->getOpcode() == BO_Sub && LHSVal.isLValue() && RHSVal.isLValue()) {
463565639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman    // Handle (intptr_t)&&A - (intptr_t)&&B.
463665639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman    if (!LHSVal.getLValueOffset().isZero() ||
463765639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman        !RHSVal.getLValueOffset().isZero())
463865639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman      return false;
463965639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman    const Expr *LHSExpr = LHSVal.getLValueBase().dyn_cast<const Expr*>();
464065639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman    const Expr *RHSExpr = RHSVal.getLValueBase().dyn_cast<const Expr*>();
464165639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman    if (!LHSExpr || !RHSExpr)
464265639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman      return false;
464365639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman    const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr);
464465639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman    const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr);
464565639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman    if (!LHSAddrExpr || !RHSAddrExpr)
464665639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman      return false;
46475930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman    // Make sure both labels come from the same function.
46485930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman    if (LHSAddrExpr->getLabel()->getDeclContext() !=
46495930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman        RHSAddrExpr->getLabel()->getDeclContext())
46505930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman      return false;
465165639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman    Result = CCValue(LHSAddrExpr, RHSAddrExpr);
465265639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman    return true;
465365639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman  }
465465639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman
465542edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedman  // All the following cases expect both operands to be an integer
4656c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  if (!LHSVal.isInt() || !RHSVal.isInt())
4657f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
4658a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman
4659c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  APSInt &LHS = LHSVal.getInt();
4660c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  APSInt &RHS = RHSVal.getInt();
466142edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedman
4662a25ae3d68d84d2b89907f998df6a396549589da5Anders Carlsson  switch (E->getOpcode()) {
466332fea9d18cc3658a1b01df5ca6f2ac302625c61dChris Lattner  default:
4664f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
46657b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith  case BO_Mul:
46667b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    return Success(CheckedIntArithmetic(Info, E, LHS, RHS,
46677b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith                                        LHS.getBitWidth() * 2,
46687b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith                                        std::multiplies<APSInt>()), E);
46697b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith  case BO_Add:
46707b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    return Success(CheckedIntArithmetic(Info, E, LHS, RHS,
46717b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith                                        LHS.getBitWidth() + 1,
46727b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith                                        std::plus<APSInt>()), E);
46737b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith  case BO_Sub:
46747b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    return Success(CheckedIntArithmetic(Info, E, LHS, RHS,
46757b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith                                        LHS.getBitWidth() + 1,
46767b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith                                        std::minus<APSInt>()), E);
4677c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  case BO_And: return Success(LHS & RHS, E);
4678c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  case BO_Xor: return Success(LHS ^ RHS, E);
4679c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  case BO_Or:  return Success(LHS | RHS, E);
46802de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Div:
46813df61308ddfbdba0897b762a129b5a38750c87d0Richard Smith  case BO_Rem:
468254176fdb044312b4b77c3da6682d3575b3728d30Chris Lattner    if (RHS == 0)
4683f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E, diag::note_expr_divide_by_zero);
46843df61308ddfbdba0897b762a129b5a38750c87d0Richard Smith    // Check for overflow case: INT_MIN / -1 or INT_MIN % -1. The latter is not
46853df61308ddfbdba0897b762a129b5a38750c87d0Richard Smith    // actually undefined behavior in C++11 due to a language defect.
4686789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    if (RHS.isNegative() && RHS.isAllOnesValue() &&
4687789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith        LHS.isSigned() && LHS.isMinSignedValue())
4688789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith      HandleOverflow(Info, E, -LHS.extend(LHS.getBitWidth() + 1), E->getType());
46893df61308ddfbdba0897b762a129b5a38750c87d0Richard Smith    return Success(E->getOpcode() == BO_Rem ? LHS % RHS : LHS / RHS, E);
46902de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Shl: {
4691789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    // During constant-folding, a negative shift is an opposite shift. Such a
4692789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    // shift is not a constant expression.
4693091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall    if (RHS.isSigned() && RHS.isNegative()) {
4694789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith      CCEDiag(E, diag::note_constexpr_negative_shift) << RHS;
4695091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall      RHS = -RHS;
4696091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall      goto shift_right;
4697091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall    }
4698091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall
4699091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall  shift_left:
4700789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    // C++11 [expr.shift]p1: Shift width must be less than the bit width of the
4701789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    // shifted type.
4702789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1);
4703789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    if (SA != RHS) {
4704789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith      CCEDiag(E, diag::note_constexpr_large_shift)
4705789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith        << RHS << E->getType() << LHS.getBitWidth();
4706789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    } else if (LHS.isSigned()) {
4707789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith      // C++11 [expr.shift]p2: A signed left shift must have a non-negative
4708789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith      // operand, and must not overflow.
4709789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith      if (LHS.isNegative())
4710789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith        CCEDiag(E, diag::note_constexpr_lshift_of_negative) << LHS;
4711789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith      else if (LHS.countLeadingZeros() <= SA)
4712789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith        HandleOverflow(Info, E, LHS.extend(LHS.getBitWidth() + SA) << SA,
4713789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith                       E->getType());
4714789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    }
4715789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith
4716c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return Success(LHS << SA, E);
47173f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar  }
47182de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Shr: {
4719789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    // During constant-folding, a negative shift is an opposite shift. Such a
4720789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    // shift is not a constant expression.
4721091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall    if (RHS.isSigned() && RHS.isNegative()) {
4722789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith      CCEDiag(E, diag::note_constexpr_negative_shift) << RHS;
4723091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall      RHS = -RHS;
4724091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall      goto shift_left;
4725091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall    }
4726091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall
4727091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall  shift_right:
4728789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    // C++11 [expr.shift]p1: Shift width must be less than the bit width of the
4729789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    // shifted type.
4730789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1);
4731789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    if (SA != RHS)
4732789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith      CCEDiag(E, diag::note_constexpr_large_shift)
4733789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith        << RHS << E->getType() << LHS.getBitWidth();
4734789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith
4735c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return Success(LHS >> SA, E);
47363f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar  }
47371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4738c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  case BO_LT: return Success(LHS < RHS, E);
4739c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  case BO_GT: return Success(LHS > RHS, E);
4740c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  case BO_LE: return Success(LHS <= RHS, E);
4741c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  case BO_GE: return Success(LHS >= RHS, E);
4742c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  case BO_EQ: return Success(LHS == RHS, E);
4743c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  case BO_NE: return Success(LHS != RHS, E);
4744b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman  }
4745a25ae3d68d84d2b89907f998df6a396549589da5Anders Carlsson}
4746a25ae3d68d84d2b89907f998df6a396549589da5Anders Carlsson
47478b752f10c394b140f9ef89e049cbad1a7676fc25Ken DyckCharUnits IntExprEvaluator::GetAlignOfType(QualType T) {
47485d484e8cf710207010720589d89602233de61d01Sebastian Redl  // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
47495d484e8cf710207010720589d89602233de61d01Sebastian Redl  //   the result is the size of the referenced type."
47505d484e8cf710207010720589d89602233de61d01Sebastian Redl  // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
47515d484e8cf710207010720589d89602233de61d01Sebastian Redl  //   result shall be the alignment of the referenced type."
47525d484e8cf710207010720589d89602233de61d01Sebastian Redl  if (const ReferenceType *Ref = T->getAs<ReferenceType>())
47535d484e8cf710207010720589d89602233de61d01Sebastian Redl    T = Ref->getPointeeType();
47549f1210c3280104417a4ad30f0a00825ac8fa718aChad Rosier
47559f1210c3280104417a4ad30f0a00825ac8fa718aChad Rosier  // __alignof is defined to return the preferred alignment.
47569f1210c3280104417a4ad30f0a00825ac8fa718aChad Rosier  return Info.Ctx.toCharUnitsFromBits(
47579f1210c3280104417a4ad30f0a00825ac8fa718aChad Rosier    Info.Ctx.getPreferredTypeAlign(T.getTypePtr()));
4758e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner}
4759e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner
47608b752f10c394b140f9ef89e049cbad1a7676fc25Ken DyckCharUnits IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
4761af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  E = E->IgnoreParens();
4762af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner
4763af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  // alignof decl is always accepted, even if it doesn't make sense: we default
47641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // to 1 in those cases.
4765af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
47668b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck    return Info.Ctx.getDeclAlign(DRE->getDecl(),
47678b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck                                 /*RefAsPointee*/true);
4768a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman
4769af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
47708b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck    return Info.Ctx.getDeclAlign(ME->getMemberDecl(),
47718b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck                                 /*RefAsPointee*/true);
4772af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner
4773e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner  return GetAlignOfType(E->getType());
4774e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner}
4775e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner
4776e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner
4777f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne/// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with
4778f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne/// a result as the expression's type.
4779f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbournebool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr(
4780f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne                                    const UnaryExprOrTypeTraitExpr *E) {
4781f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  switch(E->getKind()) {
4782f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  case UETT_AlignOf: {
4783e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner    if (E->isArgumentType())
47844f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck      return Success(GetAlignOfType(E->getArgumentType()), E);
4785e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner    else
47864f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck      return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
4787e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner  }
4788a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman
4789f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  case UETT_VecStep: {
4790f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    QualType Ty = E->getTypeOfArgument();
47910518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl
4792f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    if (Ty->isVectorType()) {
4793f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne      unsigned n = Ty->getAs<VectorType>()->getNumElements();
4794a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman
4795f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne      // The vec_step built-in functions that take a 3-component
4796f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne      // vector return 4. (OpenCL 1.1 spec 6.11.12)
4797f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne      if (n == 3)
4798f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne        n = 4;
4799f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne
4800f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne      return Success(n, E);
4801f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    } else
4802f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne      return Success(1, E);
4803f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  }
4804f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne
4805f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  case UETT_SizeOf: {
4806f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    QualType SrcTy = E->getTypeOfArgument();
4807f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
4808f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    //   the result is the size of the referenced type."
4809f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
4810f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    //   result shall be the alignment of the referenced type."
4811f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>())
4812f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne      SrcTy = Ref->getPointeeType();
4813f2da9dfef96dc11b7b5effb1d02cb427b2d71599Eli Friedman
4814180f47959a066795cc0f409433023af448bb0328Richard Smith    CharUnits Sizeof;
4815180f47959a066795cc0f409433023af448bb0328Richard Smith    if (!HandleSizeof(Info, SrcTy, Sizeof))
4816f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne      return false;
4817180f47959a066795cc0f409433023af448bb0328Richard Smith    return Success(Sizeof, E);
4818f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  }
4819f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  }
4820f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne
4821f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  llvm_unreachable("unknown expr/type trait");
4822fcee0019b76f9f368f2b3d6d4048a98232593f29Chris Lattner}
4823fcee0019b76f9f368f2b3d6d4048a98232593f29Chris Lattner
48248cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) {
48258ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  CharUnits Result;
48268cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  unsigned n = OOE->getNumComponents();
48278ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  if (n == 0)
4828f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(OOE);
48298cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  QualType CurrentType = OOE->getTypeSourceInfo()->getType();
48308ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  for (unsigned i = 0; i != n; ++i) {
48318ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    OffsetOfExpr::OffsetOfNode ON = OOE->getComponent(i);
48328ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    switch (ON.getKind()) {
48338ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    case OffsetOfExpr::OffsetOfNode::Array: {
48348cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne      const Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex());
48358ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      APSInt IdxResult;
48368ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      if (!EvaluateInteger(Idx, IdxResult, Info))
48378ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor        return false;
48388ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType);
48398ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      if (!AT)
4840f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(OOE);
48418ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      CurrentType = AT->getElementType();
48428ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType);
48438ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      Result += IdxResult.getSExtValue() * ElementSize;
48448ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor        break;
48458ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    }
4846f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
48478ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    case OffsetOfExpr::OffsetOfNode::Field: {
48488ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      FieldDecl *MemberDecl = ON.getField();
48498ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      const RecordType *RT = CurrentType->getAs<RecordType>();
4850f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      if (!RT)
4851f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(OOE);
48528ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      RecordDecl *RD = RT->getDecl();
48538ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
4854ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      unsigned i = MemberDecl->getFieldIndex();
4855cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      assert(i < RL.getFieldCount() && "offsetof field in wrong type");
4856fb1e3bc29b667f4275e1d5a43d64ec173f4f9a7dKen Dyck      Result += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i));
48578ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      CurrentType = MemberDecl->getType().getNonReferenceType();
48588ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      break;
48598ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    }
4860f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
48618ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    case OffsetOfExpr::OffsetOfNode::Identifier:
48628ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor      llvm_unreachable("dependent __builtin_offsetof");
4863f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
4864cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor    case OffsetOfExpr::OffsetOfNode::Base: {
4865cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      CXXBaseSpecifier *BaseSpec = ON.getBase();
4866cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      if (BaseSpec->isVirtual())
4867f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(OOE);
4868cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor
4869cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      // Find the layout of the class whose base we are looking into.
4870cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      const RecordType *RT = CurrentType->getAs<RecordType>();
4871f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      if (!RT)
4872f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(OOE);
4873cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      RecordDecl *RD = RT->getDecl();
4874cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
4875cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor
4876cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      // Find the base class itself.
4877cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      CurrentType = BaseSpec->getType();
4878cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      const RecordType *BaseRT = CurrentType->getAs<RecordType>();
4879cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      if (!BaseRT)
4880f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(OOE);
4881cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor
4882cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      // Add the offset to the base.
48837c7f820d70c925b29290a8563b59615816a827fcKen Dyck      Result += RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl()));
4884cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor      break;
4885cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor    }
48868ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor    }
48878ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor  }
48888cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  return Success(Result, OOE);
48898ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor}
48908ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor
4891b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattnerbool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
489275a4881047deeb3a300ff9293dc6ba8570048bb5Chris Lattner  switch (E->getOpcode()) {
48934c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner  default:
489475a4881047deeb3a300ff9293dc6ba8570048bb5Chris Lattner    // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
489575a4881047deeb3a300ff9293dc6ba8570048bb5Chris Lattner    // See C99 6.6p3.
4896f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
48972de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Extension:
48984c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner    // FIXME: Should extension allow i-c-e extension expressions in its scope?
48994c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner    // If so, we could clear the diagnostic ID.
4900f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Visit(E->getSubExpr());
49012de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Plus:
4902c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    // The result is just the value.
4903f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Visit(E->getSubExpr());
4904f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  case UO_Minus: {
4905f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!Visit(E->getSubExpr()))
4906f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return false;
4907f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!Result.isInt()) return Error(E);
4908789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    const APSInt &Value = Result.getInt();
4909789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    if (Value.isSigned() && Value.isMinSignedValue())
4910789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith      HandleOverflow(Info, E, -Value.extend(Value.getBitWidth() + 1),
4911789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith                     E->getType());
4912789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith    return Success(-Value, E);
4913f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
4914f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  case UO_Not: {
4915f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!Visit(E->getSubExpr()))
4916f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return false;
4917f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!Result.isInt()) return Error(E);
4918f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Success(~Result.getInt(), E);
4919f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
4920f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  case UO_LNot: {
4921f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    bool bres;
4922f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!EvaluateAsBooleanCondition(E->getSubExpr(), bres, Info))
4923f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return false;
4924f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Success(!bres, E);
4925f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
492606a3675627e3b3c47b49c689c8e404a33144194aAnders Carlsson  }
4927a25ae3d68d84d2b89907f998df6a396549589da5Anders Carlsson}
49281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4929732b2236ae4a4f11e7642677cebbd169c07ea877Chris Lattner/// HandleCast - This is used to evaluate implicit or explicit casts where the
4930732b2236ae4a4f11e7642677cebbd169c07ea877Chris Lattner/// result type is integer.
49318cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool IntExprEvaluator::VisitCastExpr(const CastExpr *E) {
49328cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  const Expr *SubExpr = E->getSubExpr();
493382206e267ce6cc709797127616f64672d255b310Anders Carlsson  QualType DestType = E->getType();
4934b92dac8bc2f6f73919825f9af693a8a7e89ae1d4Daniel Dunbar  QualType SrcType = SubExpr->getType();
493582206e267ce6cc709797127616f64672d255b310Anders Carlsson
493646a523285928aa07bf14803178dc04616ac85994Eli Friedman  switch (E->getCastKind()) {
493746a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_BaseToDerived:
493846a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_DerivedToBase:
493946a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_UncheckedDerivedToBase:
494046a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_Dynamic:
494146a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_ToUnion:
494246a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_ArrayToPointerDecay:
494346a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_FunctionToPointerDecay:
494446a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_NullToPointer:
494546a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_NullToMemberPointer:
494646a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_BaseToDerivedMemberPointer:
494746a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_DerivedToBaseMemberPointer:
494846a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_ConstructorConversion:
494946a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_IntegralToPointer:
495046a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_ToVoid:
495146a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_VectorSplat:
495246a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_IntegralToFloating:
495346a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_FloatingCast:
49541d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_CPointerToObjCPointerCast:
49551d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_BlockPointerToObjCPointerCast:
495646a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_AnyPointerToBlockPointerCast:
495746a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_ObjCObjectLValueCast:
495846a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_FloatingRealToComplex:
495946a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_FloatingComplexToReal:
496046a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_FloatingComplexCast:
496146a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_FloatingComplexToIntegralComplex:
496246a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_IntegralRealToComplex:
496346a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_IntegralComplexCast:
496446a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_IntegralComplexToFloatingComplex:
496546a523285928aa07bf14803178dc04616ac85994Eli Friedman    llvm_unreachable("invalid cast kind for integral value");
496646a523285928aa07bf14803178dc04616ac85994Eli Friedman
4967e50c297f92914ca996deb8b597624193273b62e4Eli Friedman  case CK_BitCast:
496846a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_Dependent:
496946a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_LValueBitCast:
497033e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCProduceObject:
497133e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCConsumeObject:
497233e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCReclaimReturnedObject:
497333e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCExtendBlockObject:
4974f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
497546a523285928aa07bf14803178dc04616ac85994Eli Friedman
49767d580a4e9e47dffc3c17aa2b957ac57ca3c4e451Richard Smith  case CK_UserDefinedConversion:
497746a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_LValueToRValue:
49787a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall  case CK_AtomicToNonAtomic:
49797a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall  case CK_NonAtomicToAtomic:
498046a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_NoOp:
4981c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return ExprEvaluatorBaseTy::VisitCastExpr(E);
498246a523285928aa07bf14803178dc04616ac85994Eli Friedman
498346a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_MemberPointerToBoolean:
498446a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_PointerToBoolean:
498546a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_IntegralToBoolean:
498646a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_FloatingToBoolean:
498746a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_FloatingComplexToBoolean:
498846a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_IntegralComplexToBoolean: {
49894efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman    bool BoolResult;
4990c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    if (!EvaluateAsBooleanCondition(SubExpr, BoolResult, Info))
49914efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman      return false;
4992131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar    return Success(BoolResult, E);
49934efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman  }
49944efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
499546a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_IntegralCast: {
4996732b2236ae4a4f11e7642677cebbd169c07ea877Chris Lattner    if (!Visit(SubExpr))
4997b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattner      return false;
4998a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar
4999be26570e3faa009bdcefedfaf04473e518940520Eli Friedman    if (!Result.isInt()) {
500065639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman      // Allow casts of address-of-label differences if they are no-ops
500165639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman      // or narrowing.  (The narrowing case isn't actually guaranteed to
500265639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman      // be constant-evaluatable except in some narrow cases which are hard
500365639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman      // to detect here.  We let it through on the assumption the user knows
500465639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman      // what they are doing.)
500565639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman      if (Result.isAddrLabelDiff())
500665639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman        return Info.Ctx.getTypeSize(DestType) <= Info.Ctx.getTypeSize(SrcType);
5007be26570e3faa009bdcefedfaf04473e518940520Eli Friedman      // Only allow casts of lvalues if they are lossless.
5008be26570e3faa009bdcefedfaf04473e518940520Eli Friedman      return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
5009be26570e3faa009bdcefedfaf04473e518940520Eli Friedman    }
501030c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar
5011f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith    return Success(HandleIntToIntCast(Info, E, DestType, SrcType,
5012f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith                                      Result.getInt()), E);
5013732b2236ae4a4f11e7642677cebbd169c07ea877Chris Lattner  }
50141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
501546a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_PointerToIntegral: {
5016c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith    CCEDiag(E, diag::note_constexpr_invalid_cast) << 2;
5017c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith
5018efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    LValue LV;
501987eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner    if (!EvaluatePointer(SubExpr, LV, Info))
5020b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattner      return false;
50214efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
5022dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar    if (LV.getLValueBase()) {
5023dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar      // Only allow based lvalue casts if they are lossless.
5024f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith      // FIXME: Allow a larger integer size than the pointer size, and allow
5025f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith      // narrowing back down to pointer width in subsequent integral casts.
5026f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith      // FIXME: Check integer type's active bits, not its type size.
5027dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar      if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
5028f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(E);
5029dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar
5030b755a9da095d2f2f04444797f1e1a9511693815bRichard Smith      LV.Designator.setInvalid();
5031efdb83e26f9a1fd2566afe54461216cd84814d42John McCall      LV.moveInto(Result);
5032dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar      return true;
5033dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar    }
50344efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
5035a73058324197b7bdfd19307965954f626e26199dKen Dyck    APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(),
5036a73058324197b7bdfd19307965954f626e26199dKen Dyck                                         SrcType);
5037f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith    return Success(HandleIntToIntCast(Info, E, DestType, SrcType, AsInt), E);
50382bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson  }
50394efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
504046a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_IntegralComplexToReal: {
5041f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    ComplexValue C;
50421725f683432715e5afe34d476024bd6f16eac3fcEli Friedman    if (!EvaluateComplex(SubExpr, C, Info))
50431725f683432715e5afe34d476024bd6f16eac3fcEli Friedman      return false;
504446a523285928aa07bf14803178dc04616ac85994Eli Friedman    return Success(C.getComplexIntReal(), E);
50451725f683432715e5afe34d476024bd6f16eac3fcEli Friedman  }
50462217c87bdc5ab357046a5453bdb06f469c41024eEli Friedman
504746a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_FloatingToIntegral: {
504846a523285928aa07bf14803178dc04616ac85994Eli Friedman    APFloat F(0.0);
504946a523285928aa07bf14803178dc04616ac85994Eli Friedman    if (!EvaluateFloat(SubExpr, F, Info))
505046a523285928aa07bf14803178dc04616ac85994Eli Friedman      return false;
5051732b2236ae4a4f11e7642677cebbd169c07ea877Chris Lattner
5052c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    APSInt Value;
5053c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    if (!HandleFloatToIntCast(Info, E, SrcType, F, DestType, Value))
5054c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      return false;
5055c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return Success(Value, E);
505646a523285928aa07bf14803178dc04616ac85994Eli Friedman  }
505746a523285928aa07bf14803178dc04616ac85994Eli Friedman  }
50581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
505946a523285928aa07bf14803178dc04616ac85994Eli Friedman  llvm_unreachable("unknown cast resulting in integral value");
5060a25ae3d68d84d2b89907f998df6a396549589da5Anders Carlsson}
50612bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson
5062722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedmanbool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
5063722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman  if (E->getSubExpr()->getType()->isAnyComplexType()) {
5064f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    ComplexValue LV;
5065f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!EvaluateComplex(E->getSubExpr(), LV, Info))
5066f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return false;
5067f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!LV.isComplexInt())
5068f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E);
5069722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman    return Success(LV.getComplexIntReal(), E);
5070722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman  }
5071722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman
5072722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman  return Visit(E->getSubExpr());
5073722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman}
5074722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman
5075664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedmanbool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
5076722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman  if (E->getSubExpr()->getType()->isComplexIntegerType()) {
5077f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    ComplexValue LV;
5078f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!EvaluateComplex(E->getSubExpr(), LV, Info))
5079f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return false;
5080f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!LV.isComplexInt())
5081f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E);
5082722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman    return Success(LV.getComplexIntImag(), E);
5083722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman  }
5084722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman
50858327fad71da34492d82c532f42a58cb4baff81a3Richard Smith  VisitIgnoredValue(E->getSubExpr());
5086664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman  return Success(0, E);
5087664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman}
5088664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman
5089ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregorbool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) {
5090ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor  return Success(E->getPackLength(), E);
5091ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor}
5092ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor
5093295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redlbool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) {
5094295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl  return Success(E->getValue(), E);
5095295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl}
5096295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl
5097f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===//
5098d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman// Float Evaluation
5099d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman//===----------------------------------------------------------------------===//
5100d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman
5101d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmannamespace {
5102770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass FloatExprEvaluator
51038cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  : public ExprEvaluatorBase<FloatExprEvaluator, bool> {
5104d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman  APFloat &Result;
5105d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmanpublic:
5106d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman  FloatExprEvaluator(EvalInfo &info, APFloat &result)
51078cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    : ExprEvaluatorBaseTy(info), Result(result) {}
5108d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman
510947a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith  bool Success(const CCValue &V, const Expr *e) {
51108cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    Result = V.getFloat();
51118cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    return true;
51128cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  }
5113d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman
511451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  bool ZeroInitialization(const Expr *E) {
5115f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith    Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
5116f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith    return true;
5117f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith  }
5118f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith
5119019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  bool VisitCallExpr(const CallExpr *E);
5120d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman
51215db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar  bool VisitUnaryOperator(const UnaryOperator *E);
5122d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman  bool VisitBinaryOperator(const BinaryOperator *E);
5123d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman  bool VisitFloatingLiteral(const FloatingLiteral *E);
51248cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCastExpr(const CastExpr *E);
51252217c87bdc5ab357046a5453bdb06f469c41024eEli Friedman
5126abd3a857ace59100305790545d1baae5877b8945John McCall  bool VisitUnaryReal(const UnaryOperator *E);
5127abd3a857ace59100305790545d1baae5877b8945John McCall  bool VisitUnaryImag(const UnaryOperator *E);
5128ba98d6bb414861965a1f22628494ea046785ecd4Eli Friedman
512951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  // FIXME: Missing: array subscript of vector, member of vector
5130d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman};
5131d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman} // end anonymous namespace
5132d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman
5133d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmanstatic bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
5134c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  assert(E->isRValue() && E->getType()->isRealFloatingType());
51358cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  return FloatExprEvaluator(Info, Result).Visit(E);
5136d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman}
5137d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman
51384ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadstatic bool TryEvaluateBuiltinNaN(const ASTContext &Context,
5139db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall                                  QualType ResultTy,
5140db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall                                  const Expr *Arg,
5141db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall                                  bool SNaN,
5142db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall                                  llvm::APFloat &Result) {
5143db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts());
5144db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  if (!S) return false;
5145db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall
5146db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy);
5147db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall
5148db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  llvm::APInt fill;
5149db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall
5150db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  // Treat empty strings as if they were zero.
5151db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  if (S->getString().empty())
5152db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall    fill = llvm::APInt(32, 0);
5153db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  else if (S->getString().getAsInteger(0, fill))
5154db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall    return false;
5155db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall
5156db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  if (SNaN)
5157db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall    Result = llvm::APFloat::getSNaN(Sem, false, &fill);
5158db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  else
5159db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall    Result = llvm::APFloat::getQNaN(Sem, false, &fill);
5160db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  return true;
5161db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall}
5162db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall
5163019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattnerbool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
5164180f47959a066795cc0f409433023af448bb0328Richard Smith  switch (E->isBuiltinCall()) {
51658cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  default:
51668cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    return ExprEvaluatorBaseTy::VisitCallExpr(E);
51678cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne
5168019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  case Builtin::BI__builtin_huge_val:
5169019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  case Builtin::BI__builtin_huge_valf:
5170019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  case Builtin::BI__builtin_huge_vall:
5171019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  case Builtin::BI__builtin_inf:
5172019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  case Builtin::BI__builtin_inff:
51737cbed03c00e246682e5292785d01e1c120ce54bdDaniel Dunbar  case Builtin::BI__builtin_infl: {
51747cbed03c00e246682e5292785d01e1c120ce54bdDaniel Dunbar    const llvm::fltSemantics &Sem =
51757cbed03c00e246682e5292785d01e1c120ce54bdDaniel Dunbar      Info.Ctx.getFloatTypeSemantics(E->getType());
517634a74ab81600a40c6324fd76adb724b803dfaf91Chris Lattner    Result = llvm::APFloat::getInf(Sem);
517734a74ab81600a40c6324fd76adb724b803dfaf91Chris Lattner    return true;
51787cbed03c00e246682e5292785d01e1c120ce54bdDaniel Dunbar  }
51791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5180db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  case Builtin::BI__builtin_nans:
5181db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  case Builtin::BI__builtin_nansf:
5182db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall  case Builtin::BI__builtin_nansl:
5183f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
5184f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                               true, Result))
5185f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E);
5186f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return true;
5187db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall
51889e62171a25e3a08fb5c49fb370f83faf5ae786f5Chris Lattner  case Builtin::BI__builtin_nan:
51899e62171a25e3a08fb5c49fb370f83faf5ae786f5Chris Lattner  case Builtin::BI__builtin_nanf:
51909e62171a25e3a08fb5c49fb370f83faf5ae786f5Chris Lattner  case Builtin::BI__builtin_nanl:
51914572baba9d18c275968ac113fd73b0e3c77cccb8Mike Stump    // If this is __builtin_nan() turn this into a nan, otherwise we
51929e62171a25e3a08fb5c49fb370f83faf5ae786f5Chris Lattner    // can't constant fold it.
5193f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
5194f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                               false, Result))
5195f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return Error(E);
5196f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return true;
51975db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar
51985db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar  case Builtin::BI__builtin_fabs:
51995db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar  case Builtin::BI__builtin_fabsf:
52005db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar  case Builtin::BI__builtin_fabsl:
52015db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar    if (!EvaluateFloat(E->getArg(0), Result, Info))
52025db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar      return false;
52031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
52045db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar    if (Result.isNegative())
52055db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar      Result.changeSign();
52065db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar    return true;
52075db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar
52081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  case Builtin::BI__builtin_copysign:
52091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  case Builtin::BI__builtin_copysignf:
52105db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar  case Builtin::BI__builtin_copysignl: {
52115db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar    APFloat RHS(0.);
52125db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar    if (!EvaluateFloat(E->getArg(0), Result, Info) ||
52135db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar        !EvaluateFloat(E->getArg(1), RHS, Info))
52145db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar      return false;
52155db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar    Result.copySign(RHS);
52165db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar    return true;
52175db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar  }
5218019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner  }
5219019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner}
5220019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner
5221abd3a857ace59100305790545d1baae5877b8945John McCallbool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
522243efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman  if (E->getSubExpr()->getType()->isAnyComplexType()) {
522343efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman    ComplexValue CV;
522443efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman    if (!EvaluateComplex(E->getSubExpr(), CV, Info))
522543efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman      return false;
522643efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman    Result = CV.FloatReal;
522743efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman    return true;
522843efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman  }
522943efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman
523043efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman  return Visit(E->getSubExpr());
5231abd3a857ace59100305790545d1baae5877b8945John McCall}
5232abd3a857ace59100305790545d1baae5877b8945John McCall
5233abd3a857ace59100305790545d1baae5877b8945John McCallbool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
523443efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman  if (E->getSubExpr()->getType()->isAnyComplexType()) {
523543efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman    ComplexValue CV;
523643efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman    if (!EvaluateComplex(E->getSubExpr(), CV, Info))
523743efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman      return false;
523843efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman    Result = CV.FloatImag;
523943efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman    return true;
524043efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman  }
524143efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman
52428327fad71da34492d82c532f42a58cb4baff81a3Richard Smith  VisitIgnoredValue(E->getSubExpr());
524343efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman  const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(E->getType());
524443efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman  Result = llvm::APFloat::getZero(Sem);
5245abd3a857ace59100305790545d1baae5877b8945John McCall  return true;
5246abd3a857ace59100305790545d1baae5877b8945John McCall}
5247abd3a857ace59100305790545d1baae5877b8945John McCall
52485db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbarbool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
52495db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar  switch (E->getOpcode()) {
5250f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  default: return Error(E);
52512de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Plus:
52527993e8a2a26bf408c2a6d45f24fffa12db336b2bRichard Smith    return EvaluateFloat(E->getSubExpr(), Result, Info);
52532de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case UO_Minus:
52547993e8a2a26bf408c2a6d45f24fffa12db336b2bRichard Smith    if (!EvaluateFloat(E->getSubExpr(), Result, Info))
52557993e8a2a26bf408c2a6d45f24fffa12db336b2bRichard Smith      return false;
52565db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar    Result.changeSign();
52575db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar    return true;
52585db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar  }
52595db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar}
5260019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner
5261d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmanbool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
5262e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma)
5263e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
526496e93660124c8028a4c3bcc038ab0cdd18cd7ab2Anders Carlsson
52655db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar  APFloat RHS(0.0);
5266745f5147e065900267c85a5568785a1991d4838fRichard Smith  bool LHSOK = EvaluateFloat(E->getLHS(), Result, Info);
5267745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!LHSOK && !Info.keepEvaluatingAfterFailure())
5268d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman    return false;
5269745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!EvaluateFloat(E->getRHS(), RHS, Info) || !LHSOK)
5270d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman    return false;
5271d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman
5272d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman  switch (E->getOpcode()) {
5273f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  default: return Error(E);
52742de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Mul:
5275d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman    Result.multiply(RHS, APFloat::rmNearestTiesToEven);
52767b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    break;
52772de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Add:
5278d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman    Result.add(RHS, APFloat::rmNearestTiesToEven);
52797b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    break;
52802de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Sub:
5281d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman    Result.subtract(RHS, APFloat::rmNearestTiesToEven);
52827b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    break;
52832de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Div:
5284d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman    Result.divide(RHS, APFloat::rmNearestTiesToEven);
52857b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    break;
5286d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman  }
52877b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith
52887b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith  if (Result.isInfinity() || Result.isNaN())
52897b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith    CCEDiag(E, diag::note_constexpr_float_arithmetic) << Result.isNaN();
52907b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith  return true;
5291d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman}
5292d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman
5293d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmanbool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
5294d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman  Result = E->getValue();
5295d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman  return true;
5296d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman}
5297d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman
52988cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) {
52998cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  const Expr* SubExpr = E->getSubExpr();
53001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
53012a523eec6a31955be876625819b89e8dc5def707Eli Friedman  switch (E->getCastKind()) {
53022a523eec6a31955be876625819b89e8dc5def707Eli Friedman  default:
5303c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return ExprEvaluatorBaseTy::VisitCastExpr(E);
53042a523eec6a31955be876625819b89e8dc5def707Eli Friedman
53052a523eec6a31955be876625819b89e8dc5def707Eli Friedman  case CK_IntegralToFloating: {
53064efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman    APSInt IntResult;
5307c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return EvaluateInteger(SubExpr, IntResult, Info) &&
5308c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith           HandleIntToFloatCast(Info, E, SubExpr->getType(), IntResult,
5309c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                E->getType(), Result);
53104efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman  }
53112a523eec6a31955be876625819b89e8dc5def707Eli Friedman
53122a523eec6a31955be876625819b89e8dc5def707Eli Friedman  case CK_FloatingCast: {
53134efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman    if (!Visit(SubExpr))
53144efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman      return false;
5315c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return HandleFloatToFloatCast(Info, E, SubExpr->getType(), E->getType(),
5316c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                  Result);
53174efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman  }
5318f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall
53192a523eec6a31955be876625819b89e8dc5def707Eli Friedman  case CK_FloatingComplexToReal: {
5320f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    ComplexValue V;
5321f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    if (!EvaluateComplex(SubExpr, V, Info))
5322f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall      return false;
5323f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    Result = V.getComplexFloatReal();
5324f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall    return true;
5325f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall  }
53262a523eec6a31955be876625819b89e8dc5def707Eli Friedman  }
53274efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman}
53284efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman
5329d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman//===----------------------------------------------------------------------===//
5330a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar// Complex Evaluation (for float and integer)
53319ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson//===----------------------------------------------------------------------===//
53329ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson
53339ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlssonnamespace {
5334770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass ComplexExprEvaluator
53358cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  : public ExprEvaluatorBase<ComplexExprEvaluator, bool> {
5336f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall  ComplexValue &Result;
53371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
53389ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlssonpublic:
5339f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall  ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result)
53408cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    : ExprEvaluatorBaseTy(info), Result(Result) {}
53411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
534247a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith  bool Success(const CCValue &V, const Expr *e) {
53438cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    Result.setFrom(V);
53448cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne    return true;
53458cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  }
53461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
53477ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  bool ZeroInitialization(const Expr *E);
53487ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman
53498cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  //===--------------------------------------------------------------------===//
53508cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  //                            Visitor Methods
53518cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  //===--------------------------------------------------------------------===//
53529ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson
53538cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitImaginaryLiteral(const ImaginaryLiteral *E);
53548cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  bool VisitCastExpr(const CastExpr *E);
5355b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman  bool VisitBinaryOperator(const BinaryOperator *E);
535696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  bool VisitUnaryOperator(const UnaryOperator *E);
53577ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  bool VisitInitListExpr(const InitListExpr *E);
5358b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman};
5359b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman} // end anonymous namespace
53601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5361b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedmanstatic bool EvaluateComplex(const Expr *E, ComplexValue &Result,
5362b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman                            EvalInfo &Info) {
5363c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  assert(E->isRValue() && E->getType()->isAnyComplexType());
53648cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  return ComplexExprEvaluator(Info, Result).Visit(E);
5365b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman}
5366b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman
53677ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedmanbool ComplexExprEvaluator::ZeroInitialization(const Expr *E) {
5368f6c17a439f3320ac620639a3ee66dbdabb93810cEli Friedman  QualType ElemTy = E->getType()->getAs<ComplexType>()->getElementType();
53697ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  if (ElemTy->isRealFloatingType()) {
53707ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    Result.makeComplexFloat();
53717ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    APFloat Zero = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(ElemTy));
53727ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    Result.FloatReal = Zero;
53737ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    Result.FloatImag = Zero;
53747ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  } else {
53757ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    Result.makeComplexInt();
53767ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    APSInt Zero = Info.Ctx.MakeIntValue(0, ElemTy);
53777ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    Result.IntReal = Zero;
53787ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    Result.IntImag = Zero;
53797ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  }
53807ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  return true;
53817ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman}
53827ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman
53838cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) {
53848cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne  const Expr* SubExpr = E->getSubExpr();
5385b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman
5386b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman  if (SubExpr->getType()->isRealFloatingType()) {
5387b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    Result.makeComplexFloat();
5388b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    APFloat &Imag = Result.FloatImag;
5389b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    if (!EvaluateFloat(SubExpr, Imag, Info))
5390b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman      return false;
5391b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman
5392b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    Result.FloatReal = APFloat(Imag.getSemantics());
5393b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    return true;
5394b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman  } else {
5395b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    assert(SubExpr->getType()->isIntegerType() &&
5396b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman           "Unexpected imaginary literal.");
5397b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman
5398b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    Result.makeComplexInt();
5399b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    APSInt &Imag = Result.IntImag;
5400b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    if (!EvaluateInteger(SubExpr, Imag, Info))
5401b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman      return false;
5402b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman
5403b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned());
5404b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    return true;
5405b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman  }
5406b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman}
5407b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman
54088cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) {
5409b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman
54108786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  switch (E->getCastKind()) {
54118786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_BitCast:
54128786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_BaseToDerived:
54138786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_DerivedToBase:
54148786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_UncheckedDerivedToBase:
54158786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_Dynamic:
54168786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_ToUnion:
54178786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_ArrayToPointerDecay:
54188786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_FunctionToPointerDecay:
54198786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_NullToPointer:
54208786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_NullToMemberPointer:
54218786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_BaseToDerivedMemberPointer:
54228786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_DerivedToBaseMemberPointer:
54238786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_MemberPointerToBoolean:
54248786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_ConstructorConversion:
54258786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_IntegralToPointer:
54268786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_PointerToIntegral:
54278786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_PointerToBoolean:
54288786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_ToVoid:
54298786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_VectorSplat:
54308786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_IntegralCast:
54318786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_IntegralToBoolean:
54328786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_IntegralToFloating:
54338786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_FloatingToIntegral:
54348786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_FloatingToBoolean:
54358786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_FloatingCast:
54361d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_CPointerToObjCPointerCast:
54371d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_BlockPointerToObjCPointerCast:
54388786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_AnyPointerToBlockPointerCast:
54398786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_ObjCObjectLValueCast:
54408786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_FloatingComplexToReal:
54418786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_FloatingComplexToBoolean:
54428786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_IntegralComplexToReal:
54438786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_IntegralComplexToBoolean:
544433e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCProduceObject:
544533e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCConsumeObject:
544633e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCReclaimReturnedObject:
544733e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCExtendBlockObject:
54488786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    llvm_unreachable("invalid cast kind for complex value");
54498786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
54508786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_LValueToRValue:
54517a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall  case CK_AtomicToNonAtomic:
54527a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall  case CK_NonAtomicToAtomic:
54538786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_NoOp:
5454c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return ExprEvaluatorBaseTy::VisitCastExpr(E);
54552bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall
54568786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_Dependent:
545746a523285928aa07bf14803178dc04616ac85994Eli Friedman  case CK_LValueBitCast:
54588786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_UserDefinedConversion:
5459f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
54608786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
54618786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_FloatingRealToComplex: {
5462b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    APFloat &Real = Result.FloatReal;
54638786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    if (!EvaluateFloat(E->getSubExpr(), Real, Info))
5464b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman      return false;
5465b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman
54668786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    Result.makeComplexFloat();
54678786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    Result.FloatImag = APFloat(Real.getSemantics());
54688786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    return true;
54698786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  }
54708786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
54718786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_FloatingComplexCast: {
54728786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    if (!Visit(E->getSubExpr()))
54738786da77984e81d48e0e1b2bd339809b1efc19f3John McCall      return false;
54748786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
54758786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    QualType To = E->getType()->getAs<ComplexType>()->getElementType();
54768786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    QualType From
54778786da77984e81d48e0e1b2bd339809b1efc19f3John McCall      = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType();
54788786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
5479c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return HandleFloatToFloatCast(Info, E, From, To, Result.FloatReal) &&
5480c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith           HandleFloatToFloatCast(Info, E, From, To, Result.FloatImag);
54818786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  }
54828786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
54838786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_FloatingComplexToIntegralComplex: {
54848786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    if (!Visit(E->getSubExpr()))
54858786da77984e81d48e0e1b2bd339809b1efc19f3John McCall      return false;
54868786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
54878786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    QualType To = E->getType()->getAs<ComplexType>()->getElementType();
54888786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    QualType From
54898786da77984e81d48e0e1b2bd339809b1efc19f3John McCall      = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType();
54908786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    Result.makeComplexInt();
5491c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return HandleFloatToIntCast(Info, E, From, Result.FloatReal,
5492c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                To, Result.IntReal) &&
5493c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith           HandleFloatToIntCast(Info, E, From, Result.FloatImag,
5494c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                To, Result.IntImag);
54958786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  }
54968786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
54978786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_IntegralRealToComplex: {
5498b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman    APSInt &Real = Result.IntReal;
54998786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    if (!EvaluateInteger(E->getSubExpr(), Real, Info))
5500b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman      return false;
55019ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson
55028786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    Result.makeComplexInt();
55038786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned());
55048786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    return true;
55058786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  }
55068786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
55078786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_IntegralComplexCast: {
55088786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    if (!Visit(E->getSubExpr()))
5509b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman      return false;
5510ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson
55118786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    QualType To = E->getType()->getAs<ComplexType>()->getElementType();
55128786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    QualType From
55138786da77984e81d48e0e1b2bd339809b1efc19f3John McCall      = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType();
55141725f683432715e5afe34d476024bd6f16eac3fcEli Friedman
5515f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith    Result.IntReal = HandleIntToIntCast(Info, E, To, From, Result.IntReal);
5516f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith    Result.IntImag = HandleIntToIntCast(Info, E, To, From, Result.IntImag);
55178786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    return true;
55188786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  }
55198786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
55208786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  case CK_IntegralComplexToFloatingComplex: {
55218786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    if (!Visit(E->getSubExpr()))
55228786da77984e81d48e0e1b2bd339809b1efc19f3John McCall      return false;
55238786da77984e81d48e0e1b2bd339809b1efc19f3John McCall
55248786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    QualType To = E->getType()->getAs<ComplexType>()->getElementType();
55258786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    QualType From
55268786da77984e81d48e0e1b2bd339809b1efc19f3John McCall      = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType();
55278786da77984e81d48e0e1b2bd339809b1efc19f3John McCall    Result.makeComplexFloat();
5528c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return HandleIntToFloatCast(Info, E, From, Result.IntReal,
5529c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                To, Result.FloatReal) &&
5530c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith           HandleIntToFloatCast(Info, E, From, Result.IntImag,
5531c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                To, Result.FloatImag);
55328786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  }
5533ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson  }
55341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
55358786da77984e81d48e0e1b2bd339809b1efc19f3John McCall  llvm_unreachable("unknown cast resulting in complex value");
55369ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson}
55379ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson
5538f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCallbool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
5539e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma)
55402ad226bdc847df6b6b6e4f832856478ab63bb3dcRichard Smith    return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
55412ad226bdc847df6b6b6e4f832856478ab63bb3dcRichard Smith
5542745f5147e065900267c85a5568785a1991d4838fRichard Smith  bool LHSOK = Visit(E->getLHS());
5543745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!LHSOK && !Info.keepEvaluatingAfterFailure())
5544f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    return false;
55451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5546f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall  ComplexValue RHS;
5547745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK)
5548f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall    return false;
5549a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar
55503f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar  assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
55513f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar         "Invalid operands to binary operator.");
5552ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson  switch (E->getOpcode()) {
5553f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  default: return Error(E);
55542de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Add:
5555a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar    if (Result.isComplexFloat()) {
5556a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar      Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
5557a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar                                       APFloat::rmNearestTiesToEven);
5558a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar      Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
5559a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar                                       APFloat::rmNearestTiesToEven);
5560a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar    } else {
5561a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar      Result.getComplexIntReal() += RHS.getComplexIntReal();
5562a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar      Result.getComplexIntImag() += RHS.getComplexIntImag();
5563a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar    }
55643f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar    break;
55652de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Sub:
5566a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar    if (Result.isComplexFloat()) {
5567a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar      Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
5568a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar                                            APFloat::rmNearestTiesToEven);
5569a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar      Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
5570a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar                                            APFloat::rmNearestTiesToEven);
5571a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar    } else {
5572a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar      Result.getComplexIntReal() -= RHS.getComplexIntReal();
5573a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar      Result.getComplexIntImag() -= RHS.getComplexIntImag();
5574a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar    }
55753f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar    break;
55762de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case BO_Mul:
55773f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar    if (Result.isComplexFloat()) {
5578f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall      ComplexValue LHS = Result;
55793f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      APFloat &LHS_r = LHS.getComplexFloatReal();
55803f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      APFloat &LHS_i = LHS.getComplexFloatImag();
55813f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      APFloat &RHS_r = RHS.getComplexFloatReal();
55823f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      APFloat &RHS_i = RHS.getComplexFloatImag();
55831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
55843f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      APFloat Tmp = LHS_r;
55853f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
55863f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Result.getComplexFloatReal() = Tmp;
55873f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Tmp = LHS_i;
55883f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
55893f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
55903f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar
55913f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Tmp = LHS_r;
55923f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
55933f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Result.getComplexFloatImag() = Tmp;
55943f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Tmp = LHS_i;
55953f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
55963f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar      Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
55973f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar    } else {
5598f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall      ComplexValue LHS = Result;
55991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result.getComplexIntReal() =
56003f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar        (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
56013f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar         LHS.getComplexIntImag() * RHS.getComplexIntImag());
56021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result.getComplexIntImag() =
56033f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar        (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
56043f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar         LHS.getComplexIntImag() * RHS.getComplexIntReal());
56053f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar    }
56063f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar    break;
560796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  case BO_Div:
560896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    if (Result.isComplexFloat()) {
560996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      ComplexValue LHS = Result;
561096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      APFloat &LHS_r = LHS.getComplexFloatReal();
561196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      APFloat &LHS_i = LHS.getComplexFloatImag();
561296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      APFloat &RHS_r = RHS.getComplexFloatReal();
561396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      APFloat &RHS_i = RHS.getComplexFloatImag();
561496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      APFloat &Res_r = Result.getComplexFloatReal();
561596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      APFloat &Res_i = Result.getComplexFloatImag();
561696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara
561796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      APFloat Den = RHS_r;
561896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Den.multiply(RHS_r, APFloat::rmNearestTiesToEven);
561996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      APFloat Tmp = RHS_i;
562096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
562196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Den.add(Tmp, APFloat::rmNearestTiesToEven);
562296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara
562396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Res_r = LHS_r;
562496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Res_r.multiply(RHS_r, APFloat::rmNearestTiesToEven);
562596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Tmp = LHS_i;
562696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
562796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Res_r.add(Tmp, APFloat::rmNearestTiesToEven);
562896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Res_r.divide(Den, APFloat::rmNearestTiesToEven);
562996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara
563096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Res_i = LHS_i;
563196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Res_i.multiply(RHS_r, APFloat::rmNearestTiesToEven);
563296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Tmp = LHS_r;
563396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
563496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Res_i.subtract(Tmp, APFloat::rmNearestTiesToEven);
563596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Res_i.divide(Den, APFloat::rmNearestTiesToEven);
563696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    } else {
5637f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0)
5638f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith        return Error(E, diag::note_expr_divide_by_zero);
5639f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
564096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      ComplexValue LHS = Result;
564196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() +
564296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara        RHS.getComplexIntImag() * RHS.getComplexIntImag();
564396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Result.getComplexIntReal() =
564496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara        (LHS.getComplexIntReal() * RHS.getComplexIntReal() +
564596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara         LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den;
564696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Result.getComplexIntImag() =
564796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara        (LHS.getComplexIntImag() * RHS.getComplexIntReal() -
564896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara         LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den;
564996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    }
565096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    break;
5651ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson  }
5652ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson
5653f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall  return true;
5654ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson}
5655ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson
565696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnarabool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
565796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  // Get the operand value into 'Result'.
565896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  if (!Visit(E->getSubExpr()))
565996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    return false;
566096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara
566196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  switch (E->getOpcode()) {
566296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  default:
5663f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return Error(E);
566496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  case UO_Extension:
566596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    return true;
566696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  case UO_Plus:
566796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    // The result is always just the subexpr.
566896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    return true;
566996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  case UO_Minus:
567096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    if (Result.isComplexFloat()) {
567196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Result.getComplexFloatReal().changeSign();
567296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Result.getComplexFloatImag().changeSign();
567396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    }
567496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    else {
567596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Result.getComplexIntReal() = -Result.getComplexIntReal();
567696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Result.getComplexIntImag() = -Result.getComplexIntImag();
567796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    }
567896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    return true;
567996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  case UO_Not:
568096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    if (Result.isComplexFloat())
568196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Result.getComplexFloatImag().changeSign();
568296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    else
568396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara      Result.getComplexIntImag() = -Result.getComplexIntImag();
568496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara    return true;
568596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara  }
568696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara}
568796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara
56887ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedmanbool ComplexExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
56897ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  if (E->getNumInits() == 2) {
56907ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    if (E->getType()->isComplexType()) {
56917ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman      Result.makeComplexFloat();
56927ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman      if (!EvaluateFloat(E->getInit(0), Result.FloatReal, Info))
56937ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman        return false;
56947ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman      if (!EvaluateFloat(E->getInit(1), Result.FloatImag, Info))
56957ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman        return false;
56967ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    } else {
56977ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman      Result.makeComplexInt();
56987ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman      if (!EvaluateInteger(E->getInit(0), Result.IntReal, Info))
56997ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman        return false;
57007ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman      if (!EvaluateInteger(E->getInit(1), Result.IntImag, Info))
57017ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman        return false;
57027ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    }
57037ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman    return true;
57047ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  }
57057ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  return ExprEvaluatorBaseTy::VisitInitListExpr(E);
57067ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman}
57077ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman
57089ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson//===----------------------------------------------------------------------===//
5709aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith// Void expression evaluation, primarily for a cast to void on the LHS of a
5710aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith// comma operator
5711aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith//===----------------------------------------------------------------------===//
5712aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith
5713aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smithnamespace {
5714aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smithclass VoidExprEvaluator
5715aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith  : public ExprEvaluatorBase<VoidExprEvaluator, bool> {
5716aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smithpublic:
5717aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith  VoidExprEvaluator(EvalInfo &Info) : ExprEvaluatorBaseTy(Info) {}
5718aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith
5719aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith  bool Success(const CCValue &V, const Expr *e) { return true; }
5720aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith
5721aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith  bool VisitCastExpr(const CastExpr *E) {
5722aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith    switch (E->getCastKind()) {
5723aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith    default:
5724aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith      return ExprEvaluatorBaseTy::VisitCastExpr(E);
5725aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith    case CK_ToVoid:
5726aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith      VisitIgnoredValue(E->getSubExpr());
5727aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith      return true;
5728aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith    }
5729aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith  }
5730aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith};
5731aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith} // end anonymous namespace
5732aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith
5733aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smithstatic bool EvaluateVoid(const Expr *E, EvalInfo &Info) {
5734aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith  assert(E->isRValue() && E->getType()->isVoidType());
5735aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith  return VoidExprEvaluator(Info).Visit(E);
5736aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith}
5737aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith
5738aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith//===----------------------------------------------------------------------===//
573951f4708c00110940ca3f337961915f2ca1668375Richard Smith// Top level Expr::EvaluateAsRValue method.
5740f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===//
5741f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner
574247a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smithstatic bool Evaluate(CCValue &Result, EvalInfo &Info, const Expr *E) {
5743c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  // In C, function designators are not lvalues, but we evaluate them as if they
5744c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  // are.
5745c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  if (E->isGLValue() || E->getType()->isFunctionType()) {
5746c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    LValue LV;
5747c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    if (!EvaluateLValue(E, LV, Info))
5748c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith      return false;
5749c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    LV.moveInto(Result);
5750c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  } else if (E->getType()->isVectorType()) {
57511e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    if (!EvaluateVector(E, Result, Info))
575259b5da6d853b4368b984700315adf7b37de05764Nate Begeman      return false;
5753575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor  } else if (E->getType()->isIntegralOrEnumerationType()) {
57541e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    if (!IntExprEvaluator(Info, Result).Visit(E))
57556dde0d5dc09f45f4d9508c964703e36fef1a0198Anders Carlsson      return false;
5756efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  } else if (E->getType()->hasPointerRepresentation()) {
5757efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    LValue LV;
5758efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    if (!EvaluatePointer(E, LV, Info))
57596dde0d5dc09f45f4d9508c964703e36fef1a0198Anders Carlsson      return false;
57601e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    LV.moveInto(Result);
5761efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  } else if (E->getType()->isRealFloatingType()) {
5762efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    llvm::APFloat F(0.0);
5763efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    if (!EvaluateFloat(E, F, Info))
57646dde0d5dc09f45f4d9508c964703e36fef1a0198Anders Carlsson      return false;
576547a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith    Result = CCValue(F);
5766efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  } else if (E->getType()->isAnyComplexType()) {
5767efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    ComplexValue C;
5768efdb83e26f9a1fd2566afe54461216cd84814d42John McCall    if (!EvaluateComplex(E, C, Info))
5769660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump      return false;
57701e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith    C.moveInto(Result);
577169c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith  } else if (E->getType()->isMemberPointerType()) {
5772e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    MemberPtr P;
5773e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    if (!EvaluateMemberPointer(E, P, Info))
5774e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      return false;
5775e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    P.moveInto(Result);
5776e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith    return true;
577751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  } else if (E->getType()->isArrayType()) {
5778180f47959a066795cc0f409433023af448bb0328Richard Smith    LValue LV;
57791bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    LV.set(E, Info.CurrentCall);
5780180f47959a066795cc0f409433023af448bb0328Richard Smith    if (!EvaluateArray(E, LV, Info.CurrentCall->Temporaries[E], Info))
5781cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith      return false;
5782180f47959a066795cc0f409433023af448bb0328Richard Smith    Result = Info.CurrentCall->Temporaries[E];
578351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  } else if (E->getType()->isRecordType()) {
5784180f47959a066795cc0f409433023af448bb0328Richard Smith    LValue LV;
57851bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith    LV.set(E, Info.CurrentCall);
5786180f47959a066795cc0f409433023af448bb0328Richard Smith    if (!EvaluateRecord(E, LV, Info.CurrentCall->Temporaries[E], Info))
5787180f47959a066795cc0f409433023af448bb0328Richard Smith      return false;
5788180f47959a066795cc0f409433023af448bb0328Richard Smith    Result = Info.CurrentCall->Temporaries[E];
5789aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith  } else if (E->getType()->isVoidType()) {
5790c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    if (Info.getLangOpts().CPlusPlus0x)
5791c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_nonliteral)
5792c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith        << E->getType();
5793c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    else
5794c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith      Info.CCEDiag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr);
5795aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith    if (!EvaluateVoid(E, Info))
5796aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith      return false;
5797c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith  } else if (Info.getLangOpts().CPlusPlus0x) {
5798c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    Info.Diag(E->getExprLoc(), diag::note_constexpr_nonliteral) << E->getType();
5799c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith    return false;
5800f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  } else {
5801dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr);
5802660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump    return false;
5803f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
5804660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump
5805660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump  return true;
5806660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump}
5807660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump
580869c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith/// EvaluateConstantExpression - Evaluate an expression as a constant expression
580969c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith/// in-place in an APValue. In some cases, the in-place evaluation is essential,
581069c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith/// since later initializers for an object can indirectly refer to subobjects
581169c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith/// which were initialized earlier.
581269c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smithstatic bool EvaluateConstantExpression(APValue &Result, EvalInfo &Info,
5813c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                       const LValue &This, const Expr *E,
5814c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                       CheckConstantExpressionKind CCEK) {
581551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (!CheckLiteralType(Info, E))
581651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return false;
581751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
581851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (E->isRValue()) {
581969c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith    // Evaluate arrays and record types in-place, so that later initializers can
582069c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith    // refer to earlier-initialized members of the object.
5821180f47959a066795cc0f409433023af448bb0328Richard Smith    if (E->getType()->isArrayType())
5822180f47959a066795cc0f409433023af448bb0328Richard Smith      return EvaluateArray(E, This, Result, Info);
5823180f47959a066795cc0f409433023af448bb0328Richard Smith    else if (E->getType()->isRecordType())
5824180f47959a066795cc0f409433023af448bb0328Richard Smith      return EvaluateRecord(E, This, Result, Info);
582569c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith  }
582669c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith
582769c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith  // For any other type, in-place evaluation is unimportant.
582869c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith  CCValue CoreConstResult;
582969c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith  return Evaluate(CoreConstResult, Info, E) &&
5830c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith         CheckConstantExpression(Info, E, CoreConstResult, Result, CCEK);
583169c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith}
583269c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith
5833f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// EvaluateAsRValue - Try to evaluate this expression, performing an implicit
5834f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// lvalue-to-rvalue cast if it is an lvalue.
5835f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) {
583651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (!CheckLiteralType(Info, E))
583751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return false;
583851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
5839f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  CCValue Value;
5840f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (!::Evaluate(Value, Info, E))
5841f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return false;
5842f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
5843f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (E->isGLValue()) {
5844f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    LValue LV;
5845f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    LV.setFrom(Value);
5846f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (!HandleLValueToRValueConversion(Info, E, E->getType(), LV, Value))
5847f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith      return false;
5848f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
5849f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
5850f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  // Check this core constant expression is a constant expression, and if so,
5851f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  // convert it to one.
5852f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  return CheckConstantExpression(Info, E, Value, Result);
5853f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith}
5854c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith
585551f4708c00110940ca3f337961915f2ca1668375Richard Smith/// EvaluateAsRValue - Return true if this is a constant which we can fold using
585656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall/// any crazy technique (that has nothing to do with language standards) that
585756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall/// we want to.  If this function returns true, it returns the folded constant
5858c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// in Result. If this expression is a glvalue, an lvalue-to-rvalue conversion
5859c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// will be applied to the result.
586051f4708c00110940ca3f337961915f2ca1668375Richard Smithbool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx) const {
5861ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith  // Fast-path evaluations of integer literals, since we sometimes see files
5862ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith  // containing vast quantities of these.
5863ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith  if (const IntegerLiteral *L = dyn_cast<IntegerLiteral>(this)) {
5864ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith    Result.Val = APValue(APSInt(L->getValue(),
5865ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith                                L->getType()->isUnsignedIntegerType()));
5866ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith    return true;
5867ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith  }
5868ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith
58692d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  // FIXME: Evaluating values of large array and record types can cause
58702d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  // performance problems. Only do so in C++11 for now.
5871e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith  if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) &&
5872e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith      !Ctx.getLangOptions().CPlusPlus0x)
58731445bbacf4c8de5f208ff4ccb302424a4d9e233eRichard Smith    return false;
58741445bbacf4c8de5f208ff4ccb302424a4d9e233eRichard Smith
5875f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  EvalInfo Info(Ctx, Result);
5876f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  return ::EvaluateAsRValue(Info, this, Result.Val);
587756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall}
587856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
58794ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool Expr::EvaluateAsBooleanCondition(bool &Result,
58804ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                      const ASTContext &Ctx) const {
5881c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  EvalResult Scratch;
588251f4708c00110940ca3f337961915f2ca1668375Richard Smith  return EvaluateAsRValue(Scratch, Ctx) &&
5883b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith         HandleConversionToBool(CCValue(const_cast<ASTContext&>(Ctx),
5884b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith                                        Scratch.Val, CCValue::GlobalValue()),
588547a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith                                Result);
5886cd7a445c6b46c5585580dfb652300c8483c0cb6bJohn McCall}
5887cd7a445c6b46c5585580dfb652300c8483c0cb6bJohn McCall
588880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smithbool Expr::EvaluateAsInt(APSInt &Result, const ASTContext &Ctx,
588980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith                         SideEffectsKind AllowSideEffects) const {
589080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  if (!getType()->isIntegralOrEnumerationType())
589180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith    return false;
589280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith
5893c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  EvalResult ExprResult;
589480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith  if (!EvaluateAsRValue(ExprResult, Ctx) || !ExprResult.Val.isInt() ||
589580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith      (!AllowSideEffects && ExprResult.HasSideEffects))
5896c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith    return false;
5897f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
5898c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  Result = ExprResult.Val.getInt();
5899c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith  return true;
5900a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith}
5901a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith
59024ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx) const {
59031b78276a75a5a0f496a82429c1ff9604d622a76dAnders Carlsson  EvalInfo Info(Ctx, Result);
59041b78276a75a5a0f496a82429c1ff9604d622a76dAnders Carlsson
5905efdb83e26f9a1fd2566afe54461216cd84814d42John McCall  LValue LV;
59069a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith  return EvaluateLValue(this, LV, Info) && !Result.HasSideEffects &&
5907c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith         CheckLValueConstantExpression(Info, this, LV, Result.Val,
5908c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith                                       CCEK_Constant);
5909b2f295c8050fb8c141bf2cf38eed0a56e99d0092Eli Friedman}
5910b2f295c8050fb8c141bf2cf38eed0a56e99d0092Eli Friedman
5911099e7f647ccda915513f2b2ec53352dc756082d3Richard Smithbool Expr::EvaluateAsInitializer(APValue &Value, const ASTContext &Ctx,
5912099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith                                 const VarDecl *VD,
5913099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith                      llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
59142d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  // FIXME: Evaluating initializers for large array and record types can cause
59152d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  // performance problems. Only do so in C++11 for now.
59162d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) &&
59172d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith      !Ctx.getLangOptions().CPlusPlus0x)
59182d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith    return false;
59192d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith
5920099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  Expr::EvalStatus EStatus;
5921099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  EStatus.Diag = &Notes;
5922099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
5923099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  EvalInfo InitInfo(Ctx, EStatus);
5924099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  InitInfo.setEvaluatingDecl(VD, Value);
5925099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
592651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (!CheckLiteralType(InitInfo, this))
592751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    return false;
592851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
5929099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  LValue LVal;
5930099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  LVal.set(VD);
5931099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
593251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  // C++11 [basic.start.init]p2:
593351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  //  Variables with static storage duration or thread storage duration shall be
593451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  //  zero-initialized before any other initialization takes place.
593551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  // This behavior is not present in C.
593651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  if (Ctx.getLangOptions().CPlusPlus && !VD->hasLocalStorage() &&
593751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      !VD->getType()->isReferenceType()) {
593851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    ImplicitValueInitExpr VIE(VD->getType());
593951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith    if (!EvaluateConstantExpression(Value, InitInfo, LVal, &VIE))
594051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith      return false;
594151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith  }
594251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith
5943099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  return EvaluateConstantExpression(Value, InitInfo, LVal, this) &&
5944099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith         !EStatus.HasSideEffects;
5945099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith}
5946099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
594751f4708c00110940ca3f337961915f2ca1668375Richard Smith/// isEvaluatable - Call EvaluateAsRValue to see if this expression can be
594851f4708c00110940ca3f337961915f2ca1668375Richard Smith/// constant folded, but discard the result.
59494ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool Expr::isEvaluatable(const ASTContext &Ctx) const {
59504fdfb0965b396f2778091f7e6c051d17ff9791baAnders Carlsson  EvalResult Result;
595151f4708c00110940ca3f337961915f2ca1668375Richard Smith  return EvaluateAsRValue(Result, Ctx) && !Result.HasSideEffects;
595245b6b9d080ac56917337d73d8f1cd6374b27b05dChris Lattner}
595351fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson
59544ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool Expr::HasSideEffects(const ASTContext &Ctx) const {
59551e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith  return HasSideEffect(Ctx).Visit(this);
5956393c247fe025ccb5f914e37e948192ea86faef8cFariborz Jahanian}
5957393c247fe025ccb5f914e37e948192ea86faef8cFariborz Jahanian
5958a6b8b2c09610b8bc4330e948ece8b940c2386406Richard SmithAPSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx) const {
59591c0cfd4599e816cfd7a8f348286bf0ad79652ffcAnders Carlsson  EvalResult EvalResult;
596051f4708c00110940ca3f337961915f2ca1668375Richard Smith  bool Result = EvaluateAsRValue(EvalResult, Ctx);
5961c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin  (void)Result;
596251fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson  assert(Result && "Could not evaluate expression");
59631c0cfd4599e816cfd7a8f348286bf0ad79652ffcAnders Carlsson  assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
596451fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson
59651c0cfd4599e816cfd7a8f348286bf0ad79652ffcAnders Carlsson  return EvalResult.Val.getInt();
596651fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson}
5967d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
5968e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara bool Expr::EvalResult::isGlobalLValue() const {
5969e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara   assert(Val.isLValue());
5970e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara   return IsGlobalLValue(Val.getLValueBase());
5971e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara }
5972e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara
5973e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara
5974d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// isIntegerConstantExpr - this recursive routine will test if an expression is
5975d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// an integer constant expression.
5976d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
5977d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
5978d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// comma, etc
5979d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall///
5980d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// FIXME: Handle offsetof.  Two things to do:  Handle GCC's __builtin_offsetof
5981d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// to support gcc 4.0+  and handle the idiom GCC recognizes with a null pointer
5982d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// cast+dereference.
5983d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
5984d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// CheckICE - This function does the fundamental ICE checking: the returned
5985d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
5986d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// Note that to reduce code duplication, this helper does no evaluation
5987d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// itself; the caller checks whether the expression is evaluatable, and
5988d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// in the rare cases where CheckICE actually cares about the evaluated
5989d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// value, it calls into Evalute.
5990d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall//
5991d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// Meanings of Val:
599251f4708c00110940ca3f337961915f2ca1668375Richard Smith// 0: This expression is an ICE.
5993d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// 1: This expression is not an ICE, but if it isn't evaluated, it's
5994d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall//    a legal subexpression for an ICE. This return value is used to handle
5995d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall//    the comma operator in C99 mode.
5996d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// 2: This expression is not an ICE, and is not a legal subexpression for one.
5997d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
59983c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohmannamespace {
59993c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman
6000d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCallstruct ICEDiag {
6001d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  unsigned Val;
6002d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  SourceLocation Loc;
6003d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6004d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  public:
6005d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
6006d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  ICEDiag() : Val(0) {}
6007d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall};
6008d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
60093c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman}
60103c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman
60113c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohmanstatic ICEDiag NoDiag() { return ICEDiag(); }
6012d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6013d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCallstatic ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
6014d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  Expr::EvalResult EVResult;
601551f4708c00110940ca3f337961915f2ca1668375Richard Smith  if (!E->EvaluateAsRValue(EVResult, Ctx) || EVResult.HasSideEffects ||
6016d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      !EVResult.Val.isInt()) {
6017d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return ICEDiag(2, E->getLocStart());
6018d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6019d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  return NoDiag();
6020d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall}
6021d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6022d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCallstatic ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
6023d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  assert(!E->isValueDependent() && "Should not see value dependent exprs!");
60242ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor  if (!E->getType()->isIntegralOrEnumerationType()) {
6025d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return ICEDiag(2, E->getLocStart());
6026d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6027d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6028d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  switch (E->getStmtClass()) {
602963c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall#define ABSTRACT_STMT(Node)
6030d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall#define STMT(Node, Base) case Expr::Node##Class:
6031d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall#define EXPR(Node, Base)
6032d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall#include "clang/AST/StmtNodes.inc"
6033d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::PredefinedExprClass:
6034d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::FloatingLiteralClass:
6035d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ImaginaryLiteralClass:
6036d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::StringLiteralClass:
6037d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ArraySubscriptExprClass:
6038d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::MemberExprClass:
6039d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CompoundAssignOperatorClass:
6040d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CompoundLiteralExprClass:
6041d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ExtVectorElementExprClass:
6042d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::DesignatedInitExprClass:
6043d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ImplicitValueInitExprClass:
6044d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ParenListExprClass:
6045d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::VAArgExprClass:
6046d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::AddrLabelExprClass:
6047d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::StmtExprClass:
6048d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXMemberCallExprClass:
6049e08ce650a2b02410eddd1f60a4aa6b3d4be71e73Peter Collingbourne  case Expr::CUDAKernelCallExprClass:
6050d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXDynamicCastExprClass:
6051d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXTypeidExprClass:
60529be88403e965cc49af76c9d33d818781d44b333eFrancois Pichet  case Expr::CXXUuidofExprClass:
6053d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXNullPtrLiteralExprClass:
6054d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXThisExprClass:
6055d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXThrowExprClass:
6056d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXNewExprClass:
6057d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXDeleteExprClass:
6058d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXPseudoDestructorExprClass:
6059d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::UnresolvedLookupExprClass:
6060d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::DependentScopeDeclRefExprClass:
6061d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXConstructExprClass:
6062d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXBindTemporaryExprClass:
60634765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  case Expr::ExprWithCleanupsClass:
6064d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXTemporaryObjectExprClass:
6065d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXUnresolvedConstructExprClass:
6066d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXDependentScopeMemberExprClass:
6067d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::UnresolvedMemberExprClass:
6068d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ObjCStringLiteralClass:
6069d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ObjCEncodeExprClass:
6070d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ObjCMessageExprClass:
6071d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ObjCSelectorExprClass:
6072d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ObjCProtocolExprClass:
6073d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ObjCIvarRefExprClass:
6074d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ObjCPropertyRefExprClass:
6075d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ObjCIsaExprClass:
6076d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ShuffleVectorExprClass:
6077d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::BlockExprClass:
6078d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::BlockDeclRefExprClass:
6079d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::NoStmtClass:
60807cd7d1ad33fdf49eef83942e8855fe20d95aa1b9John McCall  case Expr::OpaqueValueExprClass:
6081be230c36e32142cbdcdbe9c97511d097beeecbabDouglas Gregor  case Expr::PackExpansionExprClass:
6082c7793c73ba8a343de3f2552d984851985a46f159Douglas Gregor  case Expr::SubstNonTypeTemplateParmPackExprClass:
608361eee0ca33b29e102f11bab77c8b74cc00e2392bTanya Lattner  case Expr::AsTypeExprClass:
6084f85e193739c953358c865005855253af4f68a497John McCall  case Expr::ObjCIndirectCopyRestoreExprClass:
608503e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  case Expr::MaterializeTemporaryExprClass:
60864b9c2d235fb9449e249d74f48ecfec601650de93John McCall  case Expr::PseudoObjectExprClass:
6087276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman  case Expr::AtomicExprClass:
6088cea8d966f826554f0679595e9371e314e8dbc1cfSebastian Redl  case Expr::InitListExprClass:
6089cea8d966f826554f0679595e9371e314e8dbc1cfSebastian Redl    return ICEDiag(2, E->getLocStart());
6090cea8d966f826554f0679595e9371e314e8dbc1cfSebastian Redl
6091ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor  case Expr::SizeOfPackExprClass:
6092d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::GNUNullExprClass:
6093d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    // GCC considers the GNU __null value to be an integral constant expression.
6094d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return NoDiag();
6095d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
609691a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall  case Expr::SubstNonTypeTemplateParmExprClass:
609791a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall    return
609891a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall      CheckICE(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(), Ctx);
609991a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall
6100d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ParenExprClass:
6101d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
6102f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  case Expr::GenericSelectionExprClass:
6103f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    return CheckICE(cast<GenericSelectionExpr>(E)->getResultExpr(), Ctx);
6104d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::IntegerLiteralClass:
6105d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CharacterLiteralClass:
6106d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXBoolLiteralExprClass:
6107ed8abf18329df67b0abcbb3a10458bd8c1d2a595Douglas Gregor  case Expr::CXXScalarValueInitExprClass:
6108d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::UnaryTypeTraitExprClass:
61096ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet  case Expr::BinaryTypeTraitExprClass:
611021ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley  case Expr::ArrayTypeTraitExprClass:
6111552622067dc45013d240f73952fece703f5e63bdJohn Wiegley  case Expr::ExpressionTraitExprClass:
61122e156225a29407a50dd19041aa5750171ad44ea3Sebastian Redl  case Expr::CXXNoexceptExprClass:
6113d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return NoDiag();
6114d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CallExprClass:
61156cf750298d3621d8a10a6dd07fcee8e274b9d94dSean Hunt  case Expr::CXXOperatorCallExprClass: {
611605830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith    // C99 6.6/3 allows function calls within unevaluated subexpressions of
611705830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith    // constant expressions, but they can never be ICEs because an ICE cannot
611805830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith    // contain an operand of (pointer to) function type.
6119d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    const CallExpr *CE = cast<CallExpr>(E);
6120180f47959a066795cc0f409433023af448bb0328Richard Smith    if (CE->isBuiltinCall())
6121d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return CheckEvalInICE(E, Ctx);
6122d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return ICEDiag(2, E->getLocStart());
6123d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6124d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::DeclRefExprClass:
6125d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
6126d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return NoDiag();
612703f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith    if (Ctx.getLangOptions().CPlusPlus && IsConstNonVolatile(E->getType())) {
6128d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
6129d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6130d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      // Parameter variables are never constants.  Without this check,
6131d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      // getAnyInitializer() can find a default argument, which leads
6132d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      // to chaos.
6133d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      if (isa<ParmVarDecl>(D))
6134d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
6135d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6136d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      // C++ 7.1.5.1p2
6137d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      //   A variable of non-volatile const-qualified integral or enumeration
6138d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      //   type initialized by an ICE can be used in ICEs.
6139d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) {
6140db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith        if (!Dcl->getType()->isIntegralOrEnumerationType())
6141db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith          return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
6142db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith
6143099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith        const VarDecl *VD;
6144099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith        // Look for a declaration of this variable that has an initializer, and
6145099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith        // check whether it is an ICE.
6146099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith        if (Dcl->getAnyInitializer(VD) && VD->checkInitIsICE())
6147099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith          return NoDiag();
6148099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith        else
6149099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith          return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
6150d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      }
6151d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    }
6152d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return ICEDiag(2, E->getLocStart());
6153d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::UnaryOperatorClass: {
6154d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    const UnaryOperator *Exp = cast<UnaryOperator>(E);
6155d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    switch (Exp->getOpcode()) {
61562de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PostInc:
61572de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PostDec:
61582de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PreInc:
61592de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_PreDec:
61602de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_AddrOf:
61612de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Deref:
616205830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith      // C99 6.6/3 allows increment and decrement within unevaluated
616305830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith      // subexpressions of constant expressions, but they can never be ICEs
616405830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith      // because an ICE cannot contain an lvalue operand.
6165d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return ICEDiag(2, E->getLocStart());
61662de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Extension:
61672de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_LNot:
61682de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Plus:
61692de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Minus:
61702de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Not:
61712de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Real:
61722de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case UO_Imag:
6173d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return CheckICE(Exp->getSubExpr(), Ctx);
6174d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    }
6175d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6176d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    // OffsetOf falls through here.
6177d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6178d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::OffsetOfExprClass: {
6179d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      // Note that per C99, offsetof must be an ICE. And AFAIK, using
618051f4708c00110940ca3f337961915f2ca1668375Richard Smith      // EvaluateAsRValue matches the proposed gcc behavior for cases like
618105830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith      // "offsetof(struct s{int x[4];}, x[1.0])".  This doesn't affect
6182d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      // compliance: we should warn earlier for offsetof expressions with
6183d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      // array subscripts that aren't ICEs, and if the array subscripts
6184d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      // are ICEs, the value of the offsetof must be an integer constant.
6185d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return CheckEvalInICE(E, Ctx);
6186d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6187f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  case Expr::UnaryExprOrTypeTraitExprClass: {
6188f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E);
6189f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    if ((Exp->getKind() ==  UETT_SizeOf) &&
6190f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne        Exp->getTypeOfArgument()->isVariableArrayType())
6191d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return ICEDiag(2, E->getLocStart());
6192d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return NoDiag();
6193d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6194d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::BinaryOperatorClass: {
6195d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    const BinaryOperator *Exp = cast<BinaryOperator>(E);
6196d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    switch (Exp->getOpcode()) {
61972de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_PtrMemD:
61982de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_PtrMemI:
61992de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Assign:
62002de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_MulAssign:
62012de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_DivAssign:
62022de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_RemAssign:
62032de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_AddAssign:
62042de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_SubAssign:
62052de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_ShlAssign:
62062de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_ShrAssign:
62072de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_AndAssign:
62082de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_XorAssign:
62092de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_OrAssign:
621005830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith      // C99 6.6/3 allows assignments within unevaluated subexpressions of
621105830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith      // constant expressions, but they can never be ICEs because an ICE cannot
621205830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith      // contain an lvalue operand.
6213d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return ICEDiag(2, E->getLocStart());
6214d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
62152de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Mul:
62162de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Div:
62172de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Rem:
62182de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Add:
62192de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Sub:
62202de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Shl:
62212de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Shr:
62222de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LT:
62232de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_GT:
62242de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LE:
62252de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_GE:
62262de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_EQ:
62272de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_NE:
62282de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_And:
62292de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Xor:
62302de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Or:
62312de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_Comma: {
6232d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
6233d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
62342de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      if (Exp->getOpcode() == BO_Div ||
62352de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall          Exp->getOpcode() == BO_Rem) {
623651f4708c00110940ca3f337961915f2ca1668375Richard Smith        // EvaluateAsRValue gives an error for undefined Div/Rem, so make sure
6237d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        // we don't evaluate one.
62383b332ab132fa85c83833d74d400f6e126f52fbd2John McCall        if (LHSResult.Val == 0 && RHSResult.Val == 0) {
6239a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx);
6240d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall          if (REval == 0)
6241d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall            return ICEDiag(1, E->getLocStart());
6242d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall          if (REval.isSigned() && REval.isAllOnesValue()) {
6243a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith            llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx);
6244d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall            if (LEval.isMinSignedValue())
6245d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall              return ICEDiag(1, E->getLocStart());
6246d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall          }
6247d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        }
6248d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      }
62492de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      if (Exp->getOpcode() == BO_Comma) {
6250d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        if (Ctx.getLangOptions().C99) {
6251d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall          // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
6252d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall          // if it isn't evaluated.
6253d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall          if (LHSResult.Val == 0 && RHSResult.Val == 0)
6254d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall            return ICEDiag(1, E->getLocStart());
6255d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        } else {
6256d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall          // In both C89 and C++, commas in ICEs are illegal.
6257d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall          return ICEDiag(2, E->getLocStart());
6258d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        }
6259d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      }
6260d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      if (LHSResult.Val >= RHSResult.Val)
6261d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        return LHSResult;
6262d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return RHSResult;
6263d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    }
62642de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LAnd:
62652de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    case BO_LOr: {
6266d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
6267d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
6268d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      if (LHSResult.Val == 0 && RHSResult.Val == 1) {
6269d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        // Rare case where the RHS has a comma "side-effect"; we need
6270d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        // to actually check the condition to see whether the side
6271d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        // with the comma is evaluated.
62722de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall        if ((Exp->getOpcode() == BO_LAnd) !=
6273a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith            (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0))
6274d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall          return RHSResult;
6275d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        return NoDiag();
6276d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      }
6277d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6278d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      if (LHSResult.Val >= RHSResult.Val)
6279d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        return LHSResult;
6280d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return RHSResult;
6281d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    }
6282d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    }
6283d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6284d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ImplicitCastExprClass:
6285d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CStyleCastExprClass:
6286d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXFunctionalCastExprClass:
6287d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXStaticCastExprClass:
6288d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXReinterpretCastExprClass:
628932cb47174304bc7ec11478b9497c4e10f48273d9Richard Smith  case Expr::CXXConstCastExprClass:
6290f85e193739c953358c865005855253af4f68a497John McCall  case Expr::ObjCBridgedCastExprClass: {
6291d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
62922116b144cf07f2574d20517187eb8863645376ebRichard Smith    if (isa<ExplicitCastExpr>(E)) {
62932116b144cf07f2574d20517187eb8863645376ebRichard Smith      if (const FloatingLiteral *FL
62942116b144cf07f2574d20517187eb8863645376ebRichard Smith            = dyn_cast<FloatingLiteral>(SubExpr->IgnoreParenImpCasts())) {
62952116b144cf07f2574d20517187eb8863645376ebRichard Smith        unsigned DestWidth = Ctx.getIntWidth(E->getType());
62962116b144cf07f2574d20517187eb8863645376ebRichard Smith        bool DestSigned = E->getType()->isSignedIntegerOrEnumerationType();
62972116b144cf07f2574d20517187eb8863645376ebRichard Smith        APSInt IgnoredVal(DestWidth, !DestSigned);
62982116b144cf07f2574d20517187eb8863645376ebRichard Smith        bool Ignored;
62992116b144cf07f2574d20517187eb8863645376ebRichard Smith        // If the value does not fit in the destination type, the behavior is
63002116b144cf07f2574d20517187eb8863645376ebRichard Smith        // undefined, so we are not required to treat it as a constant
63012116b144cf07f2574d20517187eb8863645376ebRichard Smith        // expression.
63022116b144cf07f2574d20517187eb8863645376ebRichard Smith        if (FL->getValue().convertToInteger(IgnoredVal,
63032116b144cf07f2574d20517187eb8863645376ebRichard Smith                                            llvm::APFloat::rmTowardZero,
63042116b144cf07f2574d20517187eb8863645376ebRichard Smith                                            &Ignored) & APFloat::opInvalidOp)
63052116b144cf07f2574d20517187eb8863645376ebRichard Smith          return ICEDiag(2, E->getLocStart());
63062116b144cf07f2574d20517187eb8863645376ebRichard Smith        return NoDiag();
63072116b144cf07f2574d20517187eb8863645376ebRichard Smith      }
63082116b144cf07f2574d20517187eb8863645376ebRichard Smith    }
6309eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman    switch (cast<CastExpr>(E)->getCastKind()) {
6310eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman    case CK_LValueToRValue:
63117a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall    case CK_AtomicToNonAtomic:
63127a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall    case CK_NonAtomicToAtomic:
6313eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman    case CK_NoOp:
6314eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman    case CK_IntegralToBoolean:
6315eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman    case CK_IntegralCast:
6316d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return CheckICE(SubExpr, Ctx);
6317eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman    default:
6318eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman      return ICEDiag(2, E->getLocStart());
6319eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman    }
6320d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
632156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  case Expr::BinaryConditionalOperatorClass: {
632256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E);
632356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    ICEDiag CommonResult = CheckICE(Exp->getCommon(), Ctx);
632456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    if (CommonResult.Val == 2) return CommonResult;
632556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
632656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    if (FalseResult.Val == 2) return FalseResult;
632756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    if (CommonResult.Val == 1) return CommonResult;
632856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    if (FalseResult.Val == 1 &&
6329a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith        Exp->getCommon()->EvaluateKnownConstInt(Ctx) == 0) return NoDiag();
633056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    return FalseResult;
633156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  }
6332d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ConditionalOperatorClass: {
6333d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
6334d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    // If the condition (ignoring parens) is a __builtin_constant_p call,
6335d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    // then only the true side is actually considered in an integer constant
6336d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    // expression, and it is fully evaluated.  This is an important GNU
6337d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    // extension.  See GCC PR38377 for discussion.
6338d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    if (const CallExpr *CallCE
6339d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall        = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
634080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith      if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p)
634180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith        return CheckEvalInICE(E, Ctx);
6342d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
6343d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    if (CondResult.Val == 2)
6344d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return CondResult;
634563fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor
6346f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
6347f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
634863fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor
6349d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    if (TrueResult.Val == 2)
6350d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return TrueResult;
6351d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    if (FalseResult.Val == 2)
6352d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return FalseResult;
6353d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    if (CondResult.Val == 1)
6354d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return CondResult;
6355d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    if (TrueResult.Val == 0 && FalseResult.Val == 0)
6356d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return NoDiag();
6357d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    // Rare case where the diagnostics depend on which side is evaluated
6358d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    // Note that if we get here, CondResult is 0, and at least one of
6359d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    // TrueResult and FalseResult is non-zero.
6360a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith    if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0) {
6361d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall      return FalseResult;
6362d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    }
6363d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return TrueResult;
6364d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6365d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::CXXDefaultArgExprClass:
6366d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
6367d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  case Expr::ChooseExprClass: {
6368d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
6369d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6370d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6371d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
63723026348bd4c13a0f83b59839f64065e0fcbea253David Blaikie  llvm_unreachable("Invalid StmtClass!");
6373d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall}
6374d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall
6375f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// Evaluate an expression as a C++11 integral constant expression.
6376f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool EvaluateCPlusPlus11IntegralConstantExpr(ASTContext &Ctx,
6377f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                                                    const Expr *E,
6378f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                                                    llvm::APSInt *Value,
6379f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                                                    SourceLocation *Loc) {
6380f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (!E->getType()->isIntegralOrEnumerationType()) {
6381f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    if (Loc) *Loc = E->getExprLoc();
6382f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return false;
6383f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  }
6384f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
63854c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  APValue Result;
63864c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  if (!E->isCXX11ConstantExpr(Ctx, &Result, Loc))
6387dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith    return false;
6388dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith
63894c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  assert(Result.isInt() && "pointer cast to int is not an ICE");
63904c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  if (Value) *Value = Result.getInt();
6391dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith  return true;
6392f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith}
6393f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
6394dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smithbool Expr::isIntegerConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
6395f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (Ctx.getLangOptions().CPlusPlus0x)
6396f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, 0, Loc);
6397f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
6398d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  ICEDiag d = CheckICE(this, Ctx);
6399d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  if (d.Val != 0) {
6400d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    if (Loc) *Loc = d.Loc;
6401d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    return false;
6402d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  }
6403f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  return true;
6404f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith}
6405f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
6406f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithbool Expr::isIntegerConstantExpr(llvm::APSInt &Value, ASTContext &Ctx,
6407f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith                                 SourceLocation *Loc, bool isEvaluated) const {
6408f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (Ctx.getLangOptions().CPlusPlus0x)
6409f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, &Value, Loc);
6410f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith
6411f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (!isIntegerConstantExpr(Ctx, Loc))
6412f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith    return false;
6413f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith  if (!EvaluateAsInt(Value, Ctx))
6414d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall    llvm_unreachable("ICE cannot be evaluated!");
6415d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall  return true;
6416d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall}
64174c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith
64184c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smithbool Expr::isCXX11ConstantExpr(ASTContext &Ctx, APValue *Result,
64194c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith                               SourceLocation *Loc) const {
64204c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  // We support this checking in C++98 mode in order to diagnose compatibility
64214c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  // issues.
64224c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  assert(Ctx.getLangOptions().CPlusPlus);
64234c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith
64244c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  Expr::EvalStatus Status;
64254c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  llvm::SmallVector<PartialDiagnosticAt, 8> Diags;
64264c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  Status.Diag = &Diags;
64274c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  EvalInfo Info(Ctx, Status);
64284c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith
64294c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  APValue Scratch;
64304c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  bool IsConstExpr = ::EvaluateAsRValue(Info, this, Result ? *Result : Scratch);
64314c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith
64324c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  if (!Diags.empty()) {
64334c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    IsConstExpr = false;
64344c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    if (Loc) *Loc = Diags[0].first;
64354c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  } else if (!IsConstExpr) {
64364c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    // FIXME: This shouldn't happen.
64374c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    if (Loc) *Loc = getExprLoc();
64384c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  }
64394c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith
64404c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  return IsConstExpr;
64414c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith}
6442745f5147e065900267c85a5568785a1991d4838fRichard Smith
6443745f5147e065900267c85a5568785a1991d4838fRichard Smithbool Expr::isPotentialConstantExpr(const FunctionDecl *FD,
6444745f5147e065900267c85a5568785a1991d4838fRichard Smith                                   llvm::SmallVectorImpl<
6445745f5147e065900267c85a5568785a1991d4838fRichard Smith                                     PartialDiagnosticAt> &Diags) {
6446745f5147e065900267c85a5568785a1991d4838fRichard Smith  // FIXME: It would be useful to check constexpr function templates, but at the
6447745f5147e065900267c85a5568785a1991d4838fRichard Smith  // moment the constant expression evaluator cannot cope with the non-rigorous
6448745f5147e065900267c85a5568785a1991d4838fRichard Smith  // ASTs which we build for dependent expressions.
6449745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (FD->isDependentContext())
6450745f5147e065900267c85a5568785a1991d4838fRichard Smith    return true;
6451745f5147e065900267c85a5568785a1991d4838fRichard Smith
6452745f5147e065900267c85a5568785a1991d4838fRichard Smith  Expr::EvalStatus Status;
6453745f5147e065900267c85a5568785a1991d4838fRichard Smith  Status.Diag = &Diags;
6454745f5147e065900267c85a5568785a1991d4838fRichard Smith
6455745f5147e065900267c85a5568785a1991d4838fRichard Smith  EvalInfo Info(FD->getASTContext(), Status);
6456745f5147e065900267c85a5568785a1991d4838fRichard Smith  Info.CheckingPotentialConstantExpression = true;
6457745f5147e065900267c85a5568785a1991d4838fRichard Smith
6458745f5147e065900267c85a5568785a1991d4838fRichard Smith  const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
6459745f5147e065900267c85a5568785a1991d4838fRichard Smith  const CXXRecordDecl *RD = MD ? MD->getParent()->getCanonicalDecl() : 0;
6460745f5147e065900267c85a5568785a1991d4838fRichard Smith
6461745f5147e065900267c85a5568785a1991d4838fRichard Smith  // FIXME: Fabricate an arbitrary expression on the stack and pretend that it
6462745f5147e065900267c85a5568785a1991d4838fRichard Smith  // is a temporary being used as the 'this' pointer.
6463745f5147e065900267c85a5568785a1991d4838fRichard Smith  LValue This;
6464745f5147e065900267c85a5568785a1991d4838fRichard Smith  ImplicitValueInitExpr VIE(RD ? Info.Ctx.getRecordType(RD) : Info.Ctx.IntTy);
6465745f5147e065900267c85a5568785a1991d4838fRichard Smith  This.set(&VIE, Info.CurrentCall);
6466745f5147e065900267c85a5568785a1991d4838fRichard Smith
6467745f5147e065900267c85a5568785a1991d4838fRichard Smith  APValue Scratch;
6468745f5147e065900267c85a5568785a1991d4838fRichard Smith  ArrayRef<const Expr*> Args;
6469745f5147e065900267c85a5568785a1991d4838fRichard Smith
6470745f5147e065900267c85a5568785a1991d4838fRichard Smith  SourceLocation Loc = FD->getLocation();
6471745f5147e065900267c85a5568785a1991d4838fRichard Smith
6472745f5147e065900267c85a5568785a1991d4838fRichard Smith  if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) {
6473745f5147e065900267c85a5568785a1991d4838fRichard Smith    HandleConstructorCall(Loc, This, Args, CD, Info, Scratch);
6474745f5147e065900267c85a5568785a1991d4838fRichard Smith  } else
6475745f5147e065900267c85a5568785a1991d4838fRichard Smith    HandleFunctionCall(Loc, FD, (MD && MD->isInstance()) ? &This : 0,
6476745f5147e065900267c85a5568785a1991d4838fRichard Smith                       Args, FD->getBody(), Info, Scratch);
6477745f5147e065900267c85a5568785a1991d4838fRichard Smith
6478745f5147e065900267c85a5568785a1991d4838fRichard Smith  return Diags.empty();
6479745f5147e065900267c85a5568785a1991d4838fRichard Smith}
6480