Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1 | //===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===// |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Expr constant evaluator. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/APValue.h" |
| 15 | #include "clang/AST/ASTContext.h" |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 16 | #include "clang/AST/CharUnits.h" |
Anders Carlsson | 19cc4ab | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 17 | #include "clang/AST/RecordLayout.h" |
Seo Sanghyeon | 0fe52e1 | 2008-07-08 07:23:12 +0000 | [diff] [blame] | 18 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeLoc.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTDiagnostic.h" |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 21 | #include "clang/AST/Expr.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 22 | #include "clang/Basic/Builtins.h" |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 23 | #include "clang/Basic/TargetInfo.h" |
Mike Stump | 7462b39 | 2009-05-30 14:43:18 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallString.h" |
Mike Stump | 4572bab | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 25 | #include <cstring> |
| 26 | |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 27 | using namespace clang; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 28 | using llvm::APSInt; |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 29 | using llvm::APFloat; |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 30 | |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 31 | /// EvalInfo - This is a private struct used by the evaluator to capture |
| 32 | /// information about a subexpression as it is folded. It retains information |
| 33 | /// about the AST context, but also maintains information about the folded |
| 34 | /// expression. |
| 35 | /// |
| 36 | /// If an expression could be evaluated, it is still possible it is not a C |
| 37 | /// "integer constant expression" or constant expression. If not, this struct |
| 38 | /// captures information about how and why not. |
| 39 | /// |
| 40 | /// One bit of information passed *into* the request for constant folding |
| 41 | /// indicates whether the subexpression is "evaluated" or not according to C |
| 42 | /// rules. For example, the RHS of (0 && foo()) is not evaluated. We can |
| 43 | /// evaluate the expression regardless of what the RHS is, but C only allows |
| 44 | /// certain things in certain situations. |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 45 | namespace { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 46 | struct LValue; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 47 | struct CallStackFrame; |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 48 | struct EvalInfo; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 49 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 50 | QualType getType(APValue::LValueBase B) { |
| 51 | if (!B) return QualType(); |
| 52 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) |
| 53 | return D->getType(); |
| 54 | return B.get<const Expr*>()->getType(); |
| 55 | } |
| 56 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 57 | /// Get an LValue path entry, which is known to not be an array index, as a |
| 58 | /// field declaration. |
| 59 | const FieldDecl *getAsField(APValue::LValuePathEntry E) { |
| 60 | APValue::BaseOrMemberType Value; |
| 61 | Value.setFromOpaqueValue(E.BaseOrMember); |
| 62 | return dyn_cast<FieldDecl>(Value.getPointer()); |
| 63 | } |
| 64 | /// Get an LValue path entry, which is known to not be an array index, as a |
| 65 | /// base class declaration. |
| 66 | const CXXRecordDecl *getAsBaseClass(APValue::LValuePathEntry E) { |
| 67 | APValue::BaseOrMemberType Value; |
| 68 | Value.setFromOpaqueValue(E.BaseOrMember); |
| 69 | return dyn_cast<CXXRecordDecl>(Value.getPointer()); |
| 70 | } |
| 71 | /// Determine whether this LValue path entry for a base class names a virtual |
| 72 | /// base class. |
| 73 | bool isVirtualBaseClass(APValue::LValuePathEntry E) { |
| 74 | APValue::BaseOrMemberType Value; |
| 75 | Value.setFromOpaqueValue(E.BaseOrMember); |
| 76 | return Value.getInt(); |
| 77 | } |
| 78 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 79 | /// Determine whether the described subobject is an array element. |
| 80 | static bool SubobjectIsArrayElement(QualType Base, |
| 81 | ArrayRef<APValue::LValuePathEntry> Path) { |
| 82 | bool IsArrayElement = false; |
| 83 | const Type *T = Base.getTypePtr(); |
| 84 | for (unsigned I = 0, N = Path.size(); I != N; ++I) { |
| 85 | IsArrayElement = T && T->isArrayType(); |
| 86 | if (IsArrayElement) |
| 87 | T = T->getBaseElementTypeUnsafe(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 88 | else if (const FieldDecl *FD = getAsField(Path[I])) |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 89 | T = FD->getType().getTypePtr(); |
| 90 | else |
| 91 | // Path[I] describes a base class. |
| 92 | T = 0; |
| 93 | } |
| 94 | return IsArrayElement; |
| 95 | } |
| 96 | |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 97 | /// A path from a glvalue to a subobject of that glvalue. |
| 98 | struct SubobjectDesignator { |
| 99 | /// True if the subobject was named in a manner not supported by C++11. Such |
| 100 | /// lvalues can still be folded, but they are not core constant expressions |
| 101 | /// and we cannot perform lvalue-to-rvalue conversions on them. |
| 102 | bool Invalid : 1; |
| 103 | |
| 104 | /// Whether this designates an array element. |
| 105 | bool ArrayElement : 1; |
| 106 | |
| 107 | /// Whether this designates 'one past the end' of the current subobject. |
| 108 | bool OnePastTheEnd : 1; |
| 109 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 110 | typedef APValue::LValuePathEntry PathEntry; |
| 111 | |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 112 | /// The entries on the path from the glvalue to the designated subobject. |
| 113 | SmallVector<PathEntry, 8> Entries; |
| 114 | |
| 115 | SubobjectDesignator() : |
| 116 | Invalid(false), ArrayElement(false), OnePastTheEnd(false) {} |
| 117 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 118 | SubobjectDesignator(const APValue &V) : |
| 119 | Invalid(!V.isLValue() || !V.hasLValuePath()), ArrayElement(false), |
| 120 | OnePastTheEnd(false) { |
| 121 | if (!Invalid) { |
| 122 | ArrayRef<PathEntry> VEntries = V.getLValuePath(); |
| 123 | Entries.insert(Entries.end(), VEntries.begin(), VEntries.end()); |
| 124 | if (V.getLValueBase()) |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 125 | ArrayElement = SubobjectIsArrayElement(getType(V.getLValueBase()), |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 126 | V.getLValuePath()); |
| 127 | else |
| 128 | assert(V.getLValuePath().empty() &&"Null pointer with nonempty path"); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 129 | OnePastTheEnd = V.isLValueOnePastTheEnd(); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 133 | void setInvalid() { |
| 134 | Invalid = true; |
| 135 | Entries.clear(); |
| 136 | } |
| 137 | /// Update this designator to refer to the given element within this array. |
| 138 | void addIndex(uint64_t N) { |
| 139 | if (Invalid) return; |
| 140 | if (OnePastTheEnd) { |
| 141 | setInvalid(); |
| 142 | return; |
| 143 | } |
| 144 | PathEntry Entry; |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 145 | Entry.ArrayIndex = N; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 146 | Entries.push_back(Entry); |
| 147 | ArrayElement = true; |
| 148 | } |
| 149 | /// Update this designator to refer to the given base or member of this |
| 150 | /// object. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 151 | void addDecl(const Decl *D, bool Virtual = false) { |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 152 | if (Invalid) return; |
| 153 | if (OnePastTheEnd) { |
| 154 | setInvalid(); |
| 155 | return; |
| 156 | } |
| 157 | PathEntry Entry; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 158 | APValue::BaseOrMemberType Value(D, Virtual); |
| 159 | Entry.BaseOrMember = Value.getOpaqueValue(); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 160 | Entries.push_back(Entry); |
| 161 | ArrayElement = false; |
| 162 | } |
| 163 | /// Add N to the address of this subobject. |
| 164 | void adjustIndex(uint64_t N) { |
| 165 | if (Invalid) return; |
| 166 | if (ArrayElement) { |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 167 | // FIXME: Make sure the index stays within bounds, or one past the end. |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 168 | Entries.back().ArrayIndex += N; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 169 | return; |
| 170 | } |
| 171 | if (OnePastTheEnd && N == (uint64_t)-1) |
| 172 | OnePastTheEnd = false; |
| 173 | else if (!OnePastTheEnd && N == 1) |
| 174 | OnePastTheEnd = true; |
| 175 | else if (N != 0) |
| 176 | setInvalid(); |
| 177 | } |
| 178 | }; |
| 179 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 180 | /// A core constant value. This can be the value of any constant expression, |
| 181 | /// or a pointer or reference to a non-static object or function parameter. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 182 | /// |
| 183 | /// For an LValue, the base and offset are stored in the APValue subobject, |
| 184 | /// but the other information is stored in the SubobjectDesignator. For all |
| 185 | /// other value kinds, the value is stored directly in the APValue subobject. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 186 | class CCValue : public APValue { |
| 187 | typedef llvm::APSInt APSInt; |
| 188 | typedef llvm::APFloat APFloat; |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 189 | /// If the value is a reference or pointer into a parameter or temporary, |
| 190 | /// this is the corresponding call stack frame. |
| 191 | CallStackFrame *CallFrame; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 192 | /// If the value is a reference or pointer, this is a description of how the |
| 193 | /// subobject was specified. |
| 194 | SubobjectDesignator Designator; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 195 | public: |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 196 | struct GlobalValue {}; |
| 197 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 198 | CCValue() {} |
| 199 | explicit CCValue(const APSInt &I) : APValue(I) {} |
| 200 | explicit CCValue(const APFloat &F) : APValue(F) {} |
| 201 | CCValue(const APValue *E, unsigned N) : APValue(E, N) {} |
| 202 | CCValue(const APSInt &R, const APSInt &I) : APValue(R, I) {} |
| 203 | CCValue(const APFloat &R, const APFloat &I) : APValue(R, I) {} |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 204 | CCValue(const CCValue &V) : APValue(V), CallFrame(V.CallFrame) {} |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 205 | CCValue(LValueBase B, const CharUnits &O, CallStackFrame *F, |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 206 | const SubobjectDesignator &D) : |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 207 | APValue(B, O, APValue::NoLValuePath()), CallFrame(F), Designator(D) {} |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 208 | CCValue(const APValue &V, GlobalValue) : |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 209 | APValue(V), CallFrame(0), Designator(V) {} |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 210 | CCValue(const ValueDecl *D, bool IsDerivedMember, |
| 211 | ArrayRef<const CXXRecordDecl*> Path) : |
| 212 | APValue(D, IsDerivedMember, Path) {} |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 213 | |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 214 | CallStackFrame *getLValueFrame() const { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 215 | assert(getKind() == LValue); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 216 | return CallFrame; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 217 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 218 | SubobjectDesignator &getLValueDesignator() { |
| 219 | assert(getKind() == LValue); |
| 220 | return Designator; |
| 221 | } |
| 222 | const SubobjectDesignator &getLValueDesignator() const { |
| 223 | return const_cast<CCValue*>(this)->getLValueDesignator(); |
| 224 | } |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 225 | }; |
| 226 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 227 | /// A stack frame in the constexpr call stack. |
| 228 | struct CallStackFrame { |
| 229 | EvalInfo &Info; |
| 230 | |
| 231 | /// Parent - The caller of this stack frame. |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 232 | CallStackFrame *Caller; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 233 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 234 | /// This - The binding for the this pointer in this call, if any. |
| 235 | const LValue *This; |
| 236 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 237 | /// ParmBindings - Parameter bindings for this function call, indexed by |
| 238 | /// parameters' function scope indices. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 239 | const CCValue *Arguments; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 240 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 241 | typedef llvm::DenseMap<const Expr*, CCValue> MapTy; |
| 242 | typedef MapTy::const_iterator temp_iterator; |
| 243 | /// Temporaries - Temporary lvalues materialized within this stack frame. |
| 244 | MapTy Temporaries; |
| 245 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 246 | CallStackFrame(EvalInfo &Info, const LValue *This, |
| 247 | const CCValue *Arguments); |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 248 | ~CallStackFrame(); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 249 | }; |
| 250 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 251 | struct EvalInfo { |
| 252 | const ASTContext &Ctx; |
| 253 | |
| 254 | /// EvalStatus - Contains information about the evaluation. |
| 255 | Expr::EvalStatus &EvalStatus; |
| 256 | |
| 257 | /// CurrentCall - The top of the constexpr call stack. |
| 258 | CallStackFrame *CurrentCall; |
| 259 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 260 | /// CallStackDepth - The number of calls in the call stack right now. |
| 261 | unsigned CallStackDepth; |
| 262 | |
| 263 | typedef llvm::DenseMap<const OpaqueValueExpr*, CCValue> MapTy; |
| 264 | /// OpaqueValues - Values used as the common expression in a |
| 265 | /// BinaryConditionalOperator. |
| 266 | MapTy OpaqueValues; |
| 267 | |
| 268 | /// BottomFrame - The frame in which evaluation started. This must be |
| 269 | /// initialized last. |
| 270 | CallStackFrame BottomFrame; |
| 271 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 272 | /// EvaluatingDecl - This is the declaration whose initializer is being |
| 273 | /// evaluated, if any. |
| 274 | const VarDecl *EvaluatingDecl; |
| 275 | |
| 276 | /// EvaluatingDeclValue - This is the value being constructed for the |
| 277 | /// declaration whose initializer is being evaluated, if any. |
| 278 | APValue *EvaluatingDeclValue; |
| 279 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 280 | |
| 281 | EvalInfo(const ASTContext &C, Expr::EvalStatus &S) |
Richard Smith | c18c423 | 2011-11-21 19:36:32 +0000 | [diff] [blame] | 282 | : Ctx(C), EvalStatus(S), CurrentCall(0), CallStackDepth(0), |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 283 | BottomFrame(*this, 0, 0), EvaluatingDecl(0), EvaluatingDeclValue(0) {} |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 284 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 285 | const CCValue *getOpaqueValue(const OpaqueValueExpr *e) const { |
| 286 | MapTy::const_iterator i = OpaqueValues.find(e); |
| 287 | if (i == OpaqueValues.end()) return 0; |
| 288 | return &i->second; |
| 289 | } |
| 290 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 291 | void setEvaluatingDecl(const VarDecl *VD, APValue &Value) { |
| 292 | EvaluatingDecl = VD; |
| 293 | EvaluatingDeclValue = &Value; |
| 294 | } |
| 295 | |
Richard Smith | c18c423 | 2011-11-21 19:36:32 +0000 | [diff] [blame] | 296 | const LangOptions &getLangOpts() const { return Ctx.getLangOptions(); } |
| 297 | |
| 298 | bool atCallLimit() const { |
| 299 | return CallStackDepth > getLangOpts().ConstexprCallDepth; |
| 300 | } |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 301 | }; |
| 302 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 303 | CallStackFrame::CallStackFrame(EvalInfo &Info, const LValue *This, |
| 304 | const CCValue *Arguments) |
| 305 | : Info(Info), Caller(Info.CurrentCall), This(This), Arguments(Arguments) { |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 306 | Info.CurrentCall = this; |
| 307 | ++Info.CallStackDepth; |
| 308 | } |
| 309 | |
| 310 | CallStackFrame::~CallStackFrame() { |
| 311 | assert(Info.CurrentCall == this && "calls retired out of order"); |
| 312 | --Info.CallStackDepth; |
| 313 | Info.CurrentCall = Caller; |
| 314 | } |
| 315 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 316 | struct ComplexValue { |
| 317 | private: |
| 318 | bool IsInt; |
| 319 | |
| 320 | public: |
| 321 | APSInt IntReal, IntImag; |
| 322 | APFloat FloatReal, FloatImag; |
| 323 | |
| 324 | ComplexValue() : FloatReal(APFloat::Bogus), FloatImag(APFloat::Bogus) {} |
| 325 | |
| 326 | void makeComplexFloat() { IsInt = false; } |
| 327 | bool isComplexFloat() const { return !IsInt; } |
| 328 | APFloat &getComplexFloatReal() { return FloatReal; } |
| 329 | APFloat &getComplexFloatImag() { return FloatImag; } |
| 330 | |
| 331 | void makeComplexInt() { IsInt = true; } |
| 332 | bool isComplexInt() const { return IsInt; } |
| 333 | APSInt &getComplexIntReal() { return IntReal; } |
| 334 | APSInt &getComplexIntImag() { return IntImag; } |
| 335 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 336 | void moveInto(CCValue &v) const { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 337 | if (isComplexFloat()) |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 338 | v = CCValue(FloatReal, FloatImag); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 339 | else |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 340 | v = CCValue(IntReal, IntImag); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 341 | } |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 342 | void setFrom(const CCValue &v) { |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 343 | assert(v.isComplexFloat() || v.isComplexInt()); |
| 344 | if (v.isComplexFloat()) { |
| 345 | makeComplexFloat(); |
| 346 | FloatReal = v.getComplexFloatReal(); |
| 347 | FloatImag = v.getComplexFloatImag(); |
| 348 | } else { |
| 349 | makeComplexInt(); |
| 350 | IntReal = v.getComplexIntReal(); |
| 351 | IntImag = v.getComplexIntImag(); |
| 352 | } |
| 353 | } |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 354 | }; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 355 | |
| 356 | struct LValue { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 357 | APValue::LValueBase Base; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 358 | CharUnits Offset; |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 359 | CallStackFrame *Frame; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 360 | SubobjectDesignator Designator; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 361 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 362 | const APValue::LValueBase getLValueBase() const { return Base; } |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 363 | CharUnits &getLValueOffset() { return Offset; } |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 364 | const CharUnits &getLValueOffset() const { return Offset; } |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 365 | CallStackFrame *getLValueFrame() const { return Frame; } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 366 | SubobjectDesignator &getLValueDesignator() { return Designator; } |
| 367 | const SubobjectDesignator &getLValueDesignator() const { return Designator;} |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 368 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 369 | void moveInto(CCValue &V) const { |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 370 | V = CCValue(Base, Offset, Frame, Designator); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 371 | } |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 372 | void setFrom(const CCValue &V) { |
| 373 | assert(V.isLValue()); |
| 374 | Base = V.getLValueBase(); |
| 375 | Offset = V.getLValueOffset(); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 376 | Frame = V.getLValueFrame(); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 377 | Designator = V.getLValueDesignator(); |
| 378 | } |
| 379 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 380 | void set(APValue::LValueBase B, CallStackFrame *F = 0) { |
| 381 | Base = B; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 382 | Offset = CharUnits::Zero(); |
| 383 | Frame = F; |
| 384 | Designator = SubobjectDesignator(); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 385 | } |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 386 | }; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 387 | |
| 388 | struct MemberPtr { |
| 389 | MemberPtr() {} |
| 390 | explicit MemberPtr(const ValueDecl *Decl) : |
| 391 | DeclAndIsDerivedMember(Decl, false), Path() {} |
| 392 | |
| 393 | /// The member or (direct or indirect) field referred to by this member |
| 394 | /// pointer, or 0 if this is a null member pointer. |
| 395 | const ValueDecl *getDecl() const { |
| 396 | return DeclAndIsDerivedMember.getPointer(); |
| 397 | } |
| 398 | /// Is this actually a member of some type derived from the relevant class? |
| 399 | bool isDerivedMember() const { |
| 400 | return DeclAndIsDerivedMember.getInt(); |
| 401 | } |
| 402 | /// Get the class which the declaration actually lives in. |
| 403 | const CXXRecordDecl *getContainingRecord() const { |
| 404 | return cast<CXXRecordDecl>( |
| 405 | DeclAndIsDerivedMember.getPointer()->getDeclContext()); |
| 406 | } |
| 407 | |
| 408 | void moveInto(CCValue &V) const { |
| 409 | V = CCValue(getDecl(), isDerivedMember(), Path); |
| 410 | } |
| 411 | void setFrom(const CCValue &V) { |
| 412 | assert(V.isMemberPointer()); |
| 413 | DeclAndIsDerivedMember.setPointer(V.getMemberPointerDecl()); |
| 414 | DeclAndIsDerivedMember.setInt(V.isMemberPointerToDerivedMember()); |
| 415 | Path.clear(); |
| 416 | ArrayRef<const CXXRecordDecl*> P = V.getMemberPointerPath(); |
| 417 | Path.insert(Path.end(), P.begin(), P.end()); |
| 418 | } |
| 419 | |
| 420 | /// DeclAndIsDerivedMember - The member declaration, and a flag indicating |
| 421 | /// whether the member is a member of some class derived from the class type |
| 422 | /// of the member pointer. |
| 423 | llvm::PointerIntPair<const ValueDecl*, 1, bool> DeclAndIsDerivedMember; |
| 424 | /// Path - The path of base/derived classes from the member declaration's |
| 425 | /// class (exclusive) to the class type of the member pointer (inclusive). |
| 426 | SmallVector<const CXXRecordDecl*, 4> Path; |
| 427 | |
| 428 | /// Perform a cast towards the class of the Decl (either up or down the |
| 429 | /// hierarchy). |
| 430 | bool castBack(const CXXRecordDecl *Class) { |
| 431 | assert(!Path.empty()); |
| 432 | const CXXRecordDecl *Expected; |
| 433 | if (Path.size() >= 2) |
| 434 | Expected = Path[Path.size() - 2]; |
| 435 | else |
| 436 | Expected = getContainingRecord(); |
| 437 | if (Expected->getCanonicalDecl() != Class->getCanonicalDecl()) { |
| 438 | // C++11 [expr.static.cast]p12: In a conversion from (D::*) to (B::*), |
| 439 | // if B does not contain the original member and is not a base or |
| 440 | // derived class of the class containing the original member, the result |
| 441 | // of the cast is undefined. |
| 442 | // C++11 [conv.mem]p2 does not cover this case for a cast from (B::*) to |
| 443 | // (D::*). We consider that to be a language defect. |
| 444 | return false; |
| 445 | } |
| 446 | Path.pop_back(); |
| 447 | return true; |
| 448 | } |
| 449 | /// Perform a base-to-derived member pointer cast. |
| 450 | bool castToDerived(const CXXRecordDecl *Derived) { |
| 451 | if (!getDecl()) |
| 452 | return true; |
| 453 | if (!isDerivedMember()) { |
| 454 | Path.push_back(Derived); |
| 455 | return true; |
| 456 | } |
| 457 | if (!castBack(Derived)) |
| 458 | return false; |
| 459 | if (Path.empty()) |
| 460 | DeclAndIsDerivedMember.setInt(false); |
| 461 | return true; |
| 462 | } |
| 463 | /// Perform a derived-to-base member pointer cast. |
| 464 | bool castToBase(const CXXRecordDecl *Base) { |
| 465 | if (!getDecl()) |
| 466 | return true; |
| 467 | if (Path.empty()) |
| 468 | DeclAndIsDerivedMember.setInt(true); |
| 469 | if (isDerivedMember()) { |
| 470 | Path.push_back(Base); |
| 471 | return true; |
| 472 | } |
| 473 | return castBack(Base); |
| 474 | } |
| 475 | }; |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 476 | } |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 477 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 478 | static bool Evaluate(CCValue &Result, EvalInfo &Info, const Expr *E); |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 479 | static bool EvaluateConstantExpression(APValue &Result, EvalInfo &Info, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 480 | const LValue &This, const Expr *E); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 481 | static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info); |
| 482 | static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 483 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
| 484 | EvalInfo &Info); |
| 485 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info); |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 486 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 487 | static bool EvaluateIntegerOrLValue(const Expr *E, CCValue &Result, |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 488 | EvalInfo &Info); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 489 | static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 490 | static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 491 | |
| 492 | //===----------------------------------------------------------------------===// |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 493 | // Misc utilities |
| 494 | //===----------------------------------------------------------------------===// |
| 495 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 496 | /// Should this call expression be treated as a string literal? |
| 497 | static bool IsStringLiteralCall(const CallExpr *E) { |
| 498 | unsigned Builtin = E->isBuiltinCall(); |
| 499 | return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString || |
| 500 | Builtin == Builtin::BI__builtin___NSStringMakeConstantString); |
| 501 | } |
| 502 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 503 | static bool IsGlobalLValue(APValue::LValueBase B) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 504 | // C++11 [expr.const]p3 An address constant expression is a prvalue core |
| 505 | // constant expression of pointer type that evaluates to... |
| 506 | |
| 507 | // ... a null pointer value, or a prvalue core constant expression of type |
| 508 | // std::nullptr_t. |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 509 | if (!B) return true; |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 510 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 511 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
| 512 | // ... the address of an object with static storage duration, |
| 513 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 514 | return VD->hasGlobalStorage(); |
| 515 | // ... the address of a function, |
| 516 | return isa<FunctionDecl>(D); |
| 517 | } |
| 518 | |
| 519 | const Expr *E = B.get<const Expr*>(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 520 | switch (E->getStmtClass()) { |
| 521 | default: |
| 522 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 523 | case Expr::CompoundLiteralExprClass: |
| 524 | return cast<CompoundLiteralExpr>(E)->isFileScope(); |
| 525 | // A string literal has static storage duration. |
| 526 | case Expr::StringLiteralClass: |
| 527 | case Expr::PredefinedExprClass: |
| 528 | case Expr::ObjCStringLiteralClass: |
| 529 | case Expr::ObjCEncodeExprClass: |
| 530 | return true; |
| 531 | case Expr::CallExprClass: |
| 532 | return IsStringLiteralCall(cast<CallExpr>(E)); |
| 533 | // For GCC compatibility, &&label has static storage duration. |
| 534 | case Expr::AddrLabelExprClass: |
| 535 | return true; |
| 536 | // A Block literal expression may be used as the initialization value for |
| 537 | // Block variables at global or local static scope. |
| 538 | case Expr::BlockExprClass: |
| 539 | return !cast<BlockExpr>(E)->getBlockDecl()->hasCaptures(); |
| 540 | } |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 543 | /// Check that this reference or pointer core constant expression is a valid |
| 544 | /// value for a constant expression. Type T should be either LValue or CCValue. |
| 545 | template<typename T> |
| 546 | static bool CheckLValueConstantExpression(const T &LVal, APValue &Value) { |
| 547 | if (!IsGlobalLValue(LVal.getLValueBase())) |
| 548 | return false; |
| 549 | |
| 550 | const SubobjectDesignator &Designator = LVal.getLValueDesignator(); |
| 551 | // A constant expression must refer to an object or be a null pointer. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 552 | if (Designator.Invalid || |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 553 | (!LVal.getLValueBase() && !Designator.Entries.empty())) { |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 554 | // FIXME: This is not a constant expression. |
| 555 | Value = APValue(LVal.getLValueBase(), LVal.getLValueOffset(), |
| 556 | APValue::NoLValuePath()); |
| 557 | return true; |
| 558 | } |
| 559 | |
| 560 | Value = APValue(LVal.getLValueBase(), LVal.getLValueOffset(), |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 561 | Designator.Entries, Designator.OnePastTheEnd); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 562 | return true; |
| 563 | } |
| 564 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 565 | /// Check that this core constant expression value is a valid value for a |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 566 | /// constant expression, and if it is, produce the corresponding constant value. |
| 567 | static bool CheckConstantExpression(const CCValue &CCValue, APValue &Value) { |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 568 | if (!CCValue.isLValue()) { |
| 569 | Value = CCValue; |
| 570 | return true; |
| 571 | } |
| 572 | return CheckLValueConstantExpression(CCValue, Value); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 573 | } |
| 574 | |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 575 | const ValueDecl *GetLValueBaseDecl(const LValue &LVal) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 576 | return LVal.Base.dyn_cast<const ValueDecl*>(); |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | static bool IsLiteralLValue(const LValue &Value) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 580 | return Value.Base.dyn_cast<const Expr*>() && !Value.Frame; |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 581 | } |
| 582 | |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 583 | static bool IsWeakLValue(const LValue &Value) { |
| 584 | const ValueDecl *Decl = GetLValueBaseDecl(Value); |
Lang Hames | 0dd7a25 | 2011-12-05 20:16:26 +0000 | [diff] [blame] | 585 | return Decl && Decl->isWeak(); |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 588 | static bool EvalPointerValueAsBool(const CCValue &Value, bool &Result) { |
John McCall | 3554283 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 589 | // A null base expression indicates a null pointer. These are always |
| 590 | // evaluatable, and they are false unless the offset is zero. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 591 | if (!Value.getLValueBase()) { |
| 592 | Result = !Value.getLValueOffset().isZero(); |
John McCall | 3554283 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 593 | return true; |
| 594 | } |
Rafael Espindola | a7d3c04 | 2010-05-07 15:18:43 +0000 | [diff] [blame] | 595 | |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 596 | // Require the base expression to be a global l-value. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 597 | // FIXME: C++11 requires such conversions. Remove this check. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 598 | if (!IsGlobalLValue(Value.getLValueBase())) return false; |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 599 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 600 | // We have a non-null base. These are generally known to be true, but if it's |
| 601 | // a weak declaration it can be null at runtime. |
John McCall | 3554283 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 602 | Result = true; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 603 | const ValueDecl *Decl = Value.getLValueBase().dyn_cast<const ValueDecl*>(); |
Lang Hames | 0dd7a25 | 2011-12-05 20:16:26 +0000 | [diff] [blame] | 604 | return !Decl || !Decl->isWeak(); |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 607 | static bool HandleConversionToBool(const CCValue &Val, bool &Result) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 608 | switch (Val.getKind()) { |
| 609 | case APValue::Uninitialized: |
| 610 | return false; |
| 611 | case APValue::Int: |
| 612 | Result = Val.getInt().getBoolValue(); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 613 | return true; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 614 | case APValue::Float: |
| 615 | Result = !Val.getFloat().isZero(); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 616 | return true; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 617 | case APValue::ComplexInt: |
| 618 | Result = Val.getComplexIntReal().getBoolValue() || |
| 619 | Val.getComplexIntImag().getBoolValue(); |
| 620 | return true; |
| 621 | case APValue::ComplexFloat: |
| 622 | Result = !Val.getComplexFloatReal().isZero() || |
| 623 | !Val.getComplexFloatImag().isZero(); |
| 624 | return true; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 625 | case APValue::LValue: |
| 626 | return EvalPointerValueAsBool(Val, Result); |
| 627 | case APValue::MemberPointer: |
| 628 | Result = Val.getMemberPointerDecl(); |
| 629 | return true; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 630 | case APValue::Vector: |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 631 | case APValue::Array: |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 632 | case APValue::Struct: |
| 633 | case APValue::Union: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 634 | return false; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 635 | } |
| 636 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 637 | llvm_unreachable("unknown APValue kind"); |
| 638 | } |
| 639 | |
| 640 | static bool EvaluateAsBooleanCondition(const Expr *E, bool &Result, |
| 641 | EvalInfo &Info) { |
| 642 | assert(E->isRValue() && "missing lvalue-to-rvalue conv in bool condition"); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 643 | CCValue Val; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 644 | if (!Evaluate(Val, Info, E)) |
| 645 | return false; |
| 646 | return HandleConversionToBool(Val, Result); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 649 | static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 650 | APFloat &Value, const ASTContext &Ctx) { |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 651 | unsigned DestWidth = Ctx.getIntWidth(DestType); |
| 652 | // Determine whether we are converting to unsigned or signed. |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 653 | bool DestSigned = DestType->isSignedIntegerOrEnumerationType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 654 | |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 655 | // FIXME: Warning for overflow. |
Jeffrey Yasskin | 3e1ef78 | 2011-07-15 17:03:07 +0000 | [diff] [blame] | 656 | APSInt Result(DestWidth, !DestSigned); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 657 | bool ignored; |
Jeffrey Yasskin | 3e1ef78 | 2011-07-15 17:03:07 +0000 | [diff] [blame] | 658 | (void)Value.convertToInteger(Result, llvm::APFloat::rmTowardZero, &ignored); |
| 659 | return Result; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 662 | static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 663 | APFloat &Value, const ASTContext &Ctx) { |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 664 | bool ignored; |
| 665 | APFloat Result = Value; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 666 | Result.convert(Ctx.getFloatTypeSemantics(DestType), |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 667 | APFloat::rmNearestTiesToEven, &ignored); |
| 668 | return Result; |
| 669 | } |
| 670 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 671 | static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 672 | APSInt &Value, const ASTContext &Ctx) { |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 673 | unsigned DestWidth = Ctx.getIntWidth(DestType); |
| 674 | APSInt Result = Value; |
| 675 | // Figure out if this is a truncate, extend or noop cast. |
| 676 | // If the input is signed, do a sign extend, noop, or truncate. |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 677 | Result = Result.extOrTrunc(DestWidth); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 678 | Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType()); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 679 | return Result; |
| 680 | } |
| 681 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 682 | static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 683 | APSInt &Value, const ASTContext &Ctx) { |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 684 | |
| 685 | APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1); |
| 686 | Result.convertFromAPInt(Value, Value.isSigned(), |
| 687 | APFloat::rmNearestTiesToEven); |
| 688 | return Result; |
| 689 | } |
| 690 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 691 | static bool FindMostDerivedObject(EvalInfo &Info, const LValue &LVal, |
| 692 | const CXXRecordDecl *&MostDerivedType, |
| 693 | unsigned &MostDerivedPathLength, |
| 694 | bool &MostDerivedIsArrayElement) { |
| 695 | const SubobjectDesignator &D = LVal.Designator; |
| 696 | if (D.Invalid || !LVal.Base) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 697 | return false; |
| 698 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 699 | const Type *T = getType(LVal.Base).getTypePtr(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 700 | |
| 701 | // Find path prefix which leads to the most-derived subobject. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 702 | MostDerivedType = T->getAsCXXRecordDecl(); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 703 | MostDerivedPathLength = 0; |
| 704 | MostDerivedIsArrayElement = false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 705 | |
| 706 | for (unsigned I = 0, N = D.Entries.size(); I != N; ++I) { |
| 707 | bool IsArray = T && T->isArrayType(); |
| 708 | if (IsArray) |
| 709 | T = T->getBaseElementTypeUnsafe(); |
| 710 | else if (const FieldDecl *FD = getAsField(D.Entries[I])) |
| 711 | T = FD->getType().getTypePtr(); |
| 712 | else |
| 713 | T = 0; |
| 714 | |
| 715 | if (T) { |
| 716 | MostDerivedType = T->getAsCXXRecordDecl(); |
| 717 | MostDerivedPathLength = I + 1; |
| 718 | MostDerivedIsArrayElement = IsArray; |
| 719 | } |
| 720 | } |
| 721 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 722 | // (B*)&d + 1 has no most-derived object. |
| 723 | if (D.OnePastTheEnd && MostDerivedPathLength != D.Entries.size()) |
| 724 | return false; |
| 725 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 726 | return MostDerivedType != 0; |
| 727 | } |
| 728 | |
| 729 | static void TruncateLValueBasePath(EvalInfo &Info, LValue &Result, |
| 730 | const RecordDecl *TruncatedType, |
| 731 | unsigned TruncatedElements, |
| 732 | bool IsArrayElement) { |
| 733 | SubobjectDesignator &D = Result.Designator; |
| 734 | const RecordDecl *RD = TruncatedType; |
| 735 | for (unsigned I = TruncatedElements, N = D.Entries.size(); I != N; ++I) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 736 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 737 | const CXXRecordDecl *Base = getAsBaseClass(D.Entries[I]); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 738 | if (isVirtualBaseClass(D.Entries[I])) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 739 | Result.Offset -= Layout.getVBaseClassOffset(Base); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 740 | else |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 741 | Result.Offset -= Layout.getBaseClassOffset(Base); |
| 742 | RD = Base; |
| 743 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 744 | D.Entries.resize(TruncatedElements); |
| 745 | D.ArrayElement = IsArrayElement; |
| 746 | } |
| 747 | |
| 748 | /// If the given LValue refers to a base subobject of some object, find the most |
| 749 | /// derived object and the corresponding complete record type. This is necessary |
| 750 | /// in order to find the offset of a virtual base class. |
| 751 | static bool ExtractMostDerivedObject(EvalInfo &Info, LValue &Result, |
| 752 | const CXXRecordDecl *&MostDerivedType) { |
| 753 | unsigned MostDerivedPathLength; |
| 754 | bool MostDerivedIsArrayElement; |
| 755 | if (!FindMostDerivedObject(Info, Result, MostDerivedType, |
| 756 | MostDerivedPathLength, MostDerivedIsArrayElement)) |
| 757 | return false; |
| 758 | |
| 759 | // Remove the trailing base class path entries and their offsets. |
| 760 | TruncateLValueBasePath(Info, Result, MostDerivedType, MostDerivedPathLength, |
| 761 | MostDerivedIsArrayElement); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 762 | return true; |
| 763 | } |
| 764 | |
| 765 | static void HandleLValueDirectBase(EvalInfo &Info, LValue &Obj, |
| 766 | const CXXRecordDecl *Derived, |
| 767 | const CXXRecordDecl *Base, |
| 768 | const ASTRecordLayout *RL = 0) { |
| 769 | if (!RL) RL = &Info.Ctx.getASTRecordLayout(Derived); |
| 770 | Obj.getLValueOffset() += RL->getBaseClassOffset(Base); |
| 771 | Obj.Designator.addDecl(Base, /*Virtual*/ false); |
| 772 | } |
| 773 | |
| 774 | static bool HandleLValueBase(EvalInfo &Info, LValue &Obj, |
| 775 | const CXXRecordDecl *DerivedDecl, |
| 776 | const CXXBaseSpecifier *Base) { |
| 777 | const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl(); |
| 778 | |
| 779 | if (!Base->isVirtual()) { |
| 780 | HandleLValueDirectBase(Info, Obj, DerivedDecl, BaseDecl); |
| 781 | return true; |
| 782 | } |
| 783 | |
| 784 | // Extract most-derived object and corresponding type. |
| 785 | if (!ExtractMostDerivedObject(Info, Obj, DerivedDecl)) |
| 786 | return false; |
| 787 | |
| 788 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl); |
| 789 | Obj.getLValueOffset() += Layout.getVBaseClassOffset(BaseDecl); |
| 790 | Obj.Designator.addDecl(BaseDecl, /*Virtual*/ true); |
| 791 | return true; |
| 792 | } |
| 793 | |
| 794 | /// Update LVal to refer to the given field, which must be a member of the type |
| 795 | /// currently described by LVal. |
| 796 | static void HandleLValueMember(EvalInfo &Info, LValue &LVal, |
| 797 | const FieldDecl *FD, |
| 798 | const ASTRecordLayout *RL = 0) { |
| 799 | if (!RL) |
| 800 | RL = &Info.Ctx.getASTRecordLayout(FD->getParent()); |
| 801 | |
| 802 | unsigned I = FD->getFieldIndex(); |
| 803 | LVal.Offset += Info.Ctx.toCharUnitsFromBits(RL->getFieldOffset(I)); |
| 804 | LVal.Designator.addDecl(FD); |
| 805 | } |
| 806 | |
| 807 | /// Get the size of the given type in char units. |
| 808 | static bool HandleSizeof(EvalInfo &Info, QualType Type, CharUnits &Size) { |
| 809 | // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc |
| 810 | // extension. |
| 811 | if (Type->isVoidType() || Type->isFunctionType()) { |
| 812 | Size = CharUnits::One(); |
| 813 | return true; |
| 814 | } |
| 815 | |
| 816 | if (!Type->isConstantSizeType()) { |
| 817 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
| 818 | return false; |
| 819 | } |
| 820 | |
| 821 | Size = Info.Ctx.getTypeSizeInChars(Type); |
| 822 | return true; |
| 823 | } |
| 824 | |
| 825 | /// Update a pointer value to model pointer arithmetic. |
| 826 | /// \param Info - Information about the ongoing evaluation. |
| 827 | /// \param LVal - The pointer value to be updated. |
| 828 | /// \param EltTy - The pointee type represented by LVal. |
| 829 | /// \param Adjustment - The adjustment, in objects of type EltTy, to add. |
| 830 | static bool HandleLValueArrayAdjustment(EvalInfo &Info, LValue &LVal, |
| 831 | QualType EltTy, int64_t Adjustment) { |
| 832 | CharUnits SizeOfPointee; |
| 833 | if (!HandleSizeof(Info, EltTy, SizeOfPointee)) |
| 834 | return false; |
| 835 | |
| 836 | // Compute the new offset in the appropriate width. |
| 837 | LVal.Offset += Adjustment * SizeOfPointee; |
| 838 | LVal.Designator.adjustIndex(Adjustment); |
| 839 | return true; |
| 840 | } |
| 841 | |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 842 | /// Try to evaluate the initializer for a variable declaration. |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 843 | static bool EvaluateVarDeclInit(EvalInfo &Info, const VarDecl *VD, |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 844 | CallStackFrame *Frame, CCValue &Result) { |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 845 | // If this is a parameter to an active constexpr function call, perform |
| 846 | // argument substitution. |
| 847 | if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(VD)) { |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 848 | if (!Frame || !Frame->Arguments) |
| 849 | return false; |
| 850 | Result = Frame->Arguments[PVD->getFunctionScopeIndex()]; |
| 851 | return true; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 852 | } |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 853 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 854 | // If we're currently evaluating the initializer of this declaration, use that |
| 855 | // in-flight value. |
| 856 | if (Info.EvaluatingDecl == VD) { |
| 857 | Result = CCValue(*Info.EvaluatingDeclValue, CCValue::GlobalValue()); |
| 858 | return !Result.isUninit(); |
| 859 | } |
| 860 | |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 861 | // Never evaluate the initializer of a weak variable. We can't be sure that |
| 862 | // this is the definition which will be used. |
Lang Hames | 0dd7a25 | 2011-12-05 20:16:26 +0000 | [diff] [blame] | 863 | if (VD->isWeak()) |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 864 | return false; |
| 865 | |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 866 | const Expr *Init = VD->getAnyInitializer(); |
Richard Smith | db1822c | 2011-11-08 01:31:09 +0000 | [diff] [blame] | 867 | if (!Init || Init->isValueDependent()) |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 868 | return false; |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 869 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 870 | if (APValue *V = VD->getEvaluatedValue()) { |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 871 | Result = CCValue(*V, CCValue::GlobalValue()); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 872 | return !Result.isUninit(); |
| 873 | } |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 874 | |
| 875 | if (VD->isEvaluatingValue()) |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 876 | return false; |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 877 | |
| 878 | VD->setEvaluatingValue(); |
| 879 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 880 | Expr::EvalStatus EStatus; |
| 881 | EvalInfo InitInfo(Info.Ctx, EStatus); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 882 | APValue EvalResult; |
| 883 | InitInfo.setEvaluatingDecl(VD, EvalResult); |
| 884 | LValue LVal; |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 885 | LVal.set(VD); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 886 | // FIXME: The caller will need to know whether the value was a constant |
| 887 | // expression. If not, we should propagate up a diagnostic. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 888 | if (!EvaluateConstantExpression(EvalResult, InitInfo, LVal, Init)) { |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 889 | // FIXME: If the evaluation failure was not permanent (for instance, if we |
| 890 | // hit a variable with no declaration yet, or a constexpr function with no |
| 891 | // definition yet), the standard is unclear as to how we should behave. |
| 892 | // |
| 893 | // Either the initializer should be evaluated when the variable is defined, |
| 894 | // or a failed evaluation of the initializer should be reattempted each time |
| 895 | // it is used. |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 896 | VD->setEvaluatedValue(APValue()); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 897 | return false; |
| 898 | } |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 899 | |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 900 | VD->setEvaluatedValue(EvalResult); |
| 901 | Result = CCValue(EvalResult, CCValue::GlobalValue()); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 902 | return true; |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 903 | } |
| 904 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 905 | static bool IsConstNonVolatile(QualType T) { |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 906 | Qualifiers Quals = T.getQualifiers(); |
| 907 | return Quals.hasConst() && !Quals.hasVolatile(); |
| 908 | } |
| 909 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 910 | /// Get the base index of the given base class within an APValue representing |
| 911 | /// the given derived class. |
| 912 | static unsigned getBaseIndex(const CXXRecordDecl *Derived, |
| 913 | const CXXRecordDecl *Base) { |
| 914 | Base = Base->getCanonicalDecl(); |
| 915 | unsigned Index = 0; |
| 916 | for (CXXRecordDecl::base_class_const_iterator I = Derived->bases_begin(), |
| 917 | E = Derived->bases_end(); I != E; ++I, ++Index) { |
| 918 | if (I->getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base) |
| 919 | return Index; |
| 920 | } |
| 921 | |
| 922 | llvm_unreachable("base class missing from derived class's bases list"); |
| 923 | } |
| 924 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 925 | /// Extract the designated sub-object of an rvalue. |
| 926 | static bool ExtractSubobject(EvalInfo &Info, CCValue &Obj, QualType ObjType, |
| 927 | const SubobjectDesignator &Sub, QualType SubType) { |
| 928 | if (Sub.Invalid || Sub.OnePastTheEnd) |
| 929 | return false; |
Richard Smith | f64699e | 2011-11-11 08:28:03 +0000 | [diff] [blame] | 930 | if (Sub.Entries.empty()) |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 931 | return true; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 932 | |
| 933 | assert(!Obj.isLValue() && "extracting subobject of lvalue"); |
| 934 | const APValue *O = &Obj; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 935 | // Walk the designator's path to find the subobject. |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 936 | for (unsigned I = 0, N = Sub.Entries.size(); I != N; ++I) { |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 937 | if (ObjType->isArrayType()) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 938 | // Next subobject is an array element. |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 939 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(ObjType); |
| 940 | if (!CAT) |
| 941 | return false; |
| 942 | uint64_t Index = Sub.Entries[I].ArrayIndex; |
| 943 | if (CAT->getSize().ule(Index)) |
| 944 | return false; |
| 945 | if (O->getArrayInitializedElts() > Index) |
| 946 | O = &O->getArrayInitializedElt(Index); |
| 947 | else |
| 948 | O = &O->getArrayFiller(); |
| 949 | ObjType = CAT->getElementType(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 950 | } else if (const FieldDecl *Field = getAsField(Sub.Entries[I])) { |
| 951 | // Next subobject is a class, struct or union field. |
| 952 | RecordDecl *RD = ObjType->castAs<RecordType>()->getDecl(); |
| 953 | if (RD->isUnion()) { |
| 954 | const FieldDecl *UnionField = O->getUnionField(); |
| 955 | if (!UnionField || |
| 956 | UnionField->getCanonicalDecl() != Field->getCanonicalDecl()) |
| 957 | return false; |
| 958 | O = &O->getUnionValue(); |
| 959 | } else |
| 960 | O = &O->getStructField(Field->getFieldIndex()); |
| 961 | ObjType = Field->getType(); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 962 | } else { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 963 | // Next subobject is a base class. |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 964 | const CXXRecordDecl *Derived = ObjType->getAsCXXRecordDecl(); |
| 965 | const CXXRecordDecl *Base = getAsBaseClass(Sub.Entries[I]); |
| 966 | O = &O->getStructBase(getBaseIndex(Derived, Base)); |
| 967 | ObjType = Info.Ctx.getRecordType(Base); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 968 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 969 | |
| 970 | if (O->isUninit()) |
| 971 | return false; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 972 | } |
| 973 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 974 | Obj = CCValue(*O, CCValue::GlobalValue()); |
| 975 | return true; |
| 976 | } |
| 977 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 978 | /// HandleLValueToRValueConversion - Perform an lvalue-to-rvalue conversion on |
| 979 | /// the given lvalue. This can also be used for 'lvalue-to-lvalue' conversions |
| 980 | /// for looking up the glvalue referred to by an entity of reference type. |
| 981 | /// |
| 982 | /// \param Info - Information about the ongoing evaluation. |
| 983 | /// \param Type - The type we expect this conversion to produce. |
| 984 | /// \param LVal - The glvalue on which we are attempting to perform this action. |
| 985 | /// \param RVal - The produced value will be placed here. |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 986 | static bool HandleLValueToRValueConversion(EvalInfo &Info, QualType Type, |
| 987 | const LValue &LVal, CCValue &RVal) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 988 | const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 989 | CallStackFrame *Frame = LVal.Frame; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 990 | |
| 991 | // FIXME: Indirection through a null pointer deserves a diagnostic. |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 992 | if (!LVal.Base) |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 993 | return false; |
| 994 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 995 | if (const ValueDecl *D = LVal.Base.dyn_cast<const ValueDecl*>()) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 996 | // In C++98, const, non-volatile integers initialized with ICEs are ICEs. |
| 997 | // In C++11, constexpr, non-volatile variables initialized with constant |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 998 | // expressions are constant expressions too. Inside constexpr functions, |
| 999 | // parameters are constant expressions even if they're non-const. |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1000 | // In C, such things can also be folded, although they are not ICEs. |
| 1001 | // |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1002 | // FIXME: volatile-qualified ParmVarDecls need special handling. A literal |
| 1003 | // interpretation of C++11 suggests that volatile parameters are OK if |
| 1004 | // they're never read (there's no prohibition against constructing volatile |
| 1005 | // objects in constant expressions), but lvalue-to-rvalue conversions on |
| 1006 | // them are not permitted. |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1007 | const VarDecl *VD = dyn_cast<VarDecl>(D); |
Richard Smith | cd68992 | 2011-11-07 03:22:51 +0000 | [diff] [blame] | 1008 | if (!VD || VD->isInvalidDecl()) |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1009 | return false; |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1010 | QualType VT = VD->getType(); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1011 | if (!isa<ParmVarDecl>(VD)) { |
| 1012 | if (!IsConstNonVolatile(VT)) |
| 1013 | return false; |
Richard Smith | cd68992 | 2011-11-07 03:22:51 +0000 | [diff] [blame] | 1014 | // FIXME: Allow folding of values of any literal type in all languages. |
| 1015 | if (!VT->isIntegralOrEnumerationType() && !VT->isRealFloatingType() && |
| 1016 | !VD->isConstexpr()) |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1017 | return false; |
| 1018 | } |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1019 | if (!EvaluateVarDeclInit(Info, VD, Frame, RVal)) |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1020 | return false; |
| 1021 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1022 | if (isa<ParmVarDecl>(VD) || !VD->getAnyInitializer()->isLValue()) |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1023 | return ExtractSubobject(Info, RVal, VT, LVal.Designator, Type); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1024 | |
| 1025 | // The declaration was initialized by an lvalue, with no lvalue-to-rvalue |
| 1026 | // conversion. This happens when the declaration and the lvalue should be |
| 1027 | // considered synonymous, for instance when initializing an array of char |
| 1028 | // from a string literal. Continue as if the initializer lvalue was the |
| 1029 | // value we were originally given. |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1030 | assert(RVal.getLValueOffset().isZero() && |
| 1031 | "offset for lvalue init of non-reference"); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1032 | Base = RVal.getLValueBase().get<const Expr*>(); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1033 | Frame = RVal.getLValueFrame(); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1036 | // FIXME: Support PredefinedExpr, ObjCEncodeExpr, MakeStringConstant |
| 1037 | if (const StringLiteral *S = dyn_cast<StringLiteral>(Base)) { |
| 1038 | const SubobjectDesignator &Designator = LVal.Designator; |
| 1039 | if (Designator.Invalid || Designator.Entries.size() != 1) |
| 1040 | return false; |
| 1041 | |
| 1042 | assert(Type->isIntegerType() && "string element not integer type"); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1043 | uint64_t Index = Designator.Entries[0].ArrayIndex; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1044 | if (Index > S->getLength()) |
| 1045 | return false; |
| 1046 | APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(), |
| 1047 | Type->isUnsignedIntegerType()); |
| 1048 | if (Index < S->getLength()) |
| 1049 | Value = S->getCodeUnit(Index); |
| 1050 | RVal = CCValue(Value); |
| 1051 | return true; |
| 1052 | } |
| 1053 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1054 | if (Frame) { |
| 1055 | // If this is a temporary expression with a nontrivial initializer, grab the |
| 1056 | // value from the relevant stack frame. |
| 1057 | RVal = Frame->Temporaries[Base]; |
| 1058 | } else if (const CompoundLiteralExpr *CLE |
| 1059 | = dyn_cast<CompoundLiteralExpr>(Base)) { |
| 1060 | // In C99, a CompoundLiteralExpr is an lvalue, and we defer evaluating the |
| 1061 | // initializer until now for such expressions. Such an expression can't be |
| 1062 | // an ICE in C, so this only matters for fold. |
| 1063 | assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?"); |
| 1064 | if (!Evaluate(RVal, Info, CLE->getInitializer())) |
| 1065 | return false; |
| 1066 | } else |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1067 | return false; |
| 1068 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1069 | return ExtractSubobject(Info, RVal, Base->getType(), LVal.Designator, Type); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1072 | /// Build an lvalue for the object argument of a member function call. |
| 1073 | static bool EvaluateObjectArgument(EvalInfo &Info, const Expr *Object, |
| 1074 | LValue &This) { |
| 1075 | if (Object->getType()->isPointerType()) |
| 1076 | return EvaluatePointer(Object, This, Info); |
| 1077 | |
| 1078 | if (Object->isGLValue()) |
| 1079 | return EvaluateLValue(Object, This, Info); |
| 1080 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1081 | if (Object->getType()->isLiteralType()) |
| 1082 | return EvaluateTemporary(Object, This, Info); |
| 1083 | |
| 1084 | return false; |
| 1085 | } |
| 1086 | |
| 1087 | /// HandleMemberPointerAccess - Evaluate a member access operation and build an |
| 1088 | /// lvalue referring to the result. |
| 1089 | /// |
| 1090 | /// \param Info - Information about the ongoing evaluation. |
| 1091 | /// \param BO - The member pointer access operation. |
| 1092 | /// \param LV - Filled in with a reference to the resulting object. |
| 1093 | /// \param IncludeMember - Specifies whether the member itself is included in |
| 1094 | /// the resulting LValue subobject designator. This is not possible when |
| 1095 | /// creating a bound member function. |
| 1096 | /// \return The field or method declaration to which the member pointer refers, |
| 1097 | /// or 0 if evaluation fails. |
| 1098 | static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, |
| 1099 | const BinaryOperator *BO, |
| 1100 | LValue &LV, |
| 1101 | bool IncludeMember = true) { |
| 1102 | assert(BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI); |
| 1103 | |
| 1104 | if (!EvaluateObjectArgument(Info, BO->getLHS(), LV)) |
| 1105 | return 0; |
| 1106 | |
| 1107 | MemberPtr MemPtr; |
| 1108 | if (!EvaluateMemberPointer(BO->getRHS(), MemPtr, Info)) |
| 1109 | return 0; |
| 1110 | |
| 1111 | // C++11 [expr.mptr.oper]p6: If the second operand is the null pointer to |
| 1112 | // member value, the behavior is undefined. |
| 1113 | if (!MemPtr.getDecl()) |
| 1114 | return 0; |
| 1115 | |
| 1116 | if (MemPtr.isDerivedMember()) { |
| 1117 | // This is a member of some derived class. Truncate LV appropriately. |
| 1118 | const CXXRecordDecl *MostDerivedType; |
| 1119 | unsigned MostDerivedPathLength; |
| 1120 | bool MostDerivedIsArrayElement; |
| 1121 | if (!FindMostDerivedObject(Info, LV, MostDerivedType, MostDerivedPathLength, |
| 1122 | MostDerivedIsArrayElement)) |
| 1123 | return 0; |
| 1124 | |
| 1125 | // The end of the derived-to-base path for the base object must match the |
| 1126 | // derived-to-base path for the member pointer. |
| 1127 | if (MostDerivedPathLength + MemPtr.Path.size() > |
| 1128 | LV.Designator.Entries.size()) |
| 1129 | return 0; |
| 1130 | unsigned PathLengthToMember = |
| 1131 | LV.Designator.Entries.size() - MemPtr.Path.size(); |
| 1132 | for (unsigned I = 0, N = MemPtr.Path.size(); I != N; ++I) { |
| 1133 | const CXXRecordDecl *LVDecl = getAsBaseClass( |
| 1134 | LV.Designator.Entries[PathLengthToMember + I]); |
| 1135 | const CXXRecordDecl *MPDecl = MemPtr.Path[I]; |
| 1136 | if (LVDecl->getCanonicalDecl() != MPDecl->getCanonicalDecl()) |
| 1137 | return 0; |
| 1138 | } |
| 1139 | |
| 1140 | // Truncate the lvalue to the appropriate derived class. |
| 1141 | bool ResultIsArray = false; |
| 1142 | if (PathLengthToMember == MostDerivedPathLength) |
| 1143 | ResultIsArray = MostDerivedIsArrayElement; |
| 1144 | TruncateLValueBasePath(Info, LV, MemPtr.getContainingRecord(), |
| 1145 | PathLengthToMember, ResultIsArray); |
| 1146 | } else if (!MemPtr.Path.empty()) { |
| 1147 | // Extend the LValue path with the member pointer's path. |
| 1148 | LV.Designator.Entries.reserve(LV.Designator.Entries.size() + |
| 1149 | MemPtr.Path.size() + IncludeMember); |
| 1150 | |
| 1151 | // Walk down to the appropriate base class. |
| 1152 | QualType LVType = BO->getLHS()->getType(); |
| 1153 | if (const PointerType *PT = LVType->getAs<PointerType>()) |
| 1154 | LVType = PT->getPointeeType(); |
| 1155 | const CXXRecordDecl *RD = LVType->getAsCXXRecordDecl(); |
| 1156 | assert(RD && "member pointer access on non-class-type expression"); |
| 1157 | // The first class in the path is that of the lvalue. |
| 1158 | for (unsigned I = 1, N = MemPtr.Path.size(); I != N; ++I) { |
| 1159 | const CXXRecordDecl *Base = MemPtr.Path[N - I - 1]; |
| 1160 | HandleLValueDirectBase(Info, LV, RD, Base); |
| 1161 | RD = Base; |
| 1162 | } |
| 1163 | // Finally cast to the class containing the member. |
| 1164 | HandleLValueDirectBase(Info, LV, RD, MemPtr.getContainingRecord()); |
| 1165 | } |
| 1166 | |
| 1167 | // Add the member. Note that we cannot build bound member functions here. |
| 1168 | if (IncludeMember) { |
| 1169 | // FIXME: Deal with IndirectFieldDecls. |
| 1170 | const FieldDecl *FD = dyn_cast<FieldDecl>(MemPtr.getDecl()); |
| 1171 | if (!FD) return 0; |
| 1172 | HandleLValueMember(Info, LV, FD); |
| 1173 | } |
| 1174 | |
| 1175 | return MemPtr.getDecl(); |
| 1176 | } |
| 1177 | |
| 1178 | /// HandleBaseToDerivedCast - Apply the given base-to-derived cast operation on |
| 1179 | /// the provided lvalue, which currently refers to the base object. |
| 1180 | static bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E, |
| 1181 | LValue &Result) { |
| 1182 | const CXXRecordDecl *MostDerivedType; |
| 1183 | unsigned MostDerivedPathLength; |
| 1184 | bool MostDerivedIsArrayElement; |
| 1185 | |
| 1186 | // Check this cast doesn't take us outside the object. |
| 1187 | if (!FindMostDerivedObject(Info, Result, MostDerivedType, |
| 1188 | MostDerivedPathLength, |
| 1189 | MostDerivedIsArrayElement)) |
| 1190 | return false; |
| 1191 | SubobjectDesignator &D = Result.Designator; |
| 1192 | if (MostDerivedPathLength + E->path_size() > D.Entries.size()) |
| 1193 | return false; |
| 1194 | |
| 1195 | // Check the type of the final cast. We don't need to check the path, |
| 1196 | // since a cast can only be formed if the path is unique. |
| 1197 | unsigned NewEntriesSize = D.Entries.size() - E->path_size(); |
| 1198 | bool ResultIsArray = false; |
| 1199 | QualType TargetQT = E->getType(); |
| 1200 | if (const PointerType *PT = TargetQT->getAs<PointerType>()) |
| 1201 | TargetQT = PT->getPointeeType(); |
| 1202 | const CXXRecordDecl *TargetType = TargetQT->getAsCXXRecordDecl(); |
| 1203 | const CXXRecordDecl *FinalType; |
| 1204 | if (NewEntriesSize == MostDerivedPathLength) { |
| 1205 | ResultIsArray = MostDerivedIsArrayElement; |
| 1206 | FinalType = MostDerivedType; |
| 1207 | } else |
| 1208 | FinalType = getAsBaseClass(D.Entries[NewEntriesSize - 1]); |
| 1209 | if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl()) |
| 1210 | return false; |
| 1211 | |
| 1212 | // Truncate the lvalue to the appropriate derived class. |
| 1213 | TruncateLValueBasePath(Info, Result, TargetType, NewEntriesSize, |
| 1214 | ResultIsArray); |
| 1215 | return true; |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1218 | namespace { |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1219 | enum EvalStmtResult { |
| 1220 | /// Evaluation failed. |
| 1221 | ESR_Failed, |
| 1222 | /// Hit a 'return' statement. |
| 1223 | ESR_Returned, |
| 1224 | /// Evaluation succeeded. |
| 1225 | ESR_Succeeded |
| 1226 | }; |
| 1227 | } |
| 1228 | |
| 1229 | // Evaluate a statement. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1230 | static EvalStmtResult EvaluateStmt(CCValue &Result, EvalInfo &Info, |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1231 | const Stmt *S) { |
| 1232 | switch (S->getStmtClass()) { |
| 1233 | default: |
| 1234 | return ESR_Failed; |
| 1235 | |
| 1236 | case Stmt::NullStmtClass: |
| 1237 | case Stmt::DeclStmtClass: |
| 1238 | return ESR_Succeeded; |
| 1239 | |
| 1240 | case Stmt::ReturnStmtClass: |
| 1241 | if (Evaluate(Result, Info, cast<ReturnStmt>(S)->getRetValue())) |
| 1242 | return ESR_Returned; |
| 1243 | return ESR_Failed; |
| 1244 | |
| 1245 | case Stmt::CompoundStmtClass: { |
| 1246 | const CompoundStmt *CS = cast<CompoundStmt>(S); |
| 1247 | for (CompoundStmt::const_body_iterator BI = CS->body_begin(), |
| 1248 | BE = CS->body_end(); BI != BE; ++BI) { |
| 1249 | EvalStmtResult ESR = EvaluateStmt(Result, Info, *BI); |
| 1250 | if (ESR != ESR_Succeeded) |
| 1251 | return ESR; |
| 1252 | } |
| 1253 | return ESR_Succeeded; |
| 1254 | } |
| 1255 | } |
| 1256 | } |
| 1257 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1258 | namespace { |
Richard Smith | cd99b07 | 2011-11-11 05:48:57 +0000 | [diff] [blame] | 1259 | typedef SmallVector<CCValue, 8> ArgVector; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | /// EvaluateArgs - Evaluate the arguments to a function call. |
| 1263 | static bool EvaluateArgs(ArrayRef<const Expr*> Args, ArgVector &ArgValues, |
| 1264 | EvalInfo &Info) { |
| 1265 | for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end(); |
| 1266 | I != E; ++I) |
| 1267 | if (!Evaluate(ArgValues[I - Args.begin()], Info, *I)) |
| 1268 | return false; |
| 1269 | return true; |
| 1270 | } |
| 1271 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1272 | /// Evaluate a function call. |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1273 | static bool HandleFunctionCall(const LValue *This, ArrayRef<const Expr*> Args, |
| 1274 | const Stmt *Body, EvalInfo &Info, |
| 1275 | CCValue &Result) { |
Richard Smith | c18c423 | 2011-11-21 19:36:32 +0000 | [diff] [blame] | 1276 | if (Info.atCallLimit()) |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1277 | return false; |
| 1278 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1279 | ArgVector ArgValues(Args.size()); |
| 1280 | if (!EvaluateArgs(Args, ArgValues, Info)) |
| 1281 | return false; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1282 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1283 | CallStackFrame Frame(Info, This, ArgValues.data()); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1284 | return EvaluateStmt(Result, Info, Body) == ESR_Returned; |
| 1285 | } |
| 1286 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1287 | /// Evaluate a constructor call. |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1288 | static bool HandleConstructorCall(const LValue &This, |
| 1289 | ArrayRef<const Expr*> Args, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1290 | const CXXConstructorDecl *Definition, |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1291 | EvalInfo &Info, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1292 | APValue &Result) { |
Richard Smith | c18c423 | 2011-11-21 19:36:32 +0000 | [diff] [blame] | 1293 | if (Info.atCallLimit()) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1294 | return false; |
| 1295 | |
| 1296 | ArgVector ArgValues(Args.size()); |
| 1297 | if (!EvaluateArgs(Args, ArgValues, Info)) |
| 1298 | return false; |
| 1299 | |
| 1300 | CallStackFrame Frame(Info, &This, ArgValues.data()); |
| 1301 | |
| 1302 | // If it's a delegating constructor, just delegate. |
| 1303 | if (Definition->isDelegatingConstructor()) { |
| 1304 | CXXConstructorDecl::init_const_iterator I = Definition->init_begin(); |
| 1305 | return EvaluateConstantExpression(Result, Info, This, (*I)->getInit()); |
| 1306 | } |
| 1307 | |
| 1308 | // Reserve space for the struct members. |
| 1309 | const CXXRecordDecl *RD = Definition->getParent(); |
| 1310 | if (!RD->isUnion()) |
| 1311 | Result = APValue(APValue::UninitStruct(), RD->getNumBases(), |
| 1312 | std::distance(RD->field_begin(), RD->field_end())); |
| 1313 | |
| 1314 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 1315 | |
| 1316 | unsigned BasesSeen = 0; |
| 1317 | #ifndef NDEBUG |
| 1318 | CXXRecordDecl::base_class_const_iterator BaseIt = RD->bases_begin(); |
| 1319 | #endif |
| 1320 | for (CXXConstructorDecl::init_const_iterator I = Definition->init_begin(), |
| 1321 | E = Definition->init_end(); I != E; ++I) { |
| 1322 | if ((*I)->isBaseInitializer()) { |
| 1323 | QualType BaseType((*I)->getBaseClass(), 0); |
| 1324 | #ifndef NDEBUG |
| 1325 | // Non-virtual base classes are initialized in the order in the class |
| 1326 | // definition. We cannot have a virtual base class for a literal type. |
| 1327 | assert(!BaseIt->isVirtual() && "virtual base for literal type"); |
| 1328 | assert(Info.Ctx.hasSameType(BaseIt->getType(), BaseType) && |
| 1329 | "base class initializers not in expected order"); |
| 1330 | ++BaseIt; |
| 1331 | #endif |
| 1332 | LValue Subobject = This; |
| 1333 | HandleLValueDirectBase(Info, Subobject, RD, |
| 1334 | BaseType->getAsCXXRecordDecl(), &Layout); |
| 1335 | if (!EvaluateConstantExpression(Result.getStructBase(BasesSeen++), Info, |
| 1336 | Subobject, (*I)->getInit())) |
| 1337 | return false; |
| 1338 | } else if (FieldDecl *FD = (*I)->getMember()) { |
| 1339 | LValue Subobject = This; |
| 1340 | HandleLValueMember(Info, Subobject, FD, &Layout); |
| 1341 | if (RD->isUnion()) { |
| 1342 | Result = APValue(FD); |
| 1343 | if (!EvaluateConstantExpression(Result.getUnionValue(), Info, |
| 1344 | Subobject, (*I)->getInit())) |
| 1345 | return false; |
| 1346 | } else if (!EvaluateConstantExpression( |
| 1347 | Result.getStructField(FD->getFieldIndex()), |
| 1348 | Info, Subobject, (*I)->getInit())) |
| 1349 | return false; |
| 1350 | } else { |
| 1351 | // FIXME: handle indirect field initializers |
| 1352 | return false; |
| 1353 | } |
| 1354 | } |
| 1355 | |
| 1356 | return true; |
| 1357 | } |
| 1358 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1359 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 1360 | class HasSideEffect |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1361 | : public ConstStmtVisitor<HasSideEffect, bool> { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 1362 | const ASTContext &Ctx; |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1363 | public: |
| 1364 | |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 1365 | HasSideEffect(const ASTContext &C) : Ctx(C) {} |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1366 | |
| 1367 | // Unhandled nodes conservatively default to having side effects. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1368 | bool VisitStmt(const Stmt *S) { |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1369 | return true; |
| 1370 | } |
| 1371 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1372 | bool VisitParenExpr(const ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 1373 | bool VisitGenericSelectionExpr(const GenericSelectionExpr *E) { |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1374 | return Visit(E->getResultExpr()); |
| 1375 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1376 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 1377 | if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1378 | return true; |
| 1379 | return false; |
| 1380 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1381 | bool VisitObjCIvarRefExpr(const ObjCIvarRefExpr *E) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 1382 | if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1383 | return true; |
| 1384 | return false; |
| 1385 | } |
| 1386 | bool VisitBlockDeclRefExpr (const BlockDeclRefExpr *E) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 1387 | if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1388 | return true; |
| 1389 | return false; |
| 1390 | } |
| 1391 | |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1392 | // We don't want to evaluate BlockExprs multiple times, as they generate |
| 1393 | // a ton of code. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1394 | bool VisitBlockExpr(const BlockExpr *E) { return true; } |
| 1395 | bool VisitPredefinedExpr(const PredefinedExpr *E) { return false; } |
| 1396 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1397 | { return Visit(E->getInitializer()); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1398 | bool VisitMemberExpr(const MemberExpr *E) { return Visit(E->getBase()); } |
| 1399 | bool VisitIntegerLiteral(const IntegerLiteral *E) { return false; } |
| 1400 | bool VisitFloatingLiteral(const FloatingLiteral *E) { return false; } |
| 1401 | bool VisitStringLiteral(const StringLiteral *E) { return false; } |
| 1402 | bool VisitCharacterLiteral(const CharacterLiteral *E) { return false; } |
| 1403 | bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1404 | { return false; } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1405 | bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E) |
Mike Stump | 980ca22 | 2009-10-29 20:48:09 +0000 | [diff] [blame] | 1406 | { return Visit(E->getLHS()) || Visit(E->getRHS()); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1407 | bool VisitChooseExpr(const ChooseExpr *E) |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 1408 | { return Visit(E->getChosenSubExpr(Ctx)); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1409 | bool VisitCastExpr(const CastExpr *E) { return Visit(E->getSubExpr()); } |
| 1410 | bool VisitBinAssign(const BinaryOperator *E) { return true; } |
| 1411 | bool VisitCompoundAssignOperator(const BinaryOperator *E) { return true; } |
| 1412 | bool VisitBinaryOperator(const BinaryOperator *E) |
Mike Stump | 980ca22 | 2009-10-29 20:48:09 +0000 | [diff] [blame] | 1413 | { return Visit(E->getLHS()) || Visit(E->getRHS()); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1414 | bool VisitUnaryPreInc(const UnaryOperator *E) { return true; } |
| 1415 | bool VisitUnaryPostInc(const UnaryOperator *E) { return true; } |
| 1416 | bool VisitUnaryPreDec(const UnaryOperator *E) { return true; } |
| 1417 | bool VisitUnaryPostDec(const UnaryOperator *E) { return true; } |
| 1418 | bool VisitUnaryDeref(const UnaryOperator *E) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 1419 | if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1420 | return true; |
Mike Stump | 980ca22 | 2009-10-29 20:48:09 +0000 | [diff] [blame] | 1421 | return Visit(E->getSubExpr()); |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1422 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1423 | bool VisitUnaryOperator(const UnaryOperator *E) { return Visit(E->getSubExpr()); } |
Chris Lattner | 363ff23 | 2010-04-13 17:34:23 +0000 | [diff] [blame] | 1424 | |
| 1425 | // Has side effects if any element does. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1426 | bool VisitInitListExpr(const InitListExpr *E) { |
Chris Lattner | 363ff23 | 2010-04-13 17:34:23 +0000 | [diff] [blame] | 1427 | for (unsigned i = 0, e = E->getNumInits(); i != e; ++i) |
| 1428 | if (Visit(E->getInit(i))) return true; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1429 | if (const Expr *filler = E->getArrayFiller()) |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 1430 | return Visit(filler); |
Chris Lattner | 363ff23 | 2010-04-13 17:34:23 +0000 | [diff] [blame] | 1431 | return false; |
| 1432 | } |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 1433 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1434 | bool VisitSizeOfPackExpr(const SizeOfPackExpr *) { return false; } |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1435 | }; |
| 1436 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1437 | class OpaqueValueEvaluation { |
| 1438 | EvalInfo &info; |
| 1439 | OpaqueValueExpr *opaqueValue; |
| 1440 | |
| 1441 | public: |
| 1442 | OpaqueValueEvaluation(EvalInfo &info, OpaqueValueExpr *opaqueValue, |
| 1443 | Expr *value) |
| 1444 | : info(info), opaqueValue(opaqueValue) { |
| 1445 | |
| 1446 | // If evaluation fails, fail immediately. |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 1447 | if (!Evaluate(info.OpaqueValues[opaqueValue], info, value)) { |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1448 | this->opaqueValue = 0; |
| 1449 | return; |
| 1450 | } |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | bool hasError() const { return opaqueValue == 0; } |
| 1454 | |
| 1455 | ~OpaqueValueEvaluation() { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 1456 | // FIXME: This will not work for recursive constexpr functions using opaque |
| 1457 | // values. Restore the former value. |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1458 | if (opaqueValue) info.OpaqueValues.erase(opaqueValue); |
| 1459 | } |
| 1460 | }; |
| 1461 | |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1462 | } // end anonymous namespace |
| 1463 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1464 | //===----------------------------------------------------------------------===// |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1465 | // Generic Evaluation |
| 1466 | //===----------------------------------------------------------------------===// |
| 1467 | namespace { |
| 1468 | |
| 1469 | template <class Derived, typename RetTy=void> |
| 1470 | class ExprEvaluatorBase |
| 1471 | : public ConstStmtVisitor<Derived, RetTy> { |
| 1472 | private: |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1473 | RetTy DerivedSuccess(const CCValue &V, const Expr *E) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1474 | return static_cast<Derived*>(this)->Success(V, E); |
| 1475 | } |
| 1476 | RetTy DerivedError(const Expr *E) { |
| 1477 | return static_cast<Derived*>(this)->Error(E); |
| 1478 | } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 1479 | RetTy DerivedValueInitialization(const Expr *E) { |
| 1480 | return static_cast<Derived*>(this)->ValueInitialization(E); |
| 1481 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1482 | |
| 1483 | protected: |
| 1484 | EvalInfo &Info; |
| 1485 | typedef ConstStmtVisitor<Derived, RetTy> StmtVisitorTy; |
| 1486 | typedef ExprEvaluatorBase ExprEvaluatorBaseTy; |
| 1487 | |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 1488 | RetTy ValueInitialization(const Expr *E) { return DerivedError(E); } |
| 1489 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1490 | public: |
| 1491 | ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {} |
| 1492 | |
| 1493 | RetTy VisitStmt(const Stmt *) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1494 | llvm_unreachable("Expression evaluator should not be called on stmts"); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1495 | } |
| 1496 | RetTy VisitExpr(const Expr *E) { |
| 1497 | return DerivedError(E); |
| 1498 | } |
| 1499 | |
| 1500 | RetTy VisitParenExpr(const ParenExpr *E) |
| 1501 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| 1502 | RetTy VisitUnaryExtension(const UnaryOperator *E) |
| 1503 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| 1504 | RetTy VisitUnaryPlus(const UnaryOperator *E) |
| 1505 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| 1506 | RetTy VisitChooseExpr(const ChooseExpr *E) |
| 1507 | { return StmtVisitorTy::Visit(E->getChosenSubExpr(Info.Ctx)); } |
| 1508 | RetTy VisitGenericSelectionExpr(const GenericSelectionExpr *E) |
| 1509 | { return StmtVisitorTy::Visit(E->getResultExpr()); } |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 1510 | RetTy VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E) |
| 1511 | { return StmtVisitorTy::Visit(E->getReplacement()); } |
Richard Smith | 3d75ca8 | 2011-11-09 02:12:41 +0000 | [diff] [blame] | 1512 | RetTy VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) |
| 1513 | { return StmtVisitorTy::Visit(E->getExpr()); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1514 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1515 | RetTy VisitBinaryOperator(const BinaryOperator *E) { |
| 1516 | switch (E->getOpcode()) { |
| 1517 | default: |
| 1518 | return DerivedError(E); |
| 1519 | |
| 1520 | case BO_Comma: |
| 1521 | VisitIgnoredValue(E->getLHS()); |
| 1522 | return StmtVisitorTy::Visit(E->getRHS()); |
| 1523 | |
| 1524 | case BO_PtrMemD: |
| 1525 | case BO_PtrMemI: { |
| 1526 | LValue Obj; |
| 1527 | if (!HandleMemberPointerAccess(Info, E, Obj)) |
| 1528 | return false; |
| 1529 | CCValue Result; |
| 1530 | if (!HandleLValueToRValueConversion(Info, E->getType(), Obj, Result)) |
| 1531 | return false; |
| 1532 | return DerivedSuccess(Result, E); |
| 1533 | } |
| 1534 | } |
| 1535 | } |
| 1536 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1537 | RetTy VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) { |
| 1538 | OpaqueValueEvaluation opaque(Info, E->getOpaqueValue(), E->getCommon()); |
| 1539 | if (opaque.hasError()) |
| 1540 | return DerivedError(E); |
| 1541 | |
| 1542 | bool cond; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1543 | if (!EvaluateAsBooleanCondition(E->getCond(), cond, Info)) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1544 | return DerivedError(E); |
| 1545 | |
| 1546 | return StmtVisitorTy::Visit(cond ? E->getTrueExpr() : E->getFalseExpr()); |
| 1547 | } |
| 1548 | |
| 1549 | RetTy VisitConditionalOperator(const ConditionalOperator *E) { |
| 1550 | bool BoolResult; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1551 | if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1552 | return DerivedError(E); |
| 1553 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1554 | Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1555 | return StmtVisitorTy::Visit(EvalExpr); |
| 1556 | } |
| 1557 | |
| 1558 | RetTy VisitOpaqueValueExpr(const OpaqueValueExpr *E) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1559 | const CCValue *Value = Info.getOpaqueValue(E); |
| 1560 | if (!Value) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1561 | return (E->getSourceExpr() ? StmtVisitorTy::Visit(E->getSourceExpr()) |
| 1562 | : DerivedError(E)); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1563 | return DerivedSuccess(*Value, E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1564 | } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 1565 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1566 | RetTy VisitCallExpr(const CallExpr *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1567 | const Expr *Callee = E->getCallee()->IgnoreParens(); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1568 | QualType CalleeType = Callee->getType(); |
| 1569 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1570 | const FunctionDecl *FD = 0; |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1571 | LValue *This = 0, ThisVal; |
| 1572 | llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs()); |
Richard Smith | 6c95787 | 2011-11-10 09:31:24 +0000 | [diff] [blame] | 1573 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1574 | // Extract function decl and 'this' pointer from the callee. |
| 1575 | if (CalleeType->isSpecificBuiltinType(BuiltinType::BoundMember)) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1576 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(Callee)) { |
| 1577 | // Explicit bound member calls, such as x.f() or p->g(); |
| 1578 | if (!EvaluateObjectArgument(Info, ME->getBase(), ThisVal)) |
| 1579 | return DerivedError(ME->getBase()); |
| 1580 | This = &ThisVal; |
| 1581 | FD = dyn_cast<FunctionDecl>(ME->getMemberDecl()); |
| 1582 | if (!FD) |
| 1583 | return DerivedError(ME); |
| 1584 | } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(Callee)) { |
| 1585 | // Indirect bound member calls ('.*' or '->*'). |
| 1586 | const ValueDecl *Member = HandleMemberPointerAccess(Info, BE, ThisVal, |
| 1587 | false); |
| 1588 | This = &ThisVal; |
| 1589 | FD = dyn_cast_or_null<FunctionDecl>(Member); |
| 1590 | if (!FD) |
| 1591 | return DerivedError(Callee); |
| 1592 | } else |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1593 | return DerivedError(Callee); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1594 | } else if (CalleeType->isFunctionPointerType()) { |
| 1595 | CCValue Call; |
| 1596 | if (!Evaluate(Call, Info, Callee) || !Call.isLValue() || |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1597 | !Call.getLValueOffset().isZero()) |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1598 | return DerivedError(Callee); |
| 1599 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1600 | FD = dyn_cast_or_null<FunctionDecl>( |
| 1601 | Call.getLValueBase().dyn_cast<const ValueDecl*>()); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1602 | if (!FD) |
| 1603 | return DerivedError(Callee); |
| 1604 | |
| 1605 | // Overloaded operator calls to member functions are represented as normal |
| 1606 | // calls with '*this' as the first argument. |
| 1607 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
| 1608 | if (MD && !MD->isStatic()) { |
| 1609 | if (!EvaluateObjectArgument(Info, Args[0], ThisVal)) |
| 1610 | return false; |
| 1611 | This = &ThisVal; |
| 1612 | Args = Args.slice(1); |
| 1613 | } |
| 1614 | |
| 1615 | // Don't call function pointers which have been cast to some other type. |
| 1616 | if (!Info.Ctx.hasSameType(CalleeType->getPointeeType(), FD->getType())) |
| 1617 | return DerivedError(E); |
| 1618 | } else |
Devang Patel | 6142ca7 | 2011-11-10 17:47:39 +0000 | [diff] [blame] | 1619 | return DerivedError(E); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1620 | |
| 1621 | const FunctionDecl *Definition; |
| 1622 | Stmt *Body = FD->getBody(Definition); |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 1623 | CCValue CCResult; |
| 1624 | APValue Result; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1625 | |
| 1626 | if (Body && Definition->isConstexpr() && !Definition->isInvalidDecl() && |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1627 | HandleFunctionCall(This, Args, Body, Info, CCResult) && |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 1628 | CheckConstantExpression(CCResult, Result)) |
| 1629 | return DerivedSuccess(CCValue(Result, CCValue::GlobalValue()), E); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1630 | |
| 1631 | return DerivedError(E); |
| 1632 | } |
| 1633 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1634 | RetTy VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
| 1635 | return StmtVisitorTy::Visit(E->getInitializer()); |
| 1636 | } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 1637 | RetTy VisitInitListExpr(const InitListExpr *E) { |
| 1638 | if (Info.getLangOpts().CPlusPlus0x) { |
| 1639 | if (E->getNumInits() == 0) |
| 1640 | return DerivedValueInitialization(E); |
| 1641 | if (E->getNumInits() == 1) |
| 1642 | return StmtVisitorTy::Visit(E->getInit(0)); |
| 1643 | } |
| 1644 | return DerivedError(E); |
| 1645 | } |
| 1646 | RetTy VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { |
| 1647 | return DerivedValueInitialization(E); |
| 1648 | } |
| 1649 | RetTy VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) { |
| 1650 | return DerivedValueInitialization(E); |
| 1651 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1652 | RetTy VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) { |
| 1653 | return DerivedValueInitialization(E); |
| 1654 | } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 1655 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1656 | /// A member expression where the object is a prvalue is itself a prvalue. |
| 1657 | RetTy VisitMemberExpr(const MemberExpr *E) { |
| 1658 | assert(!E->isArrow() && "missing call to bound member function?"); |
| 1659 | |
| 1660 | CCValue Val; |
| 1661 | if (!Evaluate(Val, Info, E->getBase())) |
| 1662 | return false; |
| 1663 | |
| 1664 | QualType BaseTy = E->getBase()->getType(); |
| 1665 | |
| 1666 | const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
| 1667 | if (!FD) return false; |
| 1668 | assert(!FD->getType()->isReferenceType() && "prvalue reference?"); |
| 1669 | assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() == |
| 1670 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
| 1671 | |
| 1672 | SubobjectDesignator Designator; |
| 1673 | Designator.addDecl(FD); |
| 1674 | |
| 1675 | return ExtractSubobject(Info, Val, BaseTy, Designator, E->getType()) && |
| 1676 | DerivedSuccess(Val, E); |
| 1677 | } |
| 1678 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1679 | RetTy VisitCastExpr(const CastExpr *E) { |
| 1680 | switch (E->getCastKind()) { |
| 1681 | default: |
| 1682 | break; |
| 1683 | |
| 1684 | case CK_NoOp: |
| 1685 | return StmtVisitorTy::Visit(E->getSubExpr()); |
| 1686 | |
| 1687 | case CK_LValueToRValue: { |
| 1688 | LValue LVal; |
| 1689 | if (EvaluateLValue(E->getSubExpr(), LVal, Info)) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1690 | CCValue RVal; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1691 | if (HandleLValueToRValueConversion(Info, E->getType(), LVal, RVal)) |
| 1692 | return DerivedSuccess(RVal, E); |
| 1693 | } |
| 1694 | break; |
| 1695 | } |
| 1696 | } |
| 1697 | |
| 1698 | return DerivedError(E); |
| 1699 | } |
| 1700 | |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 1701 | /// Visit a value which is evaluated, but whose value is ignored. |
| 1702 | void VisitIgnoredValue(const Expr *E) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1703 | CCValue Scratch; |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 1704 | if (!Evaluate(Scratch, Info, E)) |
| 1705 | Info.EvalStatus.HasSideEffects = true; |
| 1706 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1707 | }; |
| 1708 | |
| 1709 | } |
| 1710 | |
| 1711 | //===----------------------------------------------------------------------===// |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1712 | // Common base class for lvalue and temporary evaluation. |
| 1713 | //===----------------------------------------------------------------------===// |
| 1714 | namespace { |
| 1715 | template<class Derived> |
| 1716 | class LValueExprEvaluatorBase |
| 1717 | : public ExprEvaluatorBase<Derived, bool> { |
| 1718 | protected: |
| 1719 | LValue &Result; |
| 1720 | typedef LValueExprEvaluatorBase LValueExprEvaluatorBaseTy; |
| 1721 | typedef ExprEvaluatorBase<Derived, bool> ExprEvaluatorBaseTy; |
| 1722 | |
| 1723 | bool Success(APValue::LValueBase B) { |
| 1724 | Result.set(B); |
| 1725 | return true; |
| 1726 | } |
| 1727 | |
| 1728 | public: |
| 1729 | LValueExprEvaluatorBase(EvalInfo &Info, LValue &Result) : |
| 1730 | ExprEvaluatorBaseTy(Info), Result(Result) {} |
| 1731 | |
| 1732 | bool Success(const CCValue &V, const Expr *E) { |
| 1733 | Result.setFrom(V); |
| 1734 | return true; |
| 1735 | } |
| 1736 | bool Error(const Expr *E) { |
| 1737 | return false; |
| 1738 | } |
| 1739 | |
| 1740 | bool CheckValidLValue() { |
| 1741 | // C++11 [basic.lval]p1: An lvalue designates a function or an object. Hence |
| 1742 | // there are no null references, nor once-past-the-end references. |
| 1743 | // FIXME: Check for one-past-the-end array indices |
| 1744 | return Result.Base && !Result.Designator.Invalid && |
| 1745 | !Result.Designator.OnePastTheEnd; |
| 1746 | } |
| 1747 | |
| 1748 | bool VisitMemberExpr(const MemberExpr *E) { |
| 1749 | // Handle non-static data members. |
| 1750 | QualType BaseTy; |
| 1751 | if (E->isArrow()) { |
| 1752 | if (!EvaluatePointer(E->getBase(), Result, this->Info)) |
| 1753 | return false; |
| 1754 | BaseTy = E->getBase()->getType()->getAs<PointerType>()->getPointeeType(); |
| 1755 | } else { |
| 1756 | if (!this->Visit(E->getBase())) |
| 1757 | return false; |
| 1758 | BaseTy = E->getBase()->getType(); |
| 1759 | } |
| 1760 | // FIXME: In C++11, require the result to be a valid lvalue. |
| 1761 | |
| 1762 | const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
| 1763 | // FIXME: Handle IndirectFieldDecls |
| 1764 | if (!FD) return false; |
| 1765 | assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() == |
| 1766 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
| 1767 | (void)BaseTy; |
| 1768 | |
| 1769 | HandleLValueMember(this->Info, Result, FD); |
| 1770 | |
| 1771 | if (FD->getType()->isReferenceType()) { |
| 1772 | CCValue RefValue; |
| 1773 | if (!HandleLValueToRValueConversion(this->Info, FD->getType(), Result, |
| 1774 | RefValue)) |
| 1775 | return false; |
| 1776 | return Success(RefValue, E); |
| 1777 | } |
| 1778 | return true; |
| 1779 | } |
| 1780 | |
| 1781 | bool VisitBinaryOperator(const BinaryOperator *E) { |
| 1782 | switch (E->getOpcode()) { |
| 1783 | default: |
| 1784 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| 1785 | |
| 1786 | case BO_PtrMemD: |
| 1787 | case BO_PtrMemI: |
| 1788 | return HandleMemberPointerAccess(this->Info, E, Result); |
| 1789 | } |
| 1790 | } |
| 1791 | |
| 1792 | bool VisitCastExpr(const CastExpr *E) { |
| 1793 | switch (E->getCastKind()) { |
| 1794 | default: |
| 1795 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 1796 | |
| 1797 | case CK_DerivedToBase: |
| 1798 | case CK_UncheckedDerivedToBase: { |
| 1799 | if (!this->Visit(E->getSubExpr())) |
| 1800 | return false; |
| 1801 | if (!CheckValidLValue()) |
| 1802 | return false; |
| 1803 | |
| 1804 | // Now figure out the necessary offset to add to the base LV to get from |
| 1805 | // the derived class to the base class. |
| 1806 | QualType Type = E->getSubExpr()->getType(); |
| 1807 | |
| 1808 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 1809 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 1810 | if (!HandleLValueBase(this->Info, Result, Type->getAsCXXRecordDecl(), |
| 1811 | *PathI)) |
| 1812 | return false; |
| 1813 | Type = (*PathI)->getType(); |
| 1814 | } |
| 1815 | |
| 1816 | return true; |
| 1817 | } |
| 1818 | } |
| 1819 | } |
| 1820 | }; |
| 1821 | } |
| 1822 | |
| 1823 | //===----------------------------------------------------------------------===// |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1824 | // LValue Evaluation |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1825 | // |
| 1826 | // This is used for evaluating lvalues (in C and C++), xvalues (in C++11), |
| 1827 | // function designators (in C), decl references to void objects (in C), and |
| 1828 | // temporaries (if building with -Wno-address-of-temporary). |
| 1829 | // |
| 1830 | // LValue evaluation produces values comprising a base expression of one of the |
| 1831 | // following types: |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1832 | // - Declarations |
| 1833 | // * VarDecl |
| 1834 | // * FunctionDecl |
| 1835 | // - Literals |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1836 | // * CompoundLiteralExpr in C |
| 1837 | // * StringLiteral |
| 1838 | // * PredefinedExpr |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1839 | // * ObjCStringLiteralExpr |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1840 | // * ObjCEncodeExpr |
| 1841 | // * AddrLabelExpr |
| 1842 | // * BlockExpr |
| 1843 | // * CallExpr for a MakeStringConstant builtin |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1844 | // - Locals and temporaries |
| 1845 | // * Any Expr, with a Frame indicating the function in which the temporary was |
| 1846 | // evaluated. |
| 1847 | // plus an offset in bytes. |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1848 | //===----------------------------------------------------------------------===// |
| 1849 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 1850 | class LValueExprEvaluator |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1851 | : public LValueExprEvaluatorBase<LValueExprEvaluator> { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1852 | public: |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1853 | LValueExprEvaluator(EvalInfo &Info, LValue &Result) : |
| 1854 | LValueExprEvaluatorBaseTy(Info, Result) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1855 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1856 | bool VisitVarDecl(const Expr *E, const VarDecl *VD); |
| 1857 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1858 | bool VisitDeclRefExpr(const DeclRefExpr *E); |
| 1859 | bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(E); } |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 1860 | bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1861 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
| 1862 | bool VisitMemberExpr(const MemberExpr *E); |
| 1863 | bool VisitStringLiteral(const StringLiteral *E) { return Success(E); } |
| 1864 | bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(E); } |
| 1865 | bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E); |
| 1866 | bool VisitUnaryDeref(const UnaryOperator *E); |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 1867 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1868 | bool VisitCastExpr(const CastExpr *E) { |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 1869 | switch (E->getCastKind()) { |
| 1870 | default: |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1871 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 1872 | |
Eli Friedman | db92422 | 2011-10-11 00:13:24 +0000 | [diff] [blame] | 1873 | case CK_LValueBitCast: |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1874 | if (!Visit(E->getSubExpr())) |
| 1875 | return false; |
| 1876 | Result.Designator.setInvalid(); |
| 1877 | return true; |
Eli Friedman | db92422 | 2011-10-11 00:13:24 +0000 | [diff] [blame] | 1878 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1879 | case CK_BaseToDerived: |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1880 | if (!Visit(E->getSubExpr())) |
| 1881 | return false; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1882 | if (!CheckValidLValue()) |
| 1883 | return false; |
| 1884 | return HandleBaseToDerivedCast(Info, E, Result); |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 1885 | } |
| 1886 | } |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 1887 | |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 1888 | // FIXME: Missing: __real__, __imag__ |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1889 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1890 | }; |
| 1891 | } // end anonymous namespace |
| 1892 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1893 | /// Evaluate an expression as an lvalue. This can be legitimately called on |
| 1894 | /// expressions which are not glvalues, in a few cases: |
| 1895 | /// * function designators in C, |
| 1896 | /// * "extern void" objects, |
| 1897 | /// * temporaries, if building with -Wno-address-of-temporary. |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1898 | static bool EvaluateLValue(const Expr* E, LValue& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1899 | assert((E->isGLValue() || E->getType()->isFunctionType() || |
| 1900 | E->getType()->isVoidType() || isa<CXXTemporaryObjectExpr>(E)) && |
| 1901 | "can't evaluate expression as an lvalue"); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1902 | return LValueExprEvaluator(Info, Result).Visit(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1903 | } |
| 1904 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1905 | bool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1906 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl())) |
| 1907 | return Success(FD); |
| 1908 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1909 | return VisitVarDecl(E, VD); |
| 1910 | return Error(E); |
| 1911 | } |
Richard Smith | 436c889 | 2011-10-24 23:14:33 +0000 | [diff] [blame] | 1912 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1913 | bool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) { |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1914 | if (!VD->getType()->isReferenceType()) { |
| 1915 | if (isa<ParmVarDecl>(VD)) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1916 | Result.set(VD, Info.CurrentCall); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1917 | return true; |
| 1918 | } |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1919 | return Success(VD); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1920 | } |
Eli Friedman | 50c39ea | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 1921 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1922 | CCValue V; |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1923 | if (EvaluateVarDeclInit(Info, VD, Info.CurrentCall, V)) |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1924 | return Success(V, E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1925 | |
| 1926 | return Error(E); |
Anders Carlsson | 35873c4 | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 1927 | } |
| 1928 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 1929 | bool LValueExprEvaluator::VisitMaterializeTemporaryExpr( |
| 1930 | const MaterializeTemporaryExpr *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1931 | if (E->GetTemporaryExpr()->isRValue()) { |
| 1932 | if (E->getType()->isRecordType() && E->getType()->isLiteralType()) |
| 1933 | return EvaluateTemporary(E->GetTemporaryExpr(), Result, Info); |
| 1934 | |
| 1935 | Result.set(E, Info.CurrentCall); |
| 1936 | return EvaluateConstantExpression(Info.CurrentCall->Temporaries[E], Info, |
| 1937 | Result, E->GetTemporaryExpr()); |
| 1938 | } |
| 1939 | |
| 1940 | // Materialization of an lvalue temporary occurs when we need to force a copy |
| 1941 | // (for instance, if it's a bitfield). |
| 1942 | // FIXME: The AST should contain an lvalue-to-rvalue node for such cases. |
| 1943 | if (!Visit(E->GetTemporaryExpr())) |
| 1944 | return false; |
| 1945 | if (!HandleLValueToRValueConversion(Info, E->getType(), Result, |
| 1946 | Info.CurrentCall->Temporaries[E])) |
| 1947 | return false; |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1948 | Result.set(E, Info.CurrentCall); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1949 | return true; |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1952 | bool |
| 1953 | LValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1954 | assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?"); |
| 1955 | // Defer visiting the literal until the lvalue-to-rvalue conversion. We can |
| 1956 | // only see this when folding in C, so there's no standard to follow here. |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1957 | return Success(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1958 | } |
| 1959 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1960 | bool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1961 | // Handle static data members. |
| 1962 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getMemberDecl())) { |
| 1963 | VisitIgnoredValue(E->getBase()); |
| 1964 | return VisitVarDecl(E, VD); |
| 1965 | } |
| 1966 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1967 | // Handle static member functions. |
| 1968 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) { |
| 1969 | if (MD->isStatic()) { |
| 1970 | VisitIgnoredValue(E->getBase()); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1971 | return Success(MD); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1972 | } |
| 1973 | } |
| 1974 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1975 | // Handle non-static data members. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1976 | return LValueExprEvaluatorBaseTy::VisitMemberExpr(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1977 | } |
| 1978 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1979 | bool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1980 | // FIXME: Deal with vectors as array subscript bases. |
| 1981 | if (E->getBase()->getType()->isVectorType()) |
| 1982 | return false; |
| 1983 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1984 | if (!EvaluatePointer(E->getBase(), Result, Info)) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1985 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1986 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1987 | APSInt Index; |
| 1988 | if (!EvaluateInteger(E->getIdx(), Index, Info)) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1989 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1990 | int64_t IndexValue |
| 1991 | = Index.isSigned() ? Index.getSExtValue() |
| 1992 | : static_cast<int64_t>(Index.getZExtValue()); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1993 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1994 | // FIXME: In C++11, require the result to be a valid lvalue. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1995 | return HandleLValueArrayAdjustment(Info, Result, E->getType(), IndexValue); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1996 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1997 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1998 | bool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1999 | // FIXME: In C++11, require the result to be a valid lvalue. |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2000 | return EvaluatePointer(E->getSubExpr(), Result, Info); |
Eli Friedman | e8761c8 | 2009-02-20 01:57:15 +0000 | [diff] [blame] | 2001 | } |
| 2002 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2003 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2004 | // Pointer Evaluation |
| 2005 | //===----------------------------------------------------------------------===// |
| 2006 | |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 2007 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 2008 | class PointerExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2009 | : public ExprEvaluatorBase<PointerExprEvaluator, bool> { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2010 | LValue &Result; |
| 2011 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2012 | bool Success(const Expr *E) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2013 | Result.set(E); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2014 | return true; |
| 2015 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 2016 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2017 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2018 | PointerExprEvaluator(EvalInfo &info, LValue &Result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2019 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2020 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2021 | bool Success(const CCValue &V, const Expr *E) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2022 | Result.setFrom(V); |
| 2023 | return true; |
| 2024 | } |
| 2025 | bool Error(const Stmt *S) { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2026 | return false; |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 2027 | } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2028 | bool ValueInitialization(const Expr *E) { |
| 2029 | return Success((Expr*)0); |
| 2030 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 2031 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2032 | bool VisitBinaryOperator(const BinaryOperator *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2033 | bool VisitCastExpr(const CastExpr* E); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2034 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2035 | bool VisitObjCStringLiteral(const ObjCStringLiteral *E) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2036 | { return Success(E); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2037 | bool VisitAddrLabelExpr(const AddrLabelExpr *E) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2038 | { return Success(E); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2039 | bool VisitCallExpr(const CallExpr *E); |
| 2040 | bool VisitBlockExpr(const BlockExpr *E) { |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2041 | if (!E->getBlockDecl()->hasCaptures()) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2042 | return Success(E); |
| 2043 | return false; |
Mike Stump | b83d287 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 2044 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2045 | bool VisitCXXThisExpr(const CXXThisExpr *E) { |
| 2046 | if (!Info.CurrentCall->This) |
| 2047 | return false; |
| 2048 | Result = *Info.CurrentCall->This; |
| 2049 | return true; |
| 2050 | } |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2051 | |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 2052 | // FIXME: Missing: @protocol, @selector |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 2053 | }; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2054 | } // end anonymous namespace |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 2055 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2056 | static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2057 | assert(E->isRValue() && E->getType()->hasPointerRepresentation()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2058 | return PointerExprEvaluator(Info, Result).Visit(E); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2059 | } |
| 2060 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2061 | bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2062 | if (E->getOpcode() != BO_Add && |
| 2063 | E->getOpcode() != BO_Sub) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2064 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2065 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2066 | const Expr *PExp = E->getLHS(); |
| 2067 | const Expr *IExp = E->getRHS(); |
| 2068 | if (IExp->getType()->isPointerType()) |
| 2069 | std::swap(PExp, IExp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2070 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2071 | if (!EvaluatePointer(PExp, Result, Info)) |
| 2072 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2073 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2074 | llvm::APSInt Offset; |
| 2075 | if (!EvaluateInteger(IExp, Offset, Info)) |
| 2076 | return false; |
| 2077 | int64_t AdditionalOffset |
| 2078 | = Offset.isSigned() ? Offset.getSExtValue() |
| 2079 | : static_cast<int64_t>(Offset.getZExtValue()); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 2080 | if (E->getOpcode() == BO_Sub) |
| 2081 | AdditionalOffset = -AdditionalOffset; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2082 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2083 | QualType Pointee = PExp->getType()->getAs<PointerType>()->getPointeeType(); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2084 | // FIXME: In C++11, require the result to be a valid lvalue. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2085 | return HandleLValueArrayAdjustment(Info, Result, Pointee, AdditionalOffset); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2086 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2087 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2088 | bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| 2089 | return EvaluateLValue(E->getSubExpr(), Result, Info); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2090 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2091 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2092 | bool PointerExprEvaluator::VisitCastExpr(const CastExpr* E) { |
| 2093 | const Expr* SubExpr = E->getSubExpr(); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2094 | |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 2095 | switch (E->getCastKind()) { |
| 2096 | default: |
| 2097 | break; |
| 2098 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2099 | case CK_BitCast: |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2100 | case CK_CPointerToObjCPointerCast: |
| 2101 | case CK_BlockPointerToObjCPointerCast: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2102 | case CK_AnyPointerToBlockPointerCast: |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 2103 | if (!Visit(SubExpr)) |
| 2104 | return false; |
| 2105 | Result.Designator.setInvalid(); |
| 2106 | return true; |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 2107 | |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 2108 | case CK_DerivedToBase: |
| 2109 | case CK_UncheckedDerivedToBase: { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2110 | if (!EvaluatePointer(E->getSubExpr(), Result, Info)) |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 2111 | return false; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2112 | if (!Result.Base && Result.Offset.isZero()) |
| 2113 | return true; |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 2114 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2115 | // Now figure out the necessary offset to add to the base LV to get from |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 2116 | // the derived class to the base class. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2117 | QualType Type = |
| 2118 | E->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType(); |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 2119 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2120 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 2121 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2122 | if (!HandleLValueBase(Info, Result, Type->getAsCXXRecordDecl(), *PathI)) |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 2123 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2124 | Type = (*PathI)->getType(); |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 2125 | } |
| 2126 | |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 2127 | return true; |
| 2128 | } |
| 2129 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2130 | case CK_BaseToDerived: |
| 2131 | if (!Visit(E->getSubExpr())) |
| 2132 | return false; |
| 2133 | if (!Result.Base && Result.Offset.isZero()) |
| 2134 | return true; |
| 2135 | return HandleBaseToDerivedCast(Info, E, Result); |
| 2136 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2137 | case CK_NullToPointer: |
| 2138 | return ValueInitialization(E); |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 2139 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2140 | case CK_IntegralToPointer: { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2141 | CCValue Value; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2142 | if (!EvaluateIntegerOrLValue(SubExpr, Value, Info)) |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 2143 | break; |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 2144 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2145 | if (Value.isInt()) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2146 | unsigned Size = Info.Ctx.getTypeSize(E->getType()); |
| 2147 | uint64_t N = Value.getInt().extOrTrunc(Size).getZExtValue(); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2148 | Result.Base = (Expr*)0; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2149 | Result.Offset = CharUnits::fromQuantity(N); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 2150 | Result.Frame = 0; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 2151 | Result.Designator.setInvalid(); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2152 | return true; |
| 2153 | } else { |
| 2154 | // Cast is of an lvalue, no need to change value. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2155 | Result.setFrom(Value); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2156 | return true; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2157 | } |
| 2158 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2159 | case CK_ArrayToPointerDecay: |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2160 | if (SubExpr->isGLValue()) { |
| 2161 | if (!EvaluateLValue(SubExpr, Result, Info)) |
| 2162 | return false; |
| 2163 | } else { |
| 2164 | Result.set(SubExpr, Info.CurrentCall); |
| 2165 | if (!EvaluateConstantExpression(Info.CurrentCall->Temporaries[SubExpr], |
| 2166 | Info, Result, SubExpr)) |
| 2167 | return false; |
| 2168 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 2169 | // The result is a pointer to the first element of the array. |
| 2170 | Result.Designator.addIndex(0); |
| 2171 | return true; |
Richard Smith | 6a7c94a | 2011-10-31 20:57:44 +0000 | [diff] [blame] | 2172 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2173 | case CK_FunctionToPointerDecay: |
Richard Smith | 6a7c94a | 2011-10-31 20:57:44 +0000 | [diff] [blame] | 2174 | return EvaluateLValue(SubExpr, Result, Info); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2175 | } |
| 2176 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2177 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2178 | } |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2179 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2180 | bool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2181 | if (IsStringLiteralCall(E)) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2182 | return Success(E); |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 2183 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2184 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2185 | } |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2186 | |
| 2187 | //===----------------------------------------------------------------------===// |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2188 | // Member Pointer Evaluation |
| 2189 | //===----------------------------------------------------------------------===// |
| 2190 | |
| 2191 | namespace { |
| 2192 | class MemberPointerExprEvaluator |
| 2193 | : public ExprEvaluatorBase<MemberPointerExprEvaluator, bool> { |
| 2194 | MemberPtr &Result; |
| 2195 | |
| 2196 | bool Success(const ValueDecl *D) { |
| 2197 | Result = MemberPtr(D); |
| 2198 | return true; |
| 2199 | } |
| 2200 | public: |
| 2201 | |
| 2202 | MemberPointerExprEvaluator(EvalInfo &Info, MemberPtr &Result) |
| 2203 | : ExprEvaluatorBaseTy(Info), Result(Result) {} |
| 2204 | |
| 2205 | bool Success(const CCValue &V, const Expr *E) { |
| 2206 | Result.setFrom(V); |
| 2207 | return true; |
| 2208 | } |
| 2209 | bool Error(const Stmt *S) { |
| 2210 | return false; |
| 2211 | } |
| 2212 | bool ValueInitialization(const Expr *E) { |
| 2213 | return Success((const ValueDecl*)0); |
| 2214 | } |
| 2215 | |
| 2216 | bool VisitCastExpr(const CastExpr *E); |
| 2217 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
| 2218 | }; |
| 2219 | } // end anonymous namespace |
| 2220 | |
| 2221 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
| 2222 | EvalInfo &Info) { |
| 2223 | assert(E->isRValue() && E->getType()->isMemberPointerType()); |
| 2224 | return MemberPointerExprEvaluator(Info, Result).Visit(E); |
| 2225 | } |
| 2226 | |
| 2227 | bool MemberPointerExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 2228 | switch (E->getCastKind()) { |
| 2229 | default: |
| 2230 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 2231 | |
| 2232 | case CK_NullToMemberPointer: |
| 2233 | return ValueInitialization(E); |
| 2234 | |
| 2235 | case CK_BaseToDerivedMemberPointer: { |
| 2236 | if (!Visit(E->getSubExpr())) |
| 2237 | return false; |
| 2238 | if (E->path_empty()) |
| 2239 | return true; |
| 2240 | // Base-to-derived member pointer casts store the path in derived-to-base |
| 2241 | // order, so iterate backwards. The CXXBaseSpecifier also provides us with |
| 2242 | // the wrong end of the derived->base arc, so stagger the path by one class. |
| 2243 | typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter; |
| 2244 | for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin()); |
| 2245 | PathI != PathE; ++PathI) { |
| 2246 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
| 2247 | const CXXRecordDecl *Derived = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 2248 | if (!Result.castToDerived(Derived)) |
| 2249 | return false; |
| 2250 | } |
| 2251 | const Type *FinalTy = E->getType()->castAs<MemberPointerType>()->getClass(); |
| 2252 | if (!Result.castToDerived(FinalTy->getAsCXXRecordDecl())) |
| 2253 | return false; |
| 2254 | return true; |
| 2255 | } |
| 2256 | |
| 2257 | case CK_DerivedToBaseMemberPointer: |
| 2258 | if (!Visit(E->getSubExpr())) |
| 2259 | return false; |
| 2260 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 2261 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 2262 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
| 2263 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 2264 | if (!Result.castToBase(Base)) |
| 2265 | return false; |
| 2266 | } |
| 2267 | return true; |
| 2268 | } |
| 2269 | } |
| 2270 | |
| 2271 | bool MemberPointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| 2272 | // C++11 [expr.unary.op]p3 has very strict rules on how the address of a |
| 2273 | // member can be formed. |
| 2274 | return Success(cast<DeclRefExpr>(E->getSubExpr())->getDecl()); |
| 2275 | } |
| 2276 | |
| 2277 | //===----------------------------------------------------------------------===// |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2278 | // Record Evaluation |
| 2279 | //===----------------------------------------------------------------------===// |
| 2280 | |
| 2281 | namespace { |
| 2282 | class RecordExprEvaluator |
| 2283 | : public ExprEvaluatorBase<RecordExprEvaluator, bool> { |
| 2284 | const LValue &This; |
| 2285 | APValue &Result; |
| 2286 | public: |
| 2287 | |
| 2288 | RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result) |
| 2289 | : ExprEvaluatorBaseTy(info), This(This), Result(Result) {} |
| 2290 | |
| 2291 | bool Success(const CCValue &V, const Expr *E) { |
| 2292 | return CheckConstantExpression(V, Result); |
| 2293 | } |
| 2294 | bool Error(const Expr *E) { return false; } |
| 2295 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2296 | bool VisitCastExpr(const CastExpr *E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2297 | bool VisitInitListExpr(const InitListExpr *E); |
| 2298 | bool VisitCXXConstructExpr(const CXXConstructExpr *E); |
| 2299 | }; |
| 2300 | } |
| 2301 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2302 | bool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 2303 | switch (E->getCastKind()) { |
| 2304 | default: |
| 2305 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 2306 | |
| 2307 | case CK_ConstructorConversion: |
| 2308 | return Visit(E->getSubExpr()); |
| 2309 | |
| 2310 | case CK_DerivedToBase: |
| 2311 | case CK_UncheckedDerivedToBase: { |
| 2312 | CCValue DerivedObject; |
| 2313 | if (!Evaluate(DerivedObject, Info, E->getSubExpr()) || |
| 2314 | !DerivedObject.isStruct()) |
| 2315 | return false; |
| 2316 | |
| 2317 | // Derived-to-base rvalue conversion: just slice off the derived part. |
| 2318 | APValue *Value = &DerivedObject; |
| 2319 | const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl(); |
| 2320 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 2321 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 2322 | assert(!(*PathI)->isVirtual() && "record rvalue with virtual base"); |
| 2323 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 2324 | Value = &Value->getStructBase(getBaseIndex(RD, Base)); |
| 2325 | RD = Base; |
| 2326 | } |
| 2327 | Result = *Value; |
| 2328 | return true; |
| 2329 | } |
| 2330 | } |
| 2331 | } |
| 2332 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2333 | bool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 2334 | const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl(); |
| 2335 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 2336 | |
| 2337 | if (RD->isUnion()) { |
| 2338 | Result = APValue(E->getInitializedFieldInUnion()); |
| 2339 | if (!E->getNumInits()) |
| 2340 | return true; |
| 2341 | LValue Subobject = This; |
| 2342 | HandleLValueMember(Info, Subobject, E->getInitializedFieldInUnion(), |
| 2343 | &Layout); |
| 2344 | return EvaluateConstantExpression(Result.getUnionValue(), Info, |
| 2345 | Subobject, E->getInit(0)); |
| 2346 | } |
| 2347 | |
| 2348 | assert((!isa<CXXRecordDecl>(RD) || !cast<CXXRecordDecl>(RD)->getNumBases()) && |
| 2349 | "initializer list for class with base classes"); |
| 2350 | Result = APValue(APValue::UninitStruct(), 0, |
| 2351 | std::distance(RD->field_begin(), RD->field_end())); |
| 2352 | unsigned ElementNo = 0; |
| 2353 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 2354 | FieldEnd = RD->field_end(); Field != FieldEnd; ++Field) { |
| 2355 | // Anonymous bit-fields are not considered members of the class for |
| 2356 | // purposes of aggregate initialization. |
| 2357 | if (Field->isUnnamedBitfield()) |
| 2358 | continue; |
| 2359 | |
| 2360 | LValue Subobject = This; |
| 2361 | HandleLValueMember(Info, Subobject, *Field, &Layout); |
| 2362 | |
| 2363 | if (ElementNo < E->getNumInits()) { |
| 2364 | if (!EvaluateConstantExpression( |
| 2365 | Result.getStructField((*Field)->getFieldIndex()), |
| 2366 | Info, Subobject, E->getInit(ElementNo++))) |
| 2367 | return false; |
| 2368 | } else { |
| 2369 | // Perform an implicit value-initialization for members beyond the end of |
| 2370 | // the initializer list. |
| 2371 | ImplicitValueInitExpr VIE(Field->getType()); |
| 2372 | if (!EvaluateConstantExpression( |
| 2373 | Result.getStructField((*Field)->getFieldIndex()), |
| 2374 | Info, Subobject, &VIE)) |
| 2375 | return false; |
| 2376 | } |
| 2377 | } |
| 2378 | |
| 2379 | return true; |
| 2380 | } |
| 2381 | |
| 2382 | bool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 2383 | const CXXConstructorDecl *FD = E->getConstructor(); |
| 2384 | const FunctionDecl *Definition = 0; |
| 2385 | FD->getBody(Definition); |
| 2386 | |
| 2387 | if (!Definition || !Definition->isConstexpr() || Definition->isInvalidDecl()) |
| 2388 | return false; |
| 2389 | |
| 2390 | // FIXME: Elide the copy/move construction wherever we can. |
| 2391 | if (E->isElidable()) |
| 2392 | if (const MaterializeTemporaryExpr *ME |
| 2393 | = dyn_cast<MaterializeTemporaryExpr>(E->getArg(0))) |
| 2394 | return Visit(ME->GetTemporaryExpr()); |
| 2395 | |
| 2396 | llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs()); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2397 | return HandleConstructorCall(This, Args, cast<CXXConstructorDecl>(Definition), |
| 2398 | Info, Result); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2399 | } |
| 2400 | |
| 2401 | static bool EvaluateRecord(const Expr *E, const LValue &This, |
| 2402 | APValue &Result, EvalInfo &Info) { |
| 2403 | assert(E->isRValue() && E->getType()->isRecordType() && |
| 2404 | E->getType()->isLiteralType() && |
| 2405 | "can't evaluate expression as a record rvalue"); |
| 2406 | return RecordExprEvaluator(Info, This, Result).Visit(E); |
| 2407 | } |
| 2408 | |
| 2409 | //===----------------------------------------------------------------------===// |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2410 | // Temporary Evaluation |
| 2411 | // |
| 2412 | // Temporaries are represented in the AST as rvalues, but generally behave like |
| 2413 | // lvalues. The full-object of which the temporary is a subobject is implicitly |
| 2414 | // materialized so that a reference can bind to it. |
| 2415 | //===----------------------------------------------------------------------===// |
| 2416 | namespace { |
| 2417 | class TemporaryExprEvaluator |
| 2418 | : public LValueExprEvaluatorBase<TemporaryExprEvaluator> { |
| 2419 | public: |
| 2420 | TemporaryExprEvaluator(EvalInfo &Info, LValue &Result) : |
| 2421 | LValueExprEvaluatorBaseTy(Info, Result) {} |
| 2422 | |
| 2423 | /// Visit an expression which constructs the value of this temporary. |
| 2424 | bool VisitConstructExpr(const Expr *E) { |
| 2425 | Result.set(E, Info.CurrentCall); |
| 2426 | return EvaluateConstantExpression(Info.CurrentCall->Temporaries[E], Info, |
| 2427 | Result, E); |
| 2428 | } |
| 2429 | |
| 2430 | bool VisitCastExpr(const CastExpr *E) { |
| 2431 | switch (E->getCastKind()) { |
| 2432 | default: |
| 2433 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
| 2434 | |
| 2435 | case CK_ConstructorConversion: |
| 2436 | return VisitConstructExpr(E->getSubExpr()); |
| 2437 | } |
| 2438 | } |
| 2439 | bool VisitInitListExpr(const InitListExpr *E) { |
| 2440 | return VisitConstructExpr(E); |
| 2441 | } |
| 2442 | bool VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 2443 | return VisitConstructExpr(E); |
| 2444 | } |
| 2445 | bool VisitCallExpr(const CallExpr *E) { |
| 2446 | return VisitConstructExpr(E); |
| 2447 | } |
| 2448 | }; |
| 2449 | } // end anonymous namespace |
| 2450 | |
| 2451 | /// Evaluate an expression of record type as a temporary. |
| 2452 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) { |
| 2453 | assert(E->isRValue() && E->getType()->isRecordType() && |
| 2454 | E->getType()->isLiteralType()); |
| 2455 | return TemporaryExprEvaluator(Info, Result).Visit(E); |
| 2456 | } |
| 2457 | |
| 2458 | //===----------------------------------------------------------------------===// |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2459 | // Vector Evaluation |
| 2460 | //===----------------------------------------------------------------------===// |
| 2461 | |
| 2462 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 2463 | class VectorExprEvaluator |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2464 | : public ExprEvaluatorBase<VectorExprEvaluator, bool> { |
| 2465 | APValue &Result; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2466 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2467 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2468 | VectorExprEvaluator(EvalInfo &info, APValue &Result) |
| 2469 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2470 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2471 | bool Success(const ArrayRef<APValue> &V, const Expr *E) { |
| 2472 | assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements()); |
| 2473 | // FIXME: remove this APValue copy. |
| 2474 | Result = APValue(V.data(), V.size()); |
| 2475 | return true; |
| 2476 | } |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 2477 | bool Success(const CCValue &V, const Expr *E) { |
| 2478 | assert(V.isVector()); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2479 | Result = V; |
| 2480 | return true; |
| 2481 | } |
| 2482 | bool Error(const Expr *E) { return false; } |
| 2483 | bool ValueInitialization(const Expr *E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2484 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2485 | bool VisitUnaryReal(const UnaryOperator *E) |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 2486 | { return Visit(E->getSubExpr()); } |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2487 | bool VisitCastExpr(const CastExpr* E); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2488 | bool VisitInitListExpr(const InitListExpr *E); |
| 2489 | bool VisitUnaryImag(const UnaryOperator *E); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 2490 | // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div, |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 2491 | // binary comparisons, binary and/or/xor, |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 2492 | // shufflevector, ExtVectorElementExpr |
| 2493 | // (Note that these require implementing conversions |
| 2494 | // between vector types.) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2495 | }; |
| 2496 | } // end anonymous namespace |
| 2497 | |
| 2498 | static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2499 | assert(E->isRValue() && E->getType()->isVectorType() &&"not a vector rvalue"); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2500 | return VectorExprEvaluator(Info, Result).Visit(E); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2501 | } |
| 2502 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2503 | bool VectorExprEvaluator::VisitCastExpr(const CastExpr* E) { |
| 2504 | const VectorType *VTy = E->getType()->castAs<VectorType>(); |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 2505 | unsigned NElts = VTy->getNumElements(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2506 | |
Richard Smith | d62ca37 | 2011-12-06 22:44:34 +0000 | [diff] [blame] | 2507 | const Expr *SE = E->getSubExpr(); |
Nate Begeman | e8c9e92 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 2508 | QualType SETy = SE->getType(); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2509 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2510 | switch (E->getCastKind()) { |
| 2511 | case CK_VectorSplat: { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2512 | APValue Val = APValue(); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2513 | if (SETy->isIntegerType()) { |
| 2514 | APSInt IntResult; |
| 2515 | if (!EvaluateInteger(SE, IntResult, Info)) |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2516 | return Error(E); |
| 2517 | Val = APValue(IntResult); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2518 | } else if (SETy->isRealFloatingType()) { |
| 2519 | APFloat F(0.0); |
| 2520 | if (!EvaluateFloat(SE, F, Info)) |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2521 | return Error(E); |
| 2522 | Val = APValue(F); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2523 | } else { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2524 | return Error(E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2525 | } |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 2526 | |
| 2527 | // Splat and create vector APValue. |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2528 | SmallVector<APValue, 4> Elts(NElts, Val); |
| 2529 | return Success(Elts, E); |
Nate Begeman | e8c9e92 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 2530 | } |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2531 | default: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2532 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2533 | } |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2534 | } |
| 2535 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2536 | bool |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2537 | VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2538 | const VectorType *VT = E->getType()->castAs<VectorType>(); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2539 | unsigned NumInits = E->getNumInits(); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 2540 | unsigned NumElements = VT->getNumElements(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2541 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2542 | QualType EltTy = VT->getElementType(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2543 | SmallVector<APValue, 4> Elements; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2544 | |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 2545 | // If a vector is initialized with a single element, that value |
| 2546 | // becomes every element of the vector, not just the first. |
| 2547 | // This is the behavior described in the IBM AltiVec documentation. |
| 2548 | if (NumInits == 1) { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2549 | |
| 2550 | // Handle the case where the vector is initialized by another |
Tanya Lattner | b92ae0e | 2011-04-15 22:42:59 +0000 | [diff] [blame] | 2551 | // vector (OpenCL 6.1.6). |
| 2552 | if (E->getInit(0)->getType()->isVectorType()) |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2553 | return Visit(E->getInit(0)); |
| 2554 | |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 2555 | APValue InitValue; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2556 | if (EltTy->isIntegerType()) { |
| 2557 | llvm::APSInt sInt(32); |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 2558 | if (!EvaluateInteger(E->getInit(0), sInt, Info)) |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2559 | return Error(E); |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 2560 | InitValue = APValue(sInt); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2561 | } else { |
| 2562 | llvm::APFloat f(0.0); |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 2563 | if (!EvaluateFloat(E->getInit(0), f, Info)) |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2564 | return Error(E); |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 2565 | InitValue = APValue(f); |
| 2566 | } |
| 2567 | for (unsigned i = 0; i < NumElements; i++) { |
| 2568 | Elements.push_back(InitValue); |
| 2569 | } |
| 2570 | } else { |
| 2571 | for (unsigned i = 0; i < NumElements; i++) { |
| 2572 | if (EltTy->isIntegerType()) { |
| 2573 | llvm::APSInt sInt(32); |
| 2574 | if (i < NumInits) { |
| 2575 | if (!EvaluateInteger(E->getInit(i), sInt, Info)) |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2576 | return Error(E); |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 2577 | } else { |
| 2578 | sInt = Info.Ctx.MakeIntValue(0, EltTy); |
| 2579 | } |
| 2580 | Elements.push_back(APValue(sInt)); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 2581 | } else { |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 2582 | llvm::APFloat f(0.0); |
| 2583 | if (i < NumInits) { |
| 2584 | if (!EvaluateFloat(E->getInit(i), f, Info)) |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2585 | return Error(E); |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 2586 | } else { |
| 2587 | f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)); |
| 2588 | } |
| 2589 | Elements.push_back(APValue(f)); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 2590 | } |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2591 | } |
| 2592 | } |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2593 | return Success(Elements, E); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2594 | } |
| 2595 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2596 | bool |
| 2597 | VectorExprEvaluator::ValueInitialization(const Expr *E) { |
| 2598 | const VectorType *VT = E->getType()->getAs<VectorType>(); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 2599 | QualType EltTy = VT->getElementType(); |
| 2600 | APValue ZeroElement; |
| 2601 | if (EltTy->isIntegerType()) |
| 2602 | ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy)); |
| 2603 | else |
| 2604 | ZeroElement = |
| 2605 | APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy))); |
| 2606 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2607 | SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2608 | return Success(Elements, E); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 2609 | } |
| 2610 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2611 | bool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 2612 | VisitIgnoredValue(E->getSubExpr()); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2613 | return ValueInitialization(E); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 2614 | } |
| 2615 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2616 | //===----------------------------------------------------------------------===// |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2617 | // Array Evaluation |
| 2618 | //===----------------------------------------------------------------------===// |
| 2619 | |
| 2620 | namespace { |
| 2621 | class ArrayExprEvaluator |
| 2622 | : public ExprEvaluatorBase<ArrayExprEvaluator, bool> { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2623 | const LValue &This; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2624 | APValue &Result; |
| 2625 | public: |
| 2626 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2627 | ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result) |
| 2628 | : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {} |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2629 | |
| 2630 | bool Success(const APValue &V, const Expr *E) { |
| 2631 | assert(V.isArray() && "Expected array type"); |
| 2632 | Result = V; |
| 2633 | return true; |
| 2634 | } |
| 2635 | bool Error(const Expr *E) { return false; } |
| 2636 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2637 | bool ValueInitialization(const Expr *E) { |
| 2638 | const ConstantArrayType *CAT = |
| 2639 | Info.Ctx.getAsConstantArrayType(E->getType()); |
| 2640 | if (!CAT) |
| 2641 | return false; |
| 2642 | |
| 2643 | Result = APValue(APValue::UninitArray(), 0, |
| 2644 | CAT->getSize().getZExtValue()); |
| 2645 | if (!Result.hasArrayFiller()) return true; |
| 2646 | |
| 2647 | // Value-initialize all elements. |
| 2648 | LValue Subobject = This; |
| 2649 | Subobject.Designator.addIndex(0); |
| 2650 | ImplicitValueInitExpr VIE(CAT->getElementType()); |
| 2651 | return EvaluateConstantExpression(Result.getArrayFiller(), Info, |
| 2652 | Subobject, &VIE); |
| 2653 | } |
| 2654 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2655 | bool VisitInitListExpr(const InitListExpr *E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2656 | bool VisitCXXConstructExpr(const CXXConstructExpr *E); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2657 | }; |
| 2658 | } // end anonymous namespace |
| 2659 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2660 | static bool EvaluateArray(const Expr *E, const LValue &This, |
| 2661 | APValue &Result, EvalInfo &Info) { |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2662 | assert(E->isRValue() && E->getType()->isArrayType() && |
| 2663 | E->getType()->isLiteralType() && "not a literal array rvalue"); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2664 | return ArrayExprEvaluator(Info, This, Result).Visit(E); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2665 | } |
| 2666 | |
| 2667 | bool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 2668 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType()); |
| 2669 | if (!CAT) |
| 2670 | return false; |
| 2671 | |
| 2672 | Result = APValue(APValue::UninitArray(), E->getNumInits(), |
| 2673 | CAT->getSize().getZExtValue()); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2674 | LValue Subobject = This; |
| 2675 | Subobject.Designator.addIndex(0); |
| 2676 | unsigned Index = 0; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2677 | for (InitListExpr::const_iterator I = E->begin(), End = E->end(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2678 | I != End; ++I, ++Index) { |
| 2679 | if (!EvaluateConstantExpression(Result.getArrayInitializedElt(Index), |
| 2680 | Info, Subobject, cast<Expr>(*I))) |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2681 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2682 | if (!HandleLValueArrayAdjustment(Info, Subobject, CAT->getElementType(), 1)) |
| 2683 | return false; |
| 2684 | } |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2685 | |
| 2686 | if (!Result.hasArrayFiller()) return true; |
| 2687 | assert(E->hasArrayFiller() && "no array filler for incomplete init list"); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2688 | // FIXME: The Subobject here isn't necessarily right. This rarely matters, |
| 2689 | // but sometimes does: |
| 2690 | // struct S { constexpr S() : p(&p) {} void *p; }; |
| 2691 | // S s[10] = {}; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2692 | return EvaluateConstantExpression(Result.getArrayFiller(), Info, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2693 | Subobject, E->getArrayFiller()); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2694 | } |
| 2695 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2696 | bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 2697 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType()); |
| 2698 | if (!CAT) |
| 2699 | return false; |
| 2700 | |
| 2701 | Result = APValue(APValue::UninitArray(), 0, CAT->getSize().getZExtValue()); |
| 2702 | if (!Result.hasArrayFiller()) |
| 2703 | return true; |
| 2704 | |
| 2705 | const CXXConstructorDecl *FD = E->getConstructor(); |
| 2706 | const FunctionDecl *Definition = 0; |
| 2707 | FD->getBody(Definition); |
| 2708 | |
| 2709 | if (!Definition || !Definition->isConstexpr() || Definition->isInvalidDecl()) |
| 2710 | return false; |
| 2711 | |
| 2712 | // FIXME: The Subobject here isn't necessarily right. This rarely matters, |
| 2713 | // but sometimes does: |
| 2714 | // struct S { constexpr S() : p(&p) {} void *p; }; |
| 2715 | // S s[10]; |
| 2716 | LValue Subobject = This; |
| 2717 | Subobject.Designator.addIndex(0); |
| 2718 | llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs()); |
| 2719 | return HandleConstructorCall(Subobject, Args, |
| 2720 | cast<CXXConstructorDecl>(Definition), |
| 2721 | Info, Result.getArrayFiller()); |
| 2722 | } |
| 2723 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2724 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2725 | // Integer Evaluation |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2726 | // |
| 2727 | // As a GNU extension, we support casting pointers to sufficiently-wide integer |
| 2728 | // types and back in constant folding. Integer values are thus represented |
| 2729 | // either as an integer-valued APValue, or as an lvalue-valued APValue. |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2730 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2731 | |
| 2732 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 2733 | class IntExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2734 | : public ExprEvaluatorBase<IntExprEvaluator, bool> { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2735 | CCValue &Result; |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 2736 | public: |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2737 | IntExprEvaluator(EvalInfo &info, CCValue &result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2738 | : ExprEvaluatorBaseTy(info), Result(result) {} |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2739 | |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 2740 | bool Success(const llvm::APSInt &SI, const Expr *E) { |
| 2741 | assert(E->getType()->isIntegralOrEnumerationType() && |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2742 | "Invalid evaluation result."); |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 2743 | assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 2744 | "Invalid evaluation result."); |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 2745 | assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 2746 | "Invalid evaluation result."); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2747 | Result = CCValue(SI); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 2748 | return true; |
| 2749 | } |
| 2750 | |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2751 | bool Success(const llvm::APInt &I, const Expr *E) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2752 | assert(E->getType()->isIntegralOrEnumerationType() && |
| 2753 | "Invalid evaluation result."); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 2754 | assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 2755 | "Invalid evaluation result."); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2756 | Result = CCValue(APSInt(I)); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 2757 | Result.getInt().setIsUnsigned( |
| 2758 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2759 | return true; |
| 2760 | } |
| 2761 | |
| 2762 | bool Success(uint64_t Value, const Expr *E) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2763 | assert(E->getType()->isIntegralOrEnumerationType() && |
| 2764 | "Invalid evaluation result."); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2765 | Result = CCValue(Info.Ctx.MakeIntValue(Value, E->getType())); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2766 | return true; |
| 2767 | } |
| 2768 | |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 2769 | bool Success(CharUnits Size, const Expr *E) { |
| 2770 | return Success(Size.getQuantity(), E); |
| 2771 | } |
| 2772 | |
| 2773 | |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 2774 | bool Error(SourceLocation L, diag::kind D, const Expr *E) { |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 2775 | // Take the first error. |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 2776 | if (Info.EvalStatus.Diag == 0) { |
| 2777 | Info.EvalStatus.DiagLoc = L; |
| 2778 | Info.EvalStatus.Diag = D; |
| 2779 | Info.EvalStatus.DiagExpr = E; |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 2780 | } |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 2781 | return false; |
Chris Lattner | 7a76778 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 2782 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2783 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2784 | bool Success(const CCValue &V, const Expr *E) { |
Richard Smith | 342f1f8 | 2011-10-29 22:55:55 +0000 | [diff] [blame] | 2785 | if (V.isLValue()) { |
| 2786 | Result = V; |
| 2787 | return true; |
| 2788 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2789 | return Success(V.getInt(), E); |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 2790 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2791 | bool Error(const Expr *E) { |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 2792 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 2793 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2794 | |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2795 | bool ValueInitialization(const Expr *E) { return Success(0, E); } |
| 2796 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2797 | //===--------------------------------------------------------------------===// |
| 2798 | // Visitor Methods |
| 2799 | //===--------------------------------------------------------------------===// |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 2800 | |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 2801 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2802 | return Success(E->getValue(), E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 2803 | } |
| 2804 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2805 | return Success(E->getValue(), E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 2806 | } |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 2807 | |
| 2808 | bool CheckReferencedDecl(const Expr *E, const Decl *D); |
| 2809 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2810 | if (CheckReferencedDecl(E, E->getDecl())) |
| 2811 | return true; |
| 2812 | |
| 2813 | return ExprEvaluatorBaseTy::VisitDeclRefExpr(E); |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 2814 | } |
| 2815 | bool VisitMemberExpr(const MemberExpr *E) { |
| 2816 | if (CheckReferencedDecl(E, E->getMemberDecl())) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2817 | VisitIgnoredValue(E->getBase()); |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 2818 | return true; |
| 2819 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2820 | |
| 2821 | return ExprEvaluatorBaseTy::VisitMemberExpr(E); |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 2822 | } |
| 2823 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2824 | bool VisitCallExpr(const CallExpr *E); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 2825 | bool VisitBinaryOperator(const BinaryOperator *E); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 2826 | bool VisitOffsetOfExpr(const OffsetOfExpr *E); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 2827 | bool VisitUnaryOperator(const UnaryOperator *E); |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 2828 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2829 | bool VisitCastExpr(const CastExpr* E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2830 | bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E); |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2831 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 2832 | bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2833 | return Success(E->getValue(), E); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 2834 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2835 | |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2836 | // Note, GNU defines __null as an integer, not a pointer. |
Anders Carlsson | 3f70456 | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 2837 | bool VisitGNUNullExpr(const GNUNullExpr *E) { |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2838 | return ValueInitialization(E); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 2839 | } |
| 2840 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2841 | bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) { |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2842 | return Success(E->getValue(), E); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2843 | } |
| 2844 | |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2845 | bool VisitBinaryTypeTraitExpr(const BinaryTypeTraitExpr *E) { |
| 2846 | return Success(E->getValue(), E); |
| 2847 | } |
| 2848 | |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 2849 | bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { |
| 2850 | return Success(E->getValue(), E); |
| 2851 | } |
| 2852 | |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 2853 | bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { |
| 2854 | return Success(E->getValue(), E); |
| 2855 | } |
| 2856 | |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 2857 | bool VisitUnaryReal(const UnaryOperator *E); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 2858 | bool VisitUnaryImag(const UnaryOperator *E); |
| 2859 | |
Sebastian Redl | 295995c | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 2860 | bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E); |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 2861 | bool VisitSizeOfPackExpr(const SizeOfPackExpr *E); |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 2862 | |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 2863 | private: |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 2864 | CharUnits GetAlignOfExpr(const Expr *E); |
| 2865 | CharUnits GetAlignOfType(QualType T); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2866 | static QualType GetObjectType(APValue::LValueBase B); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2867 | bool TryEvaluateBuiltinObjectSize(const CallExpr *E); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 2868 | // FIXME: Missing: array subscript of vector, member of vector |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 2869 | }; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2870 | } // end anonymous namespace |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 2871 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2872 | /// EvaluateIntegerOrLValue - Evaluate an rvalue integral-typed expression, and |
| 2873 | /// produce either the integer value or a pointer. |
| 2874 | /// |
| 2875 | /// GCC has a heinous extension which folds casts between pointer types and |
| 2876 | /// pointer-sized integral types. We support this by allowing the evaluation of |
| 2877 | /// an integer rvalue to produce a pointer (represented as an lvalue) instead. |
| 2878 | /// Some simple arithmetic on such values is supported (they are treated much |
| 2879 | /// like char*). |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2880 | static bool EvaluateIntegerOrLValue(const Expr* E, CCValue &Result, |
| 2881 | EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2882 | assert(E->isRValue() && E->getType()->isIntegralOrEnumerationType()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2883 | return IntExprEvaluator(Info, Result).Visit(E); |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 2884 | } |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 2885 | |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 2886 | static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2887 | CCValue Val; |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 2888 | if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt()) |
| 2889 | return false; |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 2890 | Result = Val.getInt(); |
| 2891 | return true; |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 2892 | } |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 2893 | |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 2894 | bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) { |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 2895 | // Enums are integer constant exprs. |
Abramo Bagnara | bfbdcd8 | 2011-06-30 09:36:05 +0000 | [diff] [blame] | 2896 | if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) { |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 2897 | // Check for signedness/width mismatches between E type and ECD value. |
| 2898 | bool SameSign = (ECD->getInitVal().isSigned() |
| 2899 | == E->getType()->isSignedIntegerOrEnumerationType()); |
| 2900 | bool SameWidth = (ECD->getInitVal().getBitWidth() |
| 2901 | == Info.Ctx.getIntWidth(E->getType())); |
| 2902 | if (SameSign && SameWidth) |
| 2903 | return Success(ECD->getInitVal(), E); |
| 2904 | else { |
| 2905 | // Get rid of mismatch (otherwise Success assertions will fail) |
| 2906 | // by computing a new value matching the type of E. |
| 2907 | llvm::APSInt Val = ECD->getInitVal(); |
| 2908 | if (!SameSign) |
| 2909 | Val.setIsSigned(!ECD->getInitVal().isSigned()); |
| 2910 | if (!SameWidth) |
| 2911 | Val = Val.extOrTrunc(Info.Ctx.getIntWidth(E->getType())); |
| 2912 | return Success(Val, E); |
| 2913 | } |
Abramo Bagnara | bfbdcd8 | 2011-06-30 09:36:05 +0000 | [diff] [blame] | 2914 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2915 | return false; |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 2916 | } |
| 2917 | |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 2918 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
| 2919 | /// as GCC. |
| 2920 | static int EvaluateBuiltinClassifyType(const CallExpr *E) { |
| 2921 | // The following enum mimics the values returned by GCC. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2922 | // FIXME: Does GCC differ between lvalue and rvalue references here? |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 2923 | enum gcc_type_class { |
| 2924 | no_type_class = -1, |
| 2925 | void_type_class, integer_type_class, char_type_class, |
| 2926 | enumeral_type_class, boolean_type_class, |
| 2927 | pointer_type_class, reference_type_class, offset_type_class, |
| 2928 | real_type_class, complex_type_class, |
| 2929 | function_type_class, method_type_class, |
| 2930 | record_type_class, union_type_class, |
| 2931 | array_type_class, string_type_class, |
| 2932 | lang_type_class |
| 2933 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2934 | |
| 2935 | // If no argument was supplied, default to "no_type_class". This isn't |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 2936 | // ideal, however it is what gcc does. |
| 2937 | if (E->getNumArgs() == 0) |
| 2938 | return no_type_class; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2939 | |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 2940 | QualType ArgTy = E->getArg(0)->getType(); |
| 2941 | if (ArgTy->isVoidType()) |
| 2942 | return void_type_class; |
| 2943 | else if (ArgTy->isEnumeralType()) |
| 2944 | return enumeral_type_class; |
| 2945 | else if (ArgTy->isBooleanType()) |
| 2946 | return boolean_type_class; |
| 2947 | else if (ArgTy->isCharType()) |
| 2948 | return string_type_class; // gcc doesn't appear to use char_type_class |
| 2949 | else if (ArgTy->isIntegerType()) |
| 2950 | return integer_type_class; |
| 2951 | else if (ArgTy->isPointerType()) |
| 2952 | return pointer_type_class; |
| 2953 | else if (ArgTy->isReferenceType()) |
| 2954 | return reference_type_class; |
| 2955 | else if (ArgTy->isRealType()) |
| 2956 | return real_type_class; |
| 2957 | else if (ArgTy->isComplexType()) |
| 2958 | return complex_type_class; |
| 2959 | else if (ArgTy->isFunctionType()) |
| 2960 | return function_type_class; |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 2961 | else if (ArgTy->isStructureOrClassType()) |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 2962 | return record_type_class; |
| 2963 | else if (ArgTy->isUnionType()) |
| 2964 | return union_type_class; |
| 2965 | else if (ArgTy->isArrayType()) |
| 2966 | return array_type_class; |
| 2967 | else if (ArgTy->isUnionType()) |
| 2968 | return union_type_class; |
| 2969 | else // FIXME: offset_type_class, method_type_class, & lang_type_class? |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2970 | llvm_unreachable("CallExpr::isBuiltinClassifyType(): unimplemented type"); |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 2971 | return -1; |
| 2972 | } |
| 2973 | |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 2974 | /// Retrieves the "underlying object type" of the given expression, |
| 2975 | /// as used by __builtin_object_size. |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2976 | QualType IntExprEvaluator::GetObjectType(APValue::LValueBase B) { |
| 2977 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
| 2978 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 2979 | return VD->getType(); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2980 | } else if (const Expr *E = B.get<const Expr*>()) { |
| 2981 | if (isa<CompoundLiteralExpr>(E)) |
| 2982 | return E->getType(); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 2983 | } |
| 2984 | |
| 2985 | return QualType(); |
| 2986 | } |
| 2987 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2988 | bool IntExprEvaluator::TryEvaluateBuiltinObjectSize(const CallExpr *E) { |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 2989 | // TODO: Perhaps we should let LLVM lower this? |
| 2990 | LValue Base; |
| 2991 | if (!EvaluatePointer(E->getArg(0), Base, Info)) |
| 2992 | return false; |
| 2993 | |
| 2994 | // If we can prove the base is null, lower to zero now. |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2995 | if (!Base.getLValueBase()) return Success(0, E); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 2996 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2997 | QualType T = GetObjectType(Base.getLValueBase()); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 2998 | if (T.isNull() || |
| 2999 | T->isIncompleteType() || |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 3000 | T->isFunctionType() || |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 3001 | T->isVariablyModifiedType() || |
| 3002 | T->isDependentType()) |
| 3003 | return false; |
| 3004 | |
| 3005 | CharUnits Size = Info.Ctx.getTypeSizeInChars(T); |
| 3006 | CharUnits Offset = Base.getLValueOffset(); |
| 3007 | |
| 3008 | if (!Offset.isNegative() && Offset <= Size) |
| 3009 | Size -= Offset; |
| 3010 | else |
| 3011 | Size = CharUnits::Zero(); |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 3012 | return Success(Size, E); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 3013 | } |
| 3014 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3015 | bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3016 | switch (E->isBuiltinCall()) { |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 3017 | default: |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3018 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 3019 | |
| 3020 | case Builtin::BI__builtin_object_size: { |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 3021 | if (TryEvaluateBuiltinObjectSize(E)) |
| 3022 | return true; |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 3023 | |
Eric Christopher | b2aaf51 | 2010-01-19 22:58:35 +0000 | [diff] [blame] | 3024 | // If evaluating the argument has side-effects we can't determine |
| 3025 | // the size of the object and lower it to unknown now. |
Fariborz Jahanian | 393c247 | 2009-11-05 18:03:03 +0000 | [diff] [blame] | 3026 | if (E->getArg(0)->HasSideEffects(Info.Ctx)) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3027 | if (E->getArg(1)->EvaluateKnownConstInt(Info.Ctx).getZExtValue() <= 1) |
Chris Lattner | cf18465 | 2009-11-03 19:48:51 +0000 | [diff] [blame] | 3028 | return Success(-1ULL, E); |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 3029 | return Success(0, E); |
| 3030 | } |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 3031 | |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 3032 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
| 3033 | } |
| 3034 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 3035 | case Builtin::BI__builtin_classify_type: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3036 | return Success(EvaluateBuiltinClassifyType(E), E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3037 | |
Richard Smith | e052d46 | 2011-12-09 02:04:48 +0000 | [diff] [blame^] | 3038 | case Builtin::BI__builtin_constant_p: { |
| 3039 | const Expr *Arg = E->getArg(0); |
| 3040 | QualType ArgType = Arg->getType(); |
| 3041 | // __builtin_constant_p always has one operand. The rules which gcc follows |
| 3042 | // are not precisely documented, but are as follows: |
| 3043 | // |
| 3044 | // - If the operand is of integral, floating, complex or enumeration type, |
| 3045 | // and can be folded to a known value of that type, it returns 1. |
| 3046 | // - If the operand and can be folded to a pointer to the first character |
| 3047 | // of a string literal (or such a pointer cast to an integral type), it |
| 3048 | // returns 1. |
| 3049 | // |
| 3050 | // Otherwise, it returns 0. |
| 3051 | // |
| 3052 | // FIXME: GCC also intends to return 1 for literals of aggregate types, but |
| 3053 | // its support for this does not currently work. |
| 3054 | int IsConstant = 0; |
| 3055 | if (ArgType->isIntegralOrEnumerationType()) { |
| 3056 | // Note, a pointer cast to an integral type is only a constant if it is |
| 3057 | // a pointer to the first character of a string literal. |
| 3058 | Expr::EvalResult Result; |
| 3059 | if (Arg->EvaluateAsRValue(Result, Info.Ctx) && !Result.HasSideEffects) { |
| 3060 | APValue &V = Result.Val; |
| 3061 | if (V.getKind() == APValue::LValue) { |
| 3062 | if (const Expr *E = V.getLValueBase().dyn_cast<const Expr*>()) |
| 3063 | IsConstant = isa<StringLiteral>(E) && V.getLValueOffset().isZero(); |
| 3064 | } else { |
| 3065 | IsConstant = 1; |
| 3066 | } |
| 3067 | } |
| 3068 | } else if (ArgType->isFloatingType() || ArgType->isAnyComplexType()) { |
| 3069 | IsConstant = Arg->isEvaluatable(Info.Ctx); |
| 3070 | } else if (ArgType->isPointerType() || Arg->isGLValue()) { |
| 3071 | LValue LV; |
| 3072 | // Use a separate EvalInfo: ignore constexpr parameter and 'this' bindings |
| 3073 | // during the check. |
| 3074 | Expr::EvalStatus Status; |
| 3075 | EvalInfo SubInfo(Info.Ctx, Status); |
| 3076 | if ((Arg->isGLValue() ? EvaluateLValue(Arg, LV, SubInfo) |
| 3077 | : EvaluatePointer(Arg, LV, SubInfo)) && |
| 3078 | !Status.HasSideEffects) |
| 3079 | if (const Expr *E = LV.getLValueBase().dyn_cast<const Expr*>()) |
| 3080 | IsConstant = isa<StringLiteral>(E) && LV.getLValueOffset().isZero(); |
| 3081 | } |
| 3082 | |
| 3083 | return Success(IsConstant, E); |
| 3084 | } |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 3085 | case Builtin::BI__builtin_eh_return_data_regno: { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3086 | int Operand = E->getArg(0)->EvaluateKnownConstInt(Info.Ctx).getZExtValue(); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3087 | Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(Operand); |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 3088 | return Success(Operand, E); |
| 3089 | } |
Eli Friedman | c4a2638 | 2010-02-13 00:10:10 +0000 | [diff] [blame] | 3090 | |
| 3091 | case Builtin::BI__builtin_expect: |
| 3092 | return Visit(E->getArg(0)); |
Douglas Gregor | 5726d40 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 3093 | |
| 3094 | case Builtin::BIstrlen: |
| 3095 | case Builtin::BI__builtin_strlen: |
| 3096 | // As an extension, we support strlen() and __builtin_strlen() as constant |
| 3097 | // expressions when the argument is a string literal. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3098 | if (const StringLiteral *S |
Douglas Gregor | 5726d40 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 3099 | = dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenImpCasts())) { |
| 3100 | // The string literal may have embedded null characters. Find the first |
| 3101 | // one and truncate there. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3102 | StringRef Str = S->getString(); |
| 3103 | StringRef::size_type Pos = Str.find(0); |
| 3104 | if (Pos != StringRef::npos) |
Douglas Gregor | 5726d40 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 3105 | Str = Str.substr(0, Pos); |
| 3106 | |
| 3107 | return Success(Str.size(), E); |
| 3108 | } |
| 3109 | |
| 3110 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
Eli Friedman | 454b57a | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 3111 | |
| 3112 | case Builtin::BI__atomic_is_lock_free: { |
| 3113 | APSInt SizeVal; |
| 3114 | if (!EvaluateInteger(E->getArg(0), SizeVal, Info)) |
| 3115 | return false; |
| 3116 | |
| 3117 | // For __atomic_is_lock_free(sizeof(_Atomic(T))), if the size is a power |
| 3118 | // of two less than the maximum inline atomic width, we know it is |
| 3119 | // lock-free. If the size isn't a power of two, or greater than the |
| 3120 | // maximum alignment where we promote atomics, we know it is not lock-free |
| 3121 | // (at least not in the sense of atomic_is_lock_free). Otherwise, |
| 3122 | // the answer can only be determined at runtime; for example, 16-byte |
| 3123 | // atomics have lock-free implementations on some, but not all, |
| 3124 | // x86-64 processors. |
| 3125 | |
| 3126 | // Check power-of-two. |
| 3127 | CharUnits Size = CharUnits::fromQuantity(SizeVal.getZExtValue()); |
| 3128 | if (!Size.isPowerOfTwo()) |
| 3129 | #if 0 |
| 3130 | // FIXME: Suppress this folding until the ABI for the promotion width |
| 3131 | // settles. |
| 3132 | return Success(0, E); |
| 3133 | #else |
| 3134 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
| 3135 | #endif |
| 3136 | |
| 3137 | #if 0 |
| 3138 | // Check against promotion width. |
| 3139 | // FIXME: Suppress this folding until the ABI for the promotion width |
| 3140 | // settles. |
| 3141 | unsigned PromoteWidthBits = |
| 3142 | Info.Ctx.getTargetInfo().getMaxAtomicPromoteWidth(); |
| 3143 | if (Size > Info.Ctx.toCharUnitsFromBits(PromoteWidthBits)) |
| 3144 | return Success(0, E); |
| 3145 | #endif |
| 3146 | |
| 3147 | // Check against inlining width. |
| 3148 | unsigned InlineWidthBits = |
| 3149 | Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth(); |
| 3150 | if (Size <= Info.Ctx.toCharUnitsFromBits(InlineWidthBits)) |
| 3151 | return Success(1, E); |
| 3152 | |
| 3153 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
| 3154 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 3155 | } |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 3156 | } |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 3157 | |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 3158 | static bool HasSameBase(const LValue &A, const LValue &B) { |
| 3159 | if (!A.getLValueBase()) |
| 3160 | return !B.getLValueBase(); |
| 3161 | if (!B.getLValueBase()) |
| 3162 | return false; |
| 3163 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 3164 | if (A.getLValueBase().getOpaqueValue() != |
| 3165 | B.getLValueBase().getOpaqueValue()) { |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 3166 | const Decl *ADecl = GetLValueBaseDecl(A); |
| 3167 | if (!ADecl) |
| 3168 | return false; |
| 3169 | const Decl *BDecl = GetLValueBaseDecl(B); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 3170 | if (!BDecl || ADecl->getCanonicalDecl() != BDecl->getCanonicalDecl()) |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 3171 | return false; |
| 3172 | } |
| 3173 | |
| 3174 | return IsGlobalLValue(A.getLValueBase()) || |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 3175 | A.getLValueFrame() == B.getLValueFrame(); |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 3176 | } |
| 3177 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 3178 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3179 | if (E->isAssignmentOp()) |
| 3180 | return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E); |
| 3181 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3182 | if (E->getOpcode() == BO_Comma) { |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 3183 | VisitIgnoredValue(E->getLHS()); |
| 3184 | return Visit(E->getRHS()); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 3185 | } |
| 3186 | |
| 3187 | if (E->isLogicalOp()) { |
| 3188 | // These need to be handled specially because the operands aren't |
| 3189 | // necessarily integral |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 3190 | bool lhsResult, rhsResult; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3191 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3192 | if (EvaluateAsBooleanCondition(E->getLHS(), lhsResult, Info)) { |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 3193 | // We were able to evaluate the LHS, see if we can get away with not |
| 3194 | // evaluating the RHS: 0 && X -> 0, 1 || X -> 1 |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3195 | if (lhsResult == (E->getOpcode() == BO_LOr)) |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 3196 | return Success(lhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 3197 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3198 | if (EvaluateAsBooleanCondition(E->getRHS(), rhsResult, Info)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3199 | if (E->getOpcode() == BO_LOr) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3200 | return Success(lhsResult || rhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 3201 | else |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3202 | return Success(lhsResult && rhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 3203 | } |
| 3204 | } else { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3205 | if (EvaluateAsBooleanCondition(E->getRHS(), rhsResult, Info)) { |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 3206 | // We can't evaluate the LHS; however, sometimes the result |
| 3207 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3208 | if (rhsResult == (E->getOpcode() == BO_LOr) || |
| 3209 | !rhsResult == (E->getOpcode() == BO_LAnd)) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3210 | // Since we weren't able to evaluate the left hand side, it |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 3211 | // must have had side effects. |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 3212 | Info.EvalStatus.HasSideEffects = true; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3213 | |
| 3214 | return Success(rhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 3215 | } |
| 3216 | } |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 3217 | } |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 3218 | |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 3219 | return false; |
| 3220 | } |
| 3221 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 3222 | QualType LHSTy = E->getLHS()->getType(); |
| 3223 | QualType RHSTy = E->getRHS()->getType(); |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 3224 | |
| 3225 | if (LHSTy->isAnyComplexType()) { |
| 3226 | assert(RHSTy->isAnyComplexType() && "Invalid comparison"); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 3227 | ComplexValue LHS, RHS; |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 3228 | |
| 3229 | if (!EvaluateComplex(E->getLHS(), LHS, Info)) |
| 3230 | return false; |
| 3231 | |
| 3232 | if (!EvaluateComplex(E->getRHS(), RHS, Info)) |
| 3233 | return false; |
| 3234 | |
| 3235 | if (LHS.isComplexFloat()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3236 | APFloat::cmpResult CR_r = |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 3237 | LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3238 | APFloat::cmpResult CR_i = |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 3239 | LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag()); |
| 3240 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3241 | if (E->getOpcode() == BO_EQ) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3242 | return Success((CR_r == APFloat::cmpEqual && |
| 3243 | CR_i == APFloat::cmpEqual), E); |
| 3244 | else { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3245 | assert(E->getOpcode() == BO_NE && |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3246 | "Invalid complex comparison."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3247 | return Success(((CR_r == APFloat::cmpGreaterThan || |
Mon P Wang | fc39dc4 | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 3248 | CR_r == APFloat::cmpLessThan || |
| 3249 | CR_r == APFloat::cmpUnordered) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3250 | (CR_i == APFloat::cmpGreaterThan || |
Mon P Wang | fc39dc4 | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 3251 | CR_i == APFloat::cmpLessThan || |
| 3252 | CR_i == APFloat::cmpUnordered)), E); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3253 | } |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 3254 | } else { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3255 | if (E->getOpcode() == BO_EQ) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3256 | return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() && |
| 3257 | LHS.getComplexIntImag() == RHS.getComplexIntImag()), E); |
| 3258 | else { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3259 | assert(E->getOpcode() == BO_NE && |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3260 | "Invalid compex comparison."); |
| 3261 | return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() || |
| 3262 | LHS.getComplexIntImag() != RHS.getComplexIntImag()), E); |
| 3263 | } |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 3264 | } |
| 3265 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3266 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 3267 | if (LHSTy->isRealFloatingType() && |
| 3268 | RHSTy->isRealFloatingType()) { |
| 3269 | APFloat RHS(0.0), LHS(0.0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3270 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 3271 | if (!EvaluateFloat(E->getRHS(), RHS, Info)) |
| 3272 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3273 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 3274 | if (!EvaluateFloat(E->getLHS(), LHS, Info)) |
| 3275 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3276 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 3277 | APFloat::cmpResult CR = LHS.compare(RHS); |
Anders Carlsson | 529569e | 2008-11-16 22:46:56 +0000 | [diff] [blame] | 3278 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 3279 | switch (E->getOpcode()) { |
| 3280 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3281 | llvm_unreachable("Invalid binary operator!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3282 | case BO_LT: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3283 | return Success(CR == APFloat::cmpLessThan, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3284 | case BO_GT: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3285 | return Success(CR == APFloat::cmpGreaterThan, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3286 | case BO_LE: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3287 | return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3288 | case BO_GE: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3289 | return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual, |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3290 | E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3291 | case BO_EQ: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3292 | return Success(CR == APFloat::cmpEqual, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3293 | case BO_NE: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3294 | return Success(CR == APFloat::cmpGreaterThan |
Mon P Wang | fc39dc4 | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 3295 | || CR == APFloat::cmpLessThan |
| 3296 | || CR == APFloat::cmpUnordered, E); |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 3297 | } |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 3298 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3299 | |
Eli Friedman | ad02d7d | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 3300 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 3301 | if (E->getOpcode() == BO_Sub || E->isComparisonOp()) { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3302 | LValue LHSValue; |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 3303 | if (!EvaluatePointer(E->getLHS(), LHSValue, Info)) |
| 3304 | return false; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 3305 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3306 | LValue RHSValue; |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 3307 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info)) |
| 3308 | return false; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 3309 | |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 3310 | // Reject differing bases from the normal codepath; we special-case |
| 3311 | // comparisons to null. |
| 3312 | if (!HasSameBase(LHSValue, RHSValue)) { |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 3313 | // Inequalities and subtractions between unrelated pointers have |
| 3314 | // unspecified or undefined behavior. |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 3315 | if (!E->isEqualityOp()) |
| 3316 | return false; |
Eli Friedman | ffbda40 | 2011-10-31 22:28:05 +0000 | [diff] [blame] | 3317 | // A constant address may compare equal to the address of a symbol. |
| 3318 | // The one exception is that address of an object cannot compare equal |
Eli Friedman | c45061b | 2011-10-31 22:54:30 +0000 | [diff] [blame] | 3319 | // to a null pointer constant. |
Eli Friedman | ffbda40 | 2011-10-31 22:28:05 +0000 | [diff] [blame] | 3320 | if ((!LHSValue.Base && !LHSValue.Offset.isZero()) || |
| 3321 | (!RHSValue.Base && !RHSValue.Offset.isZero())) |
| 3322 | return false; |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 3323 | // It's implementation-defined whether distinct literals will have |
Eli Friedman | c45061b | 2011-10-31 22:54:30 +0000 | [diff] [blame] | 3324 | // distinct addresses. In clang, we do not guarantee the addresses are |
Richard Smith | 74f4634 | 2011-11-04 01:10:57 +0000 | [diff] [blame] | 3325 | // distinct. However, we do know that the address of a literal will be |
| 3326 | // non-null. |
| 3327 | if ((IsLiteralLValue(LHSValue) || IsLiteralLValue(RHSValue)) && |
| 3328 | LHSValue.Base && RHSValue.Base) |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 3329 | return false; |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 3330 | // We can't tell whether weak symbols will end up pointing to the same |
| 3331 | // object. |
| 3332 | if (IsWeakLValue(LHSValue) || IsWeakLValue(RHSValue)) |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 3333 | return false; |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 3334 | // Pointers with different bases cannot represent the same object. |
Eli Friedman | c45061b | 2011-10-31 22:54:30 +0000 | [diff] [blame] | 3335 | // (Note that clang defaults to -fmerge-all-constants, which can |
| 3336 | // lead to inconsistent results for comparisons involving the address |
| 3337 | // of a constant; this generally doesn't matter in practice.) |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 3338 | return Success(E->getOpcode() == BO_NE, E); |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 3339 | } |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 3340 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3341 | // FIXME: Implement the C++11 restrictions: |
| 3342 | // - Pointer subtractions must be on elements of the same array. |
| 3343 | // - Pointer comparisons must be between members with the same access. |
| 3344 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3345 | if (E->getOpcode() == BO_Sub) { |
Chris Lattner | 4992bdd | 2010-04-20 17:13:14 +0000 | [diff] [blame] | 3346 | QualType Type = E->getLHS()->getType(); |
| 3347 | QualType ElementType = Type->getAs<PointerType>()->getPointeeType(); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 3348 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3349 | CharUnits ElementSize; |
| 3350 | if (!HandleSizeof(Info, ElementType, ElementSize)) |
| 3351 | return false; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 3352 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3353 | CharUnits Diff = LHSValue.getLValueOffset() - |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 3354 | RHSValue.getLValueOffset(); |
| 3355 | return Success(Diff / ElementSize, E); |
Eli Friedman | ad02d7d | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 3356 | } |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 3357 | |
| 3358 | const CharUnits &LHSOffset = LHSValue.getLValueOffset(); |
| 3359 | const CharUnits &RHSOffset = RHSValue.getLValueOffset(); |
| 3360 | switch (E->getOpcode()) { |
| 3361 | default: llvm_unreachable("missing comparison operator"); |
| 3362 | case BO_LT: return Success(LHSOffset < RHSOffset, E); |
| 3363 | case BO_GT: return Success(LHSOffset > RHSOffset, E); |
| 3364 | case BO_LE: return Success(LHSOffset <= RHSOffset, E); |
| 3365 | case BO_GE: return Success(LHSOffset >= RHSOffset, E); |
| 3366 | case BO_EQ: return Success(LHSOffset == RHSOffset, E); |
| 3367 | case BO_NE: return Success(LHSOffset != RHSOffset, E); |
Eli Friedman | ad02d7d | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 3368 | } |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 3369 | } |
| 3370 | } |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3371 | if (!LHSTy->isIntegralOrEnumerationType() || |
| 3372 | !RHSTy->isIntegralOrEnumerationType()) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3373 | // We can't continue from here for non-integral types. |
| 3374 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 3375 | } |
| 3376 | |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 3377 | // The LHS of a constant expr is always evaluated and needed. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3378 | CCValue LHSVal; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3379 | if (!EvaluateIntegerOrLValue(E->getLHS(), LHSVal, Info)) |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 3380 | return false; // error in subexpression. |
Eli Friedman | d9f4bcd | 2008-07-27 05:46:18 +0000 | [diff] [blame] | 3381 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3382 | if (!Visit(E->getRHS())) |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 3383 | return false; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3384 | CCValue &RHSVal = Result; |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 3385 | |
| 3386 | // Handle cases like (unsigned long)&a + 4. |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3387 | if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) { |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 3388 | CharUnits AdditionalOffset = CharUnits::fromQuantity( |
| 3389 | RHSVal.getInt().getZExtValue()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3390 | if (E->getOpcode() == BO_Add) |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3391 | LHSVal.getLValueOffset() += AdditionalOffset; |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 3392 | else |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3393 | LHSVal.getLValueOffset() -= AdditionalOffset; |
| 3394 | Result = LHSVal; |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 3395 | return true; |
| 3396 | } |
| 3397 | |
| 3398 | // Handle cases like 4 + (unsigned long)&a |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3399 | if (E->getOpcode() == BO_Add && |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3400 | RHSVal.isLValue() && LHSVal.isInt()) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3401 | RHSVal.getLValueOffset() += CharUnits::fromQuantity( |
| 3402 | LHSVal.getInt().getZExtValue()); |
| 3403 | // Note that RHSVal is Result. |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 3404 | return true; |
| 3405 | } |
| 3406 | |
| 3407 | // All the following cases expect both operands to be an integer |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3408 | if (!LHSVal.isInt() || !RHSVal.isInt()) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 3409 | return false; |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 3410 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3411 | APSInt &LHS = LHSVal.getInt(); |
| 3412 | APSInt &RHS = RHSVal.getInt(); |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 3413 | |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 3414 | switch (E->getOpcode()) { |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 3415 | default: |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 3416 | return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3417 | case BO_Mul: return Success(LHS * RHS, E); |
| 3418 | case BO_Add: return Success(LHS + RHS, E); |
| 3419 | case BO_Sub: return Success(LHS - RHS, E); |
| 3420 | case BO_And: return Success(LHS & RHS, E); |
| 3421 | case BO_Xor: return Success(LHS ^ RHS, E); |
| 3422 | case BO_Or: return Success(LHS | RHS, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3423 | case BO_Div: |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 3424 | if (RHS == 0) |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 3425 | return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3426 | return Success(LHS / RHS, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3427 | case BO_Rem: |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 3428 | if (RHS == 0) |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 3429 | return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3430 | return Success(LHS % RHS, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3431 | case BO_Shl: { |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 3432 | // During constant-folding, a negative shift is an opposite shift. |
| 3433 | if (RHS.isSigned() && RHS.isNegative()) { |
| 3434 | RHS = -RHS; |
| 3435 | goto shift_right; |
| 3436 | } |
| 3437 | |
| 3438 | shift_left: |
| 3439 | unsigned SA |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3440 | = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
| 3441 | return Success(LHS << SA, E); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 3442 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3443 | case BO_Shr: { |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 3444 | // During constant-folding, a negative shift is an opposite shift. |
| 3445 | if (RHS.isSigned() && RHS.isNegative()) { |
| 3446 | RHS = -RHS; |
| 3447 | goto shift_left; |
| 3448 | } |
| 3449 | |
| 3450 | shift_right: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3451 | unsigned SA = |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3452 | (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
| 3453 | return Success(LHS >> SA, E); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 3454 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3455 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3456 | case BO_LT: return Success(LHS < RHS, E); |
| 3457 | case BO_GT: return Success(LHS > RHS, E); |
| 3458 | case BO_LE: return Success(LHS <= RHS, E); |
| 3459 | case BO_GE: return Success(LHS >= RHS, E); |
| 3460 | case BO_EQ: return Success(LHS == RHS, E); |
| 3461 | case BO_NE: return Success(LHS != RHS, E); |
Eli Friedman | b11e778 | 2008-11-13 02:13:11 +0000 | [diff] [blame] | 3462 | } |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 3463 | } |
| 3464 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 3465 | CharUnits IntExprEvaluator::GetAlignOfType(QualType T) { |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 3466 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 3467 | // the result is the size of the referenced type." |
| 3468 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 3469 | // result shall be the alignment of the referenced type." |
| 3470 | if (const ReferenceType *Ref = T->getAs<ReferenceType>()) |
| 3471 | T = Ref->getPointeeType(); |
Chad Rosier | 9f1210c | 2011-07-26 07:03:04 +0000 | [diff] [blame] | 3472 | |
| 3473 | // __alignof is defined to return the preferred alignment. |
| 3474 | return Info.Ctx.toCharUnitsFromBits( |
| 3475 | Info.Ctx.getPreferredTypeAlign(T.getTypePtr())); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 3476 | } |
| 3477 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 3478 | CharUnits IntExprEvaluator::GetAlignOfExpr(const Expr *E) { |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 3479 | E = E->IgnoreParens(); |
| 3480 | |
| 3481 | // alignof decl is always accepted, even if it doesn't make sense: we default |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3482 | // to 1 in those cases. |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 3483 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 3484 | return Info.Ctx.getDeclAlign(DRE->getDecl(), |
| 3485 | /*RefAsPointee*/true); |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 3486 | |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 3487 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 3488 | return Info.Ctx.getDeclAlign(ME->getMemberDecl(), |
| 3489 | /*RefAsPointee*/true); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 3490 | |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 3491 | return GetAlignOfType(E->getType()); |
| 3492 | } |
| 3493 | |
| 3494 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3495 | /// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with |
| 3496 | /// a result as the expression's type. |
| 3497 | bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr( |
| 3498 | const UnaryExprOrTypeTraitExpr *E) { |
| 3499 | switch(E->getKind()) { |
| 3500 | case UETT_AlignOf: { |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 3501 | if (E->isArgumentType()) |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 3502 | return Success(GetAlignOfType(E->getArgumentType()), E); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 3503 | else |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 3504 | return Success(GetAlignOfExpr(E->getArgumentExpr()), E); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 3505 | } |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 3506 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3507 | case UETT_VecStep: { |
| 3508 | QualType Ty = E->getTypeOfArgument(); |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 3509 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3510 | if (Ty->isVectorType()) { |
| 3511 | unsigned n = Ty->getAs<VectorType>()->getNumElements(); |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 3512 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3513 | // The vec_step built-in functions that take a 3-component |
| 3514 | // vector return 4. (OpenCL 1.1 spec 6.11.12) |
| 3515 | if (n == 3) |
| 3516 | n = 4; |
Eli Friedman | f2da9df | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 3517 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3518 | return Success(n, E); |
| 3519 | } else |
| 3520 | return Success(1, E); |
| 3521 | } |
| 3522 | |
| 3523 | case UETT_SizeOf: { |
| 3524 | QualType SrcTy = E->getTypeOfArgument(); |
| 3525 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 3526 | // the result is the size of the referenced type." |
| 3527 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 3528 | // result shall be the alignment of the referenced type." |
| 3529 | if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>()) |
| 3530 | SrcTy = Ref->getPointeeType(); |
| 3531 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3532 | CharUnits Sizeof; |
| 3533 | if (!HandleSizeof(Info, SrcTy, Sizeof)) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3534 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3535 | return Success(Sizeof, E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3536 | } |
| 3537 | } |
| 3538 | |
| 3539 | llvm_unreachable("unknown expr/type trait"); |
| 3540 | return false; |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 3541 | } |
| 3542 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3543 | bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) { |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 3544 | CharUnits Result; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3545 | unsigned n = OOE->getNumComponents(); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 3546 | if (n == 0) |
| 3547 | return false; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3548 | QualType CurrentType = OOE->getTypeSourceInfo()->getType(); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 3549 | for (unsigned i = 0; i != n; ++i) { |
| 3550 | OffsetOfExpr::OffsetOfNode ON = OOE->getComponent(i); |
| 3551 | switch (ON.getKind()) { |
| 3552 | case OffsetOfExpr::OffsetOfNode::Array: { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3553 | const Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex()); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 3554 | APSInt IdxResult; |
| 3555 | if (!EvaluateInteger(Idx, IdxResult, Info)) |
| 3556 | return false; |
| 3557 | const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType); |
| 3558 | if (!AT) |
| 3559 | return false; |
| 3560 | CurrentType = AT->getElementType(); |
| 3561 | CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType); |
| 3562 | Result += IdxResult.getSExtValue() * ElementSize; |
| 3563 | break; |
| 3564 | } |
| 3565 | |
| 3566 | case OffsetOfExpr::OffsetOfNode::Field: { |
| 3567 | FieldDecl *MemberDecl = ON.getField(); |
| 3568 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
| 3569 | if (!RT) |
| 3570 | return false; |
| 3571 | RecordDecl *RD = RT->getDecl(); |
| 3572 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 3573 | unsigned i = MemberDecl->getFieldIndex(); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 3574 | assert(i < RL.getFieldCount() && "offsetof field in wrong type"); |
Ken Dyck | fb1e3bc | 2011-01-18 01:56:16 +0000 | [diff] [blame] | 3575 | Result += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i)); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 3576 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 3577 | break; |
| 3578 | } |
| 3579 | |
| 3580 | case OffsetOfExpr::OffsetOfNode::Identifier: |
| 3581 | llvm_unreachable("dependent __builtin_offsetof"); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 3582 | return false; |
| 3583 | |
| 3584 | case OffsetOfExpr::OffsetOfNode::Base: { |
| 3585 | CXXBaseSpecifier *BaseSpec = ON.getBase(); |
| 3586 | if (BaseSpec->isVirtual()) |
| 3587 | return false; |
| 3588 | |
| 3589 | // Find the layout of the class whose base we are looking into. |
| 3590 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
| 3591 | if (!RT) |
| 3592 | return false; |
| 3593 | RecordDecl *RD = RT->getDecl(); |
| 3594 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
| 3595 | |
| 3596 | // Find the base class itself. |
| 3597 | CurrentType = BaseSpec->getType(); |
| 3598 | const RecordType *BaseRT = CurrentType->getAs<RecordType>(); |
| 3599 | if (!BaseRT) |
| 3600 | return false; |
| 3601 | |
| 3602 | // Add the offset to the base. |
Ken Dyck | 7c7f820 | 2011-01-26 02:17:08 +0000 | [diff] [blame] | 3603 | Result += RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl())); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 3604 | break; |
| 3605 | } |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 3606 | } |
| 3607 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3608 | return Success(Result, OOE); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 3609 | } |
| 3610 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 3611 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3612 | if (E->getOpcode() == UO_LNot) { |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 3613 | // LNot's operand isn't necessarily an integer, so we handle it specially. |
| 3614 | bool bres; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3615 | if (!EvaluateAsBooleanCondition(E->getSubExpr(), bres, Info)) |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 3616 | return false; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3617 | return Success(!bres, E); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 3618 | } |
| 3619 | |
Daniel Dunbar | 4fff481 | 2009-02-21 18:14:20 +0000 | [diff] [blame] | 3620 | // Only handle integral operations... |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3621 | if (!E->getSubExpr()->getType()->isIntegralOrEnumerationType()) |
Daniel Dunbar | 4fff481 | 2009-02-21 18:14:20 +0000 | [diff] [blame] | 3622 | return false; |
| 3623 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3624 | // Get the operand value. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3625 | CCValue Val; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3626 | if (!Evaluate(Val, Info, E->getSubExpr())) |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 3627 | return false; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 3628 | |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 3629 | switch (E->getOpcode()) { |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 3630 | default: |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 3631 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 3632 | // See C99 6.6p3. |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 3633 | return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3634 | case UO_Extension: |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 3635 | // FIXME: Should extension allow i-c-e extension expressions in its scope? |
| 3636 | // If so, we could clear the diagnostic ID. |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3637 | return Success(Val, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3638 | case UO_Plus: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3639 | // The result is just the value. |
| 3640 | return Success(Val, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3641 | case UO_Minus: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3642 | if (!Val.isInt()) return false; |
| 3643 | return Success(-Val.getInt(), E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3644 | case UO_Not: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3645 | if (!Val.isInt()) return false; |
| 3646 | return Success(~Val.getInt(), E); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 3647 | } |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 3648 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3649 | |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 3650 | /// HandleCast - This is used to evaluate implicit or explicit casts where the |
| 3651 | /// result type is integer. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3652 | bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 3653 | const Expr *SubExpr = E->getSubExpr(); |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 3654 | QualType DestType = E->getType(); |
Daniel Dunbar | b92dac8 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 3655 | QualType SrcType = SubExpr->getType(); |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 3656 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3657 | switch (E->getCastKind()) { |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3658 | case CK_BaseToDerived: |
| 3659 | case CK_DerivedToBase: |
| 3660 | case CK_UncheckedDerivedToBase: |
| 3661 | case CK_Dynamic: |
| 3662 | case CK_ToUnion: |
| 3663 | case CK_ArrayToPointerDecay: |
| 3664 | case CK_FunctionToPointerDecay: |
| 3665 | case CK_NullToPointer: |
| 3666 | case CK_NullToMemberPointer: |
| 3667 | case CK_BaseToDerivedMemberPointer: |
| 3668 | case CK_DerivedToBaseMemberPointer: |
| 3669 | case CK_ConstructorConversion: |
| 3670 | case CK_IntegralToPointer: |
| 3671 | case CK_ToVoid: |
| 3672 | case CK_VectorSplat: |
| 3673 | case CK_IntegralToFloating: |
| 3674 | case CK_FloatingCast: |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3675 | case CK_CPointerToObjCPointerCast: |
| 3676 | case CK_BlockPointerToObjCPointerCast: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3677 | case CK_AnyPointerToBlockPointerCast: |
| 3678 | case CK_ObjCObjectLValueCast: |
| 3679 | case CK_FloatingRealToComplex: |
| 3680 | case CK_FloatingComplexToReal: |
| 3681 | case CK_FloatingComplexCast: |
| 3682 | case CK_FloatingComplexToIntegralComplex: |
| 3683 | case CK_IntegralRealToComplex: |
| 3684 | case CK_IntegralComplexCast: |
| 3685 | case CK_IntegralComplexToFloatingComplex: |
| 3686 | llvm_unreachable("invalid cast kind for integral value"); |
| 3687 | |
Eli Friedman | e50c297 | 2011-03-25 19:07:11 +0000 | [diff] [blame] | 3688 | case CK_BitCast: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3689 | case CK_Dependent: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3690 | case CK_LValueBitCast: |
| 3691 | case CK_UserDefinedConversion: |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 3692 | case CK_ARCProduceObject: |
| 3693 | case CK_ARCConsumeObject: |
| 3694 | case CK_ARCReclaimReturnedObject: |
| 3695 | case CK_ARCExtendBlockObject: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3696 | return false; |
| 3697 | |
| 3698 | case CK_LValueToRValue: |
| 3699 | case CK_NoOp: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3700 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3701 | |
| 3702 | case CK_MemberPointerToBoolean: |
| 3703 | case CK_PointerToBoolean: |
| 3704 | case CK_IntegralToBoolean: |
| 3705 | case CK_FloatingToBoolean: |
| 3706 | case CK_FloatingComplexToBoolean: |
| 3707 | case CK_IntegralComplexToBoolean: { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3708 | bool BoolResult; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3709 | if (!EvaluateAsBooleanCondition(SubExpr, BoolResult, Info)) |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3710 | return false; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3711 | return Success(BoolResult, E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3712 | } |
| 3713 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3714 | case CK_IntegralCast: { |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 3715 | if (!Visit(SubExpr)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 3716 | return false; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 3717 | |
Eli Friedman | be26570 | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 3718 | if (!Result.isInt()) { |
| 3719 | // Only allow casts of lvalues if they are lossless. |
| 3720 | return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType); |
| 3721 | } |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 3722 | |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 3723 | return Success(HandleIntToIntCast(DestType, SrcType, |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 3724 | Result.getInt(), Info.Ctx), E); |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 3725 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3726 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3727 | case CK_PointerToIntegral: { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3728 | LValue LV; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 3729 | if (!EvaluatePointer(SubExpr, LV, Info)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 3730 | return false; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3731 | |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 3732 | if (LV.getLValueBase()) { |
| 3733 | // Only allow based lvalue casts if they are lossless. |
| 3734 | if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType)) |
| 3735 | return false; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3736 | |
Richard Smith | b755a9d | 2011-11-16 07:18:12 +0000 | [diff] [blame] | 3737 | LV.Designator.setInvalid(); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3738 | LV.moveInto(Result); |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 3739 | return true; |
| 3740 | } |
| 3741 | |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 3742 | APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(), |
| 3743 | SrcType); |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 3744 | return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E); |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 3745 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3746 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3747 | case CK_IntegralComplexToReal: { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 3748 | ComplexValue C; |
Eli Friedman | 1725f68 | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 3749 | if (!EvaluateComplex(SubExpr, C, Info)) |
| 3750 | return false; |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3751 | return Success(C.getComplexIntReal(), E); |
Eli Friedman | 1725f68 | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 3752 | } |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 3753 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3754 | case CK_FloatingToIntegral: { |
| 3755 | APFloat F(0.0); |
| 3756 | if (!EvaluateFloat(SubExpr, F, Info)) |
| 3757 | return false; |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 3758 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3759 | return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E); |
| 3760 | } |
| 3761 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3762 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3763 | llvm_unreachable("unknown cast resulting in integral value"); |
| 3764 | return false; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 3765 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 3766 | |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 3767 | bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| 3768 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 3769 | ComplexValue LV; |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 3770 | if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt()) |
| 3771 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
| 3772 | return Success(LV.getComplexIntReal(), E); |
| 3773 | } |
| 3774 | |
| 3775 | return Visit(E->getSubExpr()); |
| 3776 | } |
| 3777 | |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 3778 | bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 3779 | if (E->getSubExpr()->getType()->isComplexIntegerType()) { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 3780 | ComplexValue LV; |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 3781 | if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt()) |
| 3782 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
| 3783 | return Success(LV.getComplexIntImag(), E); |
| 3784 | } |
| 3785 | |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 3786 | VisitIgnoredValue(E->getSubExpr()); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 3787 | return Success(0, E); |
| 3788 | } |
| 3789 | |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 3790 | bool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) { |
| 3791 | return Success(E->getPackLength(), E); |
| 3792 | } |
| 3793 | |
Sebastian Redl | 295995c | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 3794 | bool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) { |
| 3795 | return Success(E->getValue(), E); |
| 3796 | } |
| 3797 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3798 | //===----------------------------------------------------------------------===// |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 3799 | // Float Evaluation |
| 3800 | //===----------------------------------------------------------------------===// |
| 3801 | |
| 3802 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 3803 | class FloatExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3804 | : public ExprEvaluatorBase<FloatExprEvaluator, bool> { |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 3805 | APFloat &Result; |
| 3806 | public: |
| 3807 | FloatExprEvaluator(EvalInfo &info, APFloat &result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3808 | : ExprEvaluatorBaseTy(info), Result(result) {} |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 3809 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3810 | bool Success(const CCValue &V, const Expr *e) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3811 | Result = V.getFloat(); |
| 3812 | return true; |
| 3813 | } |
| 3814 | bool Error(const Stmt *S) { |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 3815 | return false; |
| 3816 | } |
| 3817 | |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 3818 | bool ValueInitialization(const Expr *E) { |
| 3819 | Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType())); |
| 3820 | return true; |
| 3821 | } |
| 3822 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 3823 | bool VisitCallExpr(const CallExpr *E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 3824 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 3825 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 3826 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 3827 | bool VisitFloatingLiteral(const FloatingLiteral *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3828 | bool VisitCastExpr(const CastExpr *E); |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 3829 | |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 3830 | bool VisitUnaryReal(const UnaryOperator *E); |
| 3831 | bool VisitUnaryImag(const UnaryOperator *E); |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 3832 | |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 3833 | // FIXME: Missing: array subscript of vector, member of vector, |
| 3834 | // ImplicitValueInitExpr |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 3835 | }; |
| 3836 | } // end anonymous namespace |
| 3837 | |
| 3838 | static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3839 | assert(E->isRValue() && E->getType()->isRealFloatingType()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3840 | return FloatExprEvaluator(Info, Result).Visit(E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 3841 | } |
| 3842 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3843 | static bool TryEvaluateBuiltinNaN(const ASTContext &Context, |
John McCall | db7b72a | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 3844 | QualType ResultTy, |
| 3845 | const Expr *Arg, |
| 3846 | bool SNaN, |
| 3847 | llvm::APFloat &Result) { |
| 3848 | const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts()); |
| 3849 | if (!S) return false; |
| 3850 | |
| 3851 | const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy); |
| 3852 | |
| 3853 | llvm::APInt fill; |
| 3854 | |
| 3855 | // Treat empty strings as if they were zero. |
| 3856 | if (S->getString().empty()) |
| 3857 | fill = llvm::APInt(32, 0); |
| 3858 | else if (S->getString().getAsInteger(0, fill)) |
| 3859 | return false; |
| 3860 | |
| 3861 | if (SNaN) |
| 3862 | Result = llvm::APFloat::getSNaN(Sem, false, &fill); |
| 3863 | else |
| 3864 | Result = llvm::APFloat::getQNaN(Sem, false, &fill); |
| 3865 | return true; |
| 3866 | } |
| 3867 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 3868 | bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3869 | switch (E->isBuiltinCall()) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3870 | default: |
| 3871 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
| 3872 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 3873 | case Builtin::BI__builtin_huge_val: |
| 3874 | case Builtin::BI__builtin_huge_valf: |
| 3875 | case Builtin::BI__builtin_huge_vall: |
| 3876 | case Builtin::BI__builtin_inf: |
| 3877 | case Builtin::BI__builtin_inff: |
Daniel Dunbar | 7cbed03 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 3878 | case Builtin::BI__builtin_infl: { |
| 3879 | const llvm::fltSemantics &Sem = |
| 3880 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Chris Lattner | 34a74ab | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 3881 | Result = llvm::APFloat::getInf(Sem); |
| 3882 | return true; |
Daniel Dunbar | 7cbed03 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 3883 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3884 | |
John McCall | db7b72a | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 3885 | case Builtin::BI__builtin_nans: |
| 3886 | case Builtin::BI__builtin_nansf: |
| 3887 | case Builtin::BI__builtin_nansl: |
| 3888 | return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 3889 | true, Result); |
| 3890 | |
Chris Lattner | 9e62171 | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 3891 | case Builtin::BI__builtin_nan: |
| 3892 | case Builtin::BI__builtin_nanf: |
| 3893 | case Builtin::BI__builtin_nanl: |
Mike Stump | 4572bab | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 3894 | // If this is __builtin_nan() turn this into a nan, otherwise we |
Chris Lattner | 9e62171 | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 3895 | // can't constant fold it. |
John McCall | db7b72a | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 3896 | return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 3897 | false, Result); |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 3898 | |
| 3899 | case Builtin::BI__builtin_fabs: |
| 3900 | case Builtin::BI__builtin_fabsf: |
| 3901 | case Builtin::BI__builtin_fabsl: |
| 3902 | if (!EvaluateFloat(E->getArg(0), Result, Info)) |
| 3903 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3904 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 3905 | if (Result.isNegative()) |
| 3906 | Result.changeSign(); |
| 3907 | return true; |
| 3908 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3909 | case Builtin::BI__builtin_copysign: |
| 3910 | case Builtin::BI__builtin_copysignf: |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 3911 | case Builtin::BI__builtin_copysignl: { |
| 3912 | APFloat RHS(0.); |
| 3913 | if (!EvaluateFloat(E->getArg(0), Result, Info) || |
| 3914 | !EvaluateFloat(E->getArg(1), RHS, Info)) |
| 3915 | return false; |
| 3916 | Result.copySign(RHS); |
| 3917 | return true; |
| 3918 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 3919 | } |
| 3920 | } |
| 3921 | |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 3922 | bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
Eli Friedman | 43efa31 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 3923 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 3924 | ComplexValue CV; |
| 3925 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 3926 | return false; |
| 3927 | Result = CV.FloatReal; |
| 3928 | return true; |
| 3929 | } |
| 3930 | |
| 3931 | return Visit(E->getSubExpr()); |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 3932 | } |
| 3933 | |
| 3934 | bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | 43efa31 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 3935 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 3936 | ComplexValue CV; |
| 3937 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 3938 | return false; |
| 3939 | Result = CV.FloatImag; |
| 3940 | return true; |
| 3941 | } |
| 3942 | |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 3943 | VisitIgnoredValue(E->getSubExpr()); |
Eli Friedman | 43efa31 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 3944 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(E->getType()); |
| 3945 | Result = llvm::APFloat::getZero(Sem); |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 3946 | return true; |
| 3947 | } |
| 3948 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 3949 | bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 3950 | switch (E->getOpcode()) { |
| 3951 | default: return false; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3952 | case UO_Plus: |
Richard Smith | 7993e8a | 2011-10-30 23:17:09 +0000 | [diff] [blame] | 3953 | return EvaluateFloat(E->getSubExpr(), Result, Info); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3954 | case UO_Minus: |
Richard Smith | 7993e8a | 2011-10-30 23:17:09 +0000 | [diff] [blame] | 3955 | if (!EvaluateFloat(E->getSubExpr(), Result, Info)) |
| 3956 | return false; |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 3957 | Result.changeSign(); |
| 3958 | return true; |
| 3959 | } |
| 3960 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 3961 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 3962 | bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3963 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
| 3964 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Eli Friedman | 7f92f03 | 2009-11-16 04:25:37 +0000 | [diff] [blame] | 3965 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 3966 | APFloat RHS(0.0); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 3967 | if (!EvaluateFloat(E->getLHS(), Result, Info)) |
| 3968 | return false; |
| 3969 | if (!EvaluateFloat(E->getRHS(), RHS, Info)) |
| 3970 | return false; |
| 3971 | |
| 3972 | switch (E->getOpcode()) { |
| 3973 | default: return false; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3974 | case BO_Mul: |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 3975 | Result.multiply(RHS, APFloat::rmNearestTiesToEven); |
| 3976 | return true; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3977 | case BO_Add: |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 3978 | Result.add(RHS, APFloat::rmNearestTiesToEven); |
| 3979 | return true; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3980 | case BO_Sub: |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 3981 | Result.subtract(RHS, APFloat::rmNearestTiesToEven); |
| 3982 | return true; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3983 | case BO_Div: |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 3984 | Result.divide(RHS, APFloat::rmNearestTiesToEven); |
| 3985 | return true; |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 3986 | } |
| 3987 | } |
| 3988 | |
| 3989 | bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { |
| 3990 | Result = E->getValue(); |
| 3991 | return true; |
| 3992 | } |
| 3993 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3994 | bool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 3995 | const Expr* SubExpr = E->getSubExpr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3996 | |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 3997 | switch (E->getCastKind()) { |
| 3998 | default: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3999 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 4000 | |
| 4001 | case CK_IntegralToFloating: { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4002 | APSInt IntResult; |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 4003 | if (!EvaluateInteger(SubExpr, IntResult, Info)) |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4004 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4005 | Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(), |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 4006 | IntResult, Info.Ctx); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4007 | return true; |
| 4008 | } |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 4009 | |
| 4010 | case CK_FloatingCast: { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4011 | if (!Visit(SubExpr)) |
| 4012 | return false; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 4013 | Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(), |
| 4014 | Result, Info.Ctx); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4015 | return true; |
| 4016 | } |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4017 | |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 4018 | case CK_FloatingComplexToReal: { |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4019 | ComplexValue V; |
| 4020 | if (!EvaluateComplex(SubExpr, V, Info)) |
| 4021 | return false; |
| 4022 | Result = V.getComplexFloatReal(); |
| 4023 | return true; |
| 4024 | } |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 4025 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4026 | |
| 4027 | return false; |
| 4028 | } |
| 4029 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 4030 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 4031 | // Complex Evaluation (for float and integer) |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 4032 | //===----------------------------------------------------------------------===// |
| 4033 | |
| 4034 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 4035 | class ComplexExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4036 | : public ExprEvaluatorBase<ComplexExprEvaluator, bool> { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 4037 | ComplexValue &Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4038 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 4039 | public: |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 4040 | ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4041 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
| 4042 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4043 | bool Success(const CCValue &V, const Expr *e) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4044 | Result.setFrom(V); |
| 4045 | return true; |
| 4046 | } |
| 4047 | bool Error(const Expr *E) { |
| 4048 | return false; |
| 4049 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4050 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 4051 | //===--------------------------------------------------------------------===// |
| 4052 | // Visitor Methods |
| 4053 | //===--------------------------------------------------------------------===// |
| 4054 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4055 | bool VisitImaginaryLiteral(const ImaginaryLiteral *E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4056 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4057 | bool VisitCastExpr(const CastExpr *E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4058 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 4059 | bool VisitBinaryOperator(const BinaryOperator *E); |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 4060 | bool VisitUnaryOperator(const UnaryOperator *E); |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 4061 | // FIXME Missing: ImplicitValueInitExpr, InitListExpr |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 4062 | }; |
| 4063 | } // end anonymous namespace |
| 4064 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 4065 | static bool EvaluateComplex(const Expr *E, ComplexValue &Result, |
| 4066 | EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4067 | assert(E->isRValue() && E->getType()->isAnyComplexType()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4068 | return ComplexExprEvaluator(Info, Result).Visit(E); |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 4069 | } |
| 4070 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4071 | bool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) { |
| 4072 | const Expr* SubExpr = E->getSubExpr(); |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 4073 | |
| 4074 | if (SubExpr->getType()->isRealFloatingType()) { |
| 4075 | Result.makeComplexFloat(); |
| 4076 | APFloat &Imag = Result.FloatImag; |
| 4077 | if (!EvaluateFloat(SubExpr, Imag, Info)) |
| 4078 | return false; |
| 4079 | |
| 4080 | Result.FloatReal = APFloat(Imag.getSemantics()); |
| 4081 | return true; |
| 4082 | } else { |
| 4083 | assert(SubExpr->getType()->isIntegerType() && |
| 4084 | "Unexpected imaginary literal."); |
| 4085 | |
| 4086 | Result.makeComplexInt(); |
| 4087 | APSInt &Imag = Result.IntImag; |
| 4088 | if (!EvaluateInteger(SubExpr, Imag, Info)) |
| 4089 | return false; |
| 4090 | |
| 4091 | Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned()); |
| 4092 | return true; |
| 4093 | } |
| 4094 | } |
| 4095 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4096 | bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) { |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 4097 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4098 | switch (E->getCastKind()) { |
| 4099 | case CK_BitCast: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4100 | case CK_BaseToDerived: |
| 4101 | case CK_DerivedToBase: |
| 4102 | case CK_UncheckedDerivedToBase: |
| 4103 | case CK_Dynamic: |
| 4104 | case CK_ToUnion: |
| 4105 | case CK_ArrayToPointerDecay: |
| 4106 | case CK_FunctionToPointerDecay: |
| 4107 | case CK_NullToPointer: |
| 4108 | case CK_NullToMemberPointer: |
| 4109 | case CK_BaseToDerivedMemberPointer: |
| 4110 | case CK_DerivedToBaseMemberPointer: |
| 4111 | case CK_MemberPointerToBoolean: |
| 4112 | case CK_ConstructorConversion: |
| 4113 | case CK_IntegralToPointer: |
| 4114 | case CK_PointerToIntegral: |
| 4115 | case CK_PointerToBoolean: |
| 4116 | case CK_ToVoid: |
| 4117 | case CK_VectorSplat: |
| 4118 | case CK_IntegralCast: |
| 4119 | case CK_IntegralToBoolean: |
| 4120 | case CK_IntegralToFloating: |
| 4121 | case CK_FloatingToIntegral: |
| 4122 | case CK_FloatingToBoolean: |
| 4123 | case CK_FloatingCast: |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4124 | case CK_CPointerToObjCPointerCast: |
| 4125 | case CK_BlockPointerToObjCPointerCast: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4126 | case CK_AnyPointerToBlockPointerCast: |
| 4127 | case CK_ObjCObjectLValueCast: |
| 4128 | case CK_FloatingComplexToReal: |
| 4129 | case CK_FloatingComplexToBoolean: |
| 4130 | case CK_IntegralComplexToReal: |
| 4131 | case CK_IntegralComplexToBoolean: |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 4132 | case CK_ARCProduceObject: |
| 4133 | case CK_ARCConsumeObject: |
| 4134 | case CK_ARCReclaimReturnedObject: |
| 4135 | case CK_ARCExtendBlockObject: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4136 | llvm_unreachable("invalid cast kind for complex value"); |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 4137 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4138 | case CK_LValueToRValue: |
| 4139 | case CK_NoOp: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4140 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4141 | |
| 4142 | case CK_Dependent: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4143 | case CK_LValueBitCast: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4144 | case CK_UserDefinedConversion: |
| 4145 | return false; |
| 4146 | |
| 4147 | case CK_FloatingRealToComplex: { |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 4148 | APFloat &Real = Result.FloatReal; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4149 | if (!EvaluateFloat(E->getSubExpr(), Real, Info)) |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 4150 | return false; |
| 4151 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4152 | Result.makeComplexFloat(); |
| 4153 | Result.FloatImag = APFloat(Real.getSemantics()); |
| 4154 | return true; |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 4155 | } |
| 4156 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4157 | case CK_FloatingComplexCast: { |
| 4158 | if (!Visit(E->getSubExpr())) |
| 4159 | return false; |
| 4160 | |
| 4161 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 4162 | QualType From |
| 4163 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 4164 | |
| 4165 | Result.FloatReal |
| 4166 | = HandleFloatToFloatCast(To, From, Result.FloatReal, Info.Ctx); |
| 4167 | Result.FloatImag |
| 4168 | = HandleFloatToFloatCast(To, From, Result.FloatImag, Info.Ctx); |
| 4169 | return true; |
| 4170 | } |
| 4171 | |
| 4172 | case CK_FloatingComplexToIntegralComplex: { |
| 4173 | if (!Visit(E->getSubExpr())) |
| 4174 | return false; |
| 4175 | |
| 4176 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 4177 | QualType From |
| 4178 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 4179 | Result.makeComplexInt(); |
| 4180 | Result.IntReal = HandleFloatToIntCast(To, From, Result.FloatReal, Info.Ctx); |
| 4181 | Result.IntImag = HandleFloatToIntCast(To, From, Result.FloatImag, Info.Ctx); |
| 4182 | return true; |
| 4183 | } |
| 4184 | |
| 4185 | case CK_IntegralRealToComplex: { |
| 4186 | APSInt &Real = Result.IntReal; |
| 4187 | if (!EvaluateInteger(E->getSubExpr(), Real, Info)) |
| 4188 | return false; |
| 4189 | |
| 4190 | Result.makeComplexInt(); |
| 4191 | Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned()); |
| 4192 | return true; |
| 4193 | } |
| 4194 | |
| 4195 | case CK_IntegralComplexCast: { |
| 4196 | if (!Visit(E->getSubExpr())) |
| 4197 | return false; |
| 4198 | |
| 4199 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 4200 | QualType From |
| 4201 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 4202 | |
| 4203 | Result.IntReal = HandleIntToIntCast(To, From, Result.IntReal, Info.Ctx); |
| 4204 | Result.IntImag = HandleIntToIntCast(To, From, Result.IntImag, Info.Ctx); |
| 4205 | return true; |
| 4206 | } |
| 4207 | |
| 4208 | case CK_IntegralComplexToFloatingComplex: { |
| 4209 | if (!Visit(E->getSubExpr())) |
| 4210 | return false; |
| 4211 | |
| 4212 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 4213 | QualType From |
| 4214 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 4215 | Result.makeComplexFloat(); |
| 4216 | Result.FloatReal = HandleIntToFloatCast(To, From, Result.IntReal, Info.Ctx); |
| 4217 | Result.FloatImag = HandleIntToFloatCast(To, From, Result.IntImag, Info.Ctx); |
| 4218 | return true; |
| 4219 | } |
| 4220 | } |
| 4221 | |
| 4222 | llvm_unreachable("unknown cast resulting in complex value"); |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 4223 | return false; |
| 4224 | } |
| 4225 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 4226 | bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4227 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
Richard Smith | 2ad226b | 2011-11-16 17:22:48 +0000 | [diff] [blame] | 4228 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| 4229 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 4230 | if (!Visit(E->getLHS())) |
| 4231 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4232 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 4233 | ComplexValue RHS; |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 4234 | if (!EvaluateComplex(E->getRHS(), RHS, Info)) |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 4235 | return false; |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 4236 | |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 4237 | assert(Result.isComplexFloat() == RHS.isComplexFloat() && |
| 4238 | "Invalid operands to binary operator."); |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 4239 | switch (E->getOpcode()) { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 4240 | default: return false; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4241 | case BO_Add: |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 4242 | if (Result.isComplexFloat()) { |
| 4243 | Result.getComplexFloatReal().add(RHS.getComplexFloatReal(), |
| 4244 | APFloat::rmNearestTiesToEven); |
| 4245 | Result.getComplexFloatImag().add(RHS.getComplexFloatImag(), |
| 4246 | APFloat::rmNearestTiesToEven); |
| 4247 | } else { |
| 4248 | Result.getComplexIntReal() += RHS.getComplexIntReal(); |
| 4249 | Result.getComplexIntImag() += RHS.getComplexIntImag(); |
| 4250 | } |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 4251 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4252 | case BO_Sub: |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 4253 | if (Result.isComplexFloat()) { |
| 4254 | Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(), |
| 4255 | APFloat::rmNearestTiesToEven); |
| 4256 | Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(), |
| 4257 | APFloat::rmNearestTiesToEven); |
| 4258 | } else { |
| 4259 | Result.getComplexIntReal() -= RHS.getComplexIntReal(); |
| 4260 | Result.getComplexIntImag() -= RHS.getComplexIntImag(); |
| 4261 | } |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 4262 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4263 | case BO_Mul: |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 4264 | if (Result.isComplexFloat()) { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 4265 | ComplexValue LHS = Result; |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 4266 | APFloat &LHS_r = LHS.getComplexFloatReal(); |
| 4267 | APFloat &LHS_i = LHS.getComplexFloatImag(); |
| 4268 | APFloat &RHS_r = RHS.getComplexFloatReal(); |
| 4269 | APFloat &RHS_i = RHS.getComplexFloatImag(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4270 | |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 4271 | APFloat Tmp = LHS_r; |
| 4272 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 4273 | Result.getComplexFloatReal() = Tmp; |
| 4274 | Tmp = LHS_i; |
| 4275 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 4276 | Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven); |
| 4277 | |
| 4278 | Tmp = LHS_r; |
| 4279 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 4280 | Result.getComplexFloatImag() = Tmp; |
| 4281 | Tmp = LHS_i; |
| 4282 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 4283 | Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven); |
| 4284 | } else { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 4285 | ComplexValue LHS = Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4286 | Result.getComplexIntReal() = |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 4287 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() - |
| 4288 | LHS.getComplexIntImag() * RHS.getComplexIntImag()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4289 | Result.getComplexIntImag() = |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 4290 | (LHS.getComplexIntReal() * RHS.getComplexIntImag() + |
| 4291 | LHS.getComplexIntImag() * RHS.getComplexIntReal()); |
| 4292 | } |
| 4293 | break; |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 4294 | case BO_Div: |
| 4295 | if (Result.isComplexFloat()) { |
| 4296 | ComplexValue LHS = Result; |
| 4297 | APFloat &LHS_r = LHS.getComplexFloatReal(); |
| 4298 | APFloat &LHS_i = LHS.getComplexFloatImag(); |
| 4299 | APFloat &RHS_r = RHS.getComplexFloatReal(); |
| 4300 | APFloat &RHS_i = RHS.getComplexFloatImag(); |
| 4301 | APFloat &Res_r = Result.getComplexFloatReal(); |
| 4302 | APFloat &Res_i = Result.getComplexFloatImag(); |
| 4303 | |
| 4304 | APFloat Den = RHS_r; |
| 4305 | Den.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 4306 | APFloat Tmp = RHS_i; |
| 4307 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 4308 | Den.add(Tmp, APFloat::rmNearestTiesToEven); |
| 4309 | |
| 4310 | Res_r = LHS_r; |
| 4311 | Res_r.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 4312 | Tmp = LHS_i; |
| 4313 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 4314 | Res_r.add(Tmp, APFloat::rmNearestTiesToEven); |
| 4315 | Res_r.divide(Den, APFloat::rmNearestTiesToEven); |
| 4316 | |
| 4317 | Res_i = LHS_i; |
| 4318 | Res_i.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 4319 | Tmp = LHS_r; |
| 4320 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 4321 | Res_i.subtract(Tmp, APFloat::rmNearestTiesToEven); |
| 4322 | Res_i.divide(Den, APFloat::rmNearestTiesToEven); |
| 4323 | } else { |
| 4324 | if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0) { |
| 4325 | // FIXME: what about diagnostics? |
| 4326 | return false; |
| 4327 | } |
| 4328 | ComplexValue LHS = Result; |
| 4329 | APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 4330 | RHS.getComplexIntImag() * RHS.getComplexIntImag(); |
| 4331 | Result.getComplexIntReal() = |
| 4332 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 4333 | LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den; |
| 4334 | Result.getComplexIntImag() = |
| 4335 | (LHS.getComplexIntImag() * RHS.getComplexIntReal() - |
| 4336 | LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den; |
| 4337 | } |
| 4338 | break; |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 4339 | } |
| 4340 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 4341 | return true; |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 4342 | } |
| 4343 | |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 4344 | bool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
| 4345 | // Get the operand value into 'Result'. |
| 4346 | if (!Visit(E->getSubExpr())) |
| 4347 | return false; |
| 4348 | |
| 4349 | switch (E->getOpcode()) { |
| 4350 | default: |
| 4351 | // FIXME: what about diagnostics? |
| 4352 | return false; |
| 4353 | case UO_Extension: |
| 4354 | return true; |
| 4355 | case UO_Plus: |
| 4356 | // The result is always just the subexpr. |
| 4357 | return true; |
| 4358 | case UO_Minus: |
| 4359 | if (Result.isComplexFloat()) { |
| 4360 | Result.getComplexFloatReal().changeSign(); |
| 4361 | Result.getComplexFloatImag().changeSign(); |
| 4362 | } |
| 4363 | else { |
| 4364 | Result.getComplexIntReal() = -Result.getComplexIntReal(); |
| 4365 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 4366 | } |
| 4367 | return true; |
| 4368 | case UO_Not: |
| 4369 | if (Result.isComplexFloat()) |
| 4370 | Result.getComplexFloatImag().changeSign(); |
| 4371 | else |
| 4372 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 4373 | return true; |
| 4374 | } |
| 4375 | } |
| 4376 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 4377 | //===----------------------------------------------------------------------===// |
Richard Smith | aa9c350 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 4378 | // Void expression evaluation, primarily for a cast to void on the LHS of a |
| 4379 | // comma operator |
| 4380 | //===----------------------------------------------------------------------===// |
| 4381 | |
| 4382 | namespace { |
| 4383 | class VoidExprEvaluator |
| 4384 | : public ExprEvaluatorBase<VoidExprEvaluator, bool> { |
| 4385 | public: |
| 4386 | VoidExprEvaluator(EvalInfo &Info) : ExprEvaluatorBaseTy(Info) {} |
| 4387 | |
| 4388 | bool Success(const CCValue &V, const Expr *e) { return true; } |
| 4389 | bool Error(const Expr *E) { return false; } |
| 4390 | |
| 4391 | bool VisitCastExpr(const CastExpr *E) { |
| 4392 | switch (E->getCastKind()) { |
| 4393 | default: |
| 4394 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 4395 | case CK_ToVoid: |
| 4396 | VisitIgnoredValue(E->getSubExpr()); |
| 4397 | return true; |
| 4398 | } |
| 4399 | } |
| 4400 | }; |
| 4401 | } // end anonymous namespace |
| 4402 | |
| 4403 | static bool EvaluateVoid(const Expr *E, EvalInfo &Info) { |
| 4404 | assert(E->isRValue() && E->getType()->isVoidType()); |
| 4405 | return VoidExprEvaluator(Info).Visit(E); |
| 4406 | } |
| 4407 | |
| 4408 | //===----------------------------------------------------------------------===// |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 4409 | // Top level Expr::EvaluateAsRValue method. |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4410 | //===----------------------------------------------------------------------===// |
| 4411 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4412 | static bool Evaluate(CCValue &Result, EvalInfo &Info, const Expr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4413 | // In C, function designators are not lvalues, but we evaluate them as if they |
| 4414 | // are. |
| 4415 | if (E->isGLValue() || E->getType()->isFunctionType()) { |
| 4416 | LValue LV; |
| 4417 | if (!EvaluateLValue(E, LV, Info)) |
| 4418 | return false; |
| 4419 | LV.moveInto(Result); |
| 4420 | } else if (E->getType()->isVectorType()) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 4421 | if (!EvaluateVector(E, Result, Info)) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4422 | return false; |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 4423 | } else if (E->getType()->isIntegralOrEnumerationType()) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 4424 | if (!IntExprEvaluator(Info, Result).Visit(E)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 4425 | return false; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4426 | } else if (E->getType()->hasPointerRepresentation()) { |
| 4427 | LValue LV; |
| 4428 | if (!EvaluatePointer(E, LV, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 4429 | return false; |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 4430 | LV.moveInto(Result); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4431 | } else if (E->getType()->isRealFloatingType()) { |
| 4432 | llvm::APFloat F(0.0); |
| 4433 | if (!EvaluateFloat(E, F, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 4434 | return false; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4435 | Result = CCValue(F); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4436 | } else if (E->getType()->isAnyComplexType()) { |
| 4437 | ComplexValue C; |
| 4438 | if (!EvaluateComplex(E, C, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 4439 | return false; |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 4440 | C.moveInto(Result); |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 4441 | } else if (E->getType()->isMemberPointerType()) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4442 | MemberPtr P; |
| 4443 | if (!EvaluateMemberPointer(E, P, Info)) |
| 4444 | return false; |
| 4445 | P.moveInto(Result); |
| 4446 | return true; |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 4447 | } else if (E->getType()->isArrayType() && E->getType()->isLiteralType()) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4448 | LValue LV; |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4449 | LV.set(E, Info.CurrentCall); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4450 | if (!EvaluateArray(E, LV, Info.CurrentCall->Temporaries[E], Info)) |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 4451 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4452 | Result = Info.CurrentCall->Temporaries[E]; |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 4453 | } else if (E->getType()->isRecordType() && E->getType()->isLiteralType()) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4454 | LValue LV; |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4455 | LV.set(E, Info.CurrentCall); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4456 | if (!EvaluateRecord(E, LV, Info.CurrentCall->Temporaries[E], Info)) |
| 4457 | return false; |
| 4458 | Result = Info.CurrentCall->Temporaries[E]; |
Richard Smith | aa9c350 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 4459 | } else if (E->getType()->isVoidType()) { |
| 4460 | if (!EvaluateVoid(E, Info)) |
| 4461 | return false; |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 4462 | } else |
Anders Carlsson | 9d4c157 | 2008-11-22 22:56:32 +0000 | [diff] [blame] | 4463 | return false; |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 4464 | |
Anders Carlsson | 5b45d4e | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 4465 | return true; |
| 4466 | } |
| 4467 | |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 4468 | /// EvaluateConstantExpression - Evaluate an expression as a constant expression |
| 4469 | /// in-place in an APValue. In some cases, the in-place evaluation is essential, |
| 4470 | /// since later initializers for an object can indirectly refer to subobjects |
| 4471 | /// which were initialized earlier. |
| 4472 | static bool EvaluateConstantExpression(APValue &Result, EvalInfo &Info, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4473 | const LValue &This, const Expr *E) { |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 4474 | if (E->isRValue() && E->getType()->isLiteralType()) { |
| 4475 | // Evaluate arrays and record types in-place, so that later initializers can |
| 4476 | // refer to earlier-initialized members of the object. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4477 | if (E->getType()->isArrayType()) |
| 4478 | return EvaluateArray(E, This, Result, Info); |
| 4479 | else if (E->getType()->isRecordType()) |
| 4480 | return EvaluateRecord(E, This, Result, Info); |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 4481 | } |
| 4482 | |
| 4483 | // For any other type, in-place evaluation is unimportant. |
| 4484 | CCValue CoreConstResult; |
| 4485 | return Evaluate(CoreConstResult, Info, E) && |
| 4486 | CheckConstantExpression(CoreConstResult, Result); |
| 4487 | } |
| 4488 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4489 | |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 4490 | /// EvaluateAsRValue - Return true if this is a constant which we can fold using |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4491 | /// any crazy technique (that has nothing to do with language standards) that |
| 4492 | /// we want to. If this function returns true, it returns the folded constant |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4493 | /// in Result. If this expression is a glvalue, an lvalue-to-rvalue conversion |
| 4494 | /// will be applied to the result. |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 4495 | bool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx) const { |
Richard Smith | 1445bba | 2011-11-10 03:30:42 +0000 | [diff] [blame] | 4496 | // FIXME: Evaluating initializers for large arrays can cause performance |
| 4497 | // problems, and we don't use such values yet. Once we have a more efficient |
| 4498 | // array representation, this should be reinstated, and used by CodeGen. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4499 | // The same problem affects large records. |
| 4500 | if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) && |
| 4501 | !Ctx.getLangOptions().CPlusPlus0x) |
Richard Smith | 1445bba | 2011-11-10 03:30:42 +0000 | [diff] [blame] | 4502 | return false; |
| 4503 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4504 | EvalInfo Info(Ctx, Result); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4505 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4506 | // FIXME: If this is the initializer for an lvalue, pass that in. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4507 | CCValue Value; |
| 4508 | if (!::Evaluate(Value, Info, this)) |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4509 | return false; |
| 4510 | |
| 4511 | if (isGLValue()) { |
| 4512 | LValue LV; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4513 | LV.setFrom(Value); |
| 4514 | if (!HandleLValueToRValueConversion(Info, getType(), LV, Value)) |
| 4515 | return false; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4516 | } |
| 4517 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4518 | // Check this core constant expression is a constant expression, and if so, |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 4519 | // convert it to one. |
| 4520 | return CheckConstantExpression(Value, Result.Val); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4521 | } |
| 4522 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4523 | bool Expr::EvaluateAsBooleanCondition(bool &Result, |
| 4524 | const ASTContext &Ctx) const { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4525 | EvalResult Scratch; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 4526 | return EvaluateAsRValue(Scratch, Ctx) && |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 4527 | HandleConversionToBool(CCValue(Scratch.Val, CCValue::GlobalValue()), |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4528 | Result); |
John McCall | cd7a445 | 2010-01-05 23:42:56 +0000 | [diff] [blame] | 4529 | } |
| 4530 | |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4531 | bool Expr::EvaluateAsInt(APSInt &Result, const ASTContext &Ctx) const { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4532 | EvalResult ExprResult; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 4533 | if (!EvaluateAsRValue(ExprResult, Ctx) || ExprResult.HasSideEffects || |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4534 | !ExprResult.Val.isInt()) { |
| 4535 | return false; |
| 4536 | } |
| 4537 | Result = ExprResult.Val.getInt(); |
| 4538 | return true; |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4539 | } |
| 4540 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4541 | bool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx) const { |
Anders Carlsson | 1b78276 | 2009-04-10 04:54:13 +0000 | [diff] [blame] | 4542 | EvalInfo Info(Ctx, Result); |
| 4543 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4544 | LValue LV; |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 4545 | return EvaluateLValue(this, LV, Info) && !Result.HasSideEffects && |
| 4546 | CheckLValueConstantExpression(LV, Result.Val); |
Eli Friedman | b2f295c | 2009-09-13 10:17:44 +0000 | [diff] [blame] | 4547 | } |
| 4548 | |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 4549 | /// isEvaluatable - Call EvaluateAsRValue to see if this expression can be |
| 4550 | /// constant folded, but discard the result. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4551 | bool Expr::isEvaluatable(const ASTContext &Ctx) const { |
Anders Carlsson | 4fdfb09 | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 4552 | EvalResult Result; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 4553 | return EvaluateAsRValue(Result, Ctx) && !Result.HasSideEffects; |
Chris Lattner | 45b6b9d | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 4554 | } |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 4555 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4556 | bool Expr::HasSideEffects(const ASTContext &Ctx) const { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 4557 | return HasSideEffect(Ctx).Visit(this); |
Fariborz Jahanian | 393c247 | 2009-11-05 18:03:03 +0000 | [diff] [blame] | 4558 | } |
| 4559 | |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4560 | APSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx) const { |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 4561 | EvalResult EvalResult; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 4562 | bool Result = EvaluateAsRValue(EvalResult, Ctx); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 4563 | (void)Result; |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 4564 | assert(Result && "Could not evaluate expression"); |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 4565 | assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer"); |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 4566 | |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 4567 | return EvalResult.Val.getInt(); |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 4568 | } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4569 | |
Abramo Bagnara | e17a643 | 2010-05-14 17:07:14 +0000 | [diff] [blame] | 4570 | bool Expr::EvalResult::isGlobalLValue() const { |
| 4571 | assert(Val.isLValue()); |
| 4572 | return IsGlobalLValue(Val.getLValueBase()); |
| 4573 | } |
| 4574 | |
| 4575 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4576 | /// isIntegerConstantExpr - this recursive routine will test if an expression is |
| 4577 | /// an integer constant expression. |
| 4578 | |
| 4579 | /// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, |
| 4580 | /// comma, etc |
| 4581 | /// |
| 4582 | /// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof |
| 4583 | /// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer |
| 4584 | /// cast+dereference. |
| 4585 | |
| 4586 | // CheckICE - This function does the fundamental ICE checking: the returned |
| 4587 | // ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation. |
| 4588 | // Note that to reduce code duplication, this helper does no evaluation |
| 4589 | // itself; the caller checks whether the expression is evaluatable, and |
| 4590 | // in the rare cases where CheckICE actually cares about the evaluated |
| 4591 | // value, it calls into Evalute. |
| 4592 | // |
| 4593 | // Meanings of Val: |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 4594 | // 0: This expression is an ICE. |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4595 | // 1: This expression is not an ICE, but if it isn't evaluated, it's |
| 4596 | // a legal subexpression for an ICE. This return value is used to handle |
| 4597 | // the comma operator in C99 mode. |
| 4598 | // 2: This expression is not an ICE, and is not a legal subexpression for one. |
| 4599 | |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 4600 | namespace { |
| 4601 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4602 | struct ICEDiag { |
| 4603 | unsigned Val; |
| 4604 | SourceLocation Loc; |
| 4605 | |
| 4606 | public: |
| 4607 | ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {} |
| 4608 | ICEDiag() : Val(0) {} |
| 4609 | }; |
| 4610 | |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 4611 | } |
| 4612 | |
| 4613 | static ICEDiag NoDiag() { return ICEDiag(); } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4614 | |
| 4615 | static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) { |
| 4616 | Expr::EvalResult EVResult; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 4617 | if (!E->EvaluateAsRValue(EVResult, Ctx) || EVResult.HasSideEffects || |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4618 | !EVResult.Val.isInt()) { |
| 4619 | return ICEDiag(2, E->getLocStart()); |
| 4620 | } |
| 4621 | return NoDiag(); |
| 4622 | } |
| 4623 | |
| 4624 | static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) { |
| 4625 | assert(!E->isValueDependent() && "Should not see value dependent exprs!"); |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 4626 | if (!E->getType()->isIntegralOrEnumerationType()) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4627 | return ICEDiag(2, E->getLocStart()); |
| 4628 | } |
| 4629 | |
| 4630 | switch (E->getStmtClass()) { |
John McCall | 63c00d7 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 4631 | #define ABSTRACT_STMT(Node) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4632 | #define STMT(Node, Base) case Expr::Node##Class: |
| 4633 | #define EXPR(Node, Base) |
| 4634 | #include "clang/AST/StmtNodes.inc" |
| 4635 | case Expr::PredefinedExprClass: |
| 4636 | case Expr::FloatingLiteralClass: |
| 4637 | case Expr::ImaginaryLiteralClass: |
| 4638 | case Expr::StringLiteralClass: |
| 4639 | case Expr::ArraySubscriptExprClass: |
| 4640 | case Expr::MemberExprClass: |
| 4641 | case Expr::CompoundAssignOperatorClass: |
| 4642 | case Expr::CompoundLiteralExprClass: |
| 4643 | case Expr::ExtVectorElementExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4644 | case Expr::DesignatedInitExprClass: |
| 4645 | case Expr::ImplicitValueInitExprClass: |
| 4646 | case Expr::ParenListExprClass: |
| 4647 | case Expr::VAArgExprClass: |
| 4648 | case Expr::AddrLabelExprClass: |
| 4649 | case Expr::StmtExprClass: |
| 4650 | case Expr::CXXMemberCallExprClass: |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4651 | case Expr::CUDAKernelCallExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4652 | case Expr::CXXDynamicCastExprClass: |
| 4653 | case Expr::CXXTypeidExprClass: |
Francois Pichet | 9be8840 | 2010-09-08 23:47:05 +0000 | [diff] [blame] | 4654 | case Expr::CXXUuidofExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4655 | case Expr::CXXNullPtrLiteralExprClass: |
| 4656 | case Expr::CXXThisExprClass: |
| 4657 | case Expr::CXXThrowExprClass: |
| 4658 | case Expr::CXXNewExprClass: |
| 4659 | case Expr::CXXDeleteExprClass: |
| 4660 | case Expr::CXXPseudoDestructorExprClass: |
| 4661 | case Expr::UnresolvedLookupExprClass: |
| 4662 | case Expr::DependentScopeDeclRefExprClass: |
| 4663 | case Expr::CXXConstructExprClass: |
| 4664 | case Expr::CXXBindTemporaryExprClass: |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 4665 | case Expr::ExprWithCleanupsClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4666 | case Expr::CXXTemporaryObjectExprClass: |
| 4667 | case Expr::CXXUnresolvedConstructExprClass: |
| 4668 | case Expr::CXXDependentScopeMemberExprClass: |
| 4669 | case Expr::UnresolvedMemberExprClass: |
| 4670 | case Expr::ObjCStringLiteralClass: |
| 4671 | case Expr::ObjCEncodeExprClass: |
| 4672 | case Expr::ObjCMessageExprClass: |
| 4673 | case Expr::ObjCSelectorExprClass: |
| 4674 | case Expr::ObjCProtocolExprClass: |
| 4675 | case Expr::ObjCIvarRefExprClass: |
| 4676 | case Expr::ObjCPropertyRefExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4677 | case Expr::ObjCIsaExprClass: |
| 4678 | case Expr::ShuffleVectorExprClass: |
| 4679 | case Expr::BlockExprClass: |
| 4680 | case Expr::BlockDeclRefExprClass: |
| 4681 | case Expr::NoStmtClass: |
John McCall | 7cd7d1a | 2010-11-15 23:31:06 +0000 | [diff] [blame] | 4682 | case Expr::OpaqueValueExprClass: |
Douglas Gregor | be230c3 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 4683 | case Expr::PackExpansionExprClass: |
Douglas Gregor | c7793c7 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 4684 | case Expr::SubstNonTypeTemplateParmPackExprClass: |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 4685 | case Expr::AsTypeExprClass: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4686 | case Expr::ObjCIndirectCopyRestoreExprClass: |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 4687 | case Expr::MaterializeTemporaryExprClass: |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4688 | case Expr::PseudoObjectExprClass: |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 4689 | case Expr::AtomicExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4690 | return ICEDiag(2, E->getLocStart()); |
| 4691 | |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 4692 | case Expr::InitListExprClass: |
| 4693 | if (Ctx.getLangOptions().CPlusPlus0x) { |
| 4694 | const InitListExpr *ILE = cast<InitListExpr>(E); |
| 4695 | if (ILE->getNumInits() == 0) |
| 4696 | return NoDiag(); |
| 4697 | if (ILE->getNumInits() == 1) |
| 4698 | return CheckICE(ILE->getInit(0), Ctx); |
| 4699 | // Fall through for more than 1 expression. |
| 4700 | } |
| 4701 | return ICEDiag(2, E->getLocStart()); |
| 4702 | |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 4703 | case Expr::SizeOfPackExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4704 | case Expr::GNUNullExprClass: |
| 4705 | // GCC considers the GNU __null value to be an integral constant expression. |
| 4706 | return NoDiag(); |
| 4707 | |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4708 | case Expr::SubstNonTypeTemplateParmExprClass: |
| 4709 | return |
| 4710 | CheckICE(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(), Ctx); |
| 4711 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4712 | case Expr::ParenExprClass: |
| 4713 | return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx); |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 4714 | case Expr::GenericSelectionExprClass: |
| 4715 | return CheckICE(cast<GenericSelectionExpr>(E)->getResultExpr(), Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4716 | case Expr::IntegerLiteralClass: |
| 4717 | case Expr::CharacterLiteralClass: |
| 4718 | case Expr::CXXBoolLiteralExprClass: |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 4719 | case Expr::CXXScalarValueInitExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4720 | case Expr::UnaryTypeTraitExprClass: |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 4721 | case Expr::BinaryTypeTraitExprClass: |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 4722 | case Expr::ArrayTypeTraitExprClass: |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 4723 | case Expr::ExpressionTraitExprClass: |
Sebastian Redl | 2e15622 | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 4724 | case Expr::CXXNoexceptExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4725 | return NoDiag(); |
| 4726 | case Expr::CallExprClass: |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 4727 | case Expr::CXXOperatorCallExprClass: { |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 4728 | // C99 6.6/3 allows function calls within unevaluated subexpressions of |
| 4729 | // constant expressions, but they can never be ICEs because an ICE cannot |
| 4730 | // contain an operand of (pointer to) function type. |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4731 | const CallExpr *CE = cast<CallExpr>(E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4732 | if (CE->isBuiltinCall()) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4733 | return CheckEvalInICE(E, Ctx); |
| 4734 | return ICEDiag(2, E->getLocStart()); |
| 4735 | } |
| 4736 | case Expr::DeclRefExprClass: |
| 4737 | if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl())) |
| 4738 | return NoDiag(); |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 4739 | if (Ctx.getLangOptions().CPlusPlus && IsConstNonVolatile(E->getType())) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4740 | const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl(); |
| 4741 | |
| 4742 | // Parameter variables are never constants. Without this check, |
| 4743 | // getAnyInitializer() can find a default argument, which leads |
| 4744 | // to chaos. |
| 4745 | if (isa<ParmVarDecl>(D)) |
| 4746 | return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); |
| 4747 | |
| 4748 | // C++ 7.1.5.1p2 |
| 4749 | // A variable of non-volatile const-qualified integral or enumeration |
| 4750 | // type initialized by an ICE can be used in ICEs. |
| 4751 | if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) { |
Richard Smith | db1822c | 2011-11-08 01:31:09 +0000 | [diff] [blame] | 4752 | if (!Dcl->getType()->isIntegralOrEnumerationType()) |
| 4753 | return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); |
| 4754 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4755 | // Look for a declaration of this variable that has an initializer. |
| 4756 | const VarDecl *ID = 0; |
| 4757 | const Expr *Init = Dcl->getAnyInitializer(ID); |
| 4758 | if (Init) { |
| 4759 | if (ID->isInitKnownICE()) { |
| 4760 | // We have already checked whether this subexpression is an |
| 4761 | // integral constant expression. |
| 4762 | if (ID->isInitICE()) |
| 4763 | return NoDiag(); |
| 4764 | else |
| 4765 | return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); |
| 4766 | } |
| 4767 | |
| 4768 | // It's an ICE whether or not the definition we found is |
| 4769 | // out-of-line. See DR 721 and the discussion in Clang PR |
| 4770 | // 6206 for details. |
| 4771 | |
| 4772 | if (Dcl->isCheckingICE()) { |
| 4773 | return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); |
| 4774 | } |
| 4775 | |
| 4776 | Dcl->setCheckingICE(); |
| 4777 | ICEDiag Result = CheckICE(Init, Ctx); |
| 4778 | // Cache the result of the ICE test. |
| 4779 | Dcl->setInitKnownICE(Result.Val == 0); |
| 4780 | return Result; |
| 4781 | } |
| 4782 | } |
| 4783 | } |
| 4784 | return ICEDiag(2, E->getLocStart()); |
| 4785 | case Expr::UnaryOperatorClass: { |
| 4786 | const UnaryOperator *Exp = cast<UnaryOperator>(E); |
| 4787 | switch (Exp->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4788 | case UO_PostInc: |
| 4789 | case UO_PostDec: |
| 4790 | case UO_PreInc: |
| 4791 | case UO_PreDec: |
| 4792 | case UO_AddrOf: |
| 4793 | case UO_Deref: |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 4794 | // C99 6.6/3 allows increment and decrement within unevaluated |
| 4795 | // subexpressions of constant expressions, but they can never be ICEs |
| 4796 | // because an ICE cannot contain an lvalue operand. |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4797 | return ICEDiag(2, E->getLocStart()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4798 | case UO_Extension: |
| 4799 | case UO_LNot: |
| 4800 | case UO_Plus: |
| 4801 | case UO_Minus: |
| 4802 | case UO_Not: |
| 4803 | case UO_Real: |
| 4804 | case UO_Imag: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4805 | return CheckICE(Exp->getSubExpr(), Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4806 | } |
| 4807 | |
| 4808 | // OffsetOf falls through here. |
| 4809 | } |
| 4810 | case Expr::OffsetOfExprClass: { |
| 4811 | // Note that per C99, offsetof must be an ICE. And AFAIK, using |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 4812 | // EvaluateAsRValue matches the proposed gcc behavior for cases like |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 4813 | // "offsetof(struct s{int x[4];}, x[1.0])". This doesn't affect |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4814 | // compliance: we should warn earlier for offsetof expressions with |
| 4815 | // array subscripts that aren't ICEs, and if the array subscripts |
| 4816 | // are ICEs, the value of the offsetof must be an integer constant. |
| 4817 | return CheckEvalInICE(E, Ctx); |
| 4818 | } |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 4819 | case Expr::UnaryExprOrTypeTraitExprClass: { |
| 4820 | const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E); |
| 4821 | if ((Exp->getKind() == UETT_SizeOf) && |
| 4822 | Exp->getTypeOfArgument()->isVariableArrayType()) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4823 | return ICEDiag(2, E->getLocStart()); |
| 4824 | return NoDiag(); |
| 4825 | } |
| 4826 | case Expr::BinaryOperatorClass: { |
| 4827 | const BinaryOperator *Exp = cast<BinaryOperator>(E); |
| 4828 | switch (Exp->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4829 | case BO_PtrMemD: |
| 4830 | case BO_PtrMemI: |
| 4831 | case BO_Assign: |
| 4832 | case BO_MulAssign: |
| 4833 | case BO_DivAssign: |
| 4834 | case BO_RemAssign: |
| 4835 | case BO_AddAssign: |
| 4836 | case BO_SubAssign: |
| 4837 | case BO_ShlAssign: |
| 4838 | case BO_ShrAssign: |
| 4839 | case BO_AndAssign: |
| 4840 | case BO_XorAssign: |
| 4841 | case BO_OrAssign: |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 4842 | // C99 6.6/3 allows assignments within unevaluated subexpressions of |
| 4843 | // constant expressions, but they can never be ICEs because an ICE cannot |
| 4844 | // contain an lvalue operand. |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4845 | return ICEDiag(2, E->getLocStart()); |
| 4846 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4847 | case BO_Mul: |
| 4848 | case BO_Div: |
| 4849 | case BO_Rem: |
| 4850 | case BO_Add: |
| 4851 | case BO_Sub: |
| 4852 | case BO_Shl: |
| 4853 | case BO_Shr: |
| 4854 | case BO_LT: |
| 4855 | case BO_GT: |
| 4856 | case BO_LE: |
| 4857 | case BO_GE: |
| 4858 | case BO_EQ: |
| 4859 | case BO_NE: |
| 4860 | case BO_And: |
| 4861 | case BO_Xor: |
| 4862 | case BO_Or: |
| 4863 | case BO_Comma: { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4864 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 4865 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4866 | if (Exp->getOpcode() == BO_Div || |
| 4867 | Exp->getOpcode() == BO_Rem) { |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 4868 | // EvaluateAsRValue gives an error for undefined Div/Rem, so make sure |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4869 | // we don't evaluate one. |
John McCall | 3b332ab | 2011-02-26 08:27:17 +0000 | [diff] [blame] | 4870 | if (LHSResult.Val == 0 && RHSResult.Val == 0) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4871 | llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4872 | if (REval == 0) |
| 4873 | return ICEDiag(1, E->getLocStart()); |
| 4874 | if (REval.isSigned() && REval.isAllOnesValue()) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4875 | llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4876 | if (LEval.isMinSignedValue()) |
| 4877 | return ICEDiag(1, E->getLocStart()); |
| 4878 | } |
| 4879 | } |
| 4880 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4881 | if (Exp->getOpcode() == BO_Comma) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4882 | if (Ctx.getLangOptions().C99) { |
| 4883 | // C99 6.6p3 introduces a strange edge case: comma can be in an ICE |
| 4884 | // if it isn't evaluated. |
| 4885 | if (LHSResult.Val == 0 && RHSResult.Val == 0) |
| 4886 | return ICEDiag(1, E->getLocStart()); |
| 4887 | } else { |
| 4888 | // In both C89 and C++, commas in ICEs are illegal. |
| 4889 | return ICEDiag(2, E->getLocStart()); |
| 4890 | } |
| 4891 | } |
| 4892 | if (LHSResult.Val >= RHSResult.Val) |
| 4893 | return LHSResult; |
| 4894 | return RHSResult; |
| 4895 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4896 | case BO_LAnd: |
| 4897 | case BO_LOr: { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4898 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
Douglas Gregor | 63fe681 | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 4899 | |
| 4900 | // C++0x [expr.const]p2: |
| 4901 | // [...] subexpressions of logical AND (5.14), logical OR |
| 4902 | // (5.15), and condi- tional (5.16) operations that are not |
| 4903 | // evaluated are not considered. |
| 4904 | if (Ctx.getLangOptions().CPlusPlus0x && LHSResult.Val == 0) { |
| 4905 | if (Exp->getOpcode() == BO_LAnd && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4906 | Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0) |
Douglas Gregor | 63fe681 | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 4907 | return LHSResult; |
| 4908 | |
| 4909 | if (Exp->getOpcode() == BO_LOr && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4910 | Exp->getLHS()->EvaluateKnownConstInt(Ctx) != 0) |
Douglas Gregor | 63fe681 | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 4911 | return LHSResult; |
| 4912 | } |
| 4913 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4914 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
| 4915 | if (LHSResult.Val == 0 && RHSResult.Val == 1) { |
| 4916 | // Rare case where the RHS has a comma "side-effect"; we need |
| 4917 | // to actually check the condition to see whether the side |
| 4918 | // with the comma is evaluated. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4919 | if ((Exp->getOpcode() == BO_LAnd) != |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4920 | (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0)) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4921 | return RHSResult; |
| 4922 | return NoDiag(); |
| 4923 | } |
| 4924 | |
| 4925 | if (LHSResult.Val >= RHSResult.Val) |
| 4926 | return LHSResult; |
| 4927 | return RHSResult; |
| 4928 | } |
| 4929 | } |
| 4930 | } |
| 4931 | case Expr::ImplicitCastExprClass: |
| 4932 | case Expr::CStyleCastExprClass: |
| 4933 | case Expr::CXXFunctionalCastExprClass: |
| 4934 | case Expr::CXXStaticCastExprClass: |
| 4935 | case Expr::CXXReinterpretCastExprClass: |
Richard Smith | 32cb471 | 2011-10-24 18:26:35 +0000 | [diff] [blame] | 4936 | case Expr::CXXConstCastExprClass: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4937 | case Expr::ObjCBridgedCastExprClass: { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4938 | const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr(); |
Richard Smith | 98326ed | 2011-10-25 00:21:54 +0000 | [diff] [blame] | 4939 | if (isa<ExplicitCastExpr>(E) && |
Richard Smith | 32cb471 | 2011-10-24 18:26:35 +0000 | [diff] [blame] | 4940 | isa<FloatingLiteral>(SubExpr->IgnoreParenImpCasts())) |
| 4941 | return NoDiag(); |
Eli Friedman | eea0e81 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 4942 | switch (cast<CastExpr>(E)->getCastKind()) { |
| 4943 | case CK_LValueToRValue: |
| 4944 | case CK_NoOp: |
| 4945 | case CK_IntegralToBoolean: |
| 4946 | case CK_IntegralCast: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4947 | return CheckICE(SubExpr, Ctx); |
Eli Friedman | eea0e81 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 4948 | default: |
Eli Friedman | eea0e81 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 4949 | return ICEDiag(2, E->getLocStart()); |
| 4950 | } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4951 | } |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4952 | case Expr::BinaryConditionalOperatorClass: { |
| 4953 | const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E); |
| 4954 | ICEDiag CommonResult = CheckICE(Exp->getCommon(), Ctx); |
| 4955 | if (CommonResult.Val == 2) return CommonResult; |
| 4956 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
| 4957 | if (FalseResult.Val == 2) return FalseResult; |
| 4958 | if (CommonResult.Val == 1) return CommonResult; |
| 4959 | if (FalseResult.Val == 1 && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4960 | Exp->getCommon()->EvaluateKnownConstInt(Ctx) == 0) return NoDiag(); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4961 | return FalseResult; |
| 4962 | } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4963 | case Expr::ConditionalOperatorClass: { |
| 4964 | const ConditionalOperator *Exp = cast<ConditionalOperator>(E); |
| 4965 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
| 4966 | // then only the true side is actually considered in an integer constant |
| 4967 | // expression, and it is fully evaluated. This is an important GNU |
| 4968 | // extension. See GCC PR38377 for discussion. |
| 4969 | if (const CallExpr *CallCE |
| 4970 | = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts())) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4971 | if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4972 | Expr::EvalResult EVResult; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 4973 | if (!E->EvaluateAsRValue(EVResult, Ctx) || EVResult.HasSideEffects || |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4974 | !EVResult.Val.isInt()) { |
| 4975 | return ICEDiag(2, E->getLocStart()); |
| 4976 | } |
| 4977 | return NoDiag(); |
| 4978 | } |
| 4979 | ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4980 | if (CondResult.Val == 2) |
| 4981 | return CondResult; |
Douglas Gregor | 63fe681 | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 4982 | |
| 4983 | // C++0x [expr.const]p2: |
| 4984 | // subexpressions of [...] conditional (5.16) operations that |
| 4985 | // are not evaluated are not considered |
| 4986 | bool TrueBranch = Ctx.getLangOptions().CPlusPlus0x |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4987 | ? Exp->getCond()->EvaluateKnownConstInt(Ctx) != 0 |
Douglas Gregor | 63fe681 | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 4988 | : false; |
| 4989 | ICEDiag TrueResult = NoDiag(); |
| 4990 | if (!Ctx.getLangOptions().CPlusPlus0x || TrueBranch) |
| 4991 | TrueResult = CheckICE(Exp->getTrueExpr(), Ctx); |
| 4992 | ICEDiag FalseResult = NoDiag(); |
| 4993 | if (!Ctx.getLangOptions().CPlusPlus0x || !TrueBranch) |
| 4994 | FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
| 4995 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4996 | if (TrueResult.Val == 2) |
| 4997 | return TrueResult; |
| 4998 | if (FalseResult.Val == 2) |
| 4999 | return FalseResult; |
| 5000 | if (CondResult.Val == 1) |
| 5001 | return CondResult; |
| 5002 | if (TrueResult.Val == 0 && FalseResult.Val == 0) |
| 5003 | return NoDiag(); |
| 5004 | // Rare case where the diagnostics depend on which side is evaluated |
| 5005 | // Note that if we get here, CondResult is 0, and at least one of |
| 5006 | // TrueResult and FalseResult is non-zero. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5007 | if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5008 | return FalseResult; |
| 5009 | } |
| 5010 | return TrueResult; |
| 5011 | } |
| 5012 | case Expr::CXXDefaultArgExprClass: |
| 5013 | return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx); |
| 5014 | case Expr::ChooseExprClass: { |
| 5015 | return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx); |
| 5016 | } |
| 5017 | } |
| 5018 | |
| 5019 | // Silence a GCC warning |
| 5020 | return ICEDiag(2, E->getLocStart()); |
| 5021 | } |
| 5022 | |
| 5023 | bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx, |
| 5024 | SourceLocation *Loc, bool isEvaluated) const { |
| 5025 | ICEDiag d = CheckICE(this, Ctx); |
| 5026 | if (d.Val != 0) { |
| 5027 | if (Loc) *Loc = d.Loc; |
| 5028 | return false; |
| 5029 | } |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5030 | if (!EvaluateAsInt(Result, Ctx)) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5031 | llvm_unreachable("ICE cannot be evaluated!"); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5032 | return true; |
| 5033 | } |