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