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 | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 79 | /// Find the path length and type of the most-derived subobject in the given |
| 80 | /// path, and find the size of the containing array, if any. |
| 81 | static |
| 82 | unsigned findMostDerivedSubobject(ASTContext &Ctx, QualType Base, |
| 83 | ArrayRef<APValue::LValuePathEntry> Path, |
| 84 | uint64_t &ArraySize, QualType &Type) { |
| 85 | unsigned MostDerivedLength = 0; |
| 86 | Type = Base; |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 87 | for (unsigned I = 0, N = Path.size(); I != N; ++I) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 88 | if (Type->isArrayType()) { |
| 89 | const ConstantArrayType *CAT = |
| 90 | cast<ConstantArrayType>(Ctx.getAsArrayType(Type)); |
| 91 | Type = CAT->getElementType(); |
| 92 | ArraySize = CAT->getSize().getZExtValue(); |
| 93 | MostDerivedLength = I + 1; |
| 94 | } else if (const FieldDecl *FD = getAsField(Path[I])) { |
| 95 | Type = FD->getType(); |
| 96 | ArraySize = 0; |
| 97 | MostDerivedLength = I + 1; |
| 98 | } else { |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 99 | // Path[I] describes a base class. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 100 | ArraySize = 0; |
| 101 | } |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 102 | } |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 103 | return MostDerivedLength; |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 106 | // The order of this enum is important for diagnostics. |
| 107 | enum CheckSubobjectKind { |
| 108 | CSK_Base, CSK_Derived, CSK_Field, CSK_ArrayToPointer, CSK_ArrayIndex |
| 109 | }; |
| 110 | |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 111 | /// A path from a glvalue to a subobject of that glvalue. |
| 112 | struct SubobjectDesignator { |
| 113 | /// True if the subobject was named in a manner not supported by C++11. Such |
| 114 | /// lvalues can still be folded, but they are not core constant expressions |
| 115 | /// and we cannot perform lvalue-to-rvalue conversions on them. |
| 116 | bool Invalid : 1; |
| 117 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 118 | /// Is this a pointer one past the end of an object? |
| 119 | bool IsOnePastTheEnd : 1; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 120 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 121 | /// The length of the path to the most-derived object of which this is a |
| 122 | /// subobject. |
| 123 | unsigned MostDerivedPathLength : 30; |
| 124 | |
| 125 | /// The size of the array of which the most-derived object is an element, or |
| 126 | /// 0 if the most-derived object is not an array element. |
| 127 | uint64_t MostDerivedArraySize; |
| 128 | |
| 129 | /// The type of the most derived object referred to by this address. |
| 130 | QualType MostDerivedType; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 131 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 132 | typedef APValue::LValuePathEntry PathEntry; |
| 133 | |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 134 | /// The entries on the path from the glvalue to the designated subobject. |
| 135 | SmallVector<PathEntry, 8> Entries; |
| 136 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 137 | SubobjectDesignator() : Invalid(true) {} |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 138 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 139 | explicit SubobjectDesignator(QualType T) |
| 140 | : Invalid(false), IsOnePastTheEnd(false), MostDerivedPathLength(0), |
| 141 | MostDerivedArraySize(0), MostDerivedType(T) {} |
| 142 | |
| 143 | SubobjectDesignator(ASTContext &Ctx, const APValue &V) |
| 144 | : Invalid(!V.isLValue() || !V.hasLValuePath()), IsOnePastTheEnd(false), |
| 145 | MostDerivedPathLength(0), MostDerivedArraySize(0) { |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 146 | if (!Invalid) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 147 | IsOnePastTheEnd = V.isLValueOnePastTheEnd(); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 148 | ArrayRef<PathEntry> VEntries = V.getLValuePath(); |
| 149 | Entries.insert(Entries.end(), VEntries.begin(), VEntries.end()); |
| 150 | if (V.getLValueBase()) |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 151 | MostDerivedPathLength = |
| 152 | findMostDerivedSubobject(Ctx, getType(V.getLValueBase()), |
| 153 | V.getLValuePath(), MostDerivedArraySize, |
| 154 | MostDerivedType); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 158 | void setInvalid() { |
| 159 | Invalid = true; |
| 160 | Entries.clear(); |
| 161 | } |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 162 | |
| 163 | /// Determine whether this is a one-past-the-end pointer. |
| 164 | bool isOnePastTheEnd() const { |
| 165 | if (IsOnePastTheEnd) |
| 166 | return true; |
| 167 | if (MostDerivedArraySize && |
| 168 | Entries[MostDerivedPathLength - 1].ArrayIndex == MostDerivedArraySize) |
| 169 | return true; |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | /// Check that this refers to a valid subobject. |
| 174 | bool isValidSubobject() const { |
| 175 | if (Invalid) |
| 176 | return false; |
| 177 | return !isOnePastTheEnd(); |
| 178 | } |
| 179 | /// Check that this refers to a valid subobject, and if not, produce a |
| 180 | /// relevant diagnostic and set the designator as invalid. |
| 181 | bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK); |
| 182 | |
| 183 | /// Update this designator to refer to the first element within this array. |
| 184 | void addArrayUnchecked(const ConstantArrayType *CAT) { |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 185 | PathEntry Entry; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 186 | Entry.ArrayIndex = 0; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 187 | Entries.push_back(Entry); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 188 | |
| 189 | // This is a most-derived object. |
| 190 | MostDerivedType = CAT->getElementType(); |
| 191 | MostDerivedArraySize = CAT->getSize().getZExtValue(); |
| 192 | MostDerivedPathLength = Entries.size(); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 193 | } |
| 194 | /// Update this designator to refer to the given base or member of this |
| 195 | /// object. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 196 | void addDeclUnchecked(const Decl *D, bool Virtual = false) { |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 197 | PathEntry Entry; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 198 | APValue::BaseOrMemberType Value(D, Virtual); |
| 199 | Entry.BaseOrMember = Value.getOpaqueValue(); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 200 | Entries.push_back(Entry); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 201 | |
| 202 | // If this isn't a base class, it's a new most-derived object. |
| 203 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 204 | MostDerivedType = FD->getType(); |
| 205 | MostDerivedArraySize = 0; |
| 206 | MostDerivedPathLength = Entries.size(); |
| 207 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 208 | } |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 209 | void diagnosePointerArithmetic(EvalInfo &Info, const Expr *E, uint64_t N); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 210 | /// Add N to the address of this subobject. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 211 | void adjustIndex(EvalInfo &Info, const Expr *E, uint64_t N) { |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 212 | if (Invalid) return; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 213 | if (MostDerivedPathLength == Entries.size() && MostDerivedArraySize) { |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 214 | Entries.back().ArrayIndex += N; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 215 | if (Entries.back().ArrayIndex > MostDerivedArraySize) { |
| 216 | diagnosePointerArithmetic(Info, E, Entries.back().ArrayIndex); |
| 217 | setInvalid(); |
| 218 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 219 | return; |
| 220 | } |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 221 | // [expr.add]p4: For the purposes of these operators, a pointer to a |
| 222 | // nonarray object behaves the same as a pointer to the first element of |
| 223 | // an array of length one with the type of the object as its element type. |
| 224 | if (IsOnePastTheEnd && N == (uint64_t)-1) |
| 225 | IsOnePastTheEnd = false; |
| 226 | else if (!IsOnePastTheEnd && N == 1) |
| 227 | IsOnePastTheEnd = true; |
| 228 | else if (N != 0) { |
| 229 | diagnosePointerArithmetic(Info, E, uint64_t(IsOnePastTheEnd) + N); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 230 | setInvalid(); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 231 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 232 | } |
| 233 | }; |
| 234 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 235 | /// A core constant value. This can be the value of any constant expression, |
| 236 | /// 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] | 237 | /// |
| 238 | /// For an LValue, the base and offset are stored in the APValue subobject, |
| 239 | /// but the other information is stored in the SubobjectDesignator. For all |
| 240 | /// other value kinds, the value is stored directly in the APValue subobject. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 241 | class CCValue : public APValue { |
| 242 | typedef llvm::APSInt APSInt; |
| 243 | typedef llvm::APFloat APFloat; |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 244 | /// If the value is a reference or pointer into a parameter or temporary, |
| 245 | /// this is the corresponding call stack frame. |
| 246 | CallStackFrame *CallFrame; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 247 | /// If the value is a reference or pointer, this is a description of how the |
| 248 | /// subobject was specified. |
| 249 | SubobjectDesignator Designator; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 250 | public: |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 251 | struct GlobalValue {}; |
| 252 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 253 | CCValue() {} |
| 254 | explicit CCValue(const APSInt &I) : APValue(I) {} |
| 255 | explicit CCValue(const APFloat &F) : APValue(F) {} |
| 256 | CCValue(const APValue *E, unsigned N) : APValue(E, N) {} |
| 257 | CCValue(const APSInt &R, const APSInt &I) : APValue(R, I) {} |
| 258 | CCValue(const APFloat &R, const APFloat &I) : APValue(R, I) {} |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 259 | CCValue(const CCValue &V) : APValue(V), CallFrame(V.CallFrame) {} |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 260 | CCValue(LValueBase B, const CharUnits &O, CallStackFrame *F, |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 261 | const SubobjectDesignator &D) : |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 262 | APValue(B, O, APValue::NoLValuePath()), CallFrame(F), Designator(D) {} |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 263 | CCValue(ASTContext &Ctx, const APValue &V, GlobalValue) : |
| 264 | APValue(V), CallFrame(0), Designator(Ctx, V) {} |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 265 | CCValue(const ValueDecl *D, bool IsDerivedMember, |
| 266 | ArrayRef<const CXXRecordDecl*> Path) : |
| 267 | APValue(D, IsDerivedMember, Path) {} |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 268 | CCValue(const AddrLabelExpr* LHSExpr, const AddrLabelExpr* RHSExpr) : |
| 269 | APValue(LHSExpr, RHSExpr) {} |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 270 | |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 271 | CallStackFrame *getLValueFrame() const { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 272 | assert(getKind() == LValue); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 273 | return CallFrame; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 274 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 275 | SubobjectDesignator &getLValueDesignator() { |
| 276 | assert(getKind() == LValue); |
| 277 | return Designator; |
| 278 | } |
| 279 | const SubobjectDesignator &getLValueDesignator() const { |
| 280 | return const_cast<CCValue*>(this)->getLValueDesignator(); |
| 281 | } |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 282 | }; |
| 283 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 284 | /// A stack frame in the constexpr call stack. |
| 285 | struct CallStackFrame { |
| 286 | EvalInfo &Info; |
| 287 | |
| 288 | /// Parent - The caller of this stack frame. |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 289 | CallStackFrame *Caller; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 290 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 291 | /// CallLoc - The location of the call expression for this call. |
| 292 | SourceLocation CallLoc; |
| 293 | |
| 294 | /// Callee - The function which was called. |
| 295 | const FunctionDecl *Callee; |
| 296 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 297 | /// This - The binding for the this pointer in this call, if any. |
| 298 | const LValue *This; |
| 299 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 300 | /// ParmBindings - Parameter bindings for this function call, indexed by |
| 301 | /// parameters' function scope indices. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 302 | const CCValue *Arguments; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 303 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 304 | typedef llvm::DenseMap<const Expr*, CCValue> MapTy; |
| 305 | typedef MapTy::const_iterator temp_iterator; |
| 306 | /// Temporaries - Temporary lvalues materialized within this stack frame. |
| 307 | MapTy Temporaries; |
| 308 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 309 | CallStackFrame(EvalInfo &Info, SourceLocation CallLoc, |
| 310 | const FunctionDecl *Callee, const LValue *This, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 311 | const CCValue *Arguments); |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 312 | ~CallStackFrame(); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 313 | }; |
| 314 | |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 315 | /// A partial diagnostic which we might know in advance that we are not going |
| 316 | /// to emit. |
| 317 | class OptionalDiagnostic { |
| 318 | PartialDiagnostic *Diag; |
| 319 | |
| 320 | public: |
| 321 | explicit OptionalDiagnostic(PartialDiagnostic *Diag = 0) : Diag(Diag) {} |
| 322 | |
| 323 | template<typename T> |
| 324 | OptionalDiagnostic &operator<<(const T &v) { |
| 325 | if (Diag) |
| 326 | *Diag << v; |
| 327 | return *this; |
| 328 | } |
| 329 | }; |
| 330 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 331 | struct EvalInfo { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 332 | ASTContext &Ctx; |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 333 | |
| 334 | /// EvalStatus - Contains information about the evaluation. |
| 335 | Expr::EvalStatus &EvalStatus; |
| 336 | |
| 337 | /// CurrentCall - The top of the constexpr call stack. |
| 338 | CallStackFrame *CurrentCall; |
| 339 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 340 | /// CallStackDepth - The number of calls in the call stack right now. |
| 341 | unsigned CallStackDepth; |
| 342 | |
| 343 | typedef llvm::DenseMap<const OpaqueValueExpr*, CCValue> MapTy; |
| 344 | /// OpaqueValues - Values used as the common expression in a |
| 345 | /// BinaryConditionalOperator. |
| 346 | MapTy OpaqueValues; |
| 347 | |
| 348 | /// BottomFrame - The frame in which evaluation started. This must be |
| 349 | /// initialized last. |
| 350 | CallStackFrame BottomFrame; |
| 351 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 352 | /// EvaluatingDecl - This is the declaration whose initializer is being |
| 353 | /// evaluated, if any. |
| 354 | const VarDecl *EvaluatingDecl; |
| 355 | |
| 356 | /// EvaluatingDeclValue - This is the value being constructed for the |
| 357 | /// declaration whose initializer is being evaluated, if any. |
| 358 | APValue *EvaluatingDeclValue; |
| 359 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 360 | /// HasActiveDiagnostic - Was the previous diagnostic stored? If so, further |
| 361 | /// notes attached to it will also be stored, otherwise they will not be. |
| 362 | bool HasActiveDiagnostic; |
| 363 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 364 | |
| 365 | EvalInfo(const ASTContext &C, Expr::EvalStatus &S) |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 366 | : Ctx(const_cast<ASTContext&>(C)), EvalStatus(S), CurrentCall(0), |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 367 | CallStackDepth(0), BottomFrame(*this, SourceLocation(), 0, 0, 0), |
| 368 | EvaluatingDecl(0), EvaluatingDeclValue(0), HasActiveDiagnostic(false) {} |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 369 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 370 | const CCValue *getOpaqueValue(const OpaqueValueExpr *e) const { |
| 371 | MapTy::const_iterator i = OpaqueValues.find(e); |
| 372 | if (i == OpaqueValues.end()) return 0; |
| 373 | return &i->second; |
| 374 | } |
| 375 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 376 | void setEvaluatingDecl(const VarDecl *VD, APValue &Value) { |
| 377 | EvaluatingDecl = VD; |
| 378 | EvaluatingDeclValue = &Value; |
| 379 | } |
| 380 | |
Richard Smith | c18c423 | 2011-11-21 19:36:32 +0000 | [diff] [blame] | 381 | const LangOptions &getLangOpts() const { return Ctx.getLangOptions(); } |
| 382 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 383 | bool CheckCallLimit(SourceLocation Loc) { |
| 384 | if (CallStackDepth <= getLangOpts().ConstexprCallDepth) |
| 385 | return true; |
| 386 | Diag(Loc, diag::note_constexpr_depth_limit_exceeded) |
| 387 | << getLangOpts().ConstexprCallDepth; |
| 388 | return false; |
Richard Smith | c18c423 | 2011-11-21 19:36:32 +0000 | [diff] [blame] | 389 | } |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 390 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 391 | private: |
| 392 | /// Add a diagnostic to the diagnostics list. |
| 393 | PartialDiagnostic &addDiag(SourceLocation Loc, diag::kind DiagId) { |
| 394 | PartialDiagnostic PD(DiagId, Ctx.getDiagAllocator()); |
| 395 | EvalStatus.Diag->push_back(std::make_pair(Loc, PD)); |
| 396 | return EvalStatus.Diag->back().second; |
| 397 | } |
| 398 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 399 | /// Add notes containing a call stack to the current point of evaluation. |
| 400 | void addCallStack(unsigned Limit); |
| 401 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 402 | public: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 403 | /// Diagnose that the evaluation cannot be folded. |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 404 | OptionalDiagnostic Diag(SourceLocation Loc, diag::kind DiagId |
| 405 | = diag::note_invalid_subexpr_in_const_expr, |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 406 | unsigned ExtraNotes = 0) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 407 | // If we have a prior diagnostic, it will be noting that the expression |
| 408 | // isn't a constant expression. This diagnostic is more important. |
| 409 | // FIXME: We might want to show both diagnostics to the user. |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 410 | if (EvalStatus.Diag) { |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 411 | unsigned CallStackNotes = CallStackDepth - 1; |
| 412 | unsigned Limit = Ctx.getDiagnostics().getConstexprBacktraceLimit(); |
| 413 | if (Limit) |
| 414 | CallStackNotes = std::min(CallStackNotes, Limit + 1); |
| 415 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 416 | HasActiveDiagnostic = true; |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 417 | EvalStatus.Diag->clear(); |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 418 | EvalStatus.Diag->reserve(1 + ExtraNotes + CallStackNotes); |
| 419 | addDiag(Loc, DiagId); |
| 420 | addCallStack(Limit); |
| 421 | return OptionalDiagnostic(&(*EvalStatus.Diag)[0].second); |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 422 | } |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 423 | HasActiveDiagnostic = false; |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 424 | return OptionalDiagnostic(); |
| 425 | } |
| 426 | |
| 427 | /// Diagnose that the evaluation does not produce a C++11 core constant |
| 428 | /// expression. |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 429 | OptionalDiagnostic CCEDiag(SourceLocation Loc, diag::kind DiagId |
| 430 | = diag::note_invalid_subexpr_in_const_expr, |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 431 | unsigned ExtraNotes = 0) { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 432 | // Don't override a previous diagnostic. |
| 433 | if (!EvalStatus.Diag || !EvalStatus.Diag->empty()) |
| 434 | return OptionalDiagnostic(); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 435 | return Diag(Loc, DiagId, ExtraNotes); |
| 436 | } |
| 437 | |
| 438 | /// Add a note to a prior diagnostic. |
| 439 | OptionalDiagnostic Note(SourceLocation Loc, diag::kind DiagId) { |
| 440 | if (!HasActiveDiagnostic) |
| 441 | return OptionalDiagnostic(); |
| 442 | return OptionalDiagnostic(&addDiag(Loc, DiagId)); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 443 | } |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 444 | |
| 445 | /// Add a stack of notes to a prior diagnostic. |
| 446 | void addNotes(ArrayRef<PartialDiagnosticAt> Diags) { |
| 447 | if (HasActiveDiagnostic) { |
| 448 | EvalStatus.Diag->insert(EvalStatus.Diag->end(), |
| 449 | Diags.begin(), Diags.end()); |
| 450 | } |
| 451 | } |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 452 | }; |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 453 | } |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 454 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 455 | bool SubobjectDesignator::checkSubobject(EvalInfo &Info, const Expr *E, |
| 456 | CheckSubobjectKind CSK) { |
| 457 | if (Invalid) |
| 458 | return false; |
| 459 | if (isOnePastTheEnd()) { |
| 460 | Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_past_end_subobject) |
| 461 | << CSK; |
| 462 | setInvalid(); |
| 463 | return false; |
| 464 | } |
| 465 | return true; |
| 466 | } |
| 467 | |
| 468 | void SubobjectDesignator::diagnosePointerArithmetic(EvalInfo &Info, |
| 469 | const Expr *E, uint64_t N) { |
| 470 | if (MostDerivedPathLength == Entries.size() && MostDerivedArraySize) |
| 471 | Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_array_index) |
| 472 | << static_cast<int>(N) << /*array*/ 0 |
| 473 | << static_cast<unsigned>(MostDerivedArraySize); |
| 474 | else |
| 475 | Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_array_index) |
| 476 | << static_cast<int>(N) << /*non-array*/ 1; |
| 477 | setInvalid(); |
| 478 | } |
| 479 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 480 | CallStackFrame::CallStackFrame(EvalInfo &Info, SourceLocation CallLoc, |
| 481 | const FunctionDecl *Callee, const LValue *This, |
| 482 | const CCValue *Arguments) |
| 483 | : Info(Info), Caller(Info.CurrentCall), CallLoc(CallLoc), Callee(Callee), |
| 484 | This(This), Arguments(Arguments) { |
| 485 | Info.CurrentCall = this; |
| 486 | ++Info.CallStackDepth; |
| 487 | } |
| 488 | |
| 489 | CallStackFrame::~CallStackFrame() { |
| 490 | assert(Info.CurrentCall == this && "calls retired out of order"); |
| 491 | --Info.CallStackDepth; |
| 492 | Info.CurrentCall = Caller; |
| 493 | } |
| 494 | |
| 495 | /// Produce a string describing the given constexpr call. |
| 496 | static void describeCall(CallStackFrame *Frame, llvm::raw_ostream &Out) { |
| 497 | unsigned ArgIndex = 0; |
| 498 | bool IsMemberCall = isa<CXXMethodDecl>(Frame->Callee) && |
| 499 | !isa<CXXConstructorDecl>(Frame->Callee); |
| 500 | |
| 501 | if (!IsMemberCall) |
| 502 | Out << *Frame->Callee << '('; |
| 503 | |
| 504 | for (FunctionDecl::param_const_iterator I = Frame->Callee->param_begin(), |
| 505 | E = Frame->Callee->param_end(); I != E; ++I, ++ArgIndex) { |
| 506 | if (ArgIndex > IsMemberCall) |
| 507 | Out << ", "; |
| 508 | |
| 509 | const ParmVarDecl *Param = *I; |
| 510 | const CCValue &Arg = Frame->Arguments[ArgIndex]; |
| 511 | if (!Arg.isLValue() || Arg.getLValueDesignator().Invalid) |
| 512 | Arg.printPretty(Out, Frame->Info.Ctx, Param->getType()); |
| 513 | else { |
| 514 | // Deliberately slice off the frame to form an APValue we can print. |
| 515 | APValue Value(Arg.getLValueBase(), Arg.getLValueOffset(), |
| 516 | Arg.getLValueDesignator().Entries, |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 517 | Arg.getLValueDesignator().IsOnePastTheEnd); |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 518 | Value.printPretty(Out, Frame->Info.Ctx, Param->getType()); |
| 519 | } |
| 520 | |
| 521 | if (ArgIndex == 0 && IsMemberCall) |
| 522 | Out << "->" << *Frame->Callee << '('; |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 525 | Out << ')'; |
| 526 | } |
| 527 | |
| 528 | void EvalInfo::addCallStack(unsigned Limit) { |
| 529 | // Determine which calls to skip, if any. |
| 530 | unsigned ActiveCalls = CallStackDepth - 1; |
| 531 | unsigned SkipStart = ActiveCalls, SkipEnd = SkipStart; |
| 532 | if (Limit && Limit < ActiveCalls) { |
| 533 | SkipStart = Limit / 2 + Limit % 2; |
| 534 | SkipEnd = ActiveCalls - Limit / 2; |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 537 | // Walk the call stack and add the diagnostics. |
| 538 | unsigned CallIdx = 0; |
| 539 | for (CallStackFrame *Frame = CurrentCall; Frame != &BottomFrame; |
| 540 | Frame = Frame->Caller, ++CallIdx) { |
| 541 | // Skip this call? |
| 542 | if (CallIdx >= SkipStart && CallIdx < SkipEnd) { |
| 543 | if (CallIdx == SkipStart) { |
| 544 | // Note that we're skipping calls. |
| 545 | addDiag(Frame->CallLoc, diag::note_constexpr_calls_suppressed) |
| 546 | << unsigned(ActiveCalls - Limit); |
| 547 | } |
| 548 | continue; |
| 549 | } |
| 550 | |
| 551 | llvm::SmallVector<char, 128> Buffer; |
| 552 | llvm::raw_svector_ostream Out(Buffer); |
| 553 | describeCall(Frame, Out); |
| 554 | addDiag(Frame->CallLoc, diag::note_constexpr_call_here) << Out.str(); |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | namespace { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 559 | struct ComplexValue { |
| 560 | private: |
| 561 | bool IsInt; |
| 562 | |
| 563 | public: |
| 564 | APSInt IntReal, IntImag; |
| 565 | APFloat FloatReal, FloatImag; |
| 566 | |
| 567 | ComplexValue() : FloatReal(APFloat::Bogus), FloatImag(APFloat::Bogus) {} |
| 568 | |
| 569 | void makeComplexFloat() { IsInt = false; } |
| 570 | bool isComplexFloat() const { return !IsInt; } |
| 571 | APFloat &getComplexFloatReal() { return FloatReal; } |
| 572 | APFloat &getComplexFloatImag() { return FloatImag; } |
| 573 | |
| 574 | void makeComplexInt() { IsInt = true; } |
| 575 | bool isComplexInt() const { return IsInt; } |
| 576 | APSInt &getComplexIntReal() { return IntReal; } |
| 577 | APSInt &getComplexIntImag() { return IntImag; } |
| 578 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 579 | void moveInto(CCValue &v) const { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 580 | if (isComplexFloat()) |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 581 | v = CCValue(FloatReal, FloatImag); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 582 | else |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 583 | v = CCValue(IntReal, IntImag); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 584 | } |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 585 | void setFrom(const CCValue &v) { |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 586 | assert(v.isComplexFloat() || v.isComplexInt()); |
| 587 | if (v.isComplexFloat()) { |
| 588 | makeComplexFloat(); |
| 589 | FloatReal = v.getComplexFloatReal(); |
| 590 | FloatImag = v.getComplexFloatImag(); |
| 591 | } else { |
| 592 | makeComplexInt(); |
| 593 | IntReal = v.getComplexIntReal(); |
| 594 | IntImag = v.getComplexIntImag(); |
| 595 | } |
| 596 | } |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 597 | }; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 598 | |
| 599 | struct LValue { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 600 | APValue::LValueBase Base; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 601 | CharUnits Offset; |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 602 | CallStackFrame *Frame; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 603 | SubobjectDesignator Designator; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 604 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 605 | const APValue::LValueBase getLValueBase() const { return Base; } |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 606 | CharUnits &getLValueOffset() { return Offset; } |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 607 | const CharUnits &getLValueOffset() const { return Offset; } |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 608 | CallStackFrame *getLValueFrame() const { return Frame; } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 609 | SubobjectDesignator &getLValueDesignator() { return Designator; } |
| 610 | const SubobjectDesignator &getLValueDesignator() const { return Designator;} |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 611 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 612 | void moveInto(CCValue &V) const { |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 613 | V = CCValue(Base, Offset, Frame, Designator); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 614 | } |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 615 | void setFrom(const CCValue &V) { |
| 616 | assert(V.isLValue()); |
| 617 | Base = V.getLValueBase(); |
| 618 | Offset = V.getLValueOffset(); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 619 | Frame = V.getLValueFrame(); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 620 | Designator = V.getLValueDesignator(); |
| 621 | } |
| 622 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 623 | void set(APValue::LValueBase B, CallStackFrame *F = 0) { |
| 624 | Base = B; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 625 | Offset = CharUnits::Zero(); |
| 626 | Frame = F; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 627 | Designator = SubobjectDesignator(getType(B)); |
| 628 | } |
| 629 | |
| 630 | // Check that this LValue is not based on a null pointer. If it is, produce |
| 631 | // a diagnostic and mark the designator as invalid. |
| 632 | bool checkNullPointer(EvalInfo &Info, const Expr *E, |
| 633 | CheckSubobjectKind CSK) { |
| 634 | if (Designator.Invalid) |
| 635 | return false; |
| 636 | if (!Base) { |
| 637 | Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_null_subobject) |
| 638 | << CSK; |
| 639 | Designator.setInvalid(); |
| 640 | return false; |
| 641 | } |
| 642 | return true; |
| 643 | } |
| 644 | |
| 645 | // Check this LValue refers to an object. If not, set the designator to be |
| 646 | // invalid and emit a diagnostic. |
| 647 | bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK) { |
| 648 | return checkNullPointer(Info, E, CSK) && |
| 649 | Designator.checkSubobject(Info, E, CSK); |
| 650 | } |
| 651 | |
| 652 | void addDecl(EvalInfo &Info, const Expr *E, |
| 653 | const Decl *D, bool Virtual = false) { |
| 654 | checkSubobject(Info, E, isa<FieldDecl>(D) ? CSK_Field : CSK_Base); |
| 655 | Designator.addDeclUnchecked(D, Virtual); |
| 656 | } |
| 657 | void addArray(EvalInfo &Info, const Expr *E, const ConstantArrayType *CAT) { |
| 658 | checkSubobject(Info, E, CSK_ArrayToPointer); |
| 659 | Designator.addArrayUnchecked(CAT); |
| 660 | } |
| 661 | void adjustIndex(EvalInfo &Info, const Expr *E, uint64_t N) { |
| 662 | if (!checkNullPointer(Info, E, CSK_ArrayIndex)) |
| 663 | return; |
| 664 | Designator.adjustIndex(Info, E, N); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 665 | } |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 666 | }; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 667 | |
| 668 | struct MemberPtr { |
| 669 | MemberPtr() {} |
| 670 | explicit MemberPtr(const ValueDecl *Decl) : |
| 671 | DeclAndIsDerivedMember(Decl, false), Path() {} |
| 672 | |
| 673 | /// The member or (direct or indirect) field referred to by this member |
| 674 | /// pointer, or 0 if this is a null member pointer. |
| 675 | const ValueDecl *getDecl() const { |
| 676 | return DeclAndIsDerivedMember.getPointer(); |
| 677 | } |
| 678 | /// Is this actually a member of some type derived from the relevant class? |
| 679 | bool isDerivedMember() const { |
| 680 | return DeclAndIsDerivedMember.getInt(); |
| 681 | } |
| 682 | /// Get the class which the declaration actually lives in. |
| 683 | const CXXRecordDecl *getContainingRecord() const { |
| 684 | return cast<CXXRecordDecl>( |
| 685 | DeclAndIsDerivedMember.getPointer()->getDeclContext()); |
| 686 | } |
| 687 | |
| 688 | void moveInto(CCValue &V) const { |
| 689 | V = CCValue(getDecl(), isDerivedMember(), Path); |
| 690 | } |
| 691 | void setFrom(const CCValue &V) { |
| 692 | assert(V.isMemberPointer()); |
| 693 | DeclAndIsDerivedMember.setPointer(V.getMemberPointerDecl()); |
| 694 | DeclAndIsDerivedMember.setInt(V.isMemberPointerToDerivedMember()); |
| 695 | Path.clear(); |
| 696 | ArrayRef<const CXXRecordDecl*> P = V.getMemberPointerPath(); |
| 697 | Path.insert(Path.end(), P.begin(), P.end()); |
| 698 | } |
| 699 | |
| 700 | /// DeclAndIsDerivedMember - The member declaration, and a flag indicating |
| 701 | /// whether the member is a member of some class derived from the class type |
| 702 | /// of the member pointer. |
| 703 | llvm::PointerIntPair<const ValueDecl*, 1, bool> DeclAndIsDerivedMember; |
| 704 | /// Path - The path of base/derived classes from the member declaration's |
| 705 | /// class (exclusive) to the class type of the member pointer (inclusive). |
| 706 | SmallVector<const CXXRecordDecl*, 4> Path; |
| 707 | |
| 708 | /// Perform a cast towards the class of the Decl (either up or down the |
| 709 | /// hierarchy). |
| 710 | bool castBack(const CXXRecordDecl *Class) { |
| 711 | assert(!Path.empty()); |
| 712 | const CXXRecordDecl *Expected; |
| 713 | if (Path.size() >= 2) |
| 714 | Expected = Path[Path.size() - 2]; |
| 715 | else |
| 716 | Expected = getContainingRecord(); |
| 717 | if (Expected->getCanonicalDecl() != Class->getCanonicalDecl()) { |
| 718 | // C++11 [expr.static.cast]p12: In a conversion from (D::*) to (B::*), |
| 719 | // if B does not contain the original member and is not a base or |
| 720 | // derived class of the class containing the original member, the result |
| 721 | // of the cast is undefined. |
| 722 | // C++11 [conv.mem]p2 does not cover this case for a cast from (B::*) to |
| 723 | // (D::*). We consider that to be a language defect. |
| 724 | return false; |
| 725 | } |
| 726 | Path.pop_back(); |
| 727 | return true; |
| 728 | } |
| 729 | /// Perform a base-to-derived member pointer cast. |
| 730 | bool castToDerived(const CXXRecordDecl *Derived) { |
| 731 | if (!getDecl()) |
| 732 | return true; |
| 733 | if (!isDerivedMember()) { |
| 734 | Path.push_back(Derived); |
| 735 | return true; |
| 736 | } |
| 737 | if (!castBack(Derived)) |
| 738 | return false; |
| 739 | if (Path.empty()) |
| 740 | DeclAndIsDerivedMember.setInt(false); |
| 741 | return true; |
| 742 | } |
| 743 | /// Perform a derived-to-base member pointer cast. |
| 744 | bool castToBase(const CXXRecordDecl *Base) { |
| 745 | if (!getDecl()) |
| 746 | return true; |
| 747 | if (Path.empty()) |
| 748 | DeclAndIsDerivedMember.setInt(true); |
| 749 | if (isDerivedMember()) { |
| 750 | Path.push_back(Base); |
| 751 | return true; |
| 752 | } |
| 753 | return castBack(Base); |
| 754 | } |
| 755 | }; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 756 | |
| 757 | /// Kinds of constant expression checking, for diagnostics. |
| 758 | enum CheckConstantExpressionKind { |
| 759 | CCEK_Constant, ///< A normal constant. |
| 760 | CCEK_ReturnValue, ///< A constexpr function return value. |
| 761 | CCEK_MemberInit ///< A constexpr constructor mem-initializer. |
| 762 | }; |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 763 | } |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 764 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 765 | static bool Evaluate(CCValue &Result, EvalInfo &Info, const Expr *E); |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 766 | static bool EvaluateConstantExpression(APValue &Result, EvalInfo &Info, |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 767 | const LValue &This, const Expr *E, |
| 768 | CheckConstantExpressionKind CCEK |
| 769 | = CCEK_Constant); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 770 | static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info); |
| 771 | static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 772 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
| 773 | EvalInfo &Info); |
| 774 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info); |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 775 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 776 | static bool EvaluateIntegerOrLValue(const Expr *E, CCValue &Result, |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 777 | EvalInfo &Info); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 778 | static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 779 | static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 780 | |
| 781 | //===----------------------------------------------------------------------===// |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 782 | // Misc utilities |
| 783 | //===----------------------------------------------------------------------===// |
| 784 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 785 | /// Should this call expression be treated as a string literal? |
| 786 | static bool IsStringLiteralCall(const CallExpr *E) { |
| 787 | unsigned Builtin = E->isBuiltinCall(); |
| 788 | return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString || |
| 789 | Builtin == Builtin::BI__builtin___NSStringMakeConstantString); |
| 790 | } |
| 791 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 792 | static bool IsGlobalLValue(APValue::LValueBase B) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 793 | // C++11 [expr.const]p3 An address constant expression is a prvalue core |
| 794 | // constant expression of pointer type that evaluates to... |
| 795 | |
| 796 | // ... a null pointer value, or a prvalue core constant expression of type |
| 797 | // std::nullptr_t. |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 798 | if (!B) return true; |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 799 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 800 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
| 801 | // ... the address of an object with static storage duration, |
| 802 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 803 | return VD->hasGlobalStorage(); |
| 804 | // ... the address of a function, |
| 805 | return isa<FunctionDecl>(D); |
| 806 | } |
| 807 | |
| 808 | const Expr *E = B.get<const Expr*>(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 809 | switch (E->getStmtClass()) { |
| 810 | default: |
| 811 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 812 | case Expr::CompoundLiteralExprClass: |
| 813 | return cast<CompoundLiteralExpr>(E)->isFileScope(); |
| 814 | // A string literal has static storage duration. |
| 815 | case Expr::StringLiteralClass: |
| 816 | case Expr::PredefinedExprClass: |
| 817 | case Expr::ObjCStringLiteralClass: |
| 818 | case Expr::ObjCEncodeExprClass: |
Richard Smith | 47d2145 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 819 | case Expr::CXXTypeidExprClass: |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 820 | return true; |
| 821 | case Expr::CallExprClass: |
| 822 | return IsStringLiteralCall(cast<CallExpr>(E)); |
| 823 | // For GCC compatibility, &&label has static storage duration. |
| 824 | case Expr::AddrLabelExprClass: |
| 825 | return true; |
| 826 | // A Block literal expression may be used as the initialization value for |
| 827 | // Block variables at global or local static scope. |
| 828 | case Expr::BlockExprClass: |
| 829 | return !cast<BlockExpr>(E)->getBlockDecl()->hasCaptures(); |
| 830 | } |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 831 | } |
| 832 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 833 | /// Check that this reference or pointer core constant expression is a valid |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 834 | /// value for an address or reference constant expression. Type T should be |
Richard Smith | 61e6162 | 2012-01-12 06:08:57 +0000 | [diff] [blame] | 835 | /// either LValue or CCValue. Return true if we can fold this expression, |
| 836 | /// whether or not it's a constant expression. |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 837 | template<typename T> |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 838 | static bool CheckLValueConstantExpression(EvalInfo &Info, const Expr *E, |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 839 | const T &LVal, APValue &Value, |
| 840 | CheckConstantExpressionKind CCEK) { |
| 841 | APValue::LValueBase Base = LVal.getLValueBase(); |
| 842 | const SubobjectDesignator &Designator = LVal.getLValueDesignator(); |
| 843 | |
| 844 | if (!IsGlobalLValue(Base)) { |
| 845 | if (Info.getLangOpts().CPlusPlus0x) { |
| 846 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
| 847 | Info.Diag(E->getExprLoc(), diag::note_constexpr_non_global, 1) |
| 848 | << E->isGLValue() << !Designator.Entries.empty() |
| 849 | << !!VD << CCEK << VD; |
| 850 | if (VD) |
| 851 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 852 | else |
| 853 | Info.Note(Base.dyn_cast<const Expr*>()->getExprLoc(), |
| 854 | diag::note_constexpr_temporary_here); |
| 855 | } else { |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 856 | Info.Diag(E->getExprLoc()); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 857 | } |
Richard Smith | 61e6162 | 2012-01-12 06:08:57 +0000 | [diff] [blame] | 858 | // Don't allow references to temporaries to escape. |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 859 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 860 | } |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 861 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 862 | bool IsReferenceType = E->isGLValue(); |
| 863 | |
| 864 | if (Designator.Invalid) { |
Richard Smith | 61e6162 | 2012-01-12 06:08:57 +0000 | [diff] [blame] | 865 | // This is not a core constant expression. An appropriate diagnostic will |
| 866 | // have already been produced. |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 867 | Value = APValue(LVal.getLValueBase(), LVal.getLValueOffset(), |
| 868 | APValue::NoLValuePath()); |
| 869 | return true; |
| 870 | } |
| 871 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 872 | Value = APValue(LVal.getLValueBase(), LVal.getLValueOffset(), |
| 873 | Designator.Entries, Designator.IsOnePastTheEnd); |
| 874 | |
| 875 | // Allow address constant expressions to be past-the-end pointers. This is |
| 876 | // an extension: the standard requires them to point to an object. |
| 877 | if (!IsReferenceType) |
| 878 | return true; |
| 879 | |
| 880 | // A reference constant expression must refer to an object. |
| 881 | if (!Base) { |
| 882 | // FIXME: diagnostic |
| 883 | Info.CCEDiag(E->getExprLoc()); |
Richard Smith | 61e6162 | 2012-01-12 06:08:57 +0000 | [diff] [blame] | 884 | return true; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 887 | // Does this refer one past the end of some object? |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 888 | if (Designator.isOnePastTheEnd()) { |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 889 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
| 890 | Info.Diag(E->getExprLoc(), diag::note_constexpr_past_end, 1) |
| 891 | << !Designator.Entries.empty() << !!VD << VD; |
| 892 | if (VD) |
| 893 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 894 | else |
| 895 | Info.Note(Base.dyn_cast<const Expr*>()->getExprLoc(), |
| 896 | diag::note_constexpr_temporary_here); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 897 | } |
| 898 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 899 | return true; |
| 900 | } |
| 901 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 902 | /// Check that this core constant expression is of literal type, and if not, |
| 903 | /// produce an appropriate diagnostic. |
| 904 | static bool CheckLiteralType(EvalInfo &Info, const Expr *E) { |
| 905 | if (!E->isRValue() || E->getType()->isLiteralType()) |
| 906 | return true; |
| 907 | |
| 908 | // Prvalue constant expressions must be of literal types. |
| 909 | if (Info.getLangOpts().CPlusPlus0x) |
| 910 | Info.Diag(E->getExprLoc(), diag::note_constexpr_nonliteral) |
| 911 | << E->getType(); |
| 912 | else |
| 913 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
| 914 | return false; |
| 915 | } |
| 916 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 917 | /// 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] | 918 | /// constant expression, and if it is, produce the corresponding constant value. |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 919 | /// If not, report an appropriate diagnostic. Does not check that the expression |
| 920 | /// is of literal type. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 921 | static bool CheckConstantExpression(EvalInfo &Info, const Expr *E, |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 922 | const CCValue &CCValue, APValue &Value, |
| 923 | CheckConstantExpressionKind CCEK |
| 924 | = CCEK_Constant) { |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 925 | if (!CCValue.isLValue()) { |
| 926 | Value = CCValue; |
| 927 | return true; |
| 928 | } |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 929 | return CheckLValueConstantExpression(Info, E, CCValue, Value, CCEK); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 930 | } |
| 931 | |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 932 | const ValueDecl *GetLValueBaseDecl(const LValue &LVal) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 933 | return LVal.Base.dyn_cast<const ValueDecl*>(); |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | static bool IsLiteralLValue(const LValue &Value) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 937 | return Value.Base.dyn_cast<const Expr*>() && !Value.Frame; |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 938 | } |
| 939 | |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 940 | static bool IsWeakLValue(const LValue &Value) { |
| 941 | const ValueDecl *Decl = GetLValueBaseDecl(Value); |
Lang Hames | 0dd7a25 | 2011-12-05 20:16:26 +0000 | [diff] [blame] | 942 | return Decl && Decl->isWeak(); |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 945 | static bool EvalPointerValueAsBool(const CCValue &Value, bool &Result) { |
John McCall | 3554283 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 946 | // A null base expression indicates a null pointer. These are always |
| 947 | // evaluatable, and they are false unless the offset is zero. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 948 | if (!Value.getLValueBase()) { |
| 949 | Result = !Value.getLValueOffset().isZero(); |
John McCall | 3554283 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 950 | return true; |
| 951 | } |
Rafael Espindola | a7d3c04 | 2010-05-07 15:18:43 +0000 | [diff] [blame] | 952 | |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 953 | // Require the base expression to be a global l-value. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 954 | // FIXME: C++11 requires such conversions. Remove this check. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 955 | if (!IsGlobalLValue(Value.getLValueBase())) return false; |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 956 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 957 | // We have a non-null base. These are generally known to be true, but if it's |
| 958 | // a weak declaration it can be null at runtime. |
John McCall | 3554283 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 959 | Result = true; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 960 | const ValueDecl *Decl = Value.getLValueBase().dyn_cast<const ValueDecl*>(); |
Lang Hames | 0dd7a25 | 2011-12-05 20:16:26 +0000 | [diff] [blame] | 961 | return !Decl || !Decl->isWeak(); |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 962 | } |
| 963 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 964 | static bool HandleConversionToBool(const CCValue &Val, bool &Result) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 965 | switch (Val.getKind()) { |
| 966 | case APValue::Uninitialized: |
| 967 | return false; |
| 968 | case APValue::Int: |
| 969 | Result = Val.getInt().getBoolValue(); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 970 | return true; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 971 | case APValue::Float: |
| 972 | Result = !Val.getFloat().isZero(); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 973 | return true; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 974 | case APValue::ComplexInt: |
| 975 | Result = Val.getComplexIntReal().getBoolValue() || |
| 976 | Val.getComplexIntImag().getBoolValue(); |
| 977 | return true; |
| 978 | case APValue::ComplexFloat: |
| 979 | Result = !Val.getComplexFloatReal().isZero() || |
| 980 | !Val.getComplexFloatImag().isZero(); |
| 981 | return true; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 982 | case APValue::LValue: |
| 983 | return EvalPointerValueAsBool(Val, Result); |
| 984 | case APValue::MemberPointer: |
| 985 | Result = Val.getMemberPointerDecl(); |
| 986 | return true; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 987 | case APValue::Vector: |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 988 | case APValue::Array: |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 989 | case APValue::Struct: |
| 990 | case APValue::Union: |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 991 | case APValue::AddrLabelDiff: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 992 | return false; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 993 | } |
| 994 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 995 | llvm_unreachable("unknown APValue kind"); |
| 996 | } |
| 997 | |
| 998 | static bool EvaluateAsBooleanCondition(const Expr *E, bool &Result, |
| 999 | EvalInfo &Info) { |
| 1000 | assert(E->isRValue() && "missing lvalue-to-rvalue conv in bool condition"); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1001 | CCValue Val; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1002 | if (!Evaluate(Val, Info, E)) |
| 1003 | return false; |
| 1004 | return HandleConversionToBool(Val, Result); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1007 | template<typename T> |
| 1008 | static bool HandleOverflow(EvalInfo &Info, const Expr *E, |
| 1009 | const T &SrcValue, QualType DestType) { |
| 1010 | llvm::SmallVector<char, 32> Buffer; |
| 1011 | SrcValue.toString(Buffer); |
| 1012 | Info.Diag(E->getExprLoc(), diag::note_constexpr_overflow) |
| 1013 | << StringRef(Buffer.data(), Buffer.size()) << DestType; |
| 1014 | return false; |
| 1015 | } |
| 1016 | |
| 1017 | static bool HandleFloatToIntCast(EvalInfo &Info, const Expr *E, |
| 1018 | QualType SrcType, const APFloat &Value, |
| 1019 | QualType DestType, APSInt &Result) { |
| 1020 | unsigned DestWidth = Info.Ctx.getIntWidth(DestType); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1021 | // Determine whether we are converting to unsigned or signed. |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 1022 | bool DestSigned = DestType->isSignedIntegerOrEnumerationType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1023 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1024 | Result = APSInt(DestWidth, !DestSigned); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1025 | bool ignored; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1026 | if (Value.convertToInteger(Result, llvm::APFloat::rmTowardZero, &ignored) |
| 1027 | & APFloat::opInvalidOp) |
| 1028 | return HandleOverflow(Info, E, Value, DestType); |
| 1029 | return true; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1032 | static bool HandleFloatToFloatCast(EvalInfo &Info, const Expr *E, |
| 1033 | QualType SrcType, QualType DestType, |
| 1034 | APFloat &Result) { |
| 1035 | APFloat Value = Result; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1036 | bool ignored; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1037 | if (Result.convert(Info.Ctx.getFloatTypeSemantics(DestType), |
| 1038 | APFloat::rmNearestTiesToEven, &ignored) |
| 1039 | & APFloat::opOverflow) |
| 1040 | return HandleOverflow(Info, E, Value, DestType); |
| 1041 | return true; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1044 | static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1045 | APSInt &Value, const ASTContext &Ctx) { |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1046 | unsigned DestWidth = Ctx.getIntWidth(DestType); |
| 1047 | APSInt Result = Value; |
| 1048 | // Figure out if this is a truncate, extend or noop cast. |
| 1049 | // If the input is signed, do a sign extend, noop, or truncate. |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1050 | Result = Result.extOrTrunc(DestWidth); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 1051 | Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType()); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1052 | return Result; |
| 1053 | } |
| 1054 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1055 | static bool HandleIntToFloatCast(EvalInfo &Info, const Expr *E, |
| 1056 | QualType SrcType, const APSInt &Value, |
| 1057 | QualType DestType, APFloat &Result) { |
| 1058 | Result = APFloat(Info.Ctx.getFloatTypeSemantics(DestType), 1); |
| 1059 | if (Result.convertFromAPInt(Value, Value.isSigned(), |
| 1060 | APFloat::rmNearestTiesToEven) |
| 1061 | & APFloat::opOverflow) |
| 1062 | return HandleOverflow(Info, E, Value, DestType); |
| 1063 | return true; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
Eli Friedman | e6a24e8 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 1066 | static bool EvalAndBitcastToAPInt(EvalInfo &Info, const Expr *E, |
| 1067 | llvm::APInt &Res) { |
| 1068 | CCValue SVal; |
| 1069 | if (!Evaluate(SVal, Info, E)) |
| 1070 | return false; |
| 1071 | if (SVal.isInt()) { |
| 1072 | Res = SVal.getInt(); |
| 1073 | return true; |
| 1074 | } |
| 1075 | if (SVal.isFloat()) { |
| 1076 | Res = SVal.getFloat().bitcastToAPInt(); |
| 1077 | return true; |
| 1078 | } |
| 1079 | if (SVal.isVector()) { |
| 1080 | QualType VecTy = E->getType(); |
| 1081 | unsigned VecSize = Info.Ctx.getTypeSize(VecTy); |
| 1082 | QualType EltTy = VecTy->castAs<VectorType>()->getElementType(); |
| 1083 | unsigned EltSize = Info.Ctx.getTypeSize(EltTy); |
| 1084 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
| 1085 | Res = llvm::APInt::getNullValue(VecSize); |
| 1086 | for (unsigned i = 0; i < SVal.getVectorLength(); i++) { |
| 1087 | APValue &Elt = SVal.getVectorElt(i); |
| 1088 | llvm::APInt EltAsInt; |
| 1089 | if (Elt.isInt()) { |
| 1090 | EltAsInt = Elt.getInt(); |
| 1091 | } else if (Elt.isFloat()) { |
| 1092 | EltAsInt = Elt.getFloat().bitcastToAPInt(); |
| 1093 | } else { |
| 1094 | // Don't try to handle vectors of anything other than int or float |
| 1095 | // (not sure if it's possible to hit this case). |
| 1096 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
| 1097 | return false; |
| 1098 | } |
| 1099 | unsigned BaseEltSize = EltAsInt.getBitWidth(); |
| 1100 | if (BigEndian) |
| 1101 | Res |= EltAsInt.zextOrTrunc(VecSize).rotr(i*EltSize+BaseEltSize); |
| 1102 | else |
| 1103 | Res |= EltAsInt.zextOrTrunc(VecSize).rotl(i*EltSize); |
| 1104 | } |
| 1105 | return true; |
| 1106 | } |
| 1107 | // Give up if the input isn't an int, float, or vector. For example, we |
| 1108 | // reject "(v4i16)(intptr_t)&a". |
| 1109 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
| 1110 | return false; |
| 1111 | } |
| 1112 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1113 | /// Cast an lvalue referring to a base subobject to a derived class, by |
| 1114 | /// truncating the lvalue's path to the given length. |
| 1115 | static bool CastToDerivedClass(EvalInfo &Info, const Expr *E, LValue &Result, |
| 1116 | const RecordDecl *TruncatedType, |
| 1117 | unsigned TruncatedElements) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1118 | SubobjectDesignator &D = Result.Designator; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1119 | |
| 1120 | // Check we actually point to a derived class object. |
| 1121 | if (TruncatedElements == D.Entries.size()) |
| 1122 | return true; |
| 1123 | assert(TruncatedElements >= D.MostDerivedPathLength && |
| 1124 | "not casting to a derived class"); |
| 1125 | if (!Result.checkSubobject(Info, E, CSK_Derived)) |
| 1126 | return false; |
| 1127 | |
| 1128 | // Truncate the path to the subobject, and remove any derived-to-base offsets. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1129 | const RecordDecl *RD = TruncatedType; |
| 1130 | for (unsigned I = TruncatedElements, N = D.Entries.size(); I != N; ++I) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1131 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 1132 | const CXXRecordDecl *Base = getAsBaseClass(D.Entries[I]); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1133 | if (isVirtualBaseClass(D.Entries[I])) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1134 | Result.Offset -= Layout.getVBaseClassOffset(Base); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1135 | else |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1136 | Result.Offset -= Layout.getBaseClassOffset(Base); |
| 1137 | RD = Base; |
| 1138 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1139 | D.Entries.resize(TruncatedElements); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1140 | return true; |
| 1141 | } |
| 1142 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1143 | static void HandleLValueDirectBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1144 | const CXXRecordDecl *Derived, |
| 1145 | const CXXRecordDecl *Base, |
| 1146 | const ASTRecordLayout *RL = 0) { |
| 1147 | if (!RL) RL = &Info.Ctx.getASTRecordLayout(Derived); |
| 1148 | Obj.getLValueOffset() += RL->getBaseClassOffset(Base); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1149 | Obj.addDecl(Info, E, Base, /*Virtual*/ false); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1150 | } |
| 1151 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1152 | static bool HandleLValueBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1153 | const CXXRecordDecl *DerivedDecl, |
| 1154 | const CXXBaseSpecifier *Base) { |
| 1155 | const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl(); |
| 1156 | |
| 1157 | if (!Base->isVirtual()) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1158 | HandleLValueDirectBase(Info, E, Obj, DerivedDecl, BaseDecl); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1159 | return true; |
| 1160 | } |
| 1161 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1162 | SubobjectDesignator &D = Obj.Designator; |
| 1163 | if (D.Invalid) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1164 | return false; |
| 1165 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1166 | // Extract most-derived object and corresponding type. |
| 1167 | DerivedDecl = D.MostDerivedType->getAsCXXRecordDecl(); |
| 1168 | if (!CastToDerivedClass(Info, E, Obj, DerivedDecl, D.MostDerivedPathLength)) |
| 1169 | return false; |
| 1170 | |
| 1171 | // Find the virtual base class. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1172 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl); |
| 1173 | Obj.getLValueOffset() += Layout.getVBaseClassOffset(BaseDecl); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1174 | Obj.addDecl(Info, E, BaseDecl, /*Virtual*/ true); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1175 | return true; |
| 1176 | } |
| 1177 | |
| 1178 | /// Update LVal to refer to the given field, which must be a member of the type |
| 1179 | /// currently described by LVal. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1180 | static void HandleLValueMember(EvalInfo &Info, const Expr *E, LValue &LVal, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1181 | const FieldDecl *FD, |
| 1182 | const ASTRecordLayout *RL = 0) { |
| 1183 | if (!RL) |
| 1184 | RL = &Info.Ctx.getASTRecordLayout(FD->getParent()); |
| 1185 | |
| 1186 | unsigned I = FD->getFieldIndex(); |
| 1187 | LVal.Offset += Info.Ctx.toCharUnitsFromBits(RL->getFieldOffset(I)); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1188 | LVal.addDecl(Info, E, FD); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | /// Get the size of the given type in char units. |
| 1192 | static bool HandleSizeof(EvalInfo &Info, QualType Type, CharUnits &Size) { |
| 1193 | // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc |
| 1194 | // extension. |
| 1195 | if (Type->isVoidType() || Type->isFunctionType()) { |
| 1196 | Size = CharUnits::One(); |
| 1197 | return true; |
| 1198 | } |
| 1199 | |
| 1200 | if (!Type->isConstantSizeType()) { |
| 1201 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1202 | // FIXME: Diagnostic. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1203 | return false; |
| 1204 | } |
| 1205 | |
| 1206 | Size = Info.Ctx.getTypeSizeInChars(Type); |
| 1207 | return true; |
| 1208 | } |
| 1209 | |
| 1210 | /// Update a pointer value to model pointer arithmetic. |
| 1211 | /// \param Info - Information about the ongoing evaluation. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1212 | /// \param E - The expression being evaluated, for diagnostic purposes. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1213 | /// \param LVal - The pointer value to be updated. |
| 1214 | /// \param EltTy - The pointee type represented by LVal. |
| 1215 | /// \param Adjustment - The adjustment, in objects of type EltTy, to add. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1216 | static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E, |
| 1217 | LValue &LVal, QualType EltTy, |
| 1218 | int64_t Adjustment) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1219 | CharUnits SizeOfPointee; |
| 1220 | if (!HandleSizeof(Info, EltTy, SizeOfPointee)) |
| 1221 | return false; |
| 1222 | |
| 1223 | // Compute the new offset in the appropriate width. |
| 1224 | LVal.Offset += Adjustment * SizeOfPointee; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1225 | LVal.adjustIndex(Info, E, Adjustment); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1226 | return true; |
| 1227 | } |
| 1228 | |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1229 | /// Try to evaluate the initializer for a variable declaration. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1230 | static bool EvaluateVarDeclInit(EvalInfo &Info, const Expr *E, |
| 1231 | const VarDecl *VD, |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1232 | CallStackFrame *Frame, CCValue &Result) { |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1233 | // If this is a parameter to an active constexpr function call, perform |
| 1234 | // argument substitution. |
| 1235 | if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(VD)) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1236 | if (!Frame || !Frame->Arguments) { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 1237 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1238 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1239 | } |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1240 | Result = Frame->Arguments[PVD->getFunctionScopeIndex()]; |
| 1241 | return true; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1242 | } |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1243 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1244 | // Dig out the initializer, and use the declaration which it's attached to. |
| 1245 | const Expr *Init = VD->getAnyInitializer(VD); |
| 1246 | if (!Init || Init->isValueDependent()) { |
| 1247 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
| 1248 | return false; |
| 1249 | } |
| 1250 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1251 | // If we're currently evaluating the initializer of this declaration, use that |
| 1252 | // in-flight value. |
| 1253 | if (Info.EvaluatingDecl == VD) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1254 | Result = CCValue(Info.Ctx, *Info.EvaluatingDeclValue, |
| 1255 | CCValue::GlobalValue()); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1256 | return !Result.isUninit(); |
| 1257 | } |
| 1258 | |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1259 | // Never evaluate the initializer of a weak variable. We can't be sure that |
| 1260 | // this is the definition which will be used. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1261 | if (VD->isWeak()) { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 1262 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1263 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1264 | } |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1265 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1266 | // Check that we can fold the initializer. In C++, we will have already done |
| 1267 | // this in the cases where it matters for conformance. |
| 1268 | llvm::SmallVector<PartialDiagnosticAt, 8> Notes; |
| 1269 | if (!VD->evaluateValue(Notes)) { |
| 1270 | Info.Diag(E->getExprLoc(), diag::note_constexpr_var_init_non_constant, |
| 1271 | Notes.size() + 1) << VD; |
| 1272 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1273 | Info.addNotes(Notes); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1274 | return false; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1275 | } else if (!VD->checkInitIsICE()) { |
| 1276 | Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_var_init_non_constant, |
| 1277 | Notes.size() + 1) << VD; |
| 1278 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1279 | Info.addNotes(Notes); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1280 | } |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1281 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1282 | Result = CCValue(Info.Ctx, *VD->getEvaluatedValue(), CCValue::GlobalValue()); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1283 | return true; |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1286 | static bool IsConstNonVolatile(QualType T) { |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1287 | Qualifiers Quals = T.getQualifiers(); |
| 1288 | return Quals.hasConst() && !Quals.hasVolatile(); |
| 1289 | } |
| 1290 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1291 | /// Get the base index of the given base class within an APValue representing |
| 1292 | /// the given derived class. |
| 1293 | static unsigned getBaseIndex(const CXXRecordDecl *Derived, |
| 1294 | const CXXRecordDecl *Base) { |
| 1295 | Base = Base->getCanonicalDecl(); |
| 1296 | unsigned Index = 0; |
| 1297 | for (CXXRecordDecl::base_class_const_iterator I = Derived->bases_begin(), |
| 1298 | E = Derived->bases_end(); I != E; ++I, ++Index) { |
| 1299 | if (I->getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base) |
| 1300 | return Index; |
| 1301 | } |
| 1302 | |
| 1303 | llvm_unreachable("base class missing from derived class's bases list"); |
| 1304 | } |
| 1305 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1306 | /// Extract the designated sub-object of an rvalue. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1307 | static bool ExtractSubobject(EvalInfo &Info, const Expr *E, |
| 1308 | CCValue &Obj, QualType ObjType, |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1309 | const SubobjectDesignator &Sub, QualType SubType) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1310 | if (Sub.Invalid) |
| 1311 | // A diagnostic will have already been produced. |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1312 | return false; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1313 | if (Sub.isOnePastTheEnd()) { |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1314 | Info.Diag(E->getExprLoc(), Info.getLangOpts().CPlusPlus0x ? |
Matt Beaumont-Gay | aa5d533 | 2011-12-21 19:36:37 +0000 | [diff] [blame] | 1315 | (unsigned)diag::note_constexpr_read_past_end : |
| 1316 | (unsigned)diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1317 | return false; |
| 1318 | } |
Richard Smith | f64699e | 2011-11-11 08:28:03 +0000 | [diff] [blame] | 1319 | if (Sub.Entries.empty()) |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1320 | return true; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1321 | |
| 1322 | assert(!Obj.isLValue() && "extracting subobject of lvalue"); |
| 1323 | const APValue *O = &Obj; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1324 | // Walk the designator's path to find the subobject. |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1325 | for (unsigned I = 0, N = Sub.Entries.size(); I != N; ++I) { |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1326 | if (ObjType->isArrayType()) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1327 | // Next subobject is an array element. |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1328 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(ObjType); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1329 | assert(CAT && "vla in literal type?"); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1330 | uint64_t Index = Sub.Entries[I].ArrayIndex; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1331 | if (CAT->getSize().ule(Index)) { |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1332 | // Note, it should not be possible to form a pointer with a valid |
| 1333 | // designator which points more than one past the end of the array. |
| 1334 | Info.Diag(E->getExprLoc(), Info.getLangOpts().CPlusPlus0x ? |
Matt Beaumont-Gay | aa5d533 | 2011-12-21 19:36:37 +0000 | [diff] [blame] | 1335 | (unsigned)diag::note_constexpr_read_past_end : |
| 1336 | (unsigned)diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1337 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1338 | } |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1339 | if (O->getArrayInitializedElts() > Index) |
| 1340 | O = &O->getArrayInitializedElt(Index); |
| 1341 | else |
| 1342 | O = &O->getArrayFiller(); |
| 1343 | ObjType = CAT->getElementType(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1344 | } else if (const FieldDecl *Field = getAsField(Sub.Entries[I])) { |
| 1345 | // Next subobject is a class, struct or union field. |
| 1346 | RecordDecl *RD = ObjType->castAs<RecordType>()->getDecl(); |
| 1347 | if (RD->isUnion()) { |
| 1348 | const FieldDecl *UnionField = O->getUnionField(); |
| 1349 | if (!UnionField || |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1350 | UnionField->getCanonicalDecl() != Field->getCanonicalDecl()) { |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1351 | Info.Diag(E->getExprLoc(), |
| 1352 | diag::note_constexpr_read_inactive_union_member) |
| 1353 | << Field << !UnionField << UnionField; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1354 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1355 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1356 | O = &O->getUnionValue(); |
| 1357 | } else |
| 1358 | O = &O->getStructField(Field->getFieldIndex()); |
| 1359 | ObjType = Field->getType(); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1360 | |
| 1361 | if (ObjType.isVolatileQualified()) { |
| 1362 | if (Info.getLangOpts().CPlusPlus) { |
| 1363 | // FIXME: Include a description of the path to the volatile subobject. |
| 1364 | Info.Diag(E->getExprLoc(), diag::note_constexpr_ltor_volatile_obj, 1) |
| 1365 | << 2 << Field; |
| 1366 | Info.Note(Field->getLocation(), diag::note_declared_at); |
| 1367 | } else { |
| 1368 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
| 1369 | } |
| 1370 | return false; |
| 1371 | } |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1372 | } else { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1373 | // Next subobject is a base class. |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1374 | const CXXRecordDecl *Derived = ObjType->getAsCXXRecordDecl(); |
| 1375 | const CXXRecordDecl *Base = getAsBaseClass(Sub.Entries[I]); |
| 1376 | O = &O->getStructBase(getBaseIndex(Derived, Base)); |
| 1377 | ObjType = Info.Ctx.getRecordType(Base); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1378 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1379 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1380 | if (O->isUninit()) { |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1381 | Info.Diag(E->getExprLoc(), diag::note_constexpr_read_uninit); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1382 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1383 | } |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1386 | Obj = CCValue(Info.Ctx, *O, CCValue::GlobalValue()); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1387 | return true; |
| 1388 | } |
| 1389 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1390 | /// HandleLValueToRValueConversion - Perform an lvalue-to-rvalue conversion on |
| 1391 | /// the given lvalue. This can also be used for 'lvalue-to-lvalue' conversions |
| 1392 | /// for looking up the glvalue referred to by an entity of reference type. |
| 1393 | /// |
| 1394 | /// \param Info - Information about the ongoing evaluation. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1395 | /// \param Conv - The expression for which we are performing the conversion. |
| 1396 | /// Used for diagnostics. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1397 | /// \param Type - The type we expect this conversion to produce. |
| 1398 | /// \param LVal - The glvalue on which we are attempting to perform this action. |
| 1399 | /// \param RVal - The produced value will be placed here. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1400 | static bool HandleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv, |
| 1401 | QualType Type, |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1402 | const LValue &LVal, CCValue &RVal) { |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1403 | // In C, an lvalue-to-rvalue conversion is never a constant expression. |
| 1404 | if (!Info.getLangOpts().CPlusPlus) |
| 1405 | Info.CCEDiag(Conv->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
| 1406 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1407 | if (LVal.Designator.Invalid) |
| 1408 | // A diagnostic will have already been produced. |
| 1409 | return false; |
| 1410 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1411 | const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1412 | CallStackFrame *Frame = LVal.Frame; |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1413 | SourceLocation Loc = Conv->getExprLoc(); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1414 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1415 | if (!LVal.Base) { |
| 1416 | // FIXME: Indirection through a null pointer deserves a specific diagnostic. |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1417 | Info.Diag(Loc, diag::note_invalid_subexpr_in_const_expr); |
| 1418 | return false; |
| 1419 | } |
| 1420 | |
| 1421 | // C++11 DR1311: An lvalue-to-rvalue conversion on a volatile-qualified type |
| 1422 | // is not a constant expression (even if the object is non-volatile). We also |
| 1423 | // apply this rule to C++98, in order to conform to the expected 'volatile' |
| 1424 | // semantics. |
| 1425 | if (Type.isVolatileQualified()) { |
| 1426 | if (Info.getLangOpts().CPlusPlus) |
| 1427 | Info.Diag(Loc, diag::note_constexpr_ltor_volatile_type) << Type; |
| 1428 | else |
| 1429 | Info.Diag(Loc); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1430 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1431 | } |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1432 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1433 | if (const ValueDecl *D = LVal.Base.dyn_cast<const ValueDecl*>()) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1434 | // In C++98, const, non-volatile integers initialized with ICEs are ICEs. |
| 1435 | // In C++11, constexpr, non-volatile variables initialized with constant |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1436 | // expressions are constant expressions too. Inside constexpr functions, |
| 1437 | // parameters are constant expressions even if they're non-const. |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1438 | // In C, such things can also be folded, although they are not ICEs. |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1439 | const VarDecl *VD = dyn_cast<VarDecl>(D); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1440 | if (!VD || VD->isInvalidDecl()) { |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1441 | Info.Diag(Loc); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1442 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1445 | // DR1313: If the object is volatile-qualified but the glvalue was not, |
| 1446 | // behavior is undefined so the result is not a constant expression. |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1447 | QualType VT = VD->getType(); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1448 | if (VT.isVolatileQualified()) { |
| 1449 | if (Info.getLangOpts().CPlusPlus) { |
| 1450 | Info.Diag(Loc, diag::note_constexpr_ltor_volatile_obj, 1) << 1 << VD; |
| 1451 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1452 | } else { |
| 1453 | Info.Diag(Loc); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1454 | } |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1455 | return false; |
| 1456 | } |
| 1457 | |
| 1458 | if (!isa<ParmVarDecl>(VD)) { |
| 1459 | if (VD->isConstexpr()) { |
| 1460 | // OK, we can read this variable. |
| 1461 | } else if (VT->isIntegralOrEnumerationType()) { |
| 1462 | if (!VT.isConstQualified()) { |
| 1463 | if (Info.getLangOpts().CPlusPlus) { |
| 1464 | Info.Diag(Loc, diag::note_constexpr_ltor_non_const_int, 1) << VD; |
| 1465 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1466 | } else { |
| 1467 | Info.Diag(Loc); |
| 1468 | } |
| 1469 | return false; |
| 1470 | } |
| 1471 | } else if (VT->isFloatingType() && VT.isConstQualified()) { |
| 1472 | // We support folding of const floating-point types, in order to make |
| 1473 | // static const data members of such types (supported as an extension) |
| 1474 | // more useful. |
| 1475 | if (Info.getLangOpts().CPlusPlus0x) { |
| 1476 | Info.CCEDiag(Loc, diag::note_constexpr_ltor_non_constexpr, 1) << VD; |
| 1477 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1478 | } else { |
| 1479 | Info.CCEDiag(Loc); |
| 1480 | } |
| 1481 | } else { |
| 1482 | // FIXME: Allow folding of values of any literal type in all languages. |
| 1483 | if (Info.getLangOpts().CPlusPlus0x) { |
| 1484 | Info.Diag(Loc, diag::note_constexpr_ltor_non_constexpr, 1) << VD; |
| 1485 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1486 | } else { |
| 1487 | Info.Diag(Loc); |
| 1488 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1489 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1490 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1491 | } |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1492 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1493 | if (!EvaluateVarDeclInit(Info, Conv, VD, Frame, RVal)) |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1494 | return false; |
| 1495 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1496 | if (isa<ParmVarDecl>(VD) || !VD->getAnyInitializer()->isLValue()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1497 | return ExtractSubobject(Info, Conv, RVal, VT, LVal.Designator, Type); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1498 | |
| 1499 | // The declaration was initialized by an lvalue, with no lvalue-to-rvalue |
| 1500 | // conversion. This happens when the declaration and the lvalue should be |
| 1501 | // considered synonymous, for instance when initializing an array of char |
| 1502 | // from a string literal. Continue as if the initializer lvalue was the |
| 1503 | // value we were originally given. |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1504 | assert(RVal.getLValueOffset().isZero() && |
| 1505 | "offset for lvalue init of non-reference"); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1506 | Base = RVal.getLValueBase().get<const Expr*>(); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1507 | Frame = RVal.getLValueFrame(); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1508 | } |
| 1509 | |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1510 | // Volatile temporary objects cannot be read in constant expressions. |
| 1511 | if (Base->getType().isVolatileQualified()) { |
| 1512 | if (Info.getLangOpts().CPlusPlus) { |
| 1513 | Info.Diag(Loc, diag::note_constexpr_ltor_volatile_obj, 1) << 0; |
| 1514 | Info.Note(Base->getExprLoc(), diag::note_constexpr_temporary_here); |
| 1515 | } else { |
| 1516 | Info.Diag(Loc); |
| 1517 | } |
| 1518 | return false; |
| 1519 | } |
| 1520 | |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1521 | // FIXME: Support PredefinedExpr, ObjCEncodeExpr, MakeStringConstant |
| 1522 | if (const StringLiteral *S = dyn_cast<StringLiteral>(Base)) { |
| 1523 | const SubobjectDesignator &Designator = LVal.Designator; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1524 | if (Designator.Invalid || Designator.Entries.size() != 1) { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 1525 | Info.Diag(Conv->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1526 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1527 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1528 | |
| 1529 | assert(Type->isIntegerType() && "string element not integer type"); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1530 | uint64_t Index = Designator.Entries[0].ArrayIndex; |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1531 | const ConstantArrayType *CAT = |
| 1532 | Info.Ctx.getAsConstantArrayType(S->getType()); |
| 1533 | if (Index >= CAT->getSize().getZExtValue()) { |
| 1534 | // Note, it should not be possible to form a pointer which points more |
| 1535 | // than one past the end of the array without producing a prior const expr |
| 1536 | // diagnostic. |
| 1537 | Info.Diag(Loc, diag::note_constexpr_read_past_end); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1538 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1539 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1540 | APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(), |
| 1541 | Type->isUnsignedIntegerType()); |
| 1542 | if (Index < S->getLength()) |
| 1543 | Value = S->getCodeUnit(Index); |
| 1544 | RVal = CCValue(Value); |
| 1545 | return true; |
| 1546 | } |
| 1547 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1548 | if (Frame) { |
| 1549 | // If this is a temporary expression with a nontrivial initializer, grab the |
| 1550 | // value from the relevant stack frame. |
| 1551 | RVal = Frame->Temporaries[Base]; |
| 1552 | } else if (const CompoundLiteralExpr *CLE |
| 1553 | = dyn_cast<CompoundLiteralExpr>(Base)) { |
| 1554 | // In C99, a CompoundLiteralExpr is an lvalue, and we defer evaluating the |
| 1555 | // initializer until now for such expressions. Such an expression can't be |
| 1556 | // an ICE in C, so this only matters for fold. |
| 1557 | assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?"); |
| 1558 | if (!Evaluate(RVal, Info, CLE->getInitializer())) |
| 1559 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1560 | } else { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 1561 | Info.Diag(Conv->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1562 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1563 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1564 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1565 | return ExtractSubobject(Info, Conv, RVal, Base->getType(), LVal.Designator, |
| 1566 | Type); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1567 | } |
| 1568 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1569 | /// Build an lvalue for the object argument of a member function call. |
| 1570 | static bool EvaluateObjectArgument(EvalInfo &Info, const Expr *Object, |
| 1571 | LValue &This) { |
| 1572 | if (Object->getType()->isPointerType()) |
| 1573 | return EvaluatePointer(Object, This, Info); |
| 1574 | |
| 1575 | if (Object->isGLValue()) |
| 1576 | return EvaluateLValue(Object, This, Info); |
| 1577 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1578 | if (Object->getType()->isLiteralType()) |
| 1579 | return EvaluateTemporary(Object, This, Info); |
| 1580 | |
| 1581 | return false; |
| 1582 | } |
| 1583 | |
| 1584 | /// HandleMemberPointerAccess - Evaluate a member access operation and build an |
| 1585 | /// lvalue referring to the result. |
| 1586 | /// |
| 1587 | /// \param Info - Information about the ongoing evaluation. |
| 1588 | /// \param BO - The member pointer access operation. |
| 1589 | /// \param LV - Filled in with a reference to the resulting object. |
| 1590 | /// \param IncludeMember - Specifies whether the member itself is included in |
| 1591 | /// the resulting LValue subobject designator. This is not possible when |
| 1592 | /// creating a bound member function. |
| 1593 | /// \return The field or method declaration to which the member pointer refers, |
| 1594 | /// or 0 if evaluation fails. |
| 1595 | static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, |
| 1596 | const BinaryOperator *BO, |
| 1597 | LValue &LV, |
| 1598 | bool IncludeMember = true) { |
| 1599 | assert(BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI); |
| 1600 | |
| 1601 | if (!EvaluateObjectArgument(Info, BO->getLHS(), LV)) |
| 1602 | return 0; |
| 1603 | |
| 1604 | MemberPtr MemPtr; |
| 1605 | if (!EvaluateMemberPointer(BO->getRHS(), MemPtr, Info)) |
| 1606 | return 0; |
| 1607 | |
| 1608 | // C++11 [expr.mptr.oper]p6: If the second operand is the null pointer to |
| 1609 | // member value, the behavior is undefined. |
| 1610 | if (!MemPtr.getDecl()) |
| 1611 | return 0; |
| 1612 | |
| 1613 | if (MemPtr.isDerivedMember()) { |
| 1614 | // This is a member of some derived class. Truncate LV appropriately. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1615 | // The end of the derived-to-base path for the base object must match the |
| 1616 | // derived-to-base path for the member pointer. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1617 | if (LV.Designator.MostDerivedPathLength + MemPtr.Path.size() > |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1618 | LV.Designator.Entries.size()) |
| 1619 | return 0; |
| 1620 | unsigned PathLengthToMember = |
| 1621 | LV.Designator.Entries.size() - MemPtr.Path.size(); |
| 1622 | for (unsigned I = 0, N = MemPtr.Path.size(); I != N; ++I) { |
| 1623 | const CXXRecordDecl *LVDecl = getAsBaseClass( |
| 1624 | LV.Designator.Entries[PathLengthToMember + I]); |
| 1625 | const CXXRecordDecl *MPDecl = MemPtr.Path[I]; |
| 1626 | if (LVDecl->getCanonicalDecl() != MPDecl->getCanonicalDecl()) |
| 1627 | return 0; |
| 1628 | } |
| 1629 | |
| 1630 | // Truncate the lvalue to the appropriate derived class. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1631 | if (!CastToDerivedClass(Info, BO, LV, MemPtr.getContainingRecord(), |
| 1632 | PathLengthToMember)) |
| 1633 | return 0; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1634 | } else if (!MemPtr.Path.empty()) { |
| 1635 | // Extend the LValue path with the member pointer's path. |
| 1636 | LV.Designator.Entries.reserve(LV.Designator.Entries.size() + |
| 1637 | MemPtr.Path.size() + IncludeMember); |
| 1638 | |
| 1639 | // Walk down to the appropriate base class. |
| 1640 | QualType LVType = BO->getLHS()->getType(); |
| 1641 | if (const PointerType *PT = LVType->getAs<PointerType>()) |
| 1642 | LVType = PT->getPointeeType(); |
| 1643 | const CXXRecordDecl *RD = LVType->getAsCXXRecordDecl(); |
| 1644 | assert(RD && "member pointer access on non-class-type expression"); |
| 1645 | // The first class in the path is that of the lvalue. |
| 1646 | for (unsigned I = 1, N = MemPtr.Path.size(); I != N; ++I) { |
| 1647 | const CXXRecordDecl *Base = MemPtr.Path[N - I - 1]; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1648 | HandleLValueDirectBase(Info, BO, LV, RD, Base); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1649 | RD = Base; |
| 1650 | } |
| 1651 | // Finally cast to the class containing the member. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1652 | HandleLValueDirectBase(Info, BO, LV, RD, MemPtr.getContainingRecord()); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | // Add the member. Note that we cannot build bound member functions here. |
| 1656 | if (IncludeMember) { |
| 1657 | // FIXME: Deal with IndirectFieldDecls. |
| 1658 | const FieldDecl *FD = dyn_cast<FieldDecl>(MemPtr.getDecl()); |
| 1659 | if (!FD) return 0; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1660 | HandleLValueMember(Info, BO, LV, FD); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
| 1663 | return MemPtr.getDecl(); |
| 1664 | } |
| 1665 | |
| 1666 | /// HandleBaseToDerivedCast - Apply the given base-to-derived cast operation on |
| 1667 | /// the provided lvalue, which currently refers to the base object. |
| 1668 | static bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E, |
| 1669 | LValue &Result) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1670 | SubobjectDesignator &D = Result.Designator; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1671 | if (D.Invalid || !Result.checkNullPointer(Info, E, CSK_Derived)) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1672 | return false; |
| 1673 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1674 | QualType TargetQT = E->getType(); |
| 1675 | if (const PointerType *PT = TargetQT->getAs<PointerType>()) |
| 1676 | TargetQT = PT->getPointeeType(); |
| 1677 | |
| 1678 | // Check this cast lands within the final derived-to-base subobject path. |
| 1679 | if (D.MostDerivedPathLength + E->path_size() > D.Entries.size()) { |
| 1680 | Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_invalid_downcast) |
| 1681 | << D.MostDerivedType << TargetQT; |
| 1682 | return false; |
| 1683 | } |
| 1684 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1685 | // Check the type of the final cast. We don't need to check the path, |
| 1686 | // since a cast can only be formed if the path is unique. |
| 1687 | unsigned NewEntriesSize = D.Entries.size() - E->path_size(); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1688 | const CXXRecordDecl *TargetType = TargetQT->getAsCXXRecordDecl(); |
| 1689 | const CXXRecordDecl *FinalType; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1690 | if (NewEntriesSize == D.MostDerivedPathLength) |
| 1691 | FinalType = D.MostDerivedType->getAsCXXRecordDecl(); |
| 1692 | else |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1693 | FinalType = getAsBaseClass(D.Entries[NewEntriesSize - 1]); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1694 | if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl()) { |
| 1695 | Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_invalid_downcast) |
| 1696 | << D.MostDerivedType << TargetQT; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1697 | return false; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1698 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1699 | |
| 1700 | // Truncate the lvalue to the appropriate derived class. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1701 | return CastToDerivedClass(Info, E, Result, TargetType, NewEntriesSize); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1704 | namespace { |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1705 | enum EvalStmtResult { |
| 1706 | /// Evaluation failed. |
| 1707 | ESR_Failed, |
| 1708 | /// Hit a 'return' statement. |
| 1709 | ESR_Returned, |
| 1710 | /// Evaluation succeeded. |
| 1711 | ESR_Succeeded |
| 1712 | }; |
| 1713 | } |
| 1714 | |
| 1715 | // Evaluate a statement. |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1716 | static EvalStmtResult EvaluateStmt(APValue &Result, EvalInfo &Info, |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1717 | const Stmt *S) { |
| 1718 | switch (S->getStmtClass()) { |
| 1719 | default: |
| 1720 | return ESR_Failed; |
| 1721 | |
| 1722 | case Stmt::NullStmtClass: |
| 1723 | case Stmt::DeclStmtClass: |
| 1724 | return ESR_Succeeded; |
| 1725 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1726 | case Stmt::ReturnStmtClass: { |
| 1727 | CCValue CCResult; |
| 1728 | const Expr *RetExpr = cast<ReturnStmt>(S)->getRetValue(); |
| 1729 | if (!Evaluate(CCResult, Info, RetExpr) || |
| 1730 | !CheckConstantExpression(Info, RetExpr, CCResult, Result, |
| 1731 | CCEK_ReturnValue)) |
| 1732 | return ESR_Failed; |
| 1733 | return ESR_Returned; |
| 1734 | } |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1735 | |
| 1736 | case Stmt::CompoundStmtClass: { |
| 1737 | const CompoundStmt *CS = cast<CompoundStmt>(S); |
| 1738 | for (CompoundStmt::const_body_iterator BI = CS->body_begin(), |
| 1739 | BE = CS->body_end(); BI != BE; ++BI) { |
| 1740 | EvalStmtResult ESR = EvaluateStmt(Result, Info, *BI); |
| 1741 | if (ESR != ESR_Succeeded) |
| 1742 | return ESR; |
| 1743 | } |
| 1744 | return ESR_Succeeded; |
| 1745 | } |
| 1746 | } |
| 1747 | } |
| 1748 | |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 1749 | /// CheckTrivialDefaultConstructor - Check whether a constructor is a trivial |
| 1750 | /// default constructor. If so, we'll fold it whether or not it's marked as |
| 1751 | /// constexpr. If it is marked as constexpr, we will never implicitly define it, |
| 1752 | /// so we need special handling. |
| 1753 | static bool CheckTrivialDefaultConstructor(EvalInfo &Info, SourceLocation Loc, |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1754 | const CXXConstructorDecl *CD, |
| 1755 | bool IsValueInitialization) { |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 1756 | if (!CD->isTrivial() || !CD->isDefaultConstructor()) |
| 1757 | return false; |
| 1758 | |
| 1759 | if (!CD->isConstexpr()) { |
| 1760 | if (Info.getLangOpts().CPlusPlus0x) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1761 | // Value-initialization does not call a trivial default constructor, so |
| 1762 | // such a call is a core constant expression whether or not the |
| 1763 | // constructor is constexpr. |
| 1764 | if (!IsValueInitialization) { |
| 1765 | // FIXME: If DiagDecl is an implicitly-declared special member function, |
| 1766 | // we should be much more explicit about why it's not constexpr. |
| 1767 | Info.CCEDiag(Loc, diag::note_constexpr_invalid_function, 1) |
| 1768 | << /*IsConstexpr*/0 << /*IsConstructor*/1 << CD; |
| 1769 | Info.Note(CD->getLocation(), diag::note_declared_at); |
| 1770 | } |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 1771 | } else { |
| 1772 | Info.CCEDiag(Loc, diag::note_invalid_subexpr_in_const_expr); |
| 1773 | } |
| 1774 | } |
| 1775 | return true; |
| 1776 | } |
| 1777 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1778 | /// CheckConstexprFunction - Check that a function can be called in a constant |
| 1779 | /// expression. |
| 1780 | static bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc, |
| 1781 | const FunctionDecl *Declaration, |
| 1782 | const FunctionDecl *Definition) { |
| 1783 | // Can we evaluate this function call? |
| 1784 | if (Definition && Definition->isConstexpr() && !Definition->isInvalidDecl()) |
| 1785 | return true; |
| 1786 | |
| 1787 | if (Info.getLangOpts().CPlusPlus0x) { |
| 1788 | const FunctionDecl *DiagDecl = Definition ? Definition : Declaration; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1789 | // FIXME: If DiagDecl is an implicitly-declared special member function, we |
| 1790 | // should be much more explicit about why it's not constexpr. |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1791 | Info.Diag(CallLoc, diag::note_constexpr_invalid_function, 1) |
| 1792 | << DiagDecl->isConstexpr() << isa<CXXConstructorDecl>(DiagDecl) |
| 1793 | << DiagDecl; |
| 1794 | Info.Note(DiagDecl->getLocation(), diag::note_declared_at); |
| 1795 | } else { |
| 1796 | Info.Diag(CallLoc, diag::note_invalid_subexpr_in_const_expr); |
| 1797 | } |
| 1798 | return false; |
| 1799 | } |
| 1800 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1801 | namespace { |
Richard Smith | cd99b07 | 2011-11-11 05:48:57 +0000 | [diff] [blame] | 1802 | typedef SmallVector<CCValue, 8> ArgVector; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1803 | } |
| 1804 | |
| 1805 | /// EvaluateArgs - Evaluate the arguments to a function call. |
| 1806 | static bool EvaluateArgs(ArrayRef<const Expr*> Args, ArgVector &ArgValues, |
| 1807 | EvalInfo &Info) { |
| 1808 | for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end(); |
| 1809 | I != E; ++I) |
| 1810 | if (!Evaluate(ArgValues[I - Args.begin()], Info, *I)) |
| 1811 | return false; |
| 1812 | return true; |
| 1813 | } |
| 1814 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1815 | /// Evaluate a function call. |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 1816 | static bool HandleFunctionCall(const Expr *CallExpr, const FunctionDecl *Callee, |
| 1817 | const LValue *This, |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1818 | ArrayRef<const Expr*> Args, const Stmt *Body, |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1819 | EvalInfo &Info, APValue &Result) { |
| 1820 | if (!Info.CheckCallLimit(CallExpr->getExprLoc())) |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1821 | return false; |
| 1822 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1823 | ArgVector ArgValues(Args.size()); |
| 1824 | if (!EvaluateArgs(Args, ArgValues, Info)) |
| 1825 | return false; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1826 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 1827 | CallStackFrame Frame(Info, CallExpr->getExprLoc(), Callee, This, |
| 1828 | ArgValues.data()); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1829 | return EvaluateStmt(Result, Info, Body) == ESR_Returned; |
| 1830 | } |
| 1831 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1832 | /// Evaluate a constructor call. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1833 | static bool HandleConstructorCall(const Expr *CallExpr, const LValue &This, |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1834 | ArrayRef<const Expr*> Args, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1835 | const CXXConstructorDecl *Definition, |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1836 | EvalInfo &Info, APValue &Result) { |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1837 | if (!Info.CheckCallLimit(CallExpr->getExprLoc())) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1838 | return false; |
| 1839 | |
| 1840 | ArgVector ArgValues(Args.size()); |
| 1841 | if (!EvaluateArgs(Args, ArgValues, Info)) |
| 1842 | return false; |
| 1843 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 1844 | CallStackFrame Frame(Info, CallExpr->getExprLoc(), Definition, |
| 1845 | &This, ArgValues.data()); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1846 | |
| 1847 | // If it's a delegating constructor, just delegate. |
| 1848 | if (Definition->isDelegatingConstructor()) { |
| 1849 | CXXConstructorDecl::init_const_iterator I = Definition->init_begin(); |
| 1850 | return EvaluateConstantExpression(Result, Info, This, (*I)->getInit()); |
| 1851 | } |
| 1852 | |
Richard Smith | 610a60c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 1853 | // For a trivial copy or move constructor, perform an APValue copy. This is |
| 1854 | // essential for unions, where the operations performed by the constructor |
| 1855 | // cannot be represented by ctor-initializers. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1856 | const CXXRecordDecl *RD = Definition->getParent(); |
Richard Smith | 610a60c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 1857 | if (Definition->isDefaulted() && |
| 1858 | ((Definition->isCopyConstructor() && RD->hasTrivialCopyConstructor()) || |
| 1859 | (Definition->isMoveConstructor() && RD->hasTrivialMoveConstructor()))) { |
| 1860 | LValue RHS; |
| 1861 | RHS.setFrom(ArgValues[0]); |
| 1862 | CCValue Value; |
| 1863 | return HandleLValueToRValueConversion(Info, Args[0], Args[0]->getType(), |
| 1864 | RHS, Value) && |
| 1865 | CheckConstantExpression(Info, CallExpr, Value, Result); |
| 1866 | } |
| 1867 | |
| 1868 | // Reserve space for the struct members. |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1869 | if (!RD->isUnion() && Result.isUninit()) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1870 | Result = APValue(APValue::UninitStruct(), RD->getNumBases(), |
| 1871 | std::distance(RD->field_begin(), RD->field_end())); |
| 1872 | |
| 1873 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 1874 | |
| 1875 | unsigned BasesSeen = 0; |
| 1876 | #ifndef NDEBUG |
| 1877 | CXXRecordDecl::base_class_const_iterator BaseIt = RD->bases_begin(); |
| 1878 | #endif |
| 1879 | for (CXXConstructorDecl::init_const_iterator I = Definition->init_begin(), |
| 1880 | E = Definition->init_end(); I != E; ++I) { |
| 1881 | if ((*I)->isBaseInitializer()) { |
| 1882 | QualType BaseType((*I)->getBaseClass(), 0); |
| 1883 | #ifndef NDEBUG |
| 1884 | // Non-virtual base classes are initialized in the order in the class |
| 1885 | // definition. We cannot have a virtual base class for a literal type. |
| 1886 | assert(!BaseIt->isVirtual() && "virtual base for literal type"); |
| 1887 | assert(Info.Ctx.hasSameType(BaseIt->getType(), BaseType) && |
| 1888 | "base class initializers not in expected order"); |
| 1889 | ++BaseIt; |
| 1890 | #endif |
| 1891 | LValue Subobject = This; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1892 | HandleLValueDirectBase(Info, (*I)->getInit(), Subobject, RD, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1893 | BaseType->getAsCXXRecordDecl(), &Layout); |
| 1894 | if (!EvaluateConstantExpression(Result.getStructBase(BasesSeen++), Info, |
| 1895 | Subobject, (*I)->getInit())) |
| 1896 | return false; |
| 1897 | } else if (FieldDecl *FD = (*I)->getMember()) { |
| 1898 | LValue Subobject = This; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1899 | HandleLValueMember(Info, (*I)->getInit(), Subobject, FD, &Layout); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1900 | if (RD->isUnion()) { |
| 1901 | Result = APValue(FD); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1902 | if (!EvaluateConstantExpression(Result.getUnionValue(), Info, Subobject, |
| 1903 | (*I)->getInit(), CCEK_MemberInit)) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1904 | return false; |
| 1905 | } else if (!EvaluateConstantExpression( |
| 1906 | Result.getStructField(FD->getFieldIndex()), |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1907 | Info, Subobject, (*I)->getInit(), CCEK_MemberInit)) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1908 | return false; |
| 1909 | } else { |
| 1910 | // FIXME: handle indirect field initializers |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 1911 | Info.Diag((*I)->getInit()->getExprLoc(), |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1912 | diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1913 | return false; |
| 1914 | } |
| 1915 | } |
| 1916 | |
| 1917 | return true; |
| 1918 | } |
| 1919 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1920 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 1921 | class HasSideEffect |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1922 | : public ConstStmtVisitor<HasSideEffect, bool> { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 1923 | const ASTContext &Ctx; |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1924 | public: |
| 1925 | |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 1926 | HasSideEffect(const ASTContext &C) : Ctx(C) {} |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1927 | |
| 1928 | // Unhandled nodes conservatively default to having side effects. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1929 | bool VisitStmt(const Stmt *S) { |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1930 | return true; |
| 1931 | } |
| 1932 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1933 | bool VisitParenExpr(const ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 1934 | bool VisitGenericSelectionExpr(const GenericSelectionExpr *E) { |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1935 | return Visit(E->getResultExpr()); |
| 1936 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1937 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 1938 | if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1939 | return true; |
| 1940 | return false; |
| 1941 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1942 | bool VisitObjCIvarRefExpr(const ObjCIvarRefExpr *E) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 1943 | if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1944 | return true; |
| 1945 | return false; |
| 1946 | } |
| 1947 | bool VisitBlockDeclRefExpr (const BlockDeclRefExpr *E) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 1948 | if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1949 | return true; |
| 1950 | return false; |
| 1951 | } |
| 1952 | |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1953 | // We don't want to evaluate BlockExprs multiple times, as they generate |
| 1954 | // a ton of code. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1955 | bool VisitBlockExpr(const BlockExpr *E) { return true; } |
| 1956 | bool VisitPredefinedExpr(const PredefinedExpr *E) { return false; } |
| 1957 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1958 | { return Visit(E->getInitializer()); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1959 | bool VisitMemberExpr(const MemberExpr *E) { return Visit(E->getBase()); } |
| 1960 | bool VisitIntegerLiteral(const IntegerLiteral *E) { return false; } |
| 1961 | bool VisitFloatingLiteral(const FloatingLiteral *E) { return false; } |
| 1962 | bool VisitStringLiteral(const StringLiteral *E) { return false; } |
| 1963 | bool VisitCharacterLiteral(const CharacterLiteral *E) { return false; } |
| 1964 | bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1965 | { return false; } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1966 | bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E) |
Mike Stump | 980ca22 | 2009-10-29 20:48:09 +0000 | [diff] [blame] | 1967 | { return Visit(E->getLHS()) || Visit(E->getRHS()); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1968 | bool VisitChooseExpr(const ChooseExpr *E) |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 1969 | { return Visit(E->getChosenSubExpr(Ctx)); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1970 | bool VisitCastExpr(const CastExpr *E) { return Visit(E->getSubExpr()); } |
| 1971 | bool VisitBinAssign(const BinaryOperator *E) { return true; } |
| 1972 | bool VisitCompoundAssignOperator(const BinaryOperator *E) { return true; } |
| 1973 | bool VisitBinaryOperator(const BinaryOperator *E) |
Mike Stump | 980ca22 | 2009-10-29 20:48:09 +0000 | [diff] [blame] | 1974 | { return Visit(E->getLHS()) || Visit(E->getRHS()); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1975 | bool VisitUnaryPreInc(const UnaryOperator *E) { return true; } |
| 1976 | bool VisitUnaryPostInc(const UnaryOperator *E) { return true; } |
| 1977 | bool VisitUnaryPreDec(const UnaryOperator *E) { return true; } |
| 1978 | bool VisitUnaryPostDec(const UnaryOperator *E) { return true; } |
| 1979 | bool VisitUnaryDeref(const UnaryOperator *E) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 1980 | if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1981 | return true; |
Mike Stump | 980ca22 | 2009-10-29 20:48:09 +0000 | [diff] [blame] | 1982 | return Visit(E->getSubExpr()); |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1983 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1984 | bool VisitUnaryOperator(const UnaryOperator *E) { return Visit(E->getSubExpr()); } |
Chris Lattner | 363ff23 | 2010-04-13 17:34:23 +0000 | [diff] [blame] | 1985 | |
| 1986 | // Has side effects if any element does. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1987 | bool VisitInitListExpr(const InitListExpr *E) { |
Chris Lattner | 363ff23 | 2010-04-13 17:34:23 +0000 | [diff] [blame] | 1988 | for (unsigned i = 0, e = E->getNumInits(); i != e; ++i) |
| 1989 | if (Visit(E->getInit(i))) return true; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1990 | if (const Expr *filler = E->getArrayFiller()) |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 1991 | return Visit(filler); |
Chris Lattner | 363ff23 | 2010-04-13 17:34:23 +0000 | [diff] [blame] | 1992 | return false; |
| 1993 | } |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 1994 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1995 | bool VisitSizeOfPackExpr(const SizeOfPackExpr *) { return false; } |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1996 | }; |
| 1997 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1998 | class OpaqueValueEvaluation { |
| 1999 | EvalInfo &info; |
| 2000 | OpaqueValueExpr *opaqueValue; |
| 2001 | |
| 2002 | public: |
| 2003 | OpaqueValueEvaluation(EvalInfo &info, OpaqueValueExpr *opaqueValue, |
| 2004 | Expr *value) |
| 2005 | : info(info), opaqueValue(opaqueValue) { |
| 2006 | |
| 2007 | // If evaluation fails, fail immediately. |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 2008 | if (!Evaluate(info.OpaqueValues[opaqueValue], info, value)) { |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2009 | this->opaqueValue = 0; |
| 2010 | return; |
| 2011 | } |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2012 | } |
| 2013 | |
| 2014 | bool hasError() const { return opaqueValue == 0; } |
| 2015 | |
| 2016 | ~OpaqueValueEvaluation() { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 2017 | // FIXME: This will not work for recursive constexpr functions using opaque |
| 2018 | // values. Restore the former value. |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2019 | if (opaqueValue) info.OpaqueValues.erase(opaqueValue); |
| 2020 | } |
| 2021 | }; |
| 2022 | |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 2023 | } // end anonymous namespace |
| 2024 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2025 | //===----------------------------------------------------------------------===// |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2026 | // Generic Evaluation |
| 2027 | //===----------------------------------------------------------------------===// |
| 2028 | namespace { |
| 2029 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2030 | // FIXME: RetTy is always bool. Remove it. |
| 2031 | template <class Derived, typename RetTy=bool> |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2032 | class ExprEvaluatorBase |
| 2033 | : public ConstStmtVisitor<Derived, RetTy> { |
| 2034 | private: |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2035 | RetTy DerivedSuccess(const CCValue &V, const Expr *E) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2036 | return static_cast<Derived*>(this)->Success(V, E); |
| 2037 | } |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2038 | RetTy DerivedZeroInitialization(const Expr *E) { |
| 2039 | return static_cast<Derived*>(this)->ZeroInitialization(E); |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2040 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2041 | |
| 2042 | protected: |
| 2043 | EvalInfo &Info; |
| 2044 | typedef ConstStmtVisitor<Derived, RetTy> StmtVisitorTy; |
| 2045 | typedef ExprEvaluatorBase ExprEvaluatorBaseTy; |
| 2046 | |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 2047 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
Richard Smith | d509342 | 2011-12-12 09:41:58 +0000 | [diff] [blame] | 2048 | return Info.CCEDiag(E->getExprLoc(), D); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2049 | } |
| 2050 | |
| 2051 | /// Report an evaluation error. This should only be called when an error is |
| 2052 | /// first discovered. When propagating an error, just return false. |
| 2053 | bool Error(const Expr *E, diag::kind D) { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 2054 | Info.Diag(E->getExprLoc(), D); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2055 | return false; |
| 2056 | } |
| 2057 | bool Error(const Expr *E) { |
| 2058 | return Error(E, diag::note_invalid_subexpr_in_const_expr); |
| 2059 | } |
| 2060 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2061 | RetTy ZeroInitialization(const Expr *E) { return Error(E); } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2062 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2063 | public: |
| 2064 | ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {} |
| 2065 | |
| 2066 | RetTy VisitStmt(const Stmt *) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2067 | llvm_unreachable("Expression evaluator should not be called on stmts"); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2068 | } |
| 2069 | RetTy VisitExpr(const Expr *E) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2070 | return Error(E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2071 | } |
| 2072 | |
| 2073 | RetTy VisitParenExpr(const ParenExpr *E) |
| 2074 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| 2075 | RetTy VisitUnaryExtension(const UnaryOperator *E) |
| 2076 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| 2077 | RetTy VisitUnaryPlus(const UnaryOperator *E) |
| 2078 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| 2079 | RetTy VisitChooseExpr(const ChooseExpr *E) |
| 2080 | { return StmtVisitorTy::Visit(E->getChosenSubExpr(Info.Ctx)); } |
| 2081 | RetTy VisitGenericSelectionExpr(const GenericSelectionExpr *E) |
| 2082 | { return StmtVisitorTy::Visit(E->getResultExpr()); } |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 2083 | RetTy VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E) |
| 2084 | { return StmtVisitorTy::Visit(E->getReplacement()); } |
Richard Smith | 3d75ca8 | 2011-11-09 02:12:41 +0000 | [diff] [blame] | 2085 | RetTy VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) |
| 2086 | { return StmtVisitorTy::Visit(E->getExpr()); } |
Richard Smith | bc6abe9 | 2011-12-19 22:12:41 +0000 | [diff] [blame] | 2087 | // We cannot create any objects for which cleanups are required, so there is |
| 2088 | // nothing to do here; all cleanups must come from unevaluated subexpressions. |
| 2089 | RetTy VisitExprWithCleanups(const ExprWithCleanups *E) |
| 2090 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2091 | |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 2092 | RetTy VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E) { |
| 2093 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 0; |
| 2094 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
| 2095 | } |
| 2096 | RetTy VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) { |
| 2097 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 1; |
| 2098 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
| 2099 | } |
| 2100 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2101 | RetTy VisitBinaryOperator(const BinaryOperator *E) { |
| 2102 | switch (E->getOpcode()) { |
| 2103 | default: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2104 | return Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2105 | |
| 2106 | case BO_Comma: |
| 2107 | VisitIgnoredValue(E->getLHS()); |
| 2108 | return StmtVisitorTy::Visit(E->getRHS()); |
| 2109 | |
| 2110 | case BO_PtrMemD: |
| 2111 | case BO_PtrMemI: { |
| 2112 | LValue Obj; |
| 2113 | if (!HandleMemberPointerAccess(Info, E, Obj)) |
| 2114 | return false; |
| 2115 | CCValue Result; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2116 | if (!HandleLValueToRValueConversion(Info, E, E->getType(), Obj, Result)) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2117 | return false; |
| 2118 | return DerivedSuccess(Result, E); |
| 2119 | } |
| 2120 | } |
| 2121 | } |
| 2122 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2123 | RetTy VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) { |
| 2124 | OpaqueValueEvaluation opaque(Info, E->getOpaqueValue(), E->getCommon()); |
| 2125 | if (opaque.hasError()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2126 | return false; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2127 | |
| 2128 | bool cond; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2129 | if (!EvaluateAsBooleanCondition(E->getCond(), cond, Info)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2130 | return false; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2131 | |
| 2132 | return StmtVisitorTy::Visit(cond ? E->getTrueExpr() : E->getFalseExpr()); |
| 2133 | } |
| 2134 | |
| 2135 | RetTy VisitConditionalOperator(const ConditionalOperator *E) { |
| 2136 | bool BoolResult; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2137 | if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2138 | return false; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2139 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2140 | Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2141 | return StmtVisitorTy::Visit(EvalExpr); |
| 2142 | } |
| 2143 | |
| 2144 | RetTy VisitOpaqueValueExpr(const OpaqueValueExpr *E) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2145 | const CCValue *Value = Info.getOpaqueValue(E); |
Argyrios Kyrtzidis | 4278683 | 2011-12-09 02:44:48 +0000 | [diff] [blame] | 2146 | if (!Value) { |
| 2147 | const Expr *Source = E->getSourceExpr(); |
| 2148 | if (!Source) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2149 | return Error(E); |
Argyrios Kyrtzidis | 4278683 | 2011-12-09 02:44:48 +0000 | [diff] [blame] | 2150 | if (Source == E) { // sanity checking. |
| 2151 | assert(0 && "OpaqueValueExpr recursively refers to itself"); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2152 | return Error(E); |
Argyrios Kyrtzidis | 4278683 | 2011-12-09 02:44:48 +0000 | [diff] [blame] | 2153 | } |
| 2154 | return StmtVisitorTy::Visit(Source); |
| 2155 | } |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2156 | return DerivedSuccess(*Value, E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2157 | } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2158 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2159 | RetTy VisitCallExpr(const CallExpr *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2160 | const Expr *Callee = E->getCallee()->IgnoreParens(); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2161 | QualType CalleeType = Callee->getType(); |
| 2162 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2163 | const FunctionDecl *FD = 0; |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2164 | LValue *This = 0, ThisVal; |
| 2165 | llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs()); |
Richard Smith | 6c95787 | 2011-11-10 09:31:24 +0000 | [diff] [blame] | 2166 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2167 | // Extract function decl and 'this' pointer from the callee. |
| 2168 | if (CalleeType->isSpecificBuiltinType(BuiltinType::BoundMember)) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2169 | const ValueDecl *Member = 0; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2170 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(Callee)) { |
| 2171 | // Explicit bound member calls, such as x.f() or p->g(); |
| 2172 | if (!EvaluateObjectArgument(Info, ME->getBase(), ThisVal)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2173 | return false; |
| 2174 | Member = ME->getMemberDecl(); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2175 | This = &ThisVal; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2176 | } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(Callee)) { |
| 2177 | // Indirect bound member calls ('.*' or '->*'). |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2178 | Member = HandleMemberPointerAccess(Info, BE, ThisVal, false); |
| 2179 | if (!Member) return false; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2180 | This = &ThisVal; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2181 | } else |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2182 | return Error(Callee); |
| 2183 | |
| 2184 | FD = dyn_cast<FunctionDecl>(Member); |
| 2185 | if (!FD) |
| 2186 | return Error(Callee); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2187 | } else if (CalleeType->isFunctionPointerType()) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2188 | LValue Call; |
| 2189 | if (!EvaluatePointer(Callee, Call, Info)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2190 | return false; |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2191 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2192 | if (!Call.getLValueOffset().isZero()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2193 | return Error(Callee); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2194 | FD = dyn_cast_or_null<FunctionDecl>( |
| 2195 | Call.getLValueBase().dyn_cast<const ValueDecl*>()); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2196 | if (!FD) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2197 | return Error(Callee); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2198 | |
| 2199 | // Overloaded operator calls to member functions are represented as normal |
| 2200 | // calls with '*this' as the first argument. |
| 2201 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
| 2202 | if (MD && !MD->isStatic()) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2203 | // FIXME: When selecting an implicit conversion for an overloaded |
| 2204 | // operator delete, we sometimes try to evaluate calls to conversion |
| 2205 | // operators without a 'this' parameter! |
| 2206 | if (Args.empty()) |
| 2207 | return Error(E); |
| 2208 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2209 | if (!EvaluateObjectArgument(Info, Args[0], ThisVal)) |
| 2210 | return false; |
| 2211 | This = &ThisVal; |
| 2212 | Args = Args.slice(1); |
| 2213 | } |
| 2214 | |
| 2215 | // Don't call function pointers which have been cast to some other type. |
| 2216 | if (!Info.Ctx.hasSameType(CalleeType->getPointeeType(), FD->getType())) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2217 | return Error(E); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2218 | } else |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2219 | return Error(E); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2220 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2221 | const FunctionDecl *Definition = 0; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2222 | Stmt *Body = FD->getBody(Definition); |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 2223 | APValue Result; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2224 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2225 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition) || |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 2226 | !HandleFunctionCall(E, Definition, This, Args, Body, Info, Result)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2227 | return false; |
| 2228 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2229 | return DerivedSuccess(CCValue(Info.Ctx, Result, CCValue::GlobalValue()), E); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2230 | } |
| 2231 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2232 | RetTy VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
| 2233 | return StmtVisitorTy::Visit(E->getInitializer()); |
| 2234 | } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2235 | RetTy VisitInitListExpr(const InitListExpr *E) { |
Eli Friedman | 71523d6 | 2012-01-03 23:54:05 +0000 | [diff] [blame] | 2236 | if (E->getNumInits() == 0) |
| 2237 | return DerivedZeroInitialization(E); |
| 2238 | if (E->getNumInits() == 1) |
| 2239 | return StmtVisitorTy::Visit(E->getInit(0)); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2240 | return Error(E); |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2241 | } |
| 2242 | RetTy VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2243 | return DerivedZeroInitialization(E); |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2244 | } |
| 2245 | RetTy VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2246 | return DerivedZeroInitialization(E); |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2247 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2248 | RetTy VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2249 | return DerivedZeroInitialization(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2250 | } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2251 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2252 | /// A member expression where the object is a prvalue is itself a prvalue. |
| 2253 | RetTy VisitMemberExpr(const MemberExpr *E) { |
| 2254 | assert(!E->isArrow() && "missing call to bound member function?"); |
| 2255 | |
| 2256 | CCValue Val; |
| 2257 | if (!Evaluate(Val, Info, E->getBase())) |
| 2258 | return false; |
| 2259 | |
| 2260 | QualType BaseTy = E->getBase()->getType(); |
| 2261 | |
| 2262 | const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2263 | if (!FD) return Error(E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2264 | assert(!FD->getType()->isReferenceType() && "prvalue reference?"); |
| 2265 | assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() == |
| 2266 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
| 2267 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2268 | SubobjectDesignator Designator(BaseTy); |
| 2269 | Designator.addDeclUnchecked(FD); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2270 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2271 | return ExtractSubobject(Info, E, Val, BaseTy, Designator, E->getType()) && |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2272 | DerivedSuccess(Val, E); |
| 2273 | } |
| 2274 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2275 | RetTy VisitCastExpr(const CastExpr *E) { |
| 2276 | switch (E->getCastKind()) { |
| 2277 | default: |
| 2278 | break; |
| 2279 | |
| 2280 | case CK_NoOp: |
| 2281 | return StmtVisitorTy::Visit(E->getSubExpr()); |
| 2282 | |
| 2283 | case CK_LValueToRValue: { |
| 2284 | LValue LVal; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2285 | if (!EvaluateLValue(E->getSubExpr(), LVal, Info)) |
| 2286 | return false; |
| 2287 | CCValue RVal; |
| 2288 | if (!HandleLValueToRValueConversion(Info, E, E->getType(), LVal, RVal)) |
| 2289 | return false; |
| 2290 | return DerivedSuccess(RVal, E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2291 | } |
| 2292 | } |
| 2293 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2294 | return Error(E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2295 | } |
| 2296 | |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 2297 | /// Visit a value which is evaluated, but whose value is ignored. |
| 2298 | void VisitIgnoredValue(const Expr *E) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2299 | CCValue Scratch; |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 2300 | if (!Evaluate(Scratch, Info, E)) |
| 2301 | Info.EvalStatus.HasSideEffects = true; |
| 2302 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2303 | }; |
| 2304 | |
| 2305 | } |
| 2306 | |
| 2307 | //===----------------------------------------------------------------------===// |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2308 | // Common base class for lvalue and temporary evaluation. |
| 2309 | //===----------------------------------------------------------------------===// |
| 2310 | namespace { |
| 2311 | template<class Derived> |
| 2312 | class LValueExprEvaluatorBase |
| 2313 | : public ExprEvaluatorBase<Derived, bool> { |
| 2314 | protected: |
| 2315 | LValue &Result; |
| 2316 | typedef LValueExprEvaluatorBase LValueExprEvaluatorBaseTy; |
| 2317 | typedef ExprEvaluatorBase<Derived, bool> ExprEvaluatorBaseTy; |
| 2318 | |
| 2319 | bool Success(APValue::LValueBase B) { |
| 2320 | Result.set(B); |
| 2321 | return true; |
| 2322 | } |
| 2323 | |
| 2324 | public: |
| 2325 | LValueExprEvaluatorBase(EvalInfo &Info, LValue &Result) : |
| 2326 | ExprEvaluatorBaseTy(Info), Result(Result) {} |
| 2327 | |
| 2328 | bool Success(const CCValue &V, const Expr *E) { |
| 2329 | Result.setFrom(V); |
| 2330 | return true; |
| 2331 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2332 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2333 | bool VisitMemberExpr(const MemberExpr *E) { |
| 2334 | // Handle non-static data members. |
| 2335 | QualType BaseTy; |
| 2336 | if (E->isArrow()) { |
| 2337 | if (!EvaluatePointer(E->getBase(), Result, this->Info)) |
| 2338 | return false; |
| 2339 | BaseTy = E->getBase()->getType()->getAs<PointerType>()->getPointeeType(); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2340 | } else if (E->getBase()->isRValue()) { |
Richard Smith | af2c7a1 | 2011-12-19 22:01:37 +0000 | [diff] [blame] | 2341 | assert(E->getBase()->getType()->isRecordType()); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2342 | if (!EvaluateTemporary(E->getBase(), Result, this->Info)) |
| 2343 | return false; |
| 2344 | BaseTy = E->getBase()->getType(); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2345 | } else { |
| 2346 | if (!this->Visit(E->getBase())) |
| 2347 | return false; |
| 2348 | BaseTy = E->getBase()->getType(); |
| 2349 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2350 | |
| 2351 | const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
| 2352 | // FIXME: Handle IndirectFieldDecls |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2353 | if (!FD) return this->Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2354 | assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() == |
| 2355 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
| 2356 | (void)BaseTy; |
| 2357 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2358 | HandleLValueMember(this->Info, E, Result, FD); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2359 | |
| 2360 | if (FD->getType()->isReferenceType()) { |
| 2361 | CCValue RefValue; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2362 | if (!HandleLValueToRValueConversion(this->Info, E, FD->getType(), Result, |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2363 | RefValue)) |
| 2364 | return false; |
| 2365 | return Success(RefValue, E); |
| 2366 | } |
| 2367 | return true; |
| 2368 | } |
| 2369 | |
| 2370 | bool VisitBinaryOperator(const BinaryOperator *E) { |
| 2371 | switch (E->getOpcode()) { |
| 2372 | default: |
| 2373 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| 2374 | |
| 2375 | case BO_PtrMemD: |
| 2376 | case BO_PtrMemI: |
| 2377 | return HandleMemberPointerAccess(this->Info, E, Result); |
| 2378 | } |
| 2379 | } |
| 2380 | |
| 2381 | bool VisitCastExpr(const CastExpr *E) { |
| 2382 | switch (E->getCastKind()) { |
| 2383 | default: |
| 2384 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 2385 | |
| 2386 | case CK_DerivedToBase: |
| 2387 | case CK_UncheckedDerivedToBase: { |
| 2388 | if (!this->Visit(E->getSubExpr())) |
| 2389 | return false; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2390 | |
| 2391 | // Now figure out the necessary offset to add to the base LV to get from |
| 2392 | // the derived class to the base class. |
| 2393 | QualType Type = E->getSubExpr()->getType(); |
| 2394 | |
| 2395 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 2396 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2397 | if (!HandleLValueBase(this->Info, E, Result, Type->getAsCXXRecordDecl(), |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2398 | *PathI)) |
| 2399 | return false; |
| 2400 | Type = (*PathI)->getType(); |
| 2401 | } |
| 2402 | |
| 2403 | return true; |
| 2404 | } |
| 2405 | } |
| 2406 | } |
| 2407 | }; |
| 2408 | } |
| 2409 | |
| 2410 | //===----------------------------------------------------------------------===// |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2411 | // LValue Evaluation |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2412 | // |
| 2413 | // This is used for evaluating lvalues (in C and C++), xvalues (in C++11), |
| 2414 | // function designators (in C), decl references to void objects (in C), and |
| 2415 | // temporaries (if building with -Wno-address-of-temporary). |
| 2416 | // |
| 2417 | // LValue evaluation produces values comprising a base expression of one of the |
| 2418 | // following types: |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2419 | // - Declarations |
| 2420 | // * VarDecl |
| 2421 | // * FunctionDecl |
| 2422 | // - Literals |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2423 | // * CompoundLiteralExpr in C |
| 2424 | // * StringLiteral |
Richard Smith | 47d2145 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 2425 | // * CXXTypeidExpr |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2426 | // * PredefinedExpr |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2427 | // * ObjCStringLiteralExpr |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2428 | // * ObjCEncodeExpr |
| 2429 | // * AddrLabelExpr |
| 2430 | // * BlockExpr |
| 2431 | // * CallExpr for a MakeStringConstant builtin |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2432 | // - Locals and temporaries |
| 2433 | // * Any Expr, with a Frame indicating the function in which the temporary was |
| 2434 | // evaluated. |
| 2435 | // plus an offset in bytes. |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2436 | //===----------------------------------------------------------------------===// |
| 2437 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 2438 | class LValueExprEvaluator |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2439 | : public LValueExprEvaluatorBase<LValueExprEvaluator> { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2440 | public: |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2441 | LValueExprEvaluator(EvalInfo &Info, LValue &Result) : |
| 2442 | LValueExprEvaluatorBaseTy(Info, Result) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2443 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2444 | bool VisitVarDecl(const Expr *E, const VarDecl *VD); |
| 2445 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2446 | bool VisitDeclRefExpr(const DeclRefExpr *E); |
| 2447 | bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(E); } |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 2448 | bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2449 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
| 2450 | bool VisitMemberExpr(const MemberExpr *E); |
| 2451 | bool VisitStringLiteral(const StringLiteral *E) { return Success(E); } |
| 2452 | bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(E); } |
Richard Smith | 47d2145 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 2453 | bool VisitCXXTypeidExpr(const CXXTypeidExpr *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2454 | bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E); |
| 2455 | bool VisitUnaryDeref(const UnaryOperator *E); |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 2456 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2457 | bool VisitCastExpr(const CastExpr *E) { |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 2458 | switch (E->getCastKind()) { |
| 2459 | default: |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2460 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 2461 | |
Eli Friedman | db92422 | 2011-10-11 00:13:24 +0000 | [diff] [blame] | 2462 | case CK_LValueBitCast: |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 2463 | this->CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 2464 | if (!Visit(E->getSubExpr())) |
| 2465 | return false; |
| 2466 | Result.Designator.setInvalid(); |
| 2467 | return true; |
Eli Friedman | db92422 | 2011-10-11 00:13:24 +0000 | [diff] [blame] | 2468 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2469 | case CK_BaseToDerived: |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2470 | if (!Visit(E->getSubExpr())) |
| 2471 | return false; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2472 | return HandleBaseToDerivedCast(Info, E, Result); |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 2473 | } |
| 2474 | } |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 2475 | |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 2476 | // FIXME: Missing: __real__, __imag__ |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2477 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2478 | }; |
| 2479 | } // end anonymous namespace |
| 2480 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2481 | /// Evaluate an expression as an lvalue. This can be legitimately called on |
| 2482 | /// expressions which are not glvalues, in a few cases: |
| 2483 | /// * function designators in C, |
| 2484 | /// * "extern void" objects, |
| 2485 | /// * temporaries, if building with -Wno-address-of-temporary. |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2486 | static bool EvaluateLValue(const Expr* E, LValue& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2487 | assert((E->isGLValue() || E->getType()->isFunctionType() || |
| 2488 | E->getType()->isVoidType() || isa<CXXTemporaryObjectExpr>(E)) && |
| 2489 | "can't evaluate expression as an lvalue"); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2490 | return LValueExprEvaluator(Info, Result).Visit(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2491 | } |
| 2492 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2493 | bool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2494 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl())) |
| 2495 | return Success(FD); |
| 2496 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2497 | return VisitVarDecl(E, VD); |
| 2498 | return Error(E); |
| 2499 | } |
Richard Smith | 436c889 | 2011-10-24 23:14:33 +0000 | [diff] [blame] | 2500 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2501 | bool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) { |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 2502 | if (!VD->getType()->isReferenceType()) { |
| 2503 | if (isa<ParmVarDecl>(VD)) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2504 | Result.set(VD, Info.CurrentCall); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 2505 | return true; |
| 2506 | } |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2507 | return Success(VD); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 2508 | } |
Eli Friedman | 50c39ea | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 2509 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2510 | CCValue V; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2511 | if (!EvaluateVarDeclInit(Info, E, VD, Info.CurrentCall, V)) |
| 2512 | return false; |
| 2513 | return Success(V, E); |
Anders Carlsson | 35873c4 | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 2514 | } |
| 2515 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 2516 | bool LValueExprEvaluator::VisitMaterializeTemporaryExpr( |
| 2517 | const MaterializeTemporaryExpr *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2518 | if (E->GetTemporaryExpr()->isRValue()) { |
Richard Smith | af2c7a1 | 2011-12-19 22:01:37 +0000 | [diff] [blame] | 2519 | if (E->getType()->isRecordType()) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2520 | return EvaluateTemporary(E->GetTemporaryExpr(), Result, Info); |
| 2521 | |
| 2522 | Result.set(E, Info.CurrentCall); |
| 2523 | return EvaluateConstantExpression(Info.CurrentCall->Temporaries[E], Info, |
| 2524 | Result, E->GetTemporaryExpr()); |
| 2525 | } |
| 2526 | |
| 2527 | // Materialization of an lvalue temporary occurs when we need to force a copy |
| 2528 | // (for instance, if it's a bitfield). |
| 2529 | // FIXME: The AST should contain an lvalue-to-rvalue node for such cases. |
| 2530 | if (!Visit(E->GetTemporaryExpr())) |
| 2531 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2532 | if (!HandleLValueToRValueConversion(Info, E, E->getType(), Result, |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2533 | Info.CurrentCall->Temporaries[E])) |
| 2534 | return false; |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2535 | Result.set(E, Info.CurrentCall); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2536 | return true; |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 2537 | } |
| 2538 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2539 | bool |
| 2540 | LValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2541 | assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?"); |
| 2542 | // Defer visiting the literal until the lvalue-to-rvalue conversion. We can |
| 2543 | // 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] | 2544 | return Success(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2545 | } |
| 2546 | |
Richard Smith | 47d2145 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 2547 | bool LValueExprEvaluator::VisitCXXTypeidExpr(const CXXTypeidExpr *E) { |
| 2548 | if (E->isTypeOperand()) |
| 2549 | return Success(E); |
| 2550 | CXXRecordDecl *RD = E->getExprOperand()->getType()->getAsCXXRecordDecl(); |
| 2551 | if (RD && RD->isPolymorphic()) { |
| 2552 | Info.Diag(E->getExprLoc(), diag::note_constexpr_typeid_polymorphic) |
| 2553 | << E->getExprOperand()->getType() |
| 2554 | << E->getExprOperand()->getSourceRange(); |
| 2555 | return false; |
| 2556 | } |
| 2557 | return Success(E); |
| 2558 | } |
| 2559 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2560 | bool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2561 | // Handle static data members. |
| 2562 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getMemberDecl())) { |
| 2563 | VisitIgnoredValue(E->getBase()); |
| 2564 | return VisitVarDecl(E, VD); |
| 2565 | } |
| 2566 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2567 | // Handle static member functions. |
| 2568 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) { |
| 2569 | if (MD->isStatic()) { |
| 2570 | VisitIgnoredValue(E->getBase()); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2571 | return Success(MD); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2572 | } |
| 2573 | } |
| 2574 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2575 | // Handle non-static data members. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2576 | return LValueExprEvaluatorBaseTy::VisitMemberExpr(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2577 | } |
| 2578 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2579 | bool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2580 | // FIXME: Deal with vectors as array subscript bases. |
| 2581 | if (E->getBase()->getType()->isVectorType()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2582 | return Error(E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2583 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 2584 | if (!EvaluatePointer(E->getBase(), Result, Info)) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2585 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2586 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 2587 | APSInt Index; |
| 2588 | if (!EvaluateInteger(E->getIdx(), Index, Info)) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2589 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2590 | int64_t IndexValue |
| 2591 | = Index.isSigned() ? Index.getSExtValue() |
| 2592 | : static_cast<int64_t>(Index.getZExtValue()); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 2593 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2594 | return HandleLValueArrayAdjustment(Info, E, Result, E->getType(), IndexValue); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 2595 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2596 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2597 | bool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2598 | return EvaluatePointer(E->getSubExpr(), Result, Info); |
Eli Friedman | e8761c8 | 2009-02-20 01:57:15 +0000 | [diff] [blame] | 2599 | } |
| 2600 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2601 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2602 | // Pointer Evaluation |
| 2603 | //===----------------------------------------------------------------------===// |
| 2604 | |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 2605 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 2606 | class PointerExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2607 | : public ExprEvaluatorBase<PointerExprEvaluator, bool> { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2608 | LValue &Result; |
| 2609 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2610 | bool Success(const Expr *E) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2611 | Result.set(E); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2612 | return true; |
| 2613 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 2614 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2615 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2616 | PointerExprEvaluator(EvalInfo &info, LValue &Result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2617 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2618 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2619 | bool Success(const CCValue &V, const Expr *E) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2620 | Result.setFrom(V); |
| 2621 | return true; |
| 2622 | } |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2623 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2624 | return Success((Expr*)0); |
| 2625 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 2626 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2627 | bool VisitBinaryOperator(const BinaryOperator *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2628 | bool VisitCastExpr(const CastExpr* E); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2629 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2630 | bool VisitObjCStringLiteral(const ObjCStringLiteral *E) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2631 | { return Success(E); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2632 | bool VisitAddrLabelExpr(const AddrLabelExpr *E) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2633 | { return Success(E); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2634 | bool VisitCallExpr(const CallExpr *E); |
| 2635 | bool VisitBlockExpr(const BlockExpr *E) { |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2636 | if (!E->getBlockDecl()->hasCaptures()) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2637 | return Success(E); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2638 | return Error(E); |
Mike Stump | b83d287 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 2639 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2640 | bool VisitCXXThisExpr(const CXXThisExpr *E) { |
| 2641 | if (!Info.CurrentCall->This) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2642 | return Error(E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2643 | Result = *Info.CurrentCall->This; |
| 2644 | return true; |
| 2645 | } |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2646 | |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 2647 | // FIXME: Missing: @protocol, @selector |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 2648 | }; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2649 | } // end anonymous namespace |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 2650 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2651 | static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2652 | assert(E->isRValue() && E->getType()->hasPointerRepresentation()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2653 | return PointerExprEvaluator(Info, Result).Visit(E); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2654 | } |
| 2655 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2656 | bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2657 | if (E->getOpcode() != BO_Add && |
| 2658 | E->getOpcode() != BO_Sub) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2659 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2660 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2661 | const Expr *PExp = E->getLHS(); |
| 2662 | const Expr *IExp = E->getRHS(); |
| 2663 | if (IExp->getType()->isPointerType()) |
| 2664 | std::swap(PExp, IExp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2665 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2666 | if (!EvaluatePointer(PExp, Result, Info)) |
| 2667 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2668 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2669 | llvm::APSInt Offset; |
| 2670 | if (!EvaluateInteger(IExp, Offset, Info)) |
| 2671 | return false; |
| 2672 | int64_t AdditionalOffset |
| 2673 | = Offset.isSigned() ? Offset.getSExtValue() |
| 2674 | : static_cast<int64_t>(Offset.getZExtValue()); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 2675 | if (E->getOpcode() == BO_Sub) |
| 2676 | AdditionalOffset = -AdditionalOffset; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2677 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2678 | QualType Pointee = PExp->getType()->getAs<PointerType>()->getPointeeType(); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2679 | return HandleLValueArrayAdjustment(Info, E, Result, Pointee, |
| 2680 | AdditionalOffset); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2681 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2682 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2683 | bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| 2684 | return EvaluateLValue(E->getSubExpr(), Result, Info); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2685 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2686 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2687 | bool PointerExprEvaluator::VisitCastExpr(const CastExpr* E) { |
| 2688 | const Expr* SubExpr = E->getSubExpr(); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2689 | |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 2690 | switch (E->getCastKind()) { |
| 2691 | default: |
| 2692 | break; |
| 2693 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2694 | case CK_BitCast: |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2695 | case CK_CPointerToObjCPointerCast: |
| 2696 | case CK_BlockPointerToObjCPointerCast: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2697 | case CK_AnyPointerToBlockPointerCast: |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 2698 | // Bitcasts to cv void* are static_casts, not reinterpret_casts, so are |
| 2699 | // permitted in constant expressions in C++11. Bitcasts from cv void* are |
| 2700 | // also static_casts, but we disallow them as a resolution to DR1312. |
Richard Smith | 4cd9b8f | 2011-12-12 19:10:03 +0000 | [diff] [blame] | 2701 | if (!E->getType()->isVoidPointerType()) { |
| 2702 | if (SubExpr->getType()->isVoidPointerType()) |
| 2703 | CCEDiag(E, diag::note_constexpr_invalid_cast) |
| 2704 | << 3 << SubExpr->getType(); |
| 2705 | else |
| 2706 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 2707 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 2708 | if (!Visit(SubExpr)) |
| 2709 | return false; |
| 2710 | Result.Designator.setInvalid(); |
| 2711 | return true; |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 2712 | |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 2713 | case CK_DerivedToBase: |
| 2714 | case CK_UncheckedDerivedToBase: { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2715 | if (!EvaluatePointer(E->getSubExpr(), Result, Info)) |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 2716 | return false; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2717 | if (!Result.Base && Result.Offset.isZero()) |
| 2718 | return true; |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 2719 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2720 | // 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] | 2721 | // the derived class to the base class. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2722 | QualType Type = |
| 2723 | E->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType(); |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 2724 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2725 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 2726 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2727 | if (!HandleLValueBase(Info, E, Result, Type->getAsCXXRecordDecl(), |
| 2728 | *PathI)) |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 2729 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2730 | Type = (*PathI)->getType(); |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 2731 | } |
| 2732 | |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 2733 | return true; |
| 2734 | } |
| 2735 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2736 | case CK_BaseToDerived: |
| 2737 | if (!Visit(E->getSubExpr())) |
| 2738 | return false; |
| 2739 | if (!Result.Base && Result.Offset.isZero()) |
| 2740 | return true; |
| 2741 | return HandleBaseToDerivedCast(Info, E, Result); |
| 2742 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2743 | case CK_NullToPointer: |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2744 | return ZeroInitialization(E); |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 2745 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2746 | case CK_IntegralToPointer: { |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 2747 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 2748 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2749 | CCValue Value; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2750 | if (!EvaluateIntegerOrLValue(SubExpr, Value, Info)) |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 2751 | break; |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 2752 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2753 | if (Value.isInt()) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2754 | unsigned Size = Info.Ctx.getTypeSize(E->getType()); |
| 2755 | uint64_t N = Value.getInt().extOrTrunc(Size).getZExtValue(); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2756 | Result.Base = (Expr*)0; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2757 | Result.Offset = CharUnits::fromQuantity(N); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 2758 | Result.Frame = 0; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 2759 | Result.Designator.setInvalid(); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2760 | return true; |
| 2761 | } else { |
| 2762 | // Cast is of an lvalue, no need to change value. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2763 | Result.setFrom(Value); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2764 | return true; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2765 | } |
| 2766 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2767 | case CK_ArrayToPointerDecay: |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2768 | if (SubExpr->isGLValue()) { |
| 2769 | if (!EvaluateLValue(SubExpr, Result, Info)) |
| 2770 | return false; |
| 2771 | } else { |
| 2772 | Result.set(SubExpr, Info.CurrentCall); |
| 2773 | if (!EvaluateConstantExpression(Info.CurrentCall->Temporaries[SubExpr], |
| 2774 | Info, Result, SubExpr)) |
| 2775 | return false; |
| 2776 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 2777 | // The result is a pointer to the first element of the array. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2778 | if (const ConstantArrayType *CAT |
| 2779 | = Info.Ctx.getAsConstantArrayType(SubExpr->getType())) |
| 2780 | Result.addArray(Info, E, CAT); |
| 2781 | else |
| 2782 | Result.Designator.setInvalid(); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 2783 | return true; |
Richard Smith | 6a7c94a | 2011-10-31 20:57:44 +0000 | [diff] [blame] | 2784 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2785 | case CK_FunctionToPointerDecay: |
Richard Smith | 6a7c94a | 2011-10-31 20:57:44 +0000 | [diff] [blame] | 2786 | return EvaluateLValue(SubExpr, Result, Info); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2787 | } |
| 2788 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2789 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2790 | } |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2791 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2792 | bool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2793 | if (IsStringLiteralCall(E)) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2794 | return Success(E); |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 2795 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2796 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2797 | } |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2798 | |
| 2799 | //===----------------------------------------------------------------------===// |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2800 | // Member Pointer Evaluation |
| 2801 | //===----------------------------------------------------------------------===// |
| 2802 | |
| 2803 | namespace { |
| 2804 | class MemberPointerExprEvaluator |
| 2805 | : public ExprEvaluatorBase<MemberPointerExprEvaluator, bool> { |
| 2806 | MemberPtr &Result; |
| 2807 | |
| 2808 | bool Success(const ValueDecl *D) { |
| 2809 | Result = MemberPtr(D); |
| 2810 | return true; |
| 2811 | } |
| 2812 | public: |
| 2813 | |
| 2814 | MemberPointerExprEvaluator(EvalInfo &Info, MemberPtr &Result) |
| 2815 | : ExprEvaluatorBaseTy(Info), Result(Result) {} |
| 2816 | |
| 2817 | bool Success(const CCValue &V, const Expr *E) { |
| 2818 | Result.setFrom(V); |
| 2819 | return true; |
| 2820 | } |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2821 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2822 | return Success((const ValueDecl*)0); |
| 2823 | } |
| 2824 | |
| 2825 | bool VisitCastExpr(const CastExpr *E); |
| 2826 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
| 2827 | }; |
| 2828 | } // end anonymous namespace |
| 2829 | |
| 2830 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
| 2831 | EvalInfo &Info) { |
| 2832 | assert(E->isRValue() && E->getType()->isMemberPointerType()); |
| 2833 | return MemberPointerExprEvaluator(Info, Result).Visit(E); |
| 2834 | } |
| 2835 | |
| 2836 | bool MemberPointerExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 2837 | switch (E->getCastKind()) { |
| 2838 | default: |
| 2839 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 2840 | |
| 2841 | case CK_NullToMemberPointer: |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2842 | return ZeroInitialization(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2843 | |
| 2844 | case CK_BaseToDerivedMemberPointer: { |
| 2845 | if (!Visit(E->getSubExpr())) |
| 2846 | return false; |
| 2847 | if (E->path_empty()) |
| 2848 | return true; |
| 2849 | // Base-to-derived member pointer casts store the path in derived-to-base |
| 2850 | // order, so iterate backwards. The CXXBaseSpecifier also provides us with |
| 2851 | // the wrong end of the derived->base arc, so stagger the path by one class. |
| 2852 | typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter; |
| 2853 | for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin()); |
| 2854 | PathI != PathE; ++PathI) { |
| 2855 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
| 2856 | const CXXRecordDecl *Derived = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 2857 | if (!Result.castToDerived(Derived)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2858 | return Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2859 | } |
| 2860 | const Type *FinalTy = E->getType()->castAs<MemberPointerType>()->getClass(); |
| 2861 | if (!Result.castToDerived(FinalTy->getAsCXXRecordDecl())) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2862 | return Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2863 | return true; |
| 2864 | } |
| 2865 | |
| 2866 | case CK_DerivedToBaseMemberPointer: |
| 2867 | if (!Visit(E->getSubExpr())) |
| 2868 | return false; |
| 2869 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 2870 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 2871 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
| 2872 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 2873 | if (!Result.castToBase(Base)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2874 | return Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2875 | } |
| 2876 | return true; |
| 2877 | } |
| 2878 | } |
| 2879 | |
| 2880 | bool MemberPointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| 2881 | // C++11 [expr.unary.op]p3 has very strict rules on how the address of a |
| 2882 | // member can be formed. |
| 2883 | return Success(cast<DeclRefExpr>(E->getSubExpr())->getDecl()); |
| 2884 | } |
| 2885 | |
| 2886 | //===----------------------------------------------------------------------===// |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2887 | // Record Evaluation |
| 2888 | //===----------------------------------------------------------------------===// |
| 2889 | |
| 2890 | namespace { |
| 2891 | class RecordExprEvaluator |
| 2892 | : public ExprEvaluatorBase<RecordExprEvaluator, bool> { |
| 2893 | const LValue &This; |
| 2894 | APValue &Result; |
| 2895 | public: |
| 2896 | |
| 2897 | RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result) |
| 2898 | : ExprEvaluatorBaseTy(info), This(This), Result(Result) {} |
| 2899 | |
| 2900 | bool Success(const CCValue &V, const Expr *E) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2901 | return CheckConstantExpression(Info, E, V, Result); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2902 | } |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2903 | bool ZeroInitialization(const Expr *E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2904 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2905 | bool VisitCastExpr(const CastExpr *E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2906 | bool VisitInitListExpr(const InitListExpr *E); |
| 2907 | bool VisitCXXConstructExpr(const CXXConstructExpr *E); |
| 2908 | }; |
| 2909 | } |
| 2910 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2911 | /// Perform zero-initialization on an object of non-union class type. |
| 2912 | /// C++11 [dcl.init]p5: |
| 2913 | /// To zero-initialize an object or reference of type T means: |
| 2914 | /// [...] |
| 2915 | /// -- if T is a (possibly cv-qualified) non-union class type, |
| 2916 | /// each non-static data member and each base-class subobject is |
| 2917 | /// zero-initialized |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2918 | static bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E, |
| 2919 | const RecordDecl *RD, |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2920 | const LValue &This, APValue &Result) { |
| 2921 | assert(!RD->isUnion() && "Expected non-union class type"); |
| 2922 | const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD); |
| 2923 | Result = APValue(APValue::UninitStruct(), CD ? CD->getNumBases() : 0, |
| 2924 | std::distance(RD->field_begin(), RD->field_end())); |
| 2925 | |
| 2926 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 2927 | |
| 2928 | if (CD) { |
| 2929 | unsigned Index = 0; |
| 2930 | for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(), |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2931 | End = CD->bases_end(); I != End; ++I, ++Index) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2932 | const CXXRecordDecl *Base = I->getType()->getAsCXXRecordDecl(); |
| 2933 | LValue Subobject = This; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2934 | HandleLValueDirectBase(Info, E, Subobject, CD, Base, &Layout); |
| 2935 | if (!HandleClassZeroInitialization(Info, E, Base, Subobject, |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2936 | Result.getStructBase(Index))) |
| 2937 | return false; |
| 2938 | } |
| 2939 | } |
| 2940 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2941 | for (RecordDecl::field_iterator I = RD->field_begin(), End = RD->field_end(); |
| 2942 | I != End; ++I) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2943 | // -- if T is a reference type, no initialization is performed. |
| 2944 | if ((*I)->getType()->isReferenceType()) |
| 2945 | continue; |
| 2946 | |
| 2947 | LValue Subobject = This; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2948 | HandleLValueMember(Info, E, Subobject, *I, &Layout); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2949 | |
| 2950 | ImplicitValueInitExpr VIE((*I)->getType()); |
| 2951 | if (!EvaluateConstantExpression( |
| 2952 | Result.getStructField((*I)->getFieldIndex()), Info, Subobject, &VIE)) |
| 2953 | return false; |
| 2954 | } |
| 2955 | |
| 2956 | return true; |
| 2957 | } |
| 2958 | |
| 2959 | bool RecordExprEvaluator::ZeroInitialization(const Expr *E) { |
| 2960 | const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl(); |
| 2961 | if (RD->isUnion()) { |
| 2962 | // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the |
| 2963 | // object's first non-static named data member is zero-initialized |
| 2964 | RecordDecl::field_iterator I = RD->field_begin(); |
| 2965 | if (I == RD->field_end()) { |
| 2966 | Result = APValue((const FieldDecl*)0); |
| 2967 | return true; |
| 2968 | } |
| 2969 | |
| 2970 | LValue Subobject = This; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2971 | HandleLValueMember(Info, E, Subobject, *I); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2972 | Result = APValue(*I); |
| 2973 | ImplicitValueInitExpr VIE((*I)->getType()); |
| 2974 | return EvaluateConstantExpression(Result.getUnionValue(), Info, |
| 2975 | Subobject, &VIE); |
| 2976 | } |
| 2977 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2978 | return HandleClassZeroInitialization(Info, E, RD, This, Result); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2979 | } |
| 2980 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2981 | bool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 2982 | switch (E->getCastKind()) { |
| 2983 | default: |
| 2984 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 2985 | |
| 2986 | case CK_ConstructorConversion: |
| 2987 | return Visit(E->getSubExpr()); |
| 2988 | |
| 2989 | case CK_DerivedToBase: |
| 2990 | case CK_UncheckedDerivedToBase: { |
| 2991 | CCValue DerivedObject; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2992 | if (!Evaluate(DerivedObject, Info, E->getSubExpr())) |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2993 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2994 | if (!DerivedObject.isStruct()) |
| 2995 | return Error(E->getSubExpr()); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2996 | |
| 2997 | // Derived-to-base rvalue conversion: just slice off the derived part. |
| 2998 | APValue *Value = &DerivedObject; |
| 2999 | const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl(); |
| 3000 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 3001 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 3002 | assert(!(*PathI)->isVirtual() && "record rvalue with virtual base"); |
| 3003 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 3004 | Value = &Value->getStructBase(getBaseIndex(RD, Base)); |
| 3005 | RD = Base; |
| 3006 | } |
| 3007 | Result = *Value; |
| 3008 | return true; |
| 3009 | } |
| 3010 | } |
| 3011 | } |
| 3012 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3013 | bool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 3014 | const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl(); |
| 3015 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 3016 | |
| 3017 | if (RD->isUnion()) { |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 3018 | const FieldDecl *Field = E->getInitializedFieldInUnion(); |
| 3019 | Result = APValue(Field); |
| 3020 | if (!Field) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3021 | return true; |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 3022 | |
| 3023 | // If the initializer list for a union does not contain any elements, the |
| 3024 | // first element of the union is value-initialized. |
| 3025 | ImplicitValueInitExpr VIE(Field->getType()); |
| 3026 | const Expr *InitExpr = E->getNumInits() ? E->getInit(0) : &VIE; |
| 3027 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3028 | LValue Subobject = This; |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 3029 | HandleLValueMember(Info, InitExpr, Subobject, Field, &Layout); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3030 | return EvaluateConstantExpression(Result.getUnionValue(), Info, |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 3031 | Subobject, InitExpr); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3032 | } |
| 3033 | |
| 3034 | assert((!isa<CXXRecordDecl>(RD) || !cast<CXXRecordDecl>(RD)->getNumBases()) && |
| 3035 | "initializer list for class with base classes"); |
| 3036 | Result = APValue(APValue::UninitStruct(), 0, |
| 3037 | std::distance(RD->field_begin(), RD->field_end())); |
| 3038 | unsigned ElementNo = 0; |
| 3039 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 3040 | FieldEnd = RD->field_end(); Field != FieldEnd; ++Field) { |
| 3041 | // Anonymous bit-fields are not considered members of the class for |
| 3042 | // purposes of aggregate initialization. |
| 3043 | if (Field->isUnnamedBitfield()) |
| 3044 | continue; |
| 3045 | |
| 3046 | LValue Subobject = This; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3047 | |
| 3048 | if (ElementNo < E->getNumInits()) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3049 | HandleLValueMember(Info, E->getInit(ElementNo), Subobject, *Field, |
| 3050 | &Layout); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3051 | if (!EvaluateConstantExpression( |
| 3052 | Result.getStructField((*Field)->getFieldIndex()), |
| 3053 | Info, Subobject, E->getInit(ElementNo++))) |
| 3054 | return false; |
| 3055 | } else { |
| 3056 | // Perform an implicit value-initialization for members beyond the end of |
| 3057 | // the initializer list. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3058 | HandleLValueMember(Info, E, Subobject, *Field, &Layout); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3059 | ImplicitValueInitExpr VIE(Field->getType()); |
| 3060 | if (!EvaluateConstantExpression( |
| 3061 | Result.getStructField((*Field)->getFieldIndex()), |
| 3062 | Info, Subobject, &VIE)) |
| 3063 | return false; |
| 3064 | } |
| 3065 | } |
| 3066 | |
| 3067 | return true; |
| 3068 | } |
| 3069 | |
| 3070 | bool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 3071 | const CXXConstructorDecl *FD = E->getConstructor(); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3072 | bool ZeroInit = E->requiresZeroInitialization(); |
| 3073 | if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) { |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 3074 | // If we've already performed zero-initialization, we're already done. |
| 3075 | if (!Result.isUninit()) |
| 3076 | return true; |
| 3077 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3078 | if (ZeroInit) |
| 3079 | return ZeroInitialization(E); |
| 3080 | |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 3081 | const CXXRecordDecl *RD = FD->getParent(); |
| 3082 | if (RD->isUnion()) |
| 3083 | Result = APValue((FieldDecl*)0); |
| 3084 | else |
| 3085 | Result = APValue(APValue::UninitStruct(), RD->getNumBases(), |
| 3086 | std::distance(RD->field_begin(), RD->field_end())); |
| 3087 | return true; |
| 3088 | } |
| 3089 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3090 | const FunctionDecl *Definition = 0; |
| 3091 | FD->getBody(Definition); |
| 3092 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3093 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition)) |
| 3094 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3095 | |
Richard Smith | 610a60c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 3096 | // Avoid materializing a temporary for an elidable copy/move constructor. |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3097 | if (E->isElidable() && !ZeroInit) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3098 | if (const MaterializeTemporaryExpr *ME |
| 3099 | = dyn_cast<MaterializeTemporaryExpr>(E->getArg(0))) |
| 3100 | return Visit(ME->GetTemporaryExpr()); |
| 3101 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3102 | if (ZeroInit && !ZeroInitialization(E)) |
| 3103 | return false; |
| 3104 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3105 | llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs()); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3106 | return HandleConstructorCall(E, This, Args, |
| 3107 | cast<CXXConstructorDecl>(Definition), Info, |
| 3108 | Result); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3109 | } |
| 3110 | |
| 3111 | static bool EvaluateRecord(const Expr *E, const LValue &This, |
| 3112 | APValue &Result, EvalInfo &Info) { |
| 3113 | assert(E->isRValue() && E->getType()->isRecordType() && |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3114 | "can't evaluate expression as a record rvalue"); |
| 3115 | return RecordExprEvaluator(Info, This, Result).Visit(E); |
| 3116 | } |
| 3117 | |
| 3118 | //===----------------------------------------------------------------------===// |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3119 | // Temporary Evaluation |
| 3120 | // |
| 3121 | // Temporaries are represented in the AST as rvalues, but generally behave like |
| 3122 | // lvalues. The full-object of which the temporary is a subobject is implicitly |
| 3123 | // materialized so that a reference can bind to it. |
| 3124 | //===----------------------------------------------------------------------===// |
| 3125 | namespace { |
| 3126 | class TemporaryExprEvaluator |
| 3127 | : public LValueExprEvaluatorBase<TemporaryExprEvaluator> { |
| 3128 | public: |
| 3129 | TemporaryExprEvaluator(EvalInfo &Info, LValue &Result) : |
| 3130 | LValueExprEvaluatorBaseTy(Info, Result) {} |
| 3131 | |
| 3132 | /// Visit an expression which constructs the value of this temporary. |
| 3133 | bool VisitConstructExpr(const Expr *E) { |
| 3134 | Result.set(E, Info.CurrentCall); |
| 3135 | return EvaluateConstantExpression(Info.CurrentCall->Temporaries[E], Info, |
| 3136 | Result, E); |
| 3137 | } |
| 3138 | |
| 3139 | bool VisitCastExpr(const CastExpr *E) { |
| 3140 | switch (E->getCastKind()) { |
| 3141 | default: |
| 3142 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
| 3143 | |
| 3144 | case CK_ConstructorConversion: |
| 3145 | return VisitConstructExpr(E->getSubExpr()); |
| 3146 | } |
| 3147 | } |
| 3148 | bool VisitInitListExpr(const InitListExpr *E) { |
| 3149 | return VisitConstructExpr(E); |
| 3150 | } |
| 3151 | bool VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 3152 | return VisitConstructExpr(E); |
| 3153 | } |
| 3154 | bool VisitCallExpr(const CallExpr *E) { |
| 3155 | return VisitConstructExpr(E); |
| 3156 | } |
| 3157 | }; |
| 3158 | } // end anonymous namespace |
| 3159 | |
| 3160 | /// Evaluate an expression of record type as a temporary. |
| 3161 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) { |
Richard Smith | af2c7a1 | 2011-12-19 22:01:37 +0000 | [diff] [blame] | 3162 | assert(E->isRValue() && E->getType()->isRecordType()); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3163 | return TemporaryExprEvaluator(Info, Result).Visit(E); |
| 3164 | } |
| 3165 | |
| 3166 | //===----------------------------------------------------------------------===// |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3167 | // Vector Evaluation |
| 3168 | //===----------------------------------------------------------------------===// |
| 3169 | |
| 3170 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 3171 | class VectorExprEvaluator |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3172 | : public ExprEvaluatorBase<VectorExprEvaluator, bool> { |
| 3173 | APValue &Result; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3174 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3175 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3176 | VectorExprEvaluator(EvalInfo &info, APValue &Result) |
| 3177 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3178 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3179 | bool Success(const ArrayRef<APValue> &V, const Expr *E) { |
| 3180 | assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements()); |
| 3181 | // FIXME: remove this APValue copy. |
| 3182 | Result = APValue(V.data(), V.size()); |
| 3183 | return true; |
| 3184 | } |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 3185 | bool Success(const CCValue &V, const Expr *E) { |
| 3186 | assert(V.isVector()); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3187 | Result = V; |
| 3188 | return true; |
| 3189 | } |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3190 | bool ZeroInitialization(const Expr *E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3191 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3192 | bool VisitUnaryReal(const UnaryOperator *E) |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 3193 | { return Visit(E->getSubExpr()); } |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3194 | bool VisitCastExpr(const CastExpr* E); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3195 | bool VisitInitListExpr(const InitListExpr *E); |
| 3196 | bool VisitUnaryImag(const UnaryOperator *E); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 3197 | // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div, |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 3198 | // binary comparisons, binary and/or/xor, |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 3199 | // shufflevector, ExtVectorElementExpr |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3200 | }; |
| 3201 | } // end anonymous namespace |
| 3202 | |
| 3203 | static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3204 | assert(E->isRValue() && E->getType()->isVectorType() &&"not a vector rvalue"); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3205 | return VectorExprEvaluator(Info, Result).Visit(E); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3206 | } |
| 3207 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3208 | bool VectorExprEvaluator::VisitCastExpr(const CastExpr* E) { |
| 3209 | const VectorType *VTy = E->getType()->castAs<VectorType>(); |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 3210 | unsigned NElts = VTy->getNumElements(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3211 | |
Richard Smith | d62ca37 | 2011-12-06 22:44:34 +0000 | [diff] [blame] | 3212 | const Expr *SE = E->getSubExpr(); |
Nate Begeman | e8c9e92 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 3213 | QualType SETy = SE->getType(); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3214 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3215 | switch (E->getCastKind()) { |
| 3216 | case CK_VectorSplat: { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3217 | APValue Val = APValue(); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3218 | if (SETy->isIntegerType()) { |
| 3219 | APSInt IntResult; |
| 3220 | if (!EvaluateInteger(SE, IntResult, Info)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3221 | return false; |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3222 | Val = APValue(IntResult); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3223 | } else if (SETy->isRealFloatingType()) { |
| 3224 | APFloat F(0.0); |
| 3225 | if (!EvaluateFloat(SE, F, Info)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3226 | return false; |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3227 | Val = APValue(F); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3228 | } else { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3229 | return Error(E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3230 | } |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 3231 | |
| 3232 | // Splat and create vector APValue. |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3233 | SmallVector<APValue, 4> Elts(NElts, Val); |
| 3234 | return Success(Elts, E); |
Nate Begeman | e8c9e92 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 3235 | } |
Eli Friedman | e6a24e8 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 3236 | case CK_BitCast: { |
| 3237 | // Evaluate the operand into an APInt we can extract from. |
| 3238 | llvm::APInt SValInt; |
| 3239 | if (!EvalAndBitcastToAPInt(Info, SE, SValInt)) |
| 3240 | return false; |
| 3241 | // Extract the elements |
| 3242 | QualType EltTy = VTy->getElementType(); |
| 3243 | unsigned EltSize = Info.Ctx.getTypeSize(EltTy); |
| 3244 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
| 3245 | SmallVector<APValue, 4> Elts; |
| 3246 | if (EltTy->isRealFloatingType()) { |
| 3247 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(EltTy); |
| 3248 | bool isIEESem = &Sem != &APFloat::PPCDoubleDouble; |
| 3249 | unsigned FloatEltSize = EltSize; |
| 3250 | if (&Sem == &APFloat::x87DoubleExtended) |
| 3251 | FloatEltSize = 80; |
| 3252 | for (unsigned i = 0; i < NElts; i++) { |
| 3253 | llvm::APInt Elt; |
| 3254 | if (BigEndian) |
| 3255 | Elt = SValInt.rotl(i*EltSize+FloatEltSize).trunc(FloatEltSize); |
| 3256 | else |
| 3257 | Elt = SValInt.rotr(i*EltSize).trunc(FloatEltSize); |
| 3258 | Elts.push_back(APValue(APFloat(Elt, isIEESem))); |
| 3259 | } |
| 3260 | } else if (EltTy->isIntegerType()) { |
| 3261 | for (unsigned i = 0; i < NElts; i++) { |
| 3262 | llvm::APInt Elt; |
| 3263 | if (BigEndian) |
| 3264 | Elt = SValInt.rotl(i*EltSize+EltSize).zextOrTrunc(EltSize); |
| 3265 | else |
| 3266 | Elt = SValInt.rotr(i*EltSize).zextOrTrunc(EltSize); |
| 3267 | Elts.push_back(APValue(APSInt(Elt, EltTy->isSignedIntegerType()))); |
| 3268 | } |
| 3269 | } else { |
| 3270 | return Error(E); |
| 3271 | } |
| 3272 | return Success(Elts, E); |
| 3273 | } |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3274 | default: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3275 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3276 | } |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3277 | } |
| 3278 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3279 | bool |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3280 | VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3281 | const VectorType *VT = E->getType()->castAs<VectorType>(); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3282 | unsigned NumInits = E->getNumInits(); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 3283 | unsigned NumElements = VT->getNumElements(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3284 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3285 | QualType EltTy = VT->getElementType(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3286 | SmallVector<APValue, 4> Elements; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3287 | |
Eli Friedman | 3edd5a9 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 3288 | // The number of initializers can be less than the number of |
| 3289 | // vector elements. For OpenCL, this can be due to nested vector |
| 3290 | // initialization. For GCC compatibility, missing trailing elements |
| 3291 | // should be initialized with zeroes. |
| 3292 | unsigned CountInits = 0, CountElts = 0; |
| 3293 | while (CountElts < NumElements) { |
| 3294 | // Handle nested vector initialization. |
| 3295 | if (CountInits < NumInits |
| 3296 | && E->getInit(CountInits)->getType()->isExtVectorType()) { |
| 3297 | APValue v; |
| 3298 | if (!EvaluateVector(E->getInit(CountInits), v, Info)) |
| 3299 | return Error(E); |
| 3300 | unsigned vlen = v.getVectorLength(); |
| 3301 | for (unsigned j = 0; j < vlen; j++) |
| 3302 | Elements.push_back(v.getVectorElt(j)); |
| 3303 | CountElts += vlen; |
| 3304 | } else if (EltTy->isIntegerType()) { |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3305 | llvm::APSInt sInt(32); |
Eli Friedman | 3edd5a9 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 3306 | if (CountInits < NumInits) { |
| 3307 | if (!EvaluateInteger(E->getInit(CountInits), sInt, Info)) |
| 3308 | return Error(E); |
| 3309 | } else // trailing integer zero. |
| 3310 | sInt = Info.Ctx.MakeIntValue(0, EltTy); |
| 3311 | Elements.push_back(APValue(sInt)); |
| 3312 | CountElts++; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3313 | } else { |
| 3314 | llvm::APFloat f(0.0); |
Eli Friedman | 3edd5a9 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 3315 | if (CountInits < NumInits) { |
| 3316 | if (!EvaluateFloat(E->getInit(CountInits), f, Info)) |
| 3317 | return Error(E); |
| 3318 | } else // trailing float zero. |
| 3319 | f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)); |
| 3320 | Elements.push_back(APValue(f)); |
| 3321 | CountElts++; |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 3322 | } |
Eli Friedman | 3edd5a9 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 3323 | CountInits++; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3324 | } |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3325 | return Success(Elements, E); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3326 | } |
| 3327 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3328 | bool |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3329 | VectorExprEvaluator::ZeroInitialization(const Expr *E) { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3330 | const VectorType *VT = E->getType()->getAs<VectorType>(); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 3331 | QualType EltTy = VT->getElementType(); |
| 3332 | APValue ZeroElement; |
| 3333 | if (EltTy->isIntegerType()) |
| 3334 | ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy)); |
| 3335 | else |
| 3336 | ZeroElement = |
| 3337 | APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy))); |
| 3338 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3339 | SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3340 | return Success(Elements, E); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 3341 | } |
| 3342 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3343 | bool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 3344 | VisitIgnoredValue(E->getSubExpr()); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3345 | return ZeroInitialization(E); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 3346 | } |
| 3347 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3348 | //===----------------------------------------------------------------------===// |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3349 | // Array Evaluation |
| 3350 | //===----------------------------------------------------------------------===// |
| 3351 | |
| 3352 | namespace { |
| 3353 | class ArrayExprEvaluator |
| 3354 | : public ExprEvaluatorBase<ArrayExprEvaluator, bool> { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3355 | const LValue &This; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3356 | APValue &Result; |
| 3357 | public: |
| 3358 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3359 | ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result) |
| 3360 | : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {} |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3361 | |
| 3362 | bool Success(const APValue &V, const Expr *E) { |
| 3363 | assert(V.isArray() && "Expected array type"); |
| 3364 | Result = V; |
| 3365 | return true; |
| 3366 | } |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3367 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3368 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3369 | const ConstantArrayType *CAT = |
| 3370 | Info.Ctx.getAsConstantArrayType(E->getType()); |
| 3371 | if (!CAT) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3372 | return Error(E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3373 | |
| 3374 | Result = APValue(APValue::UninitArray(), 0, |
| 3375 | CAT->getSize().getZExtValue()); |
| 3376 | if (!Result.hasArrayFiller()) return true; |
| 3377 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3378 | // Zero-initialize all elements. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3379 | LValue Subobject = This; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3380 | Subobject.addArray(Info, E, CAT); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3381 | ImplicitValueInitExpr VIE(CAT->getElementType()); |
| 3382 | return EvaluateConstantExpression(Result.getArrayFiller(), Info, |
| 3383 | Subobject, &VIE); |
| 3384 | } |
| 3385 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3386 | bool VisitInitListExpr(const InitListExpr *E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3387 | bool VisitCXXConstructExpr(const CXXConstructExpr *E); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3388 | }; |
| 3389 | } // end anonymous namespace |
| 3390 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3391 | static bool EvaluateArray(const Expr *E, const LValue &This, |
| 3392 | APValue &Result, EvalInfo &Info) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3393 | assert(E->isRValue() && E->getType()->isArrayType() && "not an array rvalue"); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3394 | return ArrayExprEvaluator(Info, This, Result).Visit(E); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3395 | } |
| 3396 | |
| 3397 | bool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 3398 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType()); |
| 3399 | if (!CAT) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3400 | return Error(E); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3401 | |
Richard Smith | 974c5f9 | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 3402 | // C++11 [dcl.init.string]p1: A char array [...] can be initialized by [...] |
| 3403 | // an appropriately-typed string literal enclosed in braces. |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 3404 | if (E->getNumInits() == 1 && E->getInit(0)->isGLValue() && |
Richard Smith | 974c5f9 | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 3405 | Info.Ctx.hasSameUnqualifiedType(E->getType(), E->getInit(0)->getType())) { |
| 3406 | LValue LV; |
| 3407 | if (!EvaluateLValue(E->getInit(0), LV, Info)) |
| 3408 | return false; |
| 3409 | uint64_t NumElements = CAT->getSize().getZExtValue(); |
| 3410 | Result = APValue(APValue::UninitArray(), NumElements, NumElements); |
| 3411 | |
| 3412 | // Copy the string literal into the array. FIXME: Do this better. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3413 | LV.addArray(Info, E, CAT); |
Richard Smith | 974c5f9 | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 3414 | for (uint64_t I = 0; I < NumElements; ++I) { |
| 3415 | CCValue Char; |
| 3416 | if (!HandleLValueToRValueConversion(Info, E->getInit(0), |
| 3417 | CAT->getElementType(), LV, Char)) |
| 3418 | return false; |
| 3419 | if (!CheckConstantExpression(Info, E->getInit(0), Char, |
| 3420 | Result.getArrayInitializedElt(I))) |
| 3421 | return false; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3422 | if (!HandleLValueArrayAdjustment(Info, E->getInit(0), LV, |
| 3423 | CAT->getElementType(), 1)) |
Richard Smith | 974c5f9 | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 3424 | return false; |
| 3425 | } |
| 3426 | return true; |
| 3427 | } |
| 3428 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3429 | Result = APValue(APValue::UninitArray(), E->getNumInits(), |
| 3430 | CAT->getSize().getZExtValue()); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3431 | LValue Subobject = This; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3432 | Subobject.addArray(Info, E, CAT); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3433 | unsigned Index = 0; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3434 | for (InitListExpr::const_iterator I = E->begin(), End = E->end(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3435 | I != End; ++I, ++Index) { |
| 3436 | if (!EvaluateConstantExpression(Result.getArrayInitializedElt(Index), |
| 3437 | Info, Subobject, cast<Expr>(*I))) |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3438 | return false; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3439 | if (!HandleLValueArrayAdjustment(Info, cast<Expr>(*I), Subobject, |
| 3440 | CAT->getElementType(), 1)) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3441 | return false; |
| 3442 | } |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3443 | |
| 3444 | if (!Result.hasArrayFiller()) return true; |
| 3445 | assert(E->hasArrayFiller() && "no array filler for incomplete init list"); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3446 | // FIXME: The Subobject here isn't necessarily right. This rarely matters, |
| 3447 | // but sometimes does: |
| 3448 | // struct S { constexpr S() : p(&p) {} void *p; }; |
| 3449 | // S s[10] = {}; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3450 | return EvaluateConstantExpression(Result.getArrayFiller(), Info, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3451 | Subobject, E->getArrayFiller()); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3452 | } |
| 3453 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3454 | bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 3455 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType()); |
| 3456 | if (!CAT) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3457 | return Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3458 | |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 3459 | bool HadZeroInit = !Result.isUninit(); |
| 3460 | if (!HadZeroInit) |
| 3461 | Result = APValue(APValue::UninitArray(), 0, CAT->getSize().getZExtValue()); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3462 | if (!Result.hasArrayFiller()) |
| 3463 | return true; |
| 3464 | |
| 3465 | const CXXConstructorDecl *FD = E->getConstructor(); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 3466 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3467 | bool ZeroInit = E->requiresZeroInitialization(); |
| 3468 | if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) { |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 3469 | if (HadZeroInit) |
| 3470 | return true; |
| 3471 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3472 | if (ZeroInit) { |
| 3473 | LValue Subobject = This; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3474 | Subobject.addArray(Info, E, CAT); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3475 | ImplicitValueInitExpr VIE(CAT->getElementType()); |
| 3476 | return EvaluateConstantExpression(Result.getArrayFiller(), Info, |
| 3477 | Subobject, &VIE); |
| 3478 | } |
| 3479 | |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 3480 | const CXXRecordDecl *RD = FD->getParent(); |
| 3481 | if (RD->isUnion()) |
| 3482 | Result.getArrayFiller() = APValue((FieldDecl*)0); |
| 3483 | else |
| 3484 | Result.getArrayFiller() = |
| 3485 | APValue(APValue::UninitStruct(), RD->getNumBases(), |
| 3486 | std::distance(RD->field_begin(), RD->field_end())); |
| 3487 | return true; |
| 3488 | } |
| 3489 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3490 | const FunctionDecl *Definition = 0; |
| 3491 | FD->getBody(Definition); |
| 3492 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3493 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition)) |
| 3494 | return false; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3495 | |
| 3496 | // FIXME: The Subobject here isn't necessarily right. This rarely matters, |
| 3497 | // but sometimes does: |
| 3498 | // struct S { constexpr S() : p(&p) {} void *p; }; |
| 3499 | // S s[10]; |
| 3500 | LValue Subobject = This; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3501 | Subobject.addArray(Info, E, CAT); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3502 | |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 3503 | if (ZeroInit && !HadZeroInit) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3504 | ImplicitValueInitExpr VIE(CAT->getElementType()); |
| 3505 | if (!EvaluateConstantExpression(Result.getArrayFiller(), Info, Subobject, |
| 3506 | &VIE)) |
| 3507 | return false; |
| 3508 | } |
| 3509 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3510 | llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs()); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3511 | return HandleConstructorCall(E, Subobject, Args, |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3512 | cast<CXXConstructorDecl>(Definition), |
| 3513 | Info, Result.getArrayFiller()); |
| 3514 | } |
| 3515 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3516 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3517 | // Integer Evaluation |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3518 | // |
| 3519 | // As a GNU extension, we support casting pointers to sufficiently-wide integer |
| 3520 | // types and back in constant folding. Integer values are thus represented |
| 3521 | // either as an integer-valued APValue, or as an lvalue-valued APValue. |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3522 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3523 | |
| 3524 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 3525 | class IntExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3526 | : public ExprEvaluatorBase<IntExprEvaluator, bool> { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3527 | CCValue &Result; |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 3528 | public: |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3529 | IntExprEvaluator(EvalInfo &info, CCValue &result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3530 | : ExprEvaluatorBaseTy(info), Result(result) {} |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3531 | |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 3532 | bool Success(const llvm::APSInt &SI, const Expr *E) { |
| 3533 | assert(E->getType()->isIntegralOrEnumerationType() && |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3534 | "Invalid evaluation result."); |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 3535 | assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 3536 | "Invalid evaluation result."); |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 3537 | assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 3538 | "Invalid evaluation result."); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3539 | Result = CCValue(SI); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 3540 | return true; |
| 3541 | } |
| 3542 | |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3543 | bool Success(const llvm::APInt &I, const Expr *E) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3544 | assert(E->getType()->isIntegralOrEnumerationType() && |
| 3545 | "Invalid evaluation result."); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 3546 | assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 3547 | "Invalid evaluation result."); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3548 | Result = CCValue(APSInt(I)); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3549 | Result.getInt().setIsUnsigned( |
| 3550 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3551 | return true; |
| 3552 | } |
| 3553 | |
| 3554 | bool Success(uint64_t Value, const Expr *E) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3555 | assert(E->getType()->isIntegralOrEnumerationType() && |
| 3556 | "Invalid evaluation result."); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3557 | Result = CCValue(Info.Ctx.MakeIntValue(Value, E->getType())); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3558 | return true; |
| 3559 | } |
| 3560 | |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 3561 | bool Success(CharUnits Size, const Expr *E) { |
| 3562 | return Success(Size.getQuantity(), E); |
| 3563 | } |
| 3564 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3565 | bool Success(const CCValue &V, const Expr *E) { |
Eli Friedman | 5930a4c | 2012-01-05 23:59:40 +0000 | [diff] [blame] | 3566 | if (V.isLValue() || V.isAddrLabelDiff()) { |
Richard Smith | 342f1f8 | 2011-10-29 22:55:55 +0000 | [diff] [blame] | 3567 | Result = V; |
| 3568 | return true; |
| 3569 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3570 | return Success(V.getInt(), E); |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 3571 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3572 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3573 | bool ZeroInitialization(const Expr *E) { return Success(0, E); } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 3574 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3575 | //===--------------------------------------------------------------------===// |
| 3576 | // Visitor Methods |
| 3577 | //===--------------------------------------------------------------------===// |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 3578 | |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 3579 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3580 | return Success(E->getValue(), E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 3581 | } |
| 3582 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3583 | return Success(E->getValue(), E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 3584 | } |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 3585 | |
| 3586 | bool CheckReferencedDecl(const Expr *E, const Decl *D); |
| 3587 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3588 | if (CheckReferencedDecl(E, E->getDecl())) |
| 3589 | return true; |
| 3590 | |
| 3591 | return ExprEvaluatorBaseTy::VisitDeclRefExpr(E); |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 3592 | } |
| 3593 | bool VisitMemberExpr(const MemberExpr *E) { |
| 3594 | if (CheckReferencedDecl(E, E->getMemberDecl())) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3595 | VisitIgnoredValue(E->getBase()); |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 3596 | return true; |
| 3597 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3598 | |
| 3599 | return ExprEvaluatorBaseTy::VisitMemberExpr(E); |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 3600 | } |
| 3601 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3602 | bool VisitCallExpr(const CallExpr *E); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 3603 | bool VisitBinaryOperator(const BinaryOperator *E); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 3604 | bool VisitOffsetOfExpr(const OffsetOfExpr *E); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 3605 | bool VisitUnaryOperator(const UnaryOperator *E); |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 3606 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3607 | bool VisitCastExpr(const CastExpr* E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3608 | bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E); |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 3609 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 3610 | bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3611 | return Success(E->getValue(), E); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 3612 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3613 | |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 3614 | // Note, GNU defines __null as an integer, not a pointer. |
Anders Carlsson | 3f70456 | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 3615 | bool VisitGNUNullExpr(const GNUNullExpr *E) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3616 | return ZeroInitialization(E); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 3617 | } |
| 3618 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 3619 | bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) { |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3620 | return Success(E->getValue(), E); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 3621 | } |
| 3622 | |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3623 | bool VisitBinaryTypeTraitExpr(const BinaryTypeTraitExpr *E) { |
| 3624 | return Success(E->getValue(), E); |
| 3625 | } |
| 3626 | |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3627 | bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { |
| 3628 | return Success(E->getValue(), E); |
| 3629 | } |
| 3630 | |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3631 | bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { |
| 3632 | return Success(E->getValue(), E); |
| 3633 | } |
| 3634 | |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 3635 | bool VisitUnaryReal(const UnaryOperator *E); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 3636 | bool VisitUnaryImag(const UnaryOperator *E); |
| 3637 | |
Sebastian Redl | 295995c | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 3638 | bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E); |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 3639 | bool VisitSizeOfPackExpr(const SizeOfPackExpr *E); |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 3640 | |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 3641 | private: |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 3642 | CharUnits GetAlignOfExpr(const Expr *E); |
| 3643 | CharUnits GetAlignOfType(QualType T); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 3644 | static QualType GetObjectType(APValue::LValueBase B); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3645 | bool TryEvaluateBuiltinObjectSize(const CallExpr *E); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 3646 | // FIXME: Missing: array subscript of vector, member of vector |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 3647 | }; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3648 | } // end anonymous namespace |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 3649 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3650 | /// EvaluateIntegerOrLValue - Evaluate an rvalue integral-typed expression, and |
| 3651 | /// produce either the integer value or a pointer. |
| 3652 | /// |
| 3653 | /// GCC has a heinous extension which folds casts between pointer types and |
| 3654 | /// pointer-sized integral types. We support this by allowing the evaluation of |
| 3655 | /// an integer rvalue to produce a pointer (represented as an lvalue) instead. |
| 3656 | /// Some simple arithmetic on such values is supported (they are treated much |
| 3657 | /// like char*). |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3658 | static bool EvaluateIntegerOrLValue(const Expr *E, CCValue &Result, |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3659 | EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3660 | assert(E->isRValue() && E->getType()->isIntegralOrEnumerationType()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3661 | return IntExprEvaluator(Info, Result).Visit(E); |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 3662 | } |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 3663 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3664 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3665 | CCValue Val; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3666 | if (!EvaluateIntegerOrLValue(E, Val, Info)) |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 3667 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3668 | if (!Val.isInt()) { |
| 3669 | // FIXME: It would be better to produce the diagnostic for casting |
| 3670 | // a pointer to an integer. |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 3671 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3672 | return false; |
| 3673 | } |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 3674 | Result = Val.getInt(); |
| 3675 | return true; |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 3676 | } |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 3677 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3678 | /// Check whether the given declaration can be directly converted to an integral |
| 3679 | /// rvalue. If not, no diagnostic is produced; there are other things we can |
| 3680 | /// try. |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 3681 | bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) { |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 3682 | // Enums are integer constant exprs. |
Abramo Bagnara | bfbdcd8 | 2011-06-30 09:36:05 +0000 | [diff] [blame] | 3683 | if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) { |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 3684 | // Check for signedness/width mismatches between E type and ECD value. |
| 3685 | bool SameSign = (ECD->getInitVal().isSigned() |
| 3686 | == E->getType()->isSignedIntegerOrEnumerationType()); |
| 3687 | bool SameWidth = (ECD->getInitVal().getBitWidth() |
| 3688 | == Info.Ctx.getIntWidth(E->getType())); |
| 3689 | if (SameSign && SameWidth) |
| 3690 | return Success(ECD->getInitVal(), E); |
| 3691 | else { |
| 3692 | // Get rid of mismatch (otherwise Success assertions will fail) |
| 3693 | // by computing a new value matching the type of E. |
| 3694 | llvm::APSInt Val = ECD->getInitVal(); |
| 3695 | if (!SameSign) |
| 3696 | Val.setIsSigned(!ECD->getInitVal().isSigned()); |
| 3697 | if (!SameWidth) |
| 3698 | Val = Val.extOrTrunc(Info.Ctx.getIntWidth(E->getType())); |
| 3699 | return Success(Val, E); |
| 3700 | } |
Abramo Bagnara | bfbdcd8 | 2011-06-30 09:36:05 +0000 | [diff] [blame] | 3701 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3702 | return false; |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 3703 | } |
| 3704 | |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 3705 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
| 3706 | /// as GCC. |
| 3707 | static int EvaluateBuiltinClassifyType(const CallExpr *E) { |
| 3708 | // The following enum mimics the values returned by GCC. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3709 | // FIXME: Does GCC differ between lvalue and rvalue references here? |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 3710 | enum gcc_type_class { |
| 3711 | no_type_class = -1, |
| 3712 | void_type_class, integer_type_class, char_type_class, |
| 3713 | enumeral_type_class, boolean_type_class, |
| 3714 | pointer_type_class, reference_type_class, offset_type_class, |
| 3715 | real_type_class, complex_type_class, |
| 3716 | function_type_class, method_type_class, |
| 3717 | record_type_class, union_type_class, |
| 3718 | array_type_class, string_type_class, |
| 3719 | lang_type_class |
| 3720 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3721 | |
| 3722 | // 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] | 3723 | // ideal, however it is what gcc does. |
| 3724 | if (E->getNumArgs() == 0) |
| 3725 | return no_type_class; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3726 | |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 3727 | QualType ArgTy = E->getArg(0)->getType(); |
| 3728 | if (ArgTy->isVoidType()) |
| 3729 | return void_type_class; |
| 3730 | else if (ArgTy->isEnumeralType()) |
| 3731 | return enumeral_type_class; |
| 3732 | else if (ArgTy->isBooleanType()) |
| 3733 | return boolean_type_class; |
| 3734 | else if (ArgTy->isCharType()) |
| 3735 | return string_type_class; // gcc doesn't appear to use char_type_class |
| 3736 | else if (ArgTy->isIntegerType()) |
| 3737 | return integer_type_class; |
| 3738 | else if (ArgTy->isPointerType()) |
| 3739 | return pointer_type_class; |
| 3740 | else if (ArgTy->isReferenceType()) |
| 3741 | return reference_type_class; |
| 3742 | else if (ArgTy->isRealType()) |
| 3743 | return real_type_class; |
| 3744 | else if (ArgTy->isComplexType()) |
| 3745 | return complex_type_class; |
| 3746 | else if (ArgTy->isFunctionType()) |
| 3747 | return function_type_class; |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 3748 | else if (ArgTy->isStructureOrClassType()) |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 3749 | return record_type_class; |
| 3750 | else if (ArgTy->isUnionType()) |
| 3751 | return union_type_class; |
| 3752 | else if (ArgTy->isArrayType()) |
| 3753 | return array_type_class; |
| 3754 | else if (ArgTy->isUnionType()) |
| 3755 | return union_type_class; |
| 3756 | else // FIXME: offset_type_class, method_type_class, & lang_type_class? |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3757 | llvm_unreachable("CallExpr::isBuiltinClassifyType(): unimplemented type"); |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 3758 | return -1; |
| 3759 | } |
| 3760 | |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 3761 | /// EvaluateBuiltinConstantPForLValue - Determine the result of |
| 3762 | /// __builtin_constant_p when applied to the given lvalue. |
| 3763 | /// |
| 3764 | /// An lvalue is only "constant" if it is a pointer or reference to the first |
| 3765 | /// character of a string literal. |
| 3766 | template<typename LValue> |
| 3767 | static bool EvaluateBuiltinConstantPForLValue(const LValue &LV) { |
| 3768 | const Expr *E = LV.getLValueBase().dyn_cast<const Expr*>(); |
| 3769 | return E && isa<StringLiteral>(E) && LV.getLValueOffset().isZero(); |
| 3770 | } |
| 3771 | |
| 3772 | /// EvaluateBuiltinConstantP - Evaluate __builtin_constant_p as similarly to |
| 3773 | /// GCC as we can manage. |
| 3774 | static bool EvaluateBuiltinConstantP(ASTContext &Ctx, const Expr *Arg) { |
| 3775 | QualType ArgType = Arg->getType(); |
| 3776 | |
| 3777 | // __builtin_constant_p always has one operand. The rules which gcc follows |
| 3778 | // are not precisely documented, but are as follows: |
| 3779 | // |
| 3780 | // - If the operand is of integral, floating, complex or enumeration type, |
| 3781 | // and can be folded to a known value of that type, it returns 1. |
| 3782 | // - If the operand and can be folded to a pointer to the first character |
| 3783 | // of a string literal (or such a pointer cast to an integral type), it |
| 3784 | // returns 1. |
| 3785 | // |
| 3786 | // Otherwise, it returns 0. |
| 3787 | // |
| 3788 | // FIXME: GCC also intends to return 1 for literals of aggregate types, but |
| 3789 | // its support for this does not currently work. |
| 3790 | if (ArgType->isIntegralOrEnumerationType()) { |
| 3791 | Expr::EvalResult Result; |
| 3792 | if (!Arg->EvaluateAsRValue(Result, Ctx) || Result.HasSideEffects) |
| 3793 | return false; |
| 3794 | |
| 3795 | APValue &V = Result.Val; |
| 3796 | if (V.getKind() == APValue::Int) |
| 3797 | return true; |
| 3798 | |
| 3799 | return EvaluateBuiltinConstantPForLValue(V); |
| 3800 | } else if (ArgType->isFloatingType() || ArgType->isAnyComplexType()) { |
| 3801 | return Arg->isEvaluatable(Ctx); |
| 3802 | } else if (ArgType->isPointerType() || Arg->isGLValue()) { |
| 3803 | LValue LV; |
| 3804 | Expr::EvalStatus Status; |
| 3805 | EvalInfo Info(Ctx, Status); |
| 3806 | if ((Arg->isGLValue() ? EvaluateLValue(Arg, LV, Info) |
| 3807 | : EvaluatePointer(Arg, LV, Info)) && |
| 3808 | !Status.HasSideEffects) |
| 3809 | return EvaluateBuiltinConstantPForLValue(LV); |
| 3810 | } |
| 3811 | |
| 3812 | // Anything else isn't considered to be sufficiently constant. |
| 3813 | return false; |
| 3814 | } |
| 3815 | |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 3816 | /// Retrieves the "underlying object type" of the given expression, |
| 3817 | /// as used by __builtin_object_size. |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 3818 | QualType IntExprEvaluator::GetObjectType(APValue::LValueBase B) { |
| 3819 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
| 3820 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 3821 | return VD->getType(); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 3822 | } else if (const Expr *E = B.get<const Expr*>()) { |
| 3823 | if (isa<CompoundLiteralExpr>(E)) |
| 3824 | return E->getType(); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 3825 | } |
| 3826 | |
| 3827 | return QualType(); |
| 3828 | } |
| 3829 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3830 | bool IntExprEvaluator::TryEvaluateBuiltinObjectSize(const CallExpr *E) { |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 3831 | // TODO: Perhaps we should let LLVM lower this? |
| 3832 | LValue Base; |
| 3833 | if (!EvaluatePointer(E->getArg(0), Base, Info)) |
| 3834 | return false; |
| 3835 | |
| 3836 | // If we can prove the base is null, lower to zero now. |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 3837 | if (!Base.getLValueBase()) return Success(0, E); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 3838 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 3839 | QualType T = GetObjectType(Base.getLValueBase()); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 3840 | if (T.isNull() || |
| 3841 | T->isIncompleteType() || |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 3842 | T->isFunctionType() || |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 3843 | T->isVariablyModifiedType() || |
| 3844 | T->isDependentType()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3845 | return Error(E); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 3846 | |
| 3847 | CharUnits Size = Info.Ctx.getTypeSizeInChars(T); |
| 3848 | CharUnits Offset = Base.getLValueOffset(); |
| 3849 | |
| 3850 | if (!Offset.isNegative() && Offset <= Size) |
| 3851 | Size -= Offset; |
| 3852 | else |
| 3853 | Size = CharUnits::Zero(); |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 3854 | return Success(Size, E); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 3855 | } |
| 3856 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3857 | bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3858 | switch (E->isBuiltinCall()) { |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 3859 | default: |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3860 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 3861 | |
| 3862 | case Builtin::BI__builtin_object_size: { |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 3863 | if (TryEvaluateBuiltinObjectSize(E)) |
| 3864 | return true; |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 3865 | |
Eric Christopher | b2aaf51 | 2010-01-19 22:58:35 +0000 | [diff] [blame] | 3866 | // If evaluating the argument has side-effects we can't determine |
| 3867 | // the size of the object and lower it to unknown now. |
Fariborz Jahanian | 393c247 | 2009-11-05 18:03:03 +0000 | [diff] [blame] | 3868 | if (E->getArg(0)->HasSideEffects(Info.Ctx)) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3869 | if (E->getArg(1)->EvaluateKnownConstInt(Info.Ctx).getZExtValue() <= 1) |
Chris Lattner | cf18465 | 2009-11-03 19:48:51 +0000 | [diff] [blame] | 3870 | return Success(-1ULL, E); |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 3871 | return Success(0, E); |
| 3872 | } |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 3873 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3874 | return Error(E); |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 3875 | } |
| 3876 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 3877 | case Builtin::BI__builtin_classify_type: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3878 | return Success(EvaluateBuiltinClassifyType(E), E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3879 | |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 3880 | case Builtin::BI__builtin_constant_p: |
| 3881 | return Success(EvaluateBuiltinConstantP(Info.Ctx, E->getArg(0)), E); |
Richard Smith | e052d46 | 2011-12-09 02:04:48 +0000 | [diff] [blame] | 3882 | |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 3883 | case Builtin::BI__builtin_eh_return_data_regno: { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3884 | int Operand = E->getArg(0)->EvaluateKnownConstInt(Info.Ctx).getZExtValue(); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3885 | Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(Operand); |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 3886 | return Success(Operand, E); |
| 3887 | } |
Eli Friedman | c4a2638 | 2010-02-13 00:10:10 +0000 | [diff] [blame] | 3888 | |
| 3889 | case Builtin::BI__builtin_expect: |
| 3890 | return Visit(E->getArg(0)); |
Douglas Gregor | 5726d40 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 3891 | |
| 3892 | case Builtin::BIstrlen: |
| 3893 | case Builtin::BI__builtin_strlen: |
| 3894 | // As an extension, we support strlen() and __builtin_strlen() as constant |
| 3895 | // expressions when the argument is a string literal. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3896 | if (const StringLiteral *S |
Douglas Gregor | 5726d40 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 3897 | = dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenImpCasts())) { |
| 3898 | // The string literal may have embedded null characters. Find the first |
| 3899 | // one and truncate there. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3900 | StringRef Str = S->getString(); |
| 3901 | StringRef::size_type Pos = Str.find(0); |
| 3902 | if (Pos != StringRef::npos) |
Douglas Gregor | 5726d40 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 3903 | Str = Str.substr(0, Pos); |
| 3904 | |
| 3905 | return Success(Str.size(), E); |
| 3906 | } |
| 3907 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3908 | return Error(E); |
Eli Friedman | 454b57a | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 3909 | |
| 3910 | case Builtin::BI__atomic_is_lock_free: { |
| 3911 | APSInt SizeVal; |
| 3912 | if (!EvaluateInteger(E->getArg(0), SizeVal, Info)) |
| 3913 | return false; |
| 3914 | |
| 3915 | // For __atomic_is_lock_free(sizeof(_Atomic(T))), if the size is a power |
| 3916 | // of two less than the maximum inline atomic width, we know it is |
| 3917 | // lock-free. If the size isn't a power of two, or greater than the |
| 3918 | // maximum alignment where we promote atomics, we know it is not lock-free |
| 3919 | // (at least not in the sense of atomic_is_lock_free). Otherwise, |
| 3920 | // the answer can only be determined at runtime; for example, 16-byte |
| 3921 | // atomics have lock-free implementations on some, but not all, |
| 3922 | // x86-64 processors. |
| 3923 | |
| 3924 | // Check power-of-two. |
| 3925 | CharUnits Size = CharUnits::fromQuantity(SizeVal.getZExtValue()); |
| 3926 | if (!Size.isPowerOfTwo()) |
| 3927 | #if 0 |
| 3928 | // FIXME: Suppress this folding until the ABI for the promotion width |
| 3929 | // settles. |
| 3930 | return Success(0, E); |
| 3931 | #else |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3932 | return Error(E); |
Eli Friedman | 454b57a | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 3933 | #endif |
| 3934 | |
| 3935 | #if 0 |
| 3936 | // Check against promotion width. |
| 3937 | // FIXME: Suppress this folding until the ABI for the promotion width |
| 3938 | // settles. |
| 3939 | unsigned PromoteWidthBits = |
| 3940 | Info.Ctx.getTargetInfo().getMaxAtomicPromoteWidth(); |
| 3941 | if (Size > Info.Ctx.toCharUnitsFromBits(PromoteWidthBits)) |
| 3942 | return Success(0, E); |
| 3943 | #endif |
| 3944 | |
| 3945 | // Check against inlining width. |
| 3946 | unsigned InlineWidthBits = |
| 3947 | Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth(); |
| 3948 | if (Size <= Info.Ctx.toCharUnitsFromBits(InlineWidthBits)) |
| 3949 | return Success(1, E); |
| 3950 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3951 | return Error(E); |
Eli Friedman | 454b57a | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 3952 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 3953 | } |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 3954 | } |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 3955 | |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 3956 | static bool HasSameBase(const LValue &A, const LValue &B) { |
| 3957 | if (!A.getLValueBase()) |
| 3958 | return !B.getLValueBase(); |
| 3959 | if (!B.getLValueBase()) |
| 3960 | return false; |
| 3961 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 3962 | if (A.getLValueBase().getOpaqueValue() != |
| 3963 | B.getLValueBase().getOpaqueValue()) { |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 3964 | const Decl *ADecl = GetLValueBaseDecl(A); |
| 3965 | if (!ADecl) |
| 3966 | return false; |
| 3967 | const Decl *BDecl = GetLValueBaseDecl(B); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 3968 | if (!BDecl || ADecl->getCanonicalDecl() != BDecl->getCanonicalDecl()) |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 3969 | return false; |
| 3970 | } |
| 3971 | |
| 3972 | return IsGlobalLValue(A.getLValueBase()) || |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 3973 | A.getLValueFrame() == B.getLValueFrame(); |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 3974 | } |
| 3975 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 3976 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3977 | if (E->isAssignmentOp()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3978 | return Error(E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3979 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3980 | if (E->getOpcode() == BO_Comma) { |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 3981 | VisitIgnoredValue(E->getLHS()); |
| 3982 | return Visit(E->getRHS()); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 3983 | } |
| 3984 | |
| 3985 | if (E->isLogicalOp()) { |
| 3986 | // These need to be handled specially because the operands aren't |
| 3987 | // necessarily integral |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 3988 | bool lhsResult, rhsResult; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3989 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3990 | if (EvaluateAsBooleanCondition(E->getLHS(), lhsResult, Info)) { |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 3991 | // We were able to evaluate the LHS, see if we can get away with not |
| 3992 | // evaluating the RHS: 0 && X -> 0, 1 || X -> 1 |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3993 | if (lhsResult == (E->getOpcode() == BO_LOr)) |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 3994 | return Success(lhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 3995 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3996 | if (EvaluateAsBooleanCondition(E->getRHS(), rhsResult, Info)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3997 | if (E->getOpcode() == BO_LOr) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3998 | return Success(lhsResult || rhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 3999 | else |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4000 | return Success(lhsResult && rhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 4001 | } |
| 4002 | } else { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4003 | // FIXME: If both evaluations fail, we should produce the diagnostic from |
| 4004 | // the LHS. If the LHS is non-constant and the RHS is unevaluatable, it's |
| 4005 | // less clear how to diagnose this. |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4006 | if (EvaluateAsBooleanCondition(E->getRHS(), rhsResult, Info)) { |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 4007 | // We can't evaluate the LHS; however, sometimes the result |
| 4008 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4009 | if (rhsResult == (E->getOpcode() == BO_LOr)) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4010 | // Since we weren't able to evaluate the left hand side, it |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 4011 | // must have had side effects. |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 4012 | Info.EvalStatus.HasSideEffects = true; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4013 | |
| 4014 | return Success(rhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 4015 | } |
| 4016 | } |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 4017 | } |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 4018 | |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 4019 | return false; |
| 4020 | } |
| 4021 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4022 | QualType LHSTy = E->getLHS()->getType(); |
| 4023 | QualType RHSTy = E->getRHS()->getType(); |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4024 | |
| 4025 | if (LHSTy->isAnyComplexType()) { |
| 4026 | assert(RHSTy->isAnyComplexType() && "Invalid comparison"); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 4027 | ComplexValue LHS, RHS; |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4028 | |
| 4029 | if (!EvaluateComplex(E->getLHS(), LHS, Info)) |
| 4030 | return false; |
| 4031 | |
| 4032 | if (!EvaluateComplex(E->getRHS(), RHS, Info)) |
| 4033 | return false; |
| 4034 | |
| 4035 | if (LHS.isComplexFloat()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4036 | APFloat::cmpResult CR_r = |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4037 | LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4038 | APFloat::cmpResult CR_i = |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4039 | LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag()); |
| 4040 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4041 | if (E->getOpcode() == BO_EQ) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4042 | return Success((CR_r == APFloat::cmpEqual && |
| 4043 | CR_i == APFloat::cmpEqual), E); |
| 4044 | else { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4045 | assert(E->getOpcode() == BO_NE && |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4046 | "Invalid complex comparison."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4047 | return Success(((CR_r == APFloat::cmpGreaterThan || |
Mon P Wang | fc39dc4 | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 4048 | CR_r == APFloat::cmpLessThan || |
| 4049 | CR_r == APFloat::cmpUnordered) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4050 | (CR_i == APFloat::cmpGreaterThan || |
Mon P Wang | fc39dc4 | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 4051 | CR_i == APFloat::cmpLessThan || |
| 4052 | CR_i == APFloat::cmpUnordered)), E); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4053 | } |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4054 | } else { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4055 | if (E->getOpcode() == BO_EQ) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4056 | return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() && |
| 4057 | LHS.getComplexIntImag() == RHS.getComplexIntImag()), E); |
| 4058 | else { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4059 | assert(E->getOpcode() == BO_NE && |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4060 | "Invalid compex comparison."); |
| 4061 | return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() || |
| 4062 | LHS.getComplexIntImag() != RHS.getComplexIntImag()), E); |
| 4063 | } |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4064 | } |
| 4065 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4066 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4067 | if (LHSTy->isRealFloatingType() && |
| 4068 | RHSTy->isRealFloatingType()) { |
| 4069 | APFloat RHS(0.0), LHS(0.0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4070 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4071 | if (!EvaluateFloat(E->getRHS(), RHS, Info)) |
| 4072 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4073 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4074 | if (!EvaluateFloat(E->getLHS(), LHS, Info)) |
| 4075 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4076 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4077 | APFloat::cmpResult CR = LHS.compare(RHS); |
Anders Carlsson | 529569e | 2008-11-16 22:46:56 +0000 | [diff] [blame] | 4078 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4079 | switch (E->getOpcode()) { |
| 4080 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4081 | llvm_unreachable("Invalid binary operator!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4082 | case BO_LT: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4083 | return Success(CR == APFloat::cmpLessThan, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4084 | case BO_GT: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4085 | return Success(CR == APFloat::cmpGreaterThan, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4086 | case BO_LE: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4087 | return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4088 | case BO_GE: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4089 | return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual, |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4090 | E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4091 | case BO_EQ: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4092 | return Success(CR == APFloat::cmpEqual, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4093 | case BO_NE: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4094 | return Success(CR == APFloat::cmpGreaterThan |
Mon P Wang | fc39dc4 | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 4095 | || CR == APFloat::cmpLessThan |
| 4096 | || CR == APFloat::cmpUnordered, E); |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4097 | } |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4098 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4099 | |
Eli Friedman | ad02d7d | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 4100 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 4101 | if (E->getOpcode() == BO_Sub || E->isComparisonOp()) { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4102 | LValue LHSValue; |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4103 | if (!EvaluatePointer(E->getLHS(), LHSValue, Info)) |
| 4104 | return false; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 4105 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4106 | LValue RHSValue; |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4107 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info)) |
| 4108 | return false; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 4109 | |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 4110 | // Reject differing bases from the normal codepath; we special-case |
| 4111 | // comparisons to null. |
| 4112 | if (!HasSameBase(LHSValue, RHSValue)) { |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 4113 | if (E->getOpcode() == BO_Sub) { |
| 4114 | // Handle &&A - &&B. |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 4115 | if (!LHSValue.Offset.isZero() || !RHSValue.Offset.isZero()) |
| 4116 | return false; |
| 4117 | const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr*>(); |
| 4118 | const Expr *RHSExpr = LHSValue.Base.dyn_cast<const Expr*>(); |
| 4119 | if (!LHSExpr || !RHSExpr) |
| 4120 | return false; |
| 4121 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); |
| 4122 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); |
| 4123 | if (!LHSAddrExpr || !RHSAddrExpr) |
| 4124 | return false; |
Eli Friedman | 5930a4c | 2012-01-05 23:59:40 +0000 | [diff] [blame] | 4125 | // Make sure both labels come from the same function. |
| 4126 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
| 4127 | RHSAddrExpr->getLabel()->getDeclContext()) |
| 4128 | return false; |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 4129 | Result = CCValue(LHSAddrExpr, RHSAddrExpr); |
| 4130 | return true; |
| 4131 | } |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 4132 | // Inequalities and subtractions between unrelated pointers have |
| 4133 | // unspecified or undefined behavior. |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 4134 | if (!E->isEqualityOp()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4135 | return Error(E); |
Eli Friedman | ffbda40 | 2011-10-31 22:28:05 +0000 | [diff] [blame] | 4136 | // A constant address may compare equal to the address of a symbol. |
| 4137 | // The one exception is that address of an object cannot compare equal |
Eli Friedman | c45061b | 2011-10-31 22:54:30 +0000 | [diff] [blame] | 4138 | // to a null pointer constant. |
Eli Friedman | ffbda40 | 2011-10-31 22:28:05 +0000 | [diff] [blame] | 4139 | if ((!LHSValue.Base && !LHSValue.Offset.isZero()) || |
| 4140 | (!RHSValue.Base && !RHSValue.Offset.isZero())) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4141 | return Error(E); |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 4142 | // It's implementation-defined whether distinct literals will have |
Eli Friedman | c45061b | 2011-10-31 22:54:30 +0000 | [diff] [blame] | 4143 | // distinct addresses. In clang, we do not guarantee the addresses are |
Richard Smith | 74f4634 | 2011-11-04 01:10:57 +0000 | [diff] [blame] | 4144 | // distinct. However, we do know that the address of a literal will be |
| 4145 | // non-null. |
| 4146 | if ((IsLiteralLValue(LHSValue) || IsLiteralLValue(RHSValue)) && |
| 4147 | LHSValue.Base && RHSValue.Base) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4148 | return Error(E); |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 4149 | // We can't tell whether weak symbols will end up pointing to the same |
| 4150 | // object. |
| 4151 | if (IsWeakLValue(LHSValue) || IsWeakLValue(RHSValue)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4152 | return Error(E); |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 4153 | // Pointers with different bases cannot represent the same object. |
Eli Friedman | c45061b | 2011-10-31 22:54:30 +0000 | [diff] [blame] | 4154 | // (Note that clang defaults to -fmerge-all-constants, which can |
| 4155 | // lead to inconsistent results for comparisons involving the address |
| 4156 | // of a constant; this generally doesn't matter in practice.) |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 4157 | return Success(E->getOpcode() == BO_NE, E); |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 4158 | } |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 4159 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 4160 | // FIXME: Implement the C++11 restrictions: |
| 4161 | // - Pointer subtractions must be on elements of the same array. |
| 4162 | // - Pointer comparisons must be between members with the same access. |
| 4163 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4164 | if (E->getOpcode() == BO_Sub) { |
Chris Lattner | 4992bdd | 2010-04-20 17:13:14 +0000 | [diff] [blame] | 4165 | QualType Type = E->getLHS()->getType(); |
| 4166 | QualType ElementType = Type->getAs<PointerType>()->getPointeeType(); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4167 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4168 | CharUnits ElementSize; |
| 4169 | if (!HandleSizeof(Info, ElementType, ElementSize)) |
| 4170 | return false; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 4171 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4172 | CharUnits Diff = LHSValue.getLValueOffset() - |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 4173 | RHSValue.getLValueOffset(); |
| 4174 | return Success(Diff / ElementSize, E); |
Eli Friedman | ad02d7d | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 4175 | } |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 4176 | |
| 4177 | const CharUnits &LHSOffset = LHSValue.getLValueOffset(); |
| 4178 | const CharUnits &RHSOffset = RHSValue.getLValueOffset(); |
| 4179 | switch (E->getOpcode()) { |
| 4180 | default: llvm_unreachable("missing comparison operator"); |
| 4181 | case BO_LT: return Success(LHSOffset < RHSOffset, E); |
| 4182 | case BO_GT: return Success(LHSOffset > RHSOffset, E); |
| 4183 | case BO_LE: return Success(LHSOffset <= RHSOffset, E); |
| 4184 | case BO_GE: return Success(LHSOffset >= RHSOffset, E); |
| 4185 | case BO_EQ: return Success(LHSOffset == RHSOffset, E); |
| 4186 | case BO_NE: return Success(LHSOffset != RHSOffset, E); |
Eli Friedman | ad02d7d | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 4187 | } |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4188 | } |
| 4189 | } |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 4190 | if (!LHSTy->isIntegralOrEnumerationType() || |
| 4191 | !RHSTy->isIntegralOrEnumerationType()) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4192 | // We can't continue from here for non-integral types. |
| 4193 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 4194 | } |
| 4195 | |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 4196 | // The LHS of a constant expr is always evaluated and needed. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4197 | CCValue LHSVal; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4198 | if (!EvaluateIntegerOrLValue(E->getLHS(), LHSVal, Info)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4199 | return false; |
Eli Friedman | d9f4bcd | 2008-07-27 05:46:18 +0000 | [diff] [blame] | 4200 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4201 | if (!Visit(E->getRHS())) |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 4202 | return false; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4203 | CCValue &RHSVal = Result; |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 4204 | |
| 4205 | // Handle cases like (unsigned long)&a + 4. |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4206 | if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) { |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 4207 | CharUnits AdditionalOffset = CharUnits::fromQuantity( |
| 4208 | RHSVal.getInt().getZExtValue()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4209 | if (E->getOpcode() == BO_Add) |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4210 | LHSVal.getLValueOffset() += AdditionalOffset; |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 4211 | else |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4212 | LHSVal.getLValueOffset() -= AdditionalOffset; |
| 4213 | Result = LHSVal; |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 4214 | return true; |
| 4215 | } |
| 4216 | |
| 4217 | // Handle cases like 4 + (unsigned long)&a |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4218 | if (E->getOpcode() == BO_Add && |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4219 | RHSVal.isLValue() && LHSVal.isInt()) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4220 | RHSVal.getLValueOffset() += CharUnits::fromQuantity( |
| 4221 | LHSVal.getInt().getZExtValue()); |
| 4222 | // Note that RHSVal is Result. |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 4223 | return true; |
| 4224 | } |
| 4225 | |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 4226 | if (E->getOpcode() == BO_Sub && LHSVal.isLValue() && RHSVal.isLValue()) { |
| 4227 | // Handle (intptr_t)&&A - (intptr_t)&&B. |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 4228 | if (!LHSVal.getLValueOffset().isZero() || |
| 4229 | !RHSVal.getLValueOffset().isZero()) |
| 4230 | return false; |
| 4231 | const Expr *LHSExpr = LHSVal.getLValueBase().dyn_cast<const Expr*>(); |
| 4232 | const Expr *RHSExpr = RHSVal.getLValueBase().dyn_cast<const Expr*>(); |
| 4233 | if (!LHSExpr || !RHSExpr) |
| 4234 | return false; |
| 4235 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); |
| 4236 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); |
| 4237 | if (!LHSAddrExpr || !RHSAddrExpr) |
| 4238 | return false; |
Eli Friedman | 5930a4c | 2012-01-05 23:59:40 +0000 | [diff] [blame] | 4239 | // Make sure both labels come from the same function. |
| 4240 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
| 4241 | RHSAddrExpr->getLabel()->getDeclContext()) |
| 4242 | return false; |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 4243 | Result = CCValue(LHSAddrExpr, RHSAddrExpr); |
| 4244 | return true; |
| 4245 | } |
| 4246 | |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 4247 | // All the following cases expect both operands to be an integer |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4248 | if (!LHSVal.isInt() || !RHSVal.isInt()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4249 | return Error(E); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 4250 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4251 | APSInt &LHS = LHSVal.getInt(); |
| 4252 | APSInt &RHS = RHSVal.getInt(); |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 4253 | |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 4254 | switch (E->getOpcode()) { |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 4255 | default: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4256 | return Error(E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4257 | case BO_Mul: return Success(LHS * RHS, E); |
| 4258 | case BO_Add: return Success(LHS + RHS, E); |
| 4259 | case BO_Sub: return Success(LHS - RHS, E); |
| 4260 | case BO_And: return Success(LHS & RHS, E); |
| 4261 | case BO_Xor: return Success(LHS ^ RHS, E); |
| 4262 | case BO_Or: return Success(LHS | RHS, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4263 | case BO_Div: |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 4264 | if (RHS == 0) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4265 | return Error(E, diag::note_expr_divide_by_zero); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4266 | return Success(LHS / RHS, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4267 | case BO_Rem: |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 4268 | if (RHS == 0) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4269 | return Error(E, diag::note_expr_divide_by_zero); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4270 | return Success(LHS % RHS, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4271 | case BO_Shl: { |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 4272 | // During constant-folding, a negative shift is an opposite shift. |
| 4273 | if (RHS.isSigned() && RHS.isNegative()) { |
| 4274 | RHS = -RHS; |
| 4275 | goto shift_right; |
| 4276 | } |
| 4277 | |
| 4278 | shift_left: |
| 4279 | unsigned SA |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4280 | = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
| 4281 | return Success(LHS << SA, E); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 4282 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4283 | case BO_Shr: { |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 4284 | // During constant-folding, a negative shift is an opposite shift. |
| 4285 | if (RHS.isSigned() && RHS.isNegative()) { |
| 4286 | RHS = -RHS; |
| 4287 | goto shift_left; |
| 4288 | } |
| 4289 | |
| 4290 | shift_right: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4291 | unsigned SA = |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4292 | (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
| 4293 | return Success(LHS >> SA, E); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 4294 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4295 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4296 | case BO_LT: return Success(LHS < RHS, E); |
| 4297 | case BO_GT: return Success(LHS > RHS, E); |
| 4298 | case BO_LE: return Success(LHS <= RHS, E); |
| 4299 | case BO_GE: return Success(LHS >= RHS, E); |
| 4300 | case BO_EQ: return Success(LHS == RHS, E); |
| 4301 | case BO_NE: return Success(LHS != RHS, E); |
Eli Friedman | b11e778 | 2008-11-13 02:13:11 +0000 | [diff] [blame] | 4302 | } |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 4303 | } |
| 4304 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 4305 | CharUnits IntExprEvaluator::GetAlignOfType(QualType T) { |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 4306 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 4307 | // the result is the size of the referenced type." |
| 4308 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 4309 | // result shall be the alignment of the referenced type." |
| 4310 | if (const ReferenceType *Ref = T->getAs<ReferenceType>()) |
| 4311 | T = Ref->getPointeeType(); |
Chad Rosier | 9f1210c | 2011-07-26 07:03:04 +0000 | [diff] [blame] | 4312 | |
| 4313 | // __alignof is defined to return the preferred alignment. |
| 4314 | return Info.Ctx.toCharUnitsFromBits( |
| 4315 | Info.Ctx.getPreferredTypeAlign(T.getTypePtr())); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 4316 | } |
| 4317 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 4318 | CharUnits IntExprEvaluator::GetAlignOfExpr(const Expr *E) { |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 4319 | E = E->IgnoreParens(); |
| 4320 | |
| 4321 | // 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] | 4322 | // to 1 in those cases. |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 4323 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 4324 | return Info.Ctx.getDeclAlign(DRE->getDecl(), |
| 4325 | /*RefAsPointee*/true); |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 4326 | |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 4327 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 4328 | return Info.Ctx.getDeclAlign(ME->getMemberDecl(), |
| 4329 | /*RefAsPointee*/true); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 4330 | |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 4331 | return GetAlignOfType(E->getType()); |
| 4332 | } |
| 4333 | |
| 4334 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 4335 | /// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with |
| 4336 | /// a result as the expression's type. |
| 4337 | bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr( |
| 4338 | const UnaryExprOrTypeTraitExpr *E) { |
| 4339 | switch(E->getKind()) { |
| 4340 | case UETT_AlignOf: { |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 4341 | if (E->isArgumentType()) |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 4342 | return Success(GetAlignOfType(E->getArgumentType()), E); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 4343 | else |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 4344 | return Success(GetAlignOfExpr(E->getArgumentExpr()), E); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 4345 | } |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 4346 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 4347 | case UETT_VecStep: { |
| 4348 | QualType Ty = E->getTypeOfArgument(); |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 4349 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 4350 | if (Ty->isVectorType()) { |
| 4351 | unsigned n = Ty->getAs<VectorType>()->getNumElements(); |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 4352 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 4353 | // The vec_step built-in functions that take a 3-component |
| 4354 | // vector return 4. (OpenCL 1.1 spec 6.11.12) |
| 4355 | if (n == 3) |
| 4356 | n = 4; |
Eli Friedman | f2da9df | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 4357 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 4358 | return Success(n, E); |
| 4359 | } else |
| 4360 | return Success(1, E); |
| 4361 | } |
| 4362 | |
| 4363 | case UETT_SizeOf: { |
| 4364 | QualType SrcTy = E->getTypeOfArgument(); |
| 4365 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 4366 | // the result is the size of the referenced type." |
| 4367 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 4368 | // result shall be the alignment of the referenced type." |
| 4369 | if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>()) |
| 4370 | SrcTy = Ref->getPointeeType(); |
| 4371 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4372 | CharUnits Sizeof; |
| 4373 | if (!HandleSizeof(Info, SrcTy, Sizeof)) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 4374 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4375 | return Success(Sizeof, E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 4376 | } |
| 4377 | } |
| 4378 | |
| 4379 | llvm_unreachable("unknown expr/type trait"); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4380 | return Error(E); |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 4381 | } |
| 4382 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4383 | bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) { |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 4384 | CharUnits Result; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4385 | unsigned n = OOE->getNumComponents(); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 4386 | if (n == 0) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4387 | return Error(OOE); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4388 | QualType CurrentType = OOE->getTypeSourceInfo()->getType(); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 4389 | for (unsigned i = 0; i != n; ++i) { |
| 4390 | OffsetOfExpr::OffsetOfNode ON = OOE->getComponent(i); |
| 4391 | switch (ON.getKind()) { |
| 4392 | case OffsetOfExpr::OffsetOfNode::Array: { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4393 | const Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex()); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 4394 | APSInt IdxResult; |
| 4395 | if (!EvaluateInteger(Idx, IdxResult, Info)) |
| 4396 | return false; |
| 4397 | const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType); |
| 4398 | if (!AT) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4399 | return Error(OOE); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 4400 | CurrentType = AT->getElementType(); |
| 4401 | CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType); |
| 4402 | Result += IdxResult.getSExtValue() * ElementSize; |
| 4403 | break; |
| 4404 | } |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4405 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 4406 | case OffsetOfExpr::OffsetOfNode::Field: { |
| 4407 | FieldDecl *MemberDecl = ON.getField(); |
| 4408 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4409 | if (!RT) |
| 4410 | return Error(OOE); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 4411 | RecordDecl *RD = RT->getDecl(); |
| 4412 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 4413 | unsigned i = MemberDecl->getFieldIndex(); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 4414 | assert(i < RL.getFieldCount() && "offsetof field in wrong type"); |
Ken Dyck | fb1e3bc | 2011-01-18 01:56:16 +0000 | [diff] [blame] | 4415 | Result += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i)); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 4416 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 4417 | break; |
| 4418 | } |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4419 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 4420 | case OffsetOfExpr::OffsetOfNode::Identifier: |
| 4421 | llvm_unreachable("dependent __builtin_offsetof"); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4422 | return Error(OOE); |
| 4423 | |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 4424 | case OffsetOfExpr::OffsetOfNode::Base: { |
| 4425 | CXXBaseSpecifier *BaseSpec = ON.getBase(); |
| 4426 | if (BaseSpec->isVirtual()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4427 | return Error(OOE); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 4428 | |
| 4429 | // Find the layout of the class whose base we are looking into. |
| 4430 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4431 | if (!RT) |
| 4432 | return Error(OOE); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 4433 | RecordDecl *RD = RT->getDecl(); |
| 4434 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
| 4435 | |
| 4436 | // Find the base class itself. |
| 4437 | CurrentType = BaseSpec->getType(); |
| 4438 | const RecordType *BaseRT = CurrentType->getAs<RecordType>(); |
| 4439 | if (!BaseRT) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4440 | return Error(OOE); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 4441 | |
| 4442 | // Add the offset to the base. |
Ken Dyck | 7c7f820 | 2011-01-26 02:17:08 +0000 | [diff] [blame] | 4443 | Result += RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl())); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 4444 | break; |
| 4445 | } |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 4446 | } |
| 4447 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4448 | return Success(Result, OOE); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 4449 | } |
| 4450 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 4451 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4452 | switch (E->getOpcode()) { |
| 4453 | default: |
| 4454 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 4455 | // See C99 6.6p3. |
| 4456 | return Error(E); |
| 4457 | case UO_Extension: |
| 4458 | // FIXME: Should extension allow i-c-e extension expressions in its scope? |
| 4459 | // If so, we could clear the diagnostic ID. |
| 4460 | return Visit(E->getSubExpr()); |
| 4461 | case UO_Plus: |
| 4462 | // The result is just the value. |
| 4463 | return Visit(E->getSubExpr()); |
| 4464 | case UO_Minus: { |
| 4465 | if (!Visit(E->getSubExpr())) |
| 4466 | return false; |
| 4467 | if (!Result.isInt()) return Error(E); |
| 4468 | return Success(-Result.getInt(), E); |
| 4469 | } |
| 4470 | case UO_Not: { |
| 4471 | if (!Visit(E->getSubExpr())) |
| 4472 | return false; |
| 4473 | if (!Result.isInt()) return Error(E); |
| 4474 | return Success(~Result.getInt(), E); |
| 4475 | } |
| 4476 | case UO_LNot: { |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 4477 | bool bres; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4478 | if (!EvaluateAsBooleanCondition(E->getSubExpr(), bres, Info)) |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 4479 | return false; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4480 | return Success(!bres, E); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 4481 | } |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 4482 | } |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 4483 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4484 | |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 4485 | /// HandleCast - This is used to evaluate implicit or explicit casts where the |
| 4486 | /// result type is integer. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4487 | bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 4488 | const Expr *SubExpr = E->getSubExpr(); |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 4489 | QualType DestType = E->getType(); |
Daniel Dunbar | b92dac8 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 4490 | QualType SrcType = SubExpr->getType(); |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 4491 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4492 | switch (E->getCastKind()) { |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4493 | case CK_BaseToDerived: |
| 4494 | case CK_DerivedToBase: |
| 4495 | case CK_UncheckedDerivedToBase: |
| 4496 | case CK_Dynamic: |
| 4497 | case CK_ToUnion: |
| 4498 | case CK_ArrayToPointerDecay: |
| 4499 | case CK_FunctionToPointerDecay: |
| 4500 | case CK_NullToPointer: |
| 4501 | case CK_NullToMemberPointer: |
| 4502 | case CK_BaseToDerivedMemberPointer: |
| 4503 | case CK_DerivedToBaseMemberPointer: |
| 4504 | case CK_ConstructorConversion: |
| 4505 | case CK_IntegralToPointer: |
| 4506 | case CK_ToVoid: |
| 4507 | case CK_VectorSplat: |
| 4508 | case CK_IntegralToFloating: |
| 4509 | case CK_FloatingCast: |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4510 | case CK_CPointerToObjCPointerCast: |
| 4511 | case CK_BlockPointerToObjCPointerCast: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4512 | case CK_AnyPointerToBlockPointerCast: |
| 4513 | case CK_ObjCObjectLValueCast: |
| 4514 | case CK_FloatingRealToComplex: |
| 4515 | case CK_FloatingComplexToReal: |
| 4516 | case CK_FloatingComplexCast: |
| 4517 | case CK_FloatingComplexToIntegralComplex: |
| 4518 | case CK_IntegralRealToComplex: |
| 4519 | case CK_IntegralComplexCast: |
| 4520 | case CK_IntegralComplexToFloatingComplex: |
| 4521 | llvm_unreachable("invalid cast kind for integral value"); |
| 4522 | |
Eli Friedman | e50c297 | 2011-03-25 19:07:11 +0000 | [diff] [blame] | 4523 | case CK_BitCast: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4524 | case CK_Dependent: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4525 | case CK_LValueBitCast: |
| 4526 | case CK_UserDefinedConversion: |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 4527 | case CK_ARCProduceObject: |
| 4528 | case CK_ARCConsumeObject: |
| 4529 | case CK_ARCReclaimReturnedObject: |
| 4530 | case CK_ARCExtendBlockObject: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4531 | return Error(E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4532 | |
| 4533 | case CK_LValueToRValue: |
| 4534 | case CK_NoOp: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4535 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4536 | |
| 4537 | case CK_MemberPointerToBoolean: |
| 4538 | case CK_PointerToBoolean: |
| 4539 | case CK_IntegralToBoolean: |
| 4540 | case CK_FloatingToBoolean: |
| 4541 | case CK_FloatingComplexToBoolean: |
| 4542 | case CK_IntegralComplexToBoolean: { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4543 | bool BoolResult; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4544 | if (!EvaluateAsBooleanCondition(SubExpr, BoolResult, Info)) |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4545 | return false; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4546 | return Success(BoolResult, E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4547 | } |
| 4548 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4549 | case CK_IntegralCast: { |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 4550 | if (!Visit(SubExpr)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 4551 | return false; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 4552 | |
Eli Friedman | be26570 | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 4553 | if (!Result.isInt()) { |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 4554 | // Allow casts of address-of-label differences if they are no-ops |
| 4555 | // or narrowing. (The narrowing case isn't actually guaranteed to |
| 4556 | // be constant-evaluatable except in some narrow cases which are hard |
| 4557 | // to detect here. We let it through on the assumption the user knows |
| 4558 | // what they are doing.) |
| 4559 | if (Result.isAddrLabelDiff()) |
| 4560 | return Info.Ctx.getTypeSize(DestType) <= Info.Ctx.getTypeSize(SrcType); |
Eli Friedman | be26570 | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 4561 | // Only allow casts of lvalues if they are lossless. |
| 4562 | return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType); |
| 4563 | } |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 4564 | |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 4565 | return Success(HandleIntToIntCast(DestType, SrcType, |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 4566 | Result.getInt(), Info.Ctx), E); |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 4567 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4568 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4569 | case CK_PointerToIntegral: { |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 4570 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 4571 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4572 | LValue LV; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 4573 | if (!EvaluatePointer(SubExpr, LV, Info)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 4574 | return false; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4575 | |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 4576 | if (LV.getLValueBase()) { |
| 4577 | // Only allow based lvalue casts if they are lossless. |
| 4578 | if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4579 | return Error(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4580 | |
Richard Smith | b755a9d | 2011-11-16 07:18:12 +0000 | [diff] [blame] | 4581 | LV.Designator.setInvalid(); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4582 | LV.moveInto(Result); |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 4583 | return true; |
| 4584 | } |
| 4585 | |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 4586 | APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(), |
| 4587 | SrcType); |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 4588 | return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E); |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 4589 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4590 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4591 | case CK_IntegralComplexToReal: { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 4592 | ComplexValue C; |
Eli Friedman | 1725f68 | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 4593 | if (!EvaluateComplex(SubExpr, C, Info)) |
| 4594 | return false; |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4595 | return Success(C.getComplexIntReal(), E); |
Eli Friedman | 1725f68 | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 4596 | } |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 4597 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4598 | case CK_FloatingToIntegral: { |
| 4599 | APFloat F(0.0); |
| 4600 | if (!EvaluateFloat(SubExpr, F, Info)) |
| 4601 | return false; |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 4602 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4603 | APSInt Value; |
| 4604 | if (!HandleFloatToIntCast(Info, E, SrcType, F, DestType, Value)) |
| 4605 | return false; |
| 4606 | return Success(Value, E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4607 | } |
| 4608 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4609 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4610 | llvm_unreachable("unknown cast resulting in integral value"); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4611 | return Error(E); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 4612 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 4613 | |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 4614 | bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| 4615 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 4616 | ComplexValue LV; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4617 | if (!EvaluateComplex(E->getSubExpr(), LV, Info)) |
| 4618 | return false; |
| 4619 | if (!LV.isComplexInt()) |
| 4620 | return Error(E); |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 4621 | return Success(LV.getComplexIntReal(), E); |
| 4622 | } |
| 4623 | |
| 4624 | return Visit(E->getSubExpr()); |
| 4625 | } |
| 4626 | |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 4627 | bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 4628 | if (E->getSubExpr()->getType()->isComplexIntegerType()) { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 4629 | ComplexValue LV; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4630 | if (!EvaluateComplex(E->getSubExpr(), LV, Info)) |
| 4631 | return false; |
| 4632 | if (!LV.isComplexInt()) |
| 4633 | return Error(E); |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 4634 | return Success(LV.getComplexIntImag(), E); |
| 4635 | } |
| 4636 | |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 4637 | VisitIgnoredValue(E->getSubExpr()); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 4638 | return Success(0, E); |
| 4639 | } |
| 4640 | |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 4641 | bool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) { |
| 4642 | return Success(E->getPackLength(), E); |
| 4643 | } |
| 4644 | |
Sebastian Redl | 295995c | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 4645 | bool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) { |
| 4646 | return Success(E->getValue(), E); |
| 4647 | } |
| 4648 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4649 | //===----------------------------------------------------------------------===// |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 4650 | // Float Evaluation |
| 4651 | //===----------------------------------------------------------------------===// |
| 4652 | |
| 4653 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 4654 | class FloatExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4655 | : public ExprEvaluatorBase<FloatExprEvaluator, bool> { |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 4656 | APFloat &Result; |
| 4657 | public: |
| 4658 | FloatExprEvaluator(EvalInfo &info, APFloat &result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4659 | : ExprEvaluatorBaseTy(info), Result(result) {} |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 4660 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4661 | bool Success(const CCValue &V, const Expr *e) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4662 | Result = V.getFloat(); |
| 4663 | return true; |
| 4664 | } |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 4665 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4666 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4667 | Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType())); |
| 4668 | return true; |
| 4669 | } |
| 4670 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 4671 | bool VisitCallExpr(const CallExpr *E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 4672 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 4673 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 4674 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 4675 | bool VisitFloatingLiteral(const FloatingLiteral *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4676 | bool VisitCastExpr(const CastExpr *E); |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 4677 | |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 4678 | bool VisitUnaryReal(const UnaryOperator *E); |
| 4679 | bool VisitUnaryImag(const UnaryOperator *E); |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 4680 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4681 | // FIXME: Missing: array subscript of vector, member of vector |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 4682 | }; |
| 4683 | } // end anonymous namespace |
| 4684 | |
| 4685 | static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4686 | assert(E->isRValue() && E->getType()->isRealFloatingType()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4687 | return FloatExprEvaluator(Info, Result).Visit(E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 4688 | } |
| 4689 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4690 | static bool TryEvaluateBuiltinNaN(const ASTContext &Context, |
John McCall | db7b72a | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 4691 | QualType ResultTy, |
| 4692 | const Expr *Arg, |
| 4693 | bool SNaN, |
| 4694 | llvm::APFloat &Result) { |
| 4695 | const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts()); |
| 4696 | if (!S) return false; |
| 4697 | |
| 4698 | const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy); |
| 4699 | |
| 4700 | llvm::APInt fill; |
| 4701 | |
| 4702 | // Treat empty strings as if they were zero. |
| 4703 | if (S->getString().empty()) |
| 4704 | fill = llvm::APInt(32, 0); |
| 4705 | else if (S->getString().getAsInteger(0, fill)) |
| 4706 | return false; |
| 4707 | |
| 4708 | if (SNaN) |
| 4709 | Result = llvm::APFloat::getSNaN(Sem, false, &fill); |
| 4710 | else |
| 4711 | Result = llvm::APFloat::getQNaN(Sem, false, &fill); |
| 4712 | return true; |
| 4713 | } |
| 4714 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 4715 | bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4716 | switch (E->isBuiltinCall()) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4717 | default: |
| 4718 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
| 4719 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 4720 | case Builtin::BI__builtin_huge_val: |
| 4721 | case Builtin::BI__builtin_huge_valf: |
| 4722 | case Builtin::BI__builtin_huge_vall: |
| 4723 | case Builtin::BI__builtin_inf: |
| 4724 | case Builtin::BI__builtin_inff: |
Daniel Dunbar | 7cbed03 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 4725 | case Builtin::BI__builtin_infl: { |
| 4726 | const llvm::fltSemantics &Sem = |
| 4727 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Chris Lattner | 34a74ab | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 4728 | Result = llvm::APFloat::getInf(Sem); |
| 4729 | return true; |
Daniel Dunbar | 7cbed03 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 4730 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4731 | |
John McCall | db7b72a | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 4732 | case Builtin::BI__builtin_nans: |
| 4733 | case Builtin::BI__builtin_nansf: |
| 4734 | case Builtin::BI__builtin_nansl: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4735 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 4736 | true, Result)) |
| 4737 | return Error(E); |
| 4738 | return true; |
John McCall | db7b72a | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 4739 | |
Chris Lattner | 9e62171 | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 4740 | case Builtin::BI__builtin_nan: |
| 4741 | case Builtin::BI__builtin_nanf: |
| 4742 | case Builtin::BI__builtin_nanl: |
Mike Stump | 4572bab | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 4743 | // If this is __builtin_nan() turn this into a nan, otherwise we |
Chris Lattner | 9e62171 | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 4744 | // can't constant fold it. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4745 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 4746 | false, Result)) |
| 4747 | return Error(E); |
| 4748 | return true; |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 4749 | |
| 4750 | case Builtin::BI__builtin_fabs: |
| 4751 | case Builtin::BI__builtin_fabsf: |
| 4752 | case Builtin::BI__builtin_fabsl: |
| 4753 | if (!EvaluateFloat(E->getArg(0), Result, Info)) |
| 4754 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4755 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 4756 | if (Result.isNegative()) |
| 4757 | Result.changeSign(); |
| 4758 | return true; |
| 4759 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4760 | case Builtin::BI__builtin_copysign: |
| 4761 | case Builtin::BI__builtin_copysignf: |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 4762 | case Builtin::BI__builtin_copysignl: { |
| 4763 | APFloat RHS(0.); |
| 4764 | if (!EvaluateFloat(E->getArg(0), Result, Info) || |
| 4765 | !EvaluateFloat(E->getArg(1), RHS, Info)) |
| 4766 | return false; |
| 4767 | Result.copySign(RHS); |
| 4768 | return true; |
| 4769 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 4770 | } |
| 4771 | } |
| 4772 | |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 4773 | bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
Eli Friedman | 43efa31 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 4774 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 4775 | ComplexValue CV; |
| 4776 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 4777 | return false; |
| 4778 | Result = CV.FloatReal; |
| 4779 | return true; |
| 4780 | } |
| 4781 | |
| 4782 | return Visit(E->getSubExpr()); |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 4783 | } |
| 4784 | |
| 4785 | bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | 43efa31 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 4786 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 4787 | ComplexValue CV; |
| 4788 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 4789 | return false; |
| 4790 | Result = CV.FloatImag; |
| 4791 | return true; |
| 4792 | } |
| 4793 | |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 4794 | VisitIgnoredValue(E->getSubExpr()); |
Eli Friedman | 43efa31 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 4795 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(E->getType()); |
| 4796 | Result = llvm::APFloat::getZero(Sem); |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 4797 | return true; |
| 4798 | } |
| 4799 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 4800 | bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 4801 | switch (E->getOpcode()) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4802 | default: return Error(E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4803 | case UO_Plus: |
Richard Smith | 7993e8a | 2011-10-30 23:17:09 +0000 | [diff] [blame] | 4804 | return EvaluateFloat(E->getSubExpr(), Result, Info); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4805 | case UO_Minus: |
Richard Smith | 7993e8a | 2011-10-30 23:17:09 +0000 | [diff] [blame] | 4806 | if (!EvaluateFloat(E->getSubExpr(), Result, Info)) |
| 4807 | return false; |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 4808 | Result.changeSign(); |
| 4809 | return true; |
| 4810 | } |
| 4811 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 4812 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 4813 | bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4814 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
| 4815 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Eli Friedman | 7f92f03 | 2009-11-16 04:25:37 +0000 | [diff] [blame] | 4816 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 4817 | APFloat RHS(0.0); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 4818 | if (!EvaluateFloat(E->getLHS(), Result, Info)) |
| 4819 | return false; |
| 4820 | if (!EvaluateFloat(E->getRHS(), RHS, Info)) |
| 4821 | return false; |
| 4822 | |
| 4823 | switch (E->getOpcode()) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4824 | default: return Error(E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4825 | case BO_Mul: |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 4826 | Result.multiply(RHS, APFloat::rmNearestTiesToEven); |
| 4827 | return true; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4828 | case BO_Add: |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 4829 | Result.add(RHS, APFloat::rmNearestTiesToEven); |
| 4830 | return true; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4831 | case BO_Sub: |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 4832 | Result.subtract(RHS, APFloat::rmNearestTiesToEven); |
| 4833 | return true; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4834 | case BO_Div: |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 4835 | Result.divide(RHS, APFloat::rmNearestTiesToEven); |
| 4836 | return true; |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 4837 | } |
| 4838 | } |
| 4839 | |
| 4840 | bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { |
| 4841 | Result = E->getValue(); |
| 4842 | return true; |
| 4843 | } |
| 4844 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4845 | bool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 4846 | const Expr* SubExpr = E->getSubExpr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4847 | |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 4848 | switch (E->getCastKind()) { |
| 4849 | default: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4850 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 4851 | |
| 4852 | case CK_IntegralToFloating: { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4853 | APSInt IntResult; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4854 | return EvaluateInteger(SubExpr, IntResult, Info) && |
| 4855 | HandleIntToFloatCast(Info, E, SubExpr->getType(), IntResult, |
| 4856 | E->getType(), Result); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4857 | } |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 4858 | |
| 4859 | case CK_FloatingCast: { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4860 | if (!Visit(SubExpr)) |
| 4861 | return false; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4862 | return HandleFloatToFloatCast(Info, E, SubExpr->getType(), E->getType(), |
| 4863 | Result); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4864 | } |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4865 | |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 4866 | case CK_FloatingComplexToReal: { |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4867 | ComplexValue V; |
| 4868 | if (!EvaluateComplex(SubExpr, V, Info)) |
| 4869 | return false; |
| 4870 | Result = V.getComplexFloatReal(); |
| 4871 | return true; |
| 4872 | } |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 4873 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4874 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4875 | return Error(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4876 | } |
| 4877 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 4878 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 4879 | // Complex Evaluation (for float and integer) |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 4880 | //===----------------------------------------------------------------------===// |
| 4881 | |
| 4882 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 4883 | class ComplexExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4884 | : public ExprEvaluatorBase<ComplexExprEvaluator, bool> { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 4885 | ComplexValue &Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4886 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 4887 | public: |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 4888 | ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4889 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
| 4890 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4891 | bool Success(const CCValue &V, const Expr *e) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4892 | Result.setFrom(V); |
| 4893 | return true; |
| 4894 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4895 | |
Eli Friedman | 7ead5c7 | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 4896 | bool ZeroInitialization(const Expr *E); |
| 4897 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 4898 | //===--------------------------------------------------------------------===// |
| 4899 | // Visitor Methods |
| 4900 | //===--------------------------------------------------------------------===// |
| 4901 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4902 | bool VisitImaginaryLiteral(const ImaginaryLiteral *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4903 | bool VisitCastExpr(const CastExpr *E); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 4904 | bool VisitBinaryOperator(const BinaryOperator *E); |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 4905 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | 7ead5c7 | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 4906 | bool VisitInitListExpr(const InitListExpr *E); |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 4907 | }; |
| 4908 | } // end anonymous namespace |
| 4909 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 4910 | static bool EvaluateComplex(const Expr *E, ComplexValue &Result, |
| 4911 | EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4912 | assert(E->isRValue() && E->getType()->isAnyComplexType()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4913 | return ComplexExprEvaluator(Info, Result).Visit(E); |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 4914 | } |
| 4915 | |
Eli Friedman | 7ead5c7 | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 4916 | bool ComplexExprEvaluator::ZeroInitialization(const Expr *E) { |
Eli Friedman | f6c17a4 | 2012-01-13 23:34:56 +0000 | [diff] [blame] | 4917 | QualType ElemTy = E->getType()->getAs<ComplexType>()->getElementType(); |
Eli Friedman | 7ead5c7 | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 4918 | if (ElemTy->isRealFloatingType()) { |
| 4919 | Result.makeComplexFloat(); |
| 4920 | APFloat Zero = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(ElemTy)); |
| 4921 | Result.FloatReal = Zero; |
| 4922 | Result.FloatImag = Zero; |
| 4923 | } else { |
| 4924 | Result.makeComplexInt(); |
| 4925 | APSInt Zero = Info.Ctx.MakeIntValue(0, ElemTy); |
| 4926 | Result.IntReal = Zero; |
| 4927 | Result.IntImag = Zero; |
| 4928 | } |
| 4929 | return true; |
| 4930 | } |
| 4931 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4932 | bool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) { |
| 4933 | const Expr* SubExpr = E->getSubExpr(); |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 4934 | |
| 4935 | if (SubExpr->getType()->isRealFloatingType()) { |
| 4936 | Result.makeComplexFloat(); |
| 4937 | APFloat &Imag = Result.FloatImag; |
| 4938 | if (!EvaluateFloat(SubExpr, Imag, Info)) |
| 4939 | return false; |
| 4940 | |
| 4941 | Result.FloatReal = APFloat(Imag.getSemantics()); |
| 4942 | return true; |
| 4943 | } else { |
| 4944 | assert(SubExpr->getType()->isIntegerType() && |
| 4945 | "Unexpected imaginary literal."); |
| 4946 | |
| 4947 | Result.makeComplexInt(); |
| 4948 | APSInt &Imag = Result.IntImag; |
| 4949 | if (!EvaluateInteger(SubExpr, Imag, Info)) |
| 4950 | return false; |
| 4951 | |
| 4952 | Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned()); |
| 4953 | return true; |
| 4954 | } |
| 4955 | } |
| 4956 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4957 | bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) { |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 4958 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4959 | switch (E->getCastKind()) { |
| 4960 | case CK_BitCast: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4961 | case CK_BaseToDerived: |
| 4962 | case CK_DerivedToBase: |
| 4963 | case CK_UncheckedDerivedToBase: |
| 4964 | case CK_Dynamic: |
| 4965 | case CK_ToUnion: |
| 4966 | case CK_ArrayToPointerDecay: |
| 4967 | case CK_FunctionToPointerDecay: |
| 4968 | case CK_NullToPointer: |
| 4969 | case CK_NullToMemberPointer: |
| 4970 | case CK_BaseToDerivedMemberPointer: |
| 4971 | case CK_DerivedToBaseMemberPointer: |
| 4972 | case CK_MemberPointerToBoolean: |
| 4973 | case CK_ConstructorConversion: |
| 4974 | case CK_IntegralToPointer: |
| 4975 | case CK_PointerToIntegral: |
| 4976 | case CK_PointerToBoolean: |
| 4977 | case CK_ToVoid: |
| 4978 | case CK_VectorSplat: |
| 4979 | case CK_IntegralCast: |
| 4980 | case CK_IntegralToBoolean: |
| 4981 | case CK_IntegralToFloating: |
| 4982 | case CK_FloatingToIntegral: |
| 4983 | case CK_FloatingToBoolean: |
| 4984 | case CK_FloatingCast: |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4985 | case CK_CPointerToObjCPointerCast: |
| 4986 | case CK_BlockPointerToObjCPointerCast: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4987 | case CK_AnyPointerToBlockPointerCast: |
| 4988 | case CK_ObjCObjectLValueCast: |
| 4989 | case CK_FloatingComplexToReal: |
| 4990 | case CK_FloatingComplexToBoolean: |
| 4991 | case CK_IntegralComplexToReal: |
| 4992 | case CK_IntegralComplexToBoolean: |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 4993 | case CK_ARCProduceObject: |
| 4994 | case CK_ARCConsumeObject: |
| 4995 | case CK_ARCReclaimReturnedObject: |
| 4996 | case CK_ARCExtendBlockObject: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4997 | llvm_unreachable("invalid cast kind for complex value"); |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 4998 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4999 | case CK_LValueToRValue: |
| 5000 | case CK_NoOp: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5001 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5002 | |
| 5003 | case CK_Dependent: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5004 | case CK_LValueBitCast: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5005 | case CK_UserDefinedConversion: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5006 | return Error(E); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5007 | |
| 5008 | case CK_FloatingRealToComplex: { |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 5009 | APFloat &Real = Result.FloatReal; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5010 | if (!EvaluateFloat(E->getSubExpr(), Real, Info)) |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 5011 | return false; |
| 5012 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5013 | Result.makeComplexFloat(); |
| 5014 | Result.FloatImag = APFloat(Real.getSemantics()); |
| 5015 | return true; |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 5016 | } |
| 5017 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5018 | case CK_FloatingComplexCast: { |
| 5019 | if (!Visit(E->getSubExpr())) |
| 5020 | return false; |
| 5021 | |
| 5022 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 5023 | QualType From |
| 5024 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 5025 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5026 | return HandleFloatToFloatCast(Info, E, From, To, Result.FloatReal) && |
| 5027 | HandleFloatToFloatCast(Info, E, From, To, Result.FloatImag); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5028 | } |
| 5029 | |
| 5030 | case CK_FloatingComplexToIntegralComplex: { |
| 5031 | if (!Visit(E->getSubExpr())) |
| 5032 | return false; |
| 5033 | |
| 5034 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 5035 | QualType From |
| 5036 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 5037 | Result.makeComplexInt(); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5038 | return HandleFloatToIntCast(Info, E, From, Result.FloatReal, |
| 5039 | To, Result.IntReal) && |
| 5040 | HandleFloatToIntCast(Info, E, From, Result.FloatImag, |
| 5041 | To, Result.IntImag); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5042 | } |
| 5043 | |
| 5044 | case CK_IntegralRealToComplex: { |
| 5045 | APSInt &Real = Result.IntReal; |
| 5046 | if (!EvaluateInteger(E->getSubExpr(), Real, Info)) |
| 5047 | return false; |
| 5048 | |
| 5049 | Result.makeComplexInt(); |
| 5050 | Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned()); |
| 5051 | return true; |
| 5052 | } |
| 5053 | |
| 5054 | case CK_IntegralComplexCast: { |
| 5055 | if (!Visit(E->getSubExpr())) |
| 5056 | return false; |
| 5057 | |
| 5058 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 5059 | QualType From |
| 5060 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 5061 | |
| 5062 | Result.IntReal = HandleIntToIntCast(To, From, Result.IntReal, Info.Ctx); |
| 5063 | Result.IntImag = HandleIntToIntCast(To, From, Result.IntImag, Info.Ctx); |
| 5064 | return true; |
| 5065 | } |
| 5066 | |
| 5067 | case CK_IntegralComplexToFloatingComplex: { |
| 5068 | if (!Visit(E->getSubExpr())) |
| 5069 | return false; |
| 5070 | |
| 5071 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 5072 | QualType From |
| 5073 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 5074 | Result.makeComplexFloat(); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5075 | return HandleIntToFloatCast(Info, E, From, Result.IntReal, |
| 5076 | To, Result.FloatReal) && |
| 5077 | HandleIntToFloatCast(Info, E, From, Result.IntImag, |
| 5078 | To, Result.FloatImag); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5079 | } |
| 5080 | } |
| 5081 | |
| 5082 | llvm_unreachable("unknown cast resulting in complex value"); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5083 | return Error(E); |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 5084 | } |
| 5085 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5086 | bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5087 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
Richard Smith | 2ad226b | 2011-11-16 17:22:48 +0000 | [diff] [blame] | 5088 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| 5089 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5090 | if (!Visit(E->getLHS())) |
| 5091 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5092 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5093 | ComplexValue RHS; |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 5094 | if (!EvaluateComplex(E->getRHS(), RHS, Info)) |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5095 | return false; |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 5096 | |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 5097 | assert(Result.isComplexFloat() == RHS.isComplexFloat() && |
| 5098 | "Invalid operands to binary operator."); |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 5099 | switch (E->getOpcode()) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5100 | default: return Error(E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5101 | case BO_Add: |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 5102 | if (Result.isComplexFloat()) { |
| 5103 | Result.getComplexFloatReal().add(RHS.getComplexFloatReal(), |
| 5104 | APFloat::rmNearestTiesToEven); |
| 5105 | Result.getComplexFloatImag().add(RHS.getComplexFloatImag(), |
| 5106 | APFloat::rmNearestTiesToEven); |
| 5107 | } else { |
| 5108 | Result.getComplexIntReal() += RHS.getComplexIntReal(); |
| 5109 | Result.getComplexIntImag() += RHS.getComplexIntImag(); |
| 5110 | } |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 5111 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5112 | case BO_Sub: |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 5113 | if (Result.isComplexFloat()) { |
| 5114 | Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(), |
| 5115 | APFloat::rmNearestTiesToEven); |
| 5116 | Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(), |
| 5117 | APFloat::rmNearestTiesToEven); |
| 5118 | } else { |
| 5119 | Result.getComplexIntReal() -= RHS.getComplexIntReal(); |
| 5120 | Result.getComplexIntImag() -= RHS.getComplexIntImag(); |
| 5121 | } |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 5122 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5123 | case BO_Mul: |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 5124 | if (Result.isComplexFloat()) { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5125 | ComplexValue LHS = Result; |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 5126 | APFloat &LHS_r = LHS.getComplexFloatReal(); |
| 5127 | APFloat &LHS_i = LHS.getComplexFloatImag(); |
| 5128 | APFloat &RHS_r = RHS.getComplexFloatReal(); |
| 5129 | APFloat &RHS_i = RHS.getComplexFloatImag(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5130 | |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 5131 | APFloat Tmp = LHS_r; |
| 5132 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 5133 | Result.getComplexFloatReal() = Tmp; |
| 5134 | Tmp = LHS_i; |
| 5135 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 5136 | Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven); |
| 5137 | |
| 5138 | Tmp = LHS_r; |
| 5139 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 5140 | Result.getComplexFloatImag() = Tmp; |
| 5141 | Tmp = LHS_i; |
| 5142 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 5143 | Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven); |
| 5144 | } else { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5145 | ComplexValue LHS = Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5146 | Result.getComplexIntReal() = |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 5147 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() - |
| 5148 | LHS.getComplexIntImag() * RHS.getComplexIntImag()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5149 | Result.getComplexIntImag() = |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 5150 | (LHS.getComplexIntReal() * RHS.getComplexIntImag() + |
| 5151 | LHS.getComplexIntImag() * RHS.getComplexIntReal()); |
| 5152 | } |
| 5153 | break; |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 5154 | case BO_Div: |
| 5155 | if (Result.isComplexFloat()) { |
| 5156 | ComplexValue LHS = Result; |
| 5157 | APFloat &LHS_r = LHS.getComplexFloatReal(); |
| 5158 | APFloat &LHS_i = LHS.getComplexFloatImag(); |
| 5159 | APFloat &RHS_r = RHS.getComplexFloatReal(); |
| 5160 | APFloat &RHS_i = RHS.getComplexFloatImag(); |
| 5161 | APFloat &Res_r = Result.getComplexFloatReal(); |
| 5162 | APFloat &Res_i = Result.getComplexFloatImag(); |
| 5163 | |
| 5164 | APFloat Den = RHS_r; |
| 5165 | Den.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 5166 | APFloat Tmp = RHS_i; |
| 5167 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 5168 | Den.add(Tmp, APFloat::rmNearestTiesToEven); |
| 5169 | |
| 5170 | Res_r = LHS_r; |
| 5171 | Res_r.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 5172 | Tmp = LHS_i; |
| 5173 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 5174 | Res_r.add(Tmp, APFloat::rmNearestTiesToEven); |
| 5175 | Res_r.divide(Den, APFloat::rmNearestTiesToEven); |
| 5176 | |
| 5177 | Res_i = LHS_i; |
| 5178 | Res_i.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 5179 | Tmp = LHS_r; |
| 5180 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 5181 | Res_i.subtract(Tmp, APFloat::rmNearestTiesToEven); |
| 5182 | Res_i.divide(Den, APFloat::rmNearestTiesToEven); |
| 5183 | } else { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5184 | if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0) |
| 5185 | return Error(E, diag::note_expr_divide_by_zero); |
| 5186 | |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 5187 | ComplexValue LHS = Result; |
| 5188 | APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 5189 | RHS.getComplexIntImag() * RHS.getComplexIntImag(); |
| 5190 | Result.getComplexIntReal() = |
| 5191 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 5192 | LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den; |
| 5193 | Result.getComplexIntImag() = |
| 5194 | (LHS.getComplexIntImag() * RHS.getComplexIntReal() - |
| 5195 | LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den; |
| 5196 | } |
| 5197 | break; |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 5198 | } |
| 5199 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5200 | return true; |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 5201 | } |
| 5202 | |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 5203 | bool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
| 5204 | // Get the operand value into 'Result'. |
| 5205 | if (!Visit(E->getSubExpr())) |
| 5206 | return false; |
| 5207 | |
| 5208 | switch (E->getOpcode()) { |
| 5209 | default: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5210 | return Error(E); |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 5211 | case UO_Extension: |
| 5212 | return true; |
| 5213 | case UO_Plus: |
| 5214 | // The result is always just the subexpr. |
| 5215 | return true; |
| 5216 | case UO_Minus: |
| 5217 | if (Result.isComplexFloat()) { |
| 5218 | Result.getComplexFloatReal().changeSign(); |
| 5219 | Result.getComplexFloatImag().changeSign(); |
| 5220 | } |
| 5221 | else { |
| 5222 | Result.getComplexIntReal() = -Result.getComplexIntReal(); |
| 5223 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 5224 | } |
| 5225 | return true; |
| 5226 | case UO_Not: |
| 5227 | if (Result.isComplexFloat()) |
| 5228 | Result.getComplexFloatImag().changeSign(); |
| 5229 | else |
| 5230 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 5231 | return true; |
| 5232 | } |
| 5233 | } |
| 5234 | |
Eli Friedman | 7ead5c7 | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 5235 | bool ComplexExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 5236 | if (E->getNumInits() == 2) { |
| 5237 | if (E->getType()->isComplexType()) { |
| 5238 | Result.makeComplexFloat(); |
| 5239 | if (!EvaluateFloat(E->getInit(0), Result.FloatReal, Info)) |
| 5240 | return false; |
| 5241 | if (!EvaluateFloat(E->getInit(1), Result.FloatImag, Info)) |
| 5242 | return false; |
| 5243 | } else { |
| 5244 | Result.makeComplexInt(); |
| 5245 | if (!EvaluateInteger(E->getInit(0), Result.IntReal, Info)) |
| 5246 | return false; |
| 5247 | if (!EvaluateInteger(E->getInit(1), Result.IntImag, Info)) |
| 5248 | return false; |
| 5249 | } |
| 5250 | return true; |
| 5251 | } |
| 5252 | return ExprEvaluatorBaseTy::VisitInitListExpr(E); |
| 5253 | } |
| 5254 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 5255 | //===----------------------------------------------------------------------===// |
Richard Smith | aa9c350 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 5256 | // Void expression evaluation, primarily for a cast to void on the LHS of a |
| 5257 | // comma operator |
| 5258 | //===----------------------------------------------------------------------===// |
| 5259 | |
| 5260 | namespace { |
| 5261 | class VoidExprEvaluator |
| 5262 | : public ExprEvaluatorBase<VoidExprEvaluator, bool> { |
| 5263 | public: |
| 5264 | VoidExprEvaluator(EvalInfo &Info) : ExprEvaluatorBaseTy(Info) {} |
| 5265 | |
| 5266 | bool Success(const CCValue &V, const Expr *e) { return true; } |
Richard Smith | aa9c350 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 5267 | |
| 5268 | bool VisitCastExpr(const CastExpr *E) { |
| 5269 | switch (E->getCastKind()) { |
| 5270 | default: |
| 5271 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 5272 | case CK_ToVoid: |
| 5273 | VisitIgnoredValue(E->getSubExpr()); |
| 5274 | return true; |
| 5275 | } |
| 5276 | } |
| 5277 | }; |
| 5278 | } // end anonymous namespace |
| 5279 | |
| 5280 | static bool EvaluateVoid(const Expr *E, EvalInfo &Info) { |
| 5281 | assert(E->isRValue() && E->getType()->isVoidType()); |
| 5282 | return VoidExprEvaluator(Info).Visit(E); |
| 5283 | } |
| 5284 | |
| 5285 | //===----------------------------------------------------------------------===// |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 5286 | // Top level Expr::EvaluateAsRValue method. |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5287 | //===----------------------------------------------------------------------===// |
| 5288 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 5289 | static bool Evaluate(CCValue &Result, EvalInfo &Info, const Expr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5290 | // In C, function designators are not lvalues, but we evaluate them as if they |
| 5291 | // are. |
| 5292 | if (E->isGLValue() || E->getType()->isFunctionType()) { |
| 5293 | LValue LV; |
| 5294 | if (!EvaluateLValue(E, LV, Info)) |
| 5295 | return false; |
| 5296 | LV.moveInto(Result); |
| 5297 | } else if (E->getType()->isVectorType()) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 5298 | if (!EvaluateVector(E, Result, Info)) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5299 | return false; |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5300 | } else if (E->getType()->isIntegralOrEnumerationType()) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 5301 | if (!IntExprEvaluator(Info, Result).Visit(E)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 5302 | return false; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5303 | } else if (E->getType()->hasPointerRepresentation()) { |
| 5304 | LValue LV; |
| 5305 | if (!EvaluatePointer(E, LV, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 5306 | return false; |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 5307 | LV.moveInto(Result); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5308 | } else if (E->getType()->isRealFloatingType()) { |
| 5309 | llvm::APFloat F(0.0); |
| 5310 | if (!EvaluateFloat(E, F, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 5311 | return false; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 5312 | Result = CCValue(F); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5313 | } else if (E->getType()->isAnyComplexType()) { |
| 5314 | ComplexValue C; |
| 5315 | if (!EvaluateComplex(E, C, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 5316 | return false; |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 5317 | C.moveInto(Result); |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 5318 | } else if (E->getType()->isMemberPointerType()) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5319 | MemberPtr P; |
| 5320 | if (!EvaluateMemberPointer(E, P, Info)) |
| 5321 | return false; |
| 5322 | P.moveInto(Result); |
| 5323 | return true; |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5324 | } else if (E->getType()->isArrayType()) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5325 | LValue LV; |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5326 | LV.set(E, Info.CurrentCall); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5327 | if (!EvaluateArray(E, LV, Info.CurrentCall->Temporaries[E], Info)) |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5328 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5329 | Result = Info.CurrentCall->Temporaries[E]; |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5330 | } else if (E->getType()->isRecordType()) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5331 | LValue LV; |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5332 | LV.set(E, Info.CurrentCall); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5333 | if (!EvaluateRecord(E, LV, Info.CurrentCall->Temporaries[E], Info)) |
| 5334 | return false; |
| 5335 | Result = Info.CurrentCall->Temporaries[E]; |
Richard Smith | aa9c350 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 5336 | } else if (E->getType()->isVoidType()) { |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5337 | if (Info.getLangOpts().CPlusPlus0x) |
| 5338 | Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_nonliteral) |
| 5339 | << E->getType(); |
| 5340 | else |
| 5341 | Info.CCEDiag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | aa9c350 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 5342 | if (!EvaluateVoid(E, Info)) |
| 5343 | return false; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5344 | } else if (Info.getLangOpts().CPlusPlus0x) { |
| 5345 | Info.Diag(E->getExprLoc(), diag::note_constexpr_nonliteral) << E->getType(); |
| 5346 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5347 | } else { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 5348 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Anders Carlsson | 9d4c157 | 2008-11-22 22:56:32 +0000 | [diff] [blame] | 5349 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5350 | } |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 5351 | |
Anders Carlsson | 5b45d4e | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 5352 | return true; |
| 5353 | } |
| 5354 | |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 5355 | /// EvaluateConstantExpression - Evaluate an expression as a constant expression |
| 5356 | /// in-place in an APValue. In some cases, the in-place evaluation is essential, |
| 5357 | /// since later initializers for an object can indirectly refer to subobjects |
| 5358 | /// which were initialized earlier. |
| 5359 | static bool EvaluateConstantExpression(APValue &Result, EvalInfo &Info, |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5360 | const LValue &This, const Expr *E, |
| 5361 | CheckConstantExpressionKind CCEK) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5362 | if (!CheckLiteralType(Info, E)) |
| 5363 | return false; |
| 5364 | |
| 5365 | if (E->isRValue()) { |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 5366 | // Evaluate arrays and record types in-place, so that later initializers can |
| 5367 | // refer to earlier-initialized members of the object. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5368 | if (E->getType()->isArrayType()) |
| 5369 | return EvaluateArray(E, This, Result, Info); |
| 5370 | else if (E->getType()->isRecordType()) |
| 5371 | return EvaluateRecord(E, This, Result, Info); |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 5372 | } |
| 5373 | |
| 5374 | // For any other type, in-place evaluation is unimportant. |
| 5375 | CCValue CoreConstResult; |
| 5376 | return Evaluate(CoreConstResult, Info, E) && |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5377 | CheckConstantExpression(Info, E, CoreConstResult, Result, CCEK); |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 5378 | } |
| 5379 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5380 | /// EvaluateAsRValue - Try to evaluate this expression, performing an implicit |
| 5381 | /// lvalue-to-rvalue cast if it is an lvalue. |
| 5382 | static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5383 | if (!CheckLiteralType(Info, E)) |
| 5384 | return false; |
| 5385 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5386 | CCValue Value; |
| 5387 | if (!::Evaluate(Value, Info, E)) |
| 5388 | return false; |
| 5389 | |
| 5390 | if (E->isGLValue()) { |
| 5391 | LValue LV; |
| 5392 | LV.setFrom(Value); |
| 5393 | if (!HandleLValueToRValueConversion(Info, E, E->getType(), LV, Value)) |
| 5394 | return false; |
| 5395 | } |
| 5396 | |
| 5397 | // Check this core constant expression is a constant expression, and if so, |
| 5398 | // convert it to one. |
| 5399 | return CheckConstantExpression(Info, E, Value, Result); |
| 5400 | } |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5401 | |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 5402 | /// 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] | 5403 | /// any crazy technique (that has nothing to do with language standards) that |
| 5404 | /// 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] | 5405 | /// in Result. If this expression is a glvalue, an lvalue-to-rvalue conversion |
| 5406 | /// will be applied to the result. |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 5407 | bool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx) const { |
Richard Smith | ee19f43 | 2011-12-10 01:10:13 +0000 | [diff] [blame] | 5408 | // Fast-path evaluations of integer literals, since we sometimes see files |
| 5409 | // containing vast quantities of these. |
| 5410 | if (const IntegerLiteral *L = dyn_cast<IntegerLiteral>(this)) { |
| 5411 | Result.Val = APValue(APSInt(L->getValue(), |
| 5412 | L->getType()->isUnsignedIntegerType())); |
| 5413 | return true; |
| 5414 | } |
| 5415 | |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame^] | 5416 | // FIXME: Evaluating values of large array and record types can cause |
| 5417 | // performance problems. Only do so in C++11 for now. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5418 | if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) && |
| 5419 | !Ctx.getLangOptions().CPlusPlus0x) |
Richard Smith | 1445bba | 2011-11-10 03:30:42 +0000 | [diff] [blame] | 5420 | return false; |
| 5421 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5422 | EvalInfo Info(Ctx, Result); |
| 5423 | return ::EvaluateAsRValue(Info, this, Result.Val); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5424 | } |
| 5425 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5426 | bool Expr::EvaluateAsBooleanCondition(bool &Result, |
| 5427 | const ASTContext &Ctx) const { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5428 | EvalResult Scratch; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 5429 | return EvaluateAsRValue(Scratch, Ctx) && |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 5430 | HandleConversionToBool(CCValue(const_cast<ASTContext&>(Ctx), |
| 5431 | Scratch.Val, CCValue::GlobalValue()), |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 5432 | Result); |
John McCall | cd7a445 | 2010-01-05 23:42:56 +0000 | [diff] [blame] | 5433 | } |
| 5434 | |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 5435 | bool Expr::EvaluateAsInt(APSInt &Result, const ASTContext &Ctx, |
| 5436 | SideEffectsKind AllowSideEffects) const { |
| 5437 | if (!getType()->isIntegralOrEnumerationType()) |
| 5438 | return false; |
| 5439 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5440 | EvalResult ExprResult; |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 5441 | if (!EvaluateAsRValue(ExprResult, Ctx) || !ExprResult.Val.isInt() || |
| 5442 | (!AllowSideEffects && ExprResult.HasSideEffects)) |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5443 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5444 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5445 | Result = ExprResult.Val.getInt(); |
| 5446 | return true; |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5447 | } |
| 5448 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5449 | bool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx) const { |
Anders Carlsson | 1b78276 | 2009-04-10 04:54:13 +0000 | [diff] [blame] | 5450 | EvalInfo Info(Ctx, Result); |
| 5451 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5452 | LValue LV; |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 5453 | return EvaluateLValue(this, LV, Info) && !Result.HasSideEffects && |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5454 | CheckLValueConstantExpression(Info, this, LV, Result.Val, |
| 5455 | CCEK_Constant); |
Eli Friedman | b2f295c | 2009-09-13 10:17:44 +0000 | [diff] [blame] | 5456 | } |
| 5457 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 5458 | bool Expr::EvaluateAsInitializer(APValue &Value, const ASTContext &Ctx, |
| 5459 | const VarDecl *VD, |
| 5460 | llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const { |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame^] | 5461 | // FIXME: Evaluating initializers for large array and record types can cause |
| 5462 | // performance problems. Only do so in C++11 for now. |
| 5463 | if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) && |
| 5464 | !Ctx.getLangOptions().CPlusPlus0x) |
| 5465 | return false; |
| 5466 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 5467 | Expr::EvalStatus EStatus; |
| 5468 | EStatus.Diag = &Notes; |
| 5469 | |
| 5470 | EvalInfo InitInfo(Ctx, EStatus); |
| 5471 | InitInfo.setEvaluatingDecl(VD, Value); |
| 5472 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5473 | if (!CheckLiteralType(InitInfo, this)) |
| 5474 | return false; |
| 5475 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 5476 | LValue LVal; |
| 5477 | LVal.set(VD); |
| 5478 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5479 | // C++11 [basic.start.init]p2: |
| 5480 | // Variables with static storage duration or thread storage duration shall be |
| 5481 | // zero-initialized before any other initialization takes place. |
| 5482 | // This behavior is not present in C. |
| 5483 | if (Ctx.getLangOptions().CPlusPlus && !VD->hasLocalStorage() && |
| 5484 | !VD->getType()->isReferenceType()) { |
| 5485 | ImplicitValueInitExpr VIE(VD->getType()); |
| 5486 | if (!EvaluateConstantExpression(Value, InitInfo, LVal, &VIE)) |
| 5487 | return false; |
| 5488 | } |
| 5489 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 5490 | return EvaluateConstantExpression(Value, InitInfo, LVal, this) && |
| 5491 | !EStatus.HasSideEffects; |
| 5492 | } |
| 5493 | |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 5494 | /// isEvaluatable - Call EvaluateAsRValue to see if this expression can be |
| 5495 | /// constant folded, but discard the result. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5496 | bool Expr::isEvaluatable(const ASTContext &Ctx) const { |
Anders Carlsson | 4fdfb09 | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 5497 | EvalResult Result; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 5498 | return EvaluateAsRValue(Result, Ctx) && !Result.HasSideEffects; |
Chris Lattner | 45b6b9d | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 5499 | } |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 5500 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5501 | bool Expr::HasSideEffects(const ASTContext &Ctx) const { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 5502 | return HasSideEffect(Ctx).Visit(this); |
Fariborz Jahanian | 393c247 | 2009-11-05 18:03:03 +0000 | [diff] [blame] | 5503 | } |
| 5504 | |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5505 | APSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx) const { |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 5506 | EvalResult EvalResult; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 5507 | bool Result = EvaluateAsRValue(EvalResult, Ctx); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 5508 | (void)Result; |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 5509 | assert(Result && "Could not evaluate expression"); |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 5510 | assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer"); |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 5511 | |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 5512 | return EvalResult.Val.getInt(); |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 5513 | } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5514 | |
Abramo Bagnara | e17a643 | 2010-05-14 17:07:14 +0000 | [diff] [blame] | 5515 | bool Expr::EvalResult::isGlobalLValue() const { |
| 5516 | assert(Val.isLValue()); |
| 5517 | return IsGlobalLValue(Val.getLValueBase()); |
| 5518 | } |
| 5519 | |
| 5520 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5521 | /// isIntegerConstantExpr - this recursive routine will test if an expression is |
| 5522 | /// an integer constant expression. |
| 5523 | |
| 5524 | /// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, |
| 5525 | /// comma, etc |
| 5526 | /// |
| 5527 | /// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof |
| 5528 | /// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer |
| 5529 | /// cast+dereference. |
| 5530 | |
| 5531 | // CheckICE - This function does the fundamental ICE checking: the returned |
| 5532 | // ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation. |
| 5533 | // Note that to reduce code duplication, this helper does no evaluation |
| 5534 | // itself; the caller checks whether the expression is evaluatable, and |
| 5535 | // in the rare cases where CheckICE actually cares about the evaluated |
| 5536 | // value, it calls into Evalute. |
| 5537 | // |
| 5538 | // Meanings of Val: |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 5539 | // 0: This expression is an ICE. |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5540 | // 1: This expression is not an ICE, but if it isn't evaluated, it's |
| 5541 | // a legal subexpression for an ICE. This return value is used to handle |
| 5542 | // the comma operator in C99 mode. |
| 5543 | // 2: This expression is not an ICE, and is not a legal subexpression for one. |
| 5544 | |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 5545 | namespace { |
| 5546 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5547 | struct ICEDiag { |
| 5548 | unsigned Val; |
| 5549 | SourceLocation Loc; |
| 5550 | |
| 5551 | public: |
| 5552 | ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {} |
| 5553 | ICEDiag() : Val(0) {} |
| 5554 | }; |
| 5555 | |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 5556 | } |
| 5557 | |
| 5558 | static ICEDiag NoDiag() { return ICEDiag(); } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5559 | |
| 5560 | static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) { |
| 5561 | Expr::EvalResult EVResult; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 5562 | if (!E->EvaluateAsRValue(EVResult, Ctx) || EVResult.HasSideEffects || |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5563 | !EVResult.Val.isInt()) { |
| 5564 | return ICEDiag(2, E->getLocStart()); |
| 5565 | } |
| 5566 | return NoDiag(); |
| 5567 | } |
| 5568 | |
| 5569 | static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) { |
| 5570 | assert(!E->isValueDependent() && "Should not see value dependent exprs!"); |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 5571 | if (!E->getType()->isIntegralOrEnumerationType()) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5572 | return ICEDiag(2, E->getLocStart()); |
| 5573 | } |
| 5574 | |
| 5575 | switch (E->getStmtClass()) { |
John McCall | 63c00d7 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 5576 | #define ABSTRACT_STMT(Node) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5577 | #define STMT(Node, Base) case Expr::Node##Class: |
| 5578 | #define EXPR(Node, Base) |
| 5579 | #include "clang/AST/StmtNodes.inc" |
| 5580 | case Expr::PredefinedExprClass: |
| 5581 | case Expr::FloatingLiteralClass: |
| 5582 | case Expr::ImaginaryLiteralClass: |
| 5583 | case Expr::StringLiteralClass: |
| 5584 | case Expr::ArraySubscriptExprClass: |
| 5585 | case Expr::MemberExprClass: |
| 5586 | case Expr::CompoundAssignOperatorClass: |
| 5587 | case Expr::CompoundLiteralExprClass: |
| 5588 | case Expr::ExtVectorElementExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5589 | case Expr::DesignatedInitExprClass: |
| 5590 | case Expr::ImplicitValueInitExprClass: |
| 5591 | case Expr::ParenListExprClass: |
| 5592 | case Expr::VAArgExprClass: |
| 5593 | case Expr::AddrLabelExprClass: |
| 5594 | case Expr::StmtExprClass: |
| 5595 | case Expr::CXXMemberCallExprClass: |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 5596 | case Expr::CUDAKernelCallExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5597 | case Expr::CXXDynamicCastExprClass: |
| 5598 | case Expr::CXXTypeidExprClass: |
Francois Pichet | 9be8840 | 2010-09-08 23:47:05 +0000 | [diff] [blame] | 5599 | case Expr::CXXUuidofExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5600 | case Expr::CXXNullPtrLiteralExprClass: |
| 5601 | case Expr::CXXThisExprClass: |
| 5602 | case Expr::CXXThrowExprClass: |
| 5603 | case Expr::CXXNewExprClass: |
| 5604 | case Expr::CXXDeleteExprClass: |
| 5605 | case Expr::CXXPseudoDestructorExprClass: |
| 5606 | case Expr::UnresolvedLookupExprClass: |
| 5607 | case Expr::DependentScopeDeclRefExprClass: |
| 5608 | case Expr::CXXConstructExprClass: |
| 5609 | case Expr::CXXBindTemporaryExprClass: |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 5610 | case Expr::ExprWithCleanupsClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5611 | case Expr::CXXTemporaryObjectExprClass: |
| 5612 | case Expr::CXXUnresolvedConstructExprClass: |
| 5613 | case Expr::CXXDependentScopeMemberExprClass: |
| 5614 | case Expr::UnresolvedMemberExprClass: |
| 5615 | case Expr::ObjCStringLiteralClass: |
| 5616 | case Expr::ObjCEncodeExprClass: |
| 5617 | case Expr::ObjCMessageExprClass: |
| 5618 | case Expr::ObjCSelectorExprClass: |
| 5619 | case Expr::ObjCProtocolExprClass: |
| 5620 | case Expr::ObjCIvarRefExprClass: |
| 5621 | case Expr::ObjCPropertyRefExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5622 | case Expr::ObjCIsaExprClass: |
| 5623 | case Expr::ShuffleVectorExprClass: |
| 5624 | case Expr::BlockExprClass: |
| 5625 | case Expr::BlockDeclRefExprClass: |
| 5626 | case Expr::NoStmtClass: |
John McCall | 7cd7d1a | 2010-11-15 23:31:06 +0000 | [diff] [blame] | 5627 | case Expr::OpaqueValueExprClass: |
Douglas Gregor | be230c3 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 5628 | case Expr::PackExpansionExprClass: |
Douglas Gregor | c7793c7 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 5629 | case Expr::SubstNonTypeTemplateParmPackExprClass: |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 5630 | case Expr::AsTypeExprClass: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5631 | case Expr::ObjCIndirectCopyRestoreExprClass: |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 5632 | case Expr::MaterializeTemporaryExprClass: |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 5633 | case Expr::PseudoObjectExprClass: |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 5634 | case Expr::AtomicExprClass: |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 5635 | case Expr::InitListExprClass: |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 5636 | return ICEDiag(2, E->getLocStart()); |
| 5637 | |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 5638 | case Expr::SizeOfPackExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5639 | case Expr::GNUNullExprClass: |
| 5640 | // GCC considers the GNU __null value to be an integral constant expression. |
| 5641 | return NoDiag(); |
| 5642 | |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 5643 | case Expr::SubstNonTypeTemplateParmExprClass: |
| 5644 | return |
| 5645 | CheckICE(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(), Ctx); |
| 5646 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5647 | case Expr::ParenExprClass: |
| 5648 | return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx); |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 5649 | case Expr::GenericSelectionExprClass: |
| 5650 | return CheckICE(cast<GenericSelectionExpr>(E)->getResultExpr(), Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5651 | case Expr::IntegerLiteralClass: |
| 5652 | case Expr::CharacterLiteralClass: |
| 5653 | case Expr::CXXBoolLiteralExprClass: |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 5654 | case Expr::CXXScalarValueInitExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5655 | case Expr::UnaryTypeTraitExprClass: |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 5656 | case Expr::BinaryTypeTraitExprClass: |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 5657 | case Expr::ArrayTypeTraitExprClass: |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 5658 | case Expr::ExpressionTraitExprClass: |
Sebastian Redl | 2e15622 | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 5659 | case Expr::CXXNoexceptExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5660 | return NoDiag(); |
| 5661 | case Expr::CallExprClass: |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 5662 | case Expr::CXXOperatorCallExprClass: { |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 5663 | // C99 6.6/3 allows function calls within unevaluated subexpressions of |
| 5664 | // constant expressions, but they can never be ICEs because an ICE cannot |
| 5665 | // contain an operand of (pointer to) function type. |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5666 | const CallExpr *CE = cast<CallExpr>(E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5667 | if (CE->isBuiltinCall()) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5668 | return CheckEvalInICE(E, Ctx); |
| 5669 | return ICEDiag(2, E->getLocStart()); |
| 5670 | } |
| 5671 | case Expr::DeclRefExprClass: |
| 5672 | if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl())) |
| 5673 | return NoDiag(); |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 5674 | if (Ctx.getLangOptions().CPlusPlus && IsConstNonVolatile(E->getType())) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5675 | const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl(); |
| 5676 | |
| 5677 | // Parameter variables are never constants. Without this check, |
| 5678 | // getAnyInitializer() can find a default argument, which leads |
| 5679 | // to chaos. |
| 5680 | if (isa<ParmVarDecl>(D)) |
| 5681 | return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); |
| 5682 | |
| 5683 | // C++ 7.1.5.1p2 |
| 5684 | // A variable of non-volatile const-qualified integral or enumeration |
| 5685 | // type initialized by an ICE can be used in ICEs. |
| 5686 | if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) { |
Richard Smith | db1822c | 2011-11-08 01:31:09 +0000 | [diff] [blame] | 5687 | if (!Dcl->getType()->isIntegralOrEnumerationType()) |
| 5688 | return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); |
| 5689 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 5690 | const VarDecl *VD; |
| 5691 | // Look for a declaration of this variable that has an initializer, and |
| 5692 | // check whether it is an ICE. |
| 5693 | if (Dcl->getAnyInitializer(VD) && VD->checkInitIsICE()) |
| 5694 | return NoDiag(); |
| 5695 | else |
| 5696 | return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5697 | } |
| 5698 | } |
| 5699 | return ICEDiag(2, E->getLocStart()); |
| 5700 | case Expr::UnaryOperatorClass: { |
| 5701 | const UnaryOperator *Exp = cast<UnaryOperator>(E); |
| 5702 | switch (Exp->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5703 | case UO_PostInc: |
| 5704 | case UO_PostDec: |
| 5705 | case UO_PreInc: |
| 5706 | case UO_PreDec: |
| 5707 | case UO_AddrOf: |
| 5708 | case UO_Deref: |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 5709 | // C99 6.6/3 allows increment and decrement within unevaluated |
| 5710 | // subexpressions of constant expressions, but they can never be ICEs |
| 5711 | // because an ICE cannot contain an lvalue operand. |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5712 | return ICEDiag(2, E->getLocStart()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5713 | case UO_Extension: |
| 5714 | case UO_LNot: |
| 5715 | case UO_Plus: |
| 5716 | case UO_Minus: |
| 5717 | case UO_Not: |
| 5718 | case UO_Real: |
| 5719 | case UO_Imag: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5720 | return CheckICE(Exp->getSubExpr(), Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5721 | } |
| 5722 | |
| 5723 | // OffsetOf falls through here. |
| 5724 | } |
| 5725 | case Expr::OffsetOfExprClass: { |
| 5726 | // Note that per C99, offsetof must be an ICE. And AFAIK, using |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 5727 | // EvaluateAsRValue matches the proposed gcc behavior for cases like |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 5728 | // "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] | 5729 | // compliance: we should warn earlier for offsetof expressions with |
| 5730 | // array subscripts that aren't ICEs, and if the array subscripts |
| 5731 | // are ICEs, the value of the offsetof must be an integer constant. |
| 5732 | return CheckEvalInICE(E, Ctx); |
| 5733 | } |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5734 | case Expr::UnaryExprOrTypeTraitExprClass: { |
| 5735 | const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E); |
| 5736 | if ((Exp->getKind() == UETT_SizeOf) && |
| 5737 | Exp->getTypeOfArgument()->isVariableArrayType()) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5738 | return ICEDiag(2, E->getLocStart()); |
| 5739 | return NoDiag(); |
| 5740 | } |
| 5741 | case Expr::BinaryOperatorClass: { |
| 5742 | const BinaryOperator *Exp = cast<BinaryOperator>(E); |
| 5743 | switch (Exp->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5744 | case BO_PtrMemD: |
| 5745 | case BO_PtrMemI: |
| 5746 | case BO_Assign: |
| 5747 | case BO_MulAssign: |
| 5748 | case BO_DivAssign: |
| 5749 | case BO_RemAssign: |
| 5750 | case BO_AddAssign: |
| 5751 | case BO_SubAssign: |
| 5752 | case BO_ShlAssign: |
| 5753 | case BO_ShrAssign: |
| 5754 | case BO_AndAssign: |
| 5755 | case BO_XorAssign: |
| 5756 | case BO_OrAssign: |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 5757 | // C99 6.6/3 allows assignments within unevaluated subexpressions of |
| 5758 | // constant expressions, but they can never be ICEs because an ICE cannot |
| 5759 | // contain an lvalue operand. |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5760 | return ICEDiag(2, E->getLocStart()); |
| 5761 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5762 | case BO_Mul: |
| 5763 | case BO_Div: |
| 5764 | case BO_Rem: |
| 5765 | case BO_Add: |
| 5766 | case BO_Sub: |
| 5767 | case BO_Shl: |
| 5768 | case BO_Shr: |
| 5769 | case BO_LT: |
| 5770 | case BO_GT: |
| 5771 | case BO_LE: |
| 5772 | case BO_GE: |
| 5773 | case BO_EQ: |
| 5774 | case BO_NE: |
| 5775 | case BO_And: |
| 5776 | case BO_Xor: |
| 5777 | case BO_Or: |
| 5778 | case BO_Comma: { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5779 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 5780 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5781 | if (Exp->getOpcode() == BO_Div || |
| 5782 | Exp->getOpcode() == BO_Rem) { |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 5783 | // EvaluateAsRValue gives an error for undefined Div/Rem, so make sure |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5784 | // we don't evaluate one. |
John McCall | 3b332ab | 2011-02-26 08:27:17 +0000 | [diff] [blame] | 5785 | if (LHSResult.Val == 0 && RHSResult.Val == 0) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5786 | llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5787 | if (REval == 0) |
| 5788 | return ICEDiag(1, E->getLocStart()); |
| 5789 | if (REval.isSigned() && REval.isAllOnesValue()) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5790 | llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5791 | if (LEval.isMinSignedValue()) |
| 5792 | return ICEDiag(1, E->getLocStart()); |
| 5793 | } |
| 5794 | } |
| 5795 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5796 | if (Exp->getOpcode() == BO_Comma) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5797 | if (Ctx.getLangOptions().C99) { |
| 5798 | // C99 6.6p3 introduces a strange edge case: comma can be in an ICE |
| 5799 | // if it isn't evaluated. |
| 5800 | if (LHSResult.Val == 0 && RHSResult.Val == 0) |
| 5801 | return ICEDiag(1, E->getLocStart()); |
| 5802 | } else { |
| 5803 | // In both C89 and C++, commas in ICEs are illegal. |
| 5804 | return ICEDiag(2, E->getLocStart()); |
| 5805 | } |
| 5806 | } |
| 5807 | if (LHSResult.Val >= RHSResult.Val) |
| 5808 | return LHSResult; |
| 5809 | return RHSResult; |
| 5810 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5811 | case BO_LAnd: |
| 5812 | case BO_LOr: { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5813 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 5814 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
| 5815 | if (LHSResult.Val == 0 && RHSResult.Val == 1) { |
| 5816 | // Rare case where the RHS has a comma "side-effect"; we need |
| 5817 | // to actually check the condition to see whether the side |
| 5818 | // with the comma is evaluated. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5819 | if ((Exp->getOpcode() == BO_LAnd) != |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5820 | (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0)) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5821 | return RHSResult; |
| 5822 | return NoDiag(); |
| 5823 | } |
| 5824 | |
| 5825 | if (LHSResult.Val >= RHSResult.Val) |
| 5826 | return LHSResult; |
| 5827 | return RHSResult; |
| 5828 | } |
| 5829 | } |
| 5830 | } |
| 5831 | case Expr::ImplicitCastExprClass: |
| 5832 | case Expr::CStyleCastExprClass: |
| 5833 | case Expr::CXXFunctionalCastExprClass: |
| 5834 | case Expr::CXXStaticCastExprClass: |
| 5835 | case Expr::CXXReinterpretCastExprClass: |
Richard Smith | 32cb471 | 2011-10-24 18:26:35 +0000 | [diff] [blame] | 5836 | case Expr::CXXConstCastExprClass: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5837 | case Expr::ObjCBridgedCastExprClass: { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5838 | const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr(); |
Richard Smith | 2116b14 | 2011-12-18 02:33:09 +0000 | [diff] [blame] | 5839 | if (isa<ExplicitCastExpr>(E)) { |
| 5840 | if (const FloatingLiteral *FL |
| 5841 | = dyn_cast<FloatingLiteral>(SubExpr->IgnoreParenImpCasts())) { |
| 5842 | unsigned DestWidth = Ctx.getIntWidth(E->getType()); |
| 5843 | bool DestSigned = E->getType()->isSignedIntegerOrEnumerationType(); |
| 5844 | APSInt IgnoredVal(DestWidth, !DestSigned); |
| 5845 | bool Ignored; |
| 5846 | // If the value does not fit in the destination type, the behavior is |
| 5847 | // undefined, so we are not required to treat it as a constant |
| 5848 | // expression. |
| 5849 | if (FL->getValue().convertToInteger(IgnoredVal, |
| 5850 | llvm::APFloat::rmTowardZero, |
| 5851 | &Ignored) & APFloat::opInvalidOp) |
| 5852 | return ICEDiag(2, E->getLocStart()); |
| 5853 | return NoDiag(); |
| 5854 | } |
| 5855 | } |
Eli Friedman | eea0e81 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 5856 | switch (cast<CastExpr>(E)->getCastKind()) { |
| 5857 | case CK_LValueToRValue: |
| 5858 | case CK_NoOp: |
| 5859 | case CK_IntegralToBoolean: |
| 5860 | case CK_IntegralCast: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5861 | return CheckICE(SubExpr, Ctx); |
Eli Friedman | eea0e81 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 5862 | default: |
Eli Friedman | eea0e81 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 5863 | return ICEDiag(2, E->getLocStart()); |
| 5864 | } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5865 | } |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5866 | case Expr::BinaryConditionalOperatorClass: { |
| 5867 | const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E); |
| 5868 | ICEDiag CommonResult = CheckICE(Exp->getCommon(), Ctx); |
| 5869 | if (CommonResult.Val == 2) return CommonResult; |
| 5870 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
| 5871 | if (FalseResult.Val == 2) return FalseResult; |
| 5872 | if (CommonResult.Val == 1) return CommonResult; |
| 5873 | if (FalseResult.Val == 1 && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5874 | Exp->getCommon()->EvaluateKnownConstInt(Ctx) == 0) return NoDiag(); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5875 | return FalseResult; |
| 5876 | } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5877 | case Expr::ConditionalOperatorClass: { |
| 5878 | const ConditionalOperator *Exp = cast<ConditionalOperator>(E); |
| 5879 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
| 5880 | // then only the true side is actually considered in an integer constant |
| 5881 | // expression, and it is fully evaluated. This is an important GNU |
| 5882 | // extension. See GCC PR38377 for discussion. |
| 5883 | if (const CallExpr *CallCE |
| 5884 | = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts())) |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 5885 | if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p) |
| 5886 | return CheckEvalInICE(E, Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5887 | ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5888 | if (CondResult.Val == 2) |
| 5889 | return CondResult; |
Douglas Gregor | 63fe681 | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 5890 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5891 | ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx); |
| 5892 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
Douglas Gregor | 63fe681 | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 5893 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5894 | if (TrueResult.Val == 2) |
| 5895 | return TrueResult; |
| 5896 | if (FalseResult.Val == 2) |
| 5897 | return FalseResult; |
| 5898 | if (CondResult.Val == 1) |
| 5899 | return CondResult; |
| 5900 | if (TrueResult.Val == 0 && FalseResult.Val == 0) |
| 5901 | return NoDiag(); |
| 5902 | // Rare case where the diagnostics depend on which side is evaluated |
| 5903 | // Note that if we get here, CondResult is 0, and at least one of |
| 5904 | // TrueResult and FalseResult is non-zero. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5905 | if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5906 | return FalseResult; |
| 5907 | } |
| 5908 | return TrueResult; |
| 5909 | } |
| 5910 | case Expr::CXXDefaultArgExprClass: |
| 5911 | return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx); |
| 5912 | case Expr::ChooseExprClass: { |
| 5913 | return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx); |
| 5914 | } |
| 5915 | } |
| 5916 | |
| 5917 | // Silence a GCC warning |
| 5918 | return ICEDiag(2, E->getLocStart()); |
| 5919 | } |
| 5920 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5921 | /// Evaluate an expression as a C++11 integral constant expression. |
| 5922 | static bool EvaluateCPlusPlus11IntegralConstantExpr(ASTContext &Ctx, |
| 5923 | const Expr *E, |
| 5924 | llvm::APSInt *Value, |
| 5925 | SourceLocation *Loc) { |
| 5926 | if (!E->getType()->isIntegralOrEnumerationType()) { |
| 5927 | if (Loc) *Loc = E->getExprLoc(); |
| 5928 | return false; |
| 5929 | } |
| 5930 | |
| 5931 | Expr::EvalResult Result; |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 5932 | llvm::SmallVector<PartialDiagnosticAt, 8> Diags; |
| 5933 | Result.Diag = &Diags; |
| 5934 | EvalInfo Info(Ctx, Result); |
| 5935 | |
| 5936 | bool IsICE = EvaluateAsRValue(Info, E, Result.Val); |
| 5937 | if (!Diags.empty()) { |
| 5938 | IsICE = false; |
| 5939 | if (Loc) *Loc = Diags[0].first; |
| 5940 | } else if (!IsICE && Loc) { |
| 5941 | *Loc = E->getExprLoc(); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5942 | } |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 5943 | |
| 5944 | if (!IsICE) |
| 5945 | return false; |
| 5946 | |
| 5947 | assert(Result.Val.isInt() && "pointer cast to int is not an ICE"); |
| 5948 | if (Value) *Value = Result.Val.getInt(); |
| 5949 | return true; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5950 | } |
| 5951 | |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 5952 | bool Expr::isIntegerConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5953 | if (Ctx.getLangOptions().CPlusPlus0x) |
| 5954 | return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, 0, Loc); |
| 5955 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5956 | ICEDiag d = CheckICE(this, Ctx); |
| 5957 | if (d.Val != 0) { |
| 5958 | if (Loc) *Loc = d.Loc; |
| 5959 | return false; |
| 5960 | } |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5961 | return true; |
| 5962 | } |
| 5963 | |
| 5964 | bool Expr::isIntegerConstantExpr(llvm::APSInt &Value, ASTContext &Ctx, |
| 5965 | SourceLocation *Loc, bool isEvaluated) const { |
| 5966 | if (Ctx.getLangOptions().CPlusPlus0x) |
| 5967 | return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, &Value, Loc); |
| 5968 | |
| 5969 | if (!isIntegerConstantExpr(Ctx, Loc)) |
| 5970 | return false; |
| 5971 | if (!EvaluateAsInt(Value, Ctx)) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5972 | llvm_unreachable("ICE cannot be evaluated!"); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5973 | return true; |
| 5974 | } |