Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1 | //===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===// |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Expr constant evaluator. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/APValue.h" |
| 15 | #include "clang/AST/ASTContext.h" |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 16 | #include "clang/AST/CharUnits.h" |
Anders Carlsson | 19cc4ab | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 17 | #include "clang/AST/RecordLayout.h" |
Seo Sanghyeon | 0fe52e1 | 2008-07-08 07:23:12 +0000 | [diff] [blame] | 18 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeLoc.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTDiagnostic.h" |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 21 | #include "clang/AST/Expr.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 22 | #include "clang/Basic/Builtins.h" |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 23 | #include "clang/Basic/TargetInfo.h" |
Mike Stump | 7462b39 | 2009-05-30 14:43:18 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallString.h" |
Mike Stump | 4572bab | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 25 | #include <cstring> |
| 26 | |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 27 | using namespace clang; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 28 | using llvm::APSInt; |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 29 | using llvm::APFloat; |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 30 | |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 31 | /// EvalInfo - This is a private struct used by the evaluator to capture |
| 32 | /// information about a subexpression as it is folded. It retains information |
| 33 | /// about the AST context, but also maintains information about the folded |
| 34 | /// expression. |
| 35 | /// |
| 36 | /// If an expression could be evaluated, it is still possible it is not a C |
| 37 | /// "integer constant expression" or constant expression. If not, this struct |
| 38 | /// captures information about how and why not. |
| 39 | /// |
| 40 | /// One bit of information passed *into* the request for constant folding |
| 41 | /// indicates whether the subexpression is "evaluated" or not according to C |
| 42 | /// rules. For example, the RHS of (0 && foo()) is not evaluated. We can |
| 43 | /// evaluate the expression regardless of what the RHS is, but C only allows |
| 44 | /// certain things in certain situations. |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 45 | namespace { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 46 | struct LValue; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 47 | struct CallStackFrame; |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 48 | struct EvalInfo; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 49 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 50 | /// Get an LValue path entry, which is known to not be an array index, as a |
| 51 | /// field declaration. |
| 52 | const FieldDecl *getAsField(APValue::LValuePathEntry E) { |
| 53 | APValue::BaseOrMemberType Value; |
| 54 | Value.setFromOpaqueValue(E.BaseOrMember); |
| 55 | return dyn_cast<FieldDecl>(Value.getPointer()); |
| 56 | } |
| 57 | /// Get an LValue path entry, which is known to not be an array index, as a |
| 58 | /// base class declaration. |
| 59 | const CXXRecordDecl *getAsBaseClass(APValue::LValuePathEntry E) { |
| 60 | APValue::BaseOrMemberType Value; |
| 61 | Value.setFromOpaqueValue(E.BaseOrMember); |
| 62 | return dyn_cast<CXXRecordDecl>(Value.getPointer()); |
| 63 | } |
| 64 | /// Determine whether this LValue path entry for a base class names a virtual |
| 65 | /// base class. |
| 66 | bool isVirtualBaseClass(APValue::LValuePathEntry E) { |
| 67 | APValue::BaseOrMemberType Value; |
| 68 | Value.setFromOpaqueValue(E.BaseOrMember); |
| 69 | return Value.getInt(); |
| 70 | } |
| 71 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 72 | /// Determine whether the described subobject is an array element. |
| 73 | static bool SubobjectIsArrayElement(QualType Base, |
| 74 | ArrayRef<APValue::LValuePathEntry> Path) { |
| 75 | bool IsArrayElement = false; |
| 76 | const Type *T = Base.getTypePtr(); |
| 77 | for (unsigned I = 0, N = Path.size(); I != N; ++I) { |
| 78 | IsArrayElement = T && T->isArrayType(); |
| 79 | if (IsArrayElement) |
| 80 | T = T->getBaseElementTypeUnsafe(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 81 | else if (const FieldDecl *FD = getAsField(Path[I])) |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 82 | T = FD->getType().getTypePtr(); |
| 83 | else |
| 84 | // Path[I] describes a base class. |
| 85 | T = 0; |
| 86 | } |
| 87 | return IsArrayElement; |
| 88 | } |
| 89 | |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 90 | /// A path from a glvalue to a subobject of that glvalue. |
| 91 | struct SubobjectDesignator { |
| 92 | /// True if the subobject was named in a manner not supported by C++11. Such |
| 93 | /// lvalues can still be folded, but they are not core constant expressions |
| 94 | /// and we cannot perform lvalue-to-rvalue conversions on them. |
| 95 | bool Invalid : 1; |
| 96 | |
| 97 | /// Whether this designates an array element. |
| 98 | bool ArrayElement : 1; |
| 99 | |
| 100 | /// Whether this designates 'one past the end' of the current subobject. |
| 101 | bool OnePastTheEnd : 1; |
| 102 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 103 | typedef APValue::LValuePathEntry PathEntry; |
| 104 | |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 105 | /// The entries on the path from the glvalue to the designated subobject. |
| 106 | SmallVector<PathEntry, 8> Entries; |
| 107 | |
| 108 | SubobjectDesignator() : |
| 109 | Invalid(false), ArrayElement(false), OnePastTheEnd(false) {} |
| 110 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 111 | SubobjectDesignator(const APValue &V) : |
| 112 | Invalid(!V.isLValue() || !V.hasLValuePath()), ArrayElement(false), |
| 113 | OnePastTheEnd(false) { |
| 114 | if (!Invalid) { |
| 115 | ArrayRef<PathEntry> VEntries = V.getLValuePath(); |
| 116 | Entries.insert(Entries.end(), VEntries.begin(), VEntries.end()); |
| 117 | if (V.getLValueBase()) |
| 118 | ArrayElement = SubobjectIsArrayElement(V.getLValueBase()->getType(), |
| 119 | V.getLValuePath()); |
| 120 | else |
| 121 | assert(V.getLValuePath().empty() &&"Null pointer with nonempty path"); |
| 122 | } |
| 123 | } |
| 124 | |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 125 | void setInvalid() { |
| 126 | Invalid = true; |
| 127 | Entries.clear(); |
| 128 | } |
| 129 | /// Update this designator to refer to the given element within this array. |
| 130 | void addIndex(uint64_t N) { |
| 131 | if (Invalid) return; |
| 132 | if (OnePastTheEnd) { |
| 133 | setInvalid(); |
| 134 | return; |
| 135 | } |
| 136 | PathEntry Entry; |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 137 | Entry.ArrayIndex = N; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 138 | Entries.push_back(Entry); |
| 139 | ArrayElement = true; |
| 140 | } |
| 141 | /// Update this designator to refer to the given base or member of this |
| 142 | /// object. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 143 | void addDecl(const Decl *D, bool Virtual = false) { |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 144 | if (Invalid) return; |
| 145 | if (OnePastTheEnd) { |
| 146 | setInvalid(); |
| 147 | return; |
| 148 | } |
| 149 | PathEntry Entry; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 150 | APValue::BaseOrMemberType Value(D, Virtual); |
| 151 | Entry.BaseOrMember = Value.getOpaqueValue(); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 152 | Entries.push_back(Entry); |
| 153 | ArrayElement = false; |
| 154 | } |
| 155 | /// Add N to the address of this subobject. |
| 156 | void adjustIndex(uint64_t N) { |
| 157 | if (Invalid) return; |
| 158 | if (ArrayElement) { |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 159 | // FIXME: Make sure the index stays within bounds, or one past the end. |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 160 | Entries.back().ArrayIndex += N; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 161 | return; |
| 162 | } |
| 163 | if (OnePastTheEnd && N == (uint64_t)-1) |
| 164 | OnePastTheEnd = false; |
| 165 | else if (!OnePastTheEnd && N == 1) |
| 166 | OnePastTheEnd = true; |
| 167 | else if (N != 0) |
| 168 | setInvalid(); |
| 169 | } |
| 170 | }; |
| 171 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 172 | /// A core constant value. This can be the value of any constant expression, |
| 173 | /// or a pointer or reference to a non-static object or function parameter. |
| 174 | class CCValue : public APValue { |
| 175 | typedef llvm::APSInt APSInt; |
| 176 | typedef llvm::APFloat APFloat; |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 177 | /// If the value is a reference or pointer into a parameter or temporary, |
| 178 | /// this is the corresponding call stack frame. |
| 179 | CallStackFrame *CallFrame; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 180 | /// If the value is a reference or pointer, this is a description of how the |
| 181 | /// subobject was specified. |
| 182 | SubobjectDesignator Designator; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 183 | public: |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 184 | struct GlobalValue {}; |
| 185 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 186 | CCValue() {} |
| 187 | explicit CCValue(const APSInt &I) : APValue(I) {} |
| 188 | explicit CCValue(const APFloat &F) : APValue(F) {} |
| 189 | CCValue(const APValue *E, unsigned N) : APValue(E, N) {} |
| 190 | CCValue(const APSInt &R, const APSInt &I) : APValue(R, I) {} |
| 191 | CCValue(const APFloat &R, const APFloat &I) : APValue(R, I) {} |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 192 | CCValue(const CCValue &V) : APValue(V), CallFrame(V.CallFrame) {} |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 193 | CCValue(const Expr *B, const CharUnits &O, CallStackFrame *F, |
| 194 | const SubobjectDesignator &D) : |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 195 | APValue(B, O, APValue::NoLValuePath()), CallFrame(F), Designator(D) {} |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 196 | CCValue(const APValue &V, GlobalValue) : |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 197 | APValue(V), CallFrame(0), Designator(V) {} |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 198 | |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 199 | CallStackFrame *getLValueFrame() const { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 200 | assert(getKind() == LValue); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 201 | return CallFrame; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 202 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 203 | SubobjectDesignator &getLValueDesignator() { |
| 204 | assert(getKind() == LValue); |
| 205 | return Designator; |
| 206 | } |
| 207 | const SubobjectDesignator &getLValueDesignator() const { |
| 208 | return const_cast<CCValue*>(this)->getLValueDesignator(); |
| 209 | } |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 210 | }; |
| 211 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 212 | /// A stack frame in the constexpr call stack. |
| 213 | struct CallStackFrame { |
| 214 | EvalInfo &Info; |
| 215 | |
| 216 | /// Parent - The caller of this stack frame. |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 217 | CallStackFrame *Caller; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 218 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 219 | /// This - The binding for the this pointer in this call, if any. |
| 220 | const LValue *This; |
| 221 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 222 | /// ParmBindings - Parameter bindings for this function call, indexed by |
| 223 | /// parameters' function scope indices. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 224 | const CCValue *Arguments; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 225 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 226 | typedef llvm::DenseMap<const Expr*, CCValue> MapTy; |
| 227 | typedef MapTy::const_iterator temp_iterator; |
| 228 | /// Temporaries - Temporary lvalues materialized within this stack frame. |
| 229 | MapTy Temporaries; |
| 230 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 231 | CallStackFrame(EvalInfo &Info, const LValue *This, |
| 232 | const CCValue *Arguments); |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 233 | ~CallStackFrame(); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 234 | }; |
| 235 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 236 | struct EvalInfo { |
| 237 | const ASTContext &Ctx; |
| 238 | |
| 239 | /// EvalStatus - Contains information about the evaluation. |
| 240 | Expr::EvalStatus &EvalStatus; |
| 241 | |
| 242 | /// CurrentCall - The top of the constexpr call stack. |
| 243 | CallStackFrame *CurrentCall; |
| 244 | |
| 245 | /// NumCalls - The number of calls we've evaluated so far. |
| 246 | unsigned NumCalls; |
| 247 | |
| 248 | /// CallStackDepth - The number of calls in the call stack right now. |
| 249 | unsigned CallStackDepth; |
| 250 | |
| 251 | typedef llvm::DenseMap<const OpaqueValueExpr*, CCValue> MapTy; |
| 252 | /// OpaqueValues - Values used as the common expression in a |
| 253 | /// BinaryConditionalOperator. |
| 254 | MapTy OpaqueValues; |
| 255 | |
| 256 | /// BottomFrame - The frame in which evaluation started. This must be |
| 257 | /// initialized last. |
| 258 | CallStackFrame BottomFrame; |
| 259 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 260 | /// EvaluatingDecl - This is the declaration whose initializer is being |
| 261 | /// evaluated, if any. |
| 262 | const VarDecl *EvaluatingDecl; |
| 263 | |
| 264 | /// EvaluatingDeclValue - This is the value being constructed for the |
| 265 | /// declaration whose initializer is being evaluated, if any. |
| 266 | APValue *EvaluatingDeclValue; |
| 267 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 268 | |
| 269 | EvalInfo(const ASTContext &C, Expr::EvalStatus &S) |
| 270 | : Ctx(C), EvalStatus(S), CurrentCall(0), NumCalls(0), CallStackDepth(0), |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 271 | BottomFrame(*this, 0, 0), EvaluatingDecl(0), EvaluatingDeclValue(0) {} |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 272 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 273 | const CCValue *getOpaqueValue(const OpaqueValueExpr *e) const { |
| 274 | MapTy::const_iterator i = OpaqueValues.find(e); |
| 275 | if (i == OpaqueValues.end()) return 0; |
| 276 | return &i->second; |
| 277 | } |
| 278 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 279 | void setEvaluatingDecl(const VarDecl *VD, APValue &Value) { |
| 280 | EvaluatingDecl = VD; |
| 281 | EvaluatingDeclValue = &Value; |
| 282 | } |
| 283 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 284 | const LangOptions &getLangOpts() { return Ctx.getLangOptions(); } |
| 285 | }; |
| 286 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 287 | CallStackFrame::CallStackFrame(EvalInfo &Info, const LValue *This, |
| 288 | const CCValue *Arguments) |
| 289 | : Info(Info), Caller(Info.CurrentCall), This(This), Arguments(Arguments) { |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 290 | Info.CurrentCall = this; |
| 291 | ++Info.CallStackDepth; |
| 292 | } |
| 293 | |
| 294 | CallStackFrame::~CallStackFrame() { |
| 295 | assert(Info.CurrentCall == this && "calls retired out of order"); |
| 296 | --Info.CallStackDepth; |
| 297 | Info.CurrentCall = Caller; |
| 298 | } |
| 299 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 300 | struct ComplexValue { |
| 301 | private: |
| 302 | bool IsInt; |
| 303 | |
| 304 | public: |
| 305 | APSInt IntReal, IntImag; |
| 306 | APFloat FloatReal, FloatImag; |
| 307 | |
| 308 | ComplexValue() : FloatReal(APFloat::Bogus), FloatImag(APFloat::Bogus) {} |
| 309 | |
| 310 | void makeComplexFloat() { IsInt = false; } |
| 311 | bool isComplexFloat() const { return !IsInt; } |
| 312 | APFloat &getComplexFloatReal() { return FloatReal; } |
| 313 | APFloat &getComplexFloatImag() { return FloatImag; } |
| 314 | |
| 315 | void makeComplexInt() { IsInt = true; } |
| 316 | bool isComplexInt() const { return IsInt; } |
| 317 | APSInt &getComplexIntReal() { return IntReal; } |
| 318 | APSInt &getComplexIntImag() { return IntImag; } |
| 319 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 320 | void moveInto(CCValue &v) const { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 321 | if (isComplexFloat()) |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 322 | v = CCValue(FloatReal, FloatImag); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 323 | else |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 324 | v = CCValue(IntReal, IntImag); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 325 | } |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 326 | void setFrom(const CCValue &v) { |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 327 | assert(v.isComplexFloat() || v.isComplexInt()); |
| 328 | if (v.isComplexFloat()) { |
| 329 | makeComplexFloat(); |
| 330 | FloatReal = v.getComplexFloatReal(); |
| 331 | FloatImag = v.getComplexFloatImag(); |
| 332 | } else { |
| 333 | makeComplexInt(); |
| 334 | IntReal = v.getComplexIntReal(); |
| 335 | IntImag = v.getComplexIntImag(); |
| 336 | } |
| 337 | } |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 338 | }; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 339 | |
| 340 | struct LValue { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 341 | const Expr *Base; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 342 | CharUnits Offset; |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 343 | CallStackFrame *Frame; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 344 | SubobjectDesignator Designator; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 345 | |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 346 | const Expr *getLValueBase() const { return Base; } |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 347 | CharUnits &getLValueOffset() { return Offset; } |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 348 | const CharUnits &getLValueOffset() const { return Offset; } |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 349 | CallStackFrame *getLValueFrame() const { return Frame; } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 350 | SubobjectDesignator &getLValueDesignator() { return Designator; } |
| 351 | const SubobjectDesignator &getLValueDesignator() const { return Designator;} |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 352 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 353 | void moveInto(CCValue &V) const { |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 354 | V = CCValue(Base, Offset, Frame, Designator); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 355 | } |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 356 | void setFrom(const CCValue &V) { |
| 357 | assert(V.isLValue()); |
| 358 | Base = V.getLValueBase(); |
| 359 | Offset = V.getLValueOffset(); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 360 | Frame = V.getLValueFrame(); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 361 | Designator = V.getLValueDesignator(); |
| 362 | } |
| 363 | |
| 364 | void setExpr(const Expr *E, CallStackFrame *F = 0) { |
| 365 | Base = E; |
| 366 | Offset = CharUnits::Zero(); |
| 367 | Frame = F; |
| 368 | Designator = SubobjectDesignator(); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 369 | } |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 370 | }; |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 371 | } |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 372 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 373 | static bool Evaluate(CCValue &Result, EvalInfo &Info, const Expr *E); |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 374 | static bool EvaluateConstantExpression(APValue &Result, EvalInfo &Info, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 375 | const LValue &This, const Expr *E); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 376 | static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info); |
| 377 | static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info); |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 378 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 379 | static bool EvaluateIntegerOrLValue(const Expr *E, CCValue &Result, |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 380 | EvalInfo &Info); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 381 | static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 382 | static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 383 | |
| 384 | //===----------------------------------------------------------------------===// |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 385 | // Misc utilities |
| 386 | //===----------------------------------------------------------------------===// |
| 387 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 388 | /// Should this call expression be treated as a string literal? |
| 389 | static bool IsStringLiteralCall(const CallExpr *E) { |
| 390 | unsigned Builtin = E->isBuiltinCall(); |
| 391 | return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString || |
| 392 | Builtin == Builtin::BI__builtin___NSStringMakeConstantString); |
| 393 | } |
| 394 | |
Abramo Bagnara | e17a643 | 2010-05-14 17:07:14 +0000 | [diff] [blame] | 395 | static bool IsGlobalLValue(const Expr* E) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 396 | // C++11 [expr.const]p3 An address constant expression is a prvalue core |
| 397 | // constant expression of pointer type that evaluates to... |
| 398 | |
| 399 | // ... a null pointer value, or a prvalue core constant expression of type |
| 400 | // std::nullptr_t. |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 401 | if (!E) return true; |
| 402 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 403 | switch (E->getStmtClass()) { |
| 404 | default: |
| 405 | return false; |
| 406 | case Expr::DeclRefExprClass: { |
| 407 | const DeclRefExpr *DRE = cast<DeclRefExpr>(E); |
| 408 | // ... the address of an object with static storage duration, |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 409 | if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) |
| 410 | return VD->hasGlobalStorage(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 411 | // ... to the address of a function, |
| 412 | if (isa<FunctionDecl>(DRE->getDecl())) |
| 413 | return true; |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 414 | return false; |
| 415 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 416 | case Expr::CompoundLiteralExprClass: |
| 417 | return cast<CompoundLiteralExpr>(E)->isFileScope(); |
| 418 | // A string literal has static storage duration. |
| 419 | case Expr::StringLiteralClass: |
| 420 | case Expr::PredefinedExprClass: |
| 421 | case Expr::ObjCStringLiteralClass: |
| 422 | case Expr::ObjCEncodeExprClass: |
| 423 | return true; |
| 424 | case Expr::CallExprClass: |
| 425 | return IsStringLiteralCall(cast<CallExpr>(E)); |
| 426 | // For GCC compatibility, &&label has static storage duration. |
| 427 | case Expr::AddrLabelExprClass: |
| 428 | return true; |
| 429 | // A Block literal expression may be used as the initialization value for |
| 430 | // Block variables at global or local static scope. |
| 431 | case Expr::BlockExprClass: |
| 432 | return !cast<BlockExpr>(E)->getBlockDecl()->hasCaptures(); |
| 433 | } |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 436 | /// Check that this reference or pointer core constant expression is a valid |
| 437 | /// value for a constant expression. Type T should be either LValue or CCValue. |
| 438 | template<typename T> |
| 439 | static bool CheckLValueConstantExpression(const T &LVal, APValue &Value) { |
| 440 | if (!IsGlobalLValue(LVal.getLValueBase())) |
| 441 | return false; |
| 442 | |
| 443 | const SubobjectDesignator &Designator = LVal.getLValueDesignator(); |
| 444 | // A constant expression must refer to an object or be a null pointer. |
| 445 | if (Designator.Invalid || Designator.OnePastTheEnd || |
| 446 | (!LVal.getLValueBase() && !Designator.Entries.empty())) { |
| 447 | // FIXME: Check for out-of-bounds array indices. |
| 448 | // FIXME: This is not a constant expression. |
| 449 | Value = APValue(LVal.getLValueBase(), LVal.getLValueOffset(), |
| 450 | APValue::NoLValuePath()); |
| 451 | return true; |
| 452 | } |
| 453 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 454 | // FIXME: Null references are not constant expressions. |
| 455 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 456 | Value = APValue(LVal.getLValueBase(), LVal.getLValueOffset(), |
| 457 | Designator.Entries); |
| 458 | return true; |
| 459 | } |
| 460 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 461 | /// Check that this core constant expression value is a valid value for a |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 462 | /// constant expression, and if it is, produce the corresponding constant value. |
| 463 | static bool CheckConstantExpression(const CCValue &CCValue, APValue &Value) { |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 464 | if (!CCValue.isLValue()) { |
| 465 | Value = CCValue; |
| 466 | return true; |
| 467 | } |
| 468 | return CheckLValueConstantExpression(CCValue, Value); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 471 | const ValueDecl *GetLValueBaseDecl(const LValue &LVal) { |
| 472 | if (!LVal.Base) |
| 473 | return 0; |
| 474 | |
| 475 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LVal.Base)) |
| 476 | return DRE->getDecl(); |
| 477 | |
| 478 | // FIXME: Static data members accessed via a MemberExpr are represented as |
| 479 | // that MemberExpr. We should use the Decl directly instead. |
| 480 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(LVal.Base)) { |
| 481 | assert(!isa<FieldDecl>(ME->getMemberDecl()) && "shouldn't see fields here"); |
| 482 | return ME->getMemberDecl(); |
| 483 | } |
| 484 | |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | static bool IsLiteralLValue(const LValue &Value) { |
| 489 | return Value.Base && |
| 490 | !isa<DeclRefExpr>(Value.Base) && |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 491 | !isa<MemberExpr>(Value.Base) && |
| 492 | !isa<MaterializeTemporaryExpr>(Value.Base); |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 495 | static bool IsWeakDecl(const ValueDecl *Decl) { |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 496 | return Decl->hasAttr<WeakAttr>() || |
| 497 | Decl->hasAttr<WeakRefAttr>() || |
| 498 | Decl->isWeakImported(); |
| 499 | } |
| 500 | |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 501 | static bool IsWeakLValue(const LValue &Value) { |
| 502 | const ValueDecl *Decl = GetLValueBaseDecl(Value); |
| 503 | return Decl && IsWeakDecl(Decl); |
| 504 | } |
| 505 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 506 | static bool EvalPointerValueAsBool(const LValue &Value, bool &Result) { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 507 | const Expr* Base = Value.Base; |
Rafael Espindola | a7d3c04 | 2010-05-07 15:18:43 +0000 | [diff] [blame] | 508 | |
John McCall | 3554283 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 509 | // A null base expression indicates a null pointer. These are always |
| 510 | // evaluatable, and they are false unless the offset is zero. |
| 511 | if (!Base) { |
| 512 | Result = !Value.Offset.isZero(); |
| 513 | return true; |
| 514 | } |
Rafael Espindola | a7d3c04 | 2010-05-07 15:18:43 +0000 | [diff] [blame] | 515 | |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 516 | // Require the base expression to be a global l-value. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 517 | // FIXME: C++11 requires such conversions. Remove this check. |
Abramo Bagnara | e17a643 | 2010-05-14 17:07:14 +0000 | [diff] [blame] | 518 | if (!IsGlobalLValue(Base)) return false; |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 519 | |
John McCall | 3554283 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 520 | // We have a non-null base expression. These are generally known to |
| 521 | // be true, but if it'a decl-ref to a weak symbol it can be null at |
| 522 | // runtime. |
John McCall | 3554283 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 523 | Result = true; |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 524 | return !IsWeakLValue(Value); |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 527 | static bool HandleConversionToBool(const CCValue &Val, bool &Result) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 528 | switch (Val.getKind()) { |
| 529 | case APValue::Uninitialized: |
| 530 | return false; |
| 531 | case APValue::Int: |
| 532 | Result = Val.getInt().getBoolValue(); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 533 | return true; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 534 | case APValue::Float: |
| 535 | Result = !Val.getFloat().isZero(); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 536 | return true; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 537 | case APValue::ComplexInt: |
| 538 | Result = Val.getComplexIntReal().getBoolValue() || |
| 539 | Val.getComplexIntImag().getBoolValue(); |
| 540 | return true; |
| 541 | case APValue::ComplexFloat: |
| 542 | Result = !Val.getComplexFloatReal().isZero() || |
| 543 | !Val.getComplexFloatImag().isZero(); |
| 544 | return true; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 545 | case APValue::LValue: { |
| 546 | LValue PointerResult; |
| 547 | PointerResult.setFrom(Val); |
| 548 | return EvalPointerValueAsBool(PointerResult, Result); |
| 549 | } |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 550 | case APValue::Vector: |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 551 | case APValue::Array: |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 552 | case APValue::Struct: |
| 553 | case APValue::Union: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 554 | return false; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 557 | llvm_unreachable("unknown APValue kind"); |
| 558 | } |
| 559 | |
| 560 | static bool EvaluateAsBooleanCondition(const Expr *E, bool &Result, |
| 561 | EvalInfo &Info) { |
| 562 | assert(E->isRValue() && "missing lvalue-to-rvalue conv in bool condition"); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 563 | CCValue Val; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 564 | if (!Evaluate(Val, Info, E)) |
| 565 | return false; |
| 566 | return HandleConversionToBool(Val, Result); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 569 | static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 570 | APFloat &Value, const ASTContext &Ctx) { |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 571 | unsigned DestWidth = Ctx.getIntWidth(DestType); |
| 572 | // Determine whether we are converting to unsigned or signed. |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 573 | bool DestSigned = DestType->isSignedIntegerOrEnumerationType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 574 | |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 575 | // FIXME: Warning for overflow. |
Jeffrey Yasskin | 3e1ef78 | 2011-07-15 17:03:07 +0000 | [diff] [blame] | 576 | APSInt Result(DestWidth, !DestSigned); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 577 | bool ignored; |
Jeffrey Yasskin | 3e1ef78 | 2011-07-15 17:03:07 +0000 | [diff] [blame] | 578 | (void)Value.convertToInteger(Result, llvm::APFloat::rmTowardZero, &ignored); |
| 579 | return Result; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 582 | static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 583 | APFloat &Value, const ASTContext &Ctx) { |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 584 | bool ignored; |
| 585 | APFloat Result = Value; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 586 | Result.convert(Ctx.getFloatTypeSemantics(DestType), |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 587 | APFloat::rmNearestTiesToEven, &ignored); |
| 588 | return Result; |
| 589 | } |
| 590 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 591 | static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 592 | APSInt &Value, const ASTContext &Ctx) { |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 593 | unsigned DestWidth = Ctx.getIntWidth(DestType); |
| 594 | APSInt Result = Value; |
| 595 | // Figure out if this is a truncate, extend or noop cast. |
| 596 | // If the input is signed, do a sign extend, noop, or truncate. |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 597 | Result = Result.extOrTrunc(DestWidth); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 598 | Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType()); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 599 | return Result; |
| 600 | } |
| 601 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 602 | static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 603 | APSInt &Value, const ASTContext &Ctx) { |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 604 | |
| 605 | APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1); |
| 606 | Result.convertFromAPInt(Value, Value.isSigned(), |
| 607 | APFloat::rmNearestTiesToEven); |
| 608 | return Result; |
| 609 | } |
| 610 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 611 | /// If the given LValue refers to a base subobject of some object, find the most |
| 612 | /// derived object and the corresponding complete record type. This is necessary |
| 613 | /// in order to find the offset of a virtual base class. |
| 614 | static bool ExtractMostDerivedObject(EvalInfo &Info, LValue &Result, |
| 615 | const CXXRecordDecl *&MostDerivedType) { |
| 616 | SubobjectDesignator &D = Result.Designator; |
| 617 | if (D.Invalid || !Result.Base) |
| 618 | return false; |
| 619 | |
| 620 | const Type *T = Result.Base->getType().getTypePtr(); |
| 621 | |
| 622 | // Find path prefix which leads to the most-derived subobject. |
| 623 | unsigned MostDerivedPathLength = 0; |
| 624 | MostDerivedType = T->getAsCXXRecordDecl(); |
| 625 | bool MostDerivedIsArrayElement = false; |
| 626 | |
| 627 | for (unsigned I = 0, N = D.Entries.size(); I != N; ++I) { |
| 628 | bool IsArray = T && T->isArrayType(); |
| 629 | if (IsArray) |
| 630 | T = T->getBaseElementTypeUnsafe(); |
| 631 | else if (const FieldDecl *FD = getAsField(D.Entries[I])) |
| 632 | T = FD->getType().getTypePtr(); |
| 633 | else |
| 634 | T = 0; |
| 635 | |
| 636 | if (T) { |
| 637 | MostDerivedType = T->getAsCXXRecordDecl(); |
| 638 | MostDerivedPathLength = I + 1; |
| 639 | MostDerivedIsArrayElement = IsArray; |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | if (!MostDerivedType) |
| 644 | return false; |
| 645 | |
| 646 | // (B*)&d + 1 has no most-derived object. |
| 647 | if (D.OnePastTheEnd && MostDerivedPathLength != D.Entries.size()) |
| 648 | return false; |
| 649 | |
| 650 | // Remove the trailing base class path entries and their offsets. |
| 651 | const RecordDecl *RD = MostDerivedType; |
| 652 | for (unsigned I = MostDerivedPathLength, N = D.Entries.size(); I != N; ++I) { |
| 653 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 654 | const CXXRecordDecl *Base = getAsBaseClass(D.Entries[I]); |
| 655 | if (isVirtualBaseClass(D.Entries[I])) { |
| 656 | assert(I == MostDerivedPathLength && |
| 657 | "virtual base class must be immediately after most-derived class"); |
| 658 | Result.Offset -= Layout.getVBaseClassOffset(Base); |
| 659 | } else |
| 660 | Result.Offset -= Layout.getBaseClassOffset(Base); |
| 661 | RD = Base; |
| 662 | } |
| 663 | D.Entries.resize(MostDerivedPathLength); |
| 664 | D.ArrayElement = MostDerivedIsArrayElement; |
| 665 | return true; |
| 666 | } |
| 667 | |
| 668 | static void HandleLValueDirectBase(EvalInfo &Info, LValue &Obj, |
| 669 | const CXXRecordDecl *Derived, |
| 670 | const CXXRecordDecl *Base, |
| 671 | const ASTRecordLayout *RL = 0) { |
| 672 | if (!RL) RL = &Info.Ctx.getASTRecordLayout(Derived); |
| 673 | Obj.getLValueOffset() += RL->getBaseClassOffset(Base); |
| 674 | Obj.Designator.addDecl(Base, /*Virtual*/ false); |
| 675 | } |
| 676 | |
| 677 | static bool HandleLValueBase(EvalInfo &Info, LValue &Obj, |
| 678 | const CXXRecordDecl *DerivedDecl, |
| 679 | const CXXBaseSpecifier *Base) { |
| 680 | const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl(); |
| 681 | |
| 682 | if (!Base->isVirtual()) { |
| 683 | HandleLValueDirectBase(Info, Obj, DerivedDecl, BaseDecl); |
| 684 | return true; |
| 685 | } |
| 686 | |
| 687 | // Extract most-derived object and corresponding type. |
| 688 | if (!ExtractMostDerivedObject(Info, Obj, DerivedDecl)) |
| 689 | return false; |
| 690 | |
| 691 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl); |
| 692 | Obj.getLValueOffset() += Layout.getVBaseClassOffset(BaseDecl); |
| 693 | Obj.Designator.addDecl(BaseDecl, /*Virtual*/ true); |
| 694 | return true; |
| 695 | } |
| 696 | |
| 697 | /// Update LVal to refer to the given field, which must be a member of the type |
| 698 | /// currently described by LVal. |
| 699 | static void HandleLValueMember(EvalInfo &Info, LValue &LVal, |
| 700 | const FieldDecl *FD, |
| 701 | const ASTRecordLayout *RL = 0) { |
| 702 | if (!RL) |
| 703 | RL = &Info.Ctx.getASTRecordLayout(FD->getParent()); |
| 704 | |
| 705 | unsigned I = FD->getFieldIndex(); |
| 706 | LVal.Offset += Info.Ctx.toCharUnitsFromBits(RL->getFieldOffset(I)); |
| 707 | LVal.Designator.addDecl(FD); |
| 708 | } |
| 709 | |
| 710 | /// Get the size of the given type in char units. |
| 711 | static bool HandleSizeof(EvalInfo &Info, QualType Type, CharUnits &Size) { |
| 712 | // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc |
| 713 | // extension. |
| 714 | if (Type->isVoidType() || Type->isFunctionType()) { |
| 715 | Size = CharUnits::One(); |
| 716 | return true; |
| 717 | } |
| 718 | |
| 719 | if (!Type->isConstantSizeType()) { |
| 720 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
| 721 | return false; |
| 722 | } |
| 723 | |
| 724 | Size = Info.Ctx.getTypeSizeInChars(Type); |
| 725 | return true; |
| 726 | } |
| 727 | |
| 728 | /// Update a pointer value to model pointer arithmetic. |
| 729 | /// \param Info - Information about the ongoing evaluation. |
| 730 | /// \param LVal - The pointer value to be updated. |
| 731 | /// \param EltTy - The pointee type represented by LVal. |
| 732 | /// \param Adjustment - The adjustment, in objects of type EltTy, to add. |
| 733 | static bool HandleLValueArrayAdjustment(EvalInfo &Info, LValue &LVal, |
| 734 | QualType EltTy, int64_t Adjustment) { |
| 735 | CharUnits SizeOfPointee; |
| 736 | if (!HandleSizeof(Info, EltTy, SizeOfPointee)) |
| 737 | return false; |
| 738 | |
| 739 | // Compute the new offset in the appropriate width. |
| 740 | LVal.Offset += Adjustment * SizeOfPointee; |
| 741 | LVal.Designator.adjustIndex(Adjustment); |
| 742 | return true; |
| 743 | } |
| 744 | |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 745 | /// Try to evaluate the initializer for a variable declaration. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 746 | static bool EvaluateVarDeclInit(EvalInfo &Info, const Expr *E,const VarDecl *VD, |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 747 | CallStackFrame *Frame, CCValue &Result) { |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 748 | // If this is a parameter to an active constexpr function call, perform |
| 749 | // argument substitution. |
| 750 | if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(VD)) { |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 751 | if (!Frame || !Frame->Arguments) |
| 752 | return false; |
| 753 | Result = Frame->Arguments[PVD->getFunctionScopeIndex()]; |
| 754 | return true; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 755 | } |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 756 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 757 | // If we're currently evaluating the initializer of this declaration, use that |
| 758 | // in-flight value. |
| 759 | if (Info.EvaluatingDecl == VD) { |
| 760 | Result = CCValue(*Info.EvaluatingDeclValue, CCValue::GlobalValue()); |
| 761 | return !Result.isUninit(); |
| 762 | } |
| 763 | |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 764 | // Never evaluate the initializer of a weak variable. We can't be sure that |
| 765 | // this is the definition which will be used. |
| 766 | if (IsWeakDecl(VD)) |
| 767 | return false; |
| 768 | |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 769 | const Expr *Init = VD->getAnyInitializer(); |
Richard Smith | db1822c | 2011-11-08 01:31:09 +0000 | [diff] [blame] | 770 | if (!Init || Init->isValueDependent()) |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 771 | return false; |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 772 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 773 | if (APValue *V = VD->getEvaluatedValue()) { |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 774 | Result = CCValue(*V, CCValue::GlobalValue()); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 775 | return !Result.isUninit(); |
| 776 | } |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 777 | |
| 778 | if (VD->isEvaluatingValue()) |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 779 | return false; |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 780 | |
| 781 | VD->setEvaluatingValue(); |
| 782 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 783 | Expr::EvalStatus EStatus; |
| 784 | EvalInfo InitInfo(Info.Ctx, EStatus); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 785 | APValue EvalResult; |
| 786 | InitInfo.setEvaluatingDecl(VD, EvalResult); |
| 787 | LValue LVal; |
| 788 | LVal.setExpr(E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 789 | // FIXME: The caller will need to know whether the value was a constant |
| 790 | // expression. If not, we should propagate up a diagnostic. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 791 | if (!EvaluateConstantExpression(EvalResult, InitInfo, LVal, Init)) { |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 792 | // FIXME: If the evaluation failure was not permanent (for instance, if we |
| 793 | // hit a variable with no declaration yet, or a constexpr function with no |
| 794 | // definition yet), the standard is unclear as to how we should behave. |
| 795 | // |
| 796 | // Either the initializer should be evaluated when the variable is defined, |
| 797 | // or a failed evaluation of the initializer should be reattempted each time |
| 798 | // it is used. |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 799 | VD->setEvaluatedValue(APValue()); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 800 | return false; |
| 801 | } |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 802 | |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 803 | VD->setEvaluatedValue(EvalResult); |
| 804 | Result = CCValue(EvalResult, CCValue::GlobalValue()); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 805 | return true; |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 808 | static bool IsConstNonVolatile(QualType T) { |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 809 | Qualifiers Quals = T.getQualifiers(); |
| 810 | return Quals.hasConst() && !Quals.hasVolatile(); |
| 811 | } |
| 812 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 813 | /// Get the base index of the given base class within an APValue representing |
| 814 | /// the given derived class. |
| 815 | static unsigned getBaseIndex(const CXXRecordDecl *Derived, |
| 816 | const CXXRecordDecl *Base) { |
| 817 | Base = Base->getCanonicalDecl(); |
| 818 | unsigned Index = 0; |
| 819 | for (CXXRecordDecl::base_class_const_iterator I = Derived->bases_begin(), |
| 820 | E = Derived->bases_end(); I != E; ++I, ++Index) { |
| 821 | if (I->getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base) |
| 822 | return Index; |
| 823 | } |
| 824 | |
| 825 | llvm_unreachable("base class missing from derived class's bases list"); |
| 826 | } |
| 827 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 828 | /// Extract the designated sub-object of an rvalue. |
| 829 | static bool ExtractSubobject(EvalInfo &Info, CCValue &Obj, QualType ObjType, |
| 830 | const SubobjectDesignator &Sub, QualType SubType) { |
| 831 | if (Sub.Invalid || Sub.OnePastTheEnd) |
| 832 | return false; |
| 833 | if (Sub.Entries.empty()) { |
| 834 | assert(Info.Ctx.hasSameUnqualifiedType(ObjType, SubType) && |
| 835 | "Unexpected subobject type"); |
| 836 | return true; |
| 837 | } |
| 838 | |
| 839 | assert(!Obj.isLValue() && "extracting subobject of lvalue"); |
| 840 | const APValue *O = &Obj; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 841 | // Walk the designator's path to find the subobject. |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 842 | for (unsigned I = 0, N = Sub.Entries.size(); I != N; ++I) { |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 843 | if (ObjType->isArrayType()) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 844 | // Next subobject is an array element. |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 845 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(ObjType); |
| 846 | if (!CAT) |
| 847 | return false; |
| 848 | uint64_t Index = Sub.Entries[I].ArrayIndex; |
| 849 | if (CAT->getSize().ule(Index)) |
| 850 | return false; |
| 851 | if (O->getArrayInitializedElts() > Index) |
| 852 | O = &O->getArrayInitializedElt(Index); |
| 853 | else |
| 854 | O = &O->getArrayFiller(); |
| 855 | ObjType = CAT->getElementType(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 856 | } else if (const FieldDecl *Field = getAsField(Sub.Entries[I])) { |
| 857 | // Next subobject is a class, struct or union field. |
| 858 | RecordDecl *RD = ObjType->castAs<RecordType>()->getDecl(); |
| 859 | if (RD->isUnion()) { |
| 860 | const FieldDecl *UnionField = O->getUnionField(); |
| 861 | if (!UnionField || |
| 862 | UnionField->getCanonicalDecl() != Field->getCanonicalDecl()) |
| 863 | return false; |
| 864 | O = &O->getUnionValue(); |
| 865 | } else |
| 866 | O = &O->getStructField(Field->getFieldIndex()); |
| 867 | ObjType = Field->getType(); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 868 | } else { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 869 | // Next subobject is a base class. |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 870 | const CXXRecordDecl *Derived = ObjType->getAsCXXRecordDecl(); |
| 871 | const CXXRecordDecl *Base = getAsBaseClass(Sub.Entries[I]); |
| 872 | O = &O->getStructBase(getBaseIndex(Derived, Base)); |
| 873 | ObjType = Info.Ctx.getRecordType(Base); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 874 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 875 | |
| 876 | if (O->isUninit()) |
| 877 | return false; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | assert(Info.Ctx.hasSameUnqualifiedType(ObjType, SubType) && |
| 881 | "Unexpected subobject type"); |
| 882 | Obj = CCValue(*O, CCValue::GlobalValue()); |
| 883 | return true; |
| 884 | } |
| 885 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 886 | /// HandleLValueToRValueConversion - Perform an lvalue-to-rvalue conversion on |
| 887 | /// the given lvalue. This can also be used for 'lvalue-to-lvalue' conversions |
| 888 | /// for looking up the glvalue referred to by an entity of reference type. |
| 889 | /// |
| 890 | /// \param Info - Information about the ongoing evaluation. |
| 891 | /// \param Type - The type we expect this conversion to produce. |
| 892 | /// \param LVal - The glvalue on which we are attempting to perform this action. |
| 893 | /// \param RVal - The produced value will be placed here. |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 894 | static bool HandleLValueToRValueConversion(EvalInfo &Info, QualType Type, |
| 895 | const LValue &LVal, CCValue &RVal) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 896 | const Expr *Base = LVal.Base; |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 897 | CallStackFrame *Frame = LVal.Frame; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 898 | |
| 899 | // FIXME: Indirection through a null pointer deserves a diagnostic. |
| 900 | if (!Base) |
| 901 | return false; |
| 902 | |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 903 | if (const ValueDecl *D = GetLValueBaseDecl(LVal)) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 904 | // In C++98, const, non-volatile integers initialized with ICEs are ICEs. |
| 905 | // In C++11, constexpr, non-volatile variables initialized with constant |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 906 | // expressions are constant expressions too. Inside constexpr functions, |
| 907 | // parameters are constant expressions even if they're non-const. |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 908 | // In C, such things can also be folded, although they are not ICEs. |
| 909 | // |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 910 | // FIXME: volatile-qualified ParmVarDecls need special handling. A literal |
| 911 | // interpretation of C++11 suggests that volatile parameters are OK if |
| 912 | // they're never read (there's no prohibition against constructing volatile |
| 913 | // objects in constant expressions), but lvalue-to-rvalue conversions on |
| 914 | // them are not permitted. |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 915 | const VarDecl *VD = dyn_cast<VarDecl>(D); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 916 | QualType VT = VD->getType(); |
Richard Smith | cd68992 | 2011-11-07 03:22:51 +0000 | [diff] [blame] | 917 | if (!VD || VD->isInvalidDecl()) |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 918 | return false; |
| 919 | if (!isa<ParmVarDecl>(VD)) { |
| 920 | if (!IsConstNonVolatile(VT)) |
| 921 | return false; |
Richard Smith | cd68992 | 2011-11-07 03:22:51 +0000 | [diff] [blame] | 922 | // FIXME: Allow folding of values of any literal type in all languages. |
| 923 | if (!VT->isIntegralOrEnumerationType() && !VT->isRealFloatingType() && |
| 924 | !VD->isConstexpr()) |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 925 | return false; |
| 926 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 927 | if (!EvaluateVarDeclInit(Info, LVal.Base, VD, Frame, RVal)) |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 928 | return false; |
| 929 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 930 | if (isa<ParmVarDecl>(VD) || !VD->getAnyInitializer()->isLValue()) |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 931 | return ExtractSubobject(Info, RVal, VT, LVal.Designator, Type); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 932 | |
| 933 | // The declaration was initialized by an lvalue, with no lvalue-to-rvalue |
| 934 | // conversion. This happens when the declaration and the lvalue should be |
| 935 | // considered synonymous, for instance when initializing an array of char |
| 936 | // from a string literal. Continue as if the initializer lvalue was the |
| 937 | // value we were originally given. |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 938 | assert(RVal.getLValueOffset().isZero() && |
| 939 | "offset for lvalue init of non-reference"); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 940 | Base = RVal.getLValueBase(); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 941 | Frame = RVal.getLValueFrame(); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 944 | // FIXME: Support PredefinedExpr, ObjCEncodeExpr, MakeStringConstant |
| 945 | if (const StringLiteral *S = dyn_cast<StringLiteral>(Base)) { |
| 946 | const SubobjectDesignator &Designator = LVal.Designator; |
| 947 | if (Designator.Invalid || Designator.Entries.size() != 1) |
| 948 | return false; |
| 949 | |
| 950 | assert(Type->isIntegerType() && "string element not integer type"); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 951 | uint64_t Index = Designator.Entries[0].ArrayIndex; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 952 | if (Index > S->getLength()) |
| 953 | return false; |
| 954 | APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(), |
| 955 | Type->isUnsignedIntegerType()); |
| 956 | if (Index < S->getLength()) |
| 957 | Value = S->getCodeUnit(Index); |
| 958 | RVal = CCValue(Value); |
| 959 | return true; |
| 960 | } |
| 961 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 962 | if (Frame) { |
| 963 | // If this is a temporary expression with a nontrivial initializer, grab the |
| 964 | // value from the relevant stack frame. |
| 965 | RVal = Frame->Temporaries[Base]; |
| 966 | } else if (const CompoundLiteralExpr *CLE |
| 967 | = dyn_cast<CompoundLiteralExpr>(Base)) { |
| 968 | // In C99, a CompoundLiteralExpr is an lvalue, and we defer evaluating the |
| 969 | // initializer until now for such expressions. Such an expression can't be |
| 970 | // an ICE in C, so this only matters for fold. |
| 971 | assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?"); |
| 972 | if (!Evaluate(RVal, Info, CLE->getInitializer())) |
| 973 | return false; |
| 974 | } else |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 975 | return false; |
| 976 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 977 | return ExtractSubobject(Info, RVal, Base->getType(), LVal.Designator, Type); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 978 | } |
| 979 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 980 | /// Build an lvalue for the object argument of a member function call. |
| 981 | static bool EvaluateObjectArgument(EvalInfo &Info, const Expr *Object, |
| 982 | LValue &This) { |
| 983 | if (Object->getType()->isPointerType()) |
| 984 | return EvaluatePointer(Object, This, Info); |
| 985 | |
| 986 | if (Object->isGLValue()) |
| 987 | return EvaluateLValue(Object, This, Info); |
| 988 | |
| 989 | // Implicitly promote a prvalue *this object to a glvalue. |
| 990 | This.setExpr(Object, Info.CurrentCall); |
| 991 | return EvaluateConstantExpression(Info.CurrentCall->Temporaries[Object], Info, |
| 992 | This, Object); |
| 993 | } |
| 994 | |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 995 | namespace { |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 996 | enum EvalStmtResult { |
| 997 | /// Evaluation failed. |
| 998 | ESR_Failed, |
| 999 | /// Hit a 'return' statement. |
| 1000 | ESR_Returned, |
| 1001 | /// Evaluation succeeded. |
| 1002 | ESR_Succeeded |
| 1003 | }; |
| 1004 | } |
| 1005 | |
| 1006 | // Evaluate a statement. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1007 | static EvalStmtResult EvaluateStmt(CCValue &Result, EvalInfo &Info, |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1008 | const Stmt *S) { |
| 1009 | switch (S->getStmtClass()) { |
| 1010 | default: |
| 1011 | return ESR_Failed; |
| 1012 | |
| 1013 | case Stmt::NullStmtClass: |
| 1014 | case Stmt::DeclStmtClass: |
| 1015 | return ESR_Succeeded; |
| 1016 | |
| 1017 | case Stmt::ReturnStmtClass: |
| 1018 | if (Evaluate(Result, Info, cast<ReturnStmt>(S)->getRetValue())) |
| 1019 | return ESR_Returned; |
| 1020 | return ESR_Failed; |
| 1021 | |
| 1022 | case Stmt::CompoundStmtClass: { |
| 1023 | const CompoundStmt *CS = cast<CompoundStmt>(S); |
| 1024 | for (CompoundStmt::const_body_iterator BI = CS->body_begin(), |
| 1025 | BE = CS->body_end(); BI != BE; ++BI) { |
| 1026 | EvalStmtResult ESR = EvaluateStmt(Result, Info, *BI); |
| 1027 | if (ESR != ESR_Succeeded) |
| 1028 | return ESR; |
| 1029 | } |
| 1030 | return ESR_Succeeded; |
| 1031 | } |
| 1032 | } |
| 1033 | } |
| 1034 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1035 | namespace { |
| 1036 | typedef SmallVector<CCValue, 16> ArgVector; |
| 1037 | } |
| 1038 | |
| 1039 | /// EvaluateArgs - Evaluate the arguments to a function call. |
| 1040 | static bool EvaluateArgs(ArrayRef<const Expr*> Args, ArgVector &ArgValues, |
| 1041 | EvalInfo &Info) { |
| 1042 | for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end(); |
| 1043 | I != E; ++I) |
| 1044 | if (!Evaluate(ArgValues[I - Args.begin()], Info, *I)) |
| 1045 | return false; |
| 1046 | return true; |
| 1047 | } |
| 1048 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1049 | /// Evaluate a function call. |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1050 | static bool HandleFunctionCall(const LValue *This, ArrayRef<const Expr*> Args, |
| 1051 | const Stmt *Body, EvalInfo &Info, |
| 1052 | CCValue &Result) { |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1053 | // FIXME: Implement a proper call limit, along with a command-line flag. |
| 1054 | if (Info.NumCalls >= 1000000 || Info.CallStackDepth >= 512) |
| 1055 | return false; |
| 1056 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1057 | ArgVector ArgValues(Args.size()); |
| 1058 | if (!EvaluateArgs(Args, ArgValues, Info)) |
| 1059 | return false; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1060 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1061 | CallStackFrame Frame(Info, This, ArgValues.data()); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1062 | return EvaluateStmt(Result, Info, Body) == ESR_Returned; |
| 1063 | } |
| 1064 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1065 | /// Evaluate a constructor call. |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1066 | static bool HandleConstructorCall(const LValue &This, |
| 1067 | ArrayRef<const Expr*> Args, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1068 | const CXXConstructorDecl *Definition, |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1069 | EvalInfo &Info, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1070 | APValue &Result) { |
| 1071 | if (Info.NumCalls >= 1000000 || Info.CallStackDepth >= 512) |
| 1072 | return false; |
| 1073 | |
| 1074 | ArgVector ArgValues(Args.size()); |
| 1075 | if (!EvaluateArgs(Args, ArgValues, Info)) |
| 1076 | return false; |
| 1077 | |
| 1078 | CallStackFrame Frame(Info, &This, ArgValues.data()); |
| 1079 | |
| 1080 | // If it's a delegating constructor, just delegate. |
| 1081 | if (Definition->isDelegatingConstructor()) { |
| 1082 | CXXConstructorDecl::init_const_iterator I = Definition->init_begin(); |
| 1083 | return EvaluateConstantExpression(Result, Info, This, (*I)->getInit()); |
| 1084 | } |
| 1085 | |
| 1086 | // Reserve space for the struct members. |
| 1087 | const CXXRecordDecl *RD = Definition->getParent(); |
| 1088 | if (!RD->isUnion()) |
| 1089 | Result = APValue(APValue::UninitStruct(), RD->getNumBases(), |
| 1090 | std::distance(RD->field_begin(), RD->field_end())); |
| 1091 | |
| 1092 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 1093 | |
| 1094 | unsigned BasesSeen = 0; |
| 1095 | #ifndef NDEBUG |
| 1096 | CXXRecordDecl::base_class_const_iterator BaseIt = RD->bases_begin(); |
| 1097 | #endif |
| 1098 | for (CXXConstructorDecl::init_const_iterator I = Definition->init_begin(), |
| 1099 | E = Definition->init_end(); I != E; ++I) { |
| 1100 | if ((*I)->isBaseInitializer()) { |
| 1101 | QualType BaseType((*I)->getBaseClass(), 0); |
| 1102 | #ifndef NDEBUG |
| 1103 | // Non-virtual base classes are initialized in the order in the class |
| 1104 | // definition. We cannot have a virtual base class for a literal type. |
| 1105 | assert(!BaseIt->isVirtual() && "virtual base for literal type"); |
| 1106 | assert(Info.Ctx.hasSameType(BaseIt->getType(), BaseType) && |
| 1107 | "base class initializers not in expected order"); |
| 1108 | ++BaseIt; |
| 1109 | #endif |
| 1110 | LValue Subobject = This; |
| 1111 | HandleLValueDirectBase(Info, Subobject, RD, |
| 1112 | BaseType->getAsCXXRecordDecl(), &Layout); |
| 1113 | if (!EvaluateConstantExpression(Result.getStructBase(BasesSeen++), Info, |
| 1114 | Subobject, (*I)->getInit())) |
| 1115 | return false; |
| 1116 | } else if (FieldDecl *FD = (*I)->getMember()) { |
| 1117 | LValue Subobject = This; |
| 1118 | HandleLValueMember(Info, Subobject, FD, &Layout); |
| 1119 | if (RD->isUnion()) { |
| 1120 | Result = APValue(FD); |
| 1121 | if (!EvaluateConstantExpression(Result.getUnionValue(), Info, |
| 1122 | Subobject, (*I)->getInit())) |
| 1123 | return false; |
| 1124 | } else if (!EvaluateConstantExpression( |
| 1125 | Result.getStructField(FD->getFieldIndex()), |
| 1126 | Info, Subobject, (*I)->getInit())) |
| 1127 | return false; |
| 1128 | } else { |
| 1129 | // FIXME: handle indirect field initializers |
| 1130 | return false; |
| 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | return true; |
| 1135 | } |
| 1136 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1137 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 1138 | class HasSideEffect |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1139 | : public ConstStmtVisitor<HasSideEffect, bool> { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 1140 | const ASTContext &Ctx; |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1141 | public: |
| 1142 | |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 1143 | HasSideEffect(const ASTContext &C) : Ctx(C) {} |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1144 | |
| 1145 | // Unhandled nodes conservatively default to having side effects. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1146 | bool VisitStmt(const Stmt *S) { |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1147 | return true; |
| 1148 | } |
| 1149 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1150 | bool VisitParenExpr(const ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 1151 | bool VisitGenericSelectionExpr(const GenericSelectionExpr *E) { |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1152 | return Visit(E->getResultExpr()); |
| 1153 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1154 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 1155 | if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1156 | return true; |
| 1157 | return false; |
| 1158 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1159 | bool VisitObjCIvarRefExpr(const ObjCIvarRefExpr *E) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 1160 | if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1161 | return true; |
| 1162 | return false; |
| 1163 | } |
| 1164 | bool VisitBlockDeclRefExpr (const BlockDeclRefExpr *E) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 1165 | if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1166 | return true; |
| 1167 | return false; |
| 1168 | } |
| 1169 | |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1170 | // We don't want to evaluate BlockExprs multiple times, as they generate |
| 1171 | // a ton of code. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1172 | bool VisitBlockExpr(const BlockExpr *E) { return true; } |
| 1173 | bool VisitPredefinedExpr(const PredefinedExpr *E) { return false; } |
| 1174 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1175 | { return Visit(E->getInitializer()); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1176 | bool VisitMemberExpr(const MemberExpr *E) { return Visit(E->getBase()); } |
| 1177 | bool VisitIntegerLiteral(const IntegerLiteral *E) { return false; } |
| 1178 | bool VisitFloatingLiteral(const FloatingLiteral *E) { return false; } |
| 1179 | bool VisitStringLiteral(const StringLiteral *E) { return false; } |
| 1180 | bool VisitCharacterLiteral(const CharacterLiteral *E) { return false; } |
| 1181 | bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1182 | { return false; } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1183 | bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E) |
Mike Stump | 980ca22 | 2009-10-29 20:48:09 +0000 | [diff] [blame] | 1184 | { return Visit(E->getLHS()) || Visit(E->getRHS()); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1185 | bool VisitChooseExpr(const ChooseExpr *E) |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 1186 | { return Visit(E->getChosenSubExpr(Ctx)); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1187 | bool VisitCastExpr(const CastExpr *E) { return Visit(E->getSubExpr()); } |
| 1188 | bool VisitBinAssign(const BinaryOperator *E) { return true; } |
| 1189 | bool VisitCompoundAssignOperator(const BinaryOperator *E) { return true; } |
| 1190 | bool VisitBinaryOperator(const BinaryOperator *E) |
Mike Stump | 980ca22 | 2009-10-29 20:48:09 +0000 | [diff] [blame] | 1191 | { return Visit(E->getLHS()) || Visit(E->getRHS()); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1192 | bool VisitUnaryPreInc(const UnaryOperator *E) { return true; } |
| 1193 | bool VisitUnaryPostInc(const UnaryOperator *E) { return true; } |
| 1194 | bool VisitUnaryPreDec(const UnaryOperator *E) { return true; } |
| 1195 | bool VisitUnaryPostDec(const UnaryOperator *E) { return true; } |
| 1196 | bool VisitUnaryDeref(const UnaryOperator *E) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 1197 | if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1198 | return true; |
Mike Stump | 980ca22 | 2009-10-29 20:48:09 +0000 | [diff] [blame] | 1199 | return Visit(E->getSubExpr()); |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1200 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1201 | bool VisitUnaryOperator(const UnaryOperator *E) { return Visit(E->getSubExpr()); } |
Chris Lattner | 363ff23 | 2010-04-13 17:34:23 +0000 | [diff] [blame] | 1202 | |
| 1203 | // Has side effects if any element does. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1204 | bool VisitInitListExpr(const InitListExpr *E) { |
Chris Lattner | 363ff23 | 2010-04-13 17:34:23 +0000 | [diff] [blame] | 1205 | for (unsigned i = 0, e = E->getNumInits(); i != e; ++i) |
| 1206 | if (Visit(E->getInit(i))) return true; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1207 | if (const Expr *filler = E->getArrayFiller()) |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 1208 | return Visit(filler); |
Chris Lattner | 363ff23 | 2010-04-13 17:34:23 +0000 | [diff] [blame] | 1209 | return false; |
| 1210 | } |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 1211 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1212 | bool VisitSizeOfPackExpr(const SizeOfPackExpr *) { return false; } |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1213 | }; |
| 1214 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1215 | class OpaqueValueEvaluation { |
| 1216 | EvalInfo &info; |
| 1217 | OpaqueValueExpr *opaqueValue; |
| 1218 | |
| 1219 | public: |
| 1220 | OpaqueValueEvaluation(EvalInfo &info, OpaqueValueExpr *opaqueValue, |
| 1221 | Expr *value) |
| 1222 | : info(info), opaqueValue(opaqueValue) { |
| 1223 | |
| 1224 | // If evaluation fails, fail immediately. |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 1225 | if (!Evaluate(info.OpaqueValues[opaqueValue], info, value)) { |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1226 | this->opaqueValue = 0; |
| 1227 | return; |
| 1228 | } |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | bool hasError() const { return opaqueValue == 0; } |
| 1232 | |
| 1233 | ~OpaqueValueEvaluation() { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 1234 | // FIXME: This will not work for recursive constexpr functions using opaque |
| 1235 | // values. Restore the former value. |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1236 | if (opaqueValue) info.OpaqueValues.erase(opaqueValue); |
| 1237 | } |
| 1238 | }; |
| 1239 | |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1240 | } // end anonymous namespace |
| 1241 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1242 | //===----------------------------------------------------------------------===// |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1243 | // Generic Evaluation |
| 1244 | //===----------------------------------------------------------------------===// |
| 1245 | namespace { |
| 1246 | |
| 1247 | template <class Derived, typename RetTy=void> |
| 1248 | class ExprEvaluatorBase |
| 1249 | : public ConstStmtVisitor<Derived, RetTy> { |
| 1250 | private: |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1251 | RetTy DerivedSuccess(const CCValue &V, const Expr *E) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1252 | return static_cast<Derived*>(this)->Success(V, E); |
| 1253 | } |
| 1254 | RetTy DerivedError(const Expr *E) { |
| 1255 | return static_cast<Derived*>(this)->Error(E); |
| 1256 | } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 1257 | RetTy DerivedValueInitialization(const Expr *E) { |
| 1258 | return static_cast<Derived*>(this)->ValueInitialization(E); |
| 1259 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1260 | |
| 1261 | protected: |
| 1262 | EvalInfo &Info; |
| 1263 | typedef ConstStmtVisitor<Derived, RetTy> StmtVisitorTy; |
| 1264 | typedef ExprEvaluatorBase ExprEvaluatorBaseTy; |
| 1265 | |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 1266 | RetTy ValueInitialization(const Expr *E) { return DerivedError(E); } |
| 1267 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1268 | public: |
| 1269 | ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {} |
| 1270 | |
| 1271 | RetTy VisitStmt(const Stmt *) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1272 | llvm_unreachable("Expression evaluator should not be called on stmts"); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1273 | } |
| 1274 | RetTy VisitExpr(const Expr *E) { |
| 1275 | return DerivedError(E); |
| 1276 | } |
| 1277 | |
| 1278 | RetTy VisitParenExpr(const ParenExpr *E) |
| 1279 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| 1280 | RetTy VisitUnaryExtension(const UnaryOperator *E) |
| 1281 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| 1282 | RetTy VisitUnaryPlus(const UnaryOperator *E) |
| 1283 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| 1284 | RetTy VisitChooseExpr(const ChooseExpr *E) |
| 1285 | { return StmtVisitorTy::Visit(E->getChosenSubExpr(Info.Ctx)); } |
| 1286 | RetTy VisitGenericSelectionExpr(const GenericSelectionExpr *E) |
| 1287 | { return StmtVisitorTy::Visit(E->getResultExpr()); } |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 1288 | RetTy VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E) |
| 1289 | { return StmtVisitorTy::Visit(E->getReplacement()); } |
Richard Smith | 3d75ca8 | 2011-11-09 02:12:41 +0000 | [diff] [blame] | 1290 | RetTy VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) |
| 1291 | { return StmtVisitorTy::Visit(E->getExpr()); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1292 | |
| 1293 | RetTy VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) { |
| 1294 | OpaqueValueEvaluation opaque(Info, E->getOpaqueValue(), E->getCommon()); |
| 1295 | if (opaque.hasError()) |
| 1296 | return DerivedError(E); |
| 1297 | |
| 1298 | bool cond; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1299 | if (!EvaluateAsBooleanCondition(E->getCond(), cond, Info)) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1300 | return DerivedError(E); |
| 1301 | |
| 1302 | return StmtVisitorTy::Visit(cond ? E->getTrueExpr() : E->getFalseExpr()); |
| 1303 | } |
| 1304 | |
| 1305 | RetTy VisitConditionalOperator(const ConditionalOperator *E) { |
| 1306 | bool BoolResult; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1307 | if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1308 | return DerivedError(E); |
| 1309 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1310 | Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1311 | return StmtVisitorTy::Visit(EvalExpr); |
| 1312 | } |
| 1313 | |
| 1314 | RetTy VisitOpaqueValueExpr(const OpaqueValueExpr *E) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1315 | const CCValue *Value = Info.getOpaqueValue(E); |
| 1316 | if (!Value) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1317 | return (E->getSourceExpr() ? StmtVisitorTy::Visit(E->getSourceExpr()) |
| 1318 | : DerivedError(E)); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1319 | return DerivedSuccess(*Value, E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1320 | } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 1321 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1322 | RetTy VisitCallExpr(const CallExpr *E) { |
| 1323 | const Expr *Callee = E->getCallee(); |
| 1324 | QualType CalleeType = Callee->getType(); |
| 1325 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1326 | const FunctionDecl *FD = 0; |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1327 | LValue *This = 0, ThisVal; |
| 1328 | llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs()); |
Richard Smith | 6c95787 | 2011-11-10 09:31:24 +0000 | [diff] [blame] | 1329 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1330 | // Extract function decl and 'this' pointer from the callee. |
| 1331 | if (CalleeType->isSpecificBuiltinType(BuiltinType::BoundMember)) { |
| 1332 | // Explicit bound member calls, such as x.f() or p->g(); |
| 1333 | // FIXME: Handle a BinaryOperator callee ('.*' or '->*'). |
| 1334 | const MemberExpr *ME = dyn_cast<MemberExpr>(Callee->IgnoreParens()); |
| 1335 | if (!ME) |
| 1336 | return DerivedError(Callee); |
| 1337 | if (!EvaluateObjectArgument(Info, ME->getBase(), ThisVal)) |
| 1338 | return DerivedError(ME->getBase()); |
| 1339 | This = &ThisVal; |
| 1340 | FD = dyn_cast<FunctionDecl>(ME->getMemberDecl()); |
| 1341 | if (!FD) |
| 1342 | return DerivedError(ME); |
| 1343 | } else if (CalleeType->isFunctionPointerType()) { |
| 1344 | CCValue Call; |
| 1345 | if (!Evaluate(Call, Info, Callee) || !Call.isLValue() || |
| 1346 | !Call.getLValueBase() || !Call.getLValueOffset().isZero()) |
| 1347 | return DerivedError(Callee); |
| 1348 | |
| 1349 | const Expr *Base = Call.getLValueBase(); |
| 1350 | |
| 1351 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) |
| 1352 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 1353 | else if (const MemberExpr *ME = dyn_cast<MemberExpr>(Base)) |
| 1354 | FD = dyn_cast<FunctionDecl>(ME->getMemberDecl()); |
| 1355 | if (!FD) |
| 1356 | return DerivedError(Callee); |
| 1357 | |
| 1358 | // Overloaded operator calls to member functions are represented as normal |
| 1359 | // calls with '*this' as the first argument. |
| 1360 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
| 1361 | if (MD && !MD->isStatic()) { |
| 1362 | if (!EvaluateObjectArgument(Info, Args[0], ThisVal)) |
| 1363 | return false; |
| 1364 | This = &ThisVal; |
| 1365 | Args = Args.slice(1); |
| 1366 | } |
| 1367 | |
| 1368 | // Don't call function pointers which have been cast to some other type. |
| 1369 | if (!Info.Ctx.hasSameType(CalleeType->getPointeeType(), FD->getType())) |
| 1370 | return DerivedError(E); |
| 1371 | } else |
Devang Patel | 6142ca7 | 2011-11-10 17:47:39 +0000 | [diff] [blame] | 1372 | return DerivedError(E); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1373 | |
| 1374 | const FunctionDecl *Definition; |
| 1375 | Stmt *Body = FD->getBody(Definition); |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 1376 | CCValue CCResult; |
| 1377 | APValue Result; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1378 | |
| 1379 | if (Body && Definition->isConstexpr() && !Definition->isInvalidDecl() && |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1380 | HandleFunctionCall(This, Args, Body, Info, CCResult) && |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 1381 | CheckConstantExpression(CCResult, Result)) |
| 1382 | return DerivedSuccess(CCValue(Result, CCValue::GlobalValue()), E); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1383 | |
| 1384 | return DerivedError(E); |
| 1385 | } |
| 1386 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1387 | RetTy VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
| 1388 | return StmtVisitorTy::Visit(E->getInitializer()); |
| 1389 | } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 1390 | RetTy VisitInitListExpr(const InitListExpr *E) { |
| 1391 | if (Info.getLangOpts().CPlusPlus0x) { |
| 1392 | if (E->getNumInits() == 0) |
| 1393 | return DerivedValueInitialization(E); |
| 1394 | if (E->getNumInits() == 1) |
| 1395 | return StmtVisitorTy::Visit(E->getInit(0)); |
| 1396 | } |
| 1397 | return DerivedError(E); |
| 1398 | } |
| 1399 | RetTy VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { |
| 1400 | return DerivedValueInitialization(E); |
| 1401 | } |
| 1402 | RetTy VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) { |
| 1403 | return DerivedValueInitialization(E); |
| 1404 | } |
| 1405 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1406 | /// A member expression where the object is a prvalue is itself a prvalue. |
| 1407 | RetTy VisitMemberExpr(const MemberExpr *E) { |
| 1408 | assert(!E->isArrow() && "missing call to bound member function?"); |
| 1409 | |
| 1410 | CCValue Val; |
| 1411 | if (!Evaluate(Val, Info, E->getBase())) |
| 1412 | return false; |
| 1413 | |
| 1414 | QualType BaseTy = E->getBase()->getType(); |
| 1415 | |
| 1416 | const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
| 1417 | if (!FD) return false; |
| 1418 | assert(!FD->getType()->isReferenceType() && "prvalue reference?"); |
| 1419 | assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() == |
| 1420 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
| 1421 | |
| 1422 | SubobjectDesignator Designator; |
| 1423 | Designator.addDecl(FD); |
| 1424 | |
| 1425 | return ExtractSubobject(Info, Val, BaseTy, Designator, E->getType()) && |
| 1426 | DerivedSuccess(Val, E); |
| 1427 | } |
| 1428 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1429 | RetTy VisitCastExpr(const CastExpr *E) { |
| 1430 | switch (E->getCastKind()) { |
| 1431 | default: |
| 1432 | break; |
| 1433 | |
| 1434 | case CK_NoOp: |
| 1435 | return StmtVisitorTy::Visit(E->getSubExpr()); |
| 1436 | |
| 1437 | case CK_LValueToRValue: { |
| 1438 | LValue LVal; |
| 1439 | if (EvaluateLValue(E->getSubExpr(), LVal, Info)) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1440 | CCValue RVal; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1441 | if (HandleLValueToRValueConversion(Info, E->getType(), LVal, RVal)) |
| 1442 | return DerivedSuccess(RVal, E); |
| 1443 | } |
| 1444 | break; |
| 1445 | } |
| 1446 | } |
| 1447 | |
| 1448 | return DerivedError(E); |
| 1449 | } |
| 1450 | |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 1451 | /// Visit a value which is evaluated, but whose value is ignored. |
| 1452 | void VisitIgnoredValue(const Expr *E) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1453 | CCValue Scratch; |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 1454 | if (!Evaluate(Scratch, Info, E)) |
| 1455 | Info.EvalStatus.HasSideEffects = true; |
| 1456 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1457 | }; |
| 1458 | |
| 1459 | } |
| 1460 | |
| 1461 | //===----------------------------------------------------------------------===// |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1462 | // LValue Evaluation |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1463 | // |
| 1464 | // This is used for evaluating lvalues (in C and C++), xvalues (in C++11), |
| 1465 | // function designators (in C), decl references to void objects (in C), and |
| 1466 | // temporaries (if building with -Wno-address-of-temporary). |
| 1467 | // |
| 1468 | // LValue evaluation produces values comprising a base expression of one of the |
| 1469 | // following types: |
| 1470 | // * DeclRefExpr |
| 1471 | // * MemberExpr for a static member |
| 1472 | // * CompoundLiteralExpr in C |
| 1473 | // * StringLiteral |
| 1474 | // * PredefinedExpr |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1475 | // * ObjCStringLiteralExpr |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1476 | // * ObjCEncodeExpr |
| 1477 | // * AddrLabelExpr |
| 1478 | // * BlockExpr |
| 1479 | // * CallExpr for a MakeStringConstant builtin |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1480 | // plus an offset in bytes. It can also produce lvalues referring to locals. In |
| 1481 | // that case, the Frame will point to a stack frame, and the Expr is used as a |
| 1482 | // key to find the relevant temporary's value. |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1483 | //===----------------------------------------------------------------------===// |
| 1484 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 1485 | class LValueExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1486 | : public ExprEvaluatorBase<LValueExprEvaluator, bool> { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1487 | LValue &Result; |
Chandler Carruth | 0124839 | 2011-08-22 17:24:56 +0000 | [diff] [blame] | 1488 | const Decl *PrevDecl; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1489 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1490 | bool Success(const Expr *E) { |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1491 | Result.setExpr(E); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1492 | return true; |
| 1493 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1494 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1495 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1496 | LValueExprEvaluator(EvalInfo &info, LValue &Result) : |
Chandler Carruth | 0124839 | 2011-08-22 17:24:56 +0000 | [diff] [blame] | 1497 | ExprEvaluatorBaseTy(info), Result(Result), PrevDecl(0) {} |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1498 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1499 | bool Success(const CCValue &V, const Expr *E) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1500 | Result.setFrom(V); |
| 1501 | return true; |
| 1502 | } |
| 1503 | bool Error(const Expr *E) { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1504 | return false; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1505 | } |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1506 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1507 | bool VisitVarDecl(const Expr *E, const VarDecl *VD); |
| 1508 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1509 | bool VisitDeclRefExpr(const DeclRefExpr *E); |
| 1510 | bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(E); } |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 1511 | bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1512 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
| 1513 | bool VisitMemberExpr(const MemberExpr *E); |
| 1514 | bool VisitStringLiteral(const StringLiteral *E) { return Success(E); } |
| 1515 | bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(E); } |
| 1516 | bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E); |
| 1517 | bool VisitUnaryDeref(const UnaryOperator *E); |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 1518 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1519 | bool VisitCastExpr(const CastExpr *E) { |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 1520 | switch (E->getCastKind()) { |
| 1521 | default: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1522 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 1523 | |
Eli Friedman | db92422 | 2011-10-11 00:13:24 +0000 | [diff] [blame] | 1524 | case CK_LValueBitCast: |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1525 | if (!Visit(E->getSubExpr())) |
| 1526 | return false; |
| 1527 | Result.Designator.setInvalid(); |
| 1528 | return true; |
Eli Friedman | db92422 | 2011-10-11 00:13:24 +0000 | [diff] [blame] | 1529 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1530 | case CK_DerivedToBase: |
| 1531 | case CK_UncheckedDerivedToBase: { |
| 1532 | if (!Visit(E->getSubExpr())) |
| 1533 | return false; |
| 1534 | |
| 1535 | // Now figure out the necessary offset to add to the base LV to get from |
| 1536 | // the derived class to the base class. |
| 1537 | QualType Type = E->getSubExpr()->getType(); |
| 1538 | |
| 1539 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 1540 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 1541 | if (!HandleLValueBase(Info, Result, Type->getAsCXXRecordDecl(), *PathI)) |
| 1542 | return false; |
| 1543 | Type = (*PathI)->getType(); |
| 1544 | } |
| 1545 | |
| 1546 | return true; |
| 1547 | } |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 1548 | } |
| 1549 | } |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 1550 | |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 1551 | // FIXME: Missing: __real__, __imag__ |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1552 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1553 | }; |
| 1554 | } // end anonymous namespace |
| 1555 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1556 | /// Evaluate an expression as an lvalue. This can be legitimately called on |
| 1557 | /// expressions which are not glvalues, in a few cases: |
| 1558 | /// * function designators in C, |
| 1559 | /// * "extern void" objects, |
| 1560 | /// * temporaries, if building with -Wno-address-of-temporary. |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1561 | static bool EvaluateLValue(const Expr* E, LValue& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1562 | assert((E->isGLValue() || E->getType()->isFunctionType() || |
| 1563 | E->getType()->isVoidType() || isa<CXXTemporaryObjectExpr>(E)) && |
| 1564 | "can't evaluate expression as an lvalue"); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1565 | return LValueExprEvaluator(Info, Result).Visit(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1568 | bool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1569 | if (isa<FunctionDecl>(E->getDecl())) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1570 | return Success(E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1571 | if (const VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) |
| 1572 | return VisitVarDecl(E, VD); |
| 1573 | return Error(E); |
| 1574 | } |
Richard Smith | 436c889 | 2011-10-24 23:14:33 +0000 | [diff] [blame] | 1575 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1576 | bool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) { |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1577 | if (!VD->getType()->isReferenceType()) { |
| 1578 | if (isa<ParmVarDecl>(VD)) { |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1579 | Result.setExpr(E, Info.CurrentCall); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1580 | return true; |
| 1581 | } |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1582 | return Success(E); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1583 | } |
Eli Friedman | 50c39ea | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 1584 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1585 | CCValue V; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1586 | if (EvaluateVarDeclInit(Info, E, VD, Info.CurrentCall, V)) |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1587 | return Success(V, E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1588 | |
| 1589 | return Error(E); |
Anders Carlsson | 35873c4 | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 1592 | bool LValueExprEvaluator::VisitMaterializeTemporaryExpr( |
| 1593 | const MaterializeTemporaryExpr *E) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1594 | Result.setExpr(E, Info.CurrentCall); |
| 1595 | return EvaluateConstantExpression(Info.CurrentCall->Temporaries[E], Info, |
| 1596 | Result, E->GetTemporaryExpr()); |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 1597 | } |
| 1598 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1599 | bool |
| 1600 | LValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1601 | assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?"); |
| 1602 | // Defer visiting the literal until the lvalue-to-rvalue conversion. We can |
| 1603 | // only see this when folding in C, so there's no standard to follow here. |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1604 | return Success(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1605 | } |
| 1606 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1607 | bool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1608 | // Handle static data members. |
| 1609 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getMemberDecl())) { |
| 1610 | VisitIgnoredValue(E->getBase()); |
| 1611 | return VisitVarDecl(E, VD); |
| 1612 | } |
| 1613 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1614 | // Handle static member functions. |
| 1615 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) { |
| 1616 | if (MD->isStatic()) { |
| 1617 | VisitIgnoredValue(E->getBase()); |
| 1618 | return Success(E); |
| 1619 | } |
| 1620 | } |
| 1621 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1622 | // Handle non-static data members. |
| 1623 | QualType BaseTy; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1624 | if (E->isArrow()) { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1625 | if (!EvaluatePointer(E->getBase(), Result, Info)) |
| 1626 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1627 | BaseTy = E->getBase()->getType()->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1628 | } else { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1629 | if (!Visit(E->getBase())) |
| 1630 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1631 | BaseTy = E->getBase()->getType(); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1634 | const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1635 | if (!FD) return false; |
| 1636 | assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() == |
| 1637 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
| 1638 | (void)BaseTy; |
Eli Friedman | 2be5861 | 2009-05-30 21:09:44 +0000 | [diff] [blame] | 1639 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1640 | HandleLValueMember(Info, Result, FD); |
Eli Friedman | 2be5861 | 2009-05-30 21:09:44 +0000 | [diff] [blame] | 1641 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1642 | if (FD->getType()->isReferenceType()) { |
| 1643 | CCValue RefValue; |
| 1644 | if (!HandleLValueToRValueConversion(Info, FD->getType(), Result, RefValue)) |
| 1645 | return false; |
| 1646 | return Success(RefValue, E); |
| 1647 | } |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1648 | return true; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1651 | bool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1652 | // FIXME: Deal with vectors as array subscript bases. |
| 1653 | if (E->getBase()->getType()->isVectorType()) |
| 1654 | return false; |
| 1655 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1656 | if (!EvaluatePointer(E->getBase(), Result, Info)) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1657 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1658 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1659 | APSInt Index; |
| 1660 | if (!EvaluateInteger(E->getIdx(), Index, Info)) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1661 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1662 | int64_t IndexValue |
| 1663 | = Index.isSigned() ? Index.getSExtValue() |
| 1664 | : static_cast<int64_t>(Index.getZExtValue()); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1665 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1666 | return HandleLValueArrayAdjustment(Info, Result, E->getType(), IndexValue); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1667 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1668 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1669 | bool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1670 | return EvaluatePointer(E->getSubExpr(), Result, Info); |
Eli Friedman | e8761c8 | 2009-02-20 01:57:15 +0000 | [diff] [blame] | 1671 | } |
| 1672 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1673 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1674 | // Pointer Evaluation |
| 1675 | //===----------------------------------------------------------------------===// |
| 1676 | |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 1677 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 1678 | class PointerExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1679 | : public ExprEvaluatorBase<PointerExprEvaluator, bool> { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1680 | LValue &Result; |
| 1681 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1682 | bool Success(const Expr *E) { |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1683 | Result.setExpr(E); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1684 | return true; |
| 1685 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 1686 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1687 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1688 | PointerExprEvaluator(EvalInfo &info, LValue &Result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1689 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1690 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1691 | bool Success(const CCValue &V, const Expr *E) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1692 | Result.setFrom(V); |
| 1693 | return true; |
| 1694 | } |
| 1695 | bool Error(const Stmt *S) { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1696 | return false; |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 1697 | } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 1698 | bool ValueInitialization(const Expr *E) { |
| 1699 | return Success((Expr*)0); |
| 1700 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 1701 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1702 | bool VisitBinaryOperator(const BinaryOperator *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1703 | bool VisitCastExpr(const CastExpr* E); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1704 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1705 | bool VisitObjCStringLiteral(const ObjCStringLiteral *E) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1706 | { return Success(E); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1707 | bool VisitAddrLabelExpr(const AddrLabelExpr *E) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1708 | { return Success(E); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1709 | bool VisitCallExpr(const CallExpr *E); |
| 1710 | bool VisitBlockExpr(const BlockExpr *E) { |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1711 | if (!E->getBlockDecl()->hasCaptures()) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1712 | return Success(E); |
| 1713 | return false; |
Mike Stump | b83d287 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 1714 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1715 | bool VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 1716 | { return ValueInitialization(E); } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1717 | bool VisitCXXThisExpr(const CXXThisExpr *E) { |
| 1718 | if (!Info.CurrentCall->This) |
| 1719 | return false; |
| 1720 | Result = *Info.CurrentCall->This; |
| 1721 | return true; |
| 1722 | } |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1723 | |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 1724 | // FIXME: Missing: @protocol, @selector |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 1725 | }; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1726 | } // end anonymous namespace |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 1727 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1728 | static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1729 | assert(E->isRValue() && E->getType()->hasPointerRepresentation()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1730 | return PointerExprEvaluator(Info, Result).Visit(E); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1733 | bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1734 | if (E->getOpcode() != BO_Add && |
| 1735 | E->getOpcode() != BO_Sub) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1736 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1737 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1738 | const Expr *PExp = E->getLHS(); |
| 1739 | const Expr *IExp = E->getRHS(); |
| 1740 | if (IExp->getType()->isPointerType()) |
| 1741 | std::swap(PExp, IExp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1742 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1743 | if (!EvaluatePointer(PExp, Result, Info)) |
| 1744 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1745 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1746 | llvm::APSInt Offset; |
| 1747 | if (!EvaluateInteger(IExp, Offset, Info)) |
| 1748 | return false; |
| 1749 | int64_t AdditionalOffset |
| 1750 | = Offset.isSigned() ? Offset.getSExtValue() |
| 1751 | : static_cast<int64_t>(Offset.getZExtValue()); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1752 | if (E->getOpcode() == BO_Sub) |
| 1753 | AdditionalOffset = -AdditionalOffset; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1754 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1755 | QualType Pointee = PExp->getType()->getAs<PointerType>()->getPointeeType(); |
| 1756 | return HandleLValueArrayAdjustment(Info, Result, Pointee, AdditionalOffset); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1757 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1758 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1759 | bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| 1760 | return EvaluateLValue(E->getSubExpr(), Result, Info); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1761 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1762 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1763 | bool PointerExprEvaluator::VisitCastExpr(const CastExpr* E) { |
| 1764 | const Expr* SubExpr = E->getSubExpr(); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1765 | |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 1766 | switch (E->getCastKind()) { |
| 1767 | default: |
| 1768 | break; |
| 1769 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1770 | case CK_BitCast: |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 1771 | case CK_CPointerToObjCPointerCast: |
| 1772 | case CK_BlockPointerToObjCPointerCast: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1773 | case CK_AnyPointerToBlockPointerCast: |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1774 | if (!Visit(SubExpr)) |
| 1775 | return false; |
| 1776 | Result.Designator.setInvalid(); |
| 1777 | return true; |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 1778 | |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 1779 | case CK_DerivedToBase: |
| 1780 | case CK_UncheckedDerivedToBase: { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1781 | if (!EvaluatePointer(E->getSubExpr(), Result, Info)) |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 1782 | return false; |
| 1783 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1784 | // Now figure out the necessary offset to add to the base LV to get from |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 1785 | // the derived class to the base class. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1786 | QualType Type = |
| 1787 | E->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType(); |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 1788 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1789 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 1790 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1791 | if (!HandleLValueBase(Info, Result, Type->getAsCXXRecordDecl(), *PathI)) |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 1792 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1793 | Type = (*PathI)->getType(); |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 1796 | return true; |
| 1797 | } |
| 1798 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1799 | case CK_NullToPointer: |
| 1800 | return ValueInitialization(E); |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 1801 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1802 | case CK_IntegralToPointer: { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1803 | CCValue Value; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1804 | if (!EvaluateIntegerOrLValue(SubExpr, Value, Info)) |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 1805 | break; |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 1806 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1807 | if (Value.isInt()) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1808 | unsigned Size = Info.Ctx.getTypeSize(E->getType()); |
| 1809 | uint64_t N = Value.getInt().extOrTrunc(Size).getZExtValue(); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1810 | Result.Base = 0; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1811 | Result.Offset = CharUnits::fromQuantity(N); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1812 | Result.Frame = 0; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1813 | Result.Designator.setInvalid(); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1814 | return true; |
| 1815 | } else { |
| 1816 | // Cast is of an lvalue, no need to change value. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1817 | Result.setFrom(Value); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1818 | return true; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1819 | } |
| 1820 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1821 | case CK_ArrayToPointerDecay: |
Richard Smith | 6a7c94a | 2011-10-31 20:57:44 +0000 | [diff] [blame] | 1822 | // FIXME: Support array-to-pointer decay on array rvalues. |
| 1823 | if (!SubExpr->isGLValue()) |
| 1824 | return Error(E); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1825 | if (!EvaluateLValue(SubExpr, Result, Info)) |
| 1826 | return false; |
| 1827 | // The result is a pointer to the first element of the array. |
| 1828 | Result.Designator.addIndex(0); |
| 1829 | return true; |
Richard Smith | 6a7c94a | 2011-10-31 20:57:44 +0000 | [diff] [blame] | 1830 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1831 | case CK_FunctionToPointerDecay: |
Richard Smith | 6a7c94a | 2011-10-31 20:57:44 +0000 | [diff] [blame] | 1832 | return EvaluateLValue(SubExpr, Result, Info); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1833 | } |
| 1834 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1835 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1836 | } |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1837 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1838 | bool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1839 | if (IsStringLiteralCall(E)) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1840 | return Success(E); |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 1841 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1842 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1843 | } |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1844 | |
| 1845 | //===----------------------------------------------------------------------===// |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1846 | // Record Evaluation |
| 1847 | //===----------------------------------------------------------------------===// |
| 1848 | |
| 1849 | namespace { |
| 1850 | class RecordExprEvaluator |
| 1851 | : public ExprEvaluatorBase<RecordExprEvaluator, bool> { |
| 1852 | const LValue &This; |
| 1853 | APValue &Result; |
| 1854 | public: |
| 1855 | |
| 1856 | RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result) |
| 1857 | : ExprEvaluatorBaseTy(info), This(This), Result(Result) {} |
| 1858 | |
| 1859 | bool Success(const CCValue &V, const Expr *E) { |
| 1860 | return CheckConstantExpression(V, Result); |
| 1861 | } |
| 1862 | bool Error(const Expr *E) { return false; } |
| 1863 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1864 | bool VisitCastExpr(const CastExpr *E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1865 | bool VisitInitListExpr(const InitListExpr *E); |
| 1866 | bool VisitCXXConstructExpr(const CXXConstructExpr *E); |
| 1867 | }; |
| 1868 | } |
| 1869 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1870 | bool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 1871 | switch (E->getCastKind()) { |
| 1872 | default: |
| 1873 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 1874 | |
| 1875 | case CK_ConstructorConversion: |
| 1876 | return Visit(E->getSubExpr()); |
| 1877 | |
| 1878 | case CK_DerivedToBase: |
| 1879 | case CK_UncheckedDerivedToBase: { |
| 1880 | CCValue DerivedObject; |
| 1881 | if (!Evaluate(DerivedObject, Info, E->getSubExpr()) || |
| 1882 | !DerivedObject.isStruct()) |
| 1883 | return false; |
| 1884 | |
| 1885 | // Derived-to-base rvalue conversion: just slice off the derived part. |
| 1886 | APValue *Value = &DerivedObject; |
| 1887 | const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl(); |
| 1888 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 1889 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 1890 | assert(!(*PathI)->isVirtual() && "record rvalue with virtual base"); |
| 1891 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 1892 | Value = &Value->getStructBase(getBaseIndex(RD, Base)); |
| 1893 | RD = Base; |
| 1894 | } |
| 1895 | Result = *Value; |
| 1896 | return true; |
| 1897 | } |
| 1898 | } |
| 1899 | } |
| 1900 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1901 | bool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 1902 | const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl(); |
| 1903 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 1904 | |
| 1905 | if (RD->isUnion()) { |
| 1906 | Result = APValue(E->getInitializedFieldInUnion()); |
| 1907 | if (!E->getNumInits()) |
| 1908 | return true; |
| 1909 | LValue Subobject = This; |
| 1910 | HandleLValueMember(Info, Subobject, E->getInitializedFieldInUnion(), |
| 1911 | &Layout); |
| 1912 | return EvaluateConstantExpression(Result.getUnionValue(), Info, |
| 1913 | Subobject, E->getInit(0)); |
| 1914 | } |
| 1915 | |
| 1916 | assert((!isa<CXXRecordDecl>(RD) || !cast<CXXRecordDecl>(RD)->getNumBases()) && |
| 1917 | "initializer list for class with base classes"); |
| 1918 | Result = APValue(APValue::UninitStruct(), 0, |
| 1919 | std::distance(RD->field_begin(), RD->field_end())); |
| 1920 | unsigned ElementNo = 0; |
| 1921 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 1922 | FieldEnd = RD->field_end(); Field != FieldEnd; ++Field) { |
| 1923 | // Anonymous bit-fields are not considered members of the class for |
| 1924 | // purposes of aggregate initialization. |
| 1925 | if (Field->isUnnamedBitfield()) |
| 1926 | continue; |
| 1927 | |
| 1928 | LValue Subobject = This; |
| 1929 | HandleLValueMember(Info, Subobject, *Field, &Layout); |
| 1930 | |
| 1931 | if (ElementNo < E->getNumInits()) { |
| 1932 | if (!EvaluateConstantExpression( |
| 1933 | Result.getStructField((*Field)->getFieldIndex()), |
| 1934 | Info, Subobject, E->getInit(ElementNo++))) |
| 1935 | return false; |
| 1936 | } else { |
| 1937 | // Perform an implicit value-initialization for members beyond the end of |
| 1938 | // the initializer list. |
| 1939 | ImplicitValueInitExpr VIE(Field->getType()); |
| 1940 | if (!EvaluateConstantExpression( |
| 1941 | Result.getStructField((*Field)->getFieldIndex()), |
| 1942 | Info, Subobject, &VIE)) |
| 1943 | return false; |
| 1944 | } |
| 1945 | } |
| 1946 | |
| 1947 | return true; |
| 1948 | } |
| 1949 | |
| 1950 | bool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 1951 | const CXXConstructorDecl *FD = E->getConstructor(); |
| 1952 | const FunctionDecl *Definition = 0; |
| 1953 | FD->getBody(Definition); |
| 1954 | |
| 1955 | if (!Definition || !Definition->isConstexpr() || Definition->isInvalidDecl()) |
| 1956 | return false; |
| 1957 | |
| 1958 | // FIXME: Elide the copy/move construction wherever we can. |
| 1959 | if (E->isElidable()) |
| 1960 | if (const MaterializeTemporaryExpr *ME |
| 1961 | = dyn_cast<MaterializeTemporaryExpr>(E->getArg(0))) |
| 1962 | return Visit(ME->GetTemporaryExpr()); |
| 1963 | |
| 1964 | llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs()); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1965 | return HandleConstructorCall(This, Args, cast<CXXConstructorDecl>(Definition), |
| 1966 | Info, Result); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1967 | } |
| 1968 | |
| 1969 | static bool EvaluateRecord(const Expr *E, const LValue &This, |
| 1970 | APValue &Result, EvalInfo &Info) { |
| 1971 | assert(E->isRValue() && E->getType()->isRecordType() && |
| 1972 | E->getType()->isLiteralType() && |
| 1973 | "can't evaluate expression as a record rvalue"); |
| 1974 | return RecordExprEvaluator(Info, This, Result).Visit(E); |
| 1975 | } |
| 1976 | |
| 1977 | //===----------------------------------------------------------------------===// |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 1978 | // Vector Evaluation |
| 1979 | //===----------------------------------------------------------------------===// |
| 1980 | |
| 1981 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 1982 | class VectorExprEvaluator |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 1983 | : public ExprEvaluatorBase<VectorExprEvaluator, bool> { |
| 1984 | APValue &Result; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 1985 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1986 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 1987 | VectorExprEvaluator(EvalInfo &info, APValue &Result) |
| 1988 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1989 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 1990 | bool Success(const ArrayRef<APValue> &V, const Expr *E) { |
| 1991 | assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements()); |
| 1992 | // FIXME: remove this APValue copy. |
| 1993 | Result = APValue(V.data(), V.size()); |
| 1994 | return true; |
| 1995 | } |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 1996 | bool Success(const CCValue &V, const Expr *E) { |
| 1997 | assert(V.isVector()); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 1998 | Result = V; |
| 1999 | return true; |
| 2000 | } |
| 2001 | bool Error(const Expr *E) { return false; } |
| 2002 | bool ValueInitialization(const Expr *E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2003 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2004 | bool VisitUnaryReal(const UnaryOperator *E) |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 2005 | { return Visit(E->getSubExpr()); } |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2006 | bool VisitCastExpr(const CastExpr* E); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2007 | bool VisitInitListExpr(const InitListExpr *E); |
| 2008 | bool VisitUnaryImag(const UnaryOperator *E); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 2009 | // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div, |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 2010 | // binary comparisons, binary and/or/xor, |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 2011 | // shufflevector, ExtVectorElementExpr |
| 2012 | // (Note that these require implementing conversions |
| 2013 | // between vector types.) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2014 | }; |
| 2015 | } // end anonymous namespace |
| 2016 | |
| 2017 | static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2018 | assert(E->isRValue() && E->getType()->isVectorType() &&"not a vector rvalue"); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2019 | return VectorExprEvaluator(Info, Result).Visit(E); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2020 | } |
| 2021 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2022 | bool VectorExprEvaluator::VisitCastExpr(const CastExpr* E) { |
| 2023 | const VectorType *VTy = E->getType()->castAs<VectorType>(); |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 2024 | QualType EltTy = VTy->getElementType(); |
| 2025 | unsigned NElts = VTy->getNumElements(); |
| 2026 | unsigned EltWidth = Info.Ctx.getTypeSize(EltTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2027 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2028 | const Expr* SE = E->getSubExpr(); |
Nate Begeman | e8c9e92 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 2029 | QualType SETy = SE->getType(); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2030 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2031 | switch (E->getCastKind()) { |
| 2032 | case CK_VectorSplat: { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2033 | APValue Val = APValue(); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2034 | if (SETy->isIntegerType()) { |
| 2035 | APSInt IntResult; |
| 2036 | if (!EvaluateInteger(SE, IntResult, Info)) |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2037 | return Error(E); |
| 2038 | Val = APValue(IntResult); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2039 | } else if (SETy->isRealFloatingType()) { |
| 2040 | APFloat F(0.0); |
| 2041 | if (!EvaluateFloat(SE, F, Info)) |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2042 | return Error(E); |
| 2043 | Val = APValue(F); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2044 | } else { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2045 | return Error(E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2046 | } |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 2047 | |
| 2048 | // Splat and create vector APValue. |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2049 | SmallVector<APValue, 4> Elts(NElts, Val); |
| 2050 | return Success(Elts, E); |
Nate Begeman | e8c9e92 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 2051 | } |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2052 | case CK_BitCast: { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2053 | // FIXME: this is wrong for any cast other than a no-op cast. |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2054 | if (SETy->isVectorType()) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2055 | return Visit(SE); |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 2056 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2057 | if (!SETy->isIntegerType()) |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2058 | return Error(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2059 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2060 | APSInt Init; |
| 2061 | if (!EvaluateInteger(SE, Init, Info)) |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2062 | return Error(E); |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 2063 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2064 | assert((EltTy->isIntegerType() || EltTy->isRealFloatingType()) && |
| 2065 | "Vectors must be composed of ints or floats"); |
| 2066 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2067 | SmallVector<APValue, 4> Elts; |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2068 | for (unsigned i = 0; i != NElts; ++i) { |
| 2069 | APSInt Tmp = Init.extOrTrunc(EltWidth); |
| 2070 | |
| 2071 | if (EltTy->isIntegerType()) |
| 2072 | Elts.push_back(APValue(Tmp)); |
| 2073 | else |
| 2074 | Elts.push_back(APValue(APFloat(Tmp))); |
| 2075 | |
| 2076 | Init >>= EltWidth; |
| 2077 | } |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2078 | return Success(Elts, E); |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 2079 | } |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2080 | default: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2081 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2082 | } |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2083 | } |
| 2084 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2085 | bool |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2086 | VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2087 | const VectorType *VT = E->getType()->castAs<VectorType>(); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2088 | unsigned NumInits = E->getNumInits(); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 2089 | unsigned NumElements = VT->getNumElements(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2090 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2091 | QualType EltTy = VT->getElementType(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2092 | SmallVector<APValue, 4> Elements; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2093 | |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 2094 | // If a vector is initialized with a single element, that value |
| 2095 | // becomes every element of the vector, not just the first. |
| 2096 | // This is the behavior described in the IBM AltiVec documentation. |
| 2097 | if (NumInits == 1) { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2098 | |
| 2099 | // Handle the case where the vector is initialized by another |
Tanya Lattner | b92ae0e | 2011-04-15 22:42:59 +0000 | [diff] [blame] | 2100 | // vector (OpenCL 6.1.6). |
| 2101 | if (E->getInit(0)->getType()->isVectorType()) |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2102 | return Visit(E->getInit(0)); |
| 2103 | |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 2104 | APValue InitValue; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2105 | if (EltTy->isIntegerType()) { |
| 2106 | llvm::APSInt sInt(32); |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 2107 | if (!EvaluateInteger(E->getInit(0), sInt, Info)) |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2108 | return Error(E); |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 2109 | InitValue = APValue(sInt); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2110 | } else { |
| 2111 | llvm::APFloat f(0.0); |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 2112 | if (!EvaluateFloat(E->getInit(0), f, Info)) |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2113 | return Error(E); |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 2114 | InitValue = APValue(f); |
| 2115 | } |
| 2116 | for (unsigned i = 0; i < NumElements; i++) { |
| 2117 | Elements.push_back(InitValue); |
| 2118 | } |
| 2119 | } else { |
| 2120 | for (unsigned i = 0; i < NumElements; i++) { |
| 2121 | if (EltTy->isIntegerType()) { |
| 2122 | llvm::APSInt sInt(32); |
| 2123 | if (i < NumInits) { |
| 2124 | if (!EvaluateInteger(E->getInit(i), sInt, Info)) |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2125 | return Error(E); |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 2126 | } else { |
| 2127 | sInt = Info.Ctx.MakeIntValue(0, EltTy); |
| 2128 | } |
| 2129 | Elements.push_back(APValue(sInt)); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 2130 | } else { |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 2131 | llvm::APFloat f(0.0); |
| 2132 | if (i < NumInits) { |
| 2133 | if (!EvaluateFloat(E->getInit(i), f, Info)) |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2134 | return Error(E); |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 2135 | } else { |
| 2136 | f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)); |
| 2137 | } |
| 2138 | Elements.push_back(APValue(f)); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 2139 | } |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2140 | } |
| 2141 | } |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2142 | return Success(Elements, E); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2143 | } |
| 2144 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2145 | bool |
| 2146 | VectorExprEvaluator::ValueInitialization(const Expr *E) { |
| 2147 | const VectorType *VT = E->getType()->getAs<VectorType>(); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 2148 | QualType EltTy = VT->getElementType(); |
| 2149 | APValue ZeroElement; |
| 2150 | if (EltTy->isIntegerType()) |
| 2151 | ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy)); |
| 2152 | else |
| 2153 | ZeroElement = |
| 2154 | APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy))); |
| 2155 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2156 | SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2157 | return Success(Elements, E); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 2158 | } |
| 2159 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2160 | bool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 2161 | VisitIgnoredValue(E->getSubExpr()); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2162 | return ValueInitialization(E); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 2163 | } |
| 2164 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2165 | //===----------------------------------------------------------------------===// |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2166 | // Array Evaluation |
| 2167 | //===----------------------------------------------------------------------===// |
| 2168 | |
| 2169 | namespace { |
| 2170 | class ArrayExprEvaluator |
| 2171 | : public ExprEvaluatorBase<ArrayExprEvaluator, bool> { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2172 | const LValue &This; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2173 | APValue &Result; |
| 2174 | public: |
| 2175 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2176 | ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result) |
| 2177 | : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {} |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2178 | |
| 2179 | bool Success(const APValue &V, const Expr *E) { |
| 2180 | assert(V.isArray() && "Expected array type"); |
| 2181 | Result = V; |
| 2182 | return true; |
| 2183 | } |
| 2184 | bool Error(const Expr *E) { return false; } |
| 2185 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2186 | bool ValueInitialization(const Expr *E) { |
| 2187 | const ConstantArrayType *CAT = |
| 2188 | Info.Ctx.getAsConstantArrayType(E->getType()); |
| 2189 | if (!CAT) |
| 2190 | return false; |
| 2191 | |
| 2192 | Result = APValue(APValue::UninitArray(), 0, |
| 2193 | CAT->getSize().getZExtValue()); |
| 2194 | if (!Result.hasArrayFiller()) return true; |
| 2195 | |
| 2196 | // Value-initialize all elements. |
| 2197 | LValue Subobject = This; |
| 2198 | Subobject.Designator.addIndex(0); |
| 2199 | ImplicitValueInitExpr VIE(CAT->getElementType()); |
| 2200 | return EvaluateConstantExpression(Result.getArrayFiller(), Info, |
| 2201 | Subobject, &VIE); |
| 2202 | } |
| 2203 | |
| 2204 | // FIXME: We also get CXXConstructExpr, in cases like: |
| 2205 | // struct S { constexpr S(); }; constexpr S s[10]; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2206 | bool VisitInitListExpr(const InitListExpr *E); |
| 2207 | }; |
| 2208 | } // end anonymous namespace |
| 2209 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2210 | static bool EvaluateArray(const Expr *E, const LValue &This, |
| 2211 | APValue &Result, EvalInfo &Info) { |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2212 | assert(E->isRValue() && E->getType()->isArrayType() && |
| 2213 | E->getType()->isLiteralType() && "not a literal array rvalue"); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2214 | return ArrayExprEvaluator(Info, This, Result).Visit(E); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2215 | } |
| 2216 | |
| 2217 | bool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 2218 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType()); |
| 2219 | if (!CAT) |
| 2220 | return false; |
| 2221 | |
| 2222 | Result = APValue(APValue::UninitArray(), E->getNumInits(), |
| 2223 | CAT->getSize().getZExtValue()); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2224 | LValue Subobject = This; |
| 2225 | Subobject.Designator.addIndex(0); |
| 2226 | unsigned Index = 0; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2227 | for (InitListExpr::const_iterator I = E->begin(), End = E->end(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2228 | I != End; ++I, ++Index) { |
| 2229 | if (!EvaluateConstantExpression(Result.getArrayInitializedElt(Index), |
| 2230 | Info, Subobject, cast<Expr>(*I))) |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2231 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2232 | if (!HandleLValueArrayAdjustment(Info, Subobject, CAT->getElementType(), 1)) |
| 2233 | return false; |
| 2234 | } |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2235 | |
| 2236 | if (!Result.hasArrayFiller()) return true; |
| 2237 | assert(E->hasArrayFiller() && "no array filler for incomplete init list"); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2238 | // FIXME: The Subobject here isn't necessarily right. This rarely matters, |
| 2239 | // but sometimes does: |
| 2240 | // struct S { constexpr S() : p(&p) {} void *p; }; |
| 2241 | // S s[10] = {}; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2242 | return EvaluateConstantExpression(Result.getArrayFiller(), Info, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2243 | Subobject, E->getArrayFiller()); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2244 | } |
| 2245 | |
| 2246 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2247 | // Integer Evaluation |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2248 | // |
| 2249 | // As a GNU extension, we support casting pointers to sufficiently-wide integer |
| 2250 | // types and back in constant folding. Integer values are thus represented |
| 2251 | // either as an integer-valued APValue, or as an lvalue-valued APValue. |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2252 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2253 | |
| 2254 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 2255 | class IntExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2256 | : public ExprEvaluatorBase<IntExprEvaluator, bool> { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2257 | CCValue &Result; |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 2258 | public: |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2259 | IntExprEvaluator(EvalInfo &info, CCValue &result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2260 | : ExprEvaluatorBaseTy(info), Result(result) {} |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2261 | |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 2262 | bool Success(const llvm::APSInt &SI, const Expr *E) { |
| 2263 | assert(E->getType()->isIntegralOrEnumerationType() && |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2264 | "Invalid evaluation result."); |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 2265 | assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 2266 | "Invalid evaluation result."); |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 2267 | assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 2268 | "Invalid evaluation result."); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2269 | Result = CCValue(SI); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 2270 | return true; |
| 2271 | } |
| 2272 | |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2273 | bool Success(const llvm::APInt &I, const Expr *E) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2274 | assert(E->getType()->isIntegralOrEnumerationType() && |
| 2275 | "Invalid evaluation result."); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 2276 | assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 2277 | "Invalid evaluation result."); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2278 | Result = CCValue(APSInt(I)); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 2279 | Result.getInt().setIsUnsigned( |
| 2280 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2281 | return true; |
| 2282 | } |
| 2283 | |
| 2284 | bool Success(uint64_t Value, const Expr *E) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2285 | assert(E->getType()->isIntegralOrEnumerationType() && |
| 2286 | "Invalid evaluation result."); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2287 | Result = CCValue(Info.Ctx.MakeIntValue(Value, E->getType())); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2288 | return true; |
| 2289 | } |
| 2290 | |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 2291 | bool Success(CharUnits Size, const Expr *E) { |
| 2292 | return Success(Size.getQuantity(), E); |
| 2293 | } |
| 2294 | |
| 2295 | |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 2296 | bool Error(SourceLocation L, diag::kind D, const Expr *E) { |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 2297 | // Take the first error. |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 2298 | if (Info.EvalStatus.Diag == 0) { |
| 2299 | Info.EvalStatus.DiagLoc = L; |
| 2300 | Info.EvalStatus.Diag = D; |
| 2301 | Info.EvalStatus.DiagExpr = E; |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 2302 | } |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 2303 | return false; |
Chris Lattner | 7a76778 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 2304 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2305 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2306 | bool Success(const CCValue &V, const Expr *E) { |
Richard Smith | 342f1f8 | 2011-10-29 22:55:55 +0000 | [diff] [blame] | 2307 | if (V.isLValue()) { |
| 2308 | Result = V; |
| 2309 | return true; |
| 2310 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2311 | return Success(V.getInt(), E); |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 2312 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2313 | bool Error(const Expr *E) { |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 2314 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 2315 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2316 | |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2317 | bool ValueInitialization(const Expr *E) { return Success(0, E); } |
| 2318 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2319 | //===--------------------------------------------------------------------===// |
| 2320 | // Visitor Methods |
| 2321 | //===--------------------------------------------------------------------===// |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 2322 | |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 2323 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2324 | return Success(E->getValue(), E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 2325 | } |
| 2326 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2327 | return Success(E->getValue(), E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 2328 | } |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 2329 | |
| 2330 | bool CheckReferencedDecl(const Expr *E, const Decl *D); |
| 2331 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2332 | if (CheckReferencedDecl(E, E->getDecl())) |
| 2333 | return true; |
| 2334 | |
| 2335 | return ExprEvaluatorBaseTy::VisitDeclRefExpr(E); |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 2336 | } |
| 2337 | bool VisitMemberExpr(const MemberExpr *E) { |
| 2338 | if (CheckReferencedDecl(E, E->getMemberDecl())) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2339 | VisitIgnoredValue(E->getBase()); |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 2340 | return true; |
| 2341 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2342 | |
| 2343 | return ExprEvaluatorBaseTy::VisitMemberExpr(E); |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 2344 | } |
| 2345 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2346 | bool VisitCallExpr(const CallExpr *E); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 2347 | bool VisitBinaryOperator(const BinaryOperator *E); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 2348 | bool VisitOffsetOfExpr(const OffsetOfExpr *E); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 2349 | bool VisitUnaryOperator(const UnaryOperator *E); |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 2350 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2351 | bool VisitCastExpr(const CastExpr* E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2352 | bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E); |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2353 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 2354 | bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2355 | return Success(E->getValue(), E); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 2356 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2357 | |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2358 | // Note, GNU defines __null as an integer, not a pointer. |
Anders Carlsson | 3f70456 | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 2359 | bool VisitGNUNullExpr(const GNUNullExpr *E) { |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2360 | return ValueInitialization(E); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 2361 | } |
| 2362 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2363 | bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) { |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2364 | return Success(E->getValue(), E); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2365 | } |
| 2366 | |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2367 | bool VisitBinaryTypeTraitExpr(const BinaryTypeTraitExpr *E) { |
| 2368 | return Success(E->getValue(), E); |
| 2369 | } |
| 2370 | |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 2371 | bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { |
| 2372 | return Success(E->getValue(), E); |
| 2373 | } |
| 2374 | |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 2375 | bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { |
| 2376 | return Success(E->getValue(), E); |
| 2377 | } |
| 2378 | |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 2379 | bool VisitUnaryReal(const UnaryOperator *E); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 2380 | bool VisitUnaryImag(const UnaryOperator *E); |
| 2381 | |
Sebastian Redl | 295995c | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 2382 | bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E); |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 2383 | bool VisitSizeOfPackExpr(const SizeOfPackExpr *E); |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 2384 | |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 2385 | private: |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 2386 | CharUnits GetAlignOfExpr(const Expr *E); |
| 2387 | CharUnits GetAlignOfType(QualType T); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 2388 | static QualType GetObjectType(const Expr *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2389 | bool TryEvaluateBuiltinObjectSize(const CallExpr *E); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 2390 | // FIXME: Missing: array subscript of vector, member of vector |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 2391 | }; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2392 | } // end anonymous namespace |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 2393 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2394 | /// EvaluateIntegerOrLValue - Evaluate an rvalue integral-typed expression, and |
| 2395 | /// produce either the integer value or a pointer. |
| 2396 | /// |
| 2397 | /// GCC has a heinous extension which folds casts between pointer types and |
| 2398 | /// pointer-sized integral types. We support this by allowing the evaluation of |
| 2399 | /// an integer rvalue to produce a pointer (represented as an lvalue) instead. |
| 2400 | /// Some simple arithmetic on such values is supported (they are treated much |
| 2401 | /// like char*). |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2402 | static bool EvaluateIntegerOrLValue(const Expr* E, CCValue &Result, |
| 2403 | EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2404 | assert(E->isRValue() && E->getType()->isIntegralOrEnumerationType()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2405 | return IntExprEvaluator(Info, Result).Visit(E); |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 2406 | } |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 2407 | |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 2408 | static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2409 | CCValue Val; |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 2410 | if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt()) |
| 2411 | return false; |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 2412 | Result = Val.getInt(); |
| 2413 | return true; |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 2414 | } |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 2415 | |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 2416 | bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) { |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 2417 | // Enums are integer constant exprs. |
Abramo Bagnara | bfbdcd8 | 2011-06-30 09:36:05 +0000 | [diff] [blame] | 2418 | if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) { |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 2419 | // Check for signedness/width mismatches between E type and ECD value. |
| 2420 | bool SameSign = (ECD->getInitVal().isSigned() |
| 2421 | == E->getType()->isSignedIntegerOrEnumerationType()); |
| 2422 | bool SameWidth = (ECD->getInitVal().getBitWidth() |
| 2423 | == Info.Ctx.getIntWidth(E->getType())); |
| 2424 | if (SameSign && SameWidth) |
| 2425 | return Success(ECD->getInitVal(), E); |
| 2426 | else { |
| 2427 | // Get rid of mismatch (otherwise Success assertions will fail) |
| 2428 | // by computing a new value matching the type of E. |
| 2429 | llvm::APSInt Val = ECD->getInitVal(); |
| 2430 | if (!SameSign) |
| 2431 | Val.setIsSigned(!ECD->getInitVal().isSigned()); |
| 2432 | if (!SameWidth) |
| 2433 | Val = Val.extOrTrunc(Info.Ctx.getIntWidth(E->getType())); |
| 2434 | return Success(Val, E); |
| 2435 | } |
Abramo Bagnara | bfbdcd8 | 2011-06-30 09:36:05 +0000 | [diff] [blame] | 2436 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2437 | return false; |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 2438 | } |
| 2439 | |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 2440 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
| 2441 | /// as GCC. |
| 2442 | static int EvaluateBuiltinClassifyType(const CallExpr *E) { |
| 2443 | // The following enum mimics the values returned by GCC. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2444 | // FIXME: Does GCC differ between lvalue and rvalue references here? |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 2445 | enum gcc_type_class { |
| 2446 | no_type_class = -1, |
| 2447 | void_type_class, integer_type_class, char_type_class, |
| 2448 | enumeral_type_class, boolean_type_class, |
| 2449 | pointer_type_class, reference_type_class, offset_type_class, |
| 2450 | real_type_class, complex_type_class, |
| 2451 | function_type_class, method_type_class, |
| 2452 | record_type_class, union_type_class, |
| 2453 | array_type_class, string_type_class, |
| 2454 | lang_type_class |
| 2455 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2456 | |
| 2457 | // If no argument was supplied, default to "no_type_class". This isn't |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 2458 | // ideal, however it is what gcc does. |
| 2459 | if (E->getNumArgs() == 0) |
| 2460 | return no_type_class; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2461 | |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 2462 | QualType ArgTy = E->getArg(0)->getType(); |
| 2463 | if (ArgTy->isVoidType()) |
| 2464 | return void_type_class; |
| 2465 | else if (ArgTy->isEnumeralType()) |
| 2466 | return enumeral_type_class; |
| 2467 | else if (ArgTy->isBooleanType()) |
| 2468 | return boolean_type_class; |
| 2469 | else if (ArgTy->isCharType()) |
| 2470 | return string_type_class; // gcc doesn't appear to use char_type_class |
| 2471 | else if (ArgTy->isIntegerType()) |
| 2472 | return integer_type_class; |
| 2473 | else if (ArgTy->isPointerType()) |
| 2474 | return pointer_type_class; |
| 2475 | else if (ArgTy->isReferenceType()) |
| 2476 | return reference_type_class; |
| 2477 | else if (ArgTy->isRealType()) |
| 2478 | return real_type_class; |
| 2479 | else if (ArgTy->isComplexType()) |
| 2480 | return complex_type_class; |
| 2481 | else if (ArgTy->isFunctionType()) |
| 2482 | return function_type_class; |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 2483 | else if (ArgTy->isStructureOrClassType()) |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 2484 | return record_type_class; |
| 2485 | else if (ArgTy->isUnionType()) |
| 2486 | return union_type_class; |
| 2487 | else if (ArgTy->isArrayType()) |
| 2488 | return array_type_class; |
| 2489 | else if (ArgTy->isUnionType()) |
| 2490 | return union_type_class; |
| 2491 | else // FIXME: offset_type_class, method_type_class, & lang_type_class? |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2492 | llvm_unreachable("CallExpr::isBuiltinClassifyType(): unimplemented type"); |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 2493 | return -1; |
| 2494 | } |
| 2495 | |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 2496 | /// Retrieves the "underlying object type" of the given expression, |
| 2497 | /// as used by __builtin_object_size. |
| 2498 | QualType IntExprEvaluator::GetObjectType(const Expr *E) { |
| 2499 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 2500 | if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) |
| 2501 | return VD->getType(); |
| 2502 | } else if (isa<CompoundLiteralExpr>(E)) { |
| 2503 | return E->getType(); |
| 2504 | } |
| 2505 | |
| 2506 | return QualType(); |
| 2507 | } |
| 2508 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2509 | bool IntExprEvaluator::TryEvaluateBuiltinObjectSize(const CallExpr *E) { |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 2510 | // TODO: Perhaps we should let LLVM lower this? |
| 2511 | LValue Base; |
| 2512 | if (!EvaluatePointer(E->getArg(0), Base, Info)) |
| 2513 | return false; |
| 2514 | |
| 2515 | // If we can prove the base is null, lower to zero now. |
| 2516 | const Expr *LVBase = Base.getLValueBase(); |
| 2517 | if (!LVBase) return Success(0, E); |
| 2518 | |
| 2519 | QualType T = GetObjectType(LVBase); |
| 2520 | if (T.isNull() || |
| 2521 | T->isIncompleteType() || |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 2522 | T->isFunctionType() || |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 2523 | T->isVariablyModifiedType() || |
| 2524 | T->isDependentType()) |
| 2525 | return false; |
| 2526 | |
| 2527 | CharUnits Size = Info.Ctx.getTypeSizeInChars(T); |
| 2528 | CharUnits Offset = Base.getLValueOffset(); |
| 2529 | |
| 2530 | if (!Offset.isNegative() && Offset <= Size) |
| 2531 | Size -= Offset; |
| 2532 | else |
| 2533 | Size = CharUnits::Zero(); |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 2534 | return Success(Size, E); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 2535 | } |
| 2536 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2537 | bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2538 | switch (E->isBuiltinCall()) { |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 2539 | default: |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2540 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 2541 | |
| 2542 | case Builtin::BI__builtin_object_size: { |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 2543 | if (TryEvaluateBuiltinObjectSize(E)) |
| 2544 | return true; |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 2545 | |
Eric Christopher | b2aaf51 | 2010-01-19 22:58:35 +0000 | [diff] [blame] | 2546 | // If evaluating the argument has side-effects we can't determine |
| 2547 | // the size of the object and lower it to unknown now. |
Fariborz Jahanian | 393c247 | 2009-11-05 18:03:03 +0000 | [diff] [blame] | 2548 | if (E->getArg(0)->HasSideEffects(Info.Ctx)) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2549 | if (E->getArg(1)->EvaluateKnownConstInt(Info.Ctx).getZExtValue() <= 1) |
Chris Lattner | cf18465 | 2009-11-03 19:48:51 +0000 | [diff] [blame] | 2550 | return Success(-1ULL, E); |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 2551 | return Success(0, E); |
| 2552 | } |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 2553 | |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 2554 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
| 2555 | } |
| 2556 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 2557 | case Builtin::BI__builtin_classify_type: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2558 | return Success(EvaluateBuiltinClassifyType(E), E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2559 | |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 2560 | case Builtin::BI__builtin_constant_p: |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 2561 | // __builtin_constant_p always has one operand: it returns true if that |
| 2562 | // operand can be folded, false otherwise. |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2563 | return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E); |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 2564 | |
| 2565 | case Builtin::BI__builtin_eh_return_data_regno: { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2566 | int Operand = E->getArg(0)->EvaluateKnownConstInt(Info.Ctx).getZExtValue(); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2567 | Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(Operand); |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 2568 | return Success(Operand, E); |
| 2569 | } |
Eli Friedman | c4a2638 | 2010-02-13 00:10:10 +0000 | [diff] [blame] | 2570 | |
| 2571 | case Builtin::BI__builtin_expect: |
| 2572 | return Visit(E->getArg(0)); |
Douglas Gregor | 5726d40 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 2573 | |
| 2574 | case Builtin::BIstrlen: |
| 2575 | case Builtin::BI__builtin_strlen: |
| 2576 | // As an extension, we support strlen() and __builtin_strlen() as constant |
| 2577 | // expressions when the argument is a string literal. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2578 | if (const StringLiteral *S |
Douglas Gregor | 5726d40 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 2579 | = dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenImpCasts())) { |
| 2580 | // The string literal may have embedded null characters. Find the first |
| 2581 | // one and truncate there. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2582 | StringRef Str = S->getString(); |
| 2583 | StringRef::size_type Pos = Str.find(0); |
| 2584 | if (Pos != StringRef::npos) |
Douglas Gregor | 5726d40 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 2585 | Str = Str.substr(0, Pos); |
| 2586 | |
| 2587 | return Success(Str.size(), E); |
| 2588 | } |
| 2589 | |
| 2590 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
Eli Friedman | 454b57a | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 2591 | |
| 2592 | case Builtin::BI__atomic_is_lock_free: { |
| 2593 | APSInt SizeVal; |
| 2594 | if (!EvaluateInteger(E->getArg(0), SizeVal, Info)) |
| 2595 | return false; |
| 2596 | |
| 2597 | // For __atomic_is_lock_free(sizeof(_Atomic(T))), if the size is a power |
| 2598 | // of two less than the maximum inline atomic width, we know it is |
| 2599 | // lock-free. If the size isn't a power of two, or greater than the |
| 2600 | // maximum alignment where we promote atomics, we know it is not lock-free |
| 2601 | // (at least not in the sense of atomic_is_lock_free). Otherwise, |
| 2602 | // the answer can only be determined at runtime; for example, 16-byte |
| 2603 | // atomics have lock-free implementations on some, but not all, |
| 2604 | // x86-64 processors. |
| 2605 | |
| 2606 | // Check power-of-two. |
| 2607 | CharUnits Size = CharUnits::fromQuantity(SizeVal.getZExtValue()); |
| 2608 | if (!Size.isPowerOfTwo()) |
| 2609 | #if 0 |
| 2610 | // FIXME: Suppress this folding until the ABI for the promotion width |
| 2611 | // settles. |
| 2612 | return Success(0, E); |
| 2613 | #else |
| 2614 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
| 2615 | #endif |
| 2616 | |
| 2617 | #if 0 |
| 2618 | // Check against promotion width. |
| 2619 | // FIXME: Suppress this folding until the ABI for the promotion width |
| 2620 | // settles. |
| 2621 | unsigned PromoteWidthBits = |
| 2622 | Info.Ctx.getTargetInfo().getMaxAtomicPromoteWidth(); |
| 2623 | if (Size > Info.Ctx.toCharUnitsFromBits(PromoteWidthBits)) |
| 2624 | return Success(0, E); |
| 2625 | #endif |
| 2626 | |
| 2627 | // Check against inlining width. |
| 2628 | unsigned InlineWidthBits = |
| 2629 | Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth(); |
| 2630 | if (Size <= Info.Ctx.toCharUnitsFromBits(InlineWidthBits)) |
| 2631 | return Success(1, E); |
| 2632 | |
| 2633 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
| 2634 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 2635 | } |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 2636 | } |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 2637 | |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 2638 | static bool HasSameBase(const LValue &A, const LValue &B) { |
| 2639 | if (!A.getLValueBase()) |
| 2640 | return !B.getLValueBase(); |
| 2641 | if (!B.getLValueBase()) |
| 2642 | return false; |
| 2643 | |
| 2644 | if (A.getLValueBase() != B.getLValueBase()) { |
| 2645 | const Decl *ADecl = GetLValueBaseDecl(A); |
| 2646 | if (!ADecl) |
| 2647 | return false; |
| 2648 | const Decl *BDecl = GetLValueBaseDecl(B); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 2649 | if (!BDecl || ADecl->getCanonicalDecl() != BDecl->getCanonicalDecl()) |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 2650 | return false; |
| 2651 | } |
| 2652 | |
| 2653 | return IsGlobalLValue(A.getLValueBase()) || |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 2654 | A.getLValueFrame() == B.getLValueFrame(); |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 2655 | } |
| 2656 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 2657 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2658 | if (E->isAssignmentOp()) |
| 2659 | return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E); |
| 2660 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2661 | if (E->getOpcode() == BO_Comma) { |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 2662 | VisitIgnoredValue(E->getLHS()); |
| 2663 | return Visit(E->getRHS()); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 2664 | } |
| 2665 | |
| 2666 | if (E->isLogicalOp()) { |
| 2667 | // These need to be handled specially because the operands aren't |
| 2668 | // necessarily integral |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 2669 | bool lhsResult, rhsResult; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2670 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2671 | if (EvaluateAsBooleanCondition(E->getLHS(), lhsResult, Info)) { |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 2672 | // We were able to evaluate the LHS, see if we can get away with not |
| 2673 | // evaluating the RHS: 0 && X -> 0, 1 || X -> 1 |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2674 | if (lhsResult == (E->getOpcode() == BO_LOr)) |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 2675 | return Success(lhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 2676 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2677 | if (EvaluateAsBooleanCondition(E->getRHS(), rhsResult, Info)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2678 | if (E->getOpcode() == BO_LOr) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2679 | return Success(lhsResult || rhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 2680 | else |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2681 | return Success(lhsResult && rhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 2682 | } |
| 2683 | } else { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2684 | if (EvaluateAsBooleanCondition(E->getRHS(), rhsResult, Info)) { |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 2685 | // We can't evaluate the LHS; however, sometimes the result |
| 2686 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2687 | if (rhsResult == (E->getOpcode() == BO_LOr) || |
| 2688 | !rhsResult == (E->getOpcode() == BO_LAnd)) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2689 | // Since we weren't able to evaluate the left hand side, it |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 2690 | // must have had side effects. |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 2691 | Info.EvalStatus.HasSideEffects = true; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2692 | |
| 2693 | return Success(rhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 2694 | } |
| 2695 | } |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 2696 | } |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 2697 | |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 2698 | return false; |
| 2699 | } |
| 2700 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 2701 | QualType LHSTy = E->getLHS()->getType(); |
| 2702 | QualType RHSTy = E->getRHS()->getType(); |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 2703 | |
| 2704 | if (LHSTy->isAnyComplexType()) { |
| 2705 | assert(RHSTy->isAnyComplexType() && "Invalid comparison"); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 2706 | ComplexValue LHS, RHS; |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 2707 | |
| 2708 | if (!EvaluateComplex(E->getLHS(), LHS, Info)) |
| 2709 | return false; |
| 2710 | |
| 2711 | if (!EvaluateComplex(E->getRHS(), RHS, Info)) |
| 2712 | return false; |
| 2713 | |
| 2714 | if (LHS.isComplexFloat()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2715 | APFloat::cmpResult CR_r = |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 2716 | LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2717 | APFloat::cmpResult CR_i = |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 2718 | LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag()); |
| 2719 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2720 | if (E->getOpcode() == BO_EQ) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2721 | return Success((CR_r == APFloat::cmpEqual && |
| 2722 | CR_i == APFloat::cmpEqual), E); |
| 2723 | else { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2724 | assert(E->getOpcode() == BO_NE && |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2725 | "Invalid complex comparison."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2726 | return Success(((CR_r == APFloat::cmpGreaterThan || |
Mon P Wang | fc39dc4 | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 2727 | CR_r == APFloat::cmpLessThan || |
| 2728 | CR_r == APFloat::cmpUnordered) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2729 | (CR_i == APFloat::cmpGreaterThan || |
Mon P Wang | fc39dc4 | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 2730 | CR_i == APFloat::cmpLessThan || |
| 2731 | CR_i == APFloat::cmpUnordered)), E); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2732 | } |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 2733 | } else { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2734 | if (E->getOpcode() == BO_EQ) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2735 | return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() && |
| 2736 | LHS.getComplexIntImag() == RHS.getComplexIntImag()), E); |
| 2737 | else { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2738 | assert(E->getOpcode() == BO_NE && |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2739 | "Invalid compex comparison."); |
| 2740 | return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() || |
| 2741 | LHS.getComplexIntImag() != RHS.getComplexIntImag()), E); |
| 2742 | } |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 2743 | } |
| 2744 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2745 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 2746 | if (LHSTy->isRealFloatingType() && |
| 2747 | RHSTy->isRealFloatingType()) { |
| 2748 | APFloat RHS(0.0), LHS(0.0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2749 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 2750 | if (!EvaluateFloat(E->getRHS(), RHS, Info)) |
| 2751 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2752 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 2753 | if (!EvaluateFloat(E->getLHS(), LHS, Info)) |
| 2754 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2755 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 2756 | APFloat::cmpResult CR = LHS.compare(RHS); |
Anders Carlsson | 529569e | 2008-11-16 22:46:56 +0000 | [diff] [blame] | 2757 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 2758 | switch (E->getOpcode()) { |
| 2759 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2760 | llvm_unreachable("Invalid binary operator!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2761 | case BO_LT: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2762 | return Success(CR == APFloat::cmpLessThan, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2763 | case BO_GT: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2764 | return Success(CR == APFloat::cmpGreaterThan, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2765 | case BO_LE: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2766 | return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2767 | case BO_GE: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2768 | return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual, |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2769 | E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2770 | case BO_EQ: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2771 | return Success(CR == APFloat::cmpEqual, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2772 | case BO_NE: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2773 | return Success(CR == APFloat::cmpGreaterThan |
Mon P Wang | fc39dc4 | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 2774 | || CR == APFloat::cmpLessThan |
| 2775 | || CR == APFloat::cmpUnordered, E); |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 2776 | } |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 2777 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2778 | |
Eli Friedman | ad02d7d | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 2779 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 2780 | if (E->getOpcode() == BO_Sub || E->isComparisonOp()) { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2781 | LValue LHSValue; |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 2782 | if (!EvaluatePointer(E->getLHS(), LHSValue, Info)) |
| 2783 | return false; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 2784 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2785 | LValue RHSValue; |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 2786 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info)) |
| 2787 | return false; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 2788 | |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 2789 | // Reject differing bases from the normal codepath; we special-case |
| 2790 | // comparisons to null. |
| 2791 | if (!HasSameBase(LHSValue, RHSValue)) { |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 2792 | // Inequalities and subtractions between unrelated pointers have |
| 2793 | // unspecified or undefined behavior. |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 2794 | if (!E->isEqualityOp()) |
| 2795 | return false; |
Eli Friedman | ffbda40 | 2011-10-31 22:28:05 +0000 | [diff] [blame] | 2796 | // A constant address may compare equal to the address of a symbol. |
| 2797 | // The one exception is that address of an object cannot compare equal |
Eli Friedman | c45061b | 2011-10-31 22:54:30 +0000 | [diff] [blame] | 2798 | // to a null pointer constant. |
Eli Friedman | ffbda40 | 2011-10-31 22:28:05 +0000 | [diff] [blame] | 2799 | if ((!LHSValue.Base && !LHSValue.Offset.isZero()) || |
| 2800 | (!RHSValue.Base && !RHSValue.Offset.isZero())) |
| 2801 | return false; |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 2802 | // It's implementation-defined whether distinct literals will have |
Eli Friedman | c45061b | 2011-10-31 22:54:30 +0000 | [diff] [blame] | 2803 | // distinct addresses. In clang, we do not guarantee the addresses are |
Richard Smith | 74f4634 | 2011-11-04 01:10:57 +0000 | [diff] [blame] | 2804 | // distinct. However, we do know that the address of a literal will be |
| 2805 | // non-null. |
| 2806 | if ((IsLiteralLValue(LHSValue) || IsLiteralLValue(RHSValue)) && |
| 2807 | LHSValue.Base && RHSValue.Base) |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 2808 | return false; |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 2809 | // We can't tell whether weak symbols will end up pointing to the same |
| 2810 | // object. |
| 2811 | if (IsWeakLValue(LHSValue) || IsWeakLValue(RHSValue)) |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 2812 | return false; |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 2813 | // Pointers with different bases cannot represent the same object. |
Eli Friedman | c45061b | 2011-10-31 22:54:30 +0000 | [diff] [blame] | 2814 | // (Note that clang defaults to -fmerge-all-constants, which can |
| 2815 | // lead to inconsistent results for comparisons involving the address |
| 2816 | // of a constant; this generally doesn't matter in practice.) |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 2817 | return Success(E->getOpcode() == BO_NE, E); |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 2818 | } |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 2819 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2820 | // FIXME: Implement the C++11 restrictions: |
| 2821 | // - Pointer subtractions must be on elements of the same array. |
| 2822 | // - Pointer comparisons must be between members with the same access. |
| 2823 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2824 | if (E->getOpcode() == BO_Sub) { |
Chris Lattner | 4992bdd | 2010-04-20 17:13:14 +0000 | [diff] [blame] | 2825 | QualType Type = E->getLHS()->getType(); |
| 2826 | QualType ElementType = Type->getAs<PointerType>()->getPointeeType(); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 2827 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2828 | CharUnits ElementSize; |
| 2829 | if (!HandleSizeof(Info, ElementType, ElementSize)) |
| 2830 | return false; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 2831 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2832 | CharUnits Diff = LHSValue.getLValueOffset() - |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 2833 | RHSValue.getLValueOffset(); |
| 2834 | return Success(Diff / ElementSize, E); |
Eli Friedman | ad02d7d | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 2835 | } |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 2836 | |
| 2837 | const CharUnits &LHSOffset = LHSValue.getLValueOffset(); |
| 2838 | const CharUnits &RHSOffset = RHSValue.getLValueOffset(); |
| 2839 | switch (E->getOpcode()) { |
| 2840 | default: llvm_unreachable("missing comparison operator"); |
| 2841 | case BO_LT: return Success(LHSOffset < RHSOffset, E); |
| 2842 | case BO_GT: return Success(LHSOffset > RHSOffset, E); |
| 2843 | case BO_LE: return Success(LHSOffset <= RHSOffset, E); |
| 2844 | case BO_GE: return Success(LHSOffset >= RHSOffset, E); |
| 2845 | case BO_EQ: return Success(LHSOffset == RHSOffset, E); |
| 2846 | case BO_NE: return Success(LHSOffset != RHSOffset, E); |
Eli Friedman | ad02d7d | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 2847 | } |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 2848 | } |
| 2849 | } |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2850 | if (!LHSTy->isIntegralOrEnumerationType() || |
| 2851 | !RHSTy->isIntegralOrEnumerationType()) { |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 2852 | // We can't continue from here for non-integral types, and they |
| 2853 | // could potentially confuse the following operations. |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 2854 | return false; |
| 2855 | } |
| 2856 | |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 2857 | // The LHS of a constant expr is always evaluated and needed. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2858 | CCValue LHSVal; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2859 | if (!EvaluateIntegerOrLValue(E->getLHS(), LHSVal, Info)) |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 2860 | return false; // error in subexpression. |
Eli Friedman | d9f4bcd | 2008-07-27 05:46:18 +0000 | [diff] [blame] | 2861 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2862 | if (!Visit(E->getRHS())) |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 2863 | return false; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2864 | CCValue &RHSVal = Result; |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 2865 | |
| 2866 | // Handle cases like (unsigned long)&a + 4. |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2867 | if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) { |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 2868 | CharUnits AdditionalOffset = CharUnits::fromQuantity( |
| 2869 | RHSVal.getInt().getZExtValue()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2870 | if (E->getOpcode() == BO_Add) |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2871 | LHSVal.getLValueOffset() += AdditionalOffset; |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 2872 | else |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2873 | LHSVal.getLValueOffset() -= AdditionalOffset; |
| 2874 | Result = LHSVal; |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 2875 | return true; |
| 2876 | } |
| 2877 | |
| 2878 | // Handle cases like 4 + (unsigned long)&a |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2879 | if (E->getOpcode() == BO_Add && |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2880 | RHSVal.isLValue() && LHSVal.isInt()) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2881 | RHSVal.getLValueOffset() += CharUnits::fromQuantity( |
| 2882 | LHSVal.getInt().getZExtValue()); |
| 2883 | // Note that RHSVal is Result. |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 2884 | return true; |
| 2885 | } |
| 2886 | |
| 2887 | // All the following cases expect both operands to be an integer |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2888 | if (!LHSVal.isInt() || !RHSVal.isInt()) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 2889 | return false; |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 2890 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2891 | APSInt &LHS = LHSVal.getInt(); |
| 2892 | APSInt &RHS = RHSVal.getInt(); |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 2893 | |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 2894 | switch (E->getOpcode()) { |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 2895 | default: |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 2896 | return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2897 | case BO_Mul: return Success(LHS * RHS, E); |
| 2898 | case BO_Add: return Success(LHS + RHS, E); |
| 2899 | case BO_Sub: return Success(LHS - RHS, E); |
| 2900 | case BO_And: return Success(LHS & RHS, E); |
| 2901 | case BO_Xor: return Success(LHS ^ RHS, E); |
| 2902 | case BO_Or: return Success(LHS | RHS, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2903 | case BO_Div: |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 2904 | if (RHS == 0) |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 2905 | return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2906 | return Success(LHS / RHS, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2907 | case BO_Rem: |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 2908 | if (RHS == 0) |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 2909 | return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2910 | return Success(LHS % RHS, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2911 | case BO_Shl: { |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 2912 | // During constant-folding, a negative shift is an opposite shift. |
| 2913 | if (RHS.isSigned() && RHS.isNegative()) { |
| 2914 | RHS = -RHS; |
| 2915 | goto shift_right; |
| 2916 | } |
| 2917 | |
| 2918 | shift_left: |
| 2919 | unsigned SA |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2920 | = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
| 2921 | return Success(LHS << SA, E); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 2922 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2923 | case BO_Shr: { |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 2924 | // During constant-folding, a negative shift is an opposite shift. |
| 2925 | if (RHS.isSigned() && RHS.isNegative()) { |
| 2926 | RHS = -RHS; |
| 2927 | goto shift_left; |
| 2928 | } |
| 2929 | |
| 2930 | shift_right: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2931 | unsigned SA = |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2932 | (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
| 2933 | return Success(LHS >> SA, E); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 2934 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2935 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2936 | case BO_LT: return Success(LHS < RHS, E); |
| 2937 | case BO_GT: return Success(LHS > RHS, E); |
| 2938 | case BO_LE: return Success(LHS <= RHS, E); |
| 2939 | case BO_GE: return Success(LHS >= RHS, E); |
| 2940 | case BO_EQ: return Success(LHS == RHS, E); |
| 2941 | case BO_NE: return Success(LHS != RHS, E); |
Eli Friedman | b11e778 | 2008-11-13 02:13:11 +0000 | [diff] [blame] | 2942 | } |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 2943 | } |
| 2944 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 2945 | CharUnits IntExprEvaluator::GetAlignOfType(QualType T) { |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 2946 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 2947 | // the result is the size of the referenced type." |
| 2948 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 2949 | // result shall be the alignment of the referenced type." |
| 2950 | if (const ReferenceType *Ref = T->getAs<ReferenceType>()) |
| 2951 | T = Ref->getPointeeType(); |
Chad Rosier | 9f1210c | 2011-07-26 07:03:04 +0000 | [diff] [blame] | 2952 | |
| 2953 | // __alignof is defined to return the preferred alignment. |
| 2954 | return Info.Ctx.toCharUnitsFromBits( |
| 2955 | Info.Ctx.getPreferredTypeAlign(T.getTypePtr())); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 2956 | } |
| 2957 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 2958 | CharUnits IntExprEvaluator::GetAlignOfExpr(const Expr *E) { |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 2959 | E = E->IgnoreParens(); |
| 2960 | |
| 2961 | // alignof decl is always accepted, even if it doesn't make sense: we default |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2962 | // to 1 in those cases. |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 2963 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 2964 | return Info.Ctx.getDeclAlign(DRE->getDecl(), |
| 2965 | /*RefAsPointee*/true); |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 2966 | |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 2967 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 2968 | return Info.Ctx.getDeclAlign(ME->getMemberDecl(), |
| 2969 | /*RefAsPointee*/true); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 2970 | |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 2971 | return GetAlignOfType(E->getType()); |
| 2972 | } |
| 2973 | |
| 2974 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2975 | /// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with |
| 2976 | /// a result as the expression's type. |
| 2977 | bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr( |
| 2978 | const UnaryExprOrTypeTraitExpr *E) { |
| 2979 | switch(E->getKind()) { |
| 2980 | case UETT_AlignOf: { |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 2981 | if (E->isArgumentType()) |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 2982 | return Success(GetAlignOfType(E->getArgumentType()), E); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 2983 | else |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 2984 | return Success(GetAlignOfExpr(E->getArgumentExpr()), E); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 2985 | } |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 2986 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2987 | case UETT_VecStep: { |
| 2988 | QualType Ty = E->getTypeOfArgument(); |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2989 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2990 | if (Ty->isVectorType()) { |
| 2991 | unsigned n = Ty->getAs<VectorType>()->getNumElements(); |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 2992 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2993 | // The vec_step built-in functions that take a 3-component |
| 2994 | // vector return 4. (OpenCL 1.1 spec 6.11.12) |
| 2995 | if (n == 3) |
| 2996 | n = 4; |
Eli Friedman | f2da9df | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 2997 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2998 | return Success(n, E); |
| 2999 | } else |
| 3000 | return Success(1, E); |
| 3001 | } |
| 3002 | |
| 3003 | case UETT_SizeOf: { |
| 3004 | QualType SrcTy = E->getTypeOfArgument(); |
| 3005 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 3006 | // the result is the size of the referenced type." |
| 3007 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 3008 | // result shall be the alignment of the referenced type." |
| 3009 | if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>()) |
| 3010 | SrcTy = Ref->getPointeeType(); |
| 3011 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3012 | CharUnits Sizeof; |
| 3013 | if (!HandleSizeof(Info, SrcTy, Sizeof)) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3014 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3015 | return Success(Sizeof, E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3016 | } |
| 3017 | } |
| 3018 | |
| 3019 | llvm_unreachable("unknown expr/type trait"); |
| 3020 | return false; |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 3021 | } |
| 3022 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3023 | bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) { |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 3024 | CharUnits Result; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3025 | unsigned n = OOE->getNumComponents(); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 3026 | if (n == 0) |
| 3027 | return false; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3028 | QualType CurrentType = OOE->getTypeSourceInfo()->getType(); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 3029 | for (unsigned i = 0; i != n; ++i) { |
| 3030 | OffsetOfExpr::OffsetOfNode ON = OOE->getComponent(i); |
| 3031 | switch (ON.getKind()) { |
| 3032 | case OffsetOfExpr::OffsetOfNode::Array: { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3033 | const Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex()); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 3034 | APSInt IdxResult; |
| 3035 | if (!EvaluateInteger(Idx, IdxResult, Info)) |
| 3036 | return false; |
| 3037 | const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType); |
| 3038 | if (!AT) |
| 3039 | return false; |
| 3040 | CurrentType = AT->getElementType(); |
| 3041 | CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType); |
| 3042 | Result += IdxResult.getSExtValue() * ElementSize; |
| 3043 | break; |
| 3044 | } |
| 3045 | |
| 3046 | case OffsetOfExpr::OffsetOfNode::Field: { |
| 3047 | FieldDecl *MemberDecl = ON.getField(); |
| 3048 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
| 3049 | if (!RT) |
| 3050 | return false; |
| 3051 | RecordDecl *RD = RT->getDecl(); |
| 3052 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 3053 | unsigned i = MemberDecl->getFieldIndex(); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 3054 | assert(i < RL.getFieldCount() && "offsetof field in wrong type"); |
Ken Dyck | fb1e3bc | 2011-01-18 01:56:16 +0000 | [diff] [blame] | 3055 | Result += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i)); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 3056 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 3057 | break; |
| 3058 | } |
| 3059 | |
| 3060 | case OffsetOfExpr::OffsetOfNode::Identifier: |
| 3061 | llvm_unreachable("dependent __builtin_offsetof"); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 3062 | return false; |
| 3063 | |
| 3064 | case OffsetOfExpr::OffsetOfNode::Base: { |
| 3065 | CXXBaseSpecifier *BaseSpec = ON.getBase(); |
| 3066 | if (BaseSpec->isVirtual()) |
| 3067 | return false; |
| 3068 | |
| 3069 | // Find the layout of the class whose base we are looking into. |
| 3070 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
| 3071 | if (!RT) |
| 3072 | return false; |
| 3073 | RecordDecl *RD = RT->getDecl(); |
| 3074 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
| 3075 | |
| 3076 | // Find the base class itself. |
| 3077 | CurrentType = BaseSpec->getType(); |
| 3078 | const RecordType *BaseRT = CurrentType->getAs<RecordType>(); |
| 3079 | if (!BaseRT) |
| 3080 | return false; |
| 3081 | |
| 3082 | // Add the offset to the base. |
Ken Dyck | 7c7f820 | 2011-01-26 02:17:08 +0000 | [diff] [blame] | 3083 | Result += RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl())); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 3084 | break; |
| 3085 | } |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 3086 | } |
| 3087 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3088 | return Success(Result, OOE); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 3089 | } |
| 3090 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 3091 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3092 | if (E->getOpcode() == UO_LNot) { |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 3093 | // LNot's operand isn't necessarily an integer, so we handle it specially. |
| 3094 | bool bres; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3095 | if (!EvaluateAsBooleanCondition(E->getSubExpr(), bres, Info)) |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 3096 | return false; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3097 | return Success(!bres, E); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 3098 | } |
| 3099 | |
Daniel Dunbar | 4fff481 | 2009-02-21 18:14:20 +0000 | [diff] [blame] | 3100 | // Only handle integral operations... |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3101 | if (!E->getSubExpr()->getType()->isIntegralOrEnumerationType()) |
Daniel Dunbar | 4fff481 | 2009-02-21 18:14:20 +0000 | [diff] [blame] | 3102 | return false; |
| 3103 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3104 | // Get the operand value. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3105 | CCValue Val; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3106 | if (!Evaluate(Val, Info, E->getSubExpr())) |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 3107 | return false; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 3108 | |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 3109 | switch (E->getOpcode()) { |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 3110 | default: |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 3111 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 3112 | // See C99 6.6p3. |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 3113 | return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3114 | case UO_Extension: |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 3115 | // FIXME: Should extension allow i-c-e extension expressions in its scope? |
| 3116 | // If so, we could clear the diagnostic ID. |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3117 | return Success(Val, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3118 | case UO_Plus: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3119 | // The result is just the value. |
| 3120 | return Success(Val, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3121 | case UO_Minus: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3122 | if (!Val.isInt()) return false; |
| 3123 | return Success(-Val.getInt(), E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3124 | case UO_Not: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3125 | if (!Val.isInt()) return false; |
| 3126 | return Success(~Val.getInt(), E); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 3127 | } |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 3128 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3129 | |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 3130 | /// HandleCast - This is used to evaluate implicit or explicit casts where the |
| 3131 | /// result type is integer. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3132 | bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 3133 | const Expr *SubExpr = E->getSubExpr(); |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 3134 | QualType DestType = E->getType(); |
Daniel Dunbar | b92dac8 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 3135 | QualType SrcType = SubExpr->getType(); |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 3136 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3137 | switch (E->getCastKind()) { |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3138 | case CK_BaseToDerived: |
| 3139 | case CK_DerivedToBase: |
| 3140 | case CK_UncheckedDerivedToBase: |
| 3141 | case CK_Dynamic: |
| 3142 | case CK_ToUnion: |
| 3143 | case CK_ArrayToPointerDecay: |
| 3144 | case CK_FunctionToPointerDecay: |
| 3145 | case CK_NullToPointer: |
| 3146 | case CK_NullToMemberPointer: |
| 3147 | case CK_BaseToDerivedMemberPointer: |
| 3148 | case CK_DerivedToBaseMemberPointer: |
| 3149 | case CK_ConstructorConversion: |
| 3150 | case CK_IntegralToPointer: |
| 3151 | case CK_ToVoid: |
| 3152 | case CK_VectorSplat: |
| 3153 | case CK_IntegralToFloating: |
| 3154 | case CK_FloatingCast: |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3155 | case CK_CPointerToObjCPointerCast: |
| 3156 | case CK_BlockPointerToObjCPointerCast: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3157 | case CK_AnyPointerToBlockPointerCast: |
| 3158 | case CK_ObjCObjectLValueCast: |
| 3159 | case CK_FloatingRealToComplex: |
| 3160 | case CK_FloatingComplexToReal: |
| 3161 | case CK_FloatingComplexCast: |
| 3162 | case CK_FloatingComplexToIntegralComplex: |
| 3163 | case CK_IntegralRealToComplex: |
| 3164 | case CK_IntegralComplexCast: |
| 3165 | case CK_IntegralComplexToFloatingComplex: |
| 3166 | llvm_unreachable("invalid cast kind for integral value"); |
| 3167 | |
Eli Friedman | e50c297 | 2011-03-25 19:07:11 +0000 | [diff] [blame] | 3168 | case CK_BitCast: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3169 | case CK_Dependent: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3170 | case CK_LValueBitCast: |
| 3171 | case CK_UserDefinedConversion: |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 3172 | case CK_ARCProduceObject: |
| 3173 | case CK_ARCConsumeObject: |
| 3174 | case CK_ARCReclaimReturnedObject: |
| 3175 | case CK_ARCExtendBlockObject: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3176 | return false; |
| 3177 | |
| 3178 | case CK_LValueToRValue: |
| 3179 | case CK_NoOp: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3180 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3181 | |
| 3182 | case CK_MemberPointerToBoolean: |
| 3183 | case CK_PointerToBoolean: |
| 3184 | case CK_IntegralToBoolean: |
| 3185 | case CK_FloatingToBoolean: |
| 3186 | case CK_FloatingComplexToBoolean: |
| 3187 | case CK_IntegralComplexToBoolean: { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3188 | bool BoolResult; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3189 | if (!EvaluateAsBooleanCondition(SubExpr, BoolResult, Info)) |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3190 | return false; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3191 | return Success(BoolResult, E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3192 | } |
| 3193 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3194 | case CK_IntegralCast: { |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 3195 | if (!Visit(SubExpr)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 3196 | return false; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 3197 | |
Eli Friedman | be26570 | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 3198 | if (!Result.isInt()) { |
| 3199 | // Only allow casts of lvalues if they are lossless. |
| 3200 | return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType); |
| 3201 | } |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 3202 | |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 3203 | return Success(HandleIntToIntCast(DestType, SrcType, |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 3204 | Result.getInt(), Info.Ctx), E); |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 3205 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3206 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3207 | case CK_PointerToIntegral: { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3208 | LValue LV; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 3209 | if (!EvaluatePointer(SubExpr, LV, Info)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 3210 | return false; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3211 | |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 3212 | if (LV.getLValueBase()) { |
| 3213 | // Only allow based lvalue casts if they are lossless. |
| 3214 | if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType)) |
| 3215 | return false; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3216 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3217 | LV.moveInto(Result); |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 3218 | return true; |
| 3219 | } |
| 3220 | |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 3221 | APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(), |
| 3222 | SrcType); |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 3223 | return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E); |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 3224 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3225 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3226 | case CK_IntegralComplexToReal: { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 3227 | ComplexValue C; |
Eli Friedman | 1725f68 | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 3228 | if (!EvaluateComplex(SubExpr, C, Info)) |
| 3229 | return false; |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3230 | return Success(C.getComplexIntReal(), E); |
Eli Friedman | 1725f68 | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 3231 | } |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 3232 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3233 | case CK_FloatingToIntegral: { |
| 3234 | APFloat F(0.0); |
| 3235 | if (!EvaluateFloat(SubExpr, F, Info)) |
| 3236 | return false; |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 3237 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3238 | return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E); |
| 3239 | } |
| 3240 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3241 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3242 | llvm_unreachable("unknown cast resulting in integral value"); |
| 3243 | return false; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 3244 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 3245 | |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 3246 | bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| 3247 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 3248 | ComplexValue LV; |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 3249 | if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt()) |
| 3250 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
| 3251 | return Success(LV.getComplexIntReal(), E); |
| 3252 | } |
| 3253 | |
| 3254 | return Visit(E->getSubExpr()); |
| 3255 | } |
| 3256 | |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 3257 | bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 3258 | if (E->getSubExpr()->getType()->isComplexIntegerType()) { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 3259 | ComplexValue LV; |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 3260 | if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt()) |
| 3261 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
| 3262 | return Success(LV.getComplexIntImag(), E); |
| 3263 | } |
| 3264 | |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 3265 | VisitIgnoredValue(E->getSubExpr()); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 3266 | return Success(0, E); |
| 3267 | } |
| 3268 | |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 3269 | bool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) { |
| 3270 | return Success(E->getPackLength(), E); |
| 3271 | } |
| 3272 | |
Sebastian Redl | 295995c | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 3273 | bool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) { |
| 3274 | return Success(E->getValue(), E); |
| 3275 | } |
| 3276 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3277 | //===----------------------------------------------------------------------===// |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 3278 | // Float Evaluation |
| 3279 | //===----------------------------------------------------------------------===// |
| 3280 | |
| 3281 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 3282 | class FloatExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3283 | : public ExprEvaluatorBase<FloatExprEvaluator, bool> { |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 3284 | APFloat &Result; |
| 3285 | public: |
| 3286 | FloatExprEvaluator(EvalInfo &info, APFloat &result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3287 | : ExprEvaluatorBaseTy(info), Result(result) {} |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 3288 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3289 | bool Success(const CCValue &V, const Expr *e) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3290 | Result = V.getFloat(); |
| 3291 | return true; |
| 3292 | } |
| 3293 | bool Error(const Stmt *S) { |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 3294 | return false; |
| 3295 | } |
| 3296 | |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 3297 | bool ValueInitialization(const Expr *E) { |
| 3298 | Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType())); |
| 3299 | return true; |
| 3300 | } |
| 3301 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 3302 | bool VisitCallExpr(const CallExpr *E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 3303 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 3304 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 3305 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 3306 | bool VisitFloatingLiteral(const FloatingLiteral *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3307 | bool VisitCastExpr(const CastExpr *E); |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 3308 | |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 3309 | bool VisitUnaryReal(const UnaryOperator *E); |
| 3310 | bool VisitUnaryImag(const UnaryOperator *E); |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 3311 | |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 3312 | // FIXME: Missing: array subscript of vector, member of vector, |
| 3313 | // ImplicitValueInitExpr |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 3314 | }; |
| 3315 | } // end anonymous namespace |
| 3316 | |
| 3317 | static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3318 | assert(E->isRValue() && E->getType()->isRealFloatingType()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3319 | return FloatExprEvaluator(Info, Result).Visit(E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 3320 | } |
| 3321 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3322 | static bool TryEvaluateBuiltinNaN(const ASTContext &Context, |
John McCall | db7b72a | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 3323 | QualType ResultTy, |
| 3324 | const Expr *Arg, |
| 3325 | bool SNaN, |
| 3326 | llvm::APFloat &Result) { |
| 3327 | const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts()); |
| 3328 | if (!S) return false; |
| 3329 | |
| 3330 | const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy); |
| 3331 | |
| 3332 | llvm::APInt fill; |
| 3333 | |
| 3334 | // Treat empty strings as if they were zero. |
| 3335 | if (S->getString().empty()) |
| 3336 | fill = llvm::APInt(32, 0); |
| 3337 | else if (S->getString().getAsInteger(0, fill)) |
| 3338 | return false; |
| 3339 | |
| 3340 | if (SNaN) |
| 3341 | Result = llvm::APFloat::getSNaN(Sem, false, &fill); |
| 3342 | else |
| 3343 | Result = llvm::APFloat::getQNaN(Sem, false, &fill); |
| 3344 | return true; |
| 3345 | } |
| 3346 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 3347 | bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3348 | switch (E->isBuiltinCall()) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3349 | default: |
| 3350 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
| 3351 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 3352 | case Builtin::BI__builtin_huge_val: |
| 3353 | case Builtin::BI__builtin_huge_valf: |
| 3354 | case Builtin::BI__builtin_huge_vall: |
| 3355 | case Builtin::BI__builtin_inf: |
| 3356 | case Builtin::BI__builtin_inff: |
Daniel Dunbar | 7cbed03 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 3357 | case Builtin::BI__builtin_infl: { |
| 3358 | const llvm::fltSemantics &Sem = |
| 3359 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Chris Lattner | 34a74ab | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 3360 | Result = llvm::APFloat::getInf(Sem); |
| 3361 | return true; |
Daniel Dunbar | 7cbed03 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 3362 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3363 | |
John McCall | db7b72a | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 3364 | case Builtin::BI__builtin_nans: |
| 3365 | case Builtin::BI__builtin_nansf: |
| 3366 | case Builtin::BI__builtin_nansl: |
| 3367 | return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 3368 | true, Result); |
| 3369 | |
Chris Lattner | 9e62171 | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 3370 | case Builtin::BI__builtin_nan: |
| 3371 | case Builtin::BI__builtin_nanf: |
| 3372 | case Builtin::BI__builtin_nanl: |
Mike Stump | 4572bab | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 3373 | // If this is __builtin_nan() turn this into a nan, otherwise we |
Chris Lattner | 9e62171 | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 3374 | // can't constant fold it. |
John McCall | db7b72a | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 3375 | return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 3376 | false, Result); |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 3377 | |
| 3378 | case Builtin::BI__builtin_fabs: |
| 3379 | case Builtin::BI__builtin_fabsf: |
| 3380 | case Builtin::BI__builtin_fabsl: |
| 3381 | if (!EvaluateFloat(E->getArg(0), Result, Info)) |
| 3382 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3383 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 3384 | if (Result.isNegative()) |
| 3385 | Result.changeSign(); |
| 3386 | return true; |
| 3387 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3388 | case Builtin::BI__builtin_copysign: |
| 3389 | case Builtin::BI__builtin_copysignf: |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 3390 | case Builtin::BI__builtin_copysignl: { |
| 3391 | APFloat RHS(0.); |
| 3392 | if (!EvaluateFloat(E->getArg(0), Result, Info) || |
| 3393 | !EvaluateFloat(E->getArg(1), RHS, Info)) |
| 3394 | return false; |
| 3395 | Result.copySign(RHS); |
| 3396 | return true; |
| 3397 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 3398 | } |
| 3399 | } |
| 3400 | |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 3401 | bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
Eli Friedman | 43efa31 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 3402 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 3403 | ComplexValue CV; |
| 3404 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 3405 | return false; |
| 3406 | Result = CV.FloatReal; |
| 3407 | return true; |
| 3408 | } |
| 3409 | |
| 3410 | return Visit(E->getSubExpr()); |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 3411 | } |
| 3412 | |
| 3413 | bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | 43efa31 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 3414 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 3415 | ComplexValue CV; |
| 3416 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 3417 | return false; |
| 3418 | Result = CV.FloatImag; |
| 3419 | return true; |
| 3420 | } |
| 3421 | |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 3422 | VisitIgnoredValue(E->getSubExpr()); |
Eli Friedman | 43efa31 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 3423 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(E->getType()); |
| 3424 | Result = llvm::APFloat::getZero(Sem); |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 3425 | return true; |
| 3426 | } |
| 3427 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 3428 | bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 3429 | switch (E->getOpcode()) { |
| 3430 | default: return false; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3431 | case UO_Plus: |
Richard Smith | 7993e8a | 2011-10-30 23:17:09 +0000 | [diff] [blame] | 3432 | return EvaluateFloat(E->getSubExpr(), Result, Info); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3433 | case UO_Minus: |
Richard Smith | 7993e8a | 2011-10-30 23:17:09 +0000 | [diff] [blame] | 3434 | if (!EvaluateFloat(E->getSubExpr(), Result, Info)) |
| 3435 | return false; |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 3436 | Result.changeSign(); |
| 3437 | return true; |
| 3438 | } |
| 3439 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 3440 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 3441 | bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3442 | if (E->getOpcode() == BO_Comma) { |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 3443 | VisitIgnoredValue(E->getLHS()); |
| 3444 | return Visit(E->getRHS()); |
Eli Friedman | 7f92f03 | 2009-11-16 04:25:37 +0000 | [diff] [blame] | 3445 | } |
| 3446 | |
Richard Smith | ee591a9 | 2011-10-28 23:26:52 +0000 | [diff] [blame] | 3447 | // We can't evaluate pointer-to-member operations or assignments. |
| 3448 | if (E->isPtrMemOp() || E->isAssignmentOp()) |
Anders Carlsson | 96e9366 | 2010-10-31 01:21:47 +0000 | [diff] [blame] | 3449 | return false; |
| 3450 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 3451 | // FIXME: Diagnostics? I really don't understand how the warnings |
| 3452 | // and errors are supposed to work. |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 3453 | APFloat RHS(0.0); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 3454 | if (!EvaluateFloat(E->getLHS(), Result, Info)) |
| 3455 | return false; |
| 3456 | if (!EvaluateFloat(E->getRHS(), RHS, Info)) |
| 3457 | return false; |
| 3458 | |
| 3459 | switch (E->getOpcode()) { |
| 3460 | default: return false; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3461 | case BO_Mul: |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 3462 | Result.multiply(RHS, APFloat::rmNearestTiesToEven); |
| 3463 | return true; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3464 | case BO_Add: |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 3465 | Result.add(RHS, APFloat::rmNearestTiesToEven); |
| 3466 | return true; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3467 | case BO_Sub: |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 3468 | Result.subtract(RHS, APFloat::rmNearestTiesToEven); |
| 3469 | return true; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3470 | case BO_Div: |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 3471 | Result.divide(RHS, APFloat::rmNearestTiesToEven); |
| 3472 | return true; |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 3473 | } |
| 3474 | } |
| 3475 | |
| 3476 | bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { |
| 3477 | Result = E->getValue(); |
| 3478 | return true; |
| 3479 | } |
| 3480 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3481 | bool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 3482 | const Expr* SubExpr = E->getSubExpr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3483 | |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 3484 | switch (E->getCastKind()) { |
| 3485 | default: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3486 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 3487 | |
| 3488 | case CK_IntegralToFloating: { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3489 | APSInt IntResult; |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 3490 | if (!EvaluateInteger(SubExpr, IntResult, Info)) |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3491 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3492 | Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(), |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 3493 | IntResult, Info.Ctx); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3494 | return true; |
| 3495 | } |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 3496 | |
| 3497 | case CK_FloatingCast: { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3498 | if (!Visit(SubExpr)) |
| 3499 | return false; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 3500 | Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(), |
| 3501 | Result, Info.Ctx); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3502 | return true; |
| 3503 | } |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3504 | |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 3505 | case CK_FloatingComplexToReal: { |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3506 | ComplexValue V; |
| 3507 | if (!EvaluateComplex(SubExpr, V, Info)) |
| 3508 | return false; |
| 3509 | Result = V.getComplexFloatReal(); |
| 3510 | return true; |
| 3511 | } |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 3512 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3513 | |
| 3514 | return false; |
| 3515 | } |
| 3516 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 3517 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 3518 | // Complex Evaluation (for float and integer) |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 3519 | //===----------------------------------------------------------------------===// |
| 3520 | |
| 3521 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 3522 | class ComplexExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3523 | : public ExprEvaluatorBase<ComplexExprEvaluator, bool> { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 3524 | ComplexValue &Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3525 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 3526 | public: |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 3527 | ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3528 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
| 3529 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3530 | bool Success(const CCValue &V, const Expr *e) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3531 | Result.setFrom(V); |
| 3532 | return true; |
| 3533 | } |
| 3534 | bool Error(const Expr *E) { |
| 3535 | return false; |
| 3536 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3537 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 3538 | //===--------------------------------------------------------------------===// |
| 3539 | // Visitor Methods |
| 3540 | //===--------------------------------------------------------------------===// |
| 3541 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3542 | bool VisitImaginaryLiteral(const ImaginaryLiteral *E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3543 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3544 | bool VisitCastExpr(const CastExpr *E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3545 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 3546 | bool VisitBinaryOperator(const BinaryOperator *E); |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 3547 | bool VisitUnaryOperator(const UnaryOperator *E); |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 3548 | // FIXME Missing: ImplicitValueInitExpr, InitListExpr |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 3549 | }; |
| 3550 | } // end anonymous namespace |
| 3551 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 3552 | static bool EvaluateComplex(const Expr *E, ComplexValue &Result, |
| 3553 | EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3554 | assert(E->isRValue() && E->getType()->isAnyComplexType()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3555 | return ComplexExprEvaluator(Info, Result).Visit(E); |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 3556 | } |
| 3557 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3558 | bool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) { |
| 3559 | const Expr* SubExpr = E->getSubExpr(); |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 3560 | |
| 3561 | if (SubExpr->getType()->isRealFloatingType()) { |
| 3562 | Result.makeComplexFloat(); |
| 3563 | APFloat &Imag = Result.FloatImag; |
| 3564 | if (!EvaluateFloat(SubExpr, Imag, Info)) |
| 3565 | return false; |
| 3566 | |
| 3567 | Result.FloatReal = APFloat(Imag.getSemantics()); |
| 3568 | return true; |
| 3569 | } else { |
| 3570 | assert(SubExpr->getType()->isIntegerType() && |
| 3571 | "Unexpected imaginary literal."); |
| 3572 | |
| 3573 | Result.makeComplexInt(); |
| 3574 | APSInt &Imag = Result.IntImag; |
| 3575 | if (!EvaluateInteger(SubExpr, Imag, Info)) |
| 3576 | return false; |
| 3577 | |
| 3578 | Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned()); |
| 3579 | return true; |
| 3580 | } |
| 3581 | } |
| 3582 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3583 | bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) { |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 3584 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3585 | switch (E->getCastKind()) { |
| 3586 | case CK_BitCast: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3587 | case CK_BaseToDerived: |
| 3588 | case CK_DerivedToBase: |
| 3589 | case CK_UncheckedDerivedToBase: |
| 3590 | case CK_Dynamic: |
| 3591 | case CK_ToUnion: |
| 3592 | case CK_ArrayToPointerDecay: |
| 3593 | case CK_FunctionToPointerDecay: |
| 3594 | case CK_NullToPointer: |
| 3595 | case CK_NullToMemberPointer: |
| 3596 | case CK_BaseToDerivedMemberPointer: |
| 3597 | case CK_DerivedToBaseMemberPointer: |
| 3598 | case CK_MemberPointerToBoolean: |
| 3599 | case CK_ConstructorConversion: |
| 3600 | case CK_IntegralToPointer: |
| 3601 | case CK_PointerToIntegral: |
| 3602 | case CK_PointerToBoolean: |
| 3603 | case CK_ToVoid: |
| 3604 | case CK_VectorSplat: |
| 3605 | case CK_IntegralCast: |
| 3606 | case CK_IntegralToBoolean: |
| 3607 | case CK_IntegralToFloating: |
| 3608 | case CK_FloatingToIntegral: |
| 3609 | case CK_FloatingToBoolean: |
| 3610 | case CK_FloatingCast: |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3611 | case CK_CPointerToObjCPointerCast: |
| 3612 | case CK_BlockPointerToObjCPointerCast: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3613 | case CK_AnyPointerToBlockPointerCast: |
| 3614 | case CK_ObjCObjectLValueCast: |
| 3615 | case CK_FloatingComplexToReal: |
| 3616 | case CK_FloatingComplexToBoolean: |
| 3617 | case CK_IntegralComplexToReal: |
| 3618 | case CK_IntegralComplexToBoolean: |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 3619 | case CK_ARCProduceObject: |
| 3620 | case CK_ARCConsumeObject: |
| 3621 | case CK_ARCReclaimReturnedObject: |
| 3622 | case CK_ARCExtendBlockObject: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3623 | llvm_unreachable("invalid cast kind for complex value"); |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 3624 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3625 | case CK_LValueToRValue: |
| 3626 | case CK_NoOp: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3627 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3628 | |
| 3629 | case CK_Dependent: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3630 | case CK_LValueBitCast: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3631 | case CK_UserDefinedConversion: |
| 3632 | return false; |
| 3633 | |
| 3634 | case CK_FloatingRealToComplex: { |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 3635 | APFloat &Real = Result.FloatReal; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3636 | if (!EvaluateFloat(E->getSubExpr(), Real, Info)) |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 3637 | return false; |
| 3638 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3639 | Result.makeComplexFloat(); |
| 3640 | Result.FloatImag = APFloat(Real.getSemantics()); |
| 3641 | return true; |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 3642 | } |
| 3643 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3644 | case CK_FloatingComplexCast: { |
| 3645 | if (!Visit(E->getSubExpr())) |
| 3646 | return false; |
| 3647 | |
| 3648 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 3649 | QualType From |
| 3650 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 3651 | |
| 3652 | Result.FloatReal |
| 3653 | = HandleFloatToFloatCast(To, From, Result.FloatReal, Info.Ctx); |
| 3654 | Result.FloatImag |
| 3655 | = HandleFloatToFloatCast(To, From, Result.FloatImag, Info.Ctx); |
| 3656 | return true; |
| 3657 | } |
| 3658 | |
| 3659 | case CK_FloatingComplexToIntegralComplex: { |
| 3660 | if (!Visit(E->getSubExpr())) |
| 3661 | return false; |
| 3662 | |
| 3663 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 3664 | QualType From |
| 3665 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 3666 | Result.makeComplexInt(); |
| 3667 | Result.IntReal = HandleFloatToIntCast(To, From, Result.FloatReal, Info.Ctx); |
| 3668 | Result.IntImag = HandleFloatToIntCast(To, From, Result.FloatImag, Info.Ctx); |
| 3669 | return true; |
| 3670 | } |
| 3671 | |
| 3672 | case CK_IntegralRealToComplex: { |
| 3673 | APSInt &Real = Result.IntReal; |
| 3674 | if (!EvaluateInteger(E->getSubExpr(), Real, Info)) |
| 3675 | return false; |
| 3676 | |
| 3677 | Result.makeComplexInt(); |
| 3678 | Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned()); |
| 3679 | return true; |
| 3680 | } |
| 3681 | |
| 3682 | case CK_IntegralComplexCast: { |
| 3683 | if (!Visit(E->getSubExpr())) |
| 3684 | return false; |
| 3685 | |
| 3686 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 3687 | QualType From |
| 3688 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 3689 | |
| 3690 | Result.IntReal = HandleIntToIntCast(To, From, Result.IntReal, Info.Ctx); |
| 3691 | Result.IntImag = HandleIntToIntCast(To, From, Result.IntImag, Info.Ctx); |
| 3692 | return true; |
| 3693 | } |
| 3694 | |
| 3695 | case CK_IntegralComplexToFloatingComplex: { |
| 3696 | if (!Visit(E->getSubExpr())) |
| 3697 | return false; |
| 3698 | |
| 3699 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 3700 | QualType From |
| 3701 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 3702 | Result.makeComplexFloat(); |
| 3703 | Result.FloatReal = HandleIntToFloatCast(To, From, Result.IntReal, Info.Ctx); |
| 3704 | Result.FloatImag = HandleIntToFloatCast(To, From, Result.IntImag, Info.Ctx); |
| 3705 | return true; |
| 3706 | } |
| 3707 | } |
| 3708 | |
| 3709 | llvm_unreachable("unknown cast resulting in complex value"); |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 3710 | return false; |
| 3711 | } |
| 3712 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 3713 | bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 3714 | if (E->getOpcode() == BO_Comma) { |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 3715 | VisitIgnoredValue(E->getLHS()); |
| 3716 | return Visit(E->getRHS()); |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 3717 | } |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 3718 | if (!Visit(E->getLHS())) |
| 3719 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3720 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 3721 | ComplexValue RHS; |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 3722 | if (!EvaluateComplex(E->getRHS(), RHS, Info)) |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 3723 | return false; |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 3724 | |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 3725 | assert(Result.isComplexFloat() == RHS.isComplexFloat() && |
| 3726 | "Invalid operands to binary operator."); |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 3727 | switch (E->getOpcode()) { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 3728 | default: return false; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3729 | case BO_Add: |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 3730 | if (Result.isComplexFloat()) { |
| 3731 | Result.getComplexFloatReal().add(RHS.getComplexFloatReal(), |
| 3732 | APFloat::rmNearestTiesToEven); |
| 3733 | Result.getComplexFloatImag().add(RHS.getComplexFloatImag(), |
| 3734 | APFloat::rmNearestTiesToEven); |
| 3735 | } else { |
| 3736 | Result.getComplexIntReal() += RHS.getComplexIntReal(); |
| 3737 | Result.getComplexIntImag() += RHS.getComplexIntImag(); |
| 3738 | } |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 3739 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3740 | case BO_Sub: |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 3741 | if (Result.isComplexFloat()) { |
| 3742 | Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(), |
| 3743 | APFloat::rmNearestTiesToEven); |
| 3744 | Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(), |
| 3745 | APFloat::rmNearestTiesToEven); |
| 3746 | } else { |
| 3747 | Result.getComplexIntReal() -= RHS.getComplexIntReal(); |
| 3748 | Result.getComplexIntImag() -= RHS.getComplexIntImag(); |
| 3749 | } |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 3750 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3751 | case BO_Mul: |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 3752 | if (Result.isComplexFloat()) { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 3753 | ComplexValue LHS = Result; |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 3754 | APFloat &LHS_r = LHS.getComplexFloatReal(); |
| 3755 | APFloat &LHS_i = LHS.getComplexFloatImag(); |
| 3756 | APFloat &RHS_r = RHS.getComplexFloatReal(); |
| 3757 | APFloat &RHS_i = RHS.getComplexFloatImag(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3758 | |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 3759 | APFloat Tmp = LHS_r; |
| 3760 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 3761 | Result.getComplexFloatReal() = Tmp; |
| 3762 | Tmp = LHS_i; |
| 3763 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 3764 | Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven); |
| 3765 | |
| 3766 | Tmp = LHS_r; |
| 3767 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 3768 | Result.getComplexFloatImag() = Tmp; |
| 3769 | Tmp = LHS_i; |
| 3770 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 3771 | Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven); |
| 3772 | } else { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 3773 | ComplexValue LHS = Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3774 | Result.getComplexIntReal() = |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 3775 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() - |
| 3776 | LHS.getComplexIntImag() * RHS.getComplexIntImag()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3777 | Result.getComplexIntImag() = |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 3778 | (LHS.getComplexIntReal() * RHS.getComplexIntImag() + |
| 3779 | LHS.getComplexIntImag() * RHS.getComplexIntReal()); |
| 3780 | } |
| 3781 | break; |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 3782 | case BO_Div: |
| 3783 | if (Result.isComplexFloat()) { |
| 3784 | ComplexValue LHS = Result; |
| 3785 | APFloat &LHS_r = LHS.getComplexFloatReal(); |
| 3786 | APFloat &LHS_i = LHS.getComplexFloatImag(); |
| 3787 | APFloat &RHS_r = RHS.getComplexFloatReal(); |
| 3788 | APFloat &RHS_i = RHS.getComplexFloatImag(); |
| 3789 | APFloat &Res_r = Result.getComplexFloatReal(); |
| 3790 | APFloat &Res_i = Result.getComplexFloatImag(); |
| 3791 | |
| 3792 | APFloat Den = RHS_r; |
| 3793 | Den.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 3794 | APFloat Tmp = RHS_i; |
| 3795 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 3796 | Den.add(Tmp, APFloat::rmNearestTiesToEven); |
| 3797 | |
| 3798 | Res_r = LHS_r; |
| 3799 | Res_r.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 3800 | Tmp = LHS_i; |
| 3801 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 3802 | Res_r.add(Tmp, APFloat::rmNearestTiesToEven); |
| 3803 | Res_r.divide(Den, APFloat::rmNearestTiesToEven); |
| 3804 | |
| 3805 | Res_i = LHS_i; |
| 3806 | Res_i.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 3807 | Tmp = LHS_r; |
| 3808 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 3809 | Res_i.subtract(Tmp, APFloat::rmNearestTiesToEven); |
| 3810 | Res_i.divide(Den, APFloat::rmNearestTiesToEven); |
| 3811 | } else { |
| 3812 | if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0) { |
| 3813 | // FIXME: what about diagnostics? |
| 3814 | return false; |
| 3815 | } |
| 3816 | ComplexValue LHS = Result; |
| 3817 | APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 3818 | RHS.getComplexIntImag() * RHS.getComplexIntImag(); |
| 3819 | Result.getComplexIntReal() = |
| 3820 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 3821 | LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den; |
| 3822 | Result.getComplexIntImag() = |
| 3823 | (LHS.getComplexIntImag() * RHS.getComplexIntReal() - |
| 3824 | LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den; |
| 3825 | } |
| 3826 | break; |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 3827 | } |
| 3828 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 3829 | return true; |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 3830 | } |
| 3831 | |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 3832 | bool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
| 3833 | // Get the operand value into 'Result'. |
| 3834 | if (!Visit(E->getSubExpr())) |
| 3835 | return false; |
| 3836 | |
| 3837 | switch (E->getOpcode()) { |
| 3838 | default: |
| 3839 | // FIXME: what about diagnostics? |
| 3840 | return false; |
| 3841 | case UO_Extension: |
| 3842 | return true; |
| 3843 | case UO_Plus: |
| 3844 | // The result is always just the subexpr. |
| 3845 | return true; |
| 3846 | case UO_Minus: |
| 3847 | if (Result.isComplexFloat()) { |
| 3848 | Result.getComplexFloatReal().changeSign(); |
| 3849 | Result.getComplexFloatImag().changeSign(); |
| 3850 | } |
| 3851 | else { |
| 3852 | Result.getComplexIntReal() = -Result.getComplexIntReal(); |
| 3853 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 3854 | } |
| 3855 | return true; |
| 3856 | case UO_Not: |
| 3857 | if (Result.isComplexFloat()) |
| 3858 | Result.getComplexFloatImag().changeSign(); |
| 3859 | else |
| 3860 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 3861 | return true; |
| 3862 | } |
| 3863 | } |
| 3864 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 3865 | //===----------------------------------------------------------------------===// |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 3866 | // Top level Expr::EvaluateAsRValue method. |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3867 | //===----------------------------------------------------------------------===// |
| 3868 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3869 | static bool Evaluate(CCValue &Result, EvalInfo &Info, const Expr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3870 | // In C, function designators are not lvalues, but we evaluate them as if they |
| 3871 | // are. |
| 3872 | if (E->isGLValue() || E->getType()->isFunctionType()) { |
| 3873 | LValue LV; |
| 3874 | if (!EvaluateLValue(E, LV, Info)) |
| 3875 | return false; |
| 3876 | LV.moveInto(Result); |
| 3877 | } else if (E->getType()->isVectorType()) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 3878 | if (!EvaluateVector(E, Result, Info)) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3879 | return false; |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3880 | } else if (E->getType()->isIntegralOrEnumerationType()) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 3881 | if (!IntExprEvaluator(Info, Result).Visit(E)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 3882 | return false; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3883 | } else if (E->getType()->hasPointerRepresentation()) { |
| 3884 | LValue LV; |
| 3885 | if (!EvaluatePointer(E, LV, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 3886 | return false; |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 3887 | LV.moveInto(Result); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3888 | } else if (E->getType()->isRealFloatingType()) { |
| 3889 | llvm::APFloat F(0.0); |
| 3890 | if (!EvaluateFloat(E, F, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 3891 | return false; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3892 | Result = CCValue(F); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3893 | } else if (E->getType()->isAnyComplexType()) { |
| 3894 | ComplexValue C; |
| 3895 | if (!EvaluateComplex(E, C, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 3896 | return false; |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 3897 | C.moveInto(Result); |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 3898 | } else if (E->getType()->isMemberPointerType()) { |
| 3899 | // FIXME: Implement evaluation of pointer-to-member types. |
| 3900 | return false; |
| 3901 | } else if (E->getType()->isArrayType() && E->getType()->isLiteralType()) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3902 | LValue LV; |
| 3903 | LV.setExpr(E, Info.CurrentCall); |
| 3904 | if (!EvaluateArray(E, LV, Info.CurrentCall->Temporaries[E], Info)) |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3905 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3906 | Result = Info.CurrentCall->Temporaries[E]; |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 3907 | } else if (E->getType()->isRecordType() && E->getType()->isLiteralType()) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3908 | LValue LV; |
| 3909 | LV.setExpr(E, Info.CurrentCall); |
| 3910 | if (!EvaluateRecord(E, LV, Info.CurrentCall->Temporaries[E], Info)) |
| 3911 | return false; |
| 3912 | Result = Info.CurrentCall->Temporaries[E]; |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 3913 | } else |
Anders Carlsson | 9d4c157 | 2008-11-22 22:56:32 +0000 | [diff] [blame] | 3914 | return false; |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 3915 | |
Anders Carlsson | 5b45d4e | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 3916 | return true; |
| 3917 | } |
| 3918 | |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 3919 | /// EvaluateConstantExpression - Evaluate an expression as a constant expression |
| 3920 | /// in-place in an APValue. In some cases, the in-place evaluation is essential, |
| 3921 | /// since later initializers for an object can indirectly refer to subobjects |
| 3922 | /// which were initialized earlier. |
| 3923 | static bool EvaluateConstantExpression(APValue &Result, EvalInfo &Info, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3924 | const LValue &This, const Expr *E) { |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 3925 | if (E->isRValue() && E->getType()->isLiteralType()) { |
| 3926 | // Evaluate arrays and record types in-place, so that later initializers can |
| 3927 | // refer to earlier-initialized members of the object. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3928 | if (E->getType()->isArrayType()) |
| 3929 | return EvaluateArray(E, This, Result, Info); |
| 3930 | else if (E->getType()->isRecordType()) |
| 3931 | return EvaluateRecord(E, This, Result, Info); |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 3932 | } |
| 3933 | |
| 3934 | // For any other type, in-place evaluation is unimportant. |
| 3935 | CCValue CoreConstResult; |
| 3936 | return Evaluate(CoreConstResult, Info, E) && |
| 3937 | CheckConstantExpression(CoreConstResult, Result); |
| 3938 | } |
| 3939 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3940 | |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 3941 | /// EvaluateAsRValue - Return true if this is a constant which we can fold using |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3942 | /// any crazy technique (that has nothing to do with language standards) that |
| 3943 | /// we want to. If this function returns true, it returns the folded constant |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3944 | /// in Result. If this expression is a glvalue, an lvalue-to-rvalue conversion |
| 3945 | /// will be applied to the result. |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 3946 | bool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx) const { |
Richard Smith | 1445bba | 2011-11-10 03:30:42 +0000 | [diff] [blame] | 3947 | // FIXME: Evaluating initializers for large arrays can cause performance |
| 3948 | // problems, and we don't use such values yet. Once we have a more efficient |
| 3949 | // array representation, this should be reinstated, and used by CodeGen. |
| 3950 | if (isRValue() && getType()->isArrayType()) |
| 3951 | return false; |
| 3952 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3953 | EvalInfo Info(Ctx, Result); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3954 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3955 | // FIXME: If this is the initializer for an lvalue, pass that in. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3956 | CCValue Value; |
| 3957 | if (!::Evaluate(Value, Info, this)) |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3958 | return false; |
| 3959 | |
| 3960 | if (isGLValue()) { |
| 3961 | LValue LV; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3962 | LV.setFrom(Value); |
| 3963 | if (!HandleLValueToRValueConversion(Info, getType(), LV, Value)) |
| 3964 | return false; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3965 | } |
| 3966 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3967 | // Check this core constant expression is a constant expression, and if so, |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 3968 | // convert it to one. |
| 3969 | return CheckConstantExpression(Value, Result.Val); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3970 | } |
| 3971 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3972 | bool Expr::EvaluateAsBooleanCondition(bool &Result, |
| 3973 | const ASTContext &Ctx) const { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3974 | EvalResult Scratch; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 3975 | return EvaluateAsRValue(Scratch, Ctx) && |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 3976 | HandleConversionToBool(CCValue(Scratch.Val, CCValue::GlobalValue()), |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3977 | Result); |
John McCall | cd7a445 | 2010-01-05 23:42:56 +0000 | [diff] [blame] | 3978 | } |
| 3979 | |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3980 | bool Expr::EvaluateAsInt(APSInt &Result, const ASTContext &Ctx) const { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3981 | EvalResult ExprResult; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 3982 | if (!EvaluateAsRValue(ExprResult, Ctx) || ExprResult.HasSideEffects || |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3983 | !ExprResult.Val.isInt()) { |
| 3984 | return false; |
| 3985 | } |
| 3986 | Result = ExprResult.Val.getInt(); |
| 3987 | return true; |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3988 | } |
| 3989 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3990 | bool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx) const { |
Anders Carlsson | 1b78276 | 2009-04-10 04:54:13 +0000 | [diff] [blame] | 3991 | EvalInfo Info(Ctx, Result); |
| 3992 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3993 | LValue LV; |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 3994 | return EvaluateLValue(this, LV, Info) && !Result.HasSideEffects && |
| 3995 | CheckLValueConstantExpression(LV, Result.Val); |
Eli Friedman | b2f295c | 2009-09-13 10:17:44 +0000 | [diff] [blame] | 3996 | } |
| 3997 | |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 3998 | /// isEvaluatable - Call EvaluateAsRValue to see if this expression can be |
| 3999 | /// constant folded, but discard the result. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4000 | bool Expr::isEvaluatable(const ASTContext &Ctx) const { |
Anders Carlsson | 4fdfb09 | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 4001 | EvalResult Result; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 4002 | return EvaluateAsRValue(Result, Ctx) && !Result.HasSideEffects; |
Chris Lattner | 45b6b9d | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 4003 | } |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 4004 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4005 | bool Expr::HasSideEffects(const ASTContext &Ctx) const { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 4006 | return HasSideEffect(Ctx).Visit(this); |
Fariborz Jahanian | 393c247 | 2009-11-05 18:03:03 +0000 | [diff] [blame] | 4007 | } |
| 4008 | |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4009 | APSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx) const { |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 4010 | EvalResult EvalResult; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 4011 | bool Result = EvaluateAsRValue(EvalResult, Ctx); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 4012 | (void)Result; |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 4013 | assert(Result && "Could not evaluate expression"); |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 4014 | assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer"); |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 4015 | |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 4016 | return EvalResult.Val.getInt(); |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 4017 | } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4018 | |
Abramo Bagnara | e17a643 | 2010-05-14 17:07:14 +0000 | [diff] [blame] | 4019 | bool Expr::EvalResult::isGlobalLValue() const { |
| 4020 | assert(Val.isLValue()); |
| 4021 | return IsGlobalLValue(Val.getLValueBase()); |
| 4022 | } |
| 4023 | |
| 4024 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4025 | /// isIntegerConstantExpr - this recursive routine will test if an expression is |
| 4026 | /// an integer constant expression. |
| 4027 | |
| 4028 | /// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, |
| 4029 | /// comma, etc |
| 4030 | /// |
| 4031 | /// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof |
| 4032 | /// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer |
| 4033 | /// cast+dereference. |
| 4034 | |
| 4035 | // CheckICE - This function does the fundamental ICE checking: the returned |
| 4036 | // ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation. |
| 4037 | // Note that to reduce code duplication, this helper does no evaluation |
| 4038 | // itself; the caller checks whether the expression is evaluatable, and |
| 4039 | // in the rare cases where CheckICE actually cares about the evaluated |
| 4040 | // value, it calls into Evalute. |
| 4041 | // |
| 4042 | // Meanings of Val: |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 4043 | // 0: This expression is an ICE. |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4044 | // 1: This expression is not an ICE, but if it isn't evaluated, it's |
| 4045 | // a legal subexpression for an ICE. This return value is used to handle |
| 4046 | // the comma operator in C99 mode. |
| 4047 | // 2: This expression is not an ICE, and is not a legal subexpression for one. |
| 4048 | |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 4049 | namespace { |
| 4050 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4051 | struct ICEDiag { |
| 4052 | unsigned Val; |
| 4053 | SourceLocation Loc; |
| 4054 | |
| 4055 | public: |
| 4056 | ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {} |
| 4057 | ICEDiag() : Val(0) {} |
| 4058 | }; |
| 4059 | |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 4060 | } |
| 4061 | |
| 4062 | static ICEDiag NoDiag() { return ICEDiag(); } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4063 | |
| 4064 | static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) { |
| 4065 | Expr::EvalResult EVResult; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 4066 | if (!E->EvaluateAsRValue(EVResult, Ctx) || EVResult.HasSideEffects || |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4067 | !EVResult.Val.isInt()) { |
| 4068 | return ICEDiag(2, E->getLocStart()); |
| 4069 | } |
| 4070 | return NoDiag(); |
| 4071 | } |
| 4072 | |
| 4073 | static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) { |
| 4074 | assert(!E->isValueDependent() && "Should not see value dependent exprs!"); |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 4075 | if (!E->getType()->isIntegralOrEnumerationType()) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4076 | return ICEDiag(2, E->getLocStart()); |
| 4077 | } |
| 4078 | |
| 4079 | switch (E->getStmtClass()) { |
John McCall | 63c00d7 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 4080 | #define ABSTRACT_STMT(Node) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4081 | #define STMT(Node, Base) case Expr::Node##Class: |
| 4082 | #define EXPR(Node, Base) |
| 4083 | #include "clang/AST/StmtNodes.inc" |
| 4084 | case Expr::PredefinedExprClass: |
| 4085 | case Expr::FloatingLiteralClass: |
| 4086 | case Expr::ImaginaryLiteralClass: |
| 4087 | case Expr::StringLiteralClass: |
| 4088 | case Expr::ArraySubscriptExprClass: |
| 4089 | case Expr::MemberExprClass: |
| 4090 | case Expr::CompoundAssignOperatorClass: |
| 4091 | case Expr::CompoundLiteralExprClass: |
| 4092 | case Expr::ExtVectorElementExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4093 | case Expr::DesignatedInitExprClass: |
| 4094 | case Expr::ImplicitValueInitExprClass: |
| 4095 | case Expr::ParenListExprClass: |
| 4096 | case Expr::VAArgExprClass: |
| 4097 | case Expr::AddrLabelExprClass: |
| 4098 | case Expr::StmtExprClass: |
| 4099 | case Expr::CXXMemberCallExprClass: |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4100 | case Expr::CUDAKernelCallExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4101 | case Expr::CXXDynamicCastExprClass: |
| 4102 | case Expr::CXXTypeidExprClass: |
Francois Pichet | 9be8840 | 2010-09-08 23:47:05 +0000 | [diff] [blame] | 4103 | case Expr::CXXUuidofExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4104 | case Expr::CXXNullPtrLiteralExprClass: |
| 4105 | case Expr::CXXThisExprClass: |
| 4106 | case Expr::CXXThrowExprClass: |
| 4107 | case Expr::CXXNewExprClass: |
| 4108 | case Expr::CXXDeleteExprClass: |
| 4109 | case Expr::CXXPseudoDestructorExprClass: |
| 4110 | case Expr::UnresolvedLookupExprClass: |
| 4111 | case Expr::DependentScopeDeclRefExprClass: |
| 4112 | case Expr::CXXConstructExprClass: |
| 4113 | case Expr::CXXBindTemporaryExprClass: |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 4114 | case Expr::ExprWithCleanupsClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4115 | case Expr::CXXTemporaryObjectExprClass: |
| 4116 | case Expr::CXXUnresolvedConstructExprClass: |
| 4117 | case Expr::CXXDependentScopeMemberExprClass: |
| 4118 | case Expr::UnresolvedMemberExprClass: |
| 4119 | case Expr::ObjCStringLiteralClass: |
| 4120 | case Expr::ObjCEncodeExprClass: |
| 4121 | case Expr::ObjCMessageExprClass: |
| 4122 | case Expr::ObjCSelectorExprClass: |
| 4123 | case Expr::ObjCProtocolExprClass: |
| 4124 | case Expr::ObjCIvarRefExprClass: |
| 4125 | case Expr::ObjCPropertyRefExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4126 | case Expr::ObjCIsaExprClass: |
| 4127 | case Expr::ShuffleVectorExprClass: |
| 4128 | case Expr::BlockExprClass: |
| 4129 | case Expr::BlockDeclRefExprClass: |
| 4130 | case Expr::NoStmtClass: |
John McCall | 7cd7d1a | 2010-11-15 23:31:06 +0000 | [diff] [blame] | 4131 | case Expr::OpaqueValueExprClass: |
Douglas Gregor | be230c3 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 4132 | case Expr::PackExpansionExprClass: |
Douglas Gregor | c7793c7 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 4133 | case Expr::SubstNonTypeTemplateParmPackExprClass: |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 4134 | case Expr::AsTypeExprClass: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4135 | case Expr::ObjCIndirectCopyRestoreExprClass: |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 4136 | case Expr::MaterializeTemporaryExprClass: |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4137 | case Expr::PseudoObjectExprClass: |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 4138 | case Expr::AtomicExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4139 | return ICEDiag(2, E->getLocStart()); |
| 4140 | |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 4141 | case Expr::InitListExprClass: |
| 4142 | if (Ctx.getLangOptions().CPlusPlus0x) { |
| 4143 | const InitListExpr *ILE = cast<InitListExpr>(E); |
| 4144 | if (ILE->getNumInits() == 0) |
| 4145 | return NoDiag(); |
| 4146 | if (ILE->getNumInits() == 1) |
| 4147 | return CheckICE(ILE->getInit(0), Ctx); |
| 4148 | // Fall through for more than 1 expression. |
| 4149 | } |
| 4150 | return ICEDiag(2, E->getLocStart()); |
| 4151 | |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 4152 | case Expr::SizeOfPackExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4153 | case Expr::GNUNullExprClass: |
| 4154 | // GCC considers the GNU __null value to be an integral constant expression. |
| 4155 | return NoDiag(); |
| 4156 | |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4157 | case Expr::SubstNonTypeTemplateParmExprClass: |
| 4158 | return |
| 4159 | CheckICE(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(), Ctx); |
| 4160 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4161 | case Expr::ParenExprClass: |
| 4162 | return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx); |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 4163 | case Expr::GenericSelectionExprClass: |
| 4164 | return CheckICE(cast<GenericSelectionExpr>(E)->getResultExpr(), Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4165 | case Expr::IntegerLiteralClass: |
| 4166 | case Expr::CharacterLiteralClass: |
| 4167 | case Expr::CXXBoolLiteralExprClass: |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 4168 | case Expr::CXXScalarValueInitExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4169 | case Expr::UnaryTypeTraitExprClass: |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 4170 | case Expr::BinaryTypeTraitExprClass: |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 4171 | case Expr::ArrayTypeTraitExprClass: |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 4172 | case Expr::ExpressionTraitExprClass: |
Sebastian Redl | 2e15622 | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 4173 | case Expr::CXXNoexceptExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4174 | return NoDiag(); |
| 4175 | case Expr::CallExprClass: |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 4176 | case Expr::CXXOperatorCallExprClass: { |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 4177 | // C99 6.6/3 allows function calls within unevaluated subexpressions of |
| 4178 | // constant expressions, but they can never be ICEs because an ICE cannot |
| 4179 | // contain an operand of (pointer to) function type. |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4180 | const CallExpr *CE = cast<CallExpr>(E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4181 | if (CE->isBuiltinCall()) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4182 | return CheckEvalInICE(E, Ctx); |
| 4183 | return ICEDiag(2, E->getLocStart()); |
| 4184 | } |
| 4185 | case Expr::DeclRefExprClass: |
| 4186 | if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl())) |
| 4187 | return NoDiag(); |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 4188 | if (Ctx.getLangOptions().CPlusPlus && IsConstNonVolatile(E->getType())) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4189 | const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl(); |
| 4190 | |
| 4191 | // Parameter variables are never constants. Without this check, |
| 4192 | // getAnyInitializer() can find a default argument, which leads |
| 4193 | // to chaos. |
| 4194 | if (isa<ParmVarDecl>(D)) |
| 4195 | return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); |
| 4196 | |
| 4197 | // C++ 7.1.5.1p2 |
| 4198 | // A variable of non-volatile const-qualified integral or enumeration |
| 4199 | // type initialized by an ICE can be used in ICEs. |
| 4200 | if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) { |
Richard Smith | db1822c | 2011-11-08 01:31:09 +0000 | [diff] [blame] | 4201 | if (!Dcl->getType()->isIntegralOrEnumerationType()) |
| 4202 | return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); |
| 4203 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4204 | // Look for a declaration of this variable that has an initializer. |
| 4205 | const VarDecl *ID = 0; |
| 4206 | const Expr *Init = Dcl->getAnyInitializer(ID); |
| 4207 | if (Init) { |
| 4208 | if (ID->isInitKnownICE()) { |
| 4209 | // We have already checked whether this subexpression is an |
| 4210 | // integral constant expression. |
| 4211 | if (ID->isInitICE()) |
| 4212 | return NoDiag(); |
| 4213 | else |
| 4214 | return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); |
| 4215 | } |
| 4216 | |
| 4217 | // It's an ICE whether or not the definition we found is |
| 4218 | // out-of-line. See DR 721 and the discussion in Clang PR |
| 4219 | // 6206 for details. |
| 4220 | |
| 4221 | if (Dcl->isCheckingICE()) { |
| 4222 | return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); |
| 4223 | } |
| 4224 | |
| 4225 | Dcl->setCheckingICE(); |
| 4226 | ICEDiag Result = CheckICE(Init, Ctx); |
| 4227 | // Cache the result of the ICE test. |
| 4228 | Dcl->setInitKnownICE(Result.Val == 0); |
| 4229 | return Result; |
| 4230 | } |
| 4231 | } |
| 4232 | } |
| 4233 | return ICEDiag(2, E->getLocStart()); |
| 4234 | case Expr::UnaryOperatorClass: { |
| 4235 | const UnaryOperator *Exp = cast<UnaryOperator>(E); |
| 4236 | switch (Exp->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4237 | case UO_PostInc: |
| 4238 | case UO_PostDec: |
| 4239 | case UO_PreInc: |
| 4240 | case UO_PreDec: |
| 4241 | case UO_AddrOf: |
| 4242 | case UO_Deref: |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 4243 | // C99 6.6/3 allows increment and decrement within unevaluated |
| 4244 | // subexpressions of constant expressions, but they can never be ICEs |
| 4245 | // because an ICE cannot contain an lvalue operand. |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4246 | return ICEDiag(2, E->getLocStart()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4247 | case UO_Extension: |
| 4248 | case UO_LNot: |
| 4249 | case UO_Plus: |
| 4250 | case UO_Minus: |
| 4251 | case UO_Not: |
| 4252 | case UO_Real: |
| 4253 | case UO_Imag: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4254 | return CheckICE(Exp->getSubExpr(), Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4255 | } |
| 4256 | |
| 4257 | // OffsetOf falls through here. |
| 4258 | } |
| 4259 | case Expr::OffsetOfExprClass: { |
| 4260 | // Note that per C99, offsetof must be an ICE. And AFAIK, using |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 4261 | // EvaluateAsRValue matches the proposed gcc behavior for cases like |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 4262 | // "offsetof(struct s{int x[4];}, x[1.0])". This doesn't affect |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4263 | // compliance: we should warn earlier for offsetof expressions with |
| 4264 | // array subscripts that aren't ICEs, and if the array subscripts |
| 4265 | // are ICEs, the value of the offsetof must be an integer constant. |
| 4266 | return CheckEvalInICE(E, Ctx); |
| 4267 | } |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 4268 | case Expr::UnaryExprOrTypeTraitExprClass: { |
| 4269 | const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E); |
| 4270 | if ((Exp->getKind() == UETT_SizeOf) && |
| 4271 | Exp->getTypeOfArgument()->isVariableArrayType()) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4272 | return ICEDiag(2, E->getLocStart()); |
| 4273 | return NoDiag(); |
| 4274 | } |
| 4275 | case Expr::BinaryOperatorClass: { |
| 4276 | const BinaryOperator *Exp = cast<BinaryOperator>(E); |
| 4277 | switch (Exp->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4278 | case BO_PtrMemD: |
| 4279 | case BO_PtrMemI: |
| 4280 | case BO_Assign: |
| 4281 | case BO_MulAssign: |
| 4282 | case BO_DivAssign: |
| 4283 | case BO_RemAssign: |
| 4284 | case BO_AddAssign: |
| 4285 | case BO_SubAssign: |
| 4286 | case BO_ShlAssign: |
| 4287 | case BO_ShrAssign: |
| 4288 | case BO_AndAssign: |
| 4289 | case BO_XorAssign: |
| 4290 | case BO_OrAssign: |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 4291 | // C99 6.6/3 allows assignments within unevaluated subexpressions of |
| 4292 | // constant expressions, but they can never be ICEs because an ICE cannot |
| 4293 | // contain an lvalue operand. |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4294 | return ICEDiag(2, E->getLocStart()); |
| 4295 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4296 | case BO_Mul: |
| 4297 | case BO_Div: |
| 4298 | case BO_Rem: |
| 4299 | case BO_Add: |
| 4300 | case BO_Sub: |
| 4301 | case BO_Shl: |
| 4302 | case BO_Shr: |
| 4303 | case BO_LT: |
| 4304 | case BO_GT: |
| 4305 | case BO_LE: |
| 4306 | case BO_GE: |
| 4307 | case BO_EQ: |
| 4308 | case BO_NE: |
| 4309 | case BO_And: |
| 4310 | case BO_Xor: |
| 4311 | case BO_Or: |
| 4312 | case BO_Comma: { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4313 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 4314 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4315 | if (Exp->getOpcode() == BO_Div || |
| 4316 | Exp->getOpcode() == BO_Rem) { |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 4317 | // EvaluateAsRValue gives an error for undefined Div/Rem, so make sure |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4318 | // we don't evaluate one. |
John McCall | 3b332ab | 2011-02-26 08:27:17 +0000 | [diff] [blame] | 4319 | if (LHSResult.Val == 0 && RHSResult.Val == 0) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4320 | llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4321 | if (REval == 0) |
| 4322 | return ICEDiag(1, E->getLocStart()); |
| 4323 | if (REval.isSigned() && REval.isAllOnesValue()) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4324 | llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4325 | if (LEval.isMinSignedValue()) |
| 4326 | return ICEDiag(1, E->getLocStart()); |
| 4327 | } |
| 4328 | } |
| 4329 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4330 | if (Exp->getOpcode() == BO_Comma) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4331 | if (Ctx.getLangOptions().C99) { |
| 4332 | // C99 6.6p3 introduces a strange edge case: comma can be in an ICE |
| 4333 | // if it isn't evaluated. |
| 4334 | if (LHSResult.Val == 0 && RHSResult.Val == 0) |
| 4335 | return ICEDiag(1, E->getLocStart()); |
| 4336 | } else { |
| 4337 | // In both C89 and C++, commas in ICEs are illegal. |
| 4338 | return ICEDiag(2, E->getLocStart()); |
| 4339 | } |
| 4340 | } |
| 4341 | if (LHSResult.Val >= RHSResult.Val) |
| 4342 | return LHSResult; |
| 4343 | return RHSResult; |
| 4344 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4345 | case BO_LAnd: |
| 4346 | case BO_LOr: { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4347 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
Douglas Gregor | 63fe681 | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 4348 | |
| 4349 | // C++0x [expr.const]p2: |
| 4350 | // [...] subexpressions of logical AND (5.14), logical OR |
| 4351 | // (5.15), and condi- tional (5.16) operations that are not |
| 4352 | // evaluated are not considered. |
| 4353 | if (Ctx.getLangOptions().CPlusPlus0x && LHSResult.Val == 0) { |
| 4354 | if (Exp->getOpcode() == BO_LAnd && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4355 | Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0) |
Douglas Gregor | 63fe681 | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 4356 | return LHSResult; |
| 4357 | |
| 4358 | if (Exp->getOpcode() == BO_LOr && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4359 | Exp->getLHS()->EvaluateKnownConstInt(Ctx) != 0) |
Douglas Gregor | 63fe681 | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 4360 | return LHSResult; |
| 4361 | } |
| 4362 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4363 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
| 4364 | if (LHSResult.Val == 0 && RHSResult.Val == 1) { |
| 4365 | // Rare case where the RHS has a comma "side-effect"; we need |
| 4366 | // to actually check the condition to see whether the side |
| 4367 | // with the comma is evaluated. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4368 | if ((Exp->getOpcode() == BO_LAnd) != |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4369 | (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0)) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4370 | return RHSResult; |
| 4371 | return NoDiag(); |
| 4372 | } |
| 4373 | |
| 4374 | if (LHSResult.Val >= RHSResult.Val) |
| 4375 | return LHSResult; |
| 4376 | return RHSResult; |
| 4377 | } |
| 4378 | } |
| 4379 | } |
| 4380 | case Expr::ImplicitCastExprClass: |
| 4381 | case Expr::CStyleCastExprClass: |
| 4382 | case Expr::CXXFunctionalCastExprClass: |
| 4383 | case Expr::CXXStaticCastExprClass: |
| 4384 | case Expr::CXXReinterpretCastExprClass: |
Richard Smith | 32cb471 | 2011-10-24 18:26:35 +0000 | [diff] [blame] | 4385 | case Expr::CXXConstCastExprClass: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4386 | case Expr::ObjCBridgedCastExprClass: { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4387 | const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr(); |
Richard Smith | 98326ed | 2011-10-25 00:21:54 +0000 | [diff] [blame] | 4388 | if (isa<ExplicitCastExpr>(E) && |
Richard Smith | 32cb471 | 2011-10-24 18:26:35 +0000 | [diff] [blame] | 4389 | isa<FloatingLiteral>(SubExpr->IgnoreParenImpCasts())) |
| 4390 | return NoDiag(); |
Eli Friedman | eea0e81 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 4391 | switch (cast<CastExpr>(E)->getCastKind()) { |
| 4392 | case CK_LValueToRValue: |
| 4393 | case CK_NoOp: |
| 4394 | case CK_IntegralToBoolean: |
| 4395 | case CK_IntegralCast: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4396 | return CheckICE(SubExpr, Ctx); |
Eli Friedman | eea0e81 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 4397 | default: |
Eli Friedman | eea0e81 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 4398 | return ICEDiag(2, E->getLocStart()); |
| 4399 | } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4400 | } |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4401 | case Expr::BinaryConditionalOperatorClass: { |
| 4402 | const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E); |
| 4403 | ICEDiag CommonResult = CheckICE(Exp->getCommon(), Ctx); |
| 4404 | if (CommonResult.Val == 2) return CommonResult; |
| 4405 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
| 4406 | if (FalseResult.Val == 2) return FalseResult; |
| 4407 | if (CommonResult.Val == 1) return CommonResult; |
| 4408 | if (FalseResult.Val == 1 && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4409 | Exp->getCommon()->EvaluateKnownConstInt(Ctx) == 0) return NoDiag(); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4410 | return FalseResult; |
| 4411 | } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4412 | case Expr::ConditionalOperatorClass: { |
| 4413 | const ConditionalOperator *Exp = cast<ConditionalOperator>(E); |
| 4414 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
| 4415 | // then only the true side is actually considered in an integer constant |
| 4416 | // expression, and it is fully evaluated. This is an important GNU |
| 4417 | // extension. See GCC PR38377 for discussion. |
| 4418 | if (const CallExpr *CallCE |
| 4419 | = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts())) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4420 | if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4421 | Expr::EvalResult EVResult; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 4422 | if (!E->EvaluateAsRValue(EVResult, Ctx) || EVResult.HasSideEffects || |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4423 | !EVResult.Val.isInt()) { |
| 4424 | return ICEDiag(2, E->getLocStart()); |
| 4425 | } |
| 4426 | return NoDiag(); |
| 4427 | } |
| 4428 | ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4429 | if (CondResult.Val == 2) |
| 4430 | return CondResult; |
Douglas Gregor | 63fe681 | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 4431 | |
| 4432 | // C++0x [expr.const]p2: |
| 4433 | // subexpressions of [...] conditional (5.16) operations that |
| 4434 | // are not evaluated are not considered |
| 4435 | bool TrueBranch = Ctx.getLangOptions().CPlusPlus0x |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4436 | ? Exp->getCond()->EvaluateKnownConstInt(Ctx) != 0 |
Douglas Gregor | 63fe681 | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 4437 | : false; |
| 4438 | ICEDiag TrueResult = NoDiag(); |
| 4439 | if (!Ctx.getLangOptions().CPlusPlus0x || TrueBranch) |
| 4440 | TrueResult = CheckICE(Exp->getTrueExpr(), Ctx); |
| 4441 | ICEDiag FalseResult = NoDiag(); |
| 4442 | if (!Ctx.getLangOptions().CPlusPlus0x || !TrueBranch) |
| 4443 | FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
| 4444 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4445 | if (TrueResult.Val == 2) |
| 4446 | return TrueResult; |
| 4447 | if (FalseResult.Val == 2) |
| 4448 | return FalseResult; |
| 4449 | if (CondResult.Val == 1) |
| 4450 | return CondResult; |
| 4451 | if (TrueResult.Val == 0 && FalseResult.Val == 0) |
| 4452 | return NoDiag(); |
| 4453 | // Rare case where the diagnostics depend on which side is evaluated |
| 4454 | // Note that if we get here, CondResult is 0, and at least one of |
| 4455 | // TrueResult and FalseResult is non-zero. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4456 | if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4457 | return FalseResult; |
| 4458 | } |
| 4459 | return TrueResult; |
| 4460 | } |
| 4461 | case Expr::CXXDefaultArgExprClass: |
| 4462 | return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx); |
| 4463 | case Expr::ChooseExprClass: { |
| 4464 | return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx); |
| 4465 | } |
| 4466 | } |
| 4467 | |
| 4468 | // Silence a GCC warning |
| 4469 | return ICEDiag(2, E->getLocStart()); |
| 4470 | } |
| 4471 | |
| 4472 | bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx, |
| 4473 | SourceLocation *Loc, bool isEvaluated) const { |
| 4474 | ICEDiag d = CheckICE(this, Ctx); |
| 4475 | if (d.Val != 0) { |
| 4476 | if (Loc) *Loc = d.Loc; |
| 4477 | return false; |
| 4478 | } |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4479 | if (!EvaluateAsInt(Result, Ctx)) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4480 | llvm_unreachable("ICE cannot be evaluated!"); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4481 | return true; |
| 4482 | } |